at91_can.c 34 KB

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