flexcan.c 41 KB

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