at91_can.c 35 KB

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