blob: 9bce846acfcc66d57036bde70e23dfc9cf509212 [file] [log] [blame]
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301/* Xilinx CAN device driver
2 *
3 * Copyright (C) 2012 - 2014 Xilinx, Inc.
4 * Copyright (C) 2009 PetaLogix. All rights reserved.
Anssi Hannula877e0b72017-02-08 13:13:40 +02005 * Copyright (C) 2017 Sandvik Mining and Construction Oy
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05306 *
7 * Description:
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.
13 *
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.
18 */
19
20#include <linux/clk.h>
21#include <linux/errno.h>
22#include <linux/init.h>
23#include <linux/interrupt.h>
24#include <linux/io.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/netdevice.h>
28#include <linux/of.h>
Anssi Hannula620050d2017-02-23 14:50:03 +020029#include <linux/of_device.h>
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +053030#include <linux/platform_device.h>
31#include <linux/skbuff.h>
Anssi Hannula620050d2017-02-23 14:50:03 +020032#include <linux/spinlock.h>
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +053033#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>
Kedareswara rao Appana47166202015-10-26 11:41:54 +053038#include <linux/pm_runtime.h>
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +053039
40#define DRIVER_NAME "xilinx_can"
41
42/* CAN registers set */
43enum xcan_reg {
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 */
62};
63
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 */
103
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 | \
Anssi Hannula83997992018-02-26 14:27:13 +0200107 XCAN_IXR_RXOFLW_MASK | XCAN_IXR_ARBLST_MASK)
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530108
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 */
116
117/* CAN frame length constants */
118#define XCAN_FRAME_MAX_DATA_LEN 8
119#define XCAN_TIMEOUT (1 * HZ)
120
121/**
122 * struct xcan_priv - This definition define CAN driver instance
123 * @can: CAN private data structure.
Anssi Hannula620050d2017-02-23 14:50:03 +0200124 * @tx_lock: Lock for synchronizing TX interrupt handling
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530125 * @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
136 */
137struct xcan_priv {
138 struct can_priv can;
Anssi Hannula620050d2017-02-23 14:50:03 +0200139 spinlock_t tx_lock;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530140 unsigned int tx_head;
141 unsigned int tx_tail;
142 unsigned int tx_max;
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,
146 u32 val);
Kedareswara rao Appana47166202015-10-26 11:41:54 +0530147 struct device *dev;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530148 void __iomem *reg_base;
149 unsigned long irq_flags;
150 struct clk *bus_clk;
151 struct clk *can_clk;
152};
153
154/* CAN Bittiming constants as per Xilinx CAN specs */
155static const struct can_bittiming_const xcan_bittiming_const = {
156 .name = DRIVER_NAME,
157 .tseg1_min = 1,
158 .tseg1_max = 16,
159 .tseg2_min = 1,
160 .tseg2_max = 8,
161 .sjw_max = 4,
162 .brp_min = 1,
163 .brp_max = 256,
164 .brp_inc = 1,
165};
166
Anssi Hannula620050d2017-02-23 14:50:03 +0200167#define XCAN_CAP_WATERMARK 0x0001
168struct xcan_devtype_data {
169 unsigned int caps;
170};
171
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530172/**
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
177 *
178 * Write data to the paricular CAN register
179 */
180static void xcan_write_reg_le(const struct xcan_priv *priv, enum xcan_reg reg,
181 u32 val)
182{
183 iowrite32(val, priv->reg_base + reg);
184}
185
186/**
187 * xcan_read_reg_le - Read a value from the device register little endian
188 * @priv: Driver private data structure
189 * @reg: Register offset
190 *
191 * Read data from the particular CAN register
192 * Return: value read from the CAN register
193 */
194static u32 xcan_read_reg_le(const struct xcan_priv *priv, enum xcan_reg reg)
195{
196 return ioread32(priv->reg_base + reg);
197}
198
199/**
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
204 *
205 * Write data to the paricular CAN register
206 */
207static void xcan_write_reg_be(const struct xcan_priv *priv, enum xcan_reg reg,
208 u32 val)
209{
210 iowrite32be(val, priv->reg_base + reg);
211}
212
213/**
214 * xcan_read_reg_be - Read a value from the device register big endian
215 * @priv: Driver private data structure
216 * @reg: Register offset
217 *
218 * Read data from the particular CAN register
219 * Return: value read from the CAN register
220 */
221static u32 xcan_read_reg_be(const struct xcan_priv *priv, enum xcan_reg reg)
222{
223 return ioread32be(priv->reg_base + reg);
224}
225
226/**
227 * set_reset_mode - Resets the CAN device mode
228 * @ndev: Pointer to net_device structure
229 *
230 * This is the driver reset mode routine.The driver
231 * enters into configuration mode.
232 *
233 * Return: 0 on success and failure value on error
234 */
235static int set_reset_mode(struct net_device *ndev)
236{
237 struct xcan_priv *priv = netdev_priv(ndev);
238 unsigned long timeout;
239
240 priv->write_reg(priv, XCAN_SRR_OFFSET, XCAN_SRR_RESET_MASK);
241
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");
246 return -ETIMEDOUT;
247 }
248 usleep_range(500, 10000);
249 }
250
Anssi Hannula620050d2017-02-23 14:50:03 +0200251 /* reset clears FIFOs */
252 priv->tx_head = 0;
253 priv->tx_tail = 0;
254
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530255 return 0;
256}
257
258/**
259 * xcan_set_bittiming - CAN set bit timing routine
260 * @ndev: Pointer to net_device structure
261 *
262 * This is the driver set bittiming routine.
263 * Return: 0 on success and failure value on error
264 */
265static int xcan_set_bittiming(struct net_device *ndev)
266{
267 struct xcan_priv *priv = netdev_priv(ndev);
268 struct can_bittiming *bt = &priv->can.bittiming;
269 u32 btr0, btr1;
270 u32 is_config_mode;
271
272 /* Check whether Xilinx CAN is in configuration mode.
273 * It cannot set bit timing if Xilinx CAN is not in configuration mode.
274 */
275 is_config_mode = priv->read_reg(priv, XCAN_SR_OFFSET) &
276 XCAN_SR_CONFIG_MASK;
277 if (!is_config_mode) {
278 netdev_alert(ndev,
279 "BUG! Cannot set bittiming - CAN is not in config mode\n");
280 return -EPERM;
281 }
282
283 /* Setting Baud Rate prescalar value in BRPR Register */
284 btr0 = (bt->brp - 1);
285
286 /* Setting Time Segment 1 in BTR Register */
287 btr1 = (bt->prop_seg + bt->phase_seg1 - 1);
288
289 /* Setting Time Segment 2 in BTR Register */
290 btr1 |= (bt->phase_seg2 - 1) << XCAN_BTR_TS2_SHIFT;
291
292 /* Setting Synchronous jump width in BTR Register */
293 btr1 |= (bt->sjw - 1) << XCAN_BTR_SJW_SHIFT;
294
295 priv->write_reg(priv, XCAN_BRPR_OFFSET, btr0);
296 priv->write_reg(priv, XCAN_BTR_OFFSET, btr1);
297
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));
301
302 return 0;
303}
304
305/**
306 * xcan_chip_start - This the drivers start routine
307 * @ndev: Pointer to net_device structure
308 *
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.
312 *
313 * Return: 0 on success and failure value on error
314 */
315static int xcan_chip_start(struct net_device *ndev)
316{
317 struct xcan_priv *priv = netdev_priv(ndev);
Sudip Mukherjeefb3ec7b2014-11-18 19:17:07 +0530318 u32 reg_msr, reg_sr_mask;
319 int err;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530320 unsigned long timeout;
321
322 /* Check if it is in reset mode */
323 err = set_reset_mode(ndev);
324 if (err < 0)
325 return err;
326
327 err = xcan_set_bittiming(ndev);
328 if (err < 0)
329 return err;
330
331 /* Enable interrupts */
332 priv->write_reg(priv, XCAN_IER_OFFSET, XCAN_INTR_ALL);
333
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;
338 } else {
339 reg_msr = 0x0;
340 reg_sr_mask = XCAN_SR_NORMAL_MASK;
341 }
342
343 priv->write_reg(priv, XCAN_MSR_OFFSET, reg_msr);
344 priv->write_reg(priv, XCAN_SRR_OFFSET, XCAN_SRR_CEN_MASK);
345
346 timeout = jiffies + XCAN_TIMEOUT;
347 while (!(priv->read_reg(priv, XCAN_SR_OFFSET) & reg_sr_mask)) {
348 if (time_after(jiffies, timeout)) {
349 netdev_warn(ndev,
350 "timed out for correct mode\n");
351 return -ETIMEDOUT;
352 }
353 }
354 netdev_dbg(ndev, "status:#x%08x\n",
355 priv->read_reg(priv, XCAN_SR_OFFSET));
356
357 priv->can.state = CAN_STATE_ERROR_ACTIVE;
358 return 0;
359}
360
361/**
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
365 *
366 * This check the drivers state and calls the
367 * the corresponding modes to set.
368 *
369 * Return: 0 on success and failure value on error
370 */
371static int xcan_do_set_mode(struct net_device *ndev, enum can_mode mode)
372{
373 int ret;
374
375 switch (mode) {
376 case CAN_MODE_START:
377 ret = xcan_chip_start(ndev);
378 if (ret < 0) {
379 netdev_err(ndev, "xcan_chip_start failed!\n");
380 return ret;
381 }
382 netif_wake_queue(ndev);
383 break;
384 default:
385 ret = -EOPNOTSUPP;
386 break;
387 }
388
389 return ret;
390}
391
392/**
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
396 *
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.
400 *
Luc Van Oostenryck97f2a582018-04-26 23:13:38 +0200401 * Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY when the tx queue is full
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530402 */
Luc Van Oostenryck97f2a582018-04-26 23:13:38 +0200403static netdev_tx_t xcan_start_xmit(struct sk_buff *skb, struct net_device *ndev)
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530404{
405 struct xcan_priv *priv = netdev_priv(ndev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530406 struct can_frame *cf = (struct can_frame *)skb->data;
407 u32 id, dlc, data[2] = {0, 0};
Anssi Hannula620050d2017-02-23 14:50:03 +0200408 unsigned long flags;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530409
410 if (can_dropped_invalid_skb(ndev, skb))
411 return NETDEV_TX_OK;
412
413 /* Check if the TX buffer is full */
414 if (unlikely(priv->read_reg(priv, XCAN_SR_OFFSET) &
415 XCAN_SR_TXFLL_MASK)) {
416 netif_stop_queue(ndev);
417 netdev_err(ndev, "BUG!, TX FIFO full when queue awake!\n");
418 return NETDEV_TX_BUSY;
419 }
420
421 /* Watch carefully on the bit sequence */
422 if (cf->can_id & CAN_EFF_FLAG) {
423 /* Extended CAN ID format */
424 id = ((cf->can_id & CAN_EFF_MASK) << XCAN_IDR_ID2_SHIFT) &
425 XCAN_IDR_ID2_MASK;
426 id |= (((cf->can_id & CAN_EFF_MASK) >>
427 (CAN_EFF_ID_BITS-CAN_SFF_ID_BITS)) <<
428 XCAN_IDR_ID1_SHIFT) & XCAN_IDR_ID1_MASK;
429
430 /* The substibute remote TX request bit should be "1"
431 * for extended frames as in the Xilinx CAN datasheet
432 */
433 id |= XCAN_IDR_IDE_MASK | XCAN_IDR_SRR_MASK;
434
435 if (cf->can_id & CAN_RTR_FLAG)
436 /* Extended frames remote TX request */
437 id |= XCAN_IDR_RTR_MASK;
438 } else {
439 /* Standard CAN ID format */
440 id = ((cf->can_id & CAN_SFF_MASK) << XCAN_IDR_ID1_SHIFT) &
441 XCAN_IDR_ID1_MASK;
442
443 if (cf->can_id & CAN_RTR_FLAG)
444 /* Standard frames remote TX request */
445 id |= XCAN_IDR_SRR_MASK;
446 }
447
448 dlc = cf->can_dlc << XCAN_DLCR_DLC_SHIFT;
449
450 if (cf->can_dlc > 0)
451 data[0] = be32_to_cpup((__be32 *)(cf->data + 0));
452 if (cf->can_dlc > 4)
453 data[1] = be32_to_cpup((__be32 *)(cf->data + 4));
454
455 can_put_echo_skb(skb, ndev, priv->tx_head % priv->tx_max);
Anssi Hannula620050d2017-02-23 14:50:03 +0200456
457 spin_lock_irqsave(&priv->tx_lock, flags);
458
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530459 priv->tx_head++;
460
461 /* Write the Frame to Xilinx CAN TX FIFO */
462 priv->write_reg(priv, XCAN_TXFIFO_ID_OFFSET, id);
463 /* If the CAN frame is RTR frame this write triggers tranmission */
464 priv->write_reg(priv, XCAN_TXFIFO_DLC_OFFSET, dlc);
465 if (!(cf->can_id & CAN_RTR_FLAG)) {
466 priv->write_reg(priv, XCAN_TXFIFO_DW1_OFFSET, data[0]);
467 /* If the CAN frame is Standard/Extended frame this
468 * write triggers tranmission
469 */
470 priv->write_reg(priv, XCAN_TXFIFO_DW2_OFFSET, data[1]);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530471 }
472
Anssi Hannula620050d2017-02-23 14:50:03 +0200473 /* Clear TX-FIFO-empty interrupt for xcan_tx_interrupt() */
474 if (priv->tx_max > 1)
475 priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_TXFEMP_MASK);
476
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530477 /* Check if the TX buffer is full */
478 if ((priv->tx_head - priv->tx_tail) == priv->tx_max)
479 netif_stop_queue(ndev);
480
Anssi Hannula620050d2017-02-23 14:50:03 +0200481 spin_unlock_irqrestore(&priv->tx_lock, flags);
482
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530483 return NETDEV_TX_OK;
484}
485
486/**
487 * xcan_rx - Is called from CAN isr to complete the received
488 * frame processing
489 * @ndev: Pointer to net_device structure
490 *
491 * This function is invoked from the CAN isr(poll) to process the Rx frames. It
492 * does minimal processing and invokes "netif_receive_skb" to complete further
493 * processing.
494 * Return: 1 on success and 0 on failure.
495 */
496static int xcan_rx(struct net_device *ndev)
497{
498 struct xcan_priv *priv = netdev_priv(ndev);
499 struct net_device_stats *stats = &ndev->stats;
500 struct can_frame *cf;
501 struct sk_buff *skb;
502 u32 id_xcan, dlc, data[2] = {0, 0};
503
504 skb = alloc_can_skb(ndev, &cf);
505 if (unlikely(!skb)) {
506 stats->rx_dropped++;
507 return 0;
508 }
509
510 /* Read a frame from Xilinx zynq CANPS */
511 id_xcan = priv->read_reg(priv, XCAN_RXFIFO_ID_OFFSET);
512 dlc = priv->read_reg(priv, XCAN_RXFIFO_DLC_OFFSET) >>
513 XCAN_DLCR_DLC_SHIFT;
514
515 /* Change Xilinx CAN data length format to socketCAN data format */
516 cf->can_dlc = get_can_dlc(dlc);
517
518 /* Change Xilinx CAN ID format to socketCAN ID format */
519 if (id_xcan & XCAN_IDR_IDE_MASK) {
520 /* The received frame is an Extended format frame */
521 cf->can_id = (id_xcan & XCAN_IDR_ID1_MASK) >> 3;
522 cf->can_id |= (id_xcan & XCAN_IDR_ID2_MASK) >>
523 XCAN_IDR_ID2_SHIFT;
524 cf->can_id |= CAN_EFF_FLAG;
525 if (id_xcan & XCAN_IDR_RTR_MASK)
526 cf->can_id |= CAN_RTR_FLAG;
527 } else {
528 /* The received frame is a standard format frame */
529 cf->can_id = (id_xcan & XCAN_IDR_ID1_MASK) >>
530 XCAN_IDR_ID1_SHIFT;
531 if (id_xcan & XCAN_IDR_SRR_MASK)
532 cf->can_id |= CAN_RTR_FLAG;
533 }
534
Jeppe Ledet-Pedersen5793aff2015-04-29 17:05:01 +0200535 /* DW1/DW2 must always be read to remove message from RXFIFO */
536 data[0] = priv->read_reg(priv, XCAN_RXFIFO_DW1_OFFSET);
537 data[1] = priv->read_reg(priv, XCAN_RXFIFO_DW2_OFFSET);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530538
Jeppe Ledet-Pedersen5793aff2015-04-29 17:05:01 +0200539 if (!(cf->can_id & CAN_RTR_FLAG)) {
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530540 /* Change Xilinx CAN data format to socketCAN data format */
541 if (cf->can_dlc > 0)
542 *(__be32 *)(cf->data) = cpu_to_be32(data[0]);
543 if (cf->can_dlc > 4)
544 *(__be32 *)(cf->data + 4) = cpu_to_be32(data[1]);
545 }
546
547 stats->rx_bytes += cf->can_dlc;
548 stats->rx_packets++;
549 netif_receive_skb(skb);
550
551 return 1;
552}
553
554/**
Anssi Hannula877e0b72017-02-08 13:13:40 +0200555 * xcan_current_error_state - Get current error state from HW
556 * @ndev: Pointer to net_device structure
557 *
558 * Checks the current CAN error state from the HW. Note that this
559 * only checks for ERROR_PASSIVE and ERROR_WARNING.
560 *
561 * Return:
562 * ERROR_PASSIVE or ERROR_WARNING if either is active, ERROR_ACTIVE
563 * otherwise.
564 */
565static enum can_state xcan_current_error_state(struct net_device *ndev)
566{
567 struct xcan_priv *priv = netdev_priv(ndev);
568 u32 status = priv->read_reg(priv, XCAN_SR_OFFSET);
569
570 if ((status & XCAN_SR_ESTAT_MASK) == XCAN_SR_ESTAT_MASK)
571 return CAN_STATE_ERROR_PASSIVE;
572 else if (status & XCAN_SR_ERRWRN_MASK)
573 return CAN_STATE_ERROR_WARNING;
574 else
575 return CAN_STATE_ERROR_ACTIVE;
576}
577
578/**
579 * xcan_set_error_state - Set new CAN error state
580 * @ndev: Pointer to net_device structure
581 * @new_state: The new CAN state to be set
582 * @cf: Error frame to be populated or NULL
583 *
584 * Set new CAN error state for the device, updating statistics and
585 * populating the error frame if given.
586 */
587static void xcan_set_error_state(struct net_device *ndev,
588 enum can_state new_state,
589 struct can_frame *cf)
590{
591 struct xcan_priv *priv = netdev_priv(ndev);
592 u32 ecr = priv->read_reg(priv, XCAN_ECR_OFFSET);
593 u32 txerr = ecr & XCAN_ECR_TEC_MASK;
594 u32 rxerr = (ecr & XCAN_ECR_REC_MASK) >> XCAN_ESR_REC_SHIFT;
Anssi Hannula6181dbc2018-01-29 17:28:24 +0200595 enum can_state tx_state = txerr >= rxerr ? new_state : 0;
596 enum can_state rx_state = txerr <= rxerr ? new_state : 0;
Anssi Hannula877e0b72017-02-08 13:13:40 +0200597
Anssi Hannula6181dbc2018-01-29 17:28:24 +0200598 /* non-ERROR states are handled elsewhere */
599 if (WARN_ON(new_state > CAN_STATE_ERROR_PASSIVE))
600 return;
601
602 can_change_state(ndev, cf, tx_state, rx_state);
Anssi Hannula877e0b72017-02-08 13:13:40 +0200603
604 if (cf) {
Anssi Hannula877e0b72017-02-08 13:13:40 +0200605 cf->data[6] = txerr;
606 cf->data[7] = rxerr;
607 }
Anssi Hannula877e0b72017-02-08 13:13:40 +0200608}
609
610/**
611 * xcan_update_error_state_after_rxtx - Update CAN error state after RX/TX
612 * @ndev: Pointer to net_device structure
613 *
614 * If the device is in a ERROR-WARNING or ERROR-PASSIVE state, check if
615 * the performed RX/TX has caused it to drop to a lesser state and set
616 * the interface state accordingly.
617 */
618static void xcan_update_error_state_after_rxtx(struct net_device *ndev)
619{
620 struct xcan_priv *priv = netdev_priv(ndev);
621 enum can_state old_state = priv->can.state;
622 enum can_state new_state;
623
624 /* changing error state due to successful frame RX/TX can only
625 * occur from these states
626 */
627 if (old_state != CAN_STATE_ERROR_WARNING &&
628 old_state != CAN_STATE_ERROR_PASSIVE)
629 return;
630
631 new_state = xcan_current_error_state(ndev);
632
633 if (new_state != old_state) {
634 struct sk_buff *skb;
635 struct can_frame *cf;
636
637 skb = alloc_can_err_skb(ndev, &cf);
638
639 xcan_set_error_state(ndev, new_state, skb ? cf : NULL);
640
641 if (skb) {
642 struct net_device_stats *stats = &ndev->stats;
643
644 stats->rx_packets++;
645 stats->rx_bytes += cf->can_dlc;
646 netif_rx(skb);
647 }
648 }
649}
650
651/**
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530652 * xcan_err_interrupt - error frame Isr
653 * @ndev: net_device pointer
654 * @isr: interrupt status register value
655 *
656 * This is the CAN error interrupt and it will
657 * check the the type of error and forward the error
658 * frame to upper layers.
659 */
660static void xcan_err_interrupt(struct net_device *ndev, u32 isr)
661{
662 struct xcan_priv *priv = netdev_priv(ndev);
663 struct net_device_stats *stats = &ndev->stats;
664 struct can_frame *cf;
665 struct sk_buff *skb;
Anssi Hannula877e0b72017-02-08 13:13:40 +0200666 u32 err_status;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530667
668 skb = alloc_can_err_skb(ndev, &cf);
669
670 err_status = priv->read_reg(priv, XCAN_ESR_OFFSET);
671 priv->write_reg(priv, XCAN_ESR_OFFSET, err_status);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530672
673 if (isr & XCAN_IXR_BSOFF_MASK) {
674 priv->can.state = CAN_STATE_BUS_OFF;
675 priv->can.can_stats.bus_off++;
676 /* Leave device in Config Mode in bus-off state */
677 priv->write_reg(priv, XCAN_SRR_OFFSET, XCAN_SRR_RESET_MASK);
678 can_bus_off(ndev);
679 if (skb)
680 cf->can_id |= CAN_ERR_BUSOFF;
Anssi Hannula877e0b72017-02-08 13:13:40 +0200681 } else {
682 enum can_state new_state = xcan_current_error_state(ndev);
683
Anssi Hannula7e2804a2017-02-08 13:32:43 +0200684 if (new_state != priv->can.state)
685 xcan_set_error_state(ndev, new_state, skb ? cf : NULL);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530686 }
687
688 /* Check for Arbitration lost interrupt */
689 if (isr & XCAN_IXR_ARBLST_MASK) {
690 priv->can.can_stats.arbitration_lost++;
691 if (skb) {
692 cf->can_id |= CAN_ERR_LOSTARB;
693 cf->data[0] = CAN_ERR_LOSTARB_UNSPEC;
694 }
695 }
696
697 /* Check for RX FIFO Overflow interrupt */
698 if (isr & XCAN_IXR_RXOFLW_MASK) {
699 stats->rx_over_errors++;
700 stats->rx_errors++;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530701 if (skb) {
702 cf->can_id |= CAN_ERR_CRTL;
703 cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
704 }
705 }
706
707 /* Check for error interrupt */
708 if (isr & XCAN_IXR_ERROR_MASK) {
Oliver Hartkoppa2ec19f2015-11-21 18:41:21 +0100709 if (skb)
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530710 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530711
712 /* Check for Ack error interrupt */
713 if (err_status & XCAN_ESR_ACKER_MASK) {
714 stats->tx_errors++;
715 if (skb) {
716 cf->can_id |= CAN_ERR_ACK;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100717 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530718 }
719 }
720
721 /* Check for Bit error interrupt */
722 if (err_status & XCAN_ESR_BERR_MASK) {
723 stats->tx_errors++;
724 if (skb) {
725 cf->can_id |= CAN_ERR_PROT;
726 cf->data[2] = CAN_ERR_PROT_BIT;
727 }
728 }
729
730 /* Check for Stuff error interrupt */
731 if (err_status & XCAN_ESR_STER_MASK) {
732 stats->rx_errors++;
733 if (skb) {
734 cf->can_id |= CAN_ERR_PROT;
735 cf->data[2] = CAN_ERR_PROT_STUFF;
736 }
737 }
738
739 /* Check for Form error interrupt */
740 if (err_status & XCAN_ESR_FMER_MASK) {
741 stats->rx_errors++;
742 if (skb) {
743 cf->can_id |= CAN_ERR_PROT;
744 cf->data[2] = CAN_ERR_PROT_FORM;
745 }
746 }
747
748 /* Check for CRC error interrupt */
749 if (err_status & XCAN_ESR_CRCER_MASK) {
750 stats->rx_errors++;
751 if (skb) {
752 cf->can_id |= CAN_ERR_PROT;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100753 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530754 }
755 }
756 priv->can.can_stats.bus_error++;
757 }
758
759 if (skb) {
760 stats->rx_packets++;
761 stats->rx_bytes += cf->can_dlc;
762 netif_rx(skb);
763 }
764
765 netdev_dbg(ndev, "%s: error status register:0x%x\n",
766 __func__, priv->read_reg(priv, XCAN_ESR_OFFSET));
767}
768
769/**
770 * xcan_state_interrupt - It will check the state of the CAN device
771 * @ndev: net_device pointer
772 * @isr: interrupt status register value
773 *
774 * This will checks the state of the CAN device
775 * and puts the device into appropriate state.
776 */
777static void xcan_state_interrupt(struct net_device *ndev, u32 isr)
778{
779 struct xcan_priv *priv = netdev_priv(ndev);
780
781 /* Check for Sleep interrupt if set put CAN device in sleep state */
782 if (isr & XCAN_IXR_SLP_MASK)
783 priv->can.state = CAN_STATE_SLEEPING;
784
785 /* Check for Wake up interrupt if set put CAN device in Active state */
786 if (isr & XCAN_IXR_WKUP_MASK)
787 priv->can.state = CAN_STATE_ERROR_ACTIVE;
788}
789
790/**
791 * xcan_rx_poll - Poll routine for rx packets (NAPI)
792 * @napi: napi structure pointer
793 * @quota: Max number of rx packets to be processed.
794 *
795 * This is the poll routine for rx part.
796 * It will process the packets maximux quota value.
797 *
798 * Return: number of packets received
799 */
800static int xcan_rx_poll(struct napi_struct *napi, int quota)
801{
802 struct net_device *ndev = napi->dev;
803 struct xcan_priv *priv = netdev_priv(ndev);
804 u32 isr, ier;
805 int work_done = 0;
806
807 isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
808 while ((isr & XCAN_IXR_RXNEMP_MASK) && (work_done < quota)) {
Anssi Hannula32852c52017-02-07 17:01:14 +0200809 work_done += xcan_rx(ndev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530810 priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_RXNEMP_MASK);
811 isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
812 }
813
Anssi Hannula877e0b72017-02-08 13:13:40 +0200814 if (work_done) {
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530815 can_led_event(ndev, CAN_LED_EVENT_RX);
Anssi Hannula877e0b72017-02-08 13:13:40 +0200816 xcan_update_error_state_after_rxtx(ndev);
817 }
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530818
819 if (work_done < quota) {
Eric Dumazet6ad20162017-01-30 08:22:01 -0800820 napi_complete_done(napi, work_done);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530821 ier = priv->read_reg(priv, XCAN_IER_OFFSET);
Anssi Hannula32852c52017-02-07 17:01:14 +0200822 ier |= XCAN_IXR_RXNEMP_MASK;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530823 priv->write_reg(priv, XCAN_IER_OFFSET, ier);
824 }
825 return work_done;
826}
827
828/**
829 * xcan_tx_interrupt - Tx Done Isr
830 * @ndev: net_device pointer
831 * @isr: Interrupt status register value
832 */
833static void xcan_tx_interrupt(struct net_device *ndev, u32 isr)
834{
835 struct xcan_priv *priv = netdev_priv(ndev);
836 struct net_device_stats *stats = &ndev->stats;
Anssi Hannula620050d2017-02-23 14:50:03 +0200837 unsigned int frames_in_fifo;
838 int frames_sent = 1; /* TXOK => at least 1 frame was sent */
839 unsigned long flags;
840 int retries = 0;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530841
Anssi Hannula620050d2017-02-23 14:50:03 +0200842 /* Synchronize with xmit as we need to know the exact number
843 * of frames in the FIFO to stay in sync due to the TXFEMP
844 * handling.
845 * This also prevents a race between netif_wake_queue() and
846 * netif_stop_queue().
847 */
848 spin_lock_irqsave(&priv->tx_lock, flags);
849
850 frames_in_fifo = priv->tx_head - priv->tx_tail;
851
852 if (WARN_ON_ONCE(frames_in_fifo == 0)) {
853 /* clear TXOK anyway to avoid getting back here */
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530854 priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_TXOK_MASK);
Anssi Hannula620050d2017-02-23 14:50:03 +0200855 spin_unlock_irqrestore(&priv->tx_lock, flags);
856 return;
857 }
858
859 /* Check if 2 frames were sent (TXOK only means that at least 1
860 * frame was sent).
861 */
862 if (frames_in_fifo > 1) {
863 WARN_ON(frames_in_fifo > priv->tx_max);
864
865 /* Synchronize TXOK and isr so that after the loop:
866 * (1) isr variable is up-to-date at least up to TXOK clear
867 * time. This avoids us clearing a TXOK of a second frame
868 * but not noticing that the FIFO is now empty and thus
869 * marking only a single frame as sent.
870 * (2) No TXOK is left. Having one could mean leaving a
871 * stray TXOK as we might process the associated frame
872 * via TXFEMP handling as we read TXFEMP *after* TXOK
873 * clear to satisfy (1).
874 */
875 while ((isr & XCAN_IXR_TXOK_MASK) && !WARN_ON(++retries == 100)) {
876 priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_TXOK_MASK);
877 isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
878 }
879
880 if (isr & XCAN_IXR_TXFEMP_MASK) {
881 /* nothing in FIFO anymore */
882 frames_sent = frames_in_fifo;
883 }
884 } else {
885 /* single frame in fifo, just clear TXOK */
886 priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_TXOK_MASK);
887 }
888
889 while (frames_sent--) {
Anssi Hannula11ee5fc2018-02-26 14:50:35 +0200890 stats->tx_bytes += can_get_echo_skb(ndev, priv->tx_tail %
891 priv->tx_max);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530892 priv->tx_tail++;
893 stats->tx_packets++;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530894 }
Anssi Hannula620050d2017-02-23 14:50:03 +0200895
896 netif_wake_queue(ndev);
897
898 spin_unlock_irqrestore(&priv->tx_lock, flags);
899
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530900 can_led_event(ndev, CAN_LED_EVENT_TX);
Anssi Hannula877e0b72017-02-08 13:13:40 +0200901 xcan_update_error_state_after_rxtx(ndev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530902}
903
904/**
905 * xcan_interrupt - CAN Isr
906 * @irq: irq number
907 * @dev_id: device id poniter
908 *
909 * This is the xilinx CAN Isr. It checks for the type of interrupt
910 * and invokes the corresponding ISR.
911 *
912 * Return:
913 * IRQ_NONE - If CAN device is in sleep mode, IRQ_HANDLED otherwise
914 */
915static irqreturn_t xcan_interrupt(int irq, void *dev_id)
916{
917 struct net_device *ndev = (struct net_device *)dev_id;
918 struct xcan_priv *priv = netdev_priv(ndev);
919 u32 isr, ier;
Anssi Hannula2f4f0f32018-02-26 14:39:59 +0200920 u32 isr_errors;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530921
922 /* Get the interrupt status from Xilinx CAN */
923 isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
924 if (!isr)
925 return IRQ_NONE;
926
927 /* Check for the type of interrupt and Processing it */
928 if (isr & (XCAN_IXR_SLP_MASK | XCAN_IXR_WKUP_MASK)) {
929 priv->write_reg(priv, XCAN_ICR_OFFSET, (XCAN_IXR_SLP_MASK |
930 XCAN_IXR_WKUP_MASK));
931 xcan_state_interrupt(ndev, isr);
932 }
933
934 /* Check for Tx interrupt and Processing it */
935 if (isr & XCAN_IXR_TXOK_MASK)
936 xcan_tx_interrupt(ndev, isr);
937
938 /* Check for the type of error interrupt and Processing it */
Anssi Hannula2f4f0f32018-02-26 14:39:59 +0200939 isr_errors = isr & (XCAN_IXR_ERROR_MASK | XCAN_IXR_RXOFLW_MASK |
940 XCAN_IXR_BSOFF_MASK | XCAN_IXR_ARBLST_MASK);
941 if (isr_errors) {
942 priv->write_reg(priv, XCAN_ICR_OFFSET, isr_errors);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530943 xcan_err_interrupt(ndev, isr);
944 }
945
946 /* Check for the type of receive interrupt and Processing it */
Anssi Hannula32852c52017-02-07 17:01:14 +0200947 if (isr & XCAN_IXR_RXNEMP_MASK) {
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530948 ier = priv->read_reg(priv, XCAN_IER_OFFSET);
Anssi Hannula32852c52017-02-07 17:01:14 +0200949 ier &= ~XCAN_IXR_RXNEMP_MASK;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530950 priv->write_reg(priv, XCAN_IER_OFFSET, ier);
951 napi_schedule(&priv->napi);
952 }
953 return IRQ_HANDLED;
954}
955
956/**
957 * xcan_chip_stop - Driver stop routine
958 * @ndev: Pointer to net_device structure
959 *
960 * This is the drivers stop routine. It will disable the
961 * interrupts and put the device into configuration mode.
962 */
963static void xcan_chip_stop(struct net_device *ndev)
964{
965 struct xcan_priv *priv = netdev_priv(ndev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530966
967 /* Disable interrupts and leave the can in configuration mode */
Anssi Hannula8ebd83b2018-05-17 15:41:19 +0300968 set_reset_mode(ndev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530969 priv->can.state = CAN_STATE_STOPPED;
970}
971
972/**
973 * xcan_open - Driver open routine
974 * @ndev: Pointer to net_device structure
975 *
976 * This is the driver open routine.
977 * Return: 0 on success and failure value on error
978 */
979static int xcan_open(struct net_device *ndev)
980{
981 struct xcan_priv *priv = netdev_priv(ndev);
982 int ret;
983
Kedareswara rao Appana47166202015-10-26 11:41:54 +0530984 ret = pm_runtime_get_sync(priv->dev);
985 if (ret < 0) {
986 netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
987 __func__, ret);
988 return ret;
989 }
990
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530991 ret = request_irq(ndev->irq, xcan_interrupt, priv->irq_flags,
992 ndev->name, ndev);
993 if (ret < 0) {
994 netdev_err(ndev, "irq allocation for CAN failed\n");
995 goto err;
996 }
997
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530998 /* Set chip into reset mode */
999 ret = set_reset_mode(ndev);
1000 if (ret < 0) {
1001 netdev_err(ndev, "mode resetting failed!\n");
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301002 goto err_irq;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301003 }
1004
1005 /* Common open */
1006 ret = open_candev(ndev);
1007 if (ret)
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301008 goto err_irq;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301009
1010 ret = xcan_chip_start(ndev);
1011 if (ret < 0) {
1012 netdev_err(ndev, "xcan_chip_start failed!\n");
1013 goto err_candev;
1014 }
1015
1016 can_led_event(ndev, CAN_LED_EVENT_OPEN);
1017 napi_enable(&priv->napi);
1018 netif_start_queue(ndev);
1019
1020 return 0;
1021
1022err_candev:
1023 close_candev(ndev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301024err_irq:
1025 free_irq(ndev->irq, ndev);
1026err:
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301027 pm_runtime_put(priv->dev);
1028
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301029 return ret;
1030}
1031
1032/**
1033 * xcan_close - Driver close routine
1034 * @ndev: Pointer to net_device structure
1035 *
1036 * Return: 0 always
1037 */
1038static int xcan_close(struct net_device *ndev)
1039{
1040 struct xcan_priv *priv = netdev_priv(ndev);
1041
1042 netif_stop_queue(ndev);
1043 napi_disable(&priv->napi);
1044 xcan_chip_stop(ndev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301045 free_irq(ndev->irq, ndev);
1046 close_candev(ndev);
1047
1048 can_led_event(ndev, CAN_LED_EVENT_STOP);
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301049 pm_runtime_put(priv->dev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301050
1051 return 0;
1052}
1053
1054/**
1055 * xcan_get_berr_counter - error counter routine
1056 * @ndev: Pointer to net_device structure
1057 * @bec: Pointer to can_berr_counter structure
1058 *
1059 * This is the driver error counter routine.
1060 * Return: 0 on success and failure value on error
1061 */
1062static int xcan_get_berr_counter(const struct net_device *ndev,
1063 struct can_berr_counter *bec)
1064{
1065 struct xcan_priv *priv = netdev_priv(ndev);
1066 int ret;
1067
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301068 ret = pm_runtime_get_sync(priv->dev);
1069 if (ret < 0) {
1070 netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
1071 __func__, ret);
1072 return ret;
1073 }
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301074
1075 bec->txerr = priv->read_reg(priv, XCAN_ECR_OFFSET) & XCAN_ECR_TEC_MASK;
1076 bec->rxerr = ((priv->read_reg(priv, XCAN_ECR_OFFSET) &
1077 XCAN_ECR_REC_MASK) >> XCAN_ESR_REC_SHIFT);
1078
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301079 pm_runtime_put(priv->dev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301080
1081 return 0;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301082}
1083
1084
1085static const struct net_device_ops xcan_netdev_ops = {
1086 .ndo_open = xcan_open,
1087 .ndo_stop = xcan_close,
1088 .ndo_start_xmit = xcan_start_xmit,
Marc Kleine-Budde92593a02014-11-18 13:16:13 +01001089 .ndo_change_mtu = can_change_mtu,
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301090};
1091
1092/**
1093 * xcan_suspend - Suspend method for the driver
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301094 * @dev: Address of the device structure
1095 *
1096 * Put the driver into low power mode.
1097 * Return: 0 on success and failure value on error
1098 */
1099static int __maybe_unused xcan_suspend(struct device *dev)
1100{
Anssi Hannula8ebd83b2018-05-17 15:41:19 +03001101 struct net_device *ndev = dev_get_drvdata(dev);
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301102
Anssi Hannula8ebd83b2018-05-17 15:41:19 +03001103 if (netif_running(ndev)) {
1104 netif_stop_queue(ndev);
1105 netif_device_detach(ndev);
1106 xcan_chip_stop(ndev);
1107 }
1108
1109 return pm_runtime_force_suspend(dev);
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301110}
1111
1112/**
1113 * xcan_resume - Resume from suspend
1114 * @dev: Address of the device structure
1115 *
1116 * Resume operation after suspend.
1117 * Return: 0 on success and failure value on error
1118 */
1119static int __maybe_unused xcan_resume(struct device *dev)
1120{
Anssi Hannula8ebd83b2018-05-17 15:41:19 +03001121 struct net_device *ndev = dev_get_drvdata(dev);
1122 int ret;
1123
1124 ret = pm_runtime_force_resume(dev);
1125 if (ret) {
1126 dev_err(dev, "pm_runtime_force_resume failed on resume\n");
1127 return ret;
1128 }
1129
1130 if (netif_running(ndev)) {
1131 ret = xcan_chip_start(ndev);
1132 if (ret) {
1133 dev_err(dev, "xcan_chip_start failed on resume\n");
1134 return ret;
1135 }
1136
1137 netif_device_attach(ndev);
1138 netif_start_queue(ndev);
1139 }
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301140
1141 return 0;
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301142}
1143
1144/**
1145 * xcan_runtime_suspend - Runtime suspend method for the driver
1146 * @dev: Address of the device structure
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301147 *
1148 * Put the driver into low power mode.
1149 * Return: 0 always
1150 */
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301151static int __maybe_unused xcan_runtime_suspend(struct device *dev)
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301152{
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301153 struct net_device *ndev = dev_get_drvdata(dev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301154 struct xcan_priv *priv = netdev_priv(ndev);
1155
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301156 clk_disable_unprepare(priv->bus_clk);
1157 clk_disable_unprepare(priv->can_clk);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301158
1159 return 0;
1160}
1161
1162/**
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301163 * xcan_runtime_resume - Runtime resume from suspend
1164 * @dev: Address of the device structure
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301165 *
1166 * Resume operation after suspend.
1167 * Return: 0 on success and failure value on error
1168 */
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301169static int __maybe_unused xcan_runtime_resume(struct device *dev)
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301170{
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301171 struct net_device *ndev = dev_get_drvdata(dev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301172 struct xcan_priv *priv = netdev_priv(ndev);
1173 int ret;
1174
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301175 ret = clk_prepare_enable(priv->bus_clk);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301176 if (ret) {
1177 dev_err(dev, "Cannot enable clock.\n");
1178 return ret;
1179 }
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301180 ret = clk_prepare_enable(priv->can_clk);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301181 if (ret) {
1182 dev_err(dev, "Cannot enable clock.\n");
1183 clk_disable_unprepare(priv->bus_clk);
1184 return ret;
1185 }
1186
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301187 return 0;
1188}
1189
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301190static const struct dev_pm_ops xcan_dev_pm_ops = {
1191 SET_SYSTEM_SLEEP_PM_OPS(xcan_suspend, xcan_resume)
1192 SET_RUNTIME_PM_OPS(xcan_runtime_suspend, xcan_runtime_resume, NULL)
1193};
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301194
Anssi Hannula620050d2017-02-23 14:50:03 +02001195static const struct xcan_devtype_data xcan_zynq_data = {
1196 .caps = XCAN_CAP_WATERMARK,
1197};
1198
1199/* Match table for OF platform binding */
1200static const struct of_device_id xcan_of_match[] = {
1201 { .compatible = "xlnx,zynq-can-1.0", .data = &xcan_zynq_data },
1202 { .compatible = "xlnx,axi-can-1.00.a", },
1203 { /* end of list */ },
1204};
1205MODULE_DEVICE_TABLE(of, xcan_of_match);
1206
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301207/**
1208 * xcan_probe - Platform registration call
1209 * @pdev: Handle to the platform device structure
1210 *
1211 * This function does all the memory allocation and registration for the CAN
1212 * device.
1213 *
1214 * Return: 0 on success and failure value on error
1215 */
1216static int xcan_probe(struct platform_device *pdev)
1217{
1218 struct resource *res; /* IO mem resources */
1219 struct net_device *ndev;
1220 struct xcan_priv *priv;
Anssi Hannula620050d2017-02-23 14:50:03 +02001221 const struct of_device_id *of_id;
1222 int caps = 0;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301223 void __iomem *addr;
Anssi Hannula620050d2017-02-23 14:50:03 +02001224 int ret, rx_max, tx_max, tx_fifo_depth;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301225
1226 /* Get the virtual base address for the device */
1227 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1228 addr = devm_ioremap_resource(&pdev->dev, res);
1229 if (IS_ERR(addr)) {
1230 ret = PTR_ERR(addr);
1231 goto err;
1232 }
1233
Anssi Hannula620050d2017-02-23 14:50:03 +02001234 ret = of_property_read_u32(pdev->dev.of_node, "tx-fifo-depth",
1235 &tx_fifo_depth);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301236 if (ret < 0)
1237 goto err;
1238
1239 ret = of_property_read_u32(pdev->dev.of_node, "rx-fifo-depth", &rx_max);
1240 if (ret < 0)
1241 goto err;
1242
Anssi Hannula620050d2017-02-23 14:50:03 +02001243 of_id = of_match_device(xcan_of_match, &pdev->dev);
1244 if (of_id) {
1245 const struct xcan_devtype_data *devtype_data = of_id->data;
1246
1247 if (devtype_data)
1248 caps = devtype_data->caps;
1249 }
1250
1251 /* There is no way to directly figure out how many frames have been
1252 * sent when the TXOK interrupt is processed. If watermark programming
1253 * is supported, we can have 2 frames in the FIFO and use TXFEMP
1254 * to determine if 1 or 2 frames have been sent.
1255 * Theoretically we should be able to use TXFWMEMP to determine up
1256 * to 3 frames, but it seems that after putting a second frame in the
1257 * FIFO, with watermark at 2 frames, it can happen that TXFWMEMP (less
1258 * than 2 frames in FIFO) is set anyway with no TXOK (a frame was
1259 * sent), which is not a sensible state - possibly TXFWMEMP is not
1260 * completely synchronized with the rest of the bits?
1261 */
1262 if (caps & XCAN_CAP_WATERMARK)
1263 tx_max = min(tx_fifo_depth, 2);
1264 else
1265 tx_max = 1;
1266
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301267 /* Create a CAN device instance */
1268 ndev = alloc_candev(sizeof(struct xcan_priv), tx_max);
1269 if (!ndev)
1270 return -ENOMEM;
1271
1272 priv = netdev_priv(ndev);
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301273 priv->dev = &pdev->dev;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301274 priv->can.bittiming_const = &xcan_bittiming_const;
1275 priv->can.do_set_mode = xcan_do_set_mode;
1276 priv->can.do_get_berr_counter = xcan_get_berr_counter;
1277 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1278 CAN_CTRLMODE_BERR_REPORTING;
1279 priv->reg_base = addr;
1280 priv->tx_max = tx_max;
Anssi Hannula620050d2017-02-23 14:50:03 +02001281 spin_lock_init(&priv->tx_lock);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301282
1283 /* Get IRQ for the device */
1284 ndev->irq = platform_get_irq(pdev, 0);
1285 ndev->flags |= IFF_ECHO; /* We support local echo */
1286
1287 platform_set_drvdata(pdev, ndev);
1288 SET_NETDEV_DEV(ndev, &pdev->dev);
1289 ndev->netdev_ops = &xcan_netdev_ops;
1290
1291 /* Getting the CAN can_clk info */
1292 priv->can_clk = devm_clk_get(&pdev->dev, "can_clk");
1293 if (IS_ERR(priv->can_clk)) {
1294 dev_err(&pdev->dev, "Device clock not found.\n");
1295 ret = PTR_ERR(priv->can_clk);
1296 goto err_free;
1297 }
1298 /* Check for type of CAN device */
1299 if (of_device_is_compatible(pdev->dev.of_node,
1300 "xlnx,zynq-can-1.0")) {
1301 priv->bus_clk = devm_clk_get(&pdev->dev, "pclk");
1302 if (IS_ERR(priv->bus_clk)) {
1303 dev_err(&pdev->dev, "bus clock not found\n");
1304 ret = PTR_ERR(priv->bus_clk);
1305 goto err_free;
1306 }
1307 } else {
1308 priv->bus_clk = devm_clk_get(&pdev->dev, "s_axi_aclk");
1309 if (IS_ERR(priv->bus_clk)) {
1310 dev_err(&pdev->dev, "bus clock not found\n");
1311 ret = PTR_ERR(priv->bus_clk);
1312 goto err_free;
1313 }
1314 }
1315
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301316 priv->write_reg = xcan_write_reg_le;
1317 priv->read_reg = xcan_read_reg_le;
1318
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301319 pm_runtime_enable(&pdev->dev);
1320 ret = pm_runtime_get_sync(&pdev->dev);
1321 if (ret < 0) {
1322 netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
1323 __func__, ret);
1324 goto err_pmdisable;
1325 }
1326
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301327 if (priv->read_reg(priv, XCAN_SR_OFFSET) != XCAN_SR_CONFIG_MASK) {
1328 priv->write_reg = xcan_write_reg_be;
1329 priv->read_reg = xcan_read_reg_be;
1330 }
1331
1332 priv->can.clock.freq = clk_get_rate(priv->can_clk);
1333
1334 netif_napi_add(ndev, &priv->napi, xcan_rx_poll, rx_max);
1335
1336 ret = register_candev(ndev);
1337 if (ret) {
1338 dev_err(&pdev->dev, "fail to register failed (err=%d)\n", ret);
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301339 goto err_disableclks;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301340 }
1341
1342 devm_can_led_init(ndev);
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301343
1344 pm_runtime_put(&pdev->dev);
1345
Anssi Hannula620050d2017-02-23 14:50:03 +02001346 netdev_dbg(ndev, "reg_base=0x%p irq=%d clock=%d, tx fifo depth: actual %d, using %d\n",
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301347 priv->reg_base, ndev->irq, priv->can.clock.freq,
Anssi Hannula620050d2017-02-23 14:50:03 +02001348 tx_fifo_depth, priv->tx_max);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301349
1350 return 0;
1351
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301352err_disableclks:
1353 pm_runtime_put(priv->dev);
1354err_pmdisable:
1355 pm_runtime_disable(&pdev->dev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301356err_free:
1357 free_candev(ndev);
1358err:
1359 return ret;
1360}
1361
1362/**
1363 * xcan_remove - Unregister the device after releasing the resources
1364 * @pdev: Handle to the platform device structure
1365 *
1366 * This function frees all the resources allocated to the device.
1367 * Return: 0 always
1368 */
1369static int xcan_remove(struct platform_device *pdev)
1370{
1371 struct net_device *ndev = platform_get_drvdata(pdev);
1372 struct xcan_priv *priv = netdev_priv(ndev);
1373
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301374 unregister_candev(ndev);
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301375 pm_runtime_disable(&pdev->dev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301376 netif_napi_del(&priv->napi);
1377 free_candev(ndev);
1378
1379 return 0;
1380}
1381
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301382static struct platform_driver xcan_driver = {
1383 .probe = xcan_probe,
1384 .remove = xcan_remove,
1385 .driver = {
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301386 .name = DRIVER_NAME,
1387 .pm = &xcan_dev_pm_ops,
1388 .of_match_table = xcan_of_match,
1389 },
1390};
1391
1392module_platform_driver(xcan_driver);
1393
1394MODULE_LICENSE("GPL");
1395MODULE_AUTHOR("Xilinx Inc");
1396MODULE_DESCRIPTION("Xilinx CAN interface");