at91_can.c 35 KB

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