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 * Send feedback to <socketcan-users@lists.berlios.de> 1299c4a634SDavid S. Miller * 1399c4a634SDavid S. Miller * 1499c4a634SDavid S. Miller * Your platform definition file should specify something like: 1599c4a634SDavid S. Miller * 1699c4a634SDavid S. Miller * static struct at91_can_data ek_can_data = { 1799c4a634SDavid S. Miller * transceiver_switch = sam9263ek_transceiver_switch, 1899c4a634SDavid S. Miller * }; 1999c4a634SDavid S. Miller * 2099c4a634SDavid S. Miller * at91_add_device_can(&ek_can_data); 2199c4a634SDavid S. Miller * 2299c4a634SDavid S. Miller */ 2399c4a634SDavid S. Miller 2499c4a634SDavid S. Miller #include <linux/clk.h> 2599c4a634SDavid S. Miller #include <linux/errno.h> 2699c4a634SDavid S. Miller #include <linux/if_arp.h> 2799c4a634SDavid S. Miller #include <linux/init.h> 2899c4a634SDavid S. Miller #include <linux/interrupt.h> 2999c4a634SDavid S. Miller #include <linux/kernel.h> 3099c4a634SDavid S. Miller #include <linux/module.h> 3199c4a634SDavid S. Miller #include <linux/netdevice.h> 3299c4a634SDavid S. Miller #include <linux/platform_device.h> 333a5655a5SMarc Kleine-Budde #include <linux/rtnetlink.h> 3499c4a634SDavid S. Miller #include <linux/skbuff.h> 3599c4a634SDavid S. Miller #include <linux/spinlock.h> 3699c4a634SDavid S. Miller #include <linux/string.h> 3799c4a634SDavid S. Miller #include <linux/types.h> 3899c4a634SDavid S. Miller 3999c4a634SDavid S. Miller #include <linux/can/dev.h> 4099c4a634SDavid S. Miller #include <linux/can/error.h> 4199c4a634SDavid S. Miller 4299c4a634SDavid S. Miller #include <mach/board.h> 4399c4a634SDavid S. Miller 449e0a2d1cSMarc Kleine-Budde #define AT91_NAPI_WEIGHT 11 4599c4a634SDavid S. Miller 4699c4a634SDavid S. Miller /* 4799c4a634SDavid S. Miller * RX/TX Mailbox split 4899c4a634SDavid S. Miller * don't dare to touch 4999c4a634SDavid S. Miller */ 509e0a2d1cSMarc Kleine-Budde #define AT91_MB_RX_NUM 11 5199c4a634SDavid S. Miller #define AT91_MB_TX_SHIFT 2 5299c4a634SDavid S. Miller 539e0a2d1cSMarc Kleine-Budde #define AT91_MB_RX_FIRST 1 5499c4a634SDavid S. Miller #define AT91_MB_RX_LAST (AT91_MB_RX_FIRST + AT91_MB_RX_NUM - 1) 5599c4a634SDavid S. Miller 5699c4a634SDavid S. Miller #define AT91_MB_RX_MASK(i) ((1 << (i)) - 1) 5799c4a634SDavid S. Miller #define AT91_MB_RX_SPLIT 8 5899c4a634SDavid S. Miller #define AT91_MB_RX_LOW_LAST (AT91_MB_RX_SPLIT - 1) 590909c1ecSMarc Kleine-Budde #define AT91_MB_RX_LOW_MASK (AT91_MB_RX_MASK(AT91_MB_RX_SPLIT) & \ 600909c1ecSMarc Kleine-Budde ~AT91_MB_RX_MASK(AT91_MB_RX_FIRST)) 6199c4a634SDavid S. Miller 6299c4a634SDavid S. Miller #define AT91_MB_TX_NUM (1 << AT91_MB_TX_SHIFT) 6399c4a634SDavid S. Miller #define AT91_MB_TX_FIRST (AT91_MB_RX_LAST + 1) 6499c4a634SDavid S. Miller #define AT91_MB_TX_LAST (AT91_MB_TX_FIRST + AT91_MB_TX_NUM - 1) 6599c4a634SDavid S. Miller 6699c4a634SDavid S. Miller #define AT91_NEXT_PRIO_SHIFT (AT91_MB_TX_SHIFT) 6799c4a634SDavid S. Miller #define AT91_NEXT_PRIO_MASK (0xf << AT91_MB_TX_SHIFT) 6899c4a634SDavid S. Miller #define AT91_NEXT_MB_MASK (AT91_MB_TX_NUM - 1) 6999c4a634SDavid S. Miller #define AT91_NEXT_MASK ((AT91_MB_TX_NUM - 1) | AT91_NEXT_PRIO_MASK) 7099c4a634SDavid S. Miller 7199c4a634SDavid S. Miller /* Common registers */ 7299c4a634SDavid S. Miller enum at91_reg { 7399c4a634SDavid S. Miller AT91_MR = 0x000, 7499c4a634SDavid S. Miller AT91_IER = 0x004, 7599c4a634SDavid S. Miller AT91_IDR = 0x008, 7699c4a634SDavid S. Miller AT91_IMR = 0x00C, 7799c4a634SDavid S. Miller AT91_SR = 0x010, 7899c4a634SDavid S. Miller AT91_BR = 0x014, 7999c4a634SDavid S. Miller AT91_TIM = 0x018, 8099c4a634SDavid S. Miller AT91_TIMESTP = 0x01C, 8199c4a634SDavid S. Miller AT91_ECR = 0x020, 8299c4a634SDavid S. Miller AT91_TCR = 0x024, 8399c4a634SDavid S. Miller AT91_ACR = 0x028, 8499c4a634SDavid S. Miller }; 8599c4a634SDavid S. Miller 8699c4a634SDavid S. Miller /* Mailbox registers (0 <= i <= 15) */ 8799c4a634SDavid S. Miller #define AT91_MMR(i) (enum at91_reg)(0x200 + ((i) * 0x20)) 8899c4a634SDavid S. Miller #define AT91_MAM(i) (enum at91_reg)(0x204 + ((i) * 0x20)) 8999c4a634SDavid S. Miller #define AT91_MID(i) (enum at91_reg)(0x208 + ((i) * 0x20)) 9099c4a634SDavid S. Miller #define AT91_MFID(i) (enum at91_reg)(0x20C + ((i) * 0x20)) 9199c4a634SDavid S. Miller #define AT91_MSR(i) (enum at91_reg)(0x210 + ((i) * 0x20)) 9299c4a634SDavid S. Miller #define AT91_MDL(i) (enum at91_reg)(0x214 + ((i) * 0x20)) 9399c4a634SDavid S. Miller #define AT91_MDH(i) (enum at91_reg)(0x218 + ((i) * 0x20)) 9499c4a634SDavid S. Miller #define AT91_MCR(i) (enum at91_reg)(0x21C + ((i) * 0x20)) 9599c4a634SDavid S. Miller 9699c4a634SDavid S. Miller /* Register bits */ 9799c4a634SDavid S. Miller #define AT91_MR_CANEN BIT(0) 9899c4a634SDavid S. Miller #define AT91_MR_LPM BIT(1) 9999c4a634SDavid S. Miller #define AT91_MR_ABM BIT(2) 10099c4a634SDavid S. Miller #define AT91_MR_OVL BIT(3) 10199c4a634SDavid S. Miller #define AT91_MR_TEOF BIT(4) 10299c4a634SDavid S. Miller #define AT91_MR_TTM BIT(5) 10399c4a634SDavid S. Miller #define AT91_MR_TIMFRZ BIT(6) 10499c4a634SDavid S. Miller #define AT91_MR_DRPT BIT(7) 10599c4a634SDavid S. Miller 10699c4a634SDavid S. Miller #define AT91_SR_RBSY BIT(29) 10799c4a634SDavid S. Miller 10899c4a634SDavid S. Miller #define AT91_MMR_PRIO_SHIFT (16) 10999c4a634SDavid S. Miller 11099c4a634SDavid S. Miller #define AT91_MID_MIDE BIT(29) 11199c4a634SDavid S. Miller 11299c4a634SDavid S. Miller #define AT91_MSR_MRTR BIT(20) 11399c4a634SDavid S. Miller #define AT91_MSR_MABT BIT(22) 11499c4a634SDavid S. Miller #define AT91_MSR_MRDY BIT(23) 11599c4a634SDavid S. Miller #define AT91_MSR_MMI BIT(24) 11699c4a634SDavid S. Miller 11799c4a634SDavid S. Miller #define AT91_MCR_MRTR BIT(20) 11899c4a634SDavid S. Miller #define AT91_MCR_MTCR BIT(23) 11999c4a634SDavid S. Miller 12099c4a634SDavid S. Miller /* Mailbox Modes */ 12199c4a634SDavid S. Miller enum at91_mb_mode { 12299c4a634SDavid S. Miller AT91_MB_MODE_DISABLED = 0, 12399c4a634SDavid S. Miller AT91_MB_MODE_RX = 1, 12499c4a634SDavid S. Miller AT91_MB_MODE_RX_OVRWR = 2, 12599c4a634SDavid S. Miller AT91_MB_MODE_TX = 3, 12699c4a634SDavid S. Miller AT91_MB_MODE_CONSUMER = 4, 12799c4a634SDavid S. Miller AT91_MB_MODE_PRODUCER = 5, 12899c4a634SDavid S. Miller }; 12999c4a634SDavid S. Miller 13099c4a634SDavid S. Miller /* Interrupt mask bits */ 13199c4a634SDavid S. Miller #define AT91_IRQ_MB_RX ((1 << (AT91_MB_RX_LAST + 1)) \ 13299c4a634SDavid S. Miller - (1 << AT91_MB_RX_FIRST)) 13399c4a634SDavid S. Miller #define AT91_IRQ_MB_TX ((1 << (AT91_MB_TX_LAST + 1)) \ 13499c4a634SDavid S. Miller - (1 << AT91_MB_TX_FIRST)) 13599c4a634SDavid S. Miller #define AT91_IRQ_MB_ALL (AT91_IRQ_MB_RX | AT91_IRQ_MB_TX) 13699c4a634SDavid S. Miller 13799c4a634SDavid S. Miller #define AT91_IRQ_ERRA (1 << 16) 13899c4a634SDavid S. Miller #define AT91_IRQ_WARN (1 << 17) 13999c4a634SDavid S. Miller #define AT91_IRQ_ERRP (1 << 18) 14099c4a634SDavid S. Miller #define AT91_IRQ_BOFF (1 << 19) 14199c4a634SDavid S. Miller #define AT91_IRQ_SLEEP (1 << 20) 14299c4a634SDavid S. Miller #define AT91_IRQ_WAKEUP (1 << 21) 14399c4a634SDavid S. Miller #define AT91_IRQ_TOVF (1 << 22) 14499c4a634SDavid S. Miller #define AT91_IRQ_TSTP (1 << 23) 14599c4a634SDavid S. Miller #define AT91_IRQ_CERR (1 << 24) 14699c4a634SDavid S. Miller #define AT91_IRQ_SERR (1 << 25) 14799c4a634SDavid S. Miller #define AT91_IRQ_AERR (1 << 26) 14899c4a634SDavid S. Miller #define AT91_IRQ_FERR (1 << 27) 14999c4a634SDavid S. Miller #define AT91_IRQ_BERR (1 << 28) 15099c4a634SDavid S. Miller 15199c4a634SDavid S. Miller #define AT91_IRQ_ERR_ALL (0x1fff0000) 15299c4a634SDavid S. Miller #define AT91_IRQ_ERR_FRAME (AT91_IRQ_CERR | AT91_IRQ_SERR | \ 15399c4a634SDavid S. Miller AT91_IRQ_AERR | AT91_IRQ_FERR | AT91_IRQ_BERR) 15499c4a634SDavid S. Miller #define AT91_IRQ_ERR_LINE (AT91_IRQ_ERRA | AT91_IRQ_WARN | \ 15599c4a634SDavid S. Miller AT91_IRQ_ERRP | AT91_IRQ_BOFF) 15699c4a634SDavid S. Miller 15799c4a634SDavid S. Miller #define AT91_IRQ_ALL (0x1fffffff) 15899c4a634SDavid S. Miller 15999c4a634SDavid S. Miller struct at91_priv { 16099c4a634SDavid S. Miller struct can_priv can; /* must be the first member! */ 16199c4a634SDavid S. Miller struct net_device *dev; 16299c4a634SDavid S. Miller struct napi_struct napi; 16399c4a634SDavid S. Miller 16499c4a634SDavid S. Miller void __iomem *reg_base; 16599c4a634SDavid S. Miller 16699c4a634SDavid S. Miller u32 reg_sr; 16799c4a634SDavid S. Miller unsigned int tx_next; 16899c4a634SDavid S. Miller unsigned int tx_echo; 16999c4a634SDavid S. Miller unsigned int rx_next; 17099c4a634SDavid S. Miller 17199c4a634SDavid S. Miller struct clk *clk; 17299c4a634SDavid S. Miller struct at91_can_data *pdata; 1733a5655a5SMarc Kleine-Budde 1743a5655a5SMarc Kleine-Budde canid_t mb0_id; 17599c4a634SDavid S. Miller }; 17699c4a634SDavid S. Miller 17799c4a634SDavid S. Miller static struct can_bittiming_const at91_bittiming_const = { 17800389b08SMarc Kleine-Budde .name = KBUILD_MODNAME, 17999c4a634SDavid S. Miller .tseg1_min = 4, 18099c4a634SDavid S. Miller .tseg1_max = 16, 18199c4a634SDavid S. Miller .tseg2_min = 2, 18299c4a634SDavid S. Miller .tseg2_max = 8, 18399c4a634SDavid S. Miller .sjw_max = 4, 18499c4a634SDavid S. Miller .brp_min = 2, 18599c4a634SDavid S. Miller .brp_max = 128, 18699c4a634SDavid S. Miller .brp_inc = 1, 18799c4a634SDavid S. Miller }; 18899c4a634SDavid S. Miller 18999c4a634SDavid S. Miller static inline int get_tx_next_mb(const struct at91_priv *priv) 19099c4a634SDavid S. Miller { 19199c4a634SDavid S. Miller return (priv->tx_next & AT91_NEXT_MB_MASK) + AT91_MB_TX_FIRST; 19299c4a634SDavid S. Miller } 19399c4a634SDavid S. Miller 19499c4a634SDavid S. Miller static inline int get_tx_next_prio(const struct at91_priv *priv) 19599c4a634SDavid S. Miller { 19699c4a634SDavid S. Miller return (priv->tx_next >> AT91_NEXT_PRIO_SHIFT) & 0xf; 19799c4a634SDavid S. Miller } 19899c4a634SDavid S. Miller 19999c4a634SDavid S. Miller static inline int get_tx_echo_mb(const struct at91_priv *priv) 20099c4a634SDavid S. Miller { 20199c4a634SDavid S. Miller return (priv->tx_echo & AT91_NEXT_MB_MASK) + AT91_MB_TX_FIRST; 20299c4a634SDavid S. Miller } 20399c4a634SDavid S. Miller 20499c4a634SDavid S. Miller static inline u32 at91_read(const struct at91_priv *priv, enum at91_reg reg) 20599c4a634SDavid S. Miller { 2067672fe73SMarc Kleine-Budde return __raw_readl(priv->reg_base + reg); 20799c4a634SDavid S. Miller } 20899c4a634SDavid S. Miller 20999c4a634SDavid S. Miller static inline void at91_write(const struct at91_priv *priv, enum at91_reg reg, 21099c4a634SDavid S. Miller u32 value) 21199c4a634SDavid S. Miller { 2127672fe73SMarc Kleine-Budde __raw_writel(value, priv->reg_base + reg); 21399c4a634SDavid S. Miller } 21499c4a634SDavid S. Miller 21599c4a634SDavid S. Miller static inline void set_mb_mode_prio(const struct at91_priv *priv, 21699c4a634SDavid S. Miller unsigned int mb, enum at91_mb_mode mode, int prio) 21799c4a634SDavid S. Miller { 21899c4a634SDavid S. Miller at91_write(priv, AT91_MMR(mb), (mode << 24) | (prio << 16)); 21999c4a634SDavid S. Miller } 22099c4a634SDavid S. Miller 22199c4a634SDavid S. Miller static inline void set_mb_mode(const struct at91_priv *priv, unsigned int mb, 22299c4a634SDavid S. Miller enum at91_mb_mode mode) 22399c4a634SDavid S. Miller { 22499c4a634SDavid S. Miller set_mb_mode_prio(priv, mb, mode, 0); 22599c4a634SDavid S. Miller } 22699c4a634SDavid S. Miller 2273a5655a5SMarc Kleine-Budde static inline u32 at91_can_id_to_reg_mid(canid_t can_id) 2283a5655a5SMarc Kleine-Budde { 2293a5655a5SMarc Kleine-Budde u32 reg_mid; 2303a5655a5SMarc Kleine-Budde 2313a5655a5SMarc Kleine-Budde if (can_id & CAN_EFF_FLAG) 2323a5655a5SMarc Kleine-Budde reg_mid = (can_id & CAN_EFF_MASK) | AT91_MID_MIDE; 2333a5655a5SMarc Kleine-Budde else 2343a5655a5SMarc Kleine-Budde reg_mid = (can_id & CAN_SFF_MASK) << 18; 2353a5655a5SMarc Kleine-Budde 2363a5655a5SMarc Kleine-Budde return reg_mid; 2373a5655a5SMarc Kleine-Budde } 2383a5655a5SMarc Kleine-Budde 23999c4a634SDavid S. Miller /* 24099c4a634SDavid S. Miller * Swtich transceiver on or off 24199c4a634SDavid S. Miller */ 24299c4a634SDavid S. Miller static void at91_transceiver_switch(const struct at91_priv *priv, int on) 24399c4a634SDavid S. Miller { 24499c4a634SDavid S. Miller if (priv->pdata && priv->pdata->transceiver_switch) 24599c4a634SDavid S. Miller priv->pdata->transceiver_switch(on); 24699c4a634SDavid S. Miller } 24799c4a634SDavid S. Miller 24899c4a634SDavid S. Miller static void at91_setup_mailboxes(struct net_device *dev) 24999c4a634SDavid S. Miller { 25099c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 25199c4a634SDavid S. Miller unsigned int i; 2523a5655a5SMarc Kleine-Budde u32 reg_mid; 25399c4a634SDavid S. Miller 25499c4a634SDavid S. Miller /* 2559e0a2d1cSMarc Kleine-Budde * Due to a chip bug (errata 50.2.6.3 & 50.3.5.3) the first 2569e0a2d1cSMarc Kleine-Budde * mailbox is disabled. The next 11 mailboxes are used as a 2579e0a2d1cSMarc Kleine-Budde * reception FIFO. The last mailbox is configured with 2589e0a2d1cSMarc Kleine-Budde * overwrite option. The overwrite flag indicates a FIFO 2599e0a2d1cSMarc Kleine-Budde * overflow. 26099c4a634SDavid S. Miller */ 2613a5655a5SMarc Kleine-Budde reg_mid = at91_can_id_to_reg_mid(priv->mb0_id); 2623a5655a5SMarc Kleine-Budde for (i = 0; i < AT91_MB_RX_FIRST; i++) { 2639e0a2d1cSMarc Kleine-Budde set_mb_mode(priv, i, AT91_MB_MODE_DISABLED); 2643a5655a5SMarc Kleine-Budde at91_write(priv, AT91_MID(i), reg_mid); 2653a5655a5SMarc Kleine-Budde at91_write(priv, AT91_MCR(i), 0x0); /* clear dlc */ 2663a5655a5SMarc Kleine-Budde } 2673a5655a5SMarc Kleine-Budde 26899c4a634SDavid S. Miller for (i = AT91_MB_RX_FIRST; i < AT91_MB_RX_LAST; i++) 26999c4a634SDavid S. Miller set_mb_mode(priv, i, AT91_MB_MODE_RX); 27099c4a634SDavid S. Miller set_mb_mode(priv, AT91_MB_RX_LAST, AT91_MB_MODE_RX_OVRWR); 27199c4a634SDavid S. Miller 2728a0e0a49SMarc Kleine-Budde /* reset acceptance mask and id register */ 2738a0e0a49SMarc Kleine-Budde for (i = AT91_MB_RX_FIRST; i <= AT91_MB_RX_LAST; i++) { 2748a0e0a49SMarc Kleine-Budde at91_write(priv, AT91_MAM(i), 0x0 ); 2758a0e0a49SMarc Kleine-Budde at91_write(priv, AT91_MID(i), AT91_MID_MIDE); 2768a0e0a49SMarc Kleine-Budde } 2778a0e0a49SMarc Kleine-Budde 27899c4a634SDavid S. Miller /* The last 4 mailboxes are used for transmitting. */ 27999c4a634SDavid S. Miller for (i = AT91_MB_TX_FIRST; i <= AT91_MB_TX_LAST; i++) 28099c4a634SDavid S. Miller set_mb_mode_prio(priv, i, AT91_MB_MODE_TX, 0); 28199c4a634SDavid S. Miller 28299c4a634SDavid S. Miller /* Reset tx and rx helper pointers */ 2830909c1ecSMarc Kleine-Budde priv->tx_next = priv->tx_echo = 0; 2840909c1ecSMarc Kleine-Budde priv->rx_next = AT91_MB_RX_FIRST; 28599c4a634SDavid S. Miller } 28699c4a634SDavid S. Miller 28799c4a634SDavid S. Miller static int at91_set_bittiming(struct net_device *dev) 28899c4a634SDavid S. Miller { 28999c4a634SDavid S. Miller const struct at91_priv *priv = netdev_priv(dev); 29099c4a634SDavid S. Miller const struct can_bittiming *bt = &priv->can.bittiming; 29199c4a634SDavid S. Miller u32 reg_br; 29299c4a634SDavid S. Miller 293dbe91325SMarc Kleine-Budde reg_br = ((priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) ? 1 << 24 : 0) | 29499c4a634SDavid S. Miller ((bt->brp - 1) << 16) | ((bt->sjw - 1) << 12) | 29599c4a634SDavid S. Miller ((bt->prop_seg - 1) << 8) | ((bt->phase_seg1 - 1) << 4) | 29699c4a634SDavid S. Miller ((bt->phase_seg2 - 1) << 0); 29799c4a634SDavid S. Miller 298882055c8SMarc Kleine-Budde netdev_info(dev, "writing AT91_BR: 0x%08x\n", reg_br); 29999c4a634SDavid S. Miller 30099c4a634SDavid S. Miller at91_write(priv, AT91_BR, reg_br); 30199c4a634SDavid S. Miller 30299c4a634SDavid S. Miller return 0; 30399c4a634SDavid S. Miller } 30499c4a634SDavid S. Miller 30533a6f298SMarc Kleine-Budde static int at91_get_berr_counter(const struct net_device *dev, 30633a6f298SMarc Kleine-Budde struct can_berr_counter *bec) 30733a6f298SMarc Kleine-Budde { 30833a6f298SMarc Kleine-Budde const struct at91_priv *priv = netdev_priv(dev); 30933a6f298SMarc Kleine-Budde u32 reg_ecr = at91_read(priv, AT91_ECR); 31033a6f298SMarc Kleine-Budde 31133a6f298SMarc Kleine-Budde bec->rxerr = reg_ecr & 0xff; 31233a6f298SMarc Kleine-Budde bec->txerr = reg_ecr >> 16; 31333a6f298SMarc Kleine-Budde 31433a6f298SMarc Kleine-Budde return 0; 31533a6f298SMarc Kleine-Budde } 31633a6f298SMarc Kleine-Budde 31799c4a634SDavid S. Miller static void at91_chip_start(struct net_device *dev) 31899c4a634SDavid S. Miller { 31999c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 32099c4a634SDavid S. Miller u32 reg_mr, reg_ier; 32199c4a634SDavid S. Miller 32299c4a634SDavid S. Miller /* disable interrupts */ 32399c4a634SDavid S. Miller at91_write(priv, AT91_IDR, AT91_IRQ_ALL); 32499c4a634SDavid S. Miller 32599c4a634SDavid S. Miller /* disable chip */ 32699c4a634SDavid S. Miller reg_mr = at91_read(priv, AT91_MR); 32799c4a634SDavid S. Miller at91_write(priv, AT91_MR, reg_mr & ~AT91_MR_CANEN); 32899c4a634SDavid S. Miller 329b156fd04SMarc Kleine-Budde at91_set_bittiming(dev); 33099c4a634SDavid S. Miller at91_setup_mailboxes(dev); 33199c4a634SDavid S. Miller at91_transceiver_switch(priv, 1); 33299c4a634SDavid S. Miller 33399c4a634SDavid S. Miller /* enable chip */ 33499c4a634SDavid S. Miller at91_write(priv, AT91_MR, AT91_MR_CANEN); 33599c4a634SDavid S. Miller 33699c4a634SDavid S. Miller priv->can.state = CAN_STATE_ERROR_ACTIVE; 33799c4a634SDavid S. Miller 33899c4a634SDavid S. Miller /* Enable interrupts */ 33999c4a634SDavid S. Miller reg_ier = AT91_IRQ_MB_RX | AT91_IRQ_ERRP | AT91_IRQ_ERR_FRAME; 34099c4a634SDavid S. Miller at91_write(priv, AT91_IDR, AT91_IRQ_ALL); 34199c4a634SDavid S. Miller at91_write(priv, AT91_IER, reg_ier); 34299c4a634SDavid S. Miller } 34399c4a634SDavid S. Miller 34499c4a634SDavid S. Miller static void at91_chip_stop(struct net_device *dev, enum can_state state) 34599c4a634SDavid S. Miller { 34699c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 34799c4a634SDavid S. Miller u32 reg_mr; 34899c4a634SDavid S. Miller 34999c4a634SDavid S. Miller /* disable interrupts */ 35099c4a634SDavid S. Miller at91_write(priv, AT91_IDR, AT91_IRQ_ALL); 35199c4a634SDavid S. Miller 35299c4a634SDavid S. Miller reg_mr = at91_read(priv, AT91_MR); 35399c4a634SDavid S. Miller at91_write(priv, AT91_MR, reg_mr & ~AT91_MR_CANEN); 35499c4a634SDavid S. Miller 35599c4a634SDavid S. Miller at91_transceiver_switch(priv, 0); 35699c4a634SDavid S. Miller priv->can.state = state; 35799c4a634SDavid S. Miller } 35899c4a634SDavid S. Miller 35999c4a634SDavid S. Miller /* 36099c4a634SDavid S. Miller * theory of operation: 36199c4a634SDavid S. Miller * 36299c4a634SDavid S. Miller * According to the datasheet priority 0 is the highest priority, 15 36399c4a634SDavid S. Miller * is the lowest. If two mailboxes have the same priority level the 36499c4a634SDavid S. Miller * message of the mailbox with the lowest number is sent first. 36599c4a634SDavid S. Miller * 36699c4a634SDavid S. Miller * We use the first TX mailbox (AT91_MB_TX_FIRST) with prio 0, then 36799c4a634SDavid S. Miller * the next mailbox with prio 0, and so on, until all mailboxes are 36899c4a634SDavid S. Miller * used. Then we start from the beginning with mailbox 36999c4a634SDavid S. Miller * AT91_MB_TX_FIRST, but with prio 1, mailbox AT91_MB_TX_FIRST + 1 37099c4a634SDavid S. Miller * prio 1. When we reach the last mailbox with prio 15, we have to 37199c4a634SDavid S. Miller * stop sending, waiting for all messages to be delivered, then start 37299c4a634SDavid S. Miller * again with mailbox AT91_MB_TX_FIRST prio 0. 37399c4a634SDavid S. Miller * 37499c4a634SDavid S. Miller * We use the priv->tx_next as counter for the next transmission 37599c4a634SDavid S. Miller * mailbox, but without the offset AT91_MB_TX_FIRST. The lower bits 37699c4a634SDavid S. Miller * encode the mailbox number, the upper 4 bits the mailbox priority: 37799c4a634SDavid S. Miller * 37899c4a634SDavid S. Miller * priv->tx_next = (prio << AT91_NEXT_PRIO_SHIFT) || 37999c4a634SDavid S. Miller * (mb - AT91_MB_TX_FIRST); 38099c4a634SDavid S. Miller * 38199c4a634SDavid S. Miller */ 38299c4a634SDavid S. Miller static netdev_tx_t at91_start_xmit(struct sk_buff *skb, struct net_device *dev) 38399c4a634SDavid S. Miller { 38499c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 38599c4a634SDavid S. Miller struct net_device_stats *stats = &dev->stats; 38699c4a634SDavid S. Miller struct can_frame *cf = (struct can_frame *)skb->data; 38799c4a634SDavid S. Miller unsigned int mb, prio; 38899c4a634SDavid S. Miller u32 reg_mid, reg_mcr; 38999c4a634SDavid S. Miller 3903ccd4c61SOliver Hartkopp if (can_dropped_invalid_skb(dev, skb)) 3913ccd4c61SOliver Hartkopp return NETDEV_TX_OK; 3923ccd4c61SOliver Hartkopp 39399c4a634SDavid S. Miller mb = get_tx_next_mb(priv); 39499c4a634SDavid S. Miller prio = get_tx_next_prio(priv); 39599c4a634SDavid S. Miller 39699c4a634SDavid S. Miller if (unlikely(!(at91_read(priv, AT91_MSR(mb)) & AT91_MSR_MRDY))) { 39799c4a634SDavid S. Miller netif_stop_queue(dev); 39899c4a634SDavid S. Miller 399882055c8SMarc Kleine-Budde netdev_err(dev, "BUG! TX buffer full when queue awake!\n"); 40099c4a634SDavid S. Miller return NETDEV_TX_BUSY; 40199c4a634SDavid S. Miller } 4023a5655a5SMarc Kleine-Budde reg_mid = at91_can_id_to_reg_mid(cf->can_id); 40399c4a634SDavid S. Miller reg_mcr = ((cf->can_id & CAN_RTR_FLAG) ? AT91_MCR_MRTR : 0) | 40499c4a634SDavid S. Miller (cf->can_dlc << 16) | AT91_MCR_MTCR; 40599c4a634SDavid S. Miller 40699c4a634SDavid S. Miller /* disable MB while writing ID (see datasheet) */ 40799c4a634SDavid S. Miller set_mb_mode(priv, mb, AT91_MB_MODE_DISABLED); 40899c4a634SDavid S. Miller at91_write(priv, AT91_MID(mb), reg_mid); 40999c4a634SDavid S. Miller set_mb_mode_prio(priv, mb, AT91_MB_MODE_TX, prio); 41099c4a634SDavid S. Miller 41199c4a634SDavid S. Miller at91_write(priv, AT91_MDL(mb), *(u32 *)(cf->data + 0)); 41299c4a634SDavid S. Miller at91_write(priv, AT91_MDH(mb), *(u32 *)(cf->data + 4)); 41399c4a634SDavid S. Miller 41499c4a634SDavid S. Miller /* This triggers transmission */ 41599c4a634SDavid S. Miller at91_write(priv, AT91_MCR(mb), reg_mcr); 41699c4a634SDavid S. Miller 41799c4a634SDavid S. Miller stats->tx_bytes += cf->can_dlc; 41899c4a634SDavid S. Miller 419*25985edcSLucas De Marchi /* _NOTE_: subtract AT91_MB_TX_FIRST offset from mb! */ 42099c4a634SDavid S. Miller can_put_echo_skb(skb, dev, mb - AT91_MB_TX_FIRST); 42199c4a634SDavid S. Miller 42299c4a634SDavid S. Miller /* 42399c4a634SDavid S. Miller * we have to stop the queue and deliver all messages in case 42499c4a634SDavid S. Miller * of a prio+mb counter wrap around. This is the case if 42599c4a634SDavid S. Miller * tx_next buffer prio and mailbox equals 0. 42699c4a634SDavid S. Miller * 42799c4a634SDavid S. Miller * also stop the queue if next buffer is still in use 42899c4a634SDavid S. Miller * (== not ready) 42999c4a634SDavid S. Miller */ 43099c4a634SDavid S. Miller priv->tx_next++; 43199c4a634SDavid S. Miller if (!(at91_read(priv, AT91_MSR(get_tx_next_mb(priv))) & 43299c4a634SDavid S. Miller AT91_MSR_MRDY) || 43399c4a634SDavid S. Miller (priv->tx_next & AT91_NEXT_MASK) == 0) 43499c4a634SDavid S. Miller netif_stop_queue(dev); 43599c4a634SDavid S. Miller 43699c4a634SDavid S. Miller /* Enable interrupt for this mailbox */ 43799c4a634SDavid S. Miller at91_write(priv, AT91_IER, 1 << mb); 43899c4a634SDavid S. Miller 43999c4a634SDavid S. Miller return NETDEV_TX_OK; 44099c4a634SDavid S. Miller } 44199c4a634SDavid S. Miller 44299c4a634SDavid S. Miller /** 44399c4a634SDavid S. Miller * at91_activate_rx_low - activate lower rx mailboxes 44499c4a634SDavid S. Miller * @priv: a91 context 44599c4a634SDavid S. Miller * 44699c4a634SDavid S. Miller * Reenables the lower mailboxes for reception of new CAN messages 44799c4a634SDavid S. Miller */ 44899c4a634SDavid S. Miller static inline void at91_activate_rx_low(const struct at91_priv *priv) 44999c4a634SDavid S. Miller { 45099c4a634SDavid S. Miller u32 mask = AT91_MB_RX_LOW_MASK; 45199c4a634SDavid S. Miller at91_write(priv, AT91_TCR, mask); 45299c4a634SDavid S. Miller } 45399c4a634SDavid S. Miller 45499c4a634SDavid S. Miller /** 45599c4a634SDavid S. Miller * at91_activate_rx_mb - reactive single rx mailbox 45699c4a634SDavid S. Miller * @priv: a91 context 45799c4a634SDavid S. Miller * @mb: mailbox to reactivate 45899c4a634SDavid S. Miller * 45999c4a634SDavid S. Miller * Reenables given mailbox for reception of new CAN messages 46099c4a634SDavid S. Miller */ 46199c4a634SDavid S. Miller static inline void at91_activate_rx_mb(const struct at91_priv *priv, 46299c4a634SDavid S. Miller unsigned int mb) 46399c4a634SDavid S. Miller { 46499c4a634SDavid S. Miller u32 mask = 1 << mb; 46599c4a634SDavid S. Miller at91_write(priv, AT91_TCR, mask); 46699c4a634SDavid S. Miller } 46799c4a634SDavid S. Miller 46899c4a634SDavid S. Miller /** 46999c4a634SDavid S. Miller * at91_rx_overflow_err - send error frame due to rx overflow 47099c4a634SDavid S. Miller * @dev: net device 47199c4a634SDavid S. Miller */ 47299c4a634SDavid S. Miller static void at91_rx_overflow_err(struct net_device *dev) 47399c4a634SDavid S. Miller { 47499c4a634SDavid S. Miller struct net_device_stats *stats = &dev->stats; 47599c4a634SDavid S. Miller struct sk_buff *skb; 47699c4a634SDavid S. Miller struct can_frame *cf; 47799c4a634SDavid S. Miller 478882055c8SMarc Kleine-Budde netdev_dbg(dev, "RX buffer overflow\n"); 47999c4a634SDavid S. Miller stats->rx_over_errors++; 48099c4a634SDavid S. Miller stats->rx_errors++; 48199c4a634SDavid S. Miller 48299c4a634SDavid S. Miller skb = alloc_can_err_skb(dev, &cf); 48399c4a634SDavid S. Miller if (unlikely(!skb)) 48499c4a634SDavid S. Miller return; 48599c4a634SDavid S. Miller 48699c4a634SDavid S. Miller cf->can_id |= CAN_ERR_CRTL; 48799c4a634SDavid S. Miller cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; 48899c4a634SDavid S. Miller netif_receive_skb(skb); 48999c4a634SDavid S. Miller 49099c4a634SDavid S. Miller stats->rx_packets++; 49199c4a634SDavid S. Miller stats->rx_bytes += cf->can_dlc; 49299c4a634SDavid S. Miller } 49399c4a634SDavid S. Miller 49499c4a634SDavid S. Miller /** 49599c4a634SDavid S. Miller * at91_read_mb - read CAN msg from mailbox (lowlevel impl) 49699c4a634SDavid S. Miller * @dev: net device 49799c4a634SDavid S. Miller * @mb: mailbox number to read from 49899c4a634SDavid S. Miller * @cf: can frame where to store message 49999c4a634SDavid S. Miller * 50099c4a634SDavid S. Miller * Reads a CAN message from the given mailbox and stores data into 50199c4a634SDavid S. Miller * given can frame. "mb" and "cf" must be valid. 50299c4a634SDavid S. Miller */ 50399c4a634SDavid S. Miller static void at91_read_mb(struct net_device *dev, unsigned int mb, 50499c4a634SDavid S. Miller struct can_frame *cf) 50599c4a634SDavid S. Miller { 50699c4a634SDavid S. Miller const struct at91_priv *priv = netdev_priv(dev); 50799c4a634SDavid S. Miller u32 reg_msr, reg_mid; 50899c4a634SDavid S. Miller 50999c4a634SDavid S. Miller reg_mid = at91_read(priv, AT91_MID(mb)); 51099c4a634SDavid S. Miller if (reg_mid & AT91_MID_MIDE) 51199c4a634SDavid S. Miller cf->can_id = ((reg_mid >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG; 51299c4a634SDavid S. Miller else 51399c4a634SDavid S. Miller cf->can_id = (reg_mid >> 18) & CAN_SFF_MASK; 51499c4a634SDavid S. Miller 51599c4a634SDavid S. Miller reg_msr = at91_read(priv, AT91_MSR(mb)); 51699c4a634SDavid S. Miller if (reg_msr & AT91_MSR_MRTR) 51799c4a634SDavid S. Miller cf->can_id |= CAN_RTR_FLAG; 518c7cd606fSOliver Hartkopp cf->can_dlc = get_can_dlc((reg_msr >> 16) & 0xf); 51999c4a634SDavid S. Miller 52099c4a634SDavid S. Miller *(u32 *)(cf->data + 0) = at91_read(priv, AT91_MDL(mb)); 52199c4a634SDavid S. Miller *(u32 *)(cf->data + 4) = at91_read(priv, AT91_MDH(mb)); 52299c4a634SDavid S. Miller 5238a0e0a49SMarc Kleine-Budde /* allow RX of extended frames */ 5248a0e0a49SMarc Kleine-Budde at91_write(priv, AT91_MID(mb), AT91_MID_MIDE); 5258a0e0a49SMarc Kleine-Budde 52699c4a634SDavid S. Miller if (unlikely(mb == AT91_MB_RX_LAST && reg_msr & AT91_MSR_MMI)) 52799c4a634SDavid S. Miller at91_rx_overflow_err(dev); 52899c4a634SDavid S. Miller } 52999c4a634SDavid S. Miller 53099c4a634SDavid S. Miller /** 53199c4a634SDavid S. Miller * at91_read_msg - read CAN message from mailbox 53299c4a634SDavid S. Miller * @dev: net device 53399c4a634SDavid S. Miller * @mb: mail box to read from 53499c4a634SDavid S. Miller * 53599c4a634SDavid S. Miller * Reads a CAN message from given mailbox, and put into linux network 53699c4a634SDavid S. Miller * RX queue, does all housekeeping chores (stats, ...) 53799c4a634SDavid S. Miller */ 53899c4a634SDavid S. Miller static void at91_read_msg(struct net_device *dev, unsigned int mb) 53999c4a634SDavid S. Miller { 54099c4a634SDavid S. Miller struct net_device_stats *stats = &dev->stats; 54199c4a634SDavid S. Miller struct can_frame *cf; 54299c4a634SDavid S. Miller struct sk_buff *skb; 54399c4a634SDavid S. Miller 54499c4a634SDavid S. Miller skb = alloc_can_skb(dev, &cf); 54599c4a634SDavid S. Miller if (unlikely(!skb)) { 54699c4a634SDavid S. Miller stats->rx_dropped++; 54799c4a634SDavid S. Miller return; 54899c4a634SDavid S. Miller } 54999c4a634SDavid S. Miller 55099c4a634SDavid S. Miller at91_read_mb(dev, mb, cf); 55199c4a634SDavid S. Miller netif_receive_skb(skb); 55299c4a634SDavid S. Miller 55399c4a634SDavid S. Miller stats->rx_packets++; 55499c4a634SDavid S. Miller stats->rx_bytes += cf->can_dlc; 55599c4a634SDavid S. Miller } 55699c4a634SDavid S. Miller 55799c4a634SDavid S. Miller /** 55899c4a634SDavid S. Miller * at91_poll_rx - read multiple CAN messages from mailboxes 55999c4a634SDavid S. Miller * @dev: net device 56099c4a634SDavid S. Miller * @quota: max number of pkgs we're allowed to receive 56199c4a634SDavid S. Miller * 56299c4a634SDavid S. Miller * Theory of Operation: 56399c4a634SDavid S. Miller * 5649e0a2d1cSMarc Kleine-Budde * 11 of the 16 mailboxes on the chip are reserved for RX. we split 5659e0a2d1cSMarc Kleine-Budde * them into 2 groups. The lower group holds 7 and upper 4 mailboxes. 56699c4a634SDavid S. Miller * 56799c4a634SDavid S. Miller * Like it or not, but the chip always saves a received CAN message 56899c4a634SDavid S. Miller * into the first free mailbox it finds (starting with the 56999c4a634SDavid S. Miller * lowest). This makes it very difficult to read the messages in the 57099c4a634SDavid S. Miller * right order from the chip. This is how we work around that problem: 57199c4a634SDavid S. Miller * 5729e0a2d1cSMarc Kleine-Budde * The first message goes into mb nr. 1 and issues an interrupt. All 57399c4a634SDavid S. Miller * rx ints are disabled in the interrupt handler and a napi poll is 57499c4a634SDavid S. Miller * scheduled. We read the mailbox, but do _not_ reenable the mb (to 57599c4a634SDavid S. Miller * receive another message). 57699c4a634SDavid S. Miller * 57799c4a634SDavid S. Miller * lower mbxs upper 5789e0a2d1cSMarc Kleine-Budde * ____^______ __^__ 57999c4a634SDavid S. Miller * / \ / \ 58099c4a634SDavid S. Miller * +-+-+-+-+-+-+-+-++-+-+-+-+ 5819e0a2d1cSMarc Kleine-Budde * | |x|x|x|x|x|x|x|| | | | | 58299c4a634SDavid S. Miller * +-+-+-+-+-+-+-+-++-+-+-+-+ 58399c4a634SDavid S. Miller * 0 0 0 0 0 0 0 0 0 0 1 1 \ mail 58499c4a634SDavid S. Miller * 0 1 2 3 4 5 6 7 8 9 0 1 / box 5859e0a2d1cSMarc Kleine-Budde * ^ 5869e0a2d1cSMarc Kleine-Budde * | 5879e0a2d1cSMarc Kleine-Budde * \ 5889e0a2d1cSMarc Kleine-Budde * unused, due to chip bug 58999c4a634SDavid S. Miller * 59099c4a634SDavid S. Miller * The variable priv->rx_next points to the next mailbox to read a 59199c4a634SDavid S. Miller * message from. As long we're in the lower mailboxes we just read the 59299c4a634SDavid S. Miller * mailbox but not reenable it. 59399c4a634SDavid S. Miller * 59499c4a634SDavid S. Miller * With completion of the last of the lower mailboxes, we reenable the 59599c4a634SDavid S. Miller * whole first group, but continue to look for filled mailboxes in the 59699c4a634SDavid S. Miller * upper mailboxes. Imagine the second group like overflow mailboxes, 59799c4a634SDavid S. Miller * which takes CAN messages if the lower goup is full. While in the 59899c4a634SDavid S. Miller * upper group we reenable the mailbox right after reading it. Giving 59999c4a634SDavid S. Miller * the chip more room to store messages. 60099c4a634SDavid S. Miller * 60199c4a634SDavid S. Miller * After finishing we look again in the lower group if we've still 60299c4a634SDavid S. Miller * quota. 60399c4a634SDavid S. Miller * 60499c4a634SDavid S. Miller */ 60599c4a634SDavid S. Miller static int at91_poll_rx(struct net_device *dev, int quota) 60699c4a634SDavid S. Miller { 60799c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 60899c4a634SDavid S. Miller u32 reg_sr = at91_read(priv, AT91_SR); 60999c4a634SDavid S. Miller const unsigned long *addr = (unsigned long *)®_sr; 61099c4a634SDavid S. Miller unsigned int mb; 61199c4a634SDavid S. Miller int received = 0; 61299c4a634SDavid S. Miller 61399c4a634SDavid S. Miller if (priv->rx_next > AT91_MB_RX_LOW_LAST && 61499c4a634SDavid S. Miller reg_sr & AT91_MB_RX_LOW_MASK) 615882055c8SMarc Kleine-Budde netdev_info(dev, 61699c4a634SDavid S. Miller "order of incoming frames cannot be guaranteed\n"); 61799c4a634SDavid S. Miller 61899c4a634SDavid S. Miller again: 6190909c1ecSMarc Kleine-Budde for (mb = find_next_bit(addr, AT91_MB_RX_LAST + 1, priv->rx_next); 6200909c1ecSMarc Kleine-Budde mb < AT91_MB_RX_LAST + 1 && quota > 0; 62199c4a634SDavid S. Miller reg_sr = at91_read(priv, AT91_SR), 6220909c1ecSMarc Kleine-Budde mb = find_next_bit(addr, AT91_MB_RX_LAST + 1, ++priv->rx_next)) { 62399c4a634SDavid S. Miller at91_read_msg(dev, mb); 62499c4a634SDavid S. Miller 62599c4a634SDavid S. Miller /* reactivate mailboxes */ 62699c4a634SDavid S. Miller if (mb == AT91_MB_RX_LOW_LAST) 62799c4a634SDavid S. Miller /* all lower mailboxed, if just finished it */ 62899c4a634SDavid S. Miller at91_activate_rx_low(priv); 62999c4a634SDavid S. Miller else if (mb > AT91_MB_RX_LOW_LAST) 63099c4a634SDavid S. Miller /* only the mailbox we read */ 63199c4a634SDavid S. Miller at91_activate_rx_mb(priv, mb); 63299c4a634SDavid S. Miller 63399c4a634SDavid S. Miller received++; 63499c4a634SDavid S. Miller quota--; 63599c4a634SDavid S. Miller } 63699c4a634SDavid S. Miller 63799c4a634SDavid S. Miller /* upper group completed, look again in lower */ 63899c4a634SDavid S. Miller if (priv->rx_next > AT91_MB_RX_LOW_LAST && 6390909c1ecSMarc Kleine-Budde quota > 0 && mb > AT91_MB_RX_LAST) { 6400909c1ecSMarc Kleine-Budde priv->rx_next = AT91_MB_RX_FIRST; 64199c4a634SDavid S. Miller goto again; 64299c4a634SDavid S. Miller } 64399c4a634SDavid S. Miller 64499c4a634SDavid S. Miller return received; 64599c4a634SDavid S. Miller } 64699c4a634SDavid S. Miller 64799c4a634SDavid S. Miller static void at91_poll_err_frame(struct net_device *dev, 64899c4a634SDavid S. Miller struct can_frame *cf, u32 reg_sr) 64999c4a634SDavid S. Miller { 65099c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 65199c4a634SDavid S. Miller 65299c4a634SDavid S. Miller /* CRC error */ 65399c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_CERR) { 654882055c8SMarc Kleine-Budde netdev_dbg(dev, "CERR irq\n"); 65599c4a634SDavid S. Miller dev->stats.rx_errors++; 65699c4a634SDavid S. Miller priv->can.can_stats.bus_error++; 65799c4a634SDavid S. Miller cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; 65899c4a634SDavid S. Miller } 65999c4a634SDavid S. Miller 66099c4a634SDavid S. Miller /* Stuffing Error */ 66199c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_SERR) { 662882055c8SMarc Kleine-Budde netdev_dbg(dev, "SERR irq\n"); 66399c4a634SDavid S. Miller dev->stats.rx_errors++; 66499c4a634SDavid S. Miller priv->can.can_stats.bus_error++; 66599c4a634SDavid S. Miller cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; 66699c4a634SDavid S. Miller cf->data[2] |= CAN_ERR_PROT_STUFF; 66799c4a634SDavid S. Miller } 66899c4a634SDavid S. Miller 66999c4a634SDavid S. Miller /* Acknowledgement Error */ 67099c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_AERR) { 671882055c8SMarc Kleine-Budde netdev_dbg(dev, "AERR irq\n"); 67299c4a634SDavid S. Miller dev->stats.tx_errors++; 67399c4a634SDavid S. Miller cf->can_id |= CAN_ERR_ACK; 67499c4a634SDavid S. Miller } 67599c4a634SDavid S. Miller 67699c4a634SDavid S. Miller /* Form error */ 67799c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_FERR) { 678882055c8SMarc Kleine-Budde netdev_dbg(dev, "FERR irq\n"); 67999c4a634SDavid S. Miller dev->stats.rx_errors++; 68099c4a634SDavid S. Miller priv->can.can_stats.bus_error++; 68199c4a634SDavid S. Miller cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; 68299c4a634SDavid S. Miller cf->data[2] |= CAN_ERR_PROT_FORM; 68399c4a634SDavid S. Miller } 68499c4a634SDavid S. Miller 68599c4a634SDavid S. Miller /* Bit Error */ 68699c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_BERR) { 687882055c8SMarc Kleine-Budde netdev_dbg(dev, "BERR irq\n"); 68899c4a634SDavid S. Miller dev->stats.tx_errors++; 68999c4a634SDavid S. Miller priv->can.can_stats.bus_error++; 69099c4a634SDavid S. Miller cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; 69199c4a634SDavid S. Miller cf->data[2] |= CAN_ERR_PROT_BIT; 69299c4a634SDavid S. Miller } 69399c4a634SDavid S. Miller } 69499c4a634SDavid S. Miller 69599c4a634SDavid S. Miller static int at91_poll_err(struct net_device *dev, int quota, u32 reg_sr) 69699c4a634SDavid S. Miller { 69799c4a634SDavid S. Miller struct sk_buff *skb; 69899c4a634SDavid S. Miller struct can_frame *cf; 69999c4a634SDavid S. Miller 70099c4a634SDavid S. Miller if (quota == 0) 70199c4a634SDavid S. Miller return 0; 70299c4a634SDavid S. Miller 70399c4a634SDavid S. Miller skb = alloc_can_err_skb(dev, &cf); 70499c4a634SDavid S. Miller if (unlikely(!skb)) 70599c4a634SDavid S. Miller return 0; 70699c4a634SDavid S. Miller 70799c4a634SDavid S. Miller at91_poll_err_frame(dev, cf, reg_sr); 70899c4a634SDavid S. Miller netif_receive_skb(skb); 70999c4a634SDavid S. Miller 71099c4a634SDavid S. Miller dev->stats.rx_packets++; 71199c4a634SDavid S. Miller dev->stats.rx_bytes += cf->can_dlc; 71299c4a634SDavid S. Miller 71399c4a634SDavid S. Miller return 1; 71499c4a634SDavid S. Miller } 71599c4a634SDavid S. Miller 71699c4a634SDavid S. Miller static int at91_poll(struct napi_struct *napi, int quota) 71799c4a634SDavid S. Miller { 71899c4a634SDavid S. Miller struct net_device *dev = napi->dev; 71999c4a634SDavid S. Miller const struct at91_priv *priv = netdev_priv(dev); 72099c4a634SDavid S. Miller u32 reg_sr = at91_read(priv, AT91_SR); 72199c4a634SDavid S. Miller int work_done = 0; 72299c4a634SDavid S. Miller 72399c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_MB_RX) 72499c4a634SDavid S. Miller work_done += at91_poll_rx(dev, quota - work_done); 72599c4a634SDavid S. Miller 72699c4a634SDavid S. Miller /* 72799c4a634SDavid S. Miller * The error bits are clear on read, 72899c4a634SDavid S. Miller * so use saved value from irq handler. 72999c4a634SDavid S. Miller */ 73099c4a634SDavid S. Miller reg_sr |= priv->reg_sr; 73199c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_ERR_FRAME) 73299c4a634SDavid S. Miller work_done += at91_poll_err(dev, quota - work_done, reg_sr); 73399c4a634SDavid S. Miller 73499c4a634SDavid S. Miller if (work_done < quota) { 73599c4a634SDavid S. Miller /* enable IRQs for frame errors and all mailboxes >= rx_next */ 73699c4a634SDavid S. Miller u32 reg_ier = AT91_IRQ_ERR_FRAME; 73799c4a634SDavid S. Miller reg_ier |= AT91_IRQ_MB_RX & ~AT91_MB_RX_MASK(priv->rx_next); 73899c4a634SDavid S. Miller 73999c4a634SDavid S. Miller napi_complete(napi); 74099c4a634SDavid S. Miller at91_write(priv, AT91_IER, reg_ier); 74199c4a634SDavid S. Miller } 74299c4a634SDavid S. Miller 74399c4a634SDavid S. Miller return work_done; 74499c4a634SDavid S. Miller } 74599c4a634SDavid S. Miller 74699c4a634SDavid S. Miller /* 74799c4a634SDavid S. Miller * theory of operation: 74899c4a634SDavid S. Miller * 74999c4a634SDavid S. Miller * priv->tx_echo holds the number of the oldest can_frame put for 75099c4a634SDavid S. Miller * transmission into the hardware, but not yet ACKed by the CAN tx 75199c4a634SDavid S. Miller * complete IRQ. 75299c4a634SDavid S. Miller * 75399c4a634SDavid S. Miller * We iterate from priv->tx_echo to priv->tx_next and check if the 75499c4a634SDavid S. Miller * packet has been transmitted, echo it back to the CAN framework. If 75599c4a634SDavid S. Miller * we discover a not yet transmitted package, stop looking for more. 75699c4a634SDavid S. Miller * 75799c4a634SDavid S. Miller */ 75899c4a634SDavid S. Miller static void at91_irq_tx(struct net_device *dev, u32 reg_sr) 75999c4a634SDavid S. Miller { 76099c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 76199c4a634SDavid S. Miller u32 reg_msr; 76299c4a634SDavid S. Miller unsigned int mb; 76399c4a634SDavid S. Miller 76499c4a634SDavid S. Miller /* masking of reg_sr not needed, already done by at91_irq */ 76599c4a634SDavid S. Miller 76699c4a634SDavid S. Miller for (/* nix */; (priv->tx_next - priv->tx_echo) > 0; priv->tx_echo++) { 76799c4a634SDavid S. Miller mb = get_tx_echo_mb(priv); 76899c4a634SDavid S. Miller 76999c4a634SDavid S. Miller /* no event in mailbox? */ 77099c4a634SDavid S. Miller if (!(reg_sr & (1 << mb))) 77199c4a634SDavid S. Miller break; 77299c4a634SDavid S. Miller 77399c4a634SDavid S. Miller /* Disable irq for this TX mailbox */ 77499c4a634SDavid S. Miller at91_write(priv, AT91_IDR, 1 << mb); 77599c4a634SDavid S. Miller 77699c4a634SDavid S. Miller /* 77799c4a634SDavid S. Miller * only echo if mailbox signals us a transfer 77899c4a634SDavid S. Miller * complete (MSR_MRDY). Otherwise it's a tansfer 77999c4a634SDavid S. Miller * abort. "can_bus_off()" takes care about the skbs 78099c4a634SDavid S. Miller * parked in the echo queue. 78199c4a634SDavid S. Miller */ 78299c4a634SDavid S. Miller reg_msr = at91_read(priv, AT91_MSR(mb)); 78399c4a634SDavid S. Miller if (likely(reg_msr & AT91_MSR_MRDY && 78499c4a634SDavid S. Miller ~reg_msr & AT91_MSR_MABT)) { 785*25985edcSLucas De Marchi /* _NOTE_: subtract AT91_MB_TX_FIRST offset from mb! */ 78699c4a634SDavid S. Miller can_get_echo_skb(dev, mb - AT91_MB_TX_FIRST); 78799c4a634SDavid S. Miller dev->stats.tx_packets++; 78899c4a634SDavid S. Miller } 78999c4a634SDavid S. Miller } 79099c4a634SDavid S. Miller 79199c4a634SDavid S. Miller /* 79299c4a634SDavid S. Miller * restart queue if we don't have a wrap around but restart if 79399c4a634SDavid S. Miller * we get a TX int for the last can frame directly before a 79499c4a634SDavid S. Miller * wrap around. 79599c4a634SDavid S. Miller */ 79699c4a634SDavid S. Miller if ((priv->tx_next & AT91_NEXT_MASK) != 0 || 79799c4a634SDavid S. Miller (priv->tx_echo & AT91_NEXT_MASK) == 0) 79899c4a634SDavid S. Miller netif_wake_queue(dev); 79999c4a634SDavid S. Miller } 80099c4a634SDavid S. Miller 80199c4a634SDavid S. Miller static void at91_irq_err_state(struct net_device *dev, 80299c4a634SDavid S. Miller struct can_frame *cf, enum can_state new_state) 80399c4a634SDavid S. Miller { 80499c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 80533a6f298SMarc Kleine-Budde u32 reg_idr = 0, reg_ier = 0; 80633a6f298SMarc Kleine-Budde struct can_berr_counter bec; 80799c4a634SDavid S. Miller 80833a6f298SMarc Kleine-Budde at91_get_berr_counter(dev, &bec); 80999c4a634SDavid S. Miller 81099c4a634SDavid S. Miller switch (priv->can.state) { 81199c4a634SDavid S. Miller case CAN_STATE_ERROR_ACTIVE: 81299c4a634SDavid S. Miller /* 81399c4a634SDavid S. Miller * from: ERROR_ACTIVE 81499c4a634SDavid S. Miller * to : ERROR_WARNING, ERROR_PASSIVE, BUS_OFF 81599c4a634SDavid S. Miller * => : there was a warning int 81699c4a634SDavid S. Miller */ 81799c4a634SDavid S. Miller if (new_state >= CAN_STATE_ERROR_WARNING && 81899c4a634SDavid S. Miller new_state <= CAN_STATE_BUS_OFF) { 819882055c8SMarc Kleine-Budde netdev_dbg(dev, "Error Warning IRQ\n"); 82099c4a634SDavid S. Miller priv->can.can_stats.error_warning++; 82199c4a634SDavid S. Miller 82299c4a634SDavid S. Miller cf->can_id |= CAN_ERR_CRTL; 82333a6f298SMarc Kleine-Budde cf->data[1] = (bec.txerr > bec.rxerr) ? 82499c4a634SDavid S. Miller CAN_ERR_CRTL_TX_WARNING : 82599c4a634SDavid S. Miller CAN_ERR_CRTL_RX_WARNING; 82699c4a634SDavid S. Miller } 82799c4a634SDavid S. Miller case CAN_STATE_ERROR_WARNING: /* fallthrough */ 82899c4a634SDavid S. Miller /* 82999c4a634SDavid S. Miller * from: ERROR_ACTIVE, ERROR_WARNING 83099c4a634SDavid S. Miller * to : ERROR_PASSIVE, BUS_OFF 83199c4a634SDavid S. Miller * => : error passive int 83299c4a634SDavid S. Miller */ 83399c4a634SDavid S. Miller if (new_state >= CAN_STATE_ERROR_PASSIVE && 83499c4a634SDavid S. Miller new_state <= CAN_STATE_BUS_OFF) { 835882055c8SMarc Kleine-Budde netdev_dbg(dev, "Error Passive IRQ\n"); 83699c4a634SDavid S. Miller priv->can.can_stats.error_passive++; 83799c4a634SDavid S. Miller 83899c4a634SDavid S. Miller cf->can_id |= CAN_ERR_CRTL; 83933a6f298SMarc Kleine-Budde cf->data[1] = (bec.txerr > bec.rxerr) ? 84099c4a634SDavid S. Miller CAN_ERR_CRTL_TX_PASSIVE : 84199c4a634SDavid S. Miller CAN_ERR_CRTL_RX_PASSIVE; 84299c4a634SDavid S. Miller } 84399c4a634SDavid S. Miller break; 84499c4a634SDavid S. Miller case CAN_STATE_BUS_OFF: 84599c4a634SDavid S. Miller /* 84699c4a634SDavid S. Miller * from: BUS_OFF 84799c4a634SDavid S. Miller * to : ERROR_ACTIVE, ERROR_WARNING, ERROR_PASSIVE 84899c4a634SDavid S. Miller */ 84999c4a634SDavid S. Miller if (new_state <= CAN_STATE_ERROR_PASSIVE) { 85099c4a634SDavid S. Miller cf->can_id |= CAN_ERR_RESTARTED; 85199c4a634SDavid S. Miller 852882055c8SMarc Kleine-Budde netdev_dbg(dev, "restarted\n"); 85399c4a634SDavid S. Miller priv->can.can_stats.restarts++; 85499c4a634SDavid S. Miller 85599c4a634SDavid S. Miller netif_carrier_on(dev); 85699c4a634SDavid S. Miller netif_wake_queue(dev); 85799c4a634SDavid S. Miller } 85899c4a634SDavid S. Miller break; 85999c4a634SDavid S. Miller default: 86099c4a634SDavid S. Miller break; 86199c4a634SDavid S. Miller } 86299c4a634SDavid S. Miller 86399c4a634SDavid S. Miller 86499c4a634SDavid S. Miller /* process state changes depending on the new state */ 86599c4a634SDavid S. Miller switch (new_state) { 86699c4a634SDavid S. Miller case CAN_STATE_ERROR_ACTIVE: 86799c4a634SDavid S. Miller /* 86899c4a634SDavid S. Miller * actually we want to enable AT91_IRQ_WARN here, but 86999c4a634SDavid S. Miller * it screws up the system under certain 87099c4a634SDavid S. Miller * circumstances. so just enable AT91_IRQ_ERRP, thus 87199c4a634SDavid S. Miller * the "fallthrough" 87299c4a634SDavid S. Miller */ 873882055c8SMarc Kleine-Budde netdev_dbg(dev, "Error Active\n"); 87499c4a634SDavid S. Miller cf->can_id |= CAN_ERR_PROT; 87599c4a634SDavid S. Miller cf->data[2] = CAN_ERR_PROT_ACTIVE; 87699c4a634SDavid S. Miller case CAN_STATE_ERROR_WARNING: /* fallthrough */ 87799c4a634SDavid S. Miller reg_idr = AT91_IRQ_ERRA | AT91_IRQ_WARN | AT91_IRQ_BOFF; 87899c4a634SDavid S. Miller reg_ier = AT91_IRQ_ERRP; 87999c4a634SDavid S. Miller break; 88099c4a634SDavid S. Miller case CAN_STATE_ERROR_PASSIVE: 88199c4a634SDavid S. Miller reg_idr = AT91_IRQ_ERRA | AT91_IRQ_WARN | AT91_IRQ_ERRP; 88299c4a634SDavid S. Miller reg_ier = AT91_IRQ_BOFF; 88399c4a634SDavid S. Miller break; 88499c4a634SDavid S. Miller case CAN_STATE_BUS_OFF: 88599c4a634SDavid S. Miller reg_idr = AT91_IRQ_ERRA | AT91_IRQ_ERRP | 88699c4a634SDavid S. Miller AT91_IRQ_WARN | AT91_IRQ_BOFF; 88799c4a634SDavid S. Miller reg_ier = 0; 88899c4a634SDavid S. Miller 88999c4a634SDavid S. Miller cf->can_id |= CAN_ERR_BUSOFF; 89099c4a634SDavid S. Miller 891882055c8SMarc Kleine-Budde netdev_dbg(dev, "bus-off\n"); 89299c4a634SDavid S. Miller netif_carrier_off(dev); 89399c4a634SDavid S. Miller priv->can.can_stats.bus_off++; 89499c4a634SDavid S. Miller 89599c4a634SDavid S. Miller /* turn off chip, if restart is disabled */ 89699c4a634SDavid S. Miller if (!priv->can.restart_ms) { 89799c4a634SDavid S. Miller at91_chip_stop(dev, CAN_STATE_BUS_OFF); 89899c4a634SDavid S. Miller return; 89999c4a634SDavid S. Miller } 90099c4a634SDavid S. Miller break; 90199c4a634SDavid S. Miller default: 90299c4a634SDavid S. Miller break; 90399c4a634SDavid S. Miller } 90499c4a634SDavid S. Miller 90599c4a634SDavid S. Miller at91_write(priv, AT91_IDR, reg_idr); 90699c4a634SDavid S. Miller at91_write(priv, AT91_IER, reg_ier); 90799c4a634SDavid S. Miller } 90899c4a634SDavid S. Miller 90999c4a634SDavid S. Miller static void at91_irq_err(struct net_device *dev) 91099c4a634SDavid S. Miller { 91199c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 91299c4a634SDavid S. Miller struct sk_buff *skb; 91399c4a634SDavid S. Miller struct can_frame *cf; 91499c4a634SDavid S. Miller enum can_state new_state; 91599c4a634SDavid S. Miller u32 reg_sr; 91699c4a634SDavid S. Miller 91799c4a634SDavid S. Miller reg_sr = at91_read(priv, AT91_SR); 91899c4a634SDavid S. Miller 91999c4a634SDavid S. Miller /* we need to look at the unmasked reg_sr */ 92099c4a634SDavid S. Miller if (unlikely(reg_sr & AT91_IRQ_BOFF)) 92199c4a634SDavid S. Miller new_state = CAN_STATE_BUS_OFF; 92299c4a634SDavid S. Miller else if (unlikely(reg_sr & AT91_IRQ_ERRP)) 92399c4a634SDavid S. Miller new_state = CAN_STATE_ERROR_PASSIVE; 92499c4a634SDavid S. Miller else if (unlikely(reg_sr & AT91_IRQ_WARN)) 92599c4a634SDavid S. Miller new_state = CAN_STATE_ERROR_WARNING; 92699c4a634SDavid S. Miller else if (likely(reg_sr & AT91_IRQ_ERRA)) 92799c4a634SDavid S. Miller new_state = CAN_STATE_ERROR_ACTIVE; 92899c4a634SDavid S. Miller else { 929882055c8SMarc Kleine-Budde netdev_err(dev, "BUG! hardware in undefined state\n"); 93099c4a634SDavid S. Miller return; 93199c4a634SDavid S. Miller } 93299c4a634SDavid S. Miller 93399c4a634SDavid S. Miller /* state hasn't changed */ 93499c4a634SDavid S. Miller if (likely(new_state == priv->can.state)) 93599c4a634SDavid S. Miller return; 93699c4a634SDavid S. Miller 93799c4a634SDavid S. Miller skb = alloc_can_err_skb(dev, &cf); 93899c4a634SDavid S. Miller if (unlikely(!skb)) 93999c4a634SDavid S. Miller return; 94099c4a634SDavid S. Miller 94199c4a634SDavid S. Miller at91_irq_err_state(dev, cf, new_state); 94299c4a634SDavid S. Miller netif_rx(skb); 94399c4a634SDavid S. Miller 94499c4a634SDavid S. Miller dev->stats.rx_packets++; 94599c4a634SDavid S. Miller dev->stats.rx_bytes += cf->can_dlc; 94699c4a634SDavid S. Miller 94799c4a634SDavid S. Miller priv->can.state = new_state; 94899c4a634SDavid S. Miller } 94999c4a634SDavid S. Miller 95099c4a634SDavid S. Miller /* 95199c4a634SDavid S. Miller * interrupt handler 95299c4a634SDavid S. Miller */ 95399c4a634SDavid S. Miller static irqreturn_t at91_irq(int irq, void *dev_id) 95499c4a634SDavid S. Miller { 95599c4a634SDavid S. Miller struct net_device *dev = dev_id; 95699c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 95799c4a634SDavid S. Miller irqreturn_t handled = IRQ_NONE; 95899c4a634SDavid S. Miller u32 reg_sr, reg_imr; 95999c4a634SDavid S. Miller 96099c4a634SDavid S. Miller reg_sr = at91_read(priv, AT91_SR); 96199c4a634SDavid S. Miller reg_imr = at91_read(priv, AT91_IMR); 96299c4a634SDavid S. Miller 96399c4a634SDavid S. Miller /* Ignore masked interrupts */ 96499c4a634SDavid S. Miller reg_sr &= reg_imr; 96599c4a634SDavid S. Miller if (!reg_sr) 96699c4a634SDavid S. Miller goto exit; 96799c4a634SDavid S. Miller 96899c4a634SDavid S. Miller handled = IRQ_HANDLED; 96999c4a634SDavid S. Miller 97099c4a634SDavid S. Miller /* Receive or error interrupt? -> napi */ 97199c4a634SDavid S. Miller if (reg_sr & (AT91_IRQ_MB_RX | AT91_IRQ_ERR_FRAME)) { 97299c4a634SDavid S. Miller /* 97399c4a634SDavid S. Miller * The error bits are clear on read, 97499c4a634SDavid S. Miller * save for later use. 97599c4a634SDavid S. Miller */ 97699c4a634SDavid S. Miller priv->reg_sr = reg_sr; 97799c4a634SDavid S. Miller at91_write(priv, AT91_IDR, 97899c4a634SDavid S. Miller AT91_IRQ_MB_RX | AT91_IRQ_ERR_FRAME); 97999c4a634SDavid S. Miller napi_schedule(&priv->napi); 98099c4a634SDavid S. Miller } 98199c4a634SDavid S. Miller 98299c4a634SDavid S. Miller /* Transmission complete interrupt */ 98399c4a634SDavid S. Miller if (reg_sr & AT91_IRQ_MB_TX) 98499c4a634SDavid S. Miller at91_irq_tx(dev, reg_sr); 98599c4a634SDavid S. Miller 98699c4a634SDavid S. Miller at91_irq_err(dev); 98799c4a634SDavid S. Miller 98899c4a634SDavid S. Miller exit: 98999c4a634SDavid S. Miller return handled; 99099c4a634SDavid S. Miller } 99199c4a634SDavid S. Miller 99299c4a634SDavid S. Miller static int at91_open(struct net_device *dev) 99399c4a634SDavid S. Miller { 99499c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 99599c4a634SDavid S. Miller int err; 99699c4a634SDavid S. Miller 99799c4a634SDavid S. Miller clk_enable(priv->clk); 99899c4a634SDavid S. Miller 99999c4a634SDavid S. Miller /* check or determine and set bittime */ 100099c4a634SDavid S. Miller err = open_candev(dev); 100199c4a634SDavid S. Miller if (err) 100299c4a634SDavid S. Miller goto out; 100399c4a634SDavid S. Miller 100499c4a634SDavid S. Miller /* register interrupt handler */ 100599c4a634SDavid S. Miller if (request_irq(dev->irq, at91_irq, IRQF_SHARED, 100699c4a634SDavid S. Miller dev->name, dev)) { 100799c4a634SDavid S. Miller err = -EAGAIN; 100899c4a634SDavid S. Miller goto out_close; 100999c4a634SDavid S. Miller } 101099c4a634SDavid S. Miller 101199c4a634SDavid S. Miller /* start chip and queuing */ 101299c4a634SDavid S. Miller at91_chip_start(dev); 101399c4a634SDavid S. Miller napi_enable(&priv->napi); 101499c4a634SDavid S. Miller netif_start_queue(dev); 101599c4a634SDavid S. Miller 101699c4a634SDavid S. Miller return 0; 101799c4a634SDavid S. Miller 101899c4a634SDavid S. Miller out_close: 101999c4a634SDavid S. Miller close_candev(dev); 102099c4a634SDavid S. Miller out: 102199c4a634SDavid S. Miller clk_disable(priv->clk); 102299c4a634SDavid S. Miller 102399c4a634SDavid S. Miller return err; 102499c4a634SDavid S. Miller } 102599c4a634SDavid S. Miller 102699c4a634SDavid S. Miller /* 102799c4a634SDavid S. Miller * stop CAN bus activity 102899c4a634SDavid S. Miller */ 102999c4a634SDavid S. Miller static int at91_close(struct net_device *dev) 103099c4a634SDavid S. Miller { 103199c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 103299c4a634SDavid S. Miller 103399c4a634SDavid S. Miller netif_stop_queue(dev); 103499c4a634SDavid S. Miller napi_disable(&priv->napi); 103599c4a634SDavid S. Miller at91_chip_stop(dev, CAN_STATE_STOPPED); 103699c4a634SDavid S. Miller 103799c4a634SDavid S. Miller free_irq(dev->irq, dev); 103899c4a634SDavid S. Miller clk_disable(priv->clk); 103999c4a634SDavid S. Miller 104099c4a634SDavid S. Miller close_candev(dev); 104199c4a634SDavid S. Miller 104299c4a634SDavid S. Miller return 0; 104399c4a634SDavid S. Miller } 104499c4a634SDavid S. Miller 104599c4a634SDavid S. Miller static int at91_set_mode(struct net_device *dev, enum can_mode mode) 104699c4a634SDavid S. Miller { 104799c4a634SDavid S. Miller switch (mode) { 104899c4a634SDavid S. Miller case CAN_MODE_START: 104999c4a634SDavid S. Miller at91_chip_start(dev); 105099c4a634SDavid S. Miller netif_wake_queue(dev); 105199c4a634SDavid S. Miller break; 105299c4a634SDavid S. Miller 105399c4a634SDavid S. Miller default: 105499c4a634SDavid S. Miller return -EOPNOTSUPP; 105599c4a634SDavid S. Miller } 105699c4a634SDavid S. Miller 105799c4a634SDavid S. Miller return 0; 105899c4a634SDavid S. Miller } 105999c4a634SDavid S. Miller 106099c4a634SDavid S. Miller static const struct net_device_ops at91_netdev_ops = { 106199c4a634SDavid S. Miller .ndo_open = at91_open, 106299c4a634SDavid S. Miller .ndo_stop = at91_close, 106399c4a634SDavid S. Miller .ndo_start_xmit = at91_start_xmit, 106499c4a634SDavid S. Miller }; 106599c4a634SDavid S. Miller 10663a5655a5SMarc Kleine-Budde static ssize_t at91_sysfs_show_mb0_id(struct device *dev, 10673a5655a5SMarc Kleine-Budde struct device_attribute *attr, char *buf) 10683a5655a5SMarc Kleine-Budde { 10693a5655a5SMarc Kleine-Budde struct at91_priv *priv = netdev_priv(to_net_dev(dev)); 10703a5655a5SMarc Kleine-Budde 10713a5655a5SMarc Kleine-Budde if (priv->mb0_id & CAN_EFF_FLAG) 10723a5655a5SMarc Kleine-Budde return snprintf(buf, PAGE_SIZE, "0x%08x\n", priv->mb0_id); 10733a5655a5SMarc Kleine-Budde else 10743a5655a5SMarc Kleine-Budde return snprintf(buf, PAGE_SIZE, "0x%03x\n", priv->mb0_id); 10753a5655a5SMarc Kleine-Budde } 10763a5655a5SMarc Kleine-Budde 10773a5655a5SMarc Kleine-Budde static ssize_t at91_sysfs_set_mb0_id(struct device *dev, 10783a5655a5SMarc Kleine-Budde struct device_attribute *attr, const char *buf, size_t count) 10793a5655a5SMarc Kleine-Budde { 10803a5655a5SMarc Kleine-Budde struct net_device *ndev = to_net_dev(dev); 10813a5655a5SMarc Kleine-Budde struct at91_priv *priv = netdev_priv(ndev); 10823a5655a5SMarc Kleine-Budde unsigned long can_id; 10833a5655a5SMarc Kleine-Budde ssize_t ret; 10843a5655a5SMarc Kleine-Budde int err; 10853a5655a5SMarc Kleine-Budde 10863a5655a5SMarc Kleine-Budde rtnl_lock(); 10873a5655a5SMarc Kleine-Budde 10883a5655a5SMarc Kleine-Budde if (ndev->flags & IFF_UP) { 10893a5655a5SMarc Kleine-Budde ret = -EBUSY; 10903a5655a5SMarc Kleine-Budde goto out; 10913a5655a5SMarc Kleine-Budde } 10923a5655a5SMarc Kleine-Budde 10933a5655a5SMarc Kleine-Budde err = strict_strtoul(buf, 0, &can_id); 10943a5655a5SMarc Kleine-Budde if (err) { 10953a5655a5SMarc Kleine-Budde ret = err; 10963a5655a5SMarc Kleine-Budde goto out; 10973a5655a5SMarc Kleine-Budde } 10983a5655a5SMarc Kleine-Budde 10993a5655a5SMarc Kleine-Budde if (can_id & CAN_EFF_FLAG) 11003a5655a5SMarc Kleine-Budde can_id &= CAN_EFF_MASK | CAN_EFF_FLAG; 11013a5655a5SMarc Kleine-Budde else 11023a5655a5SMarc Kleine-Budde can_id &= CAN_SFF_MASK; 11033a5655a5SMarc Kleine-Budde 11043a5655a5SMarc Kleine-Budde priv->mb0_id = can_id; 11053a5655a5SMarc Kleine-Budde ret = count; 11063a5655a5SMarc Kleine-Budde 11073a5655a5SMarc Kleine-Budde out: 11083a5655a5SMarc Kleine-Budde rtnl_unlock(); 11093a5655a5SMarc Kleine-Budde return ret; 11103a5655a5SMarc Kleine-Budde } 11113a5655a5SMarc Kleine-Budde 1112fef52b01SVasiliy Kulikov static DEVICE_ATTR(mb0_id, S_IWUSR | S_IRUGO, 11133a5655a5SMarc Kleine-Budde at91_sysfs_show_mb0_id, at91_sysfs_set_mb0_id); 11143a5655a5SMarc Kleine-Budde 11153a5655a5SMarc Kleine-Budde static struct attribute *at91_sysfs_attrs[] = { 11163a5655a5SMarc Kleine-Budde &dev_attr_mb0_id.attr, 11173a5655a5SMarc Kleine-Budde NULL, 11183a5655a5SMarc Kleine-Budde }; 11193a5655a5SMarc Kleine-Budde 11203a5655a5SMarc Kleine-Budde static struct attribute_group at91_sysfs_attr_group = { 11213a5655a5SMarc Kleine-Budde .attrs = at91_sysfs_attrs, 11223a5655a5SMarc Kleine-Budde }; 11233a5655a5SMarc Kleine-Budde 1124a9d992ecSMarc Kleine-Budde static int __devinit at91_can_probe(struct platform_device *pdev) 112599c4a634SDavid S. Miller { 112699c4a634SDavid S. Miller struct net_device *dev; 112799c4a634SDavid S. Miller struct at91_priv *priv; 112899c4a634SDavid S. Miller struct resource *res; 112999c4a634SDavid S. Miller struct clk *clk; 113099c4a634SDavid S. Miller void __iomem *addr; 113199c4a634SDavid S. Miller int err, irq; 113299c4a634SDavid S. Miller 113399c4a634SDavid S. Miller clk = clk_get(&pdev->dev, "can_clk"); 113499c4a634SDavid S. Miller if (IS_ERR(clk)) { 113599c4a634SDavid S. Miller dev_err(&pdev->dev, "no clock defined\n"); 113699c4a634SDavid S. Miller err = -ENODEV; 113799c4a634SDavid S. Miller goto exit; 113899c4a634SDavid S. Miller } 113999c4a634SDavid S. Miller 114099c4a634SDavid S. Miller res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 114199c4a634SDavid S. Miller irq = platform_get_irq(pdev, 0); 11424773a47dSUwe Kleine-König if (!res || irq <= 0) { 114399c4a634SDavid S. Miller err = -ENODEV; 114499c4a634SDavid S. Miller goto exit_put; 114599c4a634SDavid S. Miller } 114699c4a634SDavid S. Miller 114799c4a634SDavid S. Miller if (!request_mem_region(res->start, 114899c4a634SDavid S. Miller resource_size(res), 114999c4a634SDavid S. Miller pdev->name)) { 115099c4a634SDavid S. Miller err = -EBUSY; 115199c4a634SDavid S. Miller goto exit_put; 115299c4a634SDavid S. Miller } 115399c4a634SDavid S. Miller 115499c4a634SDavid S. Miller addr = ioremap_nocache(res->start, resource_size(res)); 115599c4a634SDavid S. Miller if (!addr) { 115699c4a634SDavid S. Miller err = -ENOMEM; 115799c4a634SDavid S. Miller goto exit_release; 115899c4a634SDavid S. Miller } 115999c4a634SDavid S. Miller 1160a6e4bc53SWolfgang Grandegger dev = alloc_candev(sizeof(struct at91_priv), AT91_MB_TX_NUM); 116199c4a634SDavid S. Miller if (!dev) { 116299c4a634SDavid S. Miller err = -ENOMEM; 116399c4a634SDavid S. Miller goto exit_iounmap; 116499c4a634SDavid S. Miller } 116599c4a634SDavid S. Miller 116699c4a634SDavid S. Miller dev->netdev_ops = &at91_netdev_ops; 116799c4a634SDavid S. Miller dev->irq = irq; 116899c4a634SDavid S. Miller dev->flags |= IFF_ECHO; 11693a5655a5SMarc Kleine-Budde dev->sysfs_groups[0] = &at91_sysfs_attr_group; 117099c4a634SDavid S. Miller 117199c4a634SDavid S. Miller priv = netdev_priv(dev); 117299c4a634SDavid S. Miller priv->can.clock.freq = clk_get_rate(clk); 117399c4a634SDavid S. Miller priv->can.bittiming_const = &at91_bittiming_const; 117499c4a634SDavid S. Miller priv->can.do_set_mode = at91_set_mode; 117533a6f298SMarc Kleine-Budde priv->can.do_get_berr_counter = at91_get_berr_counter; 1176ad72c347SChristian Pellegrin priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES; 117799c4a634SDavid S. Miller priv->reg_base = addr; 117899c4a634SDavid S. Miller priv->dev = dev; 117999c4a634SDavid S. Miller priv->clk = clk; 118099c4a634SDavid S. Miller priv->pdata = pdev->dev.platform_data; 11813a5655a5SMarc Kleine-Budde priv->mb0_id = 0x7ff; 118299c4a634SDavid S. Miller 118399c4a634SDavid S. Miller netif_napi_add(dev, &priv->napi, at91_poll, AT91_NAPI_WEIGHT); 118499c4a634SDavid S. Miller 118599c4a634SDavid S. Miller dev_set_drvdata(&pdev->dev, dev); 118699c4a634SDavid S. Miller SET_NETDEV_DEV(dev, &pdev->dev); 118799c4a634SDavid S. Miller 118899c4a634SDavid S. Miller err = register_candev(dev); 118999c4a634SDavid S. Miller if (err) { 119099c4a634SDavid S. Miller dev_err(&pdev->dev, "registering netdev failed\n"); 119199c4a634SDavid S. Miller goto exit_free; 119299c4a634SDavid S. Miller } 119399c4a634SDavid S. Miller 119499c4a634SDavid S. Miller dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n", 119599c4a634SDavid S. Miller priv->reg_base, dev->irq); 119699c4a634SDavid S. Miller 119799c4a634SDavid S. Miller return 0; 119899c4a634SDavid S. Miller 119999c4a634SDavid S. Miller exit_free: 1200759a6c76SMarc Kleine-Budde free_candev(dev); 120199c4a634SDavid S. Miller exit_iounmap: 120299c4a634SDavid S. Miller iounmap(addr); 120399c4a634SDavid S. Miller exit_release: 120499c4a634SDavid S. Miller release_mem_region(res->start, resource_size(res)); 120599c4a634SDavid S. Miller exit_put: 120699c4a634SDavid S. Miller clk_put(clk); 120799c4a634SDavid S. Miller exit: 120899c4a634SDavid S. Miller return err; 120999c4a634SDavid S. Miller } 121099c4a634SDavid S. Miller 121199c4a634SDavid S. Miller static int __devexit at91_can_remove(struct platform_device *pdev) 121299c4a634SDavid S. Miller { 121399c4a634SDavid S. Miller struct net_device *dev = platform_get_drvdata(pdev); 121499c4a634SDavid S. Miller struct at91_priv *priv = netdev_priv(dev); 121599c4a634SDavid S. Miller struct resource *res; 121699c4a634SDavid S. Miller 121799c4a634SDavid S. Miller unregister_netdev(dev); 121899c4a634SDavid S. Miller 121999c4a634SDavid S. Miller platform_set_drvdata(pdev, NULL); 122099c4a634SDavid S. Miller 122199c4a634SDavid S. Miller iounmap(priv->reg_base); 122299c4a634SDavid S. Miller 122399c4a634SDavid S. Miller res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 122499c4a634SDavid S. Miller release_mem_region(res->start, resource_size(res)); 122599c4a634SDavid S. Miller 122699c4a634SDavid S. Miller clk_put(priv->clk); 122799c4a634SDavid S. Miller 1228759a6c76SMarc Kleine-Budde free_candev(dev); 1229759a6c76SMarc Kleine-Budde 123099c4a634SDavid S. Miller return 0; 123199c4a634SDavid S. Miller } 123299c4a634SDavid S. Miller 123399c4a634SDavid S. Miller static struct platform_driver at91_can_driver = { 123499c4a634SDavid S. Miller .probe = at91_can_probe, 123599c4a634SDavid S. Miller .remove = __devexit_p(at91_can_remove), 123699c4a634SDavid S. Miller .driver = { 123700389b08SMarc Kleine-Budde .name = KBUILD_MODNAME, 123899c4a634SDavid S. Miller .owner = THIS_MODULE, 123999c4a634SDavid S. Miller }, 124099c4a634SDavid S. Miller }; 124199c4a634SDavid S. Miller 124299c4a634SDavid S. Miller static int __init at91_can_module_init(void) 124399c4a634SDavid S. Miller { 124499c4a634SDavid S. Miller return platform_driver_register(&at91_can_driver); 124599c4a634SDavid S. Miller } 124699c4a634SDavid S. Miller 124799c4a634SDavid S. Miller static void __exit at91_can_module_exit(void) 124899c4a634SDavid S. Miller { 124999c4a634SDavid S. Miller platform_driver_unregister(&at91_can_driver); 125099c4a634SDavid S. Miller } 125199c4a634SDavid S. Miller 125299c4a634SDavid S. Miller module_init(at91_can_module_init); 125399c4a634SDavid S. Miller module_exit(at91_can_module_exit); 125499c4a634SDavid S. Miller 125599c4a634SDavid S. Miller MODULE_AUTHOR("Marc Kleine-Budde <mkl@pengutronix.de>"); 125699c4a634SDavid S. Miller MODULE_LICENSE("GPL v2"); 125700389b08SMarc Kleine-Budde MODULE_DESCRIPTION(KBUILD_MODNAME " CAN netdevice driver"); 1258