2 * TI HECC (CAN) device driver
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>
7 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
8 * Copyright (C) 2019 Jeroen Hofstee <jhofstee@victronenergy.com>
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.
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.
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>
32 #include <linux/of_device.h>
33 #include <linux/regulator/consumer.h>
35 #include <linux/can/dev.h>
36 #include <linux/can/error.h>
37 #include <linux/can/led.h>
38 #include <linux/can/rx-offload.h>
40 #define DRV_NAME "ti_hecc"
41 #define HECC_MODULE_VERSION "0.7"
42 MODULE_VERSION(HECC_MODULE_VERSION);
43 #define DRV_DESC "TI High End CAN Controller Driver " HECC_MODULE_VERSION
45 /* TX / RX Mailbox Configuration */
46 #define HECC_MAX_MAILBOXES 32 /* hardware mailboxes - do not change */
47 #define MAX_TX_PRIO 0x3F /* hardware value - do not change */
49 /* Important Note: TX mailbox configuration
50 * TX mailboxes should be restricted to the number of SKB buffers to avoid
51 * maintaining SKB buffers separately. TX mailboxes should be a power of 2
52 * for the mailbox logic to work. Top mailbox numbers are reserved for RX
53 * and lower mailboxes for TX.
55 * HECC_MAX_TX_MBOX HECC_MB_TX_SHIFT
60 #define HECC_MB_TX_SHIFT 2 /* as per table above */
61 #define HECC_MAX_TX_MBOX BIT(HECC_MB_TX_SHIFT)
63 #define HECC_TX_PRIO_SHIFT (HECC_MB_TX_SHIFT)
64 #define HECC_TX_PRIO_MASK (MAX_TX_PRIO << HECC_MB_TX_SHIFT)
65 #define HECC_TX_MB_MASK (HECC_MAX_TX_MBOX - 1)
66 #define HECC_TX_MASK ((HECC_MAX_TX_MBOX - 1) | HECC_TX_PRIO_MASK)
68 /* RX mailbox configuration
70 * The remaining mailboxes are used for reception and are delivered
71 * based on their timestamp, to avoid a hardware race when CANME is
72 * changed while CAN-bus traffic is being received.
74 #define HECC_MAX_RX_MBOX (HECC_MAX_MAILBOXES - HECC_MAX_TX_MBOX)
75 #define HECC_RX_FIRST_MBOX (HECC_MAX_MAILBOXES - 1)
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 /* Remote 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 */
107 /* TI HECC RAM registers */
108 #define HECC_CANMOTS 0x80 /* Message object time stamp */
110 /* Mailbox registers */
111 #define HECC_CANMID 0x0
112 #define HECC_CANMCF 0x4
113 #define HECC_CANMDL 0x8
114 #define HECC_CANMDH 0xC
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 */
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 */
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 */
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 */
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 */
147 #define HECC_CANBTC_SAM BIT(7) /* sample points */
149 #define HECC_BUS_ERROR (HECC_CANES_FE | HECC_CANES_BE |\
150 HECC_CANES_CRCE | HECC_CANES_SE |\
153 #define HECC_CANMCF_RTR BIT(4) /* Remote transmit request */
155 #define HECC_CANGIF_MAIF BIT(17) /* Message alarm interrupt */
156 #define HECC_CANGIF_TCOIF BIT(16) /* Timer counter overflow int */
157 #define HECC_CANGIF_GMIF BIT(15) /* Global mailbox interrupt */
158 #define HECC_CANGIF_AAIF BIT(14) /* Abort ack interrupt */
159 #define HECC_CANGIF_WDIF BIT(13) /* Write denied interrupt */
160 #define HECC_CANGIF_WUIF BIT(12) /* Wake up interrupt */
161 #define HECC_CANGIF_RMLIF BIT(11) /* Receive message lost interrupt */
162 #define HECC_CANGIF_BOIF BIT(10) /* Bus off interrupt */
163 #define HECC_CANGIF_EPIF BIT(9) /* Error passive interrupt */
164 #define HECC_CANGIF_WLIF BIT(8) /* Warning level interrupt */
165 #define HECC_CANGIF_MBOX_MASK 0x1F /* Mailbox number mask */
166 #define HECC_CANGIM_I1EN BIT(1) /* Int line 1 enable */
167 #define HECC_CANGIM_I0EN BIT(0) /* Int line 0 enable */
168 #define HECC_CANGIM_DEF_MASK 0x700 /* only busoff/warning/passive */
169 #define HECC_CANGIM_SIL BIT(2) /* system interrupts to int line 1 */
171 /* CAN Bittiming constants as per HECC specs */
172 static const struct can_bittiming_const ti_hecc_bittiming_const = {
184 struct ti_hecc_priv {
185 struct can_priv can; /* MUST be first member/field */
186 struct can_rx_offload offload;
187 struct net_device *ndev;
190 void __iomem *hecc_ram;
193 spinlock_t mbx_lock; /* CANME register needs protection */
196 struct regulator *reg_xceiver;
199 static inline int get_tx_head_mb(struct ti_hecc_priv *priv)
201 return priv->tx_head & HECC_TX_MB_MASK;
204 static inline int get_tx_tail_mb(struct ti_hecc_priv *priv)
206 return priv->tx_tail & HECC_TX_MB_MASK;
209 static inline int get_tx_head_prio(struct ti_hecc_priv *priv)
211 return (priv->tx_head >> HECC_TX_PRIO_SHIFT) & MAX_TX_PRIO;
214 static inline void hecc_write_lam(struct ti_hecc_priv *priv, u32 mbxno, u32 val)
216 __raw_writel(val, priv->hecc_ram + mbxno * 4);
219 static inline u32 hecc_read_stamp(struct ti_hecc_priv *priv, u32 mbxno)
221 return __raw_readl(priv->hecc_ram + HECC_CANMOTS + mbxno * 4);
224 static inline void hecc_write_mbx(struct ti_hecc_priv *priv, u32 mbxno,
227 __raw_writel(val, priv->mbx + mbxno * 0x10 + reg);
230 static inline u32 hecc_read_mbx(struct ti_hecc_priv *priv, u32 mbxno, u32 reg)
232 return __raw_readl(priv->mbx + mbxno * 0x10 + reg);
235 static inline void hecc_write(struct ti_hecc_priv *priv, u32 reg, u32 val)
237 __raw_writel(val, priv->base + reg);
240 static inline u32 hecc_read(struct ti_hecc_priv *priv, int reg)
242 return __raw_readl(priv->base + reg);
245 static inline void hecc_set_bit(struct ti_hecc_priv *priv, int reg,
248 hecc_write(priv, reg, hecc_read(priv, reg) | bit_mask);
251 static inline void hecc_clear_bit(struct ti_hecc_priv *priv, int reg,
254 hecc_write(priv, reg, hecc_read(priv, reg) & ~bit_mask);
257 static inline u32 hecc_get_bit(struct ti_hecc_priv *priv, int reg, u32 bit_mask)
259 return (hecc_read(priv, reg) & bit_mask) ? 1 : 0;
262 static int ti_hecc_set_btc(struct ti_hecc_priv *priv)
264 struct can_bittiming *bit_timing = &priv->can.bittiming;
267 can_btc = (bit_timing->phase_seg2 - 1) & 0x7;
268 can_btc |= ((bit_timing->phase_seg1 + bit_timing->prop_seg - 1)
270 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) {
271 if (bit_timing->brp > 4)
272 can_btc |= HECC_CANBTC_SAM;
274 netdev_warn(priv->ndev,
275 "WARN: Triple sampling not set due to h/w limitations");
277 can_btc |= ((bit_timing->sjw - 1) & 0x3) << 8;
278 can_btc |= ((bit_timing->brp - 1) & 0xFF) << 16;
280 /* ERM being set to 0 by default meaning resync at falling edge */
282 hecc_write(priv, HECC_CANBTC, can_btc);
283 netdev_info(priv->ndev, "setting CANBTC=%#x\n", can_btc);
288 static int ti_hecc_transceiver_switch(const struct ti_hecc_priv *priv,
291 if (!priv->reg_xceiver)
295 return regulator_enable(priv->reg_xceiver);
297 return regulator_disable(priv->reg_xceiver);
300 static void ti_hecc_reset(struct net_device *ndev)
303 struct ti_hecc_priv *priv = netdev_priv(ndev);
305 netdev_dbg(ndev, "resetting hecc ...\n");
306 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_SRES);
308 /* Set change control request and wait till enabled */
309 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
311 /* INFO: It has been observed that at times CCE bit may not be
312 * set and hw seems to be ok even if this bit is not set so
313 * timing out with a timing of 1ms to respect the specs
315 cnt = HECC_CCE_WAIT_COUNT;
316 while (!hecc_get_bit(priv, HECC_CANES, HECC_CANES_CCE) && cnt != 0) {
321 /* Note: On HECC, BTC can be programmed only in initialization mode, so
322 * it is expected that the can bittiming parameters are set via ip
323 * utility before the device is opened
325 ti_hecc_set_btc(priv);
327 /* Clear CCR (and CANMC register) and wait for CCE = 0 enable */
328 hecc_write(priv, HECC_CANMC, 0);
330 /* INFO: CAN net stack handles bus off and hence disabling auto-bus-on
331 * hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_ABO);
334 /* INFO: It has been observed that at times CCE bit may not be
335 * set and hw seems to be ok even if this bit is not set so
337 cnt = HECC_CCE_WAIT_COUNT;
338 while (hecc_get_bit(priv, HECC_CANES, HECC_CANES_CCE) && cnt != 0) {
343 /* Enable TX and RX I/O Control pins */
344 hecc_write(priv, HECC_CANTIOC, HECC_CANTIOC_EN);
345 hecc_write(priv, HECC_CANRIOC, HECC_CANRIOC_EN);
347 /* Clear registers for clean operation */
348 hecc_write(priv, HECC_CANTA, HECC_SET_REG);
349 hecc_write(priv, HECC_CANRMP, HECC_SET_REG);
350 hecc_write(priv, HECC_CANGIF0, HECC_SET_REG);
351 hecc_write(priv, HECC_CANGIF1, HECC_SET_REG);
352 hecc_write(priv, HECC_CANME, 0);
353 hecc_write(priv, HECC_CANMD, 0);
355 /* SCC compat mode NOT supported (and not needed too) */
356 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_SCM);
359 static void ti_hecc_start(struct net_device *ndev)
361 struct ti_hecc_priv *priv = netdev_priv(ndev);
362 u32 cnt, mbxno, mbx_mask;
364 /* put HECC in initialization mode and set btc */
367 priv->tx_head = HECC_TX_MASK;
368 priv->tx_tail = HECC_TX_MASK;
370 /* Enable local and global acceptance mask registers */
371 hecc_write(priv, HECC_CANGAM, HECC_SET_REG);
373 /* Prepare configured mailboxes to receive messages */
374 for (cnt = 0; cnt < HECC_MAX_RX_MBOX; cnt++) {
375 mbxno = HECC_MAX_MAILBOXES - 1 - cnt;
376 mbx_mask = BIT(mbxno);
377 hecc_clear_bit(priv, HECC_CANME, mbx_mask);
378 hecc_write_mbx(priv, mbxno, HECC_CANMID, HECC_CANMID_AME);
379 hecc_write_lam(priv, mbxno, HECC_SET_REG);
380 hecc_set_bit(priv, HECC_CANMD, mbx_mask);
381 hecc_set_bit(priv, HECC_CANME, mbx_mask);
382 hecc_set_bit(priv, HECC_CANMIM, mbx_mask);
385 /* Prevent message over-write & Enable interrupts */
386 hecc_write(priv, HECC_CANOPC, HECC_SET_REG);
387 if (priv->use_hecc1int) {
388 hecc_write(priv, HECC_CANMIL, HECC_SET_REG);
389 hecc_write(priv, HECC_CANGIM, HECC_CANGIM_DEF_MASK |
390 HECC_CANGIM_I1EN | HECC_CANGIM_SIL);
392 hecc_write(priv, HECC_CANMIL, 0);
393 hecc_write(priv, HECC_CANGIM,
394 HECC_CANGIM_DEF_MASK | HECC_CANGIM_I0EN);
396 priv->can.state = CAN_STATE_ERROR_ACTIVE;
399 static void ti_hecc_stop(struct net_device *ndev)
401 struct ti_hecc_priv *priv = netdev_priv(ndev);
403 /* Disable interrupts and disable mailboxes */
404 hecc_write(priv, HECC_CANGIM, 0);
405 hecc_write(priv, HECC_CANMIM, 0);
406 hecc_write(priv, HECC_CANME, 0);
407 priv->can.state = CAN_STATE_STOPPED;
410 static int ti_hecc_do_set_mode(struct net_device *ndev, enum can_mode mode)
417 netif_wake_queue(ndev);
427 static int ti_hecc_get_berr_counter(const struct net_device *ndev,
428 struct can_berr_counter *bec)
430 struct ti_hecc_priv *priv = netdev_priv(ndev);
432 bec->txerr = hecc_read(priv, HECC_CANTEC);
433 bec->rxerr = hecc_read(priv, HECC_CANREC);
438 /* ti_hecc_xmit: HECC Transmit
440 * The transmit mailboxes start from 0 to HECC_MAX_TX_MBOX. In HECC the
441 * priority of the mailbox for tranmission is dependent upon priority setting
442 * field in mailbox registers. The mailbox with highest value in priority field
443 * is transmitted first. Only when two mailboxes have the same value in
444 * priority field the highest numbered mailbox is transmitted first.
446 * To utilize the HECC priority feature as described above we start with the
447 * highest numbered mailbox with highest priority level and move on to the next
448 * mailbox with the same priority level and so on. Once we loop through all the
449 * transmit mailboxes we choose the next priority level (lower) and so on
450 * until we reach the lowest priority level on the lowest numbered mailbox
451 * when we stop transmission until all mailboxes are transmitted and then
452 * restart at highest numbered mailbox with highest priority.
454 * Two counters (head and tail) are used to track the next mailbox to transmit
455 * and to track the echo buffer for already transmitted mailbox. The queue
456 * is stopped when all the mailboxes are busy or when there is a priority
457 * value roll-over happens.
459 static netdev_tx_t ti_hecc_xmit(struct sk_buff *skb, struct net_device *ndev)
461 struct ti_hecc_priv *priv = netdev_priv(ndev);
462 struct can_frame *cf = (struct can_frame *)skb->data;
463 u32 mbxno, mbx_mask, data;
466 if (can_dropped_invalid_skb(ndev, skb))
469 mbxno = get_tx_head_mb(priv);
470 mbx_mask = BIT(mbxno);
471 spin_lock_irqsave(&priv->mbx_lock, flags);
472 if (unlikely(hecc_read(priv, HECC_CANME) & mbx_mask)) {
473 spin_unlock_irqrestore(&priv->mbx_lock, flags);
474 netif_stop_queue(ndev);
475 netdev_err(priv->ndev,
476 "BUG: TX mbx not ready tx_head=%08X, tx_tail=%08X\n",
477 priv->tx_head, priv->tx_tail);
478 return NETDEV_TX_BUSY;
480 spin_unlock_irqrestore(&priv->mbx_lock, flags);
482 /* Prepare mailbox for transmission */
483 data = cf->can_dlc | (get_tx_head_prio(priv) << 8);
484 if (cf->can_id & CAN_RTR_FLAG) /* Remote transmission request */
485 data |= HECC_CANMCF_RTR;
486 hecc_write_mbx(priv, mbxno, HECC_CANMCF, data);
488 if (cf->can_id & CAN_EFF_FLAG) /* Extended frame format */
489 data = (cf->can_id & CAN_EFF_MASK) | HECC_CANMID_IDE;
490 else /* Standard frame format */
491 data = (cf->can_id & CAN_SFF_MASK) << 18;
492 hecc_write_mbx(priv, mbxno, HECC_CANMID, data);
493 hecc_write_mbx(priv, mbxno, HECC_CANMDL,
494 be32_to_cpu(*(__be32 *)(cf->data)));
496 hecc_write_mbx(priv, mbxno, HECC_CANMDH,
497 be32_to_cpu(*(__be32 *)(cf->data + 4)));
499 *(u32 *)(cf->data + 4) = 0;
500 can_put_echo_skb(skb, ndev, mbxno);
502 spin_lock_irqsave(&priv->mbx_lock, flags);
504 if ((hecc_read(priv, HECC_CANME) & BIT(get_tx_head_mb(priv))) ||
505 (priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK) {
506 netif_stop_queue(ndev);
508 hecc_set_bit(priv, HECC_CANME, mbx_mask);
509 spin_unlock_irqrestore(&priv->mbx_lock, flags);
511 hecc_clear_bit(priv, HECC_CANMD, mbx_mask);
512 hecc_set_bit(priv, HECC_CANMIM, mbx_mask);
513 hecc_write(priv, HECC_CANTRS, mbx_mask);
519 struct ti_hecc_priv *rx_offload_to_priv(struct can_rx_offload *offload)
521 return container_of(offload, struct ti_hecc_priv, offload);
524 static unsigned int ti_hecc_mailbox_read(struct can_rx_offload *offload,
525 struct can_frame *cf,
526 u32 *timestamp, unsigned int mbxno)
528 struct ti_hecc_priv *priv = rx_offload_to_priv(offload);
531 data = hecc_read_mbx(priv, mbxno, HECC_CANMID);
532 if (data & HECC_CANMID_IDE)
533 cf->can_id = (data & CAN_EFF_MASK) | CAN_EFF_FLAG;
535 cf->can_id = (data >> 18) & CAN_SFF_MASK;
537 data = hecc_read_mbx(priv, mbxno, HECC_CANMCF);
538 if (data & HECC_CANMCF_RTR)
539 cf->can_id |= CAN_RTR_FLAG;
540 cf->can_dlc = get_can_dlc(data & 0xF);
542 data = hecc_read_mbx(priv, mbxno, HECC_CANMDL);
543 *(__be32 *)(cf->data) = cpu_to_be32(data);
544 if (cf->can_dlc > 4) {
545 data = hecc_read_mbx(priv, mbxno, HECC_CANMDH);
546 *(__be32 *)(cf->data + 4) = cpu_to_be32(data);
549 *timestamp = hecc_read_stamp(priv, mbxno);
554 static int ti_hecc_error(struct net_device *ndev, int int_status,
557 struct ti_hecc_priv *priv = netdev_priv(ndev);
558 struct can_frame *cf;
562 /* propagate the error condition to the can stack */
563 skb = alloc_can_err_skb(ndev, &cf);
565 if (printk_ratelimit())
566 netdev_err(priv->ndev,
567 "%s: alloc_can_err_skb() failed\n",
572 if (int_status & HECC_CANGIF_WLIF) { /* warning level int */
573 if ((int_status & HECC_CANGIF_BOIF) == 0) {
574 priv->can.state = CAN_STATE_ERROR_WARNING;
575 ++priv->can.can_stats.error_warning;
576 cf->can_id |= CAN_ERR_CRTL;
577 if (hecc_read(priv, HECC_CANTEC) > 96)
578 cf->data[1] |= CAN_ERR_CRTL_TX_WARNING;
579 if (hecc_read(priv, HECC_CANREC) > 96)
580 cf->data[1] |= CAN_ERR_CRTL_RX_WARNING;
582 hecc_set_bit(priv, HECC_CANES, HECC_CANES_EW);
583 netdev_dbg(priv->ndev, "Error Warning interrupt\n");
584 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
587 if (int_status & HECC_CANGIF_EPIF) { /* error passive int */
588 if ((int_status & HECC_CANGIF_BOIF) == 0) {
589 priv->can.state = CAN_STATE_ERROR_PASSIVE;
590 ++priv->can.can_stats.error_passive;
591 cf->can_id |= CAN_ERR_CRTL;
592 if (hecc_read(priv, HECC_CANTEC) > 127)
593 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
594 if (hecc_read(priv, HECC_CANREC) > 127)
595 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
597 hecc_set_bit(priv, HECC_CANES, HECC_CANES_EP);
598 netdev_dbg(priv->ndev, "Error passive interrupt\n");
599 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
602 /* Need to check busoff condition in error status register too to
603 * ensure warning interrupts don't hog the system
605 if ((int_status & HECC_CANGIF_BOIF) || (err_status & HECC_CANES_BO)) {
606 priv->can.state = CAN_STATE_BUS_OFF;
607 cf->can_id |= CAN_ERR_BUSOFF;
608 hecc_set_bit(priv, HECC_CANES, HECC_CANES_BO);
609 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
610 /* Disable all interrupts in bus-off to avoid int hog */
611 hecc_write(priv, HECC_CANGIM, 0);
612 ++priv->can.can_stats.bus_off;
616 if (err_status & HECC_BUS_ERROR) {
617 ++priv->can.can_stats.bus_error;
618 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
619 if (err_status & HECC_CANES_FE) {
620 hecc_set_bit(priv, HECC_CANES, HECC_CANES_FE);
621 cf->data[2] |= CAN_ERR_PROT_FORM;
623 if (err_status & HECC_CANES_BE) {
624 hecc_set_bit(priv, HECC_CANES, HECC_CANES_BE);
625 cf->data[2] |= CAN_ERR_PROT_BIT;
627 if (err_status & HECC_CANES_SE) {
628 hecc_set_bit(priv, HECC_CANES, HECC_CANES_SE);
629 cf->data[2] |= CAN_ERR_PROT_STUFF;
631 if (err_status & HECC_CANES_CRCE) {
632 hecc_set_bit(priv, HECC_CANES, HECC_CANES_CRCE);
633 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
635 if (err_status & HECC_CANES_ACKE) {
636 hecc_set_bit(priv, HECC_CANES, HECC_CANES_ACKE);
637 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
641 timestamp = hecc_read(priv, HECC_CANLNT);
642 can_rx_offload_queue_sorted(&priv->offload, skb, timestamp);
647 static irqreturn_t ti_hecc_interrupt(int irq, void *dev_id)
649 struct net_device *ndev = (struct net_device *)dev_id;
650 struct ti_hecc_priv *priv = netdev_priv(ndev);
651 struct net_device_stats *stats = &ndev->stats;
652 u32 mbxno, mbx_mask, int_status, err_status, stamp;
653 unsigned long flags, rx_pending;
655 int_status = hecc_read(priv,
657 HECC_CANGIF1 : HECC_CANGIF0);
662 err_status = hecc_read(priv, HECC_CANES);
663 if (err_status & (HECC_BUS_ERROR | HECC_CANES_BO |
664 HECC_CANES_EP | HECC_CANES_EW))
665 ti_hecc_error(ndev, int_status, err_status);
667 if (int_status & HECC_CANGIF_GMIF) {
668 while (priv->tx_tail - priv->tx_head > 0) {
669 mbxno = get_tx_tail_mb(priv);
670 mbx_mask = BIT(mbxno);
671 if (!(mbx_mask & hecc_read(priv, HECC_CANTA)))
673 hecc_clear_bit(priv, HECC_CANMIM, mbx_mask);
674 hecc_write(priv, HECC_CANTA, mbx_mask);
675 spin_lock_irqsave(&priv->mbx_lock, flags);
676 hecc_clear_bit(priv, HECC_CANME, mbx_mask);
677 spin_unlock_irqrestore(&priv->mbx_lock, flags);
678 stamp = hecc_read_stamp(priv, mbxno);
680 can_rx_offload_get_echo_skb(&priv->offload,
683 can_led_event(ndev, CAN_LED_EVENT_TX);
687 /* restart queue if wrap-up or if queue stalled on last pkt */
688 if ((priv->tx_head == priv->tx_tail &&
689 ((priv->tx_head & HECC_TX_MASK) != HECC_TX_MASK)) ||
690 (((priv->tx_tail & HECC_TX_MASK) == HECC_TX_MASK) &&
691 ((priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK)))
692 netif_wake_queue(ndev);
694 /* offload RX mailboxes and let NAPI deliver them */
695 while ((rx_pending = hecc_read(priv, HECC_CANRMP))) {
696 can_rx_offload_irq_offload_timestamp(&priv->offload,
698 hecc_write(priv, HECC_CANRMP, rx_pending);
702 /* clear all interrupt conditions - read back to avoid spurious ints */
703 if (priv->use_hecc1int) {
704 hecc_write(priv, HECC_CANGIF1, HECC_SET_REG);
705 int_status = hecc_read(priv, HECC_CANGIF1);
707 hecc_write(priv, HECC_CANGIF0, HECC_SET_REG);
708 int_status = hecc_read(priv, HECC_CANGIF0);
714 static int ti_hecc_open(struct net_device *ndev)
716 struct ti_hecc_priv *priv = netdev_priv(ndev);
719 err = request_irq(ndev->irq, ti_hecc_interrupt, IRQF_SHARED,
722 netdev_err(ndev, "error requesting interrupt\n");
726 ti_hecc_transceiver_switch(priv, 1);
728 /* Open common can device */
729 err = open_candev(ndev);
731 netdev_err(ndev, "open_candev() failed %d\n", err);
732 ti_hecc_transceiver_switch(priv, 0);
733 free_irq(ndev->irq, ndev);
737 can_led_event(ndev, CAN_LED_EVENT_OPEN);
740 can_rx_offload_enable(&priv->offload);
741 netif_start_queue(ndev);
746 static int ti_hecc_close(struct net_device *ndev)
748 struct ti_hecc_priv *priv = netdev_priv(ndev);
750 netif_stop_queue(ndev);
751 can_rx_offload_disable(&priv->offload);
753 free_irq(ndev->irq, ndev);
755 ti_hecc_transceiver_switch(priv, 0);
757 can_led_event(ndev, CAN_LED_EVENT_STOP);
762 static const struct net_device_ops ti_hecc_netdev_ops = {
763 .ndo_open = ti_hecc_open,
764 .ndo_stop = ti_hecc_close,
765 .ndo_start_xmit = ti_hecc_xmit,
766 .ndo_change_mtu = can_change_mtu,
769 static const struct of_device_id ti_hecc_dt_ids[] = {
771 .compatible = "ti,am3517-hecc",
775 MODULE_DEVICE_TABLE(of, ti_hecc_dt_ids);
777 static int ti_hecc_probe(struct platform_device *pdev)
779 struct net_device *ndev = (struct net_device *)0;
780 struct ti_hecc_priv *priv;
781 struct device_node *np = pdev->dev.of_node;
782 struct resource *res, *irq;
783 struct regulator *reg_xceiver;
786 if (!IS_ENABLED(CONFIG_OF) || !np)
789 reg_xceiver = devm_regulator_get(&pdev->dev, "xceiver");
790 if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
791 return -EPROBE_DEFER;
792 else if (IS_ERR(reg_xceiver))
795 ndev = alloc_candev(sizeof(struct ti_hecc_priv), HECC_MAX_TX_MBOX);
797 dev_err(&pdev->dev, "alloc_candev failed\n");
800 priv = netdev_priv(ndev);
802 /* handle hecc memory */
803 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hecc");
805 dev_err(&pdev->dev, "can't get IORESOURCE_MEM hecc\n");
809 priv->base = devm_ioremap_resource(&pdev->dev, res);
810 if (IS_ERR(priv->base)) {
811 dev_err(&pdev->dev, "hecc ioremap failed\n");
812 return PTR_ERR(priv->base);
815 /* handle hecc-ram memory */
816 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hecc-ram");
818 dev_err(&pdev->dev, "can't get IORESOURCE_MEM hecc-ram\n");
822 priv->hecc_ram = devm_ioremap_resource(&pdev->dev, res);
823 if (IS_ERR(priv->hecc_ram)) {
824 dev_err(&pdev->dev, "hecc-ram ioremap failed\n");
825 return PTR_ERR(priv->hecc_ram);
828 /* handle mbx memory */
829 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mbx");
831 dev_err(&pdev->dev, "can't get IORESOURCE_MEM mbx\n");
835 priv->mbx = devm_ioremap_resource(&pdev->dev, res);
836 if (IS_ERR(priv->mbx)) {
837 dev_err(&pdev->dev, "mbx ioremap failed\n");
838 return PTR_ERR(priv->mbx);
841 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
843 dev_err(&pdev->dev, "No irq resource\n");
848 priv->reg_xceiver = reg_xceiver;
849 priv->use_hecc1int = of_property_read_bool(np, "ti,use-hecc1int");
851 priv->can.bittiming_const = &ti_hecc_bittiming_const;
852 priv->can.do_set_mode = ti_hecc_do_set_mode;
853 priv->can.do_get_berr_counter = ti_hecc_get_berr_counter;
854 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
856 spin_lock_init(&priv->mbx_lock);
857 ndev->irq = irq->start;
858 ndev->flags |= IFF_ECHO;
859 platform_set_drvdata(pdev, ndev);
860 SET_NETDEV_DEV(ndev, &pdev->dev);
861 ndev->netdev_ops = &ti_hecc_netdev_ops;
863 priv->clk = clk_get(&pdev->dev, "hecc_ck");
864 if (IS_ERR(priv->clk)) {
865 dev_err(&pdev->dev, "No clock available\n");
866 err = PTR_ERR(priv->clk);
868 goto probe_exit_candev;
870 priv->can.clock.freq = clk_get_rate(priv->clk);
872 err = clk_prepare_enable(priv->clk);
874 dev_err(&pdev->dev, "clk_prepare_enable() failed\n");
878 priv->offload.mailbox_read = ti_hecc_mailbox_read;
879 priv->offload.mb_first = HECC_RX_FIRST_MBOX;
880 priv->offload.mb_last = HECC_MAX_TX_MBOX;
881 err = can_rx_offload_add_timestamp(ndev, &priv->offload);
883 dev_err(&pdev->dev, "can_rx_offload_add_timestamp() failed\n");
887 err = register_candev(ndev);
889 dev_err(&pdev->dev, "register_candev() failed\n");
890 goto probe_exit_offload;
893 devm_can_led_init(ndev);
895 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%u)\n",
896 priv->base, (u32)ndev->irq);
901 can_rx_offload_del(&priv->offload);
910 static int ti_hecc_remove(struct platform_device *pdev)
912 struct net_device *ndev = platform_get_drvdata(pdev);
913 struct ti_hecc_priv *priv = netdev_priv(ndev);
915 unregister_candev(ndev);
916 clk_disable_unprepare(priv->clk);
918 can_rx_offload_del(&priv->offload);
925 static int ti_hecc_suspend(struct platform_device *pdev, pm_message_t state)
927 struct net_device *dev = platform_get_drvdata(pdev);
928 struct ti_hecc_priv *priv = netdev_priv(dev);
930 if (netif_running(dev)) {
931 netif_stop_queue(dev);
932 netif_device_detach(dev);
935 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_PDR);
936 priv->can.state = CAN_STATE_SLEEPING;
938 clk_disable_unprepare(priv->clk);
943 static int ti_hecc_resume(struct platform_device *pdev)
945 struct net_device *dev = platform_get_drvdata(pdev);
946 struct ti_hecc_priv *priv = netdev_priv(dev);
949 err = clk_prepare_enable(priv->clk);
953 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_PDR);
954 priv->can.state = CAN_STATE_ERROR_ACTIVE;
956 if (netif_running(dev)) {
957 netif_device_attach(dev);
958 netif_start_queue(dev);
964 #define ti_hecc_suspend NULL
965 #define ti_hecc_resume NULL
968 /* TI HECC netdevice driver: platform driver structure */
969 static struct platform_driver ti_hecc_driver = {
972 .of_match_table = ti_hecc_dt_ids,
974 .probe = ti_hecc_probe,
975 .remove = ti_hecc_remove,
976 .suspend = ti_hecc_suspend,
977 .resume = ti_hecc_resume,
980 module_platform_driver(ti_hecc_driver);
982 MODULE_AUTHOR("Anant Gole <anantgole@ti.com>");
983 MODULE_LICENSE("GPL v2");
984 MODULE_DESCRIPTION(DRV_DESC);
985 MODULE_ALIAS("platform:" DRV_NAME);