flexcan.c 41 KB

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