at91_can.c 35 KB

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