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);
486 cdev->active_interrupts = 0x0;
488 if (!cdev->net->irq) {
489 dev_dbg(cdev->dev, "Stop hrtimer\n");
490 hrtimer_cancel(&cdev->hrtimer);
494 /* Retrieve internal timestamp counter from TSCV.TSC, and shift it to 32-bit
497 static u32 m_can_get_timestamp(struct m_can_classdev *cdev)
502 tscv = m_can_read(cdev, M_CAN_TSCV);
503 tsc = FIELD_GET(TSCV_TSC_MASK, tscv);
508 static void m_can_clean(struct net_device *net)
510 struct m_can_classdev *cdev = netdev_priv(net);
511 unsigned long irqflags;
514 for (int i = 0; i != cdev->tx_fifo_size; ++i) {
515 if (!cdev->tx_ops[i].skb)
518 net->stats.tx_errors++;
519 cdev->tx_ops[i].skb = NULL;
523 for (int i = 0; i != cdev->can.echo_skb_max; ++i)
524 can_free_echo_skb(cdev->net, i, NULL);
526 netdev_reset_queue(cdev->net);
528 spin_lock_irqsave(&cdev->tx_handling_spinlock, irqflags);
529 cdev->tx_fifo_in_flight = 0;
530 spin_unlock_irqrestore(&cdev->tx_handling_spinlock, irqflags);
533 /* For peripherals, pass skb to rx-offload, which will push skb from
534 * napi. For non-peripherals, RX is done in napi already, so push
535 * directly. timestamp is used to ensure good skb ordering in
536 * rx-offload and is ignored for non-peripherals.
538 static void m_can_receive_skb(struct m_can_classdev *cdev,
542 if (cdev->is_peripheral) {
543 struct net_device_stats *stats = &cdev->net->stats;
546 err = can_rx_offload_queue_timestamp(&cdev->offload, skb,
549 stats->rx_fifo_errors++;
551 netif_receive_skb(skb);
555 static int m_can_read_fifo(struct net_device *dev, u32 fgi)
557 struct net_device_stats *stats = &dev->stats;
558 struct m_can_classdev *cdev = netdev_priv(dev);
559 struct canfd_frame *cf;
561 struct id_and_dlc fifo_header;
565 err = m_can_fifo_read(cdev, fgi, M_CAN_FIFO_ID, &fifo_header, 2);
569 if (fifo_header.dlc & RX_BUF_FDF)
570 skb = alloc_canfd_skb(dev, &cf);
572 skb = alloc_can_skb(dev, (struct can_frame **)&cf);
578 if (fifo_header.dlc & RX_BUF_FDF)
579 cf->len = can_fd_dlc2len((fifo_header.dlc >> 16) & 0x0F);
581 cf->len = can_cc_dlc2len((fifo_header.dlc >> 16) & 0x0F);
583 if (fifo_header.id & RX_BUF_XTD)
584 cf->can_id = (fifo_header.id & CAN_EFF_MASK) | CAN_EFF_FLAG;
586 cf->can_id = (fifo_header.id >> 18) & CAN_SFF_MASK;
588 if (fifo_header.id & RX_BUF_ESI) {
589 cf->flags |= CANFD_ESI;
590 netdev_dbg(dev, "ESI Error\n");
593 if (!(fifo_header.dlc & RX_BUF_FDF) && (fifo_header.id & RX_BUF_RTR)) {
594 cf->can_id |= CAN_RTR_FLAG;
596 if (fifo_header.dlc & RX_BUF_BRS)
597 cf->flags |= CANFD_BRS;
599 err = m_can_fifo_read(cdev, fgi, M_CAN_FIFO_DATA,
600 cf->data, DIV_ROUND_UP(cf->len, 4));
604 stats->rx_bytes += cf->len;
608 timestamp = FIELD_GET(RX_BUF_RXTS_MASK, fifo_header.dlc) << 16;
610 m_can_receive_skb(cdev, skb, timestamp);
617 netdev_err(dev, "FIFO read returned %d\n", err);
621 static int m_can_do_rx_poll(struct net_device *dev, int quota)
623 struct m_can_classdev *cdev = netdev_priv(dev);
632 rxfs = m_can_read(cdev, M_CAN_RXF0S);
633 if (!(rxfs & RXFS_FFL_MASK)) {
634 netdev_dbg(dev, "no messages in fifo0\n");
638 rx_count = FIELD_GET(RXFS_FFL_MASK, rxfs);
639 fgi = FIELD_GET(RXFS_FGI_MASK, rxfs);
641 for (i = 0; i < rx_count && quota > 0; ++i) {
642 err = m_can_read_fifo(dev, fgi);
649 fgi = (++fgi >= cdev->mcfg[MRAM_RXF0].num ? 0 : fgi);
653 m_can_write(cdev, M_CAN_RXF0A, ack_fgi);
661 static int m_can_handle_lost_msg(struct net_device *dev)
663 struct m_can_classdev *cdev = netdev_priv(dev);
664 struct net_device_stats *stats = &dev->stats;
666 struct can_frame *frame;
669 netdev_err(dev, "msg lost in rxf0\n");
672 stats->rx_over_errors++;
674 skb = alloc_can_err_skb(dev, &frame);
678 frame->can_id |= CAN_ERR_CRTL;
679 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
681 if (cdev->is_peripheral)
682 timestamp = m_can_get_timestamp(cdev);
684 m_can_receive_skb(cdev, skb, timestamp);
689 static int m_can_handle_lec_err(struct net_device *dev,
690 enum m_can_lec_type lec_type)
692 struct m_can_classdev *cdev = netdev_priv(dev);
693 struct net_device_stats *stats = &dev->stats;
694 struct can_frame *cf;
698 cdev->can.can_stats.bus_error++;
701 /* propagate the error condition to the CAN stack */
702 skb = alloc_can_err_skb(dev, &cf);
706 /* check for 'last error code' which tells us the
707 * type of the last error to occur on the CAN bus
709 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
712 case LEC_STUFF_ERROR:
713 netdev_dbg(dev, "stuff error\n");
714 cf->data[2] |= CAN_ERR_PROT_STUFF;
717 netdev_dbg(dev, "form error\n");
718 cf->data[2] |= CAN_ERR_PROT_FORM;
721 netdev_dbg(dev, "ack error\n");
722 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
725 netdev_dbg(dev, "bit1 error\n");
726 cf->data[2] |= CAN_ERR_PROT_BIT1;
729 netdev_dbg(dev, "bit0 error\n");
730 cf->data[2] |= CAN_ERR_PROT_BIT0;
733 netdev_dbg(dev, "CRC error\n");
734 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
740 if (cdev->is_peripheral)
741 timestamp = m_can_get_timestamp(cdev);
743 m_can_receive_skb(cdev, skb, timestamp);
748 static int __m_can_get_berr_counter(const struct net_device *dev,
749 struct can_berr_counter *bec)
751 struct m_can_classdev *cdev = netdev_priv(dev);
754 ecr = m_can_read(cdev, M_CAN_ECR);
755 bec->rxerr = FIELD_GET(ECR_REC_MASK, ecr);
756 bec->txerr = FIELD_GET(ECR_TEC_MASK, ecr);
761 static int m_can_clk_start(struct m_can_classdev *cdev)
763 if (cdev->pm_clock_support == 0)
766 return pm_runtime_resume_and_get(cdev->dev);
769 static void m_can_clk_stop(struct m_can_classdev *cdev)
771 if (cdev->pm_clock_support)
772 pm_runtime_put_sync(cdev->dev);
775 static int m_can_get_berr_counter(const struct net_device *dev,
776 struct can_berr_counter *bec)
778 struct m_can_classdev *cdev = netdev_priv(dev);
781 err = m_can_clk_start(cdev);
785 __m_can_get_berr_counter(dev, bec);
787 m_can_clk_stop(cdev);
792 static int m_can_handle_state_change(struct net_device *dev,
793 enum can_state new_state)
795 struct m_can_classdev *cdev = netdev_priv(dev);
796 struct can_frame *cf;
798 struct can_berr_counter bec;
803 case CAN_STATE_ERROR_WARNING:
804 /* error warning state */
805 cdev->can.can_stats.error_warning++;
806 cdev->can.state = CAN_STATE_ERROR_WARNING;
808 case CAN_STATE_ERROR_PASSIVE:
809 /* error passive state */
810 cdev->can.can_stats.error_passive++;
811 cdev->can.state = CAN_STATE_ERROR_PASSIVE;
813 case CAN_STATE_BUS_OFF:
815 cdev->can.state = CAN_STATE_BUS_OFF;
816 m_can_disable_all_interrupts(cdev);
817 cdev->can.can_stats.bus_off++;
824 /* propagate the error condition to the CAN stack */
825 skb = alloc_can_err_skb(dev, &cf);
829 __m_can_get_berr_counter(dev, &bec);
832 case CAN_STATE_ERROR_WARNING:
833 /* error warning state */
834 cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
835 cf->data[1] = (bec.txerr > bec.rxerr) ?
836 CAN_ERR_CRTL_TX_WARNING :
837 CAN_ERR_CRTL_RX_WARNING;
838 cf->data[6] = bec.txerr;
839 cf->data[7] = bec.rxerr;
841 case CAN_STATE_ERROR_PASSIVE:
842 /* error passive state */
843 cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
844 ecr = m_can_read(cdev, M_CAN_ECR);
846 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
848 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
849 cf->data[6] = bec.txerr;
850 cf->data[7] = bec.rxerr;
852 case CAN_STATE_BUS_OFF:
854 cf->can_id |= CAN_ERR_BUSOFF;
860 if (cdev->is_peripheral)
861 timestamp = m_can_get_timestamp(cdev);
863 m_can_receive_skb(cdev, skb, timestamp);
868 static int m_can_handle_state_errors(struct net_device *dev, u32 psr)
870 struct m_can_classdev *cdev = netdev_priv(dev);
873 if (psr & PSR_EW && cdev->can.state != CAN_STATE_ERROR_WARNING) {
874 netdev_dbg(dev, "entered error warning state\n");
875 work_done += m_can_handle_state_change(dev,
876 CAN_STATE_ERROR_WARNING);
879 if (psr & PSR_EP && cdev->can.state != CAN_STATE_ERROR_PASSIVE) {
880 netdev_dbg(dev, "entered error passive state\n");
881 work_done += m_can_handle_state_change(dev,
882 CAN_STATE_ERROR_PASSIVE);
885 if (psr & PSR_BO && cdev->can.state != CAN_STATE_BUS_OFF) {
886 netdev_dbg(dev, "entered error bus off state\n");
887 work_done += m_can_handle_state_change(dev,
894 static void m_can_handle_other_err(struct net_device *dev, u32 irqstatus)
896 if (irqstatus & IR_WDI)
897 netdev_err(dev, "Message RAM Watchdog event due to missing READY\n");
898 if (irqstatus & IR_BEU)
899 netdev_err(dev, "Bit Error Uncorrected\n");
900 if (irqstatus & IR_BEC)
901 netdev_err(dev, "Bit Error Corrected\n");
902 if (irqstatus & IR_TOO)
903 netdev_err(dev, "Timeout reached\n");
904 if (irqstatus & IR_MRAF)
905 netdev_err(dev, "Message RAM access failure occurred\n");
908 static inline bool is_lec_err(u8 lec)
910 return lec != LEC_NO_ERROR && lec != LEC_NO_CHANGE;
913 static inline bool m_can_is_protocol_err(u32 irqstatus)
915 return irqstatus & IR_ERR_LEC_31X;
918 static int m_can_handle_protocol_error(struct net_device *dev, u32 irqstatus)
920 struct net_device_stats *stats = &dev->stats;
921 struct m_can_classdev *cdev = netdev_priv(dev);
922 struct can_frame *cf;
926 /* propagate the error condition to the CAN stack */
927 skb = alloc_can_err_skb(dev, &cf);
929 /* update tx error stats since there is protocol error */
932 /* update arbitration lost status */
933 if (cdev->version >= 31 && (irqstatus & IR_PEA)) {
934 netdev_dbg(dev, "Protocol error in Arbitration fail\n");
935 cdev->can.can_stats.arbitration_lost++;
937 cf->can_id |= CAN_ERR_LOSTARB;
938 cf->data[0] |= CAN_ERR_LOSTARB_UNSPEC;
942 if (unlikely(!skb)) {
943 netdev_dbg(dev, "allocation of skb failed\n");
947 if (cdev->is_peripheral)
948 timestamp = m_can_get_timestamp(cdev);
950 m_can_receive_skb(cdev, skb, timestamp);
955 static int m_can_handle_bus_errors(struct net_device *dev, u32 irqstatus,
958 struct m_can_classdev *cdev = netdev_priv(dev);
961 if (irqstatus & IR_RF0L)
962 work_done += m_can_handle_lost_msg(dev);
964 /* handle lec errors on the bus */
965 if (cdev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) {
966 u8 lec = FIELD_GET(PSR_LEC_MASK, psr);
967 u8 dlec = FIELD_GET(PSR_DLEC_MASK, psr);
969 if (is_lec_err(lec)) {
970 netdev_dbg(dev, "Arbitration phase error detected\n");
971 work_done += m_can_handle_lec_err(dev, lec);
974 if (is_lec_err(dlec)) {
975 netdev_dbg(dev, "Data phase error detected\n");
976 work_done += m_can_handle_lec_err(dev, dlec);
980 /* handle protocol errors in arbitration phase */
981 if ((cdev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
982 m_can_is_protocol_err(irqstatus))
983 work_done += m_can_handle_protocol_error(dev, irqstatus);
985 /* other unproccessed error interrupts */
986 m_can_handle_other_err(dev, irqstatus);
991 static int m_can_rx_handler(struct net_device *dev, int quota, u32 irqstatus)
993 struct m_can_classdev *cdev = netdev_priv(dev);
1000 /* Errata workaround for issue "Needless activation of MRAF irq"
1001 * During frame reception while the MCAN is in Error Passive state
1002 * and the Receive Error Counter has the value MCAN_ECR.REC = 127,
1003 * it may happen that MCAN_IR.MRAF is set although there was no
1004 * Message RAM access failure.
1005 * If MCAN_IR.MRAF is enabled, an interrupt to the Host CPU is generated
1006 * The Message RAM Access Failure interrupt routine needs to check
1007 * whether MCAN_ECR.RP = ’1’ and MCAN_ECR.REC = 127.
1008 * In this case, reset MCAN_IR.MRAF. No further action is required.
1010 if (cdev->version <= 31 && irqstatus & IR_MRAF &&
1011 m_can_read(cdev, M_CAN_ECR) & ECR_RP) {
1012 struct can_berr_counter bec;
1014 __m_can_get_berr_counter(dev, &bec);
1015 if (bec.rxerr == 127) {
1016 m_can_write(cdev, M_CAN_IR, IR_MRAF);
1017 irqstatus &= ~IR_MRAF;
1021 if (irqstatus & IR_ERR_STATE)
1022 work_done += m_can_handle_state_errors(dev,
1023 m_can_read(cdev, M_CAN_PSR));
1025 if (irqstatus & IR_ERR_BUS_30X)
1026 work_done += m_can_handle_bus_errors(dev, irqstatus,
1027 m_can_read(cdev, M_CAN_PSR));
1029 if (irqstatus & IR_RF0N) {
1030 rx_work_or_err = m_can_do_rx_poll(dev, (quota - work_done));
1031 if (rx_work_or_err < 0)
1032 return rx_work_or_err;
1034 work_done += rx_work_or_err;
1040 static int m_can_rx_peripheral(struct net_device *dev, u32 irqstatus)
1042 struct m_can_classdev *cdev = netdev_priv(dev);
1045 work_done = m_can_rx_handler(dev, NAPI_POLL_WEIGHT, irqstatus);
1047 /* Don't re-enable interrupts if the driver had a fatal error
1048 * (e.g., FIFO read failure).
1051 m_can_disable_all_interrupts(cdev);
1056 static int m_can_poll(struct napi_struct *napi, int quota)
1058 struct net_device *dev = napi->dev;
1059 struct m_can_classdev *cdev = netdev_priv(dev);
1063 irqstatus = cdev->irqstatus | m_can_read(cdev, M_CAN_IR);
1065 work_done = m_can_rx_handler(dev, quota, irqstatus);
1067 /* Don't re-enable interrupts if the driver had a fatal error
1068 * (e.g., FIFO read failure).
1070 if (work_done >= 0 && work_done < quota) {
1071 napi_complete_done(napi, work_done);
1072 m_can_enable_all_interrupts(cdev);
1078 /* Echo tx skb and update net stats. Peripherals use rx-offload for
1079 * echo. timestamp is used for peripherals to ensure correct ordering
1080 * by rx-offload, and is ignored for non-peripherals.
1082 static unsigned int m_can_tx_update_stats(struct m_can_classdev *cdev,
1083 unsigned int msg_mark, u32 timestamp)
1085 struct net_device *dev = cdev->net;
1086 struct net_device_stats *stats = &dev->stats;
1087 unsigned int frame_len;
1089 if (cdev->is_peripheral)
1091 can_rx_offload_get_echo_skb_queue_timestamp(&cdev->offload,
1096 stats->tx_bytes += can_get_echo_skb(dev, msg_mark, &frame_len);
1098 stats->tx_packets++;
1103 static void m_can_finish_tx(struct m_can_classdev *cdev, int transmitted,
1104 unsigned int transmitted_frame_len)
1106 unsigned long irqflags;
1108 netdev_completed_queue(cdev->net, transmitted, transmitted_frame_len);
1110 spin_lock_irqsave(&cdev->tx_handling_spinlock, irqflags);
1111 if (cdev->tx_fifo_in_flight >= cdev->tx_fifo_size && transmitted > 0)
1112 netif_wake_queue(cdev->net);
1113 cdev->tx_fifo_in_flight -= transmitted;
1114 spin_unlock_irqrestore(&cdev->tx_handling_spinlock, irqflags);
1117 static netdev_tx_t m_can_start_tx(struct m_can_classdev *cdev)
1119 unsigned long irqflags;
1120 int tx_fifo_in_flight;
1122 spin_lock_irqsave(&cdev->tx_handling_spinlock, irqflags);
1123 tx_fifo_in_flight = cdev->tx_fifo_in_flight + 1;
1124 if (tx_fifo_in_flight >= cdev->tx_fifo_size) {
1125 netif_stop_queue(cdev->net);
1126 if (tx_fifo_in_flight > cdev->tx_fifo_size) {
1127 netdev_err_once(cdev->net, "hard_xmit called while TX FIFO full\n");
1128 spin_unlock_irqrestore(&cdev->tx_handling_spinlock, irqflags);
1129 return NETDEV_TX_BUSY;
1132 cdev->tx_fifo_in_flight = tx_fifo_in_flight;
1133 spin_unlock_irqrestore(&cdev->tx_handling_spinlock, irqflags);
1135 return NETDEV_TX_OK;
1138 static int m_can_echo_tx_event(struct net_device *dev)
1146 unsigned int msg_mark;
1148 unsigned int processed_frame_len = 0;
1150 struct m_can_classdev *cdev = netdev_priv(dev);
1152 /* read tx event fifo status */
1153 m_can_txefs = m_can_read(cdev, M_CAN_TXEFS);
1155 /* Get Tx Event fifo element count */
1156 txe_count = FIELD_GET(TXEFS_EFFL_MASK, m_can_txefs);
1157 fgi = FIELD_GET(TXEFS_EFGI_MASK, m_can_txefs);
1159 /* Get and process all sent elements */
1160 for (i = 0; i < txe_count; i++) {
1161 u32 txe, timestamp = 0;
1163 /* get message marker, timestamp */
1164 err = m_can_txe_fifo_read(cdev, fgi, 4, &txe);
1166 netdev_err(dev, "TXE FIFO read returned %d\n", err);
1170 msg_mark = FIELD_GET(TX_EVENT_MM_MASK, txe);
1171 timestamp = FIELD_GET(TX_EVENT_TXTS_MASK, txe) << 16;
1174 fgi = (++fgi >= cdev->mcfg[MRAM_TXE].num ? 0 : fgi);
1177 processed_frame_len += m_can_tx_update_stats(cdev, msg_mark,
1184 m_can_write(cdev, M_CAN_TXEFA, FIELD_PREP(TXEFA_EFAI_MASK,
1187 m_can_finish_tx(cdev, processed, processed_frame_len);
1192 static void m_can_coalescing_update(struct m_can_classdev *cdev, u32 ir)
1194 u32 new_interrupts = cdev->active_interrupts;
1195 bool enable_rx_timer = false;
1196 bool enable_tx_timer = false;
1198 if (!cdev->net->irq)
1201 if (cdev->rx_coalesce_usecs_irq > 0 && (ir & (IR_RF0N | IR_RF0W))) {
1202 enable_rx_timer = true;
1203 new_interrupts &= ~IR_RF0N;
1205 if (cdev->tx_coalesce_usecs_irq > 0 && (ir & (IR_TEFN | IR_TEFW))) {
1206 enable_tx_timer = true;
1207 new_interrupts &= ~IR_TEFN;
1209 if (!enable_rx_timer && !hrtimer_active(&cdev->hrtimer))
1210 new_interrupts |= IR_RF0N;
1211 if (!enable_tx_timer && !hrtimer_active(&cdev->hrtimer))
1212 new_interrupts |= IR_TEFN;
1214 m_can_interrupt_enable(cdev, new_interrupts);
1215 if (enable_rx_timer | enable_tx_timer)
1216 hrtimer_start(&cdev->hrtimer, cdev->irq_timer_wait,
1220 static irqreturn_t m_can_isr(int irq, void *dev_id)
1222 struct net_device *dev = (struct net_device *)dev_id;
1223 struct m_can_classdev *cdev = netdev_priv(dev);
1226 if (pm_runtime_suspended(cdev->dev)) {
1227 m_can_coalescing_disable(cdev);
1231 ir = m_can_read(cdev, M_CAN_IR);
1232 m_can_coalescing_update(cdev, ir);
1237 m_can_write(cdev, M_CAN_IR, ir);
1239 if (cdev->ops->clear_interrupts)
1240 cdev->ops->clear_interrupts(cdev);
1242 /* schedule NAPI in case of
1244 * - state change IRQ
1245 * - bus error IRQ and bus error reporting
1247 if (ir & (IR_RF0N | IR_RF0W | IR_ERR_ALL_30X)) {
1248 cdev->irqstatus = ir;
1249 if (!cdev->is_peripheral) {
1250 m_can_disable_all_interrupts(cdev);
1251 napi_schedule(&cdev->napi);
1255 pkts = m_can_rx_peripheral(dev, ir);
1261 if (cdev->version == 30) {
1263 /* Transmission Complete Interrupt*/
1265 unsigned int frame_len;
1267 if (cdev->is_peripheral)
1268 timestamp = m_can_get_timestamp(cdev);
1269 frame_len = m_can_tx_update_stats(cdev, 0, timestamp);
1270 m_can_finish_tx(cdev, 1, frame_len);
1273 if (ir & (IR_TEFN | IR_TEFW)) {
1274 /* New TX FIFO Element arrived */
1275 if (m_can_echo_tx_event(dev) != 0)
1280 if (cdev->is_peripheral)
1281 can_rx_offload_threaded_irq_finish(&cdev->offload);
1286 m_can_disable_all_interrupts(cdev);
1290 static enum hrtimer_restart m_can_coalescing_timer(struct hrtimer *timer)
1292 struct m_can_classdev *cdev = container_of(timer, struct m_can_classdev, hrtimer);
1294 irq_wake_thread(cdev->net->irq, cdev->net);
1296 return HRTIMER_NORESTART;
1299 static const struct can_bittiming_const m_can_bittiming_const_30X = {
1300 .name = KBUILD_MODNAME,
1301 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
1303 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
1311 static const struct can_bittiming_const m_can_data_bittiming_const_30X = {
1312 .name = KBUILD_MODNAME,
1313 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
1315 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
1323 static const struct can_bittiming_const m_can_bittiming_const_31X = {
1324 .name = KBUILD_MODNAME,
1325 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
1327 .tseg2_min = 2, /* Time segment 2 = phase_seg2 */
1335 static const struct can_bittiming_const m_can_data_bittiming_const_31X = {
1336 .name = KBUILD_MODNAME,
1337 .tseg1_min = 1, /* Time segment 1 = prop_seg + phase_seg1 */
1339 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
1347 static int m_can_set_bittiming(struct net_device *dev)
1349 struct m_can_classdev *cdev = netdev_priv(dev);
1350 const struct can_bittiming *bt = &cdev->can.bittiming;
1351 const struct can_bittiming *dbt = &cdev->can.data_bittiming;
1352 u16 brp, sjw, tseg1, tseg2;
1357 tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
1358 tseg2 = bt->phase_seg2 - 1;
1359 reg_btp = FIELD_PREP(NBTP_NBRP_MASK, brp) |
1360 FIELD_PREP(NBTP_NSJW_MASK, sjw) |
1361 FIELD_PREP(NBTP_NTSEG1_MASK, tseg1) |
1362 FIELD_PREP(NBTP_NTSEG2_MASK, tseg2);
1363 m_can_write(cdev, M_CAN_NBTP, reg_btp);
1365 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD) {
1369 tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1;
1370 tseg2 = dbt->phase_seg2 - 1;
1372 /* TDC is only needed for bitrates beyond 2.5 MBit/s.
1373 * This is mentioned in the "Bit Time Requirements for CAN FD"
1374 * paper presented at the International CAN Conference 2013
1376 if (dbt->bitrate > 2500000) {
1379 /* Use the same value of secondary sampling point
1380 * as the data sampling point
1382 ssp = dbt->sample_point;
1384 /* Equation based on Bosch's M_CAN User Manual's
1385 * Transmitter Delay Compensation Section
1387 tdco = (cdev->can.clock.freq / 1000) *
1390 /* Max valid TDCO value is 127 */
1392 netdev_warn(dev, "TDCO value of %u is beyond maximum. Using maximum possible value\n",
1397 reg_btp |= DBTP_TDC;
1398 m_can_write(cdev, M_CAN_TDCR,
1399 FIELD_PREP(TDCR_TDCO_MASK, tdco));
1402 reg_btp |= FIELD_PREP(DBTP_DBRP_MASK, brp) |
1403 FIELD_PREP(DBTP_DSJW_MASK, sjw) |
1404 FIELD_PREP(DBTP_DTSEG1_MASK, tseg1) |
1405 FIELD_PREP(DBTP_DTSEG2_MASK, tseg2);
1407 m_can_write(cdev, M_CAN_DBTP, reg_btp);
1413 /* Configure M_CAN chip:
1414 * - set rx buffer/fifo element size
1415 * - configure rx fifo
1416 * - accept non-matching frame into fifo 0
1417 * - configure tx buffer
1418 * - >= v3.1.x: TX FIFO is used
1421 * - configure timestamp generation
1423 static int m_can_chip_config(struct net_device *dev)
1425 struct m_can_classdev *cdev = netdev_priv(dev);
1426 u32 interrupts = IR_ALL_INT;
1430 err = m_can_init_ram(cdev);
1432 dev_err(cdev->dev, "Message RAM configuration failed\n");
1436 /* Disable unused interrupts */
1437 interrupts &= ~(IR_ARA | IR_ELO | IR_DRX | IR_TEFF | IR_TFE | IR_TCF |
1438 IR_HPM | IR_RF1F | IR_RF1W | IR_RF1N | IR_RF0F);
1440 err = m_can_config_enable(cdev);
1444 /* RX Buffer/FIFO Element Size 64 bytes data field */
1445 m_can_write(cdev, M_CAN_RXESC,
1446 FIELD_PREP(RXESC_RBDS_MASK, RXESC_64B) |
1447 FIELD_PREP(RXESC_F1DS_MASK, RXESC_64B) |
1448 FIELD_PREP(RXESC_F0DS_MASK, RXESC_64B));
1450 /* Accept Non-matching Frames Into FIFO 0 */
1451 m_can_write(cdev, M_CAN_GFC, 0x0);
1453 if (cdev->version == 30) {
1454 /* only support one Tx Buffer currently */
1455 m_can_write(cdev, M_CAN_TXBC, FIELD_PREP(TXBC_NDTB_MASK, 1) |
1456 cdev->mcfg[MRAM_TXB].off);
1458 /* TX FIFO is used for newer IP Core versions */
1459 m_can_write(cdev, M_CAN_TXBC,
1460 FIELD_PREP(TXBC_TFQS_MASK,
1461 cdev->mcfg[MRAM_TXB].num) |
1462 cdev->mcfg[MRAM_TXB].off);
1465 /* support 64 bytes payload */
1466 m_can_write(cdev, M_CAN_TXESC,
1467 FIELD_PREP(TXESC_TBDS_MASK, TXESC_TBDS_64B));
1470 if (cdev->version == 30) {
1471 m_can_write(cdev, M_CAN_TXEFC,
1472 FIELD_PREP(TXEFC_EFS_MASK, 1) |
1473 cdev->mcfg[MRAM_TXE].off);
1475 /* Full TX Event FIFO is used */
1476 m_can_write(cdev, M_CAN_TXEFC,
1477 FIELD_PREP(TXEFC_EFWM_MASK,
1478 cdev->tx_max_coalesced_frames_irq) |
1479 FIELD_PREP(TXEFC_EFS_MASK,
1480 cdev->mcfg[MRAM_TXE].num) |
1481 cdev->mcfg[MRAM_TXE].off);
1484 /* rx fifo configuration, blocking mode, fifo size 1 */
1485 m_can_write(cdev, M_CAN_RXF0C,
1486 FIELD_PREP(RXFC_FWM_MASK, cdev->rx_max_coalesced_frames_irq) |
1487 FIELD_PREP(RXFC_FS_MASK, cdev->mcfg[MRAM_RXF0].num) |
1488 cdev->mcfg[MRAM_RXF0].off);
1490 m_can_write(cdev, M_CAN_RXF1C,
1491 FIELD_PREP(RXFC_FS_MASK, cdev->mcfg[MRAM_RXF1].num) |
1492 cdev->mcfg[MRAM_RXF1].off);
1494 cccr = m_can_read(cdev, M_CAN_CCCR);
1495 test = m_can_read(cdev, M_CAN_TEST);
1497 if (cdev->version == 30) {
1500 cccr &= ~(CCCR_TEST | CCCR_MON | CCCR_DAR |
1501 FIELD_PREP(CCCR_CMR_MASK, FIELD_MAX(CCCR_CMR_MASK)) |
1502 FIELD_PREP(CCCR_CME_MASK, FIELD_MAX(CCCR_CME_MASK)));
1504 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD)
1505 cccr |= FIELD_PREP(CCCR_CME_MASK, CCCR_CME_CANFD_BRS);
1508 /* Version 3.1.x or 3.2.x */
1509 cccr &= ~(CCCR_TEST | CCCR_MON | CCCR_BRSE | CCCR_FDOE |
1510 CCCR_NISO | CCCR_DAR);
1512 /* Only 3.2.x has NISO Bit implemented */
1513 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
1516 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD)
1517 cccr |= (CCCR_BRSE | CCCR_FDOE);
1521 if (cdev->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
1522 cccr |= CCCR_TEST | CCCR_MON;
1526 /* Enable Monitoring (all versions) */
1527 if (cdev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1530 /* Disable Auto Retransmission (all versions) */
1531 if (cdev->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
1535 m_can_write(cdev, M_CAN_CCCR, cccr);
1536 m_can_write(cdev, M_CAN_TEST, test);
1538 /* Enable interrupts */
1539 if (!(cdev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) {
1540 if (cdev->version == 30)
1541 interrupts &= ~(IR_ERR_LEC_30X);
1543 interrupts &= ~(IR_ERR_LEC_31X);
1545 m_can_interrupt_enable(cdev, interrupts);
1547 /* route all interrupts to INT0 */
1548 m_can_write(cdev, M_CAN_ILS, ILS_ALL_INT0);
1550 /* set bittiming params */
1551 m_can_set_bittiming(dev);
1553 /* enable internal timestamp generation, with a prescaler of 16. The
1554 * prescaler is applied to the nominal bit timing
1556 m_can_write(cdev, M_CAN_TSCC,
1557 FIELD_PREP(TSCC_TCP_MASK, 0xf) |
1558 FIELD_PREP(TSCC_TSS_MASK, TSCC_TSS_INTERNAL));
1560 err = m_can_config_disable(cdev);
1564 if (cdev->ops->init)
1565 cdev->ops->init(cdev);
1570 static int m_can_start(struct net_device *dev)
1572 struct m_can_classdev *cdev = netdev_priv(dev);
1575 /* basic m_can configuration */
1576 ret = m_can_chip_config(dev);
1580 netdev_queue_set_dql_min_limit(netdev_get_tx_queue(cdev->net, 0),
1581 cdev->tx_max_coalesced_frames);
1583 cdev->can.state = CAN_STATE_ERROR_ACTIVE;
1585 m_can_enable_all_interrupts(cdev);
1587 if (cdev->version > 30)
1588 cdev->tx_fifo_putidx = FIELD_GET(TXFQS_TFQPI_MASK,
1589 m_can_read(cdev, M_CAN_TXFQS));
1591 ret = m_can_cccr_update_bits(cdev, CCCR_INIT, 0);
1593 netdev_err(dev, "failed to enter normal mode\n");
1598 static int m_can_set_mode(struct net_device *dev, enum can_mode mode)
1601 case CAN_MODE_START:
1604 netif_wake_queue(dev);
1613 /* Checks core release number of M_CAN
1614 * returns 0 if an unsupported device is detected
1615 * else it returns the release and step coded as:
1616 * return value = 10 * <release> + 1 * <step>
1618 static int m_can_check_core_release(struct m_can_classdev *cdev)
1625 /* Read Core Release Version and split into version number
1626 * Example: Version 3.2.1 => rel = 3; step = 2; substep = 1;
1628 crel_reg = m_can_read(cdev, M_CAN_CREL);
1629 rel = (u8)FIELD_GET(CREL_REL_MASK, crel_reg);
1630 step = (u8)FIELD_GET(CREL_STEP_MASK, crel_reg);
1633 /* M_CAN v3.x.y: create return value */
1636 /* Unsupported M_CAN version */
1643 /* Selectable Non ISO support only in version 3.2.x
1644 * Return 1 if the bit is writable, 0 if it is not, or negative on error.
1646 static int m_can_niso_supported(struct m_can_classdev *cdev)
1650 ret = m_can_config_enable(cdev);
1654 /* First try to set the NISO bit. */
1655 niso = m_can_cccr_update_bits(cdev, CCCR_NISO, CCCR_NISO);
1657 /* Then clear the it again. */
1658 ret = m_can_cccr_update_bits(cdev, CCCR_NISO, 0);
1660 dev_err(cdev->dev, "failed to revert the NON-ISO bit in CCCR\n");
1664 ret = m_can_config_disable(cdev);
1671 static int m_can_dev_setup(struct m_can_classdev *cdev)
1673 struct net_device *dev = cdev->net;
1674 int m_can_version, err, niso;
1676 m_can_version = m_can_check_core_release(cdev);
1677 /* return if unsupported version */
1678 if (!m_can_version) {
1679 dev_err(cdev->dev, "Unsupported version number: %2d",
1684 if (!cdev->is_peripheral)
1685 netif_napi_add(dev, &cdev->napi, m_can_poll);
1687 /* Shared properties of all M_CAN versions */
1688 cdev->version = m_can_version;
1689 cdev->can.do_set_mode = m_can_set_mode;
1690 cdev->can.do_get_berr_counter = m_can_get_berr_counter;
1692 /* Set M_CAN supported operations */
1693 cdev->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1694 CAN_CTRLMODE_LISTENONLY |
1695 CAN_CTRLMODE_BERR_REPORTING |
1697 CAN_CTRLMODE_ONE_SHOT;
1699 /* Set properties depending on M_CAN version */
1700 switch (cdev->version) {
1702 /* CAN_CTRLMODE_FD_NON_ISO is fixed with M_CAN IP v3.0.x */
1703 err = can_set_static_ctrlmode(dev, CAN_CTRLMODE_FD_NON_ISO);
1706 cdev->can.bittiming_const = &m_can_bittiming_const_30X;
1707 cdev->can.data_bittiming_const = &m_can_data_bittiming_const_30X;
1710 /* CAN_CTRLMODE_FD_NON_ISO is fixed with M_CAN IP v3.1.x */
1711 err = can_set_static_ctrlmode(dev, CAN_CTRLMODE_FD_NON_ISO);
1714 cdev->can.bittiming_const = &m_can_bittiming_const_31X;
1715 cdev->can.data_bittiming_const = &m_can_data_bittiming_const_31X;
1719 /* Support both MCAN version v3.2.x and v3.3.0 */
1720 cdev->can.bittiming_const = &m_can_bittiming_const_31X;
1721 cdev->can.data_bittiming_const = &m_can_data_bittiming_const_31X;
1723 niso = m_can_niso_supported(cdev);
1727 cdev->can.ctrlmode_supported |= CAN_CTRLMODE_FD_NON_ISO;
1730 dev_err(cdev->dev, "Unsupported version number: %2d",
1735 /* Forcing standby mode should be redundant, as the chip should be in
1736 * standby after a reset. Write the INIT bit anyways, should the chip
1737 * be configured by previous stage.
1739 return m_can_cccr_update_bits(cdev, CCCR_INIT, CCCR_INIT);
1742 static void m_can_stop(struct net_device *dev)
1744 struct m_can_classdev *cdev = netdev_priv(dev);
1747 /* disable all interrupts */
1748 m_can_disable_all_interrupts(cdev);
1750 /* Set init mode to disengage from the network */
1751 ret = m_can_cccr_update_bits(cdev, CCCR_INIT, CCCR_INIT);
1753 netdev_err(dev, "failed to enter standby mode: %pe\n",
1756 /* set the state as STOPPED */
1757 cdev->can.state = CAN_STATE_STOPPED;
1760 static int m_can_close(struct net_device *dev)
1762 struct m_can_classdev *cdev = netdev_priv(dev);
1764 netif_stop_queue(dev);
1766 if (!cdev->is_peripheral)
1767 napi_disable(&cdev->napi);
1770 m_can_clk_stop(cdev);
1771 free_irq(dev->irq, dev);
1775 if (cdev->is_peripheral) {
1776 destroy_workqueue(cdev->tx_wq);
1778 can_rx_offload_disable(&cdev->offload);
1783 phy_power_off(cdev->transceiver);
1788 static netdev_tx_t m_can_tx_handler(struct m_can_classdev *cdev,
1789 struct sk_buff *skb)
1791 struct canfd_frame *cf = (struct canfd_frame *)skb->data;
1792 u8 len_padded = DIV_ROUND_UP(cf->len, 4);
1793 struct m_can_fifo_element fifo_element;
1794 struct net_device *dev = cdev->net;
1798 unsigned int frame_len = can_skb_get_frame_len(skb);
1800 /* Generate ID field for TX buffer Element */
1801 /* Common to all supported M_CAN versions */
1802 if (cf->can_id & CAN_EFF_FLAG) {
1803 fifo_element.id = cf->can_id & CAN_EFF_MASK;
1804 fifo_element.id |= TX_BUF_XTD;
1806 fifo_element.id = ((cf->can_id & CAN_SFF_MASK) << 18);
1809 if (cf->can_id & CAN_RTR_FLAG)
1810 fifo_element.id |= TX_BUF_RTR;
1812 if (cdev->version == 30) {
1813 netif_stop_queue(dev);
1815 fifo_element.dlc = can_fd_len2dlc(cf->len) << 16;
1817 /* Write the frame ID, DLC, and payload to the FIFO element. */
1818 err = m_can_fifo_write(cdev, 0, M_CAN_FIFO_ID, &fifo_element, 2);
1822 err = m_can_fifo_write(cdev, 0, M_CAN_FIFO_DATA,
1823 cf->data, len_padded);
1827 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD) {
1828 cccr = m_can_read(cdev, M_CAN_CCCR);
1829 cccr &= ~CCCR_CMR_MASK;
1830 if (can_is_canfd_skb(skb)) {
1831 if (cf->flags & CANFD_BRS)
1832 cccr |= FIELD_PREP(CCCR_CMR_MASK,
1833 CCCR_CMR_CANFD_BRS);
1835 cccr |= FIELD_PREP(CCCR_CMR_MASK,
1838 cccr |= FIELD_PREP(CCCR_CMR_MASK, CCCR_CMR_CAN);
1840 m_can_write(cdev, M_CAN_CCCR, cccr);
1842 m_can_write(cdev, M_CAN_TXBTIE, 0x1);
1844 can_put_echo_skb(skb, dev, 0, frame_len);
1846 m_can_write(cdev, M_CAN_TXBAR, 0x1);
1847 /* End of xmit function for version 3.0.x */
1849 /* Transmit routine for version >= v3.1.x */
1851 /* get put index for frame */
1852 putidx = cdev->tx_fifo_putidx;
1854 /* Construct DLC Field, with CAN-FD configuration.
1855 * Use the put index of the fifo as the message marker,
1856 * used in the TX interrupt for sending the correct echo frame.
1859 /* get CAN FD configuration of frame */
1861 if (can_is_canfd_skb(skb)) {
1862 fdflags |= TX_BUF_FDF;
1863 if (cf->flags & CANFD_BRS)
1864 fdflags |= TX_BUF_BRS;
1867 fifo_element.dlc = FIELD_PREP(TX_BUF_MM_MASK, putidx) |
1868 FIELD_PREP(TX_BUF_DLC_MASK, can_fd_len2dlc(cf->len)) |
1869 fdflags | TX_BUF_EFC;
1871 memcpy_and_pad(fifo_element.data, CANFD_MAX_DLEN, &cf->data,
1874 err = m_can_fifo_write(cdev, putidx, M_CAN_FIFO_ID,
1875 &fifo_element, 2 + len_padded);
1879 /* Push loopback echo.
1880 * Will be looped back on TX interrupt based on message marker
1882 can_put_echo_skb(skb, dev, putidx, frame_len);
1884 if (cdev->is_peripheral) {
1885 /* Delay enabling TX FIFO element */
1886 cdev->tx_peripheral_submit |= BIT(putidx);
1888 /* Enable TX FIFO element to start transfer */
1889 m_can_write(cdev, M_CAN_TXBAR, BIT(putidx));
1891 cdev->tx_fifo_putidx = (++cdev->tx_fifo_putidx >= cdev->can.echo_skb_max ?
1892 0 : cdev->tx_fifo_putidx);
1895 return NETDEV_TX_OK;
1898 netdev_err(dev, "FIFO write returned %d\n", err);
1899 m_can_disable_all_interrupts(cdev);
1900 return NETDEV_TX_BUSY;
1903 static void m_can_tx_submit(struct m_can_classdev *cdev)
1905 if (cdev->version == 30)
1907 if (!cdev->is_peripheral)
1910 m_can_write(cdev, M_CAN_TXBAR, cdev->tx_peripheral_submit);
1911 cdev->tx_peripheral_submit = 0;
1914 static void m_can_tx_work_queue(struct work_struct *ws)
1916 struct m_can_tx_op *op = container_of(ws, struct m_can_tx_op, work);
1917 struct m_can_classdev *cdev = op->cdev;
1918 struct sk_buff *skb = op->skb;
1921 m_can_tx_handler(cdev, skb);
1923 m_can_tx_submit(cdev);
1926 static void m_can_tx_queue_skb(struct m_can_classdev *cdev, struct sk_buff *skb,
1929 cdev->tx_ops[cdev->next_tx_op].skb = skb;
1930 cdev->tx_ops[cdev->next_tx_op].submit = submit;
1931 queue_work(cdev->tx_wq, &cdev->tx_ops[cdev->next_tx_op].work);
1934 if (cdev->next_tx_op >= cdev->tx_fifo_size)
1935 cdev->next_tx_op = 0;
1938 static netdev_tx_t m_can_start_peripheral_xmit(struct m_can_classdev *cdev,
1939 struct sk_buff *skb)
1943 ++cdev->nr_txs_without_submit;
1944 if (cdev->nr_txs_without_submit >= cdev->tx_max_coalesced_frames ||
1945 !netdev_xmit_more()) {
1946 cdev->nr_txs_without_submit = 0;
1951 m_can_tx_queue_skb(cdev, skb, submit);
1953 return NETDEV_TX_OK;
1956 static netdev_tx_t m_can_start_xmit(struct sk_buff *skb,
1957 struct net_device *dev)
1959 struct m_can_classdev *cdev = netdev_priv(dev);
1960 unsigned int frame_len;
1963 if (can_dev_dropped_skb(dev, skb))
1964 return NETDEV_TX_OK;
1966 frame_len = can_skb_get_frame_len(skb);
1968 if (cdev->can.state == CAN_STATE_BUS_OFF) {
1969 m_can_clean(cdev->net);
1970 return NETDEV_TX_OK;
1973 ret = m_can_start_tx(cdev);
1974 if (ret != NETDEV_TX_OK)
1977 netdev_sent_queue(dev, frame_len);
1979 if (cdev->is_peripheral)
1980 ret = m_can_start_peripheral_xmit(cdev, skb);
1982 ret = m_can_tx_handler(cdev, skb);
1984 if (ret != NETDEV_TX_OK)
1985 netdev_completed_queue(dev, 1, frame_len);
1990 static enum hrtimer_restart hrtimer_callback(struct hrtimer *timer)
1992 struct m_can_classdev *cdev = container_of(timer, struct
1993 m_can_classdev, hrtimer);
1995 m_can_isr(0, cdev->net);
1997 hrtimer_forward_now(timer, ms_to_ktime(HRTIMER_POLL_INTERVAL_MS));
1999 return HRTIMER_RESTART;
2002 static int m_can_open(struct net_device *dev)
2004 struct m_can_classdev *cdev = netdev_priv(dev);
2007 err = phy_power_on(cdev->transceiver);
2011 err = m_can_clk_start(cdev);
2013 goto out_phy_power_off;
2015 /* open the can device */
2016 err = open_candev(dev);
2018 netdev_err(dev, "failed to open can device\n");
2019 goto exit_disable_clks;
2022 if (cdev->is_peripheral)
2023 can_rx_offload_enable(&cdev->offload);
2025 /* register interrupt handler */
2026 if (cdev->is_peripheral) {
2027 cdev->tx_wq = alloc_ordered_workqueue("mcan_wq",
2028 WQ_FREEZABLE | WQ_MEM_RECLAIM);
2034 for (int i = 0; i != cdev->tx_fifo_size; ++i) {
2035 cdev->tx_ops[i].cdev = cdev;
2036 INIT_WORK(&cdev->tx_ops[i].work, m_can_tx_work_queue);
2039 err = request_threaded_irq(dev->irq, NULL, m_can_isr,
2042 } else if (dev->irq) {
2043 err = request_irq(dev->irq, m_can_isr, IRQF_SHARED, dev->name,
2048 netdev_err(dev, "failed to request interrupt\n");
2052 /* start the m_can controller */
2053 err = m_can_start(dev);
2057 if (!cdev->is_peripheral)
2058 napi_enable(&cdev->napi);
2060 netif_start_queue(dev);
2065 if (cdev->is_peripheral)
2066 destroy_workqueue(cdev->tx_wq);
2068 if (cdev->is_peripheral)
2069 can_rx_offload_disable(&cdev->offload);
2072 m_can_clk_stop(cdev);
2074 phy_power_off(cdev->transceiver);
2078 static const struct net_device_ops m_can_netdev_ops = {
2079 .ndo_open = m_can_open,
2080 .ndo_stop = m_can_close,
2081 .ndo_start_xmit = m_can_start_xmit,
2082 .ndo_change_mtu = can_change_mtu,
2085 static int m_can_get_coalesce(struct net_device *dev,
2086 struct ethtool_coalesce *ec,
2087 struct kernel_ethtool_coalesce *kec,
2088 struct netlink_ext_ack *ext_ack)
2090 struct m_can_classdev *cdev = netdev_priv(dev);
2092 ec->rx_max_coalesced_frames_irq = cdev->rx_max_coalesced_frames_irq;
2093 ec->rx_coalesce_usecs_irq = cdev->rx_coalesce_usecs_irq;
2094 ec->tx_max_coalesced_frames = cdev->tx_max_coalesced_frames;
2095 ec->tx_max_coalesced_frames_irq = cdev->tx_max_coalesced_frames_irq;
2096 ec->tx_coalesce_usecs_irq = cdev->tx_coalesce_usecs_irq;
2101 static int m_can_set_coalesce(struct net_device *dev,
2102 struct ethtool_coalesce *ec,
2103 struct kernel_ethtool_coalesce *kec,
2104 struct netlink_ext_ack *ext_ack)
2106 struct m_can_classdev *cdev = netdev_priv(dev);
2108 if (cdev->can.state != CAN_STATE_STOPPED) {
2109 netdev_err(dev, "Device is in use, please shut it down first\n");
2113 if (ec->rx_max_coalesced_frames_irq > cdev->mcfg[MRAM_RXF0].num) {
2114 netdev_err(dev, "rx-frames-irq %u greater than the RX FIFO %u\n",
2115 ec->rx_max_coalesced_frames_irq,
2116 cdev->mcfg[MRAM_RXF0].num);
2119 if ((ec->rx_max_coalesced_frames_irq == 0) != (ec->rx_coalesce_usecs_irq == 0)) {
2120 netdev_err(dev, "rx-frames-irq and rx-usecs-irq can only be set together\n");
2123 if (ec->tx_max_coalesced_frames_irq > cdev->mcfg[MRAM_TXE].num) {
2124 netdev_err(dev, "tx-frames-irq %u greater than the TX event FIFO %u\n",
2125 ec->tx_max_coalesced_frames_irq,
2126 cdev->mcfg[MRAM_TXE].num);
2129 if (ec->tx_max_coalesced_frames_irq > cdev->mcfg[MRAM_TXB].num) {
2130 netdev_err(dev, "tx-frames-irq %u greater than the TX FIFO %u\n",
2131 ec->tx_max_coalesced_frames_irq,
2132 cdev->mcfg[MRAM_TXB].num);
2135 if ((ec->tx_max_coalesced_frames_irq == 0) != (ec->tx_coalesce_usecs_irq == 0)) {
2136 netdev_err(dev, "tx-frames-irq and tx-usecs-irq can only be set together\n");
2139 if (ec->tx_max_coalesced_frames > cdev->mcfg[MRAM_TXE].num) {
2140 netdev_err(dev, "tx-frames %u greater than the TX event FIFO %u\n",
2141 ec->tx_max_coalesced_frames,
2142 cdev->mcfg[MRAM_TXE].num);
2145 if (ec->tx_max_coalesced_frames > cdev->mcfg[MRAM_TXB].num) {
2146 netdev_err(dev, "tx-frames %u greater than the TX FIFO %u\n",
2147 ec->tx_max_coalesced_frames,
2148 cdev->mcfg[MRAM_TXB].num);
2151 if (ec->rx_coalesce_usecs_irq != 0 && ec->tx_coalesce_usecs_irq != 0 &&
2152 ec->rx_coalesce_usecs_irq != ec->tx_coalesce_usecs_irq) {
2153 netdev_err(dev, "rx-usecs-irq %u needs to be equal to tx-usecs-irq %u if both are enabled\n",
2154 ec->rx_coalesce_usecs_irq,
2155 ec->tx_coalesce_usecs_irq);
2159 cdev->rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
2160 cdev->rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
2161 cdev->tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
2162 cdev->tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
2163 cdev->tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
2165 if (cdev->rx_coalesce_usecs_irq)
2166 cdev->irq_timer_wait =
2167 ns_to_ktime(cdev->rx_coalesce_usecs_irq * NSEC_PER_USEC);
2169 cdev->irq_timer_wait =
2170 ns_to_ktime(cdev->tx_coalesce_usecs_irq * NSEC_PER_USEC);
2175 static const struct ethtool_ops m_can_ethtool_ops = {
2176 .supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS_IRQ |
2177 ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ |
2178 ETHTOOL_COALESCE_TX_USECS_IRQ |
2179 ETHTOOL_COALESCE_TX_MAX_FRAMES |
2180 ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ,
2181 .get_ts_info = ethtool_op_get_ts_info,
2182 .get_coalesce = m_can_get_coalesce,
2183 .set_coalesce = m_can_set_coalesce,
2186 static const struct ethtool_ops m_can_ethtool_ops_polling = {
2187 .get_ts_info = ethtool_op_get_ts_info,
2190 static int register_m_can_dev(struct net_device *dev)
2192 dev->flags |= IFF_ECHO; /* we support local echo */
2193 dev->netdev_ops = &m_can_netdev_ops;
2195 dev->ethtool_ops = &m_can_ethtool_ops;
2197 dev->ethtool_ops = &m_can_ethtool_ops_polling;
2199 return register_candev(dev);
2202 int m_can_check_mram_cfg(struct m_can_classdev *cdev, u32 mram_max_size)
2206 total_size = cdev->mcfg[MRAM_TXB].off - cdev->mcfg[MRAM_SIDF].off +
2207 cdev->mcfg[MRAM_TXB].num * TXB_ELEMENT_SIZE;
2208 if (total_size > mram_max_size) {
2209 dev_err(cdev->dev, "Total size of mram config(%u) exceeds mram(%u)\n",
2210 total_size, mram_max_size);
2216 EXPORT_SYMBOL_GPL(m_can_check_mram_cfg);
2218 static void m_can_of_parse_mram(struct m_can_classdev *cdev,
2219 const u32 *mram_config_vals)
2221 cdev->mcfg[MRAM_SIDF].off = mram_config_vals[0];
2222 cdev->mcfg[MRAM_SIDF].num = mram_config_vals[1];
2223 cdev->mcfg[MRAM_XIDF].off = cdev->mcfg[MRAM_SIDF].off +
2224 cdev->mcfg[MRAM_SIDF].num * SIDF_ELEMENT_SIZE;
2225 cdev->mcfg[MRAM_XIDF].num = mram_config_vals[2];
2226 cdev->mcfg[MRAM_RXF0].off = cdev->mcfg[MRAM_XIDF].off +
2227 cdev->mcfg[MRAM_XIDF].num * XIDF_ELEMENT_SIZE;
2228 cdev->mcfg[MRAM_RXF0].num = mram_config_vals[3] &
2229 FIELD_MAX(RXFC_FS_MASK);
2230 cdev->mcfg[MRAM_RXF1].off = cdev->mcfg[MRAM_RXF0].off +
2231 cdev->mcfg[MRAM_RXF0].num * RXF0_ELEMENT_SIZE;
2232 cdev->mcfg[MRAM_RXF1].num = mram_config_vals[4] &
2233 FIELD_MAX(RXFC_FS_MASK);
2234 cdev->mcfg[MRAM_RXB].off = cdev->mcfg[MRAM_RXF1].off +
2235 cdev->mcfg[MRAM_RXF1].num * RXF1_ELEMENT_SIZE;
2236 cdev->mcfg[MRAM_RXB].num = mram_config_vals[5];
2237 cdev->mcfg[MRAM_TXE].off = cdev->mcfg[MRAM_RXB].off +
2238 cdev->mcfg[MRAM_RXB].num * RXB_ELEMENT_SIZE;
2239 cdev->mcfg[MRAM_TXE].num = mram_config_vals[6];
2240 cdev->mcfg[MRAM_TXB].off = cdev->mcfg[MRAM_TXE].off +
2241 cdev->mcfg[MRAM_TXE].num * TXE_ELEMENT_SIZE;
2242 cdev->mcfg[MRAM_TXB].num = mram_config_vals[7] &
2243 FIELD_MAX(TXBC_NDTB_MASK);
2246 "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",
2247 cdev->mcfg[MRAM_SIDF].off, cdev->mcfg[MRAM_SIDF].num,
2248 cdev->mcfg[MRAM_XIDF].off, cdev->mcfg[MRAM_XIDF].num,
2249 cdev->mcfg[MRAM_RXF0].off, cdev->mcfg[MRAM_RXF0].num,
2250 cdev->mcfg[MRAM_RXF1].off, cdev->mcfg[MRAM_RXF1].num,
2251 cdev->mcfg[MRAM_RXB].off, cdev->mcfg[MRAM_RXB].num,
2252 cdev->mcfg[MRAM_TXE].off, cdev->mcfg[MRAM_TXE].num,
2253 cdev->mcfg[MRAM_TXB].off, cdev->mcfg[MRAM_TXB].num);
2256 int m_can_init_ram(struct m_can_classdev *cdev)
2261 /* initialize the entire Message RAM in use to avoid possible
2262 * ECC/parity checksum errors when reading an uninitialized buffer
2264 start = cdev->mcfg[MRAM_SIDF].off;
2265 end = cdev->mcfg[MRAM_TXB].off +
2266 cdev->mcfg[MRAM_TXB].num * TXB_ELEMENT_SIZE;
2268 for (i = start; i < end; i += 4) {
2269 err = m_can_fifo_write_no_off(cdev, i, 0x0);
2276 EXPORT_SYMBOL_GPL(m_can_init_ram);
2278 int m_can_class_get_clocks(struct m_can_classdev *cdev)
2282 cdev->hclk = devm_clk_get(cdev->dev, "hclk");
2283 cdev->cclk = devm_clk_get(cdev->dev, "cclk");
2285 if (IS_ERR(cdev->hclk) || IS_ERR(cdev->cclk)) {
2286 dev_err(cdev->dev, "no clock found\n");
2292 EXPORT_SYMBOL_GPL(m_can_class_get_clocks);
2294 struct m_can_classdev *m_can_class_allocate_dev(struct device *dev,
2297 struct m_can_classdev *class_dev = NULL;
2298 u32 mram_config_vals[MRAM_CFG_LEN];
2299 struct net_device *net_dev;
2303 ret = fwnode_property_read_u32_array(dev_fwnode(dev),
2306 sizeof(mram_config_vals) / 4);
2308 dev_err(dev, "Could not get Message RAM configuration.");
2313 * Defines the total amount of echo buffers for loopback
2315 tx_fifo_size = mram_config_vals[7];
2317 /* allocate the m_can device */
2318 net_dev = alloc_candev(sizeof_priv, tx_fifo_size);
2320 dev_err(dev, "Failed to allocate CAN device");
2324 class_dev = netdev_priv(net_dev);
2325 class_dev->net = net_dev;
2326 class_dev->dev = dev;
2327 SET_NETDEV_DEV(net_dev, dev);
2329 m_can_of_parse_mram(class_dev, mram_config_vals);
2333 EXPORT_SYMBOL_GPL(m_can_class_allocate_dev);
2335 void m_can_class_free_dev(struct net_device *net)
2339 EXPORT_SYMBOL_GPL(m_can_class_free_dev);
2341 int m_can_class_register(struct m_can_classdev *cdev)
2345 cdev->tx_fifo_size = max(1, min(cdev->mcfg[MRAM_TXB].num,
2346 cdev->mcfg[MRAM_TXE].num));
2347 if (cdev->is_peripheral) {
2349 devm_kzalloc(cdev->dev,
2350 cdev->tx_fifo_size * sizeof(*cdev->tx_ops),
2352 if (!cdev->tx_ops) {
2353 dev_err(cdev->dev, "Failed to allocate tx_ops for workqueue\n");
2358 ret = m_can_clk_start(cdev);
2362 if (cdev->is_peripheral) {
2363 ret = can_rx_offload_add_manual(cdev->net, &cdev->offload,
2369 if (!cdev->net->irq) {
2370 dev_dbg(cdev->dev, "Polling enabled, initialize hrtimer");
2371 hrtimer_init(&cdev->hrtimer, CLOCK_MONOTONIC,
2372 HRTIMER_MODE_REL_PINNED);
2373 cdev->hrtimer.function = &hrtimer_callback;
2375 hrtimer_init(&cdev->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2376 cdev->hrtimer.function = m_can_coalescing_timer;
2379 ret = m_can_dev_setup(cdev);
2381 goto rx_offload_del;
2383 ret = register_m_can_dev(cdev->net);
2385 dev_err(cdev->dev, "registering %s failed (err=%d)\n",
2386 cdev->net->name, ret);
2387 goto rx_offload_del;
2390 of_can_transceiver(cdev->net);
2392 dev_info(cdev->dev, "%s device registered (irq=%d, version=%d)\n",
2393 KBUILD_MODNAME, cdev->net->irq, cdev->version);
2396 * Stop clocks. They will be reactivated once the M_CAN device is opened
2398 m_can_clk_stop(cdev);
2403 if (cdev->is_peripheral)
2404 can_rx_offload_del(&cdev->offload);
2406 m_can_clk_stop(cdev);
2410 EXPORT_SYMBOL_GPL(m_can_class_register);
2412 void m_can_class_unregister(struct m_can_classdev *cdev)
2414 if (cdev->is_peripheral)
2415 can_rx_offload_del(&cdev->offload);
2416 unregister_candev(cdev->net);
2418 EXPORT_SYMBOL_GPL(m_can_class_unregister);
2420 int m_can_class_suspend(struct device *dev)
2422 struct m_can_classdev *cdev = dev_get_drvdata(dev);
2423 struct net_device *ndev = cdev->net;
2425 if (netif_running(ndev)) {
2426 netif_stop_queue(ndev);
2427 netif_device_detach(ndev);
2429 /* leave the chip running with rx interrupt enabled if it is
2430 * used as a wake-up source.
2432 if (cdev->pm_wake_source)
2433 m_can_write(cdev, M_CAN_IE, IR_RF0N);
2437 m_can_clk_stop(cdev);
2440 pinctrl_pm_select_sleep_state(dev);
2442 cdev->can.state = CAN_STATE_SLEEPING;
2446 EXPORT_SYMBOL_GPL(m_can_class_suspend);
2448 int m_can_class_resume(struct device *dev)
2450 struct m_can_classdev *cdev = dev_get_drvdata(dev);
2451 struct net_device *ndev = cdev->net;
2453 pinctrl_pm_select_default_state(dev);
2455 cdev->can.state = CAN_STATE_ERROR_ACTIVE;
2457 if (netif_running(ndev)) {
2460 ret = m_can_clk_start(cdev);
2464 if (cdev->pm_wake_source) {
2465 m_can_write(cdev, M_CAN_IE, cdev->active_interrupts);
2467 ret = m_can_start(ndev);
2469 m_can_clk_stop(cdev);
2474 netif_device_attach(ndev);
2475 netif_start_queue(ndev);
2480 EXPORT_SYMBOL_GPL(m_can_class_resume);
2484 MODULE_LICENSE("GPL v2");
2485 MODULE_DESCRIPTION("CAN bus driver for Bosch M_CAN controller");