199c4a634SDavid S. Miller /* 299c4a634SDavid S. Miller * at91_can.c - CAN network driver for AT91 SoC CAN controller 399c4a634SDavid S. Miller * 43e9ebd3cSHans J. Koch * (C) 2007 by Hans J. Koch <hjk@hansjkoch.de> 50909c1ecSMarc Kleine-Budde * (C) 2008, 2009, 2010, 2011 by Marc Kleine-Budde <kernel@pengutronix.de> 699c4a634SDavid S. Miller * 799c4a634SDavid S. Miller * This software may be distributed under the terms of the GNU General 899c4a634SDavid S. Miller * Public License ("GPL") version 2 as distributed in the 'COPYING' 999c4a634SDavid S. Miller * file from the main directory of the linux kernel source. 1099c4a634SDavid S. Miller * 1199c4a634SDavid S. Miller * 1299c4a634SDavid S. Miller * Your platform definition file should specify something like: 1399c4a634SDavid S. Miller * 1499c4a634SDavid S. Miller * static struct at91_can_data ek_can_data = { 1599c4a634SDavid S. Miller * transceiver_switch = sam9263ek_transceiver_switch, 1699c4a634SDavid S. Miller * }; 1799c4a634SDavid S. Miller * 1899c4a634SDavid S. Miller * at91_add_device_can(&ek_can_data); 1999c4a634SDavid S. Miller * 2099c4a634SDavid S. Miller */ 2199c4a634SDavid S. Miller 2299c4a634SDavid S. Miller #include <linux/clk.h> 2399c4a634SDavid S. Miller #include <linux/errno.h> 2499c4a634SDavid S. Miller #include <linux/if_arp.h> 2599c4a634SDavid S. Miller #include <linux/interrupt.h> 2699c4a634SDavid S. Miller #include <linux/kernel.h> 2799c4a634SDavid S. Miller #include <linux/module.h> 2899c4a634SDavid S. Miller #include <linux/netdevice.h> 293078cde7SLudovic Desroches #include <linux/of.h> 3099c4a634SDavid S. Miller #include <linux/platform_device.h> 313a5655a5SMarc Kleine-Budde #include <linux/rtnetlink.h> 3299c4a634SDavid S. Miller #include <linux/skbuff.h> 3399c4a634SDavid S. Miller #include <linux/spinlock.h> 3499c4a634SDavid S. Miller #include <linux/string.h> 3599c4a634SDavid S. Miller #include <linux/types.h> 36bcd2360cSJean-Christophe PLAGNIOL-VILLARD #include <linux/platform_data/atmel.h> 3799c4a634SDavid S. Miller 3899c4a634SDavid S. Miller #include <linux/can/dev.h> 3999c4a634SDavid S. Miller #include <linux/can/error.h> 404723f2b8SFabio Baltieri #include <linux/can/led.h> 4199c4a634SDavid S. Miller 42b049994dSMarc Kleine-Budde #define AT91_MB_MASK(i) ((1 << (i)) - 1) 4399c4a634SDavid S. Miller 4499c4a634SDavid S. Miller /* Common registers */ 4599c4a634SDavid S. Miller enum at91_reg { 4699c4a634SDavid S. Miller AT91_MR = 0x000, 4799c4a634SDavid S. Miller AT91_IER = 0x004, 4899c4a634SDavid S. Miller AT91_IDR = 0x008, 4999c4a634SDavid S. Miller AT91_IMR = 0x00C, 5099c4a634SDavid S. Miller AT91_SR = 0x010, 5199c4a634SDavid S. Miller AT91_BR = 0x014, 5299c4a634SDavid S. Miller AT91_TIM = 0x018, 5399c4a634SDavid S. Miller AT91_TIMESTP = 0x01C, 5499c4a634SDavid S. Miller AT91_ECR = 0x020, 5599c4a634SDavid S. Miller AT91_TCR = 0x024, 5699c4a634SDavid S. Miller AT91_ACR = 0x028, 5799c4a634SDavid S. Miller }; 5899c4a634SDavid S. Miller 5999c4a634SDavid S. Miller /* Mailbox registers (0 <= i <= 15) */ 6099c4a634SDavid S. Miller #define AT91_MMR(i) (enum at91_reg)(0x200 + ((i) * 0x20)) 6199c4a634SDavid S. Miller #define AT91_MAM(i) (enum at91_reg)(0x204 + ((i) * 0x20)) 6299c4a634SDavid S. Miller #define AT91_MID(i) (enum at91_reg)(0x208 + ((i) * 0x20)) 6399c4a634SDavid S. Miller #define AT91_MFID(i) (enum at91_reg)(0x20C + ((i) * 0x20)) 6499c4a634SDavid S. Miller #define AT91_MSR(i) (enum at91_reg)(0x210 + ((i) * 0x20)) 6599c4a634SDavid S. Miller #define AT91_MDL(i) (enum at91_reg)(0x214 + ((i) * 0x20)) 6699c4a634SDavid S. Miller #define AT91_MDH(i) (enum at91_reg)(0x218 + ((i) * 0x20)) 6799c4a634SDavid S. Miller #define AT91_MCR(i) (enum at91_reg)(0x21C + ((i) * 0x20)) 6899c4a634SDavid S. Miller 6999c4a634SDavid S. Miller /* Register bits */ 7099c4a634SDavid S. Miller #define AT91_MR_CANEN BIT(0) 7199c4a634SDavid S. Miller #define AT91_MR_LPM BIT(1) 7299c4a634SDavid S. Miller #define AT91_MR_ABM BIT(2) 7399c4a634SDavid S. Miller #define AT91_MR_OVL BIT(3) 7499c4a634SDavid S. Miller #define AT91_MR_TEOF BIT(4) 7599c4a634SDavid S. Miller #define AT91_MR_TTM BIT(5) 7699c4a634SDavid S. Miller #define AT91_MR_TIMFRZ BIT(6) 7799c4a634SDavid S. Miller #define AT91_MR_DRPT BIT(7) 7899c4a634SDavid S. Miller 7999c4a634SDavid S. Miller #define AT91_SR_RBSY BIT(29) 8099c4a634SDavid S. Miller 8199c4a634SDavid S. Miller #define AT91_MMR_PRIO_SHIFT (16) 8299c4a634SDavid S. Miller 8399c4a634SDavid S. Miller #define AT91_MID_MIDE BIT(29) 8499c4a634SDavid S. Miller 8599c4a634SDavid S. Miller #define AT91_MSR_MRTR BIT(20) 8699c4a634SDavid S. Miller #define AT91_MSR_MABT BIT(22) 8799c4a634SDavid S. Miller #define AT91_MSR_MRDY BIT(23) 8899c4a634SDavid S. Miller #define AT91_MSR_MMI BIT(24) 8999c4a634SDavid S. Miller 9099c4a634SDavid S. Miller #define AT91_MCR_MRTR BIT(20) 9199c4a634SDavid S. Miller #define AT91_MCR_MTCR BIT(23) 9299c4a634SDavid S. Miller 9399c4a634SDavid S. Miller /* Mailbox Modes */ 9499c4a634SDavid S. Miller enum at91_mb_mode { 9599c4a634SDavid S. Miller AT91_MB_MODE_DISABLED = 0, 9699c4a634SDavid S. Miller AT91_MB_MODE_RX = 1, 9799c4a634SDavid S. Miller AT91_MB_MODE_RX_OVRWR = 2, 9899c4a634SDavid S. Miller AT91_MB_MODE_TX = 3, 9999c4a634SDavid S. Miller AT91_MB_MODE_CONSUMER = 4, 10099c4a634SDavid S. Miller AT91_MB_MODE_PRODUCER = 5, 10199c4a634SDavid S. Miller }; 10299c4a634SDavid S. Miller 10399c4a634SDavid S. Miller /* Interrupt mask bits */ 10499c4a634SDavid S. Miller #define AT91_IRQ_ERRA (1 << 16) 10599c4a634SDavid S. Miller #define AT91_IRQ_WARN (1 << 17) 10699c4a634SDavid S. Miller #define AT91_IRQ_ERRP (1 << 18) 10799c4a634SDavid S. Miller #define AT91_IRQ_BOFF (1 << 19) 10899c4a634SDavid S. Miller #define AT91_IRQ_SLEEP (1 << 20) 10999c4a634SDavid S. Miller #define AT91_IRQ_WAKEUP (1 << 21) 11099c4a634SDavid S. Miller #define AT91_IRQ_TOVF (1 << 22) 11199c4a634SDavid S. Miller #define AT91_IRQ_TSTP (1 << 23) 11299c4a634SDavid S. Miller #define AT91_IRQ_CERR (1 << 24) 11399c4a634SDavid S. Miller #define AT91_IRQ_SERR (1 << 25) 11499c4a634SDavid S. Miller #define AT91_IRQ_AERR (1 << 26) 11599c4a634SDavid S. Miller #define AT91_IRQ_FERR (1 << 27) 11699c4a634SDavid S. Miller #define AT91_IRQ_BERR (1 << 28) 11799c4a634SDavid S. Miller 11899c4a634SDavid S. Miller #define AT91_IRQ_ERR_ALL (0x1fff0000) 11999c4a634SDavid S. Miller #define AT91_IRQ_ERR_FRAME (AT91_IRQ_CERR | AT91_IRQ_SERR | \ 12099c4a634SDavid S. Miller AT91_IRQ_AERR | AT91_IRQ_FERR | AT91_IRQ_BERR) 12199c4a634SDavid S. Miller #define AT91_IRQ_ERR_LINE (AT91_IRQ_ERRA | AT91_IRQ_WARN | \ 12299c4a634SDavid S. Miller AT91_IRQ_ERRP | AT91_IRQ_BOFF) 12399c4a634SDavid S. Miller 12499c4a634SDavid S. Miller #define AT91_IRQ_ALL (0x1fffffff) 12599c4a634SDavid S. Miller 126d3d47264SMarc Kleine-Budde enum at91_devtype { 127d3d47264SMarc Kleine-Budde AT91_DEVTYPE_SAM9263, 1286388b396SMarc Kleine-Budde AT91_DEVTYPE_SAM9X5, 129d3d47264SMarc Kleine-Budde }; 130d3d47264SMarc Kleine-Budde 131d3d47264SMarc Kleine-Budde struct at91_devtype_data { 132d3d47264SMarc Kleine-Budde unsigned int rx_first; 133d3d47264SMarc Kleine-Budde unsigned int rx_split; 134d3d47264SMarc Kleine-Budde unsigned int rx_last; 135d3d47264SMarc Kleine-Budde unsigned int tx_shift; 136d3d47264SMarc Kleine-Budde enum at91_devtype type; 137d3d47264SMarc Kleine-Budde }; 138d3d47264SMarc Kleine-Budde 13999c4a634SDavid S. Miller struct at91_priv { 14099c4a634SDavid S. Miller struct can_priv can; /* must be the first member! */ 14199c4a634SDavid S. Miller struct napi_struct napi; 14299c4a634SDavid S. Miller 14399c4a634SDavid S. Miller void __iomem *reg_base; 14499c4a634SDavid S. Miller 14599c4a634SDavid S. Miller u32 reg_sr; 14699c4a634SDavid S. Miller unsigned int tx_next; 14799c4a634SDavid S. Miller unsigned int tx_echo; 14899c4a634SDavid S. Miller unsigned int rx_next; 149d3d47264SMarc Kleine-Budde struct at91_devtype_data devtype_data; 15099c4a634SDavid S. Miller 15199c4a634SDavid S. Miller struct clk *clk; 15299c4a634SDavid S. Miller struct at91_can_data *pdata; 1533a5655a5SMarc Kleine-Budde 1543a5655a5SMarc Kleine-Budde canid_t mb0_id; 15599c4a634SDavid S. Miller }; 15699c4a634SDavid S. Miller 1573078cde7SLudovic Desroches static const struct at91_devtype_data at91_at91sam9263_data = { 158d3d47264SMarc Kleine-Budde .rx_first = 1, 159d3d47264SMarc Kleine-Budde .rx_split = 8, 160d3d47264SMarc Kleine-Budde .rx_last = 11, 161d3d47264SMarc Kleine-Budde .tx_shift = 2, 1623078cde7SLudovic Desroches .type = AT91_DEVTYPE_SAM9263, 1633078cde7SLudovic Desroches }; 1643078cde7SLudovic Desroches 1653078cde7SLudovic Desroches static const struct at91_devtype_data at91_at91sam9x5_data = { 1666388b396SMarc Kleine-Budde .rx_first = 0, 1676388b396SMarc Kleine-Budde .rx_split = 4, 1686388b396SMarc Kleine-Budde .rx_last = 5, 1696388b396SMarc Kleine-Budde .tx_shift = 1, 1703078cde7SLudovic Desroches .type = AT91_DEVTYPE_SAM9X5, 171d3d47264SMarc Kleine-Budde }; 172d3d47264SMarc Kleine-Budde 173194b9a4cSMarc Kleine-Budde static const struct can_bittiming_const at91_bittiming_const = { 17400389b08SMarc Kleine-Budde .name = KBUILD_MODNAME, 17599c4a634SDavid S. Miller .tseg1_min = 4, 17699c4a634SDavid S. Miller .tseg1_max = 16, 17799c4a634SDavid S. Miller .tseg2_min = 2, 17899c4a634SDavid S. Miller .tseg2_max = 8, 17999c4a634SDavid S. Miller .sjw_max = 4, 18099c4a634SDavid S. Miller .brp_min = 2, 18199c4a634SDavid S. Miller .brp_max = 128, 18299c4a634SDavid S. Miller .brp_inc = 1, 18399c4a634SDavid S. Miller }; 18499c4a634SDavid S. Miller 185d3d47264SMarc Kleine-Budde #define AT91_IS(_model) \ 186d3d47264SMarc Kleine-Budde static inline int at91_is_sam##_model(const struct at91_priv *priv) \ 187d3d47264SMarc Kleine-Budde { \ 188d3d47264SMarc Kleine-Budde return priv->devtype_data.type == AT91_DEVTYPE_SAM##_model; \ 189d3d47264SMarc Kleine-Budde } 190d3d47264SMarc Kleine-Budde 191d3d47264SMarc Kleine-Budde AT91_IS(9263); 1926388b396SMarc Kleine-Budde AT91_IS(9X5); 193d3d47264SMarc Kleine-Budde 194d3d47264SMarc Kleine-Budde static inline unsigned int get_mb_rx_first(const struct at91_priv *priv) 195d3d47264SMarc Kleine-Budde { 196d3d47264SMarc Kleine-Budde return priv->devtype_data.rx_first; 197d3d47264SMarc Kleine-Budde } 198d3d47264SMarc Kleine-Budde 199d3d47264SMarc Kleine-Budde static inline unsigned int get_mb_rx_last(const struct at91_priv *priv) 200d3d47264SMarc Kleine-Budde { 201d3d47264SMarc Kleine-Budde return priv->devtype_data.rx_last; 202d3d47264SMarc Kleine-Budde } 203d3d47264SMarc Kleine-Budde 204d3d47264SMarc Kleine-Budde static inline unsigned int get_mb_rx_split(const struct at91_priv *priv) 205d3d47264SMarc Kleine-Budde { 206d3d47264SMarc Kleine-Budde return priv->devtype_data.rx_split; 207d3d47264SMarc Kleine-Budde } 208d3d47264SMarc Kleine-Budde 209d3d47264SMarc Kleine-Budde static inline unsigned int get_mb_rx_num(const struct at91_priv *priv) 210d3d47264SMarc Kleine-Budde { 211d3d47264SMarc Kleine-Budde return get_mb_rx_last(priv) - get_mb_rx_first(priv) + 1; 212d3d47264SMarc Kleine-Budde } 213d3d47264SMarc Kleine-Budde 21479008997SMarc Kleine-Budde static inline unsigned int get_mb_rx_low_last(const struct at91_priv *priv) 21579008997SMarc Kleine-Budde { 216d3d47264SMarc Kleine-Budde return get_mb_rx_split(priv) - 1; 21779008997SMarc Kleine-Budde } 21879008997SMarc Kleine-Budde 21979008997SMarc Kleine-Budde static inline unsigned int get_mb_rx_low_mask(const struct at91_priv *priv) 22079008997SMarc Kleine-Budde { 221d3d47264SMarc Kleine-Budde return AT91_MB_MASK(get_mb_rx_split(priv)) & 222d3d47264SMarc Kleine-Budde ~AT91_MB_MASK(get_mb_rx_first(priv)); 223d3d47264SMarc Kleine-Budde } 224d3d47264SMarc Kleine-Budde 225d3d47264SMarc Kleine-Budde static inline unsigned int get_mb_tx_shift(const struct at91_priv *priv) 226d3d47264SMarc Kleine-Budde { 227d3d47264SMarc Kleine-Budde return priv->devtype_data.tx_shift; 22879008997SMarc Kleine-Budde } 22979008997SMarc Kleine-Budde 23079008997SMarc Kleine-Budde static inline unsigned int get_mb_tx_num(const struct at91_priv *priv) 23179008997SMarc Kleine-Budde { 232d3d47264SMarc Kleine-Budde return 1 << get_mb_tx_shift(priv); 23379008997SMarc Kleine-Budde } 23479008997SMarc Kleine-Budde 23579008997SMarc Kleine-Budde static inline unsigned int get_mb_tx_first(const struct at91_priv *priv) 23679008997SMarc Kleine-Budde { 237d3d47264SMarc Kleine-Budde return get_mb_rx_last(priv) + 1; 23879008997SMarc Kleine-Budde } 23979008997SMarc Kleine-Budde 24079008997SMarc Kleine-Budde static inline unsigned int get_mb_tx_last(const struct at91_priv *priv) 24179008997SMarc Kleine-Budde { 24279008997SMarc Kleine-Budde return get_mb_tx_first(priv) + get_mb_tx_num(priv) - 1; 24379008997SMarc Kleine-Budde } 24479008997SMarc Kleine-Budde 24579008997SMarc Kleine-Budde static inline unsigned int get_next_prio_shift(const struct at91_priv *priv) 24679008997SMarc Kleine-Budde { 247d3d47264SMarc Kleine-Budde return get_mb_tx_shift(priv); 24879008997SMarc Kleine-Budde } 24979008997SMarc Kleine-Budde 25079008997SMarc Kleine-Budde static inline unsigned int get_next_prio_mask(const struct at91_priv *priv) 25179008997SMarc Kleine-Budde { 252d3d47264SMarc Kleine-Budde return 0xf << get_mb_tx_shift(priv); 25379008997SMarc Kleine-Budde } 25479008997SMarc Kleine-Budde 25579008997SMarc Kleine-Budde static inline unsigned int get_next_mb_mask(const struct at91_priv *priv) 25679008997SMarc Kleine-Budde { 257d3d47264SMarc Kleine-Budde return AT91_MB_MASK(get_mb_tx_shift(priv)); 25879008997SMarc Kleine-Budde } 25979008997SMarc Kleine-Budde 26079008997SMarc Kleine-Budde static inline unsigned int get_next_mask(const struct at91_priv *priv) 26179008997SMarc Kleine-Budde { 26279008997SMarc Kleine-Budde return get_next_mb_mask(priv) | get_next_prio_mask(priv); 26379008997SMarc Kleine-Budde } 26479008997SMarc Kleine-Budde 26579008997SMarc Kleine-Budde static inline unsigned int get_irq_mb_rx(const struct at91_priv *priv) 26679008997SMarc Kleine-Budde { 267d3d47264SMarc Kleine-Budde return AT91_MB_MASK(get_mb_rx_last(priv) + 1) & 268d3d47264SMarc Kleine-Budde ~AT91_MB_MASK(get_mb_rx_first(priv)); 26979008997SMarc Kleine-Budde } 27079008997SMarc Kleine-Budde 27179008997SMarc Kleine-Budde static inline unsigned int get_irq_mb_tx(const struct at91_priv *priv) 27279008997SMarc Kleine-Budde { 27379008997SMarc Kleine-Budde return AT91_MB_MASK(get_mb_tx_last(priv) + 1) & 27479008997SMarc Kleine-Budde ~AT91_MB_MASK(get_mb_tx_first(priv)); 27579008997SMarc Kleine-Budde } 27679008997SMarc Kleine-Budde 2779c2e0a6dSMarc Kleine-Budde static inline unsigned int get_tx_next_mb(const struct at91_priv *priv) 27899c4a634SDavid S. Miller { 27979008997SMarc Kleine-Budde return (priv->tx_next & get_next_mb_mask(priv)) + get_mb_tx_first(priv); 28099c4a634SDavid S. Miller } 28199c4a634SDavid S. Miller 2829c2e0a6dSMarc Kleine-Budde static inline unsigned int get_tx_next_prio(const struct at91_priv *priv) 28399c4a634SDavid S. Miller { 28479008997SMarc Kleine-Budde return (priv->tx_next >> get_next_prio_shift(priv)) & 0xf; 28599c4a634SDavid S. Miller } 28699c4a634SDavid S. Miller 2879c2e0a6dSMarc Kleine-Budde static inline unsigned int get_tx_echo_mb(const struct at91_priv *priv) 28899c4a634SDavid S. Miller { 28979008997SMarc Kleine-Budde return (priv->tx_echo & get_next_mb_mask(priv)) + get_mb_tx_first(priv); 29099c4a634SDavid S. Miller } 29199c4a634SDavid S. Miller 29299c4a634SDavid S. Miller static inline u32 at91_read(const struct at91_priv *priv, enum at91_reg reg) 29399c4a634SDavid S. Miller { 294af9bfbdaSBen Dooks return readl_relaxed(priv->reg_base + reg); 29599c4a634SDavid S. Miller } 29699c4a634SDavid S. Miller 29799c4a634SDavid S. Miller static inline void at91_write(const struct at91_priv *priv, enum at91_reg reg, 29899c4a634SDavid S. Miller u32 value) 29999c4a634SDavid S. Miller { 300af9bfbdaSBen Dooks writel_relaxed(value, priv->reg_base + reg); 30199c4a634SDavid S. Miller } 30299c4a634SDavid S. Miller 30399c4a634SDavid S. Miller static inline void set_mb_mode_prio(const struct at91_priv *priv, 30499c4a634SDavid S. Miller unsigned int mb, enum at91_mb_mode mode, int prio) 30599c4a634SDavid S. Miller { 30699c4a634SDavid S. Miller at91_write(priv, AT91_MMR(mb), (mode << 24) | (prio << 16)); 30799c4a634SDavid S. Miller } 30899c4a634SDavid S. Miller 30999c4a634SDavid S. Miller static inline void set_mb_mode(const struct at91_priv *priv, unsigned int mb, 31099c4a634SDavid S. Miller enum at91_mb_mode mode) 31199c4a634SDavid S. Miller { 31299c4a634SDavid S. Miller set_mb_mode_prio(priv, mb, mode, 0); 31399c4a634SDavid S. Miller } 31499c4a634SDavid S. Miller 3153a5655a5SMarc Kleine-Budde static inline u32 at91_can_id_to_reg_mid(canid_t can_id) 3163a5655a5SMarc Kleine-Budde { 3173a5655a5SMarc Kleine-Budde u32 reg_mid; 3183a5655a5SMarc Kleine-Budde 3193a5655a5SMarc Kleine-Budde if (can_id & CAN_EFF_FLAG) 3203a5655a5SMarc Kleine-Budde reg_mid = (can_id & CAN_EFF_MASK) | AT91_MID_MIDE; 3213a5655a5SMarc Kleine-Budde else 3223a5655a5SMarc Kleine-Budde reg_mid = (can_id & CAN_SFF_MASK) << 18; 3233a5655a5SMarc Kleine-Budde 3243a5655a5SMarc Kleine-Budde return reg_mid; 3253a5655a5SMarc Kleine-Budde } 3263a5655a5SMarc Kleine-Budde 32799c4a634SDavid S. Miller /* 32899c4a634SDavid S. Miller * Swtich transceiver on or off 32999c4a634SDavid S. Miller */ 33099c4a634SDavid S. Miller static void at91_transceiver_switch(const struct at91_priv *priv, int on) 33199c4a634SDavid S. Miller { 33299c4a634SDavid S. Miller if (priv->pdata && priv->pdata->transceiver_switch) 33399c4a634SDavid S. Miller priv->pdata->transceiver_switch(on); 33499c4a634SDavid S. Miller } 33599c4a634SDavid S. Miller 33699c4a634SDavid S. Miller static void at91_setup_mailboxes(struct net_device *dev) 33799c4a634SDavid S. Miller { 33899c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 33999c4a634SDavid S. Miller unsigned int i; 3403a5655a5SMarc Kleine-Budde u32 reg_mid; 34199c4a634SDavid S. Miller 34299c4a634SDavid S. Miller /* 3439e0a2d1cSMarc Kleine-Budde * Due to a chip bug (errata 50.2.6.3 & 50.3.5.3) the first 3449e0a2d1cSMarc Kleine-Budde * mailbox is disabled. The next 11 mailboxes are used as a 3459e0a2d1cSMarc Kleine-Budde * reception FIFO. The last mailbox is configured with 3469e0a2d1cSMarc Kleine-Budde * overwrite option. The overwrite flag indicates a FIFO 3479e0a2d1cSMarc Kleine-Budde * overflow. 34899c4a634SDavid S. Miller */ 3493a5655a5SMarc Kleine-Budde reg_mid = at91_can_id_to_reg_mid(priv->mb0_id); 350d3d47264SMarc Kleine-Budde for (i = 0; i < get_mb_rx_first(priv); i++) { 3519e0a2d1cSMarc Kleine-Budde set_mb_mode(priv, i, AT91_MB_MODE_DISABLED); 3523a5655a5SMarc Kleine-Budde at91_write(priv, AT91_MID(i), reg_mid); 3533a5655a5SMarc Kleine-Budde at91_write(priv, AT91_MCR(i), 0x0); /* clear dlc */ 3543a5655a5SMarc Kleine-Budde } 3553a5655a5SMarc Kleine-Budde 356d3d47264SMarc Kleine-Budde for (i = get_mb_rx_first(priv); i < get_mb_rx_last(priv); i++) 35799c4a634SDavid S. Miller set_mb_mode(priv, i, AT91_MB_MODE_RX); 358d3d47264SMarc Kleine-Budde set_mb_mode(priv, get_mb_rx_last(priv), AT91_MB_MODE_RX_OVRWR); 35999c4a634SDavid S. Miller 3608a0e0a49SMarc Kleine-Budde /* reset acceptance mask and id register */ 361d3d47264SMarc Kleine-Budde for (i = get_mb_rx_first(priv); i <= get_mb_rx_last(priv); i++) { 3628a0e0a49SMarc Kleine-Budde at91_write(priv, AT91_MAM(i), 0x0); 3638a0e0a49SMarc Kleine-Budde at91_write(priv, AT91_MID(i), AT91_MID_MIDE); 3648a0e0a49SMarc Kleine-Budde } 3658a0e0a49SMarc Kleine-Budde 36699c4a634SDavid S. Miller /* The last 4 mailboxes are used for transmitting. */ 36779008997SMarc Kleine-Budde for (i = get_mb_tx_first(priv); i <= get_mb_tx_last(priv); i++) 36899c4a634SDavid S. Miller set_mb_mode_prio(priv, i, AT91_MB_MODE_TX, 0); 36999c4a634SDavid S. Miller 37099c4a634SDavid S. Miller /* Reset tx and rx helper pointers */ 3710909c1ecSMarc Kleine-Budde priv->tx_next = priv->tx_echo = 0; 372d3d47264SMarc Kleine-Budde priv->rx_next = get_mb_rx_first(priv); 37399c4a634SDavid S. Miller } 37499c4a634SDavid S. Miller 37599c4a634SDavid S. Miller static int at91_set_bittiming(struct net_device *dev) 37699c4a634SDavid S. Miller { 37799c4a634SDavid S. Miller const struct at91_priv *priv = netdev_priv(dev); 37899c4a634SDavid S. Miller const struct can_bittiming *bt = &priv->can.bittiming; 37999c4a634SDavid S. Miller u32 reg_br; 38099c4a634SDavid S. Miller 381dbe91325SMarc Kleine-Budde reg_br = ((priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) ? 1 << 24 : 0) | 38299c4a634SDavid S. Miller ((bt->brp - 1) << 16) | ((bt->sjw - 1) << 12) | 38399c4a634SDavid S. Miller ((bt->prop_seg - 1) << 8) | ((bt->phase_seg1 - 1) << 4) | 38499c4a634SDavid S. Miller ((bt->phase_seg2 - 1) << 0); 38599c4a634SDavid S. Miller 386882055c8SMarc Kleine-Budde netdev_info(dev, "writing AT91_BR: 0x%08x\n", reg_br); 38799c4a634SDavid S. Miller 38899c4a634SDavid S. Miller at91_write(priv, AT91_BR, reg_br); 38999c4a634SDavid S. Miller 39099c4a634SDavid S. Miller return 0; 39199c4a634SDavid S. Miller } 39299c4a634SDavid S. Miller 39333a6f298SMarc Kleine-Budde static int at91_get_berr_counter(const struct net_device *dev, 39433a6f298SMarc Kleine-Budde struct can_berr_counter *bec) 39533a6f298SMarc Kleine-Budde { 39633a6f298SMarc Kleine-Budde const struct at91_priv *priv = netdev_priv(dev); 39733a6f298SMarc Kleine-Budde u32 reg_ecr = at91_read(priv, AT91_ECR); 39833a6f298SMarc Kleine-Budde 39933a6f298SMarc Kleine-Budde bec->rxerr = reg_ecr & 0xff; 40033a6f298SMarc Kleine-Budde bec->txerr = reg_ecr >> 16; 40133a6f298SMarc Kleine-Budde 40233a6f298SMarc Kleine-Budde return 0; 40333a6f298SMarc Kleine-Budde } 40433a6f298SMarc Kleine-Budde 40599c4a634SDavid S. Miller static void at91_chip_start(struct net_device *dev) 40699c4a634SDavid S. Miller { 40799c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 40899c4a634SDavid S. Miller u32 reg_mr, reg_ier; 40999c4a634SDavid S. Miller 41099c4a634SDavid S. Miller /* disable interrupts */ 41199c4a634SDavid S. Miller at91_write(priv, AT91_IDR, AT91_IRQ_ALL); 41299c4a634SDavid S. Miller 41399c4a634SDavid S. Miller /* disable chip */ 41499c4a634SDavid S. Miller reg_mr = at91_read(priv, AT91_MR); 41599c4a634SDavid S. Miller at91_write(priv, AT91_MR, reg_mr & ~AT91_MR_CANEN); 41699c4a634SDavid S. Miller 417b156fd04SMarc Kleine-Budde at91_set_bittiming(dev); 41899c4a634SDavid S. Miller at91_setup_mailboxes(dev); 41999c4a634SDavid S. Miller at91_transceiver_switch(priv, 1); 42099c4a634SDavid S. Miller 42199c4a634SDavid S. Miller /* enable chip */ 42217a50ee4SYoann DI RUZZA if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) 42317a50ee4SYoann DI RUZZA reg_mr = AT91_MR_CANEN | AT91_MR_ABM; 42417a50ee4SYoann DI RUZZA else 42517a50ee4SYoann DI RUZZA reg_mr = AT91_MR_CANEN; 42617a50ee4SYoann DI RUZZA at91_write(priv, AT91_MR, reg_mr); 42799c4a634SDavid S. Miller 42899c4a634SDavid S. Miller priv->can.state = CAN_STATE_ERROR_ACTIVE; 42999c4a634SDavid S. Miller 43099c4a634SDavid S. Miller /* Enable interrupts */ 43179008997SMarc Kleine-Budde reg_ier = get_irq_mb_rx(priv) | AT91_IRQ_ERRP | AT91_IRQ_ERR_FRAME; 43299c4a634SDavid S. Miller at91_write(priv, AT91_IDR, AT91_IRQ_ALL); 43399c4a634SDavid S. Miller at91_write(priv, AT91_IER, reg_ier); 43499c4a634SDavid S. Miller } 43599c4a634SDavid S. Miller 43699c4a634SDavid S. Miller static void at91_chip_stop(struct net_device *dev, enum can_state state) 43799c4a634SDavid S. Miller { 43899c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 43999c4a634SDavid S. Miller u32 reg_mr; 44099c4a634SDavid S. Miller 44199c4a634SDavid S. Miller /* disable interrupts */ 44299c4a634SDavid S. Miller at91_write(priv, AT91_IDR, AT91_IRQ_ALL); 44399c4a634SDavid S. Miller 44499c4a634SDavid S. Miller reg_mr = at91_read(priv, AT91_MR); 44599c4a634SDavid S. Miller at91_write(priv, AT91_MR, reg_mr & ~AT91_MR_CANEN); 44699c4a634SDavid S. Miller 44799c4a634SDavid S. Miller at91_transceiver_switch(priv, 0); 44899c4a634SDavid S. Miller priv->can.state = state; 44999c4a634SDavid S. Miller } 45099c4a634SDavid S. Miller 45199c4a634SDavid S. Miller /* 45299c4a634SDavid S. Miller * theory of operation: 45399c4a634SDavid S. Miller * 45499c4a634SDavid S. Miller * According to the datasheet priority 0 is the highest priority, 15 45599c4a634SDavid S. Miller * is the lowest. If two mailboxes have the same priority level the 45699c4a634SDavid S. Miller * message of the mailbox with the lowest number is sent first. 45799c4a634SDavid S. Miller * 45899c4a634SDavid S. Miller * We use the first TX mailbox (AT91_MB_TX_FIRST) with prio 0, then 45999c4a634SDavid S. Miller * the next mailbox with prio 0, and so on, until all mailboxes are 46099c4a634SDavid S. Miller * used. Then we start from the beginning with mailbox 46199c4a634SDavid S. Miller * AT91_MB_TX_FIRST, but with prio 1, mailbox AT91_MB_TX_FIRST + 1 46299c4a634SDavid S. Miller * prio 1. When we reach the last mailbox with prio 15, we have to 46399c4a634SDavid S. Miller * stop sending, waiting for all messages to be delivered, then start 46499c4a634SDavid S. Miller * again with mailbox AT91_MB_TX_FIRST prio 0. 46599c4a634SDavid S. Miller * 46699c4a634SDavid S. Miller * We use the priv->tx_next as counter for the next transmission 46799c4a634SDavid S. Miller * mailbox, but without the offset AT91_MB_TX_FIRST. The lower bits 46899c4a634SDavid S. Miller * encode the mailbox number, the upper 4 bits the mailbox priority: 46999c4a634SDavid S. Miller * 470d3d47264SMarc Kleine-Budde * priv->tx_next = (prio << get_next_prio_shift(priv)) | 471d3d47264SMarc Kleine-Budde * (mb - get_mb_tx_first(priv)); 47299c4a634SDavid S. Miller * 47399c4a634SDavid S. Miller */ 47499c4a634SDavid S. Miller static netdev_tx_t at91_start_xmit(struct sk_buff *skb, struct net_device *dev) 47599c4a634SDavid S. Miller { 47699c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 47799c4a634SDavid S. Miller struct net_device_stats *stats = &dev->stats; 47899c4a634SDavid S. Miller struct can_frame *cf = (struct can_frame *)skb->data; 47999c4a634SDavid S. Miller unsigned int mb, prio; 48099c4a634SDavid S. Miller u32 reg_mid, reg_mcr; 48199c4a634SDavid S. Miller 4823ccd4c61SOliver Hartkopp if (can_dropped_invalid_skb(dev, skb)) 4833ccd4c61SOliver Hartkopp return NETDEV_TX_OK; 4843ccd4c61SOliver Hartkopp 48599c4a634SDavid S. Miller mb = get_tx_next_mb(priv); 48699c4a634SDavid S. Miller prio = get_tx_next_prio(priv); 48799c4a634SDavid S. Miller 48899c4a634SDavid S. Miller if (unlikely(!(at91_read(priv, AT91_MSR(mb)) & AT91_MSR_MRDY))) { 48999c4a634SDavid S. Miller netif_stop_queue(dev); 49099c4a634SDavid S. Miller 491882055c8SMarc Kleine-Budde netdev_err(dev, "BUG! TX buffer full when queue awake!\n"); 49299c4a634SDavid S. Miller return NETDEV_TX_BUSY; 49399c4a634SDavid S. Miller } 4943a5655a5SMarc Kleine-Budde reg_mid = at91_can_id_to_reg_mid(cf->can_id); 49599c4a634SDavid S. Miller reg_mcr = ((cf->can_id & CAN_RTR_FLAG) ? AT91_MCR_MRTR : 0) | 49699c4a634SDavid S. Miller (cf->can_dlc << 16) | AT91_MCR_MTCR; 49799c4a634SDavid S. Miller 49899c4a634SDavid S. Miller /* disable MB while writing ID (see datasheet) */ 49999c4a634SDavid S. Miller set_mb_mode(priv, mb, AT91_MB_MODE_DISABLED); 50099c4a634SDavid S. Miller at91_write(priv, AT91_MID(mb), reg_mid); 50199c4a634SDavid S. Miller set_mb_mode_prio(priv, mb, AT91_MB_MODE_TX, prio); 50299c4a634SDavid S. Miller 50399c4a634SDavid S. Miller at91_write(priv, AT91_MDL(mb), *(u32 *)(cf->data + 0)); 50499c4a634SDavid S. Miller at91_write(priv, AT91_MDH(mb), *(u32 *)(cf->data + 4)); 50599c4a634SDavid S. Miller 50699c4a634SDavid S. Miller /* This triggers transmission */ 50799c4a634SDavid S. Miller at91_write(priv, AT91_MCR(mb), reg_mcr); 50899c4a634SDavid S. Miller 50999c4a634SDavid S. Miller stats->tx_bytes += cf->can_dlc; 51099c4a634SDavid S. Miller 51125985edcSLucas De Marchi /* _NOTE_: subtract AT91_MB_TX_FIRST offset from mb! */ 51279008997SMarc Kleine-Budde can_put_echo_skb(skb, dev, mb - get_mb_tx_first(priv)); 51399c4a634SDavid S. Miller 51499c4a634SDavid S. Miller /* 51599c4a634SDavid S. Miller * we have to stop the queue and deliver all messages in case 51699c4a634SDavid S. Miller * of a prio+mb counter wrap around. This is the case if 51799c4a634SDavid S. Miller * tx_next buffer prio and mailbox equals 0. 51899c4a634SDavid S. Miller * 51999c4a634SDavid S. Miller * also stop the queue if next buffer is still in use 52099c4a634SDavid S. Miller * (== not ready) 52199c4a634SDavid S. Miller */ 52299c4a634SDavid S. Miller priv->tx_next++; 52399c4a634SDavid S. Miller if (!(at91_read(priv, AT91_MSR(get_tx_next_mb(priv))) & 52499c4a634SDavid S. Miller AT91_MSR_MRDY) || 52579008997SMarc Kleine-Budde (priv->tx_next & get_next_mask(priv)) == 0) 52699c4a634SDavid S. Miller netif_stop_queue(dev); 52799c4a634SDavid S. Miller 52899c4a634SDavid S. Miller /* Enable interrupt for this mailbox */ 52999c4a634SDavid S. Miller at91_write(priv, AT91_IER, 1 << mb); 53099c4a634SDavid S. Miller 53199c4a634SDavid S. Miller return NETDEV_TX_OK; 53299c4a634SDavid S. Miller } 53399c4a634SDavid S. Miller 53499c4a634SDavid S. Miller /** 53599c4a634SDavid S. Miller * at91_activate_rx_low - activate lower rx mailboxes 53699c4a634SDavid S. Miller * @priv: a91 context 53799c4a634SDavid S. Miller * 53899c4a634SDavid S. Miller * Reenables the lower mailboxes for reception of new CAN messages 53999c4a634SDavid S. Miller */ 54099c4a634SDavid S. Miller static inline void at91_activate_rx_low(const struct at91_priv *priv) 54199c4a634SDavid S. Miller { 54279008997SMarc Kleine-Budde u32 mask = get_mb_rx_low_mask(priv); 54399c4a634SDavid S. Miller at91_write(priv, AT91_TCR, mask); 54499c4a634SDavid S. Miller } 54599c4a634SDavid S. Miller 54699c4a634SDavid S. Miller /** 54799c4a634SDavid S. Miller * at91_activate_rx_mb - reactive single rx mailbox 54899c4a634SDavid S. Miller * @priv: a91 context 54999c4a634SDavid S. Miller * @mb: mailbox to reactivate 55099c4a634SDavid S. Miller * 55199c4a634SDavid S. Miller * Reenables given mailbox for reception of new CAN messages 55299c4a634SDavid S. Miller */ 55399c4a634SDavid S. Miller static inline void at91_activate_rx_mb(const struct at91_priv *priv, 55499c4a634SDavid S. Miller unsigned int mb) 55599c4a634SDavid S. Miller { 55699c4a634SDavid S. Miller u32 mask = 1 << mb; 55799c4a634SDavid S. Miller at91_write(priv, AT91_TCR, mask); 55899c4a634SDavid S. Miller } 55999c4a634SDavid S. Miller 56099c4a634SDavid S. Miller /** 56199c4a634SDavid S. Miller * at91_rx_overflow_err - send error frame due to rx overflow 56299c4a634SDavid S. Miller * @dev: net device 56399c4a634SDavid S. Miller */ 56499c4a634SDavid S. Miller static void at91_rx_overflow_err(struct net_device *dev) 56599c4a634SDavid S. Miller { 56699c4a634SDavid S. Miller struct net_device_stats *stats = &dev->stats; 56799c4a634SDavid S. Miller struct sk_buff *skb; 56899c4a634SDavid S. Miller struct can_frame *cf; 56999c4a634SDavid S. Miller 570882055c8SMarc Kleine-Budde netdev_dbg(dev, "RX buffer overflow\n"); 57199c4a634SDavid S. Miller stats->rx_over_errors++; 57299c4a634SDavid S. Miller stats->rx_errors++; 57399c4a634SDavid S. Miller 57499c4a634SDavid S. Miller skb = alloc_can_err_skb(dev, &cf); 57599c4a634SDavid S. Miller if (unlikely(!skb)) 57699c4a634SDavid S. Miller return; 57799c4a634SDavid S. Miller 57899c4a634SDavid S. Miller cf->can_id |= CAN_ERR_CRTL; 57999c4a634SDavid S. Miller cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; 58099c4a634SDavid S. Miller 58199c4a634SDavid S. Miller stats->rx_packets++; 58299c4a634SDavid S. Miller stats->rx_bytes += cf->can_dlc; 583*6ae3673dSMarc Kleine-Budde netif_receive_skb(skb); 58499c4a634SDavid S. Miller } 58599c4a634SDavid S. Miller 58699c4a634SDavid S. Miller /** 58799c4a634SDavid S. Miller * at91_read_mb - read CAN msg from mailbox (lowlevel impl) 58899c4a634SDavid S. Miller * @dev: net device 58999c4a634SDavid S. Miller * @mb: mailbox number to read from 59099c4a634SDavid S. Miller * @cf: can frame where to store message 59199c4a634SDavid S. Miller * 59299c4a634SDavid S. Miller * Reads a CAN message from the given mailbox and stores data into 59399c4a634SDavid S. Miller * given can frame. "mb" and "cf" must be valid. 59499c4a634SDavid S. Miller */ 59599c4a634SDavid S. Miller static void at91_read_mb(struct net_device *dev, unsigned int mb, 59699c4a634SDavid S. Miller struct can_frame *cf) 59799c4a634SDavid S. Miller { 59899c4a634SDavid S. Miller const struct at91_priv *priv = netdev_priv(dev); 59999c4a634SDavid S. Miller u32 reg_msr, reg_mid; 60099c4a634SDavid S. Miller 60199c4a634SDavid S. Miller reg_mid = at91_read(priv, AT91_MID(mb)); 60299c4a634SDavid S. Miller if (reg_mid & AT91_MID_MIDE) 60399c4a634SDavid S. Miller cf->can_id = ((reg_mid >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG; 60499c4a634SDavid S. Miller else 60599c4a634SDavid S. Miller cf->can_id = (reg_mid >> 18) & CAN_SFF_MASK; 60699c4a634SDavid S. Miller 60799c4a634SDavid S. Miller reg_msr = at91_read(priv, AT91_MSR(mb)); 608c7cd606fSOliver Hartkopp cf->can_dlc = get_can_dlc((reg_msr >> 16) & 0xf); 60999c4a634SDavid S. Miller 610e14ee40bSMarc Kleine-Budde if (reg_msr & AT91_MSR_MRTR) 611e14ee40bSMarc Kleine-Budde cf->can_id |= CAN_RTR_FLAG; 612e14ee40bSMarc Kleine-Budde else { 61399c4a634SDavid S. Miller *(u32 *)(cf->data + 0) = at91_read(priv, AT91_MDL(mb)); 61499c4a634SDavid S. Miller *(u32 *)(cf->data + 4) = at91_read(priv, AT91_MDH(mb)); 615e14ee40bSMarc Kleine-Budde } 61699c4a634SDavid S. Miller 6178a0e0a49SMarc Kleine-Budde /* allow RX of extended frames */ 6188a0e0a49SMarc Kleine-Budde at91_write(priv, AT91_MID(mb), AT91_MID_MIDE); 6198a0e0a49SMarc Kleine-Budde 620d3d47264SMarc Kleine-Budde if (unlikely(mb == get_mb_rx_last(priv) && reg_msr & AT91_MSR_MMI)) 62199c4a634SDavid S. Miller at91_rx_overflow_err(dev); 62299c4a634SDavid S. Miller } 62399c4a634SDavid S. Miller 62499c4a634SDavid S. Miller /** 62599c4a634SDavid S. Miller * at91_read_msg - read CAN message from mailbox 62699c4a634SDavid S. Miller * @dev: net device 62799c4a634SDavid S. Miller * @mb: mail box to read from 62899c4a634SDavid S. Miller * 62999c4a634SDavid S. Miller * Reads a CAN message from given mailbox, and put into linux network 63099c4a634SDavid S. Miller * RX queue, does all housekeeping chores (stats, ...) 63199c4a634SDavid S. Miller */ 63299c4a634SDavid S. Miller static void at91_read_msg(struct net_device *dev, unsigned int mb) 63399c4a634SDavid S. Miller { 63499c4a634SDavid S. Miller struct net_device_stats *stats = &dev->stats; 63599c4a634SDavid S. Miller struct can_frame *cf; 63699c4a634SDavid S. Miller struct sk_buff *skb; 63799c4a634SDavid S. Miller 63899c4a634SDavid S. Miller skb = alloc_can_skb(dev, &cf); 63999c4a634SDavid S. Miller if (unlikely(!skb)) { 64099c4a634SDavid S. Miller stats->rx_dropped++; 64199c4a634SDavid S. Miller return; 64299c4a634SDavid S. Miller } 64399c4a634SDavid S. Miller 64499c4a634SDavid S. Miller at91_read_mb(dev, mb, cf); 64599c4a634SDavid S. Miller 64699c4a634SDavid S. Miller stats->rx_packets++; 64799c4a634SDavid S. Miller stats->rx_bytes += cf->can_dlc; 648*6ae3673dSMarc Kleine-Budde netif_receive_skb(skb); 6494723f2b8SFabio Baltieri 6504723f2b8SFabio Baltieri can_led_event(dev, CAN_LED_EVENT_RX); 65199c4a634SDavid S. Miller } 65299c4a634SDavid S. Miller 65399c4a634SDavid S. Miller /** 65499c4a634SDavid S. Miller * at91_poll_rx - read multiple CAN messages from mailboxes 65599c4a634SDavid S. Miller * @dev: net device 65699c4a634SDavid S. Miller * @quota: max number of pkgs we're allowed to receive 65799c4a634SDavid S. Miller * 65899c4a634SDavid S. Miller * Theory of Operation: 65999c4a634SDavid S. Miller * 660d3d47264SMarc Kleine-Budde * About 3/4 of the mailboxes (get_mb_rx_first()...get_mb_rx_last()) 661d3d47264SMarc Kleine-Budde * on the chip are reserved for RX. We split them into 2 groups. The 662d3d47264SMarc Kleine-Budde * lower group ranges from get_mb_rx_first() to get_mb_rx_low_last(). 66399c4a634SDavid S. Miller * 66499c4a634SDavid S. Miller * Like it or not, but the chip always saves a received CAN message 66599c4a634SDavid S. Miller * into the first free mailbox it finds (starting with the 66699c4a634SDavid S. Miller * lowest). This makes it very difficult to read the messages in the 66799c4a634SDavid S. Miller * right order from the chip. This is how we work around that problem: 66899c4a634SDavid S. Miller * 6699e0a2d1cSMarc Kleine-Budde * The first message goes into mb nr. 1 and issues an interrupt. All 67099c4a634SDavid S. Miller * rx ints are disabled in the interrupt handler and a napi poll is 67199c4a634SDavid S. Miller * scheduled. We read the mailbox, but do _not_ reenable the mb (to 67299c4a634SDavid S. Miller * receive another message). 67399c4a634SDavid S. Miller * 67499c4a634SDavid S. Miller * lower mbxs upper 6759e0a2d1cSMarc Kleine-Budde * ____^______ __^__ 67699c4a634SDavid S. Miller * / \ / \ 67799c4a634SDavid S. Miller * +-+-+-+-+-+-+-+-++-+-+-+-+ 6789e0a2d1cSMarc Kleine-Budde * | |x|x|x|x|x|x|x|| | | | | 67999c4a634SDavid S. Miller * +-+-+-+-+-+-+-+-++-+-+-+-+ 68099c4a634SDavid S. Miller * 0 0 0 0 0 0 0 0 0 0 1 1 \ mail 68199c4a634SDavid S. Miller * 0 1 2 3 4 5 6 7 8 9 0 1 / box 6829e0a2d1cSMarc Kleine-Budde * ^ 6839e0a2d1cSMarc Kleine-Budde * | 6849e0a2d1cSMarc Kleine-Budde * \ 6859e0a2d1cSMarc Kleine-Budde * unused, due to chip bug 68699c4a634SDavid S. Miller * 68799c4a634SDavid S. Miller * The variable priv->rx_next points to the next mailbox to read a 68899c4a634SDavid S. Miller * message from. As long we're in the lower mailboxes we just read the 68999c4a634SDavid S. Miller * mailbox but not reenable it. 69099c4a634SDavid S. Miller * 69199c4a634SDavid S. Miller * With completion of the last of the lower mailboxes, we reenable the 69299c4a634SDavid S. Miller * whole first group, but continue to look for filled mailboxes in the 69399c4a634SDavid S. Miller * upper mailboxes. Imagine the second group like overflow mailboxes, 69499c4a634SDavid S. Miller * which takes CAN messages if the lower goup is full. While in the 69599c4a634SDavid S. Miller * upper group we reenable the mailbox right after reading it. Giving 69699c4a634SDavid S. Miller * the chip more room to store messages. 69799c4a634SDavid S. Miller * 69899c4a634SDavid S. Miller * After finishing we look again in the lower group if we've still 69999c4a634SDavid S. Miller * quota. 70099c4a634SDavid S. Miller * 70199c4a634SDavid S. Miller */ 70299c4a634SDavid S. Miller static int at91_poll_rx(struct net_device *dev, int quota) 70399c4a634SDavid S. Miller { 70499c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 70599c4a634SDavid S. Miller u32 reg_sr = at91_read(priv, AT91_SR); 70699c4a634SDavid S. Miller const unsigned long *addr = (unsigned long *)®_sr; 70799c4a634SDavid S. Miller unsigned int mb; 70899c4a634SDavid S. Miller int received = 0; 70999c4a634SDavid S. Miller 71079008997SMarc Kleine-Budde if (priv->rx_next > get_mb_rx_low_last(priv) && 71179008997SMarc Kleine-Budde reg_sr & get_mb_rx_low_mask(priv)) 712882055c8SMarc Kleine-Budde netdev_info(dev, 71399c4a634SDavid S. Miller "order of incoming frames cannot be guaranteed\n"); 71499c4a634SDavid S. Miller 71599c4a634SDavid S. Miller again: 71679008997SMarc Kleine-Budde for (mb = find_next_bit(addr, get_mb_tx_first(priv), priv->rx_next); 71779008997SMarc Kleine-Budde mb < get_mb_tx_first(priv) && quota > 0; 71899c4a634SDavid S. Miller reg_sr = at91_read(priv, AT91_SR), 71979008997SMarc Kleine-Budde mb = find_next_bit(addr, get_mb_tx_first(priv), ++priv->rx_next)) { 72099c4a634SDavid S. Miller at91_read_msg(dev, mb); 72199c4a634SDavid S. Miller 72299c4a634SDavid S. Miller /* reactivate mailboxes */ 72379008997SMarc Kleine-Budde if (mb == get_mb_rx_low_last(priv)) 72499c4a634SDavid S. Miller /* all lower mailboxed, if just finished it */ 72599c4a634SDavid S. Miller at91_activate_rx_low(priv); 72679008997SMarc Kleine-Budde else if (mb > get_mb_rx_low_last(priv)) 72799c4a634SDavid S. Miller /* only the mailbox we read */ 72899c4a634SDavid S. Miller at91_activate_rx_mb(priv, mb); 72999c4a634SDavid S. Miller 73099c4a634SDavid S. Miller received++; 73199c4a634SDavid S. Miller quota--; 73299c4a634SDavid S. Miller } 73399c4a634SDavid S. Miller 73499c4a634SDavid S. Miller /* upper group completed, look again in lower */ 73579008997SMarc Kleine-Budde if (priv->rx_next > get_mb_rx_low_last(priv) && 736d3d47264SMarc Kleine-Budde quota > 0 && mb > get_mb_rx_last(priv)) { 737d3d47264SMarc Kleine-Budde priv->rx_next = get_mb_rx_first(priv); 73899c4a634SDavid S. Miller goto again; 73999c4a634SDavid S. Miller } 74099c4a634SDavid S. Miller 74199c4a634SDavid S. Miller return received; 74299c4a634SDavid S. Miller } 74399c4a634SDavid S. Miller 74499c4a634SDavid S. Miller static void at91_poll_err_frame(struct net_device *dev, 74599c4a634SDavid S. Miller struct can_frame *cf, u32 reg_sr) 74699c4a634SDavid S. Miller { 74799c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 74899c4a634SDavid S. Miller 74999c4a634SDavid S. Miller /* CRC error */ 75099c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_CERR) { 751882055c8SMarc Kleine-Budde netdev_dbg(dev, "CERR irq\n"); 75299c4a634SDavid S. Miller dev->stats.rx_errors++; 75399c4a634SDavid S. Miller priv->can.can_stats.bus_error++; 75499c4a634SDavid S. Miller cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; 75599c4a634SDavid S. Miller } 75699c4a634SDavid S. Miller 75799c4a634SDavid S. Miller /* Stuffing Error */ 75899c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_SERR) { 759882055c8SMarc Kleine-Budde netdev_dbg(dev, "SERR irq\n"); 76099c4a634SDavid S. Miller dev->stats.rx_errors++; 76199c4a634SDavid S. Miller priv->can.can_stats.bus_error++; 76299c4a634SDavid S. Miller cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; 76399c4a634SDavid S. Miller cf->data[2] |= CAN_ERR_PROT_STUFF; 76499c4a634SDavid S. Miller } 76599c4a634SDavid S. Miller 76699c4a634SDavid S. Miller /* Acknowledgement Error */ 76799c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_AERR) { 768882055c8SMarc Kleine-Budde netdev_dbg(dev, "AERR irq\n"); 76999c4a634SDavid S. Miller dev->stats.tx_errors++; 77099c4a634SDavid S. Miller cf->can_id |= CAN_ERR_ACK; 77199c4a634SDavid S. Miller } 77299c4a634SDavid S. Miller 77399c4a634SDavid S. Miller /* Form error */ 77499c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_FERR) { 775882055c8SMarc Kleine-Budde netdev_dbg(dev, "FERR irq\n"); 77699c4a634SDavid S. Miller dev->stats.rx_errors++; 77799c4a634SDavid S. Miller priv->can.can_stats.bus_error++; 77899c4a634SDavid S. Miller cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; 77999c4a634SDavid S. Miller cf->data[2] |= CAN_ERR_PROT_FORM; 78099c4a634SDavid S. Miller } 78199c4a634SDavid S. Miller 78299c4a634SDavid S. Miller /* Bit Error */ 78399c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_BERR) { 784882055c8SMarc Kleine-Budde netdev_dbg(dev, "BERR irq\n"); 78599c4a634SDavid S. Miller dev->stats.tx_errors++; 78699c4a634SDavid S. Miller priv->can.can_stats.bus_error++; 78799c4a634SDavid S. Miller cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; 78899c4a634SDavid S. Miller cf->data[2] |= CAN_ERR_PROT_BIT; 78999c4a634SDavid S. Miller } 79099c4a634SDavid S. Miller } 79199c4a634SDavid S. Miller 79299c4a634SDavid S. Miller static int at91_poll_err(struct net_device *dev, int quota, u32 reg_sr) 79399c4a634SDavid S. Miller { 79499c4a634SDavid S. Miller struct sk_buff *skb; 79599c4a634SDavid S. Miller struct can_frame *cf; 79699c4a634SDavid S. Miller 79799c4a634SDavid S. Miller if (quota == 0) 79899c4a634SDavid S. Miller return 0; 79999c4a634SDavid S. Miller 80099c4a634SDavid S. Miller skb = alloc_can_err_skb(dev, &cf); 80199c4a634SDavid S. Miller if (unlikely(!skb)) 80299c4a634SDavid S. Miller return 0; 80399c4a634SDavid S. Miller 80499c4a634SDavid S. Miller at91_poll_err_frame(dev, cf, reg_sr); 80599c4a634SDavid S. Miller 80699c4a634SDavid S. Miller dev->stats.rx_packets++; 80799c4a634SDavid S. Miller dev->stats.rx_bytes += cf->can_dlc; 808*6ae3673dSMarc Kleine-Budde netif_receive_skb(skb); 80999c4a634SDavid S. Miller 81099c4a634SDavid S. Miller return 1; 81199c4a634SDavid S. Miller } 81299c4a634SDavid S. Miller 81399c4a634SDavid S. Miller static int at91_poll(struct napi_struct *napi, int quota) 81499c4a634SDavid S. Miller { 81599c4a634SDavid S. Miller struct net_device *dev = napi->dev; 81699c4a634SDavid S. Miller const struct at91_priv *priv = netdev_priv(dev); 81799c4a634SDavid S. Miller u32 reg_sr = at91_read(priv, AT91_SR); 81899c4a634SDavid S. Miller int work_done = 0; 81999c4a634SDavid S. Miller 82079008997SMarc Kleine-Budde if (reg_sr & get_irq_mb_rx(priv)) 82199c4a634SDavid S. Miller work_done += at91_poll_rx(dev, quota - work_done); 82299c4a634SDavid S. Miller 82399c4a634SDavid S. Miller /* 82499c4a634SDavid S. Miller * The error bits are clear on read, 82599c4a634SDavid S. Miller * so use saved value from irq handler. 82699c4a634SDavid S. Miller */ 82799c4a634SDavid S. Miller reg_sr |= priv->reg_sr; 82899c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_ERR_FRAME) 82999c4a634SDavid S. Miller work_done += at91_poll_err(dev, quota - work_done, reg_sr); 83099c4a634SDavid S. Miller 83199c4a634SDavid S. Miller if (work_done < quota) { 83299c4a634SDavid S. Miller /* enable IRQs for frame errors and all mailboxes >= rx_next */ 83399c4a634SDavid S. Miller u32 reg_ier = AT91_IRQ_ERR_FRAME; 83479008997SMarc Kleine-Budde reg_ier |= get_irq_mb_rx(priv) & ~AT91_MB_MASK(priv->rx_next); 83599c4a634SDavid S. Miller 83699c4a634SDavid S. Miller napi_complete(napi); 83799c4a634SDavid S. Miller at91_write(priv, AT91_IER, reg_ier); 83899c4a634SDavid S. Miller } 83999c4a634SDavid S. Miller 84099c4a634SDavid S. Miller return work_done; 84199c4a634SDavid S. Miller } 84299c4a634SDavid S. Miller 84399c4a634SDavid S. Miller /* 84499c4a634SDavid S. Miller * theory of operation: 84599c4a634SDavid S. Miller * 84699c4a634SDavid S. Miller * priv->tx_echo holds the number of the oldest can_frame put for 84799c4a634SDavid S. Miller * transmission into the hardware, but not yet ACKed by the CAN tx 84899c4a634SDavid S. Miller * complete IRQ. 84999c4a634SDavid S. Miller * 85099c4a634SDavid S. Miller * We iterate from priv->tx_echo to priv->tx_next and check if the 85199c4a634SDavid S. Miller * packet has been transmitted, echo it back to the CAN framework. If 85299c4a634SDavid S. Miller * we discover a not yet transmitted package, stop looking for more. 85399c4a634SDavid S. Miller * 85499c4a634SDavid S. Miller */ 85599c4a634SDavid S. Miller static void at91_irq_tx(struct net_device *dev, u32 reg_sr) 85699c4a634SDavid S. Miller { 85799c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 85899c4a634SDavid S. Miller u32 reg_msr; 85999c4a634SDavid S. Miller unsigned int mb; 86099c4a634SDavid S. Miller 86199c4a634SDavid S. Miller /* masking of reg_sr not needed, already done by at91_irq */ 86299c4a634SDavid S. Miller 86399c4a634SDavid S. Miller for (/* nix */; (priv->tx_next - priv->tx_echo) > 0; priv->tx_echo++) { 86499c4a634SDavid S. Miller mb = get_tx_echo_mb(priv); 86599c4a634SDavid S. Miller 86699c4a634SDavid S. Miller /* no event in mailbox? */ 86799c4a634SDavid S. Miller if (!(reg_sr & (1 << mb))) 86899c4a634SDavid S. Miller break; 86999c4a634SDavid S. Miller 87099c4a634SDavid S. Miller /* Disable irq for this TX mailbox */ 87199c4a634SDavid S. Miller at91_write(priv, AT91_IDR, 1 << mb); 87299c4a634SDavid S. Miller 87399c4a634SDavid S. Miller /* 87499c4a634SDavid S. Miller * only echo if mailbox signals us a transfer 87599c4a634SDavid S. Miller * complete (MSR_MRDY). Otherwise it's a tansfer 87699c4a634SDavid S. Miller * abort. "can_bus_off()" takes care about the skbs 87799c4a634SDavid S. Miller * parked in the echo queue. 87899c4a634SDavid S. Miller */ 87999c4a634SDavid S. Miller reg_msr = at91_read(priv, AT91_MSR(mb)); 88099c4a634SDavid S. Miller if (likely(reg_msr & AT91_MSR_MRDY && 88199c4a634SDavid S. Miller ~reg_msr & AT91_MSR_MABT)) { 88225985edcSLucas De Marchi /* _NOTE_: subtract AT91_MB_TX_FIRST offset from mb! */ 88379008997SMarc Kleine-Budde can_get_echo_skb(dev, mb - get_mb_tx_first(priv)); 88499c4a634SDavid S. Miller dev->stats.tx_packets++; 8854723f2b8SFabio Baltieri can_led_event(dev, CAN_LED_EVENT_TX); 88699c4a634SDavid S. Miller } 88799c4a634SDavid S. Miller } 88899c4a634SDavid S. Miller 88999c4a634SDavid S. Miller /* 89099c4a634SDavid S. Miller * restart queue if we don't have a wrap around but restart if 89199c4a634SDavid S. Miller * we get a TX int for the last can frame directly before a 89299c4a634SDavid S. Miller * wrap around. 89399c4a634SDavid S. Miller */ 89479008997SMarc Kleine-Budde if ((priv->tx_next & get_next_mask(priv)) != 0 || 89579008997SMarc Kleine-Budde (priv->tx_echo & get_next_mask(priv)) == 0) 89699c4a634SDavid S. Miller netif_wake_queue(dev); 89799c4a634SDavid S. Miller } 89899c4a634SDavid S. Miller 89999c4a634SDavid S. Miller static void at91_irq_err_state(struct net_device *dev, 90099c4a634SDavid S. Miller struct can_frame *cf, enum can_state new_state) 90199c4a634SDavid S. Miller { 90299c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 90333a6f298SMarc Kleine-Budde u32 reg_idr = 0, reg_ier = 0; 90433a6f298SMarc Kleine-Budde struct can_berr_counter bec; 90599c4a634SDavid S. Miller 90633a6f298SMarc Kleine-Budde at91_get_berr_counter(dev, &bec); 90799c4a634SDavid S. Miller 90899c4a634SDavid S. Miller switch (priv->can.state) { 90999c4a634SDavid S. Miller case CAN_STATE_ERROR_ACTIVE: 91099c4a634SDavid S. Miller /* 91199c4a634SDavid S. Miller * from: ERROR_ACTIVE 91299c4a634SDavid S. Miller * to : ERROR_WARNING, ERROR_PASSIVE, BUS_OFF 91399c4a634SDavid S. Miller * => : there was a warning int 91499c4a634SDavid S. Miller */ 91599c4a634SDavid S. Miller if (new_state >= CAN_STATE_ERROR_WARNING && 91699c4a634SDavid S. Miller new_state <= CAN_STATE_BUS_OFF) { 917882055c8SMarc Kleine-Budde netdev_dbg(dev, "Error Warning IRQ\n"); 91899c4a634SDavid S. Miller priv->can.can_stats.error_warning++; 91999c4a634SDavid S. Miller 92099c4a634SDavid S. Miller cf->can_id |= CAN_ERR_CRTL; 92133a6f298SMarc Kleine-Budde cf->data[1] = (bec.txerr > bec.rxerr) ? 92299c4a634SDavid S. Miller CAN_ERR_CRTL_TX_WARNING : 92399c4a634SDavid S. Miller CAN_ERR_CRTL_RX_WARNING; 92499c4a634SDavid S. Miller } 92599c4a634SDavid S. Miller case CAN_STATE_ERROR_WARNING: /* fallthrough */ 92699c4a634SDavid S. Miller /* 92799c4a634SDavid S. Miller * from: ERROR_ACTIVE, ERROR_WARNING 92899c4a634SDavid S. Miller * to : ERROR_PASSIVE, BUS_OFF 92999c4a634SDavid S. Miller * => : error passive int 93099c4a634SDavid S. Miller */ 93199c4a634SDavid S. Miller if (new_state >= CAN_STATE_ERROR_PASSIVE && 93299c4a634SDavid S. Miller new_state <= CAN_STATE_BUS_OFF) { 933882055c8SMarc Kleine-Budde netdev_dbg(dev, "Error Passive IRQ\n"); 93499c4a634SDavid S. Miller priv->can.can_stats.error_passive++; 93599c4a634SDavid S. Miller 93699c4a634SDavid S. Miller cf->can_id |= CAN_ERR_CRTL; 93733a6f298SMarc Kleine-Budde cf->data[1] = (bec.txerr > bec.rxerr) ? 93899c4a634SDavid S. Miller CAN_ERR_CRTL_TX_PASSIVE : 93999c4a634SDavid S. Miller CAN_ERR_CRTL_RX_PASSIVE; 94099c4a634SDavid S. Miller } 94199c4a634SDavid S. Miller break; 94299c4a634SDavid S. Miller case CAN_STATE_BUS_OFF: 94399c4a634SDavid S. Miller /* 94499c4a634SDavid S. Miller * from: BUS_OFF 94599c4a634SDavid S. Miller * to : ERROR_ACTIVE, ERROR_WARNING, ERROR_PASSIVE 94699c4a634SDavid S. Miller */ 94799c4a634SDavid S. Miller if (new_state <= CAN_STATE_ERROR_PASSIVE) { 94899c4a634SDavid S. Miller cf->can_id |= CAN_ERR_RESTARTED; 94999c4a634SDavid S. Miller 950882055c8SMarc Kleine-Budde netdev_dbg(dev, "restarted\n"); 95199c4a634SDavid S. Miller priv->can.can_stats.restarts++; 95299c4a634SDavid S. Miller 95399c4a634SDavid S. Miller netif_carrier_on(dev); 95499c4a634SDavid S. Miller netif_wake_queue(dev); 95599c4a634SDavid S. Miller } 95699c4a634SDavid S. Miller break; 95799c4a634SDavid S. Miller default: 95899c4a634SDavid S. Miller break; 95999c4a634SDavid S. Miller } 96099c4a634SDavid S. Miller 96199c4a634SDavid S. Miller 96299c4a634SDavid S. Miller /* process state changes depending on the new state */ 96399c4a634SDavid S. Miller switch (new_state) { 96499c4a634SDavid S. Miller case CAN_STATE_ERROR_ACTIVE: 96599c4a634SDavid S. Miller /* 96699c4a634SDavid S. Miller * actually we want to enable AT91_IRQ_WARN here, but 96799c4a634SDavid S. Miller * it screws up the system under certain 96899c4a634SDavid S. Miller * circumstances. so just enable AT91_IRQ_ERRP, thus 96999c4a634SDavid S. Miller * the "fallthrough" 97099c4a634SDavid S. Miller */ 971882055c8SMarc Kleine-Budde netdev_dbg(dev, "Error Active\n"); 97299c4a634SDavid S. Miller cf->can_id |= CAN_ERR_PROT; 97399c4a634SDavid S. Miller cf->data[2] = CAN_ERR_PROT_ACTIVE; 97499c4a634SDavid S. Miller case CAN_STATE_ERROR_WARNING: /* fallthrough */ 97599c4a634SDavid S. Miller reg_idr = AT91_IRQ_ERRA | AT91_IRQ_WARN | AT91_IRQ_BOFF; 97699c4a634SDavid S. Miller reg_ier = AT91_IRQ_ERRP; 97799c4a634SDavid S. Miller break; 97899c4a634SDavid S. Miller case CAN_STATE_ERROR_PASSIVE: 97999c4a634SDavid S. Miller reg_idr = AT91_IRQ_ERRA | AT91_IRQ_WARN | AT91_IRQ_ERRP; 98099c4a634SDavid S. Miller reg_ier = AT91_IRQ_BOFF; 98199c4a634SDavid S. Miller break; 98299c4a634SDavid S. Miller case CAN_STATE_BUS_OFF: 98399c4a634SDavid S. Miller reg_idr = AT91_IRQ_ERRA | AT91_IRQ_ERRP | 98499c4a634SDavid S. Miller AT91_IRQ_WARN | AT91_IRQ_BOFF; 98599c4a634SDavid S. Miller reg_ier = 0; 98699c4a634SDavid S. Miller 98799c4a634SDavid S. Miller cf->can_id |= CAN_ERR_BUSOFF; 98899c4a634SDavid S. Miller 989882055c8SMarc Kleine-Budde netdev_dbg(dev, "bus-off\n"); 99099c4a634SDavid S. Miller netif_carrier_off(dev); 99199c4a634SDavid S. Miller priv->can.can_stats.bus_off++; 99299c4a634SDavid S. Miller 99399c4a634SDavid S. Miller /* turn off chip, if restart is disabled */ 99499c4a634SDavid S. Miller if (!priv->can.restart_ms) { 99599c4a634SDavid S. Miller at91_chip_stop(dev, CAN_STATE_BUS_OFF); 99699c4a634SDavid S. Miller return; 99799c4a634SDavid S. Miller } 99899c4a634SDavid S. Miller break; 99999c4a634SDavid S. Miller default: 100099c4a634SDavid S. Miller break; 100199c4a634SDavid S. Miller } 100299c4a634SDavid S. Miller 100399c4a634SDavid S. Miller at91_write(priv, AT91_IDR, reg_idr); 100499c4a634SDavid S. Miller at91_write(priv, AT91_IER, reg_ier); 100599c4a634SDavid S. Miller } 100699c4a634SDavid S. Miller 10076388b396SMarc Kleine-Budde static int at91_get_state_by_bec(const struct net_device *dev, 10086388b396SMarc Kleine-Budde enum can_state *state) 10096388b396SMarc Kleine-Budde { 10106388b396SMarc Kleine-Budde struct can_berr_counter bec; 10116388b396SMarc Kleine-Budde int err; 10126388b396SMarc Kleine-Budde 10136388b396SMarc Kleine-Budde err = at91_get_berr_counter(dev, &bec); 10146388b396SMarc Kleine-Budde if (err) 10156388b396SMarc Kleine-Budde return err; 10166388b396SMarc Kleine-Budde 10176388b396SMarc Kleine-Budde if (bec.txerr < 96 && bec.rxerr < 96) 10186388b396SMarc Kleine-Budde *state = CAN_STATE_ERROR_ACTIVE; 10196388b396SMarc Kleine-Budde else if (bec.txerr < 128 && bec.rxerr < 128) 10206388b396SMarc Kleine-Budde *state = CAN_STATE_ERROR_WARNING; 10216388b396SMarc Kleine-Budde else if (bec.txerr < 256 && bec.rxerr < 256) 10226388b396SMarc Kleine-Budde *state = CAN_STATE_ERROR_PASSIVE; 10236388b396SMarc Kleine-Budde else 10246388b396SMarc Kleine-Budde *state = CAN_STATE_BUS_OFF; 10256388b396SMarc Kleine-Budde 10266388b396SMarc Kleine-Budde return 0; 10276388b396SMarc Kleine-Budde } 10286388b396SMarc Kleine-Budde 10296388b396SMarc Kleine-Budde 103099c4a634SDavid S. Miller static void at91_irq_err(struct net_device *dev) 103199c4a634SDavid S. Miller { 103299c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 103399c4a634SDavid S. Miller struct sk_buff *skb; 103499c4a634SDavid S. Miller struct can_frame *cf; 103599c4a634SDavid S. Miller enum can_state new_state; 103699c4a634SDavid S. Miller u32 reg_sr; 10376388b396SMarc Kleine-Budde int err; 103899c4a634SDavid S. Miller 10396388b396SMarc Kleine-Budde if (at91_is_sam9263(priv)) { 104099c4a634SDavid S. Miller reg_sr = at91_read(priv, AT91_SR); 104199c4a634SDavid S. Miller 104299c4a634SDavid S. Miller /* we need to look at the unmasked reg_sr */ 104399c4a634SDavid S. Miller if (unlikely(reg_sr & AT91_IRQ_BOFF)) 104499c4a634SDavid S. Miller new_state = CAN_STATE_BUS_OFF; 104599c4a634SDavid S. Miller else if (unlikely(reg_sr & AT91_IRQ_ERRP)) 104699c4a634SDavid S. Miller new_state = CAN_STATE_ERROR_PASSIVE; 104799c4a634SDavid S. Miller else if (unlikely(reg_sr & AT91_IRQ_WARN)) 104899c4a634SDavid S. Miller new_state = CAN_STATE_ERROR_WARNING; 104999c4a634SDavid S. Miller else if (likely(reg_sr & AT91_IRQ_ERRA)) 105099c4a634SDavid S. Miller new_state = CAN_STATE_ERROR_ACTIVE; 105199c4a634SDavid S. Miller else { 1052882055c8SMarc Kleine-Budde netdev_err(dev, "BUG! hardware in undefined state\n"); 105399c4a634SDavid S. Miller return; 105499c4a634SDavid S. Miller } 10556388b396SMarc Kleine-Budde } else { 10566388b396SMarc Kleine-Budde err = at91_get_state_by_bec(dev, &new_state); 10576388b396SMarc Kleine-Budde if (err) 10586388b396SMarc Kleine-Budde return; 10596388b396SMarc Kleine-Budde } 106099c4a634SDavid S. Miller 106199c4a634SDavid S. Miller /* state hasn't changed */ 106299c4a634SDavid S. Miller if (likely(new_state == priv->can.state)) 106399c4a634SDavid S. Miller return; 106499c4a634SDavid S. Miller 106599c4a634SDavid S. Miller skb = alloc_can_err_skb(dev, &cf); 106699c4a634SDavid S. Miller if (unlikely(!skb)) 106799c4a634SDavid S. Miller return; 106899c4a634SDavid S. Miller 106999c4a634SDavid S. Miller at91_irq_err_state(dev, cf, new_state); 107099c4a634SDavid S. Miller 107199c4a634SDavid S. Miller dev->stats.rx_packets++; 107299c4a634SDavid S. Miller dev->stats.rx_bytes += cf->can_dlc; 1073*6ae3673dSMarc Kleine-Budde netif_rx(skb); 107499c4a634SDavid S. Miller 107599c4a634SDavid S. Miller priv->can.state = new_state; 107699c4a634SDavid S. Miller } 107799c4a634SDavid S. Miller 107899c4a634SDavid S. Miller /* 107999c4a634SDavid S. Miller * interrupt handler 108099c4a634SDavid S. Miller */ 108199c4a634SDavid S. Miller static irqreturn_t at91_irq(int irq, void *dev_id) 108299c4a634SDavid S. Miller { 108399c4a634SDavid S. Miller struct net_device *dev = dev_id; 108499c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 108599c4a634SDavid S. Miller irqreturn_t handled = IRQ_NONE; 108699c4a634SDavid S. Miller u32 reg_sr, reg_imr; 108799c4a634SDavid S. Miller 108899c4a634SDavid S. Miller reg_sr = at91_read(priv, AT91_SR); 108999c4a634SDavid S. Miller reg_imr = at91_read(priv, AT91_IMR); 109099c4a634SDavid S. Miller 109199c4a634SDavid S. Miller /* Ignore masked interrupts */ 109299c4a634SDavid S. Miller reg_sr &= reg_imr; 109399c4a634SDavid S. Miller if (!reg_sr) 109499c4a634SDavid S. Miller goto exit; 109599c4a634SDavid S. Miller 109699c4a634SDavid S. Miller handled = IRQ_HANDLED; 109799c4a634SDavid S. Miller 109899c4a634SDavid S. Miller /* Receive or error interrupt? -> napi */ 109979008997SMarc Kleine-Budde if (reg_sr & (get_irq_mb_rx(priv) | AT91_IRQ_ERR_FRAME)) { 110099c4a634SDavid S. Miller /* 110199c4a634SDavid S. Miller * The error bits are clear on read, 110299c4a634SDavid S. Miller * save for later use. 110399c4a634SDavid S. Miller */ 110499c4a634SDavid S. Miller priv->reg_sr = reg_sr; 110599c4a634SDavid S. Miller at91_write(priv, AT91_IDR, 110679008997SMarc Kleine-Budde get_irq_mb_rx(priv) | AT91_IRQ_ERR_FRAME); 110799c4a634SDavid S. Miller napi_schedule(&priv->napi); 110899c4a634SDavid S. Miller } 110999c4a634SDavid S. Miller 111099c4a634SDavid S. Miller /* Transmission complete interrupt */ 111179008997SMarc Kleine-Budde if (reg_sr & get_irq_mb_tx(priv)) 111299c4a634SDavid S. Miller at91_irq_tx(dev, reg_sr); 111399c4a634SDavid S. Miller 111499c4a634SDavid S. Miller at91_irq_err(dev); 111599c4a634SDavid S. Miller 111699c4a634SDavid S. Miller exit: 111799c4a634SDavid S. Miller return handled; 111899c4a634SDavid S. Miller } 111999c4a634SDavid S. Miller 112099c4a634SDavid S. Miller static int at91_open(struct net_device *dev) 112199c4a634SDavid S. Miller { 112299c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 112399c4a634SDavid S. Miller int err; 112499c4a634SDavid S. Miller 1125e77980e5SDavid Dueck err = clk_prepare_enable(priv->clk); 1126e77980e5SDavid Dueck if (err) 1127e77980e5SDavid Dueck return err; 112899c4a634SDavid S. Miller 112999c4a634SDavid S. Miller /* check or determine and set bittime */ 113099c4a634SDavid S. Miller err = open_candev(dev); 113199c4a634SDavid S. Miller if (err) 113299c4a634SDavid S. Miller goto out; 113399c4a634SDavid S. Miller 113499c4a634SDavid S. Miller /* register interrupt handler */ 113599c4a634SDavid S. Miller if (request_irq(dev->irq, at91_irq, IRQF_SHARED, 113699c4a634SDavid S. Miller dev->name, dev)) { 113799c4a634SDavid S. Miller err = -EAGAIN; 113899c4a634SDavid S. Miller goto out_close; 113999c4a634SDavid S. Miller } 114099c4a634SDavid S. Miller 11414723f2b8SFabio Baltieri can_led_event(dev, CAN_LED_EVENT_OPEN); 11424723f2b8SFabio Baltieri 114399c4a634SDavid S. Miller /* start chip and queuing */ 114499c4a634SDavid S. Miller at91_chip_start(dev); 114599c4a634SDavid S. Miller napi_enable(&priv->napi); 114699c4a634SDavid S. Miller netif_start_queue(dev); 114799c4a634SDavid S. Miller 114899c4a634SDavid S. Miller return 0; 114999c4a634SDavid S. Miller 115099c4a634SDavid S. Miller out_close: 115199c4a634SDavid S. Miller close_candev(dev); 115299c4a634SDavid S. Miller out: 1153e77980e5SDavid Dueck clk_disable_unprepare(priv->clk); 115499c4a634SDavid S. Miller 115599c4a634SDavid S. Miller return err; 115699c4a634SDavid S. Miller } 115799c4a634SDavid S. Miller 115899c4a634SDavid S. Miller /* 115999c4a634SDavid S. Miller * stop CAN bus activity 116099c4a634SDavid S. Miller */ 116199c4a634SDavid S. Miller static int at91_close(struct net_device *dev) 116299c4a634SDavid S. Miller { 116399c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 116499c4a634SDavid S. Miller 116599c4a634SDavid S. Miller netif_stop_queue(dev); 116699c4a634SDavid S. Miller napi_disable(&priv->napi); 116799c4a634SDavid S. Miller at91_chip_stop(dev, CAN_STATE_STOPPED); 116899c4a634SDavid S. Miller 116999c4a634SDavid S. Miller free_irq(dev->irq, dev); 1170e77980e5SDavid Dueck clk_disable_unprepare(priv->clk); 117199c4a634SDavid S. Miller 117299c4a634SDavid S. Miller close_candev(dev); 117399c4a634SDavid S. Miller 11744723f2b8SFabio Baltieri can_led_event(dev, CAN_LED_EVENT_STOP); 11754723f2b8SFabio Baltieri 117699c4a634SDavid S. Miller return 0; 117799c4a634SDavid S. Miller } 117899c4a634SDavid S. Miller 117999c4a634SDavid S. Miller static int at91_set_mode(struct net_device *dev, enum can_mode mode) 118099c4a634SDavid S. Miller { 118199c4a634SDavid S. Miller switch (mode) { 118299c4a634SDavid S. Miller case CAN_MODE_START: 118399c4a634SDavid S. Miller at91_chip_start(dev); 118499c4a634SDavid S. Miller netif_wake_queue(dev); 118599c4a634SDavid S. Miller break; 118699c4a634SDavid S. Miller 118799c4a634SDavid S. Miller default: 118899c4a634SDavid S. Miller return -EOPNOTSUPP; 118999c4a634SDavid S. Miller } 119099c4a634SDavid S. Miller 119199c4a634SDavid S. Miller return 0; 119299c4a634SDavid S. Miller } 119399c4a634SDavid S. Miller 119499c4a634SDavid S. Miller static const struct net_device_ops at91_netdev_ops = { 119599c4a634SDavid S. Miller .ndo_open = at91_open, 119699c4a634SDavid S. Miller .ndo_stop = at91_close, 119799c4a634SDavid S. Miller .ndo_start_xmit = at91_start_xmit, 1198c971fa2aSOliver Hartkopp .ndo_change_mtu = can_change_mtu, 119999c4a634SDavid S. Miller }; 120099c4a634SDavid S. Miller 12013a5655a5SMarc Kleine-Budde static ssize_t at91_sysfs_show_mb0_id(struct device *dev, 12023a5655a5SMarc Kleine-Budde struct device_attribute *attr, char *buf) 12033a5655a5SMarc Kleine-Budde { 12043a5655a5SMarc Kleine-Budde struct at91_priv *priv = netdev_priv(to_net_dev(dev)); 12053a5655a5SMarc Kleine-Budde 12063a5655a5SMarc Kleine-Budde if (priv->mb0_id & CAN_EFF_FLAG) 12073a5655a5SMarc Kleine-Budde return snprintf(buf, PAGE_SIZE, "0x%08x\n", priv->mb0_id); 12083a5655a5SMarc Kleine-Budde else 12093a5655a5SMarc Kleine-Budde return snprintf(buf, PAGE_SIZE, "0x%03x\n", priv->mb0_id); 12103a5655a5SMarc Kleine-Budde } 12113a5655a5SMarc Kleine-Budde 12123a5655a5SMarc Kleine-Budde static ssize_t at91_sysfs_set_mb0_id(struct device *dev, 12133a5655a5SMarc Kleine-Budde struct device_attribute *attr, const char *buf, size_t count) 12143a5655a5SMarc Kleine-Budde { 12153a5655a5SMarc Kleine-Budde struct net_device *ndev = to_net_dev(dev); 12163a5655a5SMarc Kleine-Budde struct at91_priv *priv = netdev_priv(ndev); 12173a5655a5SMarc Kleine-Budde unsigned long can_id; 12183a5655a5SMarc Kleine-Budde ssize_t ret; 12193a5655a5SMarc Kleine-Budde int err; 12203a5655a5SMarc Kleine-Budde 12213a5655a5SMarc Kleine-Budde rtnl_lock(); 12223a5655a5SMarc Kleine-Budde 12233a5655a5SMarc Kleine-Budde if (ndev->flags & IFF_UP) { 12243a5655a5SMarc Kleine-Budde ret = -EBUSY; 12253a5655a5SMarc Kleine-Budde goto out; 12263a5655a5SMarc Kleine-Budde } 12273a5655a5SMarc Kleine-Budde 12280672f0abSJingoo Han err = kstrtoul(buf, 0, &can_id); 12293a5655a5SMarc Kleine-Budde if (err) { 12303a5655a5SMarc Kleine-Budde ret = err; 12313a5655a5SMarc Kleine-Budde goto out; 12323a5655a5SMarc Kleine-Budde } 12333a5655a5SMarc Kleine-Budde 12343a5655a5SMarc Kleine-Budde if (can_id & CAN_EFF_FLAG) 12353a5655a5SMarc Kleine-Budde can_id &= CAN_EFF_MASK | CAN_EFF_FLAG; 12363a5655a5SMarc Kleine-Budde else 12373a5655a5SMarc Kleine-Budde can_id &= CAN_SFF_MASK; 12383a5655a5SMarc Kleine-Budde 12393a5655a5SMarc Kleine-Budde priv->mb0_id = can_id; 12403a5655a5SMarc Kleine-Budde ret = count; 12413a5655a5SMarc Kleine-Budde 12423a5655a5SMarc Kleine-Budde out: 12433a5655a5SMarc Kleine-Budde rtnl_unlock(); 12443a5655a5SMarc Kleine-Budde return ret; 12453a5655a5SMarc Kleine-Budde } 12463a5655a5SMarc Kleine-Budde 1247fef52b01SVasiliy Kulikov static DEVICE_ATTR(mb0_id, S_IWUSR | S_IRUGO, 12483a5655a5SMarc Kleine-Budde at91_sysfs_show_mb0_id, at91_sysfs_set_mb0_id); 12493a5655a5SMarc Kleine-Budde 12503a5655a5SMarc Kleine-Budde static struct attribute *at91_sysfs_attrs[] = { 12513a5655a5SMarc Kleine-Budde &dev_attr_mb0_id.attr, 12523a5655a5SMarc Kleine-Budde NULL, 12533a5655a5SMarc Kleine-Budde }; 12543a5655a5SMarc Kleine-Budde 12553a5655a5SMarc Kleine-Budde static struct attribute_group at91_sysfs_attr_group = { 12563a5655a5SMarc Kleine-Budde .attrs = at91_sysfs_attrs, 12573a5655a5SMarc Kleine-Budde }; 12583a5655a5SMarc Kleine-Budde 12593078cde7SLudovic Desroches #if defined(CONFIG_OF) 12603078cde7SLudovic Desroches static const struct of_device_id at91_can_dt_ids[] = { 12613078cde7SLudovic Desroches { 12623078cde7SLudovic Desroches .compatible = "atmel,at91sam9x5-can", 12633078cde7SLudovic Desroches .data = &at91_at91sam9x5_data, 12643078cde7SLudovic Desroches }, { 12653078cde7SLudovic Desroches .compatible = "atmel,at91sam9263-can", 12663078cde7SLudovic Desroches .data = &at91_at91sam9263_data, 12673078cde7SLudovic Desroches }, { 12683078cde7SLudovic Desroches /* sentinel */ 12693078cde7SLudovic Desroches } 12703078cde7SLudovic Desroches }; 12713078cde7SLudovic Desroches MODULE_DEVICE_TABLE(of, at91_can_dt_ids); 12723078cde7SLudovic Desroches #endif 12733078cde7SLudovic Desroches 12743078cde7SLudovic Desroches static const struct at91_devtype_data *at91_can_get_driver_data(struct platform_device *pdev) 12753078cde7SLudovic Desroches { 12763078cde7SLudovic Desroches if (pdev->dev.of_node) { 12773078cde7SLudovic Desroches const struct of_device_id *match; 12783078cde7SLudovic Desroches 12793078cde7SLudovic Desroches match = of_match_node(at91_can_dt_ids, pdev->dev.of_node); 12803078cde7SLudovic Desroches if (!match) { 12813078cde7SLudovic Desroches dev_err(&pdev->dev, "no matching node found in dtb\n"); 12823078cde7SLudovic Desroches return NULL; 12833078cde7SLudovic Desroches } 12843078cde7SLudovic Desroches return (const struct at91_devtype_data *)match->data; 12853078cde7SLudovic Desroches } 12863078cde7SLudovic Desroches return (const struct at91_devtype_data *) 12873078cde7SLudovic Desroches platform_get_device_id(pdev)->driver_data; 12883078cde7SLudovic Desroches } 12893078cde7SLudovic Desroches 12903c8ac0f2SBill Pemberton static int at91_can_probe(struct platform_device *pdev) 129199c4a634SDavid S. Miller { 1292d3d47264SMarc Kleine-Budde const struct at91_devtype_data *devtype_data; 129399c4a634SDavid S. Miller struct net_device *dev; 129499c4a634SDavid S. Miller struct at91_priv *priv; 129599c4a634SDavid S. Miller struct resource *res; 129699c4a634SDavid S. Miller struct clk *clk; 129799c4a634SDavid S. Miller void __iomem *addr; 129899c4a634SDavid S. Miller int err, irq; 129999c4a634SDavid S. Miller 13003078cde7SLudovic Desroches devtype_data = at91_can_get_driver_data(pdev); 13013078cde7SLudovic Desroches if (!devtype_data) { 13023078cde7SLudovic Desroches dev_err(&pdev->dev, "no driver data\n"); 13033078cde7SLudovic Desroches err = -ENODEV; 13043078cde7SLudovic Desroches goto exit; 13053078cde7SLudovic Desroches } 1306d3d47264SMarc Kleine-Budde 130799c4a634SDavid S. Miller clk = clk_get(&pdev->dev, "can_clk"); 130899c4a634SDavid S. Miller if (IS_ERR(clk)) { 130999c4a634SDavid S. Miller dev_err(&pdev->dev, "no clock defined\n"); 131099c4a634SDavid S. Miller err = -ENODEV; 131199c4a634SDavid S. Miller goto exit; 131299c4a634SDavid S. Miller } 131399c4a634SDavid S. Miller 131499c4a634SDavid S. Miller res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 131599c4a634SDavid S. Miller irq = platform_get_irq(pdev, 0); 13164773a47dSUwe Kleine-König if (!res || irq <= 0) { 131799c4a634SDavid S. Miller err = -ENODEV; 131899c4a634SDavid S. Miller goto exit_put; 131999c4a634SDavid S. Miller } 132099c4a634SDavid S. Miller 132199c4a634SDavid S. Miller if (!request_mem_region(res->start, 132299c4a634SDavid S. Miller resource_size(res), 132399c4a634SDavid S. Miller pdev->name)) { 132499c4a634SDavid S. Miller err = -EBUSY; 132599c4a634SDavid S. Miller goto exit_put; 132699c4a634SDavid S. Miller } 132799c4a634SDavid S. Miller 132899c4a634SDavid S. Miller addr = ioremap_nocache(res->start, resource_size(res)); 132999c4a634SDavid S. Miller if (!addr) { 133099c4a634SDavid S. Miller err = -ENOMEM; 133199c4a634SDavid S. Miller goto exit_release; 133299c4a634SDavid S. Miller } 133399c4a634SDavid S. Miller 1334d3d47264SMarc Kleine-Budde dev = alloc_candev(sizeof(struct at91_priv), 1335d3d47264SMarc Kleine-Budde 1 << devtype_data->tx_shift); 133699c4a634SDavid S. Miller if (!dev) { 133799c4a634SDavid S. Miller err = -ENOMEM; 133899c4a634SDavid S. Miller goto exit_iounmap; 133999c4a634SDavid S. Miller } 134099c4a634SDavid S. Miller 134199c4a634SDavid S. Miller dev->netdev_ops = &at91_netdev_ops; 134299c4a634SDavid S. Miller dev->irq = irq; 134399c4a634SDavid S. Miller dev->flags |= IFF_ECHO; 134499c4a634SDavid S. Miller 134599c4a634SDavid S. Miller priv = netdev_priv(dev); 134699c4a634SDavid S. Miller priv->can.clock.freq = clk_get_rate(clk); 134799c4a634SDavid S. Miller priv->can.bittiming_const = &at91_bittiming_const; 134899c4a634SDavid S. Miller priv->can.do_set_mode = at91_set_mode; 134933a6f298SMarc Kleine-Budde priv->can.do_get_berr_counter = at91_get_berr_counter; 135017a50ee4SYoann DI RUZZA priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | 135117a50ee4SYoann DI RUZZA CAN_CTRLMODE_LISTENONLY; 1352d3d47264SMarc Kleine-Budde priv->reg_base = addr; 1353d3d47264SMarc Kleine-Budde priv->devtype_data = *devtype_data; 135499c4a634SDavid S. Miller priv->clk = clk; 13556cbdb918SJingoo Han priv->pdata = dev_get_platdata(&pdev->dev); 13563a5655a5SMarc Kleine-Budde priv->mb0_id = 0x7ff; 135799c4a634SDavid S. Miller 1358d3d47264SMarc Kleine-Budde netif_napi_add(dev, &priv->napi, at91_poll, get_mb_rx_num(priv)); 135999c4a634SDavid S. Miller 136007a648e6SMarc Kleine-Budde if (at91_is_sam9263(priv)) 136107a648e6SMarc Kleine-Budde dev->sysfs_groups[0] = &at91_sysfs_attr_group; 136207a648e6SMarc Kleine-Budde 136340f7e0ddSLibo Chen platform_set_drvdata(pdev, dev); 136499c4a634SDavid S. Miller SET_NETDEV_DEV(dev, &pdev->dev); 136599c4a634SDavid S. Miller 136699c4a634SDavid S. Miller err = register_candev(dev); 136799c4a634SDavid S. Miller if (err) { 136899c4a634SDavid S. Miller dev_err(&pdev->dev, "registering netdev failed\n"); 136999c4a634SDavid S. Miller goto exit_free; 137099c4a634SDavid S. Miller } 137199c4a634SDavid S. Miller 13724723f2b8SFabio Baltieri devm_can_led_init(dev); 13734723f2b8SFabio Baltieri 137499c4a634SDavid S. Miller dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n", 137599c4a634SDavid S. Miller priv->reg_base, dev->irq); 137699c4a634SDavid S. Miller 137799c4a634SDavid S. Miller return 0; 137899c4a634SDavid S. Miller 137999c4a634SDavid S. Miller exit_free: 1380759a6c76SMarc Kleine-Budde free_candev(dev); 138199c4a634SDavid S. Miller exit_iounmap: 138299c4a634SDavid S. Miller iounmap(addr); 138399c4a634SDavid S. Miller exit_release: 138499c4a634SDavid S. Miller release_mem_region(res->start, resource_size(res)); 138599c4a634SDavid S. Miller exit_put: 138699c4a634SDavid S. Miller clk_put(clk); 138799c4a634SDavid S. Miller exit: 138899c4a634SDavid S. Miller return err; 138999c4a634SDavid S. Miller } 139099c4a634SDavid S. Miller 13913c8ac0f2SBill Pemberton static int at91_can_remove(struct platform_device *pdev) 139299c4a634SDavid S. Miller { 139399c4a634SDavid S. Miller struct net_device *dev = platform_get_drvdata(pdev); 139499c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 139599c4a634SDavid S. Miller struct resource *res; 139699c4a634SDavid S. Miller 139799c4a634SDavid S. Miller unregister_netdev(dev); 139899c4a634SDavid S. Miller 139999c4a634SDavid S. Miller iounmap(priv->reg_base); 140099c4a634SDavid S. Miller 140199c4a634SDavid S. Miller res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 140299c4a634SDavid S. Miller release_mem_region(res->start, resource_size(res)); 140399c4a634SDavid S. Miller 140499c4a634SDavid S. Miller clk_put(priv->clk); 140599c4a634SDavid S. Miller 1406759a6c76SMarc Kleine-Budde free_candev(dev); 1407759a6c76SMarc Kleine-Budde 140899c4a634SDavid S. Miller return 0; 140999c4a634SDavid S. Miller } 141099c4a634SDavid S. Miller 1411d3d47264SMarc Kleine-Budde static const struct platform_device_id at91_can_id_table[] = { 1412d3d47264SMarc Kleine-Budde { 14135abbeea5SMarc Kleine-Budde .name = "at91sam9x5_can", 14143078cde7SLudovic Desroches .driver_data = (kernel_ulong_t)&at91_at91sam9x5_data, 1415d3d47264SMarc Kleine-Budde }, { 14165abbeea5SMarc Kleine-Budde .name = "at91_can", 14173078cde7SLudovic Desroches .driver_data = (kernel_ulong_t)&at91_at91sam9263_data, 14186388b396SMarc Kleine-Budde }, { 1419d3d47264SMarc Kleine-Budde /* sentinel */ 1420d3d47264SMarc Kleine-Budde } 1421d3d47264SMarc Kleine-Budde }; 142209ca71caSMarc Kleine-Budde MODULE_DEVICE_TABLE(platform, at91_can_id_table); 1423d3d47264SMarc Kleine-Budde 142499c4a634SDavid S. Miller static struct platform_driver at91_can_driver = { 142599c4a634SDavid S. Miller .probe = at91_can_probe, 14263c8ac0f2SBill Pemberton .remove = at91_can_remove, 142799c4a634SDavid S. Miller .driver = { 142800389b08SMarc Kleine-Budde .name = KBUILD_MODNAME, 14291f3e4b0cSSachin Kamat .of_match_table = of_match_ptr(at91_can_dt_ids), 143099c4a634SDavid S. Miller }, 1431d3d47264SMarc Kleine-Budde .id_table = at91_can_id_table, 143299c4a634SDavid S. Miller }; 143399c4a634SDavid S. Miller 1434871d3372SAxel Lin module_platform_driver(at91_can_driver); 143599c4a634SDavid S. Miller 143699c4a634SDavid S. Miller MODULE_AUTHOR("Marc Kleine-Budde <mkl@pengutronix.de>"); 143799c4a634SDavid S. Miller MODULE_LICENSE("GPL v2"); 143800389b08SMarc Kleine-Budde MODULE_DESCRIPTION(KBUILD_MODNAME " CAN netdevice driver"); 1439