ctu_can_fd_hw.h 24.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 36 37 38

#ifndef __CTU_CAN_FD_HW__
#define __CTU_CAN_FD_HW__

Martin Jeřábek's avatar
Martin Jeřábek committed
39 40 41 42 43 44
#include <asm/byteorder.h>

#if defined(__LITTLE_ENDIAN_BITFIELD) == defined(__BIG_ENDIAN_BITFIELD)
# error Either __BIG_ENDIAN_BITFIELD or __LITTLE_ENDIAN_BITFIELD must be defined.
#endif

45
#include "ctu_can_fd_regs.h"
Martin Jeřábek's avatar
Martin Jeřábek committed
46
#include "ctu_can_fd_frame.h"
47 48

/*
49
	MJ TODO:
50 51
	+ move small functions to *.h, make them inline
	+ either pass union arguments by value or just as u32;
52 53 54 55
	  this way they are forced on stack instead of passing in register
	  + one level of pointer indirection, which sucks performance-wise
	  - use u32 directly, as non-primitive types (however small )are not
		guaranteed to be passed in registers across all ABIs
56
*/
57 58 59

#define CTU_CAN_FD_RETR_MAX 15

Martin Jeřábek's avatar
Martin Jeřábek committed
60 61 62
#define CTU_CAN_FD_FILTER_A 0
#define CTU_CAN_FD_FILTER_B 1
#define CTU_CAN_FD_FILTER_C 2
63 64 65

#define CTU_CAN_FD_TXT_BUFFER_COUNT 4

Martin Jeřábek's avatar
Martin Jeřábek committed
66 67 68 69
#define CTU_CAN_FD_TXT_BUFFER_1 0
#define CTU_CAN_FD_TXT_BUFFER_2 1
#define CTU_CAN_FD_TXT_BUFFER_3 2
#define CTU_CAN_FD_TXT_BUFFER_4 3
70

Martin Jeřábek's avatar
Martin Jeřábek committed
71
/*
72 73 74 75
 * Status macros -> pass "ctu_can_get_status" result
 */

// True if Core is transceiver of current frame
76
#define CTU_CAN_FD_IS_TRANSMITTER(stat) (!!(stat).ts)
77 78

// True if Core is receiver of current frame
Martin Jeřábek's avatar
Martin Jeřábek committed
79
#define CTU_CAN_FD_IS_RECEIVER(stat) (!!(stat).s.rxs)
80 81

// True if Core is idle (integrating or interfame space)
Martin Jeřábek's avatar
Martin Jeřábek committed
82
#define CTU_CAN_FD_IS_IDLE(stat) (!!(stat).s.idle)
83 84

// True if Core is transmitting error frame
Martin Jeřábek's avatar
Martin Jeřábek committed
85
#define CTU_CAN_FD_ERR_FRAME(stat) (!!(stat).s.eft)
86 87

// True if Error warning limit was reached
88
#define CTU_CAN_FD_EWL(stat) (!!(stat).s.ewl)
89 90

// True if at least one TXT Buffer is empty
Martin Jeřábek's avatar
Martin Jeřábek committed
91
#define CTU_CAN_FD_TXTNF(stat) (!!(stat).s.txnf)
92 93

// True if data overrun flag of RX Buffer occurred
Martin Jeřábek's avatar
Martin Jeřábek committed
94
#define CTU_CAN_FD_DATA_OVERRUN(stat) (!!(stat).s.dor)
95 96

// True if RX Buffer is not empty
Martin Jeřábek's avatar
Martin Jeřábek committed
97
#define CTU_CAN_FD_RX_BUF_NEMPTY(stat) (!!(stat).s.rxne)
98 99


Martin Jeřábek's avatar
Martin Jeřábek committed
100
/*
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
101 102 103 104
 * Interrupt macros -> pass "ctu_can_fd_int_sts" result
 */

// Frame reveived interrupt
Martin Jeřábek's avatar
Martin Jeřábek committed
105
#define CTU_CAN_FD_RX_INT(int_stat) (!!(int_stat).s.rxi)
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
106 107

// Frame transceived interrupt
Martin Jeřábek's avatar
Martin Jeřábek committed
108
#define CTU_CAN_FD_TX_INT(int_stat) (!!(int_stat).s.txi)
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
109 110

// Error warning limit reached interrupt
Martin Jeřábek's avatar
Martin Jeřábek committed
111
#define CTU_CAN_FD_EWL_INT(int_stat) (!!(int_stat).s.ewli)
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
112 113

// RX Buffer data overrun interrupt
114
#define CTU_CAN_FD_OVERRUN_INT(int_stat) (!!(int_stat).s.doi)
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
115 116

// Core turned error passive interrupt
117
#define CTU_CAN_FD_ERR_PASSIVE_INT(int_stat) (!!(int_stat).s.epi)
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
118 119

// Error frame transmission started interrupt
120
#define CTU_CAN_FD_BUS_ERROR_INT(int_stat) (!!(int_stat).s.bei)
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
121 122

// Event logger finished interrupt
123
#define CTU_CAN_FD_LOGGER_FIN_INT(int_stat) (!!(int_stat).s.lfi)
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
124 125

// RX Buffer full interrupt
Martin Jeřábek's avatar
Martin Jeřábek committed
126
#define CTU_CAN_FD_RX_FULL_INT(int_stat) (!!(int_stat).s.rxfi)
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
127 128

// Bit-rate shifted interrupt
129
#define CTU_CAN_FD_BIT_RATE_SHIFT_INT(int_stat) (!!(int_stat).s.bsi)
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
130 131

// Receive buffer not empty interrupt
132
#define CTU_CAN_FD_RX_BUF_NEPMTY_INT(int_stat) (!!(int_stat).s.rbnei)
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
133 134

// TX Buffer received HW command interrupt
135
#define CTU_CAN_FD_TXT_BUF_HWCMD_INT(int_stat) (!!(int_stat).s.txbhci)
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
136

137 138
static inline bool CTU_CAN_FD_INT_ERROR(union ctu_can_fd_int_stat i)
{
Martin Jeřábek's avatar
Martin Jeřábek committed
139
	return i.s.ewli || i.s.doi || i.s.epi || i.s.ali;
Martin Jeřábek's avatar
Martin Jeřábek committed
140 141 142 143
}

struct ctucanfd_priv;
#ifndef ctucanfd_priv
144
struct ctucanfd_priv {
Martin Jeřábek's avatar
Martin Jeřábek committed
145
	void __iomem *mem_base;
146 147 148 149
	u32 (*read_reg)(struct ctucanfd_priv *priv,
			enum ctu_can_fd_can_registers reg);
	void (*write_reg)(struct ctucanfd_priv *priv,
			enum ctu_can_fd_can_registers reg, u32 val);
150
};
Martin Jeřábek's avatar
Martin Jeřábek committed
151
#endif
152

153 154 155 156 157 158 159 160
void ctu_can_fd_write32(struct ctucanfd_priv *priv,
			enum ctu_can_fd_can_registers reg, u32 val);
void ctu_can_fd_write32_be(struct ctucanfd_priv *priv,
			enum ctu_can_fd_can_registers reg, u32 val);
u32 ctu_can_fd_read32(struct ctucanfd_priv *priv,
			enum ctu_can_fd_can_registers reg);
u32 ctu_can_fd_read32_be(struct ctucanfd_priv *priv,
			enum ctu_can_fd_can_registers reg);
161

162 163 164

/*
 * Checks whether the core is mapped correctly at it's base address.
Martin Jeřábek's avatar
Martin Jeřábek committed
165
 *
166
 * Arguments:
167
 *	priv	Private info
168 169 170 171
 *
 * Returns:
 *	true if the core is accessible correctly, false otherwise.
 */
172
bool ctu_can_fd_check_access(struct ctucanfd_priv *priv);
173 174 175 176


/*
 * Returns version of CTU CAN FD IP Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
177
 *
178
 * Arguments:
179
 *	priv	Private info
180 181 182 183
 *
 * Returns:
 *	IP Core version in format major*10 + minor
 */
184
u32 ctu_can_fd_get_version(struct ctucanfd_priv *priv);
185 186 187 188 189


/*
 * Enables/disables the operation of CTU CAN FD Core. If disabled, the Core will
 * never start transmitting on the CAN bus, nor receiving.
Martin Jeřábek's avatar
Martin Jeřábek committed
190
 *
191
 * Arguments:
192
 *	priv	Private info
193 194
 *	enable	Enable/disable the core.
 */
195
void ctu_can_fd_enable(struct ctucanfd_priv *priv, bool enable);
196 197


Martin Jeřábek's avatar
Martin Jeřábek committed
198 199 200 201 202 203 204 205 206 207 208
/*
 * Resets the CTU CAN FD Core.
 * NOTE: After resetting, you must wait until ctu_can_fd_check_access()
 *       succeeds!
 *
 * Arguments:
 *	priv	Private info
 */
void ctu_can_fd_reset(struct ctucanfd_priv *priv);


209 210
/*
 * Configures CTU CAN FD Core to limit the amount of retransmit attempts after
211 212
 * occurence of error (Error frame, Arbitration lost). If retransmit limit is
 * disabled, the Core will attempt to retransmit inifinitely. If retransmit
213 214
 * limit is reached, the Core will finish and according TXT buffer will end up
 * in TX Error state.
Martin Jeřábek's avatar
Martin Jeřábek committed
215
 *
216
 * Arguments:
217
 *	priv	Private info
218
 *	enable	Enable/disable the retransmit limitation
Martin Jeřábek's avatar
Martin Jeřábek committed
219
 *      limit	Number to which limit the retransmission (1-CTU_CAN_FD_RETR_MAX)
220 221 222
 * Returns:
 *	True if set correctly. False if "limit" is too high.
 */
223 224
bool ctu_can_fd_set_ret_limit(struct ctucanfd_priv *priv, bool enable,
			      u8 limit);
225 226 227 228 229 230 231


/*
 * Configures CTU CAN FD Core for special operating modes by access to MODE
 * register. Following flags from "mode" are not configured by this function:
 *  CAN_CTRLMODE_ONE_SHOT, CAN_CTRLMODE_BERR_REPORTING.
 * Following flags are configured:
232 233
 *	CAN_CTRLMODE_LOOPBACK	- Bit loopback mode. Every dominant bit is
 *				  re-routed internally and not send on the bus.
234 235 236 237 238
 *	CAN_CTRLMODE_LISTENONLY	- No frame is transmitted, no dominant bit is
 *				  sent on the bus.
 *	CAN_CTRLMODE_3_SAMPLES  - Tripple sampling mode
 *	CAN_CTRLMODE_FD		- Flexible data-rate support. When not set, Core
 *				  does not accept CAN FD Frames and interprets,
239
 *				  them as form error. Capability to transmit
240 241 242 243
 *				  CAN FD Frames is not affected by this setting.
 *	CAN_CTRLMODE_PRESUME_ACK - When set, Core does not require dominant bit
 *				   in ACK field to consider the transmission as
 *				   valid.
244
 *	CAN_CTRLMODE_FD_NON_ISO  - When set, the Core transmits the frames
245
 *				   according to NON-ISO FD standard.
Martin Jeřábek's avatar
Martin Jeřábek committed
246
 *
247
 * Arguments:
248
 *	priv	Private info
Martin Jeřábek's avatar
Martin Jeřábek committed
249
 *	mode	CAN mode to be set to on the Core.
250
 */
251 252
void ctu_can_fd_set_mode_reg(struct ctucanfd_priv *priv,
			     const struct can_ctrlmode *mode);
253 254 255 256 257


/*
 * Gives command to CTU CAN FD Core to erase and reset the RX FIFO. This
 * action is finished immediately and does not need waiting.
Martin Jeřábek's avatar
Martin Jeřábek committed
258
 *
259
 * Arguments:
260
 *	priv	Private info
261
 */
262
void ctu_can_fd_rel_rx_buf(struct ctucanfd_priv *priv);
263 264 265


/*
Martin Jeřábek's avatar
Martin Jeřábek committed
266
 * Gives command to CTU CAN FD Core to clear the Data overrun flag on
267
 * the RX FIFO Buffer.
Martin Jeřábek's avatar
Martin Jeřábek committed
268
 *
269
 * Arguments:
270
 *	priv	Private info
271
 */
272
void ctu_can_fd_clr_overrun_flag(struct ctucanfd_priv *priv);
273 274 275


/*
276
 * Gives command to CTU CAN FD Core to abort the transmission immediately.
277
 * This action will most likely result in transmission of Error frame.
Martin Jeřábek's avatar
Martin Jeřábek committed
278
 *
279
 * Arguments:
280
 *	priv	Private info
281
 */
282
void ctu_can_fd_abort_tx(struct ctucanfd_priv *priv);
283 284 285 286


/*
 * Returns mode/status vector of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
287
 *
288
 * Arguments:
289
 *	priv	Private info
Martin Jeřábek's avatar
Martin Jeřábek committed
290

291 292 293
 * Returns:
 *	Mode/status structure with multiple mode flags.
 */
294 295
static inline union ctu_can_fd_mode_command_status_settings
	ctu_can_get_status(struct ctucanfd_priv *priv)
Martin Jeřábek's avatar
Martin Jeřábek committed
296
{
297
	/* MODE and STATUS are within the same word */
298
	union ctu_can_fd_mode_command_status_settings res;
299

300
	res.u32 = priv->read_reg(priv, CTU_CAN_FD_MODE);
301
	return res;
Martin Jeřábek's avatar
Martin Jeřábek committed
302 303
}

304

305 306 307 308 309 310 311 312 313 314 315 316
/*
 * Test if core is enabled..
 *
 * Arguments:
 *	priv	Private info

 * Returns:
 *	Return true if core is in enabled/active state..
 */
static inline bool ctu_can_fd_is_enabled(struct ctucanfd_priv *priv)
{
	union ctu_can_fd_mode_command_status_settings reg;
317

318
	reg.u32 = priv->read_reg(priv, CTU_CAN_FD_MODE);
319
	return reg.s.ena == CTU_CAN_ENABLED;
320 321 322
}


323 324
/*
 * Reads the interrupt status vector from CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
325
 *
326
 * Arguments:
327
 *	priv	Private info
Martin Jeřábek's avatar
Martin Jeřábek committed
328

329 330 331
 * Returns:
 *	Interrupt status vector.
 */
332 333
static inline union ctu_can_fd_int_stat
	ctu_can_fd_int_sts(struct ctucanfd_priv *priv)
Martin Jeřábek's avatar
Martin Jeřábek committed
334
{
335
	union ctu_can_fd_int_stat res;
336

337
	res.u32 = priv->read_reg(priv, CTU_CAN_FD_INT_STAT);
338
	return res;
Martin Jeřábek's avatar
Martin Jeřábek committed
339
}
340 341 342


/*
343
 * Clears the interrupts from CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
344
 *
345
 * Arguments:
346
 *	priv	Private info
347 348
 *	mask	Mask of interrupts which should be cleared.
 */
349 350
static inline void ctu_can_fd_int_clr(struct ctucanfd_priv *priv,
				      union ctu_can_fd_int_stat mask)
Martin Jeřábek's avatar
Martin Jeřábek committed
351
{
352 353 354 355 356 357 358 359 360 361 362
	priv->write_reg(priv, CTU_CAN_FD_INT_STAT, mask.u32);
}


/*
 * Sets enable interrupt bits.
 *
 * Arguments:
 *	priv	Private info
 *	mask	Mask of interrupts which should be disabled.
 */
363 364
static inline void ctu_can_fd_int_ena_set(struct ctucanfd_priv *priv,
					  union ctu_can_fd_int_stat mask)
365 366 367 368 369 370 371 372 373 374 375 376
{
	priv->write_reg(priv, CTU_CAN_FD_INT_ENA_SET, mask.u32);
}


/*
 * Clears enable interrupt bits.
 *
 * Arguments:
 *	priv	Private info
 *	mask	Mask of interrupts which should be disabled.
 */
377 378
static inline void ctu_can_fd_int_ena_clr(struct ctucanfd_priv *priv,
					  union ctu_can_fd_int_stat mask)
379 380
{
	priv->write_reg(priv, CTU_CAN_FD_INT_ENA_CLR, mask.u32);
Martin Jeřábek's avatar
Martin Jeřábek committed
381
}
382 383 384 385


/*
 * Enable/Disable interrupts of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
386
 *
387
 * Arguments:
388
 *	priv	Private info
389
 *	mask	Mask of interrupts which should be enabled/disabled.
390
 *	val	0 - disable, 1 - enable the interrupt.
391
 */
392 393
void ctu_can_fd_int_ena(struct ctucanfd_priv *priv,
			union ctu_can_fd_int_stat mask,
394
			union ctu_can_fd_int_stat val);
395 396 397 398


/*
 * Mask/Unmask interrupts of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
399
 *
400
 * Arguments:
401
 *	priv	Private info
402
 *	mask	Mask of interrupts which should be enabled/disabled.
403
 *	val	0 - unmask, 1 - mask the interrupt.
404
 */
405 406 407
void ctu_can_fd_int_mask(struct ctucanfd_priv *priv,
			 union ctu_can_fd_int_stat mask,
			 union ctu_can_fd_int_stat val);
408 409 410 411 412


/*
 * Set the modes of CTU CAN FD IP Core. All flags from "ctu_can_fd_set_mode_reg"
 * are configured, plus CAN_CTRLMODE_ONE_SHOT, CAN_CTRLMODE_BERR_REPORTING,
413
 * which are configured via "retransmit limit" and enabling error interrupts.
Martin Jeřábek's avatar
Martin Jeřábek committed
414
 *
415
 * Arguments:
416
 *	priv	Private info
417 418
 *	mode	Mode of the controller from Socket CAN.
 */
419 420
void ctu_can_fd_set_mode(struct ctucanfd_priv *priv,
			 const struct can_ctrlmode *mode);
421 422 423 424


/*
 * Set Nominal bit timing of CTU CAN FD Core.
425
 * NOTE: phase_seg1 and prop_seg may be modified if phase_seg1 > 63
426 427
 *       This is because in Linux, the constraints are only
 *       on phase_seg1+prop_seg.
428
 *
429
 * Arguments:
430
 *	priv	Private info
431 432
 *	nbt	Nominal bit timing settings of CAN Controller.
 */
433 434
void ctu_can_fd_set_nom_bittiming(struct ctucanfd_priv *priv,
				  struct can_bittiming *nbt);
435 436 437 438


/*
 * Set Data bit timing of CTU CAN FD Core.
439 440 441
 * NOTE: phase_seg1 and prop_seg may be modified if phase_seg1 > 63
 *       This is because in Linux, the constraints are only on phase_seg1+prop_seg.
 *
442
 * Arguments:
443
 *	priv	Private info
444 445
 *	nbt	Data bit timing settings of CAN Controller.
 */
446 447
void ctu_can_fd_set_data_bittiming(struct ctucanfd_priv *priv,
				   struct can_bittiming *dbt);
448 449 450 451 452 453 454


/*
 * Set error limit when CTU CAN FD Core should transfer to Error warning
 * and error passive states. If any of RX/TX counters reach this value
 * according state is changed. By default these counters are set as in
 * CAN Standard (96, 128).
Martin Jeřábek's avatar
Martin Jeřábek committed
455
 *
456
 * Arguments:
457
 *	priv	Private info
458 459 460
 *	ewl	Error warning limit
 *	erp	Error passive limit
 */
461
void ctu_can_fd_set_err_limits(struct ctucanfd_priv *priv, u8 ewl, u8 erp);
462 463 464 465


/*
 * Set default error limits to the CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
466
 *
467
 * Arguments:
468
 *	priv	Private info
469
 */
470
static inline void ctu_can_fd_set_def_err_limits(struct ctucanfd_priv *priv)
471
{
472
	ctu_can_fd_set_err_limits(priv, 96, 128);
473
}
474 475 476 477


/*
 * Read TX/RX error counters of CTU CAN FD IP Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
478
 *
479
 * Arguments:
480
 *	priv	Private info
481 482
 *	ctr	Pointer to error counter structure to fill
 */
483 484
void ctu_can_fd_read_err_ctrs(struct ctucanfd_priv *priv,
			      struct can_berr_counter *ctr);
485 486 487 488 489


/*
 * Read special error counter which returns number of Errors which were
 * detected during Nominal Bit-rate.
Martin Jeřábek's avatar
Martin Jeřábek committed
490
 *
491
 * Arguments:
492
 *	priv	Private info
493 494 495
 * Returns:
 *	Number of Error frames detected during Nominal Bit-rate
 */
496
static inline u16 ctu_can_fd_read_nom_errs(struct ctucanfd_priv *priv)
497 498
{
	union ctu_can_fd_err_norm_err_fd reg;
499

500
	reg.u32 = priv->read_reg(priv, CTU_CAN_FD_ERR_NORM);
501 502 503
	return reg.s.err_norm_val;
}

504 505 506 507


/*
 * Give command to CTU CAN FD Core to erase the nominal error counter.
Martin Jeřábek's avatar
Martin Jeřábek committed
508
 *
509
 * Arguments:
510
 *	priv	Private info
511
 */
512
static inline void ctu_can_fd_erase_nom_errs(struct ctucanfd_priv *priv)
Martin Jeřábek's avatar
Martin Jeřábek committed
513 514
{
	union ctu_can_fd_ctr_pres reg;
515

Martin Jeřábek's avatar
Martin Jeřábek committed
516 517
	reg.u32 = 0;
	reg.s.enorm = 1;
518
	priv->write_reg(priv, CTU_CAN_FD_CTR_PRES, reg.u32);
Martin Jeřábek's avatar
Martin Jeřábek committed
519
}
520 521 522 523 524 525 526


/*
 * Read special error counter which returns number of Errors which were
 * detected during Data Bit-rate.
 *
 * Arguments:
527
 *	priv	Private info
528 529 530
 * Returns:
 *	Number of Error frames detected during Data Bit-rate
 */
531
static inline u16 ctu_can_fd_read_fd_errs(struct ctucanfd_priv *priv)
532 533
{
	union ctu_can_fd_err_norm_err_fd reg;
534

535
	reg.u32 = priv->read_reg(priv, CTU_CAN_FD_ERR_NORM);
536 537
	return reg.s.err_fd_val;
}
538 539 540 541


/*
 * Give command to CTU CAN FD Core to erase the Data error counter.
Martin Jeřábek's avatar
Martin Jeřábek committed
542
 *
543
 * Arguments:
544
 *	priv	Private info
545
 */
546
static inline void ctu_can_fd_erase_fd_errs(struct ctucanfd_priv *priv)
Martin Jeřábek's avatar
Martin Jeřábek committed
547 548
{
	union ctu_can_fd_ctr_pres reg;
549

Martin Jeřábek's avatar
Martin Jeřábek committed
550 551
	reg.u32 = 0;
	reg.s.efd = 1;
552
	priv->write_reg(priv, CTU_CAN_FD_CTR_PRES, reg.u32);
Martin Jeřábek's avatar
Martin Jeřábek committed
553
}
554 555 556


/*
Martin Jeřábek's avatar
Martin Jeřábek committed
557
 * Read fault confinement state of CTU CAN FD Core
558
 * (determined by TX/RX Counters).
Martin Jeřábek's avatar
Martin Jeřábek committed
559
 *
560
 * Arguments:
561
 *	priv	Private info
562 563 564
 * Returns:
 *	Error state of the CTU CAN FD Core.
 */
565
enum can_state ctu_can_fd_read_error_state(struct ctucanfd_priv *priv);
566 567 568 569


/*
 * Set value to TX/RX error counters of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
570
 *
571
 * Arguments:
572
 *	priv	Private info
573 574 575
 * Returns:
 *	Error state of the CTU CAN FD Core.
 */
576 577
void ctu_can_fd_set_err_ctrs(struct ctucanfd_priv *priv,
			     const struct can_berr_counter *ctr);
578 579 580 581


/*
 * Check Mask filters support of given filter.
Martin Jeřábek's avatar
Martin Jeřábek committed
582
 *
583
 * Arguments:
584
 *	priv	Private info
585
 *	fnum	Filter number.
586 587 588
 * Returns:
 *	True if filter is present and can be used, False otherwise.
 */
589
bool ctu_can_fd_get_mask_filter_support(struct ctucanfd_priv *priv, u8 fnum);
590 591 592 593


/*
 * Check Range filter support of given filter.
Martin Jeřábek's avatar
Martin Jeřábek committed
594
 *
595
 * Arguments:
596
 *	priv	Private info
597 598 599
 * Returns:
 *	True if Range filter is present and can be used, False otherwise.
 */
600
bool ctu_can_fd_get_range_filter_support(struct ctucanfd_priv *priv);
601 602 603 604


/*
 * Configure mask filter of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
605
 *
606
 * Arguments:
607
 *	priv	Private info
608 609 610 611 612 613
 *	fnum	Filter number.
 *	enable  True if filter should be enabled.
 *	filter	Filter configuration.
 * Returns:
 *	True if mask filter was configured properly, false otherwise.
 */
614 615
bool ctu_can_fd_set_mask_filter(struct ctucanfd_priv *priv, u8 fnum,
				bool enable, const struct can_filter *filter);
616 617 618 619 620

/*
 * Configure range filter of CTU CAN FD Core. An identifier of RX Frame
 * will pass the Range filter if its decimal value is between lower and
 * upper threshold of range filter.
Martin Jeřábek's avatar
Martin Jeřábek committed
621
 *
622
 * Arguments:
623
 *	priv	Private info
624 625 626 627
 *	low_th	Lower threshold of identifiers which should be accepted
 *	high_th	Upper threshold of identifiers which should be accepted
 *	enable	Enable the range filter.
 */
628
void ctu_can_fd_set_range_filter(struct ctucanfd_priv *priv, canid_t low_th,
629 630 631 632
				 canid_t high_th, bool enable);

/*
 * Get size of the RX FIFO Buffer of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
633
 *
634
 * Arguments:
635
 *	priv	Private info
636 637 638
 * Returns:
 *	Size of the RX Buffer in words (32 bit)
 */
639
static inline u16 ctu_can_fd_get_rx_fifo_size(struct ctucanfd_priv *priv)
640 641
{
	union ctu_can_fd_rx_mem_info reg;
642

643
	reg.u32 = priv->read_reg(priv, CTU_CAN_FD_RX_MEM_INFO);
644 645
	return reg.s.rx_buff_size;
}
646 647 648 649


/*
 * Get number of free words in RX FIFO Buffer of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
650
 *
651
 * Arguments:
652
 *	priv	Private info
653 654 655
 * Returns:
 *	Number of free words (32 bit) in RX Buffer.
 */
656
static inline u16 ctu_can_fd_get_rx_fifo_mem_free(struct ctucanfd_priv *priv)
657 658
{
	union ctu_can_fd_rx_mem_info reg;
659

660
	reg.u32 = priv->read_reg(priv, CTU_CAN_FD_RX_MEM_INFO);
661 662
	return reg.s.rx_mem_free;
}
663 664 665 666


/*
 * Check if RX FIFO Buffer is empty.
Martin Jeřábek's avatar
Martin Jeřábek committed
667
 *
668
 * Arguments:
669
 *	priv	Private info
670 671 672
 * Returns:
 *	True if empty, false otherwise.
 */
673
static inline bool ctu_can_fd_is_rx_fifo_empty(struct ctucanfd_priv *priv)
674 675
{
	union ctu_can_fd_rx_status_rx_settings reg;
676

677
	reg.u32 = priv->read_reg(priv, CTU_CAN_FD_RX_STATUS);
Martin Jeřábek's avatar
Martin Jeřábek committed
678
	return reg.s.rxe;
679
}
680 681 682 683


/*
 * Check if RX FIFO Buffer is full.
Martin Jeřábek's avatar
Martin Jeřábek committed
684
 *
685
 * Arguments:
686
 *	priv	Private info
687 688 689
 * Returns:
 *	True if Full, false otherwise.
 */
690
static inline bool ctu_can_fd_is_rx_fifo_full(struct ctucanfd_priv *priv)
691 692
{
	union ctu_can_fd_rx_status_rx_settings reg;
693

694
	reg.u32 = priv->read_reg(priv, CTU_CAN_FD_RX_STATUS);
Martin Jeřábek's avatar
Martin Jeřábek committed
695
	return reg.s.rxf;
696
}
697 698 699 700


/*
 * Get number of CAN Frames stored in RX Buffer of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
701
 *
702
 * Arguments:
703
 *	priv	Private info
704 705 706
 * Returns:
 *	True if Full, false otherwise.
 */
707
static inline u16 ctu_can_fd_get_rx_frame_count(struct ctucanfd_priv *priv)
708 709
{
	union ctu_can_fd_rx_status_rx_settings reg;
710

711
	reg.u32 = priv->read_reg(priv, CTU_CAN_FD_RX_STATUS);
Martin Jeřábek's avatar
Martin Jeřábek committed
712
	return reg.s.rxfrc;
713
}
714 715 716 717


/*
 * Set timestamp option on RX Frame.
Martin Jeřábek's avatar
Martin Jeřábek committed
718
 *
719
 * Arguments:
720
 *	priv	Private info
721 722
 *	val	Timestamp option settings.
 */
723 724
void ctu_can_fd_set_rx_tsop(struct ctucanfd_priv *priv,
			    enum ctu_can_fd_rx_settings_rtsop val);
725 726


727 728 729 730 731 732 733 734 735
/*
 * Reads the first word of CAN Frame from RX FIFO Buffer.
 *
 * Arguments:
 *	priv	Private info
 *
 * Returns:
 *	The firts word of received frame
 */
736 737
static inline union ctu_can_fd_frame_form_w
	ctu_can_fd_read_rx_ffw(struct ctucanfd_priv *priv)
738 739
{
	union ctu_can_fd_frame_form_w ffw;
740

741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
	ffw.u32 = priv->read_reg(priv, CTU_CAN_FD_RX_DATA);
	return ffw;
}

/*
 * Reads one word of CAN Frame from RX FIFO Buffer.
 *
 * Arguments:
 *	priv	Private info
 *
 * Returns:
 *	One wword of received frame
 */
static inline u32 ctu_can_fd_read_rx_word(struct ctucanfd_priv *priv)
{
	return priv->read_reg(priv, CTU_CAN_FD_RX_DATA);
}


760
/*
Martin Jeřábek's avatar
Martin Jeřábek committed
761 762
 * Reads CAN Frame from RX FIFO Buffer and stores it to a buffer.
 *
763
 * Arguments:
764
 *	priv	Private info
765 766 767
 *	data	Pointer to buffer where the CAN Frame should be stored.
 *	ts	Pointer to u64 where RX Timestamp should be stored.
 */
768 769
void ctu_can_fd_read_rx_frame(struct ctucanfd_priv *priv,
			      struct canfd_frame *data, u64 *ts);
770 771


772 773 774
void ctu_can_fd_read_rx_frame_ffw(struct ctucanfd_priv *priv,
				  struct canfd_frame *cf, u64 *ts,
				  union ctu_can_fd_frame_form_w ffw);
Martin Jeřábek's avatar
Martin Jeřábek committed
775 776


777 778
/*
 * Returns status of TXT Buffer.
Martin Jeřábek's avatar
Martin Jeřábek committed
779
 *
780
 * Arguments:
781
 *	priv	Private info
782 783 784 785
 *	buf	TXT Buffer index (1 to CTU_CAN_FD_TXT_BUFFER_COUNT)
 * Returns:
 *	Status of the TXT Buffer.
 */
786 787
enum ctu_can_fd_tx_status_tx1s
	ctu_can_fd_get_tx_status(struct ctucanfd_priv *priv, u8 buf);
788 789 790 791


/*
 * Checks if TXT Buffer is accessible and can be written to.
Martin Jeřábek's avatar
Martin Jeřábek committed
792
 *
793
 * Arguments:
794
 *	priv	Private info
795 796 797 798
 *	buf	TXT Buffer index (1 to CTU_CAN_FD_TXT_BUFFER_COUNT)
 * Returns:
 *	Status of the TXT Buffer.
 */
799
bool ctu_can_fd_is_txt_buf_accessible(struct ctucanfd_priv *priv, u8 buf);
800 801 802 803


/*
 * Give command to TXT Buffer of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
804
 *
805
 * Arguments:
806
 *	priv	Private info
Martin Jeřábek's avatar
Martin Jeřábek committed
807
 *	cmd	Command line buffer.
808 809 810 811
 *	buf	TXT Buffer index (1 to CTU_CAN_FD_TXT_BUFFER_COUNT)
 * Returns:
 *	Status of the TXT Buffer.
 */