ctucanfd_base.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*******************************************************************************
  3. *
  4. * CTU CAN FD IP Core
  5. *
  6. * Copyright (C) 2015-2018 Ondrej Ille <ondrej.ille@gmail.com> FEE CTU
  7. * Copyright (C) 2018-2021 Ondrej Ille <ondrej.ille@gmail.com> self-funded
  8. * Copyright (C) 2018-2019 Martin Jerabek <martin.jerabek01@gmail.com> FEE CTU
  9. * Copyright (C) 2018-2022 Pavel Pisa <pisa@cmp.felk.cvut.cz> FEE CTU/self-funded
  10. *
  11. * Project advisors:
  12. * Jiri Novak <jnovak@fel.cvut.cz>
  13. * Pavel Pisa <pisa@cmp.felk.cvut.cz>
  14. *
  15. * Department of Measurement (http://meas.fel.cvut.cz/)
  16. * Faculty of Electrical Engineering (http://www.fel.cvut.cz)
  17. * Czech Technical University (http://www.cvut.cz/)
  18. ******************************************************************************/
  19. #include <linux/clk.h>
  20. #include <linux/errno.h>
  21. #include <linux/ethtool.h>
  22. #include <linux/init.h>
  23. #include <linux/bitfield.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/io.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/string.h>
  30. #include <linux/types.h>
  31. #include <linux/can/error.h>
  32. #include <linux/pm_runtime.h>
  33. #include "ctucanfd.h"
  34. #include "ctucanfd_kregs.h"
  35. #include "ctucanfd_kframe.h"
  36. #ifdef DEBUG
  37. #define ctucan_netdev_dbg(ndev, args...) \
  38. netdev_dbg(ndev, args)
  39. #else
  40. #define ctucan_netdev_dbg(...) do { } while (0)
  41. #endif
  42. #define CTUCANFD_ID 0xCAFD
  43. /* TX buffer rotation:
  44. * - when a buffer transitions to empty state, rotate order and priorities
  45. * - if more buffers seem to transition at the same time, rotate by the number of buffers
  46. * - it may be assumed that buffers transition to empty state in FIFO order (because we manage
  47. * priorities that way)
  48. * - at frame filling, do not rotate anything, just increment buffer modulo counter
  49. */
  50. #define CTUCANFD_FLAG_RX_FFW_BUFFERED 1
  51. #define CTUCAN_STATE_TO_TEXT_ENTRY(st) \
  52. [st] = #st
  53. enum ctucan_txtb_status {
  54. TXT_NOT_EXIST = 0x0,
  55. TXT_RDY = 0x1,
  56. TXT_TRAN = 0x2,
  57. TXT_ABTP = 0x3,
  58. TXT_TOK = 0x4,
  59. TXT_ERR = 0x6,
  60. TXT_ABT = 0x7,
  61. TXT_ETY = 0x8,
  62. };
  63. enum ctucan_txtb_command {
  64. TXT_CMD_SET_EMPTY = 0x01,
  65. TXT_CMD_SET_READY = 0x02,
  66. TXT_CMD_SET_ABORT = 0x04
  67. };
  68. static const struct can_bittiming_const ctu_can_fd_bit_timing_max = {
  69. .name = "ctu_can_fd",
  70. .tseg1_min = 2,
  71. .tseg1_max = 190,
  72. .tseg2_min = 1,
  73. .tseg2_max = 63,
  74. .sjw_max = 31,
  75. .brp_min = 1,
  76. .brp_max = 8,
  77. .brp_inc = 1,
  78. };
  79. static const struct can_bittiming_const ctu_can_fd_bit_timing_data_max = {
  80. .name = "ctu_can_fd",
  81. .tseg1_min = 2,
  82. .tseg1_max = 94,
  83. .tseg2_min = 1,
  84. .tseg2_max = 31,
  85. .sjw_max = 31,
  86. .brp_min = 1,
  87. .brp_max = 2,
  88. .brp_inc = 1,
  89. };
  90. static const char * const ctucan_state_strings[CAN_STATE_MAX] = {
  91. CTUCAN_STATE_TO_TEXT_ENTRY(CAN_STATE_ERROR_ACTIVE),
  92. CTUCAN_STATE_TO_TEXT_ENTRY(CAN_STATE_ERROR_WARNING),
  93. CTUCAN_STATE_TO_TEXT_ENTRY(CAN_STATE_ERROR_PASSIVE),
  94. CTUCAN_STATE_TO_TEXT_ENTRY(CAN_STATE_BUS_OFF),
  95. CTUCAN_STATE_TO_TEXT_ENTRY(CAN_STATE_STOPPED),
  96. CTUCAN_STATE_TO_TEXT_ENTRY(CAN_STATE_SLEEPING)
  97. };
  98. static void ctucan_write32_le(struct ctucan_priv *priv,
  99. enum ctu_can_fd_can_registers reg, u32 val)
  100. {
  101. iowrite32(val, priv->mem_base + reg);
  102. }
  103. static void ctucan_write32_be(struct ctucan_priv *priv,
  104. enum ctu_can_fd_can_registers reg, u32 val)
  105. {
  106. iowrite32be(val, priv->mem_base + reg);
  107. }
  108. static u32 ctucan_read32_le(struct ctucan_priv *priv,
  109. enum ctu_can_fd_can_registers reg)
  110. {
  111. return ioread32(priv->mem_base + reg);
  112. }
  113. static u32 ctucan_read32_be(struct ctucan_priv *priv,
  114. enum ctu_can_fd_can_registers reg)
  115. {
  116. return ioread32be(priv->mem_base + reg);
  117. }
  118. static void ctucan_write32(struct ctucan_priv *priv, enum ctu_can_fd_can_registers reg, u32 val)
  119. {
  120. priv->write_reg(priv, reg, val);
  121. }
  122. static u32 ctucan_read32(struct ctucan_priv *priv, enum ctu_can_fd_can_registers reg)
  123. {
  124. return priv->read_reg(priv, reg);
  125. }
  126. static void ctucan_write_txt_buf(struct ctucan_priv *priv, enum ctu_can_fd_can_registers buf_base,
  127. u32 offset, u32 val)
  128. {
  129. priv->write_reg(priv, buf_base + offset, val);
  130. }
  131. #define CTU_CAN_FD_TXTNF(priv) (!!FIELD_GET(REG_STATUS_TXNF, ctucan_read32(priv, CTUCANFD_STATUS)))
  132. #define CTU_CAN_FD_ENABLED(priv) (!!FIELD_GET(REG_MODE_ENA, ctucan_read32(priv, CTUCANFD_MODE)))
  133. /**
  134. * ctucan_state_to_str() - Converts CAN controller state code to corresponding text
  135. * @state: CAN controller state code
  136. *
  137. * Return: Pointer to string representation of the error state
  138. */
  139. static const char *ctucan_state_to_str(enum can_state state)
  140. {
  141. const char *txt = NULL;
  142. if (state >= 0 && state < CAN_STATE_MAX)
  143. txt = ctucan_state_strings[state];
  144. return txt ? txt : "UNKNOWN";
  145. }
  146. /**
  147. * ctucan_reset() - Issues software reset request to CTU CAN FD
  148. * @ndev: Pointer to net_device structure
  149. *
  150. * Return: 0 for success, -%ETIMEDOUT if CAN controller does not leave reset
  151. */
  152. static int ctucan_reset(struct net_device *ndev)
  153. {
  154. struct ctucan_priv *priv = netdev_priv(ndev);
  155. int i = 100;
  156. ctucan_write32(priv, CTUCANFD_MODE, REG_MODE_RST);
  157. clear_bit(CTUCANFD_FLAG_RX_FFW_BUFFERED, &priv->drv_flags);
  158. do {
  159. u16 device_id = FIELD_GET(REG_DEVICE_ID_DEVICE_ID,
  160. ctucan_read32(priv, CTUCANFD_DEVICE_ID));
  161. if (device_id == 0xCAFD)
  162. return 0;
  163. if (!i--) {
  164. netdev_warn(ndev, "device did not leave reset\n");
  165. return -ETIMEDOUT;
  166. }
  167. usleep_range(100, 200);
  168. } while (1);
  169. }
  170. /**
  171. * ctucan_set_btr() - Sets CAN bus bit timing in CTU CAN FD
  172. * @ndev: Pointer to net_device structure
  173. * @bt: Pointer to Bit timing structure
  174. * @nominal: True - Nominal bit timing, False - Data bit timing
  175. *
  176. * Return: 0 - OK, -%EPERM if controller is enabled
  177. */
  178. static int ctucan_set_btr(struct net_device *ndev, struct can_bittiming *bt, bool nominal)
  179. {
  180. struct ctucan_priv *priv = netdev_priv(ndev);
  181. int max_ph1_len = 31;
  182. u32 btr = 0;
  183. u32 prop_seg = bt->prop_seg;
  184. u32 phase_seg1 = bt->phase_seg1;
  185. if (CTU_CAN_FD_ENABLED(priv)) {
  186. netdev_err(ndev, "BUG! Cannot set bittiming - CAN is enabled\n");
  187. return -EPERM;
  188. }
  189. if (nominal)
  190. max_ph1_len = 63;
  191. /* The timing calculation functions have only constraints on tseg1, which is prop_seg +
  192. * phase1_seg combined. tseg1 is then split in half and stored into prog_seg and phase_seg1.
  193. * In CTU CAN FD, PROP is 6/7 bits wide but PH1 only 6/5, so we must re-distribute the
  194. * values here.
  195. */
  196. if (phase_seg1 > max_ph1_len) {
  197. prop_seg += phase_seg1 - max_ph1_len;
  198. phase_seg1 = max_ph1_len;
  199. bt->prop_seg = prop_seg;
  200. bt->phase_seg1 = phase_seg1;
  201. }
  202. if (nominal) {
  203. btr = FIELD_PREP(REG_BTR_PROP, prop_seg);
  204. btr |= FIELD_PREP(REG_BTR_PH1, phase_seg1);
  205. btr |= FIELD_PREP(REG_BTR_PH2, bt->phase_seg2);
  206. btr |= FIELD_PREP(REG_BTR_BRP, bt->brp);
  207. btr |= FIELD_PREP(REG_BTR_SJW, bt->sjw);
  208. ctucan_write32(priv, CTUCANFD_BTR, btr);
  209. } else {
  210. btr = FIELD_PREP(REG_BTR_FD_PROP_FD, prop_seg);
  211. btr |= FIELD_PREP(REG_BTR_FD_PH1_FD, phase_seg1);
  212. btr |= FIELD_PREP(REG_BTR_FD_PH2_FD, bt->phase_seg2);
  213. btr |= FIELD_PREP(REG_BTR_FD_BRP_FD, bt->brp);
  214. btr |= FIELD_PREP(REG_BTR_FD_SJW_FD, bt->sjw);
  215. ctucan_write32(priv, CTUCANFD_BTR_FD, btr);
  216. }
  217. return 0;
  218. }
  219. /**
  220. * ctucan_set_bittiming() - CAN set nominal bit timing routine
  221. * @ndev: Pointer to net_device structure
  222. *
  223. * Return: 0 on success, -%EPERM on error
  224. */
  225. static int ctucan_set_bittiming(struct net_device *ndev)
  226. {
  227. struct ctucan_priv *priv = netdev_priv(ndev);
  228. struct can_bittiming *bt = &priv->can.bittiming;
  229. /* Note that bt may be modified here */
  230. return ctucan_set_btr(ndev, bt, true);
  231. }
  232. /**
  233. * ctucan_set_data_bittiming() - CAN set data bit timing routine
  234. * @ndev: Pointer to net_device structure
  235. *
  236. * Return: 0 on success, -%EPERM on error
  237. */
  238. static int ctucan_set_data_bittiming(struct net_device *ndev)
  239. {
  240. struct ctucan_priv *priv = netdev_priv(ndev);
  241. struct can_bittiming *dbt = &priv->can.data_bittiming;
  242. /* Note that dbt may be modified here */
  243. return ctucan_set_btr(ndev, dbt, false);
  244. }
  245. /**
  246. * ctucan_set_secondary_sample_point() - Sets secondary sample point in CTU CAN FD
  247. * @ndev: Pointer to net_device structure
  248. *
  249. * Return: 0 on success, -%EPERM if controller is enabled
  250. */
  251. static int ctucan_set_secondary_sample_point(struct net_device *ndev)
  252. {
  253. struct ctucan_priv *priv = netdev_priv(ndev);
  254. struct can_bittiming *dbt = &priv->can.data_bittiming;
  255. int ssp_offset = 0;
  256. u32 ssp_cfg = 0; /* No SSP by default */
  257. if (CTU_CAN_FD_ENABLED(priv)) {
  258. netdev_err(ndev, "BUG! Cannot set SSP - CAN is enabled\n");
  259. return -EPERM;
  260. }
  261. /* Use SSP for bit-rates above 1 Mbits/s */
  262. if (dbt->bitrate > 1000000) {
  263. /* Calculate SSP in minimal time quanta */
  264. ssp_offset = (priv->can.clock.freq / 1000) * dbt->sample_point / dbt->bitrate;
  265. if (ssp_offset > 127) {
  266. netdev_warn(ndev, "SSP offset saturated to 127\n");
  267. ssp_offset = 127;
  268. }
  269. ssp_cfg = FIELD_PREP(REG_TRV_DELAY_SSP_OFFSET, ssp_offset);
  270. ssp_cfg |= FIELD_PREP(REG_TRV_DELAY_SSP_SRC, 0x1);
  271. }
  272. ctucan_write32(priv, CTUCANFD_TRV_DELAY, ssp_cfg);
  273. return 0;
  274. }
  275. /**
  276. * ctucan_set_mode() - Sets CTU CAN FDs mode
  277. * @priv: Pointer to private data
  278. * @mode: Pointer to controller modes to be set
  279. */
  280. static void ctucan_set_mode(struct ctucan_priv *priv, const struct can_ctrlmode *mode)
  281. {
  282. u32 mode_reg = ctucan_read32(priv, CTUCANFD_MODE);
  283. mode_reg = (mode->flags & CAN_CTRLMODE_LOOPBACK) ?
  284. (mode_reg | REG_MODE_ILBP) :
  285. (mode_reg & ~REG_MODE_ILBP);
  286. mode_reg = (mode->flags & CAN_CTRLMODE_LISTENONLY) ?
  287. (mode_reg | REG_MODE_BMM) :
  288. (mode_reg & ~REG_MODE_BMM);
  289. mode_reg = (mode->flags & CAN_CTRLMODE_FD) ?
  290. (mode_reg | REG_MODE_FDE) :
  291. (mode_reg & ~REG_MODE_FDE);
  292. mode_reg = (mode->flags & CAN_CTRLMODE_PRESUME_ACK) ?
  293. (mode_reg | REG_MODE_ACF) :
  294. (mode_reg & ~REG_MODE_ACF);
  295. mode_reg = (mode->flags & CAN_CTRLMODE_FD_NON_ISO) ?
  296. (mode_reg | REG_MODE_NISOFD) :
  297. (mode_reg & ~REG_MODE_NISOFD);
  298. /* One shot mode supported indirectly via Retransmit limit */
  299. mode_reg &= ~FIELD_PREP(REG_MODE_RTRTH, 0xF);
  300. mode_reg = (mode->flags & CAN_CTRLMODE_ONE_SHOT) ?
  301. (mode_reg | REG_MODE_RTRLE) :
  302. (mode_reg & ~REG_MODE_RTRLE);
  303. /* Some bits fixed:
  304. * TSTM - Off, User shall not be able to change REC/TEC by hand during operation
  305. */
  306. mode_reg &= ~REG_MODE_TSTM;
  307. ctucan_write32(priv, CTUCANFD_MODE, mode_reg);
  308. }
  309. /**
  310. * ctucan_chip_start() - This routine starts the driver
  311. * @ndev: Pointer to net_device structure
  312. *
  313. * Routine expects that chip is in reset state. It setups initial
  314. * Tx buffers for FIFO priorities, sets bittiming, enables interrupts,
  315. * switches core to operational mode and changes controller
  316. * state to %CAN_STATE_STOPPED.
  317. *
  318. * Return: 0 on success and failure value on error
  319. */
  320. static int ctucan_chip_start(struct net_device *ndev)
  321. {
  322. struct ctucan_priv *priv = netdev_priv(ndev);
  323. u32 int_ena, int_msk;
  324. u32 mode_reg;
  325. int err;
  326. struct can_ctrlmode mode;
  327. priv->txb_prio = 0x01234567;
  328. priv->txb_head = 0;
  329. priv->txb_tail = 0;
  330. ctucan_write32(priv, CTUCANFD_TX_PRIORITY, priv->txb_prio);
  331. /* Configure bit-rates and ssp */
  332. err = ctucan_set_bittiming(ndev);
  333. if (err < 0)
  334. return err;
  335. err = ctucan_set_data_bittiming(ndev);
  336. if (err < 0)
  337. return err;
  338. err = ctucan_set_secondary_sample_point(ndev);
  339. if (err < 0)
  340. return err;
  341. /* Configure modes */
  342. mode.flags = priv->can.ctrlmode;
  343. mode.mask = 0xFFFFFFFF;
  344. ctucan_set_mode(priv, &mode);
  345. /* Configure interrupts */
  346. int_ena = REG_INT_STAT_RBNEI |
  347. REG_INT_STAT_TXBHCI |
  348. REG_INT_STAT_EWLI |
  349. REG_INT_STAT_FCSI;
  350. /* Bus error reporting -> Allow Error/Arb.lost interrupts */
  351. if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) {
  352. int_ena |= REG_INT_STAT_ALI |
  353. REG_INT_STAT_BEI;
  354. }
  355. int_msk = ~int_ena; /* Mask all disabled interrupts */
  356. /* It's after reset, so there is no need to clear anything */
  357. ctucan_write32(priv, CTUCANFD_INT_MASK_SET, int_msk);
  358. ctucan_write32(priv, CTUCANFD_INT_ENA_SET, int_ena);
  359. /* Controller enters ERROR_ACTIVE on initial FCSI */
  360. priv->can.state = CAN_STATE_STOPPED;
  361. /* Enable the controller */
  362. mode_reg = ctucan_read32(priv, CTUCANFD_MODE);
  363. mode_reg |= REG_MODE_ENA;
  364. ctucan_write32(priv, CTUCANFD_MODE, mode_reg);
  365. return 0;
  366. }
  367. /**
  368. * ctucan_do_set_mode() - Sets mode of the driver
  369. * @ndev: Pointer to net_device structure
  370. * @mode: Tells the mode of the driver
  371. *
  372. * This check the drivers state and calls the corresponding modes to set.
  373. *
  374. * Return: 0 on success and failure value on error
  375. */
  376. static int ctucan_do_set_mode(struct net_device *ndev, enum can_mode mode)
  377. {
  378. int ret;
  379. switch (mode) {
  380. case CAN_MODE_START:
  381. ret = ctucan_reset(ndev);
  382. if (ret < 0)
  383. return ret;
  384. ret = ctucan_chip_start(ndev);
  385. if (ret < 0) {
  386. netdev_err(ndev, "ctucan_chip_start failed!\n");
  387. return ret;
  388. }
  389. netif_wake_queue(ndev);
  390. break;
  391. default:
  392. ret = -EOPNOTSUPP;
  393. break;
  394. }
  395. return ret;
  396. }
  397. /**
  398. * ctucan_get_tx_status() - Gets status of TXT buffer
  399. * @priv: Pointer to private data
  400. * @buf: Buffer index (0-based)
  401. *
  402. * Return: Status of TXT buffer
  403. */
  404. static enum ctucan_txtb_status ctucan_get_tx_status(struct ctucan_priv *priv, u8 buf)
  405. {
  406. u32 tx_status = ctucan_read32(priv, CTUCANFD_TX_STATUS);
  407. enum ctucan_txtb_status status = (tx_status >> (buf * 4)) & 0x7;
  408. return status;
  409. }
  410. /**
  411. * ctucan_is_txt_buf_writable() - Checks if frame can be inserted to TXT Buffer
  412. * @priv: Pointer to private data
  413. * @buf: Buffer index (0-based)
  414. *
  415. * Return: True - Frame can be inserted to TXT Buffer, False - If attempted, frame will not be
  416. * inserted to TXT Buffer
  417. */
  418. static bool ctucan_is_txt_buf_writable(struct ctucan_priv *priv, u8 buf)
  419. {
  420. enum ctucan_txtb_status buf_status;
  421. buf_status = ctucan_get_tx_status(priv, buf);
  422. if (buf_status == TXT_RDY || buf_status == TXT_TRAN || buf_status == TXT_ABTP)
  423. return false;
  424. return true;
  425. }
  426. /**
  427. * ctucan_insert_frame() - Inserts frame to TXT buffer
  428. * @priv: Pointer to private data
  429. * @cf: Pointer to CAN frame to be inserted
  430. * @buf: TXT Buffer index to which frame is inserted (0-based)
  431. * @isfdf: True - CAN FD Frame, False - CAN 2.0 Frame
  432. *
  433. * Return: True - Frame inserted successfully
  434. * False - Frame was not inserted due to one of:
  435. * 1. TXT Buffer is not writable (it is in wrong state)
  436. * 2. Invalid TXT buffer index
  437. * 3. Invalid frame length
  438. */
  439. static bool ctucan_insert_frame(struct ctucan_priv *priv, const struct canfd_frame *cf, u8 buf,
  440. bool isfdf)
  441. {
  442. u32 buf_base;
  443. u32 ffw = 0;
  444. u32 idw = 0;
  445. unsigned int i;
  446. if (buf >= priv->ntxbufs)
  447. return false;
  448. if (!ctucan_is_txt_buf_writable(priv, buf))
  449. return false;
  450. if (cf->len > CANFD_MAX_DLEN)
  451. return false;
  452. /* Prepare Frame format */
  453. if (cf->can_id & CAN_RTR_FLAG)
  454. ffw |= REG_FRAME_FORMAT_W_RTR;
  455. if (cf->can_id & CAN_EFF_FLAG)
  456. ffw |= REG_FRAME_FORMAT_W_IDE;
  457. if (isfdf) {
  458. ffw |= REG_FRAME_FORMAT_W_FDF;
  459. if (cf->flags & CANFD_BRS)
  460. ffw |= REG_FRAME_FORMAT_W_BRS;
  461. }
  462. ffw |= FIELD_PREP(REG_FRAME_FORMAT_W_DLC, can_fd_len2dlc(cf->len));
  463. /* Prepare identifier */
  464. if (cf->can_id & CAN_EFF_FLAG)
  465. idw = cf->can_id & CAN_EFF_MASK;
  466. else
  467. idw = FIELD_PREP(REG_IDENTIFIER_W_IDENTIFIER_BASE, cf->can_id & CAN_SFF_MASK);
  468. /* Write ID, Frame format, Don't write timestamp -> Time triggered transmission disabled */
  469. buf_base = (buf + 1) * 0x100;
  470. ctucan_write_txt_buf(priv, buf_base, CTUCANFD_FRAME_FORMAT_W, ffw);
  471. ctucan_write_txt_buf(priv, buf_base, CTUCANFD_IDENTIFIER_W, idw);
  472. /* Write Data payload */
  473. if (!(cf->can_id & CAN_RTR_FLAG)) {
  474. for (i = 0; i < cf->len; i += 4) {
  475. u32 data = le32_to_cpu(*(__le32 *)(cf->data + i));
  476. ctucan_write_txt_buf(priv, buf_base, CTUCANFD_DATA_1_4_W + i, data);
  477. }
  478. }
  479. return true;
  480. }
  481. /**
  482. * ctucan_give_txtb_cmd() - Applies command on TXT buffer
  483. * @priv: Pointer to private data
  484. * @cmd: Command to give
  485. * @buf: Buffer index (0-based)
  486. */
  487. static void ctucan_give_txtb_cmd(struct ctucan_priv *priv, enum ctucan_txtb_command cmd, u8 buf)
  488. {
  489. u32 tx_cmd = cmd;
  490. tx_cmd |= 1 << (buf + 8);
  491. ctucan_write32(priv, CTUCANFD_TX_COMMAND, tx_cmd);
  492. }
  493. /**
  494. * ctucan_start_xmit() - Starts the transmission
  495. * @skb: sk_buff pointer that contains data to be Txed
  496. * @ndev: Pointer to net_device structure
  497. *
  498. * Invoked from upper layers to initiate transmission. Uses the next available free TXT Buffer and
  499. * populates its fields to start the transmission.
  500. *
  501. * Return: %NETDEV_TX_OK on success, %NETDEV_TX_BUSY when no free TXT buffer is available,
  502. * negative return values reserved for error cases
  503. */
  504. static netdev_tx_t ctucan_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  505. {
  506. struct ctucan_priv *priv = netdev_priv(ndev);
  507. struct canfd_frame *cf = (struct canfd_frame *)skb->data;
  508. u32 txtb_id;
  509. bool ok;
  510. unsigned long flags;
  511. if (can_dev_dropped_skb(ndev, skb))
  512. return NETDEV_TX_OK;
  513. if (unlikely(!CTU_CAN_FD_TXTNF(priv))) {
  514. netif_stop_queue(ndev);
  515. netdev_err(ndev, "BUG!, no TXB free when queue awake!\n");
  516. return NETDEV_TX_BUSY;
  517. }
  518. txtb_id = priv->txb_head % priv->ntxbufs;
  519. ctucan_netdev_dbg(ndev, "%s: using TXB#%u\n", __func__, txtb_id);
  520. ok = ctucan_insert_frame(priv, cf, txtb_id, can_is_canfd_skb(skb));
  521. if (!ok) {
  522. netdev_err(ndev, "BUG! TXNF set but cannot insert frame into TXTB! HW Bug?");
  523. kfree_skb(skb);
  524. ndev->stats.tx_dropped++;
  525. return NETDEV_TX_OK;
  526. }
  527. can_put_echo_skb(skb, ndev, txtb_id, 0);
  528. spin_lock_irqsave(&priv->tx_lock, flags);
  529. ctucan_give_txtb_cmd(priv, TXT_CMD_SET_READY, txtb_id);
  530. priv->txb_head++;
  531. /* Check if all TX buffers are full */
  532. if (!CTU_CAN_FD_TXTNF(priv))
  533. netif_stop_queue(ndev);
  534. spin_unlock_irqrestore(&priv->tx_lock, flags);
  535. return NETDEV_TX_OK;
  536. }
  537. /**
  538. * ctucan_read_rx_frame() - Reads frame from RX FIFO
  539. * @priv: Pointer to CTU CAN FD's private data
  540. * @cf: Pointer to CAN frame struct
  541. * @ffw: Previously read frame format word
  542. *
  543. * Note: Frame format word must be read separately and provided in 'ffw'.
  544. */
  545. static void ctucan_read_rx_frame(struct ctucan_priv *priv, struct canfd_frame *cf, u32 ffw)
  546. {
  547. u32 idw;
  548. unsigned int i;
  549. unsigned int wc;
  550. unsigned int len;
  551. idw = ctucan_read32(priv, CTUCANFD_RX_DATA);
  552. if (FIELD_GET(REG_FRAME_FORMAT_W_IDE, ffw))
  553. cf->can_id = (idw & CAN_EFF_MASK) | CAN_EFF_FLAG;
  554. else
  555. cf->can_id = (idw >> 18) & CAN_SFF_MASK;
  556. /* BRS, ESI, RTR Flags */
  557. if (FIELD_GET(REG_FRAME_FORMAT_W_FDF, ffw)) {
  558. if (FIELD_GET(REG_FRAME_FORMAT_W_BRS, ffw))
  559. cf->flags |= CANFD_BRS;
  560. if (FIELD_GET(REG_FRAME_FORMAT_W_ESI_RSV, ffw))
  561. cf->flags |= CANFD_ESI;
  562. } else if (FIELD_GET(REG_FRAME_FORMAT_W_RTR, ffw)) {
  563. cf->can_id |= CAN_RTR_FLAG;
  564. }
  565. wc = FIELD_GET(REG_FRAME_FORMAT_W_RWCNT, ffw) - 3;
  566. /* DLC */
  567. if (FIELD_GET(REG_FRAME_FORMAT_W_DLC, ffw) <= 8) {
  568. len = FIELD_GET(REG_FRAME_FORMAT_W_DLC, ffw);
  569. } else {
  570. if (FIELD_GET(REG_FRAME_FORMAT_W_FDF, ffw))
  571. len = wc << 2;
  572. else
  573. len = 8;
  574. }
  575. cf->len = len;
  576. if (unlikely(len > wc * 4))
  577. len = wc * 4;
  578. /* Timestamp - Read and throw away */
  579. ctucan_read32(priv, CTUCANFD_RX_DATA);
  580. ctucan_read32(priv, CTUCANFD_RX_DATA);
  581. /* Data */
  582. for (i = 0; i < len; i += 4) {
  583. u32 data = ctucan_read32(priv, CTUCANFD_RX_DATA);
  584. *(__le32 *)(cf->data + i) = cpu_to_le32(data);
  585. }
  586. while (unlikely(i < wc * 4)) {
  587. ctucan_read32(priv, CTUCANFD_RX_DATA);
  588. i += 4;
  589. }
  590. }
  591. /**
  592. * ctucan_rx() - Called from CAN ISR to complete the received frame processing
  593. * @ndev: Pointer to net_device structure
  594. *
  595. * This function is invoked from the CAN isr(poll) to process the Rx frames. It does minimal
  596. * processing and invokes "netif_receive_skb" to complete further processing.
  597. * Return: 1 when frame is passed to the network layer, 0 when the first frame word is read but
  598. * system is out of free SKBs temporally and left code to resolve SKB allocation later,
  599. * -%EAGAIN in a case of empty Rx FIFO.
  600. */
  601. static int ctucan_rx(struct net_device *ndev)
  602. {
  603. struct ctucan_priv *priv = netdev_priv(ndev);
  604. struct net_device_stats *stats = &ndev->stats;
  605. struct canfd_frame *cf;
  606. struct sk_buff *skb;
  607. u32 ffw;
  608. if (test_bit(CTUCANFD_FLAG_RX_FFW_BUFFERED, &priv->drv_flags)) {
  609. ffw = priv->rxfrm_first_word;
  610. clear_bit(CTUCANFD_FLAG_RX_FFW_BUFFERED, &priv->drv_flags);
  611. } else {
  612. ffw = ctucan_read32(priv, CTUCANFD_RX_DATA);
  613. }
  614. if (!FIELD_GET(REG_FRAME_FORMAT_W_RWCNT, ffw))
  615. return -EAGAIN;
  616. if (FIELD_GET(REG_FRAME_FORMAT_W_FDF, ffw))
  617. skb = alloc_canfd_skb(ndev, &cf);
  618. else
  619. skb = alloc_can_skb(ndev, (struct can_frame **)&cf);
  620. if (unlikely(!skb)) {
  621. priv->rxfrm_first_word = ffw;
  622. set_bit(CTUCANFD_FLAG_RX_FFW_BUFFERED, &priv->drv_flags);
  623. return 0;
  624. }
  625. ctucan_read_rx_frame(priv, cf, ffw);
  626. stats->rx_bytes += cf->len;
  627. stats->rx_packets++;
  628. netif_receive_skb(skb);
  629. return 1;
  630. }
  631. /**
  632. * ctucan_read_fault_state() - Reads CTU CAN FDs fault confinement state.
  633. * @priv: Pointer to private data
  634. *
  635. * Returns: Fault confinement state of controller
  636. */
  637. static enum can_state ctucan_read_fault_state(struct ctucan_priv *priv)
  638. {
  639. u32 fs;
  640. u32 rec_tec;
  641. u32 ewl;
  642. fs = ctucan_read32(priv, CTUCANFD_EWL);
  643. rec_tec = ctucan_read32(priv, CTUCANFD_REC);
  644. ewl = FIELD_GET(REG_EWL_EW_LIMIT, fs);
  645. if (FIELD_GET(REG_EWL_ERA, fs)) {
  646. if (ewl > FIELD_GET(REG_REC_REC_VAL, rec_tec) &&
  647. ewl > FIELD_GET(REG_REC_TEC_VAL, rec_tec))
  648. return CAN_STATE_ERROR_ACTIVE;
  649. else
  650. return CAN_STATE_ERROR_WARNING;
  651. } else if (FIELD_GET(REG_EWL_ERP, fs)) {
  652. return CAN_STATE_ERROR_PASSIVE;
  653. } else if (FIELD_GET(REG_EWL_BOF, fs)) {
  654. return CAN_STATE_BUS_OFF;
  655. }
  656. WARN(true, "Invalid error state");
  657. return CAN_STATE_ERROR_PASSIVE;
  658. }
  659. /**
  660. * ctucan_get_rec_tec() - Reads REC/TEC counter values from controller
  661. * @priv: Pointer to private data
  662. * @bec: Pointer to Error counter structure
  663. */
  664. static void ctucan_get_rec_tec(struct ctucan_priv *priv, struct can_berr_counter *bec)
  665. {
  666. u32 err_ctrs = ctucan_read32(priv, CTUCANFD_REC);
  667. bec->rxerr = FIELD_GET(REG_REC_REC_VAL, err_ctrs);
  668. bec->txerr = FIELD_GET(REG_REC_TEC_VAL, err_ctrs);
  669. }
  670. /**
  671. * ctucan_err_interrupt() - Error frame ISR
  672. * @ndev: net_device pointer
  673. * @isr: interrupt status register value
  674. *
  675. * This is the CAN error interrupt and it will check the type of error and forward the error
  676. * frame to upper layers.
  677. */
  678. static void ctucan_err_interrupt(struct net_device *ndev, u32 isr)
  679. {
  680. struct ctucan_priv *priv = netdev_priv(ndev);
  681. struct net_device_stats *stats = &ndev->stats;
  682. struct can_frame *cf;
  683. struct sk_buff *skb;
  684. enum can_state state;
  685. struct can_berr_counter bec;
  686. u32 err_capt_alc;
  687. int dologerr = net_ratelimit();
  688. ctucan_get_rec_tec(priv, &bec);
  689. state = ctucan_read_fault_state(priv);
  690. err_capt_alc = ctucan_read32(priv, CTUCANFD_ERR_CAPT);
  691. if (dologerr)
  692. netdev_info(ndev, "%s: ISR = 0x%08x, rxerr %d, txerr %d, error type %lu, pos %lu, ALC id_field %lu, bit %lu\n",
  693. __func__, isr, bec.rxerr, bec.txerr,
  694. FIELD_GET(REG_ERR_CAPT_ERR_TYPE, err_capt_alc),
  695. FIELD_GET(REG_ERR_CAPT_ERR_POS, err_capt_alc),
  696. FIELD_GET(REG_ERR_CAPT_ALC_ID_FIELD, err_capt_alc),
  697. FIELD_GET(REG_ERR_CAPT_ALC_BIT, err_capt_alc));
  698. skb = alloc_can_err_skb(ndev, &cf);
  699. /* EWLI: error warning limit condition met
  700. * FCSI: fault confinement state changed
  701. * ALI: arbitration lost (just informative)
  702. * BEI: bus error interrupt
  703. */
  704. if (FIELD_GET(REG_INT_STAT_FCSI, isr) || FIELD_GET(REG_INT_STAT_EWLI, isr)) {
  705. netdev_info(ndev, "state changes from %s to %s\n",
  706. ctucan_state_to_str(priv->can.state),
  707. ctucan_state_to_str(state));
  708. if (priv->can.state == state)
  709. netdev_warn(ndev,
  710. "current and previous state is the same! (missed interrupt?)\n");
  711. priv->can.state = state;
  712. switch (state) {
  713. case CAN_STATE_BUS_OFF:
  714. priv->can.can_stats.bus_off++;
  715. can_bus_off(ndev);
  716. if (skb)
  717. cf->can_id |= CAN_ERR_BUSOFF;
  718. break;
  719. case CAN_STATE_ERROR_PASSIVE:
  720. priv->can.can_stats.error_passive++;
  721. if (skb) {
  722. cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
  723. cf->data[1] = (bec.rxerr > 127) ?
  724. CAN_ERR_CRTL_RX_PASSIVE :
  725. CAN_ERR_CRTL_TX_PASSIVE;
  726. cf->data[6] = bec.txerr;
  727. cf->data[7] = bec.rxerr;
  728. }
  729. break;
  730. case CAN_STATE_ERROR_WARNING:
  731. priv->can.can_stats.error_warning++;
  732. if (skb) {
  733. cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
  734. cf->data[1] |= (bec.txerr > bec.rxerr) ?
  735. CAN_ERR_CRTL_TX_WARNING :
  736. CAN_ERR_CRTL_RX_WARNING;
  737. cf->data[6] = bec.txerr;
  738. cf->data[7] = bec.rxerr;
  739. }
  740. break;
  741. case CAN_STATE_ERROR_ACTIVE:
  742. if (skb) {
  743. cf->can_id |= CAN_ERR_CNT;
  744. cf->data[1] = CAN_ERR_CRTL_ACTIVE;
  745. cf->data[6] = bec.txerr;
  746. cf->data[7] = bec.rxerr;
  747. }
  748. break;
  749. default:
  750. netdev_warn(ndev, "unhandled error state (%d:%s)!\n",
  751. state, ctucan_state_to_str(state));
  752. break;
  753. }
  754. }
  755. /* Check for Arbitration Lost interrupt */
  756. if (FIELD_GET(REG_INT_STAT_ALI, isr)) {
  757. if (dologerr)
  758. netdev_info(ndev, "arbitration lost\n");
  759. priv->can.can_stats.arbitration_lost++;
  760. if (skb) {
  761. cf->can_id |= CAN_ERR_LOSTARB;
  762. cf->data[0] = CAN_ERR_LOSTARB_UNSPEC;
  763. }
  764. }
  765. /* Check for Bus Error interrupt */
  766. if (FIELD_GET(REG_INT_STAT_BEI, isr)) {
  767. netdev_info(ndev, "bus error\n");
  768. priv->can.can_stats.bus_error++;
  769. stats->rx_errors++;
  770. if (skb) {
  771. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  772. cf->data[2] = CAN_ERR_PROT_UNSPEC;
  773. cf->data[3] = CAN_ERR_PROT_LOC_UNSPEC;
  774. }
  775. }
  776. if (skb) {
  777. stats->rx_packets++;
  778. stats->rx_bytes += cf->can_dlc;
  779. netif_rx(skb);
  780. }
  781. }
  782. /**
  783. * ctucan_rx_poll() - Poll routine for rx packets (NAPI)
  784. * @napi: NAPI structure pointer
  785. * @quota: Max number of rx packets to be processed.
  786. *
  787. * This is the poll routine for rx part. It will process the packets maximux quota value.
  788. *
  789. * Return: Number of packets received
  790. */
  791. static int ctucan_rx_poll(struct napi_struct *napi, int quota)
  792. {
  793. struct net_device *ndev = napi->dev;
  794. struct ctucan_priv *priv = netdev_priv(ndev);
  795. int work_done = 0;
  796. u32 status;
  797. u32 framecnt;
  798. int res = 1;
  799. framecnt = FIELD_GET(REG_RX_STATUS_RXFRC, ctucan_read32(priv, CTUCANFD_RX_STATUS));
  800. while (framecnt && work_done < quota && res > 0) {
  801. res = ctucan_rx(ndev);
  802. work_done++;
  803. framecnt = FIELD_GET(REG_RX_STATUS_RXFRC, ctucan_read32(priv, CTUCANFD_RX_STATUS));
  804. }
  805. /* Check for RX FIFO Overflow */
  806. status = ctucan_read32(priv, CTUCANFD_STATUS);
  807. if (FIELD_GET(REG_STATUS_DOR, status)) {
  808. struct net_device_stats *stats = &ndev->stats;
  809. struct can_frame *cf;
  810. struct sk_buff *skb;
  811. netdev_info(ndev, "rx_poll: rx fifo overflow\n");
  812. stats->rx_over_errors++;
  813. stats->rx_errors++;
  814. skb = alloc_can_err_skb(ndev, &cf);
  815. if (skb) {
  816. cf->can_id |= CAN_ERR_CRTL;
  817. cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
  818. stats->rx_packets++;
  819. stats->rx_bytes += cf->can_dlc;
  820. netif_rx(skb);
  821. }
  822. /* Clear Data Overrun */
  823. ctucan_write32(priv, CTUCANFD_COMMAND, REG_COMMAND_CDO);
  824. }
  825. if (!framecnt && res != 0) {
  826. if (napi_complete_done(napi, work_done)) {
  827. /* Clear and enable RBNEI. It is level-triggered, so
  828. * there is no race condition.
  829. */
  830. ctucan_write32(priv, CTUCANFD_INT_STAT, REG_INT_STAT_RBNEI);
  831. ctucan_write32(priv, CTUCANFD_INT_MASK_CLR, REG_INT_STAT_RBNEI);
  832. }
  833. }
  834. return work_done;
  835. }
  836. /**
  837. * ctucan_rotate_txb_prio() - Rotates priorities of TXT Buffers
  838. * @ndev: net_device pointer
  839. */
  840. static void ctucan_rotate_txb_prio(struct net_device *ndev)
  841. {
  842. struct ctucan_priv *priv = netdev_priv(ndev);
  843. u32 prio = priv->txb_prio;
  844. prio = (prio << 4) | ((prio >> ((priv->ntxbufs - 1) * 4)) & 0xF);
  845. ctucan_netdev_dbg(ndev, "%s: from 0x%08x to 0x%08x\n", __func__, priv->txb_prio, prio);
  846. priv->txb_prio = prio;
  847. ctucan_write32(priv, CTUCANFD_TX_PRIORITY, prio);
  848. }
  849. /**
  850. * ctucan_tx_interrupt() - Tx done Isr
  851. * @ndev: net_device pointer
  852. */
  853. static void ctucan_tx_interrupt(struct net_device *ndev)
  854. {
  855. struct ctucan_priv *priv = netdev_priv(ndev);
  856. struct net_device_stats *stats = &ndev->stats;
  857. bool first = true;
  858. bool some_buffers_processed;
  859. unsigned long flags;
  860. enum ctucan_txtb_status txtb_status;
  861. u32 txtb_id;
  862. /* read tx_status
  863. * if txb[n].finished (bit 2)
  864. * if ok -> echo
  865. * if error / aborted -> ?? (find how to handle oneshot mode)
  866. * txb_tail++
  867. */
  868. do {
  869. spin_lock_irqsave(&priv->tx_lock, flags);
  870. some_buffers_processed = false;
  871. while ((int)(priv->txb_head - priv->txb_tail) > 0) {
  872. txtb_id = priv->txb_tail % priv->ntxbufs;
  873. txtb_status = ctucan_get_tx_status(priv, txtb_id);
  874. ctucan_netdev_dbg(ndev, "TXI: TXB#%u: status 0x%x\n", txtb_id, txtb_status);
  875. switch (txtb_status) {
  876. case TXT_TOK:
  877. ctucan_netdev_dbg(ndev, "TXT_OK\n");
  878. stats->tx_bytes += can_get_echo_skb(ndev, txtb_id, NULL);
  879. stats->tx_packets++;
  880. break;
  881. case TXT_ERR:
  882. /* This indicated that retransmit limit has been reached. Obviously
  883. * we should not echo the frame, but also not indicate any kind of
  884. * error. If desired, it was already reported (possible multiple
  885. * times) on each arbitration lost.
  886. */
  887. netdev_warn(ndev, "TXB in Error state\n");
  888. can_free_echo_skb(ndev, txtb_id, NULL);
  889. stats->tx_dropped++;
  890. break;
  891. case TXT_ABT:
  892. /* Same as for TXT_ERR, only with different cause. We *could*
  893. * re-queue the frame, but multiqueue/abort is not supported yet
  894. * anyway.
  895. */
  896. netdev_warn(ndev, "TXB in Aborted state\n");
  897. can_free_echo_skb(ndev, txtb_id, NULL);
  898. stats->tx_dropped++;
  899. break;
  900. default:
  901. /* Bug only if the first buffer is not finished, otherwise it is
  902. * pretty much expected.
  903. */
  904. if (first) {
  905. netdev_err(ndev,
  906. "BUG: TXB#%u not in a finished state (0x%x)!\n",
  907. txtb_id, txtb_status);
  908. spin_unlock_irqrestore(&priv->tx_lock, flags);
  909. /* do not clear nor wake */
  910. return;
  911. }
  912. goto clear;
  913. }
  914. priv->txb_tail++;
  915. first = false;
  916. some_buffers_processed = true;
  917. /* Adjust priorities *before* marking the buffer as empty. */
  918. ctucan_rotate_txb_prio(ndev);
  919. ctucan_give_txtb_cmd(priv, TXT_CMD_SET_EMPTY, txtb_id);
  920. }
  921. clear:
  922. spin_unlock_irqrestore(&priv->tx_lock, flags);
  923. /* If no buffers were processed this time, we cannot clear - that would introduce
  924. * a race condition.
  925. */
  926. if (some_buffers_processed) {
  927. /* Clear the interrupt again. We do not want to receive again interrupt for
  928. * the buffer already handled. If it is the last finished one then it would
  929. * cause log of spurious interrupt.
  930. */
  931. ctucan_write32(priv, CTUCANFD_INT_STAT, REG_INT_STAT_TXBHCI);
  932. }
  933. } while (some_buffers_processed);
  934. spin_lock_irqsave(&priv->tx_lock, flags);
  935. /* Check if at least one TX buffer is free */
  936. if (CTU_CAN_FD_TXTNF(priv))
  937. netif_wake_queue(ndev);
  938. spin_unlock_irqrestore(&priv->tx_lock, flags);
  939. }
  940. /**
  941. * ctucan_interrupt() - CAN Isr
  942. * @irq: irq number
  943. * @dev_id: device id pointer
  944. *
  945. * This is the CTU CAN FD ISR. It checks for the type of interrupt
  946. * and invokes the corresponding ISR.
  947. *
  948. * Return:
  949. * IRQ_NONE - If CAN device is in sleep mode, IRQ_HANDLED otherwise
  950. */
  951. static irqreturn_t ctucan_interrupt(int irq, void *dev_id)
  952. {
  953. struct net_device *ndev = (struct net_device *)dev_id;
  954. struct ctucan_priv *priv = netdev_priv(ndev);
  955. u32 isr, icr;
  956. u32 imask;
  957. int irq_loops;
  958. for (irq_loops = 0; irq_loops < 10000; irq_loops++) {
  959. /* Get the interrupt status */
  960. isr = ctucan_read32(priv, CTUCANFD_INT_STAT);
  961. if (!isr)
  962. return irq_loops ? IRQ_HANDLED : IRQ_NONE;
  963. /* Receive Buffer Not Empty Interrupt */
  964. if (FIELD_GET(REG_INT_STAT_RBNEI, isr)) {
  965. ctucan_netdev_dbg(ndev, "RXBNEI\n");
  966. /* Mask RXBNEI the first, then clear interrupt and schedule NAPI. Even if
  967. * another IRQ fires, RBNEI will always be 0 (masked).
  968. */
  969. icr = REG_INT_STAT_RBNEI;
  970. ctucan_write32(priv, CTUCANFD_INT_MASK_SET, icr);
  971. ctucan_write32(priv, CTUCANFD_INT_STAT, icr);
  972. napi_schedule(&priv->napi);
  973. }
  974. /* TXT Buffer HW Command Interrupt */
  975. if (FIELD_GET(REG_INT_STAT_TXBHCI, isr)) {
  976. ctucan_netdev_dbg(ndev, "TXBHCI\n");
  977. /* Cleared inside */
  978. ctucan_tx_interrupt(ndev);
  979. }
  980. /* Error interrupts */
  981. if (FIELD_GET(REG_INT_STAT_EWLI, isr) ||
  982. FIELD_GET(REG_INT_STAT_FCSI, isr) ||
  983. FIELD_GET(REG_INT_STAT_ALI, isr)) {
  984. icr = isr & (REG_INT_STAT_EWLI | REG_INT_STAT_FCSI | REG_INT_STAT_ALI);
  985. ctucan_netdev_dbg(ndev, "some ERR interrupt: clearing 0x%08x\n", icr);
  986. ctucan_write32(priv, CTUCANFD_INT_STAT, icr);
  987. ctucan_err_interrupt(ndev, isr);
  988. }
  989. /* Ignore RI, TI, LFI, RFI, BSI */
  990. }
  991. netdev_err(ndev, "%s: stuck interrupt (isr=0x%08x), stopping\n", __func__, isr);
  992. if (FIELD_GET(REG_INT_STAT_TXBHCI, isr)) {
  993. int i;
  994. netdev_err(ndev, "txb_head=0x%08x txb_tail=0x%08x\n",
  995. priv->txb_head, priv->txb_tail);
  996. for (i = 0; i < priv->ntxbufs; i++) {
  997. u32 status = ctucan_get_tx_status(priv, i);
  998. netdev_err(ndev, "txb[%d] txb status=0x%08x\n", i, status);
  999. }
  1000. }
  1001. imask = 0xffffffff;
  1002. ctucan_write32(priv, CTUCANFD_INT_ENA_CLR, imask);
  1003. ctucan_write32(priv, CTUCANFD_INT_MASK_SET, imask);
  1004. return IRQ_HANDLED;
  1005. }
  1006. /**
  1007. * ctucan_chip_stop() - Driver stop routine
  1008. * @ndev: Pointer to net_device structure
  1009. *
  1010. * This is the drivers stop routine. It will disable the
  1011. * interrupts and disable the controller.
  1012. */
  1013. static void ctucan_chip_stop(struct net_device *ndev)
  1014. {
  1015. struct ctucan_priv *priv = netdev_priv(ndev);
  1016. u32 mask = 0xffffffff;
  1017. u32 mode;
  1018. /* Disable interrupts and disable CAN */
  1019. ctucan_write32(priv, CTUCANFD_INT_ENA_CLR, mask);
  1020. ctucan_write32(priv, CTUCANFD_INT_MASK_SET, mask);
  1021. mode = ctucan_read32(priv, CTUCANFD_MODE);
  1022. mode &= ~REG_MODE_ENA;
  1023. ctucan_write32(priv, CTUCANFD_MODE, mode);
  1024. priv->can.state = CAN_STATE_STOPPED;
  1025. }
  1026. /**
  1027. * ctucan_open() - Driver open routine
  1028. * @ndev: Pointer to net_device structure
  1029. *
  1030. * This is the driver open routine.
  1031. * Return: 0 on success and failure value on error
  1032. */
  1033. static int ctucan_open(struct net_device *ndev)
  1034. {
  1035. struct ctucan_priv *priv = netdev_priv(ndev);
  1036. int ret;
  1037. ret = pm_runtime_get_sync(priv->dev);
  1038. if (ret < 0) {
  1039. netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
  1040. __func__, ret);
  1041. pm_runtime_put_noidle(priv->dev);
  1042. return ret;
  1043. }
  1044. ret = ctucan_reset(ndev);
  1045. if (ret < 0)
  1046. goto err_reset;
  1047. /* Common open */
  1048. ret = open_candev(ndev);
  1049. if (ret) {
  1050. netdev_warn(ndev, "open_candev failed!\n");
  1051. goto err_open;
  1052. }
  1053. ret = request_irq(ndev->irq, ctucan_interrupt, priv->irq_flags, ndev->name, ndev);
  1054. if (ret < 0) {
  1055. netdev_err(ndev, "irq allocation for CAN failed\n");
  1056. goto err_irq;
  1057. }
  1058. ret = ctucan_chip_start(ndev);
  1059. if (ret < 0) {
  1060. netdev_err(ndev, "ctucan_chip_start failed!\n");
  1061. goto err_chip_start;
  1062. }
  1063. netdev_info(ndev, "ctu_can_fd device registered\n");
  1064. napi_enable(&priv->napi);
  1065. netif_start_queue(ndev);
  1066. return 0;
  1067. err_chip_start:
  1068. free_irq(ndev->irq, ndev);
  1069. err_irq:
  1070. close_candev(ndev);
  1071. err_open:
  1072. err_reset:
  1073. pm_runtime_put(priv->dev);
  1074. return ret;
  1075. }
  1076. /**
  1077. * ctucan_close() - Driver close routine
  1078. * @ndev: Pointer to net_device structure
  1079. *
  1080. * Return: 0 always
  1081. */
  1082. static int ctucan_close(struct net_device *ndev)
  1083. {
  1084. struct ctucan_priv *priv = netdev_priv(ndev);
  1085. netif_stop_queue(ndev);
  1086. napi_disable(&priv->napi);
  1087. ctucan_chip_stop(ndev);
  1088. free_irq(ndev->irq, ndev);
  1089. close_candev(ndev);
  1090. pm_runtime_put(priv->dev);
  1091. return 0;
  1092. }
  1093. /**
  1094. * ctucan_get_berr_counter() - error counter routine
  1095. * @ndev: Pointer to net_device structure
  1096. * @bec: Pointer to can_berr_counter structure
  1097. *
  1098. * This is the driver error counter routine.
  1099. * Return: 0 on success and failure value on error
  1100. */
  1101. static int ctucan_get_berr_counter(const struct net_device *ndev, struct can_berr_counter *bec)
  1102. {
  1103. struct ctucan_priv *priv = netdev_priv(ndev);
  1104. int ret;
  1105. ret = pm_runtime_get_sync(priv->dev);
  1106. if (ret < 0) {
  1107. netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n", __func__, ret);
  1108. pm_runtime_put_noidle(priv->dev);
  1109. return ret;
  1110. }
  1111. ctucan_get_rec_tec(priv, bec);
  1112. pm_runtime_put(priv->dev);
  1113. return 0;
  1114. }
  1115. static const struct net_device_ops ctucan_netdev_ops = {
  1116. .ndo_open = ctucan_open,
  1117. .ndo_stop = ctucan_close,
  1118. .ndo_start_xmit = ctucan_start_xmit,
  1119. .ndo_change_mtu = can_change_mtu,
  1120. };
  1121. static const struct ethtool_ops ctucan_ethtool_ops = {
  1122. .get_ts_info = ethtool_op_get_ts_info,
  1123. };
  1124. int ctucan_suspend(struct device *dev)
  1125. {
  1126. struct net_device *ndev = dev_get_drvdata(dev);
  1127. struct ctucan_priv *priv = netdev_priv(ndev);
  1128. if (netif_running(ndev)) {
  1129. netif_stop_queue(ndev);
  1130. netif_device_detach(ndev);
  1131. }
  1132. priv->can.state = CAN_STATE_SLEEPING;
  1133. return 0;
  1134. }
  1135. EXPORT_SYMBOL(ctucan_suspend);
  1136. int ctucan_resume(struct device *dev)
  1137. {
  1138. struct net_device *ndev = dev_get_drvdata(dev);
  1139. struct ctucan_priv *priv = netdev_priv(ndev);
  1140. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  1141. if (netif_running(ndev)) {
  1142. netif_device_attach(ndev);
  1143. netif_start_queue(ndev);
  1144. }
  1145. return 0;
  1146. }
  1147. EXPORT_SYMBOL(ctucan_resume);
  1148. int ctucan_probe_common(struct device *dev, void __iomem *addr, int irq, unsigned int ntxbufs,
  1149. unsigned long can_clk_rate, int pm_enable_call,
  1150. void (*set_drvdata_fnc)(struct device *dev, struct net_device *ndev))
  1151. {
  1152. struct ctucan_priv *priv;
  1153. struct net_device *ndev;
  1154. int ret;
  1155. /* Create a CAN device instance */
  1156. ndev = alloc_candev(sizeof(struct ctucan_priv), ntxbufs);
  1157. if (!ndev)
  1158. return -ENOMEM;
  1159. priv = netdev_priv(ndev);
  1160. spin_lock_init(&priv->tx_lock);
  1161. INIT_LIST_HEAD(&priv->peers_on_pdev);
  1162. priv->ntxbufs = ntxbufs;
  1163. priv->dev = dev;
  1164. priv->can.bittiming_const = &ctu_can_fd_bit_timing_max;
  1165. priv->can.data_bittiming_const = &ctu_can_fd_bit_timing_data_max;
  1166. priv->can.do_set_mode = ctucan_do_set_mode;
  1167. /* Needed for timing adjustment to be performed as soon as possible */
  1168. priv->can.do_set_bittiming = ctucan_set_bittiming;
  1169. priv->can.do_set_data_bittiming = ctucan_set_data_bittiming;
  1170. priv->can.do_get_berr_counter = ctucan_get_berr_counter;
  1171. priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK
  1172. | CAN_CTRLMODE_LISTENONLY
  1173. | CAN_CTRLMODE_FD
  1174. | CAN_CTRLMODE_PRESUME_ACK
  1175. | CAN_CTRLMODE_BERR_REPORTING
  1176. | CAN_CTRLMODE_FD_NON_ISO
  1177. | CAN_CTRLMODE_ONE_SHOT;
  1178. priv->mem_base = addr;
  1179. /* Get IRQ for the device */
  1180. ndev->irq = irq;
  1181. ndev->flags |= IFF_ECHO; /* We support local echo */
  1182. if (set_drvdata_fnc)
  1183. set_drvdata_fnc(dev, ndev);
  1184. SET_NETDEV_DEV(ndev, dev);
  1185. ndev->netdev_ops = &ctucan_netdev_ops;
  1186. ndev->ethtool_ops = &ctucan_ethtool_ops;
  1187. /* Getting the can_clk info */
  1188. if (!can_clk_rate) {
  1189. priv->can_clk = devm_clk_get(dev, NULL);
  1190. if (IS_ERR(priv->can_clk)) {
  1191. dev_err(dev, "Device clock not found.\n");
  1192. ret = PTR_ERR(priv->can_clk);
  1193. goto err_free;
  1194. }
  1195. can_clk_rate = clk_get_rate(priv->can_clk);
  1196. }
  1197. priv->write_reg = ctucan_write32_le;
  1198. priv->read_reg = ctucan_read32_le;
  1199. if (pm_enable_call)
  1200. pm_runtime_enable(dev);
  1201. ret = pm_runtime_get_sync(dev);
  1202. if (ret < 0) {
  1203. netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
  1204. __func__, ret);
  1205. pm_runtime_put_noidle(priv->dev);
  1206. goto err_pmdisable;
  1207. }
  1208. /* Check for big-endianity and set according IO-accessors */
  1209. if ((ctucan_read32(priv, CTUCANFD_DEVICE_ID) & 0xFFFF) != CTUCANFD_ID) {
  1210. priv->write_reg = ctucan_write32_be;
  1211. priv->read_reg = ctucan_read32_be;
  1212. if ((ctucan_read32(priv, CTUCANFD_DEVICE_ID) & 0xFFFF) != CTUCANFD_ID) {
  1213. netdev_err(ndev, "CTU_CAN_FD signature not found\n");
  1214. ret = -ENODEV;
  1215. goto err_deviceoff;
  1216. }
  1217. }
  1218. ret = ctucan_reset(ndev);
  1219. if (ret < 0)
  1220. goto err_deviceoff;
  1221. priv->can.clock.freq = can_clk_rate;
  1222. netif_napi_add(ndev, &priv->napi, ctucan_rx_poll);
  1223. ret = register_candev(ndev);
  1224. if (ret) {
  1225. dev_err(dev, "fail to register failed (err=%d)\n", ret);
  1226. goto err_deviceoff;
  1227. }
  1228. pm_runtime_put(dev);
  1229. netdev_dbg(ndev, "mem_base=0x%p irq=%d clock=%d, no. of txt buffers:%d\n",
  1230. priv->mem_base, ndev->irq, priv->can.clock.freq, priv->ntxbufs);
  1231. return 0;
  1232. err_deviceoff:
  1233. pm_runtime_put(priv->dev);
  1234. err_pmdisable:
  1235. if (pm_enable_call)
  1236. pm_runtime_disable(dev);
  1237. err_free:
  1238. list_del_init(&priv->peers_on_pdev);
  1239. free_candev(ndev);
  1240. return ret;
  1241. }
  1242. EXPORT_SYMBOL(ctucan_probe_common);
  1243. MODULE_LICENSE("GPL");
  1244. MODULE_AUTHOR("Martin Jerabek <martin.jerabek01@gmail.com>");
  1245. MODULE_AUTHOR("Pavel Pisa <pisa@cmp.felk.cvut.cz>");
  1246. MODULE_AUTHOR("Ondrej Ille <ondrej.ille@gmail.com>");
  1247. MODULE_DESCRIPTION("CTU CAN FD interface");