at91_can.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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 <hjk@hansjkoch.de>
  6. * (C) 2008, 2009, 2010, 2011, 2023 by Marc Kleine-Budde <kernel@pengutronix.de>
  7. */
  8. #include <linux/bitfield.h>
  9. #include <linux/clk.h>
  10. #include <linux/errno.h>
  11. #include <linux/ethtool.h>
  12. #include <linux/if_arp.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/of.h>
  18. #include <linux/phy/phy.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/rtnetlink.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/string.h>
  24. #include <linux/types.h>
  25. #include <linux/can/dev.h>
  26. #include <linux/can/error.h>
  27. #include <linux/can/rx-offload.h>
  28. #define AT91_MB_MASK(i) ((1 << (i)) - 1)
  29. /* Common registers */
  30. enum at91_reg {
  31. AT91_MR = 0x000,
  32. AT91_IER = 0x004,
  33. AT91_IDR = 0x008,
  34. AT91_IMR = 0x00C,
  35. AT91_SR = 0x010,
  36. AT91_BR = 0x014,
  37. AT91_TIM = 0x018,
  38. AT91_TIMESTP = 0x01C,
  39. AT91_ECR = 0x020,
  40. AT91_TCR = 0x024,
  41. AT91_ACR = 0x028,
  42. };
  43. /* Mailbox registers (0 <= i <= 15) */
  44. #define AT91_MMR(i) ((enum at91_reg)(0x200 + ((i) * 0x20)))
  45. #define AT91_MAM(i) ((enum at91_reg)(0x204 + ((i) * 0x20)))
  46. #define AT91_MID(i) ((enum at91_reg)(0x208 + ((i) * 0x20)))
  47. #define AT91_MFID(i) ((enum at91_reg)(0x20C + ((i) * 0x20)))
  48. #define AT91_MSR(i) ((enum at91_reg)(0x210 + ((i) * 0x20)))
  49. #define AT91_MDL(i) ((enum at91_reg)(0x214 + ((i) * 0x20)))
  50. #define AT91_MDH(i) ((enum at91_reg)(0x218 + ((i) * 0x20)))
  51. #define AT91_MCR(i) ((enum at91_reg)(0x21C + ((i) * 0x20)))
  52. /* Register bits */
  53. #define AT91_MR_CANEN BIT(0)
  54. #define AT91_MR_LPM BIT(1)
  55. #define AT91_MR_ABM BIT(2)
  56. #define AT91_MR_OVL BIT(3)
  57. #define AT91_MR_TEOF BIT(4)
  58. #define AT91_MR_TTM BIT(5)
  59. #define AT91_MR_TIMFRZ BIT(6)
  60. #define AT91_MR_DRPT BIT(7)
  61. #define AT91_SR_RBSY BIT(29)
  62. #define AT91_SR_TBSY BIT(30)
  63. #define AT91_SR_OVLSY BIT(31)
  64. #define AT91_BR_PHASE2_MASK GENMASK(2, 0)
  65. #define AT91_BR_PHASE1_MASK GENMASK(6, 4)
  66. #define AT91_BR_PROPAG_MASK GENMASK(10, 8)
  67. #define AT91_BR_SJW_MASK GENMASK(13, 12)
  68. #define AT91_BR_BRP_MASK GENMASK(22, 16)
  69. #define AT91_BR_SMP BIT(24)
  70. #define AT91_TIM_TIMER_MASK GENMASK(15, 0)
  71. #define AT91_ECR_REC_MASK GENMASK(8, 0)
  72. #define AT91_ECR_TEC_MASK GENMASK(23, 16)
  73. #define AT91_TCR_TIMRST BIT(31)
  74. #define AT91_MMR_MTIMEMARK_MASK GENMASK(15, 0)
  75. #define AT91_MMR_PRIOR_MASK GENMASK(19, 16)
  76. #define AT91_MMR_MOT_MASK GENMASK(26, 24)
  77. #define AT91_MID_MIDVB_MASK GENMASK(17, 0)
  78. #define AT91_MID_MIDVA_MASK GENMASK(28, 18)
  79. #define AT91_MID_MIDE BIT(29)
  80. #define AT91_MSR_MTIMESTAMP_MASK GENMASK(15, 0)
  81. #define AT91_MSR_MDLC_MASK GENMASK(19, 16)
  82. #define AT91_MSR_MRTR BIT(20)
  83. #define AT91_MSR_MABT BIT(22)
  84. #define AT91_MSR_MRDY BIT(23)
  85. #define AT91_MSR_MMI BIT(24)
  86. #define AT91_MCR_MDLC_MASK GENMASK(19, 16)
  87. #define AT91_MCR_MRTR BIT(20)
  88. #define AT91_MCR_MACR BIT(22)
  89. #define AT91_MCR_MTCR BIT(23)
  90. /* Mailbox Modes */
  91. enum at91_mb_mode {
  92. AT91_MB_MODE_DISABLED = 0,
  93. AT91_MB_MODE_RX = 1,
  94. AT91_MB_MODE_RX_OVRWR = 2,
  95. AT91_MB_MODE_TX = 3,
  96. AT91_MB_MODE_CONSUMER = 4,
  97. AT91_MB_MODE_PRODUCER = 5,
  98. };
  99. /* Interrupt mask bits */
  100. #define AT91_IRQ_ERRA BIT(16)
  101. #define AT91_IRQ_WARN BIT(17)
  102. #define AT91_IRQ_ERRP BIT(18)
  103. #define AT91_IRQ_BOFF BIT(19)
  104. #define AT91_IRQ_SLEEP BIT(20)
  105. #define AT91_IRQ_WAKEUP BIT(21)
  106. #define AT91_IRQ_TOVF BIT(22)
  107. #define AT91_IRQ_TSTP BIT(23)
  108. #define AT91_IRQ_CERR BIT(24)
  109. #define AT91_IRQ_SERR BIT(25)
  110. #define AT91_IRQ_AERR BIT(26)
  111. #define AT91_IRQ_FERR BIT(27)
  112. #define AT91_IRQ_BERR BIT(28)
  113. #define AT91_IRQ_ERR_ALL (0x1fff0000)
  114. #define AT91_IRQ_ERR_FRAME (AT91_IRQ_CERR | AT91_IRQ_SERR | \
  115. AT91_IRQ_AERR | AT91_IRQ_FERR | AT91_IRQ_BERR)
  116. #define AT91_IRQ_ERR_LINE (AT91_IRQ_ERRA | AT91_IRQ_WARN | \
  117. AT91_IRQ_ERRP | AT91_IRQ_BOFF)
  118. #define AT91_IRQ_ALL (0x1fffffff)
  119. enum at91_devtype {
  120. AT91_DEVTYPE_SAM9263,
  121. AT91_DEVTYPE_SAM9X5,
  122. };
  123. struct at91_devtype_data {
  124. unsigned int rx_first;
  125. unsigned int rx_last;
  126. unsigned int tx_shift;
  127. enum at91_devtype type;
  128. };
  129. struct at91_priv {
  130. struct can_priv can; /* must be the first member! */
  131. struct can_rx_offload offload;
  132. struct phy *transceiver;
  133. void __iomem *reg_base;
  134. unsigned int tx_head;
  135. unsigned int tx_tail;
  136. struct at91_devtype_data devtype_data;
  137. struct clk *clk;
  138. struct at91_can_data *pdata;
  139. canid_t mb0_id;
  140. };
  141. static inline struct at91_priv *rx_offload_to_priv(struct can_rx_offload *offload)
  142. {
  143. return container_of(offload, struct at91_priv, offload);
  144. }
  145. static const struct at91_devtype_data at91_at91sam9263_data = {
  146. .rx_first = 1,
  147. .rx_last = 11,
  148. .tx_shift = 2,
  149. .type = AT91_DEVTYPE_SAM9263,
  150. };
  151. static const struct at91_devtype_data at91_at91sam9x5_data = {
  152. .rx_first = 0,
  153. .rx_last = 5,
  154. .tx_shift = 1,
  155. .type = AT91_DEVTYPE_SAM9X5,
  156. };
  157. static const struct can_bittiming_const at91_bittiming_const = {
  158. .name = KBUILD_MODNAME,
  159. .tseg1_min = 4,
  160. .tseg1_max = 16,
  161. .tseg2_min = 2,
  162. .tseg2_max = 8,
  163. .sjw_max = 4,
  164. .brp_min = 2,
  165. .brp_max = 128,
  166. .brp_inc = 1,
  167. };
  168. #define AT91_IS(_model) \
  169. static inline int __maybe_unused at91_is_sam##_model(const struct at91_priv *priv) \
  170. { \
  171. return priv->devtype_data.type == AT91_DEVTYPE_SAM##_model; \
  172. }
  173. AT91_IS(9263);
  174. AT91_IS(9X5);
  175. static inline unsigned int get_mb_rx_first(const struct at91_priv *priv)
  176. {
  177. return priv->devtype_data.rx_first;
  178. }
  179. static inline unsigned int get_mb_rx_last(const struct at91_priv *priv)
  180. {
  181. return priv->devtype_data.rx_last;
  182. }
  183. static inline unsigned int get_mb_tx_shift(const struct at91_priv *priv)
  184. {
  185. return priv->devtype_data.tx_shift;
  186. }
  187. static inline unsigned int get_mb_tx_num(const struct at91_priv *priv)
  188. {
  189. return 1 << get_mb_tx_shift(priv);
  190. }
  191. static inline unsigned int get_mb_tx_first(const struct at91_priv *priv)
  192. {
  193. return get_mb_rx_last(priv) + 1;
  194. }
  195. static inline unsigned int get_mb_tx_last(const struct at91_priv *priv)
  196. {
  197. return get_mb_tx_first(priv) + get_mb_tx_num(priv) - 1;
  198. }
  199. static inline unsigned int get_head_prio_shift(const struct at91_priv *priv)
  200. {
  201. return get_mb_tx_shift(priv);
  202. }
  203. static inline unsigned int get_head_prio_mask(const struct at91_priv *priv)
  204. {
  205. return 0xf << get_mb_tx_shift(priv);
  206. }
  207. static inline unsigned int get_head_mb_mask(const struct at91_priv *priv)
  208. {
  209. return AT91_MB_MASK(get_mb_tx_shift(priv));
  210. }
  211. static inline unsigned int get_head_mask(const struct at91_priv *priv)
  212. {
  213. return get_head_mb_mask(priv) | get_head_prio_mask(priv);
  214. }
  215. static inline unsigned int get_irq_mb_rx(const struct at91_priv *priv)
  216. {
  217. return AT91_MB_MASK(get_mb_rx_last(priv) + 1) &
  218. ~AT91_MB_MASK(get_mb_rx_first(priv));
  219. }
  220. static inline unsigned int get_irq_mb_tx(const struct at91_priv *priv)
  221. {
  222. return AT91_MB_MASK(get_mb_tx_last(priv) + 1) &
  223. ~AT91_MB_MASK(get_mb_tx_first(priv));
  224. }
  225. static inline unsigned int get_tx_head_mb(const struct at91_priv *priv)
  226. {
  227. return (priv->tx_head & get_head_mb_mask(priv)) + get_mb_tx_first(priv);
  228. }
  229. static inline unsigned int get_tx_head_prio(const struct at91_priv *priv)
  230. {
  231. return (priv->tx_head >> get_head_prio_shift(priv)) & 0xf;
  232. }
  233. static inline unsigned int get_tx_tail_mb(const struct at91_priv *priv)
  234. {
  235. return (priv->tx_tail & get_head_mb_mask(priv)) + get_mb_tx_first(priv);
  236. }
  237. static inline u32 at91_read(const struct at91_priv *priv, enum at91_reg reg)
  238. {
  239. return readl_relaxed(priv->reg_base + reg);
  240. }
  241. static inline void at91_write(const struct at91_priv *priv, enum at91_reg reg,
  242. u32 value)
  243. {
  244. writel_relaxed(value, priv->reg_base + reg);
  245. }
  246. static inline void set_mb_mode_prio(const struct at91_priv *priv,
  247. unsigned int mb, enum at91_mb_mode mode,
  248. u8 prio)
  249. {
  250. const u32 reg_mmr = FIELD_PREP(AT91_MMR_MOT_MASK, mode) |
  251. FIELD_PREP(AT91_MMR_PRIOR_MASK, prio);
  252. at91_write(priv, AT91_MMR(mb), reg_mmr);
  253. }
  254. static inline void set_mb_mode(const struct at91_priv *priv, unsigned int mb,
  255. enum at91_mb_mode mode)
  256. {
  257. set_mb_mode_prio(priv, mb, mode, 0);
  258. }
  259. static inline u32 at91_can_id_to_reg_mid(canid_t can_id)
  260. {
  261. u32 reg_mid;
  262. if (can_id & CAN_EFF_FLAG)
  263. reg_mid = FIELD_PREP(AT91_MID_MIDVA_MASK | AT91_MID_MIDVB_MASK, can_id) |
  264. AT91_MID_MIDE;
  265. else
  266. reg_mid = FIELD_PREP(AT91_MID_MIDVA_MASK, can_id);
  267. return reg_mid;
  268. }
  269. static void at91_setup_mailboxes(struct net_device *dev)
  270. {
  271. struct at91_priv *priv = netdev_priv(dev);
  272. unsigned int i;
  273. u32 reg_mid;
  274. /* Due to a chip bug (errata 50.2.6.3 & 50.3.5.3) the first
  275. * mailbox is disabled. The next mailboxes are used as a
  276. * reception FIFO. The last of the RX mailboxes is configured with
  277. * overwrite option. The overwrite flag indicates a FIFO
  278. * overflow.
  279. */
  280. reg_mid = at91_can_id_to_reg_mid(priv->mb0_id);
  281. for (i = 0; i < get_mb_rx_first(priv); i++) {
  282. set_mb_mode(priv, i, AT91_MB_MODE_DISABLED);
  283. at91_write(priv, AT91_MID(i), reg_mid);
  284. at91_write(priv, AT91_MCR(i), 0x0); /* clear dlc */
  285. }
  286. for (i = get_mb_rx_first(priv); i < get_mb_rx_last(priv); i++)
  287. set_mb_mode(priv, i, AT91_MB_MODE_RX);
  288. set_mb_mode(priv, get_mb_rx_last(priv), AT91_MB_MODE_RX_OVRWR);
  289. /* reset acceptance mask and id register */
  290. for (i = get_mb_rx_first(priv); i <= get_mb_rx_last(priv); i++) {
  291. at91_write(priv, AT91_MAM(i), 0x0);
  292. at91_write(priv, AT91_MID(i), AT91_MID_MIDE);
  293. }
  294. /* The last mailboxes are used for transmitting. */
  295. for (i = get_mb_tx_first(priv); i <= get_mb_tx_last(priv); i++)
  296. set_mb_mode_prio(priv, i, AT91_MB_MODE_TX, 0);
  297. /* Reset tx helper pointers */
  298. priv->tx_head = priv->tx_tail = 0;
  299. }
  300. static int at91_set_bittiming(struct net_device *dev)
  301. {
  302. const struct at91_priv *priv = netdev_priv(dev);
  303. const struct can_bittiming *bt = &priv->can.bittiming;
  304. u32 reg_br = 0;
  305. if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  306. reg_br |= AT91_BR_SMP;
  307. reg_br |= FIELD_PREP(AT91_BR_BRP_MASK, bt->brp - 1) |
  308. FIELD_PREP(AT91_BR_SJW_MASK, bt->sjw - 1) |
  309. FIELD_PREP(AT91_BR_PROPAG_MASK, bt->prop_seg - 1) |
  310. FIELD_PREP(AT91_BR_PHASE1_MASK, bt->phase_seg1 - 1) |
  311. FIELD_PREP(AT91_BR_PHASE2_MASK, bt->phase_seg2 - 1);
  312. netdev_dbg(dev, "writing AT91_BR: 0x%08x\n", reg_br);
  313. at91_write(priv, AT91_BR, reg_br);
  314. return 0;
  315. }
  316. static int at91_get_berr_counter(const struct net_device *dev,
  317. struct can_berr_counter *bec)
  318. {
  319. const struct at91_priv *priv = netdev_priv(dev);
  320. u32 reg_ecr = at91_read(priv, AT91_ECR);
  321. bec->rxerr = FIELD_GET(AT91_ECR_REC_MASK, reg_ecr);
  322. bec->txerr = FIELD_GET(AT91_ECR_TEC_MASK, reg_ecr);
  323. return 0;
  324. }
  325. static void at91_chip_start(struct net_device *dev)
  326. {
  327. struct at91_priv *priv = netdev_priv(dev);
  328. u32 reg_mr, reg_ier;
  329. /* disable interrupts */
  330. at91_write(priv, AT91_IDR, AT91_IRQ_ALL);
  331. /* disable chip */
  332. reg_mr = at91_read(priv, AT91_MR);
  333. at91_write(priv, AT91_MR, reg_mr & ~AT91_MR_CANEN);
  334. at91_set_bittiming(dev);
  335. at91_setup_mailboxes(dev);
  336. /* enable chip */
  337. if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  338. reg_mr = AT91_MR_CANEN | AT91_MR_ABM;
  339. else
  340. reg_mr = AT91_MR_CANEN;
  341. at91_write(priv, AT91_MR, reg_mr);
  342. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  343. /* Dummy read to clear latched line error interrupts on
  344. * sam9x5 and newer SoCs.
  345. */
  346. at91_read(priv, AT91_SR);
  347. /* Enable interrupts */
  348. reg_ier = get_irq_mb_rx(priv) | AT91_IRQ_ERR_LINE | AT91_IRQ_ERR_FRAME;
  349. at91_write(priv, AT91_IER, reg_ier);
  350. }
  351. static void at91_chip_stop(struct net_device *dev, enum can_state state)
  352. {
  353. struct at91_priv *priv = netdev_priv(dev);
  354. u32 reg_mr;
  355. /* Abort any pending TX requests. However this doesn't seem to
  356. * work in case of bus-off on sama5d3.
  357. */
  358. at91_write(priv, AT91_ACR, get_irq_mb_tx(priv));
  359. /* disable interrupts */
  360. at91_write(priv, AT91_IDR, AT91_IRQ_ALL);
  361. reg_mr = at91_read(priv, AT91_MR);
  362. at91_write(priv, AT91_MR, reg_mr & ~AT91_MR_CANEN);
  363. priv->can.state = state;
  364. }
  365. /* theory of operation:
  366. *
  367. * According to the datasheet priority 0 is the highest priority, 15
  368. * is the lowest. If two mailboxes have the same priority level the
  369. * message of the mailbox with the lowest number is sent first.
  370. *
  371. * We use the first TX mailbox (AT91_MB_TX_FIRST) with prio 0, then
  372. * the next mailbox with prio 0, and so on, until all mailboxes are
  373. * used. Then we start from the beginning with mailbox
  374. * AT91_MB_TX_FIRST, but with prio 1, mailbox AT91_MB_TX_FIRST + 1
  375. * prio 1. When we reach the last mailbox with prio 15, we have to
  376. * stop sending, waiting for all messages to be delivered, then start
  377. * again with mailbox AT91_MB_TX_FIRST prio 0.
  378. *
  379. * We use the priv->tx_head as counter for the next transmission
  380. * mailbox, but without the offset AT91_MB_TX_FIRST. The lower bits
  381. * encode the mailbox number, the upper 4 bits the mailbox priority:
  382. *
  383. * priv->tx_head = (prio << get_next_prio_shift(priv)) |
  384. * (mb - get_mb_tx_first(priv));
  385. *
  386. */
  387. static netdev_tx_t at91_start_xmit(struct sk_buff *skb, struct net_device *dev)
  388. {
  389. struct at91_priv *priv = netdev_priv(dev);
  390. struct can_frame *cf = (struct can_frame *)skb->data;
  391. unsigned int mb, prio;
  392. u32 reg_mid, reg_mcr;
  393. if (can_dev_dropped_skb(dev, skb))
  394. return NETDEV_TX_OK;
  395. mb = get_tx_head_mb(priv);
  396. prio = get_tx_head_prio(priv);
  397. if (unlikely(!(at91_read(priv, AT91_MSR(mb)) & AT91_MSR_MRDY))) {
  398. netif_stop_queue(dev);
  399. netdev_err(dev, "BUG! TX buffer full when queue awake!\n");
  400. return NETDEV_TX_BUSY;
  401. }
  402. reg_mid = at91_can_id_to_reg_mid(cf->can_id);
  403. reg_mcr = FIELD_PREP(AT91_MCR_MDLC_MASK, cf->len) |
  404. AT91_MCR_MTCR;
  405. if (cf->can_id & CAN_RTR_FLAG)
  406. reg_mcr |= AT91_MCR_MRTR;
  407. /* disable MB while writing ID (see datasheet) */
  408. set_mb_mode(priv, mb, AT91_MB_MODE_DISABLED);
  409. at91_write(priv, AT91_MID(mb), reg_mid);
  410. set_mb_mode_prio(priv, mb, AT91_MB_MODE_TX, prio);
  411. at91_write(priv, AT91_MDL(mb), *(u32 *)(cf->data + 0));
  412. at91_write(priv, AT91_MDH(mb), *(u32 *)(cf->data + 4));
  413. /* This triggers transmission */
  414. at91_write(priv, AT91_MCR(mb), reg_mcr);
  415. /* _NOTE_: subtract AT91_MB_TX_FIRST offset from mb! */
  416. can_put_echo_skb(skb, dev, mb - get_mb_tx_first(priv), 0);
  417. /* we have to stop the queue and deliver all messages in case
  418. * of a prio+mb counter wrap around. This is the case if
  419. * tx_head buffer prio and mailbox equals 0.
  420. *
  421. * also stop the queue if next buffer is still in use
  422. * (== not ready)
  423. */
  424. priv->tx_head++;
  425. if (!(at91_read(priv, AT91_MSR(get_tx_head_mb(priv))) &
  426. AT91_MSR_MRDY) ||
  427. (priv->tx_head & get_head_mask(priv)) == 0)
  428. netif_stop_queue(dev);
  429. /* Enable interrupt for this mailbox */
  430. at91_write(priv, AT91_IER, 1 << mb);
  431. return NETDEV_TX_OK;
  432. }
  433. static inline u32 at91_get_timestamp(const struct at91_priv *priv)
  434. {
  435. return at91_read(priv, AT91_TIM);
  436. }
  437. static inline struct sk_buff *
  438. at91_alloc_can_err_skb(struct net_device *dev,
  439. struct can_frame **cf, u32 *timestamp)
  440. {
  441. const struct at91_priv *priv = netdev_priv(dev);
  442. *timestamp = at91_get_timestamp(priv);
  443. return alloc_can_err_skb(dev, cf);
  444. }
  445. /**
  446. * at91_rx_overflow_err - send error frame due to rx overflow
  447. * @dev: net device
  448. */
  449. static void at91_rx_overflow_err(struct net_device *dev)
  450. {
  451. struct net_device_stats *stats = &dev->stats;
  452. struct sk_buff *skb;
  453. struct at91_priv *priv = netdev_priv(dev);
  454. struct can_frame *cf;
  455. u32 timestamp;
  456. int err;
  457. netdev_dbg(dev, "RX buffer overflow\n");
  458. stats->rx_over_errors++;
  459. stats->rx_errors++;
  460. skb = at91_alloc_can_err_skb(dev, &cf, &timestamp);
  461. if (unlikely(!skb))
  462. return;
  463. cf->can_id |= CAN_ERR_CRTL;
  464. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  465. err = can_rx_offload_queue_timestamp(&priv->offload, skb, timestamp);
  466. if (err)
  467. stats->rx_fifo_errors++;
  468. }
  469. /**
  470. * at91_mailbox_read - read CAN msg from mailbox
  471. * @offload: rx-offload
  472. * @mb: mailbox number to read from
  473. * @timestamp: pointer to 32 bit timestamp
  474. * @drop: true indicated mailbox to mark as read and drop frame
  475. *
  476. * Reads a CAN message from the given mailbox if not empty.
  477. */
  478. static struct sk_buff *at91_mailbox_read(struct can_rx_offload *offload,
  479. unsigned int mb, u32 *timestamp,
  480. bool drop)
  481. {
  482. const struct at91_priv *priv = rx_offload_to_priv(offload);
  483. struct can_frame *cf;
  484. struct sk_buff *skb;
  485. u32 reg_msr, reg_mid;
  486. reg_msr = at91_read(priv, AT91_MSR(mb));
  487. if (!(reg_msr & AT91_MSR_MRDY))
  488. return NULL;
  489. if (unlikely(drop)) {
  490. skb = ERR_PTR(-ENOBUFS);
  491. goto mark_as_read;
  492. }
  493. skb = alloc_can_skb(offload->dev, &cf);
  494. if (unlikely(!skb)) {
  495. skb = ERR_PTR(-ENOMEM);
  496. goto mark_as_read;
  497. }
  498. reg_mid = at91_read(priv, AT91_MID(mb));
  499. if (reg_mid & AT91_MID_MIDE)
  500. cf->can_id = FIELD_GET(AT91_MID_MIDVA_MASK | AT91_MID_MIDVB_MASK, reg_mid) |
  501. CAN_EFF_FLAG;
  502. else
  503. cf->can_id = FIELD_GET(AT91_MID_MIDVA_MASK, reg_mid);
  504. /* extend timestamp to full 32 bit */
  505. *timestamp = FIELD_GET(AT91_MSR_MTIMESTAMP_MASK, reg_msr) << 16;
  506. cf->len = can_cc_dlc2len(FIELD_GET(AT91_MSR_MDLC_MASK, reg_msr));
  507. if (reg_msr & AT91_MSR_MRTR) {
  508. cf->can_id |= CAN_RTR_FLAG;
  509. } else {
  510. *(u32 *)(cf->data + 0) = at91_read(priv, AT91_MDL(mb));
  511. *(u32 *)(cf->data + 4) = at91_read(priv, AT91_MDH(mb));
  512. }
  513. /* allow RX of extended frames */
  514. at91_write(priv, AT91_MID(mb), AT91_MID_MIDE);
  515. if (unlikely(mb == get_mb_rx_last(priv) && reg_msr & AT91_MSR_MMI))
  516. at91_rx_overflow_err(offload->dev);
  517. mark_as_read:
  518. at91_write(priv, AT91_MCR(mb), AT91_MCR_MTCR);
  519. return skb;
  520. }
  521. /* theory of operation:
  522. *
  523. * priv->tx_tail holds the number of the oldest can_frame put for
  524. * transmission into the hardware, but not yet ACKed by the CAN tx
  525. * complete IRQ.
  526. *
  527. * We iterate from priv->tx_tail to priv->tx_head and check if the
  528. * packet has been transmitted, echo it back to the CAN framework. If
  529. * we discover a not yet transmitted package, stop looking for more.
  530. *
  531. */
  532. static void at91_irq_tx(struct net_device *dev, u32 reg_sr)
  533. {
  534. struct at91_priv *priv = netdev_priv(dev);
  535. u32 reg_msr;
  536. unsigned int mb;
  537. for (/* nix */; (priv->tx_head - priv->tx_tail) > 0; priv->tx_tail++) {
  538. mb = get_tx_tail_mb(priv);
  539. /* no event in mailbox? */
  540. if (!(reg_sr & (1 << mb)))
  541. break;
  542. /* Disable irq for this TX mailbox */
  543. at91_write(priv, AT91_IDR, 1 << mb);
  544. /* only echo if mailbox signals us a transfer
  545. * complete (MSR_MRDY). Otherwise it's a tansfer
  546. * abort. "can_bus_off()" takes care about the skbs
  547. * parked in the echo queue.
  548. */
  549. reg_msr = at91_read(priv, AT91_MSR(mb));
  550. if (unlikely(!(reg_msr & AT91_MSR_MRDY &&
  551. ~reg_msr & AT91_MSR_MABT)))
  552. continue;
  553. /* _NOTE_: subtract AT91_MB_TX_FIRST offset from mb! */
  554. dev->stats.tx_bytes +=
  555. can_get_echo_skb(dev, mb - get_mb_tx_first(priv), NULL);
  556. dev->stats.tx_packets++;
  557. }
  558. /* restart queue if we don't have a wrap around but restart if
  559. * we get a TX int for the last can frame directly before a
  560. * wrap around.
  561. */
  562. if ((priv->tx_head & get_head_mask(priv)) != 0 ||
  563. (priv->tx_tail & get_head_mask(priv)) == 0)
  564. netif_wake_queue(dev);
  565. }
  566. static void at91_irq_err_line(struct net_device *dev, const u32 reg_sr)
  567. {
  568. struct net_device_stats *stats = &dev->stats;
  569. enum can_state new_state, rx_state, tx_state;
  570. struct at91_priv *priv = netdev_priv(dev);
  571. struct can_berr_counter bec;
  572. struct sk_buff *skb;
  573. struct can_frame *cf;
  574. u32 timestamp;
  575. int err;
  576. at91_get_berr_counter(dev, &bec);
  577. can_state_get_by_berr_counter(dev, &bec, &tx_state, &rx_state);
  578. /* The chip automatically recovers from bus-off after 128
  579. * occurrences of 11 consecutive recessive bits.
  580. *
  581. * After an auto-recovered bus-off, the error counters no
  582. * longer reflect this fact. On the sam9263 the state bits in
  583. * the SR register show the current state (based on the
  584. * current error counters), while on sam9x5 and newer SoCs
  585. * these bits are latched.
  586. *
  587. * Take any latched bus-off information from the SR register
  588. * into account when calculating the CAN new state, to start
  589. * the standard CAN bus off handling.
  590. */
  591. if (reg_sr & AT91_IRQ_BOFF)
  592. rx_state = CAN_STATE_BUS_OFF;
  593. new_state = max(tx_state, rx_state);
  594. /* state hasn't changed */
  595. if (likely(new_state == priv->can.state))
  596. return;
  597. /* The skb allocation might fail, but can_change_state()
  598. * handles cf == NULL.
  599. */
  600. skb = at91_alloc_can_err_skb(dev, &cf, &timestamp);
  601. can_change_state(dev, cf, tx_state, rx_state);
  602. if (new_state == CAN_STATE_BUS_OFF) {
  603. at91_chip_stop(dev, CAN_STATE_BUS_OFF);
  604. can_bus_off(dev);
  605. }
  606. if (unlikely(!skb))
  607. return;
  608. if (new_state != CAN_STATE_BUS_OFF) {
  609. cf->can_id |= CAN_ERR_CNT;
  610. cf->data[6] = bec.txerr;
  611. cf->data[7] = bec.rxerr;
  612. }
  613. err = can_rx_offload_queue_timestamp(&priv->offload, skb, timestamp);
  614. if (err)
  615. stats->rx_fifo_errors++;
  616. }
  617. static void at91_irq_err_frame(struct net_device *dev, const u32 reg_sr)
  618. {
  619. struct net_device_stats *stats = &dev->stats;
  620. struct at91_priv *priv = netdev_priv(dev);
  621. struct can_frame *cf;
  622. struct sk_buff *skb;
  623. u32 timestamp;
  624. int err;
  625. priv->can.can_stats.bus_error++;
  626. skb = at91_alloc_can_err_skb(dev, &cf, &timestamp);
  627. if (cf)
  628. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  629. if (reg_sr & AT91_IRQ_CERR) {
  630. netdev_dbg(dev, "CRC error\n");
  631. stats->rx_errors++;
  632. if (cf)
  633. cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
  634. }
  635. if (reg_sr & AT91_IRQ_SERR) {
  636. netdev_dbg(dev, "Stuff error\n");
  637. stats->rx_errors++;
  638. if (cf)
  639. cf->data[2] |= CAN_ERR_PROT_STUFF;
  640. }
  641. if (reg_sr & AT91_IRQ_AERR) {
  642. netdev_dbg(dev, "NACK error\n");
  643. stats->tx_errors++;
  644. if (cf) {
  645. cf->can_id |= CAN_ERR_ACK;
  646. cf->data[2] |= CAN_ERR_PROT_TX;
  647. }
  648. }
  649. if (reg_sr & AT91_IRQ_FERR) {
  650. netdev_dbg(dev, "Format error\n");
  651. stats->rx_errors++;
  652. if (cf)
  653. cf->data[2] |= CAN_ERR_PROT_FORM;
  654. }
  655. if (reg_sr & AT91_IRQ_BERR) {
  656. netdev_dbg(dev, "Bit error\n");
  657. stats->tx_errors++;
  658. if (cf)
  659. cf->data[2] |= CAN_ERR_PROT_TX | CAN_ERR_PROT_BIT;
  660. }
  661. if (!cf)
  662. return;
  663. err = can_rx_offload_queue_timestamp(&priv->offload, skb, timestamp);
  664. if (err)
  665. stats->rx_fifo_errors++;
  666. }
  667. static u32 at91_get_reg_sr_rx(const struct at91_priv *priv, u32 *reg_sr_p)
  668. {
  669. const u32 reg_sr = at91_read(priv, AT91_SR);
  670. *reg_sr_p |= reg_sr;
  671. return reg_sr & get_irq_mb_rx(priv);
  672. }
  673. static irqreturn_t at91_irq(int irq, void *dev_id)
  674. {
  675. struct net_device *dev = dev_id;
  676. struct at91_priv *priv = netdev_priv(dev);
  677. irqreturn_t handled = IRQ_NONE;
  678. u32 reg_sr = 0, reg_sr_rx;
  679. int ret;
  680. /* Receive interrupt
  681. * Some bits of AT91_SR are cleared on read, keep them in reg_sr.
  682. */
  683. while ((reg_sr_rx = at91_get_reg_sr_rx(priv, &reg_sr))) {
  684. ret = can_rx_offload_irq_offload_timestamp(&priv->offload,
  685. reg_sr_rx);
  686. handled = IRQ_HANDLED;
  687. if (!ret)
  688. break;
  689. }
  690. /* Transmission complete interrupt */
  691. if (reg_sr & get_irq_mb_tx(priv)) {
  692. at91_irq_tx(dev, reg_sr);
  693. handled = IRQ_HANDLED;
  694. }
  695. /* Line Error interrupt */
  696. if (reg_sr & AT91_IRQ_ERR_LINE ||
  697. priv->can.state > CAN_STATE_ERROR_ACTIVE) {
  698. at91_irq_err_line(dev, reg_sr);
  699. handled = IRQ_HANDLED;
  700. }
  701. /* Frame Error Interrupt */
  702. if (reg_sr & AT91_IRQ_ERR_FRAME) {
  703. at91_irq_err_frame(dev, reg_sr);
  704. handled = IRQ_HANDLED;
  705. }
  706. if (handled)
  707. can_rx_offload_irq_finish(&priv->offload);
  708. return handled;
  709. }
  710. static int at91_open(struct net_device *dev)
  711. {
  712. struct at91_priv *priv = netdev_priv(dev);
  713. int err;
  714. err = phy_power_on(priv->transceiver);
  715. if (err)
  716. return err;
  717. /* check or determine and set bittime */
  718. err = open_candev(dev);
  719. if (err)
  720. goto out_phy_power_off;
  721. err = clk_prepare_enable(priv->clk);
  722. if (err)
  723. goto out_close_candev;
  724. /* register interrupt handler */
  725. err = request_irq(dev->irq, at91_irq, IRQF_SHARED,
  726. dev->name, dev);
  727. if (err)
  728. goto out_clock_disable_unprepare;
  729. /* start chip and queuing */
  730. at91_chip_start(dev);
  731. can_rx_offload_enable(&priv->offload);
  732. netif_start_queue(dev);
  733. return 0;
  734. out_clock_disable_unprepare:
  735. clk_disable_unprepare(priv->clk);
  736. out_close_candev:
  737. close_candev(dev);
  738. out_phy_power_off:
  739. phy_power_off(priv->transceiver);
  740. return err;
  741. }
  742. /* stop CAN bus activity
  743. */
  744. static int at91_close(struct net_device *dev)
  745. {
  746. struct at91_priv *priv = netdev_priv(dev);
  747. netif_stop_queue(dev);
  748. can_rx_offload_disable(&priv->offload);
  749. at91_chip_stop(dev, CAN_STATE_STOPPED);
  750. free_irq(dev->irq, dev);
  751. clk_disable_unprepare(priv->clk);
  752. phy_power_off(priv->transceiver);
  753. close_candev(dev);
  754. return 0;
  755. }
  756. static int at91_set_mode(struct net_device *dev, enum can_mode mode)
  757. {
  758. switch (mode) {
  759. case CAN_MODE_START:
  760. at91_chip_start(dev);
  761. netif_wake_queue(dev);
  762. break;
  763. default:
  764. return -EOPNOTSUPP;
  765. }
  766. return 0;
  767. }
  768. static const struct net_device_ops at91_netdev_ops = {
  769. .ndo_open = at91_open,
  770. .ndo_stop = at91_close,
  771. .ndo_start_xmit = at91_start_xmit,
  772. .ndo_change_mtu = can_change_mtu,
  773. };
  774. static const struct ethtool_ops at91_ethtool_ops = {
  775. .get_ts_info = ethtool_op_get_ts_info,
  776. };
  777. static ssize_t mb0_id_show(struct device *dev,
  778. struct device_attribute *attr, char *buf)
  779. {
  780. struct at91_priv *priv = netdev_priv(to_net_dev(dev));
  781. if (priv->mb0_id & CAN_EFF_FLAG)
  782. return sysfs_emit(buf, "0x%08x\n", priv->mb0_id);
  783. else
  784. return sysfs_emit(buf, "0x%03x\n", priv->mb0_id);
  785. }
  786. static ssize_t mb0_id_store(struct device *dev,
  787. struct device_attribute *attr,
  788. const char *buf, size_t count)
  789. {
  790. struct net_device *ndev = to_net_dev(dev);
  791. struct at91_priv *priv = netdev_priv(ndev);
  792. unsigned long can_id;
  793. ssize_t ret;
  794. int err;
  795. rtnl_lock();
  796. if (ndev->flags & IFF_UP) {
  797. ret = -EBUSY;
  798. goto out;
  799. }
  800. err = kstrtoul(buf, 0, &can_id);
  801. if (err) {
  802. ret = err;
  803. goto out;
  804. }
  805. if (can_id & CAN_EFF_FLAG)
  806. can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
  807. else
  808. can_id &= CAN_SFF_MASK;
  809. priv->mb0_id = can_id;
  810. ret = count;
  811. out:
  812. rtnl_unlock();
  813. return ret;
  814. }
  815. static DEVICE_ATTR_RW(mb0_id);
  816. static struct attribute *at91_sysfs_attrs[] = {
  817. &dev_attr_mb0_id.attr,
  818. NULL,
  819. };
  820. static const struct attribute_group at91_sysfs_attr_group = {
  821. .attrs = at91_sysfs_attrs,
  822. };
  823. #if defined(CONFIG_OF)
  824. static const struct of_device_id at91_can_dt_ids[] = {
  825. {
  826. .compatible = "atmel,at91sam9x5-can",
  827. .data = &at91_at91sam9x5_data,
  828. }, {
  829. .compatible = "atmel,at91sam9263-can",
  830. .data = &at91_at91sam9263_data,
  831. }, {
  832. /* sentinel */
  833. }
  834. };
  835. MODULE_DEVICE_TABLE(of, at91_can_dt_ids);
  836. #endif
  837. static const struct at91_devtype_data *at91_can_get_driver_data(struct platform_device *pdev)
  838. {
  839. if (pdev->dev.of_node) {
  840. const struct of_device_id *match;
  841. match = of_match_node(at91_can_dt_ids, pdev->dev.of_node);
  842. if (!match) {
  843. dev_err(&pdev->dev, "no matching node found in dtb\n");
  844. return NULL;
  845. }
  846. return (const struct at91_devtype_data *)match->data;
  847. }
  848. return (const struct at91_devtype_data *)
  849. platform_get_device_id(pdev)->driver_data;
  850. }
  851. static int at91_can_probe(struct platform_device *pdev)
  852. {
  853. const struct at91_devtype_data *devtype_data;
  854. struct phy *transceiver;
  855. struct net_device *dev;
  856. struct at91_priv *priv;
  857. struct resource *res;
  858. struct clk *clk;
  859. void __iomem *addr;
  860. int err, irq;
  861. devtype_data = at91_can_get_driver_data(pdev);
  862. if (!devtype_data) {
  863. dev_err(&pdev->dev, "no driver data\n");
  864. err = -ENODEV;
  865. goto exit;
  866. }
  867. clk = clk_get(&pdev->dev, "can_clk");
  868. if (IS_ERR(clk)) {
  869. dev_err(&pdev->dev, "no clock defined\n");
  870. err = -ENODEV;
  871. goto exit;
  872. }
  873. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  874. irq = platform_get_irq(pdev, 0);
  875. if (!res || irq <= 0) {
  876. err = -ENODEV;
  877. goto exit_put;
  878. }
  879. if (!request_mem_region(res->start,
  880. resource_size(res),
  881. pdev->name)) {
  882. err = -EBUSY;
  883. goto exit_put;
  884. }
  885. addr = ioremap(res->start, resource_size(res));
  886. if (!addr) {
  887. err = -ENOMEM;
  888. goto exit_release;
  889. }
  890. dev = alloc_candev(sizeof(struct at91_priv),
  891. 1 << devtype_data->tx_shift);
  892. if (!dev) {
  893. err = -ENOMEM;
  894. goto exit_iounmap;
  895. }
  896. transceiver = devm_phy_optional_get(&pdev->dev, NULL);
  897. if (IS_ERR(transceiver)) {
  898. err = PTR_ERR(transceiver);
  899. dev_err_probe(&pdev->dev, err, "failed to get phy\n");
  900. goto exit_iounmap;
  901. }
  902. dev->netdev_ops = &at91_netdev_ops;
  903. dev->ethtool_ops = &at91_ethtool_ops;
  904. dev->irq = irq;
  905. dev->flags |= IFF_ECHO;
  906. priv = netdev_priv(dev);
  907. priv->can.clock.freq = clk_get_rate(clk);
  908. priv->can.bittiming_const = &at91_bittiming_const;
  909. priv->can.do_set_mode = at91_set_mode;
  910. priv->can.do_get_berr_counter = at91_get_berr_counter;
  911. priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES |
  912. CAN_CTRLMODE_LISTENONLY;
  913. priv->reg_base = addr;
  914. priv->devtype_data = *devtype_data;
  915. priv->clk = clk;
  916. priv->pdata = dev_get_platdata(&pdev->dev);
  917. priv->mb0_id = 0x7ff;
  918. priv->offload.mailbox_read = at91_mailbox_read;
  919. priv->offload.mb_first = devtype_data->rx_first;
  920. priv->offload.mb_last = devtype_data->rx_last;
  921. can_rx_offload_add_timestamp(dev, &priv->offload);
  922. if (transceiver)
  923. priv->can.bitrate_max = transceiver->attrs.max_link_rate;
  924. if (at91_is_sam9263(priv))
  925. dev->sysfs_groups[0] = &at91_sysfs_attr_group;
  926. platform_set_drvdata(pdev, dev);
  927. SET_NETDEV_DEV(dev, &pdev->dev);
  928. err = register_candev(dev);
  929. if (err) {
  930. dev_err(&pdev->dev, "registering netdev failed\n");
  931. goto exit_free;
  932. }
  933. dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
  934. priv->reg_base, dev->irq);
  935. return 0;
  936. exit_free:
  937. free_candev(dev);
  938. exit_iounmap:
  939. iounmap(addr);
  940. exit_release:
  941. release_mem_region(res->start, resource_size(res));
  942. exit_put:
  943. clk_put(clk);
  944. exit:
  945. return err;
  946. }
  947. static void at91_can_remove(struct platform_device *pdev)
  948. {
  949. struct net_device *dev = platform_get_drvdata(pdev);
  950. struct at91_priv *priv = netdev_priv(dev);
  951. struct resource *res;
  952. unregister_netdev(dev);
  953. iounmap(priv->reg_base);
  954. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  955. release_mem_region(res->start, resource_size(res));
  956. clk_put(priv->clk);
  957. free_candev(dev);
  958. }
  959. static const struct platform_device_id at91_can_id_table[] = {
  960. {
  961. .name = "at91sam9x5_can",
  962. .driver_data = (kernel_ulong_t)&at91_at91sam9x5_data,
  963. }, {
  964. .name = "at91_can",
  965. .driver_data = (kernel_ulong_t)&at91_at91sam9263_data,
  966. }, {
  967. /* sentinel */
  968. }
  969. };
  970. MODULE_DEVICE_TABLE(platform, at91_can_id_table);
  971. static struct platform_driver at91_can_driver = {
  972. .probe = at91_can_probe,
  973. .remove = at91_can_remove,
  974. .driver = {
  975. .name = KBUILD_MODNAME,
  976. .of_match_table = of_match_ptr(at91_can_dt_ids),
  977. },
  978. .id_table = at91_can_id_table,
  979. };
  980. module_platform_driver(at91_can_driver);
  981. MODULE_AUTHOR("Marc Kleine-Budde <mkl@pengutronix.de>");
  982. MODULE_LICENSE("GPL v2");
  983. MODULE_DESCRIPTION(KBUILD_MODNAME " CAN netdevice driver");