ctu_can_fd_hw.h 22.1 KB
Newer Older
1
/*******************************************************************************
Martin Jeřábek's avatar
Martin Jeřábek committed
2
 *
3
 * CTU CAN FD IP Core
4
 * Copyright (C) 2015-2018
Martin Jeřábek's avatar
Martin Jeřábek committed
5
 *
6 7
 * Authors:
 *     Ondrej Ille <ondrej.ille@gmail.com>
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
8
 *     Martin Jerabek <martin.jerabek01@gmail.com>
Martin Jeřábek's avatar
Martin Jeřábek committed
9 10
 *
 * Project advisors:
11 12
 * 	Jiri Novak <jnovak@fel.cvut.cz>
 * 	Pavel Pisa <pisa@cmp.felk.cvut.cz>
Martin Jeřábek's avatar
Martin Jeřábek committed
13
 *
14 15 16
 * 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
17
 *
18 19 20 21
 * 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
22
 *
23 24 25 26
 * 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
27
 *
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
 * 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
*******************************************************************************/

#ifndef __CTU_CAN_FD_HW__
#define __CTU_CAN_FD_HW__

Martin Jeřábek's avatar
Martin Jeřábek committed
37 38 39 40 41 42
#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

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

/*
47
	MJ TODO:
48 49
	+ move small functions to *.h, make them inline
	+ either pass union arguments by value or just as u32;
50 51 52 53
	  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
54
*/
55 56 57

#define CTU_CAN_FD_RETR_MAX 15

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

#define CTU_CAN_FD_TXT_BUFFER_COUNT 4

Martin Jeřábek's avatar
Martin Jeřábek committed
64 65 66 67
#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
68

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

struct ctucanfd_priv;
#ifndef ctucanfd_priv
141
struct ctucanfd_priv {
Martin Jeřábek's avatar
Martin Jeřábek committed
142
	void __iomem *mem_base;
143 144
	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);
145
};
Martin Jeřábek's avatar
Martin Jeřábek committed
146
#endif
147

148
void ctu_can_fd_write32(struct ctucanfd_priv *priv, enum ctu_can_fd_can_registers reg,
Martin Jeřábek's avatar
Martin Jeřábek committed
149
			u32 val);
150
void ctu_can_fd_write32_be(struct ctucanfd_priv *priv, enum ctu_can_fd_can_registers reg,
Martin Jeřábek's avatar
Martin Jeřábek committed
151
			   u32 val);
152 153
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);
154

155 156 157

/*
 * Checks whether the core is mapped correctly at it's base address.
Martin Jeřábek's avatar
Martin Jeřábek committed
158
 *
159
 * Arguments:
160
 *	priv	Private info
161 162 163 164
 *
 * Returns:
 *	true if the core is accessible correctly, false otherwise.
 */
165
bool ctu_can_fd_check_access(struct ctucanfd_priv *priv);
166 167 168 169


/*
 * Returns version of CTU CAN FD IP Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
170
 *
171
 * Arguments:
172
 *	priv	Private info
173 174 175 176
 *
 * Returns:
 *	IP Core version in format major*10 + minor
 */
177
u32 ctu_can_fd_get_version(struct ctucanfd_priv *priv);
178 179 180 181 182


/*
 * 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
183
 *
184
 * Arguments:
185
 *	priv	Private info
186 187
 *	enable	Enable/disable the core.
 */
188
void ctu_can_fd_enable(struct ctucanfd_priv *priv, bool enable);
189 190


Martin Jeřábek's avatar
Martin Jeřábek committed
191 192 193 194 195 196 197 198 199 200 201
/*
 * 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);


202 203
/*
 * Configures CTU CAN FD Core to limit the amount of retransmit attempts after
204 205
 * occurence of error (Error frame, Arbitration lost). If retransmit limit is
 * disabled, the Core will attempt to retransmit inifinitely. If retransmit
206 207
 * 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
208
 *
209
 * Arguments:
210
 *	priv	Private info
211
 *	enable	Enable/disable the retransmit limitation
Martin Jeřábek's avatar
Martin Jeřábek committed
212
 *      limit	Number to which limit the retransmission (1-CTU_CAN_FD_RETR_MAX)
213 214 215
 * Returns:
 *	True if set correctly. False if "limit" is too high.
 */
216
bool ctu_can_fd_set_ret_limit(struct ctucanfd_priv *priv, bool enable, u8 limit);
217 218 219 220 221 222 223


/*
 * 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:
224 225
 *	CAN_CTRLMODE_LOOPBACK	- Bit loopback mode. Every dominant bit is
 *				  re-routed internally and not send on the bus.
226 227 228 229 230
 *	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,
231
 *				  them as form error. Capability to transmit
232 233 234 235
 *				  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.
236
 *	CAN_CTRLMODE_FD_NON_ISO  - When set, the Core transmits the frames
237
 *				   according to NON-ISO FD standard.
Martin Jeřábek's avatar
Martin Jeřábek committed
238
 *
239
 * Arguments:
240
 *	priv	Private info
Martin Jeřábek's avatar
Martin Jeřábek committed
241
 *	mode	CAN mode to be set to on the Core.
242
 */
243
void ctu_can_fd_set_mode_reg(struct ctucanfd_priv *priv, const struct can_ctrlmode *mode);
244 245 246 247 248


/*
 * 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
249
 *
250
 * Arguments:
251
 *	priv	Private info
252
 */
253
void ctu_can_fd_rel_rx_buf(struct ctucanfd_priv *priv);
254 255 256


/*
Martin Jeřábek's avatar
Martin Jeřábek committed
257
 * Gives command to CTU CAN FD Core to clear the Data overrun flag on
258
 * the RX FIFO Buffer.
Martin Jeřábek's avatar
Martin Jeřábek committed
259
 *
260
 * Arguments:
261
 *	priv	Private info
262
 */
263
void ctu_can_fd_clr_overrun_flag(struct ctucanfd_priv *priv);
264 265 266


/*
267
 * Gives command to CTU CAN FD Core to abort the transmission immediately.
268
 * This action will most likely result in transmission of Error frame.
Martin Jeřábek's avatar
Martin Jeřábek committed
269
 *
270
 * Arguments:
271
 *	priv	Private info
272
 */
273
void ctu_can_fd_abort_tx(struct ctucanfd_priv *priv);
274 275 276 277


/*
 * Returns mode/status vector of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
278
 *
279
 * Arguments:
280
 *	priv	Private info
Martin Jeřábek's avatar
Martin Jeřábek committed
281

282 283 284
 * Returns:
 *	Mode/status structure with multiple mode flags.
 */
285
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
286
{
287 288 289 290
	// MODE and STATUS are within the same word
	union ctu_can_fd_mode_command_status_settings res;
	res.u32 = ctu_can_fd_read32(priv, CTU_CAN_FD_MODE);
	return res;
Martin Jeřábek's avatar
Martin Jeřábek committed
291 292
}

293 294 295

/*
 * Reads the interrupt status vector from CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
296
 *
297
 * Arguments:
298
 *	priv	Private info
Martin Jeřábek's avatar
Martin Jeřábek committed
299

300 301 302
 * Returns:
 *	Interrupt status vector.
 */
303
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
304
{
305 306 307
	union ctu_can_fd_int_stat res;
	res.u32 = ctu_can_fd_read32(priv, CTU_CAN_FD_INT_STAT);
	return res;
Martin Jeřábek's avatar
Martin Jeřábek committed
308
}
309 310 311


/*
312
 * Clears the interrupts from CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
313
 *
314
 * Arguments:
315
 *	priv	Private info
316 317
 *	mask	Mask of interrupts which should be cleared.
 */
318
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
319
{
320
	ctu_can_fd_write32(priv, CTU_CAN_FD_INT_STAT, mask.u32);
Martin Jeřábek's avatar
Martin Jeřábek committed
321
}
322 323 324 325


/*
 * Enable/Disable interrupts of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
326
 *
327
 * Arguments:
328
 *	priv	Private info
329 330 331
 *	mask	Mask of interrupts which should be enabled/disabled.
 * 	val	0 - disable, 1 - enable the interrupt.
 */
332
void ctu_can_fd_int_ena(struct ctucanfd_priv *priv, union ctu_can_fd_int_stat mask,
333
			union ctu_can_fd_int_stat val);
334 335 336 337


/*
 * Mask/Unmask interrupts of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
338
 *
339
 * Arguments:
340
 *	priv	Private info
341 342 343
 *	mask	Mask of interrupts which should be enabled/disabled.
 * 	val	0 - unmask, 1 - mask the interrupt.
 */
344
void ctu_can_fd_int_mask(struct ctucanfd_priv *priv, union ctu_can_fd_int_stat mask,
345
				union ctu_can_fd_int_stat val);
346 347 348 349 350


/*
 * 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,
351
 * which are configured via "retransmit limit" and enabling error interrupts.
Martin Jeřábek's avatar
Martin Jeřábek committed
352
 *
353
 * Arguments:
354
 *	priv	Private info
355 356
 *	mode	Mode of the controller from Socket CAN.
 */
357
void ctu_can_fd_set_mode(struct ctucanfd_priv *priv, const struct can_ctrlmode *mode);
358 359 360 361


/*
 * Set Nominal bit timing of CTU CAN FD Core.
362 363 364
 * 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.
 *
365
 * Arguments:
366
 *	priv	Private info
367 368
 *	nbt	Nominal bit timing settings of CAN Controller.
 */
369
void ctu_can_fd_set_nom_bittiming(struct ctucanfd_priv *priv, struct can_bittiming *nbt);
370 371 372 373


/*
 * Set Data bit timing of CTU CAN FD Core.
374 375 376
 * 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.
 *
377
 * Arguments:
378
 *	priv	Private info
379 380
 *	nbt	Data bit timing settings of CAN Controller.
 */
381
void ctu_can_fd_set_data_bittiming(struct ctucanfd_priv *priv, struct can_bittiming *dbt);
382 383 384 385 386 387 388


/*
 * 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
389
 *
390
 * Arguments:
391
 *	priv	Private info
392 393 394
 *	ewl	Error warning limit
 *	erp	Error passive limit
 */
395
void ctu_can_fd_set_err_limits(struct ctucanfd_priv *priv, u8 ewl, u8 erp);
396 397 398 399


/*
 * Set default error limits to the CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
400
 *
401
 * Arguments:
402
 *	priv	Private info
403
 */
404
static inline void ctu_can_fd_set_def_err_limits(struct ctucanfd_priv *priv)
405
{
406
	ctu_can_fd_set_err_limits(priv, 96, 128);
407
}
408 409 410 411


/*
 * Read TX/RX error counters of CTU CAN FD IP Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
412
 *
413
 * Arguments:
414
 *	priv	Private info
415 416
 *	ctr	Pointer to error counter structure to fill
 */
Martin Jeřábek's avatar
Martin Jeřábek committed
417
void ctu_can_fd_read_err_ctrs(struct ctucanfd_priv *priv, struct can_berr_counter *ctr);
418 419 420 421 422


/*
 * 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
423
 *
424
 * Arguments:
425
 *	priv	Private info
426 427 428
 * Returns:
 *	Number of Error frames detected during Nominal Bit-rate
 */
429
static inline u16 ctu_can_fd_read_nom_errs(struct ctucanfd_priv *priv)
430 431
{
	union ctu_can_fd_err_norm_err_fd reg;
432
	reg.u32 = ctu_can_fd_read32(priv, CTU_CAN_FD_ERR_NORM);
433 434 435
	return reg.s.err_norm_val;
}

436 437 438 439


/*
 * Give command to CTU CAN FD Core to erase the nominal error counter.
Martin Jeřábek's avatar
Martin Jeřábek committed
440
 *
441
 * Arguments:
442
 *	priv	Private info
443
 */
444
static inline void ctu_can_fd_erase_nom_errs(struct ctucanfd_priv *priv)
Martin Jeřábek's avatar
Martin Jeřábek committed
445 446 447 448
{
	union ctu_can_fd_ctr_pres reg;
	reg.u32 = 0;
	reg.s.enorm = 1;
449
	ctu_can_fd_write32(priv, CTU_CAN_FD_CTR_PRES, reg.u32);
Martin Jeřábek's avatar
Martin Jeřábek committed
450
}
451 452 453 454 455 456 457


/*
 * Read special error counter which returns number of Errors which were
 * detected during Data Bit-rate.
 *
 * Arguments:
458
 *	priv	Private info
459 460 461
 * Returns:
 *	Number of Error frames detected during Data Bit-rate
 */
462
static inline u16 ctu_can_fd_read_fd_errs(struct ctucanfd_priv *priv)
463 464
{
	union ctu_can_fd_err_norm_err_fd reg;
465
	reg.u32 = ctu_can_fd_read32(priv, CTU_CAN_FD_ERR_NORM);
466 467
	return reg.s.err_fd_val;
}
468 469 470 471


/*
 * Give command to CTU CAN FD Core to erase the Data error counter.
Martin Jeřábek's avatar
Martin Jeřábek committed
472
 *
473
 * Arguments:
474
 *	priv	Private info
475
 */
476
static inline void ctu_can_fd_erase_fd_errs(struct ctucanfd_priv *priv)
Martin Jeřábek's avatar
Martin Jeřábek committed
477 478 479 480
{
	union ctu_can_fd_ctr_pres reg;
	reg.u32 = 0;
	reg.s.efd = 1;
481
	ctu_can_fd_write32(priv, CTU_CAN_FD_CTR_PRES, reg.u32);
Martin Jeřábek's avatar
Martin Jeřábek committed
482
}
483 484 485


/*
Martin Jeřábek's avatar
Martin Jeřábek committed
486
 * Read fault confinement state of CTU CAN FD Core
487
 * (determined by TX/RX Counters).
Martin Jeřábek's avatar
Martin Jeřábek committed
488
 *
489
 * Arguments:
490
 *	priv	Private info
491 492 493
 * Returns:
 *	Error state of the CTU CAN FD Core.
 */
494
enum can_state ctu_can_fd_read_error_state(struct ctucanfd_priv *priv);
495 496 497 498


/*
 * Set value to TX/RX error counters of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
499
 *
500
 * Arguments:
501
 *	priv	Private info
502 503 504
 * Returns:
 *	Error state of the CTU CAN FD Core.
 */
505
void ctu_can_fd_set_err_ctrs(struct ctucanfd_priv *priv, const struct can_berr_counter *ctr);
506 507 508 509


/*
 * Check Mask filters support of given filter.
Martin Jeřábek's avatar
Martin Jeřábek committed
510
 *
511
 * Arguments:
512
 *	priv	Private info
513 514 515 516
 * 	fnum	Filter number.
 * Returns:
 *	True if filter is present and can be used, False otherwise.
 */
517
bool ctu_can_fd_get_mask_filter_support(struct ctucanfd_priv *priv, u8 fnum);
518 519 520 521


/*
 * Check Range filter support of given filter.
Martin Jeřábek's avatar
Martin Jeřábek committed
522
 *
523
 * Arguments:
524
 *	priv	Private info
525 526 527
 * Returns:
 *	True if Range filter is present and can be used, False otherwise.
 */
528
bool ctu_can_fd_get_range_filter_support(struct ctucanfd_priv *priv);
529 530 531 532


/*
 * Configure mask filter of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
533
 *
534
 * Arguments:
535
 *	priv	Private info
536 537 538 539 540 541
 *	fnum	Filter number.
 *	enable  True if filter should be enabled.
 *	filter	Filter configuration.
 * Returns:
 *	True if mask filter was configured properly, false otherwise.
 */
542
bool ctu_can_fd_set_mask_filter(struct ctucanfd_priv *priv, u8 fnum, bool enable,
Martin Jeřábek's avatar
Martin Jeřábek committed
543
				const struct can_filter *filter);
544 545 546 547 548

/*
 * 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
549
 *
550
 * Arguments:
551
 *	priv	Private info
552 553 554 555
 *	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.
 */
556
void ctu_can_fd_set_range_filter(struct ctucanfd_priv *priv, canid_t low_th,
557 558 559 560
				 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
561
 *
562
 * Arguments:
563
 *	priv	Private info
564 565 566
 * Returns:
 *	Size of the RX Buffer in words (32 bit)
 */
567
static inline u16 ctu_can_fd_get_rx_fifo_size(struct ctucanfd_priv *priv)
568 569
{
	union ctu_can_fd_rx_mem_info reg;
570
	reg.u32 = ctu_can_fd_read32(priv, CTU_CAN_FD_RX_MEM_INFO);
571 572
	return reg.s.rx_buff_size;
}
573 574 575 576


/*
 * Get number of free words in RX FIFO Buffer of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
577
 *
578
 * Arguments:
579
 *	priv	Private info
580 581 582
 * Returns:
 *	Number of free words (32 bit) in RX Buffer.
 */
583
static inline u16 ctu_can_fd_get_rx_fifo_mem_free(struct ctucanfd_priv *priv)
584 585
{
	union ctu_can_fd_rx_mem_info reg;
586
	reg.u32 = ctu_can_fd_read32(priv, CTU_CAN_FD_RX_MEM_INFO);
587 588
	return reg.s.rx_mem_free;
}
589 590 591 592


/*
 * Check if RX FIFO Buffer is empty.
Martin Jeřábek's avatar
Martin Jeřábek committed
593
 *
594
 * Arguments:
595
 *	priv	Private info
596 597 598
 * Returns:
 *	True if empty, false otherwise.
 */
599
static inline bool ctu_can_fd_is_rx_fifo_empty(struct ctucanfd_priv *priv)
600 601
{
	union ctu_can_fd_rx_status_rx_settings reg;
602
	reg.u32 = ctu_can_fd_read32(priv, CTU_CAN_FD_RX_STATUS);
Martin Jeřábek's avatar
Martin Jeřábek committed
603
	return reg.s.rxe;
604
}
605 606 607 608


/*
 * Check if RX FIFO Buffer is full.
Martin Jeřábek's avatar
Martin Jeřábek committed
609
 *
610
 * Arguments:
611
 *	priv	Private info
612 613 614
 * Returns:
 *	True if Full, false otherwise.
 */
615
static inline bool ctu_can_fd_is_rx_fifo_full(struct ctucanfd_priv *priv)
616 617
{
	union ctu_can_fd_rx_status_rx_settings reg;
618
	reg.u32 = ctu_can_fd_read32(priv, CTU_CAN_FD_RX_STATUS);
Martin Jeřábek's avatar
Martin Jeřábek committed
619
	return reg.s.rxf;
620
}
621 622 623 624


/*
 * Get number of CAN Frames stored in RX Buffer of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
625
 *
626
 * Arguments:
627
 *	priv	Private info
628 629 630
 * Returns:
 *	True if Full, false otherwise.
 */
631
static inline u16 ctu_can_fd_get_rx_frame_count(struct ctucanfd_priv *priv)
632 633
{
	union ctu_can_fd_rx_status_rx_settings reg;
634
	reg.u32 = ctu_can_fd_read32(priv, CTU_CAN_FD_RX_STATUS);
Martin Jeřábek's avatar
Martin Jeřábek committed
635
	return reg.s.rxfrc;
636
}
637 638 639 640


/*
 * Set timestamp option on RX Frame.
Martin Jeřábek's avatar
Martin Jeřábek committed
641
 *
642
 * Arguments:
643
 *	priv	Private info
644 645
 *	val	Timestamp option settings.
 */
646
void ctu_can_fd_set_rx_tsop(struct ctucanfd_priv *priv, enum ctu_can_fd_rx_settings_rtsop val);
647 648 649


/*
Martin Jeřábek's avatar
Martin Jeřábek committed
650 651
 * Reads CAN Frame from RX FIFO Buffer and stores it to a buffer.
 *
652
 * Arguments:
653
 *	priv	Private info
654 655 656
 *	data	Pointer to buffer where the CAN Frame should be stored.
 *	ts	Pointer to u64 where RX Timestamp should be stored.
 */
657
void ctu_can_fd_read_rx_frame(struct ctucanfd_priv *priv, struct canfd_frame *data, u64 *ts);
658 659


Martin Jeřábek's avatar
Martin Jeřábek committed
660 661 662
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);


663 664
/*
 * Returns status of TXT Buffer.
Martin Jeřábek's avatar
Martin Jeřábek committed
665
 *
666
 * Arguments:
667
 *	priv	Private info
668 669 670 671
 *	buf	TXT Buffer index (1 to CTU_CAN_FD_TXT_BUFFER_COUNT)
 * Returns:
 *	Status of the TXT Buffer.
 */
672
enum ctu_can_fd_tx_status_tx1s ctu_can_fd_get_tx_status(struct ctucanfd_priv *priv, u8 buf);
673 674 675 676


/*
 * Checks if TXT Buffer is accessible and can be written to.
Martin Jeřábek's avatar
Martin Jeřábek committed
677
 *
678
 * Arguments:
679
 *	priv	Private info
680 681 682 683
 *	buf	TXT Buffer index (1 to CTU_CAN_FD_TXT_BUFFER_COUNT)
 * Returns:
 *	Status of the TXT Buffer.
 */
684
bool ctu_can_fd_is_txt_buf_accessible(struct ctucanfd_priv *priv, u8 buf);
685 686 687 688


/*
 * Give command to TXT Buffer of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
689
 *
690
 * Arguments:
691
 *	priv	Private info
Martin Jeřábek's avatar
Martin Jeřábek committed
692
 *	cmd	Command line buffer.
693 694 695 696
 *	buf	TXT Buffer index (1 to CTU_CAN_FD_TXT_BUFFER_COUNT)
 * Returns:
 *	Status of the TXT Buffer.
 */
697
bool ctu_can_fd_txt_buf_give_command(struct ctucanfd_priv *priv, u8 cmd, u8 buf);
698 699 700 701


/*
 * Give "set_empty" command to TXT Buffer.
Martin Jeřábek's avatar
Martin Jeřábek committed
702
 *
703
 * Arguments:
704
 *	priv	Private info
705 706 707 708
 *	buf	TXT Buffer index (1 to CTU_CAN_FD_TXT_BUFFER_COUNT)
 * Returns:
 *	Status of the TXT Buffer.
 */
709
static inline void ctu_can_fd_txt_set_empty(struct ctucanfd_priv *priv, u8 buf)
710
{
711
	ctu_can_fd_txt_buf_give_command(priv, 0x1, buf);
712
}
713 714 715 716


/*
 * Give "set_ready" command to TXT Buffer.
Martin Jeřábek's avatar
Martin Jeřábek committed
717
 *
718
 * Arguments:
719
 *	priv	Private info
720 721 722 723
 *	buf	TXT Buffer index (1 to CTU_CAN_FD_TXT_BUFFER_COUNT)
 * Returns:
 *	Status of the TXT Buffer.
 */
724
static inline void ctu_can_fd_txt_set_rdy(struct ctucanfd_priv *priv, u8 buf)
725
{
726
	ctu_can_fd_txt_buf_give_command(priv, 0x2, buf);
727
}
728 729 730 731


/*
 * Give "set_abort" command to TXT Buffer.
Martin Jeřábek's avatar
Martin Jeřábek committed
732
 *
733
 * Arguments:
734
 *	priv	Private info
735 736 737 738
 *	buf	TXT Buffer index (1 to CTU_CAN_FD_TXT_BUFFER_COUNT)
 * Returns:
 *	Status of the TXT Buffer.
 */
739
static inline void ctu_can_fd_txt_set_abort(struct ctucanfd_priv *priv, u8 buf)
740
{
741
	ctu_can_fd_txt_buf_give_command(priv, 0x4, buf);
742
}
743 744 745 746


/*
 * Set priority of TXT Buffers in CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
747
 *
748
 * Arguments:
749
 *	priv	Private info
750 751 752
 *	prio	Pointer to array with CTU_CAN_FD_TXT_BUFFER_COUNT number
 *		of elements with TXT Buffer priorities.
 */
753
void ctu_can_fd_set_txt_priority(struct ctucanfd_priv *priv, const u8 *prio);
754 755 756 757


/*
 * Insert CAN FD frame to TXT Buffer of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
758
 *
759
 * Arguments:
760
 *	priv	Private info
761 762 763
 *	data	Pointer to CAN Frame buffer.
 *	u64	Timestamp when the buffer should be sent.
 *	buf	Index of TXT Buffer where to insert the CAN Frame.
Martin Jeřábek's avatar
Martin Jeřábek committed
764
 * 	isfdf	True if the frame is a FD frame.
765 766 767
 * Returns:
 *	True if the frame was inserted succesfully, False otherwise.
 */
768
bool ctu_can_fd_insert_frame(struct ctucanfd_priv *priv, const struct canfd_frame *data, u64 ts,
Martin Jeřábek's avatar
Martin Jeřábek committed
769
				u8 buf, bool isfdf);
770 771 772 773 774

/*
 * Read transceiver delay as measured by CTU CAN FD Core. Note that
 * transceiver delay can be measured only after at least one CAN FD Frame with
 * BRS bit was sent since the last re-start of the Core!
Martin Jeřábek's avatar
Martin Jeřábek committed
775
 *
776
 * Arguments:
777
 *	priv	Private info