at91_can.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  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 by Marc Kleine-Budde <kernel@pengutronix.de>
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/errno.h>
  10. #include <linux/if_arp.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/rtnetlink.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/string.h>
  21. #include <linux/types.h>
  22. #include <linux/can/dev.h>
  23. #include <linux/can/error.h>
  24. #include <linux/can/led.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 (1 << 16)
  78. #define AT91_IRQ_WARN (1 << 17)
  79. #define AT91_IRQ_ERRP (1 << 18)
  80. #define AT91_IRQ_BOFF (1 << 19)
  81. #define AT91_IRQ_SLEEP (1 << 20)
  82. #define AT91_IRQ_WAKEUP (1 << 21)
  83. #define AT91_IRQ_TOVF (1 << 22)
  84. #define AT91_IRQ_TSTP (1 << 23)
  85. #define AT91_IRQ_CERR (1 << 24)
  86. #define AT91_IRQ_SERR (1 << 25)
  87. #define AT91_IRQ_AERR (1 << 26)
  88. #define AT91_IRQ_FERR (1 << 27)
  89. #define AT91_IRQ_BERR (1 << 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 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, int prio)
  242. {
  243. at91_write(priv, AT91_MMR(mb), (mode << 24) | (prio << 16));
  244. }
  245. static inline void set_mb_mode(const struct at91_priv *priv, unsigned int mb,
  246. enum at91_mb_mode mode)
  247. {
  248. set_mb_mode_prio(priv, mb, mode, 0);
  249. }
  250. static inline u32 at91_can_id_to_reg_mid(canid_t can_id)
  251. {
  252. u32 reg_mid;
  253. if (can_id & CAN_EFF_FLAG)
  254. reg_mid = (can_id & CAN_EFF_MASK) | AT91_MID_MIDE;
  255. else
  256. reg_mid = (can_id & CAN_SFF_MASK) << 18;
  257. return reg_mid;
  258. }
  259. static void at91_setup_mailboxes(struct net_device *dev)
  260. {
  261. struct at91_priv *priv = netdev_priv(dev);
  262. unsigned int i;
  263. u32 reg_mid;
  264. /*
  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. /*
  348. * theory of operation:
  349. *
  350. * According to the datasheet priority 0 is the highest priority, 15
  351. * is the lowest. If two mailboxes have the same priority level the
  352. * message of the mailbox with the lowest number is sent first.
  353. *
  354. * We use the first TX mailbox (AT91_MB_TX_FIRST) with prio 0, then
  355. * the next mailbox with prio 0, and so on, until all mailboxes are
  356. * used. Then we start from the beginning with mailbox
  357. * AT91_MB_TX_FIRST, but with prio 1, mailbox AT91_MB_TX_FIRST + 1
  358. * prio 1. When we reach the last mailbox with prio 15, we have to
  359. * stop sending, waiting for all messages to be delivered, then start
  360. * again with mailbox AT91_MB_TX_FIRST prio 0.
  361. *
  362. * We use the priv->tx_next as counter for the next transmission
  363. * mailbox, but without the offset AT91_MB_TX_FIRST. The lower bits
  364. * encode the mailbox number, the upper 4 bits the mailbox priority:
  365. *
  366. * priv->tx_next = (prio << get_next_prio_shift(priv)) |
  367. * (mb - get_mb_tx_first(priv));
  368. *
  369. */
  370. static netdev_tx_t at91_start_xmit(struct sk_buff *skb, struct net_device *dev)
  371. {
  372. struct at91_priv *priv = netdev_priv(dev);
  373. struct net_device_stats *stats = &dev->stats;
  374. struct can_frame *cf = (struct can_frame *)skb->data;
  375. unsigned int mb, prio;
  376. u32 reg_mid, reg_mcr;
  377. if (can_dropped_invalid_skb(dev, skb))
  378. return NETDEV_TX_OK;
  379. mb = get_tx_next_mb(priv);
  380. prio = get_tx_next_prio(priv);
  381. if (unlikely(!(at91_read(priv, AT91_MSR(mb)) & AT91_MSR_MRDY))) {
  382. netif_stop_queue(dev);
  383. netdev_err(dev, "BUG! TX buffer full when queue awake!\n");
  384. return NETDEV_TX_BUSY;
  385. }
  386. reg_mid = at91_can_id_to_reg_mid(cf->can_id);
  387. reg_mcr = ((cf->can_id & CAN_RTR_FLAG) ? AT91_MCR_MRTR : 0) |
  388. (cf->can_dlc << 16) | AT91_MCR_MTCR;
  389. /* disable MB while writing ID (see datasheet) */
  390. set_mb_mode(priv, mb, AT91_MB_MODE_DISABLED);
  391. at91_write(priv, AT91_MID(mb), reg_mid);
  392. set_mb_mode_prio(priv, mb, AT91_MB_MODE_TX, prio);
  393. at91_write(priv, AT91_MDL(mb), *(u32 *)(cf->data + 0));
  394. at91_write(priv, AT91_MDH(mb), *(u32 *)(cf->data + 4));
  395. /* This triggers transmission */
  396. at91_write(priv, AT91_MCR(mb), reg_mcr);
  397. stats->tx_bytes += cf->can_dlc;
  398. /* _NOTE_: subtract AT91_MB_TX_FIRST offset from mb! */
  399. can_put_echo_skb(skb, dev, mb - get_mb_tx_first(priv));
  400. /*
  401. * we have to stop the queue and deliver all messages in case
  402. * of a prio+mb counter wrap around. This is the case if
  403. * tx_next buffer prio and mailbox equals 0.
  404. *
  405. * also stop the queue if next buffer is still in use
  406. * (== not ready)
  407. */
  408. priv->tx_next++;
  409. if (!(at91_read(priv, AT91_MSR(get_tx_next_mb(priv))) &
  410. AT91_MSR_MRDY) ||
  411. (priv->tx_next & get_next_mask(priv)) == 0)
  412. netif_stop_queue(dev);
  413. /* Enable interrupt for this mailbox */
  414. at91_write(priv, AT91_IER, 1 << mb);
  415. return NETDEV_TX_OK;
  416. }
  417. /**
  418. * at91_activate_rx_low - activate lower rx mailboxes
  419. * @priv: a91 context
  420. *
  421. * Reenables the lower mailboxes for reception of new CAN messages
  422. */
  423. static inline void at91_activate_rx_low(const struct at91_priv *priv)
  424. {
  425. u32 mask = get_mb_rx_low_mask(priv);
  426. at91_write(priv, AT91_TCR, mask);
  427. }
  428. /**
  429. * at91_activate_rx_mb - reactive single rx mailbox
  430. * @priv: a91 context
  431. * @mb: mailbox to reactivate
  432. *
  433. * Reenables given mailbox for reception of new CAN messages
  434. */
  435. static inline void at91_activate_rx_mb(const struct at91_priv *priv,
  436. unsigned int mb)
  437. {
  438. u32 mask = 1 << mb;
  439. at91_write(priv, AT91_TCR, mask);
  440. }
  441. /**
  442. * at91_rx_overflow_err - send error frame due to rx overflow
  443. * @dev: net device
  444. */
  445. static void at91_rx_overflow_err(struct net_device *dev)
  446. {
  447. struct net_device_stats *stats = &dev->stats;
  448. struct sk_buff *skb;
  449. struct can_frame *cf;
  450. netdev_dbg(dev, "RX buffer overflow\n");
  451. stats->rx_over_errors++;
  452. stats->rx_errors++;
  453. skb = alloc_can_err_skb(dev, &cf);
  454. if (unlikely(!skb))
  455. return;
  456. cf->can_id |= CAN_ERR_CRTL;
  457. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  458. stats->rx_packets++;
  459. stats->rx_bytes += cf->can_dlc;
  460. netif_receive_skb(skb);
  461. }
  462. /**
  463. * at91_read_mb - read CAN msg from mailbox (lowlevel impl)
  464. * @dev: net device
  465. * @mb: mailbox number to read from
  466. * @cf: can frame where to store message
  467. *
  468. * Reads a CAN message from the given mailbox and stores data into
  469. * given can frame. "mb" and "cf" must be valid.
  470. */
  471. static void at91_read_mb(struct net_device *dev, unsigned int mb,
  472. struct can_frame *cf)
  473. {
  474. const struct at91_priv *priv = netdev_priv(dev);
  475. u32 reg_msr, reg_mid;
  476. reg_mid = at91_read(priv, AT91_MID(mb));
  477. if (reg_mid & AT91_MID_MIDE)
  478. cf->can_id = ((reg_mid >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG;
  479. else
  480. cf->can_id = (reg_mid >> 18) & CAN_SFF_MASK;
  481. reg_msr = at91_read(priv, AT91_MSR(mb));
  482. cf->can_dlc = get_can_dlc((reg_msr >> 16) & 0xf);
  483. if (reg_msr & AT91_MSR_MRTR)
  484. cf->can_id |= CAN_RTR_FLAG;
  485. else {
  486. *(u32 *)(cf->data + 0) = at91_read(priv, AT91_MDL(mb));
  487. *(u32 *)(cf->data + 4) = at91_read(priv, AT91_MDH(mb));
  488. }
  489. /* allow RX of extended frames */
  490. at91_write(priv, AT91_MID(mb), AT91_MID_MIDE);
  491. if (unlikely(mb == get_mb_rx_last(priv) && reg_msr & AT91_MSR_MMI))
  492. at91_rx_overflow_err(dev);
  493. }
  494. /**
  495. * at91_read_msg - read CAN message from mailbox
  496. * @dev: net device
  497. * @mb: mail box to read from
  498. *
  499. * Reads a CAN message from given mailbox, and put into linux network
  500. * RX queue, does all housekeeping chores (stats, ...)
  501. */
  502. static void at91_read_msg(struct net_device *dev, unsigned int mb)
  503. {
  504. struct net_device_stats *stats = &dev->stats;
  505. struct can_frame *cf;
  506. struct sk_buff *skb;
  507. skb = alloc_can_skb(dev, &cf);
  508. if (unlikely(!skb)) {
  509. stats->rx_dropped++;
  510. return;
  511. }
  512. at91_read_mb(dev, mb, cf);
  513. stats->rx_packets++;
  514. stats->rx_bytes += cf->can_dlc;
  515. netif_receive_skb(skb);
  516. can_led_event(dev, CAN_LED_EVENT_RX);
  517. }
  518. /**
  519. * at91_poll_rx - read multiple CAN messages from mailboxes
  520. * @dev: net device
  521. * @quota: max number of pkgs we're allowed to receive
  522. *
  523. * Theory of Operation:
  524. *
  525. * About 3/4 of the mailboxes (get_mb_rx_first()...get_mb_rx_last())
  526. * on the chip are reserved for RX. We split them into 2 groups. The
  527. * lower group ranges from get_mb_rx_first() to get_mb_rx_low_last().
  528. *
  529. * Like it or not, but the chip always saves a received CAN message
  530. * into the first free mailbox it finds (starting with the
  531. * lowest). This makes it very difficult to read the messages in the
  532. * right order from the chip. This is how we work around that problem:
  533. *
  534. * The first message goes into mb nr. 1 and issues an interrupt. All
  535. * rx ints are disabled in the interrupt handler and a napi poll is
  536. * scheduled. We read the mailbox, but do _not_ reenable the mb (to
  537. * receive another message).
  538. *
  539. * lower mbxs upper
  540. * ____^______ __^__
  541. * / \ / \
  542. * +-+-+-+-+-+-+-+-++-+-+-+-+
  543. * | |x|x|x|x|x|x|x|| | | | |
  544. * +-+-+-+-+-+-+-+-++-+-+-+-+
  545. * 0 0 0 0 0 0 0 0 0 0 1 1 \ mail
  546. * 0 1 2 3 4 5 6 7 8 9 0 1 / box
  547. * ^
  548. * |
  549. * \
  550. * unused, due to chip bug
  551. *
  552. * The variable priv->rx_next points to the next mailbox to read a
  553. * message from. As long we're in the lower mailboxes we just read the
  554. * mailbox but not reenable it.
  555. *
  556. * With completion of the last of the lower mailboxes, we reenable the
  557. * whole first group, but continue to look for filled mailboxes in the
  558. * upper mailboxes. Imagine the second group like overflow mailboxes,
  559. * which takes CAN messages if the lower goup is full. While in the
  560. * upper group we reenable the mailbox right after reading it. Giving
  561. * the chip more room to store messages.
  562. *
  563. * After finishing we look again in the lower group if we've still
  564. * quota.
  565. *
  566. */
  567. static int at91_poll_rx(struct net_device *dev, int quota)
  568. {
  569. struct at91_priv *priv = netdev_priv(dev);
  570. u32 reg_sr = at91_read(priv, AT91_SR);
  571. const unsigned long *addr = (unsigned long *)&reg_sr;
  572. unsigned int mb;
  573. int received = 0;
  574. if (priv->rx_next > get_mb_rx_low_last(priv) &&
  575. reg_sr & get_mb_rx_low_mask(priv))
  576. netdev_info(dev,
  577. "order of incoming frames cannot be guaranteed\n");
  578. again:
  579. for (mb = find_next_bit(addr, get_mb_tx_first(priv), priv->rx_next);
  580. mb < get_mb_tx_first(priv) && quota > 0;
  581. reg_sr = at91_read(priv, AT91_SR),
  582. mb = find_next_bit(addr, get_mb_tx_first(priv), ++priv->rx_next)) {
  583. at91_read_msg(dev, mb);
  584. /* reactivate mailboxes */
  585. if (mb == get_mb_rx_low_last(priv))
  586. /* all lower mailboxed, if just finished it */
  587. at91_activate_rx_low(priv);
  588. else if (mb > get_mb_rx_low_last(priv))
  589. /* only the mailbox we read */
  590. at91_activate_rx_mb(priv, mb);
  591. received++;
  592. quota--;
  593. }
  594. /* upper group completed, look again in lower */
  595. if (priv->rx_next > get_mb_rx_low_last(priv) &&
  596. mb > get_mb_rx_last(priv)) {
  597. priv->rx_next = get_mb_rx_first(priv);
  598. if (quota > 0)
  599. goto again;
  600. }
  601. return received;
  602. }
  603. static void at91_poll_err_frame(struct net_device *dev,
  604. struct can_frame *cf, u32 reg_sr)
  605. {
  606. struct at91_priv *priv = netdev_priv(dev);
  607. /* CRC error */
  608. if (reg_sr & AT91_IRQ_CERR) {
  609. netdev_dbg(dev, "CERR irq\n");
  610. dev->stats.rx_errors++;
  611. priv->can.can_stats.bus_error++;
  612. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  613. }
  614. /* Stuffing Error */
  615. if (reg_sr & AT91_IRQ_SERR) {
  616. netdev_dbg(dev, "SERR irq\n");
  617. dev->stats.rx_errors++;
  618. priv->can.can_stats.bus_error++;
  619. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  620. cf->data[2] |= CAN_ERR_PROT_STUFF;
  621. }
  622. /* Acknowledgement Error */
  623. if (reg_sr & AT91_IRQ_AERR) {
  624. netdev_dbg(dev, "AERR irq\n");
  625. dev->stats.tx_errors++;
  626. cf->can_id |= CAN_ERR_ACK;
  627. }
  628. /* Form error */
  629. if (reg_sr & AT91_IRQ_FERR) {
  630. netdev_dbg(dev, "FERR irq\n");
  631. dev->stats.rx_errors++;
  632. priv->can.can_stats.bus_error++;
  633. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  634. cf->data[2] |= CAN_ERR_PROT_FORM;
  635. }
  636. /* Bit Error */
  637. if (reg_sr & AT91_IRQ_BERR) {
  638. netdev_dbg(dev, "BERR irq\n");
  639. dev->stats.tx_errors++;
  640. priv->can.can_stats.bus_error++;
  641. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  642. cf->data[2] |= CAN_ERR_PROT_BIT;
  643. }
  644. }
  645. static int at91_poll_err(struct net_device *dev, int quota, u32 reg_sr)
  646. {
  647. struct sk_buff *skb;
  648. struct can_frame *cf;
  649. if (quota == 0)
  650. return 0;
  651. skb = alloc_can_err_skb(dev, &cf);
  652. if (unlikely(!skb))
  653. return 0;
  654. at91_poll_err_frame(dev, cf, reg_sr);
  655. dev->stats.rx_packets++;
  656. dev->stats.rx_bytes += cf->can_dlc;
  657. netif_receive_skb(skb);
  658. return 1;
  659. }
  660. static int at91_poll(struct napi_struct *napi, int quota)
  661. {
  662. struct net_device *dev = napi->dev;
  663. const struct at91_priv *priv = netdev_priv(dev);
  664. u32 reg_sr = at91_read(priv, AT91_SR);
  665. int work_done = 0;
  666. if (reg_sr & get_irq_mb_rx(priv))
  667. work_done += at91_poll_rx(dev, quota - work_done);
  668. /*
  669. * The error bits are clear on read,
  670. * so use saved value from irq handler.
  671. */
  672. reg_sr |= priv->reg_sr;
  673. if (reg_sr & AT91_IRQ_ERR_FRAME)
  674. work_done += at91_poll_err(dev, quota - work_done, reg_sr);
  675. if (work_done < quota) {
  676. /* enable IRQs for frame errors and all mailboxes >= rx_next */
  677. u32 reg_ier = AT91_IRQ_ERR_FRAME;
  678. reg_ier |= get_irq_mb_rx(priv) & ~AT91_MB_MASK(priv->rx_next);
  679. napi_complete_done(napi, work_done);
  680. at91_write(priv, AT91_IER, reg_ier);
  681. }
  682. return work_done;
  683. }
  684. /*
  685. * theory of operation:
  686. *
  687. * priv->tx_echo holds the number of the oldest can_frame put for
  688. * transmission into the hardware, but not yet ACKed by the CAN tx
  689. * complete IRQ.
  690. *
  691. * We iterate from priv->tx_echo to priv->tx_next and check if the
  692. * packet has been transmitted, echo it back to the CAN framework. If
  693. * we discover a not yet transmitted package, stop looking for more.
  694. *
  695. */
  696. static void at91_irq_tx(struct net_device *dev, u32 reg_sr)
  697. {
  698. struct at91_priv *priv = netdev_priv(dev);
  699. u32 reg_msr;
  700. unsigned int mb;
  701. /* masking of reg_sr not needed, already done by at91_irq */
  702. for (/* nix */; (priv->tx_next - priv->tx_echo) > 0; priv->tx_echo++) {
  703. mb = get_tx_echo_mb(priv);
  704. /* no event in mailbox? */
  705. if (!(reg_sr & (1 << mb)))
  706. break;
  707. /* Disable irq for this TX mailbox */
  708. at91_write(priv, AT91_IDR, 1 << mb);
  709. /*
  710. * only echo if mailbox signals us a transfer
  711. * complete (MSR_MRDY). Otherwise it's a tansfer
  712. * abort. "can_bus_off()" takes care about the skbs
  713. * parked in the echo queue.
  714. */
  715. reg_msr = at91_read(priv, AT91_MSR(mb));
  716. if (likely(reg_msr & AT91_MSR_MRDY &&
  717. ~reg_msr & AT91_MSR_MABT)) {
  718. /* _NOTE_: subtract AT91_MB_TX_FIRST offset from mb! */
  719. can_get_echo_skb(dev, mb - get_mb_tx_first(priv));
  720. dev->stats.tx_packets++;
  721. can_led_event(dev, CAN_LED_EVENT_TX);
  722. }
  723. }
  724. /*
  725. * restart queue if we don't have a wrap around but restart if
  726. * we get a TX int for the last can frame directly before a
  727. * wrap around.
  728. */
  729. if ((priv->tx_next & get_next_mask(priv)) != 0 ||
  730. (priv->tx_echo & get_next_mask(priv)) == 0)
  731. netif_wake_queue(dev);
  732. }
  733. static void at91_irq_err_state(struct net_device *dev,
  734. struct can_frame *cf, enum can_state new_state)
  735. {
  736. struct at91_priv *priv = netdev_priv(dev);
  737. u32 reg_idr = 0, reg_ier = 0;
  738. struct can_berr_counter bec;
  739. at91_get_berr_counter(dev, &bec);
  740. switch (priv->can.state) {
  741. case CAN_STATE_ERROR_ACTIVE:
  742. /*
  743. * from: ERROR_ACTIVE
  744. * to : ERROR_WARNING, ERROR_PASSIVE, BUS_OFF
  745. * => : there was a warning int
  746. */
  747. if (new_state >= CAN_STATE_ERROR_WARNING &&
  748. new_state <= CAN_STATE_BUS_OFF) {
  749. netdev_dbg(dev, "Error Warning IRQ\n");
  750. priv->can.can_stats.error_warning++;
  751. cf->can_id |= CAN_ERR_CRTL;
  752. cf->data[1] = (bec.txerr > bec.rxerr) ?
  753. CAN_ERR_CRTL_TX_WARNING :
  754. CAN_ERR_CRTL_RX_WARNING;
  755. }
  756. /* fall through */
  757. case CAN_STATE_ERROR_WARNING:
  758. /*
  759. * from: ERROR_ACTIVE, ERROR_WARNING
  760. * to : ERROR_PASSIVE, BUS_OFF
  761. * => : error passive int
  762. */
  763. if (new_state >= CAN_STATE_ERROR_PASSIVE &&
  764. new_state <= CAN_STATE_BUS_OFF) {
  765. netdev_dbg(dev, "Error Passive IRQ\n");
  766. priv->can.can_stats.error_passive++;
  767. cf->can_id |= CAN_ERR_CRTL;
  768. cf->data[1] = (bec.txerr > bec.rxerr) ?
  769. CAN_ERR_CRTL_TX_PASSIVE :
  770. CAN_ERR_CRTL_RX_PASSIVE;
  771. }
  772. break;
  773. case CAN_STATE_BUS_OFF:
  774. /*
  775. * from: BUS_OFF
  776. * to : ERROR_ACTIVE, ERROR_WARNING, ERROR_PASSIVE
  777. */
  778. if (new_state <= CAN_STATE_ERROR_PASSIVE) {
  779. cf->can_id |= CAN_ERR_RESTARTED;
  780. netdev_dbg(dev, "restarted\n");
  781. priv->can.can_stats.restarts++;
  782. netif_carrier_on(dev);
  783. netif_wake_queue(dev);
  784. }
  785. break;
  786. default:
  787. break;
  788. }
  789. /* process state changes depending on the new state */
  790. switch (new_state) {
  791. case CAN_STATE_ERROR_ACTIVE:
  792. /*
  793. * actually we want to enable AT91_IRQ_WARN here, but
  794. * it screws up the system under certain
  795. * circumstances. so just enable AT91_IRQ_ERRP, thus
  796. * the "fallthrough"
  797. */
  798. netdev_dbg(dev, "Error Active\n");
  799. cf->can_id |= CAN_ERR_PROT;
  800. cf->data[2] = CAN_ERR_PROT_ACTIVE;
  801. /* fall through */
  802. case CAN_STATE_ERROR_WARNING:
  803. reg_idr = AT91_IRQ_ERRA | AT91_IRQ_WARN | AT91_IRQ_BOFF;
  804. reg_ier = AT91_IRQ_ERRP;
  805. break;
  806. case CAN_STATE_ERROR_PASSIVE:
  807. reg_idr = AT91_IRQ_ERRA | AT91_IRQ_WARN | AT91_IRQ_ERRP;
  808. reg_ier = AT91_IRQ_BOFF;
  809. break;
  810. case CAN_STATE_BUS_OFF:
  811. reg_idr = AT91_IRQ_ERRA | AT91_IRQ_ERRP |
  812. AT91_IRQ_WARN | AT91_IRQ_BOFF;
  813. reg_ier = 0;
  814. cf->can_id |= CAN_ERR_BUSOFF;
  815. netdev_dbg(dev, "bus-off\n");
  816. netif_carrier_off(dev);
  817. priv->can.can_stats.bus_off++;
  818. /* turn off chip, if restart is disabled */
  819. if (!priv->can.restart_ms) {
  820. at91_chip_stop(dev, CAN_STATE_BUS_OFF);
  821. return;
  822. }
  823. break;
  824. default:
  825. break;
  826. }
  827. at91_write(priv, AT91_IDR, reg_idr);
  828. at91_write(priv, AT91_IER, reg_ier);
  829. }
  830. static int at91_get_state_by_bec(const struct net_device *dev,
  831. enum can_state *state)
  832. {
  833. struct can_berr_counter bec;
  834. int err;
  835. err = at91_get_berr_counter(dev, &bec);
  836. if (err)
  837. return err;
  838. if (bec.txerr < 96 && bec.rxerr < 96)
  839. *state = CAN_STATE_ERROR_ACTIVE;
  840. else if (bec.txerr < 128 && bec.rxerr < 128)
  841. *state = CAN_STATE_ERROR_WARNING;
  842. else if (bec.txerr < 256 && bec.rxerr < 256)
  843. *state = CAN_STATE_ERROR_PASSIVE;
  844. else
  845. *state = CAN_STATE_BUS_OFF;
  846. return 0;
  847. }
  848. static void at91_irq_err(struct net_device *dev)
  849. {
  850. struct at91_priv *priv = netdev_priv(dev);
  851. struct sk_buff *skb;
  852. struct can_frame *cf;
  853. enum can_state new_state;
  854. u32 reg_sr;
  855. int err;
  856. if (at91_is_sam9263(priv)) {
  857. reg_sr = at91_read(priv, AT91_SR);
  858. /* we need to look at the unmasked reg_sr */
  859. if (unlikely(reg_sr & AT91_IRQ_BOFF))
  860. new_state = CAN_STATE_BUS_OFF;
  861. else if (unlikely(reg_sr & AT91_IRQ_ERRP))
  862. new_state = CAN_STATE_ERROR_PASSIVE;
  863. else if (unlikely(reg_sr & AT91_IRQ_WARN))
  864. new_state = CAN_STATE_ERROR_WARNING;
  865. else if (likely(reg_sr & AT91_IRQ_ERRA))
  866. new_state = CAN_STATE_ERROR_ACTIVE;
  867. else {
  868. netdev_err(dev, "BUG! hardware in undefined state\n");
  869. return;
  870. }
  871. } else {
  872. err = at91_get_state_by_bec(dev, &new_state);
  873. if (err)
  874. return;
  875. }
  876. /* state hasn't changed */
  877. if (likely(new_state == priv->can.state))
  878. return;
  879. skb = alloc_can_err_skb(dev, &cf);
  880. if (unlikely(!skb))
  881. return;
  882. at91_irq_err_state(dev, cf, new_state);
  883. dev->stats.rx_packets++;
  884. dev->stats.rx_bytes += cf->can_dlc;
  885. netif_rx(skb);
  886. priv->can.state = new_state;
  887. }
  888. /*
  889. * interrupt handler
  890. */
  891. static irqreturn_t at91_irq(int irq, void *dev_id)
  892. {
  893. struct net_device *dev = dev_id;
  894. struct at91_priv *priv = netdev_priv(dev);
  895. irqreturn_t handled = IRQ_NONE;
  896. u32 reg_sr, reg_imr;
  897. reg_sr = at91_read(priv, AT91_SR);
  898. reg_imr = at91_read(priv, AT91_IMR);
  899. /* Ignore masked interrupts */
  900. reg_sr &= reg_imr;
  901. if (!reg_sr)
  902. goto exit;
  903. handled = IRQ_HANDLED;
  904. /* Receive or error interrupt? -> napi */
  905. if (reg_sr & (get_irq_mb_rx(priv) | AT91_IRQ_ERR_FRAME)) {
  906. /*
  907. * The error bits are clear on read,
  908. * save for later use.
  909. */
  910. priv->reg_sr = reg_sr;
  911. at91_write(priv, AT91_IDR,
  912. get_irq_mb_rx(priv) | AT91_IRQ_ERR_FRAME);
  913. napi_schedule(&priv->napi);
  914. }
  915. /* Transmission complete interrupt */
  916. if (reg_sr & get_irq_mb_tx(priv))
  917. at91_irq_tx(dev, reg_sr);
  918. at91_irq_err(dev);
  919. exit:
  920. return handled;
  921. }
  922. static int at91_open(struct net_device *dev)
  923. {
  924. struct at91_priv *priv = netdev_priv(dev);
  925. int err;
  926. err = clk_prepare_enable(priv->clk);
  927. if (err)
  928. return err;
  929. /* check or determine and set bittime */
  930. err = open_candev(dev);
  931. if (err)
  932. goto out;
  933. /* register interrupt handler */
  934. if (request_irq(dev->irq, at91_irq, IRQF_SHARED,
  935. dev->name, dev)) {
  936. err = -EAGAIN;
  937. goto out_close;
  938. }
  939. can_led_event(dev, CAN_LED_EVENT_OPEN);
  940. /* start chip and queuing */
  941. at91_chip_start(dev);
  942. napi_enable(&priv->napi);
  943. netif_start_queue(dev);
  944. return 0;
  945. out_close:
  946. close_candev(dev);
  947. out:
  948. clk_disable_unprepare(priv->clk);
  949. return err;
  950. }
  951. /*
  952. * stop CAN bus activity
  953. */
  954. static int at91_close(struct net_device *dev)
  955. {
  956. struct at91_priv *priv = netdev_priv(dev);
  957. netif_stop_queue(dev);
  958. napi_disable(&priv->napi);
  959. at91_chip_stop(dev, CAN_STATE_STOPPED);
  960. free_irq(dev->irq, dev);
  961. clk_disable_unprepare(priv->clk);
  962. close_candev(dev);
  963. can_led_event(dev, CAN_LED_EVENT_STOP);
  964. return 0;
  965. }
  966. static int at91_set_mode(struct net_device *dev, enum can_mode mode)
  967. {
  968. switch (mode) {
  969. case CAN_MODE_START:
  970. at91_chip_start(dev);
  971. netif_wake_queue(dev);
  972. break;
  973. default:
  974. return -EOPNOTSUPP;
  975. }
  976. return 0;
  977. }
  978. static const struct net_device_ops at91_netdev_ops = {
  979. .ndo_open = at91_open,
  980. .ndo_stop = at91_close,
  981. .ndo_start_xmit = at91_start_xmit,
  982. .ndo_change_mtu = can_change_mtu,
  983. };
  984. static ssize_t at91_sysfs_show_mb0_id(struct device *dev,
  985. struct device_attribute *attr, char *buf)
  986. {
  987. struct at91_priv *priv = netdev_priv(to_net_dev(dev));
  988. if (priv->mb0_id & CAN_EFF_FLAG)
  989. return snprintf(buf, PAGE_SIZE, "0x%08x\n", priv->mb0_id);
  990. else
  991. return snprintf(buf, PAGE_SIZE, "0x%03x\n", priv->mb0_id);
  992. }
  993. static ssize_t at91_sysfs_set_mb0_id(struct device *dev,
  994. struct device_attribute *attr, const char *buf, size_t count)
  995. {
  996. struct net_device *ndev = to_net_dev(dev);
  997. struct at91_priv *priv = netdev_priv(ndev);
  998. unsigned long can_id;
  999. ssize_t ret;
  1000. int err;
  1001. rtnl_lock();
  1002. if (ndev->flags & IFF_UP) {
  1003. ret = -EBUSY;
  1004. goto out;
  1005. }
  1006. err = kstrtoul(buf, 0, &can_id);
  1007. if (err) {
  1008. ret = err;
  1009. goto out;
  1010. }
  1011. if (can_id & CAN_EFF_FLAG)
  1012. can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
  1013. else
  1014. can_id &= CAN_SFF_MASK;
  1015. priv->mb0_id = can_id;
  1016. ret = count;
  1017. out:
  1018. rtnl_unlock();
  1019. return ret;
  1020. }
  1021. static DEVICE_ATTR(mb0_id, 0644, at91_sysfs_show_mb0_id, at91_sysfs_set_mb0_id);
  1022. static struct attribute *at91_sysfs_attrs[] = {
  1023. &dev_attr_mb0_id.attr,
  1024. NULL,
  1025. };
  1026. static const struct attribute_group at91_sysfs_attr_group = {
  1027. .attrs = at91_sysfs_attrs,
  1028. };
  1029. #if defined(CONFIG_OF)
  1030. static const struct of_device_id at91_can_dt_ids[] = {
  1031. {
  1032. .compatible = "atmel,at91sam9x5-can",
  1033. .data = &at91_at91sam9x5_data,
  1034. }, {
  1035. .compatible = "atmel,at91sam9263-can",
  1036. .data = &at91_at91sam9263_data,
  1037. }, {
  1038. /* sentinel */
  1039. }
  1040. };
  1041. MODULE_DEVICE_TABLE(of, at91_can_dt_ids);
  1042. #endif
  1043. static const struct at91_devtype_data *at91_can_get_driver_data(struct platform_device *pdev)
  1044. {
  1045. if (pdev->dev.of_node) {
  1046. const struct of_device_id *match;
  1047. match = of_match_node(at91_can_dt_ids, pdev->dev.of_node);
  1048. if (!match) {
  1049. dev_err(&pdev->dev, "no matching node found in dtb\n");
  1050. return NULL;
  1051. }
  1052. return (const struct at91_devtype_data *)match->data;
  1053. }
  1054. return (const struct at91_devtype_data *)
  1055. platform_get_device_id(pdev)->driver_data;
  1056. }
  1057. static int at91_can_probe(struct platform_device *pdev)
  1058. {
  1059. const struct at91_devtype_data *devtype_data;
  1060. struct net_device *dev;
  1061. struct at91_priv *priv;
  1062. struct resource *res;
  1063. struct clk *clk;
  1064. void __iomem *addr;
  1065. int err, irq;
  1066. devtype_data = at91_can_get_driver_data(pdev);
  1067. if (!devtype_data) {
  1068. dev_err(&pdev->dev, "no driver data\n");
  1069. err = -ENODEV;
  1070. goto exit;
  1071. }
  1072. clk = clk_get(&pdev->dev, "can_clk");
  1073. if (IS_ERR(clk)) {
  1074. dev_err(&pdev->dev, "no clock defined\n");
  1075. err = -ENODEV;
  1076. goto exit;
  1077. }
  1078. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1079. irq = platform_get_irq(pdev, 0);
  1080. if (!res || irq <= 0) {
  1081. err = -ENODEV;
  1082. goto exit_put;
  1083. }
  1084. if (!request_mem_region(res->start,
  1085. resource_size(res),
  1086. pdev->name)) {
  1087. err = -EBUSY;
  1088. goto exit_put;
  1089. }
  1090. addr = ioremap_nocache(res->start, resource_size(res));
  1091. if (!addr) {
  1092. err = -ENOMEM;
  1093. goto exit_release;
  1094. }
  1095. dev = alloc_candev(sizeof(struct at91_priv),
  1096. 1 << devtype_data->tx_shift);
  1097. if (!dev) {
  1098. err = -ENOMEM;
  1099. goto exit_iounmap;
  1100. }
  1101. dev->netdev_ops = &at91_netdev_ops;
  1102. dev->irq = irq;
  1103. dev->flags |= IFF_ECHO;
  1104. priv = netdev_priv(dev);
  1105. priv->can.clock.freq = clk_get_rate(clk);
  1106. priv->can.bittiming_const = &at91_bittiming_const;
  1107. priv->can.do_set_mode = at91_set_mode;
  1108. priv->can.do_get_berr_counter = at91_get_berr_counter;
  1109. priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES |
  1110. CAN_CTRLMODE_LISTENONLY;
  1111. priv->reg_base = addr;
  1112. priv->devtype_data = *devtype_data;
  1113. priv->clk = clk;
  1114. priv->pdata = dev_get_platdata(&pdev->dev);
  1115. priv->mb0_id = 0x7ff;
  1116. netif_napi_add(dev, &priv->napi, at91_poll, get_mb_rx_num(priv));
  1117. if (at91_is_sam9263(priv))
  1118. dev->sysfs_groups[0] = &at91_sysfs_attr_group;
  1119. platform_set_drvdata(pdev, dev);
  1120. SET_NETDEV_DEV(dev, &pdev->dev);
  1121. err = register_candev(dev);
  1122. if (err) {
  1123. dev_err(&pdev->dev, "registering netdev failed\n");
  1124. goto exit_free;
  1125. }
  1126. devm_can_led_init(dev);
  1127. dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
  1128. priv->reg_base, dev->irq);
  1129. return 0;
  1130. exit_free:
  1131. free_candev(dev);
  1132. exit_iounmap:
  1133. iounmap(addr);
  1134. exit_release:
  1135. release_mem_region(res->start, resource_size(res));
  1136. exit_put:
  1137. clk_put(clk);
  1138. exit:
  1139. return err;
  1140. }
  1141. static int at91_can_remove(struct platform_device *pdev)
  1142. {
  1143. struct net_device *dev = platform_get_drvdata(pdev);
  1144. struct at91_priv *priv = netdev_priv(dev);
  1145. struct resource *res;
  1146. unregister_netdev(dev);
  1147. iounmap(priv->reg_base);
  1148. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1149. release_mem_region(res->start, resource_size(res));
  1150. clk_put(priv->clk);
  1151. free_candev(dev);
  1152. return 0;
  1153. }
  1154. static const struct platform_device_id at91_can_id_table[] = {
  1155. {
  1156. .name = "at91sam9x5_can",
  1157. .driver_data = (kernel_ulong_t)&at91_at91sam9x5_data,
  1158. }, {
  1159. .name = "at91_can",
  1160. .driver_data = (kernel_ulong_t)&at91_at91sam9263_data,
  1161. }, {
  1162. /* sentinel */
  1163. }
  1164. };
  1165. MODULE_DEVICE_TABLE(platform, at91_can_id_table);
  1166. static struct platform_driver at91_can_driver = {
  1167. .probe = at91_can_probe,
  1168. .remove = at91_can_remove,
  1169. .driver = {
  1170. .name = KBUILD_MODNAME,
  1171. .of_match_table = of_match_ptr(at91_can_dt_ids),
  1172. },
  1173. .id_table = at91_can_id_table,
  1174. };
  1175. module_platform_driver(at91_can_driver);
  1176. MODULE_AUTHOR("Marc Kleine-Budde <mkl@pengutronix.de>");
  1177. MODULE_LICENSE("GPL v2");
  1178. MODULE_DESCRIPTION(KBUILD_MODNAME " CAN netdevice driver");