at91_can.c 34 KB

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