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#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/interrupt.h>
31#include <linux/delay.h>
32#include <linux/netdevice.h>
33#include <linux/if_arp.h>
34#include <linux/if_ether.h>
35#include <linux/list.h>
36#include <linux/io.h>
37#include <linux/pm_runtime.h>
38#include <linux/pinctrl/consumer.h>
39
40#include <linux/can.h>
41#include <linux/can/dev.h>
42#include <linux/can/error.h>
43#include <linux/can/led.h>
44
45#include "c_can.h"
46
47
48#define IF_ENUM_REG_LEN 11
49#define C_CAN_IFACE(reg, iface) (C_CAN_IF1_##reg + (iface) * IF_ENUM_REG_LEN)
50
51
52#define CONTROL_EX_PDR BIT(8)
53
54
55#define CONTROL_TEST BIT(7)
56#define CONTROL_CCE BIT(6)
57#define CONTROL_DISABLE_AR BIT(5)
58#define CONTROL_ENABLE_AR (0 << 5)
59#define CONTROL_EIE BIT(3)
60#define CONTROL_SIE BIT(2)
61#define CONTROL_IE BIT(1)
62#define CONTROL_INIT BIT(0)
63
64#define CONTROL_IRQMSK (CONTROL_EIE | CONTROL_IE | CONTROL_SIE)
65
66
67#define TEST_RX BIT(7)
68#define TEST_TX1 BIT(6)
69#define TEST_TX2 BIT(5)
70#define TEST_LBACK BIT(4)
71#define TEST_SILENT BIT(3)
72#define TEST_BASIC BIT(2)
73
74
75#define STATUS_PDA BIT(10)
76#define STATUS_BOFF BIT(7)
77#define STATUS_EWARN BIT(6)
78#define STATUS_EPASS BIT(5)
79#define STATUS_RXOK BIT(4)
80#define STATUS_TXOK BIT(3)
81
82
83#define ERR_CNT_TEC_MASK 0xff
84#define ERR_CNT_TEC_SHIFT 0
85#define ERR_CNT_REC_SHIFT 8
86#define ERR_CNT_REC_MASK (0x7f << ERR_CNT_REC_SHIFT)
87#define ERR_CNT_RP_SHIFT 15
88#define ERR_CNT_RP_MASK (0x1 << ERR_CNT_RP_SHIFT)
89
90
91#define BTR_BRP_MASK 0x3f
92#define BTR_BRP_SHIFT 0
93#define BTR_SJW_SHIFT 6
94#define BTR_SJW_MASK (0x3 << BTR_SJW_SHIFT)
95#define BTR_TSEG1_SHIFT 8
96#define BTR_TSEG1_MASK (0xf << BTR_TSEG1_SHIFT)
97#define BTR_TSEG2_SHIFT 12
98#define BTR_TSEG2_MASK (0x7 << BTR_TSEG2_SHIFT)
99
100
101#define BRP_EXT_BRPE_MASK 0x0f
102#define BRP_EXT_BRPE_SHIFT 0
103
104
105#define IF_COMR_BUSY BIT(15)
106
107
108#define IF_COMM_WR BIT(7)
109#define IF_COMM_MASK BIT(6)
110#define IF_COMM_ARB BIT(5)
111#define IF_COMM_CONTROL BIT(4)
112#define IF_COMM_CLR_INT_PND BIT(3)
113#define IF_COMM_TXRQST BIT(2)
114#define IF_COMM_CLR_NEWDAT IF_COMM_TXRQST
115#define IF_COMM_DATAA BIT(1)
116#define IF_COMM_DATAB BIT(0)
117
118
119#define IF_COMM_TX (IF_COMM_ARB | IF_COMM_CONTROL | \
120 IF_COMM_TXRQST | \
121 IF_COMM_DATAA | IF_COMM_DATAB)
122
123
124#define IF_COMM_RCV_LOW (IF_COMM_MASK | IF_COMM_ARB | \
125 IF_COMM_CONTROL | IF_COMM_CLR_INT_PND | \
126 IF_COMM_DATAA | IF_COMM_DATAB)
127
128
129#define IF_COMM_RCV_HIGH (IF_COMM_RCV_LOW | IF_COMM_CLR_NEWDAT)
130
131
132
133#define IF_COMM_RCV_SETUP (IF_COMM_MASK | IF_COMM_ARB | IF_COMM_CONTROL)
134
135
136#define IF_COMM_INVAL (IF_COMM_ARB | IF_COMM_CONTROL)
137
138
139#define IF_ARB_MSGVAL BIT(31)
140#define IF_ARB_MSGXTD BIT(30)
141#define IF_ARB_TRANSMIT BIT(29)
142
143
144#define IF_MCONT_NEWDAT BIT(15)
145#define IF_MCONT_MSGLST BIT(14)
146#define IF_MCONT_INTPND BIT(13)
147#define IF_MCONT_UMASK BIT(12)
148#define IF_MCONT_TXIE BIT(11)
149#define IF_MCONT_RXIE BIT(10)
150#define IF_MCONT_RMTEN BIT(9)
151#define IF_MCONT_TXRQST BIT(8)
152#define IF_MCONT_EOB BIT(7)
153#define IF_MCONT_DLC_MASK 0xf
154
155#define IF_MCONT_RCV (IF_MCONT_RXIE | IF_MCONT_UMASK)
156#define IF_MCONT_RCV_EOB (IF_MCONT_RCV | IF_MCONT_EOB)
157
158#define IF_MCONT_TX (IF_MCONT_TXIE | IF_MCONT_EOB)
159
160
161
162
163#define IF_RX 0
164#define IF_TX 1
165
166
167#define MIN_TIMEOUT_VALUE 6
168
169
170#define INIT_WAIT_MS 1000
171
172
173#define C_CAN_NAPI_WEIGHT C_CAN_MSG_OBJ_RX_NUM
174
175
176enum c_can_lec_type {
177 LEC_NO_ERROR = 0,
178 LEC_STUFF_ERROR,
179 LEC_FORM_ERROR,
180 LEC_ACK_ERROR,
181 LEC_BIT1_ERROR,
182 LEC_BIT0_ERROR,
183 LEC_CRC_ERROR,
184 LEC_UNUSED,
185 LEC_MASK = LEC_UNUSED,
186};
187
188
189
190
191
192enum c_can_bus_error_types {
193 C_CAN_NO_ERROR = 0,
194 C_CAN_BUS_OFF,
195 C_CAN_ERROR_WARNING,
196 C_CAN_ERROR_PASSIVE,
197};
198
199static const struct can_bittiming_const c_can_bittiming_const = {
200 .name = KBUILD_MODNAME,
201 .tseg1_min = 2,
202 .tseg1_max = 16,
203 .tseg2_min = 1,
204 .tseg2_max = 8,
205 .sjw_max = 4,
206 .brp_min = 1,
207 .brp_max = 1024,
208 .brp_inc = 1,
209};
210
211static inline void c_can_pm_runtime_enable(const struct c_can_priv *priv)
212{
213 if (priv->device)
214 pm_runtime_enable(priv->device);
215}
216
217static inline void c_can_pm_runtime_disable(const struct c_can_priv *priv)
218{
219 if (priv->device)
220 pm_runtime_disable(priv->device);
221}
222
223static inline void c_can_pm_runtime_get_sync(const struct c_can_priv *priv)
224{
225 if (priv->device)
226 pm_runtime_get_sync(priv->device);
227}
228
229static inline void c_can_pm_runtime_put_sync(const struct c_can_priv *priv)
230{
231 if (priv->device)
232 pm_runtime_put_sync(priv->device);
233}
234
235static inline void c_can_reset_ram(const struct c_can_priv *priv, bool enable)
236{
237 if (priv->raminit)
238 priv->raminit(priv, enable);
239}
240
241static void c_can_irq_control(struct c_can_priv *priv, bool enable)
242{
243 u32 ctrl = priv->read_reg(priv, C_CAN_CTRL_REG) & ~CONTROL_IRQMSK;
244
245 if (enable)
246 ctrl |= CONTROL_IRQMSK;
247
248 priv->write_reg(priv, C_CAN_CTRL_REG, ctrl);
249}
250
251static void c_can_obj_update(struct net_device *dev, int iface, u32 cmd, u32 obj)
252{
253 struct c_can_priv *priv = netdev_priv(dev);
254 int cnt, reg = C_CAN_IFACE(COMREQ_REG, iface);
255
256 priv->write_reg32(priv, reg, (cmd << 16) | obj);
257
258 for (cnt = MIN_TIMEOUT_VALUE; cnt; cnt--) {
259 if (!(priv->read_reg(priv, reg) & IF_COMR_BUSY))
260 return;
261 udelay(1);
262 }
263 netdev_err(dev, "Updating object timed out\n");
264
265}
266
267static inline void c_can_object_get(struct net_device *dev, int iface,
268 u32 obj, u32 cmd)
269{
270 c_can_obj_update(dev, iface, cmd, obj);
271}
272
273static inline void c_can_object_put(struct net_device *dev, int iface,
274 u32 obj, u32 cmd)
275{
276 c_can_obj_update(dev, iface, cmd | IF_COMM_WR, obj);
277}
278
279
280
281
282
283
284static void c_can_inval_tx_object(struct net_device *dev, int iface, int obj)
285{
286 struct c_can_priv *priv = netdev_priv(dev);
287
288 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), 0);
289 c_can_object_put(dev, iface, obj, IF_COMM_INVAL);
290}
291
292static void c_can_inval_msg_object(struct net_device *dev, int iface, int obj)
293{
294 struct c_can_priv *priv = netdev_priv(dev);
295
296 priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface), 0);
297 priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), 0);
298 c_can_inval_tx_object(dev, iface, obj);
299}
300
301static void c_can_setup_tx_object(struct net_device *dev, int iface,
302 struct can_frame *frame, int idx)
303{
304 struct c_can_priv *priv = netdev_priv(dev);
305 u16 ctrl = IF_MCONT_TX | frame->can_dlc;
306 bool rtr = frame->can_id & CAN_RTR_FLAG;
307 u32 arb = IF_ARB_MSGVAL;
308 int i;
309
310 if (frame->can_id & CAN_EFF_FLAG) {
311 arb |= frame->can_id & CAN_EFF_MASK;
312 arb |= IF_ARB_MSGXTD;
313 } else {
314 arb |= (frame->can_id & CAN_SFF_MASK) << 18;
315 }
316
317 if (!rtr)
318 arb |= IF_ARB_TRANSMIT;
319
320
321
322
323
324 if (rtr != (bool)test_bit(idx, &priv->tx_dir)) {
325 u32 obj = idx + C_CAN_MSG_OBJ_TX_FIRST;
326
327 c_can_inval_msg_object(dev, iface, obj);
328 change_bit(idx, &priv->tx_dir);
329 }
330
331 priv->write_reg32(priv, C_CAN_IFACE(ARB1_REG, iface), arb);
332
333 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), ctrl);
334
335 for (i = 0; i < frame->can_dlc; i += 2) {
336 priv->write_reg(priv, C_CAN_IFACE(DATA1_REG, iface) + i / 2,
337 frame->data[i] | (frame->data[i + 1] << 8));
338 }
339}
340
341static inline void c_can_activate_all_lower_rx_msg_obj(struct net_device *dev,
342 int iface)
343{
344 int i;
345
346 for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_MSG_RX_LOW_LAST; i++)
347 c_can_object_get(dev, iface, i, IF_COMM_CLR_NEWDAT);
348}
349
350static int c_can_handle_lost_msg_obj(struct net_device *dev,
351 int iface, int objno, u32 ctrl)
352{
353 struct net_device_stats *stats = &dev->stats;
354 struct c_can_priv *priv = netdev_priv(dev);
355 struct can_frame *frame;
356 struct sk_buff *skb;
357
358 ctrl &= ~(IF_MCONT_MSGLST | IF_MCONT_INTPND | IF_MCONT_NEWDAT);
359 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), ctrl);
360 c_can_object_put(dev, iface, objno, IF_COMM_CONTROL);
361
362 stats->rx_errors++;
363 stats->rx_over_errors++;
364
365
366 skb = alloc_can_err_skb(dev, &frame);
367 if (unlikely(!skb))
368 return 0;
369
370 frame->can_id |= CAN_ERR_CRTL;
371 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
372
373 netif_receive_skb(skb);
374 return 1;
375}
376
377static int c_can_read_msg_object(struct net_device *dev, int iface, u32 ctrl)
378{
379 struct net_device_stats *stats = &dev->stats;
380 struct c_can_priv *priv = netdev_priv(dev);
381 struct can_frame *frame;
382 struct sk_buff *skb;
383 u32 arb, data;
384
385 skb = alloc_can_skb(dev, &frame);
386 if (!skb) {
387 stats->rx_dropped++;
388 return -ENOMEM;
389 }
390
391 frame->can_dlc = get_can_dlc(ctrl & 0x0F);
392
393 arb = priv->read_reg32(priv, C_CAN_IFACE(ARB1_REG, iface));
394
395 if (arb & IF_ARB_MSGXTD)
396 frame->can_id = (arb & CAN_EFF_MASK) | CAN_EFF_FLAG;
397 else
398 frame->can_id = (arb >> 18) & CAN_SFF_MASK;
399
400 if (arb & IF_ARB_TRANSMIT) {
401 frame->can_id |= CAN_RTR_FLAG;
402 } else {
403 int i, dreg = C_CAN_IFACE(DATA1_REG, iface);
404
405 for (i = 0; i < frame->can_dlc; i += 2, dreg ++) {
406 data = priv->read_reg(priv, dreg);
407 frame->data[i] = data;
408 frame->data[i + 1] = data >> 8;
409 }
410 }
411
412 stats->rx_packets++;
413 stats->rx_bytes += frame->can_dlc;
414
415 netif_receive_skb(skb);
416 return 0;
417}
418
419static void c_can_setup_receive_object(struct net_device *dev, int iface,
420 u32 obj, u32 mask, u32 id, u32 mcont)
421{
422 struct c_can_priv *priv = netdev_priv(dev);
423
424 mask |= BIT(29);
425 priv->write_reg32(priv, C_CAN_IFACE(MASK1_REG, iface), mask);
426
427 id |= IF_ARB_MSGVAL;
428 priv->write_reg32(priv, C_CAN_IFACE(ARB1_REG, iface), id);
429
430 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), mcont);
431 c_can_object_put(dev, iface, obj, IF_COMM_RCV_SETUP);
432}
433
434static netdev_tx_t c_can_start_xmit(struct sk_buff *skb,
435 struct net_device *dev)
436{
437 struct can_frame *frame = (struct can_frame *)skb->data;
438 struct c_can_priv *priv = netdev_priv(dev);
439 u32 idx, obj;
440
441 if (can_dropped_invalid_skb(dev, skb))
442 return NETDEV_TX_OK;
443
444
445
446
447 idx = fls(atomic_read(&priv->tx_active));
448 obj = idx + C_CAN_MSG_OBJ_TX_FIRST;
449
450
451 if (idx == C_CAN_MSG_OBJ_TX_NUM - 1)
452 netif_stop_queue(dev);
453
454
455
456
457
458 c_can_setup_tx_object(dev, IF_TX, frame, idx);
459 priv->dlc[idx] = frame->can_dlc;
460 can_put_echo_skb(skb, dev, idx);
461
462
463 atomic_add((1 << idx), &priv->tx_active);
464
465 c_can_object_put(dev, IF_TX, obj, IF_COMM_TX);
466
467 return NETDEV_TX_OK;
468}
469
470static int c_can_wait_for_ctrl_init(struct net_device *dev,
471 struct c_can_priv *priv, u32 init)
472{
473 int retry = 0;
474
475 while (init != (priv->read_reg(priv, C_CAN_CTRL_REG) & CONTROL_INIT)) {
476 udelay(10);
477 if (retry++ > 1000) {
478 netdev_err(dev, "CCTRL: set CONTROL_INIT failed\n");
479 return -EIO;
480 }
481 }
482 return 0;
483}
484
485static int c_can_set_bittiming(struct net_device *dev)
486{
487 unsigned int reg_btr, reg_brpe, ctrl_save;
488 u8 brp, brpe, sjw, tseg1, tseg2;
489 u32 ten_bit_brp;
490 struct c_can_priv *priv = netdev_priv(dev);
491 const struct can_bittiming *bt = &priv->can.bittiming;
492 int res;
493
494
495 ten_bit_brp = bt->brp - 1;
496 brp = ten_bit_brp & BTR_BRP_MASK;
497 brpe = ten_bit_brp >> 6;
498
499 sjw = bt->sjw - 1;
500 tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
501 tseg2 = bt->phase_seg2 - 1;
502 reg_btr = brp | (sjw << BTR_SJW_SHIFT) | (tseg1 << BTR_TSEG1_SHIFT) |
503 (tseg2 << BTR_TSEG2_SHIFT);
504 reg_brpe = brpe & BRP_EXT_BRPE_MASK;
505
506 netdev_info(dev,
507 "setting BTR=%04x BRPE=%04x\n", reg_btr, reg_brpe);
508
509 ctrl_save = priv->read_reg(priv, C_CAN_CTRL_REG);
510 ctrl_save &= ~CONTROL_INIT;
511 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_CCE | CONTROL_INIT);
512 res = c_can_wait_for_ctrl_init(dev, priv, CONTROL_INIT);
513 if (res)
514 return res;
515
516 priv->write_reg(priv, C_CAN_BTR_REG, reg_btr);
517 priv->write_reg(priv, C_CAN_BRPEXT_REG, reg_brpe);
518 priv->write_reg(priv, C_CAN_CTRL_REG, ctrl_save);
519
520 return c_can_wait_for_ctrl_init(dev, priv, 0);
521}
522
523
524
525
526
527
528
529
530
531
532static void c_can_configure_msg_objects(struct net_device *dev)
533{
534 int i;
535
536
537 for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_NO_OF_OBJECTS; i++)
538 c_can_inval_msg_object(dev, IF_RX, i);
539
540
541 for (i = C_CAN_MSG_OBJ_RX_FIRST; i < C_CAN_MSG_OBJ_RX_LAST; i++)
542 c_can_setup_receive_object(dev, IF_RX, i, 0, 0, IF_MCONT_RCV);
543
544 c_can_setup_receive_object(dev, IF_RX, C_CAN_MSG_OBJ_RX_LAST, 0, 0,
545 IF_MCONT_RCV_EOB);
546}
547
548
549
550
551
552
553
554static int c_can_chip_config(struct net_device *dev)
555{
556 struct c_can_priv *priv = netdev_priv(dev);
557
558
559 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_ENABLE_AR);
560
561 if ((priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) &&
562 (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)) {
563
564 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST);
565 priv->write_reg(priv, C_CAN_TEST_REG, TEST_LBACK | TEST_SILENT);
566 } else if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
567
568 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST);
569 priv->write_reg(priv, C_CAN_TEST_REG, TEST_LBACK);
570 } else if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
571
572 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST);
573 priv->write_reg(priv, C_CAN_TEST_REG, TEST_SILENT);
574 }
575
576
577 c_can_configure_msg_objects(dev);
578
579
580 priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED);
581
582
583 atomic_set(&priv->tx_active, 0);
584 priv->rxmasked = 0;
585 priv->tx_dir = 0;
586
587
588 return c_can_set_bittiming(dev);
589}
590
591static int c_can_start(struct net_device *dev)
592{
593 struct c_can_priv *priv = netdev_priv(dev);
594 int err;
595
596
597 err = c_can_chip_config(dev);
598 if (err)
599 return err;
600
601
602 priv->comm_rcv_high = priv->type != BOSCH_D_CAN ?
603 IF_COMM_RCV_LOW : IF_COMM_RCV_HIGH;
604
605 priv->can.state = CAN_STATE_ERROR_ACTIVE;
606
607
608 pinctrl_pm_select_default_state(dev->dev.parent);
609 return 0;
610}
611
612static void c_can_stop(struct net_device *dev)
613{
614 struct c_can_priv *priv = netdev_priv(dev);
615
616 c_can_irq_control(priv, false);
617
618
619 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_INIT);
620
621
622 pinctrl_pm_select_sleep_state(dev->dev.parent);
623 priv->can.state = CAN_STATE_STOPPED;
624}
625
626static int c_can_set_mode(struct net_device *dev, enum can_mode mode)
627{
628 struct c_can_priv *priv = netdev_priv(dev);
629 int err;
630
631 switch (mode) {
632 case CAN_MODE_START:
633 err = c_can_start(dev);
634 if (err)
635 return err;
636 netif_wake_queue(dev);
637 c_can_irq_control(priv, true);
638 break;
639 default:
640 return -EOPNOTSUPP;
641 }
642
643 return 0;
644}
645
646static int __c_can_get_berr_counter(const struct net_device *dev,
647 struct can_berr_counter *bec)
648{
649 unsigned int reg_err_counter;
650 struct c_can_priv *priv = netdev_priv(dev);
651
652 reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
653 bec->rxerr = (reg_err_counter & ERR_CNT_REC_MASK) >>
654 ERR_CNT_REC_SHIFT;
655 bec->txerr = reg_err_counter & ERR_CNT_TEC_MASK;
656
657 return 0;
658}
659
660static int c_can_get_berr_counter(const struct net_device *dev,
661 struct can_berr_counter *bec)
662{
663 struct c_can_priv *priv = netdev_priv(dev);
664 int err;
665
666 c_can_pm_runtime_get_sync(priv);
667 err = __c_can_get_berr_counter(dev, bec);
668 c_can_pm_runtime_put_sync(priv);
669
670 return err;
671}
672
673static void c_can_do_tx(struct net_device *dev)
674{
675 struct c_can_priv *priv = netdev_priv(dev);
676 struct net_device_stats *stats = &dev->stats;
677 u32 idx, obj, pkts = 0, bytes = 0, pend, clr;
678
679 clr = pend = priv->read_reg(priv, C_CAN_INTPND2_REG);
680
681 while ((idx = ffs(pend))) {
682 idx--;
683 pend &= ~(1 << idx);
684 obj = idx + C_CAN_MSG_OBJ_TX_FIRST;
685 c_can_inval_tx_object(dev, IF_RX, obj);
686 can_get_echo_skb(dev, idx);
687 bytes += priv->dlc[idx];
688 pkts++;
689 }
690
691
692 atomic_sub(clr, &priv->tx_active);
693
694 if (clr & (1 << (C_CAN_MSG_OBJ_TX_NUM - 1)))
695 netif_wake_queue(dev);
696
697 if (pkts) {
698 stats->tx_bytes += bytes;
699 stats->tx_packets += pkts;
700 can_led_event(dev, CAN_LED_EVENT_TX);
701 }
702}
703
704
705
706
707
708
709static u32 c_can_adjust_pending(u32 pend)
710{
711 u32 weight, lasts;
712
713 if (pend == RECEIVE_OBJECT_BITS)
714 return pend;
715
716
717
718
719
720 weight = hweight32(pend);
721 lasts = fls(pend);
722
723
724 if (lasts == weight)
725 return pend;
726
727
728
729
730
731 for (lasts--; pend & (1 << (lasts - 1)); lasts--);
732
733 return pend & ~((1 << lasts) - 1);
734}
735
736static inline void c_can_rx_object_get(struct net_device *dev,
737 struct c_can_priv *priv, u32 obj)
738{
739 c_can_object_get(dev, IF_RX, obj, priv->comm_rcv_high);
740}
741
742static inline void c_can_rx_finalize(struct net_device *dev,
743 struct c_can_priv *priv, u32 obj)
744{
745 if (priv->type != BOSCH_D_CAN)
746 c_can_object_get(dev, IF_RX, obj, IF_COMM_CLR_NEWDAT);
747}
748
749static int c_can_read_objects(struct net_device *dev, struct c_can_priv *priv,
750 u32 pend, int quota)
751{
752 u32 pkts = 0, ctrl, obj;
753
754 while ((obj = ffs(pend)) && quota > 0) {
755 pend &= ~BIT(obj - 1);
756
757 c_can_rx_object_get(dev, priv, obj);
758 ctrl = priv->read_reg(priv, C_CAN_IFACE(MSGCTRL_REG, IF_RX));
759
760 if (ctrl & IF_MCONT_MSGLST) {
761 int n = c_can_handle_lost_msg_obj(dev, IF_RX, obj, ctrl);
762
763 pkts += n;
764 quota -= n;
765 continue;
766 }
767
768
769
770
771
772
773 if (!(ctrl & IF_MCONT_NEWDAT))
774 continue;
775
776
777 c_can_read_msg_object(dev, IF_RX, ctrl);
778
779 c_can_rx_finalize(dev, priv, obj);
780
781 pkts++;
782 quota--;
783 }
784
785 return pkts;
786}
787
788static inline u32 c_can_get_pending(struct c_can_priv *priv)
789{
790 u32 pend = priv->read_reg(priv, C_CAN_NEWDAT1_REG);
791
792 return pend;
793}
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808static int c_can_do_rx_poll(struct net_device *dev, int quota)
809{
810 struct c_can_priv *priv = netdev_priv(dev);
811 u32 pkts = 0, pend = 0, toread, n;
812
813
814
815
816
817 BUILD_BUG_ON_MSG(C_CAN_MSG_OBJ_RX_LAST > 16,
818 "Implementation does not support more message objects than 16");
819
820 while (quota > 0) {
821 if (!pend) {
822 pend = c_can_get_pending(priv);
823 if (!pend)
824 break;
825
826
827
828
829 toread = c_can_adjust_pending(pend);
830 } else {
831 toread = pend;
832 }
833
834 pend &= ~toread;
835
836 n = c_can_read_objects(dev, priv, toread, quota);
837 pkts += n;
838 quota -= n;
839 }
840
841 if (pkts)
842 can_led_event(dev, CAN_LED_EVENT_RX);
843
844 return pkts;
845}
846
847static int c_can_handle_state_change(struct net_device *dev,
848 enum c_can_bus_error_types error_type)
849{
850 unsigned int reg_err_counter;
851 unsigned int rx_err_passive;
852 struct c_can_priv *priv = netdev_priv(dev);
853 struct net_device_stats *stats = &dev->stats;
854 struct can_frame *cf;
855 struct sk_buff *skb;
856 struct can_berr_counter bec;
857
858 switch (error_type) {
859 case C_CAN_ERROR_WARNING:
860
861 priv->can.can_stats.error_warning++;
862 priv->can.state = CAN_STATE_ERROR_WARNING;
863 break;
864 case C_CAN_ERROR_PASSIVE:
865
866 priv->can.can_stats.error_passive++;
867 priv->can.state = CAN_STATE_ERROR_PASSIVE;
868 break;
869 case C_CAN_BUS_OFF:
870
871 priv->can.state = CAN_STATE_BUS_OFF;
872 priv->can.can_stats.bus_off++;
873 break;
874 default:
875 break;
876 }
877
878
879 skb = alloc_can_err_skb(dev, &cf);
880 if (unlikely(!skb))
881 return 0;
882
883 __c_can_get_berr_counter(dev, &bec);
884 reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
885 rx_err_passive = (reg_err_counter & ERR_CNT_RP_MASK) >>
886 ERR_CNT_RP_SHIFT;
887
888 switch (error_type) {
889 case C_CAN_ERROR_WARNING:
890
891 cf->can_id |= CAN_ERR_CRTL;
892 cf->data[1] = (bec.txerr > bec.rxerr) ?
893 CAN_ERR_CRTL_TX_WARNING :
894 CAN_ERR_CRTL_RX_WARNING;
895 cf->data[6] = bec.txerr;
896 cf->data[7] = bec.rxerr;
897
898 break;
899 case C_CAN_ERROR_PASSIVE:
900
901 cf->can_id |= CAN_ERR_CRTL;
902 if (rx_err_passive)
903 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
904 if (bec.txerr > 127)
905 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
906
907 cf->data[6] = bec.txerr;
908 cf->data[7] = bec.rxerr;
909 break;
910 case C_CAN_BUS_OFF:
911
912 cf->can_id |= CAN_ERR_BUSOFF;
913 can_bus_off(dev);
914 break;
915 default:
916 break;
917 }
918
919 stats->rx_packets++;
920 stats->rx_bytes += cf->can_dlc;
921 netif_receive_skb(skb);
922
923 return 1;
924}
925
926static int c_can_handle_bus_err(struct net_device *dev,
927 enum c_can_lec_type lec_type)
928{
929 struct c_can_priv *priv = netdev_priv(dev);
930 struct net_device_stats *stats = &dev->stats;
931 struct can_frame *cf;
932 struct sk_buff *skb;
933
934
935
936
937
938
939 if (lec_type == LEC_UNUSED || lec_type == LEC_NO_ERROR)
940 return 0;
941
942 if (!(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
943 return 0;
944
945
946 priv->can.can_stats.bus_error++;
947 stats->rx_errors++;
948
949
950 skb = alloc_can_err_skb(dev, &cf);
951 if (unlikely(!skb))
952 return 0;
953
954
955
956
957
958 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
959 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
960
961 switch (lec_type) {
962 case LEC_STUFF_ERROR:
963 netdev_dbg(dev, "stuff error\n");
964 cf->data[2] |= CAN_ERR_PROT_STUFF;
965 break;
966 case LEC_FORM_ERROR:
967 netdev_dbg(dev, "form error\n");
968 cf->data[2] |= CAN_ERR_PROT_FORM;
969 break;
970 case LEC_ACK_ERROR:
971 netdev_dbg(dev, "ack error\n");
972 cf->data[3] |= (CAN_ERR_PROT_LOC_ACK |
973 CAN_ERR_PROT_LOC_ACK_DEL);
974 break;
975 case LEC_BIT1_ERROR:
976 netdev_dbg(dev, "bit1 error\n");
977 cf->data[2] |= CAN_ERR_PROT_BIT1;
978 break;
979 case LEC_BIT0_ERROR:
980 netdev_dbg(dev, "bit0 error\n");
981 cf->data[2] |= CAN_ERR_PROT_BIT0;
982 break;
983 case LEC_CRC_ERROR:
984 netdev_dbg(dev, "CRC error\n");
985 cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
986 CAN_ERR_PROT_LOC_CRC_DEL);
987 break;
988 default:
989 break;
990 }
991
992 stats->rx_packets++;
993 stats->rx_bytes += cf->can_dlc;
994 netif_receive_skb(skb);
995 return 1;
996}
997
998static int c_can_poll(struct napi_struct *napi, int quota)
999{
1000 struct net_device *dev = napi->dev;
1001 struct c_can_priv *priv = netdev_priv(dev);
1002 u16 curr, last = priv->last_status;
1003 int work_done = 0;
1004
1005 priv->last_status = curr = priv->read_reg(priv, C_CAN_STS_REG);
1006
1007 if (priv->type != BOSCH_D_CAN)
1008 priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED);
1009
1010
1011 if ((curr & STATUS_EWARN) && (!(last & STATUS_EWARN))) {
1012 netdev_dbg(dev, "entered error warning state\n");
1013 work_done += c_can_handle_state_change(dev, C_CAN_ERROR_WARNING);
1014 }
1015
1016 if ((curr & STATUS_EPASS) && (!(last & STATUS_EPASS))) {
1017 netdev_dbg(dev, "entered error passive state\n");
1018 work_done += c_can_handle_state_change(dev, C_CAN_ERROR_PASSIVE);
1019 }
1020
1021 if ((curr & STATUS_BOFF) && (!(last & STATUS_BOFF))) {
1022 netdev_dbg(dev, "entered bus off state\n");
1023 work_done += c_can_handle_state_change(dev, C_CAN_BUS_OFF);
1024 goto end;
1025 }
1026
1027
1028 if ((!(curr & STATUS_BOFF)) && (last & STATUS_BOFF)) {
1029 netdev_dbg(dev, "left bus off state\n");
1030 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1031 }
1032 if ((!(curr & STATUS_EPASS)) && (last & STATUS_EPASS)) {
1033 netdev_dbg(dev, "left error passive state\n");
1034 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1035 }
1036
1037
1038 work_done += c_can_handle_bus_err(dev, curr & LEC_MASK);
1039
1040
1041 work_done += c_can_do_rx_poll(dev, (quota - work_done));
1042 c_can_do_tx(dev);
1043
1044end:
1045 if (work_done < quota) {
1046 napi_complete(napi);
1047
1048 if (priv->can.state != CAN_STATE_BUS_OFF)
1049 c_can_irq_control(priv, true);
1050 }
1051
1052 return work_done;
1053}
1054
1055static irqreturn_t c_can_isr(int irq, void *dev_id)
1056{
1057 struct net_device *dev = (struct net_device *)dev_id;
1058 struct c_can_priv *priv = netdev_priv(dev);
1059
1060 if (!priv->read_reg(priv, C_CAN_INT_REG))
1061 return IRQ_NONE;
1062
1063
1064 c_can_irq_control(priv, false);
1065 napi_schedule(&priv->napi);
1066
1067 return IRQ_HANDLED;
1068}
1069
1070static int c_can_open(struct net_device *dev)
1071{
1072 int err;
1073 struct c_can_priv *priv = netdev_priv(dev);
1074
1075 c_can_pm_runtime_get_sync(priv);
1076 c_can_reset_ram(priv, true);
1077
1078
1079 err = open_candev(dev);
1080 if (err) {
1081 netdev_err(dev, "failed to open can device\n");
1082 goto exit_open_fail;
1083 }
1084
1085
1086 err = request_irq(dev->irq, &c_can_isr, IRQF_SHARED, dev->name,
1087 dev);
1088 if (err < 0) {
1089 netdev_err(dev, "failed to request interrupt\n");
1090 goto exit_irq_fail;
1091 }
1092
1093
1094 err = c_can_start(dev);
1095 if (err)
1096 goto exit_start_fail;
1097
1098 can_led_event(dev, CAN_LED_EVENT_OPEN);
1099
1100 napi_enable(&priv->napi);
1101
1102 c_can_irq_control(priv, true);
1103 netif_start_queue(dev);
1104
1105 return 0;
1106
1107exit_start_fail:
1108 free_irq(dev->irq, dev);
1109exit_irq_fail:
1110 close_candev(dev);
1111exit_open_fail:
1112 c_can_reset_ram(priv, false);
1113 c_can_pm_runtime_put_sync(priv);
1114 return err;
1115}
1116
1117static int c_can_close(struct net_device *dev)
1118{
1119 struct c_can_priv *priv = netdev_priv(dev);
1120
1121 netif_stop_queue(dev);
1122 napi_disable(&priv->napi);
1123 c_can_stop(dev);
1124 free_irq(dev->irq, dev);
1125 close_candev(dev);
1126
1127 c_can_reset_ram(priv, false);
1128 c_can_pm_runtime_put_sync(priv);
1129
1130 can_led_event(dev, CAN_LED_EVENT_STOP);
1131
1132 return 0;
1133}
1134
1135struct net_device *alloc_c_can_dev(void)
1136{
1137 struct net_device *dev;
1138 struct c_can_priv *priv;
1139
1140 dev = alloc_candev(sizeof(struct c_can_priv), C_CAN_MSG_OBJ_TX_NUM);
1141 if (!dev)
1142 return NULL;
1143
1144 priv = netdev_priv(dev);
1145 netif_napi_add(dev, &priv->napi, c_can_poll, C_CAN_NAPI_WEIGHT);
1146
1147 priv->dev = dev;
1148 priv->can.bittiming_const = &c_can_bittiming_const;
1149 priv->can.do_set_mode = c_can_set_mode;
1150 priv->can.do_get_berr_counter = c_can_get_berr_counter;
1151 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1152 CAN_CTRLMODE_LISTENONLY |
1153 CAN_CTRLMODE_BERR_REPORTING;
1154
1155 return dev;
1156}
1157EXPORT_SYMBOL_GPL(alloc_c_can_dev);
1158
1159#ifdef CONFIG_PM
1160int c_can_power_down(struct net_device *dev)
1161{
1162 u32 val;
1163 unsigned long time_out;
1164 struct c_can_priv *priv = netdev_priv(dev);
1165
1166 if (!(dev->flags & IFF_UP))
1167 return 0;
1168
1169 WARN_ON(priv->type != BOSCH_D_CAN);
1170
1171
1172 val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
1173 val |= CONTROL_EX_PDR;
1174 priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
1175
1176
1177 time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS);
1178 while (!(priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) &&
1179 time_after(time_out, jiffies))
1180 cpu_relax();
1181
1182 if (time_after(jiffies, time_out))
1183 return -ETIMEDOUT;
1184
1185 c_can_stop(dev);
1186
1187 c_can_reset_ram(priv, false);
1188 c_can_pm_runtime_put_sync(priv);
1189
1190 return 0;
1191}
1192EXPORT_SYMBOL_GPL(c_can_power_down);
1193
1194int c_can_power_up(struct net_device *dev)
1195{
1196 u32 val;
1197 unsigned long time_out;
1198 struct c_can_priv *priv = netdev_priv(dev);
1199 int ret;
1200
1201 if (!(dev->flags & IFF_UP))
1202 return 0;
1203
1204 WARN_ON(priv->type != BOSCH_D_CAN);
1205
1206 c_can_pm_runtime_get_sync(priv);
1207 c_can_reset_ram(priv, true);
1208
1209
1210 val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
1211 val &= ~CONTROL_EX_PDR;
1212 priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
1213 val = priv->read_reg(priv, C_CAN_CTRL_REG);
1214 val &= ~CONTROL_INIT;
1215 priv->write_reg(priv, C_CAN_CTRL_REG, val);
1216
1217
1218 time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS);
1219 while ((priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) &&
1220 time_after(time_out, jiffies))
1221 cpu_relax();
1222
1223 if (time_after(jiffies, time_out))
1224 return -ETIMEDOUT;
1225
1226 ret = c_can_start(dev);
1227 if (!ret)
1228 c_can_irq_control(priv, true);
1229
1230 return ret;
1231}
1232EXPORT_SYMBOL_GPL(c_can_power_up);
1233#endif
1234
1235void free_c_can_dev(struct net_device *dev)
1236{
1237 struct c_can_priv *priv = netdev_priv(dev);
1238
1239 netif_napi_del(&priv->napi);
1240 free_candev(dev);
1241}
1242EXPORT_SYMBOL_GPL(free_c_can_dev);
1243
1244static const struct net_device_ops c_can_netdev_ops = {
1245 .ndo_open = c_can_open,
1246 .ndo_stop = c_can_close,
1247 .ndo_start_xmit = c_can_start_xmit,
1248 .ndo_change_mtu = can_change_mtu,
1249};
1250
1251int register_c_can_dev(struct net_device *dev)
1252{
1253 struct c_can_priv *priv = netdev_priv(dev);
1254 int err;
1255
1256
1257
1258
1259
1260
1261 pinctrl_pm_select_sleep_state(dev->dev.parent);
1262
1263 c_can_pm_runtime_enable(priv);
1264
1265 dev->flags |= IFF_ECHO;
1266 dev->netdev_ops = &c_can_netdev_ops;
1267
1268 err = register_candev(dev);
1269 if (err)
1270 c_can_pm_runtime_disable(priv);
1271 else
1272 devm_can_led_init(dev);
1273
1274 return err;
1275}
1276EXPORT_SYMBOL_GPL(register_c_can_dev);
1277
1278void unregister_c_can_dev(struct net_device *dev)
1279{
1280 struct c_can_priv *priv = netdev_priv(dev);
1281
1282 unregister_candev(dev);
1283
1284 c_can_pm_runtime_disable(priv);
1285}
1286EXPORT_SYMBOL_GPL(unregister_c_can_dev);
1287
1288MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
1289MODULE_LICENSE("GPL v2");
1290MODULE_DESCRIPTION("CAN bus driver for Bosch C_CAN controller");
1291