2 * CAN bus driver for Bosch M_CAN controller
4 * Copyright (C) 2014 Freescale Semiconductor, Inc.
5 * Dong Aisheng <b29396@freescale.com>
7 * Bosch M_CAN user manual can be obtained from:
8 * http://www.bosch-semiconductors.de/media/pdf_1/ipmodules_1/m_can/
9 * mcan_users_manual_v302.pdf
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/netdevice.h>
24 #include <linux/of_device.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/iopoll.h>
28 #include <linux/can/dev.h>
29 #include <linux/pinctrl/consumer.h>
32 #define M_CAN_NAPI_WEIGHT 64
34 /* message ram configuration data length */
35 #define MRAM_CFG_LEN 8
37 /* registers definition */
53 /* TDCR Register only available for version >=3.1.x */
89 /* m_can lec values */
101 enum m_can_mram_cfg {
112 /* Core Release Register (CREL) */
113 #define CREL_REL_SHIFT 28
114 #define CREL_REL_MASK (0xF << CREL_REL_SHIFT)
115 #define CREL_STEP_SHIFT 24
116 #define CREL_STEP_MASK (0xF << CREL_STEP_SHIFT)
117 #define CREL_SUBSTEP_SHIFT 20
118 #define CREL_SUBSTEP_MASK (0xF << CREL_SUBSTEP_SHIFT)
120 /* Data Bit Timing & Prescaler Register (DBTP) */
121 #define DBTP_TDC BIT(23)
122 #define DBTP_DBRP_SHIFT 16
123 #define DBTP_DBRP_MASK (0x1f << DBTP_DBRP_SHIFT)
124 #define DBTP_DTSEG1_SHIFT 8
125 #define DBTP_DTSEG1_MASK (0x1f << DBTP_DTSEG1_SHIFT)
126 #define DBTP_DTSEG2_SHIFT 4
127 #define DBTP_DTSEG2_MASK (0xf << DBTP_DTSEG2_SHIFT)
128 #define DBTP_DSJW_SHIFT 0
129 #define DBTP_DSJW_MASK (0xf << DBTP_DSJW_SHIFT)
131 /* Transmitter Delay Compensation Register (TDCR) */
132 #define TDCR_TDCO_SHIFT 8
133 #define TDCR_TDCO_MASK (0x7F << TDCR_TDCO_SHIFT)
134 #define TDCR_TDCF_SHIFT 0
135 #define TDCR_TDCF_MASK (0x7F << TDCR_TDCF_SHIFT)
137 /* Test Register (TEST) */
138 #define TEST_LBCK BIT(4)
140 /* CC Control Register(CCCR) */
141 #define CCCR_CMR_MASK 0x3
142 #define CCCR_CMR_SHIFT 10
143 #define CCCR_CMR_CANFD 0x1
144 #define CCCR_CMR_CANFD_BRS 0x2
145 #define CCCR_CMR_CAN 0x3
146 #define CCCR_CME_MASK 0x3
147 #define CCCR_CME_SHIFT 8
148 #define CCCR_CME_CAN 0
149 #define CCCR_CME_CANFD 0x1
150 #define CCCR_CME_CANFD_BRS 0x2
151 #define CCCR_TXP BIT(14)
152 #define CCCR_TEST BIT(7)
153 #define CCCR_MON BIT(5)
154 #define CCCR_CSR BIT(4)
155 #define CCCR_CSA BIT(3)
156 #define CCCR_ASM BIT(2)
157 #define CCCR_CCE BIT(1)
158 #define CCCR_INIT BIT(0)
159 #define CCCR_CANFD 0x10
160 /* for version >=3.1.x */
161 #define CCCR_EFBI BIT(13)
162 #define CCCR_PXHD BIT(12)
163 #define CCCR_BRSE BIT(9)
164 #define CCCR_FDOE BIT(8)
165 /* only for version >=3.2.x */
166 #define CCCR_NISO BIT(15)
168 /* Nominal Bit Timing & Prescaler Register (NBTP) */
169 #define NBTP_NSJW_SHIFT 25
170 #define NBTP_NSJW_MASK (0x7f << NBTP_NSJW_SHIFT)
171 #define NBTP_NBRP_SHIFT 16
172 #define NBTP_NBRP_MASK (0x1ff << NBTP_NBRP_SHIFT)
173 #define NBTP_NTSEG1_SHIFT 8
174 #define NBTP_NTSEG1_MASK (0xff << NBTP_NTSEG1_SHIFT)
175 #define NBTP_NTSEG2_SHIFT 0
176 #define NBTP_NTSEG2_MASK (0x7f << NBTP_NTSEG2_SHIFT)
178 /* Error Counter Register(ECR) */
179 #define ECR_RP BIT(15)
180 #define ECR_REC_SHIFT 8
181 #define ECR_REC_MASK (0x7f << ECR_REC_SHIFT)
182 #define ECR_TEC_SHIFT 0
183 #define ECR_TEC_MASK 0xff
185 /* Protocol Status Register(PSR) */
186 #define PSR_BO BIT(7)
187 #define PSR_EW BIT(6)
188 #define PSR_EP BIT(5)
189 #define PSR_LEC_MASK 0x7
191 /* Interrupt Register(IR) */
192 #define IR_ALL_INT 0xffffffff
194 /* Renamed bits for versions > 3.1.x */
195 #define IR_ARA BIT(29)
196 #define IR_PED BIT(28)
197 #define IR_PEA BIT(27)
199 /* Bits for version 3.0.x */
200 #define IR_STE BIT(31)
201 #define IR_FOE BIT(30)
202 #define IR_ACKE BIT(29)
203 #define IR_BE BIT(28)
204 #define IR_CRCE BIT(27)
205 #define IR_WDI BIT(26)
206 #define IR_BO BIT(25)
207 #define IR_EW BIT(24)
208 #define IR_EP BIT(23)
209 #define IR_ELO BIT(22)
210 #define IR_BEU BIT(21)
211 #define IR_BEC BIT(20)
212 #define IR_DRX BIT(19)
213 #define IR_TOO BIT(18)
214 #define IR_MRAF BIT(17)
215 #define IR_TSW BIT(16)
216 #define IR_TEFL BIT(15)
217 #define IR_TEFF BIT(14)
218 #define IR_TEFW BIT(13)
219 #define IR_TEFN BIT(12)
220 #define IR_TFE BIT(11)
221 #define IR_TCF BIT(10)
223 #define IR_HPM BIT(8)
224 #define IR_RF1L BIT(7)
225 #define IR_RF1F BIT(6)
226 #define IR_RF1W BIT(5)
227 #define IR_RF1N BIT(4)
228 #define IR_RF0L BIT(3)
229 #define IR_RF0F BIT(2)
230 #define IR_RF0W BIT(1)
231 #define IR_RF0N BIT(0)
232 #define IR_ERR_STATE (IR_BO | IR_EW | IR_EP)
234 /* Interrupts for version 3.0.x */
235 #define IR_ERR_LEC_30X (IR_STE | IR_FOE | IR_ACKE | IR_BE | IR_CRCE)
236 #define IR_ERR_BUS_30X (IR_ERR_LEC_30X | IR_WDI | IR_BEU | IR_BEC | \
237 IR_TOO | IR_MRAF | IR_TSW | IR_TEFL | IR_RF1L | \
239 #define IR_ERR_ALL_30X (IR_ERR_STATE | IR_ERR_BUS_30X)
240 /* Interrupts for version >= 3.1.x */
241 #define IR_ERR_LEC_31X (IR_PED | IR_PEA)
242 #define IR_ERR_BUS_31X (IR_ERR_LEC_31X | IR_WDI | IR_BEU | IR_BEC | \
243 IR_TOO | IR_MRAF | IR_TSW | IR_TEFL | IR_RF1L | \
245 #define IR_ERR_ALL_31X (IR_ERR_STATE | IR_ERR_BUS_31X)
247 /* Interrupt Line Select (ILS) */
248 #define ILS_ALL_INT0 0x0
249 #define ILS_ALL_INT1 0xFFFFFFFF
251 /* Interrupt Line Enable (ILE) */
252 #define ILE_EINT1 BIT(1)
253 #define ILE_EINT0 BIT(0)
255 /* Rx FIFO 0/1 Configuration (RXF0C/RXF1C) */
256 #define RXFC_FWM_SHIFT 24
257 #define RXFC_FWM_MASK (0x7f << RXFC_FWM_SHIFT)
258 #define RXFC_FS_SHIFT 16
259 #define RXFC_FS_MASK (0x7f << RXFC_FS_SHIFT)
261 /* Rx FIFO 0/1 Status (RXF0S/RXF1S) */
262 #define RXFS_RFL BIT(25)
263 #define RXFS_FF BIT(24)
264 #define RXFS_FPI_SHIFT 16
265 #define RXFS_FPI_MASK 0x3f0000
266 #define RXFS_FGI_SHIFT 8
267 #define RXFS_FGI_MASK 0x3f00
268 #define RXFS_FFL_MASK 0x7f
270 /* Rx Buffer / FIFO Element Size Configuration (RXESC) */
271 #define M_CAN_RXESC_8BYTES 0x0
272 #define M_CAN_RXESC_64BYTES 0x777
274 /* Tx Buffer Configuration(TXBC) */
275 #define TXBC_NDTB_SHIFT 16
276 #define TXBC_NDTB_MASK (0x3f << TXBC_NDTB_SHIFT)
277 #define TXBC_TFQS_SHIFT 24
278 #define TXBC_TFQS_MASK (0x3f << TXBC_TFQS_SHIFT)
280 /* Tx FIFO/Queue Status (TXFQS) */
281 #define TXFQS_TFQF BIT(21)
282 #define TXFQS_TFQPI_SHIFT 16
283 #define TXFQS_TFQPI_MASK (0x1f << TXFQS_TFQPI_SHIFT)
284 #define TXFQS_TFGI_SHIFT 8
285 #define TXFQS_TFGI_MASK (0x1f << TXFQS_TFGI_SHIFT)
286 #define TXFQS_TFFL_SHIFT 0
287 #define TXFQS_TFFL_MASK (0x3f << TXFQS_TFFL_SHIFT)
289 /* Tx Buffer Element Size Configuration(TXESC) */
290 #define TXESC_TBDS_8BYTES 0x0
291 #define TXESC_TBDS_64BYTES 0x7
293 /* Tx Event FIFO Configuration (TXEFC) */
294 #define TXEFC_EFS_SHIFT 16
295 #define TXEFC_EFS_MASK (0x3f << TXEFC_EFS_SHIFT)
297 /* Tx Event FIFO Status (TXEFS) */
298 #define TXEFS_TEFL BIT(25)
299 #define TXEFS_EFF BIT(24)
300 #define TXEFS_EFGI_SHIFT 8
301 #define TXEFS_EFGI_MASK (0x1f << TXEFS_EFGI_SHIFT)
302 #define TXEFS_EFFL_SHIFT 0
303 #define TXEFS_EFFL_MASK (0x3f << TXEFS_EFFL_SHIFT)
305 /* Tx Event FIFO Acknowledge (TXEFA) */
306 #define TXEFA_EFAI_SHIFT 0
307 #define TXEFA_EFAI_MASK (0x1f << TXEFA_EFAI_SHIFT)
309 /* Message RAM Configuration (in bytes) */
310 #define SIDF_ELEMENT_SIZE 4
311 #define XIDF_ELEMENT_SIZE 8
312 #define RXF0_ELEMENT_SIZE 72
313 #define RXF1_ELEMENT_SIZE 72
314 #define RXB_ELEMENT_SIZE 72
315 #define TXE_ELEMENT_SIZE 8
316 #define TXB_ELEMENT_SIZE 72
318 /* Message RAM Elements */
319 #define M_CAN_FIFO_ID 0x0
320 #define M_CAN_FIFO_DLC 0x4
321 #define M_CAN_FIFO_DATA(n) (0x8 + ((n) << 2))
323 /* Rx Buffer Element */
325 #define RX_BUF_ESI BIT(31)
326 #define RX_BUF_XTD BIT(30)
327 #define RX_BUF_RTR BIT(29)
329 #define RX_BUF_ANMF BIT(31)
330 #define RX_BUF_FDF BIT(21)
331 #define RX_BUF_BRS BIT(20)
333 /* Tx Buffer Element */
335 #define TX_BUF_ESI BIT(31)
336 #define TX_BUF_XTD BIT(30)
337 #define TX_BUF_RTR BIT(29)
339 #define TX_BUF_EFC BIT(23)
340 #define TX_BUF_FDF BIT(21)
341 #define TX_BUF_BRS BIT(20)
342 #define TX_BUF_MM_SHIFT 24
343 #define TX_BUF_MM_MASK (0xff << TX_BUF_MM_SHIFT)
345 /* Tx event FIFO Element */
347 #define TX_EVENT_MM_SHIFT TX_BUF_MM_SHIFT
348 #define TX_EVENT_MM_MASK (0xff << TX_EVENT_MM_SHIFT)
350 /* address offset and element number for each FIFO/Buffer in the Message RAM */
356 /* m_can private data structure */
358 struct can_priv can; /* must be the first member */
359 struct napi_struct napi;
360 struct net_device *dev;
361 struct device *device;
368 /* message ram configuration */
369 void __iomem *mram_base;
370 struct mram_cfg mcfg[MRAM_CFG_NUM];
373 static inline u32 m_can_read(const struct m_can_priv *priv, enum m_can_reg reg)
375 return readl(priv->base + reg);
378 static inline void m_can_write(const struct m_can_priv *priv,
379 enum m_can_reg reg, u32 val)
381 writel(val, priv->base + reg);
384 static inline u32 m_can_fifo_read(const struct m_can_priv *priv,
385 u32 fgi, unsigned int offset)
387 return readl(priv->mram_base + priv->mcfg[MRAM_RXF0].off +
388 fgi * RXF0_ELEMENT_SIZE + offset);
391 static inline void m_can_fifo_write(const struct m_can_priv *priv,
392 u32 fpi, unsigned int offset, u32 val)
394 writel(val, priv->mram_base + priv->mcfg[MRAM_TXB].off +
395 fpi * TXB_ELEMENT_SIZE + offset);
398 static inline u32 m_can_txe_fifo_read(const struct m_can_priv *priv,
401 return readl(priv->mram_base + priv->mcfg[MRAM_TXE].off +
402 fgi * TXE_ELEMENT_SIZE + offset);
405 static inline bool m_can_tx_fifo_full(const struct m_can_priv *priv)
407 return !!(m_can_read(priv, M_CAN_TXFQS) & TXFQS_TFQF);
410 static inline void m_can_config_endisable(const struct m_can_priv *priv,
413 u32 cccr = m_can_read(priv, M_CAN_CCCR);
418 /* enable m_can configuration */
419 m_can_write(priv, M_CAN_CCCR, cccr | CCCR_INIT);
421 /* CCCR.CCE can only be set/reset while CCCR.INIT = '1' */
422 m_can_write(priv, M_CAN_CCCR, cccr | CCCR_INIT | CCCR_CCE);
424 m_can_write(priv, M_CAN_CCCR, cccr & ~(CCCR_INIT | CCCR_CCE));
427 /* there's a delay for module initialization */
429 val = CCCR_INIT | CCCR_CCE;
431 while ((m_can_read(priv, M_CAN_CCCR) & (CCCR_INIT | CCCR_CCE)) != val) {
433 netdev_warn(priv->dev, "Failed to init module\n");
441 static inline void m_can_enable_all_interrupts(const struct m_can_priv *priv)
443 /* Only interrupt line 0 is used in this driver */
444 m_can_write(priv, M_CAN_ILE, ILE_EINT0);
447 static inline void m_can_disable_all_interrupts(const struct m_can_priv *priv)
449 m_can_write(priv, M_CAN_ILE, 0x0);
452 static void m_can_read_fifo(struct net_device *dev, u32 rxfs)
454 struct net_device_stats *stats = &dev->stats;
455 struct m_can_priv *priv = netdev_priv(dev);
456 struct canfd_frame *cf;
461 /* calculate the fifo get index for where to read data */
462 fgi = (rxfs & RXFS_FGI_MASK) >> RXFS_FGI_SHIFT;
463 dlc = m_can_fifo_read(priv, fgi, M_CAN_FIFO_DLC);
464 if (dlc & RX_BUF_FDF)
465 skb = alloc_canfd_skb(dev, &cf);
467 skb = alloc_can_skb(dev, (struct can_frame **)&cf);
473 if (dlc & RX_BUF_FDF)
474 cf->len = can_dlc2len((dlc >> 16) & 0x0F);
476 cf->len = get_can_dlc((dlc >> 16) & 0x0F);
478 id = m_can_fifo_read(priv, fgi, M_CAN_FIFO_ID);
480 cf->can_id = (id & CAN_EFF_MASK) | CAN_EFF_FLAG;
482 cf->can_id = (id >> 18) & CAN_SFF_MASK;
484 if (id & RX_BUF_ESI) {
485 cf->flags |= CANFD_ESI;
486 netdev_dbg(dev, "ESI Error\n");
489 if (!(dlc & RX_BUF_FDF) && (id & RX_BUF_RTR)) {
490 cf->can_id |= CAN_RTR_FLAG;
492 if (dlc & RX_BUF_BRS)
493 cf->flags |= CANFD_BRS;
495 for (i = 0; i < cf->len; i += 4)
496 *(u32 *)(cf->data + i) =
497 m_can_fifo_read(priv, fgi,
498 M_CAN_FIFO_DATA(i / 4));
501 /* acknowledge rx fifo 0 */
502 m_can_write(priv, M_CAN_RXF0A, fgi);
505 stats->rx_bytes += cf->len;
507 netif_receive_skb(skb);
510 static int m_can_do_rx_poll(struct net_device *dev, int quota)
512 struct m_can_priv *priv = netdev_priv(dev);
516 rxfs = m_can_read(priv, M_CAN_RXF0S);
517 if (!(rxfs & RXFS_FFL_MASK)) {
518 netdev_dbg(dev, "no messages in fifo0\n");
522 while ((rxfs & RXFS_FFL_MASK) && (quota > 0)) {
523 m_can_read_fifo(dev, rxfs);
527 rxfs = m_can_read(priv, M_CAN_RXF0S);
531 can_led_event(dev, CAN_LED_EVENT_RX);
536 static int m_can_handle_lost_msg(struct net_device *dev)
538 struct net_device_stats *stats = &dev->stats;
540 struct can_frame *frame;
542 netdev_err(dev, "msg lost in rxf0\n");
545 stats->rx_over_errors++;
547 skb = alloc_can_err_skb(dev, &frame);
551 frame->can_id |= CAN_ERR_CRTL;
552 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
554 netif_receive_skb(skb);
559 static int m_can_handle_lec_err(struct net_device *dev,
560 enum m_can_lec_type lec_type)
562 struct m_can_priv *priv = netdev_priv(dev);
563 struct net_device_stats *stats = &dev->stats;
564 struct can_frame *cf;
567 priv->can.can_stats.bus_error++;
570 /* propagate the error condition to the CAN stack */
571 skb = alloc_can_err_skb(dev, &cf);
575 /* check for 'last error code' which tells us the
576 * type of the last error to occur on the CAN bus
578 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
581 case LEC_STUFF_ERROR:
582 netdev_dbg(dev, "stuff error\n");
583 cf->data[2] |= CAN_ERR_PROT_STUFF;
586 netdev_dbg(dev, "form error\n");
587 cf->data[2] |= CAN_ERR_PROT_FORM;
590 netdev_dbg(dev, "ack error\n");
591 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
594 netdev_dbg(dev, "bit1 error\n");
595 cf->data[2] |= CAN_ERR_PROT_BIT1;
598 netdev_dbg(dev, "bit0 error\n");
599 cf->data[2] |= CAN_ERR_PROT_BIT0;
602 netdev_dbg(dev, "CRC error\n");
603 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
610 stats->rx_bytes += cf->can_dlc;
611 netif_receive_skb(skb);
616 static int __m_can_get_berr_counter(const struct net_device *dev,
617 struct can_berr_counter *bec)
619 struct m_can_priv *priv = netdev_priv(dev);
622 ecr = m_can_read(priv, M_CAN_ECR);
623 bec->rxerr = (ecr & ECR_REC_MASK) >> ECR_REC_SHIFT;
624 bec->txerr = (ecr & ECR_TEC_MASK) >> ECR_TEC_SHIFT;
629 static int m_can_clk_start(struct m_can_priv *priv)
633 err = pm_runtime_get_sync(priv->device);
635 pm_runtime_put_noidle(priv->device);
642 static void m_can_clk_stop(struct m_can_priv *priv)
644 pm_runtime_put_sync(priv->device);
647 static int m_can_get_berr_counter(const struct net_device *dev,
648 struct can_berr_counter *bec)
650 struct m_can_priv *priv = netdev_priv(dev);
653 err = m_can_clk_start(priv);
657 __m_can_get_berr_counter(dev, bec);
659 m_can_clk_stop(priv);
664 static int m_can_handle_state_change(struct net_device *dev,
665 enum can_state new_state)
667 struct m_can_priv *priv = netdev_priv(dev);
668 struct net_device_stats *stats = &dev->stats;
669 struct can_frame *cf;
671 struct can_berr_counter bec;
675 case CAN_STATE_ERROR_WARNING:
676 /* error warning state */
677 priv->can.can_stats.error_warning++;
678 priv->can.state = CAN_STATE_ERROR_WARNING;
680 case CAN_STATE_ERROR_PASSIVE:
681 /* error passive state */
682 priv->can.can_stats.error_passive++;
683 priv->can.state = CAN_STATE_ERROR_PASSIVE;
685 case CAN_STATE_BUS_OFF:
687 priv->can.state = CAN_STATE_BUS_OFF;
688 m_can_disable_all_interrupts(priv);
689 priv->can.can_stats.bus_off++;
696 /* propagate the error condition to the CAN stack */
697 skb = alloc_can_err_skb(dev, &cf);
701 __m_can_get_berr_counter(dev, &bec);
704 case CAN_STATE_ERROR_WARNING:
705 /* error warning state */
706 cf->can_id |= CAN_ERR_CRTL;
707 cf->data[1] = (bec.txerr > bec.rxerr) ?
708 CAN_ERR_CRTL_TX_WARNING :
709 CAN_ERR_CRTL_RX_WARNING;
710 cf->data[6] = bec.txerr;
711 cf->data[7] = bec.rxerr;
713 case CAN_STATE_ERROR_PASSIVE:
714 /* error passive state */
715 cf->can_id |= CAN_ERR_CRTL;
716 ecr = m_can_read(priv, M_CAN_ECR);
718 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
720 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
721 cf->data[6] = bec.txerr;
722 cf->data[7] = bec.rxerr;
724 case CAN_STATE_BUS_OFF:
726 cf->can_id |= CAN_ERR_BUSOFF;
733 stats->rx_bytes += cf->can_dlc;
734 netif_receive_skb(skb);
739 static int m_can_handle_state_errors(struct net_device *dev, u32 psr)
741 struct m_can_priv *priv = netdev_priv(dev);
744 if ((psr & PSR_EW) &&
745 (priv->can.state != CAN_STATE_ERROR_WARNING)) {
746 netdev_dbg(dev, "entered error warning state\n");
747 work_done += m_can_handle_state_change(dev,
748 CAN_STATE_ERROR_WARNING);
751 if ((psr & PSR_EP) &&
752 (priv->can.state != CAN_STATE_ERROR_PASSIVE)) {
753 netdev_dbg(dev, "entered error passive state\n");
754 work_done += m_can_handle_state_change(dev,
755 CAN_STATE_ERROR_PASSIVE);
758 if ((psr & PSR_BO) &&
759 (priv->can.state != CAN_STATE_BUS_OFF)) {
760 netdev_dbg(dev, "entered error bus off state\n");
761 work_done += m_can_handle_state_change(dev,
768 static void m_can_handle_other_err(struct net_device *dev, u32 irqstatus)
770 if (irqstatus & IR_WDI)
771 netdev_err(dev, "Message RAM Watchdog event due to missing READY\n");
772 if (irqstatus & IR_BEU)
773 netdev_err(dev, "Bit Error Uncorrected\n");
774 if (irqstatus & IR_BEC)
775 netdev_err(dev, "Bit Error Corrected\n");
776 if (irqstatus & IR_TOO)
777 netdev_err(dev, "Timeout reached\n");
778 if (irqstatus & IR_MRAF)
779 netdev_err(dev, "Message RAM access failure occurred\n");
782 static inline bool is_lec_err(u32 psr)
786 return psr && (psr != LEC_UNUSED);
789 static int m_can_handle_bus_errors(struct net_device *dev, u32 irqstatus,
792 struct m_can_priv *priv = netdev_priv(dev);
795 if (irqstatus & IR_RF0L)
796 work_done += m_can_handle_lost_msg(dev);
798 /* handle lec errors on the bus */
799 if ((priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
801 work_done += m_can_handle_lec_err(dev, psr & LEC_UNUSED);
803 /* other unproccessed error interrupts */
804 m_can_handle_other_err(dev, irqstatus);
809 static int m_can_poll(struct napi_struct *napi, int quota)
811 struct net_device *dev = napi->dev;
812 struct m_can_priv *priv = netdev_priv(dev);
816 irqstatus = priv->irqstatus | m_can_read(priv, M_CAN_IR);
820 /* Errata workaround for issue "Needless activation of MRAF irq"
821 * During frame reception while the MCAN is in Error Passive state
822 * and the Receive Error Counter has the value MCAN_ECR.REC = 127,
823 * it may happen that MCAN_IR.MRAF is set although there was no
824 * Message RAM access failure.
825 * If MCAN_IR.MRAF is enabled, an interrupt to the Host CPU is generated
826 * The Message RAM Access Failure interrupt routine needs to check
827 * whether MCAN_ECR.RP = ’1’ and MCAN_ECR.REC = 127.
828 * In this case, reset MCAN_IR.MRAF. No further action is required.
830 if ((priv->version <= 31) && (irqstatus & IR_MRAF) &&
831 (m_can_read(priv, M_CAN_ECR) & ECR_RP)) {
832 struct can_berr_counter bec;
834 __m_can_get_berr_counter(dev, &bec);
835 if (bec.rxerr == 127) {
836 m_can_write(priv, M_CAN_IR, IR_MRAF);
837 irqstatus &= ~IR_MRAF;
841 psr = m_can_read(priv, M_CAN_PSR);
842 if (irqstatus & IR_ERR_STATE)
843 work_done += m_can_handle_state_errors(dev, psr);
845 if (irqstatus & IR_ERR_BUS_30X)
846 work_done += m_can_handle_bus_errors(dev, irqstatus, psr);
848 if (irqstatus & IR_RF0N)
849 work_done += m_can_do_rx_poll(dev, (quota - work_done));
851 if (work_done < quota) {
852 napi_complete_done(napi, work_done);
853 m_can_enable_all_interrupts(priv);
860 static void m_can_echo_tx_event(struct net_device *dev)
866 unsigned int msg_mark;
868 struct m_can_priv *priv = netdev_priv(dev);
869 struct net_device_stats *stats = &dev->stats;
871 /* read tx event fifo status */
872 m_can_txefs = m_can_read(priv, M_CAN_TXEFS);
874 /* Get Tx Event fifo element count */
875 txe_count = (m_can_txefs & TXEFS_EFFL_MASK)
878 /* Get and process all sent elements */
879 for (i = 0; i < txe_count; i++) {
880 /* retrieve get index */
881 fgi = (m_can_read(priv, M_CAN_TXEFS) & TXEFS_EFGI_MASK)
884 /* get message marker */
885 msg_mark = (m_can_txe_fifo_read(priv, fgi, 4) &
886 TX_EVENT_MM_MASK) >> TX_EVENT_MM_SHIFT;
888 /* ack txe element */
889 m_can_write(priv, M_CAN_TXEFA, (TXEFA_EFAI_MASK &
890 (fgi << TXEFA_EFAI_SHIFT)));
893 stats->tx_bytes += can_get_echo_skb(dev, msg_mark);
898 static irqreturn_t m_can_isr(int irq, void *dev_id)
900 struct net_device *dev = (struct net_device *)dev_id;
901 struct m_can_priv *priv = netdev_priv(dev);
902 struct net_device_stats *stats = &dev->stats;
905 ir = m_can_read(priv, M_CAN_IR);
911 m_can_write(priv, M_CAN_IR, ir);
913 /* schedule NAPI in case of
916 * - bus error IRQ and bus error reporting
918 if ((ir & IR_RF0N) || (ir & IR_ERR_ALL_30X)) {
919 priv->irqstatus = ir;
920 m_can_disable_all_interrupts(priv);
921 napi_schedule(&priv->napi);
924 if (priv->version == 30) {
926 /* Transmission Complete Interrupt*/
927 stats->tx_bytes += can_get_echo_skb(dev, 0);
929 can_led_event(dev, CAN_LED_EVENT_TX);
930 netif_wake_queue(dev);
934 /* New TX FIFO Element arrived */
935 m_can_echo_tx_event(dev);
936 can_led_event(dev, CAN_LED_EVENT_TX);
937 if (netif_queue_stopped(dev) &&
938 !m_can_tx_fifo_full(priv))
939 netif_wake_queue(dev);
946 static const struct can_bittiming_const m_can_bittiming_const_30X = {
947 .name = KBUILD_MODNAME,
948 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
950 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
958 static const struct can_bittiming_const m_can_data_bittiming_const_30X = {
959 .name = KBUILD_MODNAME,
960 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
962 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
970 static const struct can_bittiming_const m_can_bittiming_const_31X = {
971 .name = KBUILD_MODNAME,
972 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
974 .tseg2_min = 2, /* Time segment 2 = phase_seg2 */
982 static const struct can_bittiming_const m_can_data_bittiming_const_31X = {
983 .name = KBUILD_MODNAME,
984 .tseg1_min = 1, /* Time segment 1 = prop_seg + phase_seg1 */
986 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
994 static int m_can_set_bittiming(struct net_device *dev)
996 struct m_can_priv *priv = netdev_priv(dev);
997 const struct can_bittiming *bt = &priv->can.bittiming;
998 const struct can_bittiming *dbt = &priv->can.data_bittiming;
999 u16 brp, sjw, tseg1, tseg2;
1004 tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
1005 tseg2 = bt->phase_seg2 - 1;
1006 reg_btp = (brp << NBTP_NBRP_SHIFT) | (sjw << NBTP_NSJW_SHIFT) |
1007 (tseg1 << NBTP_NTSEG1_SHIFT) | (tseg2 << NBTP_NTSEG2_SHIFT);
1008 m_can_write(priv, M_CAN_NBTP, reg_btp);
1010 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
1014 tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1;
1015 tseg2 = dbt->phase_seg2 - 1;
1017 /* TDC is only needed for bitrates beyond 2.5 MBit/s.
1018 * This is mentioned in the "Bit Time Requirements for CAN FD"
1019 * paper presented at the International CAN Conference 2013
1021 if (dbt->bitrate > 2500000) {
1024 /* Use the same value of secondary sampling point
1025 * as the data sampling point
1027 ssp = dbt->sample_point;
1029 /* Equation based on Bosch's M_CAN User Manual's
1030 * Transmitter Delay Compensation Section
1032 tdco = (priv->can.clock.freq / 1000) *
1035 /* Max valid TDCO value is 127 */
1037 netdev_warn(dev, "TDCO value of %u is beyond maximum. Using maximum possible value\n",
1042 reg_btp |= DBTP_TDC;
1043 m_can_write(priv, M_CAN_TDCR,
1044 tdco << TDCR_TDCO_SHIFT);
1047 reg_btp |= (brp << DBTP_DBRP_SHIFT) |
1048 (sjw << DBTP_DSJW_SHIFT) |
1049 (tseg1 << DBTP_DTSEG1_SHIFT) |
1050 (tseg2 << DBTP_DTSEG2_SHIFT);
1052 m_can_write(priv, M_CAN_DBTP, reg_btp);
1058 /* Configure M_CAN chip:
1059 * - set rx buffer/fifo element size
1060 * - configure rx fifo
1061 * - accept non-matching frame into fifo 0
1062 * - configure tx buffer
1063 * - >= v3.1.x: TX FIFO is used
1067 static void m_can_chip_config(struct net_device *dev)
1069 struct m_can_priv *priv = netdev_priv(dev);
1072 m_can_config_endisable(priv, true);
1074 /* RX Buffer/FIFO Element Size 64 bytes data field */
1075 m_can_write(priv, M_CAN_RXESC, M_CAN_RXESC_64BYTES);
1077 /* Accept Non-matching Frames Into FIFO 0 */
1078 m_can_write(priv, M_CAN_GFC, 0x0);
1080 if (priv->version == 30) {
1081 /* only support one Tx Buffer currently */
1082 m_can_write(priv, M_CAN_TXBC, (1 << TXBC_NDTB_SHIFT) |
1083 priv->mcfg[MRAM_TXB].off);
1085 /* TX FIFO is used for newer IP Core versions */
1086 m_can_write(priv, M_CAN_TXBC,
1087 (priv->mcfg[MRAM_TXB].num << TXBC_TFQS_SHIFT) |
1088 (priv->mcfg[MRAM_TXB].off));
1091 /* support 64 bytes payload */
1092 m_can_write(priv, M_CAN_TXESC, TXESC_TBDS_64BYTES);
1095 if (priv->version == 30) {
1096 m_can_write(priv, M_CAN_TXEFC, (1 << TXEFC_EFS_SHIFT) |
1097 priv->mcfg[MRAM_TXE].off);
1099 /* Full TX Event FIFO is used */
1100 m_can_write(priv, M_CAN_TXEFC,
1101 ((priv->mcfg[MRAM_TXE].num << TXEFC_EFS_SHIFT)
1103 priv->mcfg[MRAM_TXE].off);
1106 /* rx fifo configuration, blocking mode, fifo size 1 */
1107 m_can_write(priv, M_CAN_RXF0C,
1108 (priv->mcfg[MRAM_RXF0].num << RXFC_FS_SHIFT) |
1109 priv->mcfg[MRAM_RXF0].off);
1111 m_can_write(priv, M_CAN_RXF1C,
1112 (priv->mcfg[MRAM_RXF1].num << RXFC_FS_SHIFT) |
1113 priv->mcfg[MRAM_RXF1].off);
1115 cccr = m_can_read(priv, M_CAN_CCCR);
1116 test = m_can_read(priv, M_CAN_TEST);
1118 if (priv->version == 30) {
1121 cccr &= ~(CCCR_TEST | CCCR_MON |
1122 (CCCR_CMR_MASK << CCCR_CMR_SHIFT) |
1123 (CCCR_CME_MASK << CCCR_CME_SHIFT));
1125 if (priv->can.ctrlmode & CAN_CTRLMODE_FD)
1126 cccr |= CCCR_CME_CANFD_BRS << CCCR_CME_SHIFT;
1129 /* Version 3.1.x or 3.2.x */
1130 cccr &= ~(CCCR_TEST | CCCR_MON | CCCR_BRSE | CCCR_FDOE |
1133 /* Only 3.2.x has NISO Bit implemented */
1134 if (priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
1137 if (priv->can.ctrlmode & CAN_CTRLMODE_FD)
1138 cccr |= (CCCR_BRSE | CCCR_FDOE);
1142 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
1143 cccr |= CCCR_TEST | CCCR_MON;
1147 /* Enable Monitoring (all versions) */
1148 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1152 m_can_write(priv, M_CAN_CCCR, cccr);
1153 m_can_write(priv, M_CAN_TEST, test);
1155 /* Enable interrupts */
1156 m_can_write(priv, M_CAN_IR, IR_ALL_INT);
1157 if (!(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
1158 if (priv->version == 30)
1159 m_can_write(priv, M_CAN_IE, IR_ALL_INT &
1162 m_can_write(priv, M_CAN_IE, IR_ALL_INT &
1165 m_can_write(priv, M_CAN_IE, IR_ALL_INT);
1167 /* route all interrupts to INT0 */
1168 m_can_write(priv, M_CAN_ILS, ILS_ALL_INT0);
1170 /* set bittiming params */
1171 m_can_set_bittiming(dev);
1173 m_can_config_endisable(priv, false);
1176 static void m_can_start(struct net_device *dev)
1178 struct m_can_priv *priv = netdev_priv(dev);
1180 /* basic m_can configuration */
1181 m_can_chip_config(dev);
1183 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1185 m_can_enable_all_interrupts(priv);
1188 static int m_can_set_mode(struct net_device *dev, enum can_mode mode)
1191 case CAN_MODE_START:
1193 netif_wake_queue(dev);
1202 /* Checks core release number of M_CAN
1203 * returns 0 if an unsupported device is detected
1204 * else it returns the release and step coded as:
1205 * return value = 10 * <release> + 1 * <step>
1207 static int m_can_check_core_release(void __iomem *m_can_base)
1213 struct m_can_priv temp_priv = {
1217 /* Read Core Release Version and split into version number
1218 * Example: Version 3.2.1 => rel = 3; step = 2; substep = 1;
1220 crel_reg = m_can_read(&temp_priv, M_CAN_CREL);
1221 rel = (u8)((crel_reg & CREL_REL_MASK) >> CREL_REL_SHIFT);
1222 step = (u8)((crel_reg & CREL_STEP_MASK) >> CREL_STEP_SHIFT);
1225 /* M_CAN v3.x.y: create return value */
1228 /* Unsupported M_CAN version */
1235 /* Selectable Non ISO support only in version 3.2.x
1236 * This function checks if the bit is writable.
1238 static bool m_can_niso_supported(const struct m_can_priv *priv)
1240 u32 cccr_reg, cccr_poll;
1243 m_can_config_endisable(priv, true);
1244 cccr_reg = m_can_read(priv, M_CAN_CCCR);
1245 cccr_reg |= CCCR_NISO;
1246 m_can_write(priv, M_CAN_CCCR, cccr_reg);
1248 niso_timeout = readl_poll_timeout((priv->base + M_CAN_CCCR), cccr_poll,
1249 (cccr_poll == cccr_reg), 0, 10);
1252 cccr_reg &= ~(CCCR_NISO);
1253 m_can_write(priv, M_CAN_CCCR, cccr_reg);
1255 m_can_config_endisable(priv, false);
1257 /* return false if time out (-ETIMEDOUT), else return true */
1258 return !niso_timeout;
1261 static int m_can_dev_setup(struct platform_device *pdev, struct net_device *dev,
1264 struct m_can_priv *priv;
1267 m_can_version = m_can_check_core_release(addr);
1268 /* return if unsupported version */
1269 if (!m_can_version) {
1270 dev_err(&pdev->dev, "Unsupported version number: %2d",
1275 priv = netdev_priv(dev);
1276 netif_napi_add(dev, &priv->napi, m_can_poll, M_CAN_NAPI_WEIGHT);
1278 /* Shared properties of all M_CAN versions */
1279 priv->version = m_can_version;
1282 priv->can.do_set_mode = m_can_set_mode;
1283 priv->can.do_get_berr_counter = m_can_get_berr_counter;
1285 /* Set M_CAN supported operations */
1286 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1287 CAN_CTRLMODE_LISTENONLY |
1288 CAN_CTRLMODE_BERR_REPORTING |
1291 /* Set properties depending on M_CAN version */
1292 switch (priv->version) {
1294 /* CAN_CTRLMODE_FD_NON_ISO is fixed with M_CAN IP v3.0.x */
1295 can_set_static_ctrlmode(dev, CAN_CTRLMODE_FD_NON_ISO);
1296 priv->can.bittiming_const = &m_can_bittiming_const_30X;
1297 priv->can.data_bittiming_const =
1298 &m_can_data_bittiming_const_30X;
1301 /* CAN_CTRLMODE_FD_NON_ISO is fixed with M_CAN IP v3.1.x */
1302 can_set_static_ctrlmode(dev, CAN_CTRLMODE_FD_NON_ISO);
1303 priv->can.bittiming_const = &m_can_bittiming_const_31X;
1304 priv->can.data_bittiming_const =
1305 &m_can_data_bittiming_const_31X;
1308 priv->can.bittiming_const = &m_can_bittiming_const_31X;
1309 priv->can.data_bittiming_const =
1310 &m_can_data_bittiming_const_31X;
1311 priv->can.ctrlmode_supported |= (m_can_niso_supported(priv)
1312 ? CAN_CTRLMODE_FD_NON_ISO
1316 dev_err(&pdev->dev, "Unsupported version number: %2d",
1324 static int m_can_open(struct net_device *dev)
1326 struct m_can_priv *priv = netdev_priv(dev);
1329 err = m_can_clk_start(priv);
1333 /* open the can device */
1334 err = open_candev(dev);
1336 netdev_err(dev, "failed to open can device\n");
1337 goto exit_disable_clks;
1340 /* register interrupt handler */
1341 err = request_irq(dev->irq, m_can_isr, IRQF_SHARED, dev->name,
1344 netdev_err(dev, "failed to request interrupt\n");
1348 /* start the m_can controller */
1351 can_led_event(dev, CAN_LED_EVENT_OPEN);
1352 napi_enable(&priv->napi);
1353 netif_start_queue(dev);
1360 m_can_clk_stop(priv);
1364 static void m_can_stop(struct net_device *dev)
1366 struct m_can_priv *priv = netdev_priv(dev);
1368 /* disable all interrupts */
1369 m_can_disable_all_interrupts(priv);
1371 /* set the state as STOPPED */
1372 priv->can.state = CAN_STATE_STOPPED;
1375 static int m_can_close(struct net_device *dev)
1377 struct m_can_priv *priv = netdev_priv(dev);
1379 netif_stop_queue(dev);
1380 napi_disable(&priv->napi);
1382 m_can_clk_stop(priv);
1383 free_irq(dev->irq, dev);
1385 can_led_event(dev, CAN_LED_EVENT_STOP);
1390 static int m_can_next_echo_skb_occupied(struct net_device *dev, int putidx)
1392 struct m_can_priv *priv = netdev_priv(dev);
1393 /*get wrap around for loopback skb index */
1394 unsigned int wrap = priv->can.echo_skb_max;
1397 /* calculate next index */
1398 next_idx = (++putidx >= wrap ? 0 : putidx);
1400 /* check if occupied */
1401 return !!priv->can.echo_skb[next_idx];
1404 static netdev_tx_t m_can_start_xmit(struct sk_buff *skb,
1405 struct net_device *dev)
1407 struct m_can_priv *priv = netdev_priv(dev);
1408 struct canfd_frame *cf = (struct canfd_frame *)skb->data;
1409 u32 id, cccr, fdflags;
1413 if (can_dropped_invalid_skb(dev, skb))
1414 return NETDEV_TX_OK;
1416 /* Generate ID field for TX buffer Element */
1417 /* Common to all supported M_CAN versions */
1418 if (cf->can_id & CAN_EFF_FLAG) {
1419 id = cf->can_id & CAN_EFF_MASK;
1422 id = ((cf->can_id & CAN_SFF_MASK) << 18);
1425 if (cf->can_id & CAN_RTR_FLAG)
1428 if (priv->version == 30) {
1429 netif_stop_queue(dev);
1431 /* message ram configuration */
1432 m_can_fifo_write(priv, 0, M_CAN_FIFO_ID, id);
1433 m_can_fifo_write(priv, 0, M_CAN_FIFO_DLC,
1434 can_len2dlc(cf->len) << 16);
1436 for (i = 0; i < cf->len; i += 4)
1437 m_can_fifo_write(priv, 0,
1438 M_CAN_FIFO_DATA(i / 4),
1439 *(u32 *)(cf->data + i));
1441 can_put_echo_skb(skb, dev, 0);
1443 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
1444 cccr = m_can_read(priv, M_CAN_CCCR);
1445 cccr &= ~(CCCR_CMR_MASK << CCCR_CMR_SHIFT);
1446 if (can_is_canfd_skb(skb)) {
1447 if (cf->flags & CANFD_BRS)
1448 cccr |= CCCR_CMR_CANFD_BRS <<
1451 cccr |= CCCR_CMR_CANFD <<
1454 cccr |= CCCR_CMR_CAN << CCCR_CMR_SHIFT;
1456 m_can_write(priv, M_CAN_CCCR, cccr);
1458 m_can_write(priv, M_CAN_TXBTIE, 0x1);
1459 m_can_write(priv, M_CAN_TXBAR, 0x1);
1460 /* End of xmit function for version 3.0.x */
1462 /* Transmit routine for version >= v3.1.x */
1464 /* Check if FIFO full */
1465 if (m_can_tx_fifo_full(priv)) {
1466 /* This shouldn't happen */
1467 netif_stop_queue(dev);
1469 "TX queue active although FIFO is full.");
1470 return NETDEV_TX_BUSY;
1473 /* get put index for frame */
1474 putidx = ((m_can_read(priv, M_CAN_TXFQS) & TXFQS_TFQPI_MASK)
1475 >> TXFQS_TFQPI_SHIFT);
1476 /* Write ID Field to FIFO Element */
1477 m_can_fifo_write(priv, putidx, M_CAN_FIFO_ID, id);
1479 /* get CAN FD configuration of frame */
1481 if (can_is_canfd_skb(skb)) {
1482 fdflags |= TX_BUF_FDF;
1483 if (cf->flags & CANFD_BRS)
1484 fdflags |= TX_BUF_BRS;
1487 /* Construct DLC Field. Also contains CAN-FD configuration
1488 * use put index of fifo as message marker
1489 * it is used in TX interrupt for
1490 * sending the correct echo frame
1492 m_can_fifo_write(priv, putidx, M_CAN_FIFO_DLC,
1493 ((putidx << TX_BUF_MM_SHIFT) &
1495 (can_len2dlc(cf->len) << 16) |
1496 fdflags | TX_BUF_EFC);
1498 for (i = 0; i < cf->len; i += 4)
1499 m_can_fifo_write(priv, putidx, M_CAN_FIFO_DATA(i / 4),
1500 *(u32 *)(cf->data + i));
1502 /* Push loopback echo.
1503 * Will be looped back on TX interrupt based on message marker
1505 can_put_echo_skb(skb, dev, putidx);
1507 /* Enable TX FIFO element to start transfer */
1508 m_can_write(priv, M_CAN_TXBAR, (1 << putidx));
1510 /* stop network queue if fifo full */
1511 if (m_can_tx_fifo_full(priv) ||
1512 m_can_next_echo_skb_occupied(dev, putidx))
1513 netif_stop_queue(dev);
1516 return NETDEV_TX_OK;
1519 static const struct net_device_ops m_can_netdev_ops = {
1520 .ndo_open = m_can_open,
1521 .ndo_stop = m_can_close,
1522 .ndo_start_xmit = m_can_start_xmit,
1523 .ndo_change_mtu = can_change_mtu,
1526 static int register_m_can_dev(struct net_device *dev)
1528 dev->flags |= IFF_ECHO; /* we support local echo */
1529 dev->netdev_ops = &m_can_netdev_ops;
1531 return register_candev(dev);
1534 static void m_can_init_ram(struct m_can_priv *priv)
1538 /* initialize the entire Message RAM in use to avoid possible
1539 * ECC/parity checksum errors when reading an uninitialized buffer
1541 start = priv->mcfg[MRAM_SIDF].off;
1542 end = priv->mcfg[MRAM_TXB].off +
1543 priv->mcfg[MRAM_TXB].num * TXB_ELEMENT_SIZE;
1544 for (i = start; i < end; i += 4)
1545 writel(0x0, priv->mram_base + i);
1548 static void m_can_of_parse_mram(struct m_can_priv *priv,
1549 const u32 *mram_config_vals)
1551 priv->mcfg[MRAM_SIDF].off = mram_config_vals[0];
1552 priv->mcfg[MRAM_SIDF].num = mram_config_vals[1];
1553 priv->mcfg[MRAM_XIDF].off = priv->mcfg[MRAM_SIDF].off +
1554 priv->mcfg[MRAM_SIDF].num * SIDF_ELEMENT_SIZE;
1555 priv->mcfg[MRAM_XIDF].num = mram_config_vals[2];
1556 priv->mcfg[MRAM_RXF0].off = priv->mcfg[MRAM_XIDF].off +
1557 priv->mcfg[MRAM_XIDF].num * XIDF_ELEMENT_SIZE;
1558 priv->mcfg[MRAM_RXF0].num = mram_config_vals[3] &
1559 (RXFC_FS_MASK >> RXFC_FS_SHIFT);
1560 priv->mcfg[MRAM_RXF1].off = priv->mcfg[MRAM_RXF0].off +
1561 priv->mcfg[MRAM_RXF0].num * RXF0_ELEMENT_SIZE;
1562 priv->mcfg[MRAM_RXF1].num = mram_config_vals[4] &
1563 (RXFC_FS_MASK >> RXFC_FS_SHIFT);
1564 priv->mcfg[MRAM_RXB].off = priv->mcfg[MRAM_RXF1].off +
1565 priv->mcfg[MRAM_RXF1].num * RXF1_ELEMENT_SIZE;
1566 priv->mcfg[MRAM_RXB].num = mram_config_vals[5];
1567 priv->mcfg[MRAM_TXE].off = priv->mcfg[MRAM_RXB].off +
1568 priv->mcfg[MRAM_RXB].num * RXB_ELEMENT_SIZE;
1569 priv->mcfg[MRAM_TXE].num = mram_config_vals[6];
1570 priv->mcfg[MRAM_TXB].off = priv->mcfg[MRAM_TXE].off +
1571 priv->mcfg[MRAM_TXE].num * TXE_ELEMENT_SIZE;
1572 priv->mcfg[MRAM_TXB].num = mram_config_vals[7] &
1573 (TXBC_NDTB_MASK >> TXBC_NDTB_SHIFT);
1575 dev_dbg(priv->device,
1576 "mram_base %p sidf 0x%x %d xidf 0x%x %d rxf0 0x%x %d rxf1 0x%x %d rxb 0x%x %d txe 0x%x %d txb 0x%x %d\n",
1578 priv->mcfg[MRAM_SIDF].off, priv->mcfg[MRAM_SIDF].num,
1579 priv->mcfg[MRAM_XIDF].off, priv->mcfg[MRAM_XIDF].num,
1580 priv->mcfg[MRAM_RXF0].off, priv->mcfg[MRAM_RXF0].num,
1581 priv->mcfg[MRAM_RXF1].off, priv->mcfg[MRAM_RXF1].num,
1582 priv->mcfg[MRAM_RXB].off, priv->mcfg[MRAM_RXB].num,
1583 priv->mcfg[MRAM_TXE].off, priv->mcfg[MRAM_TXE].num,
1584 priv->mcfg[MRAM_TXB].off, priv->mcfg[MRAM_TXB].num);
1586 m_can_init_ram(priv);
1589 static int m_can_plat_probe(struct platform_device *pdev)
1591 struct net_device *dev;
1592 struct m_can_priv *priv;
1593 struct resource *res;
1595 void __iomem *mram_addr;
1596 struct clk *hclk, *cclk;
1598 struct device_node *np;
1599 u32 mram_config_vals[MRAM_CFG_LEN];
1602 np = pdev->dev.of_node;
1604 hclk = devm_clk_get(&pdev->dev, "hclk");
1605 cclk = devm_clk_get(&pdev->dev, "cclk");
1607 if (IS_ERR(hclk) || IS_ERR(cclk)) {
1608 dev_err(&pdev->dev, "no clock found\n");
1613 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "m_can");
1614 addr = devm_ioremap_resource(&pdev->dev, res);
1615 irq = platform_get_irq_byname(pdev, "int0");
1617 if (IS_ERR(addr) || irq < 0) {
1622 /* message ram could be shared */
1623 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "message_ram");
1629 mram_addr = devm_ioremap(&pdev->dev, res->start, resource_size(res));
1635 /* get message ram configuration */
1636 ret = of_property_read_u32_array(np, "bosch,mram-cfg",
1638 sizeof(mram_config_vals) / 4);
1640 dev_err(&pdev->dev, "Could not get Message RAM configuration.");
1645 * Defines the total amount of echo buffers for loopback
1647 tx_fifo_size = mram_config_vals[7];
1649 /* allocate the m_can device */
1650 dev = alloc_candev(sizeof(*priv), tx_fifo_size);
1656 priv = netdev_priv(dev);
1658 priv->device = &pdev->dev;
1661 priv->can.clock.freq = clk_get_rate(cclk);
1662 priv->mram_base = mram_addr;
1664 platform_set_drvdata(pdev, dev);
1665 SET_NETDEV_DEV(dev, &pdev->dev);
1667 /* Enable clocks. Necessary to read Core Release in order to determine
1670 pm_runtime_enable(&pdev->dev);
1671 ret = m_can_clk_start(priv);
1673 goto pm_runtime_fail;
1675 ret = m_can_dev_setup(pdev, dev, addr);
1679 ret = register_m_can_dev(dev);
1681 dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
1682 KBUILD_MODNAME, ret);
1686 m_can_of_parse_mram(priv, mram_config_vals);
1688 devm_can_led_init(dev);
1690 of_can_transceiver(dev);
1692 dev_info(&pdev->dev, "%s device registered (irq=%d, version=%d)\n",
1693 KBUILD_MODNAME, dev->irq, priv->version);
1696 * Stop clocks. They will be reactivated once the M_CAN device is opened
1699 m_can_clk_stop(priv);
1702 pm_runtime_disable(&pdev->dev);
1709 static __maybe_unused int m_can_suspend(struct device *dev)
1711 struct net_device *ndev = dev_get_drvdata(dev);
1712 struct m_can_priv *priv = netdev_priv(ndev);
1714 if (netif_running(ndev)) {
1715 netif_stop_queue(ndev);
1716 netif_device_detach(ndev);
1718 m_can_clk_stop(priv);
1721 pinctrl_pm_select_sleep_state(dev);
1723 priv->can.state = CAN_STATE_SLEEPING;
1728 static __maybe_unused int m_can_resume(struct device *dev)
1730 struct net_device *ndev = dev_get_drvdata(dev);
1731 struct m_can_priv *priv = netdev_priv(ndev);
1733 pinctrl_pm_select_default_state(dev);
1735 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1737 if (netif_running(ndev)) {
1740 ret = m_can_clk_start(priv);
1744 m_can_init_ram(priv);
1746 netif_device_attach(ndev);
1747 netif_start_queue(ndev);
1753 static void unregister_m_can_dev(struct net_device *dev)
1755 unregister_candev(dev);
1758 static int m_can_plat_remove(struct platform_device *pdev)
1760 struct net_device *dev = platform_get_drvdata(pdev);
1762 unregister_m_can_dev(dev);
1764 pm_runtime_disable(&pdev->dev);
1766 platform_set_drvdata(pdev, NULL);
1773 static int __maybe_unused m_can_runtime_suspend(struct device *dev)
1775 struct net_device *ndev = dev_get_drvdata(dev);
1776 struct m_can_priv *priv = netdev_priv(ndev);
1778 clk_disable_unprepare(priv->cclk);
1779 clk_disable_unprepare(priv->hclk);
1784 static int __maybe_unused m_can_runtime_resume(struct device *dev)
1786 struct net_device *ndev = dev_get_drvdata(dev);
1787 struct m_can_priv *priv = netdev_priv(ndev);
1790 err = clk_prepare_enable(priv->hclk);
1794 err = clk_prepare_enable(priv->cclk);
1796 clk_disable_unprepare(priv->hclk);
1801 static const struct dev_pm_ops m_can_pmops = {
1802 SET_RUNTIME_PM_OPS(m_can_runtime_suspend,
1803 m_can_runtime_resume, NULL)
1804 SET_SYSTEM_SLEEP_PM_OPS(m_can_suspend, m_can_resume)
1807 static const struct of_device_id m_can_of_table[] = {
1808 { .compatible = "bosch,m_can", .data = NULL },
1811 MODULE_DEVICE_TABLE(of, m_can_of_table);
1813 static struct platform_driver m_can_plat_driver = {
1815 .name = KBUILD_MODNAME,
1816 .of_match_table = m_can_of_table,
1819 .probe = m_can_plat_probe,
1820 .remove = m_can_plat_remove,
1823 module_platform_driver(m_can_plat_driver);
1825 MODULE_AUTHOR("Dong Aisheng <b29396@freescale.com>");
1826 MODULE_LICENSE("GPL v2");
1827 MODULE_DESCRIPTION("CAN bus driver for Bosch M_CAN controller");