1dd2878aaSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
299c4a634SDavid S. Miller /*
399c4a634SDavid S. Miller * at91_can.c - CAN network driver for AT91 SoC CAN controller
499c4a634SDavid S. Miller *
53e9ebd3cSHans J. Koch * (C) 2007 by Hans J. Koch <hjk@hansjkoch.de>
60909c1ecSMarc Kleine-Budde * (C) 2008, 2009, 2010, 2011 by Marc Kleine-Budde <kernel@pengutronix.de>
799c4a634SDavid S. Miller */
899c4a634SDavid S. Miller
999c4a634SDavid S. Miller #include <linux/clk.h>
1099c4a634SDavid S. Miller #include <linux/errno.h>
11409c188cSVincent Mailhol #include <linux/ethtool.h>
1299c4a634SDavid S. Miller #include <linux/if_arp.h>
1399c4a634SDavid S. Miller #include <linux/interrupt.h>
1499c4a634SDavid S. Miller #include <linux/kernel.h>
1599c4a634SDavid S. Miller #include <linux/module.h>
1699c4a634SDavid S. Miller #include <linux/netdevice.h>
173078cde7SLudovic Desroches #include <linux/of.h>
1899c4a634SDavid S. Miller #include <linux/platform_device.h>
193a5655a5SMarc Kleine-Budde #include <linux/rtnetlink.h>
2099c4a634SDavid S. Miller #include <linux/skbuff.h>
2199c4a634SDavid S. Miller #include <linux/spinlock.h>
2299c4a634SDavid S. Miller #include <linux/string.h>
2399c4a634SDavid S. Miller #include <linux/types.h>
2499c4a634SDavid S. Miller
2599c4a634SDavid S. Miller #include <linux/can/dev.h>
2699c4a634SDavid S. Miller #include <linux/can/error.h>
2799c4a634SDavid S. Miller
28b049994dSMarc Kleine-Budde #define AT91_MB_MASK(i) ((1 << (i)) - 1)
2999c4a634SDavid S. Miller
3099c4a634SDavid S. Miller /* Common registers */
3199c4a634SDavid S. Miller enum at91_reg {
3299c4a634SDavid S. Miller AT91_MR = 0x000,
3399c4a634SDavid S. Miller AT91_IER = 0x004,
3499c4a634SDavid S. Miller AT91_IDR = 0x008,
3599c4a634SDavid S. Miller AT91_IMR = 0x00C,
3699c4a634SDavid S. Miller AT91_SR = 0x010,
3799c4a634SDavid S. Miller AT91_BR = 0x014,
3899c4a634SDavid S. Miller AT91_TIM = 0x018,
3999c4a634SDavid S. Miller AT91_TIMESTP = 0x01C,
4099c4a634SDavid S. Miller AT91_ECR = 0x020,
4199c4a634SDavid S. Miller AT91_TCR = 0x024,
4299c4a634SDavid S. Miller AT91_ACR = 0x028,
4399c4a634SDavid S. Miller };
4499c4a634SDavid S. Miller
4599c4a634SDavid S. Miller /* Mailbox registers (0 <= i <= 15) */
4657bca980SPeng Li #define AT91_MMR(i) ((enum at91_reg)(0x200 + ((i) * 0x20)))
4757bca980SPeng Li #define AT91_MAM(i) ((enum at91_reg)(0x204 + ((i) * 0x20)))
4857bca980SPeng Li #define AT91_MID(i) ((enum at91_reg)(0x208 + ((i) * 0x20)))
4957bca980SPeng Li #define AT91_MFID(i) ((enum at91_reg)(0x20C + ((i) * 0x20)))
5057bca980SPeng Li #define AT91_MSR(i) ((enum at91_reg)(0x210 + ((i) * 0x20)))
5157bca980SPeng Li #define AT91_MDL(i) ((enum at91_reg)(0x214 + ((i) * 0x20)))
5257bca980SPeng Li #define AT91_MDH(i) ((enum at91_reg)(0x218 + ((i) * 0x20)))
5357bca980SPeng Li #define AT91_MCR(i) ((enum at91_reg)(0x21C + ((i) * 0x20)))
5499c4a634SDavid S. Miller
5599c4a634SDavid S. Miller /* Register bits */
5699c4a634SDavid S. Miller #define AT91_MR_CANEN BIT(0)
5799c4a634SDavid S. Miller #define AT91_MR_LPM BIT(1)
5899c4a634SDavid S. Miller #define AT91_MR_ABM BIT(2)
5999c4a634SDavid S. Miller #define AT91_MR_OVL BIT(3)
6099c4a634SDavid S. Miller #define AT91_MR_TEOF BIT(4)
6199c4a634SDavid S. Miller #define AT91_MR_TTM BIT(5)
6299c4a634SDavid S. Miller #define AT91_MR_TIMFRZ BIT(6)
6399c4a634SDavid S. Miller #define AT91_MR_DRPT BIT(7)
6499c4a634SDavid S. Miller
6599c4a634SDavid S. Miller #define AT91_SR_RBSY BIT(29)
6699c4a634SDavid S. Miller
6799c4a634SDavid S. Miller #define AT91_MMR_PRIO_SHIFT (16)
6899c4a634SDavid S. Miller
6999c4a634SDavid S. Miller #define AT91_MID_MIDE BIT(29)
7099c4a634SDavid S. Miller
7199c4a634SDavid S. Miller #define AT91_MSR_MRTR BIT(20)
7299c4a634SDavid S. Miller #define AT91_MSR_MABT BIT(22)
7399c4a634SDavid S. Miller #define AT91_MSR_MRDY BIT(23)
7499c4a634SDavid S. Miller #define AT91_MSR_MMI BIT(24)
7599c4a634SDavid S. Miller
7699c4a634SDavid S. Miller #define AT91_MCR_MRTR BIT(20)
7799c4a634SDavid S. Miller #define AT91_MCR_MTCR BIT(23)
7899c4a634SDavid S. Miller
7999c4a634SDavid S. Miller /* Mailbox Modes */
8099c4a634SDavid S. Miller enum at91_mb_mode {
8199c4a634SDavid S. Miller AT91_MB_MODE_DISABLED = 0,
8299c4a634SDavid S. Miller AT91_MB_MODE_RX = 1,
8399c4a634SDavid S. Miller AT91_MB_MODE_RX_OVRWR = 2,
8499c4a634SDavid S. Miller AT91_MB_MODE_TX = 3,
8599c4a634SDavid S. Miller AT91_MB_MODE_CONSUMER = 4,
8699c4a634SDavid S. Miller AT91_MB_MODE_PRODUCER = 5,
8799c4a634SDavid S. Miller };
8899c4a634SDavid S. Miller
8999c4a634SDavid S. Miller /* Interrupt mask bits */
908ed1661cSPeng Li #define AT91_IRQ_ERRA BIT(16)
918ed1661cSPeng Li #define AT91_IRQ_WARN BIT(17)
928ed1661cSPeng Li #define AT91_IRQ_ERRP BIT(18)
938ed1661cSPeng Li #define AT91_IRQ_BOFF BIT(19)
948ed1661cSPeng Li #define AT91_IRQ_SLEEP BIT(20)
958ed1661cSPeng Li #define AT91_IRQ_WAKEUP BIT(21)
968ed1661cSPeng Li #define AT91_IRQ_TOVF BIT(22)
978ed1661cSPeng Li #define AT91_IRQ_TSTP BIT(23)
988ed1661cSPeng Li #define AT91_IRQ_CERR BIT(24)
998ed1661cSPeng Li #define AT91_IRQ_SERR BIT(25)
1008ed1661cSPeng Li #define AT91_IRQ_AERR BIT(26)
1018ed1661cSPeng Li #define AT91_IRQ_FERR BIT(27)
1028ed1661cSPeng Li #define AT91_IRQ_BERR BIT(28)
10399c4a634SDavid S. Miller
10499c4a634SDavid S. Miller #define AT91_IRQ_ERR_ALL (0x1fff0000)
10599c4a634SDavid S. Miller #define AT91_IRQ_ERR_FRAME (AT91_IRQ_CERR | AT91_IRQ_SERR | \
10699c4a634SDavid S. Miller AT91_IRQ_AERR | AT91_IRQ_FERR | AT91_IRQ_BERR)
10799c4a634SDavid S. Miller #define AT91_IRQ_ERR_LINE (AT91_IRQ_ERRA | AT91_IRQ_WARN | \
10899c4a634SDavid S. Miller AT91_IRQ_ERRP | AT91_IRQ_BOFF)
10999c4a634SDavid S. Miller
11099c4a634SDavid S. Miller #define AT91_IRQ_ALL (0x1fffffff)
11199c4a634SDavid S. Miller
112d3d47264SMarc Kleine-Budde enum at91_devtype {
113d3d47264SMarc Kleine-Budde AT91_DEVTYPE_SAM9263,
1146388b396SMarc Kleine-Budde AT91_DEVTYPE_SAM9X5,
115d3d47264SMarc Kleine-Budde };
116d3d47264SMarc Kleine-Budde
117d3d47264SMarc Kleine-Budde struct at91_devtype_data {
118d3d47264SMarc Kleine-Budde unsigned int rx_first;
119d3d47264SMarc Kleine-Budde unsigned int rx_split;
120d3d47264SMarc Kleine-Budde unsigned int rx_last;
121d3d47264SMarc Kleine-Budde unsigned int tx_shift;
122d3d47264SMarc Kleine-Budde enum at91_devtype type;
123d3d47264SMarc Kleine-Budde };
124d3d47264SMarc Kleine-Budde
12599c4a634SDavid S. Miller struct at91_priv {
12699c4a634SDavid S. Miller struct can_priv can; /* must be the first member! */
12799c4a634SDavid S. Miller struct napi_struct napi;
12899c4a634SDavid S. Miller
12999c4a634SDavid S. Miller void __iomem *reg_base;
13099c4a634SDavid S. Miller
13199c4a634SDavid S. Miller u32 reg_sr;
13299c4a634SDavid S. Miller unsigned int tx_next;
13399c4a634SDavid S. Miller unsigned int tx_echo;
13499c4a634SDavid S. Miller unsigned int rx_next;
135d3d47264SMarc Kleine-Budde struct at91_devtype_data devtype_data;
13699c4a634SDavid S. Miller
13799c4a634SDavid S. Miller struct clk *clk;
13899c4a634SDavid S. Miller struct at91_can_data *pdata;
1393a5655a5SMarc Kleine-Budde
1403a5655a5SMarc Kleine-Budde canid_t mb0_id;
14199c4a634SDavid S. Miller };
14299c4a634SDavid S. Miller
1433078cde7SLudovic Desroches static const struct at91_devtype_data at91_at91sam9263_data = {
144d3d47264SMarc Kleine-Budde .rx_first = 1,
145d3d47264SMarc Kleine-Budde .rx_split = 8,
146d3d47264SMarc Kleine-Budde .rx_last = 11,
147d3d47264SMarc Kleine-Budde .tx_shift = 2,
1483078cde7SLudovic Desroches .type = AT91_DEVTYPE_SAM9263,
1493078cde7SLudovic Desroches };
1503078cde7SLudovic Desroches
1513078cde7SLudovic Desroches static const struct at91_devtype_data at91_at91sam9x5_data = {
1526388b396SMarc Kleine-Budde .rx_first = 0,
1536388b396SMarc Kleine-Budde .rx_split = 4,
1546388b396SMarc Kleine-Budde .rx_last = 5,
1556388b396SMarc Kleine-Budde .tx_shift = 1,
1563078cde7SLudovic Desroches .type = AT91_DEVTYPE_SAM9X5,
157d3d47264SMarc Kleine-Budde };
158d3d47264SMarc Kleine-Budde
159194b9a4cSMarc Kleine-Budde static const struct can_bittiming_const at91_bittiming_const = {
16000389b08SMarc Kleine-Budde .name = KBUILD_MODNAME,
16199c4a634SDavid S. Miller .tseg1_min = 4,
16299c4a634SDavid S. Miller .tseg1_max = 16,
16399c4a634SDavid S. Miller .tseg2_min = 2,
16499c4a634SDavid S. Miller .tseg2_max = 8,
16599c4a634SDavid S. Miller .sjw_max = 4,
16699c4a634SDavid S. Miller .brp_min = 2,
16799c4a634SDavid S. Miller .brp_max = 128,
16899c4a634SDavid S. Miller .brp_inc = 1,
16999c4a634SDavid S. Miller };
17099c4a634SDavid S. Miller
171d3d47264SMarc Kleine-Budde #define AT91_IS(_model) \
1724318b1aaSMarc Kleine-Budde static inline int __maybe_unused at91_is_sam##_model(const struct at91_priv *priv) \
173d3d47264SMarc Kleine-Budde { \
174d3d47264SMarc Kleine-Budde return priv->devtype_data.type == AT91_DEVTYPE_SAM##_model; \
175d3d47264SMarc Kleine-Budde }
176d3d47264SMarc Kleine-Budde
177d3d47264SMarc Kleine-Budde AT91_IS(9263);
1786388b396SMarc Kleine-Budde AT91_IS(9X5);
179d3d47264SMarc Kleine-Budde
get_mb_rx_first(const struct at91_priv * priv)180d3d47264SMarc Kleine-Budde static inline unsigned int get_mb_rx_first(const struct at91_priv *priv)
181d3d47264SMarc Kleine-Budde {
182d3d47264SMarc Kleine-Budde return priv->devtype_data.rx_first;
183d3d47264SMarc Kleine-Budde }
184d3d47264SMarc Kleine-Budde
get_mb_rx_last(const struct at91_priv * priv)185d3d47264SMarc Kleine-Budde static inline unsigned int get_mb_rx_last(const struct at91_priv *priv)
186d3d47264SMarc Kleine-Budde {
187d3d47264SMarc Kleine-Budde return priv->devtype_data.rx_last;
188d3d47264SMarc Kleine-Budde }
189d3d47264SMarc Kleine-Budde
get_mb_rx_split(const struct at91_priv * priv)190d3d47264SMarc Kleine-Budde static inline unsigned int get_mb_rx_split(const struct at91_priv *priv)
191d3d47264SMarc Kleine-Budde {
192d3d47264SMarc Kleine-Budde return priv->devtype_data.rx_split;
193d3d47264SMarc Kleine-Budde }
194d3d47264SMarc Kleine-Budde
get_mb_rx_num(const struct at91_priv * priv)195d3d47264SMarc Kleine-Budde static inline unsigned int get_mb_rx_num(const struct at91_priv *priv)
196d3d47264SMarc Kleine-Budde {
197d3d47264SMarc Kleine-Budde return get_mb_rx_last(priv) - get_mb_rx_first(priv) + 1;
198d3d47264SMarc Kleine-Budde }
199d3d47264SMarc Kleine-Budde
get_mb_rx_low_last(const struct at91_priv * priv)20079008997SMarc Kleine-Budde static inline unsigned int get_mb_rx_low_last(const struct at91_priv *priv)
20179008997SMarc Kleine-Budde {
202d3d47264SMarc Kleine-Budde return get_mb_rx_split(priv) - 1;
20379008997SMarc Kleine-Budde }
20479008997SMarc Kleine-Budde
get_mb_rx_low_mask(const struct at91_priv * priv)20579008997SMarc Kleine-Budde static inline unsigned int get_mb_rx_low_mask(const struct at91_priv *priv)
20679008997SMarc Kleine-Budde {
207d3d47264SMarc Kleine-Budde return AT91_MB_MASK(get_mb_rx_split(priv)) &
208d3d47264SMarc Kleine-Budde ~AT91_MB_MASK(get_mb_rx_first(priv));
209d3d47264SMarc Kleine-Budde }
210d3d47264SMarc Kleine-Budde
get_mb_tx_shift(const struct at91_priv * priv)211d3d47264SMarc Kleine-Budde static inline unsigned int get_mb_tx_shift(const struct at91_priv *priv)
212d3d47264SMarc Kleine-Budde {
213d3d47264SMarc Kleine-Budde return priv->devtype_data.tx_shift;
21479008997SMarc Kleine-Budde }
21579008997SMarc Kleine-Budde
get_mb_tx_num(const struct at91_priv * priv)21679008997SMarc Kleine-Budde static inline unsigned int get_mb_tx_num(const struct at91_priv *priv)
21779008997SMarc Kleine-Budde {
218d3d47264SMarc Kleine-Budde return 1 << get_mb_tx_shift(priv);
21979008997SMarc Kleine-Budde }
22079008997SMarc Kleine-Budde
get_mb_tx_first(const struct at91_priv * priv)22179008997SMarc Kleine-Budde static inline unsigned int get_mb_tx_first(const struct at91_priv *priv)
22279008997SMarc Kleine-Budde {
223d3d47264SMarc Kleine-Budde return get_mb_rx_last(priv) + 1;
22479008997SMarc Kleine-Budde }
22579008997SMarc Kleine-Budde
get_mb_tx_last(const struct at91_priv * priv)22679008997SMarc Kleine-Budde static inline unsigned int get_mb_tx_last(const struct at91_priv *priv)
22779008997SMarc Kleine-Budde {
22879008997SMarc Kleine-Budde return get_mb_tx_first(priv) + get_mb_tx_num(priv) - 1;
22979008997SMarc Kleine-Budde }
23079008997SMarc Kleine-Budde
get_next_prio_shift(const struct at91_priv * priv)23179008997SMarc Kleine-Budde static inline unsigned int get_next_prio_shift(const struct at91_priv *priv)
23279008997SMarc Kleine-Budde {
233d3d47264SMarc Kleine-Budde return get_mb_tx_shift(priv);
23479008997SMarc Kleine-Budde }
23579008997SMarc Kleine-Budde
get_next_prio_mask(const struct at91_priv * priv)23679008997SMarc Kleine-Budde static inline unsigned int get_next_prio_mask(const struct at91_priv *priv)
23779008997SMarc Kleine-Budde {
238d3d47264SMarc Kleine-Budde return 0xf << get_mb_tx_shift(priv);
23979008997SMarc Kleine-Budde }
24079008997SMarc Kleine-Budde
get_next_mb_mask(const struct at91_priv * priv)24179008997SMarc Kleine-Budde static inline unsigned int get_next_mb_mask(const struct at91_priv *priv)
24279008997SMarc Kleine-Budde {
243d3d47264SMarc Kleine-Budde return AT91_MB_MASK(get_mb_tx_shift(priv));
24479008997SMarc Kleine-Budde }
24579008997SMarc Kleine-Budde
get_next_mask(const struct at91_priv * priv)24679008997SMarc Kleine-Budde static inline unsigned int get_next_mask(const struct at91_priv *priv)
24779008997SMarc Kleine-Budde {
24879008997SMarc Kleine-Budde return get_next_mb_mask(priv) | get_next_prio_mask(priv);
24979008997SMarc Kleine-Budde }
25079008997SMarc Kleine-Budde
get_irq_mb_rx(const struct at91_priv * priv)25179008997SMarc Kleine-Budde static inline unsigned int get_irq_mb_rx(const struct at91_priv *priv)
25279008997SMarc Kleine-Budde {
253d3d47264SMarc Kleine-Budde return AT91_MB_MASK(get_mb_rx_last(priv) + 1) &
254d3d47264SMarc Kleine-Budde ~AT91_MB_MASK(get_mb_rx_first(priv));
25579008997SMarc Kleine-Budde }
25679008997SMarc Kleine-Budde
get_irq_mb_tx(const struct at91_priv * priv)25779008997SMarc Kleine-Budde static inline unsigned int get_irq_mb_tx(const struct at91_priv *priv)
25879008997SMarc Kleine-Budde {
25979008997SMarc Kleine-Budde return AT91_MB_MASK(get_mb_tx_last(priv) + 1) &
26079008997SMarc Kleine-Budde ~AT91_MB_MASK(get_mb_tx_first(priv));
26179008997SMarc Kleine-Budde }
26279008997SMarc Kleine-Budde
get_tx_next_mb(const struct at91_priv * priv)2639c2e0a6dSMarc Kleine-Budde static inline unsigned int get_tx_next_mb(const struct at91_priv *priv)
26499c4a634SDavid S. Miller {
26579008997SMarc Kleine-Budde return (priv->tx_next & get_next_mb_mask(priv)) + get_mb_tx_first(priv);
26699c4a634SDavid S. Miller }
26799c4a634SDavid S. Miller
get_tx_next_prio(const struct at91_priv * priv)2689c2e0a6dSMarc Kleine-Budde static inline unsigned int get_tx_next_prio(const struct at91_priv *priv)
26999c4a634SDavid S. Miller {
27079008997SMarc Kleine-Budde return (priv->tx_next >> get_next_prio_shift(priv)) & 0xf;
27199c4a634SDavid S. Miller }
27299c4a634SDavid S. Miller
get_tx_echo_mb(const struct at91_priv * priv)2739c2e0a6dSMarc Kleine-Budde static inline unsigned int get_tx_echo_mb(const struct at91_priv *priv)
27499c4a634SDavid S. Miller {
27579008997SMarc Kleine-Budde return (priv->tx_echo & get_next_mb_mask(priv)) + get_mb_tx_first(priv);
27699c4a634SDavid S. Miller }
27799c4a634SDavid S. Miller
at91_read(const struct at91_priv * priv,enum at91_reg reg)27899c4a634SDavid S. Miller static inline u32 at91_read(const struct at91_priv *priv, enum at91_reg reg)
27999c4a634SDavid S. Miller {
280af9bfbdaSBen Dooks return readl_relaxed(priv->reg_base + reg);
28199c4a634SDavid S. Miller }
28299c4a634SDavid S. Miller
at91_write(const struct at91_priv * priv,enum at91_reg reg,u32 value)28399c4a634SDavid S. Miller static inline void at91_write(const struct at91_priv *priv, enum at91_reg reg,
28499c4a634SDavid S. Miller u32 value)
28599c4a634SDavid S. Miller {
286af9bfbdaSBen Dooks writel_relaxed(value, priv->reg_base + reg);
28799c4a634SDavid S. Miller }
28899c4a634SDavid S. Miller
set_mb_mode_prio(const struct at91_priv * priv,unsigned int mb,enum at91_mb_mode mode,int prio)28999c4a634SDavid S. Miller static inline void set_mb_mode_prio(const struct at91_priv *priv,
290ccc5f1c9SPeng Li unsigned int mb, enum at91_mb_mode mode,
291ccc5f1c9SPeng Li int prio)
29299c4a634SDavid S. Miller {
29399c4a634SDavid S. Miller at91_write(priv, AT91_MMR(mb), (mode << 24) | (prio << 16));
29499c4a634SDavid S. Miller }
29599c4a634SDavid S. Miller
set_mb_mode(const struct at91_priv * priv,unsigned int mb,enum at91_mb_mode mode)29699c4a634SDavid S. Miller static inline void set_mb_mode(const struct at91_priv *priv, unsigned int mb,
29799c4a634SDavid S. Miller enum at91_mb_mode mode)
29899c4a634SDavid S. Miller {
29999c4a634SDavid S. Miller set_mb_mode_prio(priv, mb, mode, 0);
30099c4a634SDavid S. Miller }
30199c4a634SDavid S. Miller
at91_can_id_to_reg_mid(canid_t can_id)3023a5655a5SMarc Kleine-Budde static inline u32 at91_can_id_to_reg_mid(canid_t can_id)
3033a5655a5SMarc Kleine-Budde {
3043a5655a5SMarc Kleine-Budde u32 reg_mid;
3053a5655a5SMarc Kleine-Budde
3063a5655a5SMarc Kleine-Budde if (can_id & CAN_EFF_FLAG)
3073a5655a5SMarc Kleine-Budde reg_mid = (can_id & CAN_EFF_MASK) | AT91_MID_MIDE;
3083a5655a5SMarc Kleine-Budde else
3093a5655a5SMarc Kleine-Budde reg_mid = (can_id & CAN_SFF_MASK) << 18;
3103a5655a5SMarc Kleine-Budde
3113a5655a5SMarc Kleine-Budde return reg_mid;
3123a5655a5SMarc Kleine-Budde }
3133a5655a5SMarc Kleine-Budde
at91_setup_mailboxes(struct net_device * dev)31499c4a634SDavid S. Miller static void at91_setup_mailboxes(struct net_device *dev)
31599c4a634SDavid S. Miller {
31699c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev);
31799c4a634SDavid S. Miller unsigned int i;
3183a5655a5SMarc Kleine-Budde u32 reg_mid;
31999c4a634SDavid S. Miller
3205bbe6049SPeng Li /* Due to a chip bug (errata 50.2.6.3 & 50.3.5.3) the first
3219e0a2d1cSMarc Kleine-Budde * mailbox is disabled. The next 11 mailboxes are used as a
3229e0a2d1cSMarc Kleine-Budde * reception FIFO. The last mailbox is configured with
3239e0a2d1cSMarc Kleine-Budde * overwrite option. The overwrite flag indicates a FIFO
3249e0a2d1cSMarc Kleine-Budde * overflow.
32599c4a634SDavid S. Miller */
3263a5655a5SMarc Kleine-Budde reg_mid = at91_can_id_to_reg_mid(priv->mb0_id);
327d3d47264SMarc Kleine-Budde for (i = 0; i < get_mb_rx_first(priv); i++) {
3289e0a2d1cSMarc Kleine-Budde set_mb_mode(priv, i, AT91_MB_MODE_DISABLED);
3293a5655a5SMarc Kleine-Budde at91_write(priv, AT91_MID(i), reg_mid);
3303a5655a5SMarc Kleine-Budde at91_write(priv, AT91_MCR(i), 0x0); /* clear dlc */
3313a5655a5SMarc Kleine-Budde }
3323a5655a5SMarc Kleine-Budde
333d3d47264SMarc Kleine-Budde for (i = get_mb_rx_first(priv); i < get_mb_rx_last(priv); i++)
33499c4a634SDavid S. Miller set_mb_mode(priv, i, AT91_MB_MODE_RX);
335d3d47264SMarc Kleine-Budde set_mb_mode(priv, get_mb_rx_last(priv), AT91_MB_MODE_RX_OVRWR);
33699c4a634SDavid S. Miller
3378a0e0a49SMarc Kleine-Budde /* reset acceptance mask and id register */
338d3d47264SMarc Kleine-Budde for (i = get_mb_rx_first(priv); i <= get_mb_rx_last(priv); i++) {
3398a0e0a49SMarc Kleine-Budde at91_write(priv, AT91_MAM(i), 0x0);
3408a0e0a49SMarc Kleine-Budde at91_write(priv, AT91_MID(i), AT91_MID_MIDE);
3418a0e0a49SMarc Kleine-Budde }
3428a0e0a49SMarc Kleine-Budde
34399c4a634SDavid S. Miller /* The last 4 mailboxes are used for transmitting. */
34479008997SMarc Kleine-Budde for (i = get_mb_tx_first(priv); i <= get_mb_tx_last(priv); i++)
34599c4a634SDavid S. Miller set_mb_mode_prio(priv, i, AT91_MB_MODE_TX, 0);
34699c4a634SDavid S. Miller
34799c4a634SDavid S. Miller /* Reset tx and rx helper pointers */
3480909c1ecSMarc Kleine-Budde priv->tx_next = priv->tx_echo = 0;
349d3d47264SMarc Kleine-Budde priv->rx_next = get_mb_rx_first(priv);
35099c4a634SDavid S. Miller }
35199c4a634SDavid S. Miller
at91_set_bittiming(struct net_device * dev)35299c4a634SDavid S. Miller static int at91_set_bittiming(struct net_device *dev)
35399c4a634SDavid S. Miller {
35499c4a634SDavid S. Miller const struct at91_priv *priv = netdev_priv(dev);
35599c4a634SDavid S. Miller const struct can_bittiming *bt = &priv->can.bittiming;
35699c4a634SDavid S. Miller u32 reg_br;
35799c4a634SDavid S. Miller
358dbe91325SMarc Kleine-Budde reg_br = ((priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) ? 1 << 24 : 0) |
35999c4a634SDavid S. Miller ((bt->brp - 1) << 16) | ((bt->sjw - 1) << 12) |
36099c4a634SDavid S. Miller ((bt->prop_seg - 1) << 8) | ((bt->phase_seg1 - 1) << 4) |
36199c4a634SDavid S. Miller ((bt->phase_seg2 - 1) << 0);
36299c4a634SDavid S. Miller
363882055c8SMarc Kleine-Budde netdev_info(dev, "writing AT91_BR: 0x%08x\n", reg_br);
36499c4a634SDavid S. Miller
36599c4a634SDavid S. Miller at91_write(priv, AT91_BR, reg_br);
36699c4a634SDavid S. Miller
36799c4a634SDavid S. Miller return 0;
36899c4a634SDavid S. Miller }
36999c4a634SDavid S. Miller
at91_get_berr_counter(const struct net_device * dev,struct can_berr_counter * bec)37033a6f298SMarc Kleine-Budde static int at91_get_berr_counter(const struct net_device *dev,
37133a6f298SMarc Kleine-Budde struct can_berr_counter *bec)
37233a6f298SMarc Kleine-Budde {
37333a6f298SMarc Kleine-Budde const struct at91_priv *priv = netdev_priv(dev);
37433a6f298SMarc Kleine-Budde u32 reg_ecr = at91_read(priv, AT91_ECR);
37533a6f298SMarc Kleine-Budde
37633a6f298SMarc Kleine-Budde bec->rxerr = reg_ecr & 0xff;
37733a6f298SMarc Kleine-Budde bec->txerr = reg_ecr >> 16;
37833a6f298SMarc Kleine-Budde
37933a6f298SMarc Kleine-Budde return 0;
38033a6f298SMarc Kleine-Budde }
38133a6f298SMarc Kleine-Budde
at91_chip_start(struct net_device * dev)38299c4a634SDavid S. Miller static void at91_chip_start(struct net_device *dev)
38399c4a634SDavid S. Miller {
38499c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev);
38599c4a634SDavid S. Miller u32 reg_mr, reg_ier;
38699c4a634SDavid S. Miller
38799c4a634SDavid S. Miller /* disable interrupts */
38899c4a634SDavid S. Miller at91_write(priv, AT91_IDR, AT91_IRQ_ALL);
38999c4a634SDavid S. Miller
39099c4a634SDavid S. Miller /* disable chip */
39199c4a634SDavid S. Miller reg_mr = at91_read(priv, AT91_MR);
39299c4a634SDavid S. Miller at91_write(priv, AT91_MR, reg_mr & ~AT91_MR_CANEN);
39399c4a634SDavid S. Miller
394b156fd04SMarc Kleine-Budde at91_set_bittiming(dev);
39599c4a634SDavid S. Miller at91_setup_mailboxes(dev);
39699c4a634SDavid S. Miller
39799c4a634SDavid S. Miller /* enable chip */
39817a50ee4SYoann DI RUZZA if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
39917a50ee4SYoann DI RUZZA reg_mr = AT91_MR_CANEN | AT91_MR_ABM;
40017a50ee4SYoann DI RUZZA else
40117a50ee4SYoann DI RUZZA reg_mr = AT91_MR_CANEN;
40217a50ee4SYoann DI RUZZA at91_write(priv, AT91_MR, reg_mr);
40399c4a634SDavid S. Miller
40499c4a634SDavid S. Miller priv->can.state = CAN_STATE_ERROR_ACTIVE;
40599c4a634SDavid S. Miller
40699c4a634SDavid S. Miller /* Enable interrupts */
40779008997SMarc Kleine-Budde reg_ier = get_irq_mb_rx(priv) | AT91_IRQ_ERRP | AT91_IRQ_ERR_FRAME;
40899c4a634SDavid S. Miller at91_write(priv, AT91_IDR, AT91_IRQ_ALL);
40999c4a634SDavid S. Miller at91_write(priv, AT91_IER, reg_ier);
41099c4a634SDavid S. Miller }
41199c4a634SDavid S. Miller
at91_chip_stop(struct net_device * dev,enum can_state state)41299c4a634SDavid S. Miller static void at91_chip_stop(struct net_device *dev, enum can_state state)
41399c4a634SDavid S. Miller {
41499c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev);
41599c4a634SDavid S. Miller u32 reg_mr;
41699c4a634SDavid S. Miller
41799c4a634SDavid S. Miller /* disable interrupts */
41899c4a634SDavid S. Miller at91_write(priv, AT91_IDR, AT91_IRQ_ALL);
41999c4a634SDavid S. Miller
42099c4a634SDavid S. Miller reg_mr = at91_read(priv, AT91_MR);
42199c4a634SDavid S. Miller at91_write(priv, AT91_MR, reg_mr & ~AT91_MR_CANEN);
42299c4a634SDavid S. Miller
42399c4a634SDavid S. Miller priv->can.state = state;
42499c4a634SDavid S. Miller }
42599c4a634SDavid S. Miller
4265bbe6049SPeng Li /* theory of operation:
42799c4a634SDavid S. Miller *
42899c4a634SDavid S. Miller * According to the datasheet priority 0 is the highest priority, 15
42999c4a634SDavid S. Miller * is the lowest. If two mailboxes have the same priority level the
43099c4a634SDavid S. Miller * message of the mailbox with the lowest number is sent first.
43199c4a634SDavid S. Miller *
43299c4a634SDavid S. Miller * We use the first TX mailbox (AT91_MB_TX_FIRST) with prio 0, then
43399c4a634SDavid S. Miller * the next mailbox with prio 0, and so on, until all mailboxes are
43499c4a634SDavid S. Miller * used. Then we start from the beginning with mailbox
43599c4a634SDavid S. Miller * AT91_MB_TX_FIRST, but with prio 1, mailbox AT91_MB_TX_FIRST + 1
43699c4a634SDavid S. Miller * prio 1. When we reach the last mailbox with prio 15, we have to
43799c4a634SDavid S. Miller * stop sending, waiting for all messages to be delivered, then start
43899c4a634SDavid S. Miller * again with mailbox AT91_MB_TX_FIRST prio 0.
43999c4a634SDavid S. Miller *
44099c4a634SDavid S. Miller * We use the priv->tx_next as counter for the next transmission
44199c4a634SDavid S. Miller * mailbox, but without the offset AT91_MB_TX_FIRST. The lower bits
44299c4a634SDavid S. Miller * encode the mailbox number, the upper 4 bits the mailbox priority:
44399c4a634SDavid S. Miller *
444d3d47264SMarc Kleine-Budde * priv->tx_next = (prio << get_next_prio_shift(priv)) |
445d3d47264SMarc Kleine-Budde * (mb - get_mb_tx_first(priv));
44699c4a634SDavid S. Miller *
44799c4a634SDavid S. Miller */
at91_start_xmit(struct sk_buff * skb,struct net_device * dev)44899c4a634SDavid S. Miller static netdev_tx_t at91_start_xmit(struct sk_buff *skb, struct net_device *dev)
44999c4a634SDavid S. Miller {
45099c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev);
45199c4a634SDavid S. Miller struct can_frame *cf = (struct can_frame *)skb->data;
45299c4a634SDavid S. Miller unsigned int mb, prio;
45399c4a634SDavid S. Miller u32 reg_mid, reg_mcr;
45499c4a634SDavid S. Miller
455ae64438bSOliver Hartkopp if (can_dev_dropped_skb(dev, skb))
4563ccd4c61SOliver Hartkopp return NETDEV_TX_OK;
4573ccd4c61SOliver Hartkopp
45899c4a634SDavid S. Miller mb = get_tx_next_mb(priv);
45999c4a634SDavid S. Miller prio = get_tx_next_prio(priv);
46099c4a634SDavid S. Miller
46199c4a634SDavid S. Miller if (unlikely(!(at91_read(priv, AT91_MSR(mb)) & AT91_MSR_MRDY))) {
46299c4a634SDavid S. Miller netif_stop_queue(dev);
46399c4a634SDavid S. Miller
464882055c8SMarc Kleine-Budde netdev_err(dev, "BUG! TX buffer full when queue awake!\n");
46599c4a634SDavid S. Miller return NETDEV_TX_BUSY;
46699c4a634SDavid S. Miller }
4673a5655a5SMarc Kleine-Budde reg_mid = at91_can_id_to_reg_mid(cf->can_id);
46899c4a634SDavid S. Miller reg_mcr = ((cf->can_id & CAN_RTR_FLAG) ? AT91_MCR_MRTR : 0) |
469c7b74967SOliver Hartkopp (cf->len << 16) | AT91_MCR_MTCR;
47099c4a634SDavid S. Miller
47199c4a634SDavid S. Miller /* disable MB while writing ID (see datasheet) */
47299c4a634SDavid S. Miller set_mb_mode(priv, mb, AT91_MB_MODE_DISABLED);
47399c4a634SDavid S. Miller at91_write(priv, AT91_MID(mb), reg_mid);
47499c4a634SDavid S. Miller set_mb_mode_prio(priv, mb, AT91_MB_MODE_TX, prio);
47599c4a634SDavid S. Miller
47699c4a634SDavid S. Miller at91_write(priv, AT91_MDL(mb), *(u32 *)(cf->data + 0));
47799c4a634SDavid S. Miller at91_write(priv, AT91_MDH(mb), *(u32 *)(cf->data + 4));
47899c4a634SDavid S. Miller
47999c4a634SDavid S. Miller /* This triggers transmission */
48099c4a634SDavid S. Miller at91_write(priv, AT91_MCR(mb), reg_mcr);
48199c4a634SDavid S. Miller
48225985edcSLucas De Marchi /* _NOTE_: subtract AT91_MB_TX_FIRST offset from mb! */
4831dcb6e57SVincent Mailhol can_put_echo_skb(skb, dev, mb - get_mb_tx_first(priv), 0);
48499c4a634SDavid S. Miller
4855bbe6049SPeng Li /* we have to stop the queue and deliver all messages in case
48699c4a634SDavid S. Miller * of a prio+mb counter wrap around. This is the case if
48799c4a634SDavid S. Miller * tx_next buffer prio and mailbox equals 0.
48899c4a634SDavid S. Miller *
48999c4a634SDavid S. Miller * also stop the queue if next buffer is still in use
49099c4a634SDavid S. Miller * (== not ready)
49199c4a634SDavid S. Miller */
49299c4a634SDavid S. Miller priv->tx_next++;
49399c4a634SDavid S. Miller if (!(at91_read(priv, AT91_MSR(get_tx_next_mb(priv))) &
49499c4a634SDavid S. Miller AT91_MSR_MRDY) ||
49579008997SMarc Kleine-Budde (priv->tx_next & get_next_mask(priv)) == 0)
49699c4a634SDavid S. Miller netif_stop_queue(dev);
49799c4a634SDavid S. Miller
49899c4a634SDavid S. Miller /* Enable interrupt for this mailbox */
49999c4a634SDavid S. Miller at91_write(priv, AT91_IER, 1 << mb);
50099c4a634SDavid S. Miller
50199c4a634SDavid S. Miller return NETDEV_TX_OK;
50299c4a634SDavid S. Miller }
50399c4a634SDavid S. Miller
50499c4a634SDavid S. Miller /**
50599c4a634SDavid S. Miller * at91_activate_rx_low - activate lower rx mailboxes
50699c4a634SDavid S. Miller * @priv: a91 context
50799c4a634SDavid S. Miller *
50899c4a634SDavid S. Miller * Reenables the lower mailboxes for reception of new CAN messages
50999c4a634SDavid S. Miller */
at91_activate_rx_low(const struct at91_priv * priv)51099c4a634SDavid S. Miller static inline void at91_activate_rx_low(const struct at91_priv *priv)
51199c4a634SDavid S. Miller {
51279008997SMarc Kleine-Budde u32 mask = get_mb_rx_low_mask(priv);
513933850c4SPeng Li
51499c4a634SDavid S. Miller at91_write(priv, AT91_TCR, mask);
51599c4a634SDavid S. Miller }
51699c4a634SDavid S. Miller
51799c4a634SDavid S. Miller /**
51899c4a634SDavid S. Miller * at91_activate_rx_mb - reactive single rx mailbox
51999c4a634SDavid S. Miller * @priv: a91 context
52099c4a634SDavid S. Miller * @mb: mailbox to reactivate
52199c4a634SDavid S. Miller *
52299c4a634SDavid S. Miller * Reenables given mailbox for reception of new CAN messages
52399c4a634SDavid S. Miller */
at91_activate_rx_mb(const struct at91_priv * priv,unsigned int mb)52499c4a634SDavid S. Miller static inline void at91_activate_rx_mb(const struct at91_priv *priv,
52599c4a634SDavid S. Miller unsigned int mb)
52699c4a634SDavid S. Miller {
52799c4a634SDavid S. Miller u32 mask = 1 << mb;
528933850c4SPeng Li
52999c4a634SDavid S. Miller at91_write(priv, AT91_TCR, mask);
53099c4a634SDavid S. Miller }
53199c4a634SDavid S. Miller
53299c4a634SDavid S. Miller /**
53399c4a634SDavid S. Miller * at91_rx_overflow_err - send error frame due to rx overflow
53499c4a634SDavid S. Miller * @dev: net device
53599c4a634SDavid S. Miller */
at91_rx_overflow_err(struct net_device * dev)53699c4a634SDavid S. Miller static void at91_rx_overflow_err(struct net_device *dev)
53799c4a634SDavid S. Miller {
53899c4a634SDavid S. Miller struct net_device_stats *stats = &dev->stats;
53999c4a634SDavid S. Miller struct sk_buff *skb;
54099c4a634SDavid S. Miller struct can_frame *cf;
54199c4a634SDavid S. Miller
542882055c8SMarc Kleine-Budde netdev_dbg(dev, "RX buffer overflow\n");
54399c4a634SDavid S. Miller stats->rx_over_errors++;
54499c4a634SDavid S. Miller stats->rx_errors++;
54599c4a634SDavid S. Miller
54699c4a634SDavid S. Miller skb = alloc_can_err_skb(dev, &cf);
54799c4a634SDavid S. Miller if (unlikely(!skb))
54899c4a634SDavid S. Miller return;
54999c4a634SDavid S. Miller
55099c4a634SDavid S. Miller cf->can_id |= CAN_ERR_CRTL;
55199c4a634SDavid S. Miller cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
55299c4a634SDavid S. Miller
5536ae3673dSMarc Kleine-Budde netif_receive_skb(skb);
55499c4a634SDavid S. Miller }
55599c4a634SDavid S. Miller
55699c4a634SDavid S. Miller /**
55799c4a634SDavid S. Miller * at91_read_mb - read CAN msg from mailbox (lowlevel impl)
55899c4a634SDavid S. Miller * @dev: net device
55999c4a634SDavid S. Miller * @mb: mailbox number to read from
56099c4a634SDavid S. Miller * @cf: can frame where to store message
56199c4a634SDavid S. Miller *
56299c4a634SDavid S. Miller * Reads a CAN message from the given mailbox and stores data into
56399c4a634SDavid S. Miller * given can frame. "mb" and "cf" must be valid.
56499c4a634SDavid S. Miller */
at91_read_mb(struct net_device * dev,unsigned int mb,struct can_frame * cf)56599c4a634SDavid S. Miller static void at91_read_mb(struct net_device *dev, unsigned int mb,
56699c4a634SDavid S. Miller struct can_frame *cf)
56799c4a634SDavid S. Miller {
56899c4a634SDavid S. Miller const struct at91_priv *priv = netdev_priv(dev);
56999c4a634SDavid S. Miller u32 reg_msr, reg_mid;
57099c4a634SDavid S. Miller
57199c4a634SDavid S. Miller reg_mid = at91_read(priv, AT91_MID(mb));
57299c4a634SDavid S. Miller if (reg_mid & AT91_MID_MIDE)
57399c4a634SDavid S. Miller cf->can_id = ((reg_mid >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG;
57499c4a634SDavid S. Miller else
57599c4a634SDavid S. Miller cf->can_id = (reg_mid >> 18) & CAN_SFF_MASK;
57699c4a634SDavid S. Miller
57799c4a634SDavid S. Miller reg_msr = at91_read(priv, AT91_MSR(mb));
578c7b74967SOliver Hartkopp cf->len = can_cc_dlc2len((reg_msr >> 16) & 0xf);
57999c4a634SDavid S. Miller
58002400533SPeng Li if (reg_msr & AT91_MSR_MRTR) {
581e14ee40bSMarc Kleine-Budde cf->can_id |= CAN_RTR_FLAG;
58202400533SPeng Li } else {
58399c4a634SDavid S. Miller *(u32 *)(cf->data + 0) = at91_read(priv, AT91_MDL(mb));
58499c4a634SDavid S. Miller *(u32 *)(cf->data + 4) = at91_read(priv, AT91_MDH(mb));
585e14ee40bSMarc Kleine-Budde }
58699c4a634SDavid S. Miller
5878a0e0a49SMarc Kleine-Budde /* allow RX of extended frames */
5888a0e0a49SMarc Kleine-Budde at91_write(priv, AT91_MID(mb), AT91_MID_MIDE);
5898a0e0a49SMarc Kleine-Budde
590d3d47264SMarc Kleine-Budde if (unlikely(mb == get_mb_rx_last(priv) && reg_msr & AT91_MSR_MMI))
59199c4a634SDavid S. Miller at91_rx_overflow_err(dev);
59299c4a634SDavid S. Miller }
59399c4a634SDavid S. Miller
59499c4a634SDavid S. Miller /**
59599c4a634SDavid S. Miller * at91_read_msg - read CAN message from mailbox
59699c4a634SDavid S. Miller * @dev: net device
59799c4a634SDavid S. Miller * @mb: mail box to read from
59899c4a634SDavid S. Miller *
59999c4a634SDavid S. Miller * Reads a CAN message from given mailbox, and put into linux network
60099c4a634SDavid S. Miller * RX queue, does all housekeeping chores (stats, ...)
60199c4a634SDavid S. Miller */
at91_read_msg(struct net_device * dev,unsigned int mb)60299c4a634SDavid S. Miller static void at91_read_msg(struct net_device *dev, unsigned int mb)
60399c4a634SDavid S. Miller {
60499c4a634SDavid S. Miller struct net_device_stats *stats = &dev->stats;
60599c4a634SDavid S. Miller struct can_frame *cf;
60699c4a634SDavid S. Miller struct sk_buff *skb;
60799c4a634SDavid S. Miller
60899c4a634SDavid S. Miller skb = alloc_can_skb(dev, &cf);
60999c4a634SDavid S. Miller if (unlikely(!skb)) {
61099c4a634SDavid S. Miller stats->rx_dropped++;
61199c4a634SDavid S. Miller return;
61299c4a634SDavid S. Miller }
61399c4a634SDavid S. Miller
61499c4a634SDavid S. Miller at91_read_mb(dev, mb, cf);
61599c4a634SDavid S. Miller
61699c4a634SDavid S. Miller stats->rx_packets++;
6178e674ca7SVincent Mailhol if (!(cf->can_id & CAN_RTR_FLAG))
618c7b74967SOliver Hartkopp stats->rx_bytes += cf->len;
6198e674ca7SVincent Mailhol
6206ae3673dSMarc Kleine-Budde netif_receive_skb(skb);
62199c4a634SDavid S. Miller }
62299c4a634SDavid S. Miller
62399c4a634SDavid S. Miller /**
62499c4a634SDavid S. Miller * at91_poll_rx - read multiple CAN messages from mailboxes
62599c4a634SDavid S. Miller * @dev: net device
62699c4a634SDavid S. Miller * @quota: max number of pkgs we're allowed to receive
62799c4a634SDavid S. Miller *
62899c4a634SDavid S. Miller * Theory of Operation:
62999c4a634SDavid S. Miller *
630d3d47264SMarc Kleine-Budde * About 3/4 of the mailboxes (get_mb_rx_first()...get_mb_rx_last())
631d3d47264SMarc Kleine-Budde * on the chip are reserved for RX. We split them into 2 groups. The
632d3d47264SMarc Kleine-Budde * lower group ranges from get_mb_rx_first() to get_mb_rx_low_last().
63399c4a634SDavid S. Miller *
63499c4a634SDavid S. Miller * Like it or not, but the chip always saves a received CAN message
63599c4a634SDavid S. Miller * into the first free mailbox it finds (starting with the
63699c4a634SDavid S. Miller * lowest). This makes it very difficult to read the messages in the
63799c4a634SDavid S. Miller * right order from the chip. This is how we work around that problem:
63899c4a634SDavid S. Miller *
6399e0a2d1cSMarc Kleine-Budde * The first message goes into mb nr. 1 and issues an interrupt. All
64099c4a634SDavid S. Miller * rx ints are disabled in the interrupt handler and a napi poll is
64188bfb9a7SMarc Kleine-Budde * scheduled. We read the mailbox, but do _not_ re-enable the mb (to
64299c4a634SDavid S. Miller * receive another message).
64399c4a634SDavid S. Miller *
64499c4a634SDavid S. Miller * lower mbxs upper
6459e0a2d1cSMarc Kleine-Budde * ____^______ __^__
64699c4a634SDavid S. Miller * / \ / \
64799c4a634SDavid S. Miller * +-+-+-+-+-+-+-+-++-+-+-+-+
6489e0a2d1cSMarc Kleine-Budde * | |x|x|x|x|x|x|x|| | | | |
64999c4a634SDavid S. Miller * +-+-+-+-+-+-+-+-++-+-+-+-+
65099c4a634SDavid S. Miller * 0 0 0 0 0 0 0 0 0 0 1 1 \ mail
65199c4a634SDavid S. Miller * 0 1 2 3 4 5 6 7 8 9 0 1 / box
6529e0a2d1cSMarc Kleine-Budde * ^
6539e0a2d1cSMarc Kleine-Budde * |
6549e0a2d1cSMarc Kleine-Budde * \
6559e0a2d1cSMarc Kleine-Budde * unused, due to chip bug
65699c4a634SDavid S. Miller *
65799c4a634SDavid S. Miller * The variable priv->rx_next points to the next mailbox to read a
65899c4a634SDavid S. Miller * message from. As long we're in the lower mailboxes we just read the
65988bfb9a7SMarc Kleine-Budde * mailbox but not re-enable it.
66099c4a634SDavid S. Miller *
66188bfb9a7SMarc Kleine-Budde * With completion of the last of the lower mailboxes, we re-enable the
66299c4a634SDavid S. Miller * whole first group, but continue to look for filled mailboxes in the
66399c4a634SDavid S. Miller * upper mailboxes. Imagine the second group like overflow mailboxes,
66499c4a634SDavid S. Miller * which takes CAN messages if the lower goup is full. While in the
66588bfb9a7SMarc Kleine-Budde * upper group we re-enable the mailbox right after reading it. Giving
66699c4a634SDavid S. Miller * the chip more room to store messages.
66799c4a634SDavid S. Miller *
66899c4a634SDavid S. Miller * After finishing we look again in the lower group if we've still
66999c4a634SDavid S. Miller * quota.
67099c4a634SDavid S. Miller *
67199c4a634SDavid S. Miller */
at91_poll_rx(struct net_device * dev,int quota)67299c4a634SDavid S. Miller static int at91_poll_rx(struct net_device *dev, int quota)
67399c4a634SDavid S. Miller {
67499c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev);
67599c4a634SDavid S. Miller u32 reg_sr = at91_read(priv, AT91_SR);
67699c4a634SDavid S. Miller const unsigned long *addr = (unsigned long *)®_sr;
67799c4a634SDavid S. Miller unsigned int mb;
67899c4a634SDavid S. Miller int received = 0;
67999c4a634SDavid S. Miller
68079008997SMarc Kleine-Budde if (priv->rx_next > get_mb_rx_low_last(priv) &&
68179008997SMarc Kleine-Budde reg_sr & get_mb_rx_low_mask(priv))
682882055c8SMarc Kleine-Budde netdev_info(dev,
68399c4a634SDavid S. Miller "order of incoming frames cannot be guaranteed\n");
68499c4a634SDavid S. Miller
68599c4a634SDavid S. Miller again:
68679008997SMarc Kleine-Budde for (mb = find_next_bit(addr, get_mb_tx_first(priv), priv->rx_next);
68779008997SMarc Kleine-Budde mb < get_mb_tx_first(priv) && quota > 0;
68899c4a634SDavid S. Miller reg_sr = at91_read(priv, AT91_SR),
68979008997SMarc Kleine-Budde mb = find_next_bit(addr, get_mb_tx_first(priv), ++priv->rx_next)) {
69099c4a634SDavid S. Miller at91_read_msg(dev, mb);
69199c4a634SDavid S. Miller
69299c4a634SDavid S. Miller /* reactivate mailboxes */
69379008997SMarc Kleine-Budde if (mb == get_mb_rx_low_last(priv))
69499c4a634SDavid S. Miller /* all lower mailboxed, if just finished it */
69599c4a634SDavid S. Miller at91_activate_rx_low(priv);
69679008997SMarc Kleine-Budde else if (mb > get_mb_rx_low_last(priv))
69799c4a634SDavid S. Miller /* only the mailbox we read */
69899c4a634SDavid S. Miller at91_activate_rx_mb(priv, mb);
69999c4a634SDavid S. Miller
70099c4a634SDavid S. Miller received++;
70199c4a634SDavid S. Miller quota--;
70299c4a634SDavid S. Miller }
70399c4a634SDavid S. Miller
70499c4a634SDavid S. Miller /* upper group completed, look again in lower */
70579008997SMarc Kleine-Budde if (priv->rx_next > get_mb_rx_low_last(priv) &&
70643200a44SWolfgang Grandegger mb > get_mb_rx_last(priv)) {
707d3d47264SMarc Kleine-Budde priv->rx_next = get_mb_rx_first(priv);
70843200a44SWolfgang Grandegger if (quota > 0)
70999c4a634SDavid S. Miller goto again;
71099c4a634SDavid S. Miller }
71199c4a634SDavid S. Miller
71299c4a634SDavid S. Miller return received;
71399c4a634SDavid S. Miller }
71499c4a634SDavid S. Miller
at91_poll_err_frame(struct net_device * dev,struct can_frame * cf,u32 reg_sr)71599c4a634SDavid S. Miller static void at91_poll_err_frame(struct net_device *dev,
71699c4a634SDavid S. Miller struct can_frame *cf, u32 reg_sr)
71799c4a634SDavid S. Miller {
71899c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev);
71999c4a634SDavid S. Miller
72099c4a634SDavid S. Miller /* CRC error */
72199c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_CERR) {
722882055c8SMarc Kleine-Budde netdev_dbg(dev, "CERR irq\n");
72399c4a634SDavid S. Miller dev->stats.rx_errors++;
72499c4a634SDavid S. Miller priv->can.can_stats.bus_error++;
72599c4a634SDavid S. Miller cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
72699c4a634SDavid S. Miller }
72799c4a634SDavid S. Miller
72899c4a634SDavid S. Miller /* Stuffing Error */
72999c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_SERR) {
730882055c8SMarc Kleine-Budde netdev_dbg(dev, "SERR irq\n");
73199c4a634SDavid S. Miller dev->stats.rx_errors++;
73299c4a634SDavid S. Miller priv->can.can_stats.bus_error++;
73399c4a634SDavid S. Miller cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
73499c4a634SDavid S. Miller cf->data[2] |= CAN_ERR_PROT_STUFF;
73599c4a634SDavid S. Miller }
73699c4a634SDavid S. Miller
73799c4a634SDavid S. Miller /* Acknowledgement Error */
73899c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_AERR) {
739882055c8SMarc Kleine-Budde netdev_dbg(dev, "AERR irq\n");
74099c4a634SDavid S. Miller dev->stats.tx_errors++;
74199c4a634SDavid S. Miller cf->can_id |= CAN_ERR_ACK;
74299c4a634SDavid S. Miller }
74399c4a634SDavid S. Miller
74499c4a634SDavid S. Miller /* Form error */
74599c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_FERR) {
746882055c8SMarc Kleine-Budde netdev_dbg(dev, "FERR irq\n");
74799c4a634SDavid S. Miller dev->stats.rx_errors++;
74899c4a634SDavid S. Miller priv->can.can_stats.bus_error++;
74999c4a634SDavid S. Miller cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
75099c4a634SDavid S. Miller cf->data[2] |= CAN_ERR_PROT_FORM;
75199c4a634SDavid S. Miller }
75299c4a634SDavid S. Miller
75399c4a634SDavid S. Miller /* Bit Error */
75499c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_BERR) {
755882055c8SMarc Kleine-Budde netdev_dbg(dev, "BERR irq\n");
75699c4a634SDavid S. Miller dev->stats.tx_errors++;
75799c4a634SDavid S. Miller priv->can.can_stats.bus_error++;
75899c4a634SDavid S. Miller cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
75999c4a634SDavid S. Miller cf->data[2] |= CAN_ERR_PROT_BIT;
76099c4a634SDavid S. Miller }
76199c4a634SDavid S. Miller }
76299c4a634SDavid S. Miller
at91_poll_err(struct net_device * dev,int quota,u32 reg_sr)76399c4a634SDavid S. Miller static int at91_poll_err(struct net_device *dev, int quota, u32 reg_sr)
76499c4a634SDavid S. Miller {
76599c4a634SDavid S. Miller struct sk_buff *skb;
76699c4a634SDavid S. Miller struct can_frame *cf;
76799c4a634SDavid S. Miller
76899c4a634SDavid S. Miller if (quota == 0)
76999c4a634SDavid S. Miller return 0;
77099c4a634SDavid S. Miller
77199c4a634SDavid S. Miller skb = alloc_can_err_skb(dev, &cf);
77299c4a634SDavid S. Miller if (unlikely(!skb))
77399c4a634SDavid S. Miller return 0;
77499c4a634SDavid S. Miller
77599c4a634SDavid S. Miller at91_poll_err_frame(dev, cf, reg_sr);
77699c4a634SDavid S. Miller
7776ae3673dSMarc Kleine-Budde netif_receive_skb(skb);
77899c4a634SDavid S. Miller
77999c4a634SDavid S. Miller return 1;
78099c4a634SDavid S. Miller }
78199c4a634SDavid S. Miller
at91_poll(struct napi_struct * napi,int quota)78299c4a634SDavid S. Miller static int at91_poll(struct napi_struct *napi, int quota)
78399c4a634SDavid S. Miller {
78499c4a634SDavid S. Miller struct net_device *dev = napi->dev;
78599c4a634SDavid S. Miller const struct at91_priv *priv = netdev_priv(dev);
78699c4a634SDavid S. Miller u32 reg_sr = at91_read(priv, AT91_SR);
78799c4a634SDavid S. Miller int work_done = 0;
78899c4a634SDavid S. Miller
78979008997SMarc Kleine-Budde if (reg_sr & get_irq_mb_rx(priv))
79099c4a634SDavid S. Miller work_done += at91_poll_rx(dev, quota - work_done);
79199c4a634SDavid S. Miller
7925bbe6049SPeng Li /* The error bits are clear on read,
79399c4a634SDavid S. Miller * so use saved value from irq handler.
79499c4a634SDavid S. Miller */
79599c4a634SDavid S. Miller reg_sr |= priv->reg_sr;
79699c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_ERR_FRAME)
79799c4a634SDavid S. Miller work_done += at91_poll_err(dev, quota - work_done, reg_sr);
79899c4a634SDavid S. Miller
79999c4a634SDavid S. Miller if (work_done < quota) {
80099c4a634SDavid S. Miller /* enable IRQs for frame errors and all mailboxes >= rx_next */
80199c4a634SDavid S. Miller u32 reg_ier = AT91_IRQ_ERR_FRAME;
802933850c4SPeng Li
80379008997SMarc Kleine-Budde reg_ier |= get_irq_mb_rx(priv) & ~AT91_MB_MASK(priv->rx_next);
80499c4a634SDavid S. Miller
8056ad20165SEric Dumazet napi_complete_done(napi, work_done);
80699c4a634SDavid S. Miller at91_write(priv, AT91_IER, reg_ier);
80799c4a634SDavid S. Miller }
80899c4a634SDavid S. Miller
80999c4a634SDavid S. Miller return work_done;
81099c4a634SDavid S. Miller }
81199c4a634SDavid S. Miller
8125bbe6049SPeng Li /* theory of operation:
81399c4a634SDavid S. Miller *
81499c4a634SDavid S. Miller * priv->tx_echo holds the number of the oldest can_frame put for
81599c4a634SDavid S. Miller * transmission into the hardware, but not yet ACKed by the CAN tx
81699c4a634SDavid S. Miller * complete IRQ.
81799c4a634SDavid S. Miller *
81899c4a634SDavid S. Miller * We iterate from priv->tx_echo to priv->tx_next and check if the
81999c4a634SDavid S. Miller * packet has been transmitted, echo it back to the CAN framework. If
82099c4a634SDavid S. Miller * we discover a not yet transmitted package, stop looking for more.
82199c4a634SDavid S. Miller *
82299c4a634SDavid S. Miller */
at91_irq_tx(struct net_device * dev,u32 reg_sr)82399c4a634SDavid S. Miller static void at91_irq_tx(struct net_device *dev, u32 reg_sr)
82499c4a634SDavid S. Miller {
82599c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev);
82699c4a634SDavid S. Miller u32 reg_msr;
82799c4a634SDavid S. Miller unsigned int mb;
82899c4a634SDavid S. Miller
82999c4a634SDavid S. Miller /* masking of reg_sr not needed, already done by at91_irq */
83099c4a634SDavid S. Miller
83199c4a634SDavid S. Miller for (/* nix */; (priv->tx_next - priv->tx_echo) > 0; priv->tx_echo++) {
83299c4a634SDavid S. Miller mb = get_tx_echo_mb(priv);
83399c4a634SDavid S. Miller
83499c4a634SDavid S. Miller /* no event in mailbox? */
83599c4a634SDavid S. Miller if (!(reg_sr & (1 << mb)))
83699c4a634SDavid S. Miller break;
83799c4a634SDavid S. Miller
83899c4a634SDavid S. Miller /* Disable irq for this TX mailbox */
83999c4a634SDavid S. Miller at91_write(priv, AT91_IDR, 1 << mb);
84099c4a634SDavid S. Miller
8415bbe6049SPeng Li /* only echo if mailbox signals us a transfer
84299c4a634SDavid S. Miller * complete (MSR_MRDY). Otherwise it's a tansfer
84399c4a634SDavid S. Miller * abort. "can_bus_off()" takes care about the skbs
84499c4a634SDavid S. Miller * parked in the echo queue.
84599c4a634SDavid S. Miller */
84699c4a634SDavid S. Miller reg_msr = at91_read(priv, AT91_MSR(mb));
84799c4a634SDavid S. Miller if (likely(reg_msr & AT91_MSR_MRDY &&
84899c4a634SDavid S. Miller ~reg_msr & AT91_MSR_MABT)) {
84925985edcSLucas De Marchi /* _NOTE_: subtract AT91_MB_TX_FIRST offset from mb! */
850cc4b08c3SVincent Mailhol dev->stats.tx_bytes +=
851cc4b08c3SVincent Mailhol can_get_echo_skb(dev,
852cc4b08c3SVincent Mailhol mb - get_mb_tx_first(priv),
853cc4b08c3SVincent Mailhol NULL);
85499c4a634SDavid S. Miller dev->stats.tx_packets++;
85599c4a634SDavid S. Miller }
85699c4a634SDavid S. Miller }
85799c4a634SDavid S. Miller
8585bbe6049SPeng Li /* restart queue if we don't have a wrap around but restart if
85999c4a634SDavid S. Miller * we get a TX int for the last can frame directly before a
86099c4a634SDavid S. Miller * wrap around.
86199c4a634SDavid S. Miller */
86279008997SMarc Kleine-Budde if ((priv->tx_next & get_next_mask(priv)) != 0 ||
86379008997SMarc Kleine-Budde (priv->tx_echo & get_next_mask(priv)) == 0)
86499c4a634SDavid S. Miller netif_wake_queue(dev);
86599c4a634SDavid S. Miller }
86699c4a634SDavid S. Miller
at91_irq_err_state(struct net_device * dev,struct can_frame * cf,enum can_state new_state)86799c4a634SDavid S. Miller static void at91_irq_err_state(struct net_device *dev,
86899c4a634SDavid S. Miller struct can_frame *cf, enum can_state new_state)
86999c4a634SDavid S. Miller {
87099c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev);
87133a6f298SMarc Kleine-Budde u32 reg_idr = 0, reg_ier = 0;
87233a6f298SMarc Kleine-Budde struct can_berr_counter bec;
87399c4a634SDavid S. Miller
87433a6f298SMarc Kleine-Budde at91_get_berr_counter(dev, &bec);
87599c4a634SDavid S. Miller
87699c4a634SDavid S. Miller switch (priv->can.state) {
87799c4a634SDavid S. Miller case CAN_STATE_ERROR_ACTIVE:
8785bbe6049SPeng Li /* from: ERROR_ACTIVE
87999c4a634SDavid S. Miller * to : ERROR_WARNING, ERROR_PASSIVE, BUS_OFF
88099c4a634SDavid S. Miller * => : there was a warning int
88199c4a634SDavid S. Miller */
88299c4a634SDavid S. Miller if (new_state >= CAN_STATE_ERROR_WARNING &&
88399c4a634SDavid S. Miller new_state <= CAN_STATE_BUS_OFF) {
884882055c8SMarc Kleine-Budde netdev_dbg(dev, "Error Warning IRQ\n");
88599c4a634SDavid S. Miller priv->can.can_stats.error_warning++;
88699c4a634SDavid S. Miller
88799c4a634SDavid S. Miller cf->can_id |= CAN_ERR_CRTL;
88833a6f298SMarc Kleine-Budde cf->data[1] = (bec.txerr > bec.rxerr) ?
88999c4a634SDavid S. Miller CAN_ERR_CRTL_TX_WARNING :
89099c4a634SDavid S. Miller CAN_ERR_CRTL_RX_WARNING;
89199c4a634SDavid S. Miller }
892df561f66SGustavo A. R. Silva fallthrough;
8935a8dadbcSGustavo A. R. Silva case CAN_STATE_ERROR_WARNING:
8945bbe6049SPeng Li /* from: ERROR_ACTIVE, ERROR_WARNING
89599c4a634SDavid S. Miller * to : ERROR_PASSIVE, BUS_OFF
89699c4a634SDavid S. Miller * => : error passive int
89799c4a634SDavid S. Miller */
89899c4a634SDavid S. Miller if (new_state >= CAN_STATE_ERROR_PASSIVE &&
89999c4a634SDavid S. Miller new_state <= CAN_STATE_BUS_OFF) {
900882055c8SMarc Kleine-Budde netdev_dbg(dev, "Error Passive IRQ\n");
90199c4a634SDavid S. Miller priv->can.can_stats.error_passive++;
90299c4a634SDavid S. Miller
90399c4a634SDavid S. Miller cf->can_id |= CAN_ERR_CRTL;
90433a6f298SMarc Kleine-Budde cf->data[1] = (bec.txerr > bec.rxerr) ?
90599c4a634SDavid S. Miller CAN_ERR_CRTL_TX_PASSIVE :
90699c4a634SDavid S. Miller CAN_ERR_CRTL_RX_PASSIVE;
90799c4a634SDavid S. Miller }
90899c4a634SDavid S. Miller break;
90999c4a634SDavid S. Miller case CAN_STATE_BUS_OFF:
9105bbe6049SPeng Li /* from: BUS_OFF
91199c4a634SDavid S. Miller * to : ERROR_ACTIVE, ERROR_WARNING, ERROR_PASSIVE
91299c4a634SDavid S. Miller */
91399c4a634SDavid S. Miller if (new_state <= CAN_STATE_ERROR_PASSIVE) {
91499c4a634SDavid S. Miller cf->can_id |= CAN_ERR_RESTARTED;
91599c4a634SDavid S. Miller
916882055c8SMarc Kleine-Budde netdev_dbg(dev, "restarted\n");
91799c4a634SDavid S. Miller priv->can.can_stats.restarts++;
91899c4a634SDavid S. Miller
91999c4a634SDavid S. Miller netif_carrier_on(dev);
92099c4a634SDavid S. Miller netif_wake_queue(dev);
92199c4a634SDavid S. Miller }
92299c4a634SDavid S. Miller break;
92399c4a634SDavid S. Miller default:
92499c4a634SDavid S. Miller break;
92599c4a634SDavid S. Miller }
92699c4a634SDavid S. Miller
92799c4a634SDavid S. Miller /* process state changes depending on the new state */
92899c4a634SDavid S. Miller switch (new_state) {
92999c4a634SDavid S. Miller case CAN_STATE_ERROR_ACTIVE:
9305bbe6049SPeng Li /* actually we want to enable AT91_IRQ_WARN here, but
93199c4a634SDavid S. Miller * it screws up the system under certain
93299c4a634SDavid S. Miller * circumstances. so just enable AT91_IRQ_ERRP, thus
93399c4a634SDavid S. Miller * the "fallthrough"
93499c4a634SDavid S. Miller */
935882055c8SMarc Kleine-Budde netdev_dbg(dev, "Error Active\n");
93699c4a634SDavid S. Miller cf->can_id |= CAN_ERR_PROT;
93799c4a634SDavid S. Miller cf->data[2] = CAN_ERR_PROT_ACTIVE;
938df561f66SGustavo A. R. Silva fallthrough;
9395a8dadbcSGustavo A. R. Silva case CAN_STATE_ERROR_WARNING:
94099c4a634SDavid S. Miller reg_idr = AT91_IRQ_ERRA | AT91_IRQ_WARN | AT91_IRQ_BOFF;
94199c4a634SDavid S. Miller reg_ier = AT91_IRQ_ERRP;
94299c4a634SDavid S. Miller break;
94399c4a634SDavid S. Miller case CAN_STATE_ERROR_PASSIVE:
94499c4a634SDavid S. Miller reg_idr = AT91_IRQ_ERRA | AT91_IRQ_WARN | AT91_IRQ_ERRP;
94599c4a634SDavid S. Miller reg_ier = AT91_IRQ_BOFF;
94699c4a634SDavid S. Miller break;
94799c4a634SDavid S. Miller case CAN_STATE_BUS_OFF:
94899c4a634SDavid S. Miller reg_idr = AT91_IRQ_ERRA | AT91_IRQ_ERRP |
94999c4a634SDavid S. Miller AT91_IRQ_WARN | AT91_IRQ_BOFF;
95099c4a634SDavid S. Miller reg_ier = 0;
95199c4a634SDavid S. Miller
95299c4a634SDavid S. Miller cf->can_id |= CAN_ERR_BUSOFF;
95399c4a634SDavid S. Miller
954882055c8SMarc Kleine-Budde netdev_dbg(dev, "bus-off\n");
95599c4a634SDavid S. Miller netif_carrier_off(dev);
95699c4a634SDavid S. Miller priv->can.can_stats.bus_off++;
95799c4a634SDavid S. Miller
95899c4a634SDavid S. Miller /* turn off chip, if restart is disabled */
95999c4a634SDavid S. Miller if (!priv->can.restart_ms) {
96099c4a634SDavid S. Miller at91_chip_stop(dev, CAN_STATE_BUS_OFF);
96199c4a634SDavid S. Miller return;
96299c4a634SDavid S. Miller }
96399c4a634SDavid S. Miller break;
96499c4a634SDavid S. Miller default:
96599c4a634SDavid S. Miller break;
96699c4a634SDavid S. Miller }
96799c4a634SDavid S. Miller
96899c4a634SDavid S. Miller at91_write(priv, AT91_IDR, reg_idr);
96999c4a634SDavid S. Miller at91_write(priv, AT91_IER, reg_ier);
97099c4a634SDavid S. Miller }
97199c4a634SDavid S. Miller
at91_get_state_by_bec(const struct net_device * dev,enum can_state * state)9726388b396SMarc Kleine-Budde static int at91_get_state_by_bec(const struct net_device *dev,
9736388b396SMarc Kleine-Budde enum can_state *state)
9746388b396SMarc Kleine-Budde {
9756388b396SMarc Kleine-Budde struct can_berr_counter bec;
9766388b396SMarc Kleine-Budde int err;
9776388b396SMarc Kleine-Budde
9786388b396SMarc Kleine-Budde err = at91_get_berr_counter(dev, &bec);
9796388b396SMarc Kleine-Budde if (err)
9806388b396SMarc Kleine-Budde return err;
9816388b396SMarc Kleine-Budde
9826388b396SMarc Kleine-Budde if (bec.txerr < 96 && bec.rxerr < 96)
9836388b396SMarc Kleine-Budde *state = CAN_STATE_ERROR_ACTIVE;
9846388b396SMarc Kleine-Budde else if (bec.txerr < 128 && bec.rxerr < 128)
9856388b396SMarc Kleine-Budde *state = CAN_STATE_ERROR_WARNING;
9866388b396SMarc Kleine-Budde else if (bec.txerr < 256 && bec.rxerr < 256)
9876388b396SMarc Kleine-Budde *state = CAN_STATE_ERROR_PASSIVE;
9886388b396SMarc Kleine-Budde else
9896388b396SMarc Kleine-Budde *state = CAN_STATE_BUS_OFF;
9906388b396SMarc Kleine-Budde
9916388b396SMarc Kleine-Budde return 0;
9926388b396SMarc Kleine-Budde }
9936388b396SMarc Kleine-Budde
at91_irq_err(struct net_device * dev)99499c4a634SDavid S. Miller static void at91_irq_err(struct net_device *dev)
99599c4a634SDavid S. Miller {
99699c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev);
99799c4a634SDavid S. Miller struct sk_buff *skb;
99899c4a634SDavid S. Miller struct can_frame *cf;
99999c4a634SDavid S. Miller enum can_state new_state;
100099c4a634SDavid S. Miller u32 reg_sr;
10016388b396SMarc Kleine-Budde int err;
100299c4a634SDavid S. Miller
10036388b396SMarc Kleine-Budde if (at91_is_sam9263(priv)) {
100499c4a634SDavid S. Miller reg_sr = at91_read(priv, AT91_SR);
100599c4a634SDavid S. Miller
100699c4a634SDavid S. Miller /* we need to look at the unmasked reg_sr */
100702400533SPeng Li if (unlikely(reg_sr & AT91_IRQ_BOFF)) {
100899c4a634SDavid S. Miller new_state = CAN_STATE_BUS_OFF;
100902400533SPeng Li } else if (unlikely(reg_sr & AT91_IRQ_ERRP)) {
101099c4a634SDavid S. Miller new_state = CAN_STATE_ERROR_PASSIVE;
101102400533SPeng Li } else if (unlikely(reg_sr & AT91_IRQ_WARN)) {
101299c4a634SDavid S. Miller new_state = CAN_STATE_ERROR_WARNING;
101302400533SPeng Li } else if (likely(reg_sr & AT91_IRQ_ERRA)) {
101499c4a634SDavid S. Miller new_state = CAN_STATE_ERROR_ACTIVE;
101502400533SPeng Li } else {
1016882055c8SMarc Kleine-Budde netdev_err(dev, "BUG! hardware in undefined state\n");
101799c4a634SDavid S. Miller return;
101899c4a634SDavid S. Miller }
10196388b396SMarc Kleine-Budde } else {
10206388b396SMarc Kleine-Budde err = at91_get_state_by_bec(dev, &new_state);
10216388b396SMarc Kleine-Budde if (err)
10226388b396SMarc Kleine-Budde return;
10236388b396SMarc Kleine-Budde }
102499c4a634SDavid S. Miller
102599c4a634SDavid S. Miller /* state hasn't changed */
102699c4a634SDavid S. Miller if (likely(new_state == priv->can.state))
102799c4a634SDavid S. Miller return;
102899c4a634SDavid S. Miller
102999c4a634SDavid S. Miller skb = alloc_can_err_skb(dev, &cf);
103099c4a634SDavid S. Miller if (unlikely(!skb))
103199c4a634SDavid S. Miller return;
103299c4a634SDavid S. Miller
103399c4a634SDavid S. Miller at91_irq_err_state(dev, cf, new_state);
103499c4a634SDavid S. Miller
10356ae3673dSMarc Kleine-Budde netif_rx(skb);
103699c4a634SDavid S. Miller
103799c4a634SDavid S. Miller priv->can.state = new_state;
103899c4a634SDavid S. Miller }
103999c4a634SDavid S. Miller
10405bbe6049SPeng Li /* interrupt handler
104199c4a634SDavid S. Miller */
at91_irq(int irq,void * dev_id)104299c4a634SDavid S. Miller static irqreturn_t at91_irq(int irq, void *dev_id)
104399c4a634SDavid S. Miller {
104499c4a634SDavid S. Miller struct net_device *dev = dev_id;
104599c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev);
104699c4a634SDavid S. Miller irqreturn_t handled = IRQ_NONE;
104799c4a634SDavid S. Miller u32 reg_sr, reg_imr;
104899c4a634SDavid S. Miller
104999c4a634SDavid S. Miller reg_sr = at91_read(priv, AT91_SR);
105099c4a634SDavid S. Miller reg_imr = at91_read(priv, AT91_IMR);
105199c4a634SDavid S. Miller
105299c4a634SDavid S. Miller /* Ignore masked interrupts */
105399c4a634SDavid S. Miller reg_sr &= reg_imr;
105499c4a634SDavid S. Miller if (!reg_sr)
105599c4a634SDavid S. Miller goto exit;
105699c4a634SDavid S. Miller
105799c4a634SDavid S. Miller handled = IRQ_HANDLED;
105899c4a634SDavid S. Miller
105999c4a634SDavid S. Miller /* Receive or error interrupt? -> napi */
106079008997SMarc Kleine-Budde if (reg_sr & (get_irq_mb_rx(priv) | AT91_IRQ_ERR_FRAME)) {
10615bbe6049SPeng Li /* The error bits are clear on read,
106299c4a634SDavid S. Miller * save for later use.
106399c4a634SDavid S. Miller */
106499c4a634SDavid S. Miller priv->reg_sr = reg_sr;
106599c4a634SDavid S. Miller at91_write(priv, AT91_IDR,
106679008997SMarc Kleine-Budde get_irq_mb_rx(priv) | AT91_IRQ_ERR_FRAME);
106799c4a634SDavid S. Miller napi_schedule(&priv->napi);
106899c4a634SDavid S. Miller }
106999c4a634SDavid S. Miller
107099c4a634SDavid S. Miller /* Transmission complete interrupt */
107179008997SMarc Kleine-Budde if (reg_sr & get_irq_mb_tx(priv))
107299c4a634SDavid S. Miller at91_irq_tx(dev, reg_sr);
107399c4a634SDavid S. Miller
107499c4a634SDavid S. Miller at91_irq_err(dev);
107599c4a634SDavid S. Miller
107699c4a634SDavid S. Miller exit:
107799c4a634SDavid S. Miller return handled;
107899c4a634SDavid S. Miller }
107999c4a634SDavid S. Miller
at91_open(struct net_device * dev)108099c4a634SDavid S. Miller static int at91_open(struct net_device *dev)
108199c4a634SDavid S. Miller {
108299c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev);
108399c4a634SDavid S. Miller int err;
108499c4a634SDavid S. Miller
1085e77980e5SDavid Dueck err = clk_prepare_enable(priv->clk);
1086e77980e5SDavid Dueck if (err)
1087e77980e5SDavid Dueck return err;
108899c4a634SDavid S. Miller
108999c4a634SDavid S. Miller /* check or determine and set bittime */
109099c4a634SDavid S. Miller err = open_candev(dev);
109199c4a634SDavid S. Miller if (err)
109299c4a634SDavid S. Miller goto out;
109399c4a634SDavid S. Miller
109499c4a634SDavid S. Miller /* register interrupt handler */
109599c4a634SDavid S. Miller if (request_irq(dev->irq, at91_irq, IRQF_SHARED,
109699c4a634SDavid S. Miller dev->name, dev)) {
109799c4a634SDavid S. Miller err = -EAGAIN;
109899c4a634SDavid S. Miller goto out_close;
109999c4a634SDavid S. Miller }
110099c4a634SDavid S. Miller
110199c4a634SDavid S. Miller /* start chip and queuing */
110299c4a634SDavid S. Miller at91_chip_start(dev);
110399c4a634SDavid S. Miller napi_enable(&priv->napi);
110499c4a634SDavid S. Miller netif_start_queue(dev);
110599c4a634SDavid S. Miller
110699c4a634SDavid S. Miller return 0;
110799c4a634SDavid S. Miller
110899c4a634SDavid S. Miller out_close:
110999c4a634SDavid S. Miller close_candev(dev);
111099c4a634SDavid S. Miller out:
1111e77980e5SDavid Dueck clk_disable_unprepare(priv->clk);
111299c4a634SDavid S. Miller
111399c4a634SDavid S. Miller return err;
111499c4a634SDavid S. Miller }
111599c4a634SDavid S. Miller
11165bbe6049SPeng Li /* stop CAN bus activity
111799c4a634SDavid S. Miller */
at91_close(struct net_device * dev)111899c4a634SDavid S. Miller static int at91_close(struct net_device *dev)
111999c4a634SDavid S. Miller {
112099c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev);
112199c4a634SDavid S. Miller
112299c4a634SDavid S. Miller netif_stop_queue(dev);
112399c4a634SDavid S. Miller napi_disable(&priv->napi);
112499c4a634SDavid S. Miller at91_chip_stop(dev, CAN_STATE_STOPPED);
112599c4a634SDavid S. Miller
112699c4a634SDavid S. Miller free_irq(dev->irq, dev);
1127e77980e5SDavid Dueck clk_disable_unprepare(priv->clk);
112899c4a634SDavid S. Miller
112999c4a634SDavid S. Miller close_candev(dev);
113099c4a634SDavid S. Miller
113199c4a634SDavid S. Miller return 0;
113299c4a634SDavid S. Miller }
113399c4a634SDavid S. Miller
at91_set_mode(struct net_device * dev,enum can_mode mode)113499c4a634SDavid S. Miller static int at91_set_mode(struct net_device *dev, enum can_mode mode)
113599c4a634SDavid S. Miller {
113699c4a634SDavid S. Miller switch (mode) {
113799c4a634SDavid S. Miller case CAN_MODE_START:
113899c4a634SDavid S. Miller at91_chip_start(dev);
113999c4a634SDavid S. Miller netif_wake_queue(dev);
114099c4a634SDavid S. Miller break;
114199c4a634SDavid S. Miller
114299c4a634SDavid S. Miller default:
114399c4a634SDavid S. Miller return -EOPNOTSUPP;
114499c4a634SDavid S. Miller }
114599c4a634SDavid S. Miller
114699c4a634SDavid S. Miller return 0;
114799c4a634SDavid S. Miller }
114899c4a634SDavid S. Miller
114999c4a634SDavid S. Miller static const struct net_device_ops at91_netdev_ops = {
115099c4a634SDavid S. Miller .ndo_open = at91_open,
115199c4a634SDavid S. Miller .ndo_stop = at91_close,
115299c4a634SDavid S. Miller .ndo_start_xmit = at91_start_xmit,
1153c971fa2aSOliver Hartkopp .ndo_change_mtu = can_change_mtu,
115499c4a634SDavid S. Miller };
115599c4a634SDavid S. Miller
1156409c188cSVincent Mailhol static const struct ethtool_ops at91_ethtool_ops = {
1157409c188cSVincent Mailhol .get_ts_info = ethtool_op_get_ts_info,
1158409c188cSVincent Mailhol };
1159409c188cSVincent Mailhol
mb0_id_show(struct device * dev,struct device_attribute * attr,char * buf)116042b9fd6eSZhen Lei static ssize_t mb0_id_show(struct device *dev,
11613a5655a5SMarc Kleine-Budde struct device_attribute *attr, char *buf)
11623a5655a5SMarc Kleine-Budde {
11633a5655a5SMarc Kleine-Budde struct at91_priv *priv = netdev_priv(to_net_dev(dev));
11643a5655a5SMarc Kleine-Budde
11653a5655a5SMarc Kleine-Budde if (priv->mb0_id & CAN_EFF_FLAG)
11667bc9ab0fSQing Wang return sysfs_emit(buf, "0x%08x\n", priv->mb0_id);
11673a5655a5SMarc Kleine-Budde else
11687bc9ab0fSQing Wang return sysfs_emit(buf, "0x%03x\n", priv->mb0_id);
11693a5655a5SMarc Kleine-Budde }
11703a5655a5SMarc Kleine-Budde
mb0_id_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)117142b9fd6eSZhen Lei static ssize_t mb0_id_store(struct device *dev,
1172ccc5f1c9SPeng Li struct device_attribute *attr,
1173ccc5f1c9SPeng Li const char *buf, size_t count)
11743a5655a5SMarc Kleine-Budde {
11753a5655a5SMarc Kleine-Budde struct net_device *ndev = to_net_dev(dev);
11763a5655a5SMarc Kleine-Budde struct at91_priv *priv = netdev_priv(ndev);
11773a5655a5SMarc Kleine-Budde unsigned long can_id;
11783a5655a5SMarc Kleine-Budde ssize_t ret;
11793a5655a5SMarc Kleine-Budde int err;
11803a5655a5SMarc Kleine-Budde
11813a5655a5SMarc Kleine-Budde rtnl_lock();
11823a5655a5SMarc Kleine-Budde
11833a5655a5SMarc Kleine-Budde if (ndev->flags & IFF_UP) {
11843a5655a5SMarc Kleine-Budde ret = -EBUSY;
11853a5655a5SMarc Kleine-Budde goto out;
11863a5655a5SMarc Kleine-Budde }
11873a5655a5SMarc Kleine-Budde
11880672f0abSJingoo Han err = kstrtoul(buf, 0, &can_id);
11893a5655a5SMarc Kleine-Budde if (err) {
11903a5655a5SMarc Kleine-Budde ret = err;
11913a5655a5SMarc Kleine-Budde goto out;
11923a5655a5SMarc Kleine-Budde }
11933a5655a5SMarc Kleine-Budde
11943a5655a5SMarc Kleine-Budde if (can_id & CAN_EFF_FLAG)
11953a5655a5SMarc Kleine-Budde can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
11963a5655a5SMarc Kleine-Budde else
11973a5655a5SMarc Kleine-Budde can_id &= CAN_SFF_MASK;
11983a5655a5SMarc Kleine-Budde
11993a5655a5SMarc Kleine-Budde priv->mb0_id = can_id;
12003a5655a5SMarc Kleine-Budde ret = count;
12013a5655a5SMarc Kleine-Budde
12023a5655a5SMarc Kleine-Budde out:
12033a5655a5SMarc Kleine-Budde rtnl_unlock();
12043a5655a5SMarc Kleine-Budde return ret;
12053a5655a5SMarc Kleine-Budde }
12063a5655a5SMarc Kleine-Budde
120742b9fd6eSZhen Lei static DEVICE_ATTR_RW(mb0_id);
12083a5655a5SMarc Kleine-Budde
12093a5655a5SMarc Kleine-Budde static struct attribute *at91_sysfs_attrs[] = {
12103a5655a5SMarc Kleine-Budde &dev_attr_mb0_id.attr,
12113a5655a5SMarc Kleine-Budde NULL,
12123a5655a5SMarc Kleine-Budde };
12133a5655a5SMarc Kleine-Budde
12147ec2796eSArvind Yadav static const struct attribute_group at91_sysfs_attr_group = {
12153a5655a5SMarc Kleine-Budde .attrs = at91_sysfs_attrs,
12163a5655a5SMarc Kleine-Budde };
12173a5655a5SMarc Kleine-Budde
12183078cde7SLudovic Desroches #if defined(CONFIG_OF)
12193078cde7SLudovic Desroches static const struct of_device_id at91_can_dt_ids[] = {
12203078cde7SLudovic Desroches {
12213078cde7SLudovic Desroches .compatible = "atmel,at91sam9x5-can",
12223078cde7SLudovic Desroches .data = &at91_at91sam9x5_data,
12233078cde7SLudovic Desroches }, {
12243078cde7SLudovic Desroches .compatible = "atmel,at91sam9263-can",
12253078cde7SLudovic Desroches .data = &at91_at91sam9263_data,
12263078cde7SLudovic Desroches }, {
12273078cde7SLudovic Desroches /* sentinel */
12283078cde7SLudovic Desroches }
12293078cde7SLudovic Desroches };
12303078cde7SLudovic Desroches MODULE_DEVICE_TABLE(of, at91_can_dt_ids);
12313078cde7SLudovic Desroches #endif
12323078cde7SLudovic Desroches
at91_can_get_driver_data(struct platform_device * pdev)12333078cde7SLudovic Desroches static const struct at91_devtype_data *at91_can_get_driver_data(struct platform_device *pdev)
12343078cde7SLudovic Desroches {
12353078cde7SLudovic Desroches if (pdev->dev.of_node) {
12363078cde7SLudovic Desroches const struct of_device_id *match;
12373078cde7SLudovic Desroches
12383078cde7SLudovic Desroches match = of_match_node(at91_can_dt_ids, pdev->dev.of_node);
12393078cde7SLudovic Desroches if (!match) {
12403078cde7SLudovic Desroches dev_err(&pdev->dev, "no matching node found in dtb\n");
12413078cde7SLudovic Desroches return NULL;
12423078cde7SLudovic Desroches }
12433078cde7SLudovic Desroches return (const struct at91_devtype_data *)match->data;
12443078cde7SLudovic Desroches }
12453078cde7SLudovic Desroches return (const struct at91_devtype_data *)
12463078cde7SLudovic Desroches platform_get_device_id(pdev)->driver_data;
12473078cde7SLudovic Desroches }
12483078cde7SLudovic Desroches
at91_can_probe(struct platform_device * pdev)12493c8ac0f2SBill Pemberton static int at91_can_probe(struct platform_device *pdev)
125099c4a634SDavid S. Miller {
1251d3d47264SMarc Kleine-Budde const struct at91_devtype_data *devtype_data;
125299c4a634SDavid S. Miller struct net_device *dev;
125399c4a634SDavid S. Miller struct at91_priv *priv;
125499c4a634SDavid S. Miller struct resource *res;
125599c4a634SDavid S. Miller struct clk *clk;
125699c4a634SDavid S. Miller void __iomem *addr;
125799c4a634SDavid S. Miller int err, irq;
125899c4a634SDavid S. Miller
12593078cde7SLudovic Desroches devtype_data = at91_can_get_driver_data(pdev);
12603078cde7SLudovic Desroches if (!devtype_data) {
12613078cde7SLudovic Desroches dev_err(&pdev->dev, "no driver data\n");
12623078cde7SLudovic Desroches err = -ENODEV;
12633078cde7SLudovic Desroches goto exit;
12643078cde7SLudovic Desroches }
1265d3d47264SMarc Kleine-Budde
126699c4a634SDavid S. Miller clk = clk_get(&pdev->dev, "can_clk");
126799c4a634SDavid S. Miller if (IS_ERR(clk)) {
126899c4a634SDavid S. Miller dev_err(&pdev->dev, "no clock defined\n");
126999c4a634SDavid S. Miller err = -ENODEV;
127099c4a634SDavid S. Miller goto exit;
127199c4a634SDavid S. Miller }
127299c4a634SDavid S. Miller
127399c4a634SDavid S. Miller res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
127499c4a634SDavid S. Miller irq = platform_get_irq(pdev, 0);
12754773a47dSUwe Kleine-König if (!res || irq <= 0) {
127699c4a634SDavid S. Miller err = -ENODEV;
127799c4a634SDavid S. Miller goto exit_put;
127899c4a634SDavid S. Miller }
127999c4a634SDavid S. Miller
128099c4a634SDavid S. Miller if (!request_mem_region(res->start,
128199c4a634SDavid S. Miller resource_size(res),
128299c4a634SDavid S. Miller pdev->name)) {
128399c4a634SDavid S. Miller err = -EBUSY;
128499c4a634SDavid S. Miller goto exit_put;
128599c4a634SDavid S. Miller }
128699c4a634SDavid S. Miller
12874bdc0d67SChristoph Hellwig addr = ioremap(res->start, resource_size(res));
128899c4a634SDavid S. Miller if (!addr) {
128999c4a634SDavid S. Miller err = -ENOMEM;
129099c4a634SDavid S. Miller goto exit_release;
129199c4a634SDavid S. Miller }
129299c4a634SDavid S. Miller
1293d3d47264SMarc Kleine-Budde dev = alloc_candev(sizeof(struct at91_priv),
1294d3d47264SMarc Kleine-Budde 1 << devtype_data->tx_shift);
129599c4a634SDavid S. Miller if (!dev) {
129699c4a634SDavid S. Miller err = -ENOMEM;
129799c4a634SDavid S. Miller goto exit_iounmap;
129899c4a634SDavid S. Miller }
129999c4a634SDavid S. Miller
130099c4a634SDavid S. Miller dev->netdev_ops = &at91_netdev_ops;
1301409c188cSVincent Mailhol dev->ethtool_ops = &at91_ethtool_ops;
130299c4a634SDavid S. Miller dev->irq = irq;
130399c4a634SDavid S. Miller dev->flags |= IFF_ECHO;
130499c4a634SDavid S. Miller
130599c4a634SDavid S. Miller priv = netdev_priv(dev);
130699c4a634SDavid S. Miller priv->can.clock.freq = clk_get_rate(clk);
130799c4a634SDavid S. Miller priv->can.bittiming_const = &at91_bittiming_const;
130899c4a634SDavid S. Miller priv->can.do_set_mode = at91_set_mode;
130933a6f298SMarc Kleine-Budde priv->can.do_get_berr_counter = at91_get_berr_counter;
131017a50ee4SYoann DI RUZZA priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES |
131117a50ee4SYoann DI RUZZA CAN_CTRLMODE_LISTENONLY;
1312d3d47264SMarc Kleine-Budde priv->reg_base = addr;
1313d3d47264SMarc Kleine-Budde priv->devtype_data = *devtype_data;
131499c4a634SDavid S. Miller priv->clk = clk;
13156cbdb918SJingoo Han priv->pdata = dev_get_platdata(&pdev->dev);
13163a5655a5SMarc Kleine-Budde priv->mb0_id = 0x7ff;
131799c4a634SDavid S. Miller
1318caf6b7f8SJakub Kicinski netif_napi_add_weight(dev, &priv->napi, at91_poll, get_mb_rx_num(priv));
131999c4a634SDavid S. Miller
132007a648e6SMarc Kleine-Budde if (at91_is_sam9263(priv))
132107a648e6SMarc Kleine-Budde dev->sysfs_groups[0] = &at91_sysfs_attr_group;
132207a648e6SMarc Kleine-Budde
132340f7e0ddSLibo Chen platform_set_drvdata(pdev, dev);
132499c4a634SDavid S. Miller SET_NETDEV_DEV(dev, &pdev->dev);
132599c4a634SDavid S. Miller
132699c4a634SDavid S. Miller err = register_candev(dev);
132799c4a634SDavid S. Miller if (err) {
132899c4a634SDavid S. Miller dev_err(&pdev->dev, "registering netdev failed\n");
132999c4a634SDavid S. Miller goto exit_free;
133099c4a634SDavid S. Miller }
133199c4a634SDavid S. Miller
133299c4a634SDavid S. Miller dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
133399c4a634SDavid S. Miller priv->reg_base, dev->irq);
133499c4a634SDavid S. Miller
133599c4a634SDavid S. Miller return 0;
133699c4a634SDavid S. Miller
133799c4a634SDavid S. Miller exit_free:
1338759a6c76SMarc Kleine-Budde free_candev(dev);
133999c4a634SDavid S. Miller exit_iounmap:
134099c4a634SDavid S. Miller iounmap(addr);
134199c4a634SDavid S. Miller exit_release:
134299c4a634SDavid S. Miller release_mem_region(res->start, resource_size(res));
134399c4a634SDavid S. Miller exit_put:
134499c4a634SDavid S. Miller clk_put(clk);
134599c4a634SDavid S. Miller exit:
134699c4a634SDavid S. Miller return err;
134799c4a634SDavid S. Miller }
134899c4a634SDavid S. Miller
at91_can_remove(struct platform_device * pdev)1349*03ef5a4bSUwe Kleine-König static void at91_can_remove(struct platform_device *pdev)
135099c4a634SDavid S. Miller {
135199c4a634SDavid S. Miller struct net_device *dev = platform_get_drvdata(pdev);
135299c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev);
135399c4a634SDavid S. Miller struct resource *res;
135499c4a634SDavid S. Miller
135599c4a634SDavid S. Miller unregister_netdev(dev);
135699c4a634SDavid S. Miller
135799c4a634SDavid S. Miller iounmap(priv->reg_base);
135899c4a634SDavid S. Miller
135999c4a634SDavid S. Miller res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
136099c4a634SDavid S. Miller release_mem_region(res->start, resource_size(res));
136199c4a634SDavid S. Miller
136299c4a634SDavid S. Miller clk_put(priv->clk);
136399c4a634SDavid S. Miller
1364759a6c76SMarc Kleine-Budde free_candev(dev);
136599c4a634SDavid S. Miller }
136699c4a634SDavid S. Miller
1367d3d47264SMarc Kleine-Budde static const struct platform_device_id at91_can_id_table[] = {
1368d3d47264SMarc Kleine-Budde {
13695abbeea5SMarc Kleine-Budde .name = "at91sam9x5_can",
13703078cde7SLudovic Desroches .driver_data = (kernel_ulong_t)&at91_at91sam9x5_data,
1371d3d47264SMarc Kleine-Budde }, {
13725abbeea5SMarc Kleine-Budde .name = "at91_can",
13733078cde7SLudovic Desroches .driver_data = (kernel_ulong_t)&at91_at91sam9263_data,
13746388b396SMarc Kleine-Budde }, {
1375d3d47264SMarc Kleine-Budde /* sentinel */
1376d3d47264SMarc Kleine-Budde }
1377d3d47264SMarc Kleine-Budde };
137809ca71caSMarc Kleine-Budde MODULE_DEVICE_TABLE(platform, at91_can_id_table);
1379d3d47264SMarc Kleine-Budde
138099c4a634SDavid S. Miller static struct platform_driver at91_can_driver = {
138199c4a634SDavid S. Miller .probe = at91_can_probe,
1382*03ef5a4bSUwe Kleine-König .remove_new = at91_can_remove,
138399c4a634SDavid S. Miller .driver = {
138400389b08SMarc Kleine-Budde .name = KBUILD_MODNAME,
13851f3e4b0cSSachin Kamat .of_match_table = of_match_ptr(at91_can_dt_ids),
138699c4a634SDavid S. Miller },
1387d3d47264SMarc Kleine-Budde .id_table = at91_can_id_table,
138899c4a634SDavid S. Miller };
138999c4a634SDavid S. Miller
1390871d3372SAxel Lin module_platform_driver(at91_can_driver);
139199c4a634SDavid S. Miller
139299c4a634SDavid S. Miller MODULE_AUTHOR("Marc Kleine-Budde <mkl@pengutronix.de>");
139399c4a634SDavid S. Miller MODULE_LICENSE("GPL v2");
139400389b08SMarc Kleine-Budde MODULE_DESCRIPTION(KBUILD_MODNAME " CAN netdevice driver");
1395