]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/can/ti_hecc.c
eb8151154083b2f62711bf5bc1aa07b6d59823bb
[linux.git] / drivers / net / can / ti_hecc.c
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/led.h>
38 #include <linux/can/rx-offload.h>
39
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
44
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 */
48
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.
54  *
55  * HECC_MAX_TX_MBOX     HECC_MB_TX_SHIFT
56  * 4 (default)          2
57  * 8                    3
58  * 16                   4
59  */
60 #define HECC_MB_TX_SHIFT        2 /* as per table above */
61 #define HECC_MAX_TX_MBOX        BIT(HECC_MB_TX_SHIFT)
62
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)
67
68 /* RX mailbox configuration
69  *
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.
73  */
74 #define HECC_MAX_RX_MBOX        (HECC_MAX_MAILBOXES - HECC_MAX_TX_MBOX)
75 #define HECC_RX_FIRST_MBOX      (HECC_MAX_MAILBOXES - 1)
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    /* 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 */
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
153 #define HECC_CANMCF_RTR         BIT(4)  /* Remote transmit request */
154
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 */
170
171 /* CAN Bittiming constants as per HECC specs */
172 static const struct can_bittiming_const ti_hecc_bittiming_const = {
173         .name = DRV_NAME,
174         .tseg1_min = 1,
175         .tseg1_max = 16,
176         .tseg2_min = 1,
177         .tseg2_max = 8,
178         .sjw_max = 4,
179         .brp_min = 1,
180         .brp_max = 256,
181         .brp_inc = 1,
182 };
183
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;
188         struct clk *clk;
189         void __iomem *base;
190         void __iomem *hecc_ram;
191         void __iomem *mbx;
192         bool use_hecc1int;
193         spinlock_t mbx_lock; /* CANME register needs protection */
194         u32 tx_head;
195         u32 tx_tail;
196         struct regulator *reg_xceiver;
197 };
198
199 static inline int get_tx_head_mb(struct ti_hecc_priv *priv)
200 {
201         return priv->tx_head & HECC_TX_MB_MASK;
202 }
203
204 static inline int get_tx_tail_mb(struct ti_hecc_priv *priv)
205 {
206         return priv->tx_tail & HECC_TX_MB_MASK;
207 }
208
209 static inline int get_tx_head_prio(struct ti_hecc_priv *priv)
210 {
211         return (priv->tx_head >> HECC_TX_PRIO_SHIFT) & MAX_TX_PRIO;
212 }
213
214 static inline void hecc_write_lam(struct ti_hecc_priv *priv, u32 mbxno, u32 val)
215 {
216         __raw_writel(val, priv->hecc_ram + mbxno * 4);
217 }
218
219 static inline u32 hecc_read_stamp(struct ti_hecc_priv *priv, u32 mbxno)
220 {
221         return __raw_readl(priv->hecc_ram + HECC_CANMOTS + mbxno * 4);
222 }
223
224 static inline void hecc_write_mbx(struct ti_hecc_priv *priv, u32 mbxno,
225                                   u32 reg, u32 val)
226 {
227         __raw_writel(val, priv->mbx + mbxno * 0x10 + reg);
228 }
229
230 static inline u32 hecc_read_mbx(struct ti_hecc_priv *priv, u32 mbxno, u32 reg)
231 {
232         return __raw_readl(priv->mbx + mbxno * 0x10 + reg);
233 }
234
235 static inline void hecc_write(struct ti_hecc_priv *priv, u32 reg, u32 val)
236 {
237         __raw_writel(val, priv->base + reg);
238 }
239
240 static inline u32 hecc_read(struct ti_hecc_priv *priv, int reg)
241 {
242         return __raw_readl(priv->base + reg);
243 }
244
245 static inline void hecc_set_bit(struct ti_hecc_priv *priv, int reg,
246                                 u32 bit_mask)
247 {
248         hecc_write(priv, reg, hecc_read(priv, reg) | bit_mask);
249 }
250
251 static inline void hecc_clear_bit(struct ti_hecc_priv *priv, int reg,
252                                   u32 bit_mask)
253 {
254         hecc_write(priv, reg, hecc_read(priv, reg) & ~bit_mask);
255 }
256
257 static inline u32 hecc_get_bit(struct ti_hecc_priv *priv, int reg, u32 bit_mask)
258 {
259         return (hecc_read(priv, reg) & bit_mask) ? 1 : 0;
260 }
261
262 static int ti_hecc_set_btc(struct ti_hecc_priv *priv)
263 {
264         struct can_bittiming *bit_timing = &priv->can.bittiming;
265         u32 can_btc;
266
267         can_btc = (bit_timing->phase_seg2 - 1) & 0x7;
268         can_btc |= ((bit_timing->phase_seg1 + bit_timing->prop_seg - 1)
269                         & 0xF) << 3;
270         if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) {
271                 if (bit_timing->brp > 4)
272                         can_btc |= HECC_CANBTC_SAM;
273                 else
274                         netdev_warn(priv->ndev,
275                                     "WARN: Triple sampling not set due to h/w limitations");
276         }
277         can_btc |= ((bit_timing->sjw - 1) & 0x3) << 8;
278         can_btc |= ((bit_timing->brp - 1) & 0xFF) << 16;
279
280         /* ERM being set to 0 by default meaning resync at falling edge */
281
282         hecc_write(priv, HECC_CANBTC, can_btc);
283         netdev_info(priv->ndev, "setting CANBTC=%#x\n", can_btc);
284
285         return 0;
286 }
287
288 static int ti_hecc_transceiver_switch(const struct ti_hecc_priv *priv,
289                                       int on)
290 {
291         if (!priv->reg_xceiver)
292                 return 0;
293
294         if (on)
295                 return regulator_enable(priv->reg_xceiver);
296         else
297                 return regulator_disable(priv->reg_xceiver);
298 }
299
300 static void ti_hecc_reset(struct net_device *ndev)
301 {
302         u32 cnt;
303         struct ti_hecc_priv *priv = netdev_priv(ndev);
304
305         netdev_dbg(ndev, "resetting hecc ...\n");
306         hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_SRES);
307
308         /* Set change control request and wait till enabled */
309         hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
310
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
314          */
315         cnt = HECC_CCE_WAIT_COUNT;
316         while (!hecc_get_bit(priv, HECC_CANES, HECC_CANES_CCE) && cnt != 0) {
317                 --cnt;
318                 udelay(10);
319         }
320
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
324          */
325         ti_hecc_set_btc(priv);
326
327         /* Clear CCR (and CANMC register) and wait for CCE = 0 enable */
328         hecc_write(priv, HECC_CANMC, 0);
329
330         /* INFO: CAN net stack handles bus off and hence disabling auto-bus-on
331          * hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_ABO);
332          */
333
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
336          */
337         cnt = HECC_CCE_WAIT_COUNT;
338         while (hecc_get_bit(priv, HECC_CANES, HECC_CANES_CCE) && cnt != 0) {
339                 --cnt;
340                 udelay(10);
341         }
342
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);
346
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);
354
355         /* SCC compat mode NOT supported (and not needed too) */
356         hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_SCM);
357 }
358
359 static void ti_hecc_start(struct net_device *ndev)
360 {
361         struct ti_hecc_priv *priv = netdev_priv(ndev);
362         u32 cnt, mbxno, mbx_mask;
363
364         /* put HECC in initialization mode and set btc */
365         ti_hecc_reset(ndev);
366
367         priv->tx_head = HECC_TX_MASK;
368         priv->tx_tail = HECC_TX_MASK;
369
370         /* Enable local and global acceptance mask registers */
371         hecc_write(priv, HECC_CANGAM, HECC_SET_REG);
372
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);
383         }
384
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);
391         } else {
392                 hecc_write(priv, HECC_CANMIL, 0);
393                 hecc_write(priv, HECC_CANGIM,
394                            HECC_CANGIM_DEF_MASK | HECC_CANGIM_I0EN);
395         }
396         priv->can.state = CAN_STATE_ERROR_ACTIVE;
397 }
398
399 static void ti_hecc_stop(struct net_device *ndev)
400 {
401         struct ti_hecc_priv *priv = netdev_priv(ndev);
402
403         /* Disable the CPK; stop sending, erroring and acking */
404         hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
405
406         /* Disable interrupts and disable mailboxes */
407         hecc_write(priv, HECC_CANGIM, 0);
408         hecc_write(priv, HECC_CANMIM, 0);
409         hecc_write(priv, HECC_CANME, 0);
410         priv->can.state = CAN_STATE_STOPPED;
411 }
412
413 static int ti_hecc_do_set_mode(struct net_device *ndev, enum can_mode mode)
414 {
415         int ret = 0;
416
417         switch (mode) {
418         case CAN_MODE_START:
419                 ti_hecc_start(ndev);
420                 netif_wake_queue(ndev);
421                 break;
422         default:
423                 ret = -EOPNOTSUPP;
424                 break;
425         }
426
427         return ret;
428 }
429
430 static int ti_hecc_get_berr_counter(const struct net_device *ndev,
431                                     struct can_berr_counter *bec)
432 {
433         struct ti_hecc_priv *priv = netdev_priv(ndev);
434
435         bec->txerr = hecc_read(priv, HECC_CANTEC);
436         bec->rxerr = hecc_read(priv, HECC_CANREC);
437
438         return 0;
439 }
440
441 /* ti_hecc_xmit: HECC Transmit
442  *
443  * The transmit mailboxes start from 0 to HECC_MAX_TX_MBOX. In HECC the
444  * priority of the mailbox for tranmission is dependent upon priority setting
445  * field in mailbox registers. The mailbox with highest value in priority field
446  * is transmitted first. Only when two mailboxes have the same value in
447  * priority field the highest numbered mailbox is transmitted first.
448  *
449  * To utilize the HECC priority feature as described above we start with the
450  * highest numbered mailbox with highest priority level and move on to the next
451  * mailbox with the same priority level and so on. Once we loop through all the
452  * transmit mailboxes we choose the next priority level (lower) and so on
453  * until we reach the lowest priority level on the lowest numbered mailbox
454  * when we stop transmission until all mailboxes are transmitted and then
455  * restart at highest numbered mailbox with highest priority.
456  *
457  * Two counters (head and tail) are used to track the next mailbox to transmit
458  * and to track the echo buffer for already transmitted mailbox. The queue
459  * is stopped when all the mailboxes are busy or when there is a priority
460  * value roll-over happens.
461  */
462 static netdev_tx_t ti_hecc_xmit(struct sk_buff *skb, struct net_device *ndev)
463 {
464         struct ti_hecc_priv *priv = netdev_priv(ndev);
465         struct can_frame *cf = (struct can_frame *)skb->data;
466         u32 mbxno, mbx_mask, data;
467         unsigned long flags;
468
469         if (can_dropped_invalid_skb(ndev, skb))
470                 return NETDEV_TX_OK;
471
472         mbxno = get_tx_head_mb(priv);
473         mbx_mask = BIT(mbxno);
474         spin_lock_irqsave(&priv->mbx_lock, flags);
475         if (unlikely(hecc_read(priv, HECC_CANME) & mbx_mask)) {
476                 spin_unlock_irqrestore(&priv->mbx_lock, flags);
477                 netif_stop_queue(ndev);
478                 netdev_err(priv->ndev,
479                            "BUG: TX mbx not ready tx_head=%08X, tx_tail=%08X\n",
480                            priv->tx_head, priv->tx_tail);
481                 return NETDEV_TX_BUSY;
482         }
483         spin_unlock_irqrestore(&priv->mbx_lock, flags);
484
485         /* Prepare mailbox for transmission */
486         data = cf->can_dlc | (get_tx_head_prio(priv) << 8);
487         if (cf->can_id & CAN_RTR_FLAG) /* Remote transmission request */
488                 data |= HECC_CANMCF_RTR;
489         hecc_write_mbx(priv, mbxno, HECC_CANMCF, data);
490
491         if (cf->can_id & CAN_EFF_FLAG) /* Extended frame format */
492                 data = (cf->can_id & CAN_EFF_MASK) | HECC_CANMID_IDE;
493         else /* Standard frame format */
494                 data = (cf->can_id & CAN_SFF_MASK) << 18;
495         hecc_write_mbx(priv, mbxno, HECC_CANMID, data);
496         hecc_write_mbx(priv, mbxno, HECC_CANMDL,
497                        be32_to_cpu(*(__be32 *)(cf->data)));
498         if (cf->can_dlc > 4)
499                 hecc_write_mbx(priv, mbxno, HECC_CANMDH,
500                                be32_to_cpu(*(__be32 *)(cf->data + 4)));
501         else
502                 *(u32 *)(cf->data + 4) = 0;
503         can_put_echo_skb(skb, ndev, mbxno);
504
505         spin_lock_irqsave(&priv->mbx_lock, flags);
506         --priv->tx_head;
507         if ((hecc_read(priv, HECC_CANME) & BIT(get_tx_head_mb(priv))) ||
508             (priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK) {
509                 netif_stop_queue(ndev);
510         }
511         hecc_set_bit(priv, HECC_CANME, mbx_mask);
512         spin_unlock_irqrestore(&priv->mbx_lock, flags);
513
514         hecc_clear_bit(priv, HECC_CANMD, mbx_mask);
515         hecc_set_bit(priv, HECC_CANMIM, mbx_mask);
516         hecc_write(priv, HECC_CANTRS, mbx_mask);
517
518         return NETDEV_TX_OK;
519 }
520
521 static inline
522 struct ti_hecc_priv *rx_offload_to_priv(struct can_rx_offload *offload)
523 {
524         return container_of(offload, struct ti_hecc_priv, offload);
525 }
526
527 static unsigned int ti_hecc_mailbox_read(struct can_rx_offload *offload,
528                                          struct can_frame *cf,
529                                          u32 *timestamp, unsigned int mbxno)
530 {
531         struct ti_hecc_priv *priv = rx_offload_to_priv(offload);
532         u32 data;
533
534         data = hecc_read_mbx(priv, mbxno, HECC_CANMID);
535         if (data & HECC_CANMID_IDE)
536                 cf->can_id = (data & CAN_EFF_MASK) | CAN_EFF_FLAG;
537         else
538                 cf->can_id = (data >> 18) & CAN_SFF_MASK;
539
540         data = hecc_read_mbx(priv, mbxno, HECC_CANMCF);
541         if (data & HECC_CANMCF_RTR)
542                 cf->can_id |= CAN_RTR_FLAG;
543         cf->can_dlc = get_can_dlc(data & 0xF);
544
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);
550         }
551
552         *timestamp = hecc_read_stamp(priv, mbxno);
553
554         return 1;
555 }
556
557 static int ti_hecc_error(struct net_device *ndev, int int_status,
558                          int err_status)
559 {
560         struct ti_hecc_priv *priv = netdev_priv(ndev);
561         struct can_frame *cf;
562         struct sk_buff *skb;
563         u32 timestamp;
564         int err;
565
566         /* propagate the error condition to the can stack */
567         skb = alloc_can_err_skb(ndev, &cf);
568         if (!skb) {
569                 if (printk_ratelimit())
570                         netdev_err(priv->ndev,
571                                    "%s: alloc_can_err_skb() failed\n",
572                                    __func__);
573                 return -ENOMEM;
574         }
575
576         if (int_status & HECC_CANGIF_WLIF) { /* warning level int */
577                 if ((int_status & HECC_CANGIF_BOIF) == 0) {
578                         priv->can.state = CAN_STATE_ERROR_WARNING;
579                         ++priv->can.can_stats.error_warning;
580                         cf->can_id |= CAN_ERR_CRTL;
581                         if (hecc_read(priv, HECC_CANTEC) > 96)
582                                 cf->data[1] |= CAN_ERR_CRTL_TX_WARNING;
583                         if (hecc_read(priv, HECC_CANREC) > 96)
584                                 cf->data[1] |= CAN_ERR_CRTL_RX_WARNING;
585                 }
586                 hecc_set_bit(priv, HECC_CANES, HECC_CANES_EW);
587                 netdev_dbg(priv->ndev, "Error Warning interrupt\n");
588                 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
589         }
590
591         if (int_status & HECC_CANGIF_EPIF) { /* error passive int */
592                 if ((int_status & HECC_CANGIF_BOIF) == 0) {
593                         priv->can.state = CAN_STATE_ERROR_PASSIVE;
594                         ++priv->can.can_stats.error_passive;
595                         cf->can_id |= CAN_ERR_CRTL;
596                         if (hecc_read(priv, HECC_CANTEC) > 127)
597                                 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
598                         if (hecc_read(priv, HECC_CANREC) > 127)
599                                 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
600                 }
601                 hecc_set_bit(priv, HECC_CANES, HECC_CANES_EP);
602                 netdev_dbg(priv->ndev, "Error passive interrupt\n");
603                 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
604         }
605
606         /* Need to check busoff condition in error status register too to
607          * ensure warning interrupts don't hog the system
608          */
609         if ((int_status & HECC_CANGIF_BOIF) || (err_status & HECC_CANES_BO)) {
610                 priv->can.state = CAN_STATE_BUS_OFF;
611                 cf->can_id |= CAN_ERR_BUSOFF;
612                 hecc_set_bit(priv, HECC_CANES, HECC_CANES_BO);
613                 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
614                 /* Disable all interrupts in bus-off to avoid int hog */
615                 hecc_write(priv, HECC_CANGIM, 0);
616                 ++priv->can.can_stats.bus_off;
617                 can_bus_off(ndev);
618         }
619
620         if (err_status & HECC_BUS_ERROR) {
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                         hecc_set_bit(priv, HECC_CANES, HECC_CANES_FE);
625                         cf->data[2] |= CAN_ERR_PROT_FORM;
626                 }
627                 if (err_status & HECC_CANES_BE) {
628                         hecc_set_bit(priv, HECC_CANES, HECC_CANES_BE);
629                         cf->data[2] |= CAN_ERR_PROT_BIT;
630                 }
631                 if (err_status & HECC_CANES_SE) {
632                         hecc_set_bit(priv, HECC_CANES, HECC_CANES_SE);
633                         cf->data[2] |= CAN_ERR_PROT_STUFF;
634                 }
635                 if (err_status & HECC_CANES_CRCE) {
636                         hecc_set_bit(priv, HECC_CANES, HECC_CANES_CRCE);
637                         cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
638                 }
639                 if (err_status & HECC_CANES_ACKE) {
640                         hecc_set_bit(priv, HECC_CANES, HECC_CANES_ACKE);
641                         cf->data[3] = CAN_ERR_PROT_LOC_ACK;
642                 }
643         }
644
645         timestamp = hecc_read(priv, HECC_CANLNT);
646         err = can_rx_offload_queue_sorted(&priv->offload, skb, timestamp);
647         if (err)
648                 ndev->stats.rx_fifo_errors++;
649
650         return 0;
651 }
652
653 static irqreturn_t ti_hecc_interrupt(int irq, void *dev_id)
654 {
655         struct net_device *ndev = (struct net_device *)dev_id;
656         struct ti_hecc_priv *priv = netdev_priv(ndev);
657         struct net_device_stats *stats = &ndev->stats;
658         u32 mbxno, mbx_mask, int_status, err_status, stamp;
659         unsigned long flags, rx_pending;
660
661         int_status = hecc_read(priv,
662                                priv->use_hecc1int ?
663                                HECC_CANGIF1 : HECC_CANGIF0);
664
665         if (!int_status)
666                 return IRQ_NONE;
667
668         err_status = hecc_read(priv, HECC_CANES);
669         if (err_status & (HECC_BUS_ERROR | HECC_CANES_BO |
670                           HECC_CANES_EP | HECC_CANES_EW))
671                 ti_hecc_error(ndev, int_status, err_status);
672
673         if (int_status & HECC_CANGIF_GMIF) {
674                 while (priv->tx_tail - priv->tx_head > 0) {
675                         mbxno = get_tx_tail_mb(priv);
676                         mbx_mask = BIT(mbxno);
677                         if (!(mbx_mask & hecc_read(priv, HECC_CANTA)))
678                                 break;
679                         hecc_clear_bit(priv, HECC_CANMIM, mbx_mask);
680                         hecc_write(priv, HECC_CANTA, mbx_mask);
681                         spin_lock_irqsave(&priv->mbx_lock, flags);
682                         hecc_clear_bit(priv, HECC_CANME, mbx_mask);
683                         spin_unlock_irqrestore(&priv->mbx_lock, flags);
684                         stamp = hecc_read_stamp(priv, mbxno);
685                         stats->tx_bytes +=
686                                 can_rx_offload_get_echo_skb(&priv->offload,
687                                                             mbxno, stamp);
688                         stats->tx_packets++;
689                         can_led_event(ndev, CAN_LED_EVENT_TX);
690                         --priv->tx_tail;
691                 }
692
693                 /* restart queue if wrap-up or if queue stalled on last pkt */
694                 if ((priv->tx_head == priv->tx_tail &&
695                      ((priv->tx_head & HECC_TX_MASK) != HECC_TX_MASK)) ||
696                     (((priv->tx_tail & HECC_TX_MASK) == HECC_TX_MASK) &&
697                      ((priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK)))
698                         netif_wake_queue(ndev);
699
700                 /* offload RX mailboxes and let NAPI deliver them */
701                 while ((rx_pending = hecc_read(priv, HECC_CANRMP))) {
702                         can_rx_offload_irq_offload_timestamp(&priv->offload,
703                                                              rx_pending);
704                         hecc_write(priv, HECC_CANRMP, rx_pending);
705                 }
706         }
707
708         /* clear all interrupt conditions - read back to avoid spurious ints */
709         if (priv->use_hecc1int) {
710                 hecc_write(priv, HECC_CANGIF1, HECC_SET_REG);
711                 int_status = hecc_read(priv, HECC_CANGIF1);
712         } else {
713                 hecc_write(priv, HECC_CANGIF0, HECC_SET_REG);
714                 int_status = hecc_read(priv, HECC_CANGIF0);
715         }
716
717         return IRQ_HANDLED;
718 }
719
720 static int ti_hecc_open(struct net_device *ndev)
721 {
722         struct ti_hecc_priv *priv = netdev_priv(ndev);
723         int err;
724
725         err = request_irq(ndev->irq, ti_hecc_interrupt, IRQF_SHARED,
726                           ndev->name, ndev);
727         if (err) {
728                 netdev_err(ndev, "error requesting interrupt\n");
729                 return err;
730         }
731
732         ti_hecc_transceiver_switch(priv, 1);
733
734         /* Open common can device */
735         err = open_candev(ndev);
736         if (err) {
737                 netdev_err(ndev, "open_candev() failed %d\n", err);
738                 ti_hecc_transceiver_switch(priv, 0);
739                 free_irq(ndev->irq, ndev);
740                 return err;
741         }
742
743         can_led_event(ndev, CAN_LED_EVENT_OPEN);
744
745         ti_hecc_start(ndev);
746         can_rx_offload_enable(&priv->offload);
747         netif_start_queue(ndev);
748
749         return 0;
750 }
751
752 static int ti_hecc_close(struct net_device *ndev)
753 {
754         struct ti_hecc_priv *priv = netdev_priv(ndev);
755
756         netif_stop_queue(ndev);
757         can_rx_offload_disable(&priv->offload);
758         ti_hecc_stop(ndev);
759         free_irq(ndev->irq, ndev);
760         close_candev(ndev);
761         ti_hecc_transceiver_switch(priv, 0);
762
763         can_led_event(ndev, CAN_LED_EVENT_STOP);
764
765         return 0;
766 }
767
768 static const struct net_device_ops ti_hecc_netdev_ops = {
769         .ndo_open               = ti_hecc_open,
770         .ndo_stop               = ti_hecc_close,
771         .ndo_start_xmit         = ti_hecc_xmit,
772         .ndo_change_mtu         = can_change_mtu,
773 };
774
775 static const struct of_device_id ti_hecc_dt_ids[] = {
776         {
777                 .compatible = "ti,am3517-hecc",
778         },
779         { }
780 };
781 MODULE_DEVICE_TABLE(of, ti_hecc_dt_ids);
782
783 static int ti_hecc_probe(struct platform_device *pdev)
784 {
785         struct net_device *ndev = (struct net_device *)0;
786         struct ti_hecc_priv *priv;
787         struct device_node *np = pdev->dev.of_node;
788         struct resource *res, *irq;
789         struct regulator *reg_xceiver;
790         int err = -ENODEV;
791
792         if (!IS_ENABLED(CONFIG_OF) || !np)
793                 return -EINVAL;
794
795         reg_xceiver = devm_regulator_get(&pdev->dev, "xceiver");
796         if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
797                 return -EPROBE_DEFER;
798         else if (IS_ERR(reg_xceiver))
799                 reg_xceiver = NULL;
800
801         ndev = alloc_candev(sizeof(struct ti_hecc_priv), HECC_MAX_TX_MBOX);
802         if (!ndev) {
803                 dev_err(&pdev->dev, "alloc_candev failed\n");
804                 return -ENOMEM;
805         }
806         priv = netdev_priv(ndev);
807
808         /* handle hecc memory */
809         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hecc");
810         if (!res) {
811                 dev_err(&pdev->dev, "can't get IORESOURCE_MEM hecc\n");
812                 return -EINVAL;
813         }
814
815         priv->base = devm_ioremap_resource(&pdev->dev, res);
816         if (IS_ERR(priv->base)) {
817                 dev_err(&pdev->dev, "hecc ioremap failed\n");
818                 return PTR_ERR(priv->base);
819         }
820
821         /* handle hecc-ram memory */
822         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hecc-ram");
823         if (!res) {
824                 dev_err(&pdev->dev, "can't get IORESOURCE_MEM hecc-ram\n");
825                 return -EINVAL;
826         }
827
828         priv->hecc_ram = devm_ioremap_resource(&pdev->dev, res);
829         if (IS_ERR(priv->hecc_ram)) {
830                 dev_err(&pdev->dev, "hecc-ram ioremap failed\n");
831                 return PTR_ERR(priv->hecc_ram);
832         }
833
834         /* handle mbx memory */
835         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mbx");
836         if (!res) {
837                 dev_err(&pdev->dev, "can't get IORESOURCE_MEM mbx\n");
838                 return -EINVAL;
839         }
840
841         priv->mbx = devm_ioremap_resource(&pdev->dev, res);
842         if (IS_ERR(priv->mbx)) {
843                 dev_err(&pdev->dev, "mbx ioremap failed\n");
844                 return PTR_ERR(priv->mbx);
845         }
846
847         irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
848         if (!irq) {
849                 dev_err(&pdev->dev, "No irq resource\n");
850                 goto probe_exit;
851         }
852
853         priv->ndev = ndev;
854         priv->reg_xceiver = reg_xceiver;
855         priv->use_hecc1int = of_property_read_bool(np, "ti,use-hecc1int");
856
857         priv->can.bittiming_const = &ti_hecc_bittiming_const;
858         priv->can.do_set_mode = ti_hecc_do_set_mode;
859         priv->can.do_get_berr_counter = ti_hecc_get_berr_counter;
860         priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
861
862         spin_lock_init(&priv->mbx_lock);
863         ndev->irq = irq->start;
864         ndev->flags |= IFF_ECHO;
865         platform_set_drvdata(pdev, ndev);
866         SET_NETDEV_DEV(ndev, &pdev->dev);
867         ndev->netdev_ops = &ti_hecc_netdev_ops;
868
869         priv->clk = clk_get(&pdev->dev, "hecc_ck");
870         if (IS_ERR(priv->clk)) {
871                 dev_err(&pdev->dev, "No clock available\n");
872                 err = PTR_ERR(priv->clk);
873                 priv->clk = NULL;
874                 goto probe_exit_candev;
875         }
876         priv->can.clock.freq = clk_get_rate(priv->clk);
877
878         err = clk_prepare_enable(priv->clk);
879         if (err) {
880                 dev_err(&pdev->dev, "clk_prepare_enable() failed\n");
881                 goto probe_exit_clk;
882         }
883
884         priv->offload.mailbox_read = ti_hecc_mailbox_read;
885         priv->offload.mb_first = HECC_RX_FIRST_MBOX;
886         priv->offload.mb_last = HECC_MAX_TX_MBOX;
887         err = can_rx_offload_add_timestamp(ndev, &priv->offload);
888         if (err) {
889                 dev_err(&pdev->dev, "can_rx_offload_add_timestamp() failed\n");
890                 goto probe_exit_clk;
891         }
892
893         err = register_candev(ndev);
894         if (err) {
895                 dev_err(&pdev->dev, "register_candev() failed\n");
896                 goto probe_exit_offload;
897         }
898
899         devm_can_led_init(ndev);
900
901         dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%u)\n",
902                  priv->base, (u32)ndev->irq);
903
904         return 0;
905
906 probe_exit_offload:
907         can_rx_offload_del(&priv->offload);
908 probe_exit_clk:
909         clk_put(priv->clk);
910 probe_exit_candev:
911         free_candev(ndev);
912 probe_exit:
913         return err;
914 }
915
916 static int ti_hecc_remove(struct platform_device *pdev)
917 {
918         struct net_device *ndev = platform_get_drvdata(pdev);
919         struct ti_hecc_priv *priv = netdev_priv(ndev);
920
921         unregister_candev(ndev);
922         clk_disable_unprepare(priv->clk);
923         clk_put(priv->clk);
924         can_rx_offload_del(&priv->offload);
925         free_candev(ndev);
926
927         return 0;
928 }
929
930 #ifdef CONFIG_PM
931 static int ti_hecc_suspend(struct platform_device *pdev, pm_message_t state)
932 {
933         struct net_device *dev = platform_get_drvdata(pdev);
934         struct ti_hecc_priv *priv = netdev_priv(dev);
935
936         if (netif_running(dev)) {
937                 netif_stop_queue(dev);
938                 netif_device_detach(dev);
939         }
940
941         hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_PDR);
942         priv->can.state = CAN_STATE_SLEEPING;
943
944         clk_disable_unprepare(priv->clk);
945
946         return 0;
947 }
948
949 static int ti_hecc_resume(struct platform_device *pdev)
950 {
951         struct net_device *dev = platform_get_drvdata(pdev);
952         struct ti_hecc_priv *priv = netdev_priv(dev);
953         int err;
954
955         err = clk_prepare_enable(priv->clk);
956         if (err)
957                 return err;
958
959         hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_PDR);
960         priv->can.state = CAN_STATE_ERROR_ACTIVE;
961
962         if (netif_running(dev)) {
963                 netif_device_attach(dev);
964                 netif_start_queue(dev);
965         }
966
967         return 0;
968 }
969 #else
970 #define ti_hecc_suspend NULL
971 #define ti_hecc_resume NULL
972 #endif
973
974 /* TI HECC netdevice driver: platform driver structure */
975 static struct platform_driver ti_hecc_driver = {
976         .driver = {
977                 .name    = DRV_NAME,
978                 .of_match_table = ti_hecc_dt_ids,
979         },
980         .probe = ti_hecc_probe,
981         .remove = ti_hecc_remove,
982         .suspend = ti_hecc_suspend,
983         .resume = ti_hecc_resume,
984 };
985
986 module_platform_driver(ti_hecc_driver);
987
988 MODULE_AUTHOR("Anant Gole <anantgole@ti.com>");
989 MODULE_LICENSE("GPL v2");
990 MODULE_DESCRIPTION(DRV_DESC);
991 MODULE_ALIAS("platform:" DRV_NAME);