Skip to contentSkip to conversation

rtcan: Flexcan driver for Freescale SOCs

PATCH New Wolfgang Grandegger 1 participant 4 messages Sep 27, 2012

lore
Share & tools
lore
#0 lore raw

cover rtcan: Flexcan driver for Freescale SOCs

From: Wolfgang Grandegger <[email protected]>

This patch series adds support for the Flexcan CAN controller available on various Freescale PowerPC and ARM SOCs. It is based on the Linux-CAN driver "flexcan.c" from Linux v3.5. I have tested it on a i.MX28 board under Linux 3.4.6. To get it working on the i.MX6, I'm afraid, there are a few more mainline patches necessary, which entered the kernel for v3.6. It should not be a big deal to port them over.

It would be nice if anybody else could test the driver.

Thanks,

Wolfgang.

Wolfgang Grandegger (3):
rtcan: add some macro definitions for compatibility with Linux-CAN
rtcan: add bit-timing calulation from Linux-CAN
rtcan: add driver for Flexcan on Freescale SOCs

 include/rtdm/rtcan.h                     |   12 +-
 ksrc/drivers/can/Kconfig                 |   16 +
 ksrc/drivers/can/Makefile                |    2 +
 ksrc/drivers/can/mscan/rtcan_mscan.c     |   17 +
 ksrc/drivers/can/rtcan_dev.h             |   17 +
 ksrc/drivers/can/rtcan_flexcan.c         | 1135 ++++++++++++++++++++++++++++++
 ksrc/drivers/can/rtcan_internal.h        |   10 +
 ksrc/drivers/can/rtcan_raw_dev.c         |  127 ++++-
 ksrc/drivers/can/sja1000/rtcan_sja1000.c |   18 +
 9 files changed, 1351 insertions(+), 3 deletions(-)
 create mode 100644 ksrc/drivers/can/rtcan_flexcan.c
Signature · 1 line
1.7.7.6
#1 lore raw

PATCH 1/3 rtcan: add some macro definitions for compatibility with Linux-CAN

From: Wolfgang Grandegger <[email protected]>

---
 include/rtdm/rtcan.h              |   12 ++++++++++--
 ksrc/drivers/can/rtcan_internal.h |   10 ++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)
include/rtdm/rtcan.h +10 -2
index 4c0749d..b8bd762 100644
@@ -422,6 +422,11 @@ typedef enum CAN_MODE can_mode_t;
  * test operation. */
 #define CAN_CTRLMODE_LOOPBACK   0x2
 
+/*! Triple sampling mode
+ *
+ * In this mode the CAN controller uses Triple sampling. */
+#define CAN_CTRLMODE_3_SAMPLES  0x4
+
 /** @} */
 
 /** See @ref CAN_CTRLMODE */
@@ -433,13 +438,16 @@ typedef int can_ctrlmode_t;
  * @{ */
 enum CAN_STATE {
 	/** CAN controller is error active */
+	CAN_STATE_ERROR_ACTIVE = 0,
 	CAN_STATE_ACTIVE = 0,
 
 	/** CAN controller is error active, warning level is reached */
-	CAN_STATE_BUS_WARNING,
+	CAN_STATE_ERROR_WARNING = 1,
+	CAN_STATE_BUS_WARNING = 1,
 
 	/** CAN controller is error passive */
-	CAN_STATE_BUS_PASSIVE,
+	CAN_STATE_ERROR_PASSIVE = 2,
+	CAN_STATE_BUS_PASSIVE = 2,
 
 	/** CAN controller went into Bus Off */
 	CAN_STATE_BUS_OFF,
ksrc/drivers/can/rtcan_internal.h +10 -0
index 626cae7..35b778a 100644
@@ -115,4 +115,14 @@
 # define RTCAN_RTDM_DBG(fmt,args...) do {} while (0)
 #endif
 
+#define rtcan_priv(dev)			(dev)->priv
+#define rtcandev_dbg(dev, fmt, args...)				\
+	printk(KERN_DEBUG "%s: " fmt, (dev)->name, ##args)
+#define rtcandev_info(dev, fmt, args...)			\
+	printk(KERN_INFO "%s: " fmt, (dev)->name, ##args)
+#define rtcandev_warn(dev, fmt, args...)			\
+	printk(KERN_WARNING "%s: " fmt, (dev)->name, ##args)
+#define rtcandev_err(dev, fmt, args...)				\
+	printk(KERN_ERR "%s: " fmt, (dev)->name, ##args)
+
 #endif /* __RTCAN_INTERNAL_H_ */
Signature · 1 line
1.7.7.6
#2 lore raw

PATCH 2/3 rtcan: add bit-timing calulation from Linux-CAN

In reply to Wolfgang Grandegger

From: Wolfgang Grandegger <[email protected]>

So far we use a very simple method to calculate bit-timing parameters from the bitrate. This works fine for the SJA1000 and MSCAN but fails for the Flexcan. This patch introduces the more general bit-timing algorithm from Linux-CAN, which is based on bit-timing constants. The Kconfig option XENO_DRIVERS_CAN_CALC_BITTIME_OLD allows to set the old algorithm for backward compatibility.

---
 ksrc/drivers/can/Kconfig                 |    6 ++
 ksrc/drivers/can/mscan/rtcan_mscan.c     |   17 ++++
 ksrc/drivers/can/rtcan_dev.h             |   17 ++++
 ksrc/drivers/can/rtcan_raw_dev.c         |  127 +++++++++++++++++++++++++++++-
 ksrc/drivers/can/sja1000/rtcan_sja1000.c |   18 ++++
 5 files changed, 184 insertions(+), 1 deletions(-)
ksrc/drivers/can/Kconfig +6 -0
index e659a3c..a3917da 100644
@@ -62,6 +62,12 @@ config XENO_DRIVERS_CAN_BUS_ERR
 	processing. This option is automatically selected for CAN controllers
 	supporting bus error interrupts like the SJA1000.
 
+config XENO_DRIVERS_CAN_CALC_BITTIME_OLD
+	depends on XENO_DRIVERS_CAN
+	bool "Old bit-time calculation algorithm"
+	default n
+	help
+
 config XENO_DRIVERS_CAN_VIRT
 	depends on XENO_DRIVERS_CAN
 	tristate "Virtual CAN bus driver"
ksrc/drivers/can/mscan/rtcan_mscan.c +17 -0
index edd590e..2b53e79 100644
@@ -42,6 +42,20 @@
 
 #define MSCAN_SET_MODE_RETRIES	255
 
+#ifndef CONFIG_XENO_DRIVERS_CAN_CALC_BITTIME_OLD
+static struct can_bittiming_const mscan_bittiming_const = {
+	.name = "mscan",
+	.tseg1_min = 4,
+	.tseg1_max = 16,
+	.tseg2_min = 2,
+	.tseg2_max = 8,
+	.sjw_max = 4,
+	.brp_min = 1,
+	.brp_max = 64,
+	.brp_inc = 1,
+};
+#endif
+
 /**
  *  Reception Interrupt handler
  *
@@ -722,6 +736,9 @@ int rtcan_mscan_register(struct rtcan_device *dev, int irq, int mscan_clksrc)
 	dev->hard_start_xmit = rtcan_mscan_start_xmit;
 	dev->do_set_mode = rtcan_mscan_set_mode;
 	dev->do_set_bit_time = rtcan_mscan_set_bit_time;
+#ifndef CONFIG_XENO_DRIVERS_CAN_CALC_BITTIME_OLD
+	dev->bittiming_const = &mscan_bittiming_const;
+#endif
 
 	/* Register IRQ handler and pass device structure as arg */
 	ret = rtdm_irq_request(&dev->irq_handle, irq, rtcan_mscan_interrupt,
ksrc/drivers/can/rtcan_dev.h +17 -0
index ab0431c..65fed69 100644
@@ -51,6 +51,22 @@
 #define RTCAN_USE_REFCOUNT
 #endif
 
+/*
+ * CAN harware-dependent bit-timing constant
+ *
+ * Used for calculating and checking bit-timing parameters
+ */
+struct can_bittiming_const {
+	char name[16];		/* Name of the CAN controller hardware */
+	__u32 tseg1_min;	/* Time segement 1 = prop_seg + phase_seg1 */
+	__u32 tseg1_max;
+	__u32 tseg2_min;	/* Time segement 2 = phase_seg2 */
+	__u32 tseg2_max;
+	__u32 sjw_max;		/* Synchronisation jump width */
+	__u32 brp_min;		/* Bit-rate prescaler */
+	__u32 brp_max;
+	__u32 brp_inc;
+};
 
 struct rtcan_device {
     unsigned int        version;
@@ -94,6 +110,7 @@ struct rtcan_device {
     can_baudrate_t      baudrate;
 
     struct can_bittime  bit_time;
+    const struct can_bittiming_const *bittiming_const;
 
     /* State which the controller is in. Protected by device_lock in all
      * device structures. */
ksrc/drivers/can/rtcan_raw_dev.c +126 -1

145 lines not on this page · raw message

ksrc/drivers/can/sja1000/rtcan_sja1000.c +18 -0
index 9fd392d..cf91439 100644
@@ -83,6 +83,19 @@ MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("RT-Socket-CAN driver for SJA1000");
 MODULE_SUPPORTED_DEVICE("SJA1000 CAN controller");
 
+#ifndef CONFIG_XENO_DRIVERS_CAN_CALC_BITTIME_OLD
+static struct can_bittiming_const sja1000_bittiming_const = {
+	.name = "sja1000",
+	.tseg1_min = 1,
+	.tseg1_max = 16,
+	.tseg2_min = 1,
+	.tseg2_max = 8,
+	.sjw_max = 4,
+	.brp_min = 1,
+	.brp_max = 64,
+	.brp_inc = 1,
+};
+#endif
 
 static inline void rtcan_sja_rx_interrupt(struct rtcan_device *dev,
 					  struct rtcan_skb *skb)
@@ -622,6 +635,7 @@ int rtcan_sja_set_bit_time(struct rtcan_device *dev,
 	return -EINVAL;
     }
 
+    printk("%s: btr0=%#x btr1=%#x\n", __func__, btr0, btr1);
     chip->write_reg(dev, SJA_BTR0, btr0);
     chip->write_reg(dev, SJA_BTR1, btr1);
 
@@ -757,6 +771,10 @@ int rtcan_sja1000_register(struct rtcan_device *dev)
     dev->do_get_state = rtcan_sja_get_state;
     dev->do_set_bit_time = rtcan_sja_set_bit_time;
     dev->do_enable_bus_err = rtcan_sja_enable_bus_err;
+#ifndef CONFIG_XENO_DRIVERS_CAN_CALC_BITTIME_OLD
+    dev->bittiming_const = &sja1000_bittiming_const;
+#endif
+
     chip->bus_err_on = 1;
 
     ret = rtdm_irq_request(&dev->irq_handle,
Signature · 1 line
1.7.7.6
#3 lore raw

PATCH 3/3 rtcan: add driver for Flexcan on Freescale SOCs

In reply to Wolfgang Grandegger

From: Wolfgang Grandegger <[email protected]>

This driver is derived from "drivers/net/can/flexcan.c" of Linux v3.5. It has been tested with Xenomai under Linux v3.4.6 on a MX28 board.

---
 ksrc/drivers/can/Kconfig         |   12 +-
 ksrc/drivers/can/Makefile        |    2 +
 ksrc/drivers/can/rtcan_flexcan.c | 1135 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 1148 insertions(+), 1 deletions(-)
 create mode 100644 ksrc/drivers/can/rtcan_flexcan.c
ksrc/drivers/can/Kconfig +11 -1
index a3917da..216b807 100644
@@ -64,10 +64,13 @@ config XENO_DRIVERS_CAN_BUS_ERR
 
 config XENO_DRIVERS_CAN_CALC_BITTIME_OLD
 	depends on XENO_DRIVERS_CAN
-	bool "Old bit-time calculation algorithm"
+	bool "Old bit-time calculation algorithm (deprecated)"
 	default n
 	help
 
+	This option allows to enable the old algorithm to calculate the
+	CAN bit-timing parameters for backward compatibility.
+
 config XENO_DRIVERS_CAN_VIRT
 	depends on XENO_DRIVERS_CAN
 	tristate "Virtual CAN bus driver"
@@ -76,6 +79,13 @@ config XENO_DRIVERS_CAN_VIRT
 	This driver provides two CAN ports that are virtually interconnected.
 	More ports can be enabled with the module parameter "devices".
 
+config XENO_DRIVERS_CAN_FLEXCAN
+	depends on XENO_DRIVERS_CAN && !XENO_DRIVERS_CAN_CALC_BITTIME_OLD
+	tristate "Freescale FLEXCAN based chips"
+	help
+
+	Say Y here if you want to support for Freescale FlexCAN.
+
 source drivers/xenomai/can/mscan/Kconfig
 source drivers/xenomai/can/sja1000/Kconfig
 
ksrc/drivers/can/Makefile +2 -0
index 1b88967..41483b1 100644
@@ -5,10 +5,12 @@ ifneq ($(VERSION).$(PATCHLEVEL),2.4)
 EXTRA_CFLAGS += -D__IN_XENOMAI__ -Iinclude/xenomai -Idrivers/xenomai/can
 
 obj-$(CONFIG_XENO_DRIVERS_CAN) += xeno_can.o mscan/ sja1000/
+obj-$(CONFIG_XENO_DRIVERS_CAN_FLEXCAN) += xeno_can_flexcan.o
 obj-$(CONFIG_XENO_DRIVERS_CAN_VIRT) += xeno_can_virt.o
 
 xeno_can-y := rtcan_dev.o rtcan_socket.o rtcan_module.o rtcan_raw.o rtcan_raw_dev.o rtcan_raw_filter.o
 xeno_can_virt-y := rtcan_virt.o
+xeno_can_flexcan-y := rtcan_flexcan.o
 
 else
 
ksrc/drivers/can/rtcan_flexcan.c new file +1135 -0

1,135 lines not on this page · raw message

Signature · 1 line
1.7.7.6