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 */
50 * Important Note: TX mailbox configuration
51 * TX mailboxes should be restricted to the number of SKB buffers to avoid
52 * maintaining SKB buffers separately. TX mailboxes should be a power of 2
53 * for the mailbox logic to work. Top mailbox numbers are reserved for RX
54 * and lower mailboxes for TX.
56 * HECC_MAX_TX_MBOX HECC_MB_TX_SHIFT
61 #define HECC_MB_TX_SHIFT 2 /* as per table above */
62 #define HECC_MAX_TX_MBOX BIT(HECC_MB_TX_SHIFT)
64 #define HECC_TX_PRIO_SHIFT (HECC_MB_TX_SHIFT)
65 #define HECC_TX_PRIO_MASK (MAX_TX_PRIO << HECC_MB_TX_SHIFT)
66 #define HECC_TX_MB_MASK (HECC_MAX_TX_MBOX - 1)
67 #define HECC_TX_MASK ((HECC_MAX_TX_MBOX - 1) | HECC_TX_PRIO_MASK)
69 /* RX mailbox configuration
71 * The remaining mailboxes are used for reception and are delivered
72 * based on their timestamp, to avoid a hardware race when CANME is
73 * changed while CAN-bus traffic is being received.
75 #define HECC_MAX_RX_MBOX (HECC_MAX_MAILBOXES - HECC_MAX_TX_MBOX)
76 #define HECC_RX_FIRST_MBOX (HECC_MAX_MAILBOXES - 1)
78 /* TI HECC module registers */
79 #define HECC_CANME 0x0 /* Mailbox enable */
80 #define HECC_CANMD 0x4 /* Mailbox direction */
81 #define HECC_CANTRS 0x8 /* Transmit request set */
82 #define HECC_CANTRR 0xC /* Transmit request */
83 #define HECC_CANTA 0x10 /* Transmission acknowledge */
84 #define HECC_CANAA 0x14 /* Abort acknowledge */
85 #define HECC_CANRMP 0x18 /* Receive message pending */
86 #define HECC_CANRML 0x1C /* Remote message lost */
87 #define HECC_CANRFP 0x20 /* Remote frame pending */
88 #define HECC_CANGAM 0x24 /* SECC only:Global acceptance mask */
89 #define HECC_CANMC 0x28 /* Master control */
90 #define HECC_CANBTC 0x2C /* Bit timing configuration */
91 #define HECC_CANES 0x30 /* Error and status */
92 #define HECC_CANTEC 0x34 /* Transmit error counter */
93 #define HECC_CANREC 0x38 /* Receive error counter */
94 #define HECC_CANGIF0 0x3C /* Global interrupt flag 0 */
95 #define HECC_CANGIM 0x40 /* Global interrupt mask */
96 #define HECC_CANGIF1 0x44 /* Global interrupt flag 1 */
97 #define HECC_CANMIM 0x48 /* Mailbox interrupt mask */
98 #define HECC_CANMIL 0x4C /* Mailbox interrupt level */
99 #define HECC_CANOPC 0x50 /* Overwrite protection control */
100 #define HECC_CANTIOC 0x54 /* Transmit I/O control */
101 #define HECC_CANRIOC 0x58 /* Receive I/O control */
102 #define HECC_CANLNT 0x5C /* HECC only: Local network time */
103 #define HECC_CANTOC 0x60 /* HECC only: Time-out control */
104 #define HECC_CANTOS 0x64 /* HECC only: Time-out status */
105 #define HECC_CANTIOCE 0x68 /* SCC only:Enhanced TX I/O control */
106 #define HECC_CANRIOCE 0x6C /* SCC only:Enhanced RX I/O control */
108 /* TI HECC RAM registers */
109 #define HECC_CANMOTS 0x80 /* Message object time stamp */
111 /* Mailbox registers */
112 #define HECC_CANMID 0x0
113 #define HECC_CANMCF 0x4
114 #define HECC_CANMDL 0x8
115 #define HECC_CANMDH 0xC
117 #define HECC_SET_REG 0xFFFFFFFF
118 #define HECC_CANID_MASK 0x3FF /* 18 bits mask for extended id's */
119 #define HECC_CCE_WAIT_COUNT 100 /* Wait for ~1 sec for CCE bit */
121 #define HECC_CANMC_SCM BIT(13) /* SCC compat mode */
122 #define HECC_CANMC_CCR BIT(12) /* Change config request */
123 #define HECC_CANMC_PDR BIT(11) /* Local Power down - for sleep mode */
124 #define HECC_CANMC_ABO BIT(7) /* Auto Bus On */
125 #define HECC_CANMC_STM BIT(6) /* Self test mode - loopback */
126 #define HECC_CANMC_SRES BIT(5) /* Software reset */
128 #define HECC_CANTIOC_EN BIT(3) /* Enable CAN TX I/O pin */
129 #define HECC_CANRIOC_EN BIT(3) /* Enable CAN RX I/O pin */
131 #define HECC_CANMID_IDE BIT(31) /* Extended frame format */
132 #define HECC_CANMID_AME BIT(30) /* Acceptance mask enable */
133 #define HECC_CANMID_AAM BIT(29) /* Auto answer mode */
135 #define HECC_CANES_FE BIT(24) /* form error */
136 #define HECC_CANES_BE BIT(23) /* bit error */
137 #define HECC_CANES_SA1 BIT(22) /* stuck at dominant error */
138 #define HECC_CANES_CRCE BIT(21) /* CRC error */
139 #define HECC_CANES_SE BIT(20) /* stuff bit error */
140 #define HECC_CANES_ACKE BIT(19) /* ack error */
141 #define HECC_CANES_BO BIT(18) /* Bus off status */
142 #define HECC_CANES_EP BIT(17) /* Error passive status */
143 #define HECC_CANES_EW BIT(16) /* Error warning status */
144 #define HECC_CANES_SMA BIT(5) /* suspend mode ack */
145 #define HECC_CANES_CCE BIT(4) /* Change config enabled */
146 #define HECC_CANES_PDA BIT(3) /* Power down mode ack */
148 #define HECC_CANBTC_SAM BIT(7) /* sample points */
150 #define HECC_BUS_ERROR (HECC_CANES_FE | HECC_CANES_BE |\
151 HECC_CANES_CRCE | HECC_CANES_SE |\
154 #define HECC_CANMCF_RTR BIT(4) /* Remote transmit request */
156 #define HECC_CANGIF_MAIF BIT(17) /* Message alarm interrupt */
157 #define HECC_CANGIF_TCOIF BIT(16) /* Timer counter overflow int */
158 #define HECC_CANGIF_GMIF BIT(15) /* Global mailbox interrupt */
159 #define HECC_CANGIF_AAIF BIT(14) /* Abort ack interrupt */
160 #define HECC_CANGIF_WDIF BIT(13) /* Write denied interrupt */
161 #define HECC_CANGIF_WUIF BIT(12) /* Wake up interrupt */
162 #define HECC_CANGIF_RMLIF BIT(11) /* Receive message lost interrupt */
163 #define HECC_CANGIF_BOIF BIT(10) /* Bus off interrupt */
164 #define HECC_CANGIF_EPIF BIT(9) /* Error passive interrupt */
165 #define HECC_CANGIF_WLIF BIT(8) /* Warning level interrupt */
166 #define HECC_CANGIF_MBOX_MASK 0x1F /* Mailbox number mask */
167 #define HECC_CANGIM_I1EN BIT(1) /* Int line 1 enable */
168 #define HECC_CANGIM_I0EN BIT(0) /* Int line 0 enable */
169 #define HECC_CANGIM_DEF_MASK 0x700 /* only busoff/warning/passive */
170 #define HECC_CANGIM_SIL BIT(2) /* system interrupts to int line 1 */
172 /* CAN Bittiming constants as per HECC specs */
173 static const struct can_bittiming_const ti_hecc_bittiming_const = {
185 struct ti_hecc_priv {
186 struct can_priv can; /* MUST be first member/field */
187 struct can_rx_offload offload;
188 struct net_device *ndev;
191 void __iomem *hecc_ram;
194 spinlock_t mbx_lock; /* CANME register needs protection */
197 struct regulator *reg_xceiver;
200 static inline int get_tx_head_mb(struct ti_hecc_priv *priv)
202 return priv->tx_head & HECC_TX_MB_MASK;
205 static inline int get_tx_tail_mb(struct ti_hecc_priv *priv)
207 return priv->tx_tail & HECC_TX_MB_MASK;
210 static inline int get_tx_head_prio(struct ti_hecc_priv *priv)
212 return (priv->tx_head >> HECC_TX_PRIO_SHIFT) & MAX_TX_PRIO;
215 static inline void hecc_write_lam(struct ti_hecc_priv *priv, u32 mbxno, u32 val)
217 __raw_writel(val, priv->hecc_ram + mbxno * 4);
220 static inline u32 hecc_read_stamp(struct ti_hecc_priv *priv, u32 mbxno)
222 return __raw_readl(priv->hecc_ram + HECC_CANMOTS + mbxno * 4);
225 static inline void hecc_write_mbx(struct ti_hecc_priv *priv, u32 mbxno,
228 __raw_writel(val, priv->mbx + mbxno * 0x10 + reg);
231 static inline u32 hecc_read_mbx(struct ti_hecc_priv *priv, u32 mbxno, u32 reg)
233 return __raw_readl(priv->mbx + mbxno * 0x10 + reg);
236 static inline void hecc_write(struct ti_hecc_priv *priv, u32 reg, u32 val)
238 __raw_writel(val, priv->base + reg);
241 static inline u32 hecc_read(struct ti_hecc_priv *priv, int reg)
243 return __raw_readl(priv->base + reg);
246 static inline void hecc_set_bit(struct ti_hecc_priv *priv, int reg,
249 hecc_write(priv, reg, hecc_read(priv, reg) | bit_mask);
252 static inline void hecc_clear_bit(struct ti_hecc_priv *priv, int reg,
255 hecc_write(priv, reg, hecc_read(priv, reg) & ~bit_mask);
258 static inline u32 hecc_get_bit(struct ti_hecc_priv *priv, int reg, u32 bit_mask)
260 return (hecc_read(priv, reg) & bit_mask) ? 1 : 0;
263 static int ti_hecc_set_btc(struct ti_hecc_priv *priv)
265 struct can_bittiming *bit_timing = &priv->can.bittiming;
268 can_btc = (bit_timing->phase_seg2 - 1) & 0x7;
269 can_btc |= ((bit_timing->phase_seg1 + bit_timing->prop_seg - 1)
271 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) {
272 if (bit_timing->brp > 4)
273 can_btc |= HECC_CANBTC_SAM;
275 netdev_warn(priv->ndev, "WARN: Triple"
276 "sampling not set due to h/w limitations");
278 can_btc |= ((bit_timing->sjw - 1) & 0x3) << 8;
279 can_btc |= ((bit_timing->brp - 1) & 0xFF) << 16;
281 /* ERM being set to 0 by default meaning resync at falling edge */
283 hecc_write(priv, HECC_CANBTC, can_btc);
284 netdev_info(priv->ndev, "setting CANBTC=%#x\n", can_btc);
289 static int ti_hecc_transceiver_switch(const struct ti_hecc_priv *priv,
292 if (!priv->reg_xceiver)
296 return regulator_enable(priv->reg_xceiver);
298 return regulator_disable(priv->reg_xceiver);
301 static void ti_hecc_reset(struct net_device *ndev)
304 struct ti_hecc_priv *priv = netdev_priv(ndev);
306 netdev_dbg(ndev, "resetting hecc ...\n");
307 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_SRES);
309 /* Set change control request and wait till enabled */
310 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) {
324 * Note: On HECC, BTC can be programmed only in initialization mode, so
325 * it is expected that the can bittiming parameters are set via ip
326 * utility before the device is opened
328 ti_hecc_set_btc(priv);
330 /* Clear CCR (and CANMC register) and wait for CCE = 0 enable */
331 hecc_write(priv, HECC_CANMC, 0);
334 * INFO: CAN net stack handles bus off and hence disabling auto-bus-on
335 * hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_ABO);
339 * INFO: It has been observed that at times CCE bit may not be
340 * set and hw seems to be ok even if this bit is not set so
342 cnt = HECC_CCE_WAIT_COUNT;
343 while (hecc_get_bit(priv, HECC_CANES, HECC_CANES_CCE) && cnt != 0) {
348 /* Enable TX and RX I/O Control pins */
349 hecc_write(priv, HECC_CANTIOC, HECC_CANTIOC_EN);
350 hecc_write(priv, HECC_CANRIOC, HECC_CANRIOC_EN);
352 /* Clear registers for clean operation */
353 hecc_write(priv, HECC_CANTA, HECC_SET_REG);
354 hecc_write(priv, HECC_CANRMP, HECC_SET_REG);
355 hecc_write(priv, HECC_CANGIF0, HECC_SET_REG);
356 hecc_write(priv, HECC_CANGIF1, HECC_SET_REG);
357 hecc_write(priv, HECC_CANME, 0);
358 hecc_write(priv, HECC_CANMD, 0);
360 /* SCC compat mode NOT supported (and not needed too) */
361 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_SCM);
364 static void ti_hecc_start(struct net_device *ndev)
366 struct ti_hecc_priv *priv = netdev_priv(ndev);
367 u32 cnt, mbxno, mbx_mask;
369 /* put HECC in initialization mode and set btc */
372 priv->tx_head = priv->tx_tail = HECC_TX_MASK;
374 /* Enable local and global acceptance mask registers */
375 hecc_write(priv, HECC_CANGAM, HECC_SET_REG);
377 /* Prepare configured mailboxes to receive messages */
378 for (cnt = 0; cnt < HECC_MAX_RX_MBOX; cnt++) {
379 mbxno = HECC_MAX_MAILBOXES - 1 - cnt;
380 mbx_mask = BIT(mbxno);
381 hecc_clear_bit(priv, HECC_CANME, mbx_mask);
382 hecc_write_mbx(priv, mbxno, HECC_CANMID, HECC_CANMID_AME);
383 hecc_write_lam(priv, mbxno, HECC_SET_REG);
384 hecc_set_bit(priv, HECC_CANMD, mbx_mask);
385 hecc_set_bit(priv, HECC_CANME, mbx_mask);
386 hecc_set_bit(priv, HECC_CANMIM, mbx_mask);
389 /* Prevent message over-write & Enable interrupts */
390 hecc_write(priv, HECC_CANOPC, HECC_SET_REG);
391 if (priv->use_hecc1int) {
392 hecc_write(priv, HECC_CANMIL, HECC_SET_REG);
393 hecc_write(priv, HECC_CANGIM, HECC_CANGIM_DEF_MASK |
394 HECC_CANGIM_I1EN | HECC_CANGIM_SIL);
396 hecc_write(priv, HECC_CANMIL, 0);
397 hecc_write(priv, HECC_CANGIM,
398 HECC_CANGIM_DEF_MASK | HECC_CANGIM_I0EN);
400 priv->can.state = CAN_STATE_ERROR_ACTIVE;
403 static void ti_hecc_stop(struct net_device *ndev)
405 struct ti_hecc_priv *priv = netdev_priv(ndev);
407 /* Disable interrupts and disable mailboxes */
408 hecc_write(priv, HECC_CANGIM, 0);
409 hecc_write(priv, HECC_CANMIM, 0);
410 hecc_write(priv, HECC_CANME, 0);
411 priv->can.state = CAN_STATE_STOPPED;
414 static int ti_hecc_do_set_mode(struct net_device *ndev, enum can_mode mode)
421 netif_wake_queue(ndev);
431 static int ti_hecc_get_berr_counter(const struct net_device *ndev,
432 struct can_berr_counter *bec)
434 struct ti_hecc_priv *priv = netdev_priv(ndev);
436 bec->txerr = hecc_read(priv, HECC_CANTEC);
437 bec->rxerr = hecc_read(priv, HECC_CANREC);
443 * ti_hecc_xmit: HECC Transmit
445 * The transmit mailboxes start from 0 to HECC_MAX_TX_MBOX. In HECC the
446 * priority of the mailbox for tranmission is dependent upon priority setting
447 * field in mailbox registers. The mailbox with highest value in priority field
448 * is transmitted first. Only when two mailboxes have the same value in
449 * priority field the highest numbered mailbox is transmitted first.
451 * To utilize the HECC priority feature as described above we start with the
452 * highest numbered mailbox with highest priority level and move on to the next
453 * mailbox with the same priority level and so on. Once we loop through all the
454 * transmit mailboxes we choose the next priority level (lower) and so on
455 * until we reach the lowest priority level on the lowest numbered mailbox
456 * when we stop transmission until all mailboxes are transmitted and then
457 * restart at highest numbered mailbox with highest priority.
459 * Two counters (head and tail) are used to track the next mailbox to transmit
460 * and to track the echo buffer for already transmitted mailbox. The queue
461 * is stopped when all the mailboxes are busy or when there is a priority
462 * value roll-over happens.
464 static netdev_tx_t ti_hecc_xmit(struct sk_buff *skb, struct net_device *ndev)
466 struct ti_hecc_priv *priv = netdev_priv(ndev);
467 struct can_frame *cf = (struct can_frame *)skb->data;
468 u32 mbxno, mbx_mask, data;
471 if (can_dropped_invalid_skb(ndev, skb))
474 mbxno = get_tx_head_mb(priv);
475 mbx_mask = BIT(mbxno);
476 spin_lock_irqsave(&priv->mbx_lock, flags);
477 if (unlikely(hecc_read(priv, HECC_CANME) & mbx_mask)) {
478 spin_unlock_irqrestore(&priv->mbx_lock, flags);
479 netif_stop_queue(ndev);
480 netdev_err(priv->ndev,
481 "BUG: TX mbx not ready tx_head=%08X, tx_tail=%08X\n",
482 priv->tx_head, priv->tx_tail);
483 return NETDEV_TX_BUSY;
485 spin_unlock_irqrestore(&priv->mbx_lock, flags);
487 /* Prepare mailbox for transmission */
488 data = cf->can_dlc | (get_tx_head_prio(priv) << 8);
489 if (cf->can_id & CAN_RTR_FLAG) /* Remote transmission request */
490 data |= HECC_CANMCF_RTR;
491 hecc_write_mbx(priv, mbxno, HECC_CANMCF, data);
493 if (cf->can_id & CAN_EFF_FLAG) /* Extended frame format */
494 data = (cf->can_id & CAN_EFF_MASK) | HECC_CANMID_IDE;
495 else /* Standard frame format */
496 data = (cf->can_id & CAN_SFF_MASK) << 18;
497 hecc_write_mbx(priv, mbxno, HECC_CANMID, data);
498 hecc_write_mbx(priv, mbxno, HECC_CANMDL,
499 be32_to_cpu(*(__be32 *)(cf->data)));
501 hecc_write_mbx(priv, mbxno, HECC_CANMDH,
502 be32_to_cpu(*(__be32 *)(cf->data + 4)));
504 *(u32 *)(cf->data + 4) = 0;
505 can_put_echo_skb(skb, ndev, mbxno);
507 spin_lock_irqsave(&priv->mbx_lock, flags);
509 if ((hecc_read(priv, HECC_CANME) & BIT(get_tx_head_mb(priv))) ||
510 (priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK) {
511 netif_stop_queue(ndev);
513 hecc_set_bit(priv, HECC_CANME, mbx_mask);
514 spin_unlock_irqrestore(&priv->mbx_lock, flags);
516 hecc_clear_bit(priv, HECC_CANMD, mbx_mask);
517 hecc_set_bit(priv, HECC_CANMIM, mbx_mask);
518 hecc_write(priv, HECC_CANTRS, mbx_mask);
523 static inline struct ti_hecc_priv *rx_offload_to_priv(struct can_rx_offload *offload)
525 return container_of(offload, struct ti_hecc_priv, offload);
528 static unsigned int ti_hecc_mailbox_read(struct can_rx_offload *offload,
529 struct can_frame *cf,
530 u32 *timestamp, unsigned int mbxno)
532 struct ti_hecc_priv *priv = rx_offload_to_priv(offload);
535 mbx_mask = BIT(mbxno);
536 data = hecc_read_mbx(priv, mbxno, HECC_CANMID);
537 if (data & HECC_CANMID_IDE)
538 cf->can_id = (data & CAN_EFF_MASK) | CAN_EFF_FLAG;
540 cf->can_id = (data >> 18) & CAN_SFF_MASK;
541 data = hecc_read_mbx(priv, mbxno, HECC_CANMCF);
542 if (data & HECC_CANMCF_RTR)
543 cf->can_id |= CAN_RTR_FLAG;
544 cf->can_dlc = get_can_dlc(data & 0xF);
545 data = hecc_read_mbx(priv, mbxno, HECC_CANMDL);
546 *(__be32 *)(cf->data) = cpu_to_be32(data);
547 if (cf->can_dlc > 4) {
548 data = hecc_read_mbx(priv, mbxno, HECC_CANMDH);
549 *(__be32 *)(cf->data + 4) = cpu_to_be32(data);
552 *timestamp = hecc_read_stamp(priv, mbxno);
557 static int ti_hecc_error(struct net_device *ndev, int int_status,
560 struct ti_hecc_priv *priv = netdev_priv(ndev);
561 struct can_frame *cf;
565 /* propagate the error condition to the can stack */
566 skb = alloc_can_err_skb(ndev, &cf);
568 if (printk_ratelimit())
569 netdev_err(priv->ndev,
570 "ti_hecc_error: alloc_can_err_skb() failed\n");
574 if (int_status & HECC_CANGIF_WLIF) { /* warning level int */
575 if ((int_status & HECC_CANGIF_BOIF) == 0) {
576 priv->can.state = CAN_STATE_ERROR_WARNING;
577 ++priv->can.can_stats.error_warning;
578 cf->can_id |= CAN_ERR_CRTL;
579 if (hecc_read(priv, HECC_CANTEC) > 96)
580 cf->data[1] |= CAN_ERR_CRTL_TX_WARNING;
581 if (hecc_read(priv, HECC_CANREC) > 96)
582 cf->data[1] |= CAN_ERR_CRTL_RX_WARNING;
584 hecc_set_bit(priv, HECC_CANES, HECC_CANES_EW);
585 netdev_dbg(priv->ndev, "Error Warning interrupt\n");
586 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
589 if (int_status & HECC_CANGIF_EPIF) { /* error passive int */
590 if ((int_status & HECC_CANGIF_BOIF) == 0) {
591 priv->can.state = CAN_STATE_ERROR_PASSIVE;
592 ++priv->can.can_stats.error_passive;
593 cf->can_id |= CAN_ERR_CRTL;
594 if (hecc_read(priv, HECC_CANTEC) > 127)
595 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
596 if (hecc_read(priv, HECC_CANREC) > 127)
597 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
599 hecc_set_bit(priv, HECC_CANES, HECC_CANES_EP);
600 netdev_dbg(priv->ndev, "Error passive interrupt\n");
601 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
605 * Need to check busoff condition in error status register too to
606 * ensure warning interrupts don't hog the system
608 if ((int_status & HECC_CANGIF_BOIF) || (err_status & HECC_CANES_BO)) {
609 priv->can.state = CAN_STATE_BUS_OFF;
610 cf->can_id |= CAN_ERR_BUSOFF;
611 hecc_set_bit(priv, HECC_CANES, HECC_CANES_BO);
612 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
613 /* Disable all interrupts in bus-off to avoid int hog */
614 hecc_write(priv, HECC_CANGIM, 0);
615 ++priv->can.can_stats.bus_off;
619 if (err_status & HECC_BUS_ERROR) {
620 ++priv->can.can_stats.bus_error;
621 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
622 if (err_status & HECC_CANES_FE) {
623 hecc_set_bit(priv, HECC_CANES, HECC_CANES_FE);
624 cf->data[2] |= CAN_ERR_PROT_FORM;
626 if (err_status & HECC_CANES_BE) {
627 hecc_set_bit(priv, HECC_CANES, HECC_CANES_BE);
628 cf->data[2] |= CAN_ERR_PROT_BIT;
630 if (err_status & HECC_CANES_SE) {
631 hecc_set_bit(priv, HECC_CANES, HECC_CANES_SE);
632 cf->data[2] |= CAN_ERR_PROT_STUFF;
634 if (err_status & HECC_CANES_CRCE) {
635 hecc_set_bit(priv, HECC_CANES, HECC_CANES_CRCE);
636 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
638 if (err_status & HECC_CANES_ACKE) {
639 hecc_set_bit(priv, HECC_CANES, HECC_CANES_ACKE);
640 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
644 timestamp = hecc_read(priv, HECC_CANLNT);
645 can_rx_offload_queue_sorted(&priv->offload, skb, timestamp);
650 static irqreturn_t ti_hecc_interrupt(int irq, void *dev_id)
652 struct net_device *ndev = (struct net_device *)dev_id;
653 struct ti_hecc_priv *priv = netdev_priv(ndev);
654 struct net_device_stats *stats = &ndev->stats;
655 u32 mbxno, mbx_mask, int_status, err_status, stamp;
656 unsigned long flags, rx_pending;
658 int_status = hecc_read(priv,
659 (priv->use_hecc1int) ? HECC_CANGIF1 : HECC_CANGIF0);
664 err_status = hecc_read(priv, HECC_CANES);
665 if (err_status & (HECC_BUS_ERROR | HECC_CANES_BO |
666 HECC_CANES_EP | HECC_CANES_EW))
667 ti_hecc_error(ndev, int_status, err_status);
669 if (int_status & HECC_CANGIF_GMIF) {
670 while (priv->tx_tail - priv->tx_head > 0) {
671 mbxno = get_tx_tail_mb(priv);
672 mbx_mask = BIT(mbxno);
673 if (!(mbx_mask & hecc_read(priv, HECC_CANTA)))
675 hecc_clear_bit(priv, HECC_CANMIM, mbx_mask);
676 hecc_write(priv, HECC_CANTA, mbx_mask);
677 spin_lock_irqsave(&priv->mbx_lock, flags);
678 hecc_clear_bit(priv, HECC_CANME, mbx_mask);
679 spin_unlock_irqrestore(&priv->mbx_lock, flags);
680 stamp = hecc_read_stamp(priv, mbxno);
681 stats->tx_bytes += can_rx_offload_get_echo_skb(&priv->offload,
684 can_led_event(ndev, CAN_LED_EVENT_TX);
688 /* restart queue if wrap-up or if queue stalled on last pkt */
689 if (((priv->tx_head == priv->tx_tail) &&
690 ((priv->tx_head & HECC_TX_MASK) != HECC_TX_MASK)) ||
691 (((priv->tx_tail & HECC_TX_MASK) == HECC_TX_MASK) &&
692 ((priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK)))
693 netif_wake_queue(ndev);
695 /* offload RX mailboxes and let NAPI deliver them */
696 while ((rx_pending = hecc_read(priv, HECC_CANRMP))) {
697 can_rx_offload_irq_offload_timestamp(&priv->offload,
699 hecc_write(priv, HECC_CANRMP, rx_pending);
703 /* clear all interrupt conditions - read back to avoid spurious ints */
704 if (priv->use_hecc1int) {
705 hecc_write(priv, HECC_CANGIF1, HECC_SET_REG);
706 int_status = hecc_read(priv, HECC_CANGIF1);
708 hecc_write(priv, HECC_CANGIF0, HECC_SET_REG);
709 int_status = hecc_read(priv, HECC_CANGIF0);
715 static int ti_hecc_open(struct net_device *ndev)
717 struct ti_hecc_priv *priv = netdev_priv(ndev);
720 err = request_irq(ndev->irq, ti_hecc_interrupt, IRQF_SHARED,
723 netdev_err(ndev, "error requesting interrupt\n");
727 ti_hecc_transceiver_switch(priv, 1);
729 /* Open common can device */
730 err = open_candev(ndev);
732 netdev_err(ndev, "open_candev() failed %d\n", err);
733 ti_hecc_transceiver_switch(priv, 0);
734 free_irq(ndev->irq, ndev);
738 can_led_event(ndev, CAN_LED_EVENT_OPEN);
741 can_rx_offload_enable(&priv->offload);
742 netif_start_queue(ndev);
747 static int ti_hecc_close(struct net_device *ndev)
749 struct ti_hecc_priv *priv = netdev_priv(ndev);
751 netif_stop_queue(ndev);
752 can_rx_offload_disable(&priv->offload);
754 free_irq(ndev->irq, ndev);
756 ti_hecc_transceiver_switch(priv, 0);
758 can_led_event(ndev, CAN_LED_EVENT_STOP);
763 static const struct net_device_ops ti_hecc_netdev_ops = {
764 .ndo_open = ti_hecc_open,
765 .ndo_stop = ti_hecc_close,
766 .ndo_start_xmit = ti_hecc_xmit,
767 .ndo_change_mtu = can_change_mtu,
770 static const struct of_device_id ti_hecc_dt_ids[] = {
772 .compatible = "ti,am3517-hecc",
776 MODULE_DEVICE_TABLE(of, ti_hecc_dt_ids);
778 static int ti_hecc_probe(struct platform_device *pdev)
780 struct net_device *ndev = (struct net_device *)0;
781 struct ti_hecc_priv *priv;
782 struct device_node *np = pdev->dev.of_node;
783 struct resource *res, *irq;
784 struct regulator *reg_xceiver;
787 if (!IS_ENABLED(CONFIG_OF) || !np)
790 reg_xceiver = devm_regulator_get(&pdev->dev, "xceiver");
791 if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
792 return -EPROBE_DEFER;
793 else if (IS_ERR(reg_xceiver))
796 ndev = alloc_candev(sizeof(struct ti_hecc_priv), HECC_MAX_TX_MBOX);
798 dev_err(&pdev->dev, "alloc_candev failed\n");
801 priv = netdev_priv(ndev);
803 /* handle hecc memory */
804 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hecc");
806 dev_err(&pdev->dev, "can't get IORESOURCE_MEM hecc\n");
810 priv->base = devm_ioremap_resource(&pdev->dev, res);
811 if (IS_ERR(priv->base)) {
812 dev_err(&pdev->dev, "hecc ioremap failed\n");
813 return PTR_ERR(priv->base);
816 /* handle hecc-ram memory */
817 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hecc-ram");
819 dev_err(&pdev->dev, "can't get IORESOURCE_MEM hecc-ram\n");
823 priv->hecc_ram = devm_ioremap_resource(&pdev->dev, res);
824 if (IS_ERR(priv->hecc_ram)) {
825 dev_err(&pdev->dev, "hecc-ram ioremap failed\n");
826 return PTR_ERR(priv->hecc_ram);
829 /* handle mbx memory */
830 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mbx");
832 dev_err(&pdev->dev, "can't get IORESOURCE_MEM mbx\n");
836 priv->mbx = devm_ioremap_resource(&pdev->dev, res);
837 if (IS_ERR(priv->mbx)) {
838 dev_err(&pdev->dev, "mbx ioremap failed\n");
839 return PTR_ERR(priv->mbx);
842 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
844 dev_err(&pdev->dev, "No irq resource\n");
849 priv->reg_xceiver = reg_xceiver;
850 priv->use_hecc1int = of_property_read_bool(np, "ti,use-hecc1int");
852 priv->can.bittiming_const = &ti_hecc_bittiming_const;
853 priv->can.do_set_mode = ti_hecc_do_set_mode;
854 priv->can.do_get_berr_counter = ti_hecc_get_berr_counter;
855 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
857 spin_lock_init(&priv->mbx_lock);
858 ndev->irq = irq->start;
859 ndev->flags |= IFF_ECHO;
860 platform_set_drvdata(pdev, ndev);
861 SET_NETDEV_DEV(ndev, &pdev->dev);
862 ndev->netdev_ops = &ti_hecc_netdev_ops;
864 priv->clk = clk_get(&pdev->dev, "hecc_ck");
865 if (IS_ERR(priv->clk)) {
866 dev_err(&pdev->dev, "No clock available\n");
867 err = PTR_ERR(priv->clk);
869 goto probe_exit_candev;
871 priv->can.clock.freq = clk_get_rate(priv->clk);
873 err = clk_prepare_enable(priv->clk);
875 dev_err(&pdev->dev, "clk_prepare_enable() failed\n");
879 priv->offload.mailbox_read = ti_hecc_mailbox_read;
880 priv->offload.mb_first = HECC_RX_FIRST_MBOX;
881 priv->offload.mb_last = HECC_MAX_TX_MBOX;
882 err = can_rx_offload_add_timestamp(ndev, &priv->offload);
884 dev_err(&pdev->dev, "can_rx_offload_add_timestamp() failed\n");
888 err = register_candev(ndev);
890 dev_err(&pdev->dev, "register_candev() failed\n");
891 goto probe_exit_offload;
894 devm_can_led_init(ndev);
896 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%u)\n",
897 priv->base, (u32) ndev->irq);
902 can_rx_offload_del(&priv->offload);
911 static int ti_hecc_remove(struct platform_device *pdev)
913 struct net_device *ndev = platform_get_drvdata(pdev);
914 struct ti_hecc_priv *priv = netdev_priv(ndev);
916 unregister_candev(ndev);
917 clk_disable_unprepare(priv->clk);
919 can_rx_offload_del(&priv->offload);
926 static int ti_hecc_suspend(struct platform_device *pdev, pm_message_t state)
928 struct net_device *dev = platform_get_drvdata(pdev);
929 struct ti_hecc_priv *priv = netdev_priv(dev);
931 if (netif_running(dev)) {
932 netif_stop_queue(dev);
933 netif_device_detach(dev);
936 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_PDR);
937 priv->can.state = CAN_STATE_SLEEPING;
939 clk_disable_unprepare(priv->clk);
944 static int ti_hecc_resume(struct platform_device *pdev)
946 struct net_device *dev = platform_get_drvdata(pdev);
947 struct ti_hecc_priv *priv = netdev_priv(dev);
950 err = clk_prepare_enable(priv->clk);
954 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_PDR);
955 priv->can.state = CAN_STATE_ERROR_ACTIVE;
957 if (netif_running(dev)) {
958 netif_device_attach(dev);
959 netif_start_queue(dev);
965 #define ti_hecc_suspend NULL
966 #define ti_hecc_resume NULL
969 /* TI HECC netdevice driver: platform driver structure */
970 static struct platform_driver ti_hecc_driver = {
973 .of_match_table = ti_hecc_dt_ids,
975 .probe = ti_hecc_probe,
976 .remove = ti_hecc_remove,
977 .suspend = ti_hecc_suspend,
978 .resume = ti_hecc_resume,
981 module_platform_driver(ti_hecc_driver);
983 MODULE_AUTHOR("Anant Gole <anantgole@ti.com>");
984 MODULE_LICENSE("GPL v2");
985 MODULE_DESCRIPTION(DRV_DESC);
986 MODULE_ALIAS("platform:" DRV_NAME);