at91_can.c 35 KB

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