esdacc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (C) 2015 - 2016 Thomas Körper, esd electronic system design gmbh
  3. * Copyright (C) 2017 - 2023 Stefan Mätje, esd electronics gmbh
  4. */
  5. #include "esdacc.h"
  6. #include <linux/bitfield.h>
  7. #include <linux/delay.h>
  8. #include <linux/io.h>
  9. #include <linux/ktime.h>
  10. /* esdACC ID register layout */
  11. #define ACC_ID_ID_MASK GENMASK(28, 0)
  12. #define ACC_ID_EFF_FLAG BIT(29)
  13. /* esdACC DLC register layout */
  14. #define ACC_DLC_DLC_MASK GENMASK(3, 0)
  15. #define ACC_DLC_RTR_FLAG BIT(4)
  16. #define ACC_DLC_SSTX_FLAG BIT(24) /* Single Shot TX */
  17. /* esdACC DLC in struct acc_bmmsg_rxtxdone::acc_dlc.len only! */
  18. #define ACC_DLC_TXD_FLAG BIT(5)
  19. /* ecc value of esdACC equals SJA1000's ECC register */
  20. #define ACC_ECC_SEG 0x1f
  21. #define ACC_ECC_DIR 0x20
  22. #define ACC_ECC_BIT 0x00
  23. #define ACC_ECC_FORM 0x40
  24. #define ACC_ECC_STUFF 0x80
  25. #define ACC_ECC_MASK 0xc0
  26. /* esdACC Status Register bits. Unused bits not documented. */
  27. #define ACC_REG_STATUS_MASK_STATUS_ES BIT(17)
  28. #define ACC_REG_STATUS_MASK_STATUS_EP BIT(18)
  29. #define ACC_REG_STATUS_MASK_STATUS_BS BIT(19)
  30. /* esdACC Overview Module BM_IRQ_Mask register related defines */
  31. /* Two bit wide command masks to mask or unmask a single core IRQ */
  32. #define ACC_BM_IRQ_UNMASK BIT(0)
  33. #define ACC_BM_IRQ_MASK (ACC_BM_IRQ_UNMASK << 1)
  34. /* Command to unmask all IRQ sources. Created by shifting
  35. * and oring the two bit wide ACC_BM_IRQ_UNMASK 16 times.
  36. */
  37. #define ACC_BM_IRQ_UNMASK_ALL 0x55555555U
  38. static void acc_resetmode_enter(struct acc_core *core)
  39. {
  40. acc_set_bits(core, ACC_CORE_OF_CTRL,
  41. ACC_REG_CTRL_MASK_RESETMODE);
  42. /* Read back reset mode bit to flush PCI write posting */
  43. acc_resetmode_entered(core);
  44. }
  45. static void acc_resetmode_leave(struct acc_core *core)
  46. {
  47. acc_clear_bits(core, ACC_CORE_OF_CTRL,
  48. ACC_REG_CTRL_MASK_RESETMODE);
  49. /* Read back reset mode bit to flush PCI write posting */
  50. acc_resetmode_entered(core);
  51. }
  52. static void acc_txq_put(struct acc_core *core, u32 acc_id, u32 acc_dlc,
  53. const void *data)
  54. {
  55. acc_write32_noswap(core, ACC_CORE_OF_TXFIFO_DATA_1,
  56. *((const u32 *)(data + 4)));
  57. acc_write32_noswap(core, ACC_CORE_OF_TXFIFO_DATA_0,
  58. *((const u32 *)data));
  59. acc_write32(core, ACC_CORE_OF_TXFIFO_DLC, acc_dlc);
  60. /* CAN id must be written at last. This write starts TX. */
  61. acc_write32(core, ACC_CORE_OF_TXFIFO_ID, acc_id);
  62. }
  63. static u8 acc_tx_fifo_next(struct acc_core *core, u8 tx_fifo_idx)
  64. {
  65. ++tx_fifo_idx;
  66. if (tx_fifo_idx >= core->tx_fifo_size)
  67. tx_fifo_idx = 0U;
  68. return tx_fifo_idx;
  69. }
  70. /* Convert timestamp from esdACC time stamp ticks to ns
  71. *
  72. * The conversion factor ts2ns from time stamp counts to ns is basically
  73. * ts2ns = NSEC_PER_SEC / timestamp_frequency
  74. *
  75. * We handle here only a fixed timestamp frequency of 80MHz. The
  76. * resulting ts2ns factor would be 12.5.
  77. *
  78. * At the end we multiply by 12 and add the half of the HW timestamp
  79. * to get a multiplication by 12.5. This way any overflow is
  80. * avoided until ktime_t itself overflows.
  81. */
  82. #define ACC_TS_FACTOR (NSEC_PER_SEC / ACC_TS_FREQ_80MHZ)
  83. #define ACC_TS_80MHZ_SHIFT 1
  84. static ktime_t acc_ts2ktime(struct acc_ov *ov, u64 ts)
  85. {
  86. u64 ns;
  87. ns = (ts * ACC_TS_FACTOR) + (ts >> ACC_TS_80MHZ_SHIFT);
  88. return ns_to_ktime(ns);
  89. }
  90. #undef ACC_TS_FACTOR
  91. #undef ACC_TS_80MHZ_SHIFT
  92. void acc_init_ov(struct acc_ov *ov, struct device *dev)
  93. {
  94. u32 temp;
  95. temp = acc_ov_read32(ov, ACC_OV_OF_VERSION);
  96. ov->version = temp;
  97. ov->features = (temp >> 16);
  98. temp = acc_ov_read32(ov, ACC_OV_OF_INFO);
  99. ov->total_cores = temp;
  100. ov->active_cores = (temp >> 8);
  101. ov->core_frequency = acc_ov_read32(ov, ACC_OV_OF_CANCORE_FREQ);
  102. ov->timestamp_frequency = acc_ov_read32(ov, ACC_OV_OF_TS_FREQ_LO);
  103. /* Depending on esdACC feature NEW_PSC enable the new prescaler
  104. * or adjust core_frequency according to the implicit division by 2.
  105. */
  106. if (ov->features & ACC_OV_REG_FEAT_MASK_NEW_PSC) {
  107. acc_ov_set_bits(ov, ACC_OV_OF_MODE,
  108. ACC_OV_REG_MODE_MASK_NEW_PSC_ENABLE);
  109. } else {
  110. ov->core_frequency /= 2;
  111. }
  112. dev_dbg(dev,
  113. "esdACC v%u, freq: %u/%u, feat/strap: 0x%x/0x%x, cores: %u/%u\n",
  114. ov->version, ov->core_frequency, ov->timestamp_frequency,
  115. ov->features, acc_ov_read32(ov, ACC_OV_OF_INFO) >> 16,
  116. ov->active_cores, ov->total_cores);
  117. }
  118. void acc_init_bm_ptr(struct acc_ov *ov, struct acc_core *cores, const void *mem)
  119. {
  120. unsigned int u;
  121. /* DMA buffer layout as follows where N is the number of CAN cores
  122. * implemented in the FPGA, i.e. N = ov->total_cores
  123. *
  124. * Section Layout Section size
  125. * ----------------------------------------------
  126. * FIFO Card/Overview ACC_CORE_DMABUF_SIZE
  127. * FIFO Core0 ACC_CORE_DMABUF_SIZE
  128. * ... ...
  129. * FIFO CoreN ACC_CORE_DMABUF_SIZE
  130. * irq_cnt Card/Overview sizeof(u32)
  131. * irq_cnt Core0 sizeof(u32)
  132. * ... ...
  133. * irq_cnt CoreN sizeof(u32)
  134. */
  135. ov->bmfifo.messages = mem;
  136. ov->bmfifo.irq_cnt = mem + (ov->total_cores + 1U) * ACC_CORE_DMABUF_SIZE;
  137. for (u = 0U; u < ov->active_cores; u++) {
  138. struct acc_core *core = &cores[u];
  139. core->bmfifo.messages = mem + (u + 1U) * ACC_CORE_DMABUF_SIZE;
  140. core->bmfifo.irq_cnt = ov->bmfifo.irq_cnt + (u + 1U);
  141. }
  142. }
  143. int acc_open(struct net_device *netdev)
  144. {
  145. struct acc_net_priv *priv = netdev_priv(netdev);
  146. struct acc_core *core = priv->core;
  147. u32 tx_fifo_status;
  148. u32 ctrl;
  149. int err;
  150. /* Retry to enter RESET mode if out of sync. */
  151. if (priv->can.state != CAN_STATE_STOPPED) {
  152. netdev_warn(netdev, "Entered %s() with bad can.state: %s\n",
  153. __func__, can_get_state_str(priv->can.state));
  154. acc_resetmode_enter(core);
  155. priv->can.state = CAN_STATE_STOPPED;
  156. }
  157. err = open_candev(netdev);
  158. if (err)
  159. return err;
  160. ctrl = ACC_REG_CTRL_MASK_IE_RXTX |
  161. ACC_REG_CTRL_MASK_IE_TXERROR |
  162. ACC_REG_CTRL_MASK_IE_ERRWARN |
  163. ACC_REG_CTRL_MASK_IE_OVERRUN |
  164. ACC_REG_CTRL_MASK_IE_ERRPASS;
  165. if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
  166. ctrl |= ACC_REG_CTRL_MASK_IE_BUSERR;
  167. if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  168. ctrl |= ACC_REG_CTRL_MASK_LOM;
  169. acc_set_bits(core, ACC_CORE_OF_CTRL, ctrl);
  170. acc_resetmode_leave(core);
  171. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  172. /* Resync TX FIFO indices to HW state after (re-)start. */
  173. tx_fifo_status = acc_read32(core, ACC_CORE_OF_TXFIFO_STATUS);
  174. core->tx_fifo_head = tx_fifo_status & 0xff;
  175. core->tx_fifo_tail = (tx_fifo_status >> 8) & 0xff;
  176. netif_start_queue(netdev);
  177. return 0;
  178. }
  179. int acc_close(struct net_device *netdev)
  180. {
  181. struct acc_net_priv *priv = netdev_priv(netdev);
  182. struct acc_core *core = priv->core;
  183. acc_clear_bits(core, ACC_CORE_OF_CTRL,
  184. ACC_REG_CTRL_MASK_IE_RXTX |
  185. ACC_REG_CTRL_MASK_IE_TXERROR |
  186. ACC_REG_CTRL_MASK_IE_ERRWARN |
  187. ACC_REG_CTRL_MASK_IE_OVERRUN |
  188. ACC_REG_CTRL_MASK_IE_ERRPASS |
  189. ACC_REG_CTRL_MASK_IE_BUSERR);
  190. netif_stop_queue(netdev);
  191. acc_resetmode_enter(core);
  192. priv->can.state = CAN_STATE_STOPPED;
  193. /* Mark pending TX requests to be aborted after controller restart. */
  194. acc_write32(core, ACC_CORE_OF_TX_ABORT_MASK, 0xffff);
  195. /* ACC_REG_CTRL_MASK_LOM is only accessible in RESET mode */
  196. acc_clear_bits(core, ACC_CORE_OF_CTRL,
  197. ACC_REG_CTRL_MASK_LOM);
  198. close_candev(netdev);
  199. return 0;
  200. }
  201. netdev_tx_t acc_start_xmit(struct sk_buff *skb, struct net_device *netdev)
  202. {
  203. struct acc_net_priv *priv = netdev_priv(netdev);
  204. struct acc_core *core = priv->core;
  205. struct can_frame *cf = (struct can_frame *)skb->data;
  206. u8 tx_fifo_head = core->tx_fifo_head;
  207. int fifo_usage;
  208. u32 acc_id;
  209. u32 acc_dlc;
  210. if (can_dev_dropped_skb(netdev, skb))
  211. return NETDEV_TX_OK;
  212. /* Access core->tx_fifo_tail only once because it may be changed
  213. * from the interrupt level.
  214. */
  215. fifo_usage = tx_fifo_head - core->tx_fifo_tail;
  216. if (fifo_usage < 0)
  217. fifo_usage += core->tx_fifo_size;
  218. if (fifo_usage >= core->tx_fifo_size - 1) {
  219. netdev_err(core->netdev,
  220. "BUG: TX ring full when queue awake!\n");
  221. netif_stop_queue(netdev);
  222. return NETDEV_TX_BUSY;
  223. }
  224. if (fifo_usage == core->tx_fifo_size - 2)
  225. netif_stop_queue(netdev);
  226. acc_dlc = can_get_cc_dlc(cf, priv->can.ctrlmode);
  227. if (cf->can_id & CAN_RTR_FLAG)
  228. acc_dlc |= ACC_DLC_RTR_FLAG;
  229. if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
  230. acc_dlc |= ACC_DLC_SSTX_FLAG;
  231. if (cf->can_id & CAN_EFF_FLAG) {
  232. acc_id = cf->can_id & CAN_EFF_MASK;
  233. acc_id |= ACC_ID_EFF_FLAG;
  234. } else {
  235. acc_id = cf->can_id & CAN_SFF_MASK;
  236. }
  237. can_put_echo_skb(skb, netdev, core->tx_fifo_head, 0);
  238. core->tx_fifo_head = acc_tx_fifo_next(core, tx_fifo_head);
  239. acc_txq_put(core, acc_id, acc_dlc, cf->data);
  240. return NETDEV_TX_OK;
  241. }
  242. int acc_get_berr_counter(const struct net_device *netdev,
  243. struct can_berr_counter *bec)
  244. {
  245. struct acc_net_priv *priv = netdev_priv(netdev);
  246. u32 core_status = acc_read32(priv->core, ACC_CORE_OF_STATUS);
  247. bec->txerr = (core_status >> 8) & 0xff;
  248. bec->rxerr = core_status & 0xff;
  249. return 0;
  250. }
  251. int acc_set_mode(struct net_device *netdev, enum can_mode mode)
  252. {
  253. struct acc_net_priv *priv = netdev_priv(netdev);
  254. switch (mode) {
  255. case CAN_MODE_START:
  256. /* Paranoid FIFO index check. */
  257. {
  258. const u32 tx_fifo_status =
  259. acc_read32(priv->core, ACC_CORE_OF_TXFIFO_STATUS);
  260. const u8 hw_fifo_head = tx_fifo_status;
  261. if (hw_fifo_head != priv->core->tx_fifo_head ||
  262. hw_fifo_head != priv->core->tx_fifo_tail) {
  263. netdev_warn(netdev,
  264. "TX FIFO mismatch: T %2u H %2u; TFHW %#08x\n",
  265. priv->core->tx_fifo_tail,
  266. priv->core->tx_fifo_head,
  267. tx_fifo_status);
  268. }
  269. }
  270. acc_resetmode_leave(priv->core);
  271. /* To leave the bus-off state the esdACC controller begins
  272. * here a grace period where it counts 128 "idle conditions" (each
  273. * of 11 consecutive recessive bits) on the bus as required
  274. * by the CAN spec.
  275. *
  276. * During this time the TX FIFO may still contain already
  277. * aborted "zombie" frames that are only drained from the FIFO
  278. * at the end of the grace period.
  279. *
  280. * To not to interfere with this drain process we don't
  281. * call netif_wake_queue() here. When the controller reaches
  282. * the error-active state again, it informs us about that
  283. * with an acc_bmmsg_errstatechange message. Then
  284. * netif_wake_queue() is called from
  285. * handle_core_msg_errstatechange() instead.
  286. */
  287. break;
  288. default:
  289. return -EOPNOTSUPP;
  290. }
  291. return 0;
  292. }
  293. int acc_set_bittiming(struct net_device *netdev)
  294. {
  295. struct acc_net_priv *priv = netdev_priv(netdev);
  296. const struct can_bittiming *bt = &priv->can.bittiming;
  297. u32 brp;
  298. u32 btr;
  299. if (priv->ov->features & ACC_OV_REG_FEAT_MASK_CANFD) {
  300. u32 fbtr = 0;
  301. netdev_dbg(netdev, "bit timing: brp %u, prop %u, ph1 %u ph2 %u, sjw %u\n",
  302. bt->brp, bt->prop_seg,
  303. bt->phase_seg1, bt->phase_seg2, bt->sjw);
  304. brp = FIELD_PREP(ACC_REG_BRP_FD_MASK_BRP, bt->brp - 1);
  305. btr = FIELD_PREP(ACC_REG_BTR_FD_MASK_TSEG1, bt->phase_seg1 + bt->prop_seg - 1);
  306. btr |= FIELD_PREP(ACC_REG_BTR_FD_MASK_TSEG2, bt->phase_seg2 - 1);
  307. btr |= FIELD_PREP(ACC_REG_BTR_FD_MASK_SJW, bt->sjw - 1);
  308. /* Keep order of accesses to ACC_CORE_OF_BRP and ACC_CORE_OF_BTR. */
  309. acc_write32(priv->core, ACC_CORE_OF_BRP, brp);
  310. acc_write32(priv->core, ACC_CORE_OF_BTR, btr);
  311. netdev_dbg(netdev, "esdACC: BRP %u, NBTR 0x%08x, DBTR 0x%08x",
  312. brp, btr, fbtr);
  313. } else {
  314. netdev_dbg(netdev, "bit timing: brp %u, prop %u, ph1 %u ph2 %u, sjw %u\n",
  315. bt->brp, bt->prop_seg,
  316. bt->phase_seg1, bt->phase_seg2, bt->sjw);
  317. brp = FIELD_PREP(ACC_REG_BRP_CL_MASK_BRP, bt->brp - 1);
  318. btr = FIELD_PREP(ACC_REG_BTR_CL_MASK_TSEG1, bt->phase_seg1 + bt->prop_seg - 1);
  319. btr |= FIELD_PREP(ACC_REG_BTR_CL_MASK_TSEG2, bt->phase_seg2 - 1);
  320. btr |= FIELD_PREP(ACC_REG_BTR_CL_MASK_SJW, bt->sjw - 1);
  321. /* Keep order of accesses to ACC_CORE_OF_BRP and ACC_CORE_OF_BTR. */
  322. acc_write32(priv->core, ACC_CORE_OF_BRP, brp);
  323. acc_write32(priv->core, ACC_CORE_OF_BTR, btr);
  324. netdev_dbg(netdev, "esdACC: BRP %u, BTR 0x%08x", brp, btr);
  325. }
  326. return 0;
  327. }
  328. static void handle_core_msg_rxtxdone(struct acc_core *core,
  329. const struct acc_bmmsg_rxtxdone *msg)
  330. {
  331. struct acc_net_priv *priv = netdev_priv(core->netdev);
  332. struct net_device_stats *stats = &core->netdev->stats;
  333. struct sk_buff *skb;
  334. if (msg->acc_dlc.len & ACC_DLC_TXD_FLAG) {
  335. u8 tx_fifo_tail = core->tx_fifo_tail;
  336. if (core->tx_fifo_head == tx_fifo_tail) {
  337. netdev_warn(core->netdev,
  338. "TX interrupt, but queue is empty!?\n");
  339. return;
  340. }
  341. /* Direct access echo skb to attach HW time stamp. */
  342. skb = priv->can.echo_skb[tx_fifo_tail];
  343. if (skb) {
  344. skb_hwtstamps(skb)->hwtstamp =
  345. acc_ts2ktime(priv->ov, msg->ts);
  346. }
  347. stats->tx_packets++;
  348. stats->tx_bytes += can_get_echo_skb(core->netdev, tx_fifo_tail,
  349. NULL);
  350. core->tx_fifo_tail = acc_tx_fifo_next(core, tx_fifo_tail);
  351. netif_wake_queue(core->netdev);
  352. } else {
  353. struct can_frame *cf;
  354. skb = alloc_can_skb(core->netdev, &cf);
  355. if (!skb) {
  356. stats->rx_dropped++;
  357. return;
  358. }
  359. cf->can_id = msg->id & ACC_ID_ID_MASK;
  360. if (msg->id & ACC_ID_EFF_FLAG)
  361. cf->can_id |= CAN_EFF_FLAG;
  362. can_frame_set_cc_len(cf, msg->acc_dlc.len & ACC_DLC_DLC_MASK,
  363. priv->can.ctrlmode);
  364. if (msg->acc_dlc.len & ACC_DLC_RTR_FLAG) {
  365. cf->can_id |= CAN_RTR_FLAG;
  366. } else {
  367. memcpy(cf->data, msg->data, cf->len);
  368. stats->rx_bytes += cf->len;
  369. }
  370. stats->rx_packets++;
  371. skb_hwtstamps(skb)->hwtstamp = acc_ts2ktime(priv->ov, msg->ts);
  372. netif_rx(skb);
  373. }
  374. }
  375. static void handle_core_msg_txabort(struct acc_core *core,
  376. const struct acc_bmmsg_txabort *msg)
  377. {
  378. struct net_device_stats *stats = &core->netdev->stats;
  379. u8 tx_fifo_tail = core->tx_fifo_tail;
  380. u32 abort_mask = msg->abort_mask; /* u32 extend to avoid warnings later */
  381. /* The abort_mask shows which frames were aborted in esdACC's FIFO. */
  382. while (tx_fifo_tail != core->tx_fifo_head && (abort_mask)) {
  383. const u32 tail_mask = (1U << tx_fifo_tail);
  384. if (!(abort_mask & tail_mask))
  385. break;
  386. abort_mask &= ~tail_mask;
  387. can_free_echo_skb(core->netdev, tx_fifo_tail, NULL);
  388. stats->tx_dropped++;
  389. stats->tx_aborted_errors++;
  390. tx_fifo_tail = acc_tx_fifo_next(core, tx_fifo_tail);
  391. }
  392. core->tx_fifo_tail = tx_fifo_tail;
  393. if (abort_mask)
  394. netdev_warn(core->netdev, "Unhandled aborted messages\n");
  395. if (!acc_resetmode_entered(core))
  396. netif_wake_queue(core->netdev);
  397. }
  398. static void handle_core_msg_overrun(struct acc_core *core,
  399. const struct acc_bmmsg_overrun *msg)
  400. {
  401. struct acc_net_priv *priv = netdev_priv(core->netdev);
  402. struct net_device_stats *stats = &core->netdev->stats;
  403. struct can_frame *cf;
  404. struct sk_buff *skb;
  405. /* lost_cnt may be 0 if not supported by esdACC version */
  406. if (msg->lost_cnt) {
  407. stats->rx_errors += msg->lost_cnt;
  408. stats->rx_over_errors += msg->lost_cnt;
  409. } else {
  410. stats->rx_errors++;
  411. stats->rx_over_errors++;
  412. }
  413. skb = alloc_can_err_skb(core->netdev, &cf);
  414. if (!skb)
  415. return;
  416. cf->can_id |= CAN_ERR_CRTL;
  417. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  418. skb_hwtstamps(skb)->hwtstamp = acc_ts2ktime(priv->ov, msg->ts);
  419. netif_rx(skb);
  420. }
  421. static void handle_core_msg_buserr(struct acc_core *core,
  422. const struct acc_bmmsg_buserr *msg)
  423. {
  424. struct acc_net_priv *priv = netdev_priv(core->netdev);
  425. struct net_device_stats *stats = &core->netdev->stats;
  426. struct can_frame *cf;
  427. struct sk_buff *skb;
  428. const u32 reg_status = msg->reg_status;
  429. const u8 rxerr = reg_status;
  430. const u8 txerr = (reg_status >> 8);
  431. u8 can_err_prot_type = 0U;
  432. priv->can.can_stats.bus_error++;
  433. /* Error occurred during transmission? */
  434. if (msg->ecc & ACC_ECC_DIR) {
  435. stats->rx_errors++;
  436. } else {
  437. can_err_prot_type |= CAN_ERR_PROT_TX;
  438. stats->tx_errors++;
  439. }
  440. /* Determine error type */
  441. switch (msg->ecc & ACC_ECC_MASK) {
  442. case ACC_ECC_BIT:
  443. can_err_prot_type |= CAN_ERR_PROT_BIT;
  444. break;
  445. case ACC_ECC_FORM:
  446. can_err_prot_type |= CAN_ERR_PROT_FORM;
  447. break;
  448. case ACC_ECC_STUFF:
  449. can_err_prot_type |= CAN_ERR_PROT_STUFF;
  450. break;
  451. default:
  452. can_err_prot_type |= CAN_ERR_PROT_UNSPEC;
  453. break;
  454. }
  455. skb = alloc_can_err_skb(core->netdev, &cf);
  456. if (!skb)
  457. return;
  458. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR | CAN_ERR_CNT;
  459. /* Set protocol error type */
  460. cf->data[2] = can_err_prot_type;
  461. /* Set error location */
  462. cf->data[3] = msg->ecc & ACC_ECC_SEG;
  463. /* Insert CAN TX and RX error counters. */
  464. cf->data[6] = txerr;
  465. cf->data[7] = rxerr;
  466. skb_hwtstamps(skb)->hwtstamp = acc_ts2ktime(priv->ov, msg->ts);
  467. netif_rx(skb);
  468. }
  469. static void
  470. handle_core_msg_errstatechange(struct acc_core *core,
  471. const struct acc_bmmsg_errstatechange *msg)
  472. {
  473. struct acc_net_priv *priv = netdev_priv(core->netdev);
  474. struct can_frame *cf = NULL;
  475. struct sk_buff *skb;
  476. const u32 reg_status = msg->reg_status;
  477. const u8 rxerr = reg_status;
  478. const u8 txerr = (reg_status >> 8);
  479. enum can_state new_state;
  480. if (reg_status & ACC_REG_STATUS_MASK_STATUS_BS) {
  481. new_state = CAN_STATE_BUS_OFF;
  482. } else if (reg_status & ACC_REG_STATUS_MASK_STATUS_EP) {
  483. new_state = CAN_STATE_ERROR_PASSIVE;
  484. } else if (reg_status & ACC_REG_STATUS_MASK_STATUS_ES) {
  485. new_state = CAN_STATE_ERROR_WARNING;
  486. } else {
  487. new_state = CAN_STATE_ERROR_ACTIVE;
  488. if (priv->can.state == CAN_STATE_BUS_OFF) {
  489. /* See comment in acc_set_mode() for CAN_MODE_START */
  490. netif_wake_queue(core->netdev);
  491. }
  492. }
  493. skb = alloc_can_err_skb(core->netdev, &cf);
  494. if (new_state != priv->can.state) {
  495. enum can_state tx_state, rx_state;
  496. tx_state = (txerr >= rxerr) ?
  497. new_state : CAN_STATE_ERROR_ACTIVE;
  498. rx_state = (rxerr >= txerr) ?
  499. new_state : CAN_STATE_ERROR_ACTIVE;
  500. /* Always call can_change_state() to update the state
  501. * even if alloc_can_err_skb() may have failed.
  502. * can_change_state() can cope with a NULL cf pointer.
  503. */
  504. can_change_state(core->netdev, cf, tx_state, rx_state);
  505. }
  506. if (skb) {
  507. cf->can_id |= CAN_ERR_CNT;
  508. cf->data[6] = txerr;
  509. cf->data[7] = rxerr;
  510. skb_hwtstamps(skb)->hwtstamp = acc_ts2ktime(priv->ov, msg->ts);
  511. netif_rx(skb);
  512. }
  513. if (new_state == CAN_STATE_BUS_OFF) {
  514. acc_write32(core, ACC_CORE_OF_TX_ABORT_MASK, 0xffff);
  515. can_bus_off(core->netdev);
  516. }
  517. }
  518. static void handle_core_interrupt(struct acc_core *core)
  519. {
  520. u32 msg_fifo_head = core->bmfifo.local_irq_cnt & 0xff;
  521. while (core->bmfifo.msg_fifo_tail != msg_fifo_head) {
  522. const union acc_bmmsg *msg =
  523. &core->bmfifo.messages[core->bmfifo.msg_fifo_tail];
  524. switch (msg->msg_id) {
  525. case BM_MSG_ID_RXTXDONE:
  526. handle_core_msg_rxtxdone(core, &msg->rxtxdone);
  527. break;
  528. case BM_MSG_ID_TXABORT:
  529. handle_core_msg_txabort(core, &msg->txabort);
  530. break;
  531. case BM_MSG_ID_OVERRUN:
  532. handle_core_msg_overrun(core, &msg->overrun);
  533. break;
  534. case BM_MSG_ID_BUSERR:
  535. handle_core_msg_buserr(core, &msg->buserr);
  536. break;
  537. case BM_MSG_ID_ERRPASSIVE:
  538. case BM_MSG_ID_ERRWARN:
  539. handle_core_msg_errstatechange(core,
  540. &msg->errstatechange);
  541. break;
  542. default:
  543. /* Ignore all other BM messages (like the CAN-FD messages) */
  544. break;
  545. }
  546. core->bmfifo.msg_fifo_tail =
  547. (core->bmfifo.msg_fifo_tail + 1) & 0xff;
  548. }
  549. }
  550. /**
  551. * acc_card_interrupt() - handle the interrupts of an esdACC FPGA
  552. *
  553. * @ov: overview module structure
  554. * @cores: array of core structures
  555. *
  556. * This function handles all interrupts pending for the overview module and the
  557. * CAN cores of the esdACC FPGA.
  558. *
  559. * It examines for all cores (the overview module core and the CAN cores)
  560. * the bmfifo.irq_cnt and compares it with the previously saved
  561. * bmfifo.local_irq_cnt. An IRQ is pending if they differ. The esdACC FPGA
  562. * updates the bmfifo.irq_cnt values by DMA.
  563. *
  564. * The pending interrupts are masked by writing to the IRQ mask register at
  565. * ACC_OV_OF_BM_IRQ_MASK. This register has for each core a two bit command
  566. * field evaluated as follows:
  567. *
  568. * Define, bit pattern: meaning
  569. * 00: no action
  570. * ACC_BM_IRQ_UNMASK, 01: unmask interrupt
  571. * ACC_BM_IRQ_MASK, 10: mask interrupt
  572. * 11: no action
  573. *
  574. * For each CAN core with a pending IRQ handle_core_interrupt() handles all
  575. * busmaster messages from the message FIFO. The last handled message (FIFO
  576. * index) is written to the CAN core to acknowledge its handling.
  577. *
  578. * Last step is to unmask all interrupts in the FPGA using
  579. * ACC_BM_IRQ_UNMASK_ALL.
  580. *
  581. * Return:
  582. * IRQ_HANDLED, if card generated an interrupt that was handled
  583. * IRQ_NONE, if the interrupt is not ours
  584. */
  585. irqreturn_t acc_card_interrupt(struct acc_ov *ov, struct acc_core *cores)
  586. {
  587. u32 irqmask;
  588. int i;
  589. /* First we look for whom interrupts are pending, card/overview
  590. * or any of the cores. Two bits in irqmask are used for each;
  591. * Each two bit field is set to ACC_BM_IRQ_MASK if an IRQ is
  592. * pending.
  593. */
  594. irqmask = 0U;
  595. if (READ_ONCE(*ov->bmfifo.irq_cnt) != ov->bmfifo.local_irq_cnt) {
  596. irqmask |= ACC_BM_IRQ_MASK;
  597. ov->bmfifo.local_irq_cnt = READ_ONCE(*ov->bmfifo.irq_cnt);
  598. }
  599. for (i = 0; i < ov->active_cores; i++) {
  600. struct acc_core *core = &cores[i];
  601. if (READ_ONCE(*core->bmfifo.irq_cnt) != core->bmfifo.local_irq_cnt) {
  602. irqmask |= (ACC_BM_IRQ_MASK << (2 * (i + 1)));
  603. core->bmfifo.local_irq_cnt = READ_ONCE(*core->bmfifo.irq_cnt);
  604. }
  605. }
  606. if (!irqmask)
  607. return IRQ_NONE;
  608. /* At second we tell the card we're working on them by writing irqmask,
  609. * call handle_{ov|core}_interrupt and then acknowledge the
  610. * interrupts by writing irq_cnt:
  611. */
  612. acc_ov_write32(ov, ACC_OV_OF_BM_IRQ_MASK, irqmask);
  613. if (irqmask & ACC_BM_IRQ_MASK) {
  614. /* handle_ov_interrupt(); - no use yet. */
  615. acc_ov_write32(ov, ACC_OV_OF_BM_IRQ_COUNTER,
  616. ov->bmfifo.local_irq_cnt);
  617. }
  618. for (i = 0; i < ov->active_cores; i++) {
  619. struct acc_core *core = &cores[i];
  620. if (irqmask & (ACC_BM_IRQ_MASK << (2 * (i + 1)))) {
  621. handle_core_interrupt(core);
  622. acc_write32(core, ACC_OV_OF_BM_IRQ_COUNTER,
  623. core->bmfifo.local_irq_cnt);
  624. }
  625. }
  626. acc_ov_write32(ov, ACC_OV_OF_BM_IRQ_MASK, ACC_BM_IRQ_UNMASK_ALL);
  627. return IRQ_HANDLED;
  628. }