1 /* Xilinx CAN device driver
3 * Copyright (C) 2012 - 2014 Xilinx, Inc.
4 * Copyright (C) 2009 PetaLogix. All rights reserved.
5 * Copyright (C) 2017 Sandvik Mining and Construction Oy
8 * This driver is developed for Axi CAN IP and for Zynq CANPS Controller.
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #include <linux/clk.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/interrupt.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/netdevice.h>
29 #include <linux/of_device.h>
30 #include <linux/platform_device.h>
31 #include <linux/skbuff.h>
32 #include <linux/spinlock.h>
33 #include <linux/string.h>
34 #include <linux/types.h>
35 #include <linux/can/dev.h>
36 #include <linux/can/error.h>
37 #include <linux/can/led.h>
38 #include <linux/pm_runtime.h>
40 #define DRIVER_NAME "xilinx_can"
42 /* CAN registers set */
44 XCAN_SRR_OFFSET = 0x00, /* Software reset */
45 XCAN_MSR_OFFSET = 0x04, /* Mode select */
46 XCAN_BRPR_OFFSET = 0x08, /* Baud rate prescaler */
47 XCAN_BTR_OFFSET = 0x0C, /* Bit timing */
48 XCAN_ECR_OFFSET = 0x10, /* Error counter */
49 XCAN_ESR_OFFSET = 0x14, /* Error status */
50 XCAN_SR_OFFSET = 0x18, /* Status */
51 XCAN_ISR_OFFSET = 0x1C, /* Interrupt status */
52 XCAN_IER_OFFSET = 0x20, /* Interrupt enable */
53 XCAN_ICR_OFFSET = 0x24, /* Interrupt clear */
54 XCAN_TXFIFO_ID_OFFSET = 0x30,/* TX FIFO ID */
55 XCAN_TXFIFO_DLC_OFFSET = 0x34, /* TX FIFO DLC */
56 XCAN_TXFIFO_DW1_OFFSET = 0x38, /* TX FIFO Data Word 1 */
57 XCAN_TXFIFO_DW2_OFFSET = 0x3C, /* TX FIFO Data Word 2 */
58 XCAN_RXFIFO_ID_OFFSET = 0x50, /* RX FIFO ID */
59 XCAN_RXFIFO_DLC_OFFSET = 0x54, /* RX FIFO DLC */
60 XCAN_RXFIFO_DW1_OFFSET = 0x58, /* RX FIFO Data Word 1 */
61 XCAN_RXFIFO_DW2_OFFSET = 0x5C, /* RX FIFO Data Word 2 */
64 /* CAN register bit masks - XCAN_<REG>_<BIT>_MASK */
65 #define XCAN_SRR_CEN_MASK 0x00000002 /* CAN enable */
66 #define XCAN_SRR_RESET_MASK 0x00000001 /* Soft Reset the CAN core */
67 #define XCAN_MSR_LBACK_MASK 0x00000002 /* Loop back mode select */
68 #define XCAN_MSR_SLEEP_MASK 0x00000001 /* Sleep mode select */
69 #define XCAN_BRPR_BRP_MASK 0x000000FF /* Baud rate prescaler */
70 #define XCAN_BTR_SJW_MASK 0x00000180 /* Synchronous jump width */
71 #define XCAN_BTR_TS2_MASK 0x00000070 /* Time segment 2 */
72 #define XCAN_BTR_TS1_MASK 0x0000000F /* Time segment 1 */
73 #define XCAN_ECR_REC_MASK 0x0000FF00 /* Receive error counter */
74 #define XCAN_ECR_TEC_MASK 0x000000FF /* Transmit error counter */
75 #define XCAN_ESR_ACKER_MASK 0x00000010 /* ACK error */
76 #define XCAN_ESR_BERR_MASK 0x00000008 /* Bit error */
77 #define XCAN_ESR_STER_MASK 0x00000004 /* Stuff error */
78 #define XCAN_ESR_FMER_MASK 0x00000002 /* Form error */
79 #define XCAN_ESR_CRCER_MASK 0x00000001 /* CRC error */
80 #define XCAN_SR_TXFLL_MASK 0x00000400 /* TX FIFO is full */
81 #define XCAN_SR_ESTAT_MASK 0x00000180 /* Error status */
82 #define XCAN_SR_ERRWRN_MASK 0x00000040 /* Error warning */
83 #define XCAN_SR_NORMAL_MASK 0x00000008 /* Normal mode */
84 #define XCAN_SR_LBACK_MASK 0x00000002 /* Loop back mode */
85 #define XCAN_SR_CONFIG_MASK 0x00000001 /* Configuration mode */
86 #define XCAN_IXR_TXFEMP_MASK 0x00004000 /* TX FIFO Empty */
87 #define XCAN_IXR_WKUP_MASK 0x00000800 /* Wake up interrupt */
88 #define XCAN_IXR_SLP_MASK 0x00000400 /* Sleep interrupt */
89 #define XCAN_IXR_BSOFF_MASK 0x00000200 /* Bus off interrupt */
90 #define XCAN_IXR_ERROR_MASK 0x00000100 /* Error interrupt */
91 #define XCAN_IXR_RXNEMP_MASK 0x00000080 /* RX FIFO NotEmpty intr */
92 #define XCAN_IXR_RXOFLW_MASK 0x00000040 /* RX FIFO Overflow intr */
93 #define XCAN_IXR_RXOK_MASK 0x00000010 /* Message received intr */
94 #define XCAN_IXR_TXFLL_MASK 0x00000004 /* Tx FIFO Full intr */
95 #define XCAN_IXR_TXOK_MASK 0x00000002 /* TX successful intr */
96 #define XCAN_IXR_ARBLST_MASK 0x00000001 /* Arbitration lost intr */
97 #define XCAN_IDR_ID1_MASK 0xFFE00000 /* Standard msg identifier */
98 #define XCAN_IDR_SRR_MASK 0x00100000 /* Substitute remote TXreq */
99 #define XCAN_IDR_IDE_MASK 0x00080000 /* Identifier extension */
100 #define XCAN_IDR_ID2_MASK 0x0007FFFE /* Extended message ident */
101 #define XCAN_IDR_RTR_MASK 0x00000001 /* Remote TX request */
102 #define XCAN_DLCR_DLC_MASK 0xF0000000 /* Data length code */
104 #define XCAN_INTR_ALL (XCAN_IXR_TXOK_MASK | XCAN_IXR_BSOFF_MASK |\
105 XCAN_IXR_WKUP_MASK | XCAN_IXR_SLP_MASK | \
106 XCAN_IXR_RXNEMP_MASK | XCAN_IXR_ERROR_MASK | \
107 XCAN_IXR_RXOFLW_MASK | XCAN_IXR_ARBLST_MASK)
109 /* CAN register bit shift - XCAN_<REG>_<BIT>_SHIFT */
110 #define XCAN_BTR_SJW_SHIFT 7 /* Synchronous jump width */
111 #define XCAN_BTR_TS2_SHIFT 4 /* Time segment 2 */
112 #define XCAN_IDR_ID1_SHIFT 21 /* Standard Messg Identifier */
113 #define XCAN_IDR_ID2_SHIFT 1 /* Extended Message Identifier */
114 #define XCAN_DLCR_DLC_SHIFT 28 /* Data length code */
115 #define XCAN_ESR_REC_SHIFT 8 /* Rx Error Count */
117 /* CAN frame length constants */
118 #define XCAN_FRAME_MAX_DATA_LEN 8
119 #define XCAN_TIMEOUT (1 * HZ)
122 * struct xcan_priv - This definition define CAN driver instance
123 * @can: CAN private data structure.
124 * @tx_lock: Lock for synchronizing TX interrupt handling
125 * @tx_head: Tx CAN packets ready to send on the queue
126 * @tx_tail: Tx CAN packets successfully sended on the queue
127 * @tx_max: Maximum number packets the driver can send
128 * @napi: NAPI structure
129 * @read_reg: For reading data from CAN registers
130 * @write_reg: For writing data to CAN registers
131 * @dev: Network device data structure
132 * @reg_base: Ioremapped address to registers
133 * @irq_flags: For request_irq()
134 * @bus_clk: Pointer to struct clk
135 * @can_clk: Pointer to struct clk
140 unsigned int tx_head;
141 unsigned int tx_tail;
143 struct napi_struct napi;
144 u32 (*read_reg)(const struct xcan_priv *priv, enum xcan_reg reg);
145 void (*write_reg)(const struct xcan_priv *priv, enum xcan_reg reg,
148 void __iomem *reg_base;
149 unsigned long irq_flags;
154 /* CAN Bittiming constants as per Xilinx CAN specs */
155 static const struct can_bittiming_const xcan_bittiming_const = {
167 #define XCAN_CAP_WATERMARK 0x0001
168 struct xcan_devtype_data {
173 * xcan_write_reg_le - Write a value to the device register little endian
174 * @priv: Driver private data structure
175 * @reg: Register offset
176 * @val: Value to write at the Register offset
178 * Write data to the paricular CAN register
180 static void xcan_write_reg_le(const struct xcan_priv *priv, enum xcan_reg reg,
183 iowrite32(val, priv->reg_base + reg);
187 * xcan_read_reg_le - Read a value from the device register little endian
188 * @priv: Driver private data structure
189 * @reg: Register offset
191 * Read data from the particular CAN register
192 * Return: value read from the CAN register
194 static u32 xcan_read_reg_le(const struct xcan_priv *priv, enum xcan_reg reg)
196 return ioread32(priv->reg_base + reg);
200 * xcan_write_reg_be - Write a value to the device register big endian
201 * @priv: Driver private data structure
202 * @reg: Register offset
203 * @val: Value to write at the Register offset
205 * Write data to the paricular CAN register
207 static void xcan_write_reg_be(const struct xcan_priv *priv, enum xcan_reg reg,
210 iowrite32be(val, priv->reg_base + reg);
214 * xcan_read_reg_be - Read a value from the device register big endian
215 * @priv: Driver private data structure
216 * @reg: Register offset
218 * Read data from the particular CAN register
219 * Return: value read from the CAN register
221 static u32 xcan_read_reg_be(const struct xcan_priv *priv, enum xcan_reg reg)
223 return ioread32be(priv->reg_base + reg);
227 * set_reset_mode - Resets the CAN device mode
228 * @ndev: Pointer to net_device structure
230 * This is the driver reset mode routine.The driver
231 * enters into configuration mode.
233 * Return: 0 on success and failure value on error
235 static int set_reset_mode(struct net_device *ndev)
237 struct xcan_priv *priv = netdev_priv(ndev);
238 unsigned long timeout;
240 priv->write_reg(priv, XCAN_SRR_OFFSET, XCAN_SRR_RESET_MASK);
242 timeout = jiffies + XCAN_TIMEOUT;
243 while (!(priv->read_reg(priv, XCAN_SR_OFFSET) & XCAN_SR_CONFIG_MASK)) {
244 if (time_after(jiffies, timeout)) {
245 netdev_warn(ndev, "timed out for config mode\n");
248 usleep_range(500, 10000);
251 /* reset clears FIFOs */
259 * xcan_set_bittiming - CAN set bit timing routine
260 * @ndev: Pointer to net_device structure
262 * This is the driver set bittiming routine.
263 * Return: 0 on success and failure value on error
265 static int xcan_set_bittiming(struct net_device *ndev)
267 struct xcan_priv *priv = netdev_priv(ndev);
268 struct can_bittiming *bt = &priv->can.bittiming;
272 /* Check whether Xilinx CAN is in configuration mode.
273 * It cannot set bit timing if Xilinx CAN is not in configuration mode.
275 is_config_mode = priv->read_reg(priv, XCAN_SR_OFFSET) &
277 if (!is_config_mode) {
279 "BUG! Cannot set bittiming - CAN is not in config mode\n");
283 /* Setting Baud Rate prescalar value in BRPR Register */
284 btr0 = (bt->brp - 1);
286 /* Setting Time Segment 1 in BTR Register */
287 btr1 = (bt->prop_seg + bt->phase_seg1 - 1);
289 /* Setting Time Segment 2 in BTR Register */
290 btr1 |= (bt->phase_seg2 - 1) << XCAN_BTR_TS2_SHIFT;
292 /* Setting Synchronous jump width in BTR Register */
293 btr1 |= (bt->sjw - 1) << XCAN_BTR_SJW_SHIFT;
295 priv->write_reg(priv, XCAN_BRPR_OFFSET, btr0);
296 priv->write_reg(priv, XCAN_BTR_OFFSET, btr1);
298 netdev_dbg(ndev, "BRPR=0x%08x, BTR=0x%08x\n",
299 priv->read_reg(priv, XCAN_BRPR_OFFSET),
300 priv->read_reg(priv, XCAN_BTR_OFFSET));
306 * xcan_chip_start - This the drivers start routine
307 * @ndev: Pointer to net_device structure
309 * This is the drivers start routine.
310 * Based on the State of the CAN device it puts
311 * the CAN device into a proper mode.
313 * Return: 0 on success and failure value on error
315 static int xcan_chip_start(struct net_device *ndev)
317 struct xcan_priv *priv = netdev_priv(ndev);
318 u32 reg_msr, reg_sr_mask;
320 unsigned long timeout;
322 /* Check if it is in reset mode */
323 err = set_reset_mode(ndev);
327 err = xcan_set_bittiming(ndev);
331 /* Enable interrupts */
332 priv->write_reg(priv, XCAN_IER_OFFSET, XCAN_INTR_ALL);
334 /* Check whether it is loopback mode or normal mode */
335 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
336 reg_msr = XCAN_MSR_LBACK_MASK;
337 reg_sr_mask = XCAN_SR_LBACK_MASK;
340 reg_sr_mask = XCAN_SR_NORMAL_MASK;
343 priv->write_reg(priv, XCAN_MSR_OFFSET, reg_msr);
344 priv->write_reg(priv, XCAN_SRR_OFFSET, XCAN_SRR_CEN_MASK);
346 timeout = jiffies + XCAN_TIMEOUT;
347 while (!(priv->read_reg(priv, XCAN_SR_OFFSET) & reg_sr_mask)) {
348 if (time_after(jiffies, timeout)) {
350 "timed out for correct mode\n");
354 netdev_dbg(ndev, "status:#x%08x\n",
355 priv->read_reg(priv, XCAN_SR_OFFSET));
357 priv->can.state = CAN_STATE_ERROR_ACTIVE;
362 * xcan_do_set_mode - This sets the mode of the driver
363 * @ndev: Pointer to net_device structure
364 * @mode: Tells the mode of the driver
366 * This check the drivers state and calls the
367 * the corresponding modes to set.
369 * Return: 0 on success and failure value on error
371 static int xcan_do_set_mode(struct net_device *ndev, enum can_mode mode)
377 ret = xcan_chip_start(ndev);
379 netdev_err(ndev, "xcan_chip_start failed!\n");
382 netif_wake_queue(ndev);
393 * xcan_start_xmit - Starts the transmission
394 * @skb: sk_buff pointer that contains data to be Txed
395 * @ndev: Pointer to net_device structure
397 * This function is invoked from upper layers to initiate transmission. This
398 * function uses the next available free txbuff and populates their fields to
399 * start the transmission.
401 * Return: 0 on success and failure value on error
403 static int xcan_start_xmit(struct sk_buff *skb, struct net_device *ndev)
405 struct xcan_priv *priv = netdev_priv(ndev);
406 struct net_device_stats *stats = &ndev->stats;
407 struct can_frame *cf = (struct can_frame *)skb->data;
408 u32 id, dlc, data[2] = {0, 0};
411 if (can_dropped_invalid_skb(ndev, skb))
414 /* Check if the TX buffer is full */
415 if (unlikely(priv->read_reg(priv, XCAN_SR_OFFSET) &
416 XCAN_SR_TXFLL_MASK)) {
417 netif_stop_queue(ndev);
418 netdev_err(ndev, "BUG!, TX FIFO full when queue awake!\n");
419 return NETDEV_TX_BUSY;
422 /* Watch carefully on the bit sequence */
423 if (cf->can_id & CAN_EFF_FLAG) {
424 /* Extended CAN ID format */
425 id = ((cf->can_id & CAN_EFF_MASK) << XCAN_IDR_ID2_SHIFT) &
427 id |= (((cf->can_id & CAN_EFF_MASK) >>
428 (CAN_EFF_ID_BITS-CAN_SFF_ID_BITS)) <<
429 XCAN_IDR_ID1_SHIFT) & XCAN_IDR_ID1_MASK;
431 /* The substibute remote TX request bit should be "1"
432 * for extended frames as in the Xilinx CAN datasheet
434 id |= XCAN_IDR_IDE_MASK | XCAN_IDR_SRR_MASK;
436 if (cf->can_id & CAN_RTR_FLAG)
437 /* Extended frames remote TX request */
438 id |= XCAN_IDR_RTR_MASK;
440 /* Standard CAN ID format */
441 id = ((cf->can_id & CAN_SFF_MASK) << XCAN_IDR_ID1_SHIFT) &
444 if (cf->can_id & CAN_RTR_FLAG)
445 /* Standard frames remote TX request */
446 id |= XCAN_IDR_SRR_MASK;
449 dlc = cf->can_dlc << XCAN_DLCR_DLC_SHIFT;
452 data[0] = be32_to_cpup((__be32 *)(cf->data + 0));
454 data[1] = be32_to_cpup((__be32 *)(cf->data + 4));
456 can_put_echo_skb(skb, ndev, priv->tx_head % priv->tx_max);
458 spin_lock_irqsave(&priv->tx_lock, flags);
462 /* Write the Frame to Xilinx CAN TX FIFO */
463 priv->write_reg(priv, XCAN_TXFIFO_ID_OFFSET, id);
464 /* If the CAN frame is RTR frame this write triggers tranmission */
465 priv->write_reg(priv, XCAN_TXFIFO_DLC_OFFSET, dlc);
466 if (!(cf->can_id & CAN_RTR_FLAG)) {
467 priv->write_reg(priv, XCAN_TXFIFO_DW1_OFFSET, data[0]);
468 /* If the CAN frame is Standard/Extended frame this
469 * write triggers tranmission
471 priv->write_reg(priv, XCAN_TXFIFO_DW2_OFFSET, data[1]);
472 stats->tx_bytes += cf->can_dlc;
475 /* Clear TX-FIFO-empty interrupt for xcan_tx_interrupt() */
476 if (priv->tx_max > 1)
477 priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_TXFEMP_MASK);
479 /* Check if the TX buffer is full */
480 if ((priv->tx_head - priv->tx_tail) == priv->tx_max)
481 netif_stop_queue(ndev);
483 spin_unlock_irqrestore(&priv->tx_lock, flags);
489 * xcan_rx - Is called from CAN isr to complete the received
491 * @ndev: Pointer to net_device structure
493 * This function is invoked from the CAN isr(poll) to process the Rx frames. It
494 * does minimal processing and invokes "netif_receive_skb" to complete further
496 * Return: 1 on success and 0 on failure.
498 static int xcan_rx(struct net_device *ndev)
500 struct xcan_priv *priv = netdev_priv(ndev);
501 struct net_device_stats *stats = &ndev->stats;
502 struct can_frame *cf;
504 u32 id_xcan, dlc, data[2] = {0, 0};
506 skb = alloc_can_skb(ndev, &cf);
507 if (unlikely(!skb)) {
512 /* Read a frame from Xilinx zynq CANPS */
513 id_xcan = priv->read_reg(priv, XCAN_RXFIFO_ID_OFFSET);
514 dlc = priv->read_reg(priv, XCAN_RXFIFO_DLC_OFFSET) >>
517 /* Change Xilinx CAN data length format to socketCAN data format */
518 cf->can_dlc = get_can_dlc(dlc);
520 /* Change Xilinx CAN ID format to socketCAN ID format */
521 if (id_xcan & XCAN_IDR_IDE_MASK) {
522 /* The received frame is an Extended format frame */
523 cf->can_id = (id_xcan & XCAN_IDR_ID1_MASK) >> 3;
524 cf->can_id |= (id_xcan & XCAN_IDR_ID2_MASK) >>
526 cf->can_id |= CAN_EFF_FLAG;
527 if (id_xcan & XCAN_IDR_RTR_MASK)
528 cf->can_id |= CAN_RTR_FLAG;
530 /* The received frame is a standard format frame */
531 cf->can_id = (id_xcan & XCAN_IDR_ID1_MASK) >>
533 if (id_xcan & XCAN_IDR_SRR_MASK)
534 cf->can_id |= CAN_RTR_FLAG;
537 /* DW1/DW2 must always be read to remove message from RXFIFO */
538 data[0] = priv->read_reg(priv, XCAN_RXFIFO_DW1_OFFSET);
539 data[1] = priv->read_reg(priv, XCAN_RXFIFO_DW2_OFFSET);
541 if (!(cf->can_id & CAN_RTR_FLAG)) {
542 /* Change Xilinx CAN data format to socketCAN data format */
544 *(__be32 *)(cf->data) = cpu_to_be32(data[0]);
546 *(__be32 *)(cf->data + 4) = cpu_to_be32(data[1]);
549 stats->rx_bytes += cf->can_dlc;
551 netif_receive_skb(skb);
557 * xcan_current_error_state - Get current error state from HW
558 * @ndev: Pointer to net_device structure
560 * Checks the current CAN error state from the HW. Note that this
561 * only checks for ERROR_PASSIVE and ERROR_WARNING.
564 * ERROR_PASSIVE or ERROR_WARNING if either is active, ERROR_ACTIVE
567 static enum can_state xcan_current_error_state(struct net_device *ndev)
569 struct xcan_priv *priv = netdev_priv(ndev);
570 u32 status = priv->read_reg(priv, XCAN_SR_OFFSET);
572 if ((status & XCAN_SR_ESTAT_MASK) == XCAN_SR_ESTAT_MASK)
573 return CAN_STATE_ERROR_PASSIVE;
574 else if (status & XCAN_SR_ERRWRN_MASK)
575 return CAN_STATE_ERROR_WARNING;
577 return CAN_STATE_ERROR_ACTIVE;
581 * xcan_set_error_state - Set new CAN error state
582 * @ndev: Pointer to net_device structure
583 * @new_state: The new CAN state to be set
584 * @cf: Error frame to be populated or NULL
586 * Set new CAN error state for the device, updating statistics and
587 * populating the error frame if given.
589 static void xcan_set_error_state(struct net_device *ndev,
590 enum can_state new_state,
591 struct can_frame *cf)
593 struct xcan_priv *priv = netdev_priv(ndev);
594 u32 ecr = priv->read_reg(priv, XCAN_ECR_OFFSET);
595 u32 txerr = ecr & XCAN_ECR_TEC_MASK;
596 u32 rxerr = (ecr & XCAN_ECR_REC_MASK) >> XCAN_ESR_REC_SHIFT;
598 priv->can.state = new_state;
601 cf->can_id |= CAN_ERR_CRTL;
607 case CAN_STATE_ERROR_PASSIVE:
608 priv->can.can_stats.error_passive++;
610 cf->data[1] = (rxerr > 127) ?
611 CAN_ERR_CRTL_RX_PASSIVE :
612 CAN_ERR_CRTL_TX_PASSIVE;
614 case CAN_STATE_ERROR_WARNING:
615 priv->can.can_stats.error_warning++;
617 cf->data[1] |= (txerr > rxerr) ?
618 CAN_ERR_CRTL_TX_WARNING :
619 CAN_ERR_CRTL_RX_WARNING;
621 case CAN_STATE_ERROR_ACTIVE:
623 cf->data[1] |= CAN_ERR_CRTL_ACTIVE;
626 /* non-ERROR states are handled elsewhere */
633 * xcan_update_error_state_after_rxtx - Update CAN error state after RX/TX
634 * @ndev: Pointer to net_device structure
636 * If the device is in a ERROR-WARNING or ERROR-PASSIVE state, check if
637 * the performed RX/TX has caused it to drop to a lesser state and set
638 * the interface state accordingly.
640 static void xcan_update_error_state_after_rxtx(struct net_device *ndev)
642 struct xcan_priv *priv = netdev_priv(ndev);
643 enum can_state old_state = priv->can.state;
644 enum can_state new_state;
646 /* changing error state due to successful frame RX/TX can only
647 * occur from these states
649 if (old_state != CAN_STATE_ERROR_WARNING &&
650 old_state != CAN_STATE_ERROR_PASSIVE)
653 new_state = xcan_current_error_state(ndev);
655 if (new_state != old_state) {
657 struct can_frame *cf;
659 skb = alloc_can_err_skb(ndev, &cf);
661 xcan_set_error_state(ndev, new_state, skb ? cf : NULL);
664 struct net_device_stats *stats = &ndev->stats;
667 stats->rx_bytes += cf->can_dlc;
674 * xcan_err_interrupt - error frame Isr
675 * @ndev: net_device pointer
676 * @isr: interrupt status register value
678 * This is the CAN error interrupt and it will
679 * check the the type of error and forward the error
680 * frame to upper layers.
682 static void xcan_err_interrupt(struct net_device *ndev, u32 isr)
684 struct xcan_priv *priv = netdev_priv(ndev);
685 struct net_device_stats *stats = &ndev->stats;
686 struct can_frame *cf;
690 skb = alloc_can_err_skb(ndev, &cf);
692 err_status = priv->read_reg(priv, XCAN_ESR_OFFSET);
693 priv->write_reg(priv, XCAN_ESR_OFFSET, err_status);
695 if (isr & XCAN_IXR_BSOFF_MASK) {
696 priv->can.state = CAN_STATE_BUS_OFF;
697 priv->can.can_stats.bus_off++;
698 /* Leave device in Config Mode in bus-off state */
699 priv->write_reg(priv, XCAN_SRR_OFFSET, XCAN_SRR_RESET_MASK);
702 cf->can_id |= CAN_ERR_BUSOFF;
704 enum can_state new_state = xcan_current_error_state(ndev);
706 xcan_set_error_state(ndev, new_state, skb ? cf : NULL);
709 /* Check for Arbitration lost interrupt */
710 if (isr & XCAN_IXR_ARBLST_MASK) {
711 priv->can.can_stats.arbitration_lost++;
713 cf->can_id |= CAN_ERR_LOSTARB;
714 cf->data[0] = CAN_ERR_LOSTARB_UNSPEC;
718 /* Check for RX FIFO Overflow interrupt */
719 if (isr & XCAN_IXR_RXOFLW_MASK) {
720 stats->rx_over_errors++;
723 cf->can_id |= CAN_ERR_CRTL;
724 cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
728 /* Check for error interrupt */
729 if (isr & XCAN_IXR_ERROR_MASK) {
731 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
733 /* Check for Ack error interrupt */
734 if (err_status & XCAN_ESR_ACKER_MASK) {
737 cf->can_id |= CAN_ERR_ACK;
738 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
742 /* Check for Bit error interrupt */
743 if (err_status & XCAN_ESR_BERR_MASK) {
746 cf->can_id |= CAN_ERR_PROT;
747 cf->data[2] = CAN_ERR_PROT_BIT;
751 /* Check for Stuff error interrupt */
752 if (err_status & XCAN_ESR_STER_MASK) {
755 cf->can_id |= CAN_ERR_PROT;
756 cf->data[2] = CAN_ERR_PROT_STUFF;
760 /* Check for Form error interrupt */
761 if (err_status & XCAN_ESR_FMER_MASK) {
764 cf->can_id |= CAN_ERR_PROT;
765 cf->data[2] = CAN_ERR_PROT_FORM;
769 /* Check for CRC error interrupt */
770 if (err_status & XCAN_ESR_CRCER_MASK) {
773 cf->can_id |= CAN_ERR_PROT;
774 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
777 priv->can.can_stats.bus_error++;
782 stats->rx_bytes += cf->can_dlc;
786 netdev_dbg(ndev, "%s: error status register:0x%x\n",
787 __func__, priv->read_reg(priv, XCAN_ESR_OFFSET));
791 * xcan_state_interrupt - It will check the state of the CAN device
792 * @ndev: net_device pointer
793 * @isr: interrupt status register value
795 * This will checks the state of the CAN device
796 * and puts the device into appropriate state.
798 static void xcan_state_interrupt(struct net_device *ndev, u32 isr)
800 struct xcan_priv *priv = netdev_priv(ndev);
802 /* Check for Sleep interrupt if set put CAN device in sleep state */
803 if (isr & XCAN_IXR_SLP_MASK)
804 priv->can.state = CAN_STATE_SLEEPING;
806 /* Check for Wake up interrupt if set put CAN device in Active state */
807 if (isr & XCAN_IXR_WKUP_MASK)
808 priv->can.state = CAN_STATE_ERROR_ACTIVE;
812 * xcan_rx_poll - Poll routine for rx packets (NAPI)
813 * @napi: napi structure pointer
814 * @quota: Max number of rx packets to be processed.
816 * This is the poll routine for rx part.
817 * It will process the packets maximux quota value.
819 * Return: number of packets received
821 static int xcan_rx_poll(struct napi_struct *napi, int quota)
823 struct net_device *ndev = napi->dev;
824 struct xcan_priv *priv = netdev_priv(ndev);
828 isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
829 while ((isr & XCAN_IXR_RXNEMP_MASK) && (work_done < quota)) {
830 work_done += xcan_rx(ndev);
831 priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_RXNEMP_MASK);
832 isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
836 can_led_event(ndev, CAN_LED_EVENT_RX);
837 xcan_update_error_state_after_rxtx(ndev);
840 if (work_done < quota) {
841 napi_complete_done(napi, work_done);
842 ier = priv->read_reg(priv, XCAN_IER_OFFSET);
843 ier |= XCAN_IXR_RXNEMP_MASK;
844 priv->write_reg(priv, XCAN_IER_OFFSET, ier);
850 * xcan_tx_interrupt - Tx Done Isr
851 * @ndev: net_device pointer
852 * @isr: Interrupt status register value
854 static void xcan_tx_interrupt(struct net_device *ndev, u32 isr)
856 struct xcan_priv *priv = netdev_priv(ndev);
857 struct net_device_stats *stats = &ndev->stats;
858 unsigned int frames_in_fifo;
859 int frames_sent = 1; /* TXOK => at least 1 frame was sent */
863 /* Synchronize with xmit as we need to know the exact number
864 * of frames in the FIFO to stay in sync due to the TXFEMP
866 * This also prevents a race between netif_wake_queue() and
867 * netif_stop_queue().
869 spin_lock_irqsave(&priv->tx_lock, flags);
871 frames_in_fifo = priv->tx_head - priv->tx_tail;
873 if (WARN_ON_ONCE(frames_in_fifo == 0)) {
874 /* clear TXOK anyway to avoid getting back here */
875 priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_TXOK_MASK);
876 spin_unlock_irqrestore(&priv->tx_lock, flags);
880 /* Check if 2 frames were sent (TXOK only means that at least 1
883 if (frames_in_fifo > 1) {
884 WARN_ON(frames_in_fifo > priv->tx_max);
886 /* Synchronize TXOK and isr so that after the loop:
887 * (1) isr variable is up-to-date at least up to TXOK clear
888 * time. This avoids us clearing a TXOK of a second frame
889 * but not noticing that the FIFO is now empty and thus
890 * marking only a single frame as sent.
891 * (2) No TXOK is left. Having one could mean leaving a
892 * stray TXOK as we might process the associated frame
893 * via TXFEMP handling as we read TXFEMP *after* TXOK
894 * clear to satisfy (1).
896 while ((isr & XCAN_IXR_TXOK_MASK) && !WARN_ON(++retries == 100)) {
897 priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_TXOK_MASK);
898 isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
901 if (isr & XCAN_IXR_TXFEMP_MASK) {
902 /* nothing in FIFO anymore */
903 frames_sent = frames_in_fifo;
906 /* single frame in fifo, just clear TXOK */
907 priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_TXOK_MASK);
910 while (frames_sent--) {
911 can_get_echo_skb(ndev, priv->tx_tail %
917 netif_wake_queue(ndev);
919 spin_unlock_irqrestore(&priv->tx_lock, flags);
921 can_led_event(ndev, CAN_LED_EVENT_TX);
922 xcan_update_error_state_after_rxtx(ndev);
926 * xcan_interrupt - CAN Isr
928 * @dev_id: device id poniter
930 * This is the xilinx CAN Isr. It checks for the type of interrupt
931 * and invokes the corresponding ISR.
934 * IRQ_NONE - If CAN device is in sleep mode, IRQ_HANDLED otherwise
936 static irqreturn_t xcan_interrupt(int irq, void *dev_id)
938 struct net_device *ndev = (struct net_device *)dev_id;
939 struct xcan_priv *priv = netdev_priv(ndev);
943 /* Get the interrupt status from Xilinx CAN */
944 isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
948 /* Check for the type of interrupt and Processing it */
949 if (isr & (XCAN_IXR_SLP_MASK | XCAN_IXR_WKUP_MASK)) {
950 priv->write_reg(priv, XCAN_ICR_OFFSET, (XCAN_IXR_SLP_MASK |
951 XCAN_IXR_WKUP_MASK));
952 xcan_state_interrupt(ndev, isr);
955 /* Check for Tx interrupt and Processing it */
956 if (isr & XCAN_IXR_TXOK_MASK)
957 xcan_tx_interrupt(ndev, isr);
959 /* Check for the type of error interrupt and Processing it */
960 isr_errors = isr & (XCAN_IXR_ERROR_MASK | XCAN_IXR_RXOFLW_MASK |
961 XCAN_IXR_BSOFF_MASK | XCAN_IXR_ARBLST_MASK);
963 priv->write_reg(priv, XCAN_ICR_OFFSET, isr_errors);
964 xcan_err_interrupt(ndev, isr);
967 /* Check for the type of receive interrupt and Processing it */
968 if (isr & XCAN_IXR_RXNEMP_MASK) {
969 ier = priv->read_reg(priv, XCAN_IER_OFFSET);
970 ier &= ~XCAN_IXR_RXNEMP_MASK;
971 priv->write_reg(priv, XCAN_IER_OFFSET, ier);
972 napi_schedule(&priv->napi);
978 * xcan_chip_stop - Driver stop routine
979 * @ndev: Pointer to net_device structure
981 * This is the drivers stop routine. It will disable the
982 * interrupts and put the device into configuration mode.
984 static void xcan_chip_stop(struct net_device *ndev)
986 struct xcan_priv *priv = netdev_priv(ndev);
988 /* Disable interrupts and leave the can in configuration mode */
989 set_reset_mode(ndev);
990 priv->can.state = CAN_STATE_STOPPED;
994 * xcan_open - Driver open routine
995 * @ndev: Pointer to net_device structure
997 * This is the driver open routine.
998 * Return: 0 on success and failure value on error
1000 static int xcan_open(struct net_device *ndev)
1002 struct xcan_priv *priv = netdev_priv(ndev);
1005 ret = pm_runtime_get_sync(priv->dev);
1007 netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
1012 ret = request_irq(ndev->irq, xcan_interrupt, priv->irq_flags,
1015 netdev_err(ndev, "irq allocation for CAN failed\n");
1019 /* Set chip into reset mode */
1020 ret = set_reset_mode(ndev);
1022 netdev_err(ndev, "mode resetting failed!\n");
1027 ret = open_candev(ndev);
1031 ret = xcan_chip_start(ndev);
1033 netdev_err(ndev, "xcan_chip_start failed!\n");
1037 can_led_event(ndev, CAN_LED_EVENT_OPEN);
1038 napi_enable(&priv->napi);
1039 netif_start_queue(ndev);
1046 free_irq(ndev->irq, ndev);
1048 pm_runtime_put(priv->dev);
1054 * xcan_close - Driver close routine
1055 * @ndev: Pointer to net_device structure
1059 static int xcan_close(struct net_device *ndev)
1061 struct xcan_priv *priv = netdev_priv(ndev);
1063 netif_stop_queue(ndev);
1064 napi_disable(&priv->napi);
1065 xcan_chip_stop(ndev);
1066 free_irq(ndev->irq, ndev);
1069 can_led_event(ndev, CAN_LED_EVENT_STOP);
1070 pm_runtime_put(priv->dev);
1076 * xcan_get_berr_counter - error counter routine
1077 * @ndev: Pointer to net_device structure
1078 * @bec: Pointer to can_berr_counter structure
1080 * This is the driver error counter routine.
1081 * Return: 0 on success and failure value on error
1083 static int xcan_get_berr_counter(const struct net_device *ndev,
1084 struct can_berr_counter *bec)
1086 struct xcan_priv *priv = netdev_priv(ndev);
1089 ret = pm_runtime_get_sync(priv->dev);
1091 netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
1096 bec->txerr = priv->read_reg(priv, XCAN_ECR_OFFSET) & XCAN_ECR_TEC_MASK;
1097 bec->rxerr = ((priv->read_reg(priv, XCAN_ECR_OFFSET) &
1098 XCAN_ECR_REC_MASK) >> XCAN_ESR_REC_SHIFT);
1100 pm_runtime_put(priv->dev);
1106 static const struct net_device_ops xcan_netdev_ops = {
1107 .ndo_open = xcan_open,
1108 .ndo_stop = xcan_close,
1109 .ndo_start_xmit = xcan_start_xmit,
1110 .ndo_change_mtu = can_change_mtu,
1114 * xcan_suspend - Suspend method for the driver
1115 * @dev: Address of the device structure
1117 * Put the driver into low power mode.
1118 * Return: 0 on success and failure value on error
1120 static int __maybe_unused xcan_suspend(struct device *dev)
1122 struct net_device *ndev = dev_get_drvdata(dev);
1124 if (netif_running(ndev)) {
1125 netif_stop_queue(ndev);
1126 netif_device_detach(ndev);
1127 xcan_chip_stop(ndev);
1130 return pm_runtime_force_suspend(dev);
1134 * xcan_resume - Resume from suspend
1135 * @dev: Address of the device structure
1137 * Resume operation after suspend.
1138 * Return: 0 on success and failure value on error
1140 static int __maybe_unused xcan_resume(struct device *dev)
1142 struct net_device *ndev = dev_get_drvdata(dev);
1145 ret = pm_runtime_force_resume(dev);
1147 dev_err(dev, "pm_runtime_force_resume failed on resume\n");
1151 if (netif_running(ndev)) {
1152 ret = xcan_chip_start(ndev);
1154 dev_err(dev, "xcan_chip_start failed on resume\n");
1158 netif_device_attach(ndev);
1159 netif_start_queue(ndev);
1166 * xcan_runtime_suspend - Runtime suspend method for the driver
1167 * @dev: Address of the device structure
1169 * Put the driver into low power mode.
1172 static int __maybe_unused xcan_runtime_suspend(struct device *dev)
1174 struct net_device *ndev = dev_get_drvdata(dev);
1175 struct xcan_priv *priv = netdev_priv(ndev);
1177 clk_disable_unprepare(priv->bus_clk);
1178 clk_disable_unprepare(priv->can_clk);
1184 * xcan_runtime_resume - Runtime resume from suspend
1185 * @dev: Address of the device structure
1187 * Resume operation after suspend.
1188 * Return: 0 on success and failure value on error
1190 static int __maybe_unused xcan_runtime_resume(struct device *dev)
1192 struct net_device *ndev = dev_get_drvdata(dev);
1193 struct xcan_priv *priv = netdev_priv(ndev);
1196 ret = clk_prepare_enable(priv->bus_clk);
1198 dev_err(dev, "Cannot enable clock.\n");
1201 ret = clk_prepare_enable(priv->can_clk);
1203 dev_err(dev, "Cannot enable clock.\n");
1204 clk_disable_unprepare(priv->bus_clk);
1211 static const struct dev_pm_ops xcan_dev_pm_ops = {
1212 SET_SYSTEM_SLEEP_PM_OPS(xcan_suspend, xcan_resume)
1213 SET_RUNTIME_PM_OPS(xcan_runtime_suspend, xcan_runtime_resume, NULL)
1216 static const struct xcan_devtype_data xcan_zynq_data = {
1217 .caps = XCAN_CAP_WATERMARK,
1220 /* Match table for OF platform binding */
1221 static const struct of_device_id xcan_of_match[] = {
1222 { .compatible = "xlnx,zynq-can-1.0", .data = &xcan_zynq_data },
1223 { .compatible = "xlnx,axi-can-1.00.a", },
1224 { /* end of list */ },
1226 MODULE_DEVICE_TABLE(of, xcan_of_match);
1229 * xcan_probe - Platform registration call
1230 * @pdev: Handle to the platform device structure
1232 * This function does all the memory allocation and registration for the CAN
1235 * Return: 0 on success and failure value on error
1237 static int xcan_probe(struct platform_device *pdev)
1239 struct resource *res; /* IO mem resources */
1240 struct net_device *ndev;
1241 struct xcan_priv *priv;
1242 const struct of_device_id *of_id;
1245 int ret, rx_max, tx_max, tx_fifo_depth;
1247 /* Get the virtual base address for the device */
1248 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1249 addr = devm_ioremap_resource(&pdev->dev, res);
1251 ret = PTR_ERR(addr);
1255 ret = of_property_read_u32(pdev->dev.of_node, "tx-fifo-depth",
1260 ret = of_property_read_u32(pdev->dev.of_node, "rx-fifo-depth", &rx_max);
1264 of_id = of_match_device(xcan_of_match, &pdev->dev);
1266 const struct xcan_devtype_data *devtype_data = of_id->data;
1269 caps = devtype_data->caps;
1272 /* There is no way to directly figure out how many frames have been
1273 * sent when the TXOK interrupt is processed. If watermark programming
1274 * is supported, we can have 2 frames in the FIFO and use TXFEMP
1275 * to determine if 1 or 2 frames have been sent.
1276 * Theoretically we should be able to use TXFWMEMP to determine up
1277 * to 3 frames, but it seems that after putting a second frame in the
1278 * FIFO, with watermark at 2 frames, it can happen that TXFWMEMP (less
1279 * than 2 frames in FIFO) is set anyway with no TXOK (a frame was
1280 * sent), which is not a sensible state - possibly TXFWMEMP is not
1281 * completely synchronized with the rest of the bits?
1283 if (caps & XCAN_CAP_WATERMARK)
1284 tx_max = min(tx_fifo_depth, 2);
1288 /* Create a CAN device instance */
1289 ndev = alloc_candev(sizeof(struct xcan_priv), tx_max);
1293 priv = netdev_priv(ndev);
1294 priv->dev = &pdev->dev;
1295 priv->can.bittiming_const = &xcan_bittiming_const;
1296 priv->can.do_set_mode = xcan_do_set_mode;
1297 priv->can.do_get_berr_counter = xcan_get_berr_counter;
1298 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1299 CAN_CTRLMODE_BERR_REPORTING;
1300 priv->reg_base = addr;
1301 priv->tx_max = tx_max;
1302 spin_lock_init(&priv->tx_lock);
1304 /* Get IRQ for the device */
1305 ret = platform_get_irq(pdev, 0);
1311 ndev->flags |= IFF_ECHO; /* We support local echo */
1313 platform_set_drvdata(pdev, ndev);
1314 SET_NETDEV_DEV(ndev, &pdev->dev);
1315 ndev->netdev_ops = &xcan_netdev_ops;
1317 /* Getting the CAN can_clk info */
1318 priv->can_clk = devm_clk_get(&pdev->dev, "can_clk");
1319 if (IS_ERR(priv->can_clk)) {
1320 dev_err(&pdev->dev, "Device clock not found.\n");
1321 ret = PTR_ERR(priv->can_clk);
1324 /* Check for type of CAN device */
1325 if (of_device_is_compatible(pdev->dev.of_node,
1326 "xlnx,zynq-can-1.0")) {
1327 priv->bus_clk = devm_clk_get(&pdev->dev, "pclk");
1328 if (IS_ERR(priv->bus_clk)) {
1329 dev_err(&pdev->dev, "bus clock not found\n");
1330 ret = PTR_ERR(priv->bus_clk);
1334 priv->bus_clk = devm_clk_get(&pdev->dev, "s_axi_aclk");
1335 if (IS_ERR(priv->bus_clk)) {
1336 dev_err(&pdev->dev, "bus clock not found\n");
1337 ret = PTR_ERR(priv->bus_clk);
1342 priv->write_reg = xcan_write_reg_le;
1343 priv->read_reg = xcan_read_reg_le;
1345 pm_runtime_enable(&pdev->dev);
1346 ret = pm_runtime_get_sync(&pdev->dev);
1348 netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
1353 if (priv->read_reg(priv, XCAN_SR_OFFSET) != XCAN_SR_CONFIG_MASK) {
1354 priv->write_reg = xcan_write_reg_be;
1355 priv->read_reg = xcan_read_reg_be;
1358 priv->can.clock.freq = clk_get_rate(priv->can_clk);
1360 netif_napi_add(ndev, &priv->napi, xcan_rx_poll, rx_max);
1362 ret = register_candev(ndev);
1364 dev_err(&pdev->dev, "fail to register failed (err=%d)\n", ret);
1365 goto err_disableclks;
1368 devm_can_led_init(ndev);
1370 pm_runtime_put(&pdev->dev);
1372 netdev_dbg(ndev, "reg_base=0x%p irq=%d clock=%d, tx fifo depth: actual %d, using %d\n",
1373 priv->reg_base, ndev->irq, priv->can.clock.freq,
1374 tx_fifo_depth, priv->tx_max);
1379 pm_runtime_put(priv->dev);
1381 pm_runtime_disable(&pdev->dev);
1389 * xcan_remove - Unregister the device after releasing the resources
1390 * @pdev: Handle to the platform device structure
1392 * This function frees all the resources allocated to the device.
1395 static int xcan_remove(struct platform_device *pdev)
1397 struct net_device *ndev = platform_get_drvdata(pdev);
1398 struct xcan_priv *priv = netdev_priv(ndev);
1400 unregister_candev(ndev);
1401 pm_runtime_disable(&pdev->dev);
1402 netif_napi_del(&priv->napi);
1408 static struct platform_driver xcan_driver = {
1409 .probe = xcan_probe,
1410 .remove = xcan_remove,
1412 .name = DRIVER_NAME,
1413 .pm = &xcan_dev_pm_ops,
1414 .of_match_table = xcan_of_match,
1418 module_platform_driver(xcan_driver);
1420 MODULE_LICENSE("GPL");
1421 MODULE_AUTHOR("Xilinx Inc");
1422 MODULE_DESCRIPTION("Xilinx CAN interface");