ctucanfd_base.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  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 <[email protected]> FEE CTU
  7. * Copyright (C) 2018-2021 Ondrej Ille <[email protected]> self-funded
  8. * Copyright (C) 2018-2019 Martin Jerabek <[email protected]> FEE CTU
  9. * Copyright (C) 2018-2022 Pavel Pisa <[email protected]> FEE CTU/self-funded
  10. *
  11. * Project advisors:
  12. * Jiri Novak <[email protected]>
  13. * Pavel Pisa <[email protected]>
  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. cf->can_id |= CAN_ERR_CNT;
  743. cf->data[1] = CAN_ERR_CRTL_ACTIVE;
  744. cf->data[6] = bec.txerr;
  745. cf->data[7] = bec.rxerr;
  746. break;
  747. default:
  748. netdev_warn(ndev, "unhandled error state (%d:%s)!\n",
  749. state, ctucan_state_to_str(state));
  750. break;
  751. }
  752. }
  753. /* Check for Arbitration Lost interrupt */
  754. if (FIELD_GET(REG_INT_STAT_ALI, isr)) {
  755. if (dologerr)
  756. netdev_info(ndev, "arbitration lost\n");
  757. priv->can.can_stats.arbitration_lost++;
  758. if (skb) {
  759. cf->can_id |= CAN_ERR_LOSTARB;
  760. cf->data[0] = CAN_ERR_LOSTARB_UNSPEC;
  761. }
  762. }
  763. /* Check for Bus Error interrupt */
  764. if (FIELD_GET(REG_INT_STAT_BEI, isr)) {
  765. netdev_info(ndev, "bus error\n");
  766. priv->can.can_stats.bus_error++;
  767. stats->rx_errors++;
  768. if (skb) {
  769. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  770. cf->data[2] = CAN_ERR_PROT_UNSPEC;
  771. cf->data[3] = CAN_ERR_PROT_LOC_UNSPEC;
  772. }
  773. }
  774. if (skb) {
  775. stats->rx_packets++;
  776. stats->rx_bytes += cf->can_dlc;
  777. netif_rx(skb);
  778. }
  779. }
  780. /**
  781. * ctucan_rx_poll() - Poll routine for rx packets (NAPI)
  782. * @napi: NAPI structure pointer
  783. * @quota: Max number of rx packets to be processed.
  784. *
  785. * This is the poll routine for rx part. It will process the packets maximux quota value.
  786. *
  787. * Return: Number of packets received
  788. */
  789. static int ctucan_rx_poll(struct napi_struct *napi, int quota)
  790. {
  791. struct net_device *ndev = napi->dev;
  792. struct ctucan_priv *priv = netdev_priv(ndev);
  793. int work_done = 0;
  794. u32 status;
  795. u32 framecnt;
  796. int res = 1;
  797. framecnt = FIELD_GET(REG_RX_STATUS_RXFRC, ctucan_read32(priv, CTUCANFD_RX_STATUS));
  798. while (framecnt && work_done < quota && res > 0) {
  799. res = ctucan_rx(ndev);
  800. work_done++;
  801. framecnt = FIELD_GET(REG_RX_STATUS_RXFRC, ctucan_read32(priv, CTUCANFD_RX_STATUS));
  802. }
  803. /* Check for RX FIFO Overflow */
  804. status = ctucan_read32(priv, CTUCANFD_STATUS);
  805. if (FIELD_GET(REG_STATUS_DOR, status)) {
  806. struct net_device_stats *stats = &ndev->stats;
  807. struct can_frame *cf;
  808. struct sk_buff *skb;
  809. netdev_info(ndev, "rx_poll: rx fifo overflow\n");
  810. stats->rx_over_errors++;
  811. stats->rx_errors++;
  812. skb = alloc_can_err_skb(ndev, &cf);
  813. if (skb) {
  814. cf->can_id |= CAN_ERR_CRTL;
  815. cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
  816. stats->rx_packets++;
  817. stats->rx_bytes += cf->can_dlc;
  818. netif_rx(skb);
  819. }
  820. /* Clear Data Overrun */
  821. ctucan_write32(priv, CTUCANFD_COMMAND, REG_COMMAND_CDO);
  822. }
  823. if (!framecnt && res != 0) {
  824. if (napi_complete_done(napi, work_done)) {
  825. /* Clear and enable RBNEI. It is level-triggered, so
  826. * there is no race condition.
  827. */
  828. ctucan_write32(priv, CTUCANFD_INT_STAT, REG_INT_STAT_RBNEI);
  829. ctucan_write32(priv, CTUCANFD_INT_MASK_CLR, REG_INT_STAT_RBNEI);
  830. }
  831. }
  832. return work_done;
  833. }
  834. /**
  835. * ctucan_rotate_txb_prio() - Rotates priorities of TXT Buffers
  836. * @ndev: net_device pointer
  837. */
  838. static void ctucan_rotate_txb_prio(struct net_device *ndev)
  839. {
  840. struct ctucan_priv *priv = netdev_priv(ndev);
  841. u32 prio = priv->txb_prio;
  842. prio = (prio << 4) | ((prio >> ((priv->ntxbufs - 1) * 4)) & 0xF);
  843. ctucan_netdev_dbg(ndev, "%s: from 0x%08x to 0x%08x\n", __func__, priv->txb_prio, prio);
  844. priv->txb_prio = prio;
  845. ctucan_write32(priv, CTUCANFD_TX_PRIORITY, prio);
  846. }
  847. /**
  848. * ctucan_tx_interrupt() - Tx done Isr
  849. * @ndev: net_device pointer
  850. */
  851. static void ctucan_tx_interrupt(struct net_device *ndev)
  852. {
  853. struct ctucan_priv *priv = netdev_priv(ndev);
  854. struct net_device_stats *stats = &ndev->stats;
  855. bool first = true;
  856. bool some_buffers_processed;
  857. unsigned long flags;
  858. enum ctucan_txtb_status txtb_status;
  859. u32 txtb_id;
  860. /* read tx_status
  861. * if txb[n].finished (bit 2)
  862. * if ok -> echo
  863. * if error / aborted -> ?? (find how to handle oneshot mode)
  864. * txb_tail++
  865. */
  866. do {
  867. spin_lock_irqsave(&priv->tx_lock, flags);
  868. some_buffers_processed = false;
  869. while ((int)(priv->txb_head - priv->txb_tail) > 0) {
  870. txtb_id = priv->txb_tail % priv->ntxbufs;
  871. txtb_status = ctucan_get_tx_status(priv, txtb_id);
  872. ctucan_netdev_dbg(ndev, "TXI: TXB#%u: status 0x%x\n", txtb_id, txtb_status);
  873. switch (txtb_status) {
  874. case TXT_TOK:
  875. ctucan_netdev_dbg(ndev, "TXT_OK\n");
  876. stats->tx_bytes += can_get_echo_skb(ndev, txtb_id, NULL);
  877. stats->tx_packets++;
  878. break;
  879. case TXT_ERR:
  880. /* This indicated that retransmit limit has been reached. Obviously
  881. * we should not echo the frame, but also not indicate any kind of
  882. * error. If desired, it was already reported (possible multiple
  883. * times) on each arbitration lost.
  884. */
  885. netdev_warn(ndev, "TXB in Error state\n");
  886. can_free_echo_skb(ndev, txtb_id, NULL);
  887. stats->tx_dropped++;
  888. break;
  889. case TXT_ABT:
  890. /* Same as for TXT_ERR, only with different cause. We *could*
  891. * re-queue the frame, but multiqueue/abort is not supported yet
  892. * anyway.
  893. */
  894. netdev_warn(ndev, "TXB in Aborted state\n");
  895. can_free_echo_skb(ndev, txtb_id, NULL);
  896. stats->tx_dropped++;
  897. break;
  898. default:
  899. /* Bug only if the first buffer is not finished, otherwise it is
  900. * pretty much expected.
  901. */
  902. if (first) {
  903. netdev_err(ndev,
  904. "BUG: TXB#%u not in a finished state (0x%x)!\n",
  905. txtb_id, txtb_status);
  906. spin_unlock_irqrestore(&priv->tx_lock, flags);
  907. /* do not clear nor wake */
  908. return;
  909. }
  910. goto clear;
  911. }
  912. priv->txb_tail++;
  913. first = false;
  914. some_buffers_processed = true;
  915. /* Adjust priorities *before* marking the buffer as empty. */
  916. ctucan_rotate_txb_prio(ndev);
  917. ctucan_give_txtb_cmd(priv, TXT_CMD_SET_EMPTY, txtb_id);
  918. }
  919. clear:
  920. spin_unlock_irqrestore(&priv->tx_lock, flags);
  921. /* If no buffers were processed this time, we cannot clear - that would introduce
  922. * a race condition.
  923. */
  924. if (some_buffers_processed) {
  925. /* Clear the interrupt again. We do not want to receive again interrupt for
  926. * the buffer already handled. If it is the last finished one then it would
  927. * cause log of spurious interrupt.
  928. */
  929. ctucan_write32(priv, CTUCANFD_INT_STAT, REG_INT_STAT_TXBHCI);
  930. }
  931. } while (some_buffers_processed);
  932. spin_lock_irqsave(&priv->tx_lock, flags);
  933. /* Check if at least one TX buffer is free */
  934. if (CTU_CAN_FD_TXTNF(priv))
  935. netif_wake_queue(ndev);
  936. spin_unlock_irqrestore(&priv->tx_lock, flags);
  937. }
  938. /**
  939. * ctucan_interrupt() - CAN Isr
  940. * @irq: irq number
  941. * @dev_id: device id pointer
  942. *
  943. * This is the CTU CAN FD ISR. It checks for the type of interrupt
  944. * and invokes the corresponding ISR.
  945. *
  946. * Return:
  947. * IRQ_NONE - If CAN device is in sleep mode, IRQ_HANDLED otherwise
  948. */
  949. static irqreturn_t ctucan_interrupt(int irq, void *dev_id)
  950. {
  951. struct net_device *ndev = (struct net_device *)dev_id;
  952. struct ctucan_priv *priv = netdev_priv(ndev);
  953. u32 isr, icr;
  954. u32 imask;
  955. int irq_loops;
  956. for (irq_loops = 0; irq_loops < 10000; irq_loops++) {
  957. /* Get the interrupt status */
  958. isr = ctucan_read32(priv, CTUCANFD_INT_STAT);
  959. if (!isr)
  960. return irq_loops ? IRQ_HANDLED : IRQ_NONE;
  961. /* Receive Buffer Not Empty Interrupt */
  962. if (FIELD_GET(REG_INT_STAT_RBNEI, isr)) {
  963. ctucan_netdev_dbg(ndev, "RXBNEI\n");
  964. /* Mask RXBNEI the first, then clear interrupt and schedule NAPI. Even if
  965. * another IRQ fires, RBNEI will always be 0 (masked).
  966. */
  967. icr = REG_INT_STAT_RBNEI;
  968. ctucan_write32(priv, CTUCANFD_INT_MASK_SET, icr);
  969. ctucan_write32(priv, CTUCANFD_INT_STAT, icr);
  970. napi_schedule(&priv->napi);
  971. }
  972. /* TXT Buffer HW Command Interrupt */
  973. if (FIELD_GET(REG_INT_STAT_TXBHCI, isr)) {
  974. ctucan_netdev_dbg(ndev, "TXBHCI\n");
  975. /* Cleared inside */
  976. ctucan_tx_interrupt(ndev);
  977. }
  978. /* Error interrupts */
  979. if (FIELD_GET(REG_INT_STAT_EWLI, isr) ||
  980. FIELD_GET(REG_INT_STAT_FCSI, isr) ||
  981. FIELD_GET(REG_INT_STAT_ALI, isr)) {
  982. icr = isr & (REG_INT_STAT_EWLI | REG_INT_STAT_FCSI | REG_INT_STAT_ALI);
  983. ctucan_netdev_dbg(ndev, "some ERR interrupt: clearing 0x%08x\n", icr);
  984. ctucan_write32(priv, CTUCANFD_INT_STAT, icr);
  985. ctucan_err_interrupt(ndev, isr);
  986. }
  987. /* Ignore RI, TI, LFI, RFI, BSI */
  988. }
  989. netdev_err(ndev, "%s: stuck interrupt (isr=0x%08x), stopping\n", __func__, isr);
  990. if (FIELD_GET(REG_INT_STAT_TXBHCI, isr)) {
  991. int i;
  992. netdev_err(ndev, "txb_head=0x%08x txb_tail=0x%08x\n",
  993. priv->txb_head, priv->txb_tail);
  994. for (i = 0; i < priv->ntxbufs; i++) {
  995. u32 status = ctucan_get_tx_status(priv, i);
  996. netdev_err(ndev, "txb[%d] txb status=0x%08x\n", i, status);
  997. }
  998. }
  999. imask = 0xffffffff;
  1000. ctucan_write32(priv, CTUCANFD_INT_ENA_CLR, imask);
  1001. ctucan_write32(priv, CTUCANFD_INT_MASK_SET, imask);
  1002. return IRQ_HANDLED;
  1003. }
  1004. /**
  1005. * ctucan_chip_stop() - Driver stop routine
  1006. * @ndev: Pointer to net_device structure
  1007. *
  1008. * This is the drivers stop routine. It will disable the
  1009. * interrupts and disable the controller.
  1010. */
  1011. static void ctucan_chip_stop(struct net_device *ndev)
  1012. {
  1013. struct ctucan_priv *priv = netdev_priv(ndev);
  1014. u32 mask = 0xffffffff;
  1015. u32 mode;
  1016. /* Disable interrupts and disable CAN */
  1017. ctucan_write32(priv, CTUCANFD_INT_ENA_CLR, mask);
  1018. ctucan_write32(priv, CTUCANFD_INT_MASK_SET, mask);
  1019. mode = ctucan_read32(priv, CTUCANFD_MODE);
  1020. mode &= ~REG_MODE_ENA;
  1021. ctucan_write32(priv, CTUCANFD_MODE, mode);
  1022. priv->can.state = CAN_STATE_STOPPED;
  1023. }
  1024. /**
  1025. * ctucan_open() - Driver open routine
  1026. * @ndev: Pointer to net_device structure
  1027. *
  1028. * This is the driver open routine.
  1029. * Return: 0 on success and failure value on error
  1030. */
  1031. static int ctucan_open(struct net_device *ndev)
  1032. {
  1033. struct ctucan_priv *priv = netdev_priv(ndev);
  1034. int ret;
  1035. ret = pm_runtime_get_sync(priv->dev);
  1036. if (ret < 0) {
  1037. netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
  1038. __func__, ret);
  1039. pm_runtime_put_noidle(priv->dev);
  1040. return ret;
  1041. }
  1042. ret = ctucan_reset(ndev);
  1043. if (ret < 0)
  1044. goto err_reset;
  1045. /* Common open */
  1046. ret = open_candev(ndev);
  1047. if (ret) {
  1048. netdev_warn(ndev, "open_candev failed!\n");
  1049. goto err_open;
  1050. }
  1051. ret = request_irq(ndev->irq, ctucan_interrupt, priv->irq_flags, ndev->name, ndev);
  1052. if (ret < 0) {
  1053. netdev_err(ndev, "irq allocation for CAN failed\n");
  1054. goto err_irq;
  1055. }
  1056. ret = ctucan_chip_start(ndev);
  1057. if (ret < 0) {
  1058. netdev_err(ndev, "ctucan_chip_start failed!\n");
  1059. goto err_chip_start;
  1060. }
  1061. netdev_info(ndev, "ctu_can_fd device registered\n");
  1062. napi_enable(&priv->napi);
  1063. netif_start_queue(ndev);
  1064. return 0;
  1065. err_chip_start:
  1066. free_irq(ndev->irq, ndev);
  1067. err_irq:
  1068. close_candev(ndev);
  1069. err_open:
  1070. err_reset:
  1071. pm_runtime_put(priv->dev);
  1072. return ret;
  1073. }
  1074. /**
  1075. * ctucan_close() - Driver close routine
  1076. * @ndev: Pointer to net_device structure
  1077. *
  1078. * Return: 0 always
  1079. */
  1080. static int ctucan_close(struct net_device *ndev)
  1081. {
  1082. struct ctucan_priv *priv = netdev_priv(ndev);
  1083. netif_stop_queue(ndev);
  1084. napi_disable(&priv->napi);
  1085. ctucan_chip_stop(ndev);
  1086. free_irq(ndev->irq, ndev);
  1087. close_candev(ndev);
  1088. pm_runtime_put(priv->dev);
  1089. return 0;
  1090. }
  1091. /**
  1092. * ctucan_get_berr_counter() - error counter routine
  1093. * @ndev: Pointer to net_device structure
  1094. * @bec: Pointer to can_berr_counter structure
  1095. *
  1096. * This is the driver error counter routine.
  1097. * Return: 0 on success and failure value on error
  1098. */
  1099. static int ctucan_get_berr_counter(const struct net_device *ndev, struct can_berr_counter *bec)
  1100. {
  1101. struct ctucan_priv *priv = netdev_priv(ndev);
  1102. int ret;
  1103. ret = pm_runtime_get_sync(priv->dev);
  1104. if (ret < 0) {
  1105. netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n", __func__, ret);
  1106. pm_runtime_put_noidle(priv->dev);
  1107. return ret;
  1108. }
  1109. ctucan_get_rec_tec(priv, bec);
  1110. pm_runtime_put(priv->dev);
  1111. return 0;
  1112. }
  1113. static const struct net_device_ops ctucan_netdev_ops = {
  1114. .ndo_open = ctucan_open,
  1115. .ndo_stop = ctucan_close,
  1116. .ndo_start_xmit = ctucan_start_xmit,
  1117. .ndo_change_mtu = can_change_mtu,
  1118. };
  1119. static const struct ethtool_ops ctucan_ethtool_ops = {
  1120. .get_ts_info = ethtool_op_get_ts_info,
  1121. };
  1122. int ctucan_suspend(struct device *dev)
  1123. {
  1124. struct net_device *ndev = dev_get_drvdata(dev);
  1125. struct ctucan_priv *priv = netdev_priv(ndev);
  1126. if (netif_running(ndev)) {
  1127. netif_stop_queue(ndev);
  1128. netif_device_detach(ndev);
  1129. }
  1130. priv->can.state = CAN_STATE_SLEEPING;
  1131. return 0;
  1132. }
  1133. EXPORT_SYMBOL(ctucan_suspend);
  1134. int ctucan_resume(struct device *dev)
  1135. {
  1136. struct net_device *ndev = dev_get_drvdata(dev);
  1137. struct ctucan_priv *priv = netdev_priv(ndev);
  1138. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  1139. if (netif_running(ndev)) {
  1140. netif_device_attach(ndev);
  1141. netif_start_queue(ndev);
  1142. }
  1143. return 0;
  1144. }
  1145. EXPORT_SYMBOL(ctucan_resume);
  1146. int ctucan_probe_common(struct device *dev, void __iomem *addr, int irq, unsigned int ntxbufs,
  1147. unsigned long can_clk_rate, int pm_enable_call,
  1148. void (*set_drvdata_fnc)(struct device *dev, struct net_device *ndev))
  1149. {
  1150. struct ctucan_priv *priv;
  1151. struct net_device *ndev;
  1152. int ret;
  1153. /* Create a CAN device instance */
  1154. ndev = alloc_candev(sizeof(struct ctucan_priv), ntxbufs);
  1155. if (!ndev)
  1156. return -ENOMEM;
  1157. priv = netdev_priv(ndev);
  1158. spin_lock_init(&priv->tx_lock);
  1159. INIT_LIST_HEAD(&priv->peers_on_pdev);
  1160. priv->ntxbufs = ntxbufs;
  1161. priv->dev = dev;
  1162. priv->can.bittiming_const = &ctu_can_fd_bit_timing_max;
  1163. priv->can.data_bittiming_const = &ctu_can_fd_bit_timing_data_max;
  1164. priv->can.do_set_mode = ctucan_do_set_mode;
  1165. /* Needed for timing adjustment to be performed as soon as possible */
  1166. priv->can.do_set_bittiming = ctucan_set_bittiming;
  1167. priv->can.do_set_data_bittiming = ctucan_set_data_bittiming;
  1168. priv->can.do_get_berr_counter = ctucan_get_berr_counter;
  1169. priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK
  1170. | CAN_CTRLMODE_LISTENONLY
  1171. | CAN_CTRLMODE_FD
  1172. | CAN_CTRLMODE_PRESUME_ACK
  1173. | CAN_CTRLMODE_BERR_REPORTING
  1174. | CAN_CTRLMODE_FD_NON_ISO
  1175. | CAN_CTRLMODE_ONE_SHOT;
  1176. priv->mem_base = addr;
  1177. /* Get IRQ for the device */
  1178. ndev->irq = irq;
  1179. ndev->flags |= IFF_ECHO; /* We support local echo */
  1180. if (set_drvdata_fnc)
  1181. set_drvdata_fnc(dev, ndev);
  1182. SET_NETDEV_DEV(ndev, dev);
  1183. ndev->netdev_ops = &ctucan_netdev_ops;
  1184. ndev->ethtool_ops = &ctucan_ethtool_ops;
  1185. /* Getting the can_clk info */
  1186. if (!can_clk_rate) {
  1187. priv->can_clk = devm_clk_get(dev, NULL);
  1188. if (IS_ERR(priv->can_clk)) {
  1189. dev_err(dev, "Device clock not found.\n");
  1190. ret = PTR_ERR(priv->can_clk);
  1191. goto err_free;
  1192. }
  1193. can_clk_rate = clk_get_rate(priv->can_clk);
  1194. }
  1195. priv->write_reg = ctucan_write32_le;
  1196. priv->read_reg = ctucan_read32_le;
  1197. if (pm_enable_call)
  1198. pm_runtime_enable(dev);
  1199. ret = pm_runtime_get_sync(dev);
  1200. if (ret < 0) {
  1201. netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
  1202. __func__, ret);
  1203. pm_runtime_put_noidle(priv->dev);
  1204. goto err_pmdisable;
  1205. }
  1206. /* Check for big-endianity and set according IO-accessors */
  1207. if ((ctucan_read32(priv, CTUCANFD_DEVICE_ID) & 0xFFFF) != CTUCANFD_ID) {
  1208. priv->write_reg = ctucan_write32_be;
  1209. priv->read_reg = ctucan_read32_be;
  1210. if ((ctucan_read32(priv, CTUCANFD_DEVICE_ID) & 0xFFFF) != CTUCANFD_ID) {
  1211. netdev_err(ndev, "CTU_CAN_FD signature not found\n");
  1212. ret = -ENODEV;
  1213. goto err_deviceoff;
  1214. }
  1215. }
  1216. ret = ctucan_reset(ndev);
  1217. if (ret < 0)
  1218. goto err_deviceoff;
  1219. priv->can.clock.freq = can_clk_rate;
  1220. netif_napi_add(ndev, &priv->napi, ctucan_rx_poll);
  1221. ret = register_candev(ndev);
  1222. if (ret) {
  1223. dev_err(dev, "fail to register failed (err=%d)\n", ret);
  1224. goto err_deviceoff;
  1225. }
  1226. pm_runtime_put(dev);
  1227. netdev_dbg(ndev, "mem_base=0x%p irq=%d clock=%d, no. of txt buffers:%d\n",
  1228. priv->mem_base, ndev->irq, priv->can.clock.freq, priv->ntxbufs);
  1229. return 0;
  1230. err_deviceoff:
  1231. pm_runtime_put(priv->dev);
  1232. err_pmdisable:
  1233. if (pm_enable_call)
  1234. pm_runtime_disable(dev);
  1235. err_free:
  1236. list_del_init(&priv->peers_on_pdev);
  1237. free_candev(ndev);
  1238. return ret;
  1239. }
  1240. EXPORT_SYMBOL(ctucan_probe_common);
  1241. MODULE_LICENSE("GPL");
  1242. MODULE_AUTHOR("Martin Jerabek <[email protected]>");
  1243. MODULE_AUTHOR("Pavel Pisa <[email protected]>");
  1244. MODULE_AUTHOR("Ondrej Ille <[email protected]>");
  1245. MODULE_DESCRIPTION("CTU CAN FD interface");