Lines Matching +full:uart +full:- +full:dev
4 * SPDX-License-Identifier: Apache-2.0
8 * @brief UART driver for Intel FPGA UART Core IP
9 * Reference : Embedded Peripherals IP User Guide : 11. UART Core
20 #include <zephyr/drivers/uart.h>
91 struct uart_config uart_cfg; /* stores uart config from device tree*/
126 static int uart_altera_irq_update(const struct device *dev);
127 static int uart_altera_irq_tx_ready(const struct device *dev);
128 static int uart_altera_irq_rx_ready(const struct device *dev);
134 * This is a non-blocking function.
136 * @param dev UART device instance
139 * @return 0 if a character arrived, -1 if input buffer is empty.
140 * -EINVAL if p_char is null pointer
142 static int uart_altera_poll_in(const struct device *dev, unsigned char *p_char) in uart_altera_poll_in() argument
144 const struct uart_altera_device_config *config = dev->config; in uart_altera_poll_in()
145 struct uart_altera_device_data *data = dev->data; in uart_altera_poll_in()
146 int ret_val = -1; in uart_altera_poll_in()
154 return -EINVAL; in uart_altera_poll_in()
157 k_spinlock_key_t key = k_spin_lock(&data->lock); in uart_altera_poll_in()
160 status = sys_read32(config->base + ALTERA_AVALON_UART_STATUS_REG_OFFSET); in uart_altera_poll_in()
163 *p_char = sys_read32(config->base + ALTERA_AVALON_UART_RXDATA_REG_OFFSET); in uart_altera_poll_in()
167 k_spin_unlock(&data->lock, key); in uart_altera_poll_in()
178 * @param dev UART device instance
181 static void uart_altera_poll_out(const struct device *dev, unsigned char c) in uart_altera_poll_out() argument
183 const struct uart_altera_device_config *config = dev->config; in uart_altera_poll_out()
184 struct uart_altera_device_data *data = dev->data; in uart_altera_poll_out()
187 k_spinlock_key_t key = k_spin_lock(&data->lock); in uart_altera_poll_out()
190 /* wait until uart is free to transmit.*/ in uart_altera_poll_out()
191 status = sys_read32(config->base + ALTERA_AVALON_UART_STATUS_REG_OFFSET); in uart_altera_poll_out()
194 sys_write32(c, config->base + ALTERA_AVALON_UART_TXDATA_REG_OFFSET); in uart_altera_poll_out()
196 k_spin_unlock(&data->lock, key); in uart_altera_poll_out()
204 * @param dev UART device instance
208 static int uart_altera_init(const struct device *dev) in uart_altera_init() argument
211 struct uart_altera_device_data *data = dev->data; in uart_altera_init()
212 const struct uart_altera_device_config *config = dev->config; in uart_altera_init()
214 sys_write32(ALTERA_AVALON_UART_CLEAR_STATUS_VAL, config->base in uart_altera_init()
222 config->irq_config_func(dev); in uart_altera_init()
226 data->control_val = ALTERA_AVALON_UART_CONTROL_DCTS_MSK; in uart_altera_init()
229 sys_write32(data->control_val, config->base + ALTERA_AVALON_UART_CONTROL_REG_OFFSET); in uart_altera_init()
243 * @param dev UART device struct
248 static int uart_altera_err_check(const struct device *dev) in uart_altera_err_check() argument
250 struct uart_altera_device_data *data = dev->data; in uart_altera_err_check()
254 const struct uart_altera_device_config *config = dev->config; in uart_altera_err_check()
255 k_spinlock_key_t key = k_spin_lock(&data->lock); in uart_altera_err_check()
257 data->status_act = sys_read32(config->base + ALTERA_AVALON_UART_STATUS_REG_OFFSET); in uart_altera_err_check()
260 if (data->status_act & ALTERA_AVALON_UART_STATUS_E_MSK) { in uart_altera_err_check()
261 if (data->status_act & ALTERA_AVALON_UART_STATUS_PE_MSK) { in uart_altera_err_check()
265 if (data->status_act & ALTERA_AVALON_UART_STATUS_FE_MSK) { in uart_altera_err_check()
269 if (data->status_act & ALTERA_AVALON_UART_STATUS_BRK_MSK) { in uart_altera_err_check()
273 if (data->status_act & ALTERA_AVALON_UART_STATUS_ROE_MSK) { in uart_altera_err_check()
280 sys_write32(ALTERA_AVALON_UART_CLEAR_STATUS_VAL, config->base in uart_altera_err_check()
282 k_spin_unlock(&data->lock, key); in uart_altera_err_check()
300 if ((cfg_stored->parity == cfg_in->parity) in uart_altera_check_configuration()
301 && (cfg_stored->stop_bits == cfg_in->stop_bits) in uart_altera_check_configuration()
302 && (cfg_stored->data_bits == cfg_in->data_bits) in uart_altera_check_configuration()
303 && (cfg_stored->flow_ctrl == cfg_in->flow_ctrl)) { in uart_altera_check_configuration()
311 * @brief Set UART configuration using data from *cfg_in.
313 * @param dev UART : Device struct
316 * @return 0 if success, -ENOTSUP, if input from cfg_in is not configurable.
317 * -EINVAL if cfg_in is null pointer
319 static int uart_altera_configure(const struct device *dev, in uart_altera_configure() argument
322 const struct uart_altera_device_config *config = dev->config; in uart_altera_configure()
323 struct uart_altera_device_data * const data = dev->data; in uart_altera_configure()
324 struct uart_config * const cfg_stored = &data->uart_cfg; in uart_altera_configure()
333 return -EINVAL; in uart_altera_configure()
338 && !(config->flags & ALT_AVALON_UART_FB)) { in uart_altera_configure()
340 divisor_val = (CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC/cfg_in->baudrate) - 1; in uart_altera_configure()
341 sys_write32(divisor_val, config->base + ALTERA_AVALON_UART_DIVISOR_REG_OFFSET); in uart_altera_configure()
344 cfg_stored->baudrate = cfg_in->baudrate; in uart_altera_configure()
348 ret_val = -ENOTSUP; in uart_altera_configure()
355 * @brief Get UART configuration and stores in *cfg_out.
357 * @param dev UART : Device struct
361 * -EINVAL if cfg_out is null pointer
363 static int uart_altera_config_get(const struct device *dev, in uart_altera_config_get() argument
366 const struct uart_altera_device_data *data = dev->data; in uart_altera_config_get()
373 return -EINVAL; in uart_altera_config_get()
376 *cfg_out = data->uart_cfg; in uart_altera_config_get()
384 * This function is expected to be called from UART interrupt handler (ISR),
388 * @param dev UART device struct
394 static int uart_altera_fifo_fill(const struct device *dev, in uart_altera_fifo_fill() argument
400 const struct uart_altera_device_config *config = dev->config; in uart_altera_fifo_fill()
401 struct uart_altera_device_data *data = dev->data; in uart_altera_fifo_fill()
412 k_spinlock_key_t key = k_spin_lock(&data->lock); in uart_altera_fifo_fill()
414 if (data->status_act & ALTERA_AVALON_UART_STATUS_TRDY_MSK) { in uart_altera_fifo_fill()
415 sys_write32(*tx_data, config->base + ALTERA_AVALON_UART_TXDATA_REG_OFFSET); in uart_altera_fifo_fill()
418 data->status_act = sys_read32(config->base + ALTERA_AVALON_UART_STATUS_REG_OFFSET); in uart_altera_fifo_fill()
425 data->dcts_rising = 0; in uart_altera_fifo_fill()
428 k_spin_unlock(&data->lock, key); in uart_altera_fifo_fill()
435 * This function is expected to be called from UART interrupt handler (ISR),
439 * @param dev UART device struct
445 static int uart_altera_fifo_read(const struct device *dev, uint8_t *rx_data, in uart_altera_fifo_read() argument
450 const struct uart_altera_device_config *config = dev->config; in uart_altera_fifo_read()
451 struct uart_altera_device_data *data = dev->data; in uart_altera_fifo_read()
462 k_spinlock_key_t key = k_spin_lock(&data->lock); in uart_altera_fifo_read()
464 if (data->status_act & ALTERA_AVALON_UART_STATUS_RRDY_MSK) { in uart_altera_fifo_read()
465 *rx_data = sys_read32(config->base + ALTERA_AVALON_UART_RXDATA_REG_OFFSET); in uart_altera_fifo_read()
469 data->status_act = sys_read32(config->base + ALTERA_AVALON_UART_STATUS_REG_OFFSET); in uart_altera_fifo_read()
476 data->status_act = sys_read32(config->base + ALTERA_AVALON_UART_STATUS_REG_OFFSET); in uart_altera_fifo_read()
477 if (((data->status_act & ALTERA_AVALON_UART_STATUS_RRDY_MSK) == 0) in uart_altera_fifo_read()
478 && (data->status_act & ALTERA_AVALON_UART_STATUS_CTS_MSK)) { in uart_altera_fifo_read()
479 data->control_val |= ALTERA_AVALON_UART_CONTROL_RTS_MSK; in uart_altera_fifo_read()
480 sys_write32(data->control_val, config->base in uart_altera_fifo_read()
485 k_spin_unlock(&data->lock, key); in uart_altera_fifo_read()
493 * @param dev UART device struct
495 static void uart_altera_irq_tx_enable(const struct device *dev) in uart_altera_irq_tx_enable() argument
497 struct uart_altera_device_data *data = dev->data; in uart_altera_irq_tx_enable()
498 const struct uart_altera_device_config *config = dev->config; in uart_altera_irq_tx_enable()
500 k_spinlock_key_t key = k_spin_lock(&data->lock); in uart_altera_irq_tx_enable()
502 data->control_val |= ALTERA_AVALON_UART_CONTROL_TRDY_MSK; in uart_altera_irq_tx_enable()
506 data->control_val |= ALTERA_AVALON_UART_CONTROL_RTS_MSK; in uart_altera_irq_tx_enable()
509 sys_write32(data->control_val, config->base + ALTERA_AVALON_UART_CONTROL_REG_OFFSET); in uart_altera_irq_tx_enable()
511 k_spin_unlock(&data->lock, key); in uart_altera_irq_tx_enable()
517 * @param dev UART device struct
519 static void uart_altera_irq_tx_disable(const struct device *dev) in uart_altera_irq_tx_disable() argument
521 struct uart_altera_device_data *data = dev->data; in uart_altera_irq_tx_disable()
522 const struct uart_altera_device_config *config = dev->config; in uart_altera_irq_tx_disable()
524 k_spinlock_key_t key = k_spin_lock(&data->lock); in uart_altera_irq_tx_disable()
526 data->control_val &= ~ALTERA_AVALON_UART_CONTROL_TRDY_MSK; in uart_altera_irq_tx_disable()
530 data->control_val &= ~ALTERA_AVALON_UART_CONTROL_RTS_MSK; in uart_altera_irq_tx_disable()
533 sys_write32(data->control_val, config->base + ALTERA_AVALON_UART_CONTROL_REG_OFFSET); in uart_altera_irq_tx_disable()
535 k_spin_unlock(&data->lock, key); in uart_altera_irq_tx_disable()
539 * @brief Check if UART TX buffer can accept a new char.
541 * @param dev UART device struct
543 * @return 1 if TX interrupt is enabled and at least one char can be written to UART.
546 static int uart_altera_irq_tx_ready(const struct device *dev) in uart_altera_irq_tx_ready() argument
548 struct uart_altera_device_data *data = dev->data; in uart_altera_irq_tx_ready()
551 k_spinlock_key_t key = k_spin_lock(&data->lock); in uart_altera_irq_tx_ready()
554 if (data->control_val & ALTERA_AVALON_UART_CONTROL_TRDY_MSK) { in uart_altera_irq_tx_ready()
556 if (data->status_act & ALTERA_AVALON_UART_STATUS_TMT_MSK) { in uart_altera_irq_tx_ready()
563 if ((data->status_act & ALTERA_AVALON_UART_STATUS_CTS_MSK) == 0) { in uart_altera_irq_tx_ready()
567 if (data->dcts_rising == 0) { in uart_altera_irq_tx_ready()
574 k_spin_unlock(&data->lock, key); in uart_altera_irq_tx_ready()
582 * @param dev UART device struct
586 static int uart_altera_irq_tx_complete(const struct device *dev) in uart_altera_irq_tx_complete() argument
588 struct uart_altera_device_data *data = dev->data; in uart_altera_irq_tx_complete()
591 k_spinlock_key_t key = k_spin_lock(&data->lock); in uart_altera_irq_tx_complete()
593 if (data->status_act & ALTERA_AVALON_UART_STATUS_TMT_MSK) { in uart_altera_irq_tx_complete()
597 k_spin_unlock(&data->lock, key); in uart_altera_irq_tx_complete()
605 * @param dev UART device struct
607 static void uart_altera_irq_rx_enable(const struct device *dev) in uart_altera_irq_rx_enable() argument
609 struct uart_altera_device_data *data = dev->data; in uart_altera_irq_rx_enable()
610 const struct uart_altera_device_config *config = dev->config; in uart_altera_irq_rx_enable()
612 k_spinlock_key_t key = k_spin_lock(&data->lock); in uart_altera_irq_rx_enable()
614 data->control_val |= ALTERA_AVALON_UART_CONTROL_RRDY_MSK; in uart_altera_irq_rx_enable()
615 sys_write32(data->control_val, config->base + ALTERA_AVALON_UART_CONTROL_REG_OFFSET); in uart_altera_irq_rx_enable()
617 k_spin_unlock(&data->lock, key); in uart_altera_irq_rx_enable()
623 * @param dev UART device struct
625 static void uart_altera_irq_rx_disable(const struct device *dev) in uart_altera_irq_rx_disable() argument
627 struct uart_altera_device_data *data = dev->data; in uart_altera_irq_rx_disable()
628 const struct uart_altera_device_config *config = dev->config; in uart_altera_irq_rx_disable()
630 k_spinlock_key_t key = k_spin_lock(&data->lock); in uart_altera_irq_rx_disable()
632 data->control_val &= ~ALTERA_AVALON_UART_CONTROL_RRDY_MSK; in uart_altera_irq_rx_disable()
633 sys_write32(data->control_val, config->base + ALTERA_AVALON_UART_CONTROL_REG_OFFSET); in uart_altera_irq_rx_disable()
635 k_spin_unlock(&data->lock, key); in uart_altera_irq_rx_disable()
641 * @param dev UART device struct
645 static int uart_altera_irq_rx_ready(const struct device *dev) in uart_altera_irq_rx_ready() argument
647 struct uart_altera_device_data *data = dev->data; in uart_altera_irq_rx_ready()
650 k_spinlock_key_t key = k_spin_lock(&data->lock); in uart_altera_irq_rx_ready()
653 if (data->control_val & ALTERA_AVALON_UART_CONTROL_RRDY_MSK) { in uart_altera_irq_rx_ready()
655 if (data->status_act & ALTERA_AVALON_UART_STATUS_RRDY_MSK) { in uart_altera_irq_rx_ready()
660 k_spin_unlock(&data->lock, key); in uart_altera_irq_rx_ready()
668 * @param dev UART device struct
672 static int uart_altera_irq_update(const struct device *dev) in uart_altera_irq_update() argument
674 struct uart_altera_device_data *data = dev->data; in uart_altera_irq_update()
675 const struct uart_altera_device_config *config = dev->config; in uart_altera_irq_update()
677 k_spinlock_key_t key = k_spin_lock(&data->lock); in uart_altera_irq_update()
679 data->status_act = sys_read32(config->base + ALTERA_AVALON_UART_STATUS_REG_OFFSET); in uart_altera_irq_update()
681 k_spin_unlock(&data->lock, key); in uart_altera_irq_update()
689 * @param dev UART device struct
693 static int uart_altera_irq_is_pending(const struct device *dev) in uart_altera_irq_is_pending() argument
695 struct uart_altera_device_data *data = dev->data; in uart_altera_irq_is_pending()
698 k_spinlock_key_t key = k_spin_lock(&data->lock); in uart_altera_irq_is_pending()
700 if (data->status_act & data->control_val & ALTERA_AVALON_UART_PENDING_MASK) { in uart_altera_irq_is_pending()
704 k_spin_unlock(&data->lock, key); in uart_altera_irq_is_pending()
712 * @param dev UART device struct
716 static void uart_altera_irq_callback_set(const struct device *dev, in uart_altera_irq_callback_set() argument
720 struct uart_altera_device_data *data = dev->data; in uart_altera_irq_callback_set()
725 k_spinlock_key_t key = k_spin_lock(&data->lock); in uart_altera_irq_callback_set()
728 if (data->set_eop_cb) { in uart_altera_irq_callback_set()
729 data->cb_eop = cb; in uart_altera_irq_callback_set()
730 data->cb_data_eop = cb_data; in uart_altera_irq_callback_set()
731 data->set_eop_cb = 0; in uart_altera_irq_callback_set()
733 data->cb = cb; in uart_altera_irq_callback_set()
734 data->cb_data = cb_data; in uart_altera_irq_callback_set()
737 data->cb = cb; in uart_altera_irq_callback_set()
738 data->cb_data = cb_data; in uart_altera_irq_callback_set()
741 k_spin_unlock(&data->lock, key); in uart_altera_irq_callback_set()
750 * @param dev Pointer to UART device struct
752 static void uart_altera_dcts_isr(const struct device *dev) in uart_altera_dcts_isr() argument
754 struct uart_altera_device_data *data = dev->data; in uart_altera_dcts_isr()
755 const struct uart_altera_device_config *config = dev->config; in uart_altera_dcts_isr()
757 k_spinlock_key_t key = k_spin_lock(&data->lock); in uart_altera_dcts_isr()
760 if (data->status_act & ALTERA_AVALON_UART_STATUS_CTS_MSK) { in uart_altera_dcts_isr()
762 data->dcts_rising = 1; in uart_altera_dcts_isr()
766 if ((data->status_act & ALTERA_AVALON_UART_STATUS_RRDY_MSK) == 0) { in uart_altera_dcts_isr()
767 /* Assert RTS to inform other UART. */ in uart_altera_dcts_isr()
768 data->control_val |= ALTERA_AVALON_UART_CONTROL_RTS_MSK; in uart_altera_dcts_isr()
769 sys_write32(data->control_val, config->base in uart_altera_dcts_isr()
773 /* other UART deasserts RTS */ in uart_altera_dcts_isr()
774 if (data->status_act & ALTERA_AVALON_UART_STATUS_TMT_MSK) { in uart_altera_dcts_isr()
776 data->control_val &= ~ALTERA_AVALON_UART_CONTROL_RTS_MSK; in uart_altera_dcts_isr()
777 sys_write32(data->control_val, config->base in uart_altera_dcts_isr()
782 k_spin_unlock(&data->lock, key); in uart_altera_dcts_isr()
791 * @param dev Pointer to UART device struct
794 static void uart_altera_isr(const struct device *dev) in uart_altera_isr() argument
796 struct uart_altera_device_data *data = dev->data; in uart_altera_isr()
797 const struct uart_altera_device_config *config = dev->config; in uart_altera_isr()
799 uart_irq_callback_user_data_t callback = data->cb; in uart_altera_isr()
804 data->status_act = sys_read32(config->base + ALTERA_AVALON_UART_STATUS_REG_OFFSET); in uart_altera_isr()
805 if (data->status_act & ALTERA_AVALON_UART_STATUS_RRDY_MSK) { in uart_altera_isr()
806 data->control_val &= ~ALTERA_AVALON_UART_CONTROL_RTS_MSK; in uart_altera_isr()
807 sys_write32(data->control_val, config->base in uart_altera_isr()
813 callback(dev, data->cb_data); in uart_altera_isr()
818 data->status_act = sys_read32(config->base + ALTERA_AVALON_UART_STATUS_REG_OFFSET); in uart_altera_isr()
820 if (data->status_act & ALTERA_AVALON_UART_STATUS_EOP_MSK) { in uart_altera_isr()
821 callback = data->cb_eop; in uart_altera_isr()
823 callback(dev, data->cb_data_eop); in uart_altera_isr()
830 if (data->status_act & ALTERA_AVALON_UART_STATUS_DCTS_MSK) { in uart_altera_isr()
831 uart_altera_dcts_isr(dev); in uart_altera_isr()
836 sys_write32(ALTERA_AVALON_UART_CLEAR_STATUS_VAL, config->base in uart_altera_isr()
844 * @param dev UART device struct
850 static int uart_altera_drv_cmd(const struct device *dev, uint32_t cmd, in uart_altera_drv_cmd() argument
853 struct uart_altera_device_data *data = dev->data; in uart_altera_drv_cmd()
855 const struct uart_altera_device_config *config = dev->config; in uart_altera_drv_cmd()
857 int ret_val = -ENOTSUP; in uart_altera_drv_cmd()
858 k_spinlock_key_t key = k_spin_lock(&data->lock); in uart_altera_drv_cmd()
864 data->control_val |= ALTERA_AVALON_UART_CONTROL_EOP_MSK; in uart_altera_drv_cmd()
865 sys_write32(data->control_val, config->base in uart_altera_drv_cmd()
869 sys_write32((uint8_t) p, config->base + ALTERA_AVALON_UART_EOP_REG_OFFSET); in uart_altera_drv_cmd()
872 * to set data->cb_eop and data->cb_data_eop! in uart_altera_drv_cmd()
874 data->set_eop_cb = 1; in uart_altera_drv_cmd()
880 data->control_val &= ~ALTERA_AVALON_UART_CONTROL_EOP_MSK; in uart_altera_drv_cmd()
881 sys_write32(data->control_val, config->base in uart_altera_drv_cmd()
885 data->cb_eop = NULL; in uart_altera_drv_cmd()
886 data->cb_data_eop = NULL; in uart_altera_drv_cmd()
891 ret_val = -ENOTSUP; in uart_altera_drv_cmd()
895 k_spin_unlock(&data->lock, key); in uart_altera_drv_cmd()
904 static DEVICE_API(uart, uart_altera_driver_api) = {
936 static void uart_altera_irq_config_func_##n(const struct device *dev) \