ctu_can_fd.c 36.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2
/*******************************************************************************
Martin Jeřábek's avatar
Martin Jeřábek committed
3
 *
4
 * CTU CAN FD IP Core
5
 * Copyright (C) 2015-2018
Martin Jeřábek's avatar
Martin Jeřábek committed
6
 *
7 8
 * Authors:
 *     Ondrej Ille <ondrej.ille@gmail.com>
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
9
 *     Martin Jerabek <martin.jerabek01@gmail.com>
Martin Jeřábek's avatar
Martin Jeřábek committed
10 11
 *
 * Project advisors:
12 13
 *	Jiri Novak <jnovak@fel.cvut.cz>
 *	Pavel Pisa <pisa@cmp.felk.cvut.cz>
Martin Jeřábek's avatar
Martin Jeřábek committed
14
 *
15 16 17
 * Department of Measurement         (http://meas.fel.cvut.cz/)
 * Faculty of Electrical Engineering (http://www.fel.cvut.cz)
 * Czech Technical University        (http://www.cvut.cz/)
Martin Jeřábek's avatar
Martin Jeřábek committed
18
 *
19 20 21 22
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
Martin Jeřábek's avatar
Martin Jeřábek committed
23
 *
24 25 26 27
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
Martin Jeřábek's avatar
Martin Jeřábek committed
28
 *
29 30
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
31 32
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
Martin Jeřábek's avatar
Martin Jeřábek committed
33
 *
34
 ******************************************************************************/
35

Martin Jeřábek's avatar
Martin Jeřábek committed
36 37 38 39 40
#include <linux/clk.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/io.h>
41
#include <linux/list.h>
Martin Jeřábek's avatar
Martin Jeřábek committed
42 43 44 45 46 47 48 49 50 51 52 53
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/skbuff.h>
#include <linux/string.h>
#include <linux/types.h>
#include <linux/can/dev.h>
#include <linux/can/error.h>
#include <linux/can/led.h>
#include <linux/pm_runtime.h>
54

Martin Jeřábek's avatar
Martin Jeřábek committed
55 56
#include "ctu_can_fd_hw.h"
#include "ctu_can_fd_regs.h"
57

Martin Jeřábek's avatar
Martin Jeřábek committed
58
#define DRIVER_NAME	"ctucanfd"
59

60 61 62 63 64 65 66
static bool use_msi = 1;
module_param(use_msi, bool, 0444);
MODULE_PARM_DESC(use_msi, "PCIe implementation use MSI interrupts. Default: 1 (yes)");

static bool pci_use_second = 1;
module_param(pci_use_second, bool, 0444);
MODULE_PARM_DESC(pci_use_second, "Use the second CAN core on PCIe card. Default: 1 (yes)");
Martin Jeřábek's avatar
Martin Jeřábek committed
67 68 69 70

/*
 * TX buffer rotation:
 * - when a buffer transitions to empty state, rotate order and priorities
71 72 73 74 75 76
 * - if more buffers seem to transition at the same time, rotate
 *   by the number of buffers
 * - it may be assumed that buffers transition to empty state in FIFO order
 *   (because we manage priorities that way)
 * - at frame filling, do not rotate anything, just increment buffer modulo
 *   counter
Martin Jeřábek's avatar
Martin Jeřábek committed
77 78 79
 */

struct ctucan_priv {
80
	struct can_priv can; // must be first member!
Martin Jeřábek's avatar
Martin Jeřábek committed
81
	struct ctucanfd_priv p;
Martin Jeřábek's avatar
Martin Jeřábek committed
82 83 84 85 86 87 88 89 90

	unsigned int txb_head;
	unsigned int txb_tail;
	u32 txb_prio;
	unsigned int txb_mask;

	struct napi_struct napi;
	struct device *dev;
	struct clk *can_clk;
91 92

	int irq_flags;
93
	unsigned long drv_flags;
94 95

	struct list_head peers_on_pdev;
Martin Jeřábek's avatar
Martin Jeřábek committed
96 97
};

98 99
#define CTUCAN_FLAG_RX_SCHED	1

Martin Jeřábek's avatar
Martin Jeřábek committed
100 101
static int ctucan_reset(struct net_device *ndev)
{
Martin Jeřábek's avatar
Martin Jeřábek committed
102
	int i;
Martin Jeřábek's avatar
Martin Jeřábek committed
103
	struct ctucan_priv *priv = netdev_priv(ndev);
104

105
	netdev_dbg(ndev, "ctucan_reset");
Martin Jeřábek's avatar
Martin Jeřábek committed
106

107
	ctu_can_fd_reset(&priv->p);
108
	for (i = 0; i < 100; ++i) {
109
		if (ctu_can_fd_check_access(&priv->p))
Martin Jeřábek's avatar
Martin Jeřábek committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
			return 0;
		udelay(100);
	}

	netdev_warn(ndev, "device did not leave reset\n");
	return -ETIMEDOUT;
}



/**
 * ctucan_set_bittiming - CAN set bit timing routine
 * @ndev:	Pointer to net_device structure
 *
 * This is the driver set bittiming routine.
 * Return: 0 on success and failure value on error
 */
static int ctucan_set_bittiming(struct net_device *ndev)
{
Martin Jeřábek's avatar
Martin Jeřábek committed
129
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
130
	struct can_bittiming *bt = &priv->can.bittiming;
131

132
	netdev_dbg(ndev, "ctucan_set_bittiming");
Martin Jeřábek's avatar
Martin Jeřábek committed
133

134
	if (ctu_can_fd_is_enabled(&priv->p)) {
Martin Jeřábek's avatar
Martin Jeřábek committed
135
		netdev_alert(ndev,
Martin Jeřábek's avatar
Martin Jeřábek committed
136
				 "BUG! Cannot set bittiming - CAN is enabled\n");
Martin Jeřábek's avatar
Martin Jeřábek committed
137 138 139
		return -EPERM;
	}

Martin Jeřábek's avatar
Martin Jeřábek committed
140
	/* Note that bt may be modified here */
141
	ctu_can_fd_set_nom_bittiming(&priv->p, bt);
Martin Jeřábek's avatar
Martin Jeřábek committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155

	return 0;
}


/**
 * ctucan_set_data_bittiming - CAN set data bit timing routine
 * @ndev:	Pointer to net_device structure
 *
 * This is the driver set data bittiming routine.
 * Return: 0 on success and failure value on error
 */
static int ctucan_set_data_bittiming(struct net_device *ndev)
{
Martin Jeřábek's avatar
Martin Jeřábek committed
156
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
157
	struct can_bittiming *dbt = &priv->can.data_bittiming;
158

159
	netdev_dbg(ndev, "ctucan_set_data_bittiming");
Martin Jeřábek's avatar
Martin Jeřábek committed
160

161
	if (ctu_can_fd_is_enabled(&priv->p)) {
Martin Jeřábek's avatar
Martin Jeřábek committed
162
		netdev_alert(ndev,
Martin Jeřábek's avatar
Martin Jeřábek committed
163
				 "BUG! Cannot set bittiming - CAN is enabled\n");
Martin Jeřábek's avatar
Martin Jeřábek committed
164 165 166
		return -EPERM;
	}

Martin Jeřábek's avatar
Martin Jeřábek committed
167
	/* Note that dbt may be modified here */
168
	ctu_can_fd_set_data_bittiming(&priv->p, dbt);
Martin Jeřábek's avatar
Martin Jeřábek committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182

	return 0;
}

/**
 * ctucan_chip_start - This the drivers start routine
 * @ndev:	Pointer to net_device structure
 *
 * This is the drivers start routine.
 *
 * Return: 0 on success and failure value on error
 */
static int ctucan_chip_start(struct net_device *ndev)
{
Martin Jeřábek's avatar
Martin Jeřábek committed
183
	struct ctucan_priv *priv = netdev_priv(ndev);
184
	union ctu_can_fd_int_stat int_ena, int_msk, int_enamask_mask;
Martin Jeřábek's avatar
Martin Jeřábek committed
185 186
	int err;
	struct can_ctrlmode mode;
187

188
	netdev_dbg(ndev, "ctucan_chip_start");
Martin Jeřábek's avatar
Martin Jeřábek committed
189 190 191 192 193 194 195 196

	err = ctucan_reset(ndev);
	if (err < 0)
		return err;

	priv->txb_prio = 0x01234567;
	priv->txb_head = 0;
	priv->txb_tail = 0;
197
	priv->p.write_reg(&priv->p, CTU_CAN_FD_TX_PRIORITY, priv->txb_prio);
Martin Jeřábek's avatar
Martin Jeřábek committed
198 199


Martin Jeřábek's avatar
Martin Jeřábek committed
200
	err = ctucan_set_bittiming(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
201 202 203 204 205 206 207 208 209 210 211 212
	if (err < 0)
		return err;

	err = ctucan_set_data_bittiming(ndev);
	if (err < 0)
		return err;


	/* Enable interrupts */
	int_ena.u32 = 0;
	int_ena.s.rbnei = 1;
	int_ena.s.txbhci = 1;
213

Martin Jeřábek's avatar
Martin Jeřábek committed
214
	int_ena.s.ewli = 1;
Martin Jeřábek's avatar
Martin Jeřábek committed
215 216
	int_ena.s.epi = 1;
	int_ena.s.doi = 1;
217

218
	int_enamask_mask.u32 = 0xFFFFFFFF;
Martin Jeřábek's avatar
Martin Jeřábek committed
219 220 221

	mode.flags = priv->can.ctrlmode;
	mode.mask = 0xFFFFFFFF;
222
	ctu_can_fd_set_mode_reg(&priv->p, &mode);
Martin Jeřábek's avatar
Martin Jeřábek committed
223 224 225

	/* One shot mode supported indirectly via Retransmit limit */
	if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
226
		ctu_can_fd_set_ret_limit(&priv->p, true, 0);
Martin Jeřábek's avatar
Martin Jeřábek committed
227 228 229 230 231 232 233

	/* Bus error reporting -> Allow Error interrupt */
	if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) {
		int_ena.s.ali = 1;
		int_ena.s.bei = 1;
	}

234 235
	int_msk.u32 = ~int_ena.u32; /* mask all disabled interrupts */

236 237
	clear_bit(CTUCAN_FLAG_RX_SCHED, &priv->drv_flags);

238
	ctu_can_fd_int_mask(&priv->p, int_msk, int_enamask_mask);
239
	ctu_can_fd_int_ena(&priv->p, int_ena, int_enamask_mask);
Martin Jeřábek's avatar
Martin Jeřábek committed
240 241 242 243

	priv->can.state = CAN_STATE_ERROR_ACTIVE;

	/* Enable the controller */
244
	ctu_can_fd_enable(&priv->p, true);
Martin Jeřábek's avatar
Martin Jeřábek committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

	return 0;
}

/**
 * ctucan_do_set_mode - This sets the mode of the driver
 * @ndev:	Pointer to net_device structure
 * @mode:	Tells the mode of the driver
 *
 * This check the drivers state and calls the
 * the corresponding modes to set.
 *
 * Return: 0 on success and failure value on error
 */
static int ctucan_do_set_mode(struct net_device *ndev, enum can_mode mode)
{
Martin Jeřábek's avatar
Martin Jeřábek committed
261
	int ret;
262

263
	netdev_dbg(ndev, "ctucan_do_set_mode");
Martin Jeřábek's avatar
Martin Jeřábek committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

	switch (mode) {
	case CAN_MODE_START:
		ret = ctucan_chip_start(ndev);
		if (ret < 0) {
			netdev_err(ndev, "ctucan_chip_start failed!\n");
			return ret;
		}
		netif_wake_queue(ndev);
		break;
	default:
		ret = -EOPNOTSUPP;
		break;
	}

	return ret;
}


/**
 * ctucan_start_xmit - Starts the transmission
 * @skb:	sk_buff pointer that contains data to be Txed
 * @ndev:	Pointer to net_device structure
 *
 * This function is invoked from upper layers to initiate transmission. This
 * function uses the next available free txbuf and populates their fields to
 * start the transmission.
 *
 * Return: 0 on success and failure value on error
 */
static int ctucan_start_xmit(struct sk_buff *skb, struct net_device *ndev)
{
Martin Jeřábek's avatar
Martin Jeřábek committed
296
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
297 298 299 300 301 302 303 304
	struct net_device_stats *stats = &ndev->stats;
	struct canfd_frame *cf = (struct canfd_frame *)skb->data;
	u32 txb_id;
	bool ok;

	if (can_dropped_invalid_skb(ndev, skb))
		return NETDEV_TX_OK;

305

Martin Jeřábek's avatar
Martin Jeřábek committed
306
	/* Check if the TX buffer is full */
307
	if (unlikely(!CTU_CAN_FD_TXTNF(ctu_can_get_status(&priv->p)))) {
Martin Jeřábek's avatar
Martin Jeřábek committed
308 309 310 311 312 313
		netif_stop_queue(ndev);
		netdev_err(ndev, "BUG!, no TXB free when queue awake!\n");
		return NETDEV_TX_BUSY;
	}

	txb_id = priv->txb_head & priv->txb_mask;
314
	netdev_dbg(ndev, "%s: using TXB#%u", __func__, txb_id);
Martin Jeřábek's avatar
Martin Jeřábek committed
315
	priv->txb_head++;
316 317 318
	ok = ctu_can_fd_insert_frame(&priv->p, cf, 0, txb_id,
				     can_is_canfd_skb(skb));

Martin Jeřábek's avatar
Martin Jeřábek committed
319 320 321 322 323
	if (!ok) {
		netdev_err(ndev, "BUG! cannot insert frame into TXTB!");
		return NETDEV_TX_OK; // TODO: what else to return
	}
	can_put_echo_skb(skb, ndev, txb_id);
324
	ctu_can_fd_txt_set_rdy(&priv->p, txb_id);
Martin Jeřábek's avatar
Martin Jeřábek committed
325

326
	if (!(cf->can_id & CAN_RTR_FLAG))
Martin Jeřábek's avatar
Martin Jeřábek committed
327 328 329
		stats->tx_bytes += cf->len;

	/* Check if all TX buffers are full */
330
	if (!CTU_CAN_FD_TXTNF(ctu_can_get_status(&priv->p)))
Martin Jeřábek's avatar
Martin Jeřábek committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
		netif_stop_queue(ndev);

	return NETDEV_TX_OK;
}

/**
 * xcan_rx -  Is called from CAN isr to complete the received
 *		frame  processing
 * @ndev:	Pointer to net_device structure
 *
 * This function is invoked from the CAN isr(poll) to process the Rx frames. It
 * does minimal processing and invokes "netif_receive_skb" to complete further
 * processing.
 * Return: 1 on success and 0 on failure.
 */
static int ctucan_rx(struct net_device *ndev)
{
Martin Jeřábek's avatar
Martin Jeřábek committed
348
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
349 350 351 352 353 354
	struct net_device_stats *stats = &ndev->stats;
	struct canfd_frame *cf;
	struct sk_buff *skb;
	u64 ts;
	union ctu_can_fd_frame_form_w ffw;

355
	ffw = ctu_can_fd_read_rx_ffw(&priv->p);
Martin Jeřábek's avatar
Martin Jeřábek committed
356

Martin Jeřábek's avatar
Martin Jeřábek committed
357
	if (ffw.s.fdf == FD_CAN)
358 359 360 361
		skb = alloc_canfd_skb(ndev, &cf);
	else
		skb = alloc_can_skb(ndev, (struct can_frame **) &cf);

Martin Jeřábek's avatar
Martin Jeřábek committed
362
	if (unlikely(!skb)) {
363 364 365
		int i;
		/* Remove the rest of the frame from the controller */
		for (i = 0; i < ffw.s.rwcnt; i++)
366
			ctu_can_fd_read_rx_word(&priv->p);
367

Martin Jeřábek's avatar
Martin Jeřábek committed
368 369 370 371
		stats->rx_dropped++;
		return 0;
	}

372
	ctu_can_fd_read_rx_frame_ffw(&priv->p, cf, &ts, ffw);
Martin Jeřábek's avatar
Martin Jeřábek committed
373 374 375 376 377 378 379 380 381

	stats->rx_bytes += cf->len;
	stats->rx_packets++;
	netif_receive_skb(skb);

	return 1;
}

/**
382
 * ctucan_err_interrupt - error frame Isr
Martin Jeřábek's avatar
Martin Jeřábek committed
383 384 385 386 387 388 389
 * @ndev:	net_device pointer
 * @isr:	interrupt status register value
 *
 * This is the CAN error interrupt and it will
 * check the the type of error and forward the error
 * frame to upper layers.
 */
390 391
static void ctucan_err_interrupt(struct net_device *ndev,
				 union ctu_can_fd_int_stat isr)
Martin Jeřábek's avatar
Martin Jeřábek committed
392
{
393
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
394 395 396
	struct net_device_stats *stats = &ndev->stats;
	struct can_frame *cf;
	struct sk_buff *skb;
397
	struct can_berr_counter berr;
398 399 400

	ctu_can_fd_read_err_ctrs(&priv->p, &berr);

401 402
	netdev_info(ndev, "%s: ISR = 0x%08x, rxerr %d, txerr %d",
		    __func__, isr.u32, berr.rxerr, berr.txerr);
Martin Jeřábek's avatar
Martin Jeřábek committed
403 404 405

	skb = alloc_can_err_skb(ndev, &cf);

406 407 408 409 410 411 412 413 414 415
	/*
	 * EWI: error warning
	 * DOI: RX overrun
	 * EPI: error passive or bus off
	 * ALI: arbitration lost (just informative)
	 * BEI: bus error interrupt
	 */
	if (isr.s.epi) {
		/* error passive or bus off */
		enum can_state state = ctu_can_fd_read_error_state(&priv->p);
416

417
		netdev_info(ndev, "  epi: state = %u", state);
418 419 420
		priv->can.state = state;
		if (state == CAN_STATE_BUS_OFF) {
			priv->can.can_stats.bus_off++;
421
			netdev_info(ndev, "    bus_off");
422 423 424 425 426
			can_bus_off(ndev);
			if (skb)
				cf->can_id |= CAN_ERR_BUSOFF;
		} else if (state == CAN_STATE_ERROR_PASSIVE) {
			priv->can.can_stats.error_passive++;
427
			netdev_info(ndev, "    error_passive");
428 429 430 431 432 433 434 435
			if (skb) {
				cf->can_id |= CAN_ERR_CRTL;
				cf->data[1] = (berr.rxerr > 127) ?
						CAN_ERR_CRTL_RX_PASSIVE :
						CAN_ERR_CRTL_TX_PASSIVE;
				cf->data[6] = berr.txerr;
				cf->data[7] = berr.rxerr;
			}
436 437 438
		} else if (state == CAN_STATE_ERROR_WARNING) {
			netdev_warn(ndev, "    error_warning, but ISR[EPI] was set! (HW bug?)");
			goto err_warning;
439 440
		} else {
			netdev_warn(ndev, "    unhandled error state!");
Martin Jeřábek's avatar
Martin Jeřábek committed
441
		}
Martin Jeřábek's avatar
Martin Jeřábek committed
442
	} else if (isr.s.ewli) {
443
err_warning:
444
		/* error warning */
Martin Jeřábek's avatar
Martin Jeřábek committed
445 446
		priv->can.state = CAN_STATE_ERROR_WARNING;
		priv->can.can_stats.error_warning++;
447
		netdev_info(ndev, "  error_warning");
Martin Jeřábek's avatar
Martin Jeřábek committed
448 449
		if (skb) {
			cf->can_id |= CAN_ERR_CRTL;
450
			cf->data[1] |= (berr.txerr > berr.rxerr) ?
Martin Jeřábek's avatar
Martin Jeřábek committed
451 452
					CAN_ERR_CRTL_TX_WARNING :
					CAN_ERR_CRTL_RX_WARNING;
453 454
			cf->data[6] = berr.txerr;
			cf->data[7] = berr.rxerr;
Martin Jeřábek's avatar
Martin Jeřábek committed
455 456 457
		}
	}

458 459
	/* Check for Arbitration Lost interrupt */
	if (isr.s.ali) {
460
		netdev_info(ndev, "  arbitration lost");
Martin Jeřábek's avatar
Martin Jeřábek committed
461 462 463 464 465 466 467 468
		priv->can.can_stats.arbitration_lost++;
		if (skb) {
			cf->can_id |= CAN_ERR_LOSTARB;
			cf->data[0] = CAN_ERR_LOSTARB_UNSPEC;
		}
	}

	/* Check for RX FIFO Overflow interrupt */
469
	if (isr.s.doi) {
470 471
		union ctu_can_fd_int_stat icr;

472
		netdev_info(ndev, "  doi (rx fifo overflow)");
Martin Jeřábek's avatar
Martin Jeřábek committed
473 474 475 476 477 478
		stats->rx_over_errors++;
		stats->rx_errors++;
		if (skb) {
			cf->can_id |= CAN_ERR_CRTL;
			cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
		}
479 480 481

		/* Clear Data Overrun */
		ctu_can_fd_clr_overrun_flag(&priv->p);
482 483 484 485 486 487 488 489
		/* TODO: this still sometimes fails without the dummy read
		 * Theoretically it is possible for the 2 bus accesses (flg
		 * clear, irq clear) to become tightly adjacent and the
		 * pipelining effects will cause that the IRQ clear request
		 * is ignored.
		 * The dummy read would prevent this.
		 * We still have to write a feature test for this.
		 */
490 491 492 493
#ifdef DEBUG
		netdev_info(ndev, "  DOS=%d after COMMAND[CDR]",
				   ctu_can_get_status(&priv->p).s.dor);
#endif
494 495 496 497 498

		/* And clear the DOI flag again */
		icr.u32 = 0;
		icr.s.doi = 1;
		ctu_can_fd_int_clr(&priv->p, icr);
Martin Jeřábek's avatar
Martin Jeřábek committed
499 500
	}

501 502
	/* Check for Bus Error interrupt */
	if (isr.s.bei) {
503
		netdev_info(ndev, "  bus error");
504 505 506
		priv->can.can_stats.bus_error++;
		stats->tx_errors++; // TODO: really?
		if (skb) {
Martin Jeřábek's avatar
Martin Jeřábek committed
507
			cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
508 509
			cf->data[2] = CAN_ERR_PROT_UNSPEC;
			cf->data[3] = CAN_ERR_PROT_LOC_UNSPEC;
Martin Jeřábek's avatar
Martin Jeřábek committed
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
		}
	}

	if (skb) {
		stats->rx_packets++;
		stats->rx_bytes += cf->can_dlc;
		netif_rx(skb);
	}
}

/**
 * ctucan_rx_poll - Poll routine for rx packets (NAPI)
 * @napi:	napi structure pointer
 * @quota:	Max number of rx packets to be processed.
 *
 * This is the poll routine for rx part.
 * It will process the packets maximux quota value.
 *
 * Return: number of packets received
 */
static int ctucan_rx_poll(struct napi_struct *napi, int quota)
{
Martin Jeřábek's avatar
Martin Jeřábek committed
532
	struct net_device *ndev = napi->dev;
Martin Jeřábek's avatar
Martin Jeřábek committed
533 534 535
	struct ctucan_priv *priv = netdev_priv(ndev);
	int work_done = 0;
	union ctu_can_fd_int_stat isr, iec;
536
	//netdev_dbg(ndev, "ctucan_rx_poll");
Martin Jeřábek's avatar
Martin Jeřábek committed
537 538 539 540 541

	iec.u32 = 0;
	iec.s.rbnei = 1;

	/* Get the interrupt status */
542
	isr = ctu_can_fd_int_sts(&priv->p);
Martin Jeřábek's avatar
Martin Jeřábek committed
543
	while (isr.s.rbnei && work_done < quota) {
544
		u32 framecnt = ctu_can_fd_get_rx_frame_count(&priv->p);
545

546
		netdev_dbg(ndev, "rx_poll: RBNEI set, %d frames in RX FIFO",
Martin Jeřábek's avatar
Martin Jeřábek committed
547
			    framecnt);
548
		if (framecnt == 0) {
549
			netdev_err(ndev, "rx_poll: RBNEI set, but there are no frames in the FIFO!");
550 551
			break;
		}
552
		/* TODO: maybe process DOI too? */
553

Martin Jeřábek's avatar
Martin Jeřábek committed
554
		ctucan_rx(ndev);
555
		ctu_can_fd_int_clr(&priv->p, iec);
Martin Jeřábek's avatar
Martin Jeřábek committed
556
		work_done++;
557
		isr = ctu_can_fd_int_sts(&priv->p);
Martin Jeřábek's avatar
Martin Jeřábek committed
558 559 560 561 562 563
	}

	if (work_done)
		can_led_event(ndev, CAN_LED_EVENT_RX);

	if (work_done < quota) {
564 565
		if (napi_complete_done(napi, work_done)) {
			iec.s.doi = 1; /* Also re-enable DOI */
566 567
			clear_bit(CTUCAN_FLAG_RX_SCHED, &priv->drv_flags);
			ctu_can_fd_int_ena_set(&priv->p, iec);
568
		}
Martin Jeřábek's avatar
Martin Jeřábek committed
569 570 571 572 573 574 575
	}

	return work_done;
}

static void ctucan_rotate_txb_prio(struct net_device *ndev)
{
Martin Jeřábek's avatar
Martin Jeřábek committed
576
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
577
	u32 prio = priv->txb_prio;
578
	u32 nbuffersm1 = priv->txb_mask; /* nbuffers - 1 */
Martin Jeřábek's avatar
Martin Jeřábek committed
579

580
	prio = (prio << 4) | ((prio >> (nbuffersm1*4)) & 0xF);
581 582
	netdev_dbg(ndev, "%s: from 0x%08x to 0x%08x",
		   __func__, priv->txb_prio, prio);
Martin Jeřábek's avatar
Martin Jeřábek committed
583
	priv->txb_prio = prio;
584
	priv->p.write_reg(&priv->p, CTU_CAN_FD_TX_PRIORITY, prio);
Martin Jeřábek's avatar
Martin Jeřábek committed
585 586 587 588 589 590 591 592 593
}

/**
 * xcan_tx_interrupt - Tx Done Isr
 * @ndev:	net_device pointer
 * @isr:	Interrupt status register value
 */
static void ctucan_tx_interrupt(struct net_device *ndev)
{
Martin Jeřábek's avatar
Martin Jeřábek committed
594
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
595
	struct net_device_stats *stats = &ndev->stats;
596 597 598 599
	bool first = true;
	union ctu_can_fd_int_stat icr;
	bool some_buffers_processed;

600
	netdev_dbg(ndev, "%s", __func__);
Martin Jeřábek's avatar
Martin Jeřábek committed
601 602

	/*
603 604 605 606 607 608
	 *  read tx_status
	 *  if txb[n].finished (bit 2)
	 *	if ok -> echo
	 *	if error / aborted -> ?? (find how to handle oneshot mode)
	 *	txb_tail++
	 */
Martin Jeřábek's avatar
Martin Jeřábek committed
609 610


611 612 613 614 615 616 617 618
	icr.u32 = 0;
	icr.s.txbhci = 1;
	do {
		some_buffers_processed = false;
		while ((int)(priv->txb_head - priv->txb_tail) > 0) {
			u32 txb_idx = priv->txb_tail & priv->txb_mask;
			u32 status = ctu_can_fd_get_tx_status(&priv->p, txb_idx);

619 620 621
			netdev_dbg(ndev, "TXI: TXB#%u: status 0x%x",
				   txb_idx, status);

622 623 624 625 626
			switch (status) {
			case TXT_TOK:
				netdev_dbg(ndev, "TXT_OK");
				can_get_echo_skb(ndev, txb_idx);
				stats->tx_packets++;
627
				break;
628
			case TXT_ERR:
629 630
				/* This indicated that retransmit limit has been
				 * reached. Obviously we should not echo the
631 632 633 634
				 * frame, but also not indicate any kind
				 * of error. If desired, it was already reported
				 * (possible multiple times) on each arbitration
				 * lost.
635
				 */
636 637
				netdev_warn(ndev, "TXB in Error state");
				can_free_echo_skb(ndev, txb_idx);
638
				stats->tx_dropped++;
639
				break;
640
			case TXT_ABT:
641 642 643 644
				/* Same as for TXT_ERR, only with different
				 * cause. We *could* re-queue the frame, but
				 * multiqueue/abort is not supported yet anyway.
				 */
645 646
				netdev_warn(ndev, "TXB in Aborted state");
				can_free_echo_skb(ndev, txb_idx);
647
				stats->tx_dropped++;
648
				break;
649 650 651 652 653
			default:
				/* Bug only if the first buffer is not finished,
				 * otherwise it is pretty much expected
				 */
				if (first) {
654 655
					netdev_err(ndev, "BUG: TXB#%u not in a finished state (0x%x)!",
						   txb_idx, status);
656 657 658 659 660 661 662 663 664
					/* do not clear nor wake */
					return;
				}
				/* some_buffers_processed is still false */
				goto clear;
			}
			priv->txb_tail++;
			first = false;
			some_buffers_processed = true;
Martin Jeřábek's avatar
Martin Jeřábek committed
665

666 667 668
			/* Adjust priorities *before* marking the buffer
			 * as empty.
			 */
669 670
			ctucan_rotate_txb_prio(ndev);
			ctu_can_fd_txt_set_empty(&priv->p, txb_idx);
Martin Jeřábek's avatar
Martin Jeřábek committed
671
		}
672 673 674 675 676 677
clear:
		/* Clear the interrupt again as not to receive it again for
		 * a buffer we already handled (possibly causing the bug log)
		 */
		ctu_can_fd_int_clr(&priv->p, icr);
	} while (some_buffers_processed);
Martin Jeřábek's avatar
Martin Jeřábek committed
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
	can_led_event(ndev, CAN_LED_EVENT_TX);
	netif_wake_queue(ndev);
}

/**
 * xcan_interrupt - CAN Isr
 * @irq:	irq number
 * @dev_id:	device id poniter
 *
 * This is the xilinx CAN Isr. It checks for the type of interrupt
 * and invokes the corresponding ISR.
 *
 * Return:
 * IRQ_NONE - If CAN device is in sleep mode, IRQ_HANDLED otherwise
 */
static irqreturn_t ctucan_interrupt(int irq, void *dev_id)
{
Martin Jeřábek's avatar
Martin Jeřábek committed
695
	struct net_device *ndev = (struct net_device *)dev_id;
Martin Jeřábek's avatar
Martin Jeřábek committed
696 697
	struct ctucan_priv *priv = netdev_priv(ndev);
	union ctu_can_fd_int_stat isr, icr;
698 699
	int irq_loops = 0;

700
	netdev_dbg(ndev, "ctucan_interrupt");
Martin Jeřábek's avatar
Martin Jeřábek committed
701

702 703 704
	do {
		/* Get the interrupt status */
		isr = ctu_can_fd_int_sts(&priv->p);
Martin Jeřábek's avatar
Martin Jeřábek committed
705

706 707
		if (test_bit(CTUCAN_FLAG_RX_SCHED, &priv->drv_flags))
			isr.s.rbnei = 0;
Martin Jeřábek's avatar
Martin Jeřábek committed
708

709
		if (!isr.u32)
710
			return irq_loops ? IRQ_HANDLED : IRQ_NONE;
711

712 713 714 715 716 717
		/* Receive Buffer Not Empty Interrupt */
		if (isr.s.rbnei) {
			netdev_dbg(ndev, "RXBNEI");
			icr.u32 = 0;
			icr.s.rbnei = 1;
			ctu_can_fd_int_clr(&priv->p, icr);
Martin Jeřábek's avatar
Martin Jeřábek committed
718

719 720 721 722 723 724
			/* Disable RXBNEI and DOI */
			icr.s.doi = 1;
			ctu_can_fd_int_ena_clr(&priv->p, icr);
			set_bit(CTUCAN_FLAG_RX_SCHED, &priv->drv_flags);
			napi_schedule(&priv->napi);
		}
Martin Jeřábek's avatar
Martin Jeřábek committed
725

726 727 728 729 730 731 732 733 734 735 736
		/* TX Buffer HW Command Interrupt */
		if (isr.s.txbhci) {
			netdev_dbg(ndev, "TXBHCI");
			icr.u32 = 0;
			icr.s.txbhci = 1;
			ctu_can_fd_int_clr(&priv->p, icr);
			ctucan_tx_interrupt(ndev);
		}

		/* Error interrupts */
		if (isr.s.ewli || isr.s.doi || isr.s.epi || isr.s.ali) {
737 738 739
			union ctu_can_fd_int_stat ierrmask = { .s = {
				  .ewli = 1, .doi = 1, .epi = 1,
				  .ali = 1, .bei = 1 } };
740 741
			icr.u32 = isr.u32 & ierrmask.u32;

742 743
			netdev_dbg(ndev, "some ERR interrupt: clearing 0x%08x",
				   icr.u32);
744 745 746 747 748 749
			ctu_can_fd_int_clr(&priv->p, icr);
			ctucan_err_interrupt(ndev, isr);
		}
		/* Ignore RI, TI, LFI, RFI, BSI */
	} while (irq_loops++ < 10000);

750
	netdev_err(ndev, "%s: stuck interrupt, stopping\n", __func__);
751 752 753

	{
		union ctu_can_fd_int_stat imask, ival;
754

755 756 757 758 759
		imask.u32 = 0xffffffff;
		ival.u32 = 0;
		ctu_can_fd_int_ena(&priv->p, imask, ival);
		ctu_can_fd_int_mask(&priv->p, imask, ival);
	}
Martin Jeřábek's avatar
Martin Jeřábek committed
760 761 762 763 764 765 766 767 768 769 770 771 772

	return IRQ_HANDLED;
}

/**
 * ctucan_chip_stop - Driver stop routine
 * @ndev:	Pointer to net_device structure
 *
 * This is the drivers stop routine. It will disable the
 * interrupts and disable the controller.
 */
static void ctucan_chip_stop(struct net_device *ndev)
{
Martin Jeřábek's avatar
Martin Jeřábek committed
773
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
774
	union ctu_can_fd_int_stat ena, mask;
775

776
	netdev_dbg(ndev, "ctucan_chip_stop");
Martin Jeřábek's avatar
Martin Jeřábek committed
777 778 779 780

	ena.u32 = 0;

	/* Disable interrupts and disable can */
781 782
	ctu_can_fd_int_ena(&priv->p, ena, mask);
	ctu_can_fd_enable(&priv->p, false);
783
	clear_bit(CTUCAN_FLAG_RX_SCHED, &priv->drv_flags);
Martin Jeřábek's avatar
Martin Jeřábek committed
784 785 786 787 788 789 790 791 792 793 794 795
	priv->can.state = CAN_STATE_STOPPED;
}

/**
 * ctucan_open - Driver open routine
 * @ndev:	Pointer to net_device structure
 *
 * This is the driver open routine.
 * Return: 0 on success and failure value on error
 */
static int ctucan_open(struct net_device *ndev)
{
Martin Jeřábek's avatar
Martin Jeřábek committed
796
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
797
	int ret;
798

799
	netdev_dbg(ndev, "ctucan_open");
Martin Jeřábek's avatar
Martin Jeřábek committed
800 801 802 803 804 805 806 807 808

	ret = pm_runtime_get_sync(priv->dev);
	if (ret < 0) {
		netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
				__func__, ret);
		return ret;
	}

	/*
809 810 811 812
	 * ret = ctucan_reset(ndev);
	 * if (ret < 0)
	 *	goto err;
	 */
Martin Jeřábek's avatar
Martin Jeřábek committed
813

814
	ret = request_irq(ndev->irq, ctucan_interrupt, priv->irq_flags,
Martin Jeřábek's avatar
Martin Jeřábek committed
815 816 817 818 819 820 821 822 823
			ndev->name, ndev);
	if (ret < 0) {
		netdev_err(ndev, "irq allocation for CAN failed\n");
		goto err;
	}

	/* Common open */
	ret = open_candev(ndev);
	if (ret) {
Martin Jeřábek's avatar
Martin Jeřábek committed
824 825 826
		netdev_warn(ndev, "open_candev failed!\n");
		goto err_irq;
	}
Martin Jeřábek's avatar
Martin Jeřábek committed
827

Martin Jeřábek's avatar
Martin Jeřábek committed
828
	ret = ctucan_chip_start(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
829 830 831 832 833
	if (ret < 0) {
		netdev_err(ndev, "ctucan_chip_start failed!\n");
		goto err_candev;
	}

834
	netdev_info(ndev, "ctu_can_fd device registered");
Martin Jeřábek's avatar
Martin Jeřábek committed
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
	can_led_event(ndev, CAN_LED_EVENT_OPEN);
	napi_enable(&priv->napi);
	netif_start_queue(ndev);

	return 0;

err_candev:
	close_candev(ndev);
err_irq:
	free_irq(ndev->irq, ndev);
err:
	pm_runtime_put(priv->dev);

	return ret;
}

/**
 * ctucan_close - Driver close routine
 * @ndev:	Pointer to net_device structure
 *
 * Return: 0 always
 */
static int ctucan_close(struct net_device *ndev)
{
Martin Jeřábek's avatar
Martin Jeřábek committed
859
	struct ctucan_priv *priv = netdev_priv(ndev);
860

861
	netdev_dbg(ndev, "ctucan_close");
Martin Jeřábek's avatar
Martin Jeřábek committed
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885

	netif_stop_queue(ndev);
	napi_disable(&priv->napi);
	ctucan_chip_stop(ndev);
	free_irq(ndev->irq, ndev);
	close_candev(ndev);

	can_led_event(ndev, CAN_LED_EVENT_STOP);
	pm_runtime_put(priv->dev);

	return 0;
}

/**
 * ctucan_get_berr_counter - error counter routine
 * @ndev:	Pointer to net_device structure
 * @bec:	Pointer to can_berr_counter structure
 *
 * This is the driver error counter routine.
 * Return: 0 on success and failure value on error
 */
static int ctucan_get_berr_counter(const struct net_device *ndev,
					struct can_berr_counter *bec)
{
Martin Jeřábek's avatar
Martin Jeřábek committed
886
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
887
	int ret;
888

889
	netdev_dbg(ndev, "ctucan_get_berr_counter");
Martin Jeřábek's avatar
Martin Jeřábek committed
890 891 892 893 894 895 896 897

	ret = pm_runtime_get_sync(priv->dev);
	if (ret < 0) {
		netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
				__func__, ret);
		return ret;
	}

898
	ctu_can_fd_read_err_ctrs(&priv->p, bec);
Martin Jeřábek's avatar
Martin Jeřábek committed
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913

	pm_runtime_put(priv->dev);

	return 0;
}

static const struct net_device_ops ctucan_netdev_ops = {
	.ndo_open	= ctucan_open,
	.ndo_stop	= ctucan_close,
	.ndo_start_xmit	= ctucan_start_xmit,
	.ndo_change_mtu	= can_change_mtu,
};

static __maybe_unused int ctucan_suspend(struct device *dev)
{
Martin Jeřábek's avatar
Martin Jeřábek committed
914
	struct net_device *ndev = dev_get_drvdata(dev);
Martin Jeřábek's avatar
Martin Jeřábek committed
915
	struct ctucan_priv *priv = netdev_priv(ndev);
916

917
	netdev_dbg(ndev, "ctucan_suspend");
Martin Jeřábek's avatar
Martin Jeřábek committed
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932

	if (netif_running(ndev)) {
		netif_stop_queue(ndev);
		netif_device_detach(ndev);
	}

	priv->can.state = CAN_STATE_SLEEPING;

	return 0;
}

static __maybe_unused int ctucan_resume(struct device *dev)
{
	struct net_device *ndev = dev_get_drvdata(dev);
	struct ctucan_priv *priv = netdev_priv(ndev);
933

934
	netdev_dbg(ndev, "ctucan_resume");
Martin Jeřábek's avatar
Martin Jeřábek committed
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952

	priv->can.state = CAN_STATE_ERROR_ACTIVE;

	if (netif_running(ndev)) {
		netif_device_attach(ndev);
		netif_start_queue(ndev);
	}

	return 0;
}


static const struct dev_pm_ops ctucan_dev_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(ctucan_suspend, ctucan_resume)
};


/**
953
 * ctucan_probe_common - Device type independent registration call
Martin Jeřábek's avatar
Martin Jeřábek committed
954 955 956 957
 *
 * This function does all the memory allocation and registration for the CAN
 * device.
 *
958 959 960 961 962
 * @dev:	Handle to the generic device structure
 * @addr:	Base address of CTU CAN FD core address
 * @irq:	Interrupt number
 * @ntxbufs:	Number of implemented Tx buffers
 * @can_clk_rate: Clock rate, if 0 then clock are taken from device node
963
 * @pm_enable_call: Whether pm_runtime_enable should be called
964 965
 * @set_drvdata_fnc: Function to set network driver data for physical device
 *
Martin Jeřábek's avatar
Martin Jeřábek committed
966 967
 * Return: 0 on success and failure value on error
 */
968
static int ctucan_probe_common(struct device *dev, void __iomem *addr,
969 970 971
	      int irq, unsigned int ntxbufs, unsigned long can_clk_rate,
	      int pm_enable_call, void (*set_drvdata_fnc)(struct device *dev,
				     struct net_device *ndev))
Martin Jeřábek's avatar
Martin Jeřábek committed
972 973
{
	struct ctucan_priv *priv;
974
	struct net_device *ndev;
Martin Jeřábek's avatar
Martin Jeřábek committed
975 976 977 978 979 980 981 982
	int ret;

	/* Create a CAN device instance */
	ndev = alloc_candev(sizeof(struct ctucan_priv), ntxbufs);
	if (!ndev)
		return -ENOMEM;

	priv = netdev_priv(ndev);
983
	INIT_LIST_HEAD(&priv->peers_on_pdev);
Martin Jeřábek's avatar
Martin Jeřábek committed
984
	priv->txb_mask = ntxbufs-1;
985
	priv->dev = dev;
Martin Jeřábek's avatar
Martin Jeřábek committed
986 987 988
	priv->can.bittiming_const = &ctu_can_fd_bit_timing_max;
	priv->can.data_bittiming_const = &ctu_can_fd_bit_timing_data_max;
	priv->can.do_set_mode = ctucan_do_set_mode;
989 990 991 992 993

	/* Needed for timing adjustment to be performed as soon as possible */
	priv->can.do_set_bittiming = ctucan_set_bittiming;
	priv->can.do_set_data_bittiming = ctucan_set_data_bittiming;

Martin Jeřábek's avatar
Martin Jeřábek committed
994 995
	priv->can.do_get_berr_counter = ctucan_get_berr_counter;
	//priv->can.do_get_state = ctucan_get_state;
996 997 998 999 1000 1001 1002
	priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK
					| CAN_CTRLMODE_LISTENONLY
					| CAN_CTRLMODE_3_SAMPLES
					| CAN_CTRLMODE_FD
					| CAN_CTRLMODE_PRESUME_ACK
					| CAN_CTRLMODE_FD_NON_ISO
					| CAN_CTRLMODE_ONE_SHOT;
1003
	priv->p.mem_base = addr;
Martin Jeřábek's avatar
Martin Jeřábek committed
1004 1005

	/* Get IRQ for the device */
1006
	ndev->irq = irq;
Martin Jeřábek's avatar
Martin Jeřábek committed
1007 1008
	ndev->flags |= IFF_ECHO;	/* We support local echo */

1009 1010 1011
	if (set_drvdata_fnc != NULL)
		set_drvdata_fnc(dev, ndev);
	SET_NETDEV_DEV(ndev, dev);
Martin Jeřábek's avatar
Martin Jeřábek committed
1012 1013 1014
	ndev->netdev_ops = &ctucan_netdev_ops;

	/* Getting the CAN can_clk info */
1015 1016 1017 1018 1019 1020 1021 1022
	if (can_clk_rate == 0) {
		priv->can_clk = devm_clk_get(dev, NULL);
		if (IS_ERR(priv->can_clk)) {
			dev_err(dev, "Device clock not found.\n");
			ret = PTR_ERR(priv->can_clk);
			goto err_free;
		}
		can_clk_rate = clk_get_rate(priv->can_clk);
Martin Jeřábek's avatar
Martin Jeřábek committed
1023 1024
	}

1025 1026
	priv->p.write_reg = ctu_can_fd_write32;
	priv->p.read_reg = ctu_can_fd_read32;
Martin Jeřábek's avatar
Martin Jeřábek committed
1027

1028 1029
	if (pm_enable_call)
		pm_runtime_enable(dev);
1030
	ret = pm_runtime_get_sync(dev);
Martin Jeřábek's avatar
Martin Jeřábek committed
1031 1032 1033 1034 1035 1036
	if (ret < 0) {
		netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
			__func__, ret);
		goto err_pmdisable;
	}

1037
	if ((priv->p.read_reg(&priv->p, CTU_CAN_FD_DEVICE_ID) &
1038
			    0xFFFF) != CTU_CAN_FD_ID) {
1039 1040
		priv->p.write_reg = ctu_can_fd_write32_be;
		priv->p.read_reg = ctu_can_fd_read32_be;
1041
		if ((priv->p.read_reg(&priv->p, CTU_CAN_FD_DEVICE_ID) &
1042
			      0xFFFF) != CTU_CAN_FD_ID) {
1043
			netdev_err(ndev, "CTU_CAN_FD signature not found\n");
1044
			goto err_disableclks;
1045
		}
Martin Jeřábek's avatar
Martin Jeřábek committed
1046 1047 1048
	}

	ret = ctucan_reset(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
1049 1050
	if (ret < 0)
		goto err_pmdisable;
Martin Jeřábek's avatar
Martin Jeřábek committed
1051

1052
	priv->can.clock.freq = can_clk_rate;
Martin Jeřábek's avatar
Martin Jeřábek committed
1053 1054 1055 1056 1057

	netif_napi_add(ndev, &priv->napi, ctucan_rx_poll, NAPI_POLL_WEIGHT);

	ret = register_candev(ndev);
	if (ret) {
1058
		dev_err(dev, "fail to register failed (err=%d)\n", ret);
Martin Jeřábek's avatar
Martin Jeřábek committed
1059 1060 1061 1062 1063
		goto err_disableclks;
	}

	devm_can_led_init(ndev);

1064
	pm_runtime_put(dev);
Martin Jeřábek's avatar