ctu_can_fd.c 36 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
/* TX buffer rotation:
Martin Jeřábek's avatar
Martin Jeřábek committed
69
 * - when a buffer transitions to empty state, rotate order and priorities
70 71 72 73 74 75
 * - 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
76 77 78
 */

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

	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;
90 91

	int irq_flags;
92
	unsigned long drv_flags;
93 94

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

97 98
#define CTUCAN_FLAG_RX_SCHED	1

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

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

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

	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
126
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
127
	struct can_bittiming *bt = &priv->can.bittiming;
128

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

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

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

	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
152
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
153
	struct can_bittiming *dbt = &priv->can.data_bittiming;
154

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

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

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

	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
179
	struct ctucan_priv *priv = netdev_priv(ndev);
180
	union ctu_can_fd_int_stat int_ena, int_msk, int_enamask_mask;
Martin Jeřábek's avatar
Martin Jeřábek committed
181 182
	int err;
	struct can_ctrlmode mode;
183

184
	netdev_dbg(ndev, "ctucan_chip_start");
Martin Jeřábek's avatar
Martin Jeřábek committed
185 186 187 188 189 190 191 192

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

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

Martin Jeřábek's avatar
Martin Jeřábek committed
195
	err = ctucan_set_bittiming(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
196 197 198 199 200 201 202 203 204 205 206
	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;
207

Martin Jeřábek's avatar
Martin Jeřábek committed
208
	int_ena.s.ewli = 1;
Martin Jeřábek's avatar
Martin Jeřábek committed
209 210
	int_ena.s.epi = 1;
	int_ena.s.doi = 1;
211

212
	int_enamask_mask.u32 = 0xFFFFFFFF;
Martin Jeřábek's avatar
Martin Jeřábek committed
213 214 215

	mode.flags = priv->can.ctrlmode;
	mode.mask = 0xFFFFFFFF;
216
	ctu_can_fd_set_mode_reg(&priv->p, &mode);
Martin Jeřábek's avatar
Martin Jeřábek committed
217 218 219

	/* One shot mode supported indirectly via Retransmit limit */
	if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
220
		ctu_can_fd_set_ret_limit(&priv->p, true, 0);
Martin Jeřábek's avatar
Martin Jeřábek committed
221 222 223 224 225 226 227

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

228 229
	int_msk.u32 = ~int_ena.u32; /* mask all disabled interrupts */

230 231
	clear_bit(CTUCAN_FLAG_RX_SCHED, &priv->drv_flags);

232
	ctu_can_fd_int_mask(&priv->p, int_msk, int_enamask_mask);
233
	ctu_can_fd_int_ena(&priv->p, int_ena, int_enamask_mask);
Martin Jeřábek's avatar
Martin Jeřábek committed
234 235 236 237

	priv->can.state = CAN_STATE_ERROR_ACTIVE;

	/* Enable the controller */
238
	ctu_can_fd_enable(&priv->p, true);
Martin Jeřábek's avatar
Martin Jeřábek committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

	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
255
	int ret;
256

257
	netdev_dbg(ndev, "ctucan_do_set_mode");
Martin Jeřábek's avatar
Martin Jeřábek committed
258 259 260 261 262 263 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

	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
289
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
290 291 292 293 294 295 296 297 298
	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;

	/* Check if the TX buffer is full */
299
	if (unlikely(!CTU_CAN_FD_TXTNF(ctu_can_get_status(&priv->p)))) {
Martin Jeřábek's avatar
Martin Jeřábek committed
300 301 302 303 304 305
		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;
306
	netdev_dbg(ndev, "%s: using TXB#%u", __func__, txb_id);
Martin Jeřábek's avatar
Martin Jeřábek committed
307
	priv->txb_head++;
308 309 310
	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
311 312 313 314 315
	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);
316
	ctu_can_fd_txt_set_rdy(&priv->p, txb_id);
Martin Jeřábek's avatar
Martin Jeřábek committed
317

318
	if (!(cf->can_id & CAN_RTR_FLAG))
Martin Jeřábek's avatar
Martin Jeřábek committed
319 320 321
		stats->tx_bytes += cf->len;

	/* Check if all TX buffers are full */
322
	if (!CTU_CAN_FD_TXTNF(ctu_can_get_status(&priv->p)))
Martin Jeřábek's avatar
Martin Jeřábek committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
		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
340
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
341 342 343 344 345 346
	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;

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

Martin Jeřábek's avatar
Martin Jeřábek committed
349
	if (ffw.s.fdf == FD_CAN)
350 351
		skb = alloc_canfd_skb(ndev, &cf);
	else
352
		skb = alloc_can_skb(ndev, (struct can_frame **)&cf);
353

Martin Jeřábek's avatar
Martin Jeřábek committed
354
	if (unlikely(!skb)) {
355 356 357
		int i;
		/* Remove the rest of the frame from the controller */
		for (i = 0; i < ffw.s.rwcnt; i++)
358
			ctu_can_fd_read_rx_word(&priv->p);
359

Martin Jeřábek's avatar
Martin Jeřábek committed
360 361 362 363
		stats->rx_dropped++;
		return 0;
	}

364
	ctu_can_fd_read_rx_frame_ffw(&priv->p, cf, &ts, ffw);
Martin Jeřábek's avatar
Martin Jeřábek committed
365 366 367 368 369 370 371 372 373

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

	return 1;
}

/**
374
 * ctucan_err_interrupt - error frame Isr
Martin Jeřábek's avatar
Martin Jeřábek committed
375 376 377 378 379 380 381
 * @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.
 */
382 383
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
384
{
385
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
386 387 388
	struct net_device_stats *stats = &ndev->stats;
	struct can_frame *cf;
	struct sk_buff *skb;
389
	struct can_berr_counter berr;
390 391 392

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

393 394
	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
395 396 397

	skb = alloc_can_err_skb(ndev, &cf);

398
	/* EWI: error warning
399 400 401 402 403 404 405 406
	 * 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);
407

408
		netdev_info(ndev, "  epi: state = %u", state);
409 410 411
		priv->can.state = state;
		if (state == CAN_STATE_BUS_OFF) {
			priv->can.can_stats.bus_off++;
412
			netdev_info(ndev, "    bus_off");
413 414 415 416 417
			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++;
418
			netdev_info(ndev, "    error_passive");
419 420 421 422 423 424 425 426
			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;
			}
427 428 429
		} else if (state == CAN_STATE_ERROR_WARNING) {
			netdev_warn(ndev, "    error_warning, but ISR[EPI] was set! (HW bug?)");
			goto err_warning;
430 431
		} else {
			netdev_warn(ndev, "    unhandled error state!");
Martin Jeřábek's avatar
Martin Jeřábek committed
432
		}
Martin Jeřábek's avatar
Martin Jeřábek committed
433
	} else if (isr.s.ewli) {
434
err_warning:
435
		/* error warning */
Martin Jeřábek's avatar
Martin Jeřábek committed
436 437
		priv->can.state = CAN_STATE_ERROR_WARNING;
		priv->can.can_stats.error_warning++;
438
		netdev_info(ndev, "  error_warning");
Martin Jeřábek's avatar
Martin Jeřábek committed
439 440
		if (skb) {
			cf->can_id |= CAN_ERR_CRTL;
441
			cf->data[1] |= (berr.txerr > berr.rxerr) ?
Martin Jeřábek's avatar
Martin Jeřábek committed
442 443
					CAN_ERR_CRTL_TX_WARNING :
					CAN_ERR_CRTL_RX_WARNING;
444 445
			cf->data[6] = berr.txerr;
			cf->data[7] = berr.rxerr;
Martin Jeřábek's avatar
Martin Jeřábek committed
446 447 448
		}
	}

449 450
	/* Check for Arbitration Lost interrupt */
	if (isr.s.ali) {
451
		netdev_info(ndev, "  arbitration lost");
Martin Jeřábek's avatar
Martin Jeřábek committed
452 453 454 455 456 457 458 459
		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 */
460
	if (isr.s.doi) {
461 462
		union ctu_can_fd_int_stat icr;

463
		netdev_info(ndev, "  doi (rx fifo overflow)");
Martin Jeřábek's avatar
Martin Jeřábek committed
464 465 466 467 468 469
		stats->rx_over_errors++;
		stats->rx_errors++;
		if (skb) {
			cf->can_id |= CAN_ERR_CRTL;
			cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
		}
470 471 472

		/* Clear Data Overrun */
		ctu_can_fd_clr_overrun_flag(&priv->p);
473 474 475 476 477 478 479 480
		/* 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.
		 */
481 482
#ifdef DEBUG
		netdev_info(ndev, "  DOS=%d after COMMAND[CDR]",
483
			    ctu_can_get_status(&priv->p).s.dor);
484
#endif
485 486 487 488 489

		/* 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
490 491
	}

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

	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
523
	struct net_device *ndev = napi->dev;
Martin Jeřábek's avatar
Martin Jeřábek committed
524 525 526
	struct ctucan_priv *priv = netdev_priv(ndev);
	int work_done = 0;
	union ctu_can_fd_int_stat isr, iec;
527
	//netdev_dbg(ndev, "ctucan_rx_poll");
Martin Jeřábek's avatar
Martin Jeřábek committed
528 529 530 531 532

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

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

537
		netdev_dbg(ndev, "rx_poll: RBNEI set, %d frames in RX FIFO",
538
			   framecnt);
539
		if (framecnt == 0) {
540
			netdev_err(ndev, "rx_poll: RBNEI set, but there are no frames in the FIFO!");
541 542
			break;
		}
543
		/* TODO: maybe process DOI too? */
544

Martin Jeřábek's avatar
Martin Jeřábek committed
545
		ctucan_rx(ndev);
546
		ctu_can_fd_int_clr(&priv->p, iec);
Martin Jeřábek's avatar
Martin Jeřábek committed
547
		work_done++;
548
		isr = ctu_can_fd_int_sts(&priv->p);
Martin Jeřábek's avatar
Martin Jeřábek committed
549 550 551 552 553 554
	}

	if (work_done)
		can_led_event(ndev, CAN_LED_EVENT_RX);

	if (work_done < quota) {
555 556
		if (napi_complete_done(napi, work_done)) {
			iec.s.doi = 1; /* Also re-enable DOI */
557 558
			clear_bit(CTUCAN_FLAG_RX_SCHED, &priv->drv_flags);
			ctu_can_fd_int_ena_set(&priv->p, iec);
559
		}
Martin Jeřábek's avatar
Martin Jeřábek committed
560 561 562 563 564 565 566
	}

	return work_done;
}

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

571
	prio = (prio << 4) | ((prio >> (nbuffersm1 * 4)) & 0xF);
572 573
	netdev_dbg(ndev, "%s: from 0x%08x to 0x%08x",
		   __func__, priv->txb_prio, prio);
Martin Jeřábek's avatar
Martin Jeřábek committed
574
	priv->txb_prio = prio;
575
	priv->p.write_reg(&priv->p, CTU_CAN_FD_TX_PRIORITY, prio);
Martin Jeřábek's avatar
Martin Jeřábek committed
576 577 578 579 580 581 582 583 584
}

/**
 * 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
585
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
586
	struct net_device_stats *stats = &ndev->stats;
587 588 589 590
	bool first = true;
	union ctu_can_fd_int_stat icr;
	bool some_buffers_processed;

591
	netdev_dbg(ndev, "%s", __func__);
Martin Jeřábek's avatar
Martin Jeřábek committed
592

593
	/*  read tx_status
594 595 596 597 598
	 *  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
599

600 601 602 603 604 605 606 607
	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);

608 609 610
			netdev_dbg(ndev, "TXI: TXB#%u: status 0x%x",
				   txb_idx, status);

611 612 613 614 615
			switch (status) {
			case TXT_TOK:
				netdev_dbg(ndev, "TXT_OK");
				can_get_echo_skb(ndev, txb_idx);
				stats->tx_packets++;
616
				break;
617
			case TXT_ERR:
618 619
				/* This indicated that retransmit limit has been
				 * reached. Obviously we should not echo the
620 621 622 623
				 * frame, but also not indicate any kind
				 * of error. If desired, it was already reported
				 * (possible multiple times) on each arbitration
				 * lost.
624
				 */
625 626
				netdev_warn(ndev, "TXB in Error state");
				can_free_echo_skb(ndev, txb_idx);
627
				stats->tx_dropped++;
628
				break;
629
			case TXT_ABT:
630 631 632 633
				/* Same as for TXT_ERR, only with different
				 * cause. We *could* re-queue the frame, but
				 * multiqueue/abort is not supported yet anyway.
				 */
634 635
				netdev_warn(ndev, "TXB in Aborted state");
				can_free_echo_skb(ndev, txb_idx);
636
				stats->tx_dropped++;
637
				break;
638 639 640 641 642
			default:
				/* Bug only if the first buffer is not finished,
				 * otherwise it is pretty much expected
				 */
				if (first) {
643 644
					netdev_err(ndev, "BUG: TXB#%u not in a finished state (0x%x)!",
						   txb_idx, status);
645 646 647 648 649 650 651 652 653
					/* 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
654

655 656 657
			/* Adjust priorities *before* marking the buffer
			 * as empty.
			 */
658 659
			ctucan_rotate_txb_prio(ndev);
			ctu_can_fd_txt_set_empty(&priv->p, txb_idx);
Martin Jeřábek's avatar
Martin Jeřábek committed
660
		}
661 662 663 664 665 666
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
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
	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
684
	struct net_device *ndev = (struct net_device *)dev_id;
Martin Jeřábek's avatar
Martin Jeřábek committed
685 686
	struct ctucan_priv *priv = netdev_priv(ndev);
	union ctu_can_fd_int_stat isr, icr;
687 688
	int irq_loops = 0;

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

691 692 693
	do {
		/* Get the interrupt status */
		isr = ctu_can_fd_int_sts(&priv->p);
Martin Jeřábek's avatar
Martin Jeřábek committed
694

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

698
		if (!isr.u32)
699
			return irq_loops ? IRQ_HANDLED : IRQ_NONE;
700

701 702 703 704 705 706
		/* 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
707

708 709 710 711 712 713
			/* 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
714

715 716 717 718 719 720 721 722 723 724 725
		/* 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) {
726 727 728
			union ctu_can_fd_int_stat ierrmask = { .s = {
				  .ewli = 1, .doi = 1, .epi = 1,
				  .ali = 1, .bei = 1 } };
729 730
			icr.u32 = isr.u32 & ierrmask.u32;

731 732
			netdev_dbg(ndev, "some ERR interrupt: clearing 0x%08x",
				   icr.u32);
733 734 735 736 737 738
			ctu_can_fd_int_clr(&priv->p, icr);
			ctucan_err_interrupt(ndev, isr);
		}
		/* Ignore RI, TI, LFI, RFI, BSI */
	} while (irq_loops++ < 10000);

739
	netdev_err(ndev, "%s: stuck interrupt, stopping\n", __func__);
740 741 742

	{
		union ctu_can_fd_int_stat imask, ival;
743

744 745 746 747 748
		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
749 750 751 752 753 754 755 756 757 758 759 760 761

	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
762
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
763
	union ctu_can_fd_int_stat ena, mask;
764

765
	netdev_dbg(ndev, "ctucan_chip_stop");
Martin Jeřábek's avatar
Martin Jeřábek committed
766 767 768 769

	ena.u32 = 0;

	/* Disable interrupts and disable can */
770 771
	ctu_can_fd_int_ena(&priv->p, ena, mask);
	ctu_can_fd_enable(&priv->p, false);
772
	clear_bit(CTUCAN_FLAG_RX_SCHED, &priv->drv_flags);
Martin Jeřábek's avatar
Martin Jeřábek committed
773 774 775 776 777 778 779 780 781 782 783 784
	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
785
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
786
	int ret;
787

788
	netdev_dbg(ndev, "ctucan_open");
Martin Jeřábek's avatar
Martin Jeřábek committed
789 790 791 792

	ret = pm_runtime_get_sync(priv->dev);
	if (ret < 0) {
		netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
793
			   __func__, ret);
Martin Jeřábek's avatar
Martin Jeřábek committed
794 795 796
		return ret;
	}

797
	ret = request_irq(ndev->irq, ctucan_interrupt, priv->irq_flags,
798
			  ndev->name, ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
799 800 801 802 803 804 805 806
	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
807 808 809
		netdev_warn(ndev, "open_candev failed!\n");
		goto err_irq;
	}
Martin Jeřábek's avatar
Martin Jeřábek committed
810

Martin Jeřábek's avatar
Martin Jeřábek committed
811
	ret = ctucan_chip_start(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
812 813 814 815 816
	if (ret < 0) {
		netdev_err(ndev, "ctucan_chip_start failed!\n");
		goto err_candev;
	}

817
	netdev_info(ndev, "ctu_can_fd device registered");
Martin Jeřábek's avatar
Martin Jeřábek committed
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
	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
842
	struct ctucan_priv *priv = netdev_priv(ndev);
843

844
	netdev_dbg(ndev, "ctucan_close");
Martin Jeřábek's avatar
Martin Jeřábek committed
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866

	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,
867
				   struct can_berr_counter *bec)
Martin Jeřábek's avatar
Martin Jeřábek committed
868
{
Martin Jeřábek's avatar
Martin Jeřábek committed
869
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
870
	int ret;
871

872
	netdev_dbg(ndev, "ctucan_get_berr_counter");
Martin Jeřábek's avatar
Martin Jeřábek committed
873 874 875 876

	ret = pm_runtime_get_sync(priv->dev);
	if (ret < 0) {
		netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
877
			   __func__, ret);
Martin Jeřábek's avatar
Martin Jeřábek committed
878 879 880
		return ret;
	}

881
	ctu_can_fd_read_err_ctrs(&priv->p, bec);
Martin Jeřábek's avatar
Martin Jeřábek committed
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896

	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
897
	struct net_device *ndev = dev_get_drvdata(dev);
Martin Jeřábek's avatar
Martin Jeřábek committed
898
	struct ctucan_priv *priv = netdev_priv(ndev);
899

900
	netdev_dbg(ndev, "ctucan_suspend");
Martin Jeřábek's avatar
Martin Jeřábek committed
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915

	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);
916

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

	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)
};

/**
934
 * ctucan_probe_common - Device type independent registration call
Martin Jeřábek's avatar
Martin Jeřábek committed
935 936 937 938
 *
 * This function does all the memory allocation and registration for the CAN
 * device.
 *
939 940 941 942 943
 * @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
944
 * @pm_enable_call: Whether pm_runtime_enable should be called
945 946
 * @set_drvdata_fnc: Function to set network driver data for physical device
 *
Martin Jeřábek's avatar
Martin Jeřábek committed
947 948
 * Return: 0 on success and failure value on error
 */
949
static int ctucan_probe_common(struct device *dev, void __iomem *addr,
950 951 952
		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
953 954
{
	struct ctucan_priv *priv;
955
	struct net_device *ndev;
Martin Jeřábek's avatar
Martin Jeřábek committed
956 957 958 959 960 961 962 963
	int ret;

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

	priv = netdev_priv(ndev);
964
	INIT_LIST_HEAD(&priv->peers_on_pdev);
965
	priv->txb_mask = ntxbufs - 1;
966
	priv->dev = dev;
Martin Jeřábek's avatar
Martin Jeřábek committed
967 968 969
	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;
970 971 972 973 974

	/* 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
975 976
	priv->can.do_get_berr_counter = ctucan_get_berr_counter;
	//priv->can.do_get_state = ctucan_get_state;
977 978 979 980 981 982 983
	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;
984
	priv->p.mem_base = addr;
Martin Jeřábek's avatar
Martin Jeřábek committed
985 986

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

990 991 992
	if (set_drvdata_fnc != NULL)
		set_drvdata_fnc(dev, ndev);
	SET_NETDEV_DEV(ndev, dev);
Martin Jeřábek's avatar
Martin Jeřábek committed
993 994 995
	ndev->netdev_ops = &ctucan_netdev_ops;

	/* Getting the CAN can_clk info */
996 997 998 999 1000 1001 1002 1003
	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
1004 1005
	}

1006 1007
	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
1008

1009 1010
	if (pm_enable_call)
		pm_runtime_enable(dev);
1011
	ret = pm_runtime_get_sync(dev);
Martin Jeřábek's avatar
Martin Jeřábek committed
1012 1013
	if (ret < 0) {
		netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
1014
			   __func__, ret);
Martin Jeřábek's avatar
Martin Jeřábek committed
1015 1016 1017
		goto err_pmdisable;
	}

1018
	if ((priv->p.read_reg(&priv->p, CTU_CAN_FD_DEVICE_ID) &
1019
			    0xFFFF) != CTU_CAN_FD_ID) {
1020 1021
		priv->p.write_reg = ctu_can_fd_write32_be;
		priv->p.read_reg = ctu_can_fd_read32_be;
1022
		if ((priv->p.read_reg(&priv->p, CTU_CAN_FD_DEVICE_ID) &
1023
			      0xFFFF) != CTU_CAN_FD_ID) {
1024
			netdev_err(ndev, "CTU_CAN_FD signature not found\n");
1025
			goto err_disableclks;
1026
		}
Martin Jeřábek's avatar
Martin Jeřábek committed
1027 1028 1029
	}

	ret = ctucan_reset(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
1030 1031
	if (ret < 0)
		goto err_pmdisable;
Martin Jeřábek's avatar
Martin Jeřábek committed
1032

1033
	priv->can.clock.freq = can_clk_rate;
Martin Jeřábek's avatar
Martin Jeřábek committed
1034 1035 1036 1037 1038

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

	ret = register_candev(ndev);
	if (ret) {
1039
		dev_err(dev, "fail to register failed (err=%d)\n", ret);
Martin Jeřábek's avatar
Martin Jeřábek committed
1040 1041 1042 1043 1044
		goto err_disableclks;
	}

	devm_can_led_init(ndev);

1045
	pm_runtime_put(dev);
Martin Jeřábek's avatar
Martin Jeřábek committed
1046 1047

	netdev_dbg(ndev, "mem_base=0x%p irq=%d clock=%d, txb mask:%d\n",
1048 1049
		   priv->p.mem_base, ndev->irq, priv->can.clock.freq,
		   priv->txb_mask);
Martin Jeřábek's avatar
Martin Jeřábek committed
1050 1051 1052 1053 1054 1055

	return 0;

err_disableclks:
	pm_runtime_put(priv->dev);
err_pmdisable:
1056 1057
	if (pm_enable_call)
		pm_runtime_disable(dev);
Martin Jeřábek's avatar
Martin Jeřábek committed
1058
err_free:
1059
	list_del_init(&priv->peers_on_pdev);
Martin Jeřábek's avatar
Martin Jeřábek committed
1060
	free_candev(ndev);