flexcan.c 41 KB

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