at91_can.c 32 KB

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