rcar_canfd.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  1. /* Renesas R-Car CAN FD device driver
  2. *
  3. * Copyright (C) 2015 Renesas Electronics Corp.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. */
  10. /* The R-Car CAN FD controller can operate in either one of the below two modes
  11. * - CAN FD only mode
  12. * - Classical CAN (CAN 2.0) only mode
  13. *
  14. * This driver puts the controller in CAN FD only mode by default. In this
  15. * mode, the controller acts as a CAN FD node that can also interoperate with
  16. * CAN 2.0 nodes.
  17. *
  18. * To switch the controller to Classical CAN (CAN 2.0) only mode, add
  19. * "renesas,no-can-fd" optional property to the device tree node. A h/w reset is
  20. * also required to switch modes.
  21. *
  22. * Note: The h/w manual register naming convention is clumsy and not acceptable
  23. * to use as it is in the driver. However, those names are added as comments
  24. * wherever it is modified to a readable name.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/kernel.h>
  29. #include <linux/types.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/errno.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/can/led.h>
  35. #include <linux/can/dev.h>
  36. #include <linux/clk.h>
  37. #include <linux/of.h>
  38. #include <linux/of_device.h>
  39. #include <linux/bitmap.h>
  40. #include <linux/bitops.h>
  41. #include <linux/iopoll.h>
  42. #define RCANFD_DRV_NAME "rcar_canfd"
  43. /* Global register bits */
  44. /* RSCFDnCFDGRMCFG */
  45. #define RCANFD_GRMCFG_RCMC BIT(0)
  46. /* RSCFDnCFDGCFG / RSCFDnGCFG */
  47. #define RCANFD_GCFG_EEFE BIT(6)
  48. #define RCANFD_GCFG_CMPOC BIT(5) /* CAN FD only */
  49. #define RCANFD_GCFG_DCS BIT(4)
  50. #define RCANFD_GCFG_DCE BIT(1)
  51. #define RCANFD_GCFG_TPRI BIT(0)
  52. /* RSCFDnCFDGCTR / RSCFDnGCTR */
  53. #define RCANFD_GCTR_TSRST BIT(16)
  54. #define RCANFD_GCTR_CFMPOFIE BIT(11) /* CAN FD only */
  55. #define RCANFD_GCTR_THLEIE BIT(10)
  56. #define RCANFD_GCTR_MEIE BIT(9)
  57. #define RCANFD_GCTR_DEIE BIT(8)
  58. #define RCANFD_GCTR_GSLPR BIT(2)
  59. #define RCANFD_GCTR_GMDC_MASK (0x3)
  60. #define RCANFD_GCTR_GMDC_GOPM (0x0)
  61. #define RCANFD_GCTR_GMDC_GRESET (0x1)
  62. #define RCANFD_GCTR_GMDC_GTEST (0x2)
  63. /* RSCFDnCFDGSTS / RSCFDnGSTS */
  64. #define RCANFD_GSTS_GRAMINIT BIT(3)
  65. #define RCANFD_GSTS_GSLPSTS BIT(2)
  66. #define RCANFD_GSTS_GHLTSTS BIT(1)
  67. #define RCANFD_GSTS_GRSTSTS BIT(0)
  68. /* Non-operational status */
  69. #define RCANFD_GSTS_GNOPM (BIT(0) | BIT(1) | BIT(2) | BIT(3))
  70. /* RSCFDnCFDGERFL / RSCFDnGERFL */
  71. #define RCANFD_GERFL_EEF1 BIT(17)
  72. #define RCANFD_GERFL_EEF0 BIT(16)
  73. #define RCANFD_GERFL_CMPOF BIT(3) /* CAN FD only */
  74. #define RCANFD_GERFL_THLES BIT(2)
  75. #define RCANFD_GERFL_MES BIT(1)
  76. #define RCANFD_GERFL_DEF BIT(0)
  77. #define RCANFD_GERFL_ERR(gpriv, x) ((x) & (RCANFD_GERFL_EEF1 |\
  78. RCANFD_GERFL_EEF0 | RCANFD_GERFL_MES |\
  79. (gpriv->fdmode ?\
  80. RCANFD_GERFL_CMPOF : 0)))
  81. /* AFL Rx rules registers */
  82. /* RSCFDnCFDGAFLCFG0 / RSCFDnGAFLCFG0 */
  83. #define RCANFD_GAFLCFG_SETRNC(n, x) (((x) & 0xff) << (24 - n * 8))
  84. #define RCANFD_GAFLCFG_GETRNC(n, x) (((x) >> (24 - n * 8)) & 0xff)
  85. /* RSCFDnCFDGAFLECTR / RSCFDnGAFLECTR */
  86. #define RCANFD_GAFLECTR_AFLDAE BIT(8)
  87. #define RCANFD_GAFLECTR_AFLPN(x) ((x) & 0x1f)
  88. /* RSCFDnCFDGAFLIDj / RSCFDnGAFLIDj */
  89. #define RCANFD_GAFLID_GAFLLB BIT(29)
  90. /* RSCFDnCFDGAFLP1_j / RSCFDnGAFLP1_j */
  91. #define RCANFD_GAFLP1_GAFLFDP(x) (1 << (x))
  92. /* Channel register bits */
  93. /* RSCFDnCmCFG - Classical CAN only */
  94. #define RCANFD_CFG_SJW(x) (((x) & 0x3) << 24)
  95. #define RCANFD_CFG_TSEG2(x) (((x) & 0x7) << 20)
  96. #define RCANFD_CFG_TSEG1(x) (((x) & 0xf) << 16)
  97. #define RCANFD_CFG_BRP(x) (((x) & 0x3ff) << 0)
  98. /* RSCFDnCFDCmNCFG - CAN FD only */
  99. #define RCANFD_NCFG_NTSEG2(x) (((x) & 0x1f) << 24)
  100. #define RCANFD_NCFG_NTSEG1(x) (((x) & 0x7f) << 16)
  101. #define RCANFD_NCFG_NSJW(x) (((x) & 0x1f) << 11)
  102. #define RCANFD_NCFG_NBRP(x) (((x) & 0x3ff) << 0)
  103. /* RSCFDnCFDCmCTR / RSCFDnCmCTR */
  104. #define RCANFD_CCTR_CTME BIT(24)
  105. #define RCANFD_CCTR_ERRD BIT(23)
  106. #define RCANFD_CCTR_BOM_MASK (0x3 << 21)
  107. #define RCANFD_CCTR_BOM_ISO (0x0 << 21)
  108. #define RCANFD_CCTR_BOM_BENTRY (0x1 << 21)
  109. #define RCANFD_CCTR_BOM_BEND (0x2 << 21)
  110. #define RCANFD_CCTR_TDCVFIE BIT(19)
  111. #define RCANFD_CCTR_SOCOIE BIT(18)
  112. #define RCANFD_CCTR_EOCOIE BIT(17)
  113. #define RCANFD_CCTR_TAIE BIT(16)
  114. #define RCANFD_CCTR_ALIE BIT(15)
  115. #define RCANFD_CCTR_BLIE BIT(14)
  116. #define RCANFD_CCTR_OLIE BIT(13)
  117. #define RCANFD_CCTR_BORIE BIT(12)
  118. #define RCANFD_CCTR_BOEIE BIT(11)
  119. #define RCANFD_CCTR_EPIE BIT(10)
  120. #define RCANFD_CCTR_EWIE BIT(9)
  121. #define RCANFD_CCTR_BEIE BIT(8)
  122. #define RCANFD_CCTR_CSLPR BIT(2)
  123. #define RCANFD_CCTR_CHMDC_MASK (0x3)
  124. #define RCANFD_CCTR_CHDMC_COPM (0x0)
  125. #define RCANFD_CCTR_CHDMC_CRESET (0x1)
  126. #define RCANFD_CCTR_CHDMC_CHLT (0x2)
  127. /* RSCFDnCFDCmSTS / RSCFDnCmSTS */
  128. #define RCANFD_CSTS_COMSTS BIT(7)
  129. #define RCANFD_CSTS_RECSTS BIT(6)
  130. #define RCANFD_CSTS_TRMSTS BIT(5)
  131. #define RCANFD_CSTS_BOSTS BIT(4)
  132. #define RCANFD_CSTS_EPSTS BIT(3)
  133. #define RCANFD_CSTS_SLPSTS BIT(2)
  134. #define RCANFD_CSTS_HLTSTS BIT(1)
  135. #define RCANFD_CSTS_CRSTSTS BIT(0)
  136. #define RCANFD_CSTS_TECCNT(x) (((x) >> 24) & 0xff)
  137. #define RCANFD_CSTS_RECCNT(x) (((x) >> 16) & 0xff)
  138. /* RSCFDnCFDCmERFL / RSCFDnCmERFL */
  139. #define RCANFD_CERFL_ADERR BIT(14)
  140. #define RCANFD_CERFL_B0ERR BIT(13)
  141. #define RCANFD_CERFL_B1ERR BIT(12)
  142. #define RCANFD_CERFL_CERR BIT(11)
  143. #define RCANFD_CERFL_AERR BIT(10)
  144. #define RCANFD_CERFL_FERR BIT(9)
  145. #define RCANFD_CERFL_SERR BIT(8)
  146. #define RCANFD_CERFL_ALF BIT(7)
  147. #define RCANFD_CERFL_BLF BIT(6)
  148. #define RCANFD_CERFL_OVLF BIT(5)
  149. #define RCANFD_CERFL_BORF BIT(4)
  150. #define RCANFD_CERFL_BOEF BIT(3)
  151. #define RCANFD_CERFL_EPF BIT(2)
  152. #define RCANFD_CERFL_EWF BIT(1)
  153. #define RCANFD_CERFL_BEF BIT(0)
  154. #define RCANFD_CERFL_ERR(x) ((x) & (0x7fff)) /* above bits 14:0 */
  155. /* RSCFDnCFDCmDCFG */
  156. #define RCANFD_DCFG_DSJW(x) (((x) & 0x7) << 24)
  157. #define RCANFD_DCFG_DTSEG2(x) (((x) & 0x7) << 20)
  158. #define RCANFD_DCFG_DTSEG1(x) (((x) & 0xf) << 16)
  159. #define RCANFD_DCFG_DBRP(x) (((x) & 0xff) << 0)
  160. /* RSCFDnCFDCmFDCFG */
  161. #define RCANFD_FDCFG_TDCE BIT(9)
  162. #define RCANFD_FDCFG_TDCOC BIT(8)
  163. #define RCANFD_FDCFG_TDCO(x) (((x) & 0x7f) >> 16)
  164. /* RSCFDnCFDRFCCx */
  165. #define RCANFD_RFCC_RFIM BIT(12)
  166. #define RCANFD_RFCC_RFDC(x) (((x) & 0x7) << 8)
  167. #define RCANFD_RFCC_RFPLS(x) (((x) & 0x7) << 4)
  168. #define RCANFD_RFCC_RFIE BIT(1)
  169. #define RCANFD_RFCC_RFE BIT(0)
  170. /* RSCFDnCFDRFSTSx */
  171. #define RCANFD_RFSTS_RFIF BIT(3)
  172. #define RCANFD_RFSTS_RFMLT BIT(2)
  173. #define RCANFD_RFSTS_RFFLL BIT(1)
  174. #define RCANFD_RFSTS_RFEMP BIT(0)
  175. /* RSCFDnCFDRFIDx */
  176. #define RCANFD_RFID_RFIDE BIT(31)
  177. #define RCANFD_RFID_RFRTR BIT(30)
  178. /* RSCFDnCFDRFPTRx */
  179. #define RCANFD_RFPTR_RFDLC(x) (((x) >> 28) & 0xf)
  180. #define RCANFD_RFPTR_RFPTR(x) (((x) >> 16) & 0xfff)
  181. #define RCANFD_RFPTR_RFTS(x) (((x) >> 0) & 0xffff)
  182. /* RSCFDnCFDRFFDSTSx */
  183. #define RCANFD_RFFDSTS_RFFDF BIT(2)
  184. #define RCANFD_RFFDSTS_RFBRS BIT(1)
  185. #define RCANFD_RFFDSTS_RFESI BIT(0)
  186. /* Common FIFO bits */
  187. /* RSCFDnCFDCFCCk */
  188. #define RCANFD_CFCC_CFTML(x) (((x) & 0xf) << 20)
  189. #define RCANFD_CFCC_CFM(x) (((x) & 0x3) << 16)
  190. #define RCANFD_CFCC_CFIM BIT(12)
  191. #define RCANFD_CFCC_CFDC(x) (((x) & 0x7) << 8)
  192. #define RCANFD_CFCC_CFPLS(x) (((x) & 0x7) << 4)
  193. #define RCANFD_CFCC_CFTXIE BIT(2)
  194. #define RCANFD_CFCC_CFE BIT(0)
  195. /* RSCFDnCFDCFSTSk */
  196. #define RCANFD_CFSTS_CFMC(x) (((x) >> 8) & 0xff)
  197. #define RCANFD_CFSTS_CFTXIF BIT(4)
  198. #define RCANFD_CFSTS_CFMLT BIT(2)
  199. #define RCANFD_CFSTS_CFFLL BIT(1)
  200. #define RCANFD_CFSTS_CFEMP BIT(0)
  201. /* RSCFDnCFDCFIDk */
  202. #define RCANFD_CFID_CFIDE BIT(31)
  203. #define RCANFD_CFID_CFRTR BIT(30)
  204. #define RCANFD_CFID_CFID_MASK(x) ((x) & 0x1fffffff)
  205. /* RSCFDnCFDCFPTRk */
  206. #define RCANFD_CFPTR_CFDLC(x) (((x) & 0xf) << 28)
  207. #define RCANFD_CFPTR_CFPTR(x) (((x) & 0xfff) << 16)
  208. #define RCANFD_CFPTR_CFTS(x) (((x) & 0xff) << 0)
  209. /* RSCFDnCFDCFFDCSTSk */
  210. #define RCANFD_CFFDCSTS_CFFDF BIT(2)
  211. #define RCANFD_CFFDCSTS_CFBRS BIT(1)
  212. #define RCANFD_CFFDCSTS_CFESI BIT(0)
  213. /* This controller supports either Classical CAN only mode or CAN FD only mode.
  214. * These modes are supported in two separate set of register maps & names.
  215. * However, some of the register offsets are common for both modes. Those
  216. * offsets are listed below as Common registers.
  217. *
  218. * The CAN FD only mode specific registers & Classical CAN only mode specific
  219. * registers are listed separately. Their register names starts with
  220. * RCANFD_F_xxx & RCANFD_C_xxx respectively.
  221. */
  222. /* Common registers */
  223. /* RSCFDnCFDCmNCFG / RSCFDnCmCFG */
  224. #define RCANFD_CCFG(m) (0x0000 + (0x10 * (m)))
  225. /* RSCFDnCFDCmCTR / RSCFDnCmCTR */
  226. #define RCANFD_CCTR(m) (0x0004 + (0x10 * (m)))
  227. /* RSCFDnCFDCmSTS / RSCFDnCmSTS */
  228. #define RCANFD_CSTS(m) (0x0008 + (0x10 * (m)))
  229. /* RSCFDnCFDCmERFL / RSCFDnCmERFL */
  230. #define RCANFD_CERFL(m) (0x000C + (0x10 * (m)))
  231. /* RSCFDnCFDGCFG / RSCFDnGCFG */
  232. #define RCANFD_GCFG (0x0084)
  233. /* RSCFDnCFDGCTR / RSCFDnGCTR */
  234. #define RCANFD_GCTR (0x0088)
  235. /* RSCFDnCFDGCTS / RSCFDnGCTS */
  236. #define RCANFD_GSTS (0x008c)
  237. /* RSCFDnCFDGERFL / RSCFDnGERFL */
  238. #define RCANFD_GERFL (0x0090)
  239. /* RSCFDnCFDGTSC / RSCFDnGTSC */
  240. #define RCANFD_GTSC (0x0094)
  241. /* RSCFDnCFDGAFLECTR / RSCFDnGAFLECTR */
  242. #define RCANFD_GAFLECTR (0x0098)
  243. /* RSCFDnCFDGAFLCFG0 / RSCFDnGAFLCFG0 */
  244. #define RCANFD_GAFLCFG0 (0x009c)
  245. /* RSCFDnCFDGAFLCFG1 / RSCFDnGAFLCFG1 */
  246. #define RCANFD_GAFLCFG1 (0x00a0)
  247. /* RSCFDnCFDRMNB / RSCFDnRMNB */
  248. #define RCANFD_RMNB (0x00a4)
  249. /* RSCFDnCFDRMND / RSCFDnRMND */
  250. #define RCANFD_RMND(y) (0x00a8 + (0x04 * (y)))
  251. /* RSCFDnCFDRFCCx / RSCFDnRFCCx */
  252. #define RCANFD_RFCC(x) (0x00b8 + (0x04 * (x)))
  253. /* RSCFDnCFDRFSTSx / RSCFDnRFSTSx */
  254. #define RCANFD_RFSTS(x) (0x00d8 + (0x04 * (x)))
  255. /* RSCFDnCFDRFPCTRx / RSCFDnRFPCTRx */
  256. #define RCANFD_RFPCTR(x) (0x00f8 + (0x04 * (x)))
  257. /* Common FIFO Control registers */
  258. /* RSCFDnCFDCFCCx / RSCFDnCFCCx */
  259. #define RCANFD_CFCC(ch, idx) (0x0118 + (0x0c * (ch)) + \
  260. (0x04 * (idx)))
  261. /* RSCFDnCFDCFSTSx / RSCFDnCFSTSx */
  262. #define RCANFD_CFSTS(ch, idx) (0x0178 + (0x0c * (ch)) + \
  263. (0x04 * (idx)))
  264. /* RSCFDnCFDCFPCTRx / RSCFDnCFPCTRx */
  265. #define RCANFD_CFPCTR(ch, idx) (0x01d8 + (0x0c * (ch)) + \
  266. (0x04 * (idx)))
  267. /* RSCFDnCFDFESTS / RSCFDnFESTS */
  268. #define RCANFD_FESTS (0x0238)
  269. /* RSCFDnCFDFFSTS / RSCFDnFFSTS */
  270. #define RCANFD_FFSTS (0x023c)
  271. /* RSCFDnCFDFMSTS / RSCFDnFMSTS */
  272. #define RCANFD_FMSTS (0x0240)
  273. /* RSCFDnCFDRFISTS / RSCFDnRFISTS */
  274. #define RCANFD_RFISTS (0x0244)
  275. /* RSCFDnCFDCFRISTS / RSCFDnCFRISTS */
  276. #define RCANFD_CFRISTS (0x0248)
  277. /* RSCFDnCFDCFTISTS / RSCFDnCFTISTS */
  278. #define RCANFD_CFTISTS (0x024c)
  279. /* RSCFDnCFDTMCp / RSCFDnTMCp */
  280. #define RCANFD_TMC(p) (0x0250 + (0x01 * (p)))
  281. /* RSCFDnCFDTMSTSp / RSCFDnTMSTSp */
  282. #define RCANFD_TMSTS(p) (0x02d0 + (0x01 * (p)))
  283. /* RSCFDnCFDTMTRSTSp / RSCFDnTMTRSTSp */
  284. #define RCANFD_TMTRSTS(y) (0x0350 + (0x04 * (y)))
  285. /* RSCFDnCFDTMTARSTSp / RSCFDnTMTARSTSp */
  286. #define RCANFD_TMTARSTS(y) (0x0360 + (0x04 * (y)))
  287. /* RSCFDnCFDTMTCSTSp / RSCFDnTMTCSTSp */
  288. #define RCANFD_TMTCSTS(y) (0x0370 + (0x04 * (y)))
  289. /* RSCFDnCFDTMTASTSp / RSCFDnTMTASTSp */
  290. #define RCANFD_TMTASTS(y) (0x0380 + (0x04 * (y)))
  291. /* RSCFDnCFDTMIECy / RSCFDnTMIECy */
  292. #define RCANFD_TMIEC(y) (0x0390 + (0x04 * (y)))
  293. /* RSCFDnCFDTXQCCm / RSCFDnTXQCCm */
  294. #define RCANFD_TXQCC(m) (0x03a0 + (0x04 * (m)))
  295. /* RSCFDnCFDTXQSTSm / RSCFDnTXQSTSm */
  296. #define RCANFD_TXQSTS(m) (0x03c0 + (0x04 * (m)))
  297. /* RSCFDnCFDTXQPCTRm / RSCFDnTXQPCTRm */
  298. #define RCANFD_TXQPCTR(m) (0x03e0 + (0x04 * (m)))
  299. /* RSCFDnCFDTHLCCm / RSCFDnTHLCCm */
  300. #define RCANFD_THLCC(m) (0x0400 + (0x04 * (m)))
  301. /* RSCFDnCFDTHLSTSm / RSCFDnTHLSTSm */
  302. #define RCANFD_THLSTS(m) (0x0420 + (0x04 * (m)))
  303. /* RSCFDnCFDTHLPCTRm / RSCFDnTHLPCTRm */
  304. #define RCANFD_THLPCTR(m) (0x0440 + (0x04 * (m)))
  305. /* RSCFDnCFDGTINTSTS0 / RSCFDnGTINTSTS0 */
  306. #define RCANFD_GTINTSTS0 (0x0460)
  307. /* RSCFDnCFDGTINTSTS1 / RSCFDnGTINTSTS1 */
  308. #define RCANFD_GTINTSTS1 (0x0464)
  309. /* RSCFDnCFDGTSTCFG / RSCFDnGTSTCFG */
  310. #define RCANFD_GTSTCFG (0x0468)
  311. /* RSCFDnCFDGTSTCTR / RSCFDnGTSTCTR */
  312. #define RCANFD_GTSTCTR (0x046c)
  313. /* RSCFDnCFDGLOCKK / RSCFDnGLOCKK */
  314. #define RCANFD_GLOCKK (0x047c)
  315. /* RSCFDnCFDGRMCFG */
  316. #define RCANFD_GRMCFG (0x04fc)
  317. /* RSCFDnCFDGAFLIDj / RSCFDnGAFLIDj */
  318. #define RCANFD_GAFLID(offset, j) ((offset) + (0x10 * (j)))
  319. /* RSCFDnCFDGAFLMj / RSCFDnGAFLMj */
  320. #define RCANFD_GAFLM(offset, j) ((offset) + 0x04 + (0x10 * (j)))
  321. /* RSCFDnCFDGAFLP0j / RSCFDnGAFLP0j */
  322. #define RCANFD_GAFLP0(offset, j) ((offset) + 0x08 + (0x10 * (j)))
  323. /* RSCFDnCFDGAFLP1j / RSCFDnGAFLP1j */
  324. #define RCANFD_GAFLP1(offset, j) ((offset) + 0x0c + (0x10 * (j)))
  325. /* Classical CAN only mode register map */
  326. /* RSCFDnGAFLXXXj offset */
  327. #define RCANFD_C_GAFL_OFFSET (0x0500)
  328. /* RSCFDnRMXXXq -> RCANFD_C_RMXXX(q) */
  329. #define RCANFD_C_RMID(q) (0x0600 + (0x10 * (q)))
  330. #define RCANFD_C_RMPTR(q) (0x0604 + (0x10 * (q)))
  331. #define RCANFD_C_RMDF0(q) (0x0608 + (0x10 * (q)))
  332. #define RCANFD_C_RMDF1(q) (0x060c + (0x10 * (q)))
  333. /* RSCFDnRFXXx -> RCANFD_C_RFXX(x) */
  334. #define RCANFD_C_RFOFFSET (0x0e00)
  335. #define RCANFD_C_RFID(x) (RCANFD_C_RFOFFSET + (0x10 * (x)))
  336. #define RCANFD_C_RFPTR(x) (RCANFD_C_RFOFFSET + 0x04 + \
  337. (0x10 * (x)))
  338. #define RCANFD_C_RFDF(x, df) (RCANFD_C_RFOFFSET + 0x08 + \
  339. (0x10 * (x)) + (0x04 * (df)))
  340. /* RSCFDnCFXXk -> RCANFD_C_CFXX(ch, k) */
  341. #define RCANFD_C_CFOFFSET (0x0e80)
  342. #define RCANFD_C_CFID(ch, idx) (RCANFD_C_CFOFFSET + (0x30 * (ch)) + \
  343. (0x10 * (idx)))
  344. #define RCANFD_C_CFPTR(ch, idx) (RCANFD_C_CFOFFSET + 0x04 + \
  345. (0x30 * (ch)) + (0x10 * (idx)))
  346. #define RCANFD_C_CFDF(ch, idx, df) (RCANFD_C_CFOFFSET + 0x08 + \
  347. (0x30 * (ch)) + (0x10 * (idx)) + \
  348. (0x04 * (df)))
  349. /* RSCFDnTMXXp -> RCANFD_C_TMXX(p) */
  350. #define RCANFD_C_TMID(p) (0x1000 + (0x10 * (p)))
  351. #define RCANFD_C_TMPTR(p) (0x1004 + (0x10 * (p)))
  352. #define RCANFD_C_TMDF0(p) (0x1008 + (0x10 * (p)))
  353. #define RCANFD_C_TMDF1(p) (0x100c + (0x10 * (p)))
  354. /* RSCFDnTHLACCm */
  355. #define RCANFD_C_THLACC(m) (0x1800 + (0x04 * (m)))
  356. /* RSCFDnRPGACCr */
  357. #define RCANFD_C_RPGACC(r) (0x1900 + (0x04 * (r)))
  358. /* CAN FD mode specific register map */
  359. /* RSCFDnCFDCmXXX -> RCANFD_F_XXX(m) */
  360. #define RCANFD_F_DCFG(m) (0x0500 + (0x20 * (m)))
  361. #define RCANFD_F_CFDCFG(m) (0x0504 + (0x20 * (m)))
  362. #define RCANFD_F_CFDCTR(m) (0x0508 + (0x20 * (m)))
  363. #define RCANFD_F_CFDSTS(m) (0x050c + (0x20 * (m)))
  364. #define RCANFD_F_CFDCRC(m) (0x0510 + (0x20 * (m)))
  365. /* RSCFDnCFDGAFLXXXj offset */
  366. #define RCANFD_F_GAFL_OFFSET (0x1000)
  367. /* RSCFDnCFDRMXXXq -> RCANFD_F_RMXXX(q) */
  368. #define RCANFD_F_RMID(q) (0x2000 + (0x20 * (q)))
  369. #define RCANFD_F_RMPTR(q) (0x2004 + (0x20 * (q)))
  370. #define RCANFD_F_RMFDSTS(q) (0x2008 + (0x20 * (q)))
  371. #define RCANFD_F_RMDF(q, b) (0x200c + (0x04 * (b)) + (0x20 * (q)))
  372. /* RSCFDnCFDRFXXx -> RCANFD_F_RFXX(x) */
  373. #define RCANFD_F_RFOFFSET (0x3000)
  374. #define RCANFD_F_RFID(x) (RCANFD_F_RFOFFSET + (0x80 * (x)))
  375. #define RCANFD_F_RFPTR(x) (RCANFD_F_RFOFFSET + 0x04 + \
  376. (0x80 * (x)))
  377. #define RCANFD_F_RFFDSTS(x) (RCANFD_F_RFOFFSET + 0x08 + \
  378. (0x80 * (x)))
  379. #define RCANFD_F_RFDF(x, df) (RCANFD_F_RFOFFSET + 0x0c + \
  380. (0x80 * (x)) + (0x04 * (df)))
  381. /* RSCFDnCFDCFXXk -> RCANFD_F_CFXX(ch, k) */
  382. #define RCANFD_F_CFOFFSET (0x3400)
  383. #define RCANFD_F_CFID(ch, idx) (RCANFD_F_CFOFFSET + (0x180 * (ch)) + \
  384. (0x80 * (idx)))
  385. #define RCANFD_F_CFPTR(ch, idx) (RCANFD_F_CFOFFSET + 0x04 + \
  386. (0x180 * (ch)) + (0x80 * (idx)))
  387. #define RCANFD_F_CFFDCSTS(ch, idx) (RCANFD_F_CFOFFSET + 0x08 + \
  388. (0x180 * (ch)) + (0x80 * (idx)))
  389. #define RCANFD_F_CFDF(ch, idx, df) (RCANFD_F_CFOFFSET + 0x0c + \
  390. (0x180 * (ch)) + (0x80 * (idx)) + \
  391. (0x04 * (df)))
  392. /* RSCFDnCFDTMXXp -> RCANFD_F_TMXX(p) */
  393. #define RCANFD_F_TMID(p) (0x4000 + (0x20 * (p)))
  394. #define RCANFD_F_TMPTR(p) (0x4004 + (0x20 * (p)))
  395. #define RCANFD_F_TMFDCTR(p) (0x4008 + (0x20 * (p)))
  396. #define RCANFD_F_TMDF(p, b) (0x400c + (0x20 * (p)) + (0x04 * (b)))
  397. /* RSCFDnCFDTHLACCm */
  398. #define RCANFD_F_THLACC(m) (0x6000 + (0x04 * (m)))
  399. /* RSCFDnCFDRPGACCr */
  400. #define RCANFD_F_RPGACC(r) (0x6400 + (0x04 * (r)))
  401. /* Constants */
  402. #define RCANFD_FIFO_DEPTH 8 /* Tx FIFO depth */
  403. #define RCANFD_NAPI_WEIGHT 8 /* Rx poll quota */
  404. #define RCANFD_NUM_CHANNELS 2 /* Two channels max */
  405. #define RCANFD_CHANNELS_MASK BIT((RCANFD_NUM_CHANNELS) - 1)
  406. #define RCANFD_GAFL_PAGENUM(entry) ((entry) / 16)
  407. #define RCANFD_CHANNEL_NUMRULES 1 /* only one rule per channel */
  408. /* Rx FIFO is a global resource of the controller. There are 8 such FIFOs
  409. * available. Each channel gets a dedicated Rx FIFO (i.e.) the channel
  410. * number is added to RFFIFO index.
  411. */
  412. #define RCANFD_RFFIFO_IDX 0
  413. /* Tx/Rx or Common FIFO is a per channel resource. Each channel has 3 Common
  414. * FIFOs dedicated to them. Use the first (index 0) FIFO out of the 3 for Tx.
  415. */
  416. #define RCANFD_CFFIFO_IDX 0
  417. /* fCAN clock select register settings */
  418. enum rcar_canfd_fcanclk {
  419. RCANFD_CANFDCLK = 0, /* CANFD clock */
  420. RCANFD_EXTCLK, /* Externally input clock */
  421. };
  422. struct rcar_canfd_global;
  423. /* Channel priv data */
  424. struct rcar_canfd_channel {
  425. struct can_priv can; /* Must be the first member */
  426. struct net_device *ndev;
  427. struct rcar_canfd_global *gpriv; /* Controller reference */
  428. void __iomem *base; /* Register base address */
  429. struct napi_struct napi;
  430. u8 tx_len[RCANFD_FIFO_DEPTH]; /* For net stats */
  431. u32 tx_head; /* Incremented on xmit */
  432. u32 tx_tail; /* Incremented on xmit done */
  433. u32 channel; /* Channel number */
  434. spinlock_t tx_lock; /* To protect tx path */
  435. };
  436. /* Global priv data */
  437. struct rcar_canfd_global {
  438. struct rcar_canfd_channel *ch[RCANFD_NUM_CHANNELS];
  439. void __iomem *base; /* Register base address */
  440. struct platform_device *pdev; /* Respective platform device */
  441. struct clk *clkp; /* Peripheral clock */
  442. struct clk *can_clk; /* fCAN clock */
  443. enum rcar_canfd_fcanclk fcan; /* CANFD or Ext clock */
  444. unsigned long channels_mask; /* Enabled channels mask */
  445. bool fdmode; /* CAN FD or Classical CAN only mode */
  446. };
  447. /* CAN FD mode nominal rate constants */
  448. static const struct can_bittiming_const rcar_canfd_nom_bittiming_const = {
  449. .name = RCANFD_DRV_NAME,
  450. .tseg1_min = 2,
  451. .tseg1_max = 128,
  452. .tseg2_min = 2,
  453. .tseg2_max = 32,
  454. .sjw_max = 32,
  455. .brp_min = 1,
  456. .brp_max = 1024,
  457. .brp_inc = 1,
  458. };
  459. /* CAN FD mode data rate constants */
  460. static const struct can_bittiming_const rcar_canfd_data_bittiming_const = {
  461. .name = RCANFD_DRV_NAME,
  462. .tseg1_min = 2,
  463. .tseg1_max = 16,
  464. .tseg2_min = 2,
  465. .tseg2_max = 8,
  466. .sjw_max = 8,
  467. .brp_min = 1,
  468. .brp_max = 256,
  469. .brp_inc = 1,
  470. };
  471. /* Classical CAN mode bitrate constants */
  472. static const struct can_bittiming_const rcar_canfd_bittiming_const = {
  473. .name = RCANFD_DRV_NAME,
  474. .tseg1_min = 4,
  475. .tseg1_max = 16,
  476. .tseg2_min = 2,
  477. .tseg2_max = 8,
  478. .sjw_max = 4,
  479. .brp_min = 1,
  480. .brp_max = 1024,
  481. .brp_inc = 1,
  482. };
  483. /* Helper functions */
  484. static inline void rcar_canfd_update(u32 mask, u32 val, u32 __iomem *reg)
  485. {
  486. u32 data = readl(reg);
  487. data &= ~mask;
  488. data |= (val & mask);
  489. writel(data, reg);
  490. }
  491. static inline u32 rcar_canfd_read(void __iomem *base, u32 offset)
  492. {
  493. return readl(base + (offset));
  494. }
  495. static inline void rcar_canfd_write(void __iomem *base, u32 offset, u32 val)
  496. {
  497. writel(val, base + (offset));
  498. }
  499. static void rcar_canfd_set_bit(void __iomem *base, u32 reg, u32 val)
  500. {
  501. rcar_canfd_update(val, val, base + (reg));
  502. }
  503. static void rcar_canfd_clear_bit(void __iomem *base, u32 reg, u32 val)
  504. {
  505. rcar_canfd_update(val, 0, base + (reg));
  506. }
  507. static void rcar_canfd_update_bit(void __iomem *base, u32 reg,
  508. u32 mask, u32 val)
  509. {
  510. rcar_canfd_update(mask, val, base + (reg));
  511. }
  512. static void rcar_canfd_get_data(struct rcar_canfd_channel *priv,
  513. struct canfd_frame *cf, u32 off)
  514. {
  515. u32 i, lwords;
  516. lwords = DIV_ROUND_UP(cf->len, sizeof(u32));
  517. for (i = 0; i < lwords; i++)
  518. *((u32 *)cf->data + i) =
  519. rcar_canfd_read(priv->base, off + (i * sizeof(u32)));
  520. }
  521. static void rcar_canfd_put_data(struct rcar_canfd_channel *priv,
  522. struct canfd_frame *cf, u32 off)
  523. {
  524. u32 i, lwords;
  525. lwords = DIV_ROUND_UP(cf->len, sizeof(u32));
  526. for (i = 0; i < lwords; i++)
  527. rcar_canfd_write(priv->base, off + (i * sizeof(u32)),
  528. *((u32 *)cf->data + i));
  529. }
  530. static void rcar_canfd_tx_failure_cleanup(struct net_device *ndev)
  531. {
  532. u32 i;
  533. for (i = 0; i < RCANFD_FIFO_DEPTH; i++)
  534. can_free_echo_skb(ndev, i);
  535. }
  536. static int rcar_canfd_reset_controller(struct rcar_canfd_global *gpriv)
  537. {
  538. u32 sts, ch;
  539. int err;
  540. /* Check RAMINIT flag as CAN RAM initialization takes place
  541. * after the MCU reset
  542. */
  543. err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts,
  544. !(sts & RCANFD_GSTS_GRAMINIT), 2, 500000);
  545. if (err) {
  546. dev_dbg(&gpriv->pdev->dev, "global raminit failed\n");
  547. return err;
  548. }
  549. /* Transition to Global Reset mode */
  550. rcar_canfd_clear_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GSLPR);
  551. rcar_canfd_update_bit(gpriv->base, RCANFD_GCTR,
  552. RCANFD_GCTR_GMDC_MASK, RCANFD_GCTR_GMDC_GRESET);
  553. /* Ensure Global reset mode */
  554. err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts,
  555. (sts & RCANFD_GSTS_GRSTSTS), 2, 500000);
  556. if (err) {
  557. dev_dbg(&gpriv->pdev->dev, "global reset failed\n");
  558. return err;
  559. }
  560. /* Reset Global error flags */
  561. rcar_canfd_write(gpriv->base, RCANFD_GERFL, 0x0);
  562. /* Set the controller into appropriate mode */
  563. if (gpriv->fdmode)
  564. rcar_canfd_set_bit(gpriv->base, RCANFD_GRMCFG,
  565. RCANFD_GRMCFG_RCMC);
  566. else
  567. rcar_canfd_clear_bit(gpriv->base, RCANFD_GRMCFG,
  568. RCANFD_GRMCFG_RCMC);
  569. /* Transition all Channels to reset mode */
  570. for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) {
  571. rcar_canfd_clear_bit(gpriv->base,
  572. RCANFD_CCTR(ch), RCANFD_CCTR_CSLPR);
  573. rcar_canfd_update_bit(gpriv->base, RCANFD_CCTR(ch),
  574. RCANFD_CCTR_CHMDC_MASK,
  575. RCANFD_CCTR_CHDMC_CRESET);
  576. /* Ensure Channel reset mode */
  577. err = readl_poll_timeout((gpriv->base + RCANFD_CSTS(ch)), sts,
  578. (sts & RCANFD_CSTS_CRSTSTS),
  579. 2, 500000);
  580. if (err) {
  581. dev_dbg(&gpriv->pdev->dev,
  582. "channel %u reset failed\n", ch);
  583. return err;
  584. }
  585. }
  586. return 0;
  587. }
  588. static void rcar_canfd_configure_controller(struct rcar_canfd_global *gpriv)
  589. {
  590. u32 cfg, ch;
  591. /* Global configuration settings */
  592. /* ECC Error flag Enable */
  593. cfg = RCANFD_GCFG_EEFE;
  594. if (gpriv->fdmode)
  595. /* Truncate payload to configured message size RFPLS */
  596. cfg |= RCANFD_GCFG_CMPOC;
  597. /* Set External Clock if selected */
  598. if (gpriv->fcan != RCANFD_CANFDCLK)
  599. cfg |= RCANFD_GCFG_DCS;
  600. rcar_canfd_set_bit(gpriv->base, RCANFD_GCFG, cfg);
  601. /* Channel configuration settings */
  602. for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) {
  603. rcar_canfd_set_bit(gpriv->base, RCANFD_CCTR(ch),
  604. RCANFD_CCTR_ERRD);
  605. rcar_canfd_update_bit(gpriv->base, RCANFD_CCTR(ch),
  606. RCANFD_CCTR_BOM_MASK,
  607. RCANFD_CCTR_BOM_BENTRY);
  608. }
  609. }
  610. static void rcar_canfd_configure_afl_rules(struct rcar_canfd_global *gpriv,
  611. u32 ch)
  612. {
  613. u32 cfg;
  614. int offset, start, page, num_rules = RCANFD_CHANNEL_NUMRULES;
  615. u32 ridx = ch + RCANFD_RFFIFO_IDX;
  616. if (ch == 0) {
  617. start = 0; /* Channel 0 always starts from 0th rule */
  618. } else {
  619. /* Get number of Channel 0 rules and adjust */
  620. cfg = rcar_canfd_read(gpriv->base, RCANFD_GAFLCFG0);
  621. start = RCANFD_GAFLCFG_GETRNC(0, cfg);
  622. }
  623. /* Enable write access to entry */
  624. page = RCANFD_GAFL_PAGENUM(start);
  625. rcar_canfd_set_bit(gpriv->base, RCANFD_GAFLECTR,
  626. (RCANFD_GAFLECTR_AFLPN(page) |
  627. RCANFD_GAFLECTR_AFLDAE));
  628. /* Write number of rules for channel */
  629. rcar_canfd_set_bit(gpriv->base, RCANFD_GAFLCFG0,
  630. RCANFD_GAFLCFG_SETRNC(ch, num_rules));
  631. if (gpriv->fdmode)
  632. offset = RCANFD_F_GAFL_OFFSET;
  633. else
  634. offset = RCANFD_C_GAFL_OFFSET;
  635. /* Accept all IDs */
  636. rcar_canfd_write(gpriv->base, RCANFD_GAFLID(offset, start), 0);
  637. /* IDE or RTR is not considered for matching */
  638. rcar_canfd_write(gpriv->base, RCANFD_GAFLM(offset, start), 0);
  639. /* Any data length accepted */
  640. rcar_canfd_write(gpriv->base, RCANFD_GAFLP0(offset, start), 0);
  641. /* Place the msg in corresponding Rx FIFO entry */
  642. rcar_canfd_write(gpriv->base, RCANFD_GAFLP1(offset, start),
  643. RCANFD_GAFLP1_GAFLFDP(ridx));
  644. /* Disable write access to page */
  645. rcar_canfd_clear_bit(gpriv->base,
  646. RCANFD_GAFLECTR, RCANFD_GAFLECTR_AFLDAE);
  647. }
  648. static void rcar_canfd_configure_rx(struct rcar_canfd_global *gpriv, u32 ch)
  649. {
  650. /* Rx FIFO is used for reception */
  651. u32 cfg;
  652. u16 rfdc, rfpls;
  653. /* Select Rx FIFO based on channel */
  654. u32 ridx = ch + RCANFD_RFFIFO_IDX;
  655. rfdc = 2; /* b010 - 8 messages Rx FIFO depth */
  656. if (gpriv->fdmode)
  657. rfpls = 7; /* b111 - Max 64 bytes payload */
  658. else
  659. rfpls = 0; /* b000 - Max 8 bytes payload */
  660. cfg = (RCANFD_RFCC_RFIM | RCANFD_RFCC_RFDC(rfdc) |
  661. RCANFD_RFCC_RFPLS(rfpls) | RCANFD_RFCC_RFIE);
  662. rcar_canfd_write(gpriv->base, RCANFD_RFCC(ridx), cfg);
  663. }
  664. static void rcar_canfd_configure_tx(struct rcar_canfd_global *gpriv, u32 ch)
  665. {
  666. /* Tx/Rx(Common) FIFO configured in Tx mode is
  667. * used for transmission
  668. *
  669. * Each channel has 3 Common FIFO dedicated to them.
  670. * Use the 1st (index 0) out of 3
  671. */
  672. u32 cfg;
  673. u16 cftml, cfm, cfdc, cfpls;
  674. cftml = 0; /* 0th buffer */
  675. cfm = 1; /* b01 - Transmit mode */
  676. cfdc = 2; /* b010 - 8 messages Tx FIFO depth */
  677. if (gpriv->fdmode)
  678. cfpls = 7; /* b111 - Max 64 bytes payload */
  679. else
  680. cfpls = 0; /* b000 - Max 8 bytes payload */
  681. cfg = (RCANFD_CFCC_CFTML(cftml) | RCANFD_CFCC_CFM(cfm) |
  682. RCANFD_CFCC_CFIM | RCANFD_CFCC_CFDC(cfdc) |
  683. RCANFD_CFCC_CFPLS(cfpls) | RCANFD_CFCC_CFTXIE);
  684. rcar_canfd_write(gpriv->base, RCANFD_CFCC(ch, RCANFD_CFFIFO_IDX), cfg);
  685. if (gpriv->fdmode)
  686. /* Clear FD mode specific control/status register */
  687. rcar_canfd_write(gpriv->base,
  688. RCANFD_F_CFFDCSTS(ch, RCANFD_CFFIFO_IDX), 0);
  689. }
  690. static void rcar_canfd_enable_global_interrupts(struct rcar_canfd_global *gpriv)
  691. {
  692. u32 ctr;
  693. /* Clear any stray error interrupt flags */
  694. rcar_canfd_write(gpriv->base, RCANFD_GERFL, 0);
  695. /* Global interrupts setup */
  696. ctr = RCANFD_GCTR_MEIE;
  697. if (gpriv->fdmode)
  698. ctr |= RCANFD_GCTR_CFMPOFIE;
  699. rcar_canfd_set_bit(gpriv->base, RCANFD_GCTR, ctr);
  700. }
  701. static void rcar_canfd_disable_global_interrupts(struct rcar_canfd_global
  702. *gpriv)
  703. {
  704. /* Disable all interrupts */
  705. rcar_canfd_write(gpriv->base, RCANFD_GCTR, 0);
  706. /* Clear any stray error interrupt flags */
  707. rcar_canfd_write(gpriv->base, RCANFD_GERFL, 0);
  708. }
  709. static void rcar_canfd_enable_channel_interrupts(struct rcar_canfd_channel
  710. *priv)
  711. {
  712. u32 ctr, ch = priv->channel;
  713. /* Clear any stray error flags */
  714. rcar_canfd_write(priv->base, RCANFD_CERFL(ch), 0);
  715. /* Channel interrupts setup */
  716. ctr = (RCANFD_CCTR_TAIE |
  717. RCANFD_CCTR_ALIE | RCANFD_CCTR_BLIE |
  718. RCANFD_CCTR_OLIE | RCANFD_CCTR_BORIE |
  719. RCANFD_CCTR_BOEIE | RCANFD_CCTR_EPIE |
  720. RCANFD_CCTR_EWIE | RCANFD_CCTR_BEIE);
  721. rcar_canfd_set_bit(priv->base, RCANFD_CCTR(ch), ctr);
  722. }
  723. static void rcar_canfd_disable_channel_interrupts(struct rcar_canfd_channel
  724. *priv)
  725. {
  726. u32 ctr, ch = priv->channel;
  727. ctr = (RCANFD_CCTR_TAIE |
  728. RCANFD_CCTR_ALIE | RCANFD_CCTR_BLIE |
  729. RCANFD_CCTR_OLIE | RCANFD_CCTR_BORIE |
  730. RCANFD_CCTR_BOEIE | RCANFD_CCTR_EPIE |
  731. RCANFD_CCTR_EWIE | RCANFD_CCTR_BEIE);
  732. rcar_canfd_clear_bit(priv->base, RCANFD_CCTR(ch), ctr);
  733. /* Clear any stray error flags */
  734. rcar_canfd_write(priv->base, RCANFD_CERFL(ch), 0);
  735. }
  736. static void rcar_canfd_global_error(struct net_device *ndev)
  737. {
  738. struct rcar_canfd_channel *priv = netdev_priv(ndev);
  739. struct rcar_canfd_global *gpriv = priv->gpriv;
  740. struct net_device_stats *stats = &ndev->stats;
  741. u32 ch = priv->channel;
  742. u32 gerfl, sts;
  743. u32 ridx = ch + RCANFD_RFFIFO_IDX;
  744. gerfl = rcar_canfd_read(priv->base, RCANFD_GERFL);
  745. if ((gerfl & RCANFD_GERFL_EEF0) && (ch == 0)) {
  746. netdev_dbg(ndev, "Ch0: ECC Error flag\n");
  747. stats->tx_dropped++;
  748. }
  749. if ((gerfl & RCANFD_GERFL_EEF1) && (ch == 1)) {
  750. netdev_dbg(ndev, "Ch1: ECC Error flag\n");
  751. stats->tx_dropped++;
  752. }
  753. if (gerfl & RCANFD_GERFL_MES) {
  754. sts = rcar_canfd_read(priv->base,
  755. RCANFD_CFSTS(ch, RCANFD_CFFIFO_IDX));
  756. if (sts & RCANFD_CFSTS_CFMLT) {
  757. netdev_dbg(ndev, "Tx Message Lost flag\n");
  758. stats->tx_dropped++;
  759. rcar_canfd_write(priv->base,
  760. RCANFD_CFSTS(ch, RCANFD_CFFIFO_IDX),
  761. sts & ~RCANFD_CFSTS_CFMLT);
  762. }
  763. sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(ridx));
  764. if (sts & RCANFD_RFSTS_RFMLT) {
  765. netdev_dbg(ndev, "Rx Message Lost flag\n");
  766. stats->rx_dropped++;
  767. rcar_canfd_write(priv->base, RCANFD_RFSTS(ridx),
  768. sts & ~RCANFD_RFSTS_RFMLT);
  769. }
  770. }
  771. if (gpriv->fdmode && gerfl & RCANFD_GERFL_CMPOF) {
  772. /* Message Lost flag will be set for respective channel
  773. * when this condition happens with counters and flags
  774. * already updated.
  775. */
  776. netdev_dbg(ndev, "global payload overflow interrupt\n");
  777. }
  778. /* Clear all global error interrupts. Only affected channels bits
  779. * get cleared
  780. */
  781. rcar_canfd_write(priv->base, RCANFD_GERFL, 0);
  782. }
  783. static void rcar_canfd_error(struct net_device *ndev, u32 cerfl,
  784. u16 txerr, u16 rxerr)
  785. {
  786. struct rcar_canfd_channel *priv = netdev_priv(ndev);
  787. struct net_device_stats *stats = &ndev->stats;
  788. struct can_frame *cf;
  789. struct sk_buff *skb;
  790. u32 ch = priv->channel;
  791. netdev_dbg(ndev, "ch erfl %x txerr %u rxerr %u\n", cerfl, txerr, rxerr);
  792. /* Propagate the error condition to the CAN stack */
  793. skb = alloc_can_err_skb(ndev, &cf);
  794. if (!skb) {
  795. stats->rx_dropped++;
  796. return;
  797. }
  798. /* Channel error interrupts */
  799. if (cerfl & RCANFD_CERFL_BEF) {
  800. netdev_dbg(ndev, "Bus error\n");
  801. cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
  802. cf->data[2] = CAN_ERR_PROT_UNSPEC;
  803. priv->can.can_stats.bus_error++;
  804. }
  805. if (cerfl & RCANFD_CERFL_ADERR) {
  806. netdev_dbg(ndev, "ACK Delimiter Error\n");
  807. stats->tx_errors++;
  808. cf->data[3] |= CAN_ERR_PROT_LOC_ACK_DEL;
  809. }
  810. if (cerfl & RCANFD_CERFL_B0ERR) {
  811. netdev_dbg(ndev, "Bit Error (dominant)\n");
  812. stats->tx_errors++;
  813. cf->data[2] |= CAN_ERR_PROT_BIT0;
  814. }
  815. if (cerfl & RCANFD_CERFL_B1ERR) {
  816. netdev_dbg(ndev, "Bit Error (recessive)\n");
  817. stats->tx_errors++;
  818. cf->data[2] |= CAN_ERR_PROT_BIT1;
  819. }
  820. if (cerfl & RCANFD_CERFL_CERR) {
  821. netdev_dbg(ndev, "CRC Error\n");
  822. stats->rx_errors++;
  823. cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
  824. }
  825. if (cerfl & RCANFD_CERFL_AERR) {
  826. netdev_dbg(ndev, "ACK Error\n");
  827. stats->tx_errors++;
  828. cf->can_id |= CAN_ERR_ACK;
  829. cf->data[3] |= CAN_ERR_PROT_LOC_ACK;
  830. }
  831. if (cerfl & RCANFD_CERFL_FERR) {
  832. netdev_dbg(ndev, "Form Error\n");
  833. stats->rx_errors++;
  834. cf->data[2] |= CAN_ERR_PROT_FORM;
  835. }
  836. if (cerfl & RCANFD_CERFL_SERR) {
  837. netdev_dbg(ndev, "Stuff Error\n");
  838. stats->rx_errors++;
  839. cf->data[2] |= CAN_ERR_PROT_STUFF;
  840. }
  841. if (cerfl & RCANFD_CERFL_ALF) {
  842. netdev_dbg(ndev, "Arbitration lost Error\n");
  843. priv->can.can_stats.arbitration_lost++;
  844. cf->can_id |= CAN_ERR_LOSTARB;
  845. cf->data[0] |= CAN_ERR_LOSTARB_UNSPEC;
  846. }
  847. if (cerfl & RCANFD_CERFL_BLF) {
  848. netdev_dbg(ndev, "Bus Lock Error\n");
  849. stats->rx_errors++;
  850. cf->can_id |= CAN_ERR_BUSERROR;
  851. }
  852. if (cerfl & RCANFD_CERFL_EWF) {
  853. netdev_dbg(ndev, "Error warning interrupt\n");
  854. priv->can.state = CAN_STATE_ERROR_WARNING;
  855. priv->can.can_stats.error_warning++;
  856. cf->can_id |= CAN_ERR_CRTL;
  857. cf->data[1] = txerr > rxerr ? CAN_ERR_CRTL_TX_WARNING :
  858. CAN_ERR_CRTL_RX_WARNING;
  859. cf->data[6] = txerr;
  860. cf->data[7] = rxerr;
  861. }
  862. if (cerfl & RCANFD_CERFL_EPF) {
  863. netdev_dbg(ndev, "Error passive interrupt\n");
  864. priv->can.state = CAN_STATE_ERROR_PASSIVE;
  865. priv->can.can_stats.error_passive++;
  866. cf->can_id |= CAN_ERR_CRTL;
  867. cf->data[1] = txerr > rxerr ? CAN_ERR_CRTL_TX_PASSIVE :
  868. CAN_ERR_CRTL_RX_PASSIVE;
  869. cf->data[6] = txerr;
  870. cf->data[7] = rxerr;
  871. }
  872. if (cerfl & RCANFD_CERFL_BOEF) {
  873. netdev_dbg(ndev, "Bus-off entry interrupt\n");
  874. rcar_canfd_tx_failure_cleanup(ndev);
  875. priv->can.state = CAN_STATE_BUS_OFF;
  876. priv->can.can_stats.bus_off++;
  877. can_bus_off(ndev);
  878. cf->can_id |= CAN_ERR_BUSOFF;
  879. }
  880. if (cerfl & RCANFD_CERFL_OVLF) {
  881. netdev_dbg(ndev,
  882. "Overload Frame Transmission error interrupt\n");
  883. stats->tx_errors++;
  884. cf->can_id |= CAN_ERR_PROT;
  885. cf->data[2] |= CAN_ERR_PROT_OVERLOAD;
  886. }
  887. /* Clear channel error interrupts that are handled */
  888. rcar_canfd_write(priv->base, RCANFD_CERFL(ch),
  889. RCANFD_CERFL_ERR(~cerfl));
  890. stats->rx_packets++;
  891. stats->rx_bytes += cf->can_dlc;
  892. netif_rx(skb);
  893. }
  894. static void rcar_canfd_tx_done(struct net_device *ndev)
  895. {
  896. struct rcar_canfd_channel *priv = netdev_priv(ndev);
  897. struct net_device_stats *stats = &ndev->stats;
  898. u32 sts;
  899. unsigned long flags;
  900. u32 ch = priv->channel;
  901. do {
  902. u8 unsent, sent;
  903. sent = priv->tx_tail % RCANFD_FIFO_DEPTH;
  904. stats->tx_packets++;
  905. stats->tx_bytes += priv->tx_len[sent];
  906. priv->tx_len[sent] = 0;
  907. can_get_echo_skb(ndev, sent);
  908. spin_lock_irqsave(&priv->tx_lock, flags);
  909. priv->tx_tail++;
  910. sts = rcar_canfd_read(priv->base,
  911. RCANFD_CFSTS(ch, RCANFD_CFFIFO_IDX));
  912. unsent = RCANFD_CFSTS_CFMC(sts);
  913. /* Wake producer only when there is room */
  914. if (unsent != RCANFD_FIFO_DEPTH)
  915. netif_wake_queue(ndev);
  916. if (priv->tx_head - priv->tx_tail <= unsent) {
  917. spin_unlock_irqrestore(&priv->tx_lock, flags);
  918. break;
  919. }
  920. spin_unlock_irqrestore(&priv->tx_lock, flags);
  921. } while (1);
  922. /* Clear interrupt */
  923. rcar_canfd_write(priv->base, RCANFD_CFSTS(ch, RCANFD_CFFIFO_IDX),
  924. sts & ~RCANFD_CFSTS_CFTXIF);
  925. can_led_event(ndev, CAN_LED_EVENT_TX);
  926. }
  927. static irqreturn_t rcar_canfd_global_interrupt(int irq, void *dev_id)
  928. {
  929. struct rcar_canfd_global *gpriv = dev_id;
  930. struct net_device *ndev;
  931. struct rcar_canfd_channel *priv;
  932. u32 sts, gerfl;
  933. u32 ch, ridx;
  934. /* Global error interrupts still indicate a condition specific
  935. * to a channel. RxFIFO interrupt is a global interrupt.
  936. */
  937. for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) {
  938. priv = gpriv->ch[ch];
  939. ndev = priv->ndev;
  940. ridx = ch + RCANFD_RFFIFO_IDX;
  941. /* Global error interrupts */
  942. gerfl = rcar_canfd_read(priv->base, RCANFD_GERFL);
  943. if (unlikely(RCANFD_GERFL_ERR(gpriv, gerfl)))
  944. rcar_canfd_global_error(ndev);
  945. /* Handle Rx interrupts */
  946. sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(ridx));
  947. if (likely(sts & RCANFD_RFSTS_RFIF)) {
  948. if (napi_schedule_prep(&priv->napi)) {
  949. /* Disable Rx FIFO interrupts */
  950. rcar_canfd_clear_bit(priv->base,
  951. RCANFD_RFCC(ridx),
  952. RCANFD_RFCC_RFIE);
  953. __napi_schedule(&priv->napi);
  954. }
  955. }
  956. }
  957. return IRQ_HANDLED;
  958. }
  959. static void rcar_canfd_state_change(struct net_device *ndev,
  960. u16 txerr, u16 rxerr)
  961. {
  962. struct rcar_canfd_channel *priv = netdev_priv(ndev);
  963. struct net_device_stats *stats = &ndev->stats;
  964. enum can_state rx_state, tx_state, state = priv->can.state;
  965. struct can_frame *cf;
  966. struct sk_buff *skb;
  967. /* Handle transition from error to normal states */
  968. if (txerr < 96 && rxerr < 96)
  969. state = CAN_STATE_ERROR_ACTIVE;
  970. else if (txerr < 128 && rxerr < 128)
  971. state = CAN_STATE_ERROR_WARNING;
  972. if (state != priv->can.state) {
  973. netdev_dbg(ndev, "state: new %d, old %d: txerr %u, rxerr %u\n",
  974. state, priv->can.state, txerr, rxerr);
  975. skb = alloc_can_err_skb(ndev, &cf);
  976. if (!skb) {
  977. stats->rx_dropped++;
  978. return;
  979. }
  980. tx_state = txerr >= rxerr ? state : 0;
  981. rx_state = txerr <= rxerr ? state : 0;
  982. can_change_state(ndev, cf, tx_state, rx_state);
  983. stats->rx_packets++;
  984. stats->rx_bytes += cf->can_dlc;
  985. netif_rx(skb);
  986. }
  987. }
  988. static irqreturn_t rcar_canfd_channel_interrupt(int irq, void *dev_id)
  989. {
  990. struct rcar_canfd_global *gpriv = dev_id;
  991. struct net_device *ndev;
  992. struct rcar_canfd_channel *priv;
  993. u32 sts, ch, cerfl;
  994. u16 txerr, rxerr;
  995. /* Common FIFO is a per channel resource */
  996. for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) {
  997. priv = gpriv->ch[ch];
  998. ndev = priv->ndev;
  999. /* Channel error interrupts */
  1000. cerfl = rcar_canfd_read(priv->base, RCANFD_CERFL(ch));
  1001. sts = rcar_canfd_read(priv->base, RCANFD_CSTS(ch));
  1002. txerr = RCANFD_CSTS_TECCNT(sts);
  1003. rxerr = RCANFD_CSTS_RECCNT(sts);
  1004. if (unlikely(RCANFD_CERFL_ERR(cerfl)))
  1005. rcar_canfd_error(ndev, cerfl, txerr, rxerr);
  1006. /* Handle state change to lower states */
  1007. if (unlikely((priv->can.state != CAN_STATE_ERROR_ACTIVE) &&
  1008. (priv->can.state != CAN_STATE_BUS_OFF)))
  1009. rcar_canfd_state_change(ndev, txerr, rxerr);
  1010. /* Handle Tx interrupts */
  1011. sts = rcar_canfd_read(priv->base,
  1012. RCANFD_CFSTS(ch, RCANFD_CFFIFO_IDX));
  1013. if (likely(sts & RCANFD_CFSTS_CFTXIF))
  1014. rcar_canfd_tx_done(ndev);
  1015. }
  1016. return IRQ_HANDLED;
  1017. }
  1018. static void rcar_canfd_set_bittiming(struct net_device *dev)
  1019. {
  1020. struct rcar_canfd_channel *priv = netdev_priv(dev);
  1021. const struct can_bittiming *bt = &priv->can.bittiming;
  1022. const struct can_bittiming *dbt = &priv->can.data_bittiming;
  1023. u16 brp, sjw, tseg1, tseg2;
  1024. u32 cfg;
  1025. u32 ch = priv->channel;
  1026. /* Nominal bit timing settings */
  1027. brp = bt->brp - 1;
  1028. sjw = bt->sjw - 1;
  1029. tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
  1030. tseg2 = bt->phase_seg2 - 1;
  1031. if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
  1032. /* CAN FD only mode */
  1033. cfg = (RCANFD_NCFG_NTSEG1(tseg1) | RCANFD_NCFG_NBRP(brp) |
  1034. RCANFD_NCFG_NSJW(sjw) | RCANFD_NCFG_NTSEG2(tseg2));
  1035. rcar_canfd_write(priv->base, RCANFD_CCFG(ch), cfg);
  1036. netdev_dbg(priv->ndev, "nrate: brp %u, sjw %u, tseg1 %u, tseg2 %u\n",
  1037. brp, sjw, tseg1, tseg2);
  1038. /* Data bit timing settings */
  1039. brp = dbt->brp - 1;
  1040. sjw = dbt->sjw - 1;
  1041. tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1;
  1042. tseg2 = dbt->phase_seg2 - 1;
  1043. cfg = (RCANFD_DCFG_DTSEG1(tseg1) | RCANFD_DCFG_DBRP(brp) |
  1044. RCANFD_DCFG_DSJW(sjw) | RCANFD_DCFG_DTSEG2(tseg2));
  1045. rcar_canfd_write(priv->base, RCANFD_F_DCFG(ch), cfg);
  1046. netdev_dbg(priv->ndev, "drate: brp %u, sjw %u, tseg1 %u, tseg2 %u\n",
  1047. brp, sjw, tseg1, tseg2);
  1048. } else {
  1049. /* Classical CAN only mode */
  1050. cfg = (RCANFD_CFG_TSEG1(tseg1) | RCANFD_CFG_BRP(brp) |
  1051. RCANFD_CFG_SJW(sjw) | RCANFD_CFG_TSEG2(tseg2));
  1052. rcar_canfd_write(priv->base, RCANFD_CCFG(ch), cfg);
  1053. netdev_dbg(priv->ndev,
  1054. "rate: brp %u, sjw %u, tseg1 %u, tseg2 %u\n",
  1055. brp, sjw, tseg1, tseg2);
  1056. }
  1057. }
  1058. static int rcar_canfd_start(struct net_device *ndev)
  1059. {
  1060. struct rcar_canfd_channel *priv = netdev_priv(ndev);
  1061. int err = -EOPNOTSUPP;
  1062. u32 sts, ch = priv->channel;
  1063. u32 ridx = ch + RCANFD_RFFIFO_IDX;
  1064. rcar_canfd_set_bittiming(ndev);
  1065. rcar_canfd_enable_channel_interrupts(priv);
  1066. /* Set channel to Operational mode */
  1067. rcar_canfd_update_bit(priv->base, RCANFD_CCTR(ch),
  1068. RCANFD_CCTR_CHMDC_MASK, RCANFD_CCTR_CHDMC_COPM);
  1069. /* Verify channel mode change */
  1070. err = readl_poll_timeout((priv->base + RCANFD_CSTS(ch)), sts,
  1071. (sts & RCANFD_CSTS_COMSTS), 2, 500000);
  1072. if (err) {
  1073. netdev_err(ndev, "channel %u communication state failed\n", ch);
  1074. goto fail_mode_change;
  1075. }
  1076. /* Enable Common & Rx FIFO */
  1077. rcar_canfd_set_bit(priv->base, RCANFD_CFCC(ch, RCANFD_CFFIFO_IDX),
  1078. RCANFD_CFCC_CFE);
  1079. rcar_canfd_set_bit(priv->base, RCANFD_RFCC(ridx), RCANFD_RFCC_RFE);
  1080. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  1081. return 0;
  1082. fail_mode_change:
  1083. rcar_canfd_disable_channel_interrupts(priv);
  1084. return err;
  1085. }
  1086. static int rcar_canfd_open(struct net_device *ndev)
  1087. {
  1088. struct rcar_canfd_channel *priv = netdev_priv(ndev);
  1089. struct rcar_canfd_global *gpriv = priv->gpriv;
  1090. int err;
  1091. /* Peripheral clock is already enabled in probe */
  1092. err = clk_prepare_enable(gpriv->can_clk);
  1093. if (err) {
  1094. netdev_err(ndev, "failed to enable CAN clock, error %d\n", err);
  1095. goto out_clock;
  1096. }
  1097. err = open_candev(ndev);
  1098. if (err) {
  1099. netdev_err(ndev, "open_candev() failed, error %d\n", err);
  1100. goto out_can_clock;
  1101. }
  1102. napi_enable(&priv->napi);
  1103. err = rcar_canfd_start(ndev);
  1104. if (err)
  1105. goto out_close;
  1106. netif_start_queue(ndev);
  1107. can_led_event(ndev, CAN_LED_EVENT_OPEN);
  1108. return 0;
  1109. out_close:
  1110. napi_disable(&priv->napi);
  1111. close_candev(ndev);
  1112. out_can_clock:
  1113. clk_disable_unprepare(gpriv->can_clk);
  1114. out_clock:
  1115. return err;
  1116. }
  1117. static void rcar_canfd_stop(struct net_device *ndev)
  1118. {
  1119. struct rcar_canfd_channel *priv = netdev_priv(ndev);
  1120. int err;
  1121. u32 sts, ch = priv->channel;
  1122. u32 ridx = ch + RCANFD_RFFIFO_IDX;
  1123. /* Transition to channel reset mode */
  1124. rcar_canfd_update_bit(priv->base, RCANFD_CCTR(ch),
  1125. RCANFD_CCTR_CHMDC_MASK, RCANFD_CCTR_CHDMC_CRESET);
  1126. /* Check Channel reset mode */
  1127. err = readl_poll_timeout((priv->base + RCANFD_CSTS(ch)), sts,
  1128. (sts & RCANFD_CSTS_CRSTSTS), 2, 500000);
  1129. if (err)
  1130. netdev_err(ndev, "channel %u reset failed\n", ch);
  1131. rcar_canfd_disable_channel_interrupts(priv);
  1132. /* Disable Common & Rx FIFO */
  1133. rcar_canfd_clear_bit(priv->base, RCANFD_CFCC(ch, RCANFD_CFFIFO_IDX),
  1134. RCANFD_CFCC_CFE);
  1135. rcar_canfd_clear_bit(priv->base, RCANFD_RFCC(ridx), RCANFD_RFCC_RFE);
  1136. /* Set the state as STOPPED */
  1137. priv->can.state = CAN_STATE_STOPPED;
  1138. }
  1139. static int rcar_canfd_close(struct net_device *ndev)
  1140. {
  1141. struct rcar_canfd_channel *priv = netdev_priv(ndev);
  1142. struct rcar_canfd_global *gpriv = priv->gpriv;
  1143. netif_stop_queue(ndev);
  1144. rcar_canfd_stop(ndev);
  1145. napi_disable(&priv->napi);
  1146. clk_disable_unprepare(gpriv->can_clk);
  1147. close_candev(ndev);
  1148. can_led_event(ndev, CAN_LED_EVENT_STOP);
  1149. return 0;
  1150. }
  1151. static netdev_tx_t rcar_canfd_start_xmit(struct sk_buff *skb,
  1152. struct net_device *ndev)
  1153. {
  1154. struct rcar_canfd_channel *priv = netdev_priv(ndev);
  1155. struct canfd_frame *cf = (struct canfd_frame *)skb->data;
  1156. u32 sts = 0, id, dlc;
  1157. unsigned long flags;
  1158. u32 ch = priv->channel;
  1159. if (can_dropped_invalid_skb(ndev, skb))
  1160. return NETDEV_TX_OK;
  1161. if (cf->can_id & CAN_EFF_FLAG) {
  1162. id = cf->can_id & CAN_EFF_MASK;
  1163. id |= RCANFD_CFID_CFIDE;
  1164. } else {
  1165. id = cf->can_id & CAN_SFF_MASK;
  1166. }
  1167. if (cf->can_id & CAN_RTR_FLAG)
  1168. id |= RCANFD_CFID_CFRTR;
  1169. dlc = RCANFD_CFPTR_CFDLC(can_len2dlc(cf->len));
  1170. if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
  1171. rcar_canfd_write(priv->base,
  1172. RCANFD_F_CFID(ch, RCANFD_CFFIFO_IDX), id);
  1173. rcar_canfd_write(priv->base,
  1174. RCANFD_F_CFPTR(ch, RCANFD_CFFIFO_IDX), dlc);
  1175. if (can_is_canfd_skb(skb)) {
  1176. /* CAN FD frame format */
  1177. sts |= RCANFD_CFFDCSTS_CFFDF;
  1178. if (cf->flags & CANFD_BRS)
  1179. sts |= RCANFD_CFFDCSTS_CFBRS;
  1180. if (priv->can.state == CAN_STATE_ERROR_PASSIVE)
  1181. sts |= RCANFD_CFFDCSTS_CFESI;
  1182. }
  1183. rcar_canfd_write(priv->base,
  1184. RCANFD_F_CFFDCSTS(ch, RCANFD_CFFIFO_IDX), sts);
  1185. rcar_canfd_put_data(priv, cf,
  1186. RCANFD_F_CFDF(ch, RCANFD_CFFIFO_IDX, 0));
  1187. } else {
  1188. rcar_canfd_write(priv->base,
  1189. RCANFD_C_CFID(ch, RCANFD_CFFIFO_IDX), id);
  1190. rcar_canfd_write(priv->base,
  1191. RCANFD_C_CFPTR(ch, RCANFD_CFFIFO_IDX), dlc);
  1192. rcar_canfd_put_data(priv, cf,
  1193. RCANFD_C_CFDF(ch, RCANFD_CFFIFO_IDX, 0));
  1194. }
  1195. priv->tx_len[priv->tx_head % RCANFD_FIFO_DEPTH] = cf->len;
  1196. can_put_echo_skb(skb, ndev, priv->tx_head % RCANFD_FIFO_DEPTH);
  1197. spin_lock_irqsave(&priv->tx_lock, flags);
  1198. priv->tx_head++;
  1199. /* Stop the queue if we've filled all FIFO entries */
  1200. if (priv->tx_head - priv->tx_tail >= RCANFD_FIFO_DEPTH)
  1201. netif_stop_queue(ndev);
  1202. /* Start Tx: Write 0xff to CFPC to increment the CPU-side
  1203. * pointer for the Common FIFO
  1204. */
  1205. rcar_canfd_write(priv->base,
  1206. RCANFD_CFPCTR(ch, RCANFD_CFFIFO_IDX), 0xff);
  1207. spin_unlock_irqrestore(&priv->tx_lock, flags);
  1208. return NETDEV_TX_OK;
  1209. }
  1210. static void rcar_canfd_rx_pkt(struct rcar_canfd_channel *priv)
  1211. {
  1212. struct net_device_stats *stats = &priv->ndev->stats;
  1213. struct canfd_frame *cf;
  1214. struct sk_buff *skb;
  1215. u32 sts = 0, id, dlc;
  1216. u32 ch = priv->channel;
  1217. u32 ridx = ch + RCANFD_RFFIFO_IDX;
  1218. if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
  1219. id = rcar_canfd_read(priv->base, RCANFD_F_RFID(ridx));
  1220. dlc = rcar_canfd_read(priv->base, RCANFD_F_RFPTR(ridx));
  1221. sts = rcar_canfd_read(priv->base, RCANFD_F_RFFDSTS(ridx));
  1222. if (sts & RCANFD_RFFDSTS_RFFDF)
  1223. skb = alloc_canfd_skb(priv->ndev, &cf);
  1224. else
  1225. skb = alloc_can_skb(priv->ndev,
  1226. (struct can_frame **)&cf);
  1227. } else {
  1228. id = rcar_canfd_read(priv->base, RCANFD_C_RFID(ridx));
  1229. dlc = rcar_canfd_read(priv->base, RCANFD_C_RFPTR(ridx));
  1230. skb = alloc_can_skb(priv->ndev, (struct can_frame **)&cf);
  1231. }
  1232. if (!skb) {
  1233. stats->rx_dropped++;
  1234. return;
  1235. }
  1236. if (id & RCANFD_RFID_RFIDE)
  1237. cf->can_id = (id & CAN_EFF_MASK) | CAN_EFF_FLAG;
  1238. else
  1239. cf->can_id = id & CAN_SFF_MASK;
  1240. if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
  1241. if (sts & RCANFD_RFFDSTS_RFFDF)
  1242. cf->len = can_dlc2len(RCANFD_RFPTR_RFDLC(dlc));
  1243. else
  1244. cf->len = get_can_dlc(RCANFD_RFPTR_RFDLC(dlc));
  1245. if (sts & RCANFD_RFFDSTS_RFESI) {
  1246. cf->flags |= CANFD_ESI;
  1247. netdev_dbg(priv->ndev, "ESI Error\n");
  1248. }
  1249. if (!(sts & RCANFD_RFFDSTS_RFFDF) && (id & RCANFD_RFID_RFRTR)) {
  1250. cf->can_id |= CAN_RTR_FLAG;
  1251. } else {
  1252. if (sts & RCANFD_RFFDSTS_RFBRS)
  1253. cf->flags |= CANFD_BRS;
  1254. rcar_canfd_get_data(priv, cf, RCANFD_F_RFDF(ridx, 0));
  1255. }
  1256. } else {
  1257. cf->len = get_can_dlc(RCANFD_RFPTR_RFDLC(dlc));
  1258. if (id & RCANFD_RFID_RFRTR)
  1259. cf->can_id |= CAN_RTR_FLAG;
  1260. else
  1261. rcar_canfd_get_data(priv, cf, RCANFD_C_RFDF(ridx, 0));
  1262. }
  1263. /* Write 0xff to RFPC to increment the CPU-side
  1264. * pointer of the Rx FIFO
  1265. */
  1266. rcar_canfd_write(priv->base, RCANFD_RFPCTR(ridx), 0xff);
  1267. can_led_event(priv->ndev, CAN_LED_EVENT_RX);
  1268. stats->rx_bytes += cf->len;
  1269. stats->rx_packets++;
  1270. netif_receive_skb(skb);
  1271. }
  1272. static int rcar_canfd_rx_poll(struct napi_struct *napi, int quota)
  1273. {
  1274. struct rcar_canfd_channel *priv =
  1275. container_of(napi, struct rcar_canfd_channel, napi);
  1276. int num_pkts;
  1277. u32 sts;
  1278. u32 ch = priv->channel;
  1279. u32 ridx = ch + RCANFD_RFFIFO_IDX;
  1280. for (num_pkts = 0; num_pkts < quota; num_pkts++) {
  1281. sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(ridx));
  1282. /* Check FIFO empty condition */
  1283. if (sts & RCANFD_RFSTS_RFEMP)
  1284. break;
  1285. rcar_canfd_rx_pkt(priv);
  1286. /* Clear interrupt bit */
  1287. if (sts & RCANFD_RFSTS_RFIF)
  1288. rcar_canfd_write(priv->base, RCANFD_RFSTS(ridx),
  1289. sts & ~RCANFD_RFSTS_RFIF);
  1290. }
  1291. /* All packets processed */
  1292. if (num_pkts < quota) {
  1293. if (napi_complete_done(napi, num_pkts)) {
  1294. /* Enable Rx FIFO interrupts */
  1295. rcar_canfd_set_bit(priv->base, RCANFD_RFCC(ridx),
  1296. RCANFD_RFCC_RFIE);
  1297. }
  1298. }
  1299. return num_pkts;
  1300. }
  1301. static int rcar_canfd_do_set_mode(struct net_device *ndev, enum can_mode mode)
  1302. {
  1303. int err;
  1304. switch (mode) {
  1305. case CAN_MODE_START:
  1306. err = rcar_canfd_start(ndev);
  1307. if (err)
  1308. return err;
  1309. netif_wake_queue(ndev);
  1310. return 0;
  1311. default:
  1312. return -EOPNOTSUPP;
  1313. }
  1314. }
  1315. static int rcar_canfd_get_berr_counter(const struct net_device *dev,
  1316. struct can_berr_counter *bec)
  1317. {
  1318. struct rcar_canfd_channel *priv = netdev_priv(dev);
  1319. u32 val, ch = priv->channel;
  1320. /* Peripheral clock is already enabled in probe */
  1321. val = rcar_canfd_read(priv->base, RCANFD_CSTS(ch));
  1322. bec->txerr = RCANFD_CSTS_TECCNT(val);
  1323. bec->rxerr = RCANFD_CSTS_RECCNT(val);
  1324. return 0;
  1325. }
  1326. static const struct net_device_ops rcar_canfd_netdev_ops = {
  1327. .ndo_open = rcar_canfd_open,
  1328. .ndo_stop = rcar_canfd_close,
  1329. .ndo_start_xmit = rcar_canfd_start_xmit,
  1330. .ndo_change_mtu = can_change_mtu,
  1331. };
  1332. static int rcar_canfd_channel_probe(struct rcar_canfd_global *gpriv, u32 ch,
  1333. u32 fcan_freq)
  1334. {
  1335. struct platform_device *pdev = gpriv->pdev;
  1336. struct rcar_canfd_channel *priv;
  1337. struct net_device *ndev;
  1338. int err = -ENODEV;
  1339. ndev = alloc_candev(sizeof(*priv), RCANFD_FIFO_DEPTH);
  1340. if (!ndev) {
  1341. dev_err(&pdev->dev, "alloc_candev() failed\n");
  1342. err = -ENOMEM;
  1343. goto fail;
  1344. }
  1345. priv = netdev_priv(ndev);
  1346. ndev->netdev_ops = &rcar_canfd_netdev_ops;
  1347. ndev->flags |= IFF_ECHO;
  1348. priv->ndev = ndev;
  1349. priv->base = gpriv->base;
  1350. priv->channel = ch;
  1351. priv->can.clock.freq = fcan_freq;
  1352. dev_info(&pdev->dev, "can_clk rate is %u\n", priv->can.clock.freq);
  1353. if (gpriv->fdmode) {
  1354. priv->can.bittiming_const = &rcar_canfd_nom_bittiming_const;
  1355. priv->can.data_bittiming_const =
  1356. &rcar_canfd_data_bittiming_const;
  1357. /* Controller starts in CAN FD only mode */
  1358. can_set_static_ctrlmode(ndev, CAN_CTRLMODE_FD);
  1359. priv->can.ctrlmode_supported = CAN_CTRLMODE_BERR_REPORTING;
  1360. } else {
  1361. /* Controller starts in Classical CAN only mode */
  1362. priv->can.bittiming_const = &rcar_canfd_bittiming_const;
  1363. priv->can.ctrlmode_supported = CAN_CTRLMODE_BERR_REPORTING;
  1364. }
  1365. priv->can.do_set_mode = rcar_canfd_do_set_mode;
  1366. priv->can.do_get_berr_counter = rcar_canfd_get_berr_counter;
  1367. priv->gpriv = gpriv;
  1368. SET_NETDEV_DEV(ndev, &pdev->dev);
  1369. netif_napi_add(ndev, &priv->napi, rcar_canfd_rx_poll,
  1370. RCANFD_NAPI_WEIGHT);
  1371. err = register_candev(ndev);
  1372. if (err) {
  1373. dev_err(&pdev->dev,
  1374. "register_candev() failed, error %d\n", err);
  1375. goto fail_candev;
  1376. }
  1377. spin_lock_init(&priv->tx_lock);
  1378. devm_can_led_init(ndev);
  1379. gpriv->ch[priv->channel] = priv;
  1380. dev_info(&pdev->dev, "device registered (channel %u)\n", priv->channel);
  1381. return 0;
  1382. fail_candev:
  1383. netif_napi_del(&priv->napi);
  1384. free_candev(ndev);
  1385. fail:
  1386. return err;
  1387. }
  1388. static void rcar_canfd_channel_remove(struct rcar_canfd_global *gpriv, u32 ch)
  1389. {
  1390. struct rcar_canfd_channel *priv = gpriv->ch[ch];
  1391. if (priv) {
  1392. unregister_candev(priv->ndev);
  1393. netif_napi_del(&priv->napi);
  1394. free_candev(priv->ndev);
  1395. }
  1396. }
  1397. static int rcar_canfd_probe(struct platform_device *pdev)
  1398. {
  1399. struct resource *mem;
  1400. void __iomem *addr;
  1401. u32 sts, ch, fcan_freq;
  1402. struct rcar_canfd_global *gpriv;
  1403. struct device_node *of_child;
  1404. unsigned long channels_mask = 0;
  1405. int err, ch_irq, g_irq;
  1406. bool fdmode = true; /* CAN FD only mode - default */
  1407. if (of_property_read_bool(pdev->dev.of_node, "renesas,no-can-fd"))
  1408. fdmode = false; /* Classical CAN only mode */
  1409. of_child = of_get_child_by_name(pdev->dev.of_node, "channel0");
  1410. if (of_child && of_device_is_available(of_child))
  1411. channels_mask |= BIT(0); /* Channel 0 */
  1412. of_child = of_get_child_by_name(pdev->dev.of_node, "channel1");
  1413. if (of_child && of_device_is_available(of_child))
  1414. channels_mask |= BIT(1); /* Channel 1 */
  1415. ch_irq = platform_get_irq(pdev, 0);
  1416. if (ch_irq < 0) {
  1417. dev_err(&pdev->dev, "no Channel IRQ resource\n");
  1418. err = ch_irq;
  1419. goto fail_dev;
  1420. }
  1421. g_irq = platform_get_irq(pdev, 1);
  1422. if (g_irq < 0) {
  1423. dev_err(&pdev->dev, "no Global IRQ resource\n");
  1424. err = g_irq;
  1425. goto fail_dev;
  1426. }
  1427. /* Global controller context */
  1428. gpriv = devm_kzalloc(&pdev->dev, sizeof(*gpriv), GFP_KERNEL);
  1429. if (!gpriv) {
  1430. err = -ENOMEM;
  1431. goto fail_dev;
  1432. }
  1433. gpriv->pdev = pdev;
  1434. gpriv->channels_mask = channels_mask;
  1435. gpriv->fdmode = fdmode;
  1436. /* Peripheral clock */
  1437. gpriv->clkp = devm_clk_get(&pdev->dev, "fck");
  1438. if (IS_ERR(gpriv->clkp)) {
  1439. err = PTR_ERR(gpriv->clkp);
  1440. dev_err(&pdev->dev, "cannot get peripheral clock, error %d\n",
  1441. err);
  1442. goto fail_dev;
  1443. }
  1444. /* fCAN clock: Pick External clock. If not available fallback to
  1445. * CANFD clock
  1446. */
  1447. gpriv->can_clk = devm_clk_get(&pdev->dev, "can_clk");
  1448. if (IS_ERR(gpriv->can_clk) || (clk_get_rate(gpriv->can_clk) == 0)) {
  1449. gpriv->can_clk = devm_clk_get(&pdev->dev, "canfd");
  1450. if (IS_ERR(gpriv->can_clk)) {
  1451. err = PTR_ERR(gpriv->can_clk);
  1452. dev_err(&pdev->dev,
  1453. "cannot get canfd clock, error %d\n", err);
  1454. goto fail_dev;
  1455. }
  1456. gpriv->fcan = RCANFD_CANFDCLK;
  1457. } else {
  1458. gpriv->fcan = RCANFD_EXTCLK;
  1459. }
  1460. fcan_freq = clk_get_rate(gpriv->can_clk);
  1461. if (gpriv->fcan == RCANFD_CANFDCLK)
  1462. /* CANFD clock is further divided by (1/2) within the IP */
  1463. fcan_freq /= 2;
  1464. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1465. addr = devm_ioremap_resource(&pdev->dev, mem);
  1466. if (IS_ERR(addr)) {
  1467. err = PTR_ERR(addr);
  1468. goto fail_dev;
  1469. }
  1470. gpriv->base = addr;
  1471. /* Request IRQ that's common for both channels */
  1472. err = devm_request_irq(&pdev->dev, ch_irq,
  1473. rcar_canfd_channel_interrupt, 0,
  1474. "canfd.chn", gpriv);
  1475. if (err) {
  1476. dev_err(&pdev->dev, "devm_request_irq(%d) failed, error %d\n",
  1477. ch_irq, err);
  1478. goto fail_dev;
  1479. }
  1480. err = devm_request_irq(&pdev->dev, g_irq,
  1481. rcar_canfd_global_interrupt, 0,
  1482. "canfd.gbl", gpriv);
  1483. if (err) {
  1484. dev_err(&pdev->dev, "devm_request_irq(%d) failed, error %d\n",
  1485. g_irq, err);
  1486. goto fail_dev;
  1487. }
  1488. /* Enable peripheral clock for register access */
  1489. err = clk_prepare_enable(gpriv->clkp);
  1490. if (err) {
  1491. dev_err(&pdev->dev,
  1492. "failed to enable peripheral clock, error %d\n", err);
  1493. goto fail_dev;
  1494. }
  1495. err = rcar_canfd_reset_controller(gpriv);
  1496. if (err) {
  1497. dev_err(&pdev->dev, "reset controller failed\n");
  1498. goto fail_clk;
  1499. }
  1500. /* Controller in Global reset & Channel reset mode */
  1501. rcar_canfd_configure_controller(gpriv);
  1502. /* Configure per channel attributes */
  1503. for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) {
  1504. /* Configure Channel's Rx fifo */
  1505. rcar_canfd_configure_rx(gpriv, ch);
  1506. /* Configure Channel's Tx (Common) fifo */
  1507. rcar_canfd_configure_tx(gpriv, ch);
  1508. /* Configure receive rules */
  1509. rcar_canfd_configure_afl_rules(gpriv, ch);
  1510. }
  1511. /* Configure common interrupts */
  1512. rcar_canfd_enable_global_interrupts(gpriv);
  1513. /* Start Global operation mode */
  1514. rcar_canfd_update_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GMDC_MASK,
  1515. RCANFD_GCTR_GMDC_GOPM);
  1516. /* Verify mode change */
  1517. err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts,
  1518. !(sts & RCANFD_GSTS_GNOPM), 2, 500000);
  1519. if (err) {
  1520. dev_err(&pdev->dev, "global operational mode failed\n");
  1521. goto fail_mode;
  1522. }
  1523. for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) {
  1524. err = rcar_canfd_channel_probe(gpriv, ch, fcan_freq);
  1525. if (err)
  1526. goto fail_channel;
  1527. }
  1528. platform_set_drvdata(pdev, gpriv);
  1529. dev_info(&pdev->dev, "global operational state (clk %d, fdmode %d)\n",
  1530. gpriv->fcan, gpriv->fdmode);
  1531. return 0;
  1532. fail_channel:
  1533. for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS)
  1534. rcar_canfd_channel_remove(gpriv, ch);
  1535. fail_mode:
  1536. rcar_canfd_disable_global_interrupts(gpriv);
  1537. fail_clk:
  1538. clk_disable_unprepare(gpriv->clkp);
  1539. fail_dev:
  1540. return err;
  1541. }
  1542. static int rcar_canfd_remove(struct platform_device *pdev)
  1543. {
  1544. struct rcar_canfd_global *gpriv = platform_get_drvdata(pdev);
  1545. u32 ch;
  1546. rcar_canfd_reset_controller(gpriv);
  1547. rcar_canfd_disable_global_interrupts(gpriv);
  1548. for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) {
  1549. rcar_canfd_disable_channel_interrupts(gpriv->ch[ch]);
  1550. rcar_canfd_channel_remove(gpriv, ch);
  1551. }
  1552. /* Enter global sleep mode */
  1553. rcar_canfd_set_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GSLPR);
  1554. clk_disable_unprepare(gpriv->clkp);
  1555. return 0;
  1556. }
  1557. static int __maybe_unused rcar_canfd_suspend(struct device *dev)
  1558. {
  1559. return 0;
  1560. }
  1561. static int __maybe_unused rcar_canfd_resume(struct device *dev)
  1562. {
  1563. return 0;
  1564. }
  1565. static SIMPLE_DEV_PM_OPS(rcar_canfd_pm_ops, rcar_canfd_suspend,
  1566. rcar_canfd_resume);
  1567. static const struct of_device_id rcar_canfd_of_table[] = {
  1568. { .compatible = "renesas,rcar-gen3-canfd" },
  1569. { }
  1570. };
  1571. MODULE_DEVICE_TABLE(of, rcar_canfd_of_table);
  1572. static struct platform_driver rcar_canfd_driver = {
  1573. .driver = {
  1574. .name = RCANFD_DRV_NAME,
  1575. .of_match_table = of_match_ptr(rcar_canfd_of_table),
  1576. .pm = &rcar_canfd_pm_ops,
  1577. },
  1578. .probe = rcar_canfd_probe,
  1579. .remove = rcar_canfd_remove,
  1580. };
  1581. module_platform_driver(rcar_canfd_driver);
  1582. MODULE_AUTHOR("Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>");
  1583. MODULE_LICENSE("GPL");
  1584. MODULE_DESCRIPTION("CAN FD driver for Renesas R-Car SoC");
  1585. MODULE_ALIAS("platform:" RCANFD_DRV_NAME);