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 | /*
* Copyright (c) 2023 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <zephyr/device.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/pm/device.h>
#include "sedi_driver_uart.h"
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void uart_sedi_isr(void *arg);
static void uart_sedi_cb(struct device *port);
#endif
#define DT_DRV_COMPAT intel_sedi_uart
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
/* UART IRQ handler declaration. */
#define UART_IRQ_HANDLER_DECL(n) \
static void irq_config_uart_##n(const struct device *dev)
/* Setting configuration function. */
#define UART_CONFIG_IRQ_HANDLER_SET(n) \
.uart_irq_config_func = irq_config_uart_##n
#define UART_IRQ_HANDLER_DEFINE(n) \
static void irq_config_uart_##n(const struct device *dev) \
{ \
ARG_UNUSED(dev); \
IRQ_CONNECT(DT_INST_IRQN(n), \
DT_INST_IRQ(n, priority), uart_sedi_isr, \
DEVICE_DT_GET(DT_NODELABEL(uart##n)), \
DT_INST_IRQ(n, sense)); \
irq_enable(DT_INST_IRQN(n)); \
}
#else /*CONFIG_UART_INTERRUPT_DRIVEN */
#define UART_IRQ_HANDLER_DECL(n)
#define UART_CONFIG_IRQ_HANDLER_SET(n) (0)
#define UART_IRQ_HANDLER_DEFINE(n)
#endif /* !CONFIG_UART_INTERRUPT_DRIVEN */
/* Device init macro for UART instance. As multiple uart instances follow a
* similar definition of data structures differing only in the instance
* number.This macro makes adding instances simpler.
*/
#define UART_SEDI_DEVICE_INIT(n) \
UART_IRQ_HANDLER_DECL(n); \
static K_MUTEX_DEFINE(uart_##n##_mutex); \
static K_SEM_DEFINE(uart_##n##_tx_sem, 1, 1); \
static K_SEM_DEFINE(uart_##n##_rx_sem, 1, 1); \
static K_SEM_DEFINE(uart_##n##_sync_read_sem, 0, 1); \
static const struct uart_sedi_config_info config_info_##n = { \
DEVICE_MMIO_ROM_INIT(DT_DRV_INST(n)), \
.instance = DT_INST_PROP(n, peripheral_id), \
.baud_rate = DT_INST_PROP(n, current_speed), \
.hw_fc = DT_INST_PROP(n, hw_flow_control), \
.line_ctrl = SEDI_UART_LC_8N1, \
.mutex = &uart_##n##_mutex, \
UART_CONFIG_IRQ_HANDLER_SET(n) \
}; \
\
static struct uart_sedi_drv_data drv_data_##n; \
PM_DEVICE_DT_DEFINE(DT_NODELABEL(uart##n), \
uart_sedi_pm_action); \
DEVICE_DT_DEFINE(DT_NODELABEL(uart##n), \
&uart_sedi_init, \
PM_DEVICE_DT_GET(DT_NODELABEL(uart##n)), \
&drv_data_##n, &config_info_##n, \
PRE_KERNEL_1, \
CONFIG_SERIAL_INIT_PRIORITY, &api); \
UART_IRQ_HANDLER_DEFINE(n)
/* Convenient macro to get the controller instance. */
#define GET_CONTROLLER_INSTANCE(dev) \
(((const struct uart_sedi_config_info *) \
dev->config)->instance)
#define GET_MUTEX(dev) \
(((const struct uart_sedi_config_info *) \
dev->config)->mutex)
struct uart_sedi_config_info {
DEVICE_MMIO_ROM;
/* Specifies the uart instance for configuration. */
sedi_uart_t instance;
/* Specifies the baudrate for the uart instance. */
uint32_t baud_rate;
/* Specifies the port line contorl settings */
sedi_uart_lc_t line_ctrl;
struct k_mutex *mutex;
/* Enable / disable hardware flow control for UART. */
bool hw_fc;
/* UART irq configuration function when supporting interrupt
* mode.
*/
uart_irq_config_func_t uart_irq_config_func;
};
static int uart_sedi_init(const struct device *dev);
struct uart_sedi_drv_data {
DEVICE_MMIO_RAM;
uart_irq_callback_user_data_t user_cb;
void *unsol_rx_usr_cb_param;
uint32_t sync_rx_len;
uint32_t sync_rx_status;
void *user_data;
void *usr_rx_buff;
uint32_t usr_rx_size;
uint8_t iir_cache;
uint8_t busy_count;
};
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void uart_busy_set(const struct device *dev)
{
struct uart_sedi_drv_data *context = dev->data;
context->busy_count++;
if (context->busy_count == 1) {
pm_device_busy_set(dev);
}
}
static void uart_busy_clear(const struct device *dev)
{
struct uart_sedi_drv_data *context = dev->data;
context->busy_count--;
if (context->busy_count == 0) {
pm_device_busy_clear(dev);
}
}
#endif
#ifdef CONFIG_PM_DEVICE
#ifndef CONFIG_UART_CONSOLE
static int uart_suspend_device(const struct device *dev)
{
const struct uart_sedi_config_info *config = dev->config;
if (pm_device_is_busy(dev)) {
return -EBUSY;
}
int ret = sedi_uart_set_power(config->instance, SEDI_POWER_SUSPEND);
if (ret != SEDI_DRIVER_OK) {
return -EIO;
}
return 0;
}
static int uart_resume_device_from_suspend(const struct device *dev)
{
const struct uart_sedi_config_info *config = dev->config;
int ret;
ret = sedi_uart_set_power(config->instance, SEDI_POWER_FULL);
if (ret != SEDI_DRIVER_OK) {
return -EIO;
}
return 0;
}
static int uart_sedi_pm_action(const struct device *dev,
enum pm_device_action action)
{
int ret = 0;
switch (action) {
case PM_DEVICE_ACTION_SUSPEND:
ret = uart_suspend_device(dev);
break;
case PM_DEVICE_ACTION_RESUME:
ret = uart_resume_device_from_suspend(dev);
break;
default:
ret = -ENOTSUP;
}
return ret;
}
#else
static int uart_sedi_pm_action(const struct device *dev,
enum pm_device_action action)
{
/* do nothing if using UART print log to avoid clock gating
* pm driver already handled power management for uart.
*/
return 0;
}
#endif /* CONFIG_UART_CONSOLE */
#endif /* CONFIG_PM_DEVICE */
static int uart_sedi_poll_in(const struct device *dev, unsigned char *data)
{
sedi_uart_t instance = GET_CONTROLLER_INSTANCE(dev);
uint32_t status;
int ret = 0;
sedi_uart_get_status(instance, (uint32_t *) &status);
/* In order to check if there is any data to read from UART
* controller we should check if the SEDI_UART_RX_BUSY bit from
* 'status' is not set. This bit is set only if there is any
* pending character to read.
*/
if (!(status & SEDI_UART_RX_BUSY)) {
ret = -1;
} else {
if (sedi_uart_read(instance, data, (uint32_t *)&status)) {
ret = -1;
}
}
return ret;
}
static void uart_sedi_poll_out(const struct device *dev,
unsigned char data)
{
sedi_uart_t instance = GET_CONTROLLER_INSTANCE(dev);
sedi_uart_write(instance, data);
}
#ifdef CONFIG_UART_LINE_CTRL
static int get_xfer_error(int bsp_err)
{
int err;
switch (bsp_err) {
case SEDI_DRIVER_OK:
err = 0;
break;
case SEDI_USART_ERROR_CANCELED:
err = -ECANCELED;
break;
case SEDI_DRIVER_ERROR:
err = -EIO;
break;
case SEDI_DRIVER_ERROR_PARAMETER:
err = -EINVAL;
break;
case SEDI_DRIVER_ERROR_UNSUPPORTED:
err = -ENOTSUP;
break;
default:
err = -EFAULT;
}
return err;
}
#endif /* CONFIG_UART_LINE_CTRL */
static int uart_sedi_err_check(const struct device *dev)
{
sedi_uart_t instance = GET_CONTROLLER_INSTANCE(dev);
uint32_t status;
int ret_status = 0;
sedi_uart_get_status(instance, (uint32_t *const)&status);
if (status & SEDI_UART_RX_OE) {
ret_status = UART_ERROR_OVERRUN;
}
if (status & SEDI_UART_RX_PE) {
ret_status = UART_ERROR_PARITY;
}
if (status & SEDI_UART_RX_FE) {
ret_status = UART_ERROR_FRAMING;
}
if (status & SEDI_UART_RX_BI) {
ret_status = UART_BREAK;
}
return ret_status;
}
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static int uart_sedi_fifo_fill(const struct device *dev, const uint8_t *tx_data,
int size)
{
sedi_uart_t instance = GET_CONTROLLER_INSTANCE(dev);
return sedi_uart_fifo_fill(instance, tx_data, size);
}
static int uart_sedi_fifo_read(const struct device *dev, uint8_t *rx_data,
const int size)
{
sedi_uart_t instance = GET_CONTROLLER_INSTANCE(dev);
return sedi_uart_fifo_read(instance, rx_data, size);
}
static void uart_sedi_irq_tx_enable(const struct device *dev)
{
sedi_uart_t instance = GET_CONTROLLER_INSTANCE(dev);
sedi_uart_irq_tx_enable(instance);
}
static void uart_sedi_irq_tx_disable(const struct device *dev)
{
sedi_uart_t instance = GET_CONTROLLER_INSTANCE(dev);
sedi_uart_irq_tx_disable(instance);
}
static int uart_sedi_irq_tx_ready(const struct device *dev)
{
sedi_uart_t instance = GET_CONTROLLER_INSTANCE(dev);
return sedi_uart_irq_tx_ready(instance);
}
static int uart_sedi_irq_tx_complete(const struct device *dev)
{
sedi_uart_t instance = GET_CONTROLLER_INSTANCE(dev);
return sedi_uart_is_tx_complete(instance);
}
static void uart_sedi_irq_rx_enable(const struct device *dev)
{
sedi_uart_t instance = GET_CONTROLLER_INSTANCE(dev);
uart_busy_set(dev);
sedi_uart_irq_rx_enable(instance);
}
static void uart_sedi_irq_rx_disable(const struct device *dev)
{
sedi_uart_t instance = GET_CONTROLLER_INSTANCE(dev);
sedi_uart_irq_rx_disable(instance);
uart_busy_clear(dev);
}
static int uart_sedi_irq_rx_ready(const struct device *dev)
{
sedi_uart_t instance = GET_CONTROLLER_INSTANCE(dev);
return sedi_uart_is_irq_rx_ready(instance);
}
static void uart_sedi_irq_err_enable(const struct device *dev)
{
sedi_uart_t instance = GET_CONTROLLER_INSTANCE(dev);
sedi_uart_irq_err_enable(instance);
}
static void uart_sedi_irq_err_disable(const struct device *dev)
{
sedi_uart_t instance = GET_CONTROLLER_INSTANCE(dev);
sedi_uart_irq_err_disable(instance);
}
static int uart_sedi_irq_is_pending(const struct device *dev)
{
sedi_uart_t instance = GET_CONTROLLER_INSTANCE(dev);
return sedi_uart_is_irq_pending(instance);
}
static int uart_sedi_irq_update(const struct device *dev)
{
sedi_uart_t instance = GET_CONTROLLER_INSTANCE(dev);
sedi_uart_update_irq_cache(instance);
return 1;
}
static void uart_sedi_irq_callback_set(const struct device *dev,
uart_irq_callback_user_data_t cb,
void *user_data)
{
struct uart_sedi_drv_data *drv_data = dev->data;
drv_data->user_cb = cb;
drv_data->user_data = user_data;
}
static void uart_sedi_isr(void *arg)
{
struct device *dev = arg;
struct uart_sedi_drv_data *drv_data = dev->data;
if (drv_data->user_cb) {
drv_data->user_cb(dev, drv_data->user_data);
} else {
uart_sedi_cb(dev);
}
}
/* Called from generic callback of zephyr , set by set_cb. */
static void uart_sedi_cb(struct device *port)
{
sedi_uart_t instance = GET_CONTROLLER_INSTANCE(port);
sedi_uart_isr_handler(instance);
}
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
#ifdef CONFIG_UART_LINE_CTRL
static int uart_sedi_line_ctrl_set(struct device *dev,
uint32_t ctrl, uint32_t val)
{
sedi_uart_t instance = GET_CONTROLLER_INSTANCE(dev);
sedi_uart_config_t cfg;
uint32_t mask;
int ret;
k_mutex_lock(GET_MUTEX(dev), K_FOREVER);
switch (ctrl) {
case UART_LINE_CTRL_BAUD_RATE:
sedi_uart_get_config(instance, &cfg);
cfg.baud_rate = val;
ret = sedi_uart_set_config(instance, &cfg);
break;
default:
ret = -ENODEV;
}
k_mutex_unlock(GET_MUTEX(dev));
ret = get_xfer_error(ret);
return ret;
}
static int uart_sedi_line_ctrl_get(struct device *dev,
uint32_t ctrl, uint32_t *val)
{
sedi_uart_t instance = GET_CONTROLLER_INSTANCE(dev);
sedi_uart_config_t cfg;
uint32_t mask;
int ret;
k_mutex_lock(GET_MUTEX(dev), K_FOREVER);
switch (ctrl) {
case UART_LINE_CTRL_BAUD_RATE:
ret = sedi_uart_get_config(instance, &cfg);
*val = cfg.baud_rate;
break;
case UART_LINE_CTRL_LOOPBACK:
ret = sedi_uart_get_loopback_mode(instance, (uint32_t *)val);
break;
case UART_LINE_CTRL_AFCE:
ret = sedi_uart_get_config(instance, &cfg);
*val = cfg.hw_fc;
break;
case UART_LINE_CTRL_LINE_STATUS_REPORT_MASK:
mask = 0;
*val = 0;
ret = sedi_get_ln_status_report_mask(instance,
(uint32_t *)&mask);
*val |= ((mask & SEDI_UART_RX_OE) ? UART_ERROR_OVERRUN : 0);
*val |= ((mask & SEDI_UART_RX_PE) ? UART_ERROR_PARITY : 0);
*val |= ((mask & SEDI_UART_RX_FE) ? UART_ERROR_FRAMING : 0);
*val |= ((mask & SEDI_UART_RX_BI) ? UART_BREAK : 0);
break;
case UART_LINE_CTRL_RTS:
ret = sedi_uart_read_rts(instance, (uint32_t *)val);
break;
case UART_LINE_CTRL_CTS:
ret = sedi_uart_read_cts(instance, (uint32_t *)val);
break;
default:
ret = -ENODEV;
}
k_mutex_unlock(GET_MUTEX(dev));
ret = get_xfer_error(ret);
return ret;
}
#endif /* CONFIG_UART_LINE_CTRL */
static const struct uart_driver_api api = {
.poll_in = uart_sedi_poll_in,
.poll_out = uart_sedi_poll_out,
.err_check = uart_sedi_err_check,
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
.fifo_fill = uart_sedi_fifo_fill,
.fifo_read = uart_sedi_fifo_read,
.irq_tx_enable = uart_sedi_irq_tx_enable,
.irq_tx_disable = uart_sedi_irq_tx_disable,
.irq_tx_ready = uart_sedi_irq_tx_ready,
.irq_tx_complete = uart_sedi_irq_tx_complete,
.irq_rx_enable = uart_sedi_irq_rx_enable,
.irq_rx_disable = uart_sedi_irq_rx_disable,
.irq_rx_ready = uart_sedi_irq_rx_ready,
.irq_err_enable = uart_sedi_irq_err_enable,
.irq_err_disable = uart_sedi_irq_err_disable,
.irq_is_pending = uart_sedi_irq_is_pending,
.irq_update = uart_sedi_irq_update,
.irq_callback_set = uart_sedi_irq_callback_set,
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
#ifdef CONFIG_UART_LINE_CTRL
.line_ctrl_set = uart_sedi_line_ctrl_set,
.line_ctrl_get = uart_sedi_line_ctrl_get,
#endif /* CONFIG_UART_LINE_CTRL */
};
static int uart_sedi_init(const struct device *dev)
{
const struct uart_sedi_config_info *config = dev->config;
sedi_uart_config_t cfg;
DEVICE_MMIO_MAP(dev, K_MEM_CACHE_NONE);
sedi_uart_init(config->instance, (void *)DEVICE_MMIO_GET(dev));
cfg.line_control = config->line_ctrl;
cfg.baud_rate = config->baud_rate;
cfg.hw_fc = config->hw_fc;
/* Setting to full power and enabling clk. */
sedi_uart_set_power(config->instance, SEDI_POWER_FULL);
sedi_uart_set_config(config->instance, &cfg);
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
config->uart_irq_config_func(dev);
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
return 0;
}
DT_INST_FOREACH_STATUS_OKAY(UART_SEDI_DEVICE_INIT)
|