at91_can.c 31 KB

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