2 * CAN bus driver for Bosch M_CAN controller
4 * Copyright (C) 2014 Freescale Semiconductor, Inc.
5 * Dong Aisheng <b29396@freescale.com>
7 * Bosch M_CAN user manual can be obtained from:
8 * http://www.bosch-semiconductors.de/media/pdf_1/ipmodules_1/m_can/
9 * mcan_users_manual_v302.pdf
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/netdevice.h>
24 #include <linux/of_device.h>
25 #include <linux/platform_device.h>
27 #include <linux/can/dev.h>
30 #define M_CAN_NAPI_WEIGHT 64
32 /* message ram configuration data length */
33 #define MRAM_CFG_LEN 8
35 /* registers definition */
85 /* m_can lec values */
108 /* Test Register (TEST) */
109 #define TEST_LBCK BIT(4)
111 /* CC Control Register(CCCR) */
112 #define CCCR_TEST BIT(7)
113 #define CCCR_MON BIT(5)
114 #define CCCR_CCE BIT(1)
115 #define CCCR_INIT BIT(0)
117 /* Bit Timing & Prescaler Register (BTP) */
118 #define BTR_BRP_MASK 0x3ff
119 #define BTR_BRP_SHIFT 16
120 #define BTR_TSEG1_SHIFT 8
121 #define BTR_TSEG1_MASK (0x3f << BTR_TSEG1_SHIFT)
122 #define BTR_TSEG2_SHIFT 4
123 #define BTR_TSEG2_MASK (0xf << BTR_TSEG2_SHIFT)
124 #define BTR_SJW_SHIFT 0
125 #define BTR_SJW_MASK 0xf
127 /* Error Counter Register(ECR) */
128 #define ECR_RP BIT(15)
129 #define ECR_REC_SHIFT 8
130 #define ECR_REC_MASK (0x7f << ECR_REC_SHIFT)
131 #define ECR_TEC_SHIFT 0
132 #define ECR_TEC_MASK 0xff
134 /* Protocol Status Register(PSR) */
135 #define PSR_BO BIT(7)
136 #define PSR_EW BIT(6)
137 #define PSR_EP BIT(5)
138 #define PSR_LEC_MASK 0x7
140 /* Interrupt Register(IR) */
141 #define IR_ALL_INT 0xffffffff
142 #define IR_STE BIT(31)
143 #define IR_FOE BIT(30)
144 #define IR_ACKE BIT(29)
145 #define IR_BE BIT(28)
146 #define IR_CRCE BIT(27)
147 #define IR_WDI BIT(26)
148 #define IR_BO BIT(25)
149 #define IR_EW BIT(24)
150 #define IR_EP BIT(23)
151 #define IR_ELO BIT(22)
152 #define IR_BEU BIT(21)
153 #define IR_BEC BIT(20)
154 #define IR_DRX BIT(19)
155 #define IR_TOO BIT(18)
156 #define IR_MRAF BIT(17)
157 #define IR_TSW BIT(16)
158 #define IR_TEFL BIT(15)
159 #define IR_TEFF BIT(14)
160 #define IR_TEFW BIT(13)
161 #define IR_TEFN BIT(12)
162 #define IR_TFE BIT(11)
163 #define IR_TCF BIT(10)
165 #define IR_HPM BIT(8)
166 #define IR_RF1L BIT(7)
167 #define IR_RF1F BIT(6)
168 #define IR_RF1W BIT(5)
169 #define IR_RF1N BIT(4)
170 #define IR_RF0L BIT(3)
171 #define IR_RF0F BIT(2)
172 #define IR_RF0W BIT(1)
173 #define IR_RF0N BIT(0)
174 #define IR_ERR_STATE (IR_BO | IR_EW | IR_EP)
175 #define IR_ERR_LEC (IR_STE | IR_FOE | IR_ACKE | IR_BE | IR_CRCE)
176 #define IR_ERR_BUS (IR_ERR_LEC | IR_WDI | IR_ELO | IR_BEU | \
177 IR_BEC | IR_TOO | IR_MRAF | IR_TSW | IR_TEFL | \
179 #define IR_ERR_ALL (IR_ERR_STATE | IR_ERR_BUS)
181 /* Interrupt Line Select (ILS) */
182 #define ILS_ALL_INT0 0x0
183 #define ILS_ALL_INT1 0xFFFFFFFF
185 /* Interrupt Line Enable (ILE) */
186 #define ILE_EINT0 BIT(0)
187 #define ILE_EINT1 BIT(1)
189 /* Rx FIFO 0/1 Configuration (RXF0C/RXF1C) */
190 #define RXFC_FWM_OFF 24
191 #define RXFC_FWM_MASK 0x7f
192 #define RXFC_FWM_1 (1 << RXFC_FWM_OFF)
193 #define RXFC_FS_OFF 16
194 #define RXFC_FS_MASK 0x7f
196 /* Rx FIFO 0/1 Status (RXF0S/RXF1S) */
197 #define RXFS_RFL BIT(25)
198 #define RXFS_FF BIT(24)
199 #define RXFS_FPI_OFF 16
200 #define RXFS_FPI_MASK 0x3f0000
201 #define RXFS_FGI_OFF 8
202 #define RXFS_FGI_MASK 0x3f00
203 #define RXFS_FFL_MASK 0x7f
205 /* Rx Buffer / FIFO Element Size Configuration (RXESC) */
206 #define M_CAN_RXESC_8BYTES 0x0
208 /* Tx Buffer Configuration(TXBC) */
209 #define TXBC_NDTB_OFF 16
210 #define TXBC_NDTB_MASK 0x3f
212 /* Tx Buffer Element Size Configuration(TXESC) */
213 #define TXESC_TBDS_8BYTES 0x0
215 /* Tx Event FIFO Con.guration (TXEFC) */
216 #define TXEFC_EFS_OFF 16
217 #define TXEFC_EFS_MASK 0x3f
219 /* Message RAM Configuration (in bytes) */
220 #define SIDF_ELEMENT_SIZE 4
221 #define XIDF_ELEMENT_SIZE 8
222 #define RXF0_ELEMENT_SIZE 16
223 #define RXF1_ELEMENT_SIZE 16
224 #define RXB_ELEMENT_SIZE 16
225 #define TXE_ELEMENT_SIZE 8
226 #define TXB_ELEMENT_SIZE 16
228 /* Message RAM Elements */
229 #define M_CAN_FIFO_ID 0x0
230 #define M_CAN_FIFO_DLC 0x4
231 #define M_CAN_FIFO_DATA(n) (0x8 + ((n) << 2))
233 /* Rx Buffer Element */
234 #define RX_BUF_ESI BIT(31)
235 #define RX_BUF_XTD BIT(30)
236 #define RX_BUF_RTR BIT(29)
238 /* Tx Buffer Element */
239 #define TX_BUF_XTD BIT(30)
240 #define TX_BUF_RTR BIT(29)
242 /* address offset and element number for each FIFO/Buffer in the Message RAM */
248 /* m_can private data structure */
250 struct can_priv can; /* must be the first member */
251 struct napi_struct napi;
252 struct net_device *dev;
253 struct device *device;
259 /* message ram configuration */
260 void __iomem *mram_base;
261 struct mram_cfg mcfg[MRAM_CFG_NUM];
264 static inline u32 m_can_read(const struct m_can_priv *priv, enum m_can_reg reg)
266 return readl(priv->base + reg);
269 static inline void m_can_write(const struct m_can_priv *priv,
270 enum m_can_reg reg, u32 val)
272 writel(val, priv->base + reg);
275 static inline u32 m_can_fifo_read(const struct m_can_priv *priv,
276 u32 fgi, unsigned int offset)
278 return readl(priv->mram_base + priv->mcfg[MRAM_RXF0].off +
279 fgi * RXF0_ELEMENT_SIZE + offset);
282 static inline void m_can_fifo_write(const struct m_can_priv *priv,
283 u32 fpi, unsigned int offset, u32 val)
285 return writel(val, priv->mram_base + priv->mcfg[MRAM_TXB].off +
286 fpi * TXB_ELEMENT_SIZE + offset);
289 static inline void m_can_config_endisable(const struct m_can_priv *priv,
292 u32 cccr = m_can_read(priv, M_CAN_CCCR);
297 /* enable m_can configuration */
298 m_can_write(priv, M_CAN_CCCR, cccr | CCCR_INIT);
299 /* CCCR.CCE can only be set/reset while CCCR.INIT = '1' */
300 m_can_write(priv, M_CAN_CCCR, cccr | CCCR_INIT | CCCR_CCE);
302 m_can_write(priv, M_CAN_CCCR, cccr & ~(CCCR_INIT | CCCR_CCE));
305 /* there's a delay for module initialization */
307 val = CCCR_INIT | CCCR_CCE;
309 while ((m_can_read(priv, M_CAN_CCCR) & (CCCR_INIT | CCCR_CCE)) != val) {
311 netdev_warn(priv->dev, "Failed to init module\n");
319 static inline void m_can_enable_all_interrupts(const struct m_can_priv *priv)
321 m_can_write(priv, M_CAN_ILE, ILE_EINT0 | ILE_EINT1);
324 static inline void m_can_disable_all_interrupts(const struct m_can_priv *priv)
326 m_can_write(priv, M_CAN_ILE, 0x0);
329 static void m_can_read_fifo(const struct net_device *dev, struct can_frame *cf,
332 struct m_can_priv *priv = netdev_priv(dev);
335 /* calculate the fifo get index for where to read data */
336 fgi = (rxfs & RXFS_FGI_MASK) >> RXFS_FGI_OFF;
337 id = m_can_fifo_read(priv, fgi, M_CAN_FIFO_ID);
339 cf->can_id = (id & CAN_EFF_MASK) | CAN_EFF_FLAG;
341 cf->can_id = (id >> 18) & CAN_SFF_MASK;
343 dlc = m_can_fifo_read(priv, fgi, M_CAN_FIFO_DLC);
344 cf->can_dlc = get_can_dlc((dlc >> 16) & 0x0F);
346 if (id & RX_BUF_RTR) {
347 cf->can_id |= CAN_RTR_FLAG;
349 *(u32 *)(cf->data + 0) = m_can_fifo_read(priv, fgi,
351 *(u32 *)(cf->data + 4) = m_can_fifo_read(priv, fgi,
355 /* acknowledge rx fifo 0 */
356 m_can_write(priv, M_CAN_RXF0A, fgi);
359 static int m_can_do_rx_poll(struct net_device *dev, int quota)
361 struct m_can_priv *priv = netdev_priv(dev);
362 struct net_device_stats *stats = &dev->stats;
364 struct can_frame *frame;
368 rxfs = m_can_read(priv, M_CAN_RXF0S);
369 if (!(rxfs & RXFS_FFL_MASK)) {
370 netdev_dbg(dev, "no messages in fifo0\n");
374 while ((rxfs & RXFS_FFL_MASK) && (quota > 0)) {
376 netdev_warn(dev, "Rx FIFO 0 Message Lost\n");
378 skb = alloc_can_skb(dev, &frame);
384 m_can_read_fifo(dev, frame, rxfs);
387 stats->rx_bytes += frame->can_dlc;
389 netif_receive_skb(skb);
393 rxfs = m_can_read(priv, M_CAN_RXF0S);
397 can_led_event(dev, CAN_LED_EVENT_RX);
402 static int m_can_handle_lost_msg(struct net_device *dev)
404 struct net_device_stats *stats = &dev->stats;
406 struct can_frame *frame;
408 netdev_err(dev, "msg lost in rxf0\n");
411 stats->rx_over_errors++;
413 skb = alloc_can_err_skb(dev, &frame);
417 frame->can_id |= CAN_ERR_CRTL;
418 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
420 netif_receive_skb(skb);
425 static int m_can_handle_lec_err(struct net_device *dev,
426 enum m_can_lec_type lec_type)
428 struct m_can_priv *priv = netdev_priv(dev);
429 struct net_device_stats *stats = &dev->stats;
430 struct can_frame *cf;
433 priv->can.can_stats.bus_error++;
436 /* propagate the error condition to the CAN stack */
437 skb = alloc_can_err_skb(dev, &cf);
441 /* check for 'last error code' which tells us the
442 * type of the last error to occur on the CAN bus
444 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
445 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
448 case LEC_STUFF_ERROR:
449 netdev_dbg(dev, "stuff error\n");
450 cf->data[2] |= CAN_ERR_PROT_STUFF;
453 netdev_dbg(dev, "form error\n");
454 cf->data[2] |= CAN_ERR_PROT_FORM;
457 netdev_dbg(dev, "ack error\n");
458 cf->data[3] |= (CAN_ERR_PROT_LOC_ACK |
459 CAN_ERR_PROT_LOC_ACK_DEL);
462 netdev_dbg(dev, "bit1 error\n");
463 cf->data[2] |= CAN_ERR_PROT_BIT1;
466 netdev_dbg(dev, "bit0 error\n");
467 cf->data[2] |= CAN_ERR_PROT_BIT0;
470 netdev_dbg(dev, "CRC error\n");
471 cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
472 CAN_ERR_PROT_LOC_CRC_DEL);
479 stats->rx_bytes += cf->can_dlc;
480 netif_receive_skb(skb);
485 static int __m_can_get_berr_counter(const struct net_device *dev,
486 struct can_berr_counter *bec)
488 struct m_can_priv *priv = netdev_priv(dev);
491 ecr = m_can_read(priv, M_CAN_ECR);
492 bec->rxerr = (ecr & ECR_REC_MASK) >> ECR_REC_SHIFT;
493 bec->txerr = ecr & ECR_TEC_MASK;
498 static int m_can_get_berr_counter(const struct net_device *dev,
499 struct can_berr_counter *bec)
501 struct m_can_priv *priv = netdev_priv(dev);
504 err = clk_prepare_enable(priv->hclk);
508 err = clk_prepare_enable(priv->cclk);
510 clk_disable_unprepare(priv->hclk);
514 __m_can_get_berr_counter(dev, bec);
516 clk_disable_unprepare(priv->cclk);
517 clk_disable_unprepare(priv->hclk);
522 static int m_can_handle_state_change(struct net_device *dev,
523 enum can_state new_state)
525 struct m_can_priv *priv = netdev_priv(dev);
526 struct net_device_stats *stats = &dev->stats;
527 struct can_frame *cf;
529 struct can_berr_counter bec;
533 case CAN_STATE_ERROR_ACTIVE:
534 /* error warning state */
535 priv->can.can_stats.error_warning++;
536 priv->can.state = CAN_STATE_ERROR_WARNING;
538 case CAN_STATE_ERROR_PASSIVE:
539 /* error passive state */
540 priv->can.can_stats.error_passive++;
541 priv->can.state = CAN_STATE_ERROR_PASSIVE;
543 case CAN_STATE_BUS_OFF:
545 priv->can.state = CAN_STATE_BUS_OFF;
546 m_can_disable_all_interrupts(priv);
553 /* propagate the error condition to the CAN stack */
554 skb = alloc_can_err_skb(dev, &cf);
558 __m_can_get_berr_counter(dev, &bec);
561 case CAN_STATE_ERROR_ACTIVE:
562 /* error warning state */
563 cf->can_id |= CAN_ERR_CRTL;
564 cf->data[1] = (bec.txerr > bec.rxerr) ?
565 CAN_ERR_CRTL_TX_WARNING :
566 CAN_ERR_CRTL_RX_WARNING;
567 cf->data[6] = bec.txerr;
568 cf->data[7] = bec.rxerr;
570 case CAN_STATE_ERROR_PASSIVE:
571 /* error passive state */
572 cf->can_id |= CAN_ERR_CRTL;
573 ecr = m_can_read(priv, M_CAN_ECR);
575 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
577 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
578 cf->data[6] = bec.txerr;
579 cf->data[7] = bec.rxerr;
581 case CAN_STATE_BUS_OFF:
583 cf->can_id |= CAN_ERR_BUSOFF;
590 stats->rx_bytes += cf->can_dlc;
591 netif_receive_skb(skb);
596 static int m_can_handle_state_errors(struct net_device *dev, u32 psr)
598 struct m_can_priv *priv = netdev_priv(dev);
601 if ((psr & PSR_EW) &&
602 (priv->can.state != CAN_STATE_ERROR_WARNING)) {
603 netdev_dbg(dev, "entered error warning state\n");
604 work_done += m_can_handle_state_change(dev,
605 CAN_STATE_ERROR_WARNING);
608 if ((psr & PSR_EP) &&
609 (priv->can.state != CAN_STATE_ERROR_PASSIVE)) {
610 netdev_dbg(dev, "entered error warning state\n");
611 work_done += m_can_handle_state_change(dev,
612 CAN_STATE_ERROR_PASSIVE);
615 if ((psr & PSR_BO) &&
616 (priv->can.state != CAN_STATE_BUS_OFF)) {
617 netdev_dbg(dev, "entered error warning state\n");
618 work_done += m_can_handle_state_change(dev,
625 static void m_can_handle_other_err(struct net_device *dev, u32 irqstatus)
627 if (irqstatus & IR_WDI)
628 netdev_err(dev, "Message RAM Watchdog event due to missing READY\n");
629 if (irqstatus & IR_BEU)
630 netdev_err(dev, "Error Logging Overflow\n");
631 if (irqstatus & IR_BEU)
632 netdev_err(dev, "Bit Error Uncorrected\n");
633 if (irqstatus & IR_BEC)
634 netdev_err(dev, "Bit Error Corrected\n");
635 if (irqstatus & IR_TOO)
636 netdev_err(dev, "Timeout reached\n");
637 if (irqstatus & IR_MRAF)
638 netdev_err(dev, "Message RAM access failure occurred\n");
641 static inline bool is_lec_err(u32 psr)
645 return psr && (psr != LEC_UNUSED);
648 static int m_can_handle_bus_errors(struct net_device *dev, u32 irqstatus,
651 struct m_can_priv *priv = netdev_priv(dev);
654 if (irqstatus & IR_RF0L)
655 work_done += m_can_handle_lost_msg(dev);
657 /* handle lec errors on the bus */
658 if ((priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
660 work_done += m_can_handle_lec_err(dev, psr & LEC_UNUSED);
662 /* other unproccessed error interrupts */
663 m_can_handle_other_err(dev, irqstatus);
668 static int m_can_poll(struct napi_struct *napi, int quota)
670 struct net_device *dev = napi->dev;
671 struct m_can_priv *priv = netdev_priv(dev);
675 irqstatus = priv->irqstatus | m_can_read(priv, M_CAN_IR);
679 psr = m_can_read(priv, M_CAN_PSR);
680 if (irqstatus & IR_ERR_STATE)
681 work_done += m_can_handle_state_errors(dev, psr);
683 if (irqstatus & IR_ERR_BUS)
684 work_done += m_can_handle_bus_errors(dev, irqstatus, psr);
686 if (irqstatus & IR_RF0N)
687 work_done += m_can_do_rx_poll(dev, (quota - work_done));
689 if (work_done < quota) {
691 m_can_enable_all_interrupts(priv);
698 static irqreturn_t m_can_isr(int irq, void *dev_id)
700 struct net_device *dev = (struct net_device *)dev_id;
701 struct m_can_priv *priv = netdev_priv(dev);
702 struct net_device_stats *stats = &dev->stats;
705 ir = m_can_read(priv, M_CAN_IR);
711 m_can_write(priv, M_CAN_IR, ir);
713 /* schedule NAPI in case of
716 * - bus error IRQ and bus error reporting
718 if ((ir & IR_RF0N) || (ir & IR_ERR_ALL)) {
719 priv->irqstatus = ir;
720 m_can_disable_all_interrupts(priv);
721 napi_schedule(&priv->napi);
724 /* transmission complete interrupt */
726 stats->tx_bytes += can_get_echo_skb(dev, 0);
728 can_led_event(dev, CAN_LED_EVENT_TX);
729 netif_wake_queue(dev);
735 static const struct can_bittiming_const m_can_bittiming_const = {
736 .name = KBUILD_MODNAME,
737 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
739 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
747 static int m_can_set_bittiming(struct net_device *dev)
749 struct m_can_priv *priv = netdev_priv(dev);
750 const struct can_bittiming *bt = &priv->can.bittiming;
751 u16 brp, sjw, tseg1, tseg2;
756 tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
757 tseg2 = bt->phase_seg2 - 1;
758 reg_btp = (brp << BTR_BRP_SHIFT) | (sjw << BTR_SJW_SHIFT) |
759 (tseg1 << BTR_TSEG1_SHIFT) | (tseg2 << BTR_TSEG2_SHIFT);
760 m_can_write(priv, M_CAN_BTP, reg_btp);
761 netdev_dbg(dev, "setting BTP 0x%x\n", reg_btp);
766 /* Configure M_CAN chip:
767 * - set rx buffer/fifo element size
768 * - configure rx fifo
769 * - accept non-matching frame into fifo 0
770 * - configure tx buffer
774 static void m_can_chip_config(struct net_device *dev)
776 struct m_can_priv *priv = netdev_priv(dev);
779 m_can_config_endisable(priv, true);
781 /* RX Buffer/FIFO Element Size 8 bytes data field */
782 m_can_write(priv, M_CAN_RXESC, M_CAN_RXESC_8BYTES);
784 /* Accept Non-matching Frames Into FIFO 0 */
785 m_can_write(priv, M_CAN_GFC, 0x0);
787 /* only support one Tx Buffer currently */
788 m_can_write(priv, M_CAN_TXBC, (1 << TXBC_NDTB_OFF) |
789 priv->mcfg[MRAM_TXB].off);
791 /* only support 8 bytes firstly */
792 m_can_write(priv, M_CAN_TXESC, TXESC_TBDS_8BYTES);
794 m_can_write(priv, M_CAN_TXEFC, (1 << TXEFC_EFS_OFF) |
795 priv->mcfg[MRAM_TXE].off);
797 /* rx fifo configuration, blocking mode, fifo size 1 */
798 m_can_write(priv, M_CAN_RXF0C,
799 (priv->mcfg[MRAM_RXF0].num << RXFC_FS_OFF) |
800 RXFC_FWM_1 | priv->mcfg[MRAM_RXF0].off);
802 m_can_write(priv, M_CAN_RXF1C,
803 (priv->mcfg[MRAM_RXF1].num << RXFC_FS_OFF) |
804 RXFC_FWM_1 | priv->mcfg[MRAM_RXF1].off);
806 cccr = m_can_read(priv, M_CAN_CCCR);
807 cccr &= ~(CCCR_TEST | CCCR_MON);
808 test = m_can_read(priv, M_CAN_TEST);
811 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
814 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
819 m_can_write(priv, M_CAN_CCCR, cccr);
820 m_can_write(priv, M_CAN_TEST, test);
822 /* enable interrupts */
823 m_can_write(priv, M_CAN_IR, IR_ALL_INT);
824 if (!(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
825 m_can_write(priv, M_CAN_IE, IR_ALL_INT & ~IR_ERR_LEC);
827 m_can_write(priv, M_CAN_IE, IR_ALL_INT);
829 /* route all interrupts to INT0 */
830 m_can_write(priv, M_CAN_ILS, ILS_ALL_INT0);
832 /* set bittiming params */
833 m_can_set_bittiming(dev);
835 m_can_config_endisable(priv, false);
838 static void m_can_start(struct net_device *dev)
840 struct m_can_priv *priv = netdev_priv(dev);
842 /* basic m_can configuration */
843 m_can_chip_config(dev);
845 priv->can.state = CAN_STATE_ERROR_ACTIVE;
847 m_can_enable_all_interrupts(priv);
850 static int m_can_set_mode(struct net_device *dev, enum can_mode mode)
855 netif_wake_queue(dev);
864 static void free_m_can_dev(struct net_device *dev)
869 static struct net_device *alloc_m_can_dev(void)
871 struct net_device *dev;
872 struct m_can_priv *priv;
874 dev = alloc_candev(sizeof(*priv), 1);
878 priv = netdev_priv(dev);
879 netif_napi_add(dev, &priv->napi, m_can_poll, M_CAN_NAPI_WEIGHT);
882 priv->can.bittiming_const = &m_can_bittiming_const;
883 priv->can.do_set_mode = m_can_set_mode;
884 priv->can.do_get_berr_counter = m_can_get_berr_counter;
885 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
886 CAN_CTRLMODE_LISTENONLY |
887 CAN_CTRLMODE_BERR_REPORTING;
892 static int m_can_open(struct net_device *dev)
894 struct m_can_priv *priv = netdev_priv(dev);
897 err = clk_prepare_enable(priv->hclk);
901 err = clk_prepare_enable(priv->cclk);
903 goto exit_disable_hclk;
905 /* open the can device */
906 err = open_candev(dev);
908 netdev_err(dev, "failed to open can device\n");
909 goto exit_disable_cclk;
912 /* register interrupt handler */
913 err = request_irq(dev->irq, m_can_isr, IRQF_SHARED, dev->name,
916 netdev_err(dev, "failed to request interrupt\n");
920 /* start the m_can controller */
923 can_led_event(dev, CAN_LED_EVENT_OPEN);
924 napi_enable(&priv->napi);
925 netif_start_queue(dev);
932 clk_disable_unprepare(priv->cclk);
934 clk_disable_unprepare(priv->hclk);
938 static void m_can_stop(struct net_device *dev)
940 struct m_can_priv *priv = netdev_priv(dev);
942 /* disable all interrupts */
943 m_can_disable_all_interrupts(priv);
945 clk_disable_unprepare(priv->hclk);
946 clk_disable_unprepare(priv->cclk);
948 /* set the state as STOPPED */
949 priv->can.state = CAN_STATE_STOPPED;
952 static int m_can_close(struct net_device *dev)
954 struct m_can_priv *priv = netdev_priv(dev);
956 netif_stop_queue(dev);
957 napi_disable(&priv->napi);
959 free_irq(dev->irq, dev);
961 can_led_event(dev, CAN_LED_EVENT_STOP);
966 static netdev_tx_t m_can_start_xmit(struct sk_buff *skb,
967 struct net_device *dev)
969 struct m_can_priv *priv = netdev_priv(dev);
970 struct can_frame *cf = (struct can_frame *)skb->data;
973 if (can_dropped_invalid_skb(dev, skb))
976 netif_stop_queue(dev);
978 if (cf->can_id & CAN_EFF_FLAG) {
979 id = cf->can_id & CAN_EFF_MASK;
982 id = ((cf->can_id & CAN_SFF_MASK) << 18);
985 if (cf->can_id & CAN_RTR_FLAG)
988 /* message ram configuration */
989 m_can_fifo_write(priv, 0, M_CAN_FIFO_ID, id);
990 m_can_fifo_write(priv, 0, M_CAN_FIFO_DLC, cf->can_dlc << 16);
991 m_can_fifo_write(priv, 0, M_CAN_FIFO_DATA(0), *(u32 *)(cf->data + 0));
992 m_can_fifo_write(priv, 0, M_CAN_FIFO_DATA(1), *(u32 *)(cf->data + 4));
993 can_put_echo_skb(skb, dev, 0);
995 /* enable first TX buffer to start transfer */
996 m_can_write(priv, M_CAN_TXBTIE, 0x1);
997 m_can_write(priv, M_CAN_TXBAR, 0x1);
1002 static const struct net_device_ops m_can_netdev_ops = {
1003 .ndo_open = m_can_open,
1004 .ndo_stop = m_can_close,
1005 .ndo_start_xmit = m_can_start_xmit,
1006 .ndo_change_mtu = can_change_mtu,
1009 static int register_m_can_dev(struct net_device *dev)
1011 dev->flags |= IFF_ECHO; /* we support local echo */
1012 dev->netdev_ops = &m_can_netdev_ops;
1014 return register_candev(dev);
1017 static int m_can_of_parse_mram(struct platform_device *pdev,
1018 struct m_can_priv *priv)
1020 struct device_node *np = pdev->dev.of_node;
1021 struct resource *res;
1023 u32 out_val[MRAM_CFG_LEN];
1024 int i, start, end, ret;
1026 /* message ram could be shared */
1027 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "message_ram");
1031 addr = devm_ioremap(&pdev->dev, res->start, resource_size(res));
1035 /* get message ram configuration */
1036 ret = of_property_read_u32_array(np, "bosch,mram-cfg",
1037 out_val, sizeof(out_val) / 4);
1039 dev_err(&pdev->dev, "can not get message ram configuration\n");
1043 priv->mram_base = addr;
1044 priv->mcfg[MRAM_SIDF].off = out_val[0];
1045 priv->mcfg[MRAM_SIDF].num = out_val[1];
1046 priv->mcfg[MRAM_XIDF].off = priv->mcfg[MRAM_SIDF].off +
1047 priv->mcfg[MRAM_SIDF].num * SIDF_ELEMENT_SIZE;
1048 priv->mcfg[MRAM_XIDF].num = out_val[2];
1049 priv->mcfg[MRAM_RXF0].off = priv->mcfg[MRAM_XIDF].off +
1050 priv->mcfg[MRAM_XIDF].num * XIDF_ELEMENT_SIZE;
1051 priv->mcfg[MRAM_RXF0].num = out_val[3] & RXFC_FS_MASK;
1052 priv->mcfg[MRAM_RXF1].off = priv->mcfg[MRAM_RXF0].off +
1053 priv->mcfg[MRAM_RXF0].num * RXF0_ELEMENT_SIZE;
1054 priv->mcfg[MRAM_RXF1].num = out_val[4] & RXFC_FS_MASK;
1055 priv->mcfg[MRAM_RXB].off = priv->mcfg[MRAM_RXF1].off +
1056 priv->mcfg[MRAM_RXF1].num * RXF1_ELEMENT_SIZE;
1057 priv->mcfg[MRAM_RXB].num = out_val[5];
1058 priv->mcfg[MRAM_TXE].off = priv->mcfg[MRAM_RXB].off +
1059 priv->mcfg[MRAM_RXB].num * RXB_ELEMENT_SIZE;
1060 priv->mcfg[MRAM_TXE].num = out_val[6];
1061 priv->mcfg[MRAM_TXB].off = priv->mcfg[MRAM_TXE].off +
1062 priv->mcfg[MRAM_TXE].num * TXE_ELEMENT_SIZE;
1063 priv->mcfg[MRAM_TXB].num = out_val[7] & TXBC_NDTB_MASK;
1065 dev_dbg(&pdev->dev, "mram_base %p sidf 0x%x %d xidf 0x%x %d rxf0 0x%x %d rxf1 0x%x %d rxb 0x%x %d txe 0x%x %d txb 0x%x %d\n",
1067 priv->mcfg[MRAM_SIDF].off, priv->mcfg[MRAM_SIDF].num,
1068 priv->mcfg[MRAM_XIDF].off, priv->mcfg[MRAM_XIDF].num,
1069 priv->mcfg[MRAM_RXF0].off, priv->mcfg[MRAM_RXF0].num,
1070 priv->mcfg[MRAM_RXF1].off, priv->mcfg[MRAM_RXF1].num,
1071 priv->mcfg[MRAM_RXB].off, priv->mcfg[MRAM_RXB].num,
1072 priv->mcfg[MRAM_TXE].off, priv->mcfg[MRAM_TXE].num,
1073 priv->mcfg[MRAM_TXB].off, priv->mcfg[MRAM_TXB].num);
1075 /* initialize the entire Message RAM in use to avoid possible
1076 * ECC/parity checksum errors when reading an uninitialized buffer
1078 start = priv->mcfg[MRAM_SIDF].off;
1079 end = priv->mcfg[MRAM_TXB].off +
1080 priv->mcfg[MRAM_TXB].num * TXB_ELEMENT_SIZE;
1081 for (i = start; i < end; i += 4)
1082 writel(0x0, priv->mram_base + i);
1087 static int m_can_plat_probe(struct platform_device *pdev)
1089 struct net_device *dev;
1090 struct m_can_priv *priv;
1091 struct resource *res;
1093 struct clk *hclk, *cclk;
1096 hclk = devm_clk_get(&pdev->dev, "hclk");
1097 cclk = devm_clk_get(&pdev->dev, "cclk");
1098 if (IS_ERR(hclk) || IS_ERR(cclk)) {
1099 dev_err(&pdev->dev, "no clock find\n");
1103 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "m_can");
1104 addr = devm_ioremap_resource(&pdev->dev, res);
1105 irq = platform_get_irq_byname(pdev, "int0");
1106 if (IS_ERR(addr) || irq < 0)
1109 /* allocate the m_can device */
1110 dev = alloc_m_can_dev();
1114 priv = netdev_priv(dev);
1117 priv->device = &pdev->dev;
1120 priv->can.clock.freq = clk_get_rate(cclk);
1122 ret = m_can_of_parse_mram(pdev, priv);
1124 goto failed_free_dev;
1126 platform_set_drvdata(pdev, dev);
1127 SET_NETDEV_DEV(dev, &pdev->dev);
1129 ret = register_m_can_dev(dev);
1131 dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
1132 KBUILD_MODNAME, ret);
1133 goto failed_free_dev;
1136 devm_can_led_init(dev);
1138 dev_info(&pdev->dev, "%s device registered (regs=%p, irq=%d)\n",
1139 KBUILD_MODNAME, priv->base, dev->irq);
1144 free_m_can_dev(dev);
1148 static __maybe_unused int m_can_suspend(struct device *dev)
1150 struct net_device *ndev = dev_get_drvdata(dev);
1151 struct m_can_priv *priv = netdev_priv(ndev);
1153 if (netif_running(ndev)) {
1154 netif_stop_queue(ndev);
1155 netif_device_detach(ndev);
1158 /* TODO: enter low power */
1160 priv->can.state = CAN_STATE_SLEEPING;
1165 static __maybe_unused int m_can_resume(struct device *dev)
1167 struct net_device *ndev = dev_get_drvdata(dev);
1168 struct m_can_priv *priv = netdev_priv(ndev);
1170 /* TODO: exit low power */
1172 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1174 if (netif_running(ndev)) {
1175 netif_device_attach(ndev);
1176 netif_start_queue(ndev);
1182 static void unregister_m_can_dev(struct net_device *dev)
1184 unregister_candev(dev);
1187 static int m_can_plat_remove(struct platform_device *pdev)
1189 struct net_device *dev = platform_get_drvdata(pdev);
1191 unregister_m_can_dev(dev);
1192 platform_set_drvdata(pdev, NULL);
1194 free_m_can_dev(dev);
1199 static const struct dev_pm_ops m_can_pmops = {
1200 SET_SYSTEM_SLEEP_PM_OPS(m_can_suspend, m_can_resume)
1203 static const struct of_device_id m_can_of_table[] = {
1204 { .compatible = "bosch,m_can", .data = NULL },
1207 MODULE_DEVICE_TABLE(of, m_can_of_table);
1209 static struct platform_driver m_can_plat_driver = {
1211 .name = KBUILD_MODNAME,
1212 .of_match_table = m_can_of_table,
1215 .probe = m_can_plat_probe,
1216 .remove = m_can_plat_remove,
1219 module_platform_driver(m_can_plat_driver);
1221 MODULE_AUTHOR("Dong Aisheng <b29396@freescale.com>");
1222 MODULE_LICENSE("GPL v2");
1223 MODULE_DESCRIPTION("CAN bus driver for Bosch M_CAN controller");