at91_can.c 29 KB

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