flexcan.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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/led.h>
  26. #include <linux/clk.h>
  27. #include <linux/delay.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/io.h>
  30. #include <linux/module.h>
  31. #include <linux/of.h>
  32. #include <linux/of_device.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/regulator/consumer.h>
  35. #define DRV_NAME "flexcan"
  36. /* 8 for RX fifo and 2 error handling */
  37. #define FLEXCAN_NAPI_WEIGHT (8 + 2)
  38. /* FLEXCAN module configuration register (CANMCR) bits */
  39. #define FLEXCAN_MCR_MDIS BIT(31)
  40. #define FLEXCAN_MCR_FRZ BIT(30)
  41. #define FLEXCAN_MCR_FEN BIT(29)
  42. #define FLEXCAN_MCR_HALT BIT(28)
  43. #define FLEXCAN_MCR_NOT_RDY BIT(27)
  44. #define FLEXCAN_MCR_WAK_MSK BIT(26)
  45. #define FLEXCAN_MCR_SOFTRST BIT(25)
  46. #define FLEXCAN_MCR_FRZ_ACK BIT(24)
  47. #define FLEXCAN_MCR_SUPV BIT(23)
  48. #define FLEXCAN_MCR_SLF_WAK BIT(22)
  49. #define FLEXCAN_MCR_WRN_EN BIT(21)
  50. #define FLEXCAN_MCR_LPM_ACK BIT(20)
  51. #define FLEXCAN_MCR_WAK_SRC BIT(19)
  52. #define FLEXCAN_MCR_DOZE BIT(18)
  53. #define FLEXCAN_MCR_SRX_DIS BIT(17)
  54. #define FLEXCAN_MCR_BCC BIT(16)
  55. #define FLEXCAN_MCR_LPRIO_EN BIT(13)
  56. #define FLEXCAN_MCR_AEN BIT(12)
  57. #define FLEXCAN_MCR_MAXMB(x) ((x) & 0x7f)
  58. #define FLEXCAN_MCR_IDAM_A (0x0 << 8)
  59. #define FLEXCAN_MCR_IDAM_B (0x1 << 8)
  60. #define FLEXCAN_MCR_IDAM_C (0x2 << 8)
  61. #define FLEXCAN_MCR_IDAM_D (0x3 << 8)
  62. /* FLEXCAN control register (CANCTRL) bits */
  63. #define FLEXCAN_CTRL_PRESDIV(x) (((x) & 0xff) << 24)
  64. #define FLEXCAN_CTRL_RJW(x) (((x) & 0x03) << 22)
  65. #define FLEXCAN_CTRL_PSEG1(x) (((x) & 0x07) << 19)
  66. #define FLEXCAN_CTRL_PSEG2(x) (((x) & 0x07) << 16)
  67. #define FLEXCAN_CTRL_BOFF_MSK BIT(15)
  68. #define FLEXCAN_CTRL_ERR_MSK BIT(14)
  69. #define FLEXCAN_CTRL_CLK_SRC BIT(13)
  70. #define FLEXCAN_CTRL_LPB BIT(12)
  71. #define FLEXCAN_CTRL_TWRN_MSK BIT(11)
  72. #define FLEXCAN_CTRL_RWRN_MSK BIT(10)
  73. #define FLEXCAN_CTRL_SMP BIT(7)
  74. #define FLEXCAN_CTRL_BOFF_REC BIT(6)
  75. #define FLEXCAN_CTRL_TSYN BIT(5)
  76. #define FLEXCAN_CTRL_LBUF BIT(4)
  77. #define FLEXCAN_CTRL_LOM BIT(3)
  78. #define FLEXCAN_CTRL_PROPSEG(x) ((x) & 0x07)
  79. #define FLEXCAN_CTRL_ERR_BUS (FLEXCAN_CTRL_ERR_MSK)
  80. #define FLEXCAN_CTRL_ERR_STATE \
  81. (FLEXCAN_CTRL_TWRN_MSK | FLEXCAN_CTRL_RWRN_MSK | \
  82. FLEXCAN_CTRL_BOFF_MSK)
  83. #define FLEXCAN_CTRL_ERR_ALL \
  84. (FLEXCAN_CTRL_ERR_BUS | FLEXCAN_CTRL_ERR_STATE)
  85. /* FLEXCAN control register 2 (CTRL2) bits */
  86. #define FLEXCAN_CTRL2_ECRWRE BIT(29)
  87. #define FLEXCAN_CTRL2_WRMFRZ BIT(28)
  88. #define FLEXCAN_CTRL2_RFFN(x) (((x) & 0x0f) << 24)
  89. #define FLEXCAN_CTRL2_TASD(x) (((x) & 0x1f) << 19)
  90. #define FLEXCAN_CTRL2_MRP BIT(18)
  91. #define FLEXCAN_CTRL2_RRS BIT(17)
  92. #define FLEXCAN_CTRL2_EACEN BIT(16)
  93. /* FLEXCAN memory error control register (MECR) bits */
  94. #define FLEXCAN_MECR_ECRWRDIS BIT(31)
  95. #define FLEXCAN_MECR_HANCEI_MSK BIT(19)
  96. #define FLEXCAN_MECR_FANCEI_MSK BIT(18)
  97. #define FLEXCAN_MECR_CEI_MSK BIT(16)
  98. #define FLEXCAN_MECR_HAERRIE BIT(15)
  99. #define FLEXCAN_MECR_FAERRIE BIT(14)
  100. #define FLEXCAN_MECR_EXTERRIE BIT(13)
  101. #define FLEXCAN_MECR_RERRDIS BIT(9)
  102. #define FLEXCAN_MECR_ECCDIS BIT(8)
  103. #define FLEXCAN_MECR_NCEFAFRZ BIT(7)
  104. /* FLEXCAN error and status register (ESR) bits */
  105. #define FLEXCAN_ESR_TWRN_INT BIT(17)
  106. #define FLEXCAN_ESR_RWRN_INT BIT(16)
  107. #define FLEXCAN_ESR_BIT1_ERR BIT(15)
  108. #define FLEXCAN_ESR_BIT0_ERR BIT(14)
  109. #define FLEXCAN_ESR_ACK_ERR BIT(13)
  110. #define FLEXCAN_ESR_CRC_ERR BIT(12)
  111. #define FLEXCAN_ESR_FRM_ERR BIT(11)
  112. #define FLEXCAN_ESR_STF_ERR BIT(10)
  113. #define FLEXCAN_ESR_TX_WRN BIT(9)
  114. #define FLEXCAN_ESR_RX_WRN BIT(8)
  115. #define FLEXCAN_ESR_IDLE BIT(7)
  116. #define FLEXCAN_ESR_TXRX BIT(6)
  117. #define FLEXCAN_EST_FLT_CONF_SHIFT (4)
  118. #define FLEXCAN_ESR_FLT_CONF_MASK (0x3 << FLEXCAN_EST_FLT_CONF_SHIFT)
  119. #define FLEXCAN_ESR_FLT_CONF_ACTIVE (0x0 << FLEXCAN_EST_FLT_CONF_SHIFT)
  120. #define FLEXCAN_ESR_FLT_CONF_PASSIVE (0x1 << FLEXCAN_EST_FLT_CONF_SHIFT)
  121. #define FLEXCAN_ESR_BOFF_INT BIT(2)
  122. #define FLEXCAN_ESR_ERR_INT BIT(1)
  123. #define FLEXCAN_ESR_WAK_INT BIT(0)
  124. #define FLEXCAN_ESR_ERR_BUS \
  125. (FLEXCAN_ESR_BIT1_ERR | FLEXCAN_ESR_BIT0_ERR | \
  126. FLEXCAN_ESR_ACK_ERR | FLEXCAN_ESR_CRC_ERR | \
  127. FLEXCAN_ESR_FRM_ERR | FLEXCAN_ESR_STF_ERR)
  128. #define FLEXCAN_ESR_ERR_STATE \
  129. (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | FLEXCAN_ESR_BOFF_INT)
  130. #define FLEXCAN_ESR_ERR_ALL \
  131. (FLEXCAN_ESR_ERR_BUS | FLEXCAN_ESR_ERR_STATE)
  132. #define FLEXCAN_ESR_ALL_INT \
  133. (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | \
  134. FLEXCAN_ESR_BOFF_INT | FLEXCAN_ESR_ERR_INT)
  135. /* FLEXCAN interrupt flag register (IFLAG) bits */
  136. /* Errata ERR005829 step7: Reserve first valid MB */
  137. #define FLEXCAN_TX_BUF_RESERVED 8
  138. #define FLEXCAN_TX_BUF_ID 9
  139. #define FLEXCAN_IFLAG_BUF(x) BIT(x)
  140. #define FLEXCAN_IFLAG_RX_FIFO_OVERFLOW BIT(7)
  141. #define FLEXCAN_IFLAG_RX_FIFO_WARN BIT(6)
  142. #define FLEXCAN_IFLAG_RX_FIFO_AVAILABLE BIT(5)
  143. #define FLEXCAN_IFLAG_DEFAULT \
  144. (FLEXCAN_IFLAG_RX_FIFO_OVERFLOW | FLEXCAN_IFLAG_RX_FIFO_AVAILABLE | \
  145. FLEXCAN_IFLAG_BUF(FLEXCAN_TX_BUF_ID))
  146. /* FLEXCAN message buffers */
  147. #define FLEXCAN_MB_CODE_RX_INACTIVE (0x0 << 24)
  148. #define FLEXCAN_MB_CODE_RX_EMPTY (0x4 << 24)
  149. #define FLEXCAN_MB_CODE_RX_FULL (0x2 << 24)
  150. #define FLEXCAN_MB_CODE_RX_OVERRUN (0x6 << 24)
  151. #define FLEXCAN_MB_CODE_RX_RANSWER (0xa << 24)
  152. #define FLEXCAN_MB_CODE_TX_INACTIVE (0x8 << 24)
  153. #define FLEXCAN_MB_CODE_TX_ABORT (0x9 << 24)
  154. #define FLEXCAN_MB_CODE_TX_DATA (0xc << 24)
  155. #define FLEXCAN_MB_CODE_TX_TANSWER (0xe << 24)
  156. #define FLEXCAN_MB_CNT_SRR BIT(22)
  157. #define FLEXCAN_MB_CNT_IDE BIT(21)
  158. #define FLEXCAN_MB_CNT_RTR BIT(20)
  159. #define FLEXCAN_MB_CNT_LENGTH(x) (((x) & 0xf) << 16)
  160. #define FLEXCAN_MB_CNT_TIMESTAMP(x) ((x) & 0xffff)
  161. #define FLEXCAN_TIMEOUT_US (50)
  162. /* FLEXCAN hardware feature flags
  163. *
  164. * Below is some version info we got:
  165. * SOC Version IP-Version Glitch- [TR]WRN_INT Memory err RTR re-
  166. * Filter? connected? detection ception in MB
  167. * MX25 FlexCAN2 03.00.00.00 no no no no
  168. * MX28 FlexCAN2 03.00.04.00 yes yes no no
  169. * MX35 FlexCAN2 03.00.00.00 no no no no
  170. * MX53 FlexCAN2 03.00.00.00 yes no no no
  171. * MX6s FlexCAN3 10.00.12.00 yes yes no yes
  172. * VF610 FlexCAN3 ? no yes yes yes?
  173. *
  174. * Some SOCs do not have the RX_WARN & TX_WARN interrupt line connected.
  175. */
  176. #define FLEXCAN_QUIRK_BROKEN_ERR_STATE BIT(1) /* [TR]WRN_INT not connected */
  177. #define FLEXCAN_QUIRK_DISABLE_RXFG BIT(2) /* Disable RX FIFO Global mask */
  178. #define FLEXCAN_QUIRK_DISABLE_MECR BIT(3) /* Disble Memory error detection */
  179. /* Structure of the message buffer */
  180. struct flexcan_mb {
  181. u32 can_ctrl;
  182. u32 can_id;
  183. u32 data[2];
  184. };
  185. /* Structure of the hardware registers */
  186. struct flexcan_regs {
  187. u32 mcr; /* 0x00 */
  188. u32 ctrl; /* 0x04 */
  189. u32 timer; /* 0x08 */
  190. u32 _reserved1; /* 0x0c */
  191. u32 rxgmask; /* 0x10 */
  192. u32 rx14mask; /* 0x14 */
  193. u32 rx15mask; /* 0x18 */
  194. u32 ecr; /* 0x1c */
  195. u32 esr; /* 0x20 */
  196. u32 imask2; /* 0x24 */
  197. u32 imask1; /* 0x28 */
  198. u32 iflag2; /* 0x2c */
  199. u32 iflag1; /* 0x30 */
  200. u32 ctrl2; /* 0x34 */
  201. u32 esr2; /* 0x38 */
  202. u32 imeur; /* 0x3c */
  203. u32 lrfr; /* 0x40 */
  204. u32 crcr; /* 0x44 */
  205. u32 rxfgmask; /* 0x48 */
  206. u32 rxfir; /* 0x4c */
  207. u32 _reserved3[12]; /* 0x50 */
  208. struct flexcan_mb mb[64]; /* 0x80 */
  209. /* FIFO-mode:
  210. * MB
  211. * 0x080...0x08f 0 RX message buffer
  212. * 0x090...0x0df 1-5 reserverd
  213. * 0x0e0...0x0ff 6-7 8 entry ID table
  214. * (mx25, mx28, mx35, mx53)
  215. * 0x0e0...0x2df 6-7..37 8..128 entry ID table
  216. * size conf'ed via ctrl2::RFFN
  217. * (mx6, vf610)
  218. */
  219. u32 _reserved4[408];
  220. u32 mecr; /* 0xae0 */
  221. u32 erriar; /* 0xae4 */
  222. u32 erridpr; /* 0xae8 */
  223. u32 errippr; /* 0xaec */
  224. u32 rerrar; /* 0xaf0 */
  225. u32 rerrdr; /* 0xaf4 */
  226. u32 rerrsynr; /* 0xaf8 */
  227. u32 errsr; /* 0xafc */
  228. };
  229. struct flexcan_devtype_data {
  230. u32 quirks; /* quirks needed for different IP cores */
  231. };
  232. struct flexcan_priv {
  233. struct can_priv can;
  234. struct napi_struct napi;
  235. struct flexcan_regs __iomem *regs;
  236. u32 reg_esr;
  237. u32 reg_ctrl_default;
  238. struct clk *clk_ipg;
  239. struct clk *clk_per;
  240. struct flexcan_platform_data *pdata;
  241. const struct flexcan_devtype_data *devtype_data;
  242. struct regulator *reg_xceiver;
  243. };
  244. static struct flexcan_devtype_data fsl_p1010_devtype_data = {
  245. .quirks = FLEXCAN_QUIRK_BROKEN_ERR_STATE,
  246. };
  247. static struct flexcan_devtype_data fsl_imx28_devtype_data;
  248. static struct flexcan_devtype_data fsl_imx6q_devtype_data = {
  249. .quirks = FLEXCAN_QUIRK_DISABLE_RXFG,
  250. };
  251. static struct flexcan_devtype_data fsl_vf610_devtype_data = {
  252. .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_DISABLE_MECR,
  253. };
  254. static const struct can_bittiming_const flexcan_bittiming_const = {
  255. .name = DRV_NAME,
  256. .tseg1_min = 4,
  257. .tseg1_max = 16,
  258. .tseg2_min = 2,
  259. .tseg2_max = 8,
  260. .sjw_max = 4,
  261. .brp_min = 1,
  262. .brp_max = 256,
  263. .brp_inc = 1,
  264. };
  265. /* Abstract off the read/write for arm versus ppc. This
  266. * assumes that PPC uses big-endian registers and everything
  267. * else uses little-endian registers, independent of CPU
  268. * endianness.
  269. */
  270. #if defined(CONFIG_PPC)
  271. static inline u32 flexcan_read(void __iomem *addr)
  272. {
  273. return in_be32(addr);
  274. }
  275. static inline void flexcan_write(u32 val, void __iomem *addr)
  276. {
  277. out_be32(addr, val);
  278. }
  279. #else
  280. static inline u32 flexcan_read(void __iomem *addr)
  281. {
  282. return readl(addr);
  283. }
  284. static inline void flexcan_write(u32 val, void __iomem *addr)
  285. {
  286. writel(val, addr);
  287. }
  288. #endif
  289. static inline int flexcan_transceiver_enable(const struct flexcan_priv *priv)
  290. {
  291. if (!priv->reg_xceiver)
  292. return 0;
  293. return regulator_enable(priv->reg_xceiver);
  294. }
  295. static inline int flexcan_transceiver_disable(const struct flexcan_priv *priv)
  296. {
  297. if (!priv->reg_xceiver)
  298. return 0;
  299. return regulator_disable(priv->reg_xceiver);
  300. }
  301. static inline int flexcan_has_and_handle_berr(const struct flexcan_priv *priv,
  302. u32 reg_esr)
  303. {
  304. return (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
  305. (reg_esr & FLEXCAN_ESR_ERR_BUS);
  306. }
  307. static int flexcan_chip_enable(struct flexcan_priv *priv)
  308. {
  309. struct flexcan_regs __iomem *regs = priv->regs;
  310. unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
  311. u32 reg;
  312. reg = flexcan_read(&regs->mcr);
  313. reg &= ~FLEXCAN_MCR_MDIS;
  314. flexcan_write(reg, &regs->mcr);
  315. while (timeout-- && (flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
  316. udelay(10);
  317. if (flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK)
  318. return -ETIMEDOUT;
  319. return 0;
  320. }
  321. static int flexcan_chip_disable(struct flexcan_priv *priv)
  322. {
  323. struct flexcan_regs __iomem *regs = priv->regs;
  324. unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
  325. u32 reg;
  326. reg = flexcan_read(&regs->mcr);
  327. reg |= FLEXCAN_MCR_MDIS;
  328. flexcan_write(reg, &regs->mcr);
  329. while (timeout-- && !(flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
  330. udelay(10);
  331. if (!(flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
  332. return -ETIMEDOUT;
  333. return 0;
  334. }
  335. static int flexcan_chip_freeze(struct flexcan_priv *priv)
  336. {
  337. struct flexcan_regs __iomem *regs = priv->regs;
  338. unsigned int timeout = 1000 * 1000 * 10 / priv->can.bittiming.bitrate;
  339. u32 reg;
  340. reg = flexcan_read(&regs->mcr);
  341. reg |= FLEXCAN_MCR_HALT;
  342. flexcan_write(reg, &regs->mcr);
  343. while (timeout-- && !(flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
  344. udelay(100);
  345. if (!(flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
  346. return -ETIMEDOUT;
  347. return 0;
  348. }
  349. static int flexcan_chip_unfreeze(struct flexcan_priv *priv)
  350. {
  351. struct flexcan_regs __iomem *regs = priv->regs;
  352. unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
  353. u32 reg;
  354. reg = flexcan_read(&regs->mcr);
  355. reg &= ~FLEXCAN_MCR_HALT;
  356. flexcan_write(reg, &regs->mcr);
  357. while (timeout-- && (flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
  358. udelay(10);
  359. if (flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK)
  360. return -ETIMEDOUT;
  361. return 0;
  362. }
  363. static int flexcan_chip_softreset(struct flexcan_priv *priv)
  364. {
  365. struct flexcan_regs __iomem *regs = priv->regs;
  366. unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
  367. flexcan_write(FLEXCAN_MCR_SOFTRST, &regs->mcr);
  368. while (timeout-- && (flexcan_read(&regs->mcr) & FLEXCAN_MCR_SOFTRST))
  369. udelay(10);
  370. if (flexcan_read(&regs->mcr) & FLEXCAN_MCR_SOFTRST)
  371. return -ETIMEDOUT;
  372. return 0;
  373. }
  374. static int __flexcan_get_berr_counter(const struct net_device *dev,
  375. struct can_berr_counter *bec)
  376. {
  377. const struct flexcan_priv *priv = netdev_priv(dev);
  378. struct flexcan_regs __iomem *regs = priv->regs;
  379. u32 reg = flexcan_read(&regs->ecr);
  380. bec->txerr = (reg >> 0) & 0xff;
  381. bec->rxerr = (reg >> 8) & 0xff;
  382. return 0;
  383. }
  384. static int flexcan_get_berr_counter(const struct net_device *dev,
  385. struct can_berr_counter *bec)
  386. {
  387. const struct flexcan_priv *priv = netdev_priv(dev);
  388. int err;
  389. err = clk_prepare_enable(priv->clk_ipg);
  390. if (err)
  391. return err;
  392. err = clk_prepare_enable(priv->clk_per);
  393. if (err)
  394. goto out_disable_ipg;
  395. err = __flexcan_get_berr_counter(dev, bec);
  396. clk_disable_unprepare(priv->clk_per);
  397. out_disable_ipg:
  398. clk_disable_unprepare(priv->clk_ipg);
  399. return err;
  400. }
  401. static int flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
  402. {
  403. const struct flexcan_priv *priv = netdev_priv(dev);
  404. struct flexcan_regs __iomem *regs = priv->regs;
  405. struct can_frame *cf = (struct can_frame *)skb->data;
  406. u32 can_id;
  407. u32 data;
  408. u32 ctrl = FLEXCAN_MB_CODE_TX_DATA | (cf->can_dlc << 16);
  409. if (can_dropped_invalid_skb(dev, skb))
  410. return NETDEV_TX_OK;
  411. netif_stop_queue(dev);
  412. if (cf->can_id & CAN_EFF_FLAG) {
  413. can_id = cf->can_id & CAN_EFF_MASK;
  414. ctrl |= FLEXCAN_MB_CNT_IDE | FLEXCAN_MB_CNT_SRR;
  415. } else {
  416. can_id = (cf->can_id & CAN_SFF_MASK) << 18;
  417. }
  418. if (cf->can_id & CAN_RTR_FLAG)
  419. ctrl |= FLEXCAN_MB_CNT_RTR;
  420. if (cf->can_dlc > 0) {
  421. data = be32_to_cpup((__be32 *)&cf->data[0]);
  422. flexcan_write(data, &regs->mb[FLEXCAN_TX_BUF_ID].data[0]);
  423. }
  424. if (cf->can_dlc > 4) {
  425. data = be32_to_cpup((__be32 *)&cf->data[4]);
  426. flexcan_write(data, &regs->mb[FLEXCAN_TX_BUF_ID].data[1]);
  427. }
  428. can_put_echo_skb(skb, dev, 0);
  429. flexcan_write(can_id, &regs->mb[FLEXCAN_TX_BUF_ID].can_id);
  430. flexcan_write(ctrl, &regs->mb[FLEXCAN_TX_BUF_ID].can_ctrl);
  431. /* Errata ERR005829 step8:
  432. * Write twice INACTIVE(0x8) code to first MB.
  433. */
  434. flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
  435. &regs->mb[FLEXCAN_TX_BUF_RESERVED].can_ctrl);
  436. flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
  437. &regs->mb[FLEXCAN_TX_BUF_RESERVED].can_ctrl);
  438. return NETDEV_TX_OK;
  439. }
  440. static void do_bus_err(struct net_device *dev,
  441. struct can_frame *cf, u32 reg_esr)
  442. {
  443. struct flexcan_priv *priv = netdev_priv(dev);
  444. int rx_errors = 0, tx_errors = 0;
  445. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  446. if (reg_esr & FLEXCAN_ESR_BIT1_ERR) {
  447. netdev_dbg(dev, "BIT1_ERR irq\n");
  448. cf->data[2] |= CAN_ERR_PROT_BIT1;
  449. tx_errors = 1;
  450. }
  451. if (reg_esr & FLEXCAN_ESR_BIT0_ERR) {
  452. netdev_dbg(dev, "BIT0_ERR irq\n");
  453. cf->data[2] |= CAN_ERR_PROT_BIT0;
  454. tx_errors = 1;
  455. }
  456. if (reg_esr & FLEXCAN_ESR_ACK_ERR) {
  457. netdev_dbg(dev, "ACK_ERR irq\n");
  458. cf->can_id |= CAN_ERR_ACK;
  459. cf->data[3] = CAN_ERR_PROT_LOC_ACK;
  460. tx_errors = 1;
  461. }
  462. if (reg_esr & FLEXCAN_ESR_CRC_ERR) {
  463. netdev_dbg(dev, "CRC_ERR irq\n");
  464. cf->data[2] |= CAN_ERR_PROT_BIT;
  465. cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
  466. rx_errors = 1;
  467. }
  468. if (reg_esr & FLEXCAN_ESR_FRM_ERR) {
  469. netdev_dbg(dev, "FRM_ERR irq\n");
  470. cf->data[2] |= CAN_ERR_PROT_FORM;
  471. rx_errors = 1;
  472. }
  473. if (reg_esr & FLEXCAN_ESR_STF_ERR) {
  474. netdev_dbg(dev, "STF_ERR irq\n");
  475. cf->data[2] |= CAN_ERR_PROT_STUFF;
  476. rx_errors = 1;
  477. }
  478. priv->can.can_stats.bus_error++;
  479. if (rx_errors)
  480. dev->stats.rx_errors++;
  481. if (tx_errors)
  482. dev->stats.tx_errors++;
  483. }
  484. static int flexcan_poll_bus_err(struct net_device *dev, u32 reg_esr)
  485. {
  486. struct sk_buff *skb;
  487. struct can_frame *cf;
  488. skb = alloc_can_err_skb(dev, &cf);
  489. if (unlikely(!skb))
  490. return 0;
  491. do_bus_err(dev, cf, reg_esr);
  492. dev->stats.rx_packets++;
  493. dev->stats.rx_bytes += cf->can_dlc;
  494. netif_receive_skb(skb);
  495. return 1;
  496. }
  497. static int flexcan_poll_state(struct net_device *dev, u32 reg_esr)
  498. {
  499. struct flexcan_priv *priv = netdev_priv(dev);
  500. struct sk_buff *skb;
  501. struct can_frame *cf;
  502. enum can_state new_state = 0, rx_state = 0, tx_state = 0;
  503. int flt;
  504. struct can_berr_counter bec;
  505. flt = reg_esr & FLEXCAN_ESR_FLT_CONF_MASK;
  506. if (likely(flt == FLEXCAN_ESR_FLT_CONF_ACTIVE)) {
  507. tx_state = unlikely(reg_esr & FLEXCAN_ESR_TX_WRN) ?
  508. CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
  509. rx_state = unlikely(reg_esr & FLEXCAN_ESR_RX_WRN) ?
  510. CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
  511. new_state = max(tx_state, rx_state);
  512. } else {
  513. __flexcan_get_berr_counter(dev, &bec);
  514. new_state = flt == FLEXCAN_ESR_FLT_CONF_PASSIVE ?
  515. CAN_STATE_ERROR_PASSIVE : CAN_STATE_BUS_OFF;
  516. rx_state = bec.rxerr >= bec.txerr ? new_state : 0;
  517. tx_state = bec.rxerr <= bec.txerr ? new_state : 0;
  518. }
  519. /* state hasn't changed */
  520. if (likely(new_state == priv->can.state))
  521. return 0;
  522. skb = alloc_can_err_skb(dev, &cf);
  523. if (unlikely(!skb))
  524. return 0;
  525. can_change_state(dev, cf, tx_state, rx_state);
  526. if (unlikely(new_state == CAN_STATE_BUS_OFF))
  527. can_bus_off(dev);
  528. dev->stats.rx_packets++;
  529. dev->stats.rx_bytes += cf->can_dlc;
  530. netif_receive_skb(skb);
  531. return 1;
  532. }
  533. static void flexcan_read_fifo(const struct net_device *dev,
  534. struct can_frame *cf)
  535. {
  536. const struct flexcan_priv *priv = netdev_priv(dev);
  537. struct flexcan_regs __iomem *regs = priv->regs;
  538. struct flexcan_mb __iomem *mb = &regs->mb[0];
  539. u32 reg_ctrl, reg_id;
  540. reg_ctrl = flexcan_read(&mb->can_ctrl);
  541. reg_id = flexcan_read(&mb->can_id);
  542. if (reg_ctrl & FLEXCAN_MB_CNT_IDE)
  543. cf->can_id = ((reg_id >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG;
  544. else
  545. cf->can_id = (reg_id >> 18) & CAN_SFF_MASK;
  546. if (reg_ctrl & FLEXCAN_MB_CNT_RTR)
  547. cf->can_id |= CAN_RTR_FLAG;
  548. cf->can_dlc = get_can_dlc((reg_ctrl >> 16) & 0xf);
  549. *(__be32 *)(cf->data + 0) = cpu_to_be32(flexcan_read(&mb->data[0]));
  550. *(__be32 *)(cf->data + 4) = cpu_to_be32(flexcan_read(&mb->data[1]));
  551. /* mark as read */
  552. flexcan_write(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, &regs->iflag1);
  553. flexcan_read(&regs->timer);
  554. }
  555. static int flexcan_read_frame(struct net_device *dev)
  556. {
  557. struct net_device_stats *stats = &dev->stats;
  558. struct can_frame *cf;
  559. struct sk_buff *skb;
  560. skb = alloc_can_skb(dev, &cf);
  561. if (unlikely(!skb)) {
  562. stats->rx_dropped++;
  563. return 0;
  564. }
  565. flexcan_read_fifo(dev, cf);
  566. stats->rx_packets++;
  567. stats->rx_bytes += cf->can_dlc;
  568. netif_receive_skb(skb);
  569. can_led_event(dev, CAN_LED_EVENT_RX);
  570. return 1;
  571. }
  572. static int flexcan_poll(struct napi_struct *napi, int quota)
  573. {
  574. struct net_device *dev = napi->dev;
  575. const struct flexcan_priv *priv = netdev_priv(dev);
  576. struct flexcan_regs __iomem *regs = priv->regs;
  577. u32 reg_iflag1, reg_esr;
  578. int work_done = 0;
  579. /* The error bits are cleared on read,
  580. * use saved value from irq handler.
  581. */
  582. reg_esr = flexcan_read(&regs->esr) | priv->reg_esr;
  583. /* handle state changes */
  584. work_done += flexcan_poll_state(dev, reg_esr);
  585. /* handle RX-FIFO */
  586. reg_iflag1 = flexcan_read(&regs->iflag1);
  587. while (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE &&
  588. work_done < quota) {
  589. work_done += flexcan_read_frame(dev);
  590. reg_iflag1 = flexcan_read(&regs->iflag1);
  591. }
  592. /* report bus errors */
  593. if (flexcan_has_and_handle_berr(priv, reg_esr) && work_done < quota)
  594. work_done += flexcan_poll_bus_err(dev, reg_esr);
  595. if (work_done < quota) {
  596. napi_complete(napi);
  597. /* enable IRQs */
  598. flexcan_write(FLEXCAN_IFLAG_DEFAULT, &regs->imask1);
  599. flexcan_write(priv->reg_ctrl_default, &regs->ctrl);
  600. }
  601. return work_done;
  602. }
  603. static irqreturn_t flexcan_irq(int irq, void *dev_id)
  604. {
  605. struct net_device *dev = dev_id;
  606. struct net_device_stats *stats = &dev->stats;
  607. struct flexcan_priv *priv = netdev_priv(dev);
  608. struct flexcan_regs __iomem *regs = priv->regs;
  609. u32 reg_iflag1, reg_esr;
  610. reg_iflag1 = flexcan_read(&regs->iflag1);
  611. reg_esr = flexcan_read(&regs->esr);
  612. /* ACK all bus error and state change IRQ sources */
  613. if (reg_esr & FLEXCAN_ESR_ALL_INT)
  614. flexcan_write(reg_esr & FLEXCAN_ESR_ALL_INT, &regs->esr);
  615. /* schedule NAPI in case of:
  616. * - rx IRQ
  617. * - state change IRQ
  618. * - bus error IRQ and bus error reporting is activated
  619. */
  620. if ((reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE) ||
  621. (reg_esr & FLEXCAN_ESR_ERR_STATE) ||
  622. flexcan_has_and_handle_berr(priv, reg_esr)) {
  623. /* The error bits are cleared on read,
  624. * save them for later use.
  625. */
  626. priv->reg_esr = reg_esr & FLEXCAN_ESR_ERR_BUS;
  627. flexcan_write(FLEXCAN_IFLAG_DEFAULT &
  628. ~FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, &regs->imask1);
  629. flexcan_write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
  630. &regs->ctrl);
  631. napi_schedule(&priv->napi);
  632. }
  633. /* FIFO overflow */
  634. if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_OVERFLOW) {
  635. flexcan_write(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW, &regs->iflag1);
  636. dev->stats.rx_over_errors++;
  637. dev->stats.rx_errors++;
  638. }
  639. /* transmission complete interrupt */
  640. if (reg_iflag1 & (1 << FLEXCAN_TX_BUF_ID)) {
  641. stats->tx_bytes += can_get_echo_skb(dev, 0);
  642. stats->tx_packets++;
  643. can_led_event(dev, CAN_LED_EVENT_TX);
  644. /* after sending a RTR frame MB is in RX mode */
  645. flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
  646. &regs->mb[FLEXCAN_TX_BUF_ID].can_ctrl);
  647. flexcan_write((1 << FLEXCAN_TX_BUF_ID), &regs->iflag1);
  648. netif_wake_queue(dev);
  649. }
  650. return IRQ_HANDLED;
  651. }
  652. static void flexcan_set_bittiming(struct net_device *dev)
  653. {
  654. const struct flexcan_priv *priv = netdev_priv(dev);
  655. const struct can_bittiming *bt = &priv->can.bittiming;
  656. struct flexcan_regs __iomem *regs = priv->regs;
  657. u32 reg;
  658. reg = flexcan_read(&regs->ctrl);
  659. reg &= ~(FLEXCAN_CTRL_PRESDIV(0xff) |
  660. FLEXCAN_CTRL_RJW(0x3) |
  661. FLEXCAN_CTRL_PSEG1(0x7) |
  662. FLEXCAN_CTRL_PSEG2(0x7) |
  663. FLEXCAN_CTRL_PROPSEG(0x7) |
  664. FLEXCAN_CTRL_LPB |
  665. FLEXCAN_CTRL_SMP |
  666. FLEXCAN_CTRL_LOM);
  667. reg |= FLEXCAN_CTRL_PRESDIV(bt->brp - 1) |
  668. FLEXCAN_CTRL_PSEG1(bt->phase_seg1 - 1) |
  669. FLEXCAN_CTRL_PSEG2(bt->phase_seg2 - 1) |
  670. FLEXCAN_CTRL_RJW(bt->sjw - 1) |
  671. FLEXCAN_CTRL_PROPSEG(bt->prop_seg - 1);
  672. if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
  673. reg |= FLEXCAN_CTRL_LPB;
  674. if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  675. reg |= FLEXCAN_CTRL_LOM;
  676. if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  677. reg |= FLEXCAN_CTRL_SMP;
  678. netdev_dbg(dev, "writing ctrl=0x%08x\n", reg);
  679. flexcan_write(reg, &regs->ctrl);
  680. /* print chip status */
  681. netdev_dbg(dev, "%s: mcr=0x%08x ctrl=0x%08x\n", __func__,
  682. flexcan_read(&regs->mcr), flexcan_read(&regs->ctrl));
  683. }
  684. /* flexcan_chip_start
  685. *
  686. * this functions is entered with clocks enabled
  687. *
  688. */
  689. static int flexcan_chip_start(struct net_device *dev)
  690. {
  691. struct flexcan_priv *priv = netdev_priv(dev);
  692. struct flexcan_regs __iomem *regs = priv->regs;
  693. u32 reg_mcr, reg_ctrl, reg_ctrl2, reg_mecr;
  694. int err, i;
  695. /* enable module */
  696. err = flexcan_chip_enable(priv);
  697. if (err)
  698. return err;
  699. /* soft reset */
  700. err = flexcan_chip_softreset(priv);
  701. if (err)
  702. goto out_chip_disable;
  703. flexcan_set_bittiming(dev);
  704. /* MCR
  705. *
  706. * enable freeze
  707. * enable fifo
  708. * halt now
  709. * only supervisor access
  710. * enable warning int
  711. * disable local echo
  712. * choose format C
  713. * set max mailbox number
  714. */
  715. reg_mcr = flexcan_read(&regs->mcr);
  716. reg_mcr &= ~FLEXCAN_MCR_MAXMB(0xff);
  717. reg_mcr |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_FEN | FLEXCAN_MCR_HALT |
  718. FLEXCAN_MCR_SUPV | FLEXCAN_MCR_WRN_EN | FLEXCAN_MCR_SRX_DIS |
  719. FLEXCAN_MCR_IDAM_C | FLEXCAN_MCR_MAXMB(FLEXCAN_TX_BUF_ID);
  720. netdev_dbg(dev, "%s: writing mcr=0x%08x", __func__, reg_mcr);
  721. flexcan_write(reg_mcr, &regs->mcr);
  722. /* CTRL
  723. *
  724. * disable timer sync feature
  725. *
  726. * disable auto busoff recovery
  727. * transmit lowest buffer first
  728. *
  729. * enable tx and rx warning interrupt
  730. * enable bus off interrupt
  731. * (== FLEXCAN_CTRL_ERR_STATE)
  732. */
  733. reg_ctrl = flexcan_read(&regs->ctrl);
  734. reg_ctrl &= ~FLEXCAN_CTRL_TSYN;
  735. reg_ctrl |= FLEXCAN_CTRL_BOFF_REC | FLEXCAN_CTRL_LBUF |
  736. FLEXCAN_CTRL_ERR_STATE;
  737. /* enable the "error interrupt" (FLEXCAN_CTRL_ERR_MSK),
  738. * on most Flexcan cores, too. Otherwise we don't get
  739. * any error warning or passive interrupts.
  740. */
  741. if (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_ERR_STATE ||
  742. priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
  743. reg_ctrl |= FLEXCAN_CTRL_ERR_MSK;
  744. else
  745. reg_ctrl &= ~FLEXCAN_CTRL_ERR_MSK;
  746. /* save for later use */
  747. priv->reg_ctrl_default = reg_ctrl;
  748. /* leave interrupts disabled for now */
  749. reg_ctrl &= ~FLEXCAN_CTRL_ERR_ALL;
  750. netdev_dbg(dev, "%s: writing ctrl=0x%08x", __func__, reg_ctrl);
  751. flexcan_write(reg_ctrl, &regs->ctrl);
  752. /* clear and invalidate all mailboxes first */
  753. for (i = FLEXCAN_TX_BUF_ID; i < ARRAY_SIZE(regs->mb); i++) {
  754. flexcan_write(FLEXCAN_MB_CODE_RX_INACTIVE,
  755. &regs->mb[i].can_ctrl);
  756. }
  757. /* Errata ERR005829: mark first TX mailbox as INACTIVE */
  758. flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
  759. &regs->mb[FLEXCAN_TX_BUF_RESERVED].can_ctrl);
  760. /* mark TX mailbox as INACTIVE */
  761. flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
  762. &regs->mb[FLEXCAN_TX_BUF_ID].can_ctrl);
  763. /* acceptance mask/acceptance code (accept everything) */
  764. flexcan_write(0x0, &regs->rxgmask);
  765. flexcan_write(0x0, &regs->rx14mask);
  766. flexcan_write(0x0, &regs->rx15mask);
  767. if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_RXFG)
  768. flexcan_write(0x0, &regs->rxfgmask);
  769. /* On Vybrid, disable memory error detection interrupts
  770. * and freeze mode.
  771. * This also works around errata e5295 which generates
  772. * false positive memory errors and put the device in
  773. * freeze mode.
  774. */
  775. if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_MECR) {
  776. /* Follow the protocol as described in "Detection
  777. * and Correction of Memory Errors" to write to
  778. * MECR register
  779. */
  780. reg_ctrl2 = flexcan_read(&regs->ctrl2);
  781. reg_ctrl2 |= FLEXCAN_CTRL2_ECRWRE;
  782. flexcan_write(reg_ctrl2, &regs->ctrl2);
  783. reg_mecr = flexcan_read(&regs->mecr);
  784. reg_mecr &= ~FLEXCAN_MECR_ECRWRDIS;
  785. flexcan_write(reg_mecr, &regs->mecr);
  786. reg_mecr &= ~(FLEXCAN_MECR_NCEFAFRZ | FLEXCAN_MECR_HANCEI_MSK |
  787. FLEXCAN_MECR_FANCEI_MSK);
  788. flexcan_write(reg_mecr, &regs->mecr);
  789. }
  790. err = flexcan_transceiver_enable(priv);
  791. if (err)
  792. goto out_chip_disable;
  793. /* synchronize with the can bus */
  794. err = flexcan_chip_unfreeze(priv);
  795. if (err)
  796. goto out_transceiver_disable;
  797. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  798. /* enable interrupts atomically */
  799. disable_irq(dev->irq);
  800. flexcan_write(priv->reg_ctrl_default, &regs->ctrl);
  801. flexcan_write(FLEXCAN_IFLAG_DEFAULT, &regs->imask1);
  802. enable_irq(dev->irq);
  803. /* print chip status */
  804. netdev_dbg(dev, "%s: reading mcr=0x%08x ctrl=0x%08x\n", __func__,
  805. flexcan_read(&regs->mcr), flexcan_read(&regs->ctrl));
  806. return 0;
  807. out_transceiver_disable:
  808. flexcan_transceiver_disable(priv);
  809. out_chip_disable:
  810. flexcan_chip_disable(priv);
  811. return err;
  812. }
  813. /* flexcan_chip_stop
  814. *
  815. * this functions is entered with clocks enabled
  816. */
  817. static void flexcan_chip_stop(struct net_device *dev)
  818. {
  819. struct flexcan_priv *priv = netdev_priv(dev);
  820. struct flexcan_regs __iomem *regs = priv->regs;
  821. /* freeze + disable module */
  822. flexcan_chip_freeze(priv);
  823. flexcan_chip_disable(priv);
  824. /* Disable all interrupts */
  825. flexcan_write(0, &regs->imask1);
  826. flexcan_write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
  827. &regs->ctrl);
  828. flexcan_transceiver_disable(priv);
  829. priv->can.state = CAN_STATE_STOPPED;
  830. }
  831. static int flexcan_open(struct net_device *dev)
  832. {
  833. struct flexcan_priv *priv = netdev_priv(dev);
  834. int err;
  835. err = clk_prepare_enable(priv->clk_ipg);
  836. if (err)
  837. return err;
  838. err = clk_prepare_enable(priv->clk_per);
  839. if (err)
  840. goto out_disable_ipg;
  841. err = open_candev(dev);
  842. if (err)
  843. goto out_disable_per;
  844. err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev);
  845. if (err)
  846. goto out_close;
  847. /* start chip and queuing */
  848. err = flexcan_chip_start(dev);
  849. if (err)
  850. goto out_free_irq;
  851. can_led_event(dev, CAN_LED_EVENT_OPEN);
  852. napi_enable(&priv->napi);
  853. netif_start_queue(dev);
  854. return 0;
  855. out_free_irq:
  856. free_irq(dev->irq, dev);
  857. out_close:
  858. close_candev(dev);
  859. out_disable_per:
  860. clk_disable_unprepare(priv->clk_per);
  861. out_disable_ipg:
  862. clk_disable_unprepare(priv->clk_ipg);
  863. return err;
  864. }
  865. static int flexcan_close(struct net_device *dev)
  866. {
  867. struct flexcan_priv *priv = netdev_priv(dev);
  868. netif_stop_queue(dev);
  869. napi_disable(&priv->napi);
  870. flexcan_chip_stop(dev);
  871. free_irq(dev->irq, dev);
  872. clk_disable_unprepare(priv->clk_per);
  873. clk_disable_unprepare(priv->clk_ipg);
  874. close_candev(dev);
  875. can_led_event(dev, CAN_LED_EVENT_STOP);
  876. return 0;
  877. }
  878. static int flexcan_set_mode(struct net_device *dev, enum can_mode mode)
  879. {
  880. int err;
  881. switch (mode) {
  882. case CAN_MODE_START:
  883. err = flexcan_chip_start(dev);
  884. if (err)
  885. return err;
  886. netif_wake_queue(dev);
  887. break;
  888. default:
  889. return -EOPNOTSUPP;
  890. }
  891. return 0;
  892. }
  893. static const struct net_device_ops flexcan_netdev_ops = {
  894. .ndo_open = flexcan_open,
  895. .ndo_stop = flexcan_close,
  896. .ndo_start_xmit = flexcan_start_xmit,
  897. .ndo_change_mtu = can_change_mtu,
  898. };
  899. static int register_flexcandev(struct net_device *dev)
  900. {
  901. struct flexcan_priv *priv = netdev_priv(dev);
  902. struct flexcan_regs __iomem *regs = priv->regs;
  903. u32 reg, err;
  904. err = clk_prepare_enable(priv->clk_ipg);
  905. if (err)
  906. return err;
  907. err = clk_prepare_enable(priv->clk_per);
  908. if (err)
  909. goto out_disable_ipg;
  910. /* select "bus clock", chip must be disabled */
  911. err = flexcan_chip_disable(priv);
  912. if (err)
  913. goto out_disable_per;
  914. reg = flexcan_read(&regs->ctrl);
  915. reg |= FLEXCAN_CTRL_CLK_SRC;
  916. flexcan_write(reg, &regs->ctrl);
  917. err = flexcan_chip_enable(priv);
  918. if (err)
  919. goto out_chip_disable;
  920. /* set freeze, halt and activate FIFO, restrict register access */
  921. reg = flexcan_read(&regs->mcr);
  922. reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT |
  923. FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV;
  924. flexcan_write(reg, &regs->mcr);
  925. /* Currently we only support newer versions of this core
  926. * featuring a RX FIFO. Older cores found on some Coldfire
  927. * derivates are not yet supported.
  928. */
  929. reg = flexcan_read(&regs->mcr);
  930. if (!(reg & FLEXCAN_MCR_FEN)) {
  931. netdev_err(dev, "Could not enable RX FIFO, unsupported core\n");
  932. err = -ENODEV;
  933. goto out_chip_disable;
  934. }
  935. err = register_candev(dev);
  936. /* disable core and turn off clocks */
  937. out_chip_disable:
  938. flexcan_chip_disable(priv);
  939. out_disable_per:
  940. clk_disable_unprepare(priv->clk_per);
  941. out_disable_ipg:
  942. clk_disable_unprepare(priv->clk_ipg);
  943. return err;
  944. }
  945. static void unregister_flexcandev(struct net_device *dev)
  946. {
  947. unregister_candev(dev);
  948. }
  949. static const struct of_device_id flexcan_of_match[] = {
  950. { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, },
  951. { .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, },
  952. { .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, },
  953. { .compatible = "fsl,vf610-flexcan", .data = &fsl_vf610_devtype_data, },
  954. { /* sentinel */ },
  955. };
  956. MODULE_DEVICE_TABLE(of, flexcan_of_match);
  957. static const struct platform_device_id flexcan_id_table[] = {
  958. { .name = "flexcan", .driver_data = (kernel_ulong_t)&fsl_p1010_devtype_data, },
  959. { /* sentinel */ },
  960. };
  961. MODULE_DEVICE_TABLE(platform, flexcan_id_table);
  962. static int flexcan_probe(struct platform_device *pdev)
  963. {
  964. const struct of_device_id *of_id;
  965. const struct flexcan_devtype_data *devtype_data;
  966. struct net_device *dev;
  967. struct flexcan_priv *priv;
  968. struct regulator *reg_xceiver;
  969. struct resource *mem;
  970. struct clk *clk_ipg = NULL, *clk_per = NULL;
  971. struct flexcan_regs __iomem *regs;
  972. int err, irq;
  973. u32 clock_freq = 0;
  974. reg_xceiver = devm_regulator_get(&pdev->dev, "xceiver");
  975. if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
  976. return -EPROBE_DEFER;
  977. else if (IS_ERR(reg_xceiver))
  978. reg_xceiver = NULL;
  979. if (pdev->dev.of_node)
  980. of_property_read_u32(pdev->dev.of_node,
  981. "clock-frequency", &clock_freq);
  982. if (!clock_freq) {
  983. clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  984. if (IS_ERR(clk_ipg)) {
  985. dev_err(&pdev->dev, "no ipg clock defined\n");
  986. return PTR_ERR(clk_ipg);
  987. }
  988. clk_per = devm_clk_get(&pdev->dev, "per");
  989. if (IS_ERR(clk_per)) {
  990. dev_err(&pdev->dev, "no per clock defined\n");
  991. return PTR_ERR(clk_per);
  992. }
  993. clock_freq = clk_get_rate(clk_per);
  994. }
  995. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  996. irq = platform_get_irq(pdev, 0);
  997. if (irq <= 0)
  998. return -ENODEV;
  999. regs = devm_ioremap_resource(&pdev->dev, mem);
  1000. if (IS_ERR(regs))
  1001. return PTR_ERR(regs);
  1002. of_id = of_match_device(flexcan_of_match, &pdev->dev);
  1003. if (of_id) {
  1004. devtype_data = of_id->data;
  1005. } else if (platform_get_device_id(pdev)->driver_data) {
  1006. devtype_data = (struct flexcan_devtype_data *)
  1007. platform_get_device_id(pdev)->driver_data;
  1008. } else {
  1009. return -ENODEV;
  1010. }
  1011. dev = alloc_candev(sizeof(struct flexcan_priv), 1);
  1012. if (!dev)
  1013. return -ENOMEM;
  1014. dev->netdev_ops = &flexcan_netdev_ops;
  1015. dev->irq = irq;
  1016. dev->flags |= IFF_ECHO;
  1017. priv = netdev_priv(dev);
  1018. priv->can.clock.freq = clock_freq;
  1019. priv->can.bittiming_const = &flexcan_bittiming_const;
  1020. priv->can.do_set_mode = flexcan_set_mode;
  1021. priv->can.do_get_berr_counter = flexcan_get_berr_counter;
  1022. priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
  1023. CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_3_SAMPLES |
  1024. CAN_CTRLMODE_BERR_REPORTING;
  1025. priv->regs = regs;
  1026. priv->clk_ipg = clk_ipg;
  1027. priv->clk_per = clk_per;
  1028. priv->pdata = dev_get_platdata(&pdev->dev);
  1029. priv->devtype_data = devtype_data;
  1030. priv->reg_xceiver = reg_xceiver;
  1031. netif_napi_add(dev, &priv->napi, flexcan_poll, FLEXCAN_NAPI_WEIGHT);
  1032. platform_set_drvdata(pdev, dev);
  1033. SET_NETDEV_DEV(dev, &pdev->dev);
  1034. err = register_flexcandev(dev);
  1035. if (err) {
  1036. dev_err(&pdev->dev, "registering netdev failed\n");
  1037. goto failed_register;
  1038. }
  1039. devm_can_led_init(dev);
  1040. dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
  1041. priv->regs, dev->irq);
  1042. return 0;
  1043. failed_register:
  1044. free_candev(dev);
  1045. return err;
  1046. }
  1047. static int flexcan_remove(struct platform_device *pdev)
  1048. {
  1049. struct net_device *dev = platform_get_drvdata(pdev);
  1050. struct flexcan_priv *priv = netdev_priv(dev);
  1051. unregister_flexcandev(dev);
  1052. netif_napi_del(&priv->napi);
  1053. free_candev(dev);
  1054. return 0;
  1055. }
  1056. static int __maybe_unused flexcan_suspend(struct device *device)
  1057. {
  1058. struct net_device *dev = dev_get_drvdata(device);
  1059. struct flexcan_priv *priv = netdev_priv(dev);
  1060. int err;
  1061. if (netif_running(dev)) {
  1062. err = flexcan_chip_disable(priv);
  1063. if (err)
  1064. return err;
  1065. netif_stop_queue(dev);
  1066. netif_device_detach(dev);
  1067. }
  1068. priv->can.state = CAN_STATE_SLEEPING;
  1069. return 0;
  1070. }
  1071. static int __maybe_unused flexcan_resume(struct device *device)
  1072. {
  1073. struct net_device *dev = dev_get_drvdata(device);
  1074. struct flexcan_priv *priv = netdev_priv(dev);
  1075. int err;
  1076. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  1077. if (netif_running(dev)) {
  1078. netif_device_attach(dev);
  1079. netif_start_queue(dev);
  1080. err = flexcan_chip_enable(priv);
  1081. if (err)
  1082. return err;
  1083. }
  1084. return 0;
  1085. }
  1086. static SIMPLE_DEV_PM_OPS(flexcan_pm_ops, flexcan_suspend, flexcan_resume);
  1087. static struct platform_driver flexcan_driver = {
  1088. .driver = {
  1089. .name = DRV_NAME,
  1090. .pm = &flexcan_pm_ops,
  1091. .of_match_table = flexcan_of_match,
  1092. },
  1093. .probe = flexcan_probe,
  1094. .remove = flexcan_remove,
  1095. .id_table = flexcan_id_table,
  1096. };
  1097. module_platform_driver(flexcan_driver);
  1098. MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, "
  1099. "Marc Kleine-Budde <kernel@pengutronix.de>");
  1100. MODULE_LICENSE("GPL v2");
  1101. MODULE_DESCRIPTION("CAN port driver for flexcan based chip");