Loading...
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 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 427 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 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 | /*
* Copyright (c) 2017-2023 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @brief UART driver for Intel FPGA UART Core IP
* Reference : Embedded Peripherals IP User Guide : 12. JTAG UART Core
*
*/
#include <zephyr/kernel.h>
#include <zephyr/arch/cpu.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/sys/sys_io.h>
#include <zephyr/sys/__assert.h>
#define DT_DRV_COMPAT altr_jtag_uart
#define UART_ALTERA_JTAG_DATA_OFFSET 0x00 /* DATA : Register offset */
#define UART_ALTERA_JTAG_CTRL_OFFSET 0x04 /* CTRL : Register offset */
#define UART_IE_TX (1 << 1) /* CTRL : TX Interrupt Enable */
#define UART_IE_RX (1 << 0) /* CTRL : RX Interrupt Enable */
#define UART_DATA_MASK 0xFF /* DATA : Data Mask */
#define UART_WFIFO_MASK 0xFFFF0000 /* CTRL : Transmit FIFO Mask */
#define UART_WFIFO_OFST (16)
#define ALTERA_AVALON_JTAG_UART_DATA_DATA_OFST (0)
#define ALTERA_AVALON_JTAG_UART_DATA_RVALID_MSK (0x00008000)
#define ALTERA_AVALON_JTAG_UART_CONTROL_RI_MSK (0x00000100)
#define ALTERA_AVALON_JTAG_UART_CONTROL_WI_MSK (0x00000200)
#ifdef CONFIG_UART_ALTERA_JTAG_HAL
#include "altera_avalon_jtag_uart.h"
#include "altera_avalon_jtag_uart_regs.h"
extern int altera_avalon_jtag_uart_read(altera_avalon_jtag_uart_state *sp,
char *buffer, int space, int flags);
extern int altera_avalon_jtag_uart_write(altera_avalon_jtag_uart_state *sp,
const char *ptr, int count, int flags);
#else
/* device data */
struct uart_altera_jtag_device_data {
struct k_spinlock lock;
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
uart_irq_callback_user_data_t cb; /* Callback function pointer */
void *cb_data; /* Callback function arg */
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
};
/* device config */
struct uart_altera_jtag_device_config {
mm_reg_t base;
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
uart_irq_config_func_t irq_config_func;
unsigned int irq_num;
uint16_t write_fifo_depth;
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
};
#endif /* CONFIG_UART_ALTERA_JTAG_HAL */
#ifndef CONFIG_UART_ALTERA_JTAG_HAL
/**
* @brief Poll the device for input.
*
* @param dev UART device instance
* @param c Pointer to character
*
* @return 0 if a character arrived, -1 otherwise.
* -EINVAL if c is null pointer.
*/
static int uart_altera_jtag_poll_in(const struct device *dev,
unsigned char *c)
{
int ret = -1;
const struct uart_altera_jtag_device_config *config = dev->config;
struct uart_altera_jtag_device_data *data = dev->data;
unsigned int input_data;
/* generate fatal error if CONFIG_ASSERT is enabled. */
__ASSERT(c != NULL, "c is null pointer!");
/* Stop, if c is null pointer */
if (c == NULL) {
return -EINVAL;
}
k_spinlock_key_t key = k_spin_lock(&data->lock);
input_data = sys_read32(config->base + UART_ALTERA_JTAG_DATA_OFFSET);
/* check if data is valid. */
if (input_data & ALTERA_AVALON_JTAG_UART_DATA_RVALID_MSK) {
*c = (input_data & UART_DATA_MASK) >> ALTERA_AVALON_JTAG_UART_DATA_DATA_OFST;
ret = 0;
}
k_spin_unlock(&data->lock, key);
return ret;
}
#endif /* CONFIG_UART_ALTERA_JTAG_HAL */
/**
* @brief Output a character in polled mode.
*
* This routine checks if the transmitter is full.
* When the transmitter is not full, it writes a character to the data register.
* It waits and blocks the calling thread, otherwise. This function is a blocking call.
*
* @param dev UART device instance
* @param c Character to send
*/
static void uart_altera_jtag_poll_out(const struct device *dev,
unsigned char c)
{
#ifdef CONFIG_UART_ALTERA_JTAG_HAL
altera_avalon_jtag_uart_state ustate;
ustate.base = JTAG_UART_0_BASE;
altera_avalon_jtag_uart_write(&ustate, &c, 1, 0);
#else
const struct uart_altera_jtag_device_config *config = dev->config;
struct uart_altera_jtag_device_data *data = dev->data;
k_spinlock_key_t key = k_spin_lock(&data->lock);
/* While TX FIFO full */
while (!(sys_read32(config->base + UART_ALTERA_JTAG_CTRL_OFFSET) & UART_WFIFO_MASK)) {
}
sys_write8(c, config->base + UART_ALTERA_JTAG_DATA_OFFSET);
k_spin_unlock(&data->lock, key);
#endif /* CONFIG_UART_ALTERA_JTAG_HAL */
}
/**
* @brief Initialise an instance of the driver
*
* This function initialise the interrupt configuration for the driver.
*
* @param dev UART device instance
*
* @return 0 to indicate success.
*/
static int uart_altera_jtag_init(const struct device *dev)
{
/*
* Work around to clear interrupt enable bits
* as it is not being done by HAL driver explicitly.
*/
#ifdef CONFIG_UART_ALTERA_JTAG_HAL
IOWR_ALTERA_AVALON_JTAG_UART_CONTROL(JTAG_UART_0_BASE, 0);
#else
const struct uart_altera_jtag_device_config *config = dev->config;
uint32_t ctrl_val = sys_read32(config->base + UART_ALTERA_JTAG_CTRL_OFFSET);
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
/*
* Enable hardware interrupt.
* The corresponding csr from IP still needs to be set,
* so that the IP generates interrupt signal.
*/
config->irq_config_func(dev);
#endif
/* Disable the tx and rx interrupt signals from JTAG core IP. */
ctrl_val &= ~(UART_IE_TX | UART_IE_RX);
sys_write32(ctrl_val, config->base + UART_ALTERA_JTAG_CTRL_OFFSET);
#endif /* CONFIG_UART_ALTERA_JTAG_HAL */
return 0;
}
/*
* Functions for Interrupt driven API
*/
#if defined(CONFIG_UART_INTERRUPT_DRIVEN) && !defined(CONFIG_UART_ALTERA_JTAG_HAL)
/**
* @brief Fill FIFO with data
* This function is expected to be called from UART interrupt handler (ISR),
* if uart_irq_tx_ready() returns true.
*
* @param dev UART device instance
* @param tx_data Data to transmit
* @param size Number of bytes to send
*
* @return Number of bytes sent
*/
static int uart_altera_jtag_fifo_fill(const struct device *dev,
const uint8_t *tx_data,
int size)
{
const struct uart_altera_jtag_device_config *config = dev->config;
struct uart_altera_jtag_device_data *data = dev->data;
uint32_t ctrl_val;
uint32_t space = 0;
int i;
/* generate fatal error if CONFIG_ASSERT is enabled. */
__ASSERT(tx_data != NULL, "tx buffer is null pointer!");
/* Stop, if buffer is null pointer */
if (tx_data == NULL) {
return 0;
}
k_spinlock_key_t key = k_spin_lock(&data->lock);
ctrl_val = sys_read32(config->base + UART_ALTERA_JTAG_CTRL_OFFSET);
space = (ctrl_val & UART_WFIFO_MASK) >> UART_WFIFO_OFST;
/* guard for tx data overflow:
* make sure that driver is not sending more than current available space.
*/
for (i = 0; (i < size) && (i < space); i++) {
sys_write8(tx_data[i], config->base + UART_ALTERA_JTAG_DATA_OFFSET);
}
k_spin_unlock(&data->lock, key);
return i;
}
/**
* @brief Read data from FIFO
* This function is expected to be called from UART interrupt handler (ISR),
* if uart_irq_rx_ready() returns true.
*
* @param dev UART device instance
* @param rx_data Data container
* @param size Container size
*
* @return Number of bytes read
*/
static int uart_altera_jtag_fifo_read(const struct device *dev, uint8_t *rx_data,
const int size)
{
const struct uart_altera_jtag_device_config *config = dev->config;
struct uart_altera_jtag_device_data *data = dev->data;
int i;
unsigned int input_data;
/* generate fatal error if CONFIG_ASSERT is enabled. */
__ASSERT(rx_data != NULL, "Rx buffer is null pointer!");
/* Stop, if buffer is null pointer */
if (rx_data == NULL) {
return 0;
}
k_spinlock_key_t key = k_spin_lock(&data->lock);
for (i = 0; i < size; i++) {
input_data = sys_read32(config->base + UART_ALTERA_JTAG_DATA_OFFSET);
if (input_data & ALTERA_AVALON_JTAG_UART_DATA_RVALID_MSK) {
rx_data[i] = (input_data & UART_DATA_MASK) >>
ALTERA_AVALON_JTAG_UART_DATA_DATA_OFST;
} else {
/* break upon invalid data or no more data */
break;
}
}
k_spin_unlock(&data->lock, key);
return i;
}
/**
* @brief Enable TX interrupt in IER
*
* @param dev UART device instance
*/
static void uart_altera_jtag_irq_tx_enable(const struct device *dev)
{
struct uart_altera_jtag_device_data *data = dev->data;
const struct uart_altera_jtag_device_config *config = dev->config;
uint32_t ctrl_val = sys_read32(config->base + UART_ALTERA_JTAG_CTRL_OFFSET);
k_spinlock_key_t key = k_spin_lock(&data->lock);
ctrl_val |= UART_IE_TX;
sys_write32(ctrl_val, config->base + UART_ALTERA_JTAG_CTRL_OFFSET);
k_spin_unlock(&data->lock, key);
}
/**
* @brief Disable TX interrupt in IER
*
* @param dev UART device instance
*/
static void uart_altera_jtag_irq_tx_disable(const struct device *dev)
{
struct uart_altera_jtag_device_data *data = dev->data;
const struct uart_altera_jtag_device_config *config = dev->config;
uint32_t ctrl_val = sys_read32(config->base + UART_ALTERA_JTAG_CTRL_OFFSET);
k_spinlock_key_t key = k_spin_lock(&data->lock);
ctrl_val &= ~UART_IE_TX;
sys_write32(ctrl_val, config->base + UART_ALTERA_JTAG_CTRL_OFFSET);
k_spin_unlock(&data->lock, key);
}
/**
* @brief Check if UART TX buffer can accept a new char.
*
* @param dev UART device instance
*
* @return 1 if TX interrupt is enabled and at least one char can be written to UART.
* 0 If device is not ready to write a new byte.
*/
static int uart_altera_jtag_irq_tx_ready(const struct device *dev)
{
struct uart_altera_jtag_device_data *data = dev->data;
const struct uart_altera_jtag_device_config *config = dev->config;
uint32_t ctrl_val = sys_read32(config->base + UART_ALTERA_JTAG_CTRL_OFFSET);
int ret = 0;
uint32_t space = 0;
k_spinlock_key_t key = k_spin_lock(&data->lock);
/* if TX interrupt is enabled */
if (ctrl_val & ALTERA_AVALON_JTAG_UART_CONTROL_WI_MSK) {
/* check for space in tx fifo */
space = (ctrl_val & UART_WFIFO_MASK) >> UART_WFIFO_OFST;
if (space) {
ret = 1;
}
}
k_spin_unlock(&data->lock, key);
return ret;
}
/**
* @brief Check if nothing remains to be transmitted
*
* @param dev UART device instance
*
* @return 1 if nothing remains to be transmitted, 0 otherwise
*/
static int uart_altera_jtag_irq_tx_complete(const struct device *dev)
{
struct uart_altera_jtag_device_data *data = dev->data;
const struct uart_altera_jtag_device_config *config = dev->config;
uint32_t ctrl_val = sys_read32(config->base + UART_ALTERA_JTAG_CTRL_OFFSET);
int ret = 0;
uint32_t space = 0;
k_spinlock_key_t key = k_spin_lock(&data->lock);
/* note: This is checked indirectly via the space in tx fifo. */
space = (ctrl_val & UART_WFIFO_MASK) >> UART_WFIFO_OFST;
if (space == config->write_fifo_depth) {
ret = 1;
}
k_spin_unlock(&data->lock, key);
return ret;
}
/**
* @brief Enable RX interrupt in IER
*
* @param dev UART device instance
*/
static void uart_altera_jtag_irq_rx_enable(const struct device *dev)
{
struct uart_altera_jtag_device_data *data = dev->data;
const struct uart_altera_jtag_device_config *config = dev->config;
uint32_t ctrl_val = sys_read32(config->base + UART_ALTERA_JTAG_CTRL_OFFSET);
k_spinlock_key_t key = k_spin_lock(&data->lock);
ctrl_val |= UART_IE_RX;
sys_write32(ctrl_val, config->base + UART_ALTERA_JTAG_CTRL_OFFSET);
k_spin_unlock(&data->lock, key);
}
/**
* @brief Disable RX interrupt in IER
*
* @param dev UART device instance
*/
static void uart_altera_jtag_irq_rx_disable(const struct device *dev)
{
struct uart_altera_jtag_device_data *data = dev->data;
const struct uart_altera_jtag_device_config *config = dev->config;
uint32_t ctrl_val = sys_read32(config->base + UART_ALTERA_JTAG_CTRL_OFFSET);
k_spinlock_key_t key = k_spin_lock(&data->lock);
ctrl_val &= ~UART_IE_RX;
sys_write32(ctrl_val, config->base + UART_ALTERA_JTAG_CTRL_OFFSET);
k_spin_unlock(&data->lock, key);
}
/**
* @brief Check if Rx IRQ has been raised
*
* @param dev UART device instance
*
* @return 1 if an IRQ is ready, 0 otherwise
*/
static int uart_altera_jtag_irq_rx_ready(const struct device *dev)
{
struct uart_altera_jtag_device_data *data = dev->data;
const struct uart_altera_jtag_device_config *config = dev->config;
uint32_t ctrl_val = sys_read32(config->base + UART_ALTERA_JTAG_CTRL_OFFSET);
int ret = 0;
k_spinlock_key_t key = k_spin_lock(&data->lock);
if (ctrl_val & ALTERA_AVALON_JTAG_UART_CONTROL_RI_MSK) {
ret = 1;
}
k_spin_unlock(&data->lock, key);
return ret;
}
/**
* @brief Update cached contents of IIR
*
* @param dev UART device instance
*
* @return Always 1
*/
static int uart_altera_jtag_irq_update(const struct device *dev)
{
return 1;
}
/**
* @brief Check if any IRQ is pending
*
* @param dev UART device instance
*
* @return 1 if an IRQ is pending, 0 otherwise
*/
static int uart_altera_jtag_irq_is_pending(const struct device *dev)
{
struct uart_altera_jtag_device_data *data = dev->data;
k_spinlock_key_t key = k_spin_lock(&data->lock);
const struct uart_altera_jtag_device_config *config = dev->config;
uint32_t ctrl_val = sys_read32(config->base + UART_ALTERA_JTAG_CTRL_OFFSET);
int ret = 0;
if (ctrl_val &
(ALTERA_AVALON_JTAG_UART_CONTROL_RI_MSK|ALTERA_AVALON_JTAG_UART_CONTROL_WI_MSK)) {
ret = 1;
}
k_spin_unlock(&data->lock, key);
return ret;
}
/**
* @brief Set the callback function pointer for IRQ.
*
* @param dev UART device instance
* @param cb Callback function pointer.
* @param cb_data Data to pass to callback function.
*/
static void uart_altera_jtag_irq_callback_set(const struct device *dev,
uart_irq_callback_user_data_t cb,
void *cb_data)
{
struct uart_altera_jtag_device_data *data = dev->data;
/* generate fatal error if CONFIG_ASSERT is enabled. */
__ASSERT(cb != NULL, "uart_irq_callback_user_data_t cb is null pointer!");
k_spinlock_key_t key = k_spin_lock(&data->lock);
data->cb = cb;
data->cb_data = cb_data;
k_spin_unlock(&data->lock, key);
}
/**
* @brief Interrupt service routine.
*
* This simply calls the callback function, if one exists.
*
* @param dev Pointer to UART device struct
*/
static void uart_altera_jtag_isr(const struct device *dev)
{
struct uart_altera_jtag_device_data *data = dev->data;
uart_irq_callback_user_data_t callback = data->cb;
if (callback) {
callback(dev, data->cb_data);
}
}
#endif /* CONFIG_UART_INTERRUPT_DRIVEN && !CONFIG_UART_ALTERA_JTAG_HAL */
static const struct uart_driver_api uart_altera_jtag_driver_api = {
#ifndef CONFIG_UART_ALTERA_JTAG_HAL
.poll_in = uart_altera_jtag_poll_in,
#endif /* CONFIG_UART_ALTERA_JTAG_HAL */
.poll_out = uart_altera_jtag_poll_out,
.err_check = NULL,
#if defined(CONFIG_UART_INTERRUPT_DRIVEN) && !defined(CONFIG_UART_ALTERA_JTAG_HAL)
.fifo_fill = uart_altera_jtag_fifo_fill,
.fifo_read = uart_altera_jtag_fifo_read,
.irq_tx_enable = uart_altera_jtag_irq_tx_enable,
.irq_tx_disable = uart_altera_jtag_irq_tx_disable,
.irq_tx_ready = uart_altera_jtag_irq_tx_ready,
.irq_tx_complete = uart_altera_jtag_irq_tx_complete,
.irq_rx_enable = uart_altera_jtag_irq_rx_enable,
.irq_rx_disable = uart_altera_jtag_irq_rx_disable,
.irq_rx_ready = uart_altera_jtag_irq_rx_ready,
.irq_is_pending = uart_altera_jtag_irq_is_pending,
.irq_update = uart_altera_jtag_irq_update,
.irq_callback_set = uart_altera_jtag_irq_callback_set,
#endif /* CONFIG_UART_INTERRUPT_DRIVEN && !CONFIG_UART_ALTERA_JTAG_HAL */
};
#ifdef CONFIG_UART_ALTERA_JTAG_HAL
#define UART_ALTERA_JTAG_DEVICE_INIT(n) \
DEVICE_DT_INST_DEFINE(n, uart_altera_jtag_init, NULL, NULL, NULL, PRE_KERNEL_1, \
CONFIG_SERIAL_INIT_PRIORITY, \
&uart_altera_jtag_driver_api);
#else
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
#define UART_ALTERA_JTAG_CONFIG_FUNC(n) \
static void uart_altera_jtag_irq_config_func_##n(const struct device *dev) \
{ \
IRQ_CONNECT(DT_INST_IRQN(n), \
DT_INST_IRQ(n, priority), \
uart_altera_jtag_isr, \
DEVICE_DT_INST_GET(n), 0); \
\
irq_enable(DT_INST_IRQN(n)); \
}
#define UART_ALTERA_JTAG_CONFIG_INIT(n) \
.irq_config_func = uart_altera_jtag_irq_config_func_##n, \
.irq_num = DT_INST_IRQN(n), \
.write_fifo_depth = DT_INST_PROP_OR(n, write_fifo_depth, 0),\
#else
#define UART_ALTERA_JTAG_CONFIG_FUNC(n)
#define UART_ALTERA_JTAG_CONFIG_INIT(n)
#define UART_ALTERA_JTAG_DATA_INIT(n)
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
#define UART_ALTERA_JTAG_DEVICE_INIT(n) \
UART_ALTERA_JTAG_CONFIG_FUNC(n) \
static struct uart_altera_jtag_device_data uart_altera_jtag_device_data_##n = { \
}; \
\
static const struct uart_altera_jtag_device_config uart_altera_jtag_dev_cfg_##n = { \
.base = DT_INST_REG_ADDR(n), \
UART_ALTERA_JTAG_CONFIG_INIT(n) \
}; \
DEVICE_DT_INST_DEFINE(n, \
uart_altera_jtag_init, \
NULL, \
&uart_altera_jtag_device_data_##n, \
&uart_altera_jtag_dev_cfg_##n, \
PRE_KERNEL_1, \
CONFIG_SERIAL_INIT_PRIORITY, \
&uart_altera_jtag_driver_api);
#endif /* CONFIG_UART_ALTERA_JTAG_HAL */
DT_INST_FOREACH_STATUS_OKAY(UART_ALTERA_JTAG_DEVICE_INIT)
|