flexcan.c 49 KB

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