at91_can.c 29 KB

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