ctu_can_fd.c 25.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*******************************************************************************
 * 
 * CTU CAN FD IP Core
 * Copyright (C) 2015-2018 Ondrej Ille <ondrej.ille@gmail.com>
 * 
 * Project advisors and co-authors: 
 * 	Jiri Novak <jnovak@fel.cvut.cz>
 * 	Pavel Pisa <pisa@cmp.felk.cvut.cz>
 * 	Martin Jerabek <jerabma7@fel.cvut.cz>
 * 
 * Department of Measurement         (http://meas.fel.cvut.cz/)
 * Faculty of Electrical Engineering (http://www.fel.cvut.cz)
 * Czech Technical University        (http://www.cvut.cz/)
 * 
 * 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.
 * 
 * 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.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
*******************************************************************************/

Martin Jeřábek's avatar
Martin Jeřábek committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <linux/clk.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#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>
48

Martin Jeřábek's avatar
Martin Jeřábek committed
49 50
#include "ctu_can_fd_hw.h"
#include "ctu_can_fd_regs.h"
51

Martin Jeřábek's avatar
Martin Jeřábek committed
52
#define DRIVER_NAME	"ctucanfd"
53

Martin Jeřábek's avatar
Martin Jeřábek committed
54 55 56 57 58 59 60 61 62 63 64

/*
 * TX buffer rotation:
 * - when a buffer transitions to empty state, rotate order and priorities
 * - 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
 */

struct ctucan_priv {
65
	struct can_priv can; // must be first member!
Martin Jeřábek's avatar
Martin Jeřábek committed
66
	struct ctucanfd_priv p;
Martin Jeřábek's avatar
Martin Jeřábek committed
67 68 69 70 71 72 73 74 75 76 77 78 79

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

static int ctucan_reset(struct net_device *ndev)
{
Martin Jeřábek's avatar
Martin Jeřábek committed
80
	int i;
Martin Jeřábek's avatar
Martin Jeřábek committed
81
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
82
	netdev_info(ndev, "ctucan_reset");
Martin Jeřábek's avatar
Martin Jeřábek committed
83

84
	ctu_can_fd_reset(&priv->p);
Martin Jeřábek's avatar
Martin Jeřábek committed
85
	for (i=0; i<100; ++i) {
86
		if (ctu_can_fd_check_access(&priv->p))
Martin Jeřábek's avatar
Martin Jeřábek committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
			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
106
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
107 108
	struct can_bittiming *bt = &priv->can.bittiming;
	union ctu_can_fd_mode_command_status_settings mode;
Martin Jeřábek's avatar
Martin Jeřábek committed
109
	netdev_info(ndev, "ctucan_set_bittiming");
Martin Jeřábek's avatar
Martin Jeřábek committed
110

Martin Jeřábek's avatar
Martin Jeřábek committed
111
	mode.u32 = priv->p.read_reg(&priv->p, CTU_CAN_FD_MODE);
Martin Jeřábek's avatar
Martin Jeřábek committed
112 113 114

	if (mode.s.ena) {
		netdev_alert(ndev,
Martin Jeřábek's avatar
Martin Jeřábek committed
115
				 "BUG! Cannot set bittiming - CAN is enabled\n");
Martin Jeřábek's avatar
Martin Jeřábek committed
116 117 118
		return -EPERM;
	}

119
	ctu_can_fd_set_nom_bittiming(&priv->p, bt);
Martin Jeřábek's avatar
Martin Jeřábek committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133

	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
134
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
135 136
	struct can_bittiming *dbt = &priv->can.data_bittiming;
	union ctu_can_fd_mode_command_status_settings mode;
Martin Jeřábek's avatar
Martin Jeřábek committed
137
	netdev_info(ndev, "ctucan_set_data_bittiming");
Martin Jeřábek's avatar
Martin Jeřábek committed
138

Martin Jeřábek's avatar
Martin Jeřábek committed
139
	mode.u32 = priv->p.read_reg(&priv->p, CTU_CAN_FD_MODE);
Martin Jeřábek's avatar
Martin Jeřábek committed
140 141 142

	if (mode.s.ena) {
		netdev_alert(ndev,
Martin Jeřábek's avatar
Martin Jeřábek committed
143
				 "BUG! Cannot set bittiming - CAN is enabled\n");
Martin Jeřábek's avatar
Martin Jeřábek committed
144 145 146
		return -EPERM;
	}

147
	ctu_can_fd_set_data_bittiming(&priv->p, dbt);
Martin Jeřábek's avatar
Martin Jeřábek committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161

	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
162
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
163 164 165
	union ctu_can_fd_int_stat int_ena, int_msk;
	int err;
	struct can_ctrlmode mode;
Martin Jeřábek's avatar
Martin Jeřábek committed
166
	netdev_info(ndev, "ctucan_chip_start");
Martin Jeřábek's avatar
Martin Jeřábek committed
167 168 169 170 171 172 173 174

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

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


Martin Jeřábek's avatar
Martin Jeřábek committed
178
	err = ctucan_set_bittiming(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	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;
	/*
	int_ena.s.ei = 1;
	int_ena.s.epi = 1;
	int_ena.s.doi = 1;
	 */
	int_msk.u32 = 0xFFFFFFFF;

	mode.flags = priv->can.ctrlmode;
	mode.mask = 0xFFFFFFFF;
200
	ctu_can_fd_set_mode_reg(&priv->p, &mode);
Martin Jeřábek's avatar
Martin Jeřábek committed
201 202 203

	/* One shot mode supported indirectly via Retransmit limit */
	if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
204
		ctu_can_fd_set_ret_limit(&priv->p, true, 0);
Martin Jeřábek's avatar
Martin Jeřábek committed
205 206 207 208 209 210 211

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

212
	ctu_can_fd_int_ena(&priv->p, int_ena, int_msk);
213
	ctu_can_fd_int_mask(&priv->p, int_ena, int_msk);
Martin Jeřábek's avatar
Martin Jeřábek committed
214 215 216 217

	priv->can.state = CAN_STATE_ERROR_ACTIVE;

	/* Enable the controller */
218
	ctu_can_fd_enable(&priv->p, true);
Martin Jeřábek's avatar
Martin Jeřábek committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

	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
235 236
	int ret;
	netdev_info(ndev, "ctucan_do_set_mode");
Martin Jeřábek's avatar
Martin Jeřábek committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

	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
269
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
270 271 272 273 274 275 276 277
	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;

278

Martin Jeřábek's avatar
Martin Jeřábek committed
279
	/* Check if the TX buffer is full */
280
	if (unlikely(!CTU_CAN_FD_TXTNF(ctu_can_get_status(&priv->p)))) {
Martin Jeřábek's avatar
Martin Jeřábek committed
281 282 283 284 285 286
		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;
287
	netdev_info(ndev, "ctucan_start_xmit: using TXB#%u", txb_id);
Martin Jeřábek's avatar
Martin Jeřábek committed
288
	priv->txb_head++;
289
	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
290 291 292 293 294
	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);
295
	ctu_can_fd_txt_set_rdy(&priv->p, txb_id);
Martin Jeřábek's avatar
Martin Jeřábek committed
296 297 298 299 300 301

	if (!(cf->can_id & CAN_RTR_FLAG)) {
		stats->tx_bytes += cf->len;
	}

	/* Check if all TX buffers are full */
302
	if (!CTU_CAN_FD_TXTNF(ctu_can_get_status(&priv->p)))
Martin Jeřábek's avatar
Martin Jeřábek committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
		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
320
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
321 322 323 324 325
	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;
326
	//netdev_info(ndev, "ctucan_rx");
Martin Jeřábek's avatar
Martin Jeřábek committed
327 328


329 330 331 332 333 334
	ffw.u32 = priv->p.read_reg(&priv->p, CTU_CAN_FD_RX_DATA);
	if (ffw.s.fr_type == FD_CAN)
		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
335
	if (unlikely(!skb)) {
336 337 338 339 340
		int i;
		/* Remove the rest of the frame from the controller */
		for (i = 0; i < ffw.s.rwcnt; i++)
			priv->p.read_reg(&priv->p, CTU_CAN_FD_RX_DATA);

Martin Jeřábek's avatar
Martin Jeřábek committed
341 342 343 344
		stats->rx_dropped++;
		return 0;
	}

345
	ctu_can_fd_read_rx_frame_ffw(&priv->p, cf, &ts, ffw);
Martin Jeřábek's avatar
Martin Jeřábek committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

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

	return 1;
}

/**
 * xcan_err_interrupt - error frame Isr
 * @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.
 */
#if 0
static void xcan_err_interrupt(struct net_device *ndev, u32 isr)
{
Martin Jeřábek's avatar
Martin Jeřábek committed
366
	struct xcan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
367 368 369 370
	struct net_device_stats *stats = &ndev->stats;
	struct can_frame *cf;
	struct sk_buff *skb;
	u32 err_status, status, txerr = 0, rxerr = 0;
Martin Jeřábek's avatar
Martin Jeřábek committed
371
	netdev_info(ndev, "xcan_err_interrupt");
Martin Jeřábek's avatar
Martin Jeřábek committed
372 373 374

	skb = alloc_can_err_skb(ndev, &cf);

375 376 377 378
	err_status = priv->p.read_reg(&priv->p, XCAN_ESR_OFFSET);
	priv->p.write_reg(&priv->p, XCAN_ESR_OFFSET, err_status);
	txerr = priv->p.read_reg(&priv->p, XCAN_ECR_OFFSET) & XCAN_ECR_TEC_MASK;
	rxerr = ((priv->p.read_reg(&priv->p, XCAN_ECR_OFFSET) &
Martin Jeřábek's avatar
Martin Jeřábek committed
379
			XCAN_ECR_REC_MASK) >> XCAN_ESR_REC_SHIFT);
380
	status = priv->p.read_reg(&priv->p, XCAN_SR_OFFSET);
Martin Jeřábek's avatar
Martin Jeřábek committed
381 382 383 384 385

	if (isr & XCAN_IXR_BSOFF_MASK) {
		priv->can.state = CAN_STATE_BUS_OFF;
		priv->can.can_stats.bus_off++;
		/* Leave device in Config Mode in bus-off state */
386
		priv->p.write_reg(&priv->p, XCAN_SRR_OFFSET, XCAN_SRR_RESET_MASK);
Martin Jeřábek's avatar
Martin Jeřábek committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
		can_bus_off(ndev);
		if (skb)
			cf->can_id |= CAN_ERR_BUSOFF;
	} else if ((status & XCAN_SR_ESTAT_MASK) == XCAN_SR_ESTAT_MASK) {
		priv->can.state = CAN_STATE_ERROR_PASSIVE;
		priv->can.can_stats.error_passive++;
		if (skb) {
			cf->can_id |= CAN_ERR_CRTL;
			cf->data[1] = (rxerr > 127) ?
					CAN_ERR_CRTL_RX_PASSIVE :
					CAN_ERR_CRTL_TX_PASSIVE;
			cf->data[6] = txerr;
			cf->data[7] = rxerr;
		}
	} else if (status & XCAN_SR_ERRWRN_MASK) {
		priv->can.state = CAN_STATE_ERROR_WARNING;
		priv->can.can_stats.error_warning++;
		if (skb) {
			cf->can_id |= CAN_ERR_CRTL;
			cf->data[1] |= (txerr > rxerr) ?
					CAN_ERR_CRTL_TX_WARNING :
					CAN_ERR_CRTL_RX_WARNING;
			cf->data[6] = txerr;
			cf->data[7] = rxerr;
		}
	}

	/* Check for Arbitration lost interrupt */
	if (isr & XCAN_IXR_ARBLST_MASK) {
		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 */
	if (isr & XCAN_IXR_RXOFLW_MASK) {
		stats->rx_over_errors++;
		stats->rx_errors++;
427
		priv->p.write_reg(&priv->p, XCAN_SRR_OFFSET, XCAN_SRR_RESET_MASK);
Martin Jeřábek's avatar
Martin Jeřábek committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
		if (skb) {
			cf->can_id |= CAN_ERR_CRTL;
			cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
		}
	}

	/* Check for error interrupt */
	if (isr & XCAN_IXR_ERROR_MASK) {
		if (skb)
			cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;

		/* Check for Ack error interrupt */
		if (err_status & XCAN_ESR_ACKER_MASK) {
			stats->tx_errors++;
			if (skb) {
				cf->can_id |= CAN_ERR_ACK;
				cf->data[3] = CAN_ERR_PROT_LOC_ACK;
			}
		}

		/* Check for Bit error interrupt */
		if (err_status & XCAN_ESR_BERR_MASK) {
			stats->tx_errors++;
			if (skb) {
				cf->can_id |= CAN_ERR_PROT;
				cf->data[2] = CAN_ERR_PROT_BIT;
			}
		}

		/* Check for Stuff error interrupt */
		if (err_status & XCAN_ESR_STER_MASK) {
			stats->rx_errors++;
			if (skb) {
				cf->can_id |= CAN_ERR_PROT;
				cf->data[2] = CAN_ERR_PROT_STUFF;
			}
		}

		/* Check for Form error interrupt */
		if (err_status & XCAN_ESR_FMER_MASK) {
			stats->rx_errors++;
			if (skb) {
				cf->can_id |= CAN_ERR_PROT;
				cf->data[2] = CAN_ERR_PROT_FORM;
			}
		}

		/* Check for CRC error interrupt */
		if (err_status & XCAN_ESR_CRCER_MASK) {
			stats->rx_errors++;
			if (skb) {
				cf->can_id |= CAN_ERR_PROT;
				cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
			}
		}
			priv->can.can_stats.bus_error++;
	}

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

	netdev_dbg(ndev, "%s: error status register:0x%x\n",
493
			__func__, priv->p.read_reg(&priv->p, XCAN_ESR_OFFSET));
Martin Jeřábek's avatar
Martin Jeřábek committed
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
}
#endif

/**
 * 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
509
	struct net_device *ndev = napi->dev;
Martin Jeřábek's avatar
Martin Jeřábek committed
510 511 512
	struct ctucan_priv *priv = netdev_priv(ndev);
	int work_done = 0;
	union ctu_can_fd_int_stat isr, iec;
513
	//netdev_info(ndev, "ctucan_rx_poll");
Martin Jeřábek's avatar
Martin Jeřábek committed
514 515 516 517 518

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

	/* Get the interrupt status */
519
	isr = ctu_can_fd_int_sts(&priv->p);
Martin Jeřábek's avatar
Martin Jeřábek committed
520
	while (isr.s.rbnei && work_done < quota) {
521
		ctu_can_fd_int_clr(&priv->p, iec);
Martin Jeřábek's avatar
Martin Jeřábek committed
522 523
		ctucan_rx(ndev);
		work_done++;
524
		isr = ctu_can_fd_int_sts(&priv->p);
Martin Jeřábek's avatar
Martin Jeřábek committed
525 526 527 528 529 530 531
	}

	if (work_done)
		can_led_event(ndev, CAN_LED_EVENT_RX);

	if (work_done < quota) {
		napi_complete(napi);
532
		priv->p.write_reg(&priv->p, CTU_CAN_FD_INT_ENA_SET, iec.u32);
Martin Jeřábek's avatar
Martin Jeřábek committed
533 534 535 536 537 538 539
	}

	return work_done;
}

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

544 545
	prio = (prio << 4) | ((prio >> (nbuffersm1*4)) & 0xF);
	netdev_info(ndev, "ctucan_rotate_txb_prio: from 0x%08x to 0x%08x", priv->txb_prio, prio);
Martin Jeřábek's avatar
Martin Jeřábek committed
546
	priv->txb_prio = prio;
547
	priv->p.write_reg(&priv->p, CTU_CAN_FD_TX_PRIORITY, prio);
Martin Jeřábek's avatar
Martin Jeřábek committed
548 549 550 551 552 553 554 555 556
}

/**
 * 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
557
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
558
	struct net_device_stats *stats = &ndev->stats;
Martin Jeřábek's avatar
Martin Jeřábek committed
559
	netdev_info(ndev, "ctucan_tx_interrupt");
Martin Jeřábek's avatar
Martin Jeřábek committed
560 561 562 563 564 565 566 567 568 569 570 571

	/*
		read tx_status
		if txb[n].finished (bit 2)
			if ok -> echo
			if error / aborted -> ?? (find how to handle oneshot mode)
			txb_tail++
	*/


	while ((priv->txb_head - priv->txb_tail > 0)) {
		u32 txb_idx = priv->txb_tail & priv->txb_mask;
572
		u32 status = ctu_can_fd_get_tx_status(&priv->p, txb_idx);
Martin Jeřábek's avatar
Martin Jeřábek committed
573

574
		netdev_info(ndev, "TXI: TXB#%u: status 0x%x", txb_idx, status);
Martin Jeřábek's avatar
Martin Jeřábek committed
575 576
		switch (status) {
		case TXT_TOK:
Martin Jeřábek's avatar
Martin Jeřábek committed
577
			netdev_info(ndev, "TXT_OK");
578
			can_get_echo_skb(ndev, txb_idx);
Martin Jeřábek's avatar
Martin Jeřábek committed
579 580 581 582
			stats->tx_packets++;
		break;
		case TXT_ERR:
			netdev_warn(ndev, "TXB in Error state");
583
			can_free_echo_skb(ndev, txb_idx);
Martin Jeřábek's avatar
Martin Jeřábek committed
584 585 586 587
			// TODO: send some error frame - but what should it contain?
		break;
		case TXT_ABT:
			netdev_warn(ndev, "TXB in Aborted state");
588
			can_free_echo_skb(ndev, txb_idx);
Martin Jeřábek's avatar
Martin Jeřábek committed
589 590 591 592 593 594 595 596 597 598 599
			// TODO: send some error frame - but what should it contain?
		break;
		default:
			netdev_err(ndev, "BUG: TXB not in a finished state!");
			// what to do???
			return;
		}
		priv->txb_tail++;

		/* Adjust priorities *before* marking the buffer as empty. */
		ctucan_rotate_txb_prio(ndev);
600
		ctu_can_fd_txt_set_empty(&priv->p, txb_idx);
Martin Jeřábek's avatar
Martin Jeřábek committed
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
	}
	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
619
	struct net_device *ndev = (struct net_device *)dev_id;
Martin Jeřábek's avatar
Martin Jeřábek committed
620 621
	struct ctucan_priv *priv = netdev_priv(ndev);
	union ctu_can_fd_int_stat isr, icr;
Martin Jeřábek's avatar
Martin Jeřábek committed
622
	netdev_info(ndev, "ctucan_interrupt");
Martin Jeřábek's avatar
Martin Jeřábek committed
623 624

	/* Get the interrupt status */
625
	isr = ctu_can_fd_int_sts(&priv->p);
Martin Jeřábek's avatar
Martin Jeřábek committed
626 627 628 629 630 631 632

	if (!isr.u32)
		return IRQ_NONE;

	/* Receive Buffer Not Empty Interrupt */
	if (isr.s.rbnei) {
		netdev_info(ndev, "RXBNEI");
Martin Jeřábek's avatar
Martin Jeřábek committed
633
		icr.u32 = 0;
Martin Jeřábek's avatar
Martin Jeřábek committed
634
		icr.s.rbnei = 1;
635
		priv->p.write_reg(&priv->p, CTU_CAN_FD_INT_ENA_CLR, icr.u32);
Martin Jeřábek's avatar
Martin Jeřábek committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
		napi_schedule(&priv->napi);
	}
	#define CTUCANFD_INT_RI      BIT(0)
	#define CTUCANFD_INT_TI      BIT(1)
	#define CTUCANFD_INT_EI      BIT(2)
	#define CTUCANFD_INT_DOI     BIT(3)
	#define CTUCANFD_INT_EPI     BIT(4)
	#define CTUCANFD_INT_ALI     BIT(5)
	#define CTUCANFD_INT_BEI     BIT(6)
	#define CTUCANFD_INT_LFI     BIT(7)
	#define CTUCANFD_INT_RFI     BIT(8)
	#define CTUCANFD_INT_BSI     BIT(9)
	#define CTUCANFD_INT_RBNEI   BIT(10)
	#define CTUCANFD_INT_TXBHCI  BIT(11)
	#define CTUCANFD_INT_ERROR (CTUCANFD_INT_EI | CTUCANFD_INT_DOI | CTUCANFD_INT_EPI | CTUCANFD_INT_ALI | CTUCANFD_INT_BEI)

	/* TX Buffer HW Command Interrupt */
	if (isr.s.txbhci) {
Martin Jeřábek's avatar
Martin Jeřábek committed
654 655
		netdev_info(ndev, "TXBHCI");
		icr.u32 = 0;
Martin Jeřábek's avatar
Martin Jeřábek committed
656
		icr.s.txbhci = 1;
657
		ctu_can_fd_int_clr(&priv->p, icr);
Martin Jeřábek's avatar
Martin Jeřábek committed
658 659 660 661 662 663
		ctucan_tx_interrupt(ndev);
	}

	/* Error interrupts */
	if (isr.s.ei || isr.s.doi || isr.s.epi || isr.s.ali) {
		icr.u32 = isr.u32 & CTUCANFD_INT_ERROR;
664
		ctu_can_fd_int_clr(&priv->p, icr);
Martin Jeřábek's avatar
Martin Jeřábek committed
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
		netdev_warn(ndev, "err interrupt handler not implemented!");
		//ctucan_err_interrupt(ndev, isr);
	}

	/* Ignore RI, TI, LFI, RFI, BSI */

	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
683
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
684
	union ctu_can_fd_int_stat ena, mask;
Martin Jeřábek's avatar
Martin Jeřábek committed
685
	netdev_info(ndev, "ctucan_chip_stop");
Martin Jeřábek's avatar
Martin Jeřábek committed
686 687 688 689 690

	ena.u32 = 0;
	mask.u32 = 0xFFFFFFFF;

	/* Disable interrupts and disable can */
691
	ctu_can_fd_int_ena(&priv->p, ena, mask);
692
	ctu_can_fd_int_mask(&priv->p, ena, mask);
693
	ctu_can_fd_enable(&priv->p, false);
Martin Jeřábek's avatar
Martin Jeřábek committed
694 695 696 697 698 699 700 701 702 703 704 705
	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
706
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
707
	int ret;
Martin Jeřábek's avatar
Martin Jeřábek committed
708
	netdev_info(ndev, "ctucan_open");
Martin Jeřábek's avatar
Martin Jeřábek committed
709 710 711 712 713 714 715 716 717 718 719 720

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

	/*
	ret = ctucan_reset(ndev);
	if (ret < 0)
		goto err;
Martin Jeřábek's avatar
Martin Jeřábek committed
721
	*/
Martin Jeřábek's avatar
Martin Jeřábek committed
722 723 724 725 726 727 728 729 730 731 732

	ret = request_irq(ndev->irq, ctucan_interrupt, /*priv->irq_flags*/0,
			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
733 734 735
		netdev_warn(ndev, "open_candev failed!\n");
		goto err_irq;
	}
Martin Jeřábek's avatar
Martin Jeřábek committed
736

Martin Jeřábek's avatar
Martin Jeřábek committed
737
	ret = ctucan_chip_start(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
	if (ret < 0) {
		netdev_err(ndev, "ctucan_chip_start failed!\n");
		goto err_candev;
	}

	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
767 768
	struct ctucan_priv *priv = netdev_priv(ndev);
	netdev_info(ndev, "ctucan_close");
Martin Jeřábek's avatar
Martin Jeřábek committed
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792

	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
793
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
794
	int ret;
Martin Jeřábek's avatar
Martin Jeřábek committed
795
	netdev_info(ndev, "ctucan_get_berr_counter");
Martin Jeřábek's avatar
Martin Jeřábek committed
796 797 798 799 800 801 802 803

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

804
	ctu_can_fd_read_err_ctrs(&priv->p, bec);
Martin Jeřábek's avatar
Martin Jeřábek committed
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819

	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
820
	struct net_device *ndev = dev_get_drvdata(dev);
Martin Jeřábek's avatar
Martin Jeřábek committed
821
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
822
	netdev_info(ndev, "ctucan_suspend");
Martin Jeřábek's avatar
Martin Jeřábek committed
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837

	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);
Martin Jeřábek's avatar
Martin Jeřábek committed
838
	netdev_info(ndev, "ctucan_resume");
Martin Jeřábek's avatar
Martin Jeřábek committed
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886

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


/**
 * ctucan_probe - Platform registration call
 * @pdev:	Handle to the platform device structure
 *
 * This function does all the memory allocation and registration for the CAN
 * device.
 *
 * Return: 0 on success and failure value on error
 */
static int ctucan_probe(struct platform_device *pdev)
{
	struct resource *res; /* IO mem resources */
	struct net_device *ndev;
	struct ctucan_priv *priv;
	void __iomem *addr;
	int ret;
	unsigned int ntxbufs;

	/* Get the virtual base address for the device */
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	addr = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(addr)) {
		ret = PTR_ERR(addr);
		goto err;
	}

	/*
	ret = of_property_read_u32(pdev->dev.of_node, "tx-fifo-depth", &tx_max);
	if (ret < 0)
		goto err;
	*/
887
	ntxbufs = 4;
Martin Jeřábek's avatar
Martin Jeřábek committed
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904

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

	priv = netdev_priv(ndev);
	priv->txb_mask = ntxbufs-1;
	priv->dev = &pdev->dev;
	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;
	//priv->can.do_set_bittiming = ctucan_set_bittiming;
	//priv->can.do_set_data_bittiming = ctucan_set_data_bittiming;
	priv->can.do_get_berr_counter = ctucan_get_berr_counter;
	//priv->can.do_get_state = ctucan_get_state;
	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*/;
905
	priv->p.mem_base = addr;
Martin Jeřábek's avatar
Martin Jeřábek committed
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922

	/* Get IRQ for the device */
	ndev->irq = platform_get_irq(pdev, 0);
	ndev->flags |= IFF_ECHO;	/* We support local echo */

	platform_set_drvdata(pdev, ndev);
	SET_NETDEV_DEV(ndev, &pdev->dev);
	ndev->netdev_ops = &ctucan_netdev_ops;

	/* Getting the CAN can_clk info */
	priv->can_clk = devm_clk_get(&pdev->dev, NULL);
	if (IS_ERR(priv->can_clk)) {
		dev_err(&pdev->dev, "Device clock not found.\n");
		ret = PTR_ERR(priv->can_clk);
		goto err_free;
	}

923 924
	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
925 926 927 928 929 930 931 932 933

	pm_runtime_enable(&pdev->dev);
	ret = pm_runtime_get_sync(&pdev->dev);
	if (ret < 0) {
		netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
			__func__, ret);
		goto err_pmdisable;
	}

934 935 936
	if ((priv->p.read_reg(&priv->p, CTU_CAN_FD_DEVICE_ID) & 0xFFFF) != CTU_CAN_FD_ID) {
		priv->p.write_reg = ctu_can_fd_write32_be;
		priv->p.read_reg = ctu_can_fd_read32_be;
Martin Jeřábek's avatar
Martin Jeřábek committed
937 938 939
	}

	ret = ctucan_reset(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
940 941
	if (ret < 0)
		goto err_pmdisable;
Martin Jeřábek's avatar
Martin Jeřábek committed
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957

	priv->can.clock.freq = clk_get_rate(priv->can_clk);

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

	ret = register_candev(ndev);
	if (ret) {
		dev_err(&pdev->dev, "fail to register failed (err=%d)\n", ret);
		goto err_disableclks;
	}

	devm_can_led_init(ndev);

	pm_runtime_put(&pdev->dev);

	netdev_dbg(ndev, "mem_base=0x%p irq=%d clock=%d, txb mask:%d\n",
958
			priv->p.mem_base, ndev->irq, priv->can.clock.freq,
Martin Jeřábek's avatar
Martin Jeřábek committed
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
			priv->txb_mask);

	return 0;

err_disableclks:
	pm_runtime_put(priv->dev);
err_pmdisable:
	pm_runtime_disable(&pdev->dev);
err_free:
	free_candev(ndev);
err:
	return ret;
}

/**
 * ctucan_remove - Unregister the device after releasing the resources
 * @pdev:	Handle to the platform device structure
 *
 * This function frees all the resources allocated to the device.
 * Return: 0 always
 */
static int ctucan_remove(struct platform_device *pdev)
{
	struct net_device *ndev = platform_get_drvdata(pdev);
	struct ctucan_priv *priv = netdev_priv(ndev);
Martin Jeřábek's avatar
Martin Jeřábek committed
984
	netdev_info(ndev, "ctucan_remove");
Martin Jeřábek's avatar
Martin Jeřábek committed
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003

	unregister_candev(ndev);
	pm_runtime_disable(&pdev->dev);
	netif_napi_del(&priv->napi);
	free_candev(ndev);

	return 0;
}


/* Match table for OF platform binding */
static const struct of_device_id ctucan_of_match[] = {
	{ .compatible = "ctu,canfd-1.0", },
	{ .compatible = "ctu,ctucanfd", },
	{ /* end of list */ },
};
MODULE_DEVICE_TABLE(of, ctucan_of_match);

static struct platform_driver ctucanfd_driver = {
Martin Jeřábek's avatar
Martin Jeřábek committed
1004 1005 1006 1007 1008 1009 1010
	.probe	= ctucan_probe,
	.remove	= ctucan_remove,
	.driver	= {
		.name = DRIVER_NAME,
		.pm = &ctucan_dev_pm_ops,
		.of_match_table	= ctucan_of_match,
	},
Martin Jeřábek's avatar
Martin Jeřábek committed
1011 1012 1013 1014 1015 1016 1017
};

module_platform_driver(ctucanfd_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Martin Jerabek");
MODULE_DESCRIPTION("CTU CAN FD interface");