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
6 #include <linux/bits.h>
7 #include <linux/can/dev.h>
8 #include <linux/kernel.h>
9 #include <linux/netdevice.h>
10 #include <linux/units.h>
12 #define ACC_TS_FREQ_80MHZ (80 * HZ_PER_MHZ)
13 #define ACC_I2C_ADDON_DETECT_DELAY_MS 10
15 /* esdACC Overview Module */
16 #define ACC_OV_OF_PROBE 0x0000
17 #define ACC_OV_OF_VERSION 0x0004
18 #define ACC_OV_OF_INFO 0x0008
19 #define ACC_OV_OF_CANCORE_FREQ 0x000c
20 #define ACC_OV_OF_TS_FREQ_LO 0x0010
21 #define ACC_OV_OF_TS_FREQ_HI 0x0014
22 #define ACC_OV_OF_IRQ_STATUS_CORES 0x0018
23 #define ACC_OV_OF_TS_CURR_LO 0x001c
24 #define ACC_OV_OF_TS_CURR_HI 0x0020
25 #define ACC_OV_OF_IRQ_STATUS 0x0028
26 #define ACC_OV_OF_MODE 0x002c
27 #define ACC_OV_OF_BM_IRQ_COUNTER 0x0070
28 #define ACC_OV_OF_BM_IRQ_MASK 0x0074
29 #define ACC_OV_OF_MSI_DATA 0x0080
30 #define ACC_OV_OF_MSI_ADDRESSOFFSET 0x0084
32 /* Feature flags are contained in the upper 16 bit of the version
33 * register at ACC_OV_OF_VERSION but only used with these masks after
34 * extraction into an extra variable => (xx - 16).
36 #define ACC_OV_REG_FEAT_MASK_CANFD BIT(27 - 16)
37 #define ACC_OV_REG_FEAT_MASK_NEW_PSC BIT(28 - 16)
39 #define ACC_OV_REG_MODE_MASK_ENDIAN_LITTLE BIT(0)
40 #define ACC_OV_REG_MODE_MASK_BM_ENABLE BIT(1)
41 #define ACC_OV_REG_MODE_MASK_MODE_LED BIT(2)
42 #define ACC_OV_REG_MODE_MASK_TIMER_ENABLE BIT(4)
43 #define ACC_OV_REG_MODE_MASK_TIMER_ONE_SHOT BIT(5)
44 #define ACC_OV_REG_MODE_MASK_TIMER_ABSOLUTE BIT(6)
45 #define ACC_OV_REG_MODE_MASK_TIMER GENMASK(6, 4)
46 #define ACC_OV_REG_MODE_MASK_TS_SRC GENMASK(8, 7)
47 #define ACC_OV_REG_MODE_MASK_I2C_ENABLE BIT(11)
48 #define ACC_OV_REG_MODE_MASK_MSI_ENABLE BIT(14)
49 #define ACC_OV_REG_MODE_MASK_NEW_PSC_ENABLE BIT(15)
50 #define ACC_OV_REG_MODE_MASK_FPGA_RESET BIT(31)
52 /* esdACC CAN Core Module */
53 #define ACC_CORE_OF_CTRL_MODE 0x0000
54 #define ACC_CORE_OF_STATUS_IRQ 0x0008
55 #define ACC_CORE_OF_BRP 0x000c
56 #define ACC_CORE_OF_BTR 0x0010
57 #define ACC_CORE_OF_FBTR 0x0014
58 #define ACC_CORE_OF_STATUS 0x0030
59 #define ACC_CORE_OF_TXFIFO_CONFIG 0x0048
60 #define ACC_CORE_OF_TXFIFO_STATUS 0x004c
61 #define ACC_CORE_OF_TX_STATUS_IRQ 0x0050
62 #define ACC_CORE_OF_TX_ABORT_MASK 0x0054
63 #define ACC_CORE_OF_BM_IRQ_COUNTER 0x0070
64 #define ACC_CORE_OF_TXFIFO_ID 0x00c0
65 #define ACC_CORE_OF_TXFIFO_DLC 0x00c4
66 #define ACC_CORE_OF_TXFIFO_DATA_0 0x00c8
67 #define ACC_CORE_OF_TXFIFO_DATA_1 0x00cc
69 #define ACC_REG_CONTROL_MASK_MODE_RESETMODE BIT(0)
70 #define ACC_REG_CONTROL_MASK_MODE_LOM BIT(1)
71 #define ACC_REG_CONTROL_MASK_MODE_STM BIT(2)
72 #define ACC_REG_CONTROL_MASK_MODE_TRANSEN BIT(5)
73 #define ACC_REG_CONTROL_MASK_MODE_TS BIT(6)
74 #define ACC_REG_CONTROL_MASK_MODE_SCHEDULE BIT(7)
76 #define ACC_REG_CONTROL_MASK_IE_RXTX BIT(8)
77 #define ACC_REG_CONTROL_MASK_IE_TXERROR BIT(9)
78 #define ACC_REG_CONTROL_MASK_IE_ERRWARN BIT(10)
79 #define ACC_REG_CONTROL_MASK_IE_OVERRUN BIT(11)
80 #define ACC_REG_CONTROL_MASK_IE_TSI BIT(12)
81 #define ACC_REG_CONTROL_MASK_IE_ERRPASS BIT(13)
82 #define ACC_REG_CONTROL_MASK_IE_ALI BIT(14)
83 #define ACC_REG_CONTROL_MASK_IE_BUSERR BIT(15)
85 /* BRP and BTR register layout for CAN-Classic version */
86 #define ACC_REG_BRP_CL_MASK_BRP GENMASK(8, 0)
87 #define ACC_REG_BTR_CL_MASK_TSEG1 GENMASK(3, 0)
88 #define ACC_REG_BTR_CL_MASK_TSEG2 GENMASK(18, 16)
89 #define ACC_REG_BTR_CL_MASK_SJW GENMASK(25, 24)
91 /* BRP and BTR register layout for CAN-FD version */
92 #define ACC_REG_BRP_FD_MASK_BRP GENMASK(7, 0)
93 #define ACC_REG_BTR_FD_MASK_TSEG1 GENMASK(7, 0)
94 #define ACC_REG_BTR_FD_MASK_TSEG2 GENMASK(22, 16)
95 #define ACC_REG_BTR_FD_MASK_SJW GENMASK(30, 24)
97 /* 256 BM_MSGs of 32 byte size */
98 #define ACC_CORE_DMAMSG_SIZE 32U
99 #define ACC_CORE_DMABUF_SIZE (256U * ACC_CORE_DMAMSG_SIZE)
102 BM_MSG_ID_RXTXDONE = 0x01,
103 BM_MSG_ID_TXABORT = 0x02,
104 BM_MSG_ID_OVERRUN = 0x03,
105 BM_MSG_ID_BUSERR = 0x04,
106 BM_MSG_ID_ERRPASSIVE = 0x05,
107 BM_MSG_ID_ERRWARN = 0x06,
108 BM_MSG_ID_TIMESLICE = 0x07,
109 BM_MSG_ID_HWTIMER = 0x08,
110 BM_MSG_ID_HOTPLUG = 0x09,
113 /* The struct acc_bmmsg_* structure declarations that follow here provide
114 * access to the ring buffer of bus master messages maintained by the FPGA
115 * bus master engine. All bus master messages have the same size of
116 * ACC_CORE_DMAMSG_SIZE and a minimum alignment of ACC_CORE_DMAMSG_SIZE in
119 * All structure members are natural aligned. Therefore we should not need
120 * a __packed attribute. All struct acc_bmmsg_* declarations have at least
121 * reserved* members to fill the structure to the full ACC_CORE_DMAMSG_SIZE.
123 * A failure of this property due padding will be detected at compile time
124 * by static_assert(sizeof(union acc_bmmsg) == ACC_CORE_DMAMSG_SIZE).
127 struct acc_bmmsg_rxtxdone {
140 u8 data[CAN_MAX_DLEN];
141 /* Time stamps in struct acc_ov::timestamp_frequency ticks. */
145 struct acc_bmmsg_txabort {
156 struct acc_bmmsg_overrun {
167 struct acc_bmmsg_buserr {
180 struct acc_bmmsg_errstatechange {
191 struct acc_bmmsg_timeslice {
201 struct acc_bmmsg_hwtimer {
209 struct acc_bmmsg_hotplug {
217 struct acc_bmmsg_rxtxdone rxtxdone;
218 struct acc_bmmsg_txabort txabort;
219 struct acc_bmmsg_overrun overrun;
220 struct acc_bmmsg_buserr buserr;
221 struct acc_bmmsg_errstatechange errstatechange;
222 struct acc_bmmsg_timeslice timeslice;
223 struct acc_bmmsg_hwtimer hwtimer;
226 /* Check size of union acc_bmmsg to be of expected size. */
227 static_assert(sizeof(union acc_bmmsg) == ACC_CORE_DMAMSG_SIZE);
230 const union acc_bmmsg *messages;
231 /* irq_cnt points to an u32 value where the esdACC FPGA deposits
232 * the bm_fifo head index in coherent DMA memory. Only bits 7..0
233 * are valid. Use READ_ONCE() to access this memory location.
242 struct net_device *netdev;
243 struct acc_bmfifo bmfifo;
251 struct acc_bmfifo bmfifo;
252 u32 timestamp_frequency;
260 struct acc_net_priv {
261 struct can_priv can; /* must be the first member! */
262 struct acc_core *core;
266 static inline u32 acc_read32(struct acc_core *core, unsigned short offs)
268 return ioread32be(core->addr + offs);
271 static inline void acc_write32(struct acc_core *core,
272 unsigned short offs, u32 v)
274 iowrite32be(v, core->addr + offs);
277 static inline void acc_write32_noswap(struct acc_core *core,
278 unsigned short offs, u32 v)
280 iowrite32(v, core->addr + offs);
283 static inline void acc_set_bits(struct acc_core *core,
284 unsigned short offs, u32 mask)
286 u32 v = acc_read32(core, offs);
289 acc_write32(core, offs, v);
292 static inline void acc_clear_bits(struct acc_core *core,
293 unsigned short offs, u32 mask)
295 u32 v = acc_read32(core, offs);
298 acc_write32(core, offs, v);
301 static inline int acc_resetmode_entered(struct acc_core *core)
303 u32 ctrl = acc_read32(core, ACC_CORE_OF_CTRL_MODE);
305 return (ctrl & ACC_REG_CONTROL_MASK_MODE_RESETMODE) != 0;
308 static inline u32 acc_ov_read32(struct acc_ov *ov, unsigned short offs)
310 return ioread32be(ov->addr + offs);
313 static inline void acc_ov_write32(struct acc_ov *ov,
314 unsigned short offs, u32 v)
316 iowrite32be(v, ov->addr + offs);
319 static inline void acc_ov_set_bits(struct acc_ov *ov,
320 unsigned short offs, u32 b)
322 u32 v = acc_ov_read32(ov, offs);
325 acc_ov_write32(ov, offs, v);
328 static inline void acc_ov_clear_bits(struct acc_ov *ov,
329 unsigned short offs, u32 b)
331 u32 v = acc_ov_read32(ov, offs);
334 acc_ov_write32(ov, offs, v);
337 static inline void acc_reset_fpga(struct acc_ov *ov)
339 acc_ov_write32(ov, ACC_OV_OF_MODE, ACC_OV_REG_MODE_MASK_FPGA_RESET);
341 /* (Re-)start and wait for completion of addon detection on the I^2C bus */
342 acc_ov_set_bits(ov, ACC_OV_OF_MODE, ACC_OV_REG_MODE_MASK_I2C_ENABLE);
343 mdelay(ACC_I2C_ADDON_DETECT_DELAY_MS);
346 void acc_init_ov(struct acc_ov *ov, struct device *dev);
347 void acc_init_bm_ptr(struct acc_ov *ov, struct acc_core *cores,
349 int acc_open(struct net_device *netdev);
350 int acc_close(struct net_device *netdev);
351 netdev_tx_t acc_start_xmit(struct sk_buff *skb, struct net_device *netdev);
352 int acc_get_berr_counter(const struct net_device *netdev,
353 struct can_berr_counter *bec);
354 int acc_set_mode(struct net_device *netdev, enum can_mode mode);
355 int acc_set_bittiming(struct net_device *netdev);
356 irqreturn_t acc_card_interrupt(struct acc_ov *ov, struct acc_core *cores);