at91_can.c 29 KB

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