Skip to contentSkip to conversation

CAN: Add Flexcan CAN controller driver

PATCH Superseded Sascha Hauer 3 participants 10 messages Jul 28, 2009

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 CAN: Add Flexcan CAN controller driver

Hi,

Here is the second version of the flexcan driver.

Sascha

Quoted 1 line

From a1ea20bc862778f2b64efc18d21d05e5debb9562 Mon Sep 17 00:00:00 2001

From: Sascha Hauer <[email protected]>
Date: Tue, 21 Jul 2009 10:47:19 +0200
Subject: [PATCH] CAN: Add Flexcan CAN driver

This core is found on some Freescale SoCs and also some Coldfire SoCs. Support for Coldfire is missing though at the moment as they have an older revision of the core which does not have RX FIFO support.

V2: integrated comments from Wolfgang Grandegger

---
 drivers/net/can/Kconfig   |    6 +
 drivers/net/can/Makefile  |    1 +
 drivers/net/can/flexcan.c |  692 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 699 insertions(+), 0 deletions(-)
 create mode 100644 drivers/net/can/flexcan.c
drivers/net/can/Kconfig +6 -0
index 33821a8..99c6da4 100644
@@ -74,6 +74,12 @@ config CAN_KVASER_PCI
 	  This driver is for the the PCIcanx and PCIcan cards (1, 2 or
 	  4 channel) from Kvaser (http://www.kvaser.com).
 
+config CAN_FLEXCAN
+	tristate "Support for Freescale FLEXCAN based chips"
+	depends on CAN_DEV
+	---help---
+	  Say Y here if you want to support for Freescale FlexCAN.
+
 config CAN_DEBUG_DEVICES
 	bool "CAN devices debugging messages"
 	depends on CAN
drivers/net/can/Makefile +1 -0
index 523a941..25f2032 100644
@@ -8,5 +8,6 @@ obj-$(CONFIG_CAN_DEV)		+= can-dev.o
 can-dev-y			:= dev.o
 
 obj-$(CONFIG_CAN_SJA1000)	+= sja1000/
+obj-$(CONFIG_CAN_FLEXCAN)	+= flexcan.o
 
 ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG
drivers/net/can/flexcan.c new file +692 -0

692 lines not on this page · raw message

Signature · 7 lines
1.6.3.3

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#1 lore raw
Quoted 3 lines · Sascha Hauer

Hi,

Here is the second version of the flexcan driver.

Hi Sascha,

some more points i forgot to mention, sorry ...

drivers/net/can/flexcan.c line 126

Quoted 6 lines
drivers/net/can/flexcan.c new file +6 -0
+/* Structure of the message buffer */
+struct flexcan_mb {
+	u32	can_dlc;
+	u32	can_id;
+	u32	data[2];
+};

This looks really hackish and does not reflect the structure of a flexcan message buffer! The data is 'u8' and the name of 'dlc' for the description/flag register is bad.

drivers/net/can/flexcan.c line 175

Quoted 9 lines
drivers/net/can/flexcan.c new file +8 -0
+
+static int flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
+{
+	struct can_frame *frame = (struct can_frame *)skb->data;
+	struct flexcan_priv *priv = netdev_priv(dev);
+	struct flexcan_regs __iomem *regs = priv->base;
 
+	u32 can_id;
+	u32 dlc = MB_CNT_CODE(0xc) | (frame->can_dlc << 16);

Naming this variable 'dlc' does not hit the point. See below.

drivers/net/can/flexcan.c line 176

Quoted 1 line
drivers/net/can/flexcan.c new file +1 -0
+	u32 *can_data;

Really this needs to be fixed up by defining a proper mailbox struct.

drivers/net/can/flexcan.c line 181

Quoted 5 lines
drivers/net/can/flexcan.c new file +5 -0
+
+	netif_stop_queue(dev);
+
+	if (frame->can_id & CAN_EFF_FLAG) {
+		can_id = frame->can_id & MB_ID_EXT;

Please use CAN_EFF_MASK here.

drivers/net/can/flexcan.c line 185

Quoted 4 lines
drivers/net/can/flexcan.c new file +4 -0
+		dlc |= MB_CNT_IDE | MB_CNT_SRR;
+	} else {
+		can_id = (frame->can_id & CAN_SFF_MASK) << 18;
+	}

Just nitpicking for Kernel coding style:
remove the last '{' and '}' pair.

drivers/net/can/flexcan.c line 195

Quoted 10 lines
drivers/net/can/flexcan.c new file +10 -0
+
+	if (frame->can_id & CAN_RTR_FLAG)
+		dlc |= MB_CNT_RTR;
+
+	writel(dlc, &regs->cantxfg[TX_BUF_ID].can_dlc);
+	writel(can_id, &regs->cantxfg[TX_BUF_ID].can_id);
+
+	can_data = (u32 *)frame->data;
+	writel(cpu_to_be32(*can_data), &regs->cantxfg[TX_BUF_ID].data[0]);
+	writel(cpu_to_be32(*(can_data + 1)), &regs->cantxfg[TX_BUF_ID].data[1]);

IMHO it is not really transparent, that this is a correct handling to copy the
can_frame.data[] on all architectures. I bet creating a for-statement
regarding the dlc is not slower and makes really clear, what's going on here.

Quoted 5 lines
drivers/net/can/flexcan.c new file +5 -0
+
+	writel(dlc, &regs->cantxfg[TX_BUF_ID].can_dlc);
+
+	kfree_skb(skb);
+

drivers/net/can/flexcan.c line 210

Quoted 10 lines
drivers/net/can/flexcan.c new file +10 -0
+	return NETDEV_TX_OK;
+}
+
+static void flexcan_rx_frame(struct net_device *ndev,
+		struct flexcan_mb __iomem *mb)
+{
+	struct net_device_stats *stats = &ndev->stats;
+	struct sk_buff *skb;
+	struct can_frame *frame;
+	int ctrl = readl(&mb->can_dlc);

'ctrl' is much better than 'dlc' naming above :-)

drivers/net/can/flexcan.c line 211

Quoted 1 line
drivers/net/can/flexcan.c new file +1 -0
+	int length = (ctrl >> 16) & 0x0f;

is probably not enough ... use %9 or an additional check.

drivers/net/can/flexcan.c line 224

Quoted 13 lines
drivers/net/can/flexcan.c new file +13 -0
+	u32 id;
+
+	skb = dev_alloc_skb(sizeof(struct can_frame));
+	if (!skb) {
+		stats->rx_dropped++;
+		return;
+	}
+
+	frame = (struct can_frame *)skb_put(skb,
+			sizeof(struct can_frame));
+
+	frame->can_dlc = length;
+	id = readl(&mb->can_id) & MB_ID_EXT;

like above use CAN_EFF_MASK

or at least rename MB_ID_EXT to MB_ID_EXT_MASK

Quoted 4 lines
drivers/net/can/flexcan.c new file +4 -0
+
+	if (ctrl & MB_CNT_IDE) {
+		frame->can_id = id & CAN_EFF_MASK;
+		frame->can_id |= CAN_EFF_FLAG;

drivers/net/can/flexcan.c line 238

Quoted 10 lines
drivers/net/can/flexcan.c new file +10 -0
+		if (ctrl & MB_CNT_RTR)
+			frame->can_id |= CAN_RTR_FLAG;
+	} else {
+		frame->can_id  = id >> 18;
+		if (ctrl & MB_CNT_RTR)
+			frame->can_id |= CAN_RTR_FLAG;
+	}
+
+	*(u32 *)frame->data = be32_to_cpu(readl(&mb->data[0]));
+	*((u32 *)frame->data + 1) = be32_to_cpu(readl(&mb->data[1]));

Same as above.

Please write readable an maintainable code and let the compiler do his job.

Thanks,
Oliver

#2 lore raw

Hi Oliver,

Quoted 10 lines · Oliver Hartkopp
Quoted 3 lines · Sascha Hauer

Hi,

Here is the second version of the flexcan driver.

Hi Sascha,

some more points i forgot to mention, sorry ...

Quoted 11 lines

drivers/net/can/flexcan.c line 126

Quoted 6 lines
drivers/net/can/flexcan.c new file +6 -0
+/* Structure of the message buffer */
+struct flexcan_mb {
+	u32	can_dlc;
+	u32	can_id;
+	u32	data[2];
+};

This looks really hackish and does not reflect the structure of a flexcan message buffer! The data is 'u8' and the name of 'dlc' for the description/flag register is bad.

see below..

Quoted 18 lines

drivers/net/can/flexcan.c line 175

Quoted 10 lines
drivers/net/can/flexcan.c new file +8 -0
+
+static int flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
+{
+	struct can_frame *frame = (struct can_frame *)skb->data;
+	struct flexcan_priv *priv = netdev_priv(dev);
+	struct flexcan_regs __iomem *regs = priv->base;
 
+	u32 can_id;
+	u32 dlc = MB_CNT_CODE(0xc) | (frame->can_dlc << 16);

Naming this variable 'dlc' does not hit the point. See below.

drivers/net/can/flexcan.c line 176

Quoted 1 line
drivers/net/can/flexcan.c new file +1 -0
+	u32 *can_data;

Really this needs to be fixed up by defining a proper mailbox struct.

Quoted 7 lines

drivers/net/can/flexcan.c line 181

Quoted 5 lines
drivers/net/can/flexcan.c new file +5 -0
+
+	netif_stop_queue(dev);
+
+	if (frame->can_id & CAN_EFF_FLAG) {
+		can_id = frame->can_id & MB_ID_EXT;

Please use CAN_EFF_MASK here.

I used MX_ID_EXT intentionally because it it flexcan specific and just happens to be the same as CAN_EFF_MASK. I can change it if you like.

Quoted 9 lines

drivers/net/can/flexcan.c line 185

Quoted 6 lines
drivers/net/can/flexcan.c new file +4 -0
+		dlc |= MB_CNT_IDE | MB_CNT_SRR;
+	} else {
+		can_id = (frame->can_id & CAN_SFF_MASK) << 18;
+	}

Just nitpicking for Kernel coding style:
remove the last '{' and '}' pair.

No, Documentation/CondingStyle suggests that if one branch needs braces the other branch should use them, too.

Quoted 15 lines

drivers/net/can/flexcan.c line 195

Quoted 11 lines
drivers/net/can/flexcan.c new file +10 -0
+
+	if (frame->can_id & CAN_RTR_FLAG)
+		dlc |= MB_CNT_RTR;
+
+	writel(dlc, &regs->cantxfg[TX_BUF_ID].can_dlc);
+	writel(can_id, &regs->cantxfg[TX_BUF_ID].can_id);
+
+	can_data = (u32 *)frame->data;
+	writel(cpu_to_be32(*can_data), &regs->cantxfg[TX_BUF_ID].data[0]);
+	writel(cpu_to_be32(*(can_data + 1)), &regs->cantxfg[TX_BUF_ID].data[1]);

IMHO it is not really transparent, that this is a correct handling to copy the
can_frame.data[] on all architectures. I bet creating a for-statement
regarding the dlc is not slower and makes really clear, what's going on here.

This is indeed a problem here. The original Coldfire code I used as a template used a loop around unsigned char * which did the wrong thing for me. So yes, this is not generic here, but I have no idea how the generic code looks like. As Coldfire is big endian this doesn't seem that wrong.

Quoted 10 lines
Quoted 10 lines
drivers/net/can/flexcan.c new file +9 -0
+
+	writel(dlc, &regs->cantxfg[TX_BUF_ID].can_dlc);
+
+	kfree_skb(skb);
+
+	return NETDEV_TX_OK;
+}
+
+static void flexcan_rx_frame(struct net_device *ndev,
Quoted 8 lines

drivers/net/can/flexcan.c line 210

Quoted 6 lines
drivers/net/can/flexcan.c new file +6 -0
+		struct flexcan_mb __iomem *mb)
+{
+	struct net_device_stats *stats = &ndev->stats;
+	struct sk_buff *skb;
+	struct can_frame *frame;
+	int ctrl = readl(&mb->can_dlc);

'ctrl' is much better than 'dlc' naming above :-)

ok, will change it above.

Quoted 4 lines

drivers/net/can/flexcan.c line 211

Quoted 2 lines
drivers/net/can/flexcan.c new file +1 -0
+	int length = (ctrl >> 16) & 0x0f;

is probably not enough ... use %9 or an additional check.

ok, I'll add a sanity check.

Quoted 41 lines
Quoted 4 lines
drivers/net/can/flexcan.c new file +3 -0
+	u32 id;
+
+	skb = dev_alloc_skb(sizeof(struct can_frame));

drivers/net/can/flexcan.c line 224

Quoted 10 lines
drivers/net/can/flexcan.c new file +10 -0
+	if (!skb) {
+		stats->rx_dropped++;
+		return;
+	}
+
+	frame = (struct can_frame *)skb_put(skb,
+			sizeof(struct can_frame));
+
+	frame->can_dlc = length;
+	id = readl(&mb->can_id) & MB_ID_EXT;

like above use CAN_EFF_MASK

or at least rename MB_ID_EXT to MB_ID_EXT_MASK

Quoted 4 lines
drivers/net/can/flexcan.c new file +4 -0
+
+	if (ctrl & MB_CNT_IDE) {
+		frame->can_id = id & CAN_EFF_MASK;
+		frame->can_id |= CAN_EFF_FLAG;

drivers/net/can/flexcan.c line 238

Quoted 10 lines
drivers/net/can/flexcan.c new file +10 -0
+		if (ctrl & MB_CNT_RTR)
+			frame->can_id |= CAN_RTR_FLAG;
+	} else {
+		frame->can_id  = id >> 18;
+		if (ctrl & MB_CNT_RTR)
+			frame->can_id |= CAN_RTR_FLAG;
+	}
+
+	*(u32 *)frame->data = be32_to_cpu(readl(&mb->data[0]));
+	*((u32 *)frame->data + 1) = be32_to_cpu(readl(&mb->data[1]));

Same as above.

Please write readable an maintainable code and let the compiler do his job.

Thanks,
Oliver

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#3 lore raw
Quoted 23 lines · Sascha Hauer

Hi Oliver,

Quoted 9 lines · Oliver Hartkopp
Quoted 3 lines · Sascha Hauer

Hi,

Here is the second version of the flexcan driver.

Hi Sascha,

some more points i forgot to mention, sorry ...

Quoted 10 lines

drivers/net/can/flexcan.c line 126

Quoted 6 lines
drivers/net/can/flexcan.c new file +6 -0
+/* Structure of the message buffer */
+struct flexcan_mb {
+	u32	can_dlc;
+	u32	can_id;
+	u32	data[2];
+};

This looks really hackish and does not reflect the structure of a flexcan message buffer! The data is 'u8' and the name of 'dlc' for the description/flag register is bad.

Quoted 1 line

see below..

Especially can_dlc, can_id and data[] are known from struct can_frame which really can confuse here ...

Quoted 22 lines
Quoted 15 lines

drivers/net/can/flexcan.c line 175

Quoted 9 lines
drivers/net/can/flexcan.c new file +8 -0
+
+static int flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
+{
+	struct can_frame *frame = (struct can_frame *)skb->data;
+	struct flexcan_priv *priv = netdev_priv(dev);
+	struct flexcan_regs __iomem *regs = priv->base;
+	u32 can_id;
+	u32 dlc = MB_CNT_CODE(0xc) | (frame->can_dlc << 16);

Naming this variable 'dlc' does not hit the point. See below.

drivers/net/can/flexcan.c line 176

Quoted 1 line
drivers/net/can/flexcan.c new file +1 -0
+	u32 *can_data;

Really this needs to be fixed up by defining a proper mailbox struct.

Quoted 6 lines

drivers/net/can/flexcan.c line 181

Quoted 5 lines
drivers/net/can/flexcan.c new file +5 -0
+
+	netif_stop_queue(dev);
+
+	if (frame->can_id & CAN_EFF_FLAG) {
+		can_id = frame->can_id & MB_ID_EXT;

Please use CAN_EFF_MASK here.

Quoted 2 lines

I used MX_ID_EXT intentionally because it it flexcan specific and just happens to be the same as CAN_EFF_MASK. I can change it if you like.

Yes, i've seen that. I would tend to use CAN_EFF_MASK here as you apply it on frame->can_id.

When you get it from the controller MB_ID_EXT_MASK would be the better one.

Quoted 11 lines
Quoted 8 lines

drivers/net/can/flexcan.c line 185

Quoted 6 lines
drivers/net/can/flexcan.c new file +4 -0
+		dlc |= MB_CNT_IDE | MB_CNT_SRR;
+	} else {
+		can_id = (frame->can_id & CAN_SFF_MASK) << 18;
+	}

Just nitpicking for Kernel coding style:
remove the last '{' and '}' pair.

No, Documentation/CondingStyle suggests that if one branch needs braces the other branch should use them, too.

Sorry. Didn't know that.

Quoted 15 lines
Quoted 14 lines

drivers/net/can/flexcan.c line 195

Quoted 11 lines
drivers/net/can/flexcan.c new file +10 -0
+
+	if (frame->can_id & CAN_RTR_FLAG)
+		dlc |= MB_CNT_RTR;
+
+	writel(dlc, &regs->cantxfg[TX_BUF_ID].can_dlc);
+	writel(can_id, &regs->cantxfg[TX_BUF_ID].can_id);
+
+	can_data = (u32 *)frame->data;
+	writel(cpu_to_be32(*can_data), &regs->cantxfg[TX_BUF_ID].data[0]);
+	writel(cpu_to_be32(*(can_data + 1)), &regs->cantxfg[TX_BUF_ID].data[1]);

IMHO it is not really transparent, that this is a correct handling to copy the
can_frame.data[] on all architectures. I bet creating a for-statement
regarding the dlc is not slower and makes really clear, what's going on here.

Quoted 3 lines

This is indeed a problem here. The original Coldfire code I used as a template used a loop around unsigned char * which did the wrong thing for me.

This could be a good starting point for an investigation ;-)

Quoted 3 lines

So yes, this is not generic here, but I have no idea how the generic code looks like. As Coldfire is big endian this doesn't seem that wrong.

I would try to define a proper flexcan_mb struct like

struct flexcan_mb {
	u8 code;
	u8 ctrl;
	u16 timestamp;
	u32 id;
	u8 data[8];
}

And then see how it looks like ;-)

Regards,
Oliver

#4 lore raw
Quoted 8 lines · Oliver Hartkopp
Quoted 6 lines · Sascha Hauer
Quoted 6 lines

drivers/net/can/flexcan.c line 191

Quoted 6 lines
drivers/net/can/flexcan.c new file +6 -0
+
+	if (frame->can_id & CAN_RTR_FLAG)
+		dlc |= MB_CNT_RTR;
+
+	writel(dlc, &regs->cantxfg[TX_BUF_ID].can_dlc);
+	writel(can_id, &regs->cantxfg[TX_BUF_ID].can_id);

Are you sure, that this is correct?

Indeed i wonder, if it would make sense to skip the entire struct flexcan_mb approach and fiddle byte-by-byte inside the registers ...

Regards,
Oliver

#5 lore raw
Quoted 11 lines · Oliver Hartkopp
Quoted 8 lines · Oliver Hartkopp
Quoted 6 lines · Sascha Hauer
Quoted 6 lines

drivers/net/can/flexcan.c line 191

Quoted 6 lines
drivers/net/can/flexcan.c new file +6 -0
+
+	if (frame->can_id & CAN_RTR_FLAG)
+		dlc |= MB_CNT_RTR;
+
+	writel(dlc, &regs->cantxfg[TX_BUF_ID].can_dlc);
+	writel(can_id, &regs->cantxfg[TX_BUF_ID].can_id);

Are you sure, that this is correct?

Yes, I am sure, at least on my hardware.

Quoted 3 lines

Indeed i wonder, if it would make sense to skip the entire struct flexcan_mb approach and fiddle byte-by-byte inside the registers ...

You'll have to to 32 bit accesses to get it right on little and big endian.

what we can do is:

writel(can_data[0] << 24 | can_data[1] << 16 | can_data[2] << 8 | can_data[3],

	msg_buf + 0x8);

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#6 lore raw
Quoted 11 lines · Sascha Hauer
Quoted 9 lines · Oliver Hartkopp
Quoted 7 lines · Oliver Hartkopp
Quoted 6 lines · Sascha Hauer
Quoted 6 lines

drivers/net/can/flexcan.c line 191

Quoted 6 lines
drivers/net/can/flexcan.c new file +6 -0
+
+	if (frame->can_id & CAN_RTR_FLAG)
+		dlc |= MB_CNT_RTR;
+
+	writel(dlc, &regs->cantxfg[TX_BUF_ID].can_dlc);
+	writel(can_id, &regs->cantxfg[TX_BUF_ID].can_id);

Are you sure, that this is correct?

Quoted 1 line

Yes, I am sure, at least on my hardware.

I looked into the writel() macro which does a cpu_to_le32() to the value - sorry that i did not check this before ...

Quoted 11 lines
Quoted 3 lines

Indeed i wonder, if it would make sense to skip the entire struct flexcan_mb approach and fiddle byte-by-byte inside the registers ...

You'll have to to 32 bit accesses to get it right on little and big endian.

what we can do is:

writel(can_data[0] << 24 | can_data[1] << 16 | can_data[2] << 8 | can_data[3],

	msg_buf + 0x8);

This looks easier to understand and makes things clear, when you look into the specification.

Regards,
Oliver

#7 lore raw
Quoted 24 lines · Oliver Hartkopp
Quoted 11 lines · Sascha Hauer
Quoted 9 lines · Oliver Hartkopp
Quoted 7 lines · Oliver Hartkopp
Quoted 6 lines · Sascha Hauer
Quoted 6 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+
+	if (frame->can_id & CAN_RTR_FLAG)
+		dlc |= MB_CNT_RTR;
+
+	writel(dlc, &regs->cantxfg[TX_BUF_ID].can_dlc);
+	writel(can_id, &regs->cantxfg[TX_BUF_ID].can_id);

Are you sure, that this is correct?

Quoted 1 line

Yes, I am sure, at least on my hardware.

I looked into the writel() macro which does a cpu_to_le32() to the value - sorry that i did not check this before ...

Quoted 7 lines
Quoted 3 lines

Indeed i wonder, if it would make sense to skip the entire struct flexcan_mb approach and fiddle byte-by-byte inside the registers ...

You'll have to to 32 bit accesses to get it right on little and big endian.

Quoted 7 lines
Quoted 4 lines

what we can do is:

writel(can_data[0] << 24 | can_data[1] << 16 | can_data[2] << 8 | can_data[3],

	msg_buf + 0x8);

This looks easier to understand and makes things clear, when you look into the specification.

Ok, I'll change it like this.

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#8 lore raw
Quoted 21 lines · Sascha Hauer
Quoted 20 lines · Oliver Hartkopp
Quoted 11 lines · Sascha Hauer
Quoted 9 lines · Oliver Hartkopp
Quoted 7 lines · Oliver Hartkopp
Quoted 6 lines · Sascha Hauer
Quoted 6 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+
+	if (frame->can_id & CAN_RTR_FLAG)
+		dlc |= MB_CNT_RTR;
+
+	writel(dlc, &regs->cantxfg[TX_BUF_ID].can_dlc);
+	writel(can_id, &regs->cantxfg[TX_BUF_ID].can_id);

Are you sure, that this is correct?

Yes, I am sure, at least on my hardware.

I looked into the writel() macro which does a cpu_to_le32() to the value - sorry that i did not check this before ...

Quoted 5 lines
Quoted 2 lines

Indeed i wonder, if it would make sense to skip the entire struct flexcan_mb approach and fiddle byte-by-byte inside the registers ...

You'll have to to 32 bit accesses to get it right on little and big endian.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

what we can do is:

writel(can_data[0] << 24 | can_data[1] << 16 | can_data[2] << 8 | can_data[3],

	msg_buf + 0x8);

This looks easier to understand and makes things clear, when you look into the specification.

Ok, I'll change it like this.

OK, even if I do not really share Oliver's concerns. The second write could be suppressed if dlc <= 4.

Wolfgang.

#9 lore raw
In reply to Oliver Hartkopp
Quoted 100 lines · Oliver Hartkopp
Quoted 23 lines · Sascha Hauer

Hi Oliver,

Quoted 9 lines · Oliver Hartkopp
Quoted 3 lines · Sascha Hauer

Hi,

Here is the second version of the flexcan driver.

Hi Sascha,

some more points i forgot to mention, sorry ...

Quoted 10 lines

drivers/net/can/flexcan.c line 126

Quoted 6 lines
drivers/net/can/flexcan.c new file +6 -0
+/* Structure of the message buffer */
+struct flexcan_mb {
+	u32	can_dlc;
+	u32	can_id;
+	u32	data[2];
+};

This looks really hackish and does not reflect the structure of a flexcan message buffer! The data is 'u8' and the name of 'dlc' for the description/flag register is bad.

Quoted 1 line

see below..

Especially can_dlc, can_id and data[] are known from struct can_frame which really can confuse here ...

Quoted 22 lines
Quoted 15 lines

drivers/net/can/flexcan.c line 175

Quoted 9 lines
drivers/net/can/flexcan.c new file +8 -0
+
+static int flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
+{
+	struct can_frame *frame = (struct can_frame *)skb->data;
+	struct flexcan_priv *priv = netdev_priv(dev);
+	struct flexcan_regs __iomem *regs = priv->base;
+	u32 can_id;
+	u32 dlc = MB_CNT_CODE(0xc) | (frame->can_dlc << 16);

Naming this variable 'dlc' does not hit the point. See below.

drivers/net/can/flexcan.c line 176

Quoted 1 line
drivers/net/can/flexcan.c new file +1 -0
+	u32 *can_data;

Really this needs to be fixed up by defining a proper mailbox struct.

Quoted 6 lines

drivers/net/can/flexcan.c line 181

Quoted 5 lines
drivers/net/can/flexcan.c new file +5 -0
+
+	netif_stop_queue(dev);
+
+	if (frame->can_id & CAN_EFF_FLAG) {
+		can_id = frame->can_id & MB_ID_EXT;

Please use CAN_EFF_MASK here.

Quoted 2 lines

I used MX_ID_EXT intentionally because it it flexcan specific and just happens to be the same as CAN_EFF_MASK. I can change it if you like.

Yes, i've seen that. I would tend to use CAN_EFF_MASK here as you apply it on frame->can_id.

When you get it from the controller MB_ID_EXT_MASK would be the better one.

Quoted 11 lines
Quoted 8 lines

drivers/net/can/flexcan.c line 185

Quoted 6 lines
drivers/net/can/flexcan.c new file +4 -0
+		dlc |= MB_CNT_IDE | MB_CNT_SRR;
+	} else {
+		can_id = (frame->can_id & CAN_SFF_MASK) << 18;
+	}

Just nitpicking for Kernel coding style:
remove the last '{' and '}' pair.

No, Documentation/CondingStyle suggests that if one branch needs braces the other branch should use them, too.

Sorry. Didn't know that.

Quoted 15 lines
Quoted 14 lines

drivers/net/can/flexcan.c line 195

Quoted 11 lines
drivers/net/can/flexcan.c new file +10 -0
+
+	if (frame->can_id & CAN_RTR_FLAG)
+		dlc |= MB_CNT_RTR;
+
+	writel(dlc, &regs->cantxfg[TX_BUF_ID].can_dlc);
+	writel(can_id, &regs->cantxfg[TX_BUF_ID].can_id);
+
+	can_data = (u32 *)frame->data;
+	writel(cpu_to_be32(*can_data), &regs->cantxfg[TX_BUF_ID].data[0]);
+	writel(cpu_to_be32(*(can_data + 1)), &regs->cantxfg[TX_BUF_ID].data[1]);

IMHO it is not really transparent, that this is a correct handling to copy the
can_frame.data[] on all architectures. I bet creating a for-statement
regarding the dlc is not slower and makes really clear, what's going on here.

Quoted 3 lines

This is indeed a problem here. The original Coldfire code I used as a template used a loop around unsigned char * which did the wrong thing for me.

This could be a good starting point for an investigation ;-)

Quoted 3 lines

So yes, this is not generic here, but I have no idea how the generic code looks like. As Coldfire is big endian this doesn't seem that wrong.

I would try to define a proper flexcan_mb struct like

Quoted 7 lines
struct flexcan_mb {
	u8 code;
	u8 ctrl;
	u16 timestamp;
	u32 id;
	u8 data[8];
}

I can't properly define it like this because I have a little endian system and u8 code is on offset 3 on my hardware. I just checked it because I always get confused with endian problems ;)

I hope you're not suggesting me to do something like

#ifdef __LITTLE_ENDIAN

struct flexcan_mb {
	u16 timestamp;
	u8 ctrl;
	u8 code;
	u32 id;
	u8 data[8];
}
#else
struct flexcan_mb {
	u8 code;
	u8 ctrl;
	u16 timestamp;
	u32 id;
	u8 data[8];
}

#endif

(which would still require endian specific handling for the actual CAN data)

Quoted 2 lines

And then see how it looks like ;-)

Well, I would have to do a le/be conversion manually whereas cpu_to_be32 *should* do the right thing. I don't have any ColdFire hardware to test though.

The more I think about it the more I think that my original code does the right thing(tm)

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |