at91_can.c 34 KB

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