2 * CAN bus driver for Bosch M_CAN controller
4 * Copyright (C) 2014 Freescale Semiconductor, Inc.
5 * Dong Aisheng <b29396@freescale.com>
7 * Bosch M_CAN user manual can be obtained from:
8 * http://www.bosch-semiconductors.de/media/pdf_1/ipmodules_1/m_can/
9 * mcan_users_manual_v302.pdf
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/netdevice.h>
24 #include <linux/of_device.h>
25 #include <linux/platform_device.h>
27 #include <linux/can/dev.h>
30 #define M_CAN_NAPI_WEIGHT 64
32 /* message ram configuration data length */
33 #define MRAM_CFG_LEN 8
35 /* registers definition */
85 /* m_can lec values */
108 /* Fast Bit Timing & Prescaler Register (FBTP) */
109 #define FBTR_FBRP_MASK 0x1f
110 #define FBTR_FBRP_SHIFT 16
111 #define FBTR_FTSEG1_SHIFT 8
112 #define FBTR_FTSEG1_MASK (0xf << FBTR_FTSEG1_SHIFT)
113 #define FBTR_FTSEG2_SHIFT 4
114 #define FBTR_FTSEG2_MASK (0x7 << FBTR_FTSEG2_SHIFT)
115 #define FBTR_FSJW_SHIFT 0
116 #define FBTR_FSJW_MASK 0x3
118 /* Test Register (TEST) */
119 #define TEST_LBCK BIT(4)
121 /* CC Control Register(CCCR) */
122 #define CCCR_TEST BIT(7)
123 #define CCCR_CMR_MASK 0x3
124 #define CCCR_CMR_SHIFT 10
125 #define CCCR_CMR_CANFD 0x1
126 #define CCCR_CMR_CANFD_BRS 0x2
127 #define CCCR_CMR_CAN 0x3
128 #define CCCR_CME_MASK 0x3
129 #define CCCR_CME_SHIFT 8
130 #define CCCR_CME_CAN 0
131 #define CCCR_CME_CANFD 0x1
132 #define CCCR_CME_CANFD_BRS 0x2
133 #define CCCR_TEST BIT(7)
134 #define CCCR_MON BIT(5)
135 #define CCCR_CCE BIT(1)
136 #define CCCR_INIT BIT(0)
137 #define CCCR_CANFD 0x10
139 /* Bit Timing & Prescaler Register (BTP) */
140 #define BTR_BRP_MASK 0x3ff
141 #define BTR_BRP_SHIFT 16
142 #define BTR_TSEG1_SHIFT 8
143 #define BTR_TSEG1_MASK (0x3f << BTR_TSEG1_SHIFT)
144 #define BTR_TSEG2_SHIFT 4
145 #define BTR_TSEG2_MASK (0xf << BTR_TSEG2_SHIFT)
146 #define BTR_SJW_SHIFT 0
147 #define BTR_SJW_MASK 0xf
149 /* Error Counter Register(ECR) */
150 #define ECR_RP BIT(15)
151 #define ECR_REC_SHIFT 8
152 #define ECR_REC_MASK (0x7f << ECR_REC_SHIFT)
153 #define ECR_TEC_SHIFT 0
154 #define ECR_TEC_MASK 0xff
156 /* Protocol Status Register(PSR) */
157 #define PSR_BO BIT(7)
158 #define PSR_EW BIT(6)
159 #define PSR_EP BIT(5)
160 #define PSR_LEC_MASK 0x7
162 /* Interrupt Register(IR) */
163 #define IR_ALL_INT 0xffffffff
164 #define IR_STE BIT(31)
165 #define IR_FOE BIT(30)
166 #define IR_ACKE BIT(29)
167 #define IR_BE BIT(28)
168 #define IR_CRCE BIT(27)
169 #define IR_WDI BIT(26)
170 #define IR_BO BIT(25)
171 #define IR_EW BIT(24)
172 #define IR_EP BIT(23)
173 #define IR_ELO BIT(22)
174 #define IR_BEU BIT(21)
175 #define IR_BEC BIT(20)
176 #define IR_DRX BIT(19)
177 #define IR_TOO BIT(18)
178 #define IR_MRAF BIT(17)
179 #define IR_TSW BIT(16)
180 #define IR_TEFL BIT(15)
181 #define IR_TEFF BIT(14)
182 #define IR_TEFW BIT(13)
183 #define IR_TEFN BIT(12)
184 #define IR_TFE BIT(11)
185 #define IR_TCF BIT(10)
187 #define IR_HPM BIT(8)
188 #define IR_RF1L BIT(7)
189 #define IR_RF1F BIT(6)
190 #define IR_RF1W BIT(5)
191 #define IR_RF1N BIT(4)
192 #define IR_RF0L BIT(3)
193 #define IR_RF0F BIT(2)
194 #define IR_RF0W BIT(1)
195 #define IR_RF0N BIT(0)
196 #define IR_ERR_STATE (IR_BO | IR_EW | IR_EP)
197 #define IR_ERR_LEC (IR_STE | IR_FOE | IR_ACKE | IR_BE | IR_CRCE)
198 #define IR_ERR_BUS (IR_ERR_LEC | IR_WDI | IR_ELO | IR_BEU | \
199 IR_BEC | IR_TOO | IR_MRAF | IR_TSW | IR_TEFL | \
201 #define IR_ERR_ALL (IR_ERR_STATE | IR_ERR_BUS)
203 /* Interrupt Line Select (ILS) */
204 #define ILS_ALL_INT0 0x0
205 #define ILS_ALL_INT1 0xFFFFFFFF
207 /* Interrupt Line Enable (ILE) */
208 #define ILE_EINT0 BIT(0)
209 #define ILE_EINT1 BIT(1)
211 /* Rx FIFO 0/1 Configuration (RXF0C/RXF1C) */
212 #define RXFC_FWM_OFF 24
213 #define RXFC_FWM_MASK 0x7f
214 #define RXFC_FWM_1 (1 << RXFC_FWM_OFF)
215 #define RXFC_FS_OFF 16
216 #define RXFC_FS_MASK 0x7f
218 /* Rx FIFO 0/1 Status (RXF0S/RXF1S) */
219 #define RXFS_RFL BIT(25)
220 #define RXFS_FF BIT(24)
221 #define RXFS_FPI_OFF 16
222 #define RXFS_FPI_MASK 0x3f0000
223 #define RXFS_FGI_OFF 8
224 #define RXFS_FGI_MASK 0x3f00
225 #define RXFS_FFL_MASK 0x7f
227 /* Rx Buffer / FIFO Element Size Configuration (RXESC) */
228 #define M_CAN_RXESC_8BYTES 0x0
229 #define M_CAN_RXESC_64BYTES 0x777
231 /* Tx Buffer Configuration(TXBC) */
232 #define TXBC_NDTB_OFF 16
233 #define TXBC_NDTB_MASK 0x3f
235 /* Tx Buffer Element Size Configuration(TXESC) */
236 #define TXESC_TBDS_8BYTES 0x0
237 #define TXESC_TBDS_64BYTES 0x7
239 /* Tx Event FIFO Con.guration (TXEFC) */
240 #define TXEFC_EFS_OFF 16
241 #define TXEFC_EFS_MASK 0x3f
243 /* Message RAM Configuration (in bytes) */
244 #define SIDF_ELEMENT_SIZE 4
245 #define XIDF_ELEMENT_SIZE 8
246 #define RXF0_ELEMENT_SIZE 72
247 #define RXF1_ELEMENT_SIZE 72
248 #define RXB_ELEMENT_SIZE 16
249 #define TXE_ELEMENT_SIZE 8
250 #define TXB_ELEMENT_SIZE 72
252 /* Message RAM Elements */
253 #define M_CAN_FIFO_ID 0x0
254 #define M_CAN_FIFO_DLC 0x4
255 #define M_CAN_FIFO_DATA(n) (0x8 + ((n) << 2))
257 /* Rx Buffer Element */
259 #define RX_BUF_ESI BIT(31)
260 #define RX_BUF_XTD BIT(30)
261 #define RX_BUF_RTR BIT(29)
263 #define RX_BUF_ANMF BIT(31)
264 #define RX_BUF_EDL BIT(21)
265 #define RX_BUF_BRS BIT(20)
267 /* Tx Buffer Element */
269 #define TX_BUF_XTD BIT(30)
270 #define TX_BUF_RTR BIT(29)
272 /* address offset and element number for each FIFO/Buffer in the Message RAM */
278 /* m_can private data structure */
280 struct can_priv can
; /* must be the first member */
281 struct napi_struct napi
;
282 struct net_device
*dev
;
283 struct device
*device
;
289 /* message ram configuration */
290 void __iomem
*mram_base
;
291 struct mram_cfg mcfg
[MRAM_CFG_NUM
];
294 static inline u32
m_can_read(const struct m_can_priv
*priv
, enum m_can_reg reg
)
296 return readl(priv
->base
+ reg
);
299 static inline void m_can_write(const struct m_can_priv
*priv
,
300 enum m_can_reg reg
, u32 val
)
302 writel(val
, priv
->base
+ reg
);
305 static inline u32
m_can_fifo_read(const struct m_can_priv
*priv
,
306 u32 fgi
, unsigned int offset
)
308 return readl(priv
->mram_base
+ priv
->mcfg
[MRAM_RXF0
].off
+
309 fgi
* RXF0_ELEMENT_SIZE
+ offset
);
312 static inline void m_can_fifo_write(const struct m_can_priv
*priv
,
313 u32 fpi
, unsigned int offset
, u32 val
)
315 return writel(val
, priv
->mram_base
+ priv
->mcfg
[MRAM_TXB
].off
+
316 fpi
* TXB_ELEMENT_SIZE
+ offset
);
319 static inline void m_can_config_endisable(const struct m_can_priv
*priv
,
322 u32 cccr
= m_can_read(priv
, M_CAN_CCCR
);
327 /* enable m_can configuration */
328 m_can_write(priv
, M_CAN_CCCR
, cccr
| CCCR_INIT
);
330 /* CCCR.CCE can only be set/reset while CCCR.INIT = '1' */
331 m_can_write(priv
, M_CAN_CCCR
, cccr
| CCCR_INIT
| CCCR_CCE
);
333 m_can_write(priv
, M_CAN_CCCR
, cccr
& ~(CCCR_INIT
| CCCR_CCE
));
336 /* there's a delay for module initialization */
338 val
= CCCR_INIT
| CCCR_CCE
;
340 while ((m_can_read(priv
, M_CAN_CCCR
) & (CCCR_INIT
| CCCR_CCE
)) != val
) {
342 netdev_warn(priv
->dev
, "Failed to init module\n");
350 static inline void m_can_enable_all_interrupts(const struct m_can_priv
*priv
)
352 m_can_write(priv
, M_CAN_ILE
, ILE_EINT0
| ILE_EINT1
);
355 static inline void m_can_disable_all_interrupts(const struct m_can_priv
*priv
)
357 m_can_write(priv
, M_CAN_ILE
, 0x0);
360 static void m_can_read_fifo(struct net_device
*dev
, u32 rxfs
)
362 struct net_device_stats
*stats
= &dev
->stats
;
363 struct m_can_priv
*priv
= netdev_priv(dev
);
364 struct canfd_frame
*cf
;
369 /* calculate the fifo get index for where to read data */
370 fgi
= (rxfs
& RXFS_FGI_MASK
) >> RXFS_FGI_OFF
;
371 dlc
= m_can_fifo_read(priv
, fgi
, M_CAN_FIFO_DLC
);
372 if (dlc
& RX_BUF_EDL
)
373 skb
= alloc_canfd_skb(dev
, &cf
);
375 skb
= alloc_can_skb(dev
, (struct can_frame
**)&cf
);
381 if (dlc
& RX_BUF_EDL
)
382 cf
->len
= can_dlc2len((dlc
>> 16) & 0x0F);
384 cf
->len
= get_can_dlc((dlc
>> 16) & 0x0F);
386 id
= m_can_fifo_read(priv
, fgi
, M_CAN_FIFO_ID
);
388 cf
->can_id
= (id
& CAN_EFF_MASK
) | CAN_EFF_FLAG
;
390 cf
->can_id
= (id
>> 18) & CAN_SFF_MASK
;
392 if (id
& RX_BUF_ESI
) {
393 cf
->flags
|= CANFD_ESI
;
394 netdev_dbg(dev
, "ESI Error\n");
397 if (!(dlc
& RX_BUF_EDL
) && (id
& RX_BUF_RTR
)) {
398 cf
->can_id
|= CAN_RTR_FLAG
;
400 if (dlc
& RX_BUF_BRS
)
401 cf
->flags
|= CANFD_BRS
;
403 for (i
= 0; i
< cf
->len
; i
+= 4)
404 *(u32
*)(cf
->data
+ i
) =
405 m_can_fifo_read(priv
, fgi
,
406 M_CAN_FIFO_DATA(i
/ 4));
409 /* acknowledge rx fifo 0 */
410 m_can_write(priv
, M_CAN_RXF0A
, fgi
);
413 stats
->rx_bytes
+= cf
->len
;
415 netif_receive_skb(skb
);
418 static int m_can_do_rx_poll(struct net_device
*dev
, int quota
)
420 struct m_can_priv
*priv
= netdev_priv(dev
);
424 rxfs
= m_can_read(priv
, M_CAN_RXF0S
);
425 if (!(rxfs
& RXFS_FFL_MASK
)) {
426 netdev_dbg(dev
, "no messages in fifo0\n");
430 while ((rxfs
& RXFS_FFL_MASK
) && (quota
> 0)) {
432 netdev_warn(dev
, "Rx FIFO 0 Message Lost\n");
434 m_can_read_fifo(dev
, rxfs
);
438 rxfs
= m_can_read(priv
, M_CAN_RXF0S
);
442 can_led_event(dev
, CAN_LED_EVENT_RX
);
447 static int m_can_handle_lost_msg(struct net_device
*dev
)
449 struct net_device_stats
*stats
= &dev
->stats
;
451 struct can_frame
*frame
;
453 netdev_err(dev
, "msg lost in rxf0\n");
456 stats
->rx_over_errors
++;
458 skb
= alloc_can_err_skb(dev
, &frame
);
462 frame
->can_id
|= CAN_ERR_CRTL
;
463 frame
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
465 netif_receive_skb(skb
);
470 static int m_can_handle_lec_err(struct net_device
*dev
,
471 enum m_can_lec_type lec_type
)
473 struct m_can_priv
*priv
= netdev_priv(dev
);
474 struct net_device_stats
*stats
= &dev
->stats
;
475 struct can_frame
*cf
;
478 priv
->can
.can_stats
.bus_error
++;
481 /* propagate the error condition to the CAN stack */
482 skb
= alloc_can_err_skb(dev
, &cf
);
486 /* check for 'last error code' which tells us the
487 * type of the last error to occur on the CAN bus
489 cf
->can_id
|= CAN_ERR_PROT
| CAN_ERR_BUSERROR
;
490 cf
->data
[2] |= CAN_ERR_PROT_UNSPEC
;
493 case LEC_STUFF_ERROR
:
494 netdev_dbg(dev
, "stuff error\n");
495 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
498 netdev_dbg(dev
, "form error\n");
499 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
502 netdev_dbg(dev
, "ack error\n");
503 cf
->data
[3] |= (CAN_ERR_PROT_LOC_ACK
|
504 CAN_ERR_PROT_LOC_ACK_DEL
);
507 netdev_dbg(dev
, "bit1 error\n");
508 cf
->data
[2] |= CAN_ERR_PROT_BIT1
;
511 netdev_dbg(dev
, "bit0 error\n");
512 cf
->data
[2] |= CAN_ERR_PROT_BIT0
;
515 netdev_dbg(dev
, "CRC error\n");
516 cf
->data
[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ
|
517 CAN_ERR_PROT_LOC_CRC_DEL
);
524 stats
->rx_bytes
+= cf
->can_dlc
;
525 netif_receive_skb(skb
);
530 static int __m_can_get_berr_counter(const struct net_device
*dev
,
531 struct can_berr_counter
*bec
)
533 struct m_can_priv
*priv
= netdev_priv(dev
);
536 ecr
= m_can_read(priv
, M_CAN_ECR
);
537 bec
->rxerr
= (ecr
& ECR_REC_MASK
) >> ECR_REC_SHIFT
;
538 bec
->txerr
= ecr
& ECR_TEC_MASK
;
543 static int m_can_get_berr_counter(const struct net_device
*dev
,
544 struct can_berr_counter
*bec
)
546 struct m_can_priv
*priv
= netdev_priv(dev
);
549 err
= clk_prepare_enable(priv
->hclk
);
553 err
= clk_prepare_enable(priv
->cclk
);
555 clk_disable_unprepare(priv
->hclk
);
559 __m_can_get_berr_counter(dev
, bec
);
561 clk_disable_unprepare(priv
->cclk
);
562 clk_disable_unprepare(priv
->hclk
);
567 static int m_can_handle_state_change(struct net_device
*dev
,
568 enum can_state new_state
)
570 struct m_can_priv
*priv
= netdev_priv(dev
);
571 struct net_device_stats
*stats
= &dev
->stats
;
572 struct can_frame
*cf
;
574 struct can_berr_counter bec
;
578 case CAN_STATE_ERROR_ACTIVE
:
579 /* error warning state */
580 priv
->can
.can_stats
.error_warning
++;
581 priv
->can
.state
= CAN_STATE_ERROR_WARNING
;
583 case CAN_STATE_ERROR_PASSIVE
:
584 /* error passive state */
585 priv
->can
.can_stats
.error_passive
++;
586 priv
->can
.state
= CAN_STATE_ERROR_PASSIVE
;
588 case CAN_STATE_BUS_OFF
:
590 priv
->can
.state
= CAN_STATE_BUS_OFF
;
591 m_can_disable_all_interrupts(priv
);
592 priv
->can
.can_stats
.bus_off
++;
599 /* propagate the error condition to the CAN stack */
600 skb
= alloc_can_err_skb(dev
, &cf
);
604 __m_can_get_berr_counter(dev
, &bec
);
607 case CAN_STATE_ERROR_ACTIVE
:
608 /* error warning state */
609 cf
->can_id
|= CAN_ERR_CRTL
;
610 cf
->data
[1] = (bec
.txerr
> bec
.rxerr
) ?
611 CAN_ERR_CRTL_TX_WARNING
:
612 CAN_ERR_CRTL_RX_WARNING
;
613 cf
->data
[6] = bec
.txerr
;
614 cf
->data
[7] = bec
.rxerr
;
616 case CAN_STATE_ERROR_PASSIVE
:
617 /* error passive state */
618 cf
->can_id
|= CAN_ERR_CRTL
;
619 ecr
= m_can_read(priv
, M_CAN_ECR
);
621 cf
->data
[1] |= CAN_ERR_CRTL_RX_PASSIVE
;
623 cf
->data
[1] |= CAN_ERR_CRTL_TX_PASSIVE
;
624 cf
->data
[6] = bec
.txerr
;
625 cf
->data
[7] = bec
.rxerr
;
627 case CAN_STATE_BUS_OFF
:
629 cf
->can_id
|= CAN_ERR_BUSOFF
;
636 stats
->rx_bytes
+= cf
->can_dlc
;
637 netif_receive_skb(skb
);
642 static int m_can_handle_state_errors(struct net_device
*dev
, u32 psr
)
644 struct m_can_priv
*priv
= netdev_priv(dev
);
647 if ((psr
& PSR_EW
) &&
648 (priv
->can
.state
!= CAN_STATE_ERROR_WARNING
)) {
649 netdev_dbg(dev
, "entered error warning state\n");
650 work_done
+= m_can_handle_state_change(dev
,
651 CAN_STATE_ERROR_WARNING
);
654 if ((psr
& PSR_EP
) &&
655 (priv
->can
.state
!= CAN_STATE_ERROR_PASSIVE
)) {
656 netdev_dbg(dev
, "entered error passive state\n");
657 work_done
+= m_can_handle_state_change(dev
,
658 CAN_STATE_ERROR_PASSIVE
);
661 if ((psr
& PSR_BO
) &&
662 (priv
->can
.state
!= CAN_STATE_BUS_OFF
)) {
663 netdev_dbg(dev
, "entered error bus off state\n");
664 work_done
+= m_can_handle_state_change(dev
,
671 static void m_can_handle_other_err(struct net_device
*dev
, u32 irqstatus
)
673 if (irqstatus
& IR_WDI
)
674 netdev_err(dev
, "Message RAM Watchdog event due to missing READY\n");
675 if (irqstatus
& IR_ELO
)
676 netdev_err(dev
, "Error Logging Overflow\n");
677 if (irqstatus
& IR_BEU
)
678 netdev_err(dev
, "Bit Error Uncorrected\n");
679 if (irqstatus
& IR_BEC
)
680 netdev_err(dev
, "Bit Error Corrected\n");
681 if (irqstatus
& IR_TOO
)
682 netdev_err(dev
, "Timeout reached\n");
683 if (irqstatus
& IR_MRAF
)
684 netdev_err(dev
, "Message RAM access failure occurred\n");
687 static inline bool is_lec_err(u32 psr
)
691 return psr
&& (psr
!= LEC_UNUSED
);
694 static int m_can_handle_bus_errors(struct net_device
*dev
, u32 irqstatus
,
697 struct m_can_priv
*priv
= netdev_priv(dev
);
700 if (irqstatus
& IR_RF0L
)
701 work_done
+= m_can_handle_lost_msg(dev
);
703 /* handle lec errors on the bus */
704 if ((priv
->can
.ctrlmode
& CAN_CTRLMODE_BERR_REPORTING
) &&
706 work_done
+= m_can_handle_lec_err(dev
, psr
& LEC_UNUSED
);
708 /* other unproccessed error interrupts */
709 m_can_handle_other_err(dev
, irqstatus
);
714 static int m_can_poll(struct napi_struct
*napi
, int quota
)
716 struct net_device
*dev
= napi
->dev
;
717 struct m_can_priv
*priv
= netdev_priv(dev
);
721 irqstatus
= priv
->irqstatus
| m_can_read(priv
, M_CAN_IR
);
725 psr
= m_can_read(priv
, M_CAN_PSR
);
726 if (irqstatus
& IR_ERR_STATE
)
727 work_done
+= m_can_handle_state_errors(dev
, psr
);
729 if (irqstatus
& IR_ERR_BUS
)
730 work_done
+= m_can_handle_bus_errors(dev
, irqstatus
, psr
);
732 if (irqstatus
& IR_RF0N
)
733 work_done
+= m_can_do_rx_poll(dev
, (quota
- work_done
));
735 if (work_done
< quota
) {
737 m_can_enable_all_interrupts(priv
);
744 static irqreturn_t
m_can_isr(int irq
, void *dev_id
)
746 struct net_device
*dev
= (struct net_device
*)dev_id
;
747 struct m_can_priv
*priv
= netdev_priv(dev
);
748 struct net_device_stats
*stats
= &dev
->stats
;
751 ir
= m_can_read(priv
, M_CAN_IR
);
757 m_can_write(priv
, M_CAN_IR
, ir
);
759 /* schedule NAPI in case of
762 * - bus error IRQ and bus error reporting
764 if ((ir
& IR_RF0N
) || (ir
& IR_ERR_ALL
)) {
765 priv
->irqstatus
= ir
;
766 m_can_disable_all_interrupts(priv
);
767 napi_schedule(&priv
->napi
);
770 /* transmission complete interrupt */
772 stats
->tx_bytes
+= can_get_echo_skb(dev
, 0);
774 can_led_event(dev
, CAN_LED_EVENT_TX
);
775 netif_wake_queue(dev
);
781 static const struct can_bittiming_const m_can_bittiming_const
= {
782 .name
= KBUILD_MODNAME
,
783 .tseg1_min
= 2, /* Time segment 1 = prop_seg + phase_seg1 */
785 .tseg2_min
= 1, /* Time segment 2 = phase_seg2 */
793 static const struct can_bittiming_const m_can_data_bittiming_const
= {
794 .name
= KBUILD_MODNAME
,
795 .tseg1_min
= 2, /* Time segment 1 = prop_seg + phase_seg1 */
797 .tseg2_min
= 1, /* Time segment 2 = phase_seg2 */
805 static int m_can_set_bittiming(struct net_device
*dev
)
807 struct m_can_priv
*priv
= netdev_priv(dev
);
808 const struct can_bittiming
*bt
= &priv
->can
.bittiming
;
809 const struct can_bittiming
*dbt
= &priv
->can
.data_bittiming
;
810 u16 brp
, sjw
, tseg1
, tseg2
;
815 tseg1
= bt
->prop_seg
+ bt
->phase_seg1
- 1;
816 tseg2
= bt
->phase_seg2
- 1;
817 reg_btp
= (brp
<< BTR_BRP_SHIFT
) | (sjw
<< BTR_SJW_SHIFT
) |
818 (tseg1
<< BTR_TSEG1_SHIFT
) | (tseg2
<< BTR_TSEG2_SHIFT
);
819 m_can_write(priv
, M_CAN_BTP
, reg_btp
);
821 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_FD
) {
824 tseg1
= dbt
->prop_seg
+ dbt
->phase_seg1
- 1;
825 tseg2
= dbt
->phase_seg2
- 1;
826 reg_btp
= (brp
<< FBTR_FBRP_SHIFT
) | (sjw
<< FBTR_FSJW_SHIFT
) |
827 (tseg1
<< FBTR_FTSEG1_SHIFT
) |
828 (tseg2
<< FBTR_FTSEG2_SHIFT
);
829 m_can_write(priv
, M_CAN_FBTP
, reg_btp
);
835 /* Configure M_CAN chip:
836 * - set rx buffer/fifo element size
837 * - configure rx fifo
838 * - accept non-matching frame into fifo 0
839 * - configure tx buffer
843 static void m_can_chip_config(struct net_device
*dev
)
845 struct m_can_priv
*priv
= netdev_priv(dev
);
848 m_can_config_endisable(priv
, true);
850 /* RX Buffer/FIFO Element Size 64 bytes data field */
851 m_can_write(priv
, M_CAN_RXESC
, M_CAN_RXESC_64BYTES
);
853 /* Accept Non-matching Frames Into FIFO 0 */
854 m_can_write(priv
, M_CAN_GFC
, 0x0);
856 /* only support one Tx Buffer currently */
857 m_can_write(priv
, M_CAN_TXBC
, (1 << TXBC_NDTB_OFF
) |
858 priv
->mcfg
[MRAM_TXB
].off
);
860 /* support 64 bytes payload */
861 m_can_write(priv
, M_CAN_TXESC
, TXESC_TBDS_64BYTES
);
863 m_can_write(priv
, M_CAN_TXEFC
, (1 << TXEFC_EFS_OFF
) |
864 priv
->mcfg
[MRAM_TXE
].off
);
866 /* rx fifo configuration, blocking mode, fifo size 1 */
867 m_can_write(priv
, M_CAN_RXF0C
,
868 (priv
->mcfg
[MRAM_RXF0
].num
<< RXFC_FS_OFF
) |
869 RXFC_FWM_1
| priv
->mcfg
[MRAM_RXF0
].off
);
871 m_can_write(priv
, M_CAN_RXF1C
,
872 (priv
->mcfg
[MRAM_RXF1
].num
<< RXFC_FS_OFF
) |
873 RXFC_FWM_1
| priv
->mcfg
[MRAM_RXF1
].off
);
875 cccr
= m_can_read(priv
, M_CAN_CCCR
);
876 cccr
&= ~(CCCR_TEST
| CCCR_MON
| (CCCR_CMR_MASK
<< CCCR_CMR_SHIFT
) |
877 (CCCR_CME_MASK
<< CCCR_CME_SHIFT
));
878 test
= m_can_read(priv
, M_CAN_TEST
);
881 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LISTENONLY
)
884 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LOOPBACK
) {
889 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_FD
)
890 cccr
|= CCCR_CME_CANFD_BRS
<< CCCR_CME_SHIFT
;
892 m_can_write(priv
, M_CAN_CCCR
, cccr
);
893 m_can_write(priv
, M_CAN_TEST
, test
);
895 /* enable interrupts */
896 m_can_write(priv
, M_CAN_IR
, IR_ALL_INT
);
897 if (!(priv
->can
.ctrlmode
& CAN_CTRLMODE_BERR_REPORTING
))
898 m_can_write(priv
, M_CAN_IE
, IR_ALL_INT
& ~IR_ERR_LEC
);
900 m_can_write(priv
, M_CAN_IE
, IR_ALL_INT
);
902 /* route all interrupts to INT0 */
903 m_can_write(priv
, M_CAN_ILS
, ILS_ALL_INT0
);
905 /* set bittiming params */
906 m_can_set_bittiming(dev
);
908 m_can_config_endisable(priv
, false);
911 static void m_can_start(struct net_device
*dev
)
913 struct m_can_priv
*priv
= netdev_priv(dev
);
915 /* basic m_can configuration */
916 m_can_chip_config(dev
);
918 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
920 m_can_enable_all_interrupts(priv
);
923 static int m_can_set_mode(struct net_device
*dev
, enum can_mode mode
)
928 netif_wake_queue(dev
);
937 static void free_m_can_dev(struct net_device
*dev
)
942 static struct net_device
*alloc_m_can_dev(void)
944 struct net_device
*dev
;
945 struct m_can_priv
*priv
;
947 dev
= alloc_candev(sizeof(*priv
), 1);
951 priv
= netdev_priv(dev
);
952 netif_napi_add(dev
, &priv
->napi
, m_can_poll
, M_CAN_NAPI_WEIGHT
);
955 priv
->can
.bittiming_const
= &m_can_bittiming_const
;
956 priv
->can
.data_bittiming_const
= &m_can_data_bittiming_const
;
957 priv
->can
.do_set_mode
= m_can_set_mode
;
958 priv
->can
.do_get_berr_counter
= m_can_get_berr_counter
;
959 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_LOOPBACK
|
960 CAN_CTRLMODE_LISTENONLY
|
961 CAN_CTRLMODE_BERR_REPORTING
|
967 static int m_can_open(struct net_device
*dev
)
969 struct m_can_priv
*priv
= netdev_priv(dev
);
972 err
= clk_prepare_enable(priv
->hclk
);
976 err
= clk_prepare_enable(priv
->cclk
);
978 goto exit_disable_hclk
;
980 /* open the can device */
981 err
= open_candev(dev
);
983 netdev_err(dev
, "failed to open can device\n");
984 goto exit_disable_cclk
;
987 /* register interrupt handler */
988 err
= request_irq(dev
->irq
, m_can_isr
, IRQF_SHARED
, dev
->name
,
991 netdev_err(dev
, "failed to request interrupt\n");
995 /* start the m_can controller */
998 can_led_event(dev
, CAN_LED_EVENT_OPEN
);
999 napi_enable(&priv
->napi
);
1000 netif_start_queue(dev
);
1007 clk_disable_unprepare(priv
->cclk
);
1009 clk_disable_unprepare(priv
->hclk
);
1013 static void m_can_stop(struct net_device
*dev
)
1015 struct m_can_priv
*priv
= netdev_priv(dev
);
1017 /* disable all interrupts */
1018 m_can_disable_all_interrupts(priv
);
1020 clk_disable_unprepare(priv
->hclk
);
1021 clk_disable_unprepare(priv
->cclk
);
1023 /* set the state as STOPPED */
1024 priv
->can
.state
= CAN_STATE_STOPPED
;
1027 static int m_can_close(struct net_device
*dev
)
1029 struct m_can_priv
*priv
= netdev_priv(dev
);
1031 netif_stop_queue(dev
);
1032 napi_disable(&priv
->napi
);
1034 free_irq(dev
->irq
, dev
);
1036 can_led_event(dev
, CAN_LED_EVENT_STOP
);
1041 static netdev_tx_t
m_can_start_xmit(struct sk_buff
*skb
,
1042 struct net_device
*dev
)
1044 struct m_can_priv
*priv
= netdev_priv(dev
);
1045 struct canfd_frame
*cf
= (struct canfd_frame
*)skb
->data
;
1049 if (can_dropped_invalid_skb(dev
, skb
))
1050 return NETDEV_TX_OK
;
1052 netif_stop_queue(dev
);
1054 if (cf
->can_id
& CAN_EFF_FLAG
) {
1055 id
= cf
->can_id
& CAN_EFF_MASK
;
1058 id
= ((cf
->can_id
& CAN_SFF_MASK
) << 18);
1061 if (cf
->can_id
& CAN_RTR_FLAG
)
1064 /* message ram configuration */
1065 m_can_fifo_write(priv
, 0, M_CAN_FIFO_ID
, id
);
1066 m_can_fifo_write(priv
, 0, M_CAN_FIFO_DLC
, can_len2dlc(cf
->len
) << 16);
1068 for (i
= 0; i
< cf
->len
; i
+= 4)
1069 m_can_fifo_write(priv
, 0, M_CAN_FIFO_DATA(i
/ 4),
1070 *(u32
*)(cf
->data
+ i
));
1072 can_put_echo_skb(skb
, dev
, 0);
1074 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_FD
) {
1075 cccr
= m_can_read(priv
, M_CAN_CCCR
);
1076 cccr
&= ~(CCCR_CMR_MASK
<< CCCR_CMR_SHIFT
);
1077 if (can_is_canfd_skb(skb
)) {
1078 if (cf
->flags
& CANFD_BRS
)
1079 cccr
|= CCCR_CMR_CANFD_BRS
<< CCCR_CMR_SHIFT
;
1081 cccr
|= CCCR_CMR_CANFD
<< CCCR_CMR_SHIFT
;
1083 cccr
|= CCCR_CMR_CAN
<< CCCR_CMR_SHIFT
;
1085 m_can_write(priv
, M_CAN_CCCR
, cccr
);
1088 /* enable first TX buffer to start transfer */
1089 m_can_write(priv
, M_CAN_TXBTIE
, 0x1);
1090 m_can_write(priv
, M_CAN_TXBAR
, 0x1);
1092 return NETDEV_TX_OK
;
1095 static const struct net_device_ops m_can_netdev_ops
= {
1096 .ndo_open
= m_can_open
,
1097 .ndo_stop
= m_can_close
,
1098 .ndo_start_xmit
= m_can_start_xmit
,
1099 .ndo_change_mtu
= can_change_mtu
,
1102 static int register_m_can_dev(struct net_device
*dev
)
1104 dev
->flags
|= IFF_ECHO
; /* we support local echo */
1105 dev
->netdev_ops
= &m_can_netdev_ops
;
1107 return register_candev(dev
);
1110 static int m_can_of_parse_mram(struct platform_device
*pdev
,
1111 struct m_can_priv
*priv
)
1113 struct device_node
*np
= pdev
->dev
.of_node
;
1114 struct resource
*res
;
1116 u32 out_val
[MRAM_CFG_LEN
];
1117 int i
, start
, end
, ret
;
1119 /* message ram could be shared */
1120 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "message_ram");
1124 addr
= devm_ioremap(&pdev
->dev
, res
->start
, resource_size(res
));
1128 /* get message ram configuration */
1129 ret
= of_property_read_u32_array(np
, "bosch,mram-cfg",
1130 out_val
, sizeof(out_val
) / 4);
1132 dev_err(&pdev
->dev
, "can not get message ram configuration\n");
1136 priv
->mram_base
= addr
;
1137 priv
->mcfg
[MRAM_SIDF
].off
= out_val
[0];
1138 priv
->mcfg
[MRAM_SIDF
].num
= out_val
[1];
1139 priv
->mcfg
[MRAM_XIDF
].off
= priv
->mcfg
[MRAM_SIDF
].off
+
1140 priv
->mcfg
[MRAM_SIDF
].num
* SIDF_ELEMENT_SIZE
;
1141 priv
->mcfg
[MRAM_XIDF
].num
= out_val
[2];
1142 priv
->mcfg
[MRAM_RXF0
].off
= priv
->mcfg
[MRAM_XIDF
].off
+
1143 priv
->mcfg
[MRAM_XIDF
].num
* XIDF_ELEMENT_SIZE
;
1144 priv
->mcfg
[MRAM_RXF0
].num
= out_val
[3] & RXFC_FS_MASK
;
1145 priv
->mcfg
[MRAM_RXF1
].off
= priv
->mcfg
[MRAM_RXF0
].off
+
1146 priv
->mcfg
[MRAM_RXF0
].num
* RXF0_ELEMENT_SIZE
;
1147 priv
->mcfg
[MRAM_RXF1
].num
= out_val
[4] & RXFC_FS_MASK
;
1148 priv
->mcfg
[MRAM_RXB
].off
= priv
->mcfg
[MRAM_RXF1
].off
+
1149 priv
->mcfg
[MRAM_RXF1
].num
* RXF1_ELEMENT_SIZE
;
1150 priv
->mcfg
[MRAM_RXB
].num
= out_val
[5];
1151 priv
->mcfg
[MRAM_TXE
].off
= priv
->mcfg
[MRAM_RXB
].off
+
1152 priv
->mcfg
[MRAM_RXB
].num
* RXB_ELEMENT_SIZE
;
1153 priv
->mcfg
[MRAM_TXE
].num
= out_val
[6];
1154 priv
->mcfg
[MRAM_TXB
].off
= priv
->mcfg
[MRAM_TXE
].off
+
1155 priv
->mcfg
[MRAM_TXE
].num
* TXE_ELEMENT_SIZE
;
1156 priv
->mcfg
[MRAM_TXB
].num
= out_val
[7] & TXBC_NDTB_MASK
;
1158 dev_dbg(&pdev
->dev
, "mram_base %p sidf 0x%x %d xidf 0x%x %d rxf0 0x%x %d rxf1 0x%x %d rxb 0x%x %d txe 0x%x %d txb 0x%x %d\n",
1160 priv
->mcfg
[MRAM_SIDF
].off
, priv
->mcfg
[MRAM_SIDF
].num
,
1161 priv
->mcfg
[MRAM_XIDF
].off
, priv
->mcfg
[MRAM_XIDF
].num
,
1162 priv
->mcfg
[MRAM_RXF0
].off
, priv
->mcfg
[MRAM_RXF0
].num
,
1163 priv
->mcfg
[MRAM_RXF1
].off
, priv
->mcfg
[MRAM_RXF1
].num
,
1164 priv
->mcfg
[MRAM_RXB
].off
, priv
->mcfg
[MRAM_RXB
].num
,
1165 priv
->mcfg
[MRAM_TXE
].off
, priv
->mcfg
[MRAM_TXE
].num
,
1166 priv
->mcfg
[MRAM_TXB
].off
, priv
->mcfg
[MRAM_TXB
].num
);
1168 /* initialize the entire Message RAM in use to avoid possible
1169 * ECC/parity checksum errors when reading an uninitialized buffer
1171 start
= priv
->mcfg
[MRAM_SIDF
].off
;
1172 end
= priv
->mcfg
[MRAM_TXB
].off
+
1173 priv
->mcfg
[MRAM_TXB
].num
* TXB_ELEMENT_SIZE
;
1174 for (i
= start
; i
< end
; i
+= 4)
1175 writel(0x0, priv
->mram_base
+ i
);
1180 static int m_can_plat_probe(struct platform_device
*pdev
)
1182 struct net_device
*dev
;
1183 struct m_can_priv
*priv
;
1184 struct resource
*res
;
1186 struct clk
*hclk
, *cclk
;
1189 hclk
= devm_clk_get(&pdev
->dev
, "hclk");
1190 cclk
= devm_clk_get(&pdev
->dev
, "cclk");
1191 if (IS_ERR(hclk
) || IS_ERR(cclk
)) {
1192 dev_err(&pdev
->dev
, "no clock find\n");
1196 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "m_can");
1197 addr
= devm_ioremap_resource(&pdev
->dev
, res
);
1198 irq
= platform_get_irq_byname(pdev
, "int0");
1199 if (IS_ERR(addr
) || irq
< 0)
1202 /* allocate the m_can device */
1203 dev
= alloc_m_can_dev();
1207 priv
= netdev_priv(dev
);
1210 priv
->device
= &pdev
->dev
;
1213 priv
->can
.clock
.freq
= clk_get_rate(cclk
);
1215 ret
= m_can_of_parse_mram(pdev
, priv
);
1217 goto failed_free_dev
;
1219 platform_set_drvdata(pdev
, dev
);
1220 SET_NETDEV_DEV(dev
, &pdev
->dev
);
1222 ret
= register_m_can_dev(dev
);
1224 dev_err(&pdev
->dev
, "registering %s failed (err=%d)\n",
1225 KBUILD_MODNAME
, ret
);
1226 goto failed_free_dev
;
1229 devm_can_led_init(dev
);
1231 dev_info(&pdev
->dev
, "%s device registered (regs=%p, irq=%d)\n",
1232 KBUILD_MODNAME
, priv
->base
, dev
->irq
);
1237 free_m_can_dev(dev
);
1241 static __maybe_unused
int m_can_suspend(struct device
*dev
)
1243 struct net_device
*ndev
= dev_get_drvdata(dev
);
1244 struct m_can_priv
*priv
= netdev_priv(ndev
);
1246 if (netif_running(ndev
)) {
1247 netif_stop_queue(ndev
);
1248 netif_device_detach(ndev
);
1251 /* TODO: enter low power */
1253 priv
->can
.state
= CAN_STATE_SLEEPING
;
1258 static __maybe_unused
int m_can_resume(struct device
*dev
)
1260 struct net_device
*ndev
= dev_get_drvdata(dev
);
1261 struct m_can_priv
*priv
= netdev_priv(ndev
);
1263 /* TODO: exit low power */
1265 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
1267 if (netif_running(ndev
)) {
1268 netif_device_attach(ndev
);
1269 netif_start_queue(ndev
);
1275 static void unregister_m_can_dev(struct net_device
*dev
)
1277 unregister_candev(dev
);
1280 static int m_can_plat_remove(struct platform_device
*pdev
)
1282 struct net_device
*dev
= platform_get_drvdata(pdev
);
1284 unregister_m_can_dev(dev
);
1285 platform_set_drvdata(pdev
, NULL
);
1287 free_m_can_dev(dev
);
1292 static const struct dev_pm_ops m_can_pmops
= {
1293 SET_SYSTEM_SLEEP_PM_OPS(m_can_suspend
, m_can_resume
)
1296 static const struct of_device_id m_can_of_table
[] = {
1297 { .compatible
= "bosch,m_can", .data
= NULL
},
1300 MODULE_DEVICE_TABLE(of
, m_can_of_table
);
1302 static struct platform_driver m_can_plat_driver
= {
1304 .name
= KBUILD_MODNAME
,
1305 .of_match_table
= m_can_of_table
,
1308 .probe
= m_can_plat_probe
,
1309 .remove
= m_can_plat_remove
,
1312 module_platform_driver(m_can_plat_driver
);
1314 MODULE_AUTHOR("Dong Aisheng <b29396@freescale.com>");
1315 MODULE_LICENSE("GPL v2");
1316 MODULE_DESCRIPTION("CAN bus driver for Bosch M_CAN controller");