flexcan.c 26 KB

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