ctu_can_fd_hw.h 24.3 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
	+ consider move of more small functions to *.h, make them inline
51
	+ 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

152 153 154 155 156 157 158 159
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);
160

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

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

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

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

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

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

/*
 * 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
250
 *
251
 * Arguments:
252
 *	priv	Private info
253
 */
254
void ctu_can_fd_rel_rx_buf(struct ctucanfd_priv *priv);
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
 * Gives command to CTU CAN FD Core to abort the transmission immediately.
267
 * This action will most likely result in transmission of Error frame.
Martin Jeřábek's avatar
Martin Jeřábek committed
268
 *
269
 * Arguments:
270
 *	priv	Private info
271
 */
272
void ctu_can_fd_abort_tx(struct ctucanfd_priv *priv);
273 274 275

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

280 281 282
 * Returns:
 *	Mode/status structure with multiple mode flags.
 */
283
static inline union ctu_can_fd_status
284
	ctu_can_get_status(struct ctucanfd_priv *priv)
Martin Jeřábek's avatar
Martin Jeřábek committed
285
{
286
	/* MODE and STATUS are within the same word */
287
	union ctu_can_fd_status res;
288

289
	res.u32 = priv->read_reg(priv, CTU_CAN_FD_STATUS);
290
	return res;
Martin Jeřábek's avatar
Martin Jeřábek committed
291 292
}

293 294 295 296 297 298 299 300 301 302 303
/*
 * 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)
{
304
	union ctu_can_fd_mode_settings reg;
305

306
	reg.u32 = priv->read_reg(priv, CTU_CAN_FD_MODE);
307
	return reg.s.ena == CTU_CAN_ENABLED;
308 309
}

310 311
/*
 * Reads the interrupt status vector from CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
312
 *
313
 * Arguments:
314
 *	priv	Private info
Martin Jeřábek's avatar
Martin Jeřábek committed
315

316 317 318
 * Returns:
 *	Interrupt status vector.
 */
319 320
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
321
{
322
	union ctu_can_fd_int_stat res;
323

324
	res.u32 = priv->read_reg(priv, CTU_CAN_FD_INT_STAT);
325
	return res;
Martin Jeřábek's avatar
Martin Jeřábek committed
326
}
327 328

/*
329
 * Clears the interrupts from CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
330
 *
331
 * Arguments:
332
 *	priv	Private info
333 334
 *	mask	Mask of interrupts which should be cleared.
 */
335 336
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
337
{
338 339 340 341 342 343 344 345 346 347
	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.
 */
348 349
static inline void ctu_can_fd_int_ena_set(struct ctucanfd_priv *priv,
					  union ctu_can_fd_int_stat mask)
350 351 352 353 354 355 356 357 358 359 360
{
	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.
 */
361 362
static inline void ctu_can_fd_int_ena_clr(struct ctucanfd_priv *priv,
					  union ctu_can_fd_int_stat mask)
363 364
{
	priv->write_reg(priv, CTU_CAN_FD_INT_ENA_CLR, mask.u32);
Martin Jeřábek's avatar
Martin Jeřábek committed
365
}
366 367 368

/*
 * Enable/Disable interrupts of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
369
 *
370
 * Arguments:
371
 *	priv	Private info
372
 *	mask	Mask of interrupts which should be enabled/disabled.
373
 *	val	0 - disable, 1 - enable the interrupt.
374
 */
375 376
void ctu_can_fd_int_ena(struct ctucanfd_priv *priv,
			union ctu_can_fd_int_stat mask,
377
			union ctu_can_fd_int_stat val);
378 379 380

/*
 * Mask/Unmask interrupts of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
381
 *
382
 * Arguments:
383
 *	priv	Private info
384
 *	mask	Mask of interrupts which should be enabled/disabled.
385
 *	val	0 - unmask, 1 - mask the interrupt.
386
 */
387 388 389
void ctu_can_fd_int_mask(struct ctucanfd_priv *priv,
			 union ctu_can_fd_int_stat mask,
			 union ctu_can_fd_int_stat val);
390 391 392 393

/*
 * 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,
394
 * which are configured via "retransmit limit" and enabling error interrupts.
Martin Jeřábek's avatar
Martin Jeřábek committed
395
 *
396
 * Arguments:
397
 *	priv	Private info
398 399
 *	mode	Mode of the controller from Socket CAN.
 */
400 401
void ctu_can_fd_set_mode(struct ctucanfd_priv *priv,
			 const struct can_ctrlmode *mode);
402 403 404

/*
 * Set Nominal bit timing of CTU CAN FD Core.
405
 * NOTE: phase_seg1 and prop_seg may be modified if phase_seg1 > 63
406 407
 *       This is because in Linux, the constraints are only
 *       on phase_seg1+prop_seg.
408
 *
409
 * Arguments:
410
 *	priv	Private info
411 412
 *	nbt	Nominal bit timing settings of CAN Controller.
 */
413 414
void ctu_can_fd_set_nom_bittiming(struct ctucanfd_priv *priv,
				  struct can_bittiming *nbt);
415 416 417

/*
 * Set Data bit timing of CTU CAN FD Core.
418
 * NOTE: phase_seg1 and prop_seg may be modified if phase_seg1 > 63
419 420
 *       This is because in Linux, the constraints are only
 *       on phase_seg1+prop_seg.
421
 *
422
 * Arguments:
423
 *	priv	Private info
424 425
 *	nbt	Data bit timing settings of CAN Controller.
 */
426 427
void ctu_can_fd_set_data_bittiming(struct ctucanfd_priv *priv,
				   struct can_bittiming *dbt);
428 429 430 431 432 433

/*
 * 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
434
 *
435
 * Arguments:
436
 *	priv	Private info
437 438 439
 *	ewl	Error warning limit
 *	erp	Error passive limit
 */
440
void ctu_can_fd_set_err_limits(struct ctucanfd_priv *priv, u8 ewl, u8 erp);
441 442 443

/*
 * Set default error limits to the CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
444
 *
445
 * Arguments:
446
 *	priv	Private info
447
 */
448
static inline void ctu_can_fd_set_def_err_limits(struct ctucanfd_priv *priv)
449
{
450
	ctu_can_fd_set_err_limits(priv, 96, 128);
451
}
452 453 454

/*
 * Read TX/RX error counters of CTU CAN FD IP Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
455
 *
456
 * Arguments:
457
 *	priv	Private info
458 459
 *	ctr	Pointer to error counter structure to fill
 */
460 461
void ctu_can_fd_read_err_ctrs(struct ctucanfd_priv *priv,
			      struct can_berr_counter *ctr);
462 463 464 465

/*
 * 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
466
 *
467
 * Arguments:
468
 *	priv	Private info
469 470 471
 * Returns:
 *	Number of Error frames detected during Nominal Bit-rate
 */
472
static inline u16 ctu_can_fd_read_nom_errs(struct ctucanfd_priv *priv)
473 474
{
	union ctu_can_fd_err_norm_err_fd reg;
475

476
	reg.u32 = priv->read_reg(priv, CTU_CAN_FD_ERR_NORM);
477 478 479
	return reg.s.err_norm_val;
}

480 481
/*
 * Give command to CTU CAN FD Core to erase the nominal error counter.
Martin Jeřábek's avatar
Martin Jeřábek committed
482
 *
483
 * Arguments:
484
 *	priv	Private info
485
 */
486
static inline void ctu_can_fd_erase_nom_errs(struct ctucanfd_priv *priv)
Martin Jeřábek's avatar
Martin Jeřábek committed
487 488
{
	union ctu_can_fd_ctr_pres reg;
489

Martin Jeřábek's avatar
Martin Jeřábek committed
490 491
	reg.u32 = 0;
	reg.s.enorm = 1;
492
	priv->write_reg(priv, CTU_CAN_FD_CTR_PRES, reg.u32);
Martin Jeřábek's avatar
Martin Jeřábek committed
493
}
494 495 496 497 498 499

/*
 * Read special error counter which returns number of Errors which were
 * detected during Data Bit-rate.
 *
 * Arguments:
500
 *	priv	Private info
501 502 503
 * Returns:
 *	Number of Error frames detected during Data Bit-rate
 */
504
static inline u16 ctu_can_fd_read_fd_errs(struct ctucanfd_priv *priv)
505 506
{
	union ctu_can_fd_err_norm_err_fd reg;
507

508
	reg.u32 = priv->read_reg(priv, CTU_CAN_FD_ERR_NORM);
509 510
	return reg.s.err_fd_val;
}
511 512 513

/*
 * Give command to CTU CAN FD Core to erase the Data error counter.
Martin Jeřábek's avatar
Martin Jeřábek committed
514
 *
515
 * Arguments:
516
 *	priv	Private info
517
 */
518
static inline void ctu_can_fd_erase_fd_errs(struct ctucanfd_priv *priv)
Martin Jeřábek's avatar
Martin Jeřábek committed
519 520
{
	union ctu_can_fd_ctr_pres reg;
521

Martin Jeřábek's avatar
Martin Jeřábek committed
522 523
	reg.u32 = 0;
	reg.s.efd = 1;
524
	priv->write_reg(priv, CTU_CAN_FD_CTR_PRES, reg.u32);
Martin Jeřábek's avatar
Martin Jeřábek committed
525
}
526 527

/*
Martin Jeřábek's avatar
Martin Jeřábek committed
528
 * Read fault confinement state of CTU CAN FD Core
529
 * (determined by TX/RX Counters).
Martin Jeřábek's avatar
Martin Jeřábek committed
530
 *
531
 * Arguments:
532
 *	priv	Private info
533 534 535
 * Returns:
 *	Error state of the CTU CAN FD Core.
 */
536
enum can_state ctu_can_fd_read_error_state(struct ctucanfd_priv *priv);
537 538 539

/*
 * Set value to TX/RX error counters of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
540
 *
541
 * Arguments:
542
 *	priv	Private info
543 544 545
 * Returns:
 *	Error state of the CTU CAN FD Core.
 */
546 547
void ctu_can_fd_set_err_ctrs(struct ctucanfd_priv *priv,
			     const struct can_berr_counter *ctr);
548 549 550

/*
 * Check Mask filters support of given filter.
Martin Jeřábek's avatar
Martin Jeřábek committed
551
 *
552
 * Arguments:
553
 *	priv	Private info
554
 *	fnum	Filter number.
555 556 557
 * Returns:
 *	True if filter is present and can be used, False otherwise.
 */
558
bool ctu_can_fd_get_mask_filter_support(struct ctucanfd_priv *priv, u8 fnum);
559 560 561

/*
 * Check Range filter support of given filter.
Martin Jeřábek's avatar
Martin Jeřábek committed
562
 *
563
 * Arguments:
564
 *	priv	Private info
565 566 567
 * Returns:
 *	True if Range filter is present and can be used, False otherwise.
 */
568
bool ctu_can_fd_get_range_filter_support(struct ctucanfd_priv *priv);
569 570 571

/*
 * Configure mask filter of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
572
 *
573
 * Arguments:
574
 *	priv	Private info
575 576 577 578 579 580
 *	fnum	Filter number.
 *	enable  True if filter should be enabled.
 *	filter	Filter configuration.
 * Returns:
 *	True if mask filter was configured properly, false otherwise.
 */
581 582
bool ctu_can_fd_set_mask_filter(struct ctucanfd_priv *priv, u8 fnum,
				bool enable, const struct can_filter *filter);
583 584 585 586 587

/*
 * 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
588
 *
589
 * Arguments:
590
 *	priv	Private info
591 592 593 594
 *	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.
 */
595
void ctu_can_fd_set_range_filter(struct ctucanfd_priv *priv, canid_t low_th,
596 597 598 599
				 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
600
 *
601
 * Arguments:
602
 *	priv	Private info
603 604 605
 * Returns:
 *	Size of the RX Buffer in words (32 bit)
 */
606
static inline u16 ctu_can_fd_get_rx_fifo_size(struct ctucanfd_priv *priv)
607 608
{
	union ctu_can_fd_rx_mem_info reg;
609

610
	reg.u32 = priv->read_reg(priv, CTU_CAN_FD_RX_MEM_INFO);
611 612
	return reg.s.rx_buff_size;
}
613 614 615

/*
 * Get number of free words in RX FIFO Buffer of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
616
 *
617
 * Arguments:
618
 *	priv	Private info
619 620 621
 * Returns:
 *	Number of free words (32 bit) in RX Buffer.
 */
622
static inline u16 ctu_can_fd_get_rx_fifo_mem_free(struct ctucanfd_priv *priv)
623 624
{
	union ctu_can_fd_rx_mem_info reg;
625

626
	reg.u32 = priv->read_reg(priv, CTU_CAN_FD_RX_MEM_INFO);
627 628
	return reg.s.rx_mem_free;
}
629 630 631

/*
 * Check if RX FIFO Buffer is empty.
Martin Jeřábek's avatar
Martin Jeřábek committed
632
 *
633
 * Arguments:
634
 *	priv	Private info
635 636 637
 * Returns:
 *	True if empty, false otherwise.
 */
638
static inline bool ctu_can_fd_is_rx_fifo_empty(struct ctucanfd_priv *priv)
639 640
{
	union ctu_can_fd_rx_status_rx_settings reg;
641

642
	reg.u32 = priv->read_reg(priv, CTU_CAN_FD_RX_STATUS);
Martin Jeřábek's avatar
Martin Jeřábek committed
643
	return reg.s.rxe;
644
}
645 646 647

/*
 * Check if RX FIFO Buffer is full.
Martin Jeřábek's avatar
Martin Jeřábek committed
648
 *
649
 * Arguments:
650
 *	priv	Private info
651 652 653
 * Returns:
 *	True if Full, false otherwise.
 */
654
static inline bool ctu_can_fd_is_rx_fifo_full(struct ctucanfd_priv *priv)
655 656
{
	union ctu_can_fd_rx_status_rx_settings reg;
657

658
	reg.u32 = priv->read_reg(priv, CTU_CAN_FD_RX_STATUS);
Martin Jeřábek's avatar
Martin Jeřábek committed
659
	return reg.s.rxf;
660
}
661 662 663

/*
 * Get number of CAN Frames stored in RX Buffer of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
664
 *
665
 * Arguments:
666
 *	priv	Private info
667 668 669
 * Returns:
 *	True if Full, false otherwise.
 */
670
static inline u16 ctu_can_fd_get_rx_frame_count(struct ctucanfd_priv *priv)
671 672
{
	union ctu_can_fd_rx_status_rx_settings reg;
673

674
	reg.u32 = priv->read_reg(priv, CTU_CAN_FD_RX_STATUS);
Martin Jeřábek's avatar
Martin Jeřábek committed
675
	return reg.s.rxfrc;
676
}
677 678 679

/*
 * Set timestamp option on RX Frame.
Martin Jeřábek's avatar
Martin Jeřábek committed
680
 *
681
 * Arguments:
682
 *	priv	Private info
683 684
 *	val	Timestamp option settings.
 */
685 686
void ctu_can_fd_set_rx_tsop(struct ctucanfd_priv *priv,
			    enum ctu_can_fd_rx_settings_rtsop val);
687

688 689 690 691 692 693 694 695 696
/*
 * Reads the first word of CAN Frame from RX FIFO Buffer.
 *
 * Arguments:
 *	priv	Private info
 *
 * Returns:
 *	The firts word of received frame
 */
697 698
static inline union ctu_can_fd_frame_form_w
	ctu_can_fd_read_rx_ffw(struct ctucanfd_priv *priv)
699 700
{
	union ctu_can_fd_frame_form_w ffw;
701

702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
	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);
}

720
/*
Martin Jeřábek's avatar
Martin Jeřábek committed
721 722
 * Reads CAN Frame from RX FIFO Buffer and stores it to a buffer.
 *
723
 * Arguments:
724
 *	priv	Private info
725 726 727
 *	data	Pointer to buffer where the CAN Frame should be stored.
 *	ts	Pointer to u64 where RX Timestamp should be stored.
 */
728 729
void ctu_can_fd_read_rx_frame(struct ctucanfd_priv *priv,
			      struct canfd_frame *data, u64 *ts);
730

731 732 733 734 735 736 737 738 739
/*
 * Reads rest of CAN Frame from RX FIFO Buffer and stores it to a buffer.
 *
 * Arguments:
 *	priv	Private info
 *	data	Pointer to buffer where the CAN Frame should be stored.
 *	ts	Pointer to u64 where RX Timestamp should be stored.
 *	ffw     Already read the first frame control word by the caller
 */
740 741 742
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
743

744 745
/*
 * Returns status of TXT Buffer.
Martin Jeřábek's avatar
Martin Jeřábek committed
746
 *
747
 * Arguments:
748
 *	priv	Private info
749 750 751 752
 *	buf	TXT Buffer index (1 to CTU_CAN_FD_TXT_BUFFER_COUNT)
 * Returns:
 *	Status of the TXT Buffer.
 */
753 754
enum ctu_can_fd_tx_status_tx1s
	ctu_can_fd_get_tx_status(struct ctucanfd_priv *priv, u8 buf);
755 756 757

/*
 * Checks if TXT Buffer is accessible and can be written to.
Martin Jeřábek's avatar
Martin Jeřábek committed
758
 *
759
 * Arguments:
760
 *	priv	Private info
761 762 763 764
 *	buf	TXT Buffer index (1 to CTU_CAN_FD_TXT_BUFFER_COUNT)
 * Returns:
 *	Status of the TXT Buffer.
 */
765
bool ctu_can_fd_is_txt_buf_accessible(struct ctucanfd_priv *priv, u8 buf);
766 767 768

/*
 * Give command to TXT Buffer of CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
769
 *
770
 * Arguments:
771
 *	priv	Private info
Martin Jeřábek's avatar
Martin Jeřábek committed
772
 *	cmd	Command line buffer.
773 774 775 776
 *	buf	TXT Buffer index (1 to CTU_CAN_FD_TXT_BUFFER_COUNT)
 * Returns:
 *	Status of the TXT Buffer.
 */
777 778
bool ctu_can_fd_txt_buf_give_command(struct ctucanfd_priv *priv, u8 cmd,
				     u8 buf);
779 780 781

/*
 * Give "set_empty" command to TXT Buffer.
Martin Jeřábek's avatar
Martin Jeřábek committed
782
 *
783
 * Arguments:
784
 *	priv	Private info
785 786 787 788
 *	buf	TXT Buffer index (1 to CTU_CAN_FD_TXT_BUFFER_COUNT)
 * Returns:
 *	Status of the TXT Buffer.
 */
789
static inline void ctu_can_fd_txt_set_empty(struct ctucanfd_priv *priv, u8 buf)
790
{
791
	ctu_can_fd_txt_buf_give_command(priv, 0x1, buf);
792
}
793 794 795

/*
 * Give "set_ready" command to TXT Buffer.
Martin Jeřábek's avatar
Martin Jeřábek committed
796
 *
797
 * Arguments:
798
 *	priv	Private info
799 800 801 802
 *	buf	TXT Buffer index (1 to CTU_CAN_FD_TXT_BUFFER_COUNT)
 * Returns:
 *	Status of the TXT Buffer.
 */
803
static inline void ctu_can_fd_txt_set_rdy(struct ctucanfd_priv *priv, u8 buf)
804
{
805
	ctu_can_fd_txt_buf_give_command(priv, 0x2, buf);
806
}
807 808 809

/*
 * Give "set_abort" command to TXT Buffer.
Martin Jeřábek's avatar
Martin Jeřábek committed
810
 *
811
 * Arguments:
812
 *	priv	Private info
813 814 815 816
 *	buf	TXT Buffer index (1 to CTU_CAN_FD_TXT_BUFFER_COUNT)
 * Returns:
 *	Status of the TXT Buffer.
 */
817
static inline void ctu_can_fd_txt_set_abort(struct ctucanfd_priv *priv, u8 buf)
818
{
819
	ctu_can_fd_txt_buf_give_command(priv, 0x4, buf);
820
}
821 822 823

/*
 * Set priority of TXT Buffers in CTU CAN FD Core.
Martin Jeřábek's avatar
Martin Jeřábek committed
824
 *
825
 * Arguments:
826
 *	priv	Private info
827 828 829
 *	prio	Pointer to array with CTU_CAN_FD_TXT_BUFFER_COUNT number
 *		of elements with TXT Buffer priorities.
<