xref: /openbmc/linux/drivers/net/can/ti_hecc.c (revision 5fb859f7)
1 /*
2  * TI HECC (CAN) device driver
3  *
4  * This driver supports TI's HECC (High End CAN Controller module) and the
5  * specs for the same is available at <http://www.ti.com>
6  *
7  * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
8  * Copyright (C) 2019 Jeroen Hofstee <jhofstee@victronenergy.com>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation version 2.
13  *
14  * This program is distributed as is WITHOUT ANY WARRANTY of any
15  * kind, whether express or implied; without even the implied warranty
16  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  */
20 
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/types.h>
24 #include <linux/interrupt.h>
25 #include <linux/errno.h>
26 #include <linux/netdevice.h>
27 #include <linux/skbuff.h>
28 #include <linux/platform_device.h>
29 #include <linux/clk.h>
30 #include <linux/io.h>
31 #include <linux/of.h>
32 #include <linux/of_device.h>
33 #include <linux/regulator/consumer.h>
34 
35 #include <linux/can/dev.h>
36 #include <linux/can/error.h>
37 #include <linux/can/rx-offload.h>
38 
39 #define DRV_NAME "ti_hecc"
40 #define HECC_MODULE_VERSION     "0.7"
41 MODULE_VERSION(HECC_MODULE_VERSION);
42 #define DRV_DESC "TI High End CAN Controller Driver " HECC_MODULE_VERSION
43 
44 /* TX / RX Mailbox Configuration */
45 #define HECC_MAX_MAILBOXES	32	/* hardware mailboxes - do not change */
46 #define MAX_TX_PRIO		0x3F	/* hardware value - do not change */
47 
48 /* Important Note: TX mailbox configuration
49  * TX mailboxes should be restricted to the number of SKB buffers to avoid
50  * maintaining SKB buffers separately. TX mailboxes should be a power of 2
51  * for the mailbox logic to work.  Top mailbox numbers are reserved for RX
52  * and lower mailboxes for TX.
53  *
54  * HECC_MAX_TX_MBOX	HECC_MB_TX_SHIFT
55  * 4 (default)		2
56  * 8			3
57  * 16			4
58  */
59 #define HECC_MB_TX_SHIFT	2 /* as per table above */
60 #define HECC_MAX_TX_MBOX	BIT(HECC_MB_TX_SHIFT)
61 
62 #define HECC_TX_PRIO_SHIFT	(HECC_MB_TX_SHIFT)
63 #define HECC_TX_PRIO_MASK	(MAX_TX_PRIO << HECC_MB_TX_SHIFT)
64 #define HECC_TX_MB_MASK		(HECC_MAX_TX_MBOX - 1)
65 #define HECC_TX_MASK		((HECC_MAX_TX_MBOX - 1) | HECC_TX_PRIO_MASK)
66 
67 /* RX mailbox configuration
68  *
69  * The remaining mailboxes are used for reception and are delivered
70  * based on their timestamp, to avoid a hardware race when CANME is
71  * changed while CAN-bus traffic is being received.
72  */
73 #define HECC_MAX_RX_MBOX	(HECC_MAX_MAILBOXES - HECC_MAX_TX_MBOX)
74 #define HECC_RX_FIRST_MBOX	(HECC_MAX_MAILBOXES - 1)
75 #define HECC_RX_LAST_MBOX	(HECC_MAX_TX_MBOX)
76 
77 /* TI HECC module registers */
78 #define HECC_CANME		0x0	/* Mailbox enable */
79 #define HECC_CANMD		0x4	/* Mailbox direction */
80 #define HECC_CANTRS		0x8	/* Transmit request set */
81 #define HECC_CANTRR		0xC	/* Transmit request */
82 #define HECC_CANTA		0x10	/* Transmission acknowledge */
83 #define HECC_CANAA		0x14	/* Abort acknowledge */
84 #define HECC_CANRMP		0x18	/* Receive message pending */
85 #define HECC_CANRML		0x1C	/* Receive message lost */
86 #define HECC_CANRFP		0x20	/* Remote frame pending */
87 #define HECC_CANGAM		0x24	/* SECC only:Global acceptance mask */
88 #define HECC_CANMC		0x28	/* Master control */
89 #define HECC_CANBTC		0x2C	/* Bit timing configuration */
90 #define HECC_CANES		0x30	/* Error and status */
91 #define HECC_CANTEC		0x34	/* Transmit error counter */
92 #define HECC_CANREC		0x38	/* Receive error counter */
93 #define HECC_CANGIF0		0x3C	/* Global interrupt flag 0 */
94 #define HECC_CANGIM		0x40	/* Global interrupt mask */
95 #define HECC_CANGIF1		0x44	/* Global interrupt flag 1 */
96 #define HECC_CANMIM		0x48	/* Mailbox interrupt mask */
97 #define HECC_CANMIL		0x4C	/* Mailbox interrupt level */
98 #define HECC_CANOPC		0x50	/* Overwrite protection control */
99 #define HECC_CANTIOC		0x54	/* Transmit I/O control */
100 #define HECC_CANRIOC		0x58	/* Receive I/O control */
101 #define HECC_CANLNT		0x5C	/* HECC only: Local network time */
102 #define HECC_CANTOC		0x60	/* HECC only: Time-out control */
103 #define HECC_CANTOS		0x64	/* HECC only: Time-out status */
104 #define HECC_CANTIOCE		0x68	/* SCC only:Enhanced TX I/O control */
105 #define HECC_CANRIOCE		0x6C	/* SCC only:Enhanced RX I/O control */
106 
107 /* TI HECC RAM registers */
108 #define HECC_CANMOTS		0x80	/* Message object time stamp */
109 
110 /* Mailbox registers */
111 #define HECC_CANMID		0x0
112 #define HECC_CANMCF		0x4
113 #define HECC_CANMDL		0x8
114 #define HECC_CANMDH		0xC
115 
116 #define HECC_SET_REG		0xFFFFFFFF
117 #define HECC_CANID_MASK		0x3FF	/* 18 bits mask for extended id's */
118 #define HECC_CCE_WAIT_COUNT     100	/* Wait for ~1 sec for CCE bit */
119 
120 #define HECC_CANMC_SCM		BIT(13)	/* SCC compat mode */
121 #define HECC_CANMC_CCR		BIT(12)	/* Change config request */
122 #define HECC_CANMC_PDR		BIT(11)	/* Local Power down - for sleep mode */
123 #define HECC_CANMC_ABO		BIT(7)	/* Auto Bus On */
124 #define HECC_CANMC_STM		BIT(6)	/* Self test mode - loopback */
125 #define HECC_CANMC_SRES		BIT(5)	/* Software reset */
126 
127 #define HECC_CANTIOC_EN		BIT(3)	/* Enable CAN TX I/O pin */
128 #define HECC_CANRIOC_EN		BIT(3)	/* Enable CAN RX I/O pin */
129 
130 #define HECC_CANMID_IDE		BIT(31)	/* Extended frame format */
131 #define HECC_CANMID_AME		BIT(30)	/* Acceptance mask enable */
132 #define HECC_CANMID_AAM		BIT(29)	/* Auto answer mode */
133 
134 #define HECC_CANES_FE		BIT(24)	/* form error */
135 #define HECC_CANES_BE		BIT(23)	/* bit error */
136 #define HECC_CANES_SA1		BIT(22)	/* stuck at dominant error */
137 #define HECC_CANES_CRCE		BIT(21)	/* CRC error */
138 #define HECC_CANES_SE		BIT(20)	/* stuff bit error */
139 #define HECC_CANES_ACKE		BIT(19)	/* ack error */
140 #define HECC_CANES_BO		BIT(18)	/* Bus off status */
141 #define HECC_CANES_EP		BIT(17)	/* Error passive status */
142 #define HECC_CANES_EW		BIT(16)	/* Error warning status */
143 #define HECC_CANES_SMA		BIT(5)	/* suspend mode ack */
144 #define HECC_CANES_CCE		BIT(4)	/* Change config enabled */
145 #define HECC_CANES_PDA		BIT(3)	/* Power down mode ack */
146 
147 #define HECC_CANBTC_SAM		BIT(7)	/* sample points */
148 
149 #define HECC_BUS_ERROR		(HECC_CANES_FE | HECC_CANES_BE |\
150 				HECC_CANES_CRCE | HECC_CANES_SE |\
151 				HECC_CANES_ACKE)
152 #define HECC_CANES_FLAGS	(HECC_BUS_ERROR | HECC_CANES_BO |\
153 				HECC_CANES_EP | HECC_CANES_EW)
154 
155 #define HECC_CANMCF_RTR		BIT(4)	/* Remote transmit request */
156 
157 #define HECC_CANGIF_MAIF	BIT(17)	/* Message alarm interrupt */
158 #define HECC_CANGIF_TCOIF	BIT(16) /* Timer counter overflow int */
159 #define HECC_CANGIF_GMIF	BIT(15)	/* Global mailbox interrupt */
160 #define HECC_CANGIF_AAIF	BIT(14)	/* Abort ack interrupt */
161 #define HECC_CANGIF_WDIF	BIT(13)	/* Write denied interrupt */
162 #define HECC_CANGIF_WUIF	BIT(12)	/* Wake up interrupt */
163 #define HECC_CANGIF_RMLIF	BIT(11)	/* Receive message lost interrupt */
164 #define HECC_CANGIF_BOIF	BIT(10)	/* Bus off interrupt */
165 #define HECC_CANGIF_EPIF	BIT(9)	/* Error passive interrupt */
166 #define HECC_CANGIF_WLIF	BIT(8)	/* Warning level interrupt */
167 #define HECC_CANGIF_MBOX_MASK	0x1F	/* Mailbox number mask */
168 #define HECC_CANGIM_I1EN	BIT(1)	/* Int line 1 enable */
169 #define HECC_CANGIM_I0EN	BIT(0)	/* Int line 0 enable */
170 #define HECC_CANGIM_DEF_MASK	0x700	/* only busoff/warning/passive */
171 #define HECC_CANGIM_SIL		BIT(2)	/* system interrupts to int line 1 */
172 
173 /* CAN Bittiming constants as per HECC specs */
174 static const struct can_bittiming_const ti_hecc_bittiming_const = {
175 	.name = DRV_NAME,
176 	.tseg1_min = 1,
177 	.tseg1_max = 16,
178 	.tseg2_min = 1,
179 	.tseg2_max = 8,
180 	.sjw_max = 4,
181 	.brp_min = 1,
182 	.brp_max = 256,
183 	.brp_inc = 1,
184 };
185 
186 struct ti_hecc_priv {
187 	struct can_priv can;	/* MUST be first member/field */
188 	struct can_rx_offload offload;
189 	struct net_device *ndev;
190 	struct clk *clk;
191 	void __iomem *base;
192 	void __iomem *hecc_ram;
193 	void __iomem *mbx;
194 	bool use_hecc1int;
195 	spinlock_t mbx_lock; /* CANME register needs protection */
196 	u32 tx_head;
197 	u32 tx_tail;
198 	struct regulator *reg_xceiver;
199 };
200 
201 static inline int get_tx_head_mb(struct ti_hecc_priv *priv)
202 {
203 	return priv->tx_head & HECC_TX_MB_MASK;
204 }
205 
206 static inline int get_tx_tail_mb(struct ti_hecc_priv *priv)
207 {
208 	return priv->tx_tail & HECC_TX_MB_MASK;
209 }
210 
211 static inline int get_tx_head_prio(struct ti_hecc_priv *priv)
212 {
213 	return (priv->tx_head >> HECC_TX_PRIO_SHIFT) & MAX_TX_PRIO;
214 }
215 
216 static inline void hecc_write_lam(struct ti_hecc_priv *priv, u32 mbxno, u32 val)
217 {
218 	__raw_writel(val, priv->hecc_ram + mbxno * 4);
219 }
220 
221 static inline u32 hecc_read_stamp(struct ti_hecc_priv *priv, u32 mbxno)
222 {
223 	return __raw_readl(priv->hecc_ram + HECC_CANMOTS + mbxno * 4);
224 }
225 
226 static inline void hecc_write_mbx(struct ti_hecc_priv *priv, u32 mbxno,
227 				  u32 reg, u32 val)
228 {
229 	__raw_writel(val, priv->mbx + mbxno * 0x10 + reg);
230 }
231 
232 static inline u32 hecc_read_mbx(struct ti_hecc_priv *priv, u32 mbxno, u32 reg)
233 {
234 	return __raw_readl(priv->mbx + mbxno * 0x10 + reg);
235 }
236 
237 static inline void hecc_write(struct ti_hecc_priv *priv, u32 reg, u32 val)
238 {
239 	__raw_writel(val, priv->base + reg);
240 }
241 
242 static inline u32 hecc_read(struct ti_hecc_priv *priv, int reg)
243 {
244 	return __raw_readl(priv->base + reg);
245 }
246 
247 static inline void hecc_set_bit(struct ti_hecc_priv *priv, int reg,
248 				u32 bit_mask)
249 {
250 	hecc_write(priv, reg, hecc_read(priv, reg) | bit_mask);
251 }
252 
253 static inline void hecc_clear_bit(struct ti_hecc_priv *priv, int reg,
254 				  u32 bit_mask)
255 {
256 	hecc_write(priv, reg, hecc_read(priv, reg) & ~bit_mask);
257 }
258 
259 static inline u32 hecc_get_bit(struct ti_hecc_priv *priv, int reg, u32 bit_mask)
260 {
261 	return (hecc_read(priv, reg) & bit_mask) ? 1 : 0;
262 }
263 
264 static int ti_hecc_set_btc(struct ti_hecc_priv *priv)
265 {
266 	struct can_bittiming *bit_timing = &priv->can.bittiming;
267 	u32 can_btc;
268 
269 	can_btc = (bit_timing->phase_seg2 - 1) & 0x7;
270 	can_btc |= ((bit_timing->phase_seg1 + bit_timing->prop_seg - 1)
271 			& 0xF) << 3;
272 	if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) {
273 		if (bit_timing->brp > 4)
274 			can_btc |= HECC_CANBTC_SAM;
275 		else
276 			netdev_warn(priv->ndev,
277 				    "WARN: Triple sampling not set due to h/w limitations");
278 	}
279 	can_btc |= ((bit_timing->sjw - 1) & 0x3) << 8;
280 	can_btc |= ((bit_timing->brp - 1) & 0xFF) << 16;
281 
282 	/* ERM being set to 0 by default meaning resync at falling edge */
283 
284 	hecc_write(priv, HECC_CANBTC, can_btc);
285 	netdev_info(priv->ndev, "setting CANBTC=%#x\n", can_btc);
286 
287 	return 0;
288 }
289 
290 static int ti_hecc_transceiver_switch(const struct ti_hecc_priv *priv,
291 				      int on)
292 {
293 	if (!priv->reg_xceiver)
294 		return 0;
295 
296 	if (on)
297 		return regulator_enable(priv->reg_xceiver);
298 	else
299 		return regulator_disable(priv->reg_xceiver);
300 }
301 
302 static void ti_hecc_reset(struct net_device *ndev)
303 {
304 	u32 cnt;
305 	struct ti_hecc_priv *priv = netdev_priv(ndev);
306 
307 	netdev_dbg(ndev, "resetting hecc ...\n");
308 	hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_SRES);
309 
310 	/* Set change control request and wait till enabled */
311 	hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
312 
313 	/* INFO: It has been observed that at times CCE bit may not be
314 	 * set and hw seems to be ok even if this bit is not set so
315 	 * timing out with a timing of 1ms to respect the specs
316 	 */
317 	cnt = HECC_CCE_WAIT_COUNT;
318 	while (!hecc_get_bit(priv, HECC_CANES, HECC_CANES_CCE) && cnt != 0) {
319 		--cnt;
320 		udelay(10);
321 	}
322 
323 	/* Note: On HECC, BTC can be programmed only in initialization mode, so
324 	 * it is expected that the can bittiming parameters are set via ip
325 	 * utility before the device is opened
326 	 */
327 	ti_hecc_set_btc(priv);
328 
329 	/* Clear CCR (and CANMC register) and wait for CCE = 0 enable */
330 	hecc_write(priv, HECC_CANMC, 0);
331 
332 	/* INFO: CAN net stack handles bus off and hence disabling auto-bus-on
333 	 * hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_ABO);
334 	 */
335 
336 	/* INFO: It has been observed that at times CCE bit may not be
337 	 * set and hw seems to be ok even if this bit is not set so
338 	 */
339 	cnt = HECC_CCE_WAIT_COUNT;
340 	while (hecc_get_bit(priv, HECC_CANES, HECC_CANES_CCE) && cnt != 0) {
341 		--cnt;
342 		udelay(10);
343 	}
344 
345 	/* Enable TX and RX I/O Control pins */
346 	hecc_write(priv, HECC_CANTIOC, HECC_CANTIOC_EN);
347 	hecc_write(priv, HECC_CANRIOC, HECC_CANRIOC_EN);
348 
349 	/* Clear registers for clean operation */
350 	hecc_write(priv, HECC_CANTA, HECC_SET_REG);
351 	hecc_write(priv, HECC_CANRMP, HECC_SET_REG);
352 	hecc_write(priv, HECC_CANGIF0, HECC_SET_REG);
353 	hecc_write(priv, HECC_CANGIF1, HECC_SET_REG);
354 	hecc_write(priv, HECC_CANME, 0);
355 	hecc_write(priv, HECC_CANMD, 0);
356 
357 	/* SCC compat mode NOT supported (and not needed too) */
358 	hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_SCM);
359 }
360 
361 static void ti_hecc_start(struct net_device *ndev)
362 {
363 	struct ti_hecc_priv *priv = netdev_priv(ndev);
364 	u32 cnt, mbxno, mbx_mask;
365 
366 	/* put HECC in initialization mode and set btc */
367 	ti_hecc_reset(ndev);
368 
369 	priv->tx_head = HECC_TX_MASK;
370 	priv->tx_tail = HECC_TX_MASK;
371 
372 	/* Enable local and global acceptance mask registers */
373 	hecc_write(priv, HECC_CANGAM, HECC_SET_REG);
374 
375 	/* Prepare configured mailboxes to receive messages */
376 	for (cnt = 0; cnt < HECC_MAX_RX_MBOX; cnt++) {
377 		mbxno = HECC_MAX_MAILBOXES - 1 - cnt;
378 		mbx_mask = BIT(mbxno);
379 		hecc_clear_bit(priv, HECC_CANME, mbx_mask);
380 		hecc_write_mbx(priv, mbxno, HECC_CANMID, HECC_CANMID_AME);
381 		hecc_write_lam(priv, mbxno, HECC_SET_REG);
382 		hecc_set_bit(priv, HECC_CANMD, mbx_mask);
383 		hecc_set_bit(priv, HECC_CANME, mbx_mask);
384 		hecc_set_bit(priv, HECC_CANMIM, mbx_mask);
385 	}
386 
387 	/* Enable tx interrupts */
388 	hecc_set_bit(priv, HECC_CANMIM, BIT(HECC_MAX_TX_MBOX) - 1);
389 
390 	/* Prevent message over-write to create a rx fifo, but not for
391 	 * the lowest priority mailbox, since that allows detecting
392 	 * overflows instead of the hardware silently dropping the
393 	 * messages.
394 	 */
395 	mbx_mask = ~BIT(HECC_RX_LAST_MBOX);
396 	hecc_write(priv, HECC_CANOPC, mbx_mask);
397 
398 	/* Enable interrupts */
399 	if (priv->use_hecc1int) {
400 		hecc_write(priv, HECC_CANMIL, HECC_SET_REG);
401 		hecc_write(priv, HECC_CANGIM, HECC_CANGIM_DEF_MASK |
402 			HECC_CANGIM_I1EN | HECC_CANGIM_SIL);
403 	} else {
404 		hecc_write(priv, HECC_CANMIL, 0);
405 		hecc_write(priv, HECC_CANGIM,
406 			   HECC_CANGIM_DEF_MASK | HECC_CANGIM_I0EN);
407 	}
408 	priv->can.state = CAN_STATE_ERROR_ACTIVE;
409 }
410 
411 static void ti_hecc_stop(struct net_device *ndev)
412 {
413 	struct ti_hecc_priv *priv = netdev_priv(ndev);
414 
415 	/* Disable the CPK; stop sending, erroring and acking */
416 	hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
417 
418 	/* Disable interrupts and disable mailboxes */
419 	hecc_write(priv, HECC_CANGIM, 0);
420 	hecc_write(priv, HECC_CANMIM, 0);
421 	hecc_write(priv, HECC_CANME, 0);
422 	priv->can.state = CAN_STATE_STOPPED;
423 }
424 
425 static int ti_hecc_do_set_mode(struct net_device *ndev, enum can_mode mode)
426 {
427 	int ret = 0;
428 
429 	switch (mode) {
430 	case CAN_MODE_START:
431 		ti_hecc_start(ndev);
432 		netif_wake_queue(ndev);
433 		break;
434 	default:
435 		ret = -EOPNOTSUPP;
436 		break;
437 	}
438 
439 	return ret;
440 }
441 
442 static int ti_hecc_get_berr_counter(const struct net_device *ndev,
443 				    struct can_berr_counter *bec)
444 {
445 	struct ti_hecc_priv *priv = netdev_priv(ndev);
446 
447 	bec->txerr = hecc_read(priv, HECC_CANTEC);
448 	bec->rxerr = hecc_read(priv, HECC_CANREC);
449 
450 	return 0;
451 }
452 
453 /* ti_hecc_xmit: HECC Transmit
454  *
455  * The transmit mailboxes start from 0 to HECC_MAX_TX_MBOX. In HECC the
456  * priority of the mailbox for transmission is dependent upon priority setting
457  * field in mailbox registers. The mailbox with highest value in priority field
458  * is transmitted first. Only when two mailboxes have the same value in
459  * priority field the highest numbered mailbox is transmitted first.
460  *
461  * To utilize the HECC priority feature as described above we start with the
462  * highest numbered mailbox with highest priority level and move on to the next
463  * mailbox with the same priority level and so on. Once we loop through all the
464  * transmit mailboxes we choose the next priority level (lower) and so on
465  * until we reach the lowest priority level on the lowest numbered mailbox
466  * when we stop transmission until all mailboxes are transmitted and then
467  * restart at highest numbered mailbox with highest priority.
468  *
469  * Two counters (head and tail) are used to track the next mailbox to transmit
470  * and to track the echo buffer for already transmitted mailbox. The queue
471  * is stopped when all the mailboxes are busy or when there is a priority
472  * value roll-over happens.
473  */
474 static netdev_tx_t ti_hecc_xmit(struct sk_buff *skb, struct net_device *ndev)
475 {
476 	struct ti_hecc_priv *priv = netdev_priv(ndev);
477 	struct can_frame *cf = (struct can_frame *)skb->data;
478 	u32 mbxno, mbx_mask, data;
479 	unsigned long flags;
480 
481 	if (can_dropped_invalid_skb(ndev, skb))
482 		return NETDEV_TX_OK;
483 
484 	mbxno = get_tx_head_mb(priv);
485 	mbx_mask = BIT(mbxno);
486 	spin_lock_irqsave(&priv->mbx_lock, flags);
487 	if (unlikely(hecc_read(priv, HECC_CANME) & mbx_mask)) {
488 		spin_unlock_irqrestore(&priv->mbx_lock, flags);
489 		netif_stop_queue(ndev);
490 		netdev_err(priv->ndev,
491 			   "BUG: TX mbx not ready tx_head=%08X, tx_tail=%08X\n",
492 			   priv->tx_head, priv->tx_tail);
493 		return NETDEV_TX_BUSY;
494 	}
495 	spin_unlock_irqrestore(&priv->mbx_lock, flags);
496 
497 	/* Prepare mailbox for transmission */
498 	data = cf->len | (get_tx_head_prio(priv) << 8);
499 	if (cf->can_id & CAN_RTR_FLAG) /* Remote transmission request */
500 		data |= HECC_CANMCF_RTR;
501 	hecc_write_mbx(priv, mbxno, HECC_CANMCF, data);
502 
503 	if (cf->can_id & CAN_EFF_FLAG) /* Extended frame format */
504 		data = (cf->can_id & CAN_EFF_MASK) | HECC_CANMID_IDE;
505 	else /* Standard frame format */
506 		data = (cf->can_id & CAN_SFF_MASK) << 18;
507 	hecc_write_mbx(priv, mbxno, HECC_CANMID, data);
508 	hecc_write_mbx(priv, mbxno, HECC_CANMDL,
509 		       be32_to_cpu(*(__be32 *)(cf->data)));
510 	if (cf->len > 4)
511 		hecc_write_mbx(priv, mbxno, HECC_CANMDH,
512 			       be32_to_cpu(*(__be32 *)(cf->data + 4)));
513 	else
514 		*(u32 *)(cf->data + 4) = 0;
515 	can_put_echo_skb(skb, ndev, mbxno, 0);
516 
517 	spin_lock_irqsave(&priv->mbx_lock, flags);
518 	--priv->tx_head;
519 	if ((hecc_read(priv, HECC_CANME) & BIT(get_tx_head_mb(priv))) ||
520 	    (priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK) {
521 		netif_stop_queue(ndev);
522 	}
523 	hecc_set_bit(priv, HECC_CANME, mbx_mask);
524 	spin_unlock_irqrestore(&priv->mbx_lock, flags);
525 
526 	hecc_write(priv, HECC_CANTRS, mbx_mask);
527 
528 	return NETDEV_TX_OK;
529 }
530 
531 static inline
532 struct ti_hecc_priv *rx_offload_to_priv(struct can_rx_offload *offload)
533 {
534 	return container_of(offload, struct ti_hecc_priv, offload);
535 }
536 
537 static struct sk_buff *ti_hecc_mailbox_read(struct can_rx_offload *offload,
538 					    unsigned int mbxno, u32 *timestamp,
539 					    bool drop)
540 {
541 	struct ti_hecc_priv *priv = rx_offload_to_priv(offload);
542 	struct sk_buff *skb;
543 	struct can_frame *cf;
544 	u32 data, mbx_mask;
545 
546 	mbx_mask = BIT(mbxno);
547 
548 	if (unlikely(drop)) {
549 		skb = ERR_PTR(-ENOBUFS);
550 		goto mark_as_read;
551 	}
552 
553 	skb = alloc_can_skb(offload->dev, &cf);
554 	if (unlikely(!skb)) {
555 		skb = ERR_PTR(-ENOMEM);
556 		goto mark_as_read;
557 	}
558 
559 	data = hecc_read_mbx(priv, mbxno, HECC_CANMID);
560 	if (data & HECC_CANMID_IDE)
561 		cf->can_id = (data & CAN_EFF_MASK) | CAN_EFF_FLAG;
562 	else
563 		cf->can_id = (data >> 18) & CAN_SFF_MASK;
564 
565 	data = hecc_read_mbx(priv, mbxno, HECC_CANMCF);
566 	if (data & HECC_CANMCF_RTR)
567 		cf->can_id |= CAN_RTR_FLAG;
568 	cf->len = can_cc_dlc2len(data & 0xF);
569 
570 	data = hecc_read_mbx(priv, mbxno, HECC_CANMDL);
571 	*(__be32 *)(cf->data) = cpu_to_be32(data);
572 	if (cf->len > 4) {
573 		data = hecc_read_mbx(priv, mbxno, HECC_CANMDH);
574 		*(__be32 *)(cf->data + 4) = cpu_to_be32(data);
575 	}
576 
577 	*timestamp = hecc_read_stamp(priv, mbxno);
578 
579 	/* Check for FIFO overrun.
580 	 *
581 	 * All but the last RX mailbox have activated overwrite
582 	 * protection. So skip check for overrun, if we're not
583 	 * handling the last RX mailbox.
584 	 *
585 	 * As the overwrite protection for the last RX mailbox is
586 	 * disabled, the CAN core might update while we're reading
587 	 * it. This means the skb might be inconsistent.
588 	 *
589 	 * Return an error to let rx-offload discard this CAN frame.
590 	 */
591 	if (unlikely(mbxno == HECC_RX_LAST_MBOX &&
592 		     hecc_read(priv, HECC_CANRML) & mbx_mask))
593 		skb = ERR_PTR(-ENOBUFS);
594 
595  mark_as_read:
596 	hecc_write(priv, HECC_CANRMP, mbx_mask);
597 
598 	return skb;
599 }
600 
601 static int ti_hecc_error(struct net_device *ndev, int int_status,
602 			 int err_status)
603 {
604 	struct ti_hecc_priv *priv = netdev_priv(ndev);
605 	struct can_frame *cf;
606 	struct sk_buff *skb;
607 	u32 timestamp;
608 	int err;
609 
610 	if (err_status & HECC_BUS_ERROR) {
611 		/* propagate the error condition to the can stack */
612 		skb = alloc_can_err_skb(ndev, &cf);
613 		if (!skb) {
614 			if (net_ratelimit())
615 				netdev_err(priv->ndev,
616 					   "%s: alloc_can_err_skb() failed\n",
617 					   __func__);
618 			return -ENOMEM;
619 		}
620 
621 		++priv->can.can_stats.bus_error;
622 		cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
623 		if (err_status & HECC_CANES_FE)
624 			cf->data[2] |= CAN_ERR_PROT_FORM;
625 		if (err_status & HECC_CANES_BE)
626 			cf->data[2] |= CAN_ERR_PROT_BIT;
627 		if (err_status & HECC_CANES_SE)
628 			cf->data[2] |= CAN_ERR_PROT_STUFF;
629 		if (err_status & HECC_CANES_CRCE)
630 			cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
631 		if (err_status & HECC_CANES_ACKE)
632 			cf->data[3] = CAN_ERR_PROT_LOC_ACK;
633 
634 		timestamp = hecc_read(priv, HECC_CANLNT);
635 		err = can_rx_offload_queue_timestamp(&priv->offload, skb,
636 						  timestamp);
637 		if (err)
638 			ndev->stats.rx_fifo_errors++;
639 	}
640 
641 	hecc_write(priv, HECC_CANES, HECC_CANES_FLAGS);
642 
643 	return 0;
644 }
645 
646 static void ti_hecc_change_state(struct net_device *ndev,
647 				 enum can_state rx_state,
648 				 enum can_state tx_state)
649 {
650 	struct ti_hecc_priv *priv = netdev_priv(ndev);
651 	struct can_frame *cf;
652 	struct sk_buff *skb;
653 	u32 timestamp;
654 	int err;
655 
656 	skb = alloc_can_err_skb(priv->ndev, &cf);
657 	if (unlikely(!skb)) {
658 		priv->can.state = max(tx_state, rx_state);
659 		return;
660 	}
661 
662 	can_change_state(priv->ndev, cf, tx_state, rx_state);
663 
664 	if (max(tx_state, rx_state) != CAN_STATE_BUS_OFF) {
665 		cf->can_id |= CAN_ERR_CNT;
666 		cf->data[6] = hecc_read(priv, HECC_CANTEC);
667 		cf->data[7] = hecc_read(priv, HECC_CANREC);
668 	}
669 
670 	timestamp = hecc_read(priv, HECC_CANLNT);
671 	err = can_rx_offload_queue_timestamp(&priv->offload, skb, timestamp);
672 	if (err)
673 		ndev->stats.rx_fifo_errors++;
674 }
675 
676 static irqreturn_t ti_hecc_interrupt(int irq, void *dev_id)
677 {
678 	struct net_device *ndev = (struct net_device *)dev_id;
679 	struct ti_hecc_priv *priv = netdev_priv(ndev);
680 	struct net_device_stats *stats = &ndev->stats;
681 	u32 mbxno, mbx_mask, int_status, err_status, stamp;
682 	unsigned long flags, rx_pending;
683 	u32 handled = 0;
684 
685 	int_status = hecc_read(priv,
686 			       priv->use_hecc1int ?
687 			       HECC_CANGIF1 : HECC_CANGIF0);
688 
689 	if (!int_status)
690 		return IRQ_NONE;
691 
692 	err_status = hecc_read(priv, HECC_CANES);
693 	if (unlikely(err_status & HECC_CANES_FLAGS))
694 		ti_hecc_error(ndev, int_status, err_status);
695 
696 	if (unlikely(int_status & HECC_CANGIM_DEF_MASK)) {
697 		enum can_state rx_state, tx_state;
698 		u32 rec = hecc_read(priv, HECC_CANREC);
699 		u32 tec = hecc_read(priv, HECC_CANTEC);
700 
701 		if (int_status & HECC_CANGIF_WLIF) {
702 			handled |= HECC_CANGIF_WLIF;
703 			rx_state = rec >= tec ? CAN_STATE_ERROR_WARNING : 0;
704 			tx_state = rec <= tec ? CAN_STATE_ERROR_WARNING : 0;
705 			netdev_dbg(priv->ndev, "Error Warning interrupt\n");
706 			ti_hecc_change_state(ndev, rx_state, tx_state);
707 		}
708 
709 		if (int_status & HECC_CANGIF_EPIF) {
710 			handled |= HECC_CANGIF_EPIF;
711 			rx_state = rec >= tec ? CAN_STATE_ERROR_PASSIVE : 0;
712 			tx_state = rec <= tec ? CAN_STATE_ERROR_PASSIVE : 0;
713 			netdev_dbg(priv->ndev, "Error passive interrupt\n");
714 			ti_hecc_change_state(ndev, rx_state, tx_state);
715 		}
716 
717 		if (int_status & HECC_CANGIF_BOIF) {
718 			handled |= HECC_CANGIF_BOIF;
719 			rx_state = CAN_STATE_BUS_OFF;
720 			tx_state = CAN_STATE_BUS_OFF;
721 			netdev_dbg(priv->ndev, "Bus off interrupt\n");
722 
723 			/* Disable all interrupts */
724 			hecc_write(priv, HECC_CANGIM, 0);
725 			can_bus_off(ndev);
726 			ti_hecc_change_state(ndev, rx_state, tx_state);
727 		}
728 	} else if (unlikely(priv->can.state != CAN_STATE_ERROR_ACTIVE)) {
729 		enum can_state new_state, tx_state, rx_state;
730 		u32 rec = hecc_read(priv, HECC_CANREC);
731 		u32 tec = hecc_read(priv, HECC_CANTEC);
732 
733 		if (rec >= 128 || tec >= 128)
734 			new_state = CAN_STATE_ERROR_PASSIVE;
735 		else if (rec >= 96 || tec >= 96)
736 			new_state = CAN_STATE_ERROR_WARNING;
737 		else
738 			new_state = CAN_STATE_ERROR_ACTIVE;
739 
740 		if (new_state < priv->can.state) {
741 			rx_state = rec >= tec ? new_state : 0;
742 			tx_state = rec <= tec ? new_state : 0;
743 			ti_hecc_change_state(ndev, rx_state, tx_state);
744 		}
745 	}
746 
747 	if (int_status & HECC_CANGIF_GMIF) {
748 		while (priv->tx_tail - priv->tx_head > 0) {
749 			mbxno = get_tx_tail_mb(priv);
750 			mbx_mask = BIT(mbxno);
751 			if (!(mbx_mask & hecc_read(priv, HECC_CANTA)))
752 				break;
753 			hecc_write(priv, HECC_CANTA, mbx_mask);
754 			spin_lock_irqsave(&priv->mbx_lock, flags);
755 			hecc_clear_bit(priv, HECC_CANME, mbx_mask);
756 			spin_unlock_irqrestore(&priv->mbx_lock, flags);
757 			stamp = hecc_read_stamp(priv, mbxno);
758 			stats->tx_bytes +=
759 				can_rx_offload_get_echo_skb(&priv->offload,
760 							    mbxno, stamp, NULL);
761 			stats->tx_packets++;
762 			--priv->tx_tail;
763 		}
764 
765 		/* restart queue if wrap-up or if queue stalled on last pkt */
766 		if ((priv->tx_head == priv->tx_tail &&
767 		     ((priv->tx_head & HECC_TX_MASK) != HECC_TX_MASK)) ||
768 		    (((priv->tx_tail & HECC_TX_MASK) == HECC_TX_MASK) &&
769 		     ((priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK)))
770 			netif_wake_queue(ndev);
771 
772 		/* offload RX mailboxes and let NAPI deliver them */
773 		while ((rx_pending = hecc_read(priv, HECC_CANRMP))) {
774 			can_rx_offload_irq_offload_timestamp(&priv->offload,
775 							     rx_pending);
776 		}
777 	}
778 
779 	/* clear all interrupt conditions - read back to avoid spurious ints */
780 	if (priv->use_hecc1int) {
781 		hecc_write(priv, HECC_CANGIF1, handled);
782 		int_status = hecc_read(priv, HECC_CANGIF1);
783 	} else {
784 		hecc_write(priv, HECC_CANGIF0, handled);
785 		int_status = hecc_read(priv, HECC_CANGIF0);
786 	}
787 
788 	can_rx_offload_irq_finish(&priv->offload);
789 
790 	return IRQ_HANDLED;
791 }
792 
793 static int ti_hecc_open(struct net_device *ndev)
794 {
795 	struct ti_hecc_priv *priv = netdev_priv(ndev);
796 	int err;
797 
798 	err = request_irq(ndev->irq, ti_hecc_interrupt, IRQF_SHARED,
799 			  ndev->name, ndev);
800 	if (err) {
801 		netdev_err(ndev, "error requesting interrupt\n");
802 		return err;
803 	}
804 
805 	ti_hecc_transceiver_switch(priv, 1);
806 
807 	/* Open common can device */
808 	err = open_candev(ndev);
809 	if (err) {
810 		netdev_err(ndev, "open_candev() failed %d\n", err);
811 		ti_hecc_transceiver_switch(priv, 0);
812 		free_irq(ndev->irq, ndev);
813 		return err;
814 	}
815 
816 	ti_hecc_start(ndev);
817 	can_rx_offload_enable(&priv->offload);
818 	netif_start_queue(ndev);
819 
820 	return 0;
821 }
822 
823 static int ti_hecc_close(struct net_device *ndev)
824 {
825 	struct ti_hecc_priv *priv = netdev_priv(ndev);
826 
827 	netif_stop_queue(ndev);
828 	can_rx_offload_disable(&priv->offload);
829 	ti_hecc_stop(ndev);
830 	free_irq(ndev->irq, ndev);
831 	close_candev(ndev);
832 	ti_hecc_transceiver_switch(priv, 0);
833 
834 	return 0;
835 }
836 
837 static const struct net_device_ops ti_hecc_netdev_ops = {
838 	.ndo_open		= ti_hecc_open,
839 	.ndo_stop		= ti_hecc_close,
840 	.ndo_start_xmit		= ti_hecc_xmit,
841 	.ndo_change_mtu		= can_change_mtu,
842 };
843 
844 static const struct of_device_id ti_hecc_dt_ids[] = {
845 	{
846 		.compatible = "ti,am3517-hecc",
847 	},
848 	{ }
849 };
850 MODULE_DEVICE_TABLE(of, ti_hecc_dt_ids);
851 
852 static int ti_hecc_probe(struct platform_device *pdev)
853 {
854 	struct net_device *ndev = (struct net_device *)0;
855 	struct ti_hecc_priv *priv;
856 	struct device_node *np = pdev->dev.of_node;
857 	struct regulator *reg_xceiver;
858 	int err = -ENODEV;
859 
860 	if (!IS_ENABLED(CONFIG_OF) || !np)
861 		return -EINVAL;
862 
863 	reg_xceiver = devm_regulator_get(&pdev->dev, "xceiver");
864 	if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
865 		return -EPROBE_DEFER;
866 	else if (IS_ERR(reg_xceiver))
867 		reg_xceiver = NULL;
868 
869 	ndev = alloc_candev(sizeof(struct ti_hecc_priv), HECC_MAX_TX_MBOX);
870 	if (!ndev) {
871 		dev_err(&pdev->dev, "alloc_candev failed\n");
872 		return -ENOMEM;
873 	}
874 	priv = netdev_priv(ndev);
875 
876 	/* handle hecc memory */
877 	priv->base = devm_platform_ioremap_resource_byname(pdev, "hecc");
878 	if (IS_ERR(priv->base)) {
879 		dev_err(&pdev->dev, "hecc ioremap failed\n");
880 		err = PTR_ERR(priv->base);
881 		goto probe_exit_candev;
882 	}
883 
884 	/* handle hecc-ram memory */
885 	priv->hecc_ram = devm_platform_ioremap_resource_byname(pdev,
886 							       "hecc-ram");
887 	if (IS_ERR(priv->hecc_ram)) {
888 		dev_err(&pdev->dev, "hecc-ram ioremap failed\n");
889 		err = PTR_ERR(priv->hecc_ram);
890 		goto probe_exit_candev;
891 	}
892 
893 	/* handle mbx memory */
894 	priv->mbx = devm_platform_ioremap_resource_byname(pdev, "mbx");
895 	if (IS_ERR(priv->mbx)) {
896 		dev_err(&pdev->dev, "mbx ioremap failed\n");
897 		err = PTR_ERR(priv->mbx);
898 		goto probe_exit_candev;
899 	}
900 
901 	ndev->irq = platform_get_irq(pdev, 0);
902 	if (ndev->irq < 0) {
903 		err = ndev->irq;
904 		goto probe_exit_candev;
905 	}
906 
907 	priv->ndev = ndev;
908 	priv->reg_xceiver = reg_xceiver;
909 	priv->use_hecc1int = of_property_read_bool(np, "ti,use-hecc1int");
910 
911 	priv->can.bittiming_const = &ti_hecc_bittiming_const;
912 	priv->can.do_set_mode = ti_hecc_do_set_mode;
913 	priv->can.do_get_berr_counter = ti_hecc_get_berr_counter;
914 	priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
915 
916 	spin_lock_init(&priv->mbx_lock);
917 	ndev->flags |= IFF_ECHO;
918 	platform_set_drvdata(pdev, ndev);
919 	SET_NETDEV_DEV(ndev, &pdev->dev);
920 	ndev->netdev_ops = &ti_hecc_netdev_ops;
921 
922 	priv->clk = clk_get(&pdev->dev, "hecc_ck");
923 	if (IS_ERR(priv->clk)) {
924 		dev_err(&pdev->dev, "No clock available\n");
925 		err = PTR_ERR(priv->clk);
926 		priv->clk = NULL;
927 		goto probe_exit_candev;
928 	}
929 	priv->can.clock.freq = clk_get_rate(priv->clk);
930 
931 	err = clk_prepare_enable(priv->clk);
932 	if (err) {
933 		dev_err(&pdev->dev, "clk_prepare_enable() failed\n");
934 		goto probe_exit_release_clk;
935 	}
936 
937 	priv->offload.mailbox_read = ti_hecc_mailbox_read;
938 	priv->offload.mb_first = HECC_RX_FIRST_MBOX;
939 	priv->offload.mb_last = HECC_RX_LAST_MBOX;
940 	err = can_rx_offload_add_timestamp(ndev, &priv->offload);
941 	if (err) {
942 		dev_err(&pdev->dev, "can_rx_offload_add_timestamp() failed\n");
943 		goto probe_exit_disable_clk;
944 	}
945 
946 	err = register_candev(ndev);
947 	if (err) {
948 		dev_err(&pdev->dev, "register_candev() failed\n");
949 		goto probe_exit_offload;
950 	}
951 
952 	dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%u)\n",
953 		 priv->base, (u32)ndev->irq);
954 
955 	return 0;
956 
957 probe_exit_offload:
958 	can_rx_offload_del(&priv->offload);
959 probe_exit_disable_clk:
960 	clk_disable_unprepare(priv->clk);
961 probe_exit_release_clk:
962 	clk_put(priv->clk);
963 probe_exit_candev:
964 	free_candev(ndev);
965 
966 	return err;
967 }
968 
969 static int ti_hecc_remove(struct platform_device *pdev)
970 {
971 	struct net_device *ndev = platform_get_drvdata(pdev);
972 	struct ti_hecc_priv *priv = netdev_priv(ndev);
973 
974 	unregister_candev(ndev);
975 	clk_disable_unprepare(priv->clk);
976 	clk_put(priv->clk);
977 	can_rx_offload_del(&priv->offload);
978 	free_candev(ndev);
979 
980 	return 0;
981 }
982 
983 #ifdef CONFIG_PM
984 static int ti_hecc_suspend(struct platform_device *pdev, pm_message_t state)
985 {
986 	struct net_device *dev = platform_get_drvdata(pdev);
987 	struct ti_hecc_priv *priv = netdev_priv(dev);
988 
989 	if (netif_running(dev)) {
990 		netif_stop_queue(dev);
991 		netif_device_detach(dev);
992 	}
993 
994 	hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_PDR);
995 	priv->can.state = CAN_STATE_SLEEPING;
996 
997 	clk_disable_unprepare(priv->clk);
998 
999 	return 0;
1000 }
1001 
1002 static int ti_hecc_resume(struct platform_device *pdev)
1003 {
1004 	struct net_device *dev = platform_get_drvdata(pdev);
1005 	struct ti_hecc_priv *priv = netdev_priv(dev);
1006 	int err;
1007 
1008 	err = clk_prepare_enable(priv->clk);
1009 	if (err)
1010 		return err;
1011 
1012 	hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_PDR);
1013 	priv->can.state = CAN_STATE_ERROR_ACTIVE;
1014 
1015 	if (netif_running(dev)) {
1016 		netif_device_attach(dev);
1017 		netif_start_queue(dev);
1018 	}
1019 
1020 	return 0;
1021 }
1022 #else
1023 #define ti_hecc_suspend NULL
1024 #define ti_hecc_resume NULL
1025 #endif
1026 
1027 /* TI HECC netdevice driver: platform driver structure */
1028 static struct platform_driver ti_hecc_driver = {
1029 	.driver = {
1030 		.name    = DRV_NAME,
1031 		.of_match_table = ti_hecc_dt_ids,
1032 	},
1033 	.probe = ti_hecc_probe,
1034 	.remove = ti_hecc_remove,
1035 	.suspend = ti_hecc_suspend,
1036 	.resume = ti_hecc_resume,
1037 };
1038 
1039 module_platform_driver(ti_hecc_driver);
1040 
1041 MODULE_AUTHOR("Anant Gole <anantgole@ti.com>");
1042 MODULE_LICENSE("GPL v2");
1043 MODULE_DESCRIPTION(DRV_DESC);
1044 MODULE_ALIAS("platform:" DRV_NAME);
1045