1 // SPDX-License-Identifier: GPL-2.0
2 // CAN bus driver for Bosch M_CAN controller
3 // Copyright (C) 2014 Freescale Semiconductor, Inc.
5 // Copyright (C) 2018-19 Texas Instruments Incorporated - http://www.ti.com/
7 /* Bosch M_CAN user manual can be obtained from:
8 * https://github.com/linux-can/can-doc/tree/master/m_can
11 #include <linux/bitfield.h>
12 #include <linux/can/dev.h>
13 #include <linux/ethtool.h>
14 #include <linux/hrtimer.h>
15 #include <linux/interrupt.h>
17 #include <linux/iopoll.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/netdevice.h>
22 #include <linux/phy/phy.h>
23 #include <linux/pinctrl/consumer.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
29 /* registers definition */
45 /* TDCR Register only available for version >=3.1.x */
81 /* message ram configuration data length */
82 #define MRAM_CFG_LEN 8
84 /* Core Release Register (CREL) */
85 #define CREL_REL_MASK GENMASK(31, 28)
86 #define CREL_STEP_MASK GENMASK(27, 24)
87 #define CREL_SUBSTEP_MASK GENMASK(23, 20)
89 /* Data Bit Timing & Prescaler Register (DBTP) */
90 #define DBTP_TDC BIT(23)
91 #define DBTP_DBRP_MASK GENMASK(20, 16)
92 #define DBTP_DTSEG1_MASK GENMASK(12, 8)
93 #define DBTP_DTSEG2_MASK GENMASK(7, 4)
94 #define DBTP_DSJW_MASK GENMASK(3, 0)
96 /* Transmitter Delay Compensation Register (TDCR) */
97 #define TDCR_TDCO_MASK GENMASK(14, 8)
98 #define TDCR_TDCF_MASK GENMASK(6, 0)
100 /* Test Register (TEST) */
101 #define TEST_LBCK BIT(4)
103 /* CC Control Register (CCCR) */
104 #define CCCR_TXP BIT(14)
105 #define CCCR_TEST BIT(7)
106 #define CCCR_DAR BIT(6)
107 #define CCCR_MON BIT(5)
108 #define CCCR_CSR BIT(4)
109 #define CCCR_CSA BIT(3)
110 #define CCCR_ASM BIT(2)
111 #define CCCR_CCE BIT(1)
112 #define CCCR_INIT BIT(0)
113 /* for version 3.0.x */
114 #define CCCR_CMR_MASK GENMASK(11, 10)
115 #define CCCR_CMR_CANFD 0x1
116 #define CCCR_CMR_CANFD_BRS 0x2
117 #define CCCR_CMR_CAN 0x3
118 #define CCCR_CME_MASK GENMASK(9, 8)
119 #define CCCR_CME_CAN 0
120 #define CCCR_CME_CANFD 0x1
121 #define CCCR_CME_CANFD_BRS 0x2
122 /* for version >=3.1.x */
123 #define CCCR_EFBI BIT(13)
124 #define CCCR_PXHD BIT(12)
125 #define CCCR_BRSE BIT(9)
126 #define CCCR_FDOE BIT(8)
127 /* for version >=3.2.x */
128 #define CCCR_NISO BIT(15)
129 /* for version >=3.3.x */
130 #define CCCR_WMM BIT(11)
131 #define CCCR_UTSU BIT(10)
133 /* Nominal Bit Timing & Prescaler Register (NBTP) */
134 #define NBTP_NSJW_MASK GENMASK(31, 25)
135 #define NBTP_NBRP_MASK GENMASK(24, 16)
136 #define NBTP_NTSEG1_MASK GENMASK(15, 8)
137 #define NBTP_NTSEG2_MASK GENMASK(6, 0)
139 /* Timestamp Counter Configuration Register (TSCC) */
140 #define TSCC_TCP_MASK GENMASK(19, 16)
141 #define TSCC_TSS_MASK GENMASK(1, 0)
142 #define TSCC_TSS_DISABLE 0x0
143 #define TSCC_TSS_INTERNAL 0x1
144 #define TSCC_TSS_EXTERNAL 0x2
146 /* Timestamp Counter Value Register (TSCV) */
147 #define TSCV_TSC_MASK GENMASK(15, 0)
149 /* Error Counter Register (ECR) */
150 #define ECR_RP BIT(15)
151 #define ECR_REC_MASK GENMASK(14, 8)
152 #define ECR_TEC_MASK GENMASK(7, 0)
154 /* Protocol Status Register (PSR) */
155 #define PSR_BO BIT(7)
156 #define PSR_EW BIT(6)
157 #define PSR_EP BIT(5)
158 #define PSR_LEC_MASK GENMASK(2, 0)
159 #define PSR_DLEC_MASK GENMASK(10, 8)
161 /* Interrupt Register (IR) */
162 #define IR_ALL_INT 0xffffffff
164 /* Renamed bits for versions > 3.1.x */
165 #define IR_ARA BIT(29)
166 #define IR_PED BIT(28)
167 #define IR_PEA BIT(27)
169 /* Bits for version 3.0.x */
170 #define IR_STE BIT(31)
171 #define IR_FOE BIT(30)
172 #define IR_ACKE BIT(29)
173 #define IR_BE BIT(28)
174 #define IR_CRCE BIT(27)
175 #define IR_WDI BIT(26)
176 #define IR_BO BIT(25)
177 #define IR_EW BIT(24)
178 #define IR_EP BIT(23)
179 #define IR_ELO BIT(22)
180 #define IR_BEU BIT(21)
181 #define IR_BEC BIT(20)
182 #define IR_DRX BIT(19)
183 #define IR_TOO BIT(18)
184 #define IR_MRAF BIT(17)
185 #define IR_TSW BIT(16)
186 #define IR_TEFL BIT(15)
187 #define IR_TEFF BIT(14)
188 #define IR_TEFW BIT(13)
189 #define IR_TEFN BIT(12)
190 #define IR_TFE BIT(11)
191 #define IR_TCF BIT(10)
193 #define IR_HPM BIT(8)
194 #define IR_RF1L BIT(7)
195 #define IR_RF1F BIT(6)
196 #define IR_RF1W BIT(5)
197 #define IR_RF1N BIT(4)
198 #define IR_RF0L BIT(3)
199 #define IR_RF0F BIT(2)
200 #define IR_RF0W BIT(1)
201 #define IR_RF0N BIT(0)
202 #define IR_ERR_STATE (IR_BO | IR_EW | IR_EP)
204 /* Interrupts for version 3.0.x */
205 #define IR_ERR_LEC_30X (IR_STE | IR_FOE | IR_ACKE | IR_BE | IR_CRCE)
206 #define IR_ERR_BUS_30X (IR_ERR_LEC_30X | IR_WDI | IR_BEU | IR_BEC | \
207 IR_TOO | IR_MRAF | IR_TSW | IR_TEFL | IR_RF1L | \
209 #define IR_ERR_ALL_30X (IR_ERR_STATE | IR_ERR_BUS_30X)
211 /* Interrupts for version >= 3.1.x */
212 #define IR_ERR_LEC_31X (IR_PED | IR_PEA)
213 #define IR_ERR_BUS_31X (IR_ERR_LEC_31X | IR_WDI | IR_BEU | IR_BEC | \
214 IR_TOO | IR_MRAF | IR_TSW | IR_TEFL | IR_RF1L | \
216 #define IR_ERR_ALL_31X (IR_ERR_STATE | IR_ERR_BUS_31X)
218 /* Interrupt Line Select (ILS) */
219 #define ILS_ALL_INT0 0x0
220 #define ILS_ALL_INT1 0xFFFFFFFF
222 /* Interrupt Line Enable (ILE) */
223 #define ILE_EINT1 BIT(1)
224 #define ILE_EINT0 BIT(0)
226 /* Rx FIFO 0/1 Configuration (RXF0C/RXF1C) */
227 #define RXFC_FWM_MASK GENMASK(30, 24)
228 #define RXFC_FS_MASK GENMASK(22, 16)
230 /* Rx FIFO 0/1 Status (RXF0S/RXF1S) */
231 #define RXFS_RFL BIT(25)
232 #define RXFS_FF BIT(24)
233 #define RXFS_FPI_MASK GENMASK(21, 16)
234 #define RXFS_FGI_MASK GENMASK(13, 8)
235 #define RXFS_FFL_MASK GENMASK(6, 0)
237 /* Rx Buffer / FIFO Element Size Configuration (RXESC) */
238 #define RXESC_RBDS_MASK GENMASK(10, 8)
239 #define RXESC_F1DS_MASK GENMASK(6, 4)
240 #define RXESC_F0DS_MASK GENMASK(2, 0)
241 #define RXESC_64B 0x7
243 /* Tx Buffer Configuration (TXBC) */
244 #define TXBC_TFQS_MASK GENMASK(29, 24)
245 #define TXBC_NDTB_MASK GENMASK(21, 16)
247 /* Tx FIFO/Queue Status (TXFQS) */
248 #define TXFQS_TFQF BIT(21)
249 #define TXFQS_TFQPI_MASK GENMASK(20, 16)
250 #define TXFQS_TFGI_MASK GENMASK(12, 8)
251 #define TXFQS_TFFL_MASK GENMASK(5, 0)
253 /* Tx Buffer Element Size Configuration (TXESC) */
254 #define TXESC_TBDS_MASK GENMASK(2, 0)
255 #define TXESC_TBDS_64B 0x7
257 /* Tx Event FIFO Configuration (TXEFC) */
258 #define TXEFC_EFWM_MASK GENMASK(29, 24)
259 #define TXEFC_EFS_MASK GENMASK(21, 16)
261 /* Tx Event FIFO Status (TXEFS) */
262 #define TXEFS_TEFL BIT(25)
263 #define TXEFS_EFF BIT(24)
264 #define TXEFS_EFGI_MASK GENMASK(12, 8)
265 #define TXEFS_EFFL_MASK GENMASK(5, 0)
267 /* Tx Event FIFO Acknowledge (TXEFA) */
268 #define TXEFA_EFAI_MASK GENMASK(4, 0)
270 /* Message RAM Configuration (in bytes) */
271 #define SIDF_ELEMENT_SIZE 4
272 #define XIDF_ELEMENT_SIZE 8
273 #define RXF0_ELEMENT_SIZE 72
274 #define RXF1_ELEMENT_SIZE 72
275 #define RXB_ELEMENT_SIZE 72
276 #define TXE_ELEMENT_SIZE 8
277 #define TXB_ELEMENT_SIZE 72
279 /* Message RAM Elements */
280 #define M_CAN_FIFO_ID 0x0
281 #define M_CAN_FIFO_DLC 0x4
282 #define M_CAN_FIFO_DATA 0x8
284 /* Rx Buffer Element */
286 #define RX_BUF_ESI BIT(31)
287 #define RX_BUF_XTD BIT(30)
288 #define RX_BUF_RTR BIT(29)
290 #define RX_BUF_ANMF BIT(31)
291 #define RX_BUF_FDF BIT(21)
292 #define RX_BUF_BRS BIT(20)
293 #define RX_BUF_RXTS_MASK GENMASK(15, 0)
295 /* Tx Buffer Element */
297 #define TX_BUF_ESI BIT(31)
298 #define TX_BUF_XTD BIT(30)
299 #define TX_BUF_RTR BIT(29)
301 #define TX_BUF_EFC BIT(23)
302 #define TX_BUF_FDF BIT(21)
303 #define TX_BUF_BRS BIT(20)
304 #define TX_BUF_MM_MASK GENMASK(31, 24)
305 #define TX_BUF_DLC_MASK GENMASK(19, 16)
307 /* Tx event FIFO Element */
309 #define TX_EVENT_MM_MASK GENMASK(31, 24)
310 #define TX_EVENT_TXTS_MASK GENMASK(15, 0)
312 /* Hrtimer polling interval */
313 #define HRTIMER_POLL_INTERVAL_MS 1
315 /* The ID and DLC registers are adjacent in M_CAN FIFO memory,
316 * and we can save a (potentially slow) bus round trip by combining
317 * reads and writes to them.
324 struct m_can_fifo_element {
327 u8 data[CANFD_MAX_DLEN];
330 static inline u32 m_can_read(struct m_can_classdev *cdev, enum m_can_reg reg)
332 return cdev->ops->read_reg(cdev, reg);
335 static inline void m_can_write(struct m_can_classdev *cdev, enum m_can_reg reg,
338 cdev->ops->write_reg(cdev, reg, val);
342 m_can_fifo_read(struct m_can_classdev *cdev,
343 u32 fgi, unsigned int offset, void *val, size_t val_count)
345 u32 addr_offset = cdev->mcfg[MRAM_RXF0].off + fgi * RXF0_ELEMENT_SIZE +
351 return cdev->ops->read_fifo(cdev, addr_offset, val, val_count);
355 m_can_fifo_write(struct m_can_classdev *cdev,
356 u32 fpi, unsigned int offset, const void *val, size_t val_count)
358 u32 addr_offset = cdev->mcfg[MRAM_TXB].off + fpi * TXB_ELEMENT_SIZE +
364 return cdev->ops->write_fifo(cdev, addr_offset, val, val_count);
367 static inline int m_can_fifo_write_no_off(struct m_can_classdev *cdev,
370 return cdev->ops->write_fifo(cdev, fpi, &val, 1);
374 m_can_txe_fifo_read(struct m_can_classdev *cdev, u32 fgi, u32 offset, u32 *val)
376 u32 addr_offset = cdev->mcfg[MRAM_TXE].off + fgi * TXE_ELEMENT_SIZE +
379 return cdev->ops->read_fifo(cdev, addr_offset, val, 1);
382 static int m_can_cccr_update_bits(struct m_can_classdev *cdev, u32 mask, u32 val)
384 u32 val_before = m_can_read(cdev, M_CAN_CCCR);
385 u32 val_after = (val_before & ~mask) | val;
388 if (!(mask & CCCR_INIT) && !(val_before & CCCR_INIT)) {
390 "refusing to configure device when in normal mode\n");
394 /* The chip should be in standby mode when changing the CCCR register,
395 * and some chips set the CSR and CSA bits when in standby. Furthermore,
396 * the CSR and CSA bits should be written as zeros, even when they read
399 val_after &= ~(CCCR_CSR | CCCR_CSA);
404 /* Write the desired value in each try, as setting some bits in
405 * the CCCR register require other bits to be set first. E.g.
406 * setting the NISO bit requires setting the CCE bit first.
408 m_can_write(cdev, M_CAN_CCCR, val_after);
410 val_read = m_can_read(cdev, M_CAN_CCCR) & ~(CCCR_CSR | CCCR_CSA);
412 if (val_read == val_after)
421 static int m_can_config_enable(struct m_can_classdev *cdev)
425 /* CCCR_INIT must be set in order to set CCCR_CCE, but access to
426 * configuration registers should only be enabled when in standby mode,
427 * where CCCR_INIT is always set.
429 err = m_can_cccr_update_bits(cdev, CCCR_CCE, CCCR_CCE);
431 netdev_err(cdev->net, "failed to enable configuration mode\n");
436 static int m_can_config_disable(struct m_can_classdev *cdev)
440 /* Only clear CCCR_CCE, since CCCR_INIT cannot be cleared while in
443 err = m_can_cccr_update_bits(cdev, CCCR_CCE, 0);
445 netdev_err(cdev->net, "failed to disable configuration registers\n");
450 static void m_can_interrupt_enable(struct m_can_classdev *cdev, u32 interrupts)
452 if (cdev->active_interrupts == interrupts)
454 cdev->ops->write_reg(cdev, M_CAN_IE, interrupts);
455 cdev->active_interrupts = interrupts;
458 static void m_can_coalescing_disable(struct m_can_classdev *cdev)
460 u32 new_interrupts = cdev->active_interrupts | IR_RF0N | IR_TEFN;
465 hrtimer_cancel(&cdev->hrtimer);
466 m_can_interrupt_enable(cdev, new_interrupts);
469 static inline void m_can_enable_all_interrupts(struct m_can_classdev *cdev)
471 if (!cdev->net->irq) {
472 dev_dbg(cdev->dev, "Start hrtimer\n");
473 hrtimer_start(&cdev->hrtimer,
474 ms_to_ktime(HRTIMER_POLL_INTERVAL_MS),
475 HRTIMER_MODE_REL_PINNED);
478 /* Only interrupt line 0 is used in this driver */
479 m_can_write(cdev, M_CAN_ILE, ILE_EINT0);
482 static inline void m_can_disable_all_interrupts(struct m_can_classdev *cdev)
484 m_can_coalescing_disable(cdev);
485 m_can_write(cdev, M_CAN_ILE, 0x0);
487 if (!cdev->net->irq) {
488 dev_dbg(cdev->dev, "Stop hrtimer\n");
489 hrtimer_try_to_cancel(&cdev->hrtimer);
493 /* Retrieve internal timestamp counter from TSCV.TSC, and shift it to 32-bit
496 static u32 m_can_get_timestamp(struct m_can_classdev *cdev)
501 tscv = m_can_read(cdev, M_CAN_TSCV);
502 tsc = FIELD_GET(TSCV_TSC_MASK, tscv);
507 static void m_can_clean(struct net_device *net)
509 struct m_can_classdev *cdev = netdev_priv(net);
510 unsigned long irqflags;
513 for (int i = 0; i != cdev->tx_fifo_size; ++i) {
514 if (!cdev->tx_ops[i].skb)
517 net->stats.tx_errors++;
518 cdev->tx_ops[i].skb = NULL;
522 for (int i = 0; i != cdev->can.echo_skb_max; ++i)
523 can_free_echo_skb(cdev->net, i, NULL);
525 netdev_reset_queue(cdev->net);
527 spin_lock_irqsave(&cdev->tx_handling_spinlock, irqflags);
528 cdev->tx_fifo_in_flight = 0;
529 spin_unlock_irqrestore(&cdev->tx_handling_spinlock, irqflags);
532 /* For peripherals, pass skb to rx-offload, which will push skb from
533 * napi. For non-peripherals, RX is done in napi already, so push
534 * directly. timestamp is used to ensure good skb ordering in
535 * rx-offload and is ignored for non-peripherals.
537 static void m_can_receive_skb(struct m_can_classdev *cdev,
541 if (cdev->is_peripheral) {
542 struct net_device_stats *stats = &cdev->net->stats;
545 err = can_rx_offload_queue_timestamp(&cdev->offload, skb,
548 stats->rx_fifo_errors++;
550 netif_receive_skb(skb);
554 static int m_can_read_fifo(struct net_device *dev, u32 fgi)
556 struct net_device_stats *stats = &dev->stats;
557 struct m_can_classdev *cdev = netdev_priv(dev);
558 struct canfd_frame *cf;
560 struct id_and_dlc fifo_header;
564 err = m_can_fifo_read(cdev, fgi, M_CAN_FIFO_ID, &fifo_header, 2);
568 if (fifo_header.dlc & RX_BUF_FDF)
569 skb = alloc_canfd_skb(dev, &cf);
571 skb = alloc_can_skb(dev, (struct can_frame **)&cf);
577 if (fifo_header.dlc & RX_BUF_FDF)
578 cf->len = can_fd_dlc2len((fifo_header.dlc >> 16) & 0x0F);
580 cf->len = can_cc_dlc2len((fifo_header.dlc >> 16) & 0x0F);
582 if (fifo_header.id & RX_BUF_XTD)
583 cf->can_id = (fifo_header.id & CAN_EFF_MASK) | CAN_EFF_FLAG;
585 cf->can_id = (fifo_header.id >> 18) & CAN_SFF_MASK;
587 if (fifo_header.id & RX_BUF_ESI) {
588 cf->flags |= CANFD_ESI;
589 netdev_dbg(dev, "ESI Error\n");
592 if (!(fifo_header.dlc & RX_BUF_FDF) && (fifo_header.id & RX_BUF_RTR)) {
593 cf->can_id |= CAN_RTR_FLAG;
595 if (fifo_header.dlc & RX_BUF_BRS)
596 cf->flags |= CANFD_BRS;
598 err = m_can_fifo_read(cdev, fgi, M_CAN_FIFO_DATA,
599 cf->data, DIV_ROUND_UP(cf->len, 4));
603 stats->rx_bytes += cf->len;
607 timestamp = FIELD_GET(RX_BUF_RXTS_MASK, fifo_header.dlc) << 16;
609 m_can_receive_skb(cdev, skb, timestamp);
616 netdev_err(dev, "FIFO read returned %d\n", err);
620 static int m_can_do_rx_poll(struct net_device *dev, int quota)
622 struct m_can_classdev *cdev = netdev_priv(dev);
631 rxfs = m_can_read(cdev, M_CAN_RXF0S);
632 if (!(rxfs & RXFS_FFL_MASK)) {
633 netdev_dbg(dev, "no messages in fifo0\n");
637 rx_count = FIELD_GET(RXFS_FFL_MASK, rxfs);
638 fgi = FIELD_GET(RXFS_FGI_MASK, rxfs);
640 for (i = 0; i < rx_count && quota > 0; ++i) {
641 err = m_can_read_fifo(dev, fgi);
648 fgi = (++fgi >= cdev->mcfg[MRAM_RXF0].num ? 0 : fgi);
652 m_can_write(cdev, M_CAN_RXF0A, ack_fgi);
660 static int m_can_handle_lost_msg(struct net_device *dev)
662 struct m_can_classdev *cdev = netdev_priv(dev);
663 struct net_device_stats *stats = &dev->stats;
665 struct can_frame *frame;
668 netdev_err(dev, "msg lost in rxf0\n");
671 stats->rx_over_errors++;
673 skb = alloc_can_err_skb(dev, &frame);
677 frame->can_id |= CAN_ERR_CRTL;
678 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
680 if (cdev->is_peripheral)
681 timestamp = m_can_get_timestamp(cdev);
683 m_can_receive_skb(cdev, skb, timestamp);
688 static int m_can_handle_lec_err(struct net_device *dev,
689 enum m_can_lec_type lec_type)
691 struct m_can_classdev *cdev = netdev_priv(dev);
692 struct net_device_stats *stats = &dev->stats;
693 struct can_frame *cf;
697 cdev->can.can_stats.bus_error++;
699 /* propagate the error condition to the CAN stack */
700 skb = alloc_can_err_skb(dev, &cf);
702 /* check for 'last error code' which tells us the
703 * type of the last error to occur on the CAN bus
706 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
709 case LEC_STUFF_ERROR:
710 netdev_dbg(dev, "stuff error\n");
713 cf->data[2] |= CAN_ERR_PROT_STUFF;
716 netdev_dbg(dev, "form error\n");
719 cf->data[2] |= CAN_ERR_PROT_FORM;
722 netdev_dbg(dev, "ack error\n");
725 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
728 netdev_dbg(dev, "bit1 error\n");
731 cf->data[2] |= CAN_ERR_PROT_BIT1;
734 netdev_dbg(dev, "bit0 error\n");
737 cf->data[2] |= CAN_ERR_PROT_BIT0;
740 netdev_dbg(dev, "CRC error\n");
743 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
752 if (cdev->is_peripheral)
753 timestamp = m_can_get_timestamp(cdev);
755 m_can_receive_skb(cdev, skb, timestamp);
760 static int __m_can_get_berr_counter(const struct net_device *dev,
761 struct can_berr_counter *bec)
763 struct m_can_classdev *cdev = netdev_priv(dev);
766 ecr = m_can_read(cdev, M_CAN_ECR);
767 bec->rxerr = FIELD_GET(ECR_REC_MASK, ecr);
768 bec->txerr = FIELD_GET(ECR_TEC_MASK, ecr);
773 static int m_can_clk_start(struct m_can_classdev *cdev)
775 if (cdev->pm_clock_support == 0)
778 return pm_runtime_resume_and_get(cdev->dev);
781 static void m_can_clk_stop(struct m_can_classdev *cdev)
783 if (cdev->pm_clock_support)
784 pm_runtime_put_sync(cdev->dev);
787 static int m_can_get_berr_counter(const struct net_device *dev,
788 struct can_berr_counter *bec)
790 struct m_can_classdev *cdev = netdev_priv(dev);
793 err = m_can_clk_start(cdev);
797 __m_can_get_berr_counter(dev, bec);
799 m_can_clk_stop(cdev);
804 static int m_can_handle_state_change(struct net_device *dev,
805 enum can_state new_state)
807 struct m_can_classdev *cdev = netdev_priv(dev);
808 struct can_frame *cf;
810 struct can_berr_counter bec;
815 case CAN_STATE_ERROR_WARNING:
816 /* error warning state */
817 cdev->can.can_stats.error_warning++;
818 cdev->can.state = CAN_STATE_ERROR_WARNING;
820 case CAN_STATE_ERROR_PASSIVE:
821 /* error passive state */
822 cdev->can.can_stats.error_passive++;
823 cdev->can.state = CAN_STATE_ERROR_PASSIVE;
825 case CAN_STATE_BUS_OFF:
827 cdev->can.state = CAN_STATE_BUS_OFF;
828 m_can_disable_all_interrupts(cdev);
829 cdev->can.can_stats.bus_off++;
836 /* propagate the error condition to the CAN stack */
837 skb = alloc_can_err_skb(dev, &cf);
841 __m_can_get_berr_counter(dev, &bec);
844 case CAN_STATE_ERROR_WARNING:
845 /* error warning state */
846 cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
847 cf->data[1] = (bec.txerr > bec.rxerr) ?
848 CAN_ERR_CRTL_TX_WARNING :
849 CAN_ERR_CRTL_RX_WARNING;
850 cf->data[6] = bec.txerr;
851 cf->data[7] = bec.rxerr;
853 case CAN_STATE_ERROR_PASSIVE:
854 /* error passive state */
855 cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
856 ecr = m_can_read(cdev, M_CAN_ECR);
858 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
860 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
861 cf->data[6] = bec.txerr;
862 cf->data[7] = bec.rxerr;
864 case CAN_STATE_BUS_OFF:
866 cf->can_id |= CAN_ERR_BUSOFF;
872 if (cdev->is_peripheral)
873 timestamp = m_can_get_timestamp(cdev);
875 m_can_receive_skb(cdev, skb, timestamp);
880 static int m_can_handle_state_errors(struct net_device *dev, u32 psr)
882 struct m_can_classdev *cdev = netdev_priv(dev);
885 if (psr & PSR_EW && cdev->can.state != CAN_STATE_ERROR_WARNING) {
886 netdev_dbg(dev, "entered error warning state\n");
887 work_done += m_can_handle_state_change(dev,
888 CAN_STATE_ERROR_WARNING);
891 if (psr & PSR_EP && cdev->can.state != CAN_STATE_ERROR_PASSIVE) {
892 netdev_dbg(dev, "entered error passive state\n");
893 work_done += m_can_handle_state_change(dev,
894 CAN_STATE_ERROR_PASSIVE);
897 if (psr & PSR_BO && cdev->can.state != CAN_STATE_BUS_OFF) {
898 netdev_dbg(dev, "entered error bus off state\n");
899 work_done += m_can_handle_state_change(dev,
906 static void m_can_handle_other_err(struct net_device *dev, u32 irqstatus)
908 if (irqstatus & IR_WDI)
909 netdev_err(dev, "Message RAM Watchdog event due to missing READY\n");
910 if (irqstatus & IR_BEU)
911 netdev_err(dev, "Bit Error Uncorrected\n");
912 if (irqstatus & IR_BEC)
913 netdev_err(dev, "Bit Error Corrected\n");
914 if (irqstatus & IR_TOO)
915 netdev_err(dev, "Timeout reached\n");
916 if (irqstatus & IR_MRAF)
917 netdev_err(dev, "Message RAM access failure occurred\n");
920 static inline bool is_lec_err(u8 lec)
922 return lec != LEC_NO_ERROR && lec != LEC_NO_CHANGE;
925 static inline bool m_can_is_protocol_err(u32 irqstatus)
927 return irqstatus & IR_ERR_LEC_31X;
930 static int m_can_handle_protocol_error(struct net_device *dev, u32 irqstatus)
932 struct net_device_stats *stats = &dev->stats;
933 struct m_can_classdev *cdev = netdev_priv(dev);
934 struct can_frame *cf;
938 /* propagate the error condition to the CAN stack */
939 skb = alloc_can_err_skb(dev, &cf);
941 /* update tx error stats since there is protocol error */
944 /* update arbitration lost status */
945 if (cdev->version >= 31 && (irqstatus & IR_PEA)) {
946 netdev_dbg(dev, "Protocol error in Arbitration fail\n");
947 cdev->can.can_stats.arbitration_lost++;
949 cf->can_id |= CAN_ERR_LOSTARB;
950 cf->data[0] |= CAN_ERR_LOSTARB_UNSPEC;
954 if (unlikely(!skb)) {
955 netdev_dbg(dev, "allocation of skb failed\n");
959 if (cdev->is_peripheral)
960 timestamp = m_can_get_timestamp(cdev);
962 m_can_receive_skb(cdev, skb, timestamp);
967 static int m_can_handle_bus_errors(struct net_device *dev, u32 irqstatus,
970 struct m_can_classdev *cdev = netdev_priv(dev);
973 if (irqstatus & IR_RF0L)
974 work_done += m_can_handle_lost_msg(dev);
976 /* handle lec errors on the bus */
977 if (cdev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) {
978 u8 lec = FIELD_GET(PSR_LEC_MASK, psr);
979 u8 dlec = FIELD_GET(PSR_DLEC_MASK, psr);
981 if (is_lec_err(lec)) {
982 netdev_dbg(dev, "Arbitration phase error detected\n");
983 work_done += m_can_handle_lec_err(dev, lec);
986 if (is_lec_err(dlec)) {
987 netdev_dbg(dev, "Data phase error detected\n");
988 work_done += m_can_handle_lec_err(dev, dlec);
992 /* handle protocol errors in arbitration phase */
993 if ((cdev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
994 m_can_is_protocol_err(irqstatus))
995 work_done += m_can_handle_protocol_error(dev, irqstatus);
997 /* other unproccessed error interrupts */
998 m_can_handle_other_err(dev, irqstatus);
1003 static int m_can_rx_handler(struct net_device *dev, int quota, u32 irqstatus)
1005 struct m_can_classdev *cdev = netdev_priv(dev);
1012 /* Errata workaround for issue "Needless activation of MRAF irq"
1013 * During frame reception while the MCAN is in Error Passive state
1014 * and the Receive Error Counter has the value MCAN_ECR.REC = 127,
1015 * it may happen that MCAN_IR.MRAF is set although there was no
1016 * Message RAM access failure.
1017 * If MCAN_IR.MRAF is enabled, an interrupt to the Host CPU is generated
1018 * The Message RAM Access Failure interrupt routine needs to check
1019 * whether MCAN_ECR.RP = ’1’ and MCAN_ECR.REC = 127.
1020 * In this case, reset MCAN_IR.MRAF. No further action is required.
1022 if (cdev->version <= 31 && irqstatus & IR_MRAF &&
1023 m_can_read(cdev, M_CAN_ECR) & ECR_RP) {
1024 struct can_berr_counter bec;
1026 __m_can_get_berr_counter(dev, &bec);
1027 if (bec.rxerr == 127) {
1028 m_can_write(cdev, M_CAN_IR, IR_MRAF);
1029 irqstatus &= ~IR_MRAF;
1033 if (irqstatus & IR_ERR_STATE)
1034 work_done += m_can_handle_state_errors(dev,
1035 m_can_read(cdev, M_CAN_PSR));
1037 if (irqstatus & IR_ERR_BUS_30X)
1038 work_done += m_can_handle_bus_errors(dev, irqstatus,
1039 m_can_read(cdev, M_CAN_PSR));
1041 if (irqstatus & IR_RF0N) {
1042 rx_work_or_err = m_can_do_rx_poll(dev, (quota - work_done));
1043 if (rx_work_or_err < 0)
1044 return rx_work_or_err;
1046 work_done += rx_work_or_err;
1052 static int m_can_poll(struct napi_struct *napi, int quota)
1054 struct net_device *dev = napi->dev;
1055 struct m_can_classdev *cdev = netdev_priv(dev);
1059 irqstatus = cdev->irqstatus | m_can_read(cdev, M_CAN_IR);
1061 work_done = m_can_rx_handler(dev, quota, irqstatus);
1063 /* Don't re-enable interrupts if the driver had a fatal error
1064 * (e.g., FIFO read failure).
1066 if (work_done >= 0 && work_done < quota) {
1067 napi_complete_done(napi, work_done);
1068 m_can_enable_all_interrupts(cdev);
1074 /* Echo tx skb and update net stats. Peripherals use rx-offload for
1075 * echo. timestamp is used for peripherals to ensure correct ordering
1076 * by rx-offload, and is ignored for non-peripherals.
1078 static unsigned int m_can_tx_update_stats(struct m_can_classdev *cdev,
1079 unsigned int msg_mark, u32 timestamp)
1081 struct net_device *dev = cdev->net;
1082 struct net_device_stats *stats = &dev->stats;
1083 unsigned int frame_len;
1085 if (cdev->is_peripheral)
1087 can_rx_offload_get_echo_skb_queue_timestamp(&cdev->offload,
1092 stats->tx_bytes += can_get_echo_skb(dev, msg_mark, &frame_len);
1094 stats->tx_packets++;
1099 static void m_can_finish_tx(struct m_can_classdev *cdev, int transmitted,
1100 unsigned int transmitted_frame_len)
1102 unsigned long irqflags;
1104 netdev_completed_queue(cdev->net, transmitted, transmitted_frame_len);
1106 spin_lock_irqsave(&cdev->tx_handling_spinlock, irqflags);
1107 if (cdev->tx_fifo_in_flight >= cdev->tx_fifo_size && transmitted > 0)
1108 netif_wake_queue(cdev->net);
1109 cdev->tx_fifo_in_flight -= transmitted;
1110 spin_unlock_irqrestore(&cdev->tx_handling_spinlock, irqflags);
1113 static netdev_tx_t m_can_start_tx(struct m_can_classdev *cdev)
1115 unsigned long irqflags;
1116 int tx_fifo_in_flight;
1118 spin_lock_irqsave(&cdev->tx_handling_spinlock, irqflags);
1119 tx_fifo_in_flight = cdev->tx_fifo_in_flight + 1;
1120 if (tx_fifo_in_flight >= cdev->tx_fifo_size) {
1121 netif_stop_queue(cdev->net);
1122 if (tx_fifo_in_flight > cdev->tx_fifo_size) {
1123 netdev_err_once(cdev->net, "hard_xmit called while TX FIFO full\n");
1124 spin_unlock_irqrestore(&cdev->tx_handling_spinlock, irqflags);
1125 return NETDEV_TX_BUSY;
1128 cdev->tx_fifo_in_flight = tx_fifo_in_flight;
1129 spin_unlock_irqrestore(&cdev->tx_handling_spinlock, irqflags);
1131 return NETDEV_TX_OK;
1134 static int m_can_echo_tx_event(struct net_device *dev)
1142 unsigned int msg_mark;
1144 unsigned int processed_frame_len = 0;
1146 struct m_can_classdev *cdev = netdev_priv(dev);
1148 /* read tx event fifo status */
1149 m_can_txefs = m_can_read(cdev, M_CAN_TXEFS);
1151 /* Get Tx Event fifo element count */
1152 txe_count = FIELD_GET(TXEFS_EFFL_MASK, m_can_txefs);
1153 fgi = FIELD_GET(TXEFS_EFGI_MASK, m_can_txefs);
1155 /* Get and process all sent elements */
1156 for (i = 0; i < txe_count; i++) {
1157 u32 txe, timestamp = 0;
1159 /* get message marker, timestamp */
1160 err = m_can_txe_fifo_read(cdev, fgi, 4, &txe);
1162 netdev_err(dev, "TXE FIFO read returned %d\n", err);
1166 msg_mark = FIELD_GET(TX_EVENT_MM_MASK, txe);
1167 timestamp = FIELD_GET(TX_EVENT_TXTS_MASK, txe) << 16;
1170 fgi = (++fgi >= cdev->mcfg[MRAM_TXE].num ? 0 : fgi);
1173 processed_frame_len += m_can_tx_update_stats(cdev, msg_mark,
1180 m_can_write(cdev, M_CAN_TXEFA, FIELD_PREP(TXEFA_EFAI_MASK,
1183 m_can_finish_tx(cdev, processed, processed_frame_len);
1188 static void m_can_coalescing_update(struct m_can_classdev *cdev, u32 ir)
1190 u32 new_interrupts = cdev->active_interrupts;
1191 bool enable_rx_timer = false;
1192 bool enable_tx_timer = false;
1194 if (!cdev->net->irq)
1197 if (cdev->rx_coalesce_usecs_irq > 0 && (ir & (IR_RF0N | IR_RF0W))) {
1198 enable_rx_timer = true;
1199 new_interrupts &= ~IR_RF0N;
1201 if (cdev->tx_coalesce_usecs_irq > 0 && (ir & (IR_TEFN | IR_TEFW))) {
1202 enable_tx_timer = true;
1203 new_interrupts &= ~IR_TEFN;
1205 if (!enable_rx_timer && !hrtimer_active(&cdev->hrtimer))
1206 new_interrupts |= IR_RF0N;
1207 if (!enable_tx_timer && !hrtimer_active(&cdev->hrtimer))
1208 new_interrupts |= IR_TEFN;
1210 m_can_interrupt_enable(cdev, new_interrupts);
1211 if (enable_rx_timer | enable_tx_timer)
1212 hrtimer_start(&cdev->hrtimer, cdev->irq_timer_wait,
1216 /* This interrupt handler is called either from the interrupt thread or a
1217 * hrtimer. This has implications like cancelling a timer won't be possible
1220 static int m_can_interrupt_handler(struct m_can_classdev *cdev)
1222 struct net_device *dev = cdev->net;
1223 u32 ir = 0, ir_read;
1226 if (pm_runtime_suspended(cdev->dev))
1229 /* The m_can controller signals its interrupt status as a level, but
1230 * depending in the integration the CPU may interpret the signal as
1231 * edge-triggered (for example with m_can_pci). For these
1232 * edge-triggered integrations, we must observe that IR is 0 at least
1233 * once to be sure that the next interrupt will generate an edge.
1235 while ((ir_read = m_can_read(cdev, M_CAN_IR)) != 0) {
1239 m_can_write(cdev, M_CAN_IR, ir);
1241 if (!cdev->irq_edge_triggered)
1245 m_can_coalescing_update(cdev, ir);
1249 if (cdev->ops->clear_interrupts)
1250 cdev->ops->clear_interrupts(cdev);
1252 /* schedule NAPI in case of
1254 * - state change IRQ
1255 * - bus error IRQ and bus error reporting
1257 if (ir & (IR_RF0N | IR_RF0W | IR_ERR_ALL_30X)) {
1258 cdev->irqstatus = ir;
1259 if (!cdev->is_peripheral) {
1260 m_can_disable_all_interrupts(cdev);
1261 napi_schedule(&cdev->napi);
1263 ret = m_can_rx_handler(dev, NAPI_POLL_WEIGHT, ir);
1269 if (cdev->version == 30) {
1271 /* Transmission Complete Interrupt*/
1273 unsigned int frame_len;
1275 if (cdev->is_peripheral)
1276 timestamp = m_can_get_timestamp(cdev);
1277 frame_len = m_can_tx_update_stats(cdev, 0, timestamp);
1278 m_can_finish_tx(cdev, 1, frame_len);
1281 if (ir & (IR_TEFN | IR_TEFW)) {
1282 /* New TX FIFO Element arrived */
1283 ret = m_can_echo_tx_event(dev);
1289 if (cdev->is_peripheral)
1290 can_rx_offload_threaded_irq_finish(&cdev->offload);
1295 static irqreturn_t m_can_isr(int irq, void *dev_id)
1297 struct net_device *dev = (struct net_device *)dev_id;
1298 struct m_can_classdev *cdev = netdev_priv(dev);
1301 ret = m_can_interrupt_handler(cdev);
1303 m_can_disable_all_interrupts(cdev);
1310 static enum hrtimer_restart m_can_coalescing_timer(struct hrtimer *timer)
1312 struct m_can_classdev *cdev = container_of(timer, struct m_can_classdev, hrtimer);
1314 if (cdev->can.state == CAN_STATE_BUS_OFF ||
1315 cdev->can.state == CAN_STATE_STOPPED)
1316 return HRTIMER_NORESTART;
1318 irq_wake_thread(cdev->net->irq, cdev->net);
1320 return HRTIMER_NORESTART;
1323 static const struct can_bittiming_const m_can_bittiming_const_30X = {
1324 .name = KBUILD_MODNAME,
1325 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
1327 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
1335 static const struct can_bittiming_const m_can_data_bittiming_const_30X = {
1336 .name = KBUILD_MODNAME,
1337 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
1339 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
1347 static const struct can_bittiming_const m_can_bittiming_const_31X = {
1348 .name = KBUILD_MODNAME,
1349 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
1351 .tseg2_min = 2, /* Time segment 2 = phase_seg2 */
1359 static const struct can_bittiming_const m_can_data_bittiming_const_31X = {
1360 .name = KBUILD_MODNAME,
1361 .tseg1_min = 1, /* Time segment 1 = prop_seg + phase_seg1 */
1363 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
1371 static int m_can_set_bittiming(struct net_device *dev)
1373 struct m_can_classdev *cdev = netdev_priv(dev);
1374 const struct can_bittiming *bt = &cdev->can.bittiming;
1375 const struct can_bittiming *dbt = &cdev->can.data_bittiming;
1376 u16 brp, sjw, tseg1, tseg2;
1381 tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
1382 tseg2 = bt->phase_seg2 - 1;
1383 reg_btp = FIELD_PREP(NBTP_NBRP_MASK, brp) |
1384 FIELD_PREP(NBTP_NSJW_MASK, sjw) |
1385 FIELD_PREP(NBTP_NTSEG1_MASK, tseg1) |
1386 FIELD_PREP(NBTP_NTSEG2_MASK, tseg2);
1387 m_can_write(cdev, M_CAN_NBTP, reg_btp);
1389 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD) {
1393 tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1;
1394 tseg2 = dbt->phase_seg2 - 1;
1396 /* TDC is only needed for bitrates beyond 2.5 MBit/s.
1397 * This is mentioned in the "Bit Time Requirements for CAN FD"
1398 * paper presented at the International CAN Conference 2013
1400 if (dbt->bitrate > 2500000) {
1403 /* Use the same value of secondary sampling point
1404 * as the data sampling point
1406 ssp = dbt->sample_point;
1408 /* Equation based on Bosch's M_CAN User Manual's
1409 * Transmitter Delay Compensation Section
1411 tdco = (cdev->can.clock.freq / 1000) *
1414 /* Max valid TDCO value is 127 */
1416 netdev_warn(dev, "TDCO value of %u is beyond maximum. Using maximum possible value\n",
1421 reg_btp |= DBTP_TDC;
1422 m_can_write(cdev, M_CAN_TDCR,
1423 FIELD_PREP(TDCR_TDCO_MASK, tdco));
1426 reg_btp |= FIELD_PREP(DBTP_DBRP_MASK, brp) |
1427 FIELD_PREP(DBTP_DSJW_MASK, sjw) |
1428 FIELD_PREP(DBTP_DTSEG1_MASK, tseg1) |
1429 FIELD_PREP(DBTP_DTSEG2_MASK, tseg2);
1431 m_can_write(cdev, M_CAN_DBTP, reg_btp);
1437 /* Configure M_CAN chip:
1438 * - set rx buffer/fifo element size
1439 * - configure rx fifo
1440 * - accept non-matching frame into fifo 0
1441 * - configure tx buffer
1442 * - >= v3.1.x: TX FIFO is used
1445 * - configure timestamp generation
1447 static int m_can_chip_config(struct net_device *dev)
1449 struct m_can_classdev *cdev = netdev_priv(dev);
1450 u32 interrupts = IR_ALL_INT;
1454 err = m_can_init_ram(cdev);
1456 dev_err(cdev->dev, "Message RAM configuration failed\n");
1460 /* Disable unused interrupts */
1461 interrupts &= ~(IR_ARA | IR_ELO | IR_DRX | IR_TEFF | IR_TFE | IR_TCF |
1462 IR_HPM | IR_RF1F | IR_RF1W | IR_RF1N | IR_RF0F |
1465 err = m_can_config_enable(cdev);
1469 /* RX Buffer/FIFO Element Size 64 bytes data field */
1470 m_can_write(cdev, M_CAN_RXESC,
1471 FIELD_PREP(RXESC_RBDS_MASK, RXESC_64B) |
1472 FIELD_PREP(RXESC_F1DS_MASK, RXESC_64B) |
1473 FIELD_PREP(RXESC_F0DS_MASK, RXESC_64B));
1475 /* Accept Non-matching Frames Into FIFO 0 */
1476 m_can_write(cdev, M_CAN_GFC, 0x0);
1478 if (cdev->version == 30) {
1479 /* only support one Tx Buffer currently */
1480 m_can_write(cdev, M_CAN_TXBC, FIELD_PREP(TXBC_NDTB_MASK, 1) |
1481 cdev->mcfg[MRAM_TXB].off);
1483 /* TX FIFO is used for newer IP Core versions */
1484 m_can_write(cdev, M_CAN_TXBC,
1485 FIELD_PREP(TXBC_TFQS_MASK,
1486 cdev->mcfg[MRAM_TXB].num) |
1487 cdev->mcfg[MRAM_TXB].off);
1490 /* support 64 bytes payload */
1491 m_can_write(cdev, M_CAN_TXESC,
1492 FIELD_PREP(TXESC_TBDS_MASK, TXESC_TBDS_64B));
1495 if (cdev->version == 30) {
1496 m_can_write(cdev, M_CAN_TXEFC,
1497 FIELD_PREP(TXEFC_EFS_MASK, 1) |
1498 cdev->mcfg[MRAM_TXE].off);
1500 /* Full TX Event FIFO is used */
1501 m_can_write(cdev, M_CAN_TXEFC,
1502 FIELD_PREP(TXEFC_EFWM_MASK,
1503 cdev->tx_max_coalesced_frames_irq) |
1504 FIELD_PREP(TXEFC_EFS_MASK,
1505 cdev->mcfg[MRAM_TXE].num) |
1506 cdev->mcfg[MRAM_TXE].off);
1509 /* rx fifo configuration, blocking mode, fifo size 1 */
1510 m_can_write(cdev, M_CAN_RXF0C,
1511 FIELD_PREP(RXFC_FWM_MASK, cdev->rx_max_coalesced_frames_irq) |
1512 FIELD_PREP(RXFC_FS_MASK, cdev->mcfg[MRAM_RXF0].num) |
1513 cdev->mcfg[MRAM_RXF0].off);
1515 m_can_write(cdev, M_CAN_RXF1C,
1516 FIELD_PREP(RXFC_FS_MASK, cdev->mcfg[MRAM_RXF1].num) |
1517 cdev->mcfg[MRAM_RXF1].off);
1519 cccr = m_can_read(cdev, M_CAN_CCCR);
1520 test = m_can_read(cdev, M_CAN_TEST);
1522 if (cdev->version == 30) {
1525 cccr &= ~(CCCR_TEST | CCCR_MON | CCCR_DAR |
1526 FIELD_PREP(CCCR_CMR_MASK, FIELD_MAX(CCCR_CMR_MASK)) |
1527 FIELD_PREP(CCCR_CME_MASK, FIELD_MAX(CCCR_CME_MASK)));
1529 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD)
1530 cccr |= FIELD_PREP(CCCR_CME_MASK, CCCR_CME_CANFD_BRS);
1533 /* Version 3.1.x or 3.2.x */
1534 cccr &= ~(CCCR_TEST | CCCR_MON | CCCR_BRSE | CCCR_FDOE |
1535 CCCR_NISO | CCCR_DAR);
1537 /* Only 3.2.x has NISO Bit implemented */
1538 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
1541 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD)
1542 cccr |= (CCCR_BRSE | CCCR_FDOE);
1546 if (cdev->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
1547 cccr |= CCCR_TEST | CCCR_MON;
1551 /* Enable Monitoring (all versions) */
1552 if (cdev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1555 /* Disable Auto Retransmission (all versions) */
1556 if (cdev->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
1560 m_can_write(cdev, M_CAN_CCCR, cccr);
1561 m_can_write(cdev, M_CAN_TEST, test);
1563 /* Enable interrupts */
1564 if (!(cdev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) {
1565 if (cdev->version == 30)
1566 interrupts &= ~(IR_ERR_LEC_30X);
1568 interrupts &= ~(IR_ERR_LEC_31X);
1570 cdev->active_interrupts = 0;
1571 m_can_interrupt_enable(cdev, interrupts);
1573 /* route all interrupts to INT0 */
1574 m_can_write(cdev, M_CAN_ILS, ILS_ALL_INT0);
1576 /* set bittiming params */
1577 m_can_set_bittiming(dev);
1579 /* enable internal timestamp generation, with a prescaler of 16. The
1580 * prescaler is applied to the nominal bit timing
1582 m_can_write(cdev, M_CAN_TSCC,
1583 FIELD_PREP(TSCC_TCP_MASK, 0xf) |
1584 FIELD_PREP(TSCC_TSS_MASK, TSCC_TSS_INTERNAL));
1586 err = m_can_config_disable(cdev);
1590 if (cdev->ops->init)
1591 cdev->ops->init(cdev);
1596 static int m_can_start(struct net_device *dev)
1598 struct m_can_classdev *cdev = netdev_priv(dev);
1601 /* basic m_can configuration */
1602 ret = m_can_chip_config(dev);
1606 netdev_queue_set_dql_min_limit(netdev_get_tx_queue(cdev->net, 0),
1607 cdev->tx_max_coalesced_frames);
1609 cdev->can.state = CAN_STATE_ERROR_ACTIVE;
1611 m_can_enable_all_interrupts(cdev);
1613 if (cdev->version > 30)
1614 cdev->tx_fifo_putidx = FIELD_GET(TXFQS_TFQPI_MASK,
1615 m_can_read(cdev, M_CAN_TXFQS));
1617 ret = m_can_cccr_update_bits(cdev, CCCR_INIT, 0);
1619 netdev_err(dev, "failed to enter normal mode\n");
1624 static int m_can_set_mode(struct net_device *dev, enum can_mode mode)
1627 case CAN_MODE_START:
1630 netif_wake_queue(dev);
1639 /* Checks core release number of M_CAN
1640 * returns 0 if an unsupported device is detected
1641 * else it returns the release and step coded as:
1642 * return value = 10 * <release> + 1 * <step>
1644 static int m_can_check_core_release(struct m_can_classdev *cdev)
1651 /* Read Core Release Version and split into version number
1652 * Example: Version 3.2.1 => rel = 3; step = 2; substep = 1;
1654 crel_reg = m_can_read(cdev, M_CAN_CREL);
1655 rel = (u8)FIELD_GET(CREL_REL_MASK, crel_reg);
1656 step = (u8)FIELD_GET(CREL_STEP_MASK, crel_reg);
1659 /* M_CAN v3.x.y: create return value */
1662 /* Unsupported M_CAN version */
1669 /* Selectable Non ISO support only in version 3.2.x
1670 * Return 1 if the bit is writable, 0 if it is not, or negative on error.
1672 static int m_can_niso_supported(struct m_can_classdev *cdev)
1676 ret = m_can_config_enable(cdev);
1680 /* First try to set the NISO bit. */
1681 niso = m_can_cccr_update_bits(cdev, CCCR_NISO, CCCR_NISO);
1683 /* Then clear the it again. */
1684 ret = m_can_cccr_update_bits(cdev, CCCR_NISO, 0);
1686 dev_err(cdev->dev, "failed to revert the NON-ISO bit in CCCR\n");
1690 ret = m_can_config_disable(cdev);
1697 static int m_can_dev_setup(struct m_can_classdev *cdev)
1699 struct net_device *dev = cdev->net;
1700 int m_can_version, err, niso;
1702 m_can_version = m_can_check_core_release(cdev);
1703 /* return if unsupported version */
1704 if (!m_can_version) {
1705 dev_err(cdev->dev, "Unsupported version number: %2d",
1710 /* Write the INIT bit, in case no hardware reset has happened before
1711 * the probe (for example, it was observed that the Intel Elkhart Lake
1712 * SoCs do not properly reset the CAN controllers on reboot)
1714 err = m_can_cccr_update_bits(cdev, CCCR_INIT, CCCR_INIT);
1718 if (!cdev->is_peripheral)
1719 netif_napi_add(dev, &cdev->napi, m_can_poll);
1721 /* Shared properties of all M_CAN versions */
1722 cdev->version = m_can_version;
1723 cdev->can.do_set_mode = m_can_set_mode;
1724 cdev->can.do_get_berr_counter = m_can_get_berr_counter;
1726 /* Set M_CAN supported operations */
1727 cdev->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1728 CAN_CTRLMODE_LISTENONLY |
1729 CAN_CTRLMODE_BERR_REPORTING |
1731 CAN_CTRLMODE_ONE_SHOT;
1733 /* Set properties depending on M_CAN version */
1734 switch (cdev->version) {
1736 /* CAN_CTRLMODE_FD_NON_ISO is fixed with M_CAN IP v3.0.x */
1737 err = can_set_static_ctrlmode(dev, CAN_CTRLMODE_FD_NON_ISO);
1740 cdev->can.bittiming_const = &m_can_bittiming_const_30X;
1741 cdev->can.data_bittiming_const = &m_can_data_bittiming_const_30X;
1744 /* CAN_CTRLMODE_FD_NON_ISO is fixed with M_CAN IP v3.1.x */
1745 err = can_set_static_ctrlmode(dev, CAN_CTRLMODE_FD_NON_ISO);
1748 cdev->can.bittiming_const = &m_can_bittiming_const_31X;
1749 cdev->can.data_bittiming_const = &m_can_data_bittiming_const_31X;
1753 /* Support both MCAN version v3.2.x and v3.3.0 */
1754 cdev->can.bittiming_const = &m_can_bittiming_const_31X;
1755 cdev->can.data_bittiming_const = &m_can_data_bittiming_const_31X;
1757 niso = m_can_niso_supported(cdev);
1761 cdev->can.ctrlmode_supported |= CAN_CTRLMODE_FD_NON_ISO;
1764 dev_err(cdev->dev, "Unsupported version number: %2d",
1772 static void m_can_stop(struct net_device *dev)
1774 struct m_can_classdev *cdev = netdev_priv(dev);
1777 /* disable all interrupts */
1778 m_can_disable_all_interrupts(cdev);
1780 /* Set init mode to disengage from the network */
1781 ret = m_can_cccr_update_bits(cdev, CCCR_INIT, CCCR_INIT);
1783 netdev_err(dev, "failed to enter standby mode: %pe\n",
1786 /* set the state as STOPPED */
1787 cdev->can.state = CAN_STATE_STOPPED;
1789 if (cdev->ops->deinit) {
1790 ret = cdev->ops->deinit(cdev);
1792 netdev_err(dev, "failed to deinitialize: %pe\n",
1797 static int m_can_close(struct net_device *dev)
1799 struct m_can_classdev *cdev = netdev_priv(dev);
1801 netif_stop_queue(dev);
1805 free_irq(dev->irq, dev);
1809 if (cdev->is_peripheral) {
1810 destroy_workqueue(cdev->tx_wq);
1812 can_rx_offload_disable(&cdev->offload);
1814 napi_disable(&cdev->napi);
1819 m_can_clk_stop(cdev);
1820 phy_power_off(cdev->transceiver);
1825 static netdev_tx_t m_can_tx_handler(struct m_can_classdev *cdev,
1826 struct sk_buff *skb)
1828 struct canfd_frame *cf = (struct canfd_frame *)skb->data;
1829 u8 len_padded = DIV_ROUND_UP(cf->len, 4);
1830 struct m_can_fifo_element fifo_element;
1831 struct net_device *dev = cdev->net;
1835 unsigned int frame_len = can_skb_get_frame_len(skb);
1837 /* Generate ID field for TX buffer Element */
1838 /* Common to all supported M_CAN versions */
1839 if (cf->can_id & CAN_EFF_FLAG) {
1840 fifo_element.id = cf->can_id & CAN_EFF_MASK;
1841 fifo_element.id |= TX_BUF_XTD;
1843 fifo_element.id = ((cf->can_id & CAN_SFF_MASK) << 18);
1846 if (cf->can_id & CAN_RTR_FLAG)
1847 fifo_element.id |= TX_BUF_RTR;
1849 if (cdev->version == 30) {
1850 netif_stop_queue(dev);
1852 fifo_element.dlc = can_fd_len2dlc(cf->len) << 16;
1854 /* Write the frame ID, DLC, and payload to the FIFO element. */
1855 err = m_can_fifo_write(cdev, 0, M_CAN_FIFO_ID, &fifo_element, 2);
1859 err = m_can_fifo_write(cdev, 0, M_CAN_FIFO_DATA,
1860 cf->data, len_padded);
1864 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD) {
1865 cccr = m_can_read(cdev, M_CAN_CCCR);
1866 cccr &= ~CCCR_CMR_MASK;
1867 if (can_is_canfd_skb(skb)) {
1868 if (cf->flags & CANFD_BRS)
1869 cccr |= FIELD_PREP(CCCR_CMR_MASK,
1870 CCCR_CMR_CANFD_BRS);
1872 cccr |= FIELD_PREP(CCCR_CMR_MASK,
1875 cccr |= FIELD_PREP(CCCR_CMR_MASK, CCCR_CMR_CAN);
1877 m_can_write(cdev, M_CAN_CCCR, cccr);
1879 m_can_write(cdev, M_CAN_TXBTIE, 0x1);
1881 can_put_echo_skb(skb, dev, 0, frame_len);
1883 m_can_write(cdev, M_CAN_TXBAR, 0x1);
1884 /* End of xmit function for version 3.0.x */
1886 /* Transmit routine for version >= v3.1.x */
1888 /* get put index for frame */
1889 putidx = cdev->tx_fifo_putidx;
1891 /* Construct DLC Field, with CAN-FD configuration.
1892 * Use the put index of the fifo as the message marker,
1893 * used in the TX interrupt for sending the correct echo frame.
1896 /* get CAN FD configuration of frame */
1898 if (can_is_canfd_skb(skb)) {
1899 fdflags |= TX_BUF_FDF;
1900 if (cf->flags & CANFD_BRS)
1901 fdflags |= TX_BUF_BRS;
1904 fifo_element.dlc = FIELD_PREP(TX_BUF_MM_MASK, putidx) |
1905 FIELD_PREP(TX_BUF_DLC_MASK, can_fd_len2dlc(cf->len)) |
1906 fdflags | TX_BUF_EFC;
1908 memcpy_and_pad(fifo_element.data, CANFD_MAX_DLEN, &cf->data,
1911 err = m_can_fifo_write(cdev, putidx, M_CAN_FIFO_ID,
1912 &fifo_element, 2 + len_padded);
1916 /* Push loopback echo.
1917 * Will be looped back on TX interrupt based on message marker
1919 can_put_echo_skb(skb, dev, putidx, frame_len);
1921 if (cdev->is_peripheral) {
1922 /* Delay enabling TX FIFO element */
1923 cdev->tx_peripheral_submit |= BIT(putidx);
1925 /* Enable TX FIFO element to start transfer */
1926 m_can_write(cdev, M_CAN_TXBAR, BIT(putidx));
1928 cdev->tx_fifo_putidx = (++cdev->tx_fifo_putidx >= cdev->can.echo_skb_max ?
1929 0 : cdev->tx_fifo_putidx);
1932 return NETDEV_TX_OK;
1935 netdev_err(dev, "FIFO write returned %d\n", err);
1936 m_can_disable_all_interrupts(cdev);
1937 return NETDEV_TX_BUSY;
1940 static void m_can_tx_submit(struct m_can_classdev *cdev)
1942 if (cdev->version == 30)
1944 if (!cdev->is_peripheral)
1947 m_can_write(cdev, M_CAN_TXBAR, cdev->tx_peripheral_submit);
1948 cdev->tx_peripheral_submit = 0;
1951 static void m_can_tx_work_queue(struct work_struct *ws)
1953 struct m_can_tx_op *op = container_of(ws, struct m_can_tx_op, work);
1954 struct m_can_classdev *cdev = op->cdev;
1955 struct sk_buff *skb = op->skb;
1958 m_can_tx_handler(cdev, skb);
1960 m_can_tx_submit(cdev);
1963 static void m_can_tx_queue_skb(struct m_can_classdev *cdev, struct sk_buff *skb,
1966 cdev->tx_ops[cdev->next_tx_op].skb = skb;
1967 cdev->tx_ops[cdev->next_tx_op].submit = submit;
1968 queue_work(cdev->tx_wq, &cdev->tx_ops[cdev->next_tx_op].work);
1971 if (cdev->next_tx_op >= cdev->tx_fifo_size)
1972 cdev->next_tx_op = 0;
1975 static netdev_tx_t m_can_start_peripheral_xmit(struct m_can_classdev *cdev,
1976 struct sk_buff *skb)
1980 ++cdev->nr_txs_without_submit;
1981 if (cdev->nr_txs_without_submit >= cdev->tx_max_coalesced_frames ||
1982 !netdev_xmit_more()) {
1983 cdev->nr_txs_without_submit = 0;
1988 m_can_tx_queue_skb(cdev, skb, submit);
1990 return NETDEV_TX_OK;
1993 static netdev_tx_t m_can_start_xmit(struct sk_buff *skb,
1994 struct net_device *dev)
1996 struct m_can_classdev *cdev = netdev_priv(dev);
1997 unsigned int frame_len;
2000 if (can_dev_dropped_skb(dev, skb))
2001 return NETDEV_TX_OK;
2003 frame_len = can_skb_get_frame_len(skb);
2005 if (cdev->can.state == CAN_STATE_BUS_OFF) {
2006 m_can_clean(cdev->net);
2007 return NETDEV_TX_OK;
2010 ret = m_can_start_tx(cdev);
2011 if (ret != NETDEV_TX_OK)
2014 netdev_sent_queue(dev, frame_len);
2016 if (cdev->is_peripheral)
2017 ret = m_can_start_peripheral_xmit(cdev, skb);
2019 ret = m_can_tx_handler(cdev, skb);
2021 if (ret != NETDEV_TX_OK)
2022 netdev_completed_queue(dev, 1, frame_len);
2027 static enum hrtimer_restart hrtimer_callback(struct hrtimer *timer)
2029 struct m_can_classdev *cdev = container_of(timer, struct
2030 m_can_classdev, hrtimer);
2033 if (cdev->can.state == CAN_STATE_BUS_OFF ||
2034 cdev->can.state == CAN_STATE_STOPPED)
2035 return HRTIMER_NORESTART;
2037 ret = m_can_interrupt_handler(cdev);
2039 /* On error or if napi is scheduled to read, stop the timer */
2040 if (ret < 0 || napi_is_scheduled(&cdev->napi))
2041 return HRTIMER_NORESTART;
2043 hrtimer_forward_now(timer, ms_to_ktime(HRTIMER_POLL_INTERVAL_MS));
2045 return HRTIMER_RESTART;
2048 static int m_can_open(struct net_device *dev)
2050 struct m_can_classdev *cdev = netdev_priv(dev);
2053 err = phy_power_on(cdev->transceiver);
2057 err = m_can_clk_start(cdev);
2059 goto out_phy_power_off;
2061 /* open the can device */
2062 err = open_candev(dev);
2064 netdev_err(dev, "failed to open can device\n");
2065 goto exit_disable_clks;
2068 if (cdev->is_peripheral)
2069 can_rx_offload_enable(&cdev->offload);
2071 napi_enable(&cdev->napi);
2073 /* register interrupt handler */
2074 if (cdev->is_peripheral) {
2075 cdev->tx_wq = alloc_ordered_workqueue("mcan_wq",
2076 WQ_FREEZABLE | WQ_MEM_RECLAIM);
2082 for (int i = 0; i != cdev->tx_fifo_size; ++i) {
2083 cdev->tx_ops[i].cdev = cdev;
2084 INIT_WORK(&cdev->tx_ops[i].work, m_can_tx_work_queue);
2087 err = request_threaded_irq(dev->irq, NULL, m_can_isr,
2090 } else if (dev->irq) {
2091 err = request_irq(dev->irq, m_can_isr, IRQF_SHARED, dev->name,
2096 netdev_err(dev, "failed to request interrupt\n");
2100 /* start the m_can controller */
2101 err = m_can_start(dev);
2103 goto exit_start_fail;
2105 netif_start_queue(dev);
2110 if (cdev->is_peripheral || dev->irq)
2111 free_irq(dev->irq, dev);
2113 if (cdev->is_peripheral)
2114 destroy_workqueue(cdev->tx_wq);
2116 if (cdev->is_peripheral)
2117 can_rx_offload_disable(&cdev->offload);
2119 napi_disable(&cdev->napi);
2122 m_can_clk_stop(cdev);
2124 phy_power_off(cdev->transceiver);
2128 static const struct net_device_ops m_can_netdev_ops = {
2129 .ndo_open = m_can_open,
2130 .ndo_stop = m_can_close,
2131 .ndo_start_xmit = m_can_start_xmit,
2132 .ndo_change_mtu = can_change_mtu,
2135 static int m_can_get_coalesce(struct net_device *dev,
2136 struct ethtool_coalesce *ec,
2137 struct kernel_ethtool_coalesce *kec,
2138 struct netlink_ext_ack *ext_ack)
2140 struct m_can_classdev *cdev = netdev_priv(dev);
2142 ec->rx_max_coalesced_frames_irq = cdev->rx_max_coalesced_frames_irq;
2143 ec->rx_coalesce_usecs_irq = cdev->rx_coalesce_usecs_irq;
2144 ec->tx_max_coalesced_frames = cdev->tx_max_coalesced_frames;
2145 ec->tx_max_coalesced_frames_irq = cdev->tx_max_coalesced_frames_irq;
2146 ec->tx_coalesce_usecs_irq = cdev->tx_coalesce_usecs_irq;
2151 static int m_can_set_coalesce(struct net_device *dev,
2152 struct ethtool_coalesce *ec,
2153 struct kernel_ethtool_coalesce *kec,
2154 struct netlink_ext_ack *ext_ack)
2156 struct m_can_classdev *cdev = netdev_priv(dev);
2158 if (cdev->can.state != CAN_STATE_STOPPED) {
2159 netdev_err(dev, "Device is in use, please shut it down first\n");
2163 if (ec->rx_max_coalesced_frames_irq > cdev->mcfg[MRAM_RXF0].num) {
2164 netdev_err(dev, "rx-frames-irq %u greater than the RX FIFO %u\n",
2165 ec->rx_max_coalesced_frames_irq,
2166 cdev->mcfg[MRAM_RXF0].num);
2169 if ((ec->rx_max_coalesced_frames_irq == 0) != (ec->rx_coalesce_usecs_irq == 0)) {
2170 netdev_err(dev, "rx-frames-irq and rx-usecs-irq can only be set together\n");
2173 if (ec->tx_max_coalesced_frames_irq > cdev->mcfg[MRAM_TXE].num) {
2174 netdev_err(dev, "tx-frames-irq %u greater than the TX event FIFO %u\n",
2175 ec->tx_max_coalesced_frames_irq,
2176 cdev->mcfg[MRAM_TXE].num);
2179 if (ec->tx_max_coalesced_frames_irq > cdev->mcfg[MRAM_TXB].num) {
2180 netdev_err(dev, "tx-frames-irq %u greater than the TX FIFO %u\n",
2181 ec->tx_max_coalesced_frames_irq,
2182 cdev->mcfg[MRAM_TXB].num);
2185 if ((ec->tx_max_coalesced_frames_irq == 0) != (ec->tx_coalesce_usecs_irq == 0)) {
2186 netdev_err(dev, "tx-frames-irq and tx-usecs-irq can only be set together\n");
2189 if (ec->tx_max_coalesced_frames > cdev->mcfg[MRAM_TXE].num) {
2190 netdev_err(dev, "tx-frames %u greater than the TX event FIFO %u\n",
2191 ec->tx_max_coalesced_frames,
2192 cdev->mcfg[MRAM_TXE].num);
2195 if (ec->tx_max_coalesced_frames > cdev->mcfg[MRAM_TXB].num) {
2196 netdev_err(dev, "tx-frames %u greater than the TX FIFO %u\n",
2197 ec->tx_max_coalesced_frames,
2198 cdev->mcfg[MRAM_TXB].num);
2201 if (ec->rx_coalesce_usecs_irq != 0 && ec->tx_coalesce_usecs_irq != 0 &&
2202 ec->rx_coalesce_usecs_irq != ec->tx_coalesce_usecs_irq) {
2203 netdev_err(dev, "rx-usecs-irq %u needs to be equal to tx-usecs-irq %u if both are enabled\n",
2204 ec->rx_coalesce_usecs_irq,
2205 ec->tx_coalesce_usecs_irq);
2209 cdev->rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
2210 cdev->rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
2211 cdev->tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
2212 cdev->tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
2213 cdev->tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
2215 if (cdev->rx_coalesce_usecs_irq)
2216 cdev->irq_timer_wait =
2217 ns_to_ktime(cdev->rx_coalesce_usecs_irq * NSEC_PER_USEC);
2219 cdev->irq_timer_wait =
2220 ns_to_ktime(cdev->tx_coalesce_usecs_irq * NSEC_PER_USEC);
2225 static const struct ethtool_ops m_can_ethtool_ops_coalescing = {
2226 .supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS_IRQ |
2227 ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ |
2228 ETHTOOL_COALESCE_TX_USECS_IRQ |
2229 ETHTOOL_COALESCE_TX_MAX_FRAMES |
2230 ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ,
2231 .get_ts_info = ethtool_op_get_ts_info,
2232 .get_coalesce = m_can_get_coalesce,
2233 .set_coalesce = m_can_set_coalesce,
2236 static const struct ethtool_ops m_can_ethtool_ops = {
2237 .get_ts_info = ethtool_op_get_ts_info,
2240 static int register_m_can_dev(struct m_can_classdev *cdev)
2242 struct net_device *dev = cdev->net;
2244 dev->flags |= IFF_ECHO; /* we support local echo */
2245 dev->netdev_ops = &m_can_netdev_ops;
2246 if (dev->irq && cdev->is_peripheral)
2247 dev->ethtool_ops = &m_can_ethtool_ops_coalescing;
2249 dev->ethtool_ops = &m_can_ethtool_ops;
2251 return register_candev(dev);
2254 int m_can_check_mram_cfg(struct m_can_classdev *cdev, u32 mram_max_size)
2258 total_size = cdev->mcfg[MRAM_TXB].off - cdev->mcfg[MRAM_SIDF].off +
2259 cdev->mcfg[MRAM_TXB].num * TXB_ELEMENT_SIZE;
2260 if (total_size > mram_max_size) {
2261 dev_err(cdev->dev, "Total size of mram config(%u) exceeds mram(%u)\n",
2262 total_size, mram_max_size);
2268 EXPORT_SYMBOL_GPL(m_can_check_mram_cfg);
2270 static void m_can_of_parse_mram(struct m_can_classdev *cdev,
2271 const u32 *mram_config_vals)
2273 cdev->mcfg[MRAM_SIDF].off = mram_config_vals[0];
2274 cdev->mcfg[MRAM_SIDF].num = mram_config_vals[1];
2275 cdev->mcfg[MRAM_XIDF].off = cdev->mcfg[MRAM_SIDF].off +
2276 cdev->mcfg[MRAM_SIDF].num * SIDF_ELEMENT_SIZE;
2277 cdev->mcfg[MRAM_XIDF].num = mram_config_vals[2];
2278 cdev->mcfg[MRAM_RXF0].off = cdev->mcfg[MRAM_XIDF].off +
2279 cdev->mcfg[MRAM_XIDF].num * XIDF_ELEMENT_SIZE;
2280 cdev->mcfg[MRAM_RXF0].num = mram_config_vals[3] &
2281 FIELD_MAX(RXFC_FS_MASK);
2282 cdev->mcfg[MRAM_RXF1].off = cdev->mcfg[MRAM_RXF0].off +
2283 cdev->mcfg[MRAM_RXF0].num * RXF0_ELEMENT_SIZE;
2284 cdev->mcfg[MRAM_RXF1].num = mram_config_vals[4] &
2285 FIELD_MAX(RXFC_FS_MASK);
2286 cdev->mcfg[MRAM_RXB].off = cdev->mcfg[MRAM_RXF1].off +
2287 cdev->mcfg[MRAM_RXF1].num * RXF1_ELEMENT_SIZE;
2288 cdev->mcfg[MRAM_RXB].num = mram_config_vals[5];
2289 cdev->mcfg[MRAM_TXE].off = cdev->mcfg[MRAM_RXB].off +
2290 cdev->mcfg[MRAM_RXB].num * RXB_ELEMENT_SIZE;
2291 cdev->mcfg[MRAM_TXE].num = mram_config_vals[6];
2292 cdev->mcfg[MRAM_TXB].off = cdev->mcfg[MRAM_TXE].off +
2293 cdev->mcfg[MRAM_TXE].num * TXE_ELEMENT_SIZE;
2294 cdev->mcfg[MRAM_TXB].num = mram_config_vals[7] &
2295 FIELD_MAX(TXBC_NDTB_MASK);
2298 "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",
2299 cdev->mcfg[MRAM_SIDF].off, cdev->mcfg[MRAM_SIDF].num,
2300 cdev->mcfg[MRAM_XIDF].off, cdev->mcfg[MRAM_XIDF].num,
2301 cdev->mcfg[MRAM_RXF0].off, cdev->mcfg[MRAM_RXF0].num,
2302 cdev->mcfg[MRAM_RXF1].off, cdev->mcfg[MRAM_RXF1].num,
2303 cdev->mcfg[MRAM_RXB].off, cdev->mcfg[MRAM_RXB].num,
2304 cdev->mcfg[MRAM_TXE].off, cdev->mcfg[MRAM_TXE].num,
2305 cdev->mcfg[MRAM_TXB].off, cdev->mcfg[MRAM_TXB].num);
2308 int m_can_init_ram(struct m_can_classdev *cdev)
2313 /* initialize the entire Message RAM in use to avoid possible
2314 * ECC/parity checksum errors when reading an uninitialized buffer
2316 start = cdev->mcfg[MRAM_SIDF].off;
2317 end = cdev->mcfg[MRAM_TXB].off +
2318 cdev->mcfg[MRAM_TXB].num * TXB_ELEMENT_SIZE;
2320 for (i = start; i < end; i += 4) {
2321 err = m_can_fifo_write_no_off(cdev, i, 0x0);
2328 EXPORT_SYMBOL_GPL(m_can_init_ram);
2330 int m_can_class_get_clocks(struct m_can_classdev *cdev)
2334 cdev->hclk = devm_clk_get(cdev->dev, "hclk");
2335 cdev->cclk = devm_clk_get(cdev->dev, "cclk");
2337 if (IS_ERR(cdev->hclk) || IS_ERR(cdev->cclk)) {
2338 dev_err(cdev->dev, "no clock found\n");
2344 EXPORT_SYMBOL_GPL(m_can_class_get_clocks);
2346 struct m_can_classdev *m_can_class_allocate_dev(struct device *dev,
2349 struct m_can_classdev *class_dev = NULL;
2350 u32 mram_config_vals[MRAM_CFG_LEN];
2351 struct net_device *net_dev;
2355 ret = fwnode_property_read_u32_array(dev_fwnode(dev),
2358 sizeof(mram_config_vals) / 4);
2360 dev_err(dev, "Could not get Message RAM configuration.");
2365 * Defines the total amount of echo buffers for loopback
2367 tx_fifo_size = mram_config_vals[7];
2369 /* allocate the m_can device */
2370 net_dev = alloc_candev(sizeof_priv, tx_fifo_size);
2372 dev_err(dev, "Failed to allocate CAN device");
2376 class_dev = netdev_priv(net_dev);
2377 class_dev->net = net_dev;
2378 class_dev->dev = dev;
2379 SET_NETDEV_DEV(net_dev, dev);
2381 m_can_of_parse_mram(class_dev, mram_config_vals);
2385 EXPORT_SYMBOL_GPL(m_can_class_allocate_dev);
2387 void m_can_class_free_dev(struct net_device *net)
2391 EXPORT_SYMBOL_GPL(m_can_class_free_dev);
2393 int m_can_class_register(struct m_can_classdev *cdev)
2397 cdev->tx_fifo_size = max(1, min(cdev->mcfg[MRAM_TXB].num,
2398 cdev->mcfg[MRAM_TXE].num));
2399 if (cdev->is_peripheral) {
2401 devm_kzalloc(cdev->dev,
2402 cdev->tx_fifo_size * sizeof(*cdev->tx_ops),
2404 if (!cdev->tx_ops) {
2405 dev_err(cdev->dev, "Failed to allocate tx_ops for workqueue\n");
2410 ret = m_can_clk_start(cdev);
2414 if (cdev->is_peripheral) {
2415 ret = can_rx_offload_add_manual(cdev->net, &cdev->offload,
2421 if (!cdev->net->irq) {
2422 dev_dbg(cdev->dev, "Polling enabled, initialize hrtimer");
2423 hrtimer_init(&cdev->hrtimer, CLOCK_MONOTONIC,
2424 HRTIMER_MODE_REL_PINNED);
2425 cdev->hrtimer.function = &hrtimer_callback;
2427 hrtimer_init(&cdev->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2428 cdev->hrtimer.function = m_can_coalescing_timer;
2431 ret = m_can_dev_setup(cdev);
2433 goto rx_offload_del;
2435 ret = register_m_can_dev(cdev);
2437 dev_err(cdev->dev, "registering %s failed (err=%d)\n",
2438 cdev->net->name, ret);
2439 goto rx_offload_del;
2442 of_can_transceiver(cdev->net);
2444 dev_info(cdev->dev, "%s device registered (irq=%d, version=%d)\n",
2445 KBUILD_MODNAME, cdev->net->irq, cdev->version);
2448 * Stop clocks. They will be reactivated once the M_CAN device is opened
2450 m_can_clk_stop(cdev);
2455 if (cdev->is_peripheral)
2456 can_rx_offload_del(&cdev->offload);
2458 m_can_clk_stop(cdev);
2462 EXPORT_SYMBOL_GPL(m_can_class_register);
2464 void m_can_class_unregister(struct m_can_classdev *cdev)
2466 if (cdev->is_peripheral)
2467 can_rx_offload_del(&cdev->offload);
2468 unregister_candev(cdev->net);
2470 EXPORT_SYMBOL_GPL(m_can_class_unregister);
2472 int m_can_class_suspend(struct device *dev)
2474 struct m_can_classdev *cdev = dev_get_drvdata(dev);
2475 struct net_device *ndev = cdev->net;
2478 if (netif_running(ndev)) {
2479 netif_stop_queue(ndev);
2480 netif_device_detach(ndev);
2482 /* leave the chip running with rx interrupt enabled if it is
2483 * used as a wake-up source. Coalescing needs to be reset then,
2484 * the timer is cancelled here, interrupts are done in resume.
2486 if (cdev->pm_wake_source) {
2487 hrtimer_cancel(&cdev->hrtimer);
2488 m_can_write(cdev, M_CAN_IE, IR_RF0N);
2490 if (cdev->ops->deinit)
2491 ret = cdev->ops->deinit(cdev);
2496 m_can_clk_stop(cdev);
2499 pinctrl_pm_select_sleep_state(dev);
2501 cdev->can.state = CAN_STATE_SLEEPING;
2505 EXPORT_SYMBOL_GPL(m_can_class_suspend);
2507 int m_can_class_resume(struct device *dev)
2509 struct m_can_classdev *cdev = dev_get_drvdata(dev);
2510 struct net_device *ndev = cdev->net;
2513 pinctrl_pm_select_default_state(dev);
2515 cdev->can.state = CAN_STATE_ERROR_ACTIVE;
2517 if (netif_running(ndev)) {
2518 ret = m_can_clk_start(cdev);
2522 if (cdev->pm_wake_source) {
2523 /* Restore active interrupts but disable coalescing as
2524 * we may have missed important waterlevel interrupts
2525 * between suspend and resume. Timers are already
2526 * stopped in suspend. Here we enable all interrupts
2529 cdev->active_interrupts |= IR_RF0N | IR_TEFN;
2531 if (cdev->ops->init)
2532 ret = cdev->ops->init(cdev);
2534 m_can_write(cdev, M_CAN_IE, cdev->active_interrupts);
2536 ret = m_can_start(ndev);
2538 m_can_clk_stop(cdev);
2543 netif_device_attach(ndev);
2544 netif_start_queue(ndev);
2549 EXPORT_SYMBOL_GPL(m_can_class_resume);
2553 MODULE_LICENSE("GPL v2");
2554 MODULE_DESCRIPTION("CAN bus driver for Bosch M_CAN controller");