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/rx-offload.h>
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
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 */
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.
54 * HECC_MAX_TX_MBOX HECC_MB_TX_SHIFT
59 #define HECC_MB_TX_SHIFT 2 /* as per table above */
60 #define HECC_MAX_TX_MBOX BIT(HECC_MB_TX_SHIFT)
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)
67 /* RX mailbox configuration
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.
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)
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 */
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 |\
152 #define HECC_CANES_FLAGS (HECC_BUS_ERROR | HECC_CANES_BO |\
153 HECC_CANES_EP | HECC_CANES_EW)
155 #define HECC_CANMCF_RTR BIT(4) /* Remote transmit request */
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 */
173 /* CAN Bittiming constants as per HECC specs */
174 static const struct can_bittiming_const ti_hecc_bittiming_const = {
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;
192 void __iomem *hecc_ram;
195 spinlock_t mbx_lock; /* CANME register needs protection */
198 struct regulator *reg_xceiver;
201 static inline int get_tx_head_mb(struct ti_hecc_priv *priv)
203 return priv->tx_head & HECC_TX_MB_MASK;
206 static inline int get_tx_tail_mb(struct ti_hecc_priv *priv)
208 return priv->tx_tail & HECC_TX_MB_MASK;
211 static inline int get_tx_head_prio(struct ti_hecc_priv *priv)
213 return (priv->tx_head >> HECC_TX_PRIO_SHIFT) & MAX_TX_PRIO;
216 static inline void hecc_write_lam(struct ti_hecc_priv *priv, u32 mbxno, u32 val)
218 __raw_writel(val, priv->hecc_ram + mbxno * 4);
221 static inline u32 hecc_read_stamp(struct ti_hecc_priv *priv, u32 mbxno)
223 return __raw_readl(priv->hecc_ram + HECC_CANMOTS + mbxno * 4);
226 static inline void hecc_write_mbx(struct ti_hecc_priv *priv, u32 mbxno,
229 __raw_writel(val, priv->mbx + mbxno * 0x10 + reg);
232 static inline u32 hecc_read_mbx(struct ti_hecc_priv *priv, u32 mbxno, u32 reg)
234 return __raw_readl(priv->mbx + mbxno * 0x10 + reg);
237 static inline void hecc_write(struct ti_hecc_priv *priv, u32 reg, u32 val)
239 __raw_writel(val, priv->base + reg);
242 static inline u32 hecc_read(struct ti_hecc_priv *priv, int reg)
244 return __raw_readl(priv->base + reg);
247 static inline void hecc_set_bit(struct ti_hecc_priv *priv, int reg,
250 hecc_write(priv, reg, hecc_read(priv, reg) | bit_mask);
253 static inline void hecc_clear_bit(struct ti_hecc_priv *priv, int reg,
256 hecc_write(priv, reg, hecc_read(priv, reg) & ~bit_mask);
259 static inline u32 hecc_get_bit(struct ti_hecc_priv *priv, int reg, u32 bit_mask)
261 return (hecc_read(priv, reg) & bit_mask) ? 1 : 0;
264 static int ti_hecc_set_btc(struct ti_hecc_priv *priv)
266 struct can_bittiming *bit_timing = &priv->can.bittiming;
269 can_btc = (bit_timing->phase_seg2 - 1) & 0x7;
270 can_btc |= ((bit_timing->phase_seg1 + bit_timing->prop_seg - 1)
272 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) {
273 if (bit_timing->brp > 4)
274 can_btc |= HECC_CANBTC_SAM;
276 netdev_warn(priv->ndev,
277 "WARN: Triple sampling not set due to h/w limitations");
279 can_btc |= ((bit_timing->sjw - 1) & 0x3) << 8;
280 can_btc |= ((bit_timing->brp - 1) & 0xFF) << 16;
282 /* ERM being set to 0 by default meaning resync at falling edge */
284 hecc_write(priv, HECC_CANBTC, can_btc);
285 netdev_info(priv->ndev, "setting CANBTC=%#x\n", can_btc);
290 static int ti_hecc_transceiver_switch(const struct ti_hecc_priv *priv,
293 if (!priv->reg_xceiver)
297 return regulator_enable(priv->reg_xceiver);
299 return regulator_disable(priv->reg_xceiver);
302 static void ti_hecc_reset(struct net_device *ndev)
305 struct ti_hecc_priv *priv = netdev_priv(ndev);
307 netdev_dbg(ndev, "resetting hecc ...\n");
308 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_SRES);
310 /* Set change control request and wait till enabled */
311 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
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
317 cnt = HECC_CCE_WAIT_COUNT;
318 while (!hecc_get_bit(priv, HECC_CANES, HECC_CANES_CCE) && cnt != 0) {
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
327 ti_hecc_set_btc(priv);
329 /* Clear CCR (and CANMC register) and wait for CCE = 0 enable */
330 hecc_write(priv, HECC_CANMC, 0);
332 /* INFO: CAN net stack handles bus off and hence disabling auto-bus-on
333 * hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_ABO);
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
339 cnt = HECC_CCE_WAIT_COUNT;
340 while (hecc_get_bit(priv, HECC_CANES, HECC_CANES_CCE) && cnt != 0) {
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);
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);
357 /* SCC compat mode NOT supported (and not needed too) */
358 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_SCM);
361 static void ti_hecc_start(struct net_device *ndev)
363 struct ti_hecc_priv *priv = netdev_priv(ndev);
364 u32 cnt, mbxno, mbx_mask;
366 /* put HECC in initialization mode and set btc */
369 priv->tx_head = HECC_TX_MASK;
370 priv->tx_tail = HECC_TX_MASK;
372 /* Enable local and global acceptance mask registers */
373 hecc_write(priv, HECC_CANGAM, HECC_SET_REG);
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);
387 /* Enable tx interrupts */
388 hecc_set_bit(priv, HECC_CANMIM, BIT(HECC_MAX_TX_MBOX) - 1);
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
395 mbx_mask = ~BIT(HECC_RX_LAST_MBOX);
396 hecc_write(priv, HECC_CANOPC, mbx_mask);
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);
404 hecc_write(priv, HECC_CANMIL, 0);
405 hecc_write(priv, HECC_CANGIM,
406 HECC_CANGIM_DEF_MASK | HECC_CANGIM_I0EN);
408 priv->can.state = CAN_STATE_ERROR_ACTIVE;
411 static void ti_hecc_stop(struct net_device *ndev)
413 struct ti_hecc_priv *priv = netdev_priv(ndev);
415 /* Disable the CPK; stop sending, erroring and acking */
416 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
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;
425 static int ti_hecc_do_set_mode(struct net_device *ndev, enum can_mode mode)
432 netif_wake_queue(ndev);
442 static int ti_hecc_get_berr_counter(const struct net_device *ndev,
443 struct can_berr_counter *bec)
445 struct ti_hecc_priv *priv = netdev_priv(ndev);
447 bec->txerr = hecc_read(priv, HECC_CANTEC);
448 bec->rxerr = hecc_read(priv, HECC_CANREC);
453 /* ti_hecc_xmit: HECC Transmit
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.
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.
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.
474 static netdev_tx_t ti_hecc_xmit(struct sk_buff *skb, struct net_device *ndev)
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;
481 if (can_dropped_invalid_skb(ndev, skb))
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;
495 spin_unlock_irqrestore(&priv->mbx_lock, flags);
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);
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)));
511 hecc_write_mbx(priv, mbxno, HECC_CANMDH,
512 be32_to_cpu(*(__be32 *)(cf->data + 4)));
514 *(u32 *)(cf->data + 4) = 0;
515 can_put_echo_skb(skb, ndev, mbxno, 0);
517 spin_lock_irqsave(&priv->mbx_lock, flags);
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);
523 hecc_set_bit(priv, HECC_CANME, mbx_mask);
524 spin_unlock_irqrestore(&priv->mbx_lock, flags);
526 hecc_write(priv, HECC_CANTRS, mbx_mask);
532 struct ti_hecc_priv *rx_offload_to_priv(struct can_rx_offload *offload)
534 return container_of(offload, struct ti_hecc_priv, offload);
537 static struct sk_buff *ti_hecc_mailbox_read(struct can_rx_offload *offload,
538 unsigned int mbxno, u32 *timestamp,
541 struct ti_hecc_priv *priv = rx_offload_to_priv(offload);
543 struct can_frame *cf;
546 mbx_mask = BIT(mbxno);
548 if (unlikely(drop)) {
549 skb = ERR_PTR(-ENOBUFS);
553 skb = alloc_can_skb(offload->dev, &cf);
554 if (unlikely(!skb)) {
555 skb = ERR_PTR(-ENOMEM);
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;
563 cf->can_id = (data >> 18) & CAN_SFF_MASK;
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);
570 data = hecc_read_mbx(priv, mbxno, HECC_CANMDL);
571 *(__be32 *)(cf->data) = cpu_to_be32(data);
573 data = hecc_read_mbx(priv, mbxno, HECC_CANMDH);
574 *(__be32 *)(cf->data + 4) = cpu_to_be32(data);
577 *timestamp = hecc_read_stamp(priv, mbxno);
579 /* Check for FIFO overrun.
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.
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.
589 * Return an error to let rx-offload discard this CAN frame.
591 if (unlikely(mbxno == HECC_RX_LAST_MBOX &&
592 hecc_read(priv, HECC_CANRML) & mbx_mask))
593 skb = ERR_PTR(-ENOBUFS);
596 hecc_write(priv, HECC_CANRMP, mbx_mask);
601 static int ti_hecc_error(struct net_device *ndev, int int_status,
604 struct ti_hecc_priv *priv = netdev_priv(ndev);
605 struct can_frame *cf;
610 if (err_status & HECC_BUS_ERROR) {
611 /* propagate the error condition to the can stack */
612 skb = alloc_can_err_skb(ndev, &cf);
615 netdev_err(priv->ndev,
616 "%s: alloc_can_err_skb() failed\n",
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;
634 timestamp = hecc_read(priv, HECC_CANLNT);
635 err = can_rx_offload_queue_timestamp(&priv->offload, skb,
638 ndev->stats.rx_fifo_errors++;
641 hecc_write(priv, HECC_CANES, HECC_CANES_FLAGS);
646 static void ti_hecc_change_state(struct net_device *ndev,
647 enum can_state rx_state,
648 enum can_state tx_state)
650 struct ti_hecc_priv *priv = netdev_priv(ndev);
651 struct can_frame *cf;
656 skb = alloc_can_err_skb(priv->ndev, &cf);
657 if (unlikely(!skb)) {
658 priv->can.state = max(tx_state, rx_state);
662 can_change_state(priv->ndev, cf, tx_state, rx_state);
664 if (max(tx_state, rx_state) != CAN_STATE_BUS_OFF) {
665 cf->data[6] = hecc_read(priv, HECC_CANTEC);
666 cf->data[7] = hecc_read(priv, HECC_CANREC);
669 timestamp = hecc_read(priv, HECC_CANLNT);
670 err = can_rx_offload_queue_timestamp(&priv->offload, skb, timestamp);
672 ndev->stats.rx_fifo_errors++;
675 static irqreturn_t ti_hecc_interrupt(int irq, void *dev_id)
677 struct net_device *ndev = (struct net_device *)dev_id;
678 struct ti_hecc_priv *priv = netdev_priv(ndev);
679 struct net_device_stats *stats = &ndev->stats;
680 u32 mbxno, mbx_mask, int_status, err_status, stamp;
681 unsigned long flags, rx_pending;
684 int_status = hecc_read(priv,
686 HECC_CANGIF1 : HECC_CANGIF0);
691 err_status = hecc_read(priv, HECC_CANES);
692 if (unlikely(err_status & HECC_CANES_FLAGS))
693 ti_hecc_error(ndev, int_status, err_status);
695 if (unlikely(int_status & HECC_CANGIM_DEF_MASK)) {
696 enum can_state rx_state, tx_state;
697 u32 rec = hecc_read(priv, HECC_CANREC);
698 u32 tec = hecc_read(priv, HECC_CANTEC);
700 if (int_status & HECC_CANGIF_WLIF) {
701 handled |= HECC_CANGIF_WLIF;
702 rx_state = rec >= tec ? CAN_STATE_ERROR_WARNING : 0;
703 tx_state = rec <= tec ? CAN_STATE_ERROR_WARNING : 0;
704 netdev_dbg(priv->ndev, "Error Warning interrupt\n");
705 ti_hecc_change_state(ndev, rx_state, tx_state);
708 if (int_status & HECC_CANGIF_EPIF) {
709 handled |= HECC_CANGIF_EPIF;
710 rx_state = rec >= tec ? CAN_STATE_ERROR_PASSIVE : 0;
711 tx_state = rec <= tec ? CAN_STATE_ERROR_PASSIVE : 0;
712 netdev_dbg(priv->ndev, "Error passive interrupt\n");
713 ti_hecc_change_state(ndev, rx_state, tx_state);
716 if (int_status & HECC_CANGIF_BOIF) {
717 handled |= HECC_CANGIF_BOIF;
718 rx_state = CAN_STATE_BUS_OFF;
719 tx_state = CAN_STATE_BUS_OFF;
720 netdev_dbg(priv->ndev, "Bus off interrupt\n");
722 /* Disable all interrupts */
723 hecc_write(priv, HECC_CANGIM, 0);
725 ti_hecc_change_state(ndev, rx_state, tx_state);
727 } else if (unlikely(priv->can.state != CAN_STATE_ERROR_ACTIVE)) {
728 enum can_state new_state, tx_state, rx_state;
729 u32 rec = hecc_read(priv, HECC_CANREC);
730 u32 tec = hecc_read(priv, HECC_CANTEC);
732 if (rec >= 128 || tec >= 128)
733 new_state = CAN_STATE_ERROR_PASSIVE;
734 else if (rec >= 96 || tec >= 96)
735 new_state = CAN_STATE_ERROR_WARNING;
737 new_state = CAN_STATE_ERROR_ACTIVE;
739 if (new_state < priv->can.state) {
740 rx_state = rec >= tec ? new_state : 0;
741 tx_state = rec <= tec ? new_state : 0;
742 ti_hecc_change_state(ndev, rx_state, tx_state);
746 if (int_status & HECC_CANGIF_GMIF) {
747 while (priv->tx_tail - priv->tx_head > 0) {
748 mbxno = get_tx_tail_mb(priv);
749 mbx_mask = BIT(mbxno);
750 if (!(mbx_mask & hecc_read(priv, HECC_CANTA)))
752 hecc_write(priv, HECC_CANTA, mbx_mask);
753 spin_lock_irqsave(&priv->mbx_lock, flags);
754 hecc_clear_bit(priv, HECC_CANME, mbx_mask);
755 spin_unlock_irqrestore(&priv->mbx_lock, flags);
756 stamp = hecc_read_stamp(priv, mbxno);
758 can_rx_offload_get_echo_skb(&priv->offload,
764 /* restart queue if wrap-up or if queue stalled on last pkt */
765 if ((priv->tx_head == priv->tx_tail &&
766 ((priv->tx_head & HECC_TX_MASK) != HECC_TX_MASK)) ||
767 (((priv->tx_tail & HECC_TX_MASK) == HECC_TX_MASK) &&
768 ((priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK)))
769 netif_wake_queue(ndev);
771 /* offload RX mailboxes and let NAPI deliver them */
772 while ((rx_pending = hecc_read(priv, HECC_CANRMP))) {
773 can_rx_offload_irq_offload_timestamp(&priv->offload,
778 /* clear all interrupt conditions - read back to avoid spurious ints */
779 if (priv->use_hecc1int) {
780 hecc_write(priv, HECC_CANGIF1, handled);
781 int_status = hecc_read(priv, HECC_CANGIF1);
783 hecc_write(priv, HECC_CANGIF0, handled);
784 int_status = hecc_read(priv, HECC_CANGIF0);
787 can_rx_offload_irq_finish(&priv->offload);
792 static int ti_hecc_open(struct net_device *ndev)
794 struct ti_hecc_priv *priv = netdev_priv(ndev);
797 err = request_irq(ndev->irq, ti_hecc_interrupt, IRQF_SHARED,
800 netdev_err(ndev, "error requesting interrupt\n");
804 ti_hecc_transceiver_switch(priv, 1);
806 /* Open common can device */
807 err = open_candev(ndev);
809 netdev_err(ndev, "open_candev() failed %d\n", err);
810 ti_hecc_transceiver_switch(priv, 0);
811 free_irq(ndev->irq, ndev);
816 can_rx_offload_enable(&priv->offload);
817 netif_start_queue(ndev);
822 static int ti_hecc_close(struct net_device *ndev)
824 struct ti_hecc_priv *priv = netdev_priv(ndev);
826 netif_stop_queue(ndev);
827 can_rx_offload_disable(&priv->offload);
829 free_irq(ndev->irq, ndev);
831 ti_hecc_transceiver_switch(priv, 0);
836 static const struct net_device_ops ti_hecc_netdev_ops = {
837 .ndo_open = ti_hecc_open,
838 .ndo_stop = ti_hecc_close,
839 .ndo_start_xmit = ti_hecc_xmit,
840 .ndo_change_mtu = can_change_mtu,
843 static const struct of_device_id ti_hecc_dt_ids[] = {
845 .compatible = "ti,am3517-hecc",
849 MODULE_DEVICE_TABLE(of, ti_hecc_dt_ids);
851 static int ti_hecc_probe(struct platform_device *pdev)
853 struct net_device *ndev = (struct net_device *)0;
854 struct ti_hecc_priv *priv;
855 struct device_node *np = pdev->dev.of_node;
856 struct regulator *reg_xceiver;
859 if (!IS_ENABLED(CONFIG_OF) || !np)
862 reg_xceiver = devm_regulator_get(&pdev->dev, "xceiver");
863 if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
864 return -EPROBE_DEFER;
865 else if (IS_ERR(reg_xceiver))
868 ndev = alloc_candev(sizeof(struct ti_hecc_priv), HECC_MAX_TX_MBOX);
870 dev_err(&pdev->dev, "alloc_candev failed\n");
873 priv = netdev_priv(ndev);
875 /* handle hecc memory */
876 priv->base = devm_platform_ioremap_resource_byname(pdev, "hecc");
877 if (IS_ERR(priv->base)) {
878 dev_err(&pdev->dev, "hecc ioremap failed\n");
879 err = PTR_ERR(priv->base);
880 goto probe_exit_candev;
883 /* handle hecc-ram memory */
884 priv->hecc_ram = devm_platform_ioremap_resource_byname(pdev,
886 if (IS_ERR(priv->hecc_ram)) {
887 dev_err(&pdev->dev, "hecc-ram ioremap failed\n");
888 err = PTR_ERR(priv->hecc_ram);
889 goto probe_exit_candev;
892 /* handle mbx memory */
893 priv->mbx = devm_platform_ioremap_resource_byname(pdev, "mbx");
894 if (IS_ERR(priv->mbx)) {
895 dev_err(&pdev->dev, "mbx ioremap failed\n");
896 err = PTR_ERR(priv->mbx);
897 goto probe_exit_candev;
900 ndev->irq = platform_get_irq(pdev, 0);
903 goto probe_exit_candev;
907 priv->reg_xceiver = reg_xceiver;
908 priv->use_hecc1int = of_property_read_bool(np, "ti,use-hecc1int");
910 priv->can.bittiming_const = &ti_hecc_bittiming_const;
911 priv->can.do_set_mode = ti_hecc_do_set_mode;
912 priv->can.do_get_berr_counter = ti_hecc_get_berr_counter;
913 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
915 spin_lock_init(&priv->mbx_lock);
916 ndev->flags |= IFF_ECHO;
917 platform_set_drvdata(pdev, ndev);
918 SET_NETDEV_DEV(ndev, &pdev->dev);
919 ndev->netdev_ops = &ti_hecc_netdev_ops;
921 priv->clk = clk_get(&pdev->dev, "hecc_ck");
922 if (IS_ERR(priv->clk)) {
923 dev_err(&pdev->dev, "No clock available\n");
924 err = PTR_ERR(priv->clk);
926 goto probe_exit_candev;
928 priv->can.clock.freq = clk_get_rate(priv->clk);
930 err = clk_prepare_enable(priv->clk);
932 dev_err(&pdev->dev, "clk_prepare_enable() failed\n");
933 goto probe_exit_release_clk;
936 priv->offload.mailbox_read = ti_hecc_mailbox_read;
937 priv->offload.mb_first = HECC_RX_FIRST_MBOX;
938 priv->offload.mb_last = HECC_RX_LAST_MBOX;
939 err = can_rx_offload_add_timestamp(ndev, &priv->offload);
941 dev_err(&pdev->dev, "can_rx_offload_add_timestamp() failed\n");
942 goto probe_exit_disable_clk;
945 err = register_candev(ndev);
947 dev_err(&pdev->dev, "register_candev() failed\n");
948 goto probe_exit_offload;
951 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%u)\n",
952 priv->base, (u32)ndev->irq);
957 can_rx_offload_del(&priv->offload);
958 probe_exit_disable_clk:
959 clk_disable_unprepare(priv->clk);
960 probe_exit_release_clk:
968 static int ti_hecc_remove(struct platform_device *pdev)
970 struct net_device *ndev = platform_get_drvdata(pdev);
971 struct ti_hecc_priv *priv = netdev_priv(ndev);
973 unregister_candev(ndev);
974 clk_disable_unprepare(priv->clk);
976 can_rx_offload_del(&priv->offload);
983 static int ti_hecc_suspend(struct platform_device *pdev, pm_message_t state)
985 struct net_device *dev = platform_get_drvdata(pdev);
986 struct ti_hecc_priv *priv = netdev_priv(dev);
988 if (netif_running(dev)) {
989 netif_stop_queue(dev);
990 netif_device_detach(dev);
993 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_PDR);
994 priv->can.state = CAN_STATE_SLEEPING;
996 clk_disable_unprepare(priv->clk);
1001 static int ti_hecc_resume(struct platform_device *pdev)
1003 struct net_device *dev = platform_get_drvdata(pdev);
1004 struct ti_hecc_priv *priv = netdev_priv(dev);
1007 err = clk_prepare_enable(priv->clk);
1011 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_PDR);
1012 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1014 if (netif_running(dev)) {
1015 netif_device_attach(dev);
1016 netif_start_queue(dev);
1022 #define ti_hecc_suspend NULL
1023 #define ti_hecc_resume NULL
1026 /* TI HECC netdevice driver: platform driver structure */
1027 static struct platform_driver ti_hecc_driver = {
1030 .of_match_table = ti_hecc_dt_ids,
1032 .probe = ti_hecc_probe,
1033 .remove = ti_hecc_remove,
1034 .suspend = ti_hecc_suspend,
1035 .resume = ti_hecc_resume,
1038 module_platform_driver(ti_hecc_driver);
1040 MODULE_AUTHOR("Anant Gole <anantgole@ti.com>");
1041 MODULE_LICENSE("GPL v2");
1042 MODULE_DESCRIPTION(DRV_DESC);
1043 MODULE_ALIAS("platform:" DRV_NAME);