at91_can.c 33 KB

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