blob: 204a7890dc9cf5989687360628b6fb7f572c745d [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 */
Anssi Hannula1598efe2018-02-26 15:56:51 +020054 XCAN_TXFIFO_OFFSET = 0x30, /* TX FIFO base */
55 XCAN_RXFIFO_OFFSET = 0x50, /* RX FIFO base */
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +053056};
57
Anssi Hannula1598efe2018-02-26 15:56:51 +020058#define XCAN_FRAME_ID_OFFSET(frame_base) ((frame_base) + 0x00)
59#define XCAN_FRAME_DLC_OFFSET(frame_base) ((frame_base) + 0x04)
60#define XCAN_FRAME_DW1_OFFSET(frame_base) ((frame_base) + 0x08)
61#define XCAN_FRAME_DW2_OFFSET(frame_base) ((frame_base) + 0x0C)
62
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +053063/* CAN register bit masks - XCAN_<REG>_<BIT>_MASK */
64#define XCAN_SRR_CEN_MASK 0x00000002 /* CAN enable */
65#define XCAN_SRR_RESET_MASK 0x00000001 /* Soft Reset the CAN core */
66#define XCAN_MSR_LBACK_MASK 0x00000002 /* Loop back mode select */
67#define XCAN_MSR_SLEEP_MASK 0x00000001 /* Sleep mode select */
68#define XCAN_BRPR_BRP_MASK 0x000000FF /* Baud rate prescaler */
69#define XCAN_BTR_SJW_MASK 0x00000180 /* Synchronous jump width */
70#define XCAN_BTR_TS2_MASK 0x00000070 /* Time segment 2 */
71#define XCAN_BTR_TS1_MASK 0x0000000F /* Time segment 1 */
72#define XCAN_ECR_REC_MASK 0x0000FF00 /* Receive error counter */
73#define XCAN_ECR_TEC_MASK 0x000000FF /* Transmit error counter */
74#define XCAN_ESR_ACKER_MASK 0x00000010 /* ACK error */
75#define XCAN_ESR_BERR_MASK 0x00000008 /* Bit error */
76#define XCAN_ESR_STER_MASK 0x00000004 /* Stuff error */
77#define XCAN_ESR_FMER_MASK 0x00000002 /* Form error */
78#define XCAN_ESR_CRCER_MASK 0x00000001 /* CRC error */
79#define XCAN_SR_TXFLL_MASK 0x00000400 /* TX FIFO is full */
80#define XCAN_SR_ESTAT_MASK 0x00000180 /* Error status */
81#define XCAN_SR_ERRWRN_MASK 0x00000040 /* Error warning */
82#define XCAN_SR_NORMAL_MASK 0x00000008 /* Normal mode */
83#define XCAN_SR_LBACK_MASK 0x00000002 /* Loop back mode */
84#define XCAN_SR_CONFIG_MASK 0x00000001 /* Configuration mode */
85#define XCAN_IXR_TXFEMP_MASK 0x00004000 /* TX FIFO Empty */
86#define XCAN_IXR_WKUP_MASK 0x00000800 /* Wake up interrupt */
87#define XCAN_IXR_SLP_MASK 0x00000400 /* Sleep interrupt */
88#define XCAN_IXR_BSOFF_MASK 0x00000200 /* Bus off interrupt */
89#define XCAN_IXR_ERROR_MASK 0x00000100 /* Error interrupt */
90#define XCAN_IXR_RXNEMP_MASK 0x00000080 /* RX FIFO NotEmpty intr */
91#define XCAN_IXR_RXOFLW_MASK 0x00000040 /* RX FIFO Overflow intr */
92#define XCAN_IXR_RXOK_MASK 0x00000010 /* Message received intr */
93#define XCAN_IXR_TXFLL_MASK 0x00000004 /* Tx FIFO Full intr */
94#define XCAN_IXR_TXOK_MASK 0x00000002 /* TX successful intr */
95#define XCAN_IXR_ARBLST_MASK 0x00000001 /* Arbitration lost intr */
96#define XCAN_IDR_ID1_MASK 0xFFE00000 /* Standard msg identifier */
97#define XCAN_IDR_SRR_MASK 0x00100000 /* Substitute remote TXreq */
98#define XCAN_IDR_IDE_MASK 0x00080000 /* Identifier extension */
99#define XCAN_IDR_ID2_MASK 0x0007FFFE /* Extended message ident */
100#define XCAN_IDR_RTR_MASK 0x00000001 /* Remote TX request */
101#define XCAN_DLCR_DLC_MASK 0xF0000000 /* Data length code */
102
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530103/* CAN register bit shift - XCAN_<REG>_<BIT>_SHIFT */
104#define XCAN_BTR_SJW_SHIFT 7 /* Synchronous jump width */
105#define XCAN_BTR_TS2_SHIFT 4 /* Time segment 2 */
106#define XCAN_IDR_ID1_SHIFT 21 /* Standard Messg Identifier */
107#define XCAN_IDR_ID2_SHIFT 1 /* Extended Message Identifier */
108#define XCAN_DLCR_DLC_SHIFT 28 /* Data length code */
109#define XCAN_ESR_REC_SHIFT 8 /* Rx Error Count */
110
111/* CAN frame length constants */
112#define XCAN_FRAME_MAX_DATA_LEN 8
113#define XCAN_TIMEOUT (1 * HZ)
114
Anssi Hannula1598efe2018-02-26 15:56:51 +0200115/* TX-FIFO-empty interrupt available */
116#define XCAN_FLAG_TXFEMP 0x0001
117
118struct xcan_devtype_data {
119 unsigned int flags;
120 const struct can_bittiming_const *bittiming_const;
121 const char *bus_clk_name;
122};
123
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530124/**
125 * struct xcan_priv - This definition define CAN driver instance
126 * @can: CAN private data structure.
Anssi Hannula620050d2017-02-23 14:50:03 +0200127 * @tx_lock: Lock for synchronizing TX interrupt handling
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530128 * @tx_head: Tx CAN packets ready to send on the queue
129 * @tx_tail: Tx CAN packets successfully sended on the queue
130 * @tx_max: Maximum number packets the driver can send
131 * @napi: NAPI structure
132 * @read_reg: For reading data from CAN registers
133 * @write_reg: For writing data to CAN registers
134 * @dev: Network device data structure
135 * @reg_base: Ioremapped address to registers
136 * @irq_flags: For request_irq()
137 * @bus_clk: Pointer to struct clk
138 * @can_clk: Pointer to struct clk
Anssi Hannula1598efe2018-02-26 15:56:51 +0200139 * @devtype: Device type specific constants
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530140 */
141struct xcan_priv {
142 struct can_priv can;
Anssi Hannula620050d2017-02-23 14:50:03 +0200143 spinlock_t tx_lock;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530144 unsigned int tx_head;
145 unsigned int tx_tail;
146 unsigned int tx_max;
147 struct napi_struct napi;
148 u32 (*read_reg)(const struct xcan_priv *priv, enum xcan_reg reg);
149 void (*write_reg)(const struct xcan_priv *priv, enum xcan_reg reg,
150 u32 val);
Kedareswara rao Appana47166202015-10-26 11:41:54 +0530151 struct device *dev;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530152 void __iomem *reg_base;
153 unsigned long irq_flags;
154 struct clk *bus_clk;
155 struct clk *can_clk;
Anssi Hannula1598efe2018-02-26 15:56:51 +0200156 struct xcan_devtype_data devtype;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530157};
158
159/* CAN Bittiming constants as per Xilinx CAN specs */
160static const struct can_bittiming_const xcan_bittiming_const = {
161 .name = DRIVER_NAME,
162 .tseg1_min = 1,
163 .tseg1_max = 16,
164 .tseg2_min = 1,
165 .tseg2_max = 8,
166 .sjw_max = 4,
167 .brp_min = 1,
168 .brp_max = 256,
169 .brp_inc = 1,
170};
171
172/**
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 */
Anssi Hannula1598efe2018-02-26 15:56:51 +0200332 priv->write_reg(priv, XCAN_IER_OFFSET,
333 XCAN_IXR_TXOK_MASK | XCAN_IXR_BSOFF_MASK |
334 XCAN_IXR_WKUP_MASK | XCAN_IXR_SLP_MASK |
335 XCAN_IXR_RXNEMP_MASK | XCAN_IXR_ERROR_MASK |
336 XCAN_IXR_RXOFLW_MASK | XCAN_IXR_ARBLST_MASK);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530337
338 /* Check whether it is loopback mode or normal mode */
339 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
340 reg_msr = XCAN_MSR_LBACK_MASK;
341 reg_sr_mask = XCAN_SR_LBACK_MASK;
342 } else {
343 reg_msr = 0x0;
344 reg_sr_mask = XCAN_SR_NORMAL_MASK;
345 }
346
347 priv->write_reg(priv, XCAN_MSR_OFFSET, reg_msr);
348 priv->write_reg(priv, XCAN_SRR_OFFSET, XCAN_SRR_CEN_MASK);
349
350 timeout = jiffies + XCAN_TIMEOUT;
351 while (!(priv->read_reg(priv, XCAN_SR_OFFSET) & reg_sr_mask)) {
352 if (time_after(jiffies, timeout)) {
353 netdev_warn(ndev,
354 "timed out for correct mode\n");
355 return -ETIMEDOUT;
356 }
357 }
358 netdev_dbg(ndev, "status:#x%08x\n",
359 priv->read_reg(priv, XCAN_SR_OFFSET));
360
361 priv->can.state = CAN_STATE_ERROR_ACTIVE;
362 return 0;
363}
364
365/**
366 * xcan_do_set_mode - This sets the mode of the driver
367 * @ndev: Pointer to net_device structure
368 * @mode: Tells the mode of the driver
369 *
370 * This check the drivers state and calls the
371 * the corresponding modes to set.
372 *
373 * Return: 0 on success and failure value on error
374 */
375static int xcan_do_set_mode(struct net_device *ndev, enum can_mode mode)
376{
377 int ret;
378
379 switch (mode) {
380 case CAN_MODE_START:
381 ret = xcan_chip_start(ndev);
382 if (ret < 0) {
383 netdev_err(ndev, "xcan_chip_start failed!\n");
384 return ret;
385 }
386 netif_wake_queue(ndev);
387 break;
388 default:
389 ret = -EOPNOTSUPP;
390 break;
391 }
392
393 return ret;
394}
395
396/**
Anssi Hannula1598efe2018-02-26 15:56:51 +0200397 * xcan_write_frame - Write a frame to HW
398 * @skb: sk_buff pointer that contains data to be Txed
399 * @frame_offset: Register offset to write the frame to
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530400 */
Anssi Hannula1598efe2018-02-26 15:56:51 +0200401static void xcan_write_frame(struct xcan_priv *priv, struct sk_buff *skb,
402 int frame_offset)
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530403{
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530404 u32 id, dlc, data[2] = {0, 0};
Anssi Hannula1598efe2018-02-26 15:56:51 +0200405 struct can_frame *cf = (struct can_frame *)skb->data;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530406
407 /* Watch carefully on the bit sequence */
408 if (cf->can_id & CAN_EFF_FLAG) {
409 /* Extended CAN ID format */
410 id = ((cf->can_id & CAN_EFF_MASK) << XCAN_IDR_ID2_SHIFT) &
411 XCAN_IDR_ID2_MASK;
412 id |= (((cf->can_id & CAN_EFF_MASK) >>
413 (CAN_EFF_ID_BITS-CAN_SFF_ID_BITS)) <<
414 XCAN_IDR_ID1_SHIFT) & XCAN_IDR_ID1_MASK;
415
416 /* The substibute remote TX request bit should be "1"
417 * for extended frames as in the Xilinx CAN datasheet
418 */
419 id |= XCAN_IDR_IDE_MASK | XCAN_IDR_SRR_MASK;
420
421 if (cf->can_id & CAN_RTR_FLAG)
422 /* Extended frames remote TX request */
423 id |= XCAN_IDR_RTR_MASK;
424 } else {
425 /* Standard CAN ID format */
426 id = ((cf->can_id & CAN_SFF_MASK) << XCAN_IDR_ID1_SHIFT) &
427 XCAN_IDR_ID1_MASK;
428
429 if (cf->can_id & CAN_RTR_FLAG)
430 /* Standard frames remote TX request */
431 id |= XCAN_IDR_SRR_MASK;
432 }
433
434 dlc = cf->can_dlc << XCAN_DLCR_DLC_SHIFT;
435
436 if (cf->can_dlc > 0)
437 data[0] = be32_to_cpup((__be32 *)(cf->data + 0));
438 if (cf->can_dlc > 4)
439 data[1] = be32_to_cpup((__be32 *)(cf->data + 4));
440
Anssi Hannula1598efe2018-02-26 15:56:51 +0200441 priv->write_reg(priv, XCAN_FRAME_ID_OFFSET(frame_offset), id);
442 /* If the CAN frame is RTR frame this write triggers transmission */
443 priv->write_reg(priv, XCAN_FRAME_DLC_OFFSET(frame_offset), dlc);
444 if (!(cf->can_id & CAN_RTR_FLAG)) {
445 priv->write_reg(priv, XCAN_FRAME_DW1_OFFSET(frame_offset),
446 data[0]);
447 /* If the CAN frame is Standard/Extended frame this
448 * write triggers transmission
449 */
450 priv->write_reg(priv, XCAN_FRAME_DW2_OFFSET(frame_offset),
451 data[1]);
452 }
453}
454
455/**
456 * xcan_start_xmit_fifo - Starts the transmission (FIFO mode)
457 *
458 * Return: 0 on success, -ENOSPC if FIFO is full.
459 */
460static int xcan_start_xmit_fifo(struct sk_buff *skb, struct net_device *ndev)
461{
462 struct xcan_priv *priv = netdev_priv(ndev);
463 unsigned long flags;
464
465 /* Check if the TX buffer is full */
466 if (unlikely(priv->read_reg(priv, XCAN_SR_OFFSET) &
467 XCAN_SR_TXFLL_MASK))
468 return -ENOSPC;
469
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530470 can_put_echo_skb(skb, ndev, priv->tx_head % priv->tx_max);
Anssi Hannula620050d2017-02-23 14:50:03 +0200471
472 spin_lock_irqsave(&priv->tx_lock, flags);
473
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530474 priv->tx_head++;
475
Anssi Hannula1598efe2018-02-26 15:56:51 +0200476 xcan_write_frame(priv, skb, XCAN_TXFIFO_OFFSET);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530477
Anssi Hannula620050d2017-02-23 14:50:03 +0200478 /* Clear TX-FIFO-empty interrupt for xcan_tx_interrupt() */
479 if (priv->tx_max > 1)
480 priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_TXFEMP_MASK);
481
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530482 /* Check if the TX buffer is full */
483 if ((priv->tx_head - priv->tx_tail) == priv->tx_max)
484 netif_stop_queue(ndev);
485
Anssi Hannula620050d2017-02-23 14:50:03 +0200486 spin_unlock_irqrestore(&priv->tx_lock, flags);
487
Anssi Hannula1598efe2018-02-26 15:56:51 +0200488 return 0;
489}
490
491/**
492 * xcan_start_xmit - Starts the transmission
493 * @skb: sk_buff pointer that contains data to be Txed
494 * @ndev: Pointer to net_device structure
495 *
496 * This function is invoked from upper layers to initiate transmission. This
497 * function uses the next available free txbuff and populates their fields to
498 * start the transmission.
499 *
500 * Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY when the tx queue is full
501 */
502static int xcan_start_xmit(struct sk_buff *skb, struct net_device *ndev)
503{
504 int ret;
505
506 if (can_dropped_invalid_skb(ndev, skb))
507 return NETDEV_TX_OK;
508
509 ret = xcan_start_xmit_fifo(skb, ndev);
510
511 if (ret < 0) {
512 netdev_err(ndev, "BUG!, TX full when queue awake!\n");
513 netif_stop_queue(ndev);
514 return NETDEV_TX_BUSY;
515 }
516
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530517 return NETDEV_TX_OK;
518}
519
520/**
521 * xcan_rx - Is called from CAN isr to complete the received
522 * frame processing
523 * @ndev: Pointer to net_device structure
Anssi Hannula1598efe2018-02-26 15:56:51 +0200524 * @frame_base: Register offset to the frame to be read
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530525 *
526 * This function is invoked from the CAN isr(poll) to process the Rx frames. It
527 * does minimal processing and invokes "netif_receive_skb" to complete further
528 * processing.
529 * Return: 1 on success and 0 on failure.
530 */
Anssi Hannula1598efe2018-02-26 15:56:51 +0200531static int xcan_rx(struct net_device *ndev, int frame_base)
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530532{
533 struct xcan_priv *priv = netdev_priv(ndev);
534 struct net_device_stats *stats = &ndev->stats;
535 struct can_frame *cf;
536 struct sk_buff *skb;
537 u32 id_xcan, dlc, data[2] = {0, 0};
538
539 skb = alloc_can_skb(ndev, &cf);
540 if (unlikely(!skb)) {
541 stats->rx_dropped++;
542 return 0;
543 }
544
545 /* Read a frame from Xilinx zynq CANPS */
Anssi Hannula1598efe2018-02-26 15:56:51 +0200546 id_xcan = priv->read_reg(priv, XCAN_FRAME_ID_OFFSET(frame_base));
547 dlc = priv->read_reg(priv, XCAN_FRAME_DLC_OFFSET(frame_base)) >>
548 XCAN_DLCR_DLC_SHIFT;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530549
550 /* Change Xilinx CAN data length format to socketCAN data format */
551 cf->can_dlc = get_can_dlc(dlc);
552
553 /* Change Xilinx CAN ID format to socketCAN ID format */
554 if (id_xcan & XCAN_IDR_IDE_MASK) {
555 /* The received frame is an Extended format frame */
556 cf->can_id = (id_xcan & XCAN_IDR_ID1_MASK) >> 3;
557 cf->can_id |= (id_xcan & XCAN_IDR_ID2_MASK) >>
558 XCAN_IDR_ID2_SHIFT;
559 cf->can_id |= CAN_EFF_FLAG;
560 if (id_xcan & XCAN_IDR_RTR_MASK)
561 cf->can_id |= CAN_RTR_FLAG;
562 } else {
563 /* The received frame is a standard format frame */
564 cf->can_id = (id_xcan & XCAN_IDR_ID1_MASK) >>
565 XCAN_IDR_ID1_SHIFT;
566 if (id_xcan & XCAN_IDR_SRR_MASK)
567 cf->can_id |= CAN_RTR_FLAG;
568 }
569
Jeppe Ledet-Pedersen5793aff2015-04-29 17:05:01 +0200570 /* DW1/DW2 must always be read to remove message from RXFIFO */
Anssi Hannula1598efe2018-02-26 15:56:51 +0200571 data[0] = priv->read_reg(priv, XCAN_FRAME_DW1_OFFSET(frame_base));
572 data[1] = priv->read_reg(priv, XCAN_FRAME_DW2_OFFSET(frame_base));
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530573
Jeppe Ledet-Pedersen5793aff2015-04-29 17:05:01 +0200574 if (!(cf->can_id & CAN_RTR_FLAG)) {
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530575 /* Change Xilinx CAN data format to socketCAN data format */
576 if (cf->can_dlc > 0)
577 *(__be32 *)(cf->data) = cpu_to_be32(data[0]);
578 if (cf->can_dlc > 4)
579 *(__be32 *)(cf->data + 4) = cpu_to_be32(data[1]);
580 }
581
582 stats->rx_bytes += cf->can_dlc;
583 stats->rx_packets++;
584 netif_receive_skb(skb);
585
586 return 1;
587}
588
589/**
Anssi Hannula877e0b72017-02-08 13:13:40 +0200590 * xcan_current_error_state - Get current error state from HW
591 * @ndev: Pointer to net_device structure
592 *
593 * Checks the current CAN error state from the HW. Note that this
594 * only checks for ERROR_PASSIVE and ERROR_WARNING.
595 *
596 * Return:
597 * ERROR_PASSIVE or ERROR_WARNING if either is active, ERROR_ACTIVE
598 * otherwise.
599 */
600static enum can_state xcan_current_error_state(struct net_device *ndev)
601{
602 struct xcan_priv *priv = netdev_priv(ndev);
603 u32 status = priv->read_reg(priv, XCAN_SR_OFFSET);
604
605 if ((status & XCAN_SR_ESTAT_MASK) == XCAN_SR_ESTAT_MASK)
606 return CAN_STATE_ERROR_PASSIVE;
607 else if (status & XCAN_SR_ERRWRN_MASK)
608 return CAN_STATE_ERROR_WARNING;
609 else
610 return CAN_STATE_ERROR_ACTIVE;
611}
612
613/**
614 * xcan_set_error_state - Set new CAN error state
615 * @ndev: Pointer to net_device structure
616 * @new_state: The new CAN state to be set
617 * @cf: Error frame to be populated or NULL
618 *
619 * Set new CAN error state for the device, updating statistics and
620 * populating the error frame if given.
621 */
622static void xcan_set_error_state(struct net_device *ndev,
623 enum can_state new_state,
624 struct can_frame *cf)
625{
626 struct xcan_priv *priv = netdev_priv(ndev);
627 u32 ecr = priv->read_reg(priv, XCAN_ECR_OFFSET);
628 u32 txerr = ecr & XCAN_ECR_TEC_MASK;
629 u32 rxerr = (ecr & XCAN_ECR_REC_MASK) >> XCAN_ESR_REC_SHIFT;
Anssi Hannula6181dbc2018-01-29 17:28:24 +0200630 enum can_state tx_state = txerr >= rxerr ? new_state : 0;
631 enum can_state rx_state = txerr <= rxerr ? new_state : 0;
Anssi Hannula877e0b72017-02-08 13:13:40 +0200632
Anssi Hannula6181dbc2018-01-29 17:28:24 +0200633 /* non-ERROR states are handled elsewhere */
634 if (WARN_ON(new_state > CAN_STATE_ERROR_PASSIVE))
635 return;
636
637 can_change_state(ndev, cf, tx_state, rx_state);
Anssi Hannula877e0b72017-02-08 13:13:40 +0200638
639 if (cf) {
Anssi Hannula877e0b72017-02-08 13:13:40 +0200640 cf->data[6] = txerr;
641 cf->data[7] = rxerr;
642 }
Anssi Hannula877e0b72017-02-08 13:13:40 +0200643}
644
645/**
646 * xcan_update_error_state_after_rxtx - Update CAN error state after RX/TX
647 * @ndev: Pointer to net_device structure
648 *
649 * If the device is in a ERROR-WARNING or ERROR-PASSIVE state, check if
650 * the performed RX/TX has caused it to drop to a lesser state and set
651 * the interface state accordingly.
652 */
653static void xcan_update_error_state_after_rxtx(struct net_device *ndev)
654{
655 struct xcan_priv *priv = netdev_priv(ndev);
656 enum can_state old_state = priv->can.state;
657 enum can_state new_state;
658
659 /* changing error state due to successful frame RX/TX can only
660 * occur from these states
661 */
662 if (old_state != CAN_STATE_ERROR_WARNING &&
663 old_state != CAN_STATE_ERROR_PASSIVE)
664 return;
665
666 new_state = xcan_current_error_state(ndev);
667
668 if (new_state != old_state) {
669 struct sk_buff *skb;
670 struct can_frame *cf;
671
672 skb = alloc_can_err_skb(ndev, &cf);
673
674 xcan_set_error_state(ndev, new_state, skb ? cf : NULL);
675
676 if (skb) {
677 struct net_device_stats *stats = &ndev->stats;
678
679 stats->rx_packets++;
680 stats->rx_bytes += cf->can_dlc;
681 netif_rx(skb);
682 }
683 }
684}
685
686/**
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530687 * xcan_err_interrupt - error frame Isr
688 * @ndev: net_device pointer
689 * @isr: interrupt status register value
690 *
691 * This is the CAN error interrupt and it will
692 * check the the type of error and forward the error
693 * frame to upper layers.
694 */
695static void xcan_err_interrupt(struct net_device *ndev, u32 isr)
696{
697 struct xcan_priv *priv = netdev_priv(ndev);
698 struct net_device_stats *stats = &ndev->stats;
699 struct can_frame *cf;
700 struct sk_buff *skb;
Anssi Hannula877e0b72017-02-08 13:13:40 +0200701 u32 err_status;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530702
703 skb = alloc_can_err_skb(ndev, &cf);
704
705 err_status = priv->read_reg(priv, XCAN_ESR_OFFSET);
706 priv->write_reg(priv, XCAN_ESR_OFFSET, err_status);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530707
708 if (isr & XCAN_IXR_BSOFF_MASK) {
709 priv->can.state = CAN_STATE_BUS_OFF;
710 priv->can.can_stats.bus_off++;
711 /* Leave device in Config Mode in bus-off state */
712 priv->write_reg(priv, XCAN_SRR_OFFSET, XCAN_SRR_RESET_MASK);
713 can_bus_off(ndev);
714 if (skb)
715 cf->can_id |= CAN_ERR_BUSOFF;
Anssi Hannula877e0b72017-02-08 13:13:40 +0200716 } else {
717 enum can_state new_state = xcan_current_error_state(ndev);
718
Anssi Hannula7e2804a2017-02-08 13:32:43 +0200719 if (new_state != priv->can.state)
720 xcan_set_error_state(ndev, new_state, skb ? cf : NULL);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530721 }
722
723 /* Check for Arbitration lost interrupt */
724 if (isr & XCAN_IXR_ARBLST_MASK) {
725 priv->can.can_stats.arbitration_lost++;
726 if (skb) {
727 cf->can_id |= CAN_ERR_LOSTARB;
728 cf->data[0] = CAN_ERR_LOSTARB_UNSPEC;
729 }
730 }
731
732 /* Check for RX FIFO Overflow interrupt */
733 if (isr & XCAN_IXR_RXOFLW_MASK) {
734 stats->rx_over_errors++;
735 stats->rx_errors++;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530736 if (skb) {
737 cf->can_id |= CAN_ERR_CRTL;
738 cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
739 }
740 }
741
742 /* Check for error interrupt */
743 if (isr & XCAN_IXR_ERROR_MASK) {
Oliver Hartkoppa2ec19f2015-11-21 18:41:21 +0100744 if (skb)
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530745 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530746
747 /* Check for Ack error interrupt */
748 if (err_status & XCAN_ESR_ACKER_MASK) {
749 stats->tx_errors++;
750 if (skb) {
751 cf->can_id |= CAN_ERR_ACK;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100752 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530753 }
754 }
755
756 /* Check for Bit error interrupt */
757 if (err_status & XCAN_ESR_BERR_MASK) {
758 stats->tx_errors++;
759 if (skb) {
760 cf->can_id |= CAN_ERR_PROT;
761 cf->data[2] = CAN_ERR_PROT_BIT;
762 }
763 }
764
765 /* Check for Stuff error interrupt */
766 if (err_status & XCAN_ESR_STER_MASK) {
767 stats->rx_errors++;
768 if (skb) {
769 cf->can_id |= CAN_ERR_PROT;
770 cf->data[2] = CAN_ERR_PROT_STUFF;
771 }
772 }
773
774 /* Check for Form error interrupt */
775 if (err_status & XCAN_ESR_FMER_MASK) {
776 stats->rx_errors++;
777 if (skb) {
778 cf->can_id |= CAN_ERR_PROT;
779 cf->data[2] = CAN_ERR_PROT_FORM;
780 }
781 }
782
783 /* Check for CRC error interrupt */
784 if (err_status & XCAN_ESR_CRCER_MASK) {
785 stats->rx_errors++;
786 if (skb) {
787 cf->can_id |= CAN_ERR_PROT;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100788 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530789 }
790 }
791 priv->can.can_stats.bus_error++;
792 }
793
794 if (skb) {
795 stats->rx_packets++;
796 stats->rx_bytes += cf->can_dlc;
797 netif_rx(skb);
798 }
799
800 netdev_dbg(ndev, "%s: error status register:0x%x\n",
801 __func__, priv->read_reg(priv, XCAN_ESR_OFFSET));
802}
803
804/**
805 * xcan_state_interrupt - It will check the state of the CAN device
806 * @ndev: net_device pointer
807 * @isr: interrupt status register value
808 *
809 * This will checks the state of the CAN device
810 * and puts the device into appropriate state.
811 */
812static void xcan_state_interrupt(struct net_device *ndev, u32 isr)
813{
814 struct xcan_priv *priv = netdev_priv(ndev);
815
816 /* Check for Sleep interrupt if set put CAN device in sleep state */
817 if (isr & XCAN_IXR_SLP_MASK)
818 priv->can.state = CAN_STATE_SLEEPING;
819
820 /* Check for Wake up interrupt if set put CAN device in Active state */
821 if (isr & XCAN_IXR_WKUP_MASK)
822 priv->can.state = CAN_STATE_ERROR_ACTIVE;
823}
824
825/**
826 * xcan_rx_poll - Poll routine for rx packets (NAPI)
827 * @napi: napi structure pointer
828 * @quota: Max number of rx packets to be processed.
829 *
830 * This is the poll routine for rx part.
831 * It will process the packets maximux quota value.
832 *
833 * Return: number of packets received
834 */
835static int xcan_rx_poll(struct napi_struct *napi, int quota)
836{
837 struct net_device *ndev = napi->dev;
838 struct xcan_priv *priv = netdev_priv(ndev);
839 u32 isr, ier;
840 int work_done = 0;
841
842 isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
843 while ((isr & XCAN_IXR_RXNEMP_MASK) && (work_done < quota)) {
Anssi Hannula1598efe2018-02-26 15:56:51 +0200844 work_done += xcan_rx(ndev, XCAN_RXFIFO_OFFSET);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530845 priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_RXNEMP_MASK);
846 isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
847 }
848
Anssi Hannula877e0b72017-02-08 13:13:40 +0200849 if (work_done) {
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530850 can_led_event(ndev, CAN_LED_EVENT_RX);
Anssi Hannula877e0b72017-02-08 13:13:40 +0200851 xcan_update_error_state_after_rxtx(ndev);
852 }
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530853
854 if (work_done < quota) {
Eric Dumazet6ad20162017-01-30 08:22:01 -0800855 napi_complete_done(napi, work_done);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530856 ier = priv->read_reg(priv, XCAN_IER_OFFSET);
Anssi Hannula32852c52017-02-07 17:01:14 +0200857 ier |= XCAN_IXR_RXNEMP_MASK;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530858 priv->write_reg(priv, XCAN_IER_OFFSET, ier);
859 }
860 return work_done;
861}
862
863/**
864 * xcan_tx_interrupt - Tx Done Isr
865 * @ndev: net_device pointer
866 * @isr: Interrupt status register value
867 */
868static void xcan_tx_interrupt(struct net_device *ndev, u32 isr)
869{
870 struct xcan_priv *priv = netdev_priv(ndev);
871 struct net_device_stats *stats = &ndev->stats;
Anssi Hannula620050d2017-02-23 14:50:03 +0200872 unsigned int frames_in_fifo;
873 int frames_sent = 1; /* TXOK => at least 1 frame was sent */
874 unsigned long flags;
875 int retries = 0;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530876
Anssi Hannula620050d2017-02-23 14:50:03 +0200877 /* Synchronize with xmit as we need to know the exact number
878 * of frames in the FIFO to stay in sync due to the TXFEMP
879 * handling.
880 * This also prevents a race between netif_wake_queue() and
881 * netif_stop_queue().
882 */
883 spin_lock_irqsave(&priv->tx_lock, flags);
884
885 frames_in_fifo = priv->tx_head - priv->tx_tail;
886
887 if (WARN_ON_ONCE(frames_in_fifo == 0)) {
888 /* clear TXOK anyway to avoid getting back here */
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530889 priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_TXOK_MASK);
Anssi Hannula620050d2017-02-23 14:50:03 +0200890 spin_unlock_irqrestore(&priv->tx_lock, flags);
891 return;
892 }
893
894 /* Check if 2 frames were sent (TXOK only means that at least 1
895 * frame was sent).
896 */
897 if (frames_in_fifo > 1) {
898 WARN_ON(frames_in_fifo > priv->tx_max);
899
900 /* Synchronize TXOK and isr so that after the loop:
901 * (1) isr variable is up-to-date at least up to TXOK clear
902 * time. This avoids us clearing a TXOK of a second frame
903 * but not noticing that the FIFO is now empty and thus
904 * marking only a single frame as sent.
905 * (2) No TXOK is left. Having one could mean leaving a
906 * stray TXOK as we might process the associated frame
907 * via TXFEMP handling as we read TXFEMP *after* TXOK
908 * clear to satisfy (1).
909 */
910 while ((isr & XCAN_IXR_TXOK_MASK) && !WARN_ON(++retries == 100)) {
911 priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_TXOK_MASK);
912 isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
913 }
914
915 if (isr & XCAN_IXR_TXFEMP_MASK) {
916 /* nothing in FIFO anymore */
917 frames_sent = frames_in_fifo;
918 }
919 } else {
920 /* single frame in fifo, just clear TXOK */
921 priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_TXOK_MASK);
922 }
923
924 while (frames_sent--) {
Anssi Hannula11ee5fc2018-02-26 14:50:35 +0200925 stats->tx_bytes += can_get_echo_skb(ndev, priv->tx_tail %
926 priv->tx_max);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530927 priv->tx_tail++;
928 stats->tx_packets++;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530929 }
Anssi Hannula620050d2017-02-23 14:50:03 +0200930
931 netif_wake_queue(ndev);
932
933 spin_unlock_irqrestore(&priv->tx_lock, flags);
934
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530935 can_led_event(ndev, CAN_LED_EVENT_TX);
Anssi Hannula877e0b72017-02-08 13:13:40 +0200936 xcan_update_error_state_after_rxtx(ndev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530937}
938
939/**
940 * xcan_interrupt - CAN Isr
941 * @irq: irq number
942 * @dev_id: device id poniter
943 *
944 * This is the xilinx CAN Isr. It checks for the type of interrupt
945 * and invokes the corresponding ISR.
946 *
947 * Return:
948 * IRQ_NONE - If CAN device is in sleep mode, IRQ_HANDLED otherwise
949 */
950static irqreturn_t xcan_interrupt(int irq, void *dev_id)
951{
952 struct net_device *ndev = (struct net_device *)dev_id;
953 struct xcan_priv *priv = netdev_priv(ndev);
954 u32 isr, ier;
Anssi Hannula2f4f0f32018-02-26 14:39:59 +0200955 u32 isr_errors;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530956
957 /* Get the interrupt status from Xilinx CAN */
958 isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
959 if (!isr)
960 return IRQ_NONE;
961
962 /* Check for the type of interrupt and Processing it */
963 if (isr & (XCAN_IXR_SLP_MASK | XCAN_IXR_WKUP_MASK)) {
964 priv->write_reg(priv, XCAN_ICR_OFFSET, (XCAN_IXR_SLP_MASK |
965 XCAN_IXR_WKUP_MASK));
966 xcan_state_interrupt(ndev, isr);
967 }
968
969 /* Check for Tx interrupt and Processing it */
970 if (isr & XCAN_IXR_TXOK_MASK)
971 xcan_tx_interrupt(ndev, isr);
972
973 /* Check for the type of error interrupt and Processing it */
Anssi Hannula2f4f0f32018-02-26 14:39:59 +0200974 isr_errors = isr & (XCAN_IXR_ERROR_MASK | XCAN_IXR_RXOFLW_MASK |
975 XCAN_IXR_BSOFF_MASK | XCAN_IXR_ARBLST_MASK);
976 if (isr_errors) {
977 priv->write_reg(priv, XCAN_ICR_OFFSET, isr_errors);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530978 xcan_err_interrupt(ndev, isr);
979 }
980
981 /* Check for the type of receive interrupt and Processing it */
Anssi Hannula32852c52017-02-07 17:01:14 +0200982 if (isr & XCAN_IXR_RXNEMP_MASK) {
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530983 ier = priv->read_reg(priv, XCAN_IER_OFFSET);
Anssi Hannula32852c52017-02-07 17:01:14 +0200984 ier &= ~XCAN_IXR_RXNEMP_MASK;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +0530985 priv->write_reg(priv, XCAN_IER_OFFSET, ier);
986 napi_schedule(&priv->napi);
987 }
988 return IRQ_HANDLED;
989}
990
991/**
992 * xcan_chip_stop - Driver stop routine
993 * @ndev: Pointer to net_device structure
994 *
995 * This is the drivers stop routine. It will disable the
996 * interrupts and put the device into configuration mode.
997 */
998static void xcan_chip_stop(struct net_device *ndev)
999{
1000 struct xcan_priv *priv = netdev_priv(ndev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301001
1002 /* Disable interrupts and leave the can in configuration mode */
Anssi Hannula8ebd83b2018-05-17 15:41:19 +03001003 set_reset_mode(ndev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301004 priv->can.state = CAN_STATE_STOPPED;
1005}
1006
1007/**
1008 * xcan_open - Driver open routine
1009 * @ndev: Pointer to net_device structure
1010 *
1011 * This is the driver open routine.
1012 * Return: 0 on success and failure value on error
1013 */
1014static int xcan_open(struct net_device *ndev)
1015{
1016 struct xcan_priv *priv = netdev_priv(ndev);
1017 int ret;
1018
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301019 ret = pm_runtime_get_sync(priv->dev);
1020 if (ret < 0) {
1021 netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
1022 __func__, ret);
1023 return ret;
1024 }
1025
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301026 ret = request_irq(ndev->irq, xcan_interrupt, priv->irq_flags,
1027 ndev->name, ndev);
1028 if (ret < 0) {
1029 netdev_err(ndev, "irq allocation for CAN failed\n");
1030 goto err;
1031 }
1032
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301033 /* Set chip into reset mode */
1034 ret = set_reset_mode(ndev);
1035 if (ret < 0) {
1036 netdev_err(ndev, "mode resetting failed!\n");
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301037 goto err_irq;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301038 }
1039
1040 /* Common open */
1041 ret = open_candev(ndev);
1042 if (ret)
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301043 goto err_irq;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301044
1045 ret = xcan_chip_start(ndev);
1046 if (ret < 0) {
1047 netdev_err(ndev, "xcan_chip_start failed!\n");
1048 goto err_candev;
1049 }
1050
1051 can_led_event(ndev, CAN_LED_EVENT_OPEN);
1052 napi_enable(&priv->napi);
1053 netif_start_queue(ndev);
1054
1055 return 0;
1056
1057err_candev:
1058 close_candev(ndev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301059err_irq:
1060 free_irq(ndev->irq, ndev);
1061err:
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301062 pm_runtime_put(priv->dev);
1063
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301064 return ret;
1065}
1066
1067/**
1068 * xcan_close - Driver close routine
1069 * @ndev: Pointer to net_device structure
1070 *
1071 * Return: 0 always
1072 */
1073static int xcan_close(struct net_device *ndev)
1074{
1075 struct xcan_priv *priv = netdev_priv(ndev);
1076
1077 netif_stop_queue(ndev);
1078 napi_disable(&priv->napi);
1079 xcan_chip_stop(ndev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301080 free_irq(ndev->irq, ndev);
1081 close_candev(ndev);
1082
1083 can_led_event(ndev, CAN_LED_EVENT_STOP);
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301084 pm_runtime_put(priv->dev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301085
1086 return 0;
1087}
1088
1089/**
1090 * xcan_get_berr_counter - error counter routine
1091 * @ndev: Pointer to net_device structure
1092 * @bec: Pointer to can_berr_counter structure
1093 *
1094 * This is the driver error counter routine.
1095 * Return: 0 on success and failure value on error
1096 */
1097static int xcan_get_berr_counter(const struct net_device *ndev,
1098 struct can_berr_counter *bec)
1099{
1100 struct xcan_priv *priv = netdev_priv(ndev);
1101 int ret;
1102
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301103 ret = pm_runtime_get_sync(priv->dev);
1104 if (ret < 0) {
1105 netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
1106 __func__, ret);
1107 return ret;
1108 }
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301109
1110 bec->txerr = priv->read_reg(priv, XCAN_ECR_OFFSET) & XCAN_ECR_TEC_MASK;
1111 bec->rxerr = ((priv->read_reg(priv, XCAN_ECR_OFFSET) &
1112 XCAN_ECR_REC_MASK) >> XCAN_ESR_REC_SHIFT);
1113
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301114 pm_runtime_put(priv->dev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301115
1116 return 0;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301117}
1118
1119
1120static const struct net_device_ops xcan_netdev_ops = {
1121 .ndo_open = xcan_open,
1122 .ndo_stop = xcan_close,
1123 .ndo_start_xmit = xcan_start_xmit,
Marc Kleine-Budde92593a02014-11-18 13:16:13 +01001124 .ndo_change_mtu = can_change_mtu,
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301125};
1126
1127/**
1128 * xcan_suspend - Suspend method for the driver
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301129 * @dev: Address of the device structure
1130 *
1131 * Put the driver into low power mode.
1132 * Return: 0 on success and failure value on error
1133 */
1134static int __maybe_unused xcan_suspend(struct device *dev)
1135{
Anssi Hannula8ebd83b2018-05-17 15:41:19 +03001136 struct net_device *ndev = dev_get_drvdata(dev);
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301137
Anssi Hannula8ebd83b2018-05-17 15:41:19 +03001138 if (netif_running(ndev)) {
1139 netif_stop_queue(ndev);
1140 netif_device_detach(ndev);
1141 xcan_chip_stop(ndev);
1142 }
1143
1144 return pm_runtime_force_suspend(dev);
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301145}
1146
1147/**
1148 * xcan_resume - Resume from suspend
1149 * @dev: Address of the device structure
1150 *
1151 * Resume operation after suspend.
1152 * Return: 0 on success and failure value on error
1153 */
1154static int __maybe_unused xcan_resume(struct device *dev)
1155{
Anssi Hannula8ebd83b2018-05-17 15:41:19 +03001156 struct net_device *ndev = dev_get_drvdata(dev);
1157 int ret;
1158
1159 ret = pm_runtime_force_resume(dev);
1160 if (ret) {
1161 dev_err(dev, "pm_runtime_force_resume failed on resume\n");
1162 return ret;
1163 }
1164
1165 if (netif_running(ndev)) {
1166 ret = xcan_chip_start(ndev);
1167 if (ret) {
1168 dev_err(dev, "xcan_chip_start failed on resume\n");
1169 return ret;
1170 }
1171
1172 netif_device_attach(ndev);
1173 netif_start_queue(ndev);
1174 }
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301175
1176 return 0;
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301177}
1178
1179/**
1180 * xcan_runtime_suspend - Runtime suspend method for the driver
1181 * @dev: Address of the device structure
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301182 *
1183 * Put the driver into low power mode.
1184 * Return: 0 always
1185 */
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301186static int __maybe_unused xcan_runtime_suspend(struct device *dev)
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301187{
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301188 struct net_device *ndev = dev_get_drvdata(dev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301189 struct xcan_priv *priv = netdev_priv(ndev);
1190
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301191 clk_disable_unprepare(priv->bus_clk);
1192 clk_disable_unprepare(priv->can_clk);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301193
1194 return 0;
1195}
1196
1197/**
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301198 * xcan_runtime_resume - Runtime resume from suspend
1199 * @dev: Address of the device structure
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301200 *
1201 * Resume operation after suspend.
1202 * Return: 0 on success and failure value on error
1203 */
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301204static int __maybe_unused xcan_runtime_resume(struct device *dev)
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301205{
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301206 struct net_device *ndev = dev_get_drvdata(dev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301207 struct xcan_priv *priv = netdev_priv(ndev);
1208 int ret;
1209
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301210 ret = clk_prepare_enable(priv->bus_clk);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301211 if (ret) {
1212 dev_err(dev, "Cannot enable clock.\n");
1213 return ret;
1214 }
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301215 ret = clk_prepare_enable(priv->can_clk);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301216 if (ret) {
1217 dev_err(dev, "Cannot enable clock.\n");
1218 clk_disable_unprepare(priv->bus_clk);
1219 return ret;
1220 }
1221
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301222 return 0;
1223}
1224
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301225static const struct dev_pm_ops xcan_dev_pm_ops = {
1226 SET_SYSTEM_SLEEP_PM_OPS(xcan_suspend, xcan_resume)
1227 SET_RUNTIME_PM_OPS(xcan_runtime_suspend, xcan_runtime_resume, NULL)
1228};
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301229
Anssi Hannula620050d2017-02-23 14:50:03 +02001230static const struct xcan_devtype_data xcan_zynq_data = {
Anssi Hannula1598efe2018-02-26 15:56:51 +02001231 .flags = XCAN_FLAG_TXFEMP,
1232 .bittiming_const = &xcan_bittiming_const,
1233 .bus_clk_name = "pclk",
1234};
1235
1236static const struct xcan_devtype_data xcan_axi_data = {
1237 .flags = 0,
1238 .bittiming_const = &xcan_bittiming_const,
1239 .bus_clk_name = "s_axi_aclk",
Anssi Hannula620050d2017-02-23 14:50:03 +02001240};
1241
1242/* Match table for OF platform binding */
1243static const struct of_device_id xcan_of_match[] = {
1244 { .compatible = "xlnx,zynq-can-1.0", .data = &xcan_zynq_data },
Anssi Hannula1598efe2018-02-26 15:56:51 +02001245 { .compatible = "xlnx,axi-can-1.00.a", .data = &xcan_axi_data },
Anssi Hannula620050d2017-02-23 14:50:03 +02001246 { /* end of list */ },
1247};
1248MODULE_DEVICE_TABLE(of, xcan_of_match);
1249
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301250/**
1251 * xcan_probe - Platform registration call
1252 * @pdev: Handle to the platform device structure
1253 *
1254 * This function does all the memory allocation and registration for the CAN
1255 * device.
1256 *
1257 * Return: 0 on success and failure value on error
1258 */
1259static int xcan_probe(struct platform_device *pdev)
1260{
1261 struct resource *res; /* IO mem resources */
1262 struct net_device *ndev;
1263 struct xcan_priv *priv;
Anssi Hannula620050d2017-02-23 14:50:03 +02001264 const struct of_device_id *of_id;
Anssi Hannula1598efe2018-02-26 15:56:51 +02001265 const struct xcan_devtype_data *devtype = &xcan_axi_data;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301266 void __iomem *addr;
Anssi Hannula620050d2017-02-23 14:50:03 +02001267 int ret, rx_max, tx_max, tx_fifo_depth;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301268
1269 /* Get the virtual base address for the device */
1270 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1271 addr = devm_ioremap_resource(&pdev->dev, res);
1272 if (IS_ERR(addr)) {
1273 ret = PTR_ERR(addr);
1274 goto err;
1275 }
1276
Anssi Hannula620050d2017-02-23 14:50:03 +02001277 ret = of_property_read_u32(pdev->dev.of_node, "tx-fifo-depth",
1278 &tx_fifo_depth);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301279 if (ret < 0)
1280 goto err;
1281
1282 ret = of_property_read_u32(pdev->dev.of_node, "rx-fifo-depth", &rx_max);
1283 if (ret < 0)
1284 goto err;
1285
Anssi Hannula620050d2017-02-23 14:50:03 +02001286 of_id = of_match_device(xcan_of_match, &pdev->dev);
Anssi Hannula1598efe2018-02-26 15:56:51 +02001287 if (of_id && of_id->data)
1288 devtype = of_id->data;
Anssi Hannula620050d2017-02-23 14:50:03 +02001289
1290 /* There is no way to directly figure out how many frames have been
1291 * sent when the TXOK interrupt is processed. If watermark programming
1292 * is supported, we can have 2 frames in the FIFO and use TXFEMP
1293 * to determine if 1 or 2 frames have been sent.
1294 * Theoretically we should be able to use TXFWMEMP to determine up
1295 * to 3 frames, but it seems that after putting a second frame in the
1296 * FIFO, with watermark at 2 frames, it can happen that TXFWMEMP (less
1297 * than 2 frames in FIFO) is set anyway with no TXOK (a frame was
1298 * sent), which is not a sensible state - possibly TXFWMEMP is not
1299 * completely synchronized with the rest of the bits?
1300 */
Anssi Hannula1598efe2018-02-26 15:56:51 +02001301 if (devtype->flags & XCAN_FLAG_TXFEMP)
Anssi Hannula620050d2017-02-23 14:50:03 +02001302 tx_max = min(tx_fifo_depth, 2);
1303 else
1304 tx_max = 1;
1305
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301306 /* Create a CAN device instance */
1307 ndev = alloc_candev(sizeof(struct xcan_priv), tx_max);
1308 if (!ndev)
1309 return -ENOMEM;
1310
1311 priv = netdev_priv(ndev);
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301312 priv->dev = &pdev->dev;
Anssi Hannula1598efe2018-02-26 15:56:51 +02001313 priv->can.bittiming_const = devtype->bittiming_const;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301314 priv->can.do_set_mode = xcan_do_set_mode;
1315 priv->can.do_get_berr_counter = xcan_get_berr_counter;
1316 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1317 CAN_CTRLMODE_BERR_REPORTING;
1318 priv->reg_base = addr;
1319 priv->tx_max = tx_max;
Anssi Hannula1598efe2018-02-26 15:56:51 +02001320 priv->devtype = *devtype;
Anssi Hannula620050d2017-02-23 14:50:03 +02001321 spin_lock_init(&priv->tx_lock);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301322
1323 /* Get IRQ for the device */
1324 ndev->irq = platform_get_irq(pdev, 0);
1325 ndev->flags |= IFF_ECHO; /* We support local echo */
1326
1327 platform_set_drvdata(pdev, ndev);
1328 SET_NETDEV_DEV(ndev, &pdev->dev);
1329 ndev->netdev_ops = &xcan_netdev_ops;
1330
1331 /* Getting the CAN can_clk info */
1332 priv->can_clk = devm_clk_get(&pdev->dev, "can_clk");
1333 if (IS_ERR(priv->can_clk)) {
1334 dev_err(&pdev->dev, "Device clock not found.\n");
1335 ret = PTR_ERR(priv->can_clk);
1336 goto err_free;
1337 }
Anssi Hannula1598efe2018-02-26 15:56:51 +02001338
1339 priv->bus_clk = devm_clk_get(&pdev->dev, devtype->bus_clk_name);
1340 if (IS_ERR(priv->bus_clk)) {
1341 dev_err(&pdev->dev, "bus clock not found\n");
1342 ret = PTR_ERR(priv->bus_clk);
1343 goto err_free;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301344 }
1345
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301346 priv->write_reg = xcan_write_reg_le;
1347 priv->read_reg = xcan_read_reg_le;
1348
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301349 pm_runtime_enable(&pdev->dev);
1350 ret = pm_runtime_get_sync(&pdev->dev);
1351 if (ret < 0) {
1352 netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
1353 __func__, ret);
1354 goto err_pmdisable;
1355 }
1356
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301357 if (priv->read_reg(priv, XCAN_SR_OFFSET) != XCAN_SR_CONFIG_MASK) {
1358 priv->write_reg = xcan_write_reg_be;
1359 priv->read_reg = xcan_read_reg_be;
1360 }
1361
1362 priv->can.clock.freq = clk_get_rate(priv->can_clk);
1363
1364 netif_napi_add(ndev, &priv->napi, xcan_rx_poll, rx_max);
1365
1366 ret = register_candev(ndev);
1367 if (ret) {
1368 dev_err(&pdev->dev, "fail to register failed (err=%d)\n", ret);
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301369 goto err_disableclks;
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301370 }
1371
1372 devm_can_led_init(ndev);
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301373
1374 pm_runtime_put(&pdev->dev);
1375
Anssi Hannula620050d2017-02-23 14:50:03 +02001376 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 +05301377 priv->reg_base, ndev->irq, priv->can.clock.freq,
Anssi Hannula620050d2017-02-23 14:50:03 +02001378 tx_fifo_depth, priv->tx_max);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301379
1380 return 0;
1381
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301382err_disableclks:
1383 pm_runtime_put(priv->dev);
1384err_pmdisable:
1385 pm_runtime_disable(&pdev->dev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301386err_free:
1387 free_candev(ndev);
1388err:
1389 return ret;
1390}
1391
1392/**
1393 * xcan_remove - Unregister the device after releasing the resources
1394 * @pdev: Handle to the platform device structure
1395 *
1396 * This function frees all the resources allocated to the device.
1397 * Return: 0 always
1398 */
1399static int xcan_remove(struct platform_device *pdev)
1400{
1401 struct net_device *ndev = platform_get_drvdata(pdev);
1402 struct xcan_priv *priv = netdev_priv(ndev);
1403
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301404 unregister_candev(ndev);
Kedareswara rao Appana47166202015-10-26 11:41:54 +05301405 pm_runtime_disable(&pdev->dev);
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301406 netif_napi_del(&priv->napi);
1407 free_candev(ndev);
1408
1409 return 0;
1410}
1411
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301412static struct platform_driver xcan_driver = {
1413 .probe = xcan_probe,
1414 .remove = xcan_remove,
1415 .driver = {
Kedareswara rao Appanab1201e442014-05-21 12:30:59 +05301416 .name = DRIVER_NAME,
1417 .pm = &xcan_dev_pm_ops,
1418 .of_match_table = xcan_of_match,
1419 },
1420};
1421
1422module_platform_driver(xcan_driver);
1423
1424MODULE_LICENSE("GPL");
1425MODULE_AUTHOR("Xilinx Inc");
1426MODULE_DESCRIPTION("Xilinx CAN interface");