at91_can.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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. mb = get_tx_next_mb(priv);
  289. prio = get_tx_next_prio(priv);
  290. if (unlikely(!(at91_read(priv, AT91_MSR(mb)) & AT91_MSR_MRDY))) {
  291. netif_stop_queue(dev);
  292. dev_err(dev->dev.parent,
  293. "BUG! TX buffer full when queue awake!\n");
  294. return NETDEV_TX_BUSY;
  295. }
  296. if (cf->can_id & CAN_EFF_FLAG)
  297. reg_mid = (cf->can_id & CAN_EFF_MASK) | AT91_MID_MIDE;
  298. else
  299. reg_mid = (cf->can_id & CAN_SFF_MASK) << 18;
  300. reg_mcr = ((cf->can_id & CAN_RTR_FLAG) ? AT91_MCR_MRTR : 0) |
  301. (cf->can_dlc << 16) | AT91_MCR_MTCR;
  302. /* disable MB while writing ID (see datasheet) */
  303. set_mb_mode(priv, mb, AT91_MB_MODE_DISABLED);
  304. at91_write(priv, AT91_MID(mb), reg_mid);
  305. set_mb_mode_prio(priv, mb, AT91_MB_MODE_TX, prio);
  306. at91_write(priv, AT91_MDL(mb), *(u32 *)(cf->data + 0));
  307. at91_write(priv, AT91_MDH(mb), *(u32 *)(cf->data + 4));
  308. /* This triggers transmission */
  309. at91_write(priv, AT91_MCR(mb), reg_mcr);
  310. stats->tx_bytes += cf->can_dlc;
  311. dev->trans_start = jiffies;
  312. /* _NOTE_: substract AT91_MB_TX_FIRST offset from mb! */
  313. can_put_echo_skb(skb, dev, mb - AT91_MB_TX_FIRST);
  314. /*
  315. * we have to stop the queue and deliver all messages in case
  316. * of a prio+mb counter wrap around. This is the case if
  317. * tx_next buffer prio and mailbox equals 0.
  318. *
  319. * also stop the queue if next buffer is still in use
  320. * (== not ready)
  321. */
  322. priv->tx_next++;
  323. if (!(at91_read(priv, AT91_MSR(get_tx_next_mb(priv))) &
  324. AT91_MSR_MRDY) ||
  325. (priv->tx_next & AT91_NEXT_MASK) == 0)
  326. netif_stop_queue(dev);
  327. /* Enable interrupt for this mailbox */
  328. at91_write(priv, AT91_IER, 1 << mb);
  329. return NETDEV_TX_OK;
  330. }
  331. /**
  332. * at91_activate_rx_low - activate lower rx mailboxes
  333. * @priv: a91 context
  334. *
  335. * Reenables the lower mailboxes for reception of new CAN messages
  336. */
  337. static inline void at91_activate_rx_low(const struct at91_priv *priv)
  338. {
  339. u32 mask = AT91_MB_RX_LOW_MASK;
  340. at91_write(priv, AT91_TCR, mask);
  341. }
  342. /**
  343. * at91_activate_rx_mb - reactive single rx mailbox
  344. * @priv: a91 context
  345. * @mb: mailbox to reactivate
  346. *
  347. * Reenables given mailbox for reception of new CAN messages
  348. */
  349. static inline void at91_activate_rx_mb(const struct at91_priv *priv,
  350. unsigned int mb)
  351. {
  352. u32 mask = 1 << mb;
  353. at91_write(priv, AT91_TCR, mask);
  354. }
  355. /**
  356. * at91_rx_overflow_err - send error frame due to rx overflow
  357. * @dev: net device
  358. */
  359. static void at91_rx_overflow_err(struct net_device *dev)
  360. {
  361. struct net_device_stats *stats = &dev->stats;
  362. struct sk_buff *skb;
  363. struct can_frame *cf;
  364. dev_dbg(dev->dev.parent, "RX buffer overflow\n");
  365. stats->rx_over_errors++;
  366. stats->rx_errors++;
  367. skb = alloc_can_err_skb(dev, &cf);
  368. if (unlikely(!skb))
  369. return;
  370. cf->can_id |= CAN_ERR_CRTL;
  371. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  372. netif_receive_skb(skb);
  373. stats->rx_packets++;
  374. stats->rx_bytes += cf->can_dlc;
  375. }
  376. /**
  377. * at91_read_mb - read CAN msg from mailbox (lowlevel impl)
  378. * @dev: net device
  379. * @mb: mailbox number to read from
  380. * @cf: can frame where to store message
  381. *
  382. * Reads a CAN message from the given mailbox and stores data into
  383. * given can frame. "mb" and "cf" must be valid.
  384. */
  385. static void at91_read_mb(struct net_device *dev, unsigned int mb,
  386. struct can_frame *cf)
  387. {
  388. const struct at91_priv *priv = netdev_priv(dev);
  389. u32 reg_msr, reg_mid;
  390. reg_mid = at91_read(priv, AT91_MID(mb));
  391. if (reg_mid & AT91_MID_MIDE)
  392. cf->can_id = ((reg_mid >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG;
  393. else
  394. cf->can_id = (reg_mid >> 18) & CAN_SFF_MASK;
  395. reg_msr = at91_read(priv, AT91_MSR(mb));
  396. if (reg_msr & AT91_MSR_MRTR)
  397. cf->can_id |= CAN_RTR_FLAG;
  398. cf->can_dlc = get_can_dlc((reg_msr >> 16) & 0xf);
  399. *(u32 *)(cf->data + 0) = at91_read(priv, AT91_MDL(mb));
  400. *(u32 *)(cf->data + 4) = at91_read(priv, AT91_MDH(mb));
  401. if (unlikely(mb == AT91_MB_RX_LAST && reg_msr & AT91_MSR_MMI))
  402. at91_rx_overflow_err(dev);
  403. }
  404. /**
  405. * at91_read_msg - read CAN message from mailbox
  406. * @dev: net device
  407. * @mb: mail box to read from
  408. *
  409. * Reads a CAN message from given mailbox, and put into linux network
  410. * RX queue, does all housekeeping chores (stats, ...)
  411. */
  412. static void at91_read_msg(struct net_device *dev, unsigned int mb)
  413. {
  414. struct net_device_stats *stats = &dev->stats;
  415. struct can_frame *cf;
  416. struct sk_buff *skb;
  417. skb = alloc_can_skb(dev, &cf);
  418. if (unlikely(!skb)) {
  419. stats->rx_dropped++;
  420. return;
  421. }
  422. at91_read_mb(dev, mb, cf);
  423. netif_receive_skb(skb);
  424. stats->rx_packets++;
  425. stats->rx_bytes += cf->can_dlc;
  426. }
  427. /**
  428. * at91_poll_rx - read multiple CAN messages from mailboxes
  429. * @dev: net device
  430. * @quota: max number of pkgs we're allowed to receive
  431. *
  432. * Theory of Operation:
  433. *
  434. * 12 of the 16 mailboxes on the chip are reserved for RX. we split
  435. * them into 2 groups. The lower group holds 8 and upper 4 mailboxes.
  436. *
  437. * Like it or not, but the chip always saves a received CAN message
  438. * into the first free mailbox it finds (starting with the
  439. * lowest). This makes it very difficult to read the messages in the
  440. * right order from the chip. This is how we work around that problem:
  441. *
  442. * The first message goes into mb nr. 0 and issues an interrupt. All
  443. * rx ints are disabled in the interrupt handler and a napi poll is
  444. * scheduled. We read the mailbox, but do _not_ reenable the mb (to
  445. * receive another message).
  446. *
  447. * lower mbxs upper
  448. * ______^______ __^__
  449. * / \ / \
  450. * +-+-+-+-+-+-+-+-++-+-+-+-+
  451. * |x|x|x|x|x|x|x|x|| | | | |
  452. * +-+-+-+-+-+-+-+-++-+-+-+-+
  453. * 0 0 0 0 0 0 0 0 0 0 1 1 \ mail
  454. * 0 1 2 3 4 5 6 7 8 9 0 1 / box
  455. *
  456. * The variable priv->rx_next points to the next mailbox to read a
  457. * message from. As long we're in the lower mailboxes we just read the
  458. * mailbox but not reenable it.
  459. *
  460. * With completion of the last of the lower mailboxes, we reenable the
  461. * whole first group, but continue to look for filled mailboxes in the
  462. * upper mailboxes. Imagine the second group like overflow mailboxes,
  463. * which takes CAN messages if the lower goup is full. While in the
  464. * upper group we reenable the mailbox right after reading it. Giving
  465. * the chip more room to store messages.
  466. *
  467. * After finishing we look again in the lower group if we've still
  468. * quota.
  469. *
  470. */
  471. static int at91_poll_rx(struct net_device *dev, int quota)
  472. {
  473. struct at91_priv *priv = netdev_priv(dev);
  474. u32 reg_sr = at91_read(priv, AT91_SR);
  475. const unsigned long *addr = (unsigned long *)&reg_sr;
  476. unsigned int mb;
  477. int received = 0;
  478. if (priv->rx_next > AT91_MB_RX_LOW_LAST &&
  479. reg_sr & AT91_MB_RX_LOW_MASK)
  480. dev_info(dev->dev.parent,
  481. "order of incoming frames cannot be guaranteed\n");
  482. again:
  483. for (mb = find_next_bit(addr, AT91_MB_RX_NUM, priv->rx_next);
  484. mb < AT91_MB_RX_NUM && quota > 0;
  485. reg_sr = at91_read(priv, AT91_SR),
  486. mb = find_next_bit(addr, AT91_MB_RX_NUM, ++priv->rx_next)) {
  487. at91_read_msg(dev, mb);
  488. /* reactivate mailboxes */
  489. if (mb == AT91_MB_RX_LOW_LAST)
  490. /* all lower mailboxed, if just finished it */
  491. at91_activate_rx_low(priv);
  492. else if (mb > AT91_MB_RX_LOW_LAST)
  493. /* only the mailbox we read */
  494. at91_activate_rx_mb(priv, mb);
  495. received++;
  496. quota--;
  497. }
  498. /* upper group completed, look again in lower */
  499. if (priv->rx_next > AT91_MB_RX_LOW_LAST &&
  500. quota > 0 && mb >= AT91_MB_RX_NUM) {
  501. priv->rx_next = 0;
  502. goto again;
  503. }
  504. return received;
  505. }
  506. static void at91_poll_err_frame(struct net_device *dev,
  507. struct can_frame *cf, u32 reg_sr)
  508. {
  509. struct at91_priv *priv = netdev_priv(dev);
  510. /* CRC error */
  511. if (reg_sr & AT91_IRQ_CERR) {
  512. dev_dbg(dev->dev.parent, "CERR irq\n");
  513. dev->stats.rx_errors++;
  514. priv->can.can_stats.bus_error++;
  515. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  516. }
  517. /* Stuffing Error */
  518. if (reg_sr & AT91_IRQ_SERR) {
  519. dev_dbg(dev->dev.parent, "SERR 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. cf->data[2] |= CAN_ERR_PROT_STUFF;
  524. }
  525. /* Acknowledgement Error */
  526. if (reg_sr & AT91_IRQ_AERR) {
  527. dev_dbg(dev->dev.parent, "AERR irq\n");
  528. dev->stats.tx_errors++;
  529. cf->can_id |= CAN_ERR_ACK;
  530. }
  531. /* Form error */
  532. if (reg_sr & AT91_IRQ_FERR) {
  533. dev_dbg(dev->dev.parent, "FERR irq\n");
  534. dev->stats.rx_errors++;
  535. priv->can.can_stats.bus_error++;
  536. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  537. cf->data[2] |= CAN_ERR_PROT_FORM;
  538. }
  539. /* Bit Error */
  540. if (reg_sr & AT91_IRQ_BERR) {
  541. dev_dbg(dev->dev.parent, "BERR irq\n");
  542. dev->stats.tx_errors++;
  543. priv->can.can_stats.bus_error++;
  544. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  545. cf->data[2] |= CAN_ERR_PROT_BIT;
  546. }
  547. }
  548. static int at91_poll_err(struct net_device *dev, int quota, u32 reg_sr)
  549. {
  550. struct sk_buff *skb;
  551. struct can_frame *cf;
  552. if (quota == 0)
  553. return 0;
  554. skb = alloc_can_err_skb(dev, &cf);
  555. if (unlikely(!skb))
  556. return 0;
  557. at91_poll_err_frame(dev, cf, reg_sr);
  558. netif_receive_skb(skb);
  559. dev->last_rx = jiffies;
  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->last_rx = jiffies;
  763. dev->stats.rx_packets++;
  764. dev->stats.rx_bytes += cf->can_dlc;
  765. priv->can.state = new_state;
  766. }
  767. /*
  768. * interrupt handler
  769. */
  770. static irqreturn_t at91_irq(int irq, void *dev_id)
  771. {
  772. struct net_device *dev = dev_id;
  773. struct at91_priv *priv = netdev_priv(dev);
  774. irqreturn_t handled = IRQ_NONE;
  775. u32 reg_sr, reg_imr;
  776. reg_sr = at91_read(priv, AT91_SR);
  777. reg_imr = at91_read(priv, AT91_IMR);
  778. /* Ignore masked interrupts */
  779. reg_sr &= reg_imr;
  780. if (!reg_sr)
  781. goto exit;
  782. handled = IRQ_HANDLED;
  783. /* Receive or error interrupt? -> napi */
  784. if (reg_sr & (AT91_IRQ_MB_RX | AT91_IRQ_ERR_FRAME)) {
  785. /*
  786. * The error bits are clear on read,
  787. * save for later use.
  788. */
  789. priv->reg_sr = reg_sr;
  790. at91_write(priv, AT91_IDR,
  791. AT91_IRQ_MB_RX | AT91_IRQ_ERR_FRAME);
  792. napi_schedule(&priv->napi);
  793. }
  794. /* Transmission complete interrupt */
  795. if (reg_sr & AT91_IRQ_MB_TX)
  796. at91_irq_tx(dev, reg_sr);
  797. at91_irq_err(dev);
  798. exit:
  799. return handled;
  800. }
  801. static int at91_open(struct net_device *dev)
  802. {
  803. struct at91_priv *priv = netdev_priv(dev);
  804. int err;
  805. clk_enable(priv->clk);
  806. /* check or determine and set bittime */
  807. err = open_candev(dev);
  808. if (err)
  809. goto out;
  810. /* register interrupt handler */
  811. if (request_irq(dev->irq, at91_irq, IRQF_SHARED,
  812. dev->name, dev)) {
  813. err = -EAGAIN;
  814. goto out_close;
  815. }
  816. /* start chip and queuing */
  817. at91_chip_start(dev);
  818. napi_enable(&priv->napi);
  819. netif_start_queue(dev);
  820. return 0;
  821. out_close:
  822. close_candev(dev);
  823. out:
  824. clk_disable(priv->clk);
  825. return err;
  826. }
  827. /*
  828. * stop CAN bus activity
  829. */
  830. static int at91_close(struct net_device *dev)
  831. {
  832. struct at91_priv *priv = netdev_priv(dev);
  833. netif_stop_queue(dev);
  834. napi_disable(&priv->napi);
  835. at91_chip_stop(dev, CAN_STATE_STOPPED);
  836. free_irq(dev->irq, dev);
  837. clk_disable(priv->clk);
  838. close_candev(dev);
  839. return 0;
  840. }
  841. static int at91_set_mode(struct net_device *dev, enum can_mode mode)
  842. {
  843. switch (mode) {
  844. case CAN_MODE_START:
  845. at91_chip_start(dev);
  846. netif_wake_queue(dev);
  847. break;
  848. default:
  849. return -EOPNOTSUPP;
  850. }
  851. return 0;
  852. }
  853. static const struct net_device_ops at91_netdev_ops = {
  854. .ndo_open = at91_open,
  855. .ndo_stop = at91_close,
  856. .ndo_start_xmit = at91_start_xmit,
  857. };
  858. static int __init at91_can_probe(struct platform_device *pdev)
  859. {
  860. struct net_device *dev;
  861. struct at91_priv *priv;
  862. struct resource *res;
  863. struct clk *clk;
  864. void __iomem *addr;
  865. int err, irq;
  866. clk = clk_get(&pdev->dev, "can_clk");
  867. if (IS_ERR(clk)) {
  868. dev_err(&pdev->dev, "no clock defined\n");
  869. err = -ENODEV;
  870. goto exit;
  871. }
  872. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  873. irq = platform_get_irq(pdev, 0);
  874. if (!res || irq <= 0) {
  875. err = -ENODEV;
  876. goto exit_put;
  877. }
  878. if (!request_mem_region(res->start,
  879. resource_size(res),
  880. pdev->name)) {
  881. err = -EBUSY;
  882. goto exit_put;
  883. }
  884. addr = ioremap_nocache(res->start, resource_size(res));
  885. if (!addr) {
  886. err = -ENOMEM;
  887. goto exit_release;
  888. }
  889. dev = alloc_candev(sizeof(struct at91_priv), AT91_MB_TX_NUM);
  890. if (!dev) {
  891. err = -ENOMEM;
  892. goto exit_iounmap;
  893. }
  894. dev->netdev_ops = &at91_netdev_ops;
  895. dev->irq = irq;
  896. dev->flags |= IFF_ECHO;
  897. priv = netdev_priv(dev);
  898. priv->can.clock.freq = clk_get_rate(clk);
  899. priv->can.bittiming_const = &at91_bittiming_const;
  900. priv->can.do_set_bittiming = at91_set_bittiming;
  901. priv->can.do_set_mode = at91_set_mode;
  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");