1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39#include <linux/module.h>
40#include <linux/kernel.h>
41#include <linux/types.h>
42#include <linux/interrupt.h>
43#include <linux/errno.h>
44#include <linux/netdevice.h>
45#include <linux/skbuff.h>
46#include <linux/platform_device.h>
47#include <linux/clk.h>
48#include <linux/io.h>
49
50#include <linux/can/dev.h>
51#include <linux/can/error.h>
52#include <linux/can/led.h>
53#include <linux/can/platform/ti_hecc.h>
54
55#define DRV_NAME "ti_hecc"
56#define HECC_MODULE_VERSION "0.7"
57MODULE_VERSION(HECC_MODULE_VERSION);
58#define DRV_DESC "TI High End CAN Controller Driver " HECC_MODULE_VERSION
59
60
61#define HECC_MAX_MAILBOXES 32
62#define MAX_TX_PRIO 0x3F
63
64
65
66
67
68
69
70
71
72
73
74
75
76#define HECC_MB_TX_SHIFT 2
77#define HECC_MAX_TX_MBOX BIT(HECC_MB_TX_SHIFT)
78
79#define HECC_TX_PRIO_SHIFT (HECC_MB_TX_SHIFT)
80#define HECC_TX_PRIO_MASK (MAX_TX_PRIO << HECC_MB_TX_SHIFT)
81#define HECC_TX_MB_MASK (HECC_MAX_TX_MBOX - 1)
82#define HECC_TX_MASK ((HECC_MAX_TX_MBOX - 1) | HECC_TX_PRIO_MASK)
83#define HECC_TX_MBOX_MASK (~(BIT(HECC_MAX_TX_MBOX) - 1))
84#define HECC_DEF_NAPI_WEIGHT HECC_MAX_RX_MBOX
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102#define HECC_MAX_RX_MBOX (HECC_MAX_MAILBOXES - HECC_MAX_TX_MBOX)
103#define HECC_RX_BUFFER_MBOX 12
104#define HECC_RX_FIRST_MBOX (HECC_MAX_MAILBOXES - 1)
105#define HECC_RX_HIGH_MBOX_MASK (~(BIT(HECC_RX_BUFFER_MBOX) - 1))
106
107
108#define HECC_CANME 0x0
109#define HECC_CANMD 0x4
110#define HECC_CANTRS 0x8
111#define HECC_CANTRR 0xC
112#define HECC_CANTA 0x10
113#define HECC_CANAA 0x14
114#define HECC_CANRMP 0x18
115#define HECC_CANRML 0x1C
116#define HECC_CANRFP 0x20
117#define HECC_CANGAM 0x24
118#define HECC_CANMC 0x28
119#define HECC_CANBTC 0x2C
120#define HECC_CANES 0x30
121#define HECC_CANTEC 0x34
122#define HECC_CANREC 0x38
123#define HECC_CANGIF0 0x3C
124#define HECC_CANGIM 0x40
125#define HECC_CANGIF1 0x44
126#define HECC_CANMIM 0x48
127#define HECC_CANMIL 0x4C
128#define HECC_CANOPC 0x50
129#define HECC_CANTIOC 0x54
130#define HECC_CANRIOC 0x58
131#define HECC_CANLNT 0x5C
132#define HECC_CANTOC 0x60
133#define HECC_CANTOS 0x64
134#define HECC_CANTIOCE 0x68
135#define HECC_CANRIOCE 0x6C
136
137
138#define HECC_CANMID 0x0
139#define HECC_CANMCF 0x4
140#define HECC_CANMDL 0x8
141#define HECC_CANMDH 0xC
142
143#define HECC_SET_REG 0xFFFFFFFF
144#define HECC_CANID_MASK 0x3FF
145#define HECC_CCE_WAIT_COUNT 100
146
147#define HECC_CANMC_SCM BIT(13)
148#define HECC_CANMC_CCR BIT(12)
149#define HECC_CANMC_PDR BIT(11)
150#define HECC_CANMC_ABO BIT(7)
151#define HECC_CANMC_STM BIT(6)
152#define HECC_CANMC_SRES BIT(5)
153
154#define HECC_CANTIOC_EN BIT(3)
155#define HECC_CANRIOC_EN BIT(3)
156
157#define HECC_CANMID_IDE BIT(31)
158#define HECC_CANMID_AME BIT(30)
159#define HECC_CANMID_AAM BIT(29)
160
161#define HECC_CANES_FE BIT(24)
162#define HECC_CANES_BE BIT(23)
163#define HECC_CANES_SA1 BIT(22)
164#define HECC_CANES_CRCE BIT(21)
165#define HECC_CANES_SE BIT(20)
166#define HECC_CANES_ACKE BIT(19)
167#define HECC_CANES_BO BIT(18)
168#define HECC_CANES_EP BIT(17)
169#define HECC_CANES_EW BIT(16)
170#define HECC_CANES_SMA BIT(5)
171#define HECC_CANES_CCE BIT(4)
172#define HECC_CANES_PDA BIT(3)
173
174#define HECC_CANBTC_SAM BIT(7)
175
176#define HECC_BUS_ERROR (HECC_CANES_FE | HECC_CANES_BE |\
177 HECC_CANES_CRCE | HECC_CANES_SE |\
178 HECC_CANES_ACKE)
179
180#define HECC_CANMCF_RTR BIT(4)
181
182#define HECC_CANGIF_MAIF BIT(17)
183#define HECC_CANGIF_TCOIF BIT(16)
184#define HECC_CANGIF_GMIF BIT(15)
185#define HECC_CANGIF_AAIF BIT(14)
186#define HECC_CANGIF_WDIF BIT(13)
187#define HECC_CANGIF_WUIF BIT(12)
188#define HECC_CANGIF_RMLIF BIT(11)
189#define HECC_CANGIF_BOIF BIT(10)
190#define HECC_CANGIF_EPIF BIT(9)
191#define HECC_CANGIF_WLIF BIT(8)
192#define HECC_CANGIF_MBOX_MASK 0x1F
193#define HECC_CANGIM_I1EN BIT(1)
194#define HECC_CANGIM_I0EN BIT(0)
195#define HECC_CANGIM_DEF_MASK 0x700
196#define HECC_CANGIM_SIL BIT(2)
197
198
199static const struct can_bittiming_const ti_hecc_bittiming_const = {
200 .name = DRV_NAME,
201 .tseg1_min = 1,
202 .tseg1_max = 16,
203 .tseg2_min = 1,
204 .tseg2_max = 8,
205 .sjw_max = 4,
206 .brp_min = 1,
207 .brp_max = 256,
208 .brp_inc = 1,
209};
210
211struct ti_hecc_priv {
212 struct can_priv can;
213 struct napi_struct napi;
214 struct net_device *ndev;
215 struct clk *clk;
216 void __iomem *base;
217 u32 scc_ram_offset;
218 u32 hecc_ram_offset;
219 u32 mbx_offset;
220 u32 int_line;
221 spinlock_t mbx_lock;
222 u32 tx_head;
223 u32 tx_tail;
224 u32 rx_next;
225 void (*transceiver_switch)(int);
226};
227
228static inline int get_tx_head_mb(struct ti_hecc_priv *priv)
229{
230 return priv->tx_head & HECC_TX_MB_MASK;
231}
232
233static inline int get_tx_tail_mb(struct ti_hecc_priv *priv)
234{
235 return priv->tx_tail & HECC_TX_MB_MASK;
236}
237
238static inline int get_tx_head_prio(struct ti_hecc_priv *priv)
239{
240 return (priv->tx_head >> HECC_TX_PRIO_SHIFT) & MAX_TX_PRIO;
241}
242
243static inline void hecc_write_lam(struct ti_hecc_priv *priv, u32 mbxno, u32 val)
244{
245 __raw_writel(val, priv->base + priv->hecc_ram_offset + mbxno * 4);
246}
247
248static inline void hecc_write_mbx(struct ti_hecc_priv *priv, u32 mbxno,
249 u32 reg, u32 val)
250{
251 __raw_writel(val, priv->base + priv->mbx_offset + mbxno * 0x10 +
252 reg);
253}
254
255static inline u32 hecc_read_mbx(struct ti_hecc_priv *priv, u32 mbxno, u32 reg)
256{
257 return __raw_readl(priv->base + priv->mbx_offset + mbxno * 0x10 +
258 reg);
259}
260
261static inline void hecc_write(struct ti_hecc_priv *priv, u32 reg, u32 val)
262{
263 __raw_writel(val, priv->base + reg);
264}
265
266static inline u32 hecc_read(struct ti_hecc_priv *priv, int reg)
267{
268 return __raw_readl(priv->base + reg);
269}
270
271static inline void hecc_set_bit(struct ti_hecc_priv *priv, int reg,
272 u32 bit_mask)
273{
274 hecc_write(priv, reg, hecc_read(priv, reg) | bit_mask);
275}
276
277static inline void hecc_clear_bit(struct ti_hecc_priv *priv, int reg,
278 u32 bit_mask)
279{
280 hecc_write(priv, reg, hecc_read(priv, reg) & ~bit_mask);
281}
282
283static inline u32 hecc_get_bit(struct ti_hecc_priv *priv, int reg, u32 bit_mask)
284{
285 return (hecc_read(priv, reg) & bit_mask) ? 1 : 0;
286}
287
288static int ti_hecc_set_btc(struct ti_hecc_priv *priv)
289{
290 struct can_bittiming *bit_timing = &priv->can.bittiming;
291 u32 can_btc;
292
293 can_btc = (bit_timing->phase_seg2 - 1) & 0x7;
294 can_btc |= ((bit_timing->phase_seg1 + bit_timing->prop_seg - 1)
295 & 0xF) << 3;
296 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) {
297 if (bit_timing->brp > 4)
298 can_btc |= HECC_CANBTC_SAM;
299 else
300 netdev_warn(priv->ndev, "WARN: Triple"
301 "sampling not set due to h/w limitations");
302 }
303 can_btc |= ((bit_timing->sjw - 1) & 0x3) << 8;
304 can_btc |= ((bit_timing->brp - 1) & 0xFF) << 16;
305
306
307
308 hecc_write(priv, HECC_CANBTC, can_btc);
309 netdev_info(priv->ndev, "setting CANBTC=%#x\n", can_btc);
310
311 return 0;
312}
313
314static void ti_hecc_transceiver_switch(const struct ti_hecc_priv *priv,
315 int on)
316{
317 if (priv->transceiver_switch)
318 priv->transceiver_switch(on);
319}
320
321static void ti_hecc_reset(struct net_device *ndev)
322{
323 u32 cnt;
324 struct ti_hecc_priv *priv = netdev_priv(ndev);
325
326 netdev_dbg(ndev, "resetting hecc ...\n");
327 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_SRES);
328
329
330 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
331
332
333
334
335
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
344
345
346
347
348 ti_hecc_set_btc(priv);
349
350
351 hecc_write(priv, HECC_CANMC, 0);
352
353
354
355
356
357
358
359
360
361
362 cnt = HECC_CCE_WAIT_COUNT;
363 while (hecc_get_bit(priv, HECC_CANES, HECC_CANES_CCE) && cnt != 0) {
364 --cnt;
365 udelay(10);
366 }
367
368
369 hecc_write(priv, HECC_CANTIOC, HECC_CANTIOC_EN);
370 hecc_write(priv, HECC_CANRIOC, HECC_CANRIOC_EN);
371
372
373 hecc_write(priv, HECC_CANTA, HECC_SET_REG);
374 hecc_write(priv, HECC_CANRMP, HECC_SET_REG);
375 hecc_write(priv, HECC_CANGIF0, HECC_SET_REG);
376 hecc_write(priv, HECC_CANGIF1, HECC_SET_REG);
377 hecc_write(priv, HECC_CANME, 0);
378 hecc_write(priv, HECC_CANMD, 0);
379
380
381 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_SCM);
382}
383
384static void ti_hecc_start(struct net_device *ndev)
385{
386 struct ti_hecc_priv *priv = netdev_priv(ndev);
387 u32 cnt, mbxno, mbx_mask;
388
389
390 ti_hecc_reset(ndev);
391
392 priv->tx_head = priv->tx_tail = HECC_TX_MASK;
393 priv->rx_next = HECC_RX_FIRST_MBOX;
394
395
396 hecc_write(priv, HECC_CANGAM, HECC_SET_REG);
397
398
399 for (cnt = 0; cnt < HECC_MAX_RX_MBOX; cnt++) {
400 mbxno = HECC_MAX_MAILBOXES - 1 - cnt;
401 mbx_mask = BIT(mbxno);
402 hecc_clear_bit(priv, HECC_CANME, mbx_mask);
403 hecc_write_mbx(priv, mbxno, HECC_CANMID, HECC_CANMID_AME);
404 hecc_write_lam(priv, mbxno, HECC_SET_REG);
405 hecc_set_bit(priv, HECC_CANMD, mbx_mask);
406 hecc_set_bit(priv, HECC_CANME, mbx_mask);
407 hecc_set_bit(priv, HECC_CANMIM, mbx_mask);
408 }
409
410
411 hecc_write(priv, HECC_CANOPC, HECC_SET_REG);
412 if (priv->int_line) {
413 hecc_write(priv, HECC_CANMIL, HECC_SET_REG);
414 hecc_write(priv, HECC_CANGIM, HECC_CANGIM_DEF_MASK |
415 HECC_CANGIM_I1EN | HECC_CANGIM_SIL);
416 } else {
417 hecc_write(priv, HECC_CANMIL, 0);
418 hecc_write(priv, HECC_CANGIM,
419 HECC_CANGIM_DEF_MASK | HECC_CANGIM_I0EN);
420 }
421 priv->can.state = CAN_STATE_ERROR_ACTIVE;
422}
423
424static void ti_hecc_stop(struct net_device *ndev)
425{
426 struct ti_hecc_priv *priv = netdev_priv(ndev);
427
428
429 hecc_write(priv, HECC_CANGIM, 0);
430 hecc_write(priv, HECC_CANMIM, 0);
431 hecc_write(priv, HECC_CANME, 0);
432 priv->can.state = CAN_STATE_STOPPED;
433}
434
435static int ti_hecc_do_set_mode(struct net_device *ndev, enum can_mode mode)
436{
437 int ret = 0;
438
439 switch (mode) {
440 case CAN_MODE_START:
441 ti_hecc_start(ndev);
442 netif_wake_queue(ndev);
443 break;
444 default:
445 ret = -EOPNOTSUPP;
446 break;
447 }
448
449 return ret;
450}
451
452static int ti_hecc_get_berr_counter(const struct net_device *ndev,
453 struct can_berr_counter *bec)
454{
455 struct ti_hecc_priv *priv = netdev_priv(ndev);
456
457 bec->txerr = hecc_read(priv, HECC_CANTEC);
458 bec->rxerr = hecc_read(priv, HECC_CANREC);
459
460 return 0;
461}
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485static netdev_tx_t ti_hecc_xmit(struct sk_buff *skb, struct net_device *ndev)
486{
487 struct ti_hecc_priv *priv = netdev_priv(ndev);
488 struct can_frame *cf = (struct can_frame *)skb->data;
489 u32 mbxno, mbx_mask, data;
490 unsigned long flags;
491
492 if (can_dropped_invalid_skb(ndev, skb))
493 return NETDEV_TX_OK;
494
495 mbxno = get_tx_head_mb(priv);
496 mbx_mask = BIT(mbxno);
497 spin_lock_irqsave(&priv->mbx_lock, flags);
498 if (unlikely(hecc_read(priv, HECC_CANME) & mbx_mask)) {
499 spin_unlock_irqrestore(&priv->mbx_lock, flags);
500 netif_stop_queue(ndev);
501 netdev_err(priv->ndev,
502 "BUG: TX mbx not ready tx_head=%08X, tx_tail=%08X\n",
503 priv->tx_head, priv->tx_tail);
504 return NETDEV_TX_BUSY;
505 }
506 spin_unlock_irqrestore(&priv->mbx_lock, flags);
507
508
509 data = cf->can_dlc | (get_tx_head_prio(priv) << 8);
510 if (cf->can_id & CAN_RTR_FLAG)
511 data |= HECC_CANMCF_RTR;
512 hecc_write_mbx(priv, mbxno, HECC_CANMCF, data);
513
514 if (cf->can_id & CAN_EFF_FLAG)
515 data = (cf->can_id & CAN_EFF_MASK) | HECC_CANMID_IDE;
516 else
517 data = (cf->can_id & CAN_SFF_MASK) << 18;
518 hecc_write_mbx(priv, mbxno, HECC_CANMID, data);
519 hecc_write_mbx(priv, mbxno, HECC_CANMDL,
520 be32_to_cpu(*(__be32 *)(cf->data)));
521 if (cf->can_dlc > 4)
522 hecc_write_mbx(priv, mbxno, HECC_CANMDH,
523 be32_to_cpu(*(__be32 *)(cf->data + 4)));
524 else
525 *(u32 *)(cf->data + 4) = 0;
526 can_put_echo_skb(skb, ndev, mbxno);
527
528 spin_lock_irqsave(&priv->mbx_lock, flags);
529 --priv->tx_head;
530 if ((hecc_read(priv, HECC_CANME) & BIT(get_tx_head_mb(priv))) ||
531 (priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK) {
532 netif_stop_queue(ndev);
533 }
534 hecc_set_bit(priv, HECC_CANME, mbx_mask);
535 spin_unlock_irqrestore(&priv->mbx_lock, flags);
536
537 hecc_clear_bit(priv, HECC_CANMD, mbx_mask);
538 hecc_set_bit(priv, HECC_CANMIM, mbx_mask);
539 hecc_write(priv, HECC_CANTRS, mbx_mask);
540
541 return NETDEV_TX_OK;
542}
543
544static int ti_hecc_rx_pkt(struct ti_hecc_priv *priv, int mbxno)
545{
546 struct net_device_stats *stats = &priv->ndev->stats;
547 struct can_frame *cf;
548 struct sk_buff *skb;
549 u32 data, mbx_mask;
550 unsigned long flags;
551
552 skb = alloc_can_skb(priv->ndev, &cf);
553 if (!skb) {
554 if (printk_ratelimit())
555 netdev_err(priv->ndev,
556 "ti_hecc_rx_pkt: alloc_can_skb() failed\n");
557 return -ENOMEM;
558 }
559
560 mbx_mask = BIT(mbxno);
561 data = hecc_read_mbx(priv, mbxno, HECC_CANMID);
562 if (data & HECC_CANMID_IDE)
563 cf->can_id = (data & CAN_EFF_MASK) | CAN_EFF_FLAG;
564 else
565 cf->can_id = (data >> 18) & CAN_SFF_MASK;
566 data = hecc_read_mbx(priv, mbxno, HECC_CANMCF);
567 if (data & HECC_CANMCF_RTR)
568 cf->can_id |= CAN_RTR_FLAG;
569 cf->can_dlc = get_can_dlc(data & 0xF);
570 data = hecc_read_mbx(priv, mbxno, HECC_CANMDL);
571 *(__be32 *)(cf->data) = cpu_to_be32(data);
572 if (cf->can_dlc > 4) {
573 data = hecc_read_mbx(priv, mbxno, HECC_CANMDH);
574 *(__be32 *)(cf->data + 4) = cpu_to_be32(data);
575 }
576 spin_lock_irqsave(&priv->mbx_lock, flags);
577 hecc_clear_bit(priv, HECC_CANME, mbx_mask);
578 hecc_write(priv, HECC_CANRMP, mbx_mask);
579
580 if (priv->rx_next < HECC_RX_BUFFER_MBOX)
581 hecc_set_bit(priv, HECC_CANME, mbx_mask);
582 spin_unlock_irqrestore(&priv->mbx_lock, flags);
583
584 stats->rx_bytes += cf->can_dlc;
585 can_led_event(priv->ndev, CAN_LED_EVENT_RX);
586 netif_receive_skb(skb);
587 stats->rx_packets++;
588
589 return 0;
590}
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613static int ti_hecc_rx_poll(struct napi_struct *napi, int quota)
614{
615 struct net_device *ndev = napi->dev;
616 struct ti_hecc_priv *priv = netdev_priv(ndev);
617 u32 num_pkts = 0;
618 u32 mbx_mask;
619 unsigned long pending_pkts, flags;
620
621 if (!netif_running(ndev))
622 return 0;
623
624 while ((pending_pkts = hecc_read(priv, HECC_CANRMP)) &&
625 num_pkts < quota) {
626 mbx_mask = BIT(priv->rx_next);
627 if (mbx_mask & pending_pkts) {
628 if (ti_hecc_rx_pkt(priv, priv->rx_next) < 0)
629 return num_pkts;
630 ++num_pkts;
631 } else if (priv->rx_next > HECC_RX_BUFFER_MBOX) {
632 break;
633 }
634 --priv->rx_next;
635 if (priv->rx_next == HECC_RX_BUFFER_MBOX) {
636
637 spin_lock_irqsave(&priv->mbx_lock, flags);
638 mbx_mask = hecc_read(priv, HECC_CANME);
639 mbx_mask |= HECC_RX_HIGH_MBOX_MASK;
640 hecc_write(priv, HECC_CANME, mbx_mask);
641 spin_unlock_irqrestore(&priv->mbx_lock, flags);
642 } else if (priv->rx_next == HECC_MAX_TX_MBOX - 1) {
643 priv->rx_next = HECC_RX_FIRST_MBOX;
644 break;
645 }
646 }
647
648
649 if (hecc_read(priv, HECC_CANRMP) == 0) {
650 napi_complete(napi);
651
652 mbx_mask = hecc_read(priv, HECC_CANMIM);
653 mbx_mask |= HECC_TX_MBOX_MASK;
654 hecc_write(priv, HECC_CANMIM, mbx_mask);
655 }
656
657 return num_pkts;
658}
659
660static int ti_hecc_error(struct net_device *ndev, int int_status,
661 int err_status)
662{
663 struct ti_hecc_priv *priv = netdev_priv(ndev);
664 struct net_device_stats *stats = &ndev->stats;
665 struct can_frame *cf;
666 struct sk_buff *skb;
667
668
669 skb = alloc_can_err_skb(ndev, &cf);
670 if (!skb) {
671 if (printk_ratelimit())
672 netdev_err(priv->ndev,
673 "ti_hecc_error: alloc_can_err_skb() failed\n");
674 return -ENOMEM;
675 }
676
677 if (int_status & HECC_CANGIF_WLIF) {
678 if ((int_status & HECC_CANGIF_BOIF) == 0) {
679 priv->can.state = CAN_STATE_ERROR_WARNING;
680 ++priv->can.can_stats.error_warning;
681 cf->can_id |= CAN_ERR_CRTL;
682 if (hecc_read(priv, HECC_CANTEC) > 96)
683 cf->data[1] |= CAN_ERR_CRTL_TX_WARNING;
684 if (hecc_read(priv, HECC_CANREC) > 96)
685 cf->data[1] |= CAN_ERR_CRTL_RX_WARNING;
686 }
687 hecc_set_bit(priv, HECC_CANES, HECC_CANES_EW);
688 netdev_dbg(priv->ndev, "Error Warning interrupt\n");
689 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
690 }
691
692 if (int_status & HECC_CANGIF_EPIF) {
693 if ((int_status & HECC_CANGIF_BOIF) == 0) {
694 priv->can.state = CAN_STATE_ERROR_PASSIVE;
695 ++priv->can.can_stats.error_passive;
696 cf->can_id |= CAN_ERR_CRTL;
697 if (hecc_read(priv, HECC_CANTEC) > 127)
698 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
699 if (hecc_read(priv, HECC_CANREC) > 127)
700 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
701 }
702 hecc_set_bit(priv, HECC_CANES, HECC_CANES_EP);
703 netdev_dbg(priv->ndev, "Error passive interrupt\n");
704 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
705 }
706
707
708
709
710
711 if ((int_status & HECC_CANGIF_BOIF) || (err_status & HECC_CANES_BO)) {
712 priv->can.state = CAN_STATE_BUS_OFF;
713 cf->can_id |= CAN_ERR_BUSOFF;
714 hecc_set_bit(priv, HECC_CANES, HECC_CANES_BO);
715 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
716
717 hecc_write(priv, HECC_CANGIM, 0);
718 ++priv->can.can_stats.bus_off;
719 can_bus_off(ndev);
720 }
721
722 if (err_status & HECC_BUS_ERROR) {
723 ++priv->can.can_stats.bus_error;
724 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
725 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
726 if (err_status & HECC_CANES_FE) {
727 hecc_set_bit(priv, HECC_CANES, HECC_CANES_FE);
728 cf->data[2] |= CAN_ERR_PROT_FORM;
729 }
730 if (err_status & HECC_CANES_BE) {
731 hecc_set_bit(priv, HECC_CANES, HECC_CANES_BE);
732 cf->data[2] |= CAN_ERR_PROT_BIT;
733 }
734 if (err_status & HECC_CANES_SE) {
735 hecc_set_bit(priv, HECC_CANES, HECC_CANES_SE);
736 cf->data[2] |= CAN_ERR_PROT_STUFF;
737 }
738 if (err_status & HECC_CANES_CRCE) {
739 hecc_set_bit(priv, HECC_CANES, HECC_CANES_CRCE);
740 cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ |
741 CAN_ERR_PROT_LOC_CRC_DEL;
742 }
743 if (err_status & HECC_CANES_ACKE) {
744 hecc_set_bit(priv, HECC_CANES, HECC_CANES_ACKE);
745 cf->data[3] |= CAN_ERR_PROT_LOC_ACK |
746 CAN_ERR_PROT_LOC_ACK_DEL;
747 }
748 }
749
750 netif_rx(skb);
751 stats->rx_packets++;
752 stats->rx_bytes += cf->can_dlc;
753
754 return 0;
755}
756
757static irqreturn_t ti_hecc_interrupt(int irq, void *dev_id)
758{
759 struct net_device *ndev = (struct net_device *)dev_id;
760 struct ti_hecc_priv *priv = netdev_priv(ndev);
761 struct net_device_stats *stats = &ndev->stats;
762 u32 mbxno, mbx_mask, int_status, err_status;
763 unsigned long ack, flags;
764
765 int_status = hecc_read(priv,
766 (priv->int_line) ? HECC_CANGIF1 : HECC_CANGIF0);
767
768 if (!int_status)
769 return IRQ_NONE;
770
771 err_status = hecc_read(priv, HECC_CANES);
772 if (err_status & (HECC_BUS_ERROR | HECC_CANES_BO |
773 HECC_CANES_EP | HECC_CANES_EW))
774 ti_hecc_error(ndev, int_status, err_status);
775
776 if (int_status & HECC_CANGIF_GMIF) {
777 while (priv->tx_tail - priv->tx_head > 0) {
778 mbxno = get_tx_tail_mb(priv);
779 mbx_mask = BIT(mbxno);
780 if (!(mbx_mask & hecc_read(priv, HECC_CANTA)))
781 break;
782 hecc_clear_bit(priv, HECC_CANMIM, mbx_mask);
783 hecc_write(priv, HECC_CANTA, mbx_mask);
784 spin_lock_irqsave(&priv->mbx_lock, flags);
785 hecc_clear_bit(priv, HECC_CANME, mbx_mask);
786 spin_unlock_irqrestore(&priv->mbx_lock, flags);
787 stats->tx_bytes += hecc_read_mbx(priv, mbxno,
788 HECC_CANMCF) & 0xF;
789 stats->tx_packets++;
790 can_led_event(ndev, CAN_LED_EVENT_TX);
791 can_get_echo_skb(ndev, mbxno);
792 --priv->tx_tail;
793 }
794
795
796 if (((priv->tx_head == priv->tx_tail) &&
797 ((priv->tx_head & HECC_TX_MASK) != HECC_TX_MASK)) ||
798 (((priv->tx_tail & HECC_TX_MASK) == HECC_TX_MASK) &&
799 ((priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK)))
800 netif_wake_queue(ndev);
801
802
803 if (hecc_read(priv, HECC_CANRMP)) {
804 ack = hecc_read(priv, HECC_CANMIM);
805 ack &= BIT(HECC_MAX_TX_MBOX) - 1;
806 hecc_write(priv, HECC_CANMIM, ack);
807 napi_schedule(&priv->napi);
808 }
809 }
810
811
812 if (priv->int_line) {
813 hecc_write(priv, HECC_CANGIF1, HECC_SET_REG);
814 int_status = hecc_read(priv, HECC_CANGIF1);
815 } else {
816 hecc_write(priv, HECC_CANGIF0, HECC_SET_REG);
817 int_status = hecc_read(priv, HECC_CANGIF0);
818 }
819
820 return IRQ_HANDLED;
821}
822
823static int ti_hecc_open(struct net_device *ndev)
824{
825 struct ti_hecc_priv *priv = netdev_priv(ndev);
826 int err;
827
828 err = request_irq(ndev->irq, ti_hecc_interrupt, IRQF_SHARED,
829 ndev->name, ndev);
830 if (err) {
831 netdev_err(ndev, "error requesting interrupt\n");
832 return err;
833 }
834
835 ti_hecc_transceiver_switch(priv, 1);
836
837
838 err = open_candev(ndev);
839 if (err) {
840 netdev_err(ndev, "open_candev() failed %d\n", err);
841 ti_hecc_transceiver_switch(priv, 0);
842 free_irq(ndev->irq, ndev);
843 return err;
844 }
845
846 can_led_event(ndev, CAN_LED_EVENT_OPEN);
847
848 ti_hecc_start(ndev);
849 napi_enable(&priv->napi);
850 netif_start_queue(ndev);
851
852 return 0;
853}
854
855static int ti_hecc_close(struct net_device *ndev)
856{
857 struct ti_hecc_priv *priv = netdev_priv(ndev);
858
859 netif_stop_queue(ndev);
860 napi_disable(&priv->napi);
861 ti_hecc_stop(ndev);
862 free_irq(ndev->irq, ndev);
863 close_candev(ndev);
864 ti_hecc_transceiver_switch(priv, 0);
865
866 can_led_event(ndev, CAN_LED_EVENT_STOP);
867
868 return 0;
869}
870
871static const struct net_device_ops ti_hecc_netdev_ops = {
872 .ndo_open = ti_hecc_open,
873 .ndo_stop = ti_hecc_close,
874 .ndo_start_xmit = ti_hecc_xmit,
875 .ndo_change_mtu = can_change_mtu,
876};
877
878static int ti_hecc_probe(struct platform_device *pdev)
879{
880 struct net_device *ndev = (struct net_device *)0;
881 struct ti_hecc_priv *priv;
882 struct ti_hecc_platform_data *pdata;
883 struct resource *mem, *irq;
884 void __iomem *addr;
885 int err = -ENODEV;
886
887 pdata = dev_get_platdata(&pdev->dev);
888 if (!pdata) {
889 dev_err(&pdev->dev, "No platform data\n");
890 goto probe_exit;
891 }
892
893 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
894 if (!mem) {
895 dev_err(&pdev->dev, "No mem resources\n");
896 goto probe_exit;
897 }
898 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
899 if (!irq) {
900 dev_err(&pdev->dev, "No irq resource\n");
901 goto probe_exit;
902 }
903 if (!request_mem_region(mem->start, resource_size(mem), pdev->name)) {
904 dev_err(&pdev->dev, "HECC region already claimed\n");
905 err = -EBUSY;
906 goto probe_exit;
907 }
908 addr = ioremap(mem->start, resource_size(mem));
909 if (!addr) {
910 dev_err(&pdev->dev, "ioremap failed\n");
911 err = -ENOMEM;
912 goto probe_exit_free_region;
913 }
914
915 ndev = alloc_candev(sizeof(struct ti_hecc_priv), HECC_MAX_TX_MBOX);
916 if (!ndev) {
917 dev_err(&pdev->dev, "alloc_candev failed\n");
918 err = -ENOMEM;
919 goto probe_exit_iounmap;
920 }
921
922 priv = netdev_priv(ndev);
923 priv->ndev = ndev;
924 priv->base = addr;
925 priv->scc_ram_offset = pdata->scc_ram_offset;
926 priv->hecc_ram_offset = pdata->hecc_ram_offset;
927 priv->mbx_offset = pdata->mbx_offset;
928 priv->int_line = pdata->int_line;
929 priv->transceiver_switch = pdata->transceiver_switch;
930
931 priv->can.bittiming_const = &ti_hecc_bittiming_const;
932 priv->can.do_set_mode = ti_hecc_do_set_mode;
933 priv->can.do_get_berr_counter = ti_hecc_get_berr_counter;
934 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
935
936 spin_lock_init(&priv->mbx_lock);
937 ndev->irq = irq->start;
938 ndev->flags |= IFF_ECHO;
939 platform_set_drvdata(pdev, ndev);
940 SET_NETDEV_DEV(ndev, &pdev->dev);
941 ndev->netdev_ops = &ti_hecc_netdev_ops;
942
943 priv->clk = clk_get(&pdev->dev, "hecc_ck");
944 if (IS_ERR(priv->clk)) {
945 dev_err(&pdev->dev, "No clock available\n");
946 err = PTR_ERR(priv->clk);
947 priv->clk = NULL;
948 goto probe_exit_candev;
949 }
950 priv->can.clock.freq = clk_get_rate(priv->clk);
951 netif_napi_add(ndev, &priv->napi, ti_hecc_rx_poll,
952 HECC_DEF_NAPI_WEIGHT);
953
954 clk_enable(priv->clk);
955 err = register_candev(ndev);
956 if (err) {
957 dev_err(&pdev->dev, "register_candev() failed\n");
958 goto probe_exit_clk;
959 }
960
961 devm_can_led_init(ndev);
962
963 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%u)\n",
964 priv->base, (u32) ndev->irq);
965
966 return 0;
967
968probe_exit_clk:
969 clk_put(priv->clk);
970probe_exit_candev:
971 free_candev(ndev);
972probe_exit_iounmap:
973 iounmap(addr);
974probe_exit_free_region:
975 release_mem_region(mem->start, resource_size(mem));
976probe_exit:
977 return err;
978}
979
980static int ti_hecc_remove(struct platform_device *pdev)
981{
982 struct resource *res;
983 struct net_device *ndev = platform_get_drvdata(pdev);
984 struct ti_hecc_priv *priv = netdev_priv(ndev);
985
986 unregister_candev(ndev);
987 clk_disable(priv->clk);
988 clk_put(priv->clk);
989 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
990 iounmap(priv->base);
991 release_mem_region(res->start, resource_size(res));
992 free_candev(ndev);
993
994 return 0;
995}
996
997
998#ifdef CONFIG_PM
999static int ti_hecc_suspend(struct platform_device *pdev, pm_message_t state)
1000{
1001 struct net_device *dev = platform_get_drvdata(pdev);
1002 struct ti_hecc_priv *priv = netdev_priv(dev);
1003
1004 if (netif_running(dev)) {
1005 netif_stop_queue(dev);
1006 netif_device_detach(dev);
1007 }
1008
1009 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_PDR);
1010 priv->can.state = CAN_STATE_SLEEPING;
1011
1012 clk_disable(priv->clk);
1013
1014 return 0;
1015}
1016
1017static int ti_hecc_resume(struct platform_device *pdev)
1018{
1019 struct net_device *dev = platform_get_drvdata(pdev);
1020 struct ti_hecc_priv *priv = netdev_priv(dev);
1021
1022 clk_enable(priv->clk);
1023
1024 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_PDR);
1025 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1026
1027 if (netif_running(dev)) {
1028 netif_device_attach(dev);
1029 netif_start_queue(dev);
1030 }
1031
1032 return 0;
1033}
1034#else
1035#define ti_hecc_suspend NULL
1036#define ti_hecc_resume NULL
1037#endif
1038
1039
1040static struct platform_driver ti_hecc_driver = {
1041 .driver = {
1042 .name = DRV_NAME,
1043 },
1044 .probe = ti_hecc_probe,
1045 .remove = ti_hecc_remove,
1046 .suspend = ti_hecc_suspend,
1047 .resume = ti_hecc_resume,
1048};
1049
1050module_platform_driver(ti_hecc_driver);
1051
1052MODULE_AUTHOR("Anant Gole <anantgole@ti.com>");
1053MODULE_LICENSE("GPL v2");
1054MODULE_DESCRIPTION(DRV_DESC);
1055MODULE_ALIAS("platform:" DRV_NAME);
1056