at91_can.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. /*
  2. * at91_can.c - CAN network driver for AT91 SoC CAN controller
  3. *
  4. * (C) 2007 by Hans J. Koch <hjk@linutronix.de>
  5. * (C) 2008, 2009 by Marc Kleine-Budde <kernel@pengutronix.de>
  6. *
  7. * This software may be distributed under the terms of the GNU General
  8. * Public License ("GPL") version 2 as distributed in the 'COPYING'
  9. * file from the main directory of the linux kernel source.
  10. *
  11. * Send feedback to <socketcan-users@lists.berlios.de>
  12. *
  13. *
  14. * Your platform definition file should specify something like:
  15. *
  16. * static struct at91_can_data ek_can_data = {
  17. * transceiver_switch = sam9263ek_transceiver_switch,
  18. * };
  19. *
  20. * at91_add_device_can(&ek_can_data);
  21. *
  22. */
  23. #include <linux/clk.h>
  24. #include <linux/errno.h>
  25. #include <linux/if_arp.h>
  26. #include <linux/init.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/string.h>
  35. #include <linux/types.h>
  36. #include <linux/can.h>
  37. #include <linux/can/dev.h>
  38. #include <linux/can/error.h>
  39. #include <mach/board.h>
  40. #define DRV_NAME "at91_can"
  41. #define AT91_NAPI_WEIGHT 12
  42. /*
  43. * RX/TX Mailbox split
  44. * don't dare to touch
  45. */
  46. #define AT91_MB_RX_NUM 12
  47. #define AT91_MB_TX_SHIFT 2
  48. #define AT91_MB_RX_FIRST 0
  49. #define AT91_MB_RX_LAST (AT91_MB_RX_FIRST + AT91_MB_RX_NUM - 1)
  50. #define AT91_MB_RX_MASK(i) ((1 << (i)) - 1)
  51. #define AT91_MB_RX_SPLIT 8
  52. #define AT91_MB_RX_LOW_LAST (AT91_MB_RX_SPLIT - 1)
  53. #define AT91_MB_RX_LOW_MASK (AT91_MB_RX_MASK(AT91_MB_RX_SPLIT))
  54. #define AT91_MB_TX_NUM (1 << AT91_MB_TX_SHIFT)
  55. #define AT91_MB_TX_FIRST (AT91_MB_RX_LAST + 1)
  56. #define AT91_MB_TX_LAST (AT91_MB_TX_FIRST + AT91_MB_TX_NUM - 1)
  57. #define AT91_NEXT_PRIO_SHIFT (AT91_MB_TX_SHIFT)
  58. #define AT91_NEXT_PRIO_MASK (0xf << AT91_MB_TX_SHIFT)
  59. #define AT91_NEXT_MB_MASK (AT91_MB_TX_NUM - 1)
  60. #define AT91_NEXT_MASK ((AT91_MB_TX_NUM - 1) | AT91_NEXT_PRIO_MASK)
  61. /* Common registers */
  62. enum at91_reg {
  63. AT91_MR = 0x000,
  64. AT91_IER = 0x004,
  65. AT91_IDR = 0x008,
  66. AT91_IMR = 0x00C,
  67. AT91_SR = 0x010,
  68. AT91_BR = 0x014,
  69. AT91_TIM = 0x018,
  70. AT91_TIMESTP = 0x01C,
  71. AT91_ECR = 0x020,
  72. AT91_TCR = 0x024,
  73. AT91_ACR = 0x028,
  74. };
  75. /* Mailbox registers (0 <= i <= 15) */
  76. #define AT91_MMR(i) (enum at91_reg)(0x200 + ((i) * 0x20))
  77. #define AT91_MAM(i) (enum at91_reg)(0x204 + ((i) * 0x20))
  78. #define AT91_MID(i) (enum at91_reg)(0x208 + ((i) * 0x20))
  79. #define AT91_MFID(i) (enum at91_reg)(0x20C + ((i) * 0x20))
  80. #define AT91_MSR(i) (enum at91_reg)(0x210 + ((i) * 0x20))
  81. #define AT91_MDL(i) (enum at91_reg)(0x214 + ((i) * 0x20))
  82. #define AT91_MDH(i) (enum at91_reg)(0x218 + ((i) * 0x20))
  83. #define AT91_MCR(i) (enum at91_reg)(0x21C + ((i) * 0x20))
  84. /* Register bits */
  85. #define AT91_MR_CANEN BIT(0)
  86. #define AT91_MR_LPM BIT(1)
  87. #define AT91_MR_ABM BIT(2)
  88. #define AT91_MR_OVL BIT(3)
  89. #define AT91_MR_TEOF BIT(4)
  90. #define AT91_MR_TTM BIT(5)
  91. #define AT91_MR_TIMFRZ BIT(6)
  92. #define AT91_MR_DRPT BIT(7)
  93. #define AT91_SR_RBSY BIT(29)
  94. #define AT91_MMR_PRIO_SHIFT (16)
  95. #define AT91_MID_MIDE BIT(29)
  96. #define AT91_MSR_MRTR BIT(20)
  97. #define AT91_MSR_MABT BIT(22)
  98. #define AT91_MSR_MRDY BIT(23)
  99. #define AT91_MSR_MMI BIT(24)
  100. #define AT91_MCR_MRTR BIT(20)
  101. #define AT91_MCR_MTCR BIT(23)
  102. /* Mailbox Modes */
  103. enum at91_mb_mode {
  104. AT91_MB_MODE_DISABLED = 0,
  105. AT91_MB_MODE_RX = 1,
  106. AT91_MB_MODE_RX_OVRWR = 2,
  107. AT91_MB_MODE_TX = 3,
  108. AT91_MB_MODE_CONSUMER = 4,
  109. AT91_MB_MODE_PRODUCER = 5,
  110. };
  111. /* Interrupt mask bits */
  112. #define AT91_IRQ_MB_RX ((1 << (AT91_MB_RX_LAST + 1)) \
  113. - (1 << AT91_MB_RX_FIRST))
  114. #define AT91_IRQ_MB_TX ((1 << (AT91_MB_TX_LAST + 1)) \
  115. - (1 << AT91_MB_TX_FIRST))
  116. #define AT91_IRQ_MB_ALL (AT91_IRQ_MB_RX | AT91_IRQ_MB_TX)
  117. #define AT91_IRQ_ERRA (1 << 16)
  118. #define AT91_IRQ_WARN (1 << 17)
  119. #define AT91_IRQ_ERRP (1 << 18)
  120. #define AT91_IRQ_BOFF (1 << 19)
  121. #define AT91_IRQ_SLEEP (1 << 20)
  122. #define AT91_IRQ_WAKEUP (1 << 21)
  123. #define AT91_IRQ_TOVF (1 << 22)
  124. #define AT91_IRQ_TSTP (1 << 23)
  125. #define AT91_IRQ_CERR (1 << 24)
  126. #define AT91_IRQ_SERR (1 << 25)
  127. #define AT91_IRQ_AERR (1 << 26)
  128. #define AT91_IRQ_FERR (1 << 27)
  129. #define AT91_IRQ_BERR (1 << 28)
  130. #define AT91_IRQ_ERR_ALL (0x1fff0000)
  131. #define AT91_IRQ_ERR_FRAME (AT91_IRQ_CERR | AT91_IRQ_SERR | \
  132. AT91_IRQ_AERR | AT91_IRQ_FERR | AT91_IRQ_BERR)
  133. #define AT91_IRQ_ERR_LINE (AT91_IRQ_ERRA | AT91_IRQ_WARN | \
  134. AT91_IRQ_ERRP | AT91_IRQ_BOFF)
  135. #define AT91_IRQ_ALL (0x1fffffff)
  136. struct at91_priv {
  137. struct can_priv can; /* must be the first member! */
  138. struct net_device *dev;
  139. struct napi_struct napi;
  140. void __iomem *reg_base;
  141. u32 reg_sr;
  142. unsigned int tx_next;
  143. unsigned int tx_echo;
  144. unsigned int rx_next;
  145. struct clk *clk;
  146. struct at91_can_data *pdata;
  147. };
  148. static struct can_bittiming_const at91_bittiming_const = {
  149. .tseg1_min = 4,
  150. .tseg1_max = 16,
  151. .tseg2_min = 2,
  152. .tseg2_max = 8,
  153. .sjw_max = 4,
  154. .brp_min = 2,
  155. .brp_max = 128,
  156. .brp_inc = 1,
  157. };
  158. static inline int get_tx_next_mb(const struct at91_priv *priv)
  159. {
  160. return (priv->tx_next & AT91_NEXT_MB_MASK) + AT91_MB_TX_FIRST;
  161. }
  162. static inline int get_tx_next_prio(const struct at91_priv *priv)
  163. {
  164. return (priv->tx_next >> AT91_NEXT_PRIO_SHIFT) & 0xf;
  165. }
  166. static inline int get_tx_echo_mb(const struct at91_priv *priv)
  167. {
  168. return (priv->tx_echo & AT91_NEXT_MB_MASK) + AT91_MB_TX_FIRST;
  169. }
  170. static inline u32 at91_read(const struct at91_priv *priv, enum at91_reg reg)
  171. {
  172. return readl(priv->reg_base + reg);
  173. }
  174. static inline void at91_write(const struct at91_priv *priv, enum at91_reg reg,
  175. u32 value)
  176. {
  177. writel(value, priv->reg_base + reg);
  178. }
  179. static inline void set_mb_mode_prio(const struct at91_priv *priv,
  180. unsigned int mb, enum at91_mb_mode mode, int prio)
  181. {
  182. at91_write(priv, AT91_MMR(mb), (mode << 24) | (prio << 16));
  183. }
  184. static inline void set_mb_mode(const struct at91_priv *priv, unsigned int mb,
  185. enum at91_mb_mode mode)
  186. {
  187. set_mb_mode_prio(priv, mb, mode, 0);
  188. }
  189. static struct sk_buff *alloc_can_skb(struct net_device *dev,
  190. struct can_frame **cf)
  191. {
  192. struct sk_buff *skb;
  193. skb = netdev_alloc_skb(dev, sizeof(struct can_frame));
  194. if (unlikely(!skb))
  195. return NULL;
  196. skb->protocol = htons(ETH_P_CAN);
  197. skb->ip_summed = CHECKSUM_UNNECESSARY;
  198. *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
  199. return skb;
  200. }
  201. static struct sk_buff *alloc_can_err_skb(struct net_device *dev,
  202. struct can_frame **cf)
  203. {
  204. struct sk_buff *skb;
  205. skb = alloc_can_skb(dev, cf);
  206. if (unlikely(!skb))
  207. return NULL;
  208. memset(*cf, 0, sizeof(struct can_frame));
  209. (*cf)->can_id = CAN_ERR_FLAG;
  210. (*cf)->can_dlc = CAN_ERR_DLC;
  211. return skb;
  212. }
  213. /*
  214. * Swtich transceiver on or off
  215. */
  216. static void at91_transceiver_switch(const struct at91_priv *priv, int on)
  217. {
  218. if (priv->pdata && priv->pdata->transceiver_switch)
  219. priv->pdata->transceiver_switch(on);
  220. }
  221. static void at91_setup_mailboxes(struct net_device *dev)
  222. {
  223. struct at91_priv *priv = netdev_priv(dev);
  224. unsigned int i;
  225. /*
  226. * The first 12 mailboxes are used as a reception FIFO. The
  227. * last mailbox is configured with overwrite option. The
  228. * overwrite flag indicates a FIFO overflow.
  229. */
  230. for (i = AT91_MB_RX_FIRST; i < AT91_MB_RX_LAST; i++)
  231. set_mb_mode(priv, i, AT91_MB_MODE_RX);
  232. set_mb_mode(priv, AT91_MB_RX_LAST, AT91_MB_MODE_RX_OVRWR);
  233. /* The last 4 mailboxes are used for transmitting. */
  234. for (i = AT91_MB_TX_FIRST; i <= AT91_MB_TX_LAST; i++)
  235. set_mb_mode_prio(priv, i, AT91_MB_MODE_TX, 0);
  236. /* Reset tx and rx helper pointers */
  237. priv->tx_next = priv->tx_echo = priv->rx_next = 0;
  238. }
  239. static int at91_set_bittiming(struct net_device *dev)
  240. {
  241. const struct at91_priv *priv = netdev_priv(dev);
  242. const struct can_bittiming *bt = &priv->can.bittiming;
  243. u32 reg_br;
  244. reg_br = ((priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) << 24) |
  245. ((bt->brp - 1) << 16) | ((bt->sjw - 1) << 12) |
  246. ((bt->prop_seg - 1) << 8) | ((bt->phase_seg1 - 1) << 4) |
  247. ((bt->phase_seg2 - 1) << 0);
  248. dev_info(dev->dev.parent, "writing AT91_BR: 0x%08x\n", reg_br);
  249. at91_write(priv, AT91_BR, reg_br);
  250. return 0;
  251. }
  252. static void at91_chip_start(struct net_device *dev)
  253. {
  254. struct at91_priv *priv = netdev_priv(dev);
  255. u32 reg_mr, reg_ier;
  256. /* disable interrupts */
  257. at91_write(priv, AT91_IDR, AT91_IRQ_ALL);
  258. /* disable chip */
  259. reg_mr = at91_read(priv, AT91_MR);
  260. at91_write(priv, AT91_MR, reg_mr & ~AT91_MR_CANEN);
  261. at91_setup_mailboxes(dev);
  262. at91_transceiver_switch(priv, 1);
  263. /* enable chip */
  264. at91_write(priv, AT91_MR, AT91_MR_CANEN);
  265. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  266. /* Enable interrupts */
  267. reg_ier = AT91_IRQ_MB_RX | AT91_IRQ_ERRP | AT91_IRQ_ERR_FRAME;
  268. at91_write(priv, AT91_IDR, AT91_IRQ_ALL);
  269. at91_write(priv, AT91_IER, reg_ier);
  270. }
  271. static void at91_chip_stop(struct net_device *dev, enum can_state state)
  272. {
  273. struct at91_priv *priv = netdev_priv(dev);
  274. u32 reg_mr;
  275. /* disable interrupts */
  276. at91_write(priv, AT91_IDR, AT91_IRQ_ALL);
  277. reg_mr = at91_read(priv, AT91_MR);
  278. at91_write(priv, AT91_MR, reg_mr & ~AT91_MR_CANEN);
  279. at91_transceiver_switch(priv, 0);
  280. priv->can.state = state;
  281. }
  282. /*
  283. * theory of operation:
  284. *
  285. * According to the datasheet priority 0 is the highest priority, 15
  286. * is the lowest. If two mailboxes have the same priority level the
  287. * message of the mailbox with the lowest number is sent first.
  288. *
  289. * We use the first TX mailbox (AT91_MB_TX_FIRST) with prio 0, then
  290. * the next mailbox with prio 0, and so on, until all mailboxes are
  291. * used. Then we start from the beginning with mailbox
  292. * AT91_MB_TX_FIRST, but with prio 1, mailbox AT91_MB_TX_FIRST + 1
  293. * prio 1. When we reach the last mailbox with prio 15, we have to
  294. * stop sending, waiting for all messages to be delivered, then start
  295. * again with mailbox AT91_MB_TX_FIRST prio 0.
  296. *
  297. * We use the priv->tx_next as counter for the next transmission
  298. * mailbox, but without the offset AT91_MB_TX_FIRST. The lower bits
  299. * encode the mailbox number, the upper 4 bits the mailbox priority:
  300. *
  301. * priv->tx_next = (prio << AT91_NEXT_PRIO_SHIFT) ||
  302. * (mb - AT91_MB_TX_FIRST);
  303. *
  304. */
  305. static netdev_tx_t at91_start_xmit(struct sk_buff *skb, struct net_device *dev)
  306. {
  307. struct at91_priv *priv = netdev_priv(dev);
  308. struct net_device_stats *stats = &dev->stats;
  309. struct can_frame *cf = (struct can_frame *)skb->data;
  310. unsigned int mb, prio;
  311. u32 reg_mid, reg_mcr;
  312. mb = get_tx_next_mb(priv);
  313. prio = get_tx_next_prio(priv);
  314. if (unlikely(!(at91_read(priv, AT91_MSR(mb)) & AT91_MSR_MRDY))) {
  315. netif_stop_queue(dev);
  316. dev_err(dev->dev.parent,
  317. "BUG! TX buffer full when queue awake!\n");
  318. return NETDEV_TX_BUSY;
  319. }
  320. if (cf->can_id & CAN_EFF_FLAG)
  321. reg_mid = (cf->can_id & CAN_EFF_MASK) | AT91_MID_MIDE;
  322. else
  323. reg_mid = (cf->can_id & CAN_SFF_MASK) << 18;
  324. reg_mcr = ((cf->can_id & CAN_RTR_FLAG) ? AT91_MCR_MRTR : 0) |
  325. (cf->can_dlc << 16) | AT91_MCR_MTCR;
  326. /* disable MB while writing ID (see datasheet) */
  327. set_mb_mode(priv, mb, AT91_MB_MODE_DISABLED);
  328. at91_write(priv, AT91_MID(mb), reg_mid);
  329. set_mb_mode_prio(priv, mb, AT91_MB_MODE_TX, prio);
  330. at91_write(priv, AT91_MDL(mb), *(u32 *)(cf->data + 0));
  331. at91_write(priv, AT91_MDH(mb), *(u32 *)(cf->data + 4));
  332. /* This triggers transmission */
  333. at91_write(priv, AT91_MCR(mb), reg_mcr);
  334. stats->tx_bytes += cf->can_dlc;
  335. dev->trans_start = jiffies;
  336. /* _NOTE_: substract AT91_MB_TX_FIRST offset from mb! */
  337. can_put_echo_skb(skb, dev, mb - AT91_MB_TX_FIRST);
  338. /*
  339. * we have to stop the queue and deliver all messages in case
  340. * of a prio+mb counter wrap around. This is the case if
  341. * tx_next buffer prio and mailbox equals 0.
  342. *
  343. * also stop the queue if next buffer is still in use
  344. * (== not ready)
  345. */
  346. priv->tx_next++;
  347. if (!(at91_read(priv, AT91_MSR(get_tx_next_mb(priv))) &
  348. AT91_MSR_MRDY) ||
  349. (priv->tx_next & AT91_NEXT_MASK) == 0)
  350. netif_stop_queue(dev);
  351. /* Enable interrupt for this mailbox */
  352. at91_write(priv, AT91_IER, 1 << mb);
  353. return NETDEV_TX_OK;
  354. }
  355. /**
  356. * at91_activate_rx_low - activate lower rx mailboxes
  357. * @priv: a91 context
  358. *
  359. * Reenables the lower mailboxes for reception of new CAN messages
  360. */
  361. static inline void at91_activate_rx_low(const struct at91_priv *priv)
  362. {
  363. u32 mask = AT91_MB_RX_LOW_MASK;
  364. at91_write(priv, AT91_TCR, mask);
  365. }
  366. /**
  367. * at91_activate_rx_mb - reactive single rx mailbox
  368. * @priv: a91 context
  369. * @mb: mailbox to reactivate
  370. *
  371. * Reenables given mailbox for reception of new CAN messages
  372. */
  373. static inline void at91_activate_rx_mb(const struct at91_priv *priv,
  374. unsigned int mb)
  375. {
  376. u32 mask = 1 << mb;
  377. at91_write(priv, AT91_TCR, mask);
  378. }
  379. /**
  380. * at91_rx_overflow_err - send error frame due to rx overflow
  381. * @dev: net device
  382. */
  383. static void at91_rx_overflow_err(struct net_device *dev)
  384. {
  385. struct net_device_stats *stats = &dev->stats;
  386. struct sk_buff *skb;
  387. struct can_frame *cf;
  388. dev_dbg(dev->dev.parent, "RX buffer overflow\n");
  389. stats->rx_over_errors++;
  390. stats->rx_errors++;
  391. skb = alloc_can_err_skb(dev, &cf);
  392. if (unlikely(!skb))
  393. return;
  394. cf->can_id |= CAN_ERR_CRTL;
  395. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  396. netif_receive_skb(skb);
  397. stats->rx_packets++;
  398. stats->rx_bytes += cf->can_dlc;
  399. }
  400. /**
  401. * at91_read_mb - read CAN msg from mailbox (lowlevel impl)
  402. * @dev: net device
  403. * @mb: mailbox number to read from
  404. * @cf: can frame where to store message
  405. *
  406. * Reads a CAN message from the given mailbox and stores data into
  407. * given can frame. "mb" and "cf" must be valid.
  408. */
  409. static void at91_read_mb(struct net_device *dev, unsigned int mb,
  410. struct can_frame *cf)
  411. {
  412. const struct at91_priv *priv = netdev_priv(dev);
  413. u32 reg_msr, reg_mid;
  414. reg_mid = at91_read(priv, AT91_MID(mb));
  415. if (reg_mid & AT91_MID_MIDE)
  416. cf->can_id = ((reg_mid >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG;
  417. else
  418. cf->can_id = (reg_mid >> 18) & CAN_SFF_MASK;
  419. reg_msr = at91_read(priv, AT91_MSR(mb));
  420. if (reg_msr & AT91_MSR_MRTR)
  421. cf->can_id |= CAN_RTR_FLAG;
  422. cf->can_dlc = min_t(__u8, (reg_msr >> 16) & 0xf, 8);
  423. *(u32 *)(cf->data + 0) = at91_read(priv, AT91_MDL(mb));
  424. *(u32 *)(cf->data + 4) = at91_read(priv, AT91_MDH(mb));
  425. if (unlikely(mb == AT91_MB_RX_LAST && reg_msr & AT91_MSR_MMI))
  426. at91_rx_overflow_err(dev);
  427. }
  428. /**
  429. * at91_read_msg - read CAN message from mailbox
  430. * @dev: net device
  431. * @mb: mail box to read from
  432. *
  433. * Reads a CAN message from given mailbox, and put into linux network
  434. * RX queue, does all housekeeping chores (stats, ...)
  435. */
  436. static void at91_read_msg(struct net_device *dev, unsigned int mb)
  437. {
  438. struct net_device_stats *stats = &dev->stats;
  439. struct can_frame *cf;
  440. struct sk_buff *skb;
  441. skb = alloc_can_skb(dev, &cf);
  442. if (unlikely(!skb)) {
  443. stats->rx_dropped++;
  444. return;
  445. }
  446. at91_read_mb(dev, mb, cf);
  447. netif_receive_skb(skb);
  448. stats->rx_packets++;
  449. stats->rx_bytes += cf->can_dlc;
  450. }
  451. /**
  452. * at91_poll_rx - read multiple CAN messages from mailboxes
  453. * @dev: net device
  454. * @quota: max number of pkgs we're allowed to receive
  455. *
  456. * Theory of Operation:
  457. *
  458. * 12 of the 16 mailboxes on the chip are reserved for RX. we split
  459. * them into 2 groups. The lower group holds 8 and upper 4 mailboxes.
  460. *
  461. * Like it or not, but the chip always saves a received CAN message
  462. * into the first free mailbox it finds (starting with the
  463. * lowest). This makes it very difficult to read the messages in the
  464. * right order from the chip. This is how we work around that problem:
  465. *
  466. * The first message goes into mb nr. 0 and issues an interrupt. All
  467. * rx ints are disabled in the interrupt handler and a napi poll is
  468. * scheduled. We read the mailbox, but do _not_ reenable the mb (to
  469. * receive another message).
  470. *
  471. * lower mbxs upper
  472. * ______^______ __^__
  473. * / \ / \
  474. * +-+-+-+-+-+-+-+-++-+-+-+-+
  475. * |x|x|x|x|x|x|x|x|| | | | |
  476. * +-+-+-+-+-+-+-+-++-+-+-+-+
  477. * 0 0 0 0 0 0 0 0 0 0 1 1 \ mail
  478. * 0 1 2 3 4 5 6 7 8 9 0 1 / box
  479. *
  480. * The variable priv->rx_next points to the next mailbox to read a
  481. * message from. As long we're in the lower mailboxes we just read the
  482. * mailbox but not reenable it.
  483. *
  484. * With completion of the last of the lower mailboxes, we reenable the
  485. * whole first group, but continue to look for filled mailboxes in the
  486. * upper mailboxes. Imagine the second group like overflow mailboxes,
  487. * which takes CAN messages if the lower goup is full. While in the
  488. * upper group we reenable the mailbox right after reading it. Giving
  489. * the chip more room to store messages.
  490. *
  491. * After finishing we look again in the lower group if we've still
  492. * quota.
  493. *
  494. */
  495. static int at91_poll_rx(struct net_device *dev, int quota)
  496. {
  497. struct at91_priv *priv = netdev_priv(dev);
  498. u32 reg_sr = at91_read(priv, AT91_SR);
  499. const unsigned long *addr = (unsigned long *)&reg_sr;
  500. unsigned int mb;
  501. int received = 0;
  502. if (priv->rx_next > AT91_MB_RX_LOW_LAST &&
  503. reg_sr & AT91_MB_RX_LOW_MASK)
  504. dev_info(dev->dev.parent,
  505. "order of incoming frames cannot be guaranteed\n");
  506. again:
  507. for (mb = find_next_bit(addr, AT91_MB_RX_NUM, priv->rx_next);
  508. mb < AT91_MB_RX_NUM && quota > 0;
  509. reg_sr = at91_read(priv, AT91_SR),
  510. mb = find_next_bit(addr, AT91_MB_RX_NUM, ++priv->rx_next)) {
  511. at91_read_msg(dev, mb);
  512. /* reactivate mailboxes */
  513. if (mb == AT91_MB_RX_LOW_LAST)
  514. /* all lower mailboxed, if just finished it */
  515. at91_activate_rx_low(priv);
  516. else if (mb > AT91_MB_RX_LOW_LAST)
  517. /* only the mailbox we read */
  518. at91_activate_rx_mb(priv, mb);
  519. received++;
  520. quota--;
  521. }
  522. /* upper group completed, look again in lower */
  523. if (priv->rx_next > AT91_MB_RX_LOW_LAST &&
  524. quota > 0 && mb >= AT91_MB_RX_NUM) {
  525. priv->rx_next = 0;
  526. goto again;
  527. }
  528. return received;
  529. }
  530. static void at91_poll_err_frame(struct net_device *dev,
  531. struct can_frame *cf, u32 reg_sr)
  532. {
  533. struct at91_priv *priv = netdev_priv(dev);
  534. /* CRC error */
  535. if (reg_sr & AT91_IRQ_CERR) {
  536. dev_dbg(dev->dev.parent, "CERR irq\n");
  537. dev->stats.rx_errors++;
  538. priv->can.can_stats.bus_error++;
  539. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  540. }
  541. /* Stuffing Error */
  542. if (reg_sr & AT91_IRQ_SERR) {
  543. dev_dbg(dev->dev.parent, "SERR irq\n");
  544. dev->stats.rx_errors++;
  545. priv->can.can_stats.bus_error++;
  546. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  547. cf->data[2] |= CAN_ERR_PROT_STUFF;
  548. }
  549. /* Acknowledgement Error */
  550. if (reg_sr & AT91_IRQ_AERR) {
  551. dev_dbg(dev->dev.parent, "AERR irq\n");
  552. dev->stats.tx_errors++;
  553. cf->can_id |= CAN_ERR_ACK;
  554. }
  555. /* Form error */
  556. if (reg_sr & AT91_IRQ_FERR) {
  557. dev_dbg(dev->dev.parent, "FERR irq\n");
  558. dev->stats.rx_errors++;
  559. priv->can.can_stats.bus_error++;
  560. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  561. cf->data[2] |= CAN_ERR_PROT_FORM;
  562. }
  563. /* Bit Error */
  564. if (reg_sr & AT91_IRQ_BERR) {
  565. dev_dbg(dev->dev.parent, "BERR irq\n");
  566. dev->stats.tx_errors++;
  567. priv->can.can_stats.bus_error++;
  568. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  569. cf->data[2] |= CAN_ERR_PROT_BIT;
  570. }
  571. }
  572. static int at91_poll_err(struct net_device *dev, int quota, u32 reg_sr)
  573. {
  574. struct sk_buff *skb;
  575. struct can_frame *cf;
  576. if (quota == 0)
  577. return 0;
  578. skb = alloc_can_err_skb(dev, &cf);
  579. if (unlikely(!skb))
  580. return 0;
  581. at91_poll_err_frame(dev, cf, reg_sr);
  582. netif_receive_skb(skb);
  583. dev->last_rx = jiffies;
  584. dev->stats.rx_packets++;
  585. dev->stats.rx_bytes += cf->can_dlc;
  586. return 1;
  587. }
  588. static int at91_poll(struct napi_struct *napi, int quota)
  589. {
  590. struct net_device *dev = napi->dev;
  591. const struct at91_priv *priv = netdev_priv(dev);
  592. u32 reg_sr = at91_read(priv, AT91_SR);
  593. int work_done = 0;
  594. if (reg_sr & AT91_IRQ_MB_RX)
  595. work_done += at91_poll_rx(dev, quota - work_done);
  596. /*
  597. * The error bits are clear on read,
  598. * so use saved value from irq handler.
  599. */
  600. reg_sr |= priv->reg_sr;
  601. if (reg_sr & AT91_IRQ_ERR_FRAME)
  602. work_done += at91_poll_err(dev, quota - work_done, reg_sr);
  603. if (work_done < quota) {
  604. /* enable IRQs for frame errors and all mailboxes >= rx_next */
  605. u32 reg_ier = AT91_IRQ_ERR_FRAME;
  606. reg_ier |= AT91_IRQ_MB_RX & ~AT91_MB_RX_MASK(priv->rx_next);
  607. napi_complete(napi);
  608. at91_write(priv, AT91_IER, reg_ier);
  609. }
  610. return work_done;
  611. }
  612. /*
  613. * theory of operation:
  614. *
  615. * priv->tx_echo holds the number of the oldest can_frame put for
  616. * transmission into the hardware, but not yet ACKed by the CAN tx
  617. * complete IRQ.
  618. *
  619. * We iterate from priv->tx_echo to priv->tx_next and check if the
  620. * packet has been transmitted, echo it back to the CAN framework. If
  621. * we discover a not yet transmitted package, stop looking for more.
  622. *
  623. */
  624. static void at91_irq_tx(struct net_device *dev, u32 reg_sr)
  625. {
  626. struct at91_priv *priv = netdev_priv(dev);
  627. u32 reg_msr;
  628. unsigned int mb;
  629. /* masking of reg_sr not needed, already done by at91_irq */
  630. for (/* nix */; (priv->tx_next - priv->tx_echo) > 0; priv->tx_echo++) {
  631. mb = get_tx_echo_mb(priv);
  632. /* no event in mailbox? */
  633. if (!(reg_sr & (1 << mb)))
  634. break;
  635. /* Disable irq for this TX mailbox */
  636. at91_write(priv, AT91_IDR, 1 << mb);
  637. /*
  638. * only echo if mailbox signals us a transfer
  639. * complete (MSR_MRDY). Otherwise it's a tansfer
  640. * abort. "can_bus_off()" takes care about the skbs
  641. * parked in the echo queue.
  642. */
  643. reg_msr = at91_read(priv, AT91_MSR(mb));
  644. if (likely(reg_msr & AT91_MSR_MRDY &&
  645. ~reg_msr & AT91_MSR_MABT)) {
  646. /* _NOTE_: substract AT91_MB_TX_FIRST offset from mb! */
  647. can_get_echo_skb(dev, mb - AT91_MB_TX_FIRST);
  648. dev->stats.tx_packets++;
  649. }
  650. }
  651. /*
  652. * restart queue if we don't have a wrap around but restart if
  653. * we get a TX int for the last can frame directly before a
  654. * wrap around.
  655. */
  656. if ((priv->tx_next & AT91_NEXT_MASK) != 0 ||
  657. (priv->tx_echo & AT91_NEXT_MASK) == 0)
  658. netif_wake_queue(dev);
  659. }
  660. static void at91_irq_err_state(struct net_device *dev,
  661. struct can_frame *cf, enum can_state new_state)
  662. {
  663. struct at91_priv *priv = netdev_priv(dev);
  664. u32 reg_idr, reg_ier, reg_ecr;
  665. u8 tec, rec;
  666. reg_ecr = at91_read(priv, AT91_ECR);
  667. rec = reg_ecr & 0xff;
  668. tec = reg_ecr >> 16;
  669. switch (priv->can.state) {
  670. case CAN_STATE_ERROR_ACTIVE:
  671. /*
  672. * from: ERROR_ACTIVE
  673. * to : ERROR_WARNING, ERROR_PASSIVE, BUS_OFF
  674. * => : there was a warning int
  675. */
  676. if (new_state >= CAN_STATE_ERROR_WARNING &&
  677. new_state <= CAN_STATE_BUS_OFF) {
  678. dev_dbg(dev->dev.parent, "Error Warning IRQ\n");
  679. priv->can.can_stats.error_warning++;
  680. cf->can_id |= CAN_ERR_CRTL;
  681. cf->data[1] = (tec > rec) ?
  682. CAN_ERR_CRTL_TX_WARNING :
  683. CAN_ERR_CRTL_RX_WARNING;
  684. }
  685. case CAN_STATE_ERROR_WARNING: /* fallthrough */
  686. /*
  687. * from: ERROR_ACTIVE, ERROR_WARNING
  688. * to : ERROR_PASSIVE, BUS_OFF
  689. * => : error passive int
  690. */
  691. if (new_state >= CAN_STATE_ERROR_PASSIVE &&
  692. new_state <= CAN_STATE_BUS_OFF) {
  693. dev_dbg(dev->dev.parent, "Error Passive IRQ\n");
  694. priv->can.can_stats.error_passive++;
  695. cf->can_id |= CAN_ERR_CRTL;
  696. cf->data[1] = (tec > rec) ?
  697. CAN_ERR_CRTL_TX_PASSIVE :
  698. CAN_ERR_CRTL_RX_PASSIVE;
  699. }
  700. break;
  701. case CAN_STATE_BUS_OFF:
  702. /*
  703. * from: BUS_OFF
  704. * to : ERROR_ACTIVE, ERROR_WARNING, ERROR_PASSIVE
  705. */
  706. if (new_state <= CAN_STATE_ERROR_PASSIVE) {
  707. cf->can_id |= CAN_ERR_RESTARTED;
  708. dev_dbg(dev->dev.parent, "restarted\n");
  709. priv->can.can_stats.restarts++;
  710. netif_carrier_on(dev);
  711. netif_wake_queue(dev);
  712. }
  713. break;
  714. default:
  715. break;
  716. }
  717. /* process state changes depending on the new state */
  718. switch (new_state) {
  719. case CAN_STATE_ERROR_ACTIVE:
  720. /*
  721. * actually we want to enable AT91_IRQ_WARN here, but
  722. * it screws up the system under certain
  723. * circumstances. so just enable AT91_IRQ_ERRP, thus
  724. * the "fallthrough"
  725. */
  726. dev_dbg(dev->dev.parent, "Error Active\n");
  727. cf->can_id |= CAN_ERR_PROT;
  728. cf->data[2] = CAN_ERR_PROT_ACTIVE;
  729. case CAN_STATE_ERROR_WARNING: /* fallthrough */
  730. reg_idr = AT91_IRQ_ERRA | AT91_IRQ_WARN | AT91_IRQ_BOFF;
  731. reg_ier = AT91_IRQ_ERRP;
  732. break;
  733. case CAN_STATE_ERROR_PASSIVE:
  734. reg_idr = AT91_IRQ_ERRA | AT91_IRQ_WARN | AT91_IRQ_ERRP;
  735. reg_ier = AT91_IRQ_BOFF;
  736. break;
  737. case CAN_STATE_BUS_OFF:
  738. reg_idr = AT91_IRQ_ERRA | AT91_IRQ_ERRP |
  739. AT91_IRQ_WARN | AT91_IRQ_BOFF;
  740. reg_ier = 0;
  741. cf->can_id |= CAN_ERR_BUSOFF;
  742. dev_dbg(dev->dev.parent, "bus-off\n");
  743. netif_carrier_off(dev);
  744. priv->can.can_stats.bus_off++;
  745. /* turn off chip, if restart is disabled */
  746. if (!priv->can.restart_ms) {
  747. at91_chip_stop(dev, CAN_STATE_BUS_OFF);
  748. return;
  749. }
  750. break;
  751. default:
  752. break;
  753. }
  754. at91_write(priv, AT91_IDR, reg_idr);
  755. at91_write(priv, AT91_IER, reg_ier);
  756. }
  757. static void at91_irq_err(struct net_device *dev)
  758. {
  759. struct at91_priv *priv = netdev_priv(dev);
  760. struct sk_buff *skb;
  761. struct can_frame *cf;
  762. enum can_state new_state;
  763. u32 reg_sr;
  764. reg_sr = at91_read(priv, AT91_SR);
  765. /* we need to look at the unmasked reg_sr */
  766. if (unlikely(reg_sr & AT91_IRQ_BOFF))
  767. new_state = CAN_STATE_BUS_OFF;
  768. else if (unlikely(reg_sr & AT91_IRQ_ERRP))
  769. new_state = CAN_STATE_ERROR_PASSIVE;
  770. else if (unlikely(reg_sr & AT91_IRQ_WARN))
  771. new_state = CAN_STATE_ERROR_WARNING;
  772. else if (likely(reg_sr & AT91_IRQ_ERRA))
  773. new_state = CAN_STATE_ERROR_ACTIVE;
  774. else {
  775. dev_err(dev->dev.parent, "BUG! hardware in undefined state\n");
  776. return;
  777. }
  778. /* state hasn't changed */
  779. if (likely(new_state == priv->can.state))
  780. return;
  781. skb = alloc_can_err_skb(dev, &cf);
  782. if (unlikely(!skb))
  783. return;
  784. at91_irq_err_state(dev, cf, new_state);
  785. netif_rx(skb);
  786. dev->last_rx = jiffies;
  787. dev->stats.rx_packets++;
  788. dev->stats.rx_bytes += cf->can_dlc;
  789. priv->can.state = new_state;
  790. }
  791. /*
  792. * interrupt handler
  793. */
  794. static irqreturn_t at91_irq(int irq, void *dev_id)
  795. {
  796. struct net_device *dev = dev_id;
  797. struct at91_priv *priv = netdev_priv(dev);
  798. irqreturn_t handled = IRQ_NONE;
  799. u32 reg_sr, reg_imr;
  800. reg_sr = at91_read(priv, AT91_SR);
  801. reg_imr = at91_read(priv, AT91_IMR);
  802. /* Ignore masked interrupts */
  803. reg_sr &= reg_imr;
  804. if (!reg_sr)
  805. goto exit;
  806. handled = IRQ_HANDLED;
  807. /* Receive or error interrupt? -> napi */
  808. if (reg_sr & (AT91_IRQ_MB_RX | AT91_IRQ_ERR_FRAME)) {
  809. /*
  810. * The error bits are clear on read,
  811. * save for later use.
  812. */
  813. priv->reg_sr = reg_sr;
  814. at91_write(priv, AT91_IDR,
  815. AT91_IRQ_MB_RX | AT91_IRQ_ERR_FRAME);
  816. napi_schedule(&priv->napi);
  817. }
  818. /* Transmission complete interrupt */
  819. if (reg_sr & AT91_IRQ_MB_TX)
  820. at91_irq_tx(dev, reg_sr);
  821. at91_irq_err(dev);
  822. exit:
  823. return handled;
  824. }
  825. static int at91_open(struct net_device *dev)
  826. {
  827. struct at91_priv *priv = netdev_priv(dev);
  828. int err;
  829. clk_enable(priv->clk);
  830. /* check or determine and set bittime */
  831. err = open_candev(dev);
  832. if (err)
  833. goto out;
  834. /* register interrupt handler */
  835. if (request_irq(dev->irq, at91_irq, IRQF_SHARED,
  836. dev->name, dev)) {
  837. err = -EAGAIN;
  838. goto out_close;
  839. }
  840. /* start chip and queuing */
  841. at91_chip_start(dev);
  842. napi_enable(&priv->napi);
  843. netif_start_queue(dev);
  844. return 0;
  845. out_close:
  846. close_candev(dev);
  847. out:
  848. clk_disable(priv->clk);
  849. return err;
  850. }
  851. /*
  852. * stop CAN bus activity
  853. */
  854. static int at91_close(struct net_device *dev)
  855. {
  856. struct at91_priv *priv = netdev_priv(dev);
  857. netif_stop_queue(dev);
  858. napi_disable(&priv->napi);
  859. at91_chip_stop(dev, CAN_STATE_STOPPED);
  860. free_irq(dev->irq, dev);
  861. clk_disable(priv->clk);
  862. close_candev(dev);
  863. return 0;
  864. }
  865. static int at91_set_mode(struct net_device *dev, enum can_mode mode)
  866. {
  867. switch (mode) {
  868. case CAN_MODE_START:
  869. at91_chip_start(dev);
  870. netif_wake_queue(dev);
  871. break;
  872. default:
  873. return -EOPNOTSUPP;
  874. }
  875. return 0;
  876. }
  877. static const struct net_device_ops at91_netdev_ops = {
  878. .ndo_open = at91_open,
  879. .ndo_stop = at91_close,
  880. .ndo_start_xmit = at91_start_xmit,
  881. };
  882. static int __init at91_can_probe(struct platform_device *pdev)
  883. {
  884. struct net_device *dev;
  885. struct at91_priv *priv;
  886. struct resource *res;
  887. struct clk *clk;
  888. void __iomem *addr;
  889. int err, irq;
  890. clk = clk_get(&pdev->dev, "can_clk");
  891. if (IS_ERR(clk)) {
  892. dev_err(&pdev->dev, "no clock defined\n");
  893. err = -ENODEV;
  894. goto exit;
  895. }
  896. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  897. irq = platform_get_irq(pdev, 0);
  898. if (!res || !irq) {
  899. err = -ENODEV;
  900. goto exit_put;
  901. }
  902. if (!request_mem_region(res->start,
  903. resource_size(res),
  904. pdev->name)) {
  905. err = -EBUSY;
  906. goto exit_put;
  907. }
  908. addr = ioremap_nocache(res->start, resource_size(res));
  909. if (!addr) {
  910. err = -ENOMEM;
  911. goto exit_release;
  912. }
  913. dev = alloc_candev(sizeof(struct at91_priv));
  914. if (!dev) {
  915. err = -ENOMEM;
  916. goto exit_iounmap;
  917. }
  918. dev->netdev_ops = &at91_netdev_ops;
  919. dev->irq = irq;
  920. dev->flags |= IFF_ECHO;
  921. priv = netdev_priv(dev);
  922. priv->can.clock.freq = clk_get_rate(clk);
  923. priv->can.bittiming_const = &at91_bittiming_const;
  924. priv->can.do_set_bittiming = at91_set_bittiming;
  925. priv->can.do_set_mode = at91_set_mode;
  926. priv->reg_base = addr;
  927. priv->dev = dev;
  928. priv->clk = clk;
  929. priv->pdata = pdev->dev.platform_data;
  930. netif_napi_add(dev, &priv->napi, at91_poll, AT91_NAPI_WEIGHT);
  931. dev_set_drvdata(&pdev->dev, dev);
  932. SET_NETDEV_DEV(dev, &pdev->dev);
  933. err = register_candev(dev);
  934. if (err) {
  935. dev_err(&pdev->dev, "registering netdev failed\n");
  936. goto exit_free;
  937. }
  938. dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
  939. priv->reg_base, dev->irq);
  940. return 0;
  941. exit_free:
  942. free_netdev(dev);
  943. exit_iounmap:
  944. iounmap(addr);
  945. exit_release:
  946. release_mem_region(res->start, resource_size(res));
  947. exit_put:
  948. clk_put(clk);
  949. exit:
  950. return err;
  951. }
  952. static int __devexit at91_can_remove(struct platform_device *pdev)
  953. {
  954. struct net_device *dev = platform_get_drvdata(pdev);
  955. struct at91_priv *priv = netdev_priv(dev);
  956. struct resource *res;
  957. unregister_netdev(dev);
  958. platform_set_drvdata(pdev, NULL);
  959. free_netdev(dev);
  960. iounmap(priv->reg_base);
  961. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  962. release_mem_region(res->start, resource_size(res));
  963. clk_put(priv->clk);
  964. return 0;
  965. }
  966. static struct platform_driver at91_can_driver = {
  967. .probe = at91_can_probe,
  968. .remove = __devexit_p(at91_can_remove),
  969. .driver = {
  970. .name = DRV_NAME,
  971. .owner = THIS_MODULE,
  972. },
  973. };
  974. static int __init at91_can_module_init(void)
  975. {
  976. printk(KERN_INFO "%s netdevice driver\n", DRV_NAME);
  977. return platform_driver_register(&at91_can_driver);
  978. }
  979. static void __exit at91_can_module_exit(void)
  980. {
  981. platform_driver_unregister(&at91_can_driver);
  982. printk(KERN_INFO "%s: driver removed\n", DRV_NAME);
  983. }
  984. module_init(at91_can_module_init);
  985. module_exit(at91_can_module_exit);
  986. MODULE_AUTHOR("Marc Kleine-Budde <mkl@pengutronix.de>");
  987. MODULE_LICENSE("GPL v2");
  988. MODULE_DESCRIPTION(DRV_NAME " CAN netdevice driver");