flexcan.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /*
  2. * flexcan.c - FLEXCAN CAN controller driver
  3. *
  4. * Copyright (c) 2005-2006 Varma Electronics Oy
  5. * Copyright (c) 2009 Sascha Hauer, Pengutronix
  6. * Copyright (c) 2010 Marc Kleine-Budde, Pengutronix
  7. *
  8. * Based on code originally by Andrey Volkov <avolkov@varma-el.com>
  9. *
  10. * LICENCE:
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation version 2.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. */
  21. #include <linux/netdevice.h>
  22. #include <linux/can.h>
  23. #include <linux/can/dev.h>
  24. #include <linux/can/error.h>
  25. #include <linux/can/platform/flexcan.h>
  26. #include <linux/clk.h>
  27. #include <linux/delay.h>
  28. #include <linux/if_arp.h>
  29. #include <linux/if_ether.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/io.h>
  32. #include <linux/kernel.h>
  33. #include <linux/list.h>
  34. #include <linux/module.h>
  35. #include <linux/platform_device.h>
  36. #include <mach/clock.h>
  37. #define DRV_NAME "flexcan"
  38. /* 8 for RX fifo and 2 error handling */
  39. #define FLEXCAN_NAPI_WEIGHT (8 + 2)
  40. /* FLEXCAN module configuration register (CANMCR) bits */
  41. #define FLEXCAN_MCR_MDIS BIT(31)
  42. #define FLEXCAN_MCR_FRZ BIT(30)
  43. #define FLEXCAN_MCR_FEN BIT(29)
  44. #define FLEXCAN_MCR_HALT BIT(28)
  45. #define FLEXCAN_MCR_NOT_RDY BIT(27)
  46. #define FLEXCAN_MCR_WAK_MSK BIT(26)
  47. #define FLEXCAN_MCR_SOFTRST BIT(25)
  48. #define FLEXCAN_MCR_FRZ_ACK BIT(24)
  49. #define FLEXCAN_MCR_SUPV BIT(23)
  50. #define FLEXCAN_MCR_SLF_WAK BIT(22)
  51. #define FLEXCAN_MCR_WRN_EN BIT(21)
  52. #define FLEXCAN_MCR_LPM_ACK BIT(20)
  53. #define FLEXCAN_MCR_WAK_SRC BIT(19)
  54. #define FLEXCAN_MCR_DOZE BIT(18)
  55. #define FLEXCAN_MCR_SRX_DIS BIT(17)
  56. #define FLEXCAN_MCR_BCC BIT(16)
  57. #define FLEXCAN_MCR_LPRIO_EN BIT(13)
  58. #define FLEXCAN_MCR_AEN BIT(12)
  59. #define FLEXCAN_MCR_MAXMB(x) ((x) & 0xf)
  60. #define FLEXCAN_MCR_IDAM_A (0 << 8)
  61. #define FLEXCAN_MCR_IDAM_B (1 << 8)
  62. #define FLEXCAN_MCR_IDAM_C (2 << 8)
  63. #define FLEXCAN_MCR_IDAM_D (3 << 8)
  64. /* FLEXCAN control register (CANCTRL) bits */
  65. #define FLEXCAN_CTRL_PRESDIV(x) (((x) & 0xff) << 24)
  66. #define FLEXCAN_CTRL_RJW(x) (((x) & 0x03) << 22)
  67. #define FLEXCAN_CTRL_PSEG1(x) (((x) & 0x07) << 19)
  68. #define FLEXCAN_CTRL_PSEG2(x) (((x) & 0x07) << 16)
  69. #define FLEXCAN_CTRL_BOFF_MSK BIT(15)
  70. #define FLEXCAN_CTRL_ERR_MSK BIT(14)
  71. #define FLEXCAN_CTRL_CLK_SRC BIT(13)
  72. #define FLEXCAN_CTRL_LPB BIT(12)
  73. #define FLEXCAN_CTRL_TWRN_MSK BIT(11)
  74. #define FLEXCAN_CTRL_RWRN_MSK BIT(10)
  75. #define FLEXCAN_CTRL_SMP BIT(7)
  76. #define FLEXCAN_CTRL_BOFF_REC BIT(6)
  77. #define FLEXCAN_CTRL_TSYN BIT(5)
  78. #define FLEXCAN_CTRL_LBUF BIT(4)
  79. #define FLEXCAN_CTRL_LOM BIT(3)
  80. #define FLEXCAN_CTRL_PROPSEG(x) ((x) & 0x07)
  81. #define FLEXCAN_CTRL_ERR_BUS (FLEXCAN_CTRL_ERR_MSK)
  82. #define FLEXCAN_CTRL_ERR_STATE \
  83. (FLEXCAN_CTRL_TWRN_MSK | FLEXCAN_CTRL_RWRN_MSK | \
  84. FLEXCAN_CTRL_BOFF_MSK)
  85. #define FLEXCAN_CTRL_ERR_ALL \
  86. (FLEXCAN_CTRL_ERR_BUS | FLEXCAN_CTRL_ERR_STATE)
  87. /* FLEXCAN error and status register (ESR) bits */
  88. #define FLEXCAN_ESR_TWRN_INT BIT(17)
  89. #define FLEXCAN_ESR_RWRN_INT BIT(16)
  90. #define FLEXCAN_ESR_BIT1_ERR BIT(15)
  91. #define FLEXCAN_ESR_BIT0_ERR BIT(14)
  92. #define FLEXCAN_ESR_ACK_ERR BIT(13)
  93. #define FLEXCAN_ESR_CRC_ERR BIT(12)
  94. #define FLEXCAN_ESR_FRM_ERR BIT(11)
  95. #define FLEXCAN_ESR_STF_ERR BIT(10)
  96. #define FLEXCAN_ESR_TX_WRN BIT(9)
  97. #define FLEXCAN_ESR_RX_WRN BIT(8)
  98. #define FLEXCAN_ESR_IDLE BIT(7)
  99. #define FLEXCAN_ESR_TXRX BIT(6)
  100. #define FLEXCAN_EST_FLT_CONF_SHIFT (4)
  101. #define FLEXCAN_ESR_FLT_CONF_MASK (0x3 << FLEXCAN_EST_FLT_CONF_SHIFT)
  102. #define FLEXCAN_ESR_FLT_CONF_ACTIVE (0x0 << FLEXCAN_EST_FLT_CONF_SHIFT)
  103. #define FLEXCAN_ESR_FLT_CONF_PASSIVE (0x1 << FLEXCAN_EST_FLT_CONF_SHIFT)
  104. #define FLEXCAN_ESR_BOFF_INT BIT(2)
  105. #define FLEXCAN_ESR_ERR_INT BIT(1)
  106. #define FLEXCAN_ESR_WAK_INT BIT(0)
  107. #define FLEXCAN_ESR_ERR_BUS \
  108. (FLEXCAN_ESR_BIT1_ERR | FLEXCAN_ESR_BIT0_ERR | \
  109. FLEXCAN_ESR_ACK_ERR | FLEXCAN_ESR_CRC_ERR | \
  110. FLEXCAN_ESR_FRM_ERR | FLEXCAN_ESR_STF_ERR)
  111. #define FLEXCAN_ESR_ERR_STATE \
  112. (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | FLEXCAN_ESR_BOFF_INT)
  113. #define FLEXCAN_ESR_ERR_ALL \
  114. (FLEXCAN_ESR_ERR_BUS | FLEXCAN_ESR_ERR_STATE)
  115. /* FLEXCAN interrupt flag register (IFLAG) bits */
  116. #define FLEXCAN_TX_BUF_ID 8
  117. #define FLEXCAN_IFLAG_BUF(x) BIT(x)
  118. #define FLEXCAN_IFLAG_RX_FIFO_OVERFLOW BIT(7)
  119. #define FLEXCAN_IFLAG_RX_FIFO_WARN BIT(6)
  120. #define FLEXCAN_IFLAG_RX_FIFO_AVAILABLE BIT(5)
  121. #define FLEXCAN_IFLAG_DEFAULT \
  122. (FLEXCAN_IFLAG_RX_FIFO_OVERFLOW | FLEXCAN_IFLAG_RX_FIFO_AVAILABLE | \
  123. FLEXCAN_IFLAG_BUF(FLEXCAN_TX_BUF_ID))
  124. /* FLEXCAN message buffers */
  125. #define FLEXCAN_MB_CNT_CODE(x) (((x) & 0xf) << 24)
  126. #define FLEXCAN_MB_CNT_SRR BIT(22)
  127. #define FLEXCAN_MB_CNT_IDE BIT(21)
  128. #define FLEXCAN_MB_CNT_RTR BIT(20)
  129. #define FLEXCAN_MB_CNT_LENGTH(x) (((x) & 0xf) << 16)
  130. #define FLEXCAN_MB_CNT_TIMESTAMP(x) ((x) & 0xffff)
  131. #define FLEXCAN_MB_CODE_MASK (0xf0ffffff)
  132. /* Structure of the message buffer */
  133. struct flexcan_mb {
  134. u32 can_ctrl;
  135. u32 can_id;
  136. u32 data[2];
  137. };
  138. /* Structure of the hardware registers */
  139. struct flexcan_regs {
  140. u32 mcr; /* 0x00 */
  141. u32 ctrl; /* 0x04 */
  142. u32 timer; /* 0x08 */
  143. u32 _reserved1; /* 0x0c */
  144. u32 rxgmask; /* 0x10 */
  145. u32 rx14mask; /* 0x14 */
  146. u32 rx15mask; /* 0x18 */
  147. u32 ecr; /* 0x1c */
  148. u32 esr; /* 0x20 */
  149. u32 imask2; /* 0x24 */
  150. u32 imask1; /* 0x28 */
  151. u32 iflag2; /* 0x2c */
  152. u32 iflag1; /* 0x30 */
  153. u32 _reserved2[19];
  154. struct flexcan_mb cantxfg[64];
  155. };
  156. struct flexcan_priv {
  157. struct can_priv can;
  158. struct net_device *dev;
  159. struct napi_struct napi;
  160. void __iomem *base;
  161. u32 reg_esr;
  162. u32 reg_ctrl_default;
  163. struct clk *clk;
  164. struct flexcan_platform_data *pdata;
  165. };
  166. static struct can_bittiming_const flexcan_bittiming_const = {
  167. .name = DRV_NAME,
  168. .tseg1_min = 4,
  169. .tseg1_max = 16,
  170. .tseg2_min = 2,
  171. .tseg2_max = 8,
  172. .sjw_max = 4,
  173. .brp_min = 1,
  174. .brp_max = 256,
  175. .brp_inc = 1,
  176. };
  177. /*
  178. * Swtich transceiver on or off
  179. */
  180. static void flexcan_transceiver_switch(const struct flexcan_priv *priv, int on)
  181. {
  182. if (priv->pdata && priv->pdata->transceiver_switch)
  183. priv->pdata->transceiver_switch(on);
  184. }
  185. static inline int flexcan_has_and_handle_berr(const struct flexcan_priv *priv,
  186. u32 reg_esr)
  187. {
  188. return (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
  189. (reg_esr & FLEXCAN_ESR_ERR_BUS);
  190. }
  191. static inline void flexcan_chip_enable(struct flexcan_priv *priv)
  192. {
  193. struct flexcan_regs __iomem *regs = priv->base;
  194. u32 reg;
  195. reg = readl(&regs->mcr);
  196. reg &= ~FLEXCAN_MCR_MDIS;
  197. writel(reg, &regs->mcr);
  198. udelay(10);
  199. }
  200. static inline void flexcan_chip_disable(struct flexcan_priv *priv)
  201. {
  202. struct flexcan_regs __iomem *regs = priv->base;
  203. u32 reg;
  204. reg = readl(&regs->mcr);
  205. reg |= FLEXCAN_MCR_MDIS;
  206. writel(reg, &regs->mcr);
  207. }
  208. static int flexcan_get_berr_counter(const struct net_device *dev,
  209. struct can_berr_counter *bec)
  210. {
  211. const struct flexcan_priv *priv = netdev_priv(dev);
  212. struct flexcan_regs __iomem *regs = priv->base;
  213. u32 reg = readl(&regs->ecr);
  214. bec->txerr = (reg >> 0) & 0xff;
  215. bec->rxerr = (reg >> 8) & 0xff;
  216. return 0;
  217. }
  218. static int flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
  219. {
  220. const struct flexcan_priv *priv = netdev_priv(dev);
  221. struct net_device_stats *stats = &dev->stats;
  222. struct flexcan_regs __iomem *regs = priv->base;
  223. struct can_frame *cf = (struct can_frame *)skb->data;
  224. u32 can_id;
  225. u32 ctrl = FLEXCAN_MB_CNT_CODE(0xc) | (cf->can_dlc << 16);
  226. if (can_dropped_invalid_skb(dev, skb))
  227. return NETDEV_TX_OK;
  228. netif_stop_queue(dev);
  229. if (cf->can_id & CAN_EFF_FLAG) {
  230. can_id = cf->can_id & CAN_EFF_MASK;
  231. ctrl |= FLEXCAN_MB_CNT_IDE | FLEXCAN_MB_CNT_SRR;
  232. } else {
  233. can_id = (cf->can_id & CAN_SFF_MASK) << 18;
  234. }
  235. if (cf->can_id & CAN_RTR_FLAG)
  236. ctrl |= FLEXCAN_MB_CNT_RTR;
  237. if (cf->can_dlc > 0) {
  238. u32 data = be32_to_cpup((__be32 *)&cf->data[0]);
  239. writel(data, &regs->cantxfg[FLEXCAN_TX_BUF_ID].data[0]);
  240. }
  241. if (cf->can_dlc > 3) {
  242. u32 data = be32_to_cpup((__be32 *)&cf->data[4]);
  243. writel(data, &regs->cantxfg[FLEXCAN_TX_BUF_ID].data[1]);
  244. }
  245. writel(can_id, &regs->cantxfg[FLEXCAN_TX_BUF_ID].can_id);
  246. writel(ctrl, &regs->cantxfg[FLEXCAN_TX_BUF_ID].can_ctrl);
  247. kfree_skb(skb);
  248. /* tx_packets is incremented in flexcan_irq */
  249. stats->tx_bytes += cf->can_dlc;
  250. return NETDEV_TX_OK;
  251. }
  252. static void do_bus_err(struct net_device *dev,
  253. struct can_frame *cf, u32 reg_esr)
  254. {
  255. struct flexcan_priv *priv = netdev_priv(dev);
  256. int rx_errors = 0, tx_errors = 0;
  257. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  258. if (reg_esr & FLEXCAN_ESR_BIT1_ERR) {
  259. dev_dbg(dev->dev.parent, "BIT1_ERR irq\n");
  260. cf->data[2] |= CAN_ERR_PROT_BIT1;
  261. tx_errors = 1;
  262. }
  263. if (reg_esr & FLEXCAN_ESR_BIT0_ERR) {
  264. dev_dbg(dev->dev.parent, "BIT0_ERR irq\n");
  265. cf->data[2] |= CAN_ERR_PROT_BIT0;
  266. tx_errors = 1;
  267. }
  268. if (reg_esr & FLEXCAN_ESR_ACK_ERR) {
  269. dev_dbg(dev->dev.parent, "ACK_ERR irq\n");
  270. cf->can_id |= CAN_ERR_ACK;
  271. cf->data[3] |= CAN_ERR_PROT_LOC_ACK;
  272. tx_errors = 1;
  273. }
  274. if (reg_esr & FLEXCAN_ESR_CRC_ERR) {
  275. dev_dbg(dev->dev.parent, "CRC_ERR irq\n");
  276. cf->data[2] |= CAN_ERR_PROT_BIT;
  277. cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
  278. rx_errors = 1;
  279. }
  280. if (reg_esr & FLEXCAN_ESR_FRM_ERR) {
  281. dev_dbg(dev->dev.parent, "FRM_ERR irq\n");
  282. cf->data[2] |= CAN_ERR_PROT_FORM;
  283. rx_errors = 1;
  284. }
  285. if (reg_esr & FLEXCAN_ESR_STF_ERR) {
  286. dev_dbg(dev->dev.parent, "STF_ERR irq\n");
  287. cf->data[2] |= CAN_ERR_PROT_STUFF;
  288. rx_errors = 1;
  289. }
  290. priv->can.can_stats.bus_error++;
  291. if (rx_errors)
  292. dev->stats.rx_errors++;
  293. if (tx_errors)
  294. dev->stats.tx_errors++;
  295. }
  296. static int flexcan_poll_bus_err(struct net_device *dev, u32 reg_esr)
  297. {
  298. struct sk_buff *skb;
  299. struct can_frame *cf;
  300. skb = alloc_can_err_skb(dev, &cf);
  301. if (unlikely(!skb))
  302. return 0;
  303. do_bus_err(dev, cf, reg_esr);
  304. netif_receive_skb(skb);
  305. dev->stats.rx_packets++;
  306. dev->stats.rx_bytes += cf->can_dlc;
  307. return 1;
  308. }
  309. static void do_state(struct net_device *dev,
  310. struct can_frame *cf, enum can_state new_state)
  311. {
  312. struct flexcan_priv *priv = netdev_priv(dev);
  313. struct can_berr_counter bec;
  314. flexcan_get_berr_counter(dev, &bec);
  315. switch (priv->can.state) {
  316. case CAN_STATE_ERROR_ACTIVE:
  317. /*
  318. * from: ERROR_ACTIVE
  319. * to : ERROR_WARNING, ERROR_PASSIVE, BUS_OFF
  320. * => : there was a warning int
  321. */
  322. if (new_state >= CAN_STATE_ERROR_WARNING &&
  323. new_state <= CAN_STATE_BUS_OFF) {
  324. dev_dbg(dev->dev.parent, "Error Warning IRQ\n");
  325. priv->can.can_stats.error_warning++;
  326. cf->can_id |= CAN_ERR_CRTL;
  327. cf->data[1] = (bec.txerr > bec.rxerr) ?
  328. CAN_ERR_CRTL_TX_WARNING :
  329. CAN_ERR_CRTL_RX_WARNING;
  330. }
  331. case CAN_STATE_ERROR_WARNING: /* fallthrough */
  332. /*
  333. * from: ERROR_ACTIVE, ERROR_WARNING
  334. * to : ERROR_PASSIVE, BUS_OFF
  335. * => : error passive int
  336. */
  337. if (new_state >= CAN_STATE_ERROR_PASSIVE &&
  338. new_state <= CAN_STATE_BUS_OFF) {
  339. dev_dbg(dev->dev.parent, "Error Passive IRQ\n");
  340. priv->can.can_stats.error_passive++;
  341. cf->can_id |= CAN_ERR_CRTL;
  342. cf->data[1] = (bec.txerr > bec.rxerr) ?
  343. CAN_ERR_CRTL_TX_PASSIVE :
  344. CAN_ERR_CRTL_RX_PASSIVE;
  345. }
  346. break;
  347. case CAN_STATE_BUS_OFF:
  348. dev_err(dev->dev.parent,
  349. "BUG! hardware recovered automatically from BUS_OFF\n");
  350. break;
  351. default:
  352. break;
  353. }
  354. /* process state changes depending on the new state */
  355. switch (new_state) {
  356. case CAN_STATE_ERROR_ACTIVE:
  357. dev_dbg(dev->dev.parent, "Error Active\n");
  358. cf->can_id |= CAN_ERR_PROT;
  359. cf->data[2] = CAN_ERR_PROT_ACTIVE;
  360. break;
  361. case CAN_STATE_BUS_OFF:
  362. cf->can_id |= CAN_ERR_BUSOFF;
  363. can_bus_off(dev);
  364. break;
  365. default:
  366. break;
  367. }
  368. }
  369. static int flexcan_poll_state(struct net_device *dev, u32 reg_esr)
  370. {
  371. struct flexcan_priv *priv = netdev_priv(dev);
  372. struct sk_buff *skb;
  373. struct can_frame *cf;
  374. enum can_state new_state;
  375. int flt;
  376. flt = reg_esr & FLEXCAN_ESR_FLT_CONF_MASK;
  377. if (likely(flt == FLEXCAN_ESR_FLT_CONF_ACTIVE)) {
  378. if (likely(!(reg_esr & (FLEXCAN_ESR_TX_WRN |
  379. FLEXCAN_ESR_RX_WRN))))
  380. new_state = CAN_STATE_ERROR_ACTIVE;
  381. else
  382. new_state = CAN_STATE_ERROR_WARNING;
  383. } else if (unlikely(flt == FLEXCAN_ESR_FLT_CONF_PASSIVE))
  384. new_state = CAN_STATE_ERROR_PASSIVE;
  385. else
  386. new_state = CAN_STATE_BUS_OFF;
  387. /* state hasn't changed */
  388. if (likely(new_state == priv->can.state))
  389. return 0;
  390. skb = alloc_can_err_skb(dev, &cf);
  391. if (unlikely(!skb))
  392. return 0;
  393. do_state(dev, cf, new_state);
  394. priv->can.state = new_state;
  395. netif_receive_skb(skb);
  396. dev->stats.rx_packets++;
  397. dev->stats.rx_bytes += cf->can_dlc;
  398. return 1;
  399. }
  400. static void flexcan_read_fifo(const struct net_device *dev,
  401. struct can_frame *cf)
  402. {
  403. const struct flexcan_priv *priv = netdev_priv(dev);
  404. struct flexcan_regs __iomem *regs = priv->base;
  405. struct flexcan_mb __iomem *mb = &regs->cantxfg[0];
  406. u32 reg_ctrl, reg_id;
  407. reg_ctrl = readl(&mb->can_ctrl);
  408. reg_id = readl(&mb->can_id);
  409. if (reg_ctrl & FLEXCAN_MB_CNT_IDE)
  410. cf->can_id = ((reg_id >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG;
  411. else
  412. cf->can_id = (reg_id >> 18) & CAN_SFF_MASK;
  413. if (reg_ctrl & FLEXCAN_MB_CNT_RTR)
  414. cf->can_id |= CAN_RTR_FLAG;
  415. cf->can_dlc = get_can_dlc((reg_ctrl >> 16) & 0xf);
  416. *(__be32 *)(cf->data + 0) = cpu_to_be32(readl(&mb->data[0]));
  417. *(__be32 *)(cf->data + 4) = cpu_to_be32(readl(&mb->data[1]));
  418. /* mark as read */
  419. writel(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, &regs->iflag1);
  420. readl(&regs->timer);
  421. }
  422. static int flexcan_read_frame(struct net_device *dev)
  423. {
  424. struct net_device_stats *stats = &dev->stats;
  425. struct can_frame *cf;
  426. struct sk_buff *skb;
  427. skb = alloc_can_skb(dev, &cf);
  428. if (unlikely(!skb)) {
  429. stats->rx_dropped++;
  430. return 0;
  431. }
  432. flexcan_read_fifo(dev, cf);
  433. netif_receive_skb(skb);
  434. stats->rx_packets++;
  435. stats->rx_bytes += cf->can_dlc;
  436. return 1;
  437. }
  438. static int flexcan_poll(struct napi_struct *napi, int quota)
  439. {
  440. struct net_device *dev = napi->dev;
  441. const struct flexcan_priv *priv = netdev_priv(dev);
  442. struct flexcan_regs __iomem *regs = priv->base;
  443. u32 reg_iflag1, reg_esr;
  444. int work_done = 0;
  445. /*
  446. * The error bits are cleared on read,
  447. * use saved value from irq handler.
  448. */
  449. reg_esr = readl(&regs->esr) | priv->reg_esr;
  450. /* handle state changes */
  451. work_done += flexcan_poll_state(dev, reg_esr);
  452. /* handle RX-FIFO */
  453. reg_iflag1 = readl(&regs->iflag1);
  454. while (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE &&
  455. work_done < quota) {
  456. work_done += flexcan_read_frame(dev);
  457. reg_iflag1 = readl(&regs->iflag1);
  458. }
  459. /* report bus errors */
  460. if (flexcan_has_and_handle_berr(priv, reg_esr) && work_done < quota)
  461. work_done += flexcan_poll_bus_err(dev, reg_esr);
  462. if (work_done < quota) {
  463. napi_complete(napi);
  464. /* enable IRQs */
  465. writel(FLEXCAN_IFLAG_DEFAULT, &regs->imask1);
  466. writel(priv->reg_ctrl_default, &regs->ctrl);
  467. }
  468. return work_done;
  469. }
  470. static irqreturn_t flexcan_irq(int irq, void *dev_id)
  471. {
  472. struct net_device *dev = dev_id;
  473. struct net_device_stats *stats = &dev->stats;
  474. struct flexcan_priv *priv = netdev_priv(dev);
  475. struct flexcan_regs __iomem *regs = priv->base;
  476. u32 reg_iflag1, reg_esr;
  477. reg_iflag1 = readl(&regs->iflag1);
  478. reg_esr = readl(&regs->esr);
  479. writel(FLEXCAN_ESR_ERR_INT, &regs->esr); /* ACK err IRQ */
  480. /*
  481. * schedule NAPI in case of:
  482. * - rx IRQ
  483. * - state change IRQ
  484. * - bus error IRQ and bus error reporting is activated
  485. */
  486. if ((reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE) ||
  487. (reg_esr & FLEXCAN_ESR_ERR_STATE) ||
  488. flexcan_has_and_handle_berr(priv, reg_esr)) {
  489. /*
  490. * The error bits are cleared on read,
  491. * save them for later use.
  492. */
  493. priv->reg_esr = reg_esr & FLEXCAN_ESR_ERR_BUS;
  494. writel(FLEXCAN_IFLAG_DEFAULT & ~FLEXCAN_IFLAG_RX_FIFO_AVAILABLE,
  495. &regs->imask1);
  496. writel(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
  497. &regs->ctrl);
  498. napi_schedule(&priv->napi);
  499. }
  500. /* FIFO overflow */
  501. if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_OVERFLOW) {
  502. writel(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW, &regs->iflag1);
  503. dev->stats.rx_over_errors++;
  504. dev->stats.rx_errors++;
  505. }
  506. /* transmission complete interrupt */
  507. if (reg_iflag1 & (1 << FLEXCAN_TX_BUF_ID)) {
  508. /* tx_bytes is incremented in flexcan_start_xmit */
  509. stats->tx_packets++;
  510. writel((1 << FLEXCAN_TX_BUF_ID), &regs->iflag1);
  511. netif_wake_queue(dev);
  512. }
  513. return IRQ_HANDLED;
  514. }
  515. static void flexcan_set_bittiming(struct net_device *dev)
  516. {
  517. const struct flexcan_priv *priv = netdev_priv(dev);
  518. const struct can_bittiming *bt = &priv->can.bittiming;
  519. struct flexcan_regs __iomem *regs = priv->base;
  520. u32 reg;
  521. reg = readl(&regs->ctrl);
  522. reg &= ~(FLEXCAN_CTRL_PRESDIV(0xff) |
  523. FLEXCAN_CTRL_RJW(0x3) |
  524. FLEXCAN_CTRL_PSEG1(0x7) |
  525. FLEXCAN_CTRL_PSEG2(0x7) |
  526. FLEXCAN_CTRL_PROPSEG(0x7) |
  527. FLEXCAN_CTRL_LPB |
  528. FLEXCAN_CTRL_SMP |
  529. FLEXCAN_CTRL_LOM);
  530. reg |= FLEXCAN_CTRL_PRESDIV(bt->brp - 1) |
  531. FLEXCAN_CTRL_PSEG1(bt->phase_seg1 - 1) |
  532. FLEXCAN_CTRL_PSEG2(bt->phase_seg2 - 1) |
  533. FLEXCAN_CTRL_RJW(bt->sjw - 1) |
  534. FLEXCAN_CTRL_PROPSEG(bt->prop_seg - 1);
  535. if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
  536. reg |= FLEXCAN_CTRL_LPB;
  537. if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  538. reg |= FLEXCAN_CTRL_LOM;
  539. if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  540. reg |= FLEXCAN_CTRL_SMP;
  541. dev_info(dev->dev.parent, "writing ctrl=0x%08x\n", reg);
  542. writel(reg, &regs->ctrl);
  543. /* print chip status */
  544. dev_dbg(dev->dev.parent, "%s: mcr=0x%08x ctrl=0x%08x\n", __func__,
  545. readl(&regs->mcr), readl(&regs->ctrl));
  546. }
  547. /*
  548. * flexcan_chip_start
  549. *
  550. * this functions is entered with clocks enabled
  551. *
  552. */
  553. static int flexcan_chip_start(struct net_device *dev)
  554. {
  555. struct flexcan_priv *priv = netdev_priv(dev);
  556. struct flexcan_regs __iomem *regs = priv->base;
  557. unsigned int i;
  558. int err;
  559. u32 reg_mcr, reg_ctrl;
  560. /* enable module */
  561. flexcan_chip_enable(priv);
  562. /* soft reset */
  563. writel(FLEXCAN_MCR_SOFTRST, &regs->mcr);
  564. udelay(10);
  565. reg_mcr = readl(&regs->mcr);
  566. if (reg_mcr & FLEXCAN_MCR_SOFTRST) {
  567. dev_err(dev->dev.parent,
  568. "Failed to softreset can module (mcr=0x%08x)\n",
  569. reg_mcr);
  570. err = -ENODEV;
  571. goto out;
  572. }
  573. flexcan_set_bittiming(dev);
  574. /*
  575. * MCR
  576. *
  577. * enable freeze
  578. * enable fifo
  579. * halt now
  580. * only supervisor access
  581. * enable warning int
  582. * choose format C
  583. *
  584. */
  585. reg_mcr = readl(&regs->mcr);
  586. reg_mcr |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_FEN | FLEXCAN_MCR_HALT |
  587. FLEXCAN_MCR_SUPV | FLEXCAN_MCR_WRN_EN |
  588. FLEXCAN_MCR_IDAM_C;
  589. dev_dbg(dev->dev.parent, "%s: writing mcr=0x%08x", __func__, reg_mcr);
  590. writel(reg_mcr, &regs->mcr);
  591. /*
  592. * CTRL
  593. *
  594. * disable timer sync feature
  595. *
  596. * disable auto busoff recovery
  597. * transmit lowest buffer first
  598. *
  599. * enable tx and rx warning interrupt
  600. * enable bus off interrupt
  601. * (== FLEXCAN_CTRL_ERR_STATE)
  602. *
  603. * _note_: we enable the "error interrupt"
  604. * (FLEXCAN_CTRL_ERR_MSK), too. Otherwise we don't get any
  605. * warning or bus passive interrupts.
  606. */
  607. reg_ctrl = readl(&regs->ctrl);
  608. reg_ctrl &= ~FLEXCAN_CTRL_TSYN;
  609. reg_ctrl |= FLEXCAN_CTRL_BOFF_REC | FLEXCAN_CTRL_LBUF |
  610. FLEXCAN_CTRL_ERR_STATE | FLEXCAN_CTRL_ERR_MSK;
  611. /* save for later use */
  612. priv->reg_ctrl_default = reg_ctrl;
  613. dev_dbg(dev->dev.parent, "%s: writing ctrl=0x%08x", __func__, reg_ctrl);
  614. writel(reg_ctrl, &regs->ctrl);
  615. for (i = 0; i < ARRAY_SIZE(regs->cantxfg); i++) {
  616. writel(0, &regs->cantxfg[i].can_ctrl);
  617. writel(0, &regs->cantxfg[i].can_id);
  618. writel(0, &regs->cantxfg[i].data[0]);
  619. writel(0, &regs->cantxfg[i].data[1]);
  620. /* put MB into rx queue */
  621. writel(FLEXCAN_MB_CNT_CODE(0x4), &regs->cantxfg[i].can_ctrl);
  622. }
  623. /* acceptance mask/acceptance code (accept everything) */
  624. writel(0x0, &regs->rxgmask);
  625. writel(0x0, &regs->rx14mask);
  626. writel(0x0, &regs->rx15mask);
  627. flexcan_transceiver_switch(priv, 1);
  628. /* synchronize with the can bus */
  629. reg_mcr = readl(&regs->mcr);
  630. reg_mcr &= ~FLEXCAN_MCR_HALT;
  631. writel(reg_mcr, &regs->mcr);
  632. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  633. /* enable FIFO interrupts */
  634. writel(FLEXCAN_IFLAG_DEFAULT, &regs->imask1);
  635. /* print chip status */
  636. dev_dbg(dev->dev.parent, "%s: reading mcr=0x%08x ctrl=0x%08x\n",
  637. __func__, readl(&regs->mcr), readl(&regs->ctrl));
  638. return 0;
  639. out:
  640. flexcan_chip_disable(priv);
  641. return err;
  642. }
  643. /*
  644. * flexcan_chip_stop
  645. *
  646. * this functions is entered with clocks enabled
  647. *
  648. */
  649. static void flexcan_chip_stop(struct net_device *dev)
  650. {
  651. struct flexcan_priv *priv = netdev_priv(dev);
  652. struct flexcan_regs __iomem *regs = priv->base;
  653. u32 reg;
  654. /* Disable all interrupts */
  655. writel(0, &regs->imask1);
  656. /* Disable + halt module */
  657. reg = readl(&regs->mcr);
  658. reg |= FLEXCAN_MCR_MDIS | FLEXCAN_MCR_HALT;
  659. writel(reg, &regs->mcr);
  660. flexcan_transceiver_switch(priv, 0);
  661. priv->can.state = CAN_STATE_STOPPED;
  662. return;
  663. }
  664. static int flexcan_open(struct net_device *dev)
  665. {
  666. struct flexcan_priv *priv = netdev_priv(dev);
  667. int err;
  668. clk_enable(priv->clk);
  669. err = open_candev(dev);
  670. if (err)
  671. goto out;
  672. err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev);
  673. if (err)
  674. goto out_close;
  675. /* start chip and queuing */
  676. err = flexcan_chip_start(dev);
  677. if (err)
  678. goto out_close;
  679. napi_enable(&priv->napi);
  680. netif_start_queue(dev);
  681. return 0;
  682. out_close:
  683. close_candev(dev);
  684. out:
  685. clk_disable(priv->clk);
  686. return err;
  687. }
  688. static int flexcan_close(struct net_device *dev)
  689. {
  690. struct flexcan_priv *priv = netdev_priv(dev);
  691. netif_stop_queue(dev);
  692. napi_disable(&priv->napi);
  693. flexcan_chip_stop(dev);
  694. free_irq(dev->irq, dev);
  695. clk_disable(priv->clk);
  696. close_candev(dev);
  697. return 0;
  698. }
  699. static int flexcan_set_mode(struct net_device *dev, enum can_mode mode)
  700. {
  701. int err;
  702. switch (mode) {
  703. case CAN_MODE_START:
  704. err = flexcan_chip_start(dev);
  705. if (err)
  706. return err;
  707. netif_wake_queue(dev);
  708. break;
  709. default:
  710. return -EOPNOTSUPP;
  711. }
  712. return 0;
  713. }
  714. static const struct net_device_ops flexcan_netdev_ops = {
  715. .ndo_open = flexcan_open,
  716. .ndo_stop = flexcan_close,
  717. .ndo_start_xmit = flexcan_start_xmit,
  718. };
  719. static int __devinit register_flexcandev(struct net_device *dev)
  720. {
  721. struct flexcan_priv *priv = netdev_priv(dev);
  722. struct flexcan_regs __iomem *regs = priv->base;
  723. u32 reg, err;
  724. clk_enable(priv->clk);
  725. /* select "bus clock", chip must be disabled */
  726. flexcan_chip_disable(priv);
  727. reg = readl(&regs->ctrl);
  728. reg |= FLEXCAN_CTRL_CLK_SRC;
  729. writel(reg, &regs->ctrl);
  730. flexcan_chip_enable(priv);
  731. /* set freeze, halt and activate FIFO, restrict register access */
  732. reg = readl(&regs->mcr);
  733. reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT |
  734. FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV;
  735. writel(reg, &regs->mcr);
  736. /*
  737. * Currently we only support newer versions of this core
  738. * featuring a RX FIFO. Older cores found on some Coldfire
  739. * derivates are not yet supported.
  740. */
  741. reg = readl(&regs->mcr);
  742. if (!(reg & FLEXCAN_MCR_FEN)) {
  743. dev_err(dev->dev.parent,
  744. "Could not enable RX FIFO, unsupported core\n");
  745. err = -ENODEV;
  746. goto out;
  747. }
  748. err = register_candev(dev);
  749. out:
  750. /* disable core and turn off clocks */
  751. flexcan_chip_disable(priv);
  752. clk_disable(priv->clk);
  753. return err;
  754. }
  755. static void __devexit unregister_flexcandev(struct net_device *dev)
  756. {
  757. unregister_candev(dev);
  758. }
  759. static int __devinit flexcan_probe(struct platform_device *pdev)
  760. {
  761. struct net_device *dev;
  762. struct flexcan_priv *priv;
  763. struct resource *mem;
  764. struct clk *clk;
  765. void __iomem *base;
  766. resource_size_t mem_size;
  767. int err, irq;
  768. clk = clk_get(&pdev->dev, NULL);
  769. if (IS_ERR(clk)) {
  770. dev_err(&pdev->dev, "no clock defined\n");
  771. err = PTR_ERR(clk);
  772. goto failed_clock;
  773. }
  774. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  775. irq = platform_get_irq(pdev, 0);
  776. if (!mem || irq <= 0) {
  777. err = -ENODEV;
  778. goto failed_get;
  779. }
  780. mem_size = resource_size(mem);
  781. if (!request_mem_region(mem->start, mem_size, pdev->name)) {
  782. err = -EBUSY;
  783. goto failed_get;
  784. }
  785. base = ioremap(mem->start, mem_size);
  786. if (!base) {
  787. err = -ENOMEM;
  788. goto failed_map;
  789. }
  790. dev = alloc_candev(sizeof(struct flexcan_priv), 0);
  791. if (!dev) {
  792. err = -ENOMEM;
  793. goto failed_alloc;
  794. }
  795. dev->netdev_ops = &flexcan_netdev_ops;
  796. dev->irq = irq;
  797. dev->flags |= IFF_ECHO; /* we support local echo in hardware */
  798. priv = netdev_priv(dev);
  799. priv->can.clock.freq = clk_get_rate(clk);
  800. priv->can.bittiming_const = &flexcan_bittiming_const;
  801. priv->can.do_set_mode = flexcan_set_mode;
  802. priv->can.do_get_berr_counter = flexcan_get_berr_counter;
  803. priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
  804. CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_3_SAMPLES |
  805. CAN_CTRLMODE_BERR_REPORTING;
  806. priv->base = base;
  807. priv->dev = dev;
  808. priv->clk = clk;
  809. priv->pdata = pdev->dev.platform_data;
  810. netif_napi_add(dev, &priv->napi, flexcan_poll, FLEXCAN_NAPI_WEIGHT);
  811. dev_set_drvdata(&pdev->dev, dev);
  812. SET_NETDEV_DEV(dev, &pdev->dev);
  813. err = register_flexcandev(dev);
  814. if (err) {
  815. dev_err(&pdev->dev, "registering netdev failed\n");
  816. goto failed_register;
  817. }
  818. dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
  819. priv->base, dev->irq);
  820. return 0;
  821. failed_register:
  822. free_candev(dev);
  823. failed_alloc:
  824. iounmap(base);
  825. failed_map:
  826. release_mem_region(mem->start, mem_size);
  827. failed_get:
  828. clk_put(clk);
  829. failed_clock:
  830. return err;
  831. }
  832. static int __devexit flexcan_remove(struct platform_device *pdev)
  833. {
  834. struct net_device *dev = platform_get_drvdata(pdev);
  835. struct flexcan_priv *priv = netdev_priv(dev);
  836. struct resource *mem;
  837. unregister_flexcandev(dev);
  838. platform_set_drvdata(pdev, NULL);
  839. iounmap(priv->base);
  840. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  841. release_mem_region(mem->start, resource_size(mem));
  842. clk_put(priv->clk);
  843. free_candev(dev);
  844. return 0;
  845. }
  846. static struct platform_driver flexcan_driver = {
  847. .driver.name = DRV_NAME,
  848. .probe = flexcan_probe,
  849. .remove = __devexit_p(flexcan_remove),
  850. };
  851. static int __init flexcan_init(void)
  852. {
  853. pr_info("%s netdevice driver\n", DRV_NAME);
  854. return platform_driver_register(&flexcan_driver);
  855. }
  856. static void __exit flexcan_exit(void)
  857. {
  858. platform_driver_unregister(&flexcan_driver);
  859. pr_info("%s: driver removed\n", DRV_NAME);
  860. }
  861. module_init(flexcan_init);
  862. module_exit(flexcan_exit);
  863. MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, "
  864. "Marc Kleine-Budde <kernel@pengutronix.de>");
  865. MODULE_LICENSE("GPL v2");
  866. MODULE_DESCRIPTION("CAN port driver for flexcan based chip");