esdacc.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 <linux/bits.h>
  6. #include <linux/can/dev.h>
  7. #include <linux/kernel.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/units.h>
  10. #define ACC_TS_FREQ_80MHZ (80 * HZ_PER_MHZ)
  11. #define ACC_I2C_ADDON_DETECT_DELAY_MS 10
  12. /* esdACC Overview Module */
  13. #define ACC_OV_OF_PROBE 0x0000
  14. #define ACC_OV_OF_VERSION 0x0004
  15. #define ACC_OV_OF_INFO 0x0008
  16. #define ACC_OV_OF_CANCORE_FREQ 0x000c
  17. #define ACC_OV_OF_TS_FREQ_LO 0x0010
  18. #define ACC_OV_OF_TS_FREQ_HI 0x0014
  19. #define ACC_OV_OF_IRQ_STATUS_CORES 0x0018
  20. #define ACC_OV_OF_TS_CURR_LO 0x001c
  21. #define ACC_OV_OF_TS_CURR_HI 0x0020
  22. #define ACC_OV_OF_IRQ_STATUS 0x0028
  23. #define ACC_OV_OF_MODE 0x002c
  24. #define ACC_OV_OF_BM_IRQ_COUNTER 0x0070
  25. #define ACC_OV_OF_BM_IRQ_MASK 0x0074
  26. #define ACC_OV_OF_MSI_DATA 0x0080
  27. #define ACC_OV_OF_MSI_ADDRESSOFFSET 0x0084
  28. /* Feature flags are contained in the upper 16 bit of the version
  29. * register at ACC_OV_OF_VERSION but only used with these masks after
  30. * extraction into an extra variable => (xx - 16).
  31. */
  32. #define ACC_OV_REG_FEAT_MASK_CANFD BIT(27 - 16)
  33. #define ACC_OV_REG_FEAT_MASK_NEW_PSC BIT(28 - 16)
  34. #define ACC_OV_REG_FEAT_MASK_DAR BIT(30 - 16)
  35. #define ACC_OV_REG_MODE_MASK_ENDIAN_LITTLE BIT(0)
  36. #define ACC_OV_REG_MODE_MASK_BM_ENABLE BIT(1)
  37. #define ACC_OV_REG_MODE_MASK_MODE_LED BIT(2)
  38. #define ACC_OV_REG_MODE_MASK_TIMER_ENABLE BIT(4)
  39. #define ACC_OV_REG_MODE_MASK_TIMER_ONE_SHOT BIT(5)
  40. #define ACC_OV_REG_MODE_MASK_TIMER_ABSOLUTE BIT(6)
  41. #define ACC_OV_REG_MODE_MASK_TIMER GENMASK(6, 4)
  42. #define ACC_OV_REG_MODE_MASK_TS_SRC GENMASK(8, 7)
  43. #define ACC_OV_REG_MODE_MASK_I2C_ENABLE BIT(11)
  44. #define ACC_OV_REG_MODE_MASK_MSI_ENABLE BIT(14)
  45. #define ACC_OV_REG_MODE_MASK_NEW_PSC_ENABLE BIT(15)
  46. #define ACC_OV_REG_MODE_MASK_FPGA_RESET BIT(31)
  47. /* esdACC CAN Core Module */
  48. #define ACC_CORE_OF_CTRL 0x0000
  49. #define ACC_CORE_OF_STATUS_IRQ 0x0008
  50. #define ACC_CORE_OF_BRP 0x000c
  51. #define ACC_CORE_OF_BTR 0x0010
  52. #define ACC_CORE_OF_FBTR 0x0014
  53. #define ACC_CORE_OF_STATUS 0x0030
  54. #define ACC_CORE_OF_TXFIFO_CONFIG 0x0048
  55. #define ACC_CORE_OF_TXFIFO_STATUS 0x004c
  56. #define ACC_CORE_OF_TX_STATUS_IRQ 0x0050
  57. #define ACC_CORE_OF_TX_ABORT_MASK 0x0054
  58. #define ACC_CORE_OF_BM_IRQ_COUNTER 0x0070
  59. #define ACC_CORE_OF_TXFIFO_ID 0x00c0
  60. #define ACC_CORE_OF_TXFIFO_DLC 0x00c4
  61. #define ACC_CORE_OF_TXFIFO_DATA_0 0x00c8
  62. #define ACC_CORE_OF_TXFIFO_DATA_1 0x00cc
  63. /* CTRL register layout */
  64. #define ACC_REG_CTRL_MASK_RESETMODE BIT(0)
  65. #define ACC_REG_CTRL_MASK_LOM BIT(1)
  66. #define ACC_REG_CTRL_MASK_STM BIT(2)
  67. #define ACC_REG_CTRL_MASK_TRANSEN BIT(5)
  68. #define ACC_REG_CTRL_MASK_TS BIT(6)
  69. #define ACC_REG_CTRL_MASK_SCHEDULE BIT(7)
  70. #define ACC_REG_CTRL_MASK_IE_RXTX BIT(8)
  71. #define ACC_REG_CTRL_MASK_IE_TXERROR BIT(9)
  72. #define ACC_REG_CTRL_MASK_IE_ERRWARN BIT(10)
  73. #define ACC_REG_CTRL_MASK_IE_OVERRUN BIT(11)
  74. #define ACC_REG_CTRL_MASK_IE_TSI BIT(12)
  75. #define ACC_REG_CTRL_MASK_IE_ERRPASS BIT(13)
  76. #define ACC_REG_CTRL_MASK_IE_ALI BIT(14)
  77. #define ACC_REG_CTRL_MASK_IE_BUSERR BIT(15)
  78. /* BRP and BTR register layout for CAN-Classic version */
  79. #define ACC_REG_BRP_CL_MASK_BRP GENMASK(8, 0)
  80. #define ACC_REG_BTR_CL_MASK_TSEG1 GENMASK(3, 0)
  81. #define ACC_REG_BTR_CL_MASK_TSEG2 GENMASK(18, 16)
  82. #define ACC_REG_BTR_CL_MASK_SJW GENMASK(25, 24)
  83. /* BRP and BTR register layout for CAN-FD version */
  84. #define ACC_REG_BRP_FD_MASK_BRP GENMASK(7, 0)
  85. #define ACC_REG_BTR_FD_MASK_TSEG1 GENMASK(7, 0)
  86. #define ACC_REG_BTR_FD_MASK_TSEG2 GENMASK(22, 16)
  87. #define ACC_REG_BTR_FD_MASK_SJW GENMASK(30, 24)
  88. /* 256 BM_MSGs of 32 byte size */
  89. #define ACC_CORE_DMAMSG_SIZE 32U
  90. #define ACC_CORE_DMABUF_SIZE (256U * ACC_CORE_DMAMSG_SIZE)
  91. enum acc_bmmsg_id {
  92. BM_MSG_ID_RXTXDONE = 0x01,
  93. BM_MSG_ID_TXABORT = 0x02,
  94. BM_MSG_ID_OVERRUN = 0x03,
  95. BM_MSG_ID_BUSERR = 0x04,
  96. BM_MSG_ID_ERRPASSIVE = 0x05,
  97. BM_MSG_ID_ERRWARN = 0x06,
  98. BM_MSG_ID_TIMESLICE = 0x07,
  99. BM_MSG_ID_HWTIMER = 0x08,
  100. BM_MSG_ID_HOTPLUG = 0x09,
  101. };
  102. /* The struct acc_bmmsg_* structure declarations that follow here provide
  103. * access to the ring buffer of bus master messages maintained by the FPGA
  104. * bus master engine. All bus master messages have the same size of
  105. * ACC_CORE_DMAMSG_SIZE and a minimum alignment of ACC_CORE_DMAMSG_SIZE in
  106. * memory.
  107. *
  108. * All structure members are natural aligned. Therefore we should not need
  109. * a __packed attribute. All struct acc_bmmsg_* declarations have at least
  110. * reserved* members to fill the structure to the full ACC_CORE_DMAMSG_SIZE.
  111. *
  112. * A failure of this property due padding will be detected at compile time
  113. * by static_assert(sizeof(union acc_bmmsg) == ACC_CORE_DMAMSG_SIZE).
  114. */
  115. struct acc_bmmsg_rxtxdone {
  116. u8 msg_id;
  117. u8 txfifo_level;
  118. u8 reserved1[2];
  119. u8 txtsfifo_level;
  120. u8 reserved2[3];
  121. u32 id;
  122. struct {
  123. u8 len;
  124. u8 txdfifo_idx;
  125. u8 zeroes8;
  126. u8 reserved;
  127. } acc_dlc;
  128. u8 data[CAN_MAX_DLEN];
  129. /* Time stamps in struct acc_ov::timestamp_frequency ticks. */
  130. u64 ts;
  131. };
  132. struct acc_bmmsg_txabort {
  133. u8 msg_id;
  134. u8 txfifo_level;
  135. u16 abort_mask;
  136. u8 txtsfifo_level;
  137. u8 reserved2[1];
  138. u16 abort_mask_txts;
  139. u64 ts;
  140. u32 reserved3[4];
  141. };
  142. struct acc_bmmsg_overrun {
  143. u8 msg_id;
  144. u8 txfifo_level;
  145. u8 lost_cnt;
  146. u8 reserved1;
  147. u8 txtsfifo_level;
  148. u8 reserved2[3];
  149. u64 ts;
  150. u32 reserved3[4];
  151. };
  152. struct acc_bmmsg_buserr {
  153. u8 msg_id;
  154. u8 txfifo_level;
  155. u8 ecc;
  156. u8 reserved1;
  157. u8 txtsfifo_level;
  158. u8 reserved2[3];
  159. u64 ts;
  160. u32 reg_status;
  161. u32 reg_btr;
  162. u32 reserved3[2];
  163. };
  164. struct acc_bmmsg_errstatechange {
  165. u8 msg_id;
  166. u8 txfifo_level;
  167. u8 reserved1[2];
  168. u8 txtsfifo_level;
  169. u8 reserved2[3];
  170. u64 ts;
  171. u32 reg_status;
  172. u32 reserved3[3];
  173. };
  174. struct acc_bmmsg_timeslice {
  175. u8 msg_id;
  176. u8 txfifo_level;
  177. u8 reserved1[2];
  178. u8 txtsfifo_level;
  179. u8 reserved2[3];
  180. u64 ts;
  181. u32 reserved3[4];
  182. };
  183. struct acc_bmmsg_hwtimer {
  184. u8 msg_id;
  185. u8 reserved1[3];
  186. u32 reserved2[1];
  187. u64 timer;
  188. u32 reserved3[4];
  189. };
  190. struct acc_bmmsg_hotplug {
  191. u8 msg_id;
  192. u8 reserved1[3];
  193. u32 reserved2[7];
  194. };
  195. union acc_bmmsg {
  196. u8 msg_id;
  197. struct acc_bmmsg_rxtxdone rxtxdone;
  198. struct acc_bmmsg_txabort txabort;
  199. struct acc_bmmsg_overrun overrun;
  200. struct acc_bmmsg_buserr buserr;
  201. struct acc_bmmsg_errstatechange errstatechange;
  202. struct acc_bmmsg_timeslice timeslice;
  203. struct acc_bmmsg_hwtimer hwtimer;
  204. };
  205. /* Check size of union acc_bmmsg to be of expected size. */
  206. static_assert(sizeof(union acc_bmmsg) == ACC_CORE_DMAMSG_SIZE);
  207. struct acc_bmfifo {
  208. const union acc_bmmsg *messages;
  209. /* irq_cnt points to an u32 value where the esdACC FPGA deposits
  210. * the bm_fifo head index in coherent DMA memory. Only bits 7..0
  211. * are valid. Use READ_ONCE() to access this memory location.
  212. */
  213. const u32 *irq_cnt;
  214. u32 local_irq_cnt;
  215. u32 msg_fifo_tail;
  216. };
  217. struct acc_core {
  218. void __iomem *addr;
  219. struct net_device *netdev;
  220. struct acc_bmfifo bmfifo;
  221. u8 tx_fifo_size;
  222. u8 tx_fifo_head;
  223. u8 tx_fifo_tail;
  224. };
  225. struct acc_ov {
  226. void __iomem *addr;
  227. struct acc_bmfifo bmfifo;
  228. u32 timestamp_frequency;
  229. u32 core_frequency;
  230. u16 version;
  231. u16 features;
  232. u8 total_cores;
  233. u8 active_cores;
  234. };
  235. struct acc_net_priv {
  236. struct can_priv can; /* must be the first member! */
  237. struct acc_core *core;
  238. struct acc_ov *ov;
  239. };
  240. static inline u32 acc_read32(struct acc_core *core, unsigned short offs)
  241. {
  242. return ioread32be(core->addr + offs);
  243. }
  244. static inline void acc_write32(struct acc_core *core,
  245. unsigned short offs, u32 v)
  246. {
  247. iowrite32be(v, core->addr + offs);
  248. }
  249. static inline void acc_write32_noswap(struct acc_core *core,
  250. unsigned short offs, u32 v)
  251. {
  252. iowrite32(v, core->addr + offs);
  253. }
  254. static inline void acc_set_bits(struct acc_core *core,
  255. unsigned short offs, u32 mask)
  256. {
  257. u32 v = acc_read32(core, offs);
  258. v |= mask;
  259. acc_write32(core, offs, v);
  260. }
  261. static inline void acc_clear_bits(struct acc_core *core,
  262. unsigned short offs, u32 mask)
  263. {
  264. u32 v = acc_read32(core, offs);
  265. v &= ~mask;
  266. acc_write32(core, offs, v);
  267. }
  268. static inline int acc_resetmode_entered(struct acc_core *core)
  269. {
  270. u32 ctrl = acc_read32(core, ACC_CORE_OF_CTRL);
  271. return (ctrl & ACC_REG_CTRL_MASK_RESETMODE) != 0;
  272. }
  273. static inline u32 acc_ov_read32(struct acc_ov *ov, unsigned short offs)
  274. {
  275. return ioread32be(ov->addr + offs);
  276. }
  277. static inline void acc_ov_write32(struct acc_ov *ov,
  278. unsigned short offs, u32 v)
  279. {
  280. iowrite32be(v, ov->addr + offs);
  281. }
  282. static inline void acc_ov_set_bits(struct acc_ov *ov,
  283. unsigned short offs, u32 b)
  284. {
  285. u32 v = acc_ov_read32(ov, offs);
  286. v |= b;
  287. acc_ov_write32(ov, offs, v);
  288. }
  289. static inline void acc_ov_clear_bits(struct acc_ov *ov,
  290. unsigned short offs, u32 b)
  291. {
  292. u32 v = acc_ov_read32(ov, offs);
  293. v &= ~b;
  294. acc_ov_write32(ov, offs, v);
  295. }
  296. static inline void acc_reset_fpga(struct acc_ov *ov)
  297. {
  298. acc_ov_write32(ov, ACC_OV_OF_MODE, ACC_OV_REG_MODE_MASK_FPGA_RESET);
  299. /* (Re-)start and wait for completion of addon detection on the I^2C bus */
  300. acc_ov_set_bits(ov, ACC_OV_OF_MODE, ACC_OV_REG_MODE_MASK_I2C_ENABLE);
  301. mdelay(ACC_I2C_ADDON_DETECT_DELAY_MS);
  302. }
  303. void acc_init_ov(struct acc_ov *ov, struct device *dev);
  304. void acc_init_bm_ptr(struct acc_ov *ov, struct acc_core *cores,
  305. const void *mem);
  306. int acc_open(struct net_device *netdev);
  307. int acc_close(struct net_device *netdev);
  308. netdev_tx_t acc_start_xmit(struct sk_buff *skb, struct net_device *netdev);
  309. int acc_get_berr_counter(const struct net_device *netdev,
  310. struct can_berr_counter *bec);
  311. int acc_set_mode(struct net_device *netdev, enum can_mode mode);
  312. int acc_set_bittiming(struct net_device *netdev);
  313. irqreturn_t acc_card_interrupt(struct acc_ov *ov, struct acc_core *cores);