1 /*
2 * Copyright (c) 2010, 2012-2015 Wind River Systems, Inc.
3 * Copyright (c) 2020 Intel Corp.
4 * Copyright (c) 2021 Microchip Technology Inc.
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 /**
10 * @brief Microchip XEC UART Serial Driver
11 *
12 * This is the driver for the Microchip XEC MCU UART. It is NS16550 compatible.
13 *
14 */
15
16 #define DT_DRV_COMPAT microchip_xec_uart
17
18 #include <errno.h>
19 #include <zephyr/kernel.h>
20 #include <zephyr/arch/cpu.h>
21 #include <zephyr/types.h>
22 #include <soc.h>
23
24 #include <zephyr/init.h>
25 #include <zephyr/toolchain.h>
26 #include <zephyr/linker/sections.h>
27 #ifdef CONFIG_SOC_SERIES_MEC172X
28 #include <zephyr/drivers/clock_control/mchp_xec_clock_control.h>
29 #include <zephyr/drivers/interrupt_controller/intc_mchp_xec_ecia.h>
30 #endif
31 #include <zephyr/drivers/pinctrl.h>
32 #include <zephyr/drivers/uart.h>
33 #include <zephyr/drivers/gpio.h>
34 #include <zephyr/sys/sys_io.h>
35 #include <zephyr/spinlock.h>
36 #include <zephyr/irq.h>
37 #include <zephyr/pm/device.h>
38 #include <zephyr/pm/policy.h>
39
40 #include <zephyr/logging/log.h>
41 LOG_MODULE_REGISTER(uart_xec, CONFIG_UART_LOG_LEVEL);
42
43 /* Clock source is 1.8432 MHz derived from PLL 48 MHz */
44 #define XEC_UART_CLK_SRC_1P8M 0
45 /* Clock source is PLL 48 MHz output */
46 #define XEC_UART_CLK_SRC_48M 1
47 /* Clock source is the UART_CLK alternate pin function. */
48 #define XEC_UART_CLK_SRC_EXT_PIN 2
49
50 /* register definitions */
51
52 #define REG_THR 0x00 /* Transmitter holding reg. */
53 #define REG_RDR 0x00 /* Receiver data reg. */
54 #define REG_BRDL 0x00 /* Baud rate divisor (LSB) */
55 #define REG_BRDH 0x01 /* Baud rate divisor (MSB) */
56 #define REG_IER 0x01 /* Interrupt enable reg. */
57 #define REG_IIR 0x02 /* Interrupt ID reg. */
58 #define REG_FCR 0x02 /* FIFO control reg. */
59 #define REG_LCR 0x03 /* Line control reg. */
60 #define REG_MDC 0x04 /* Modem control reg. */
61 #define REG_LSR 0x05 /* Line status reg. */
62 #define REG_MSR 0x06 /* Modem status reg. */
63 #define REG_SCR 0x07 /* scratch register */
64 #define REG_LD_ACTV 0x330 /* Logical Device activate */
65 #define REG_LD_CFG 0x3f0 /* Logical Device configuration */
66
67 /* equates for interrupt enable register */
68
69 #define IER_RXRDY 0x01 /* receiver data ready */
70 #define IER_TBE 0x02 /* transmit bit enable */
71 #define IER_LSR 0x04 /* line status interrupts */
72 #define IER_MSI 0x08 /* modem status interrupts */
73
74 /* equates for interrupt identification register */
75
76 #define IIR_MSTAT 0x00 /* modem status interrupt */
77 #define IIR_NIP 0x01 /* no interrupt pending */
78 #define IIR_THRE 0x02 /* transmit holding register empty interrupt */
79 #define IIR_RBRF 0x04 /* receiver buffer register full interrupt */
80 #define IIR_LS 0x06 /* receiver line status interrupt */
81 #define IIR_MASK 0x07 /* interrupt id bits mask */
82 #define IIR_ID 0x06 /* interrupt ID mask without NIP */
83
84 /* equates for FIFO control register */
85
86 #define FCR_FIFO 0x01 /* enable XMIT and RCVR FIFO */
87 #define FCR_RCVRCLR 0x02 /* clear RCVR FIFO */
88 #define FCR_XMITCLR 0x04 /* clear XMIT FIFO */
89
90 /*
91 * Per PC16550D (Literature Number: SNLS378B):
92 *
93 * RXRDY, Mode 0: When in the 16450 Mode (FCR0 = 0) or in
94 * the FIFO Mode (FCR0 = 1, FCR3 = 0) and there is at least 1
95 * character in the RCVR FIFO or RCVR holding register, the
96 * RXRDY pin (29) will be low active. Once it is activated the
97 * RXRDY pin will go inactive when there are no more charac-
98 * ters in the FIFO or holding register.
99 *
100 * RXRDY, Mode 1: In the FIFO Mode (FCR0 = 1) when the
101 * FCR3 = 1 and the trigger level or the timeout has been
102 * reached, the RXRDY pin will go low active. Once it is acti-
103 * vated it will go inactive when there are no more characters
104 * in the FIFO or holding register.
105 *
106 * TXRDY, Mode 0: In the 16450 Mode (FCR0 = 0) or in the
107 * FIFO Mode (FCR0 = 1, FCR3 = 0) and there are no charac-
108 * ters in the XMIT FIFO or XMIT holding register, the TXRDY
109 * pin (24) will be low active. Once it is activated the TXRDY
110 * pin will go inactive after the first character is loaded into the
111 * XMIT FIFO or holding register.
112 *
113 * TXRDY, Mode 1: In the FIFO Mode (FCR0 = 1) when
114 * FCR3 = 1 and there are no characters in the XMIT FIFO, the
115 * TXRDY pin will go low active. This pin will become inactive
116 * when the XMIT FIFO is completely full.
117 */
118 #define FCR_MODE0 0x00 /* set receiver in mode 0 */
119 #define FCR_MODE1 0x08 /* set receiver in mode 1 */
120
121 /* RCVR FIFO interrupt levels: trigger interrupt with this bytes in FIFO */
122 #define FCR_FIFO_1 0x00 /* 1 byte in RCVR FIFO */
123 #define FCR_FIFO_4 0x40 /* 4 bytes in RCVR FIFO */
124 #define FCR_FIFO_8 0x80 /* 8 bytes in RCVR FIFO */
125 #define FCR_FIFO_14 0xC0 /* 14 bytes in RCVR FIFO */
126
127 /* constants for line control register */
128
129 #define LCR_CS5 0x00 /* 5 bits data size */
130 #define LCR_CS6 0x01 /* 6 bits data size */
131 #define LCR_CS7 0x02 /* 7 bits data size */
132 #define LCR_CS8 0x03 /* 8 bits data size */
133 #define LCR_2_STB 0x04 /* 2 stop bits */
134 #define LCR_1_STB 0x00 /* 1 stop bit */
135 #define LCR_PEN 0x08 /* parity enable */
136 #define LCR_PDIS 0x00 /* parity disable */
137 #define LCR_EPS 0x10 /* even parity select */
138 #define LCR_SP 0x20 /* stick parity select */
139 #define LCR_SBRK 0x40 /* break control bit */
140 #define LCR_DLAB 0x80 /* divisor latch access enable */
141
142 /* constants for the modem control register */
143
144 #define MCR_DTR 0x01 /* dtr output */
145 #define MCR_RTS 0x02 /* rts output */
146 #define MCR_OUT1 0x04 /* output #1 */
147 #define MCR_OUT2 0x08 /* output #2 */
148 #define MCR_LOOP 0x10 /* loop back */
149 #define MCR_AFCE 0x20 /* auto flow control enable */
150
151 /* constants for line status register */
152
153 #define LSR_RXRDY 0x01 /* receiver data available */
154 #define LSR_OE 0x02 /* overrun error */
155 #define LSR_PE 0x04 /* parity error */
156 #define LSR_FE 0x08 /* framing error */
157 #define LSR_BI 0x10 /* break interrupt */
158 #define LSR_EOB_MASK 0x1E /* Error or Break mask */
159 #define LSR_THRE 0x20 /* transmit holding register empty */
160 #define LSR_TEMT 0x40 /* transmitter empty */
161
162 /* constants for modem status register */
163
164 #define MSR_DCTS 0x01 /* cts change */
165 #define MSR_DDSR 0x02 /* dsr change */
166 #define MSR_DRI 0x04 /* ring change */
167 #define MSR_DDCD 0x08 /* data carrier change */
168 #define MSR_CTS 0x10 /* complement of cts */
169 #define MSR_DSR 0x20 /* complement of dsr */
170 #define MSR_RI 0x40 /* complement of ring signal */
171 #define MSR_DCD 0x80 /* complement of dcd */
172
173 #define IIRC(dev) (((struct uart_xec_dev_data *)(dev)->data)->iir_cache)
174
175 enum uart_xec_pm_policy_state_flag {
176 UART_XEC_PM_POLICY_STATE_TX_FLAG,
177 UART_XEC_PM_POLICY_STATE_RX_FLAG,
178 UART_XEC_PM_POLICY_STATE_FLAG_COUNT,
179 };
180
181 /* device config */
182 struct uart_xec_device_config {
183 struct uart_regs *regs;
184 uint32_t sys_clk_freq;
185 uint8_t girq_id;
186 uint8_t girq_pos;
187 uint8_t pcr_idx;
188 uint8_t pcr_bitpos;
189 const struct pinctrl_dev_config *pcfg;
190 #if defined(CONFIG_UART_INTERRUPT_DRIVEN) || defined(CONFIG_UART_ASYNC_API)
191 uart_irq_config_func_t irq_config_func;
192 #endif
193 #ifdef CONFIG_PM_DEVICE
194 struct gpio_dt_spec wakerx_gpio;
195 bool wakeup_source;
196 #endif
197 };
198
199 /** Device data structure */
200 struct uart_xec_dev_data {
201 struct uart_config uart_config;
202 struct k_spinlock lock;
203
204 uint8_t fcr_cache; /**< cache of FCR write only register */
205 uint8_t iir_cache; /**< cache of IIR since it clears when read */
206 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
207 uart_irq_callback_user_data_t cb; /**< Callback function pointer */
208 void *cb_data; /**< Callback function arg */
209 #endif
210 };
211
212 #ifdef CONFIG_PM_DEVICE
213 ATOMIC_DEFINE(pm_policy_state_flag, UART_XEC_PM_POLICY_STATE_FLAG_COUNT);
214 #endif
215
216 #if defined(CONFIG_PM_DEVICE) && defined(CONFIG_UART_CONSOLE_INPUT_EXPIRED)
217 struct k_work_delayable rx_refresh_timeout_work;
218 #endif
219
220 static const struct uart_driver_api uart_xec_driver_api;
221
222 #if defined(CONFIG_PM_DEVICE) && defined(CONFIG_UART_CONSOLE_INPUT_EXPIRED)
uart_xec_pm_policy_state_lock_get(enum uart_xec_pm_policy_state_flag flag)223 static void uart_xec_pm_policy_state_lock_get(enum uart_xec_pm_policy_state_flag flag)
224 {
225 if (atomic_test_and_set_bit(pm_policy_state_flag, flag) == 0) {
226 pm_policy_state_lock_get(PM_STATE_SUSPEND_TO_IDLE, PM_ALL_SUBSTATES);
227 }
228 }
229
uart_xec_pm_policy_state_lock_put(enum uart_xec_pm_policy_state_flag flag)230 static void uart_xec_pm_policy_state_lock_put(enum uart_xec_pm_policy_state_flag flag)
231 {
232 if (atomic_test_and_clear_bit(pm_policy_state_flag, flag) == 1) {
233 pm_policy_state_lock_put(PM_STATE_SUSPEND_TO_IDLE, PM_ALL_SUBSTATES);
234 }
235 }
236 #endif
237
238 #ifdef CONFIG_SOC_SERIES_MEC172X
239
uart_clr_slp_en(const struct device * dev)240 static void uart_clr_slp_en(const struct device *dev)
241 {
242 struct uart_xec_device_config const *dev_cfg = dev->config;
243
244 z_mchp_xec_pcr_periph_sleep(dev_cfg->pcr_idx, dev_cfg->pcr_bitpos, 0);
245 }
246
uart_xec_girq_clr(const struct device * dev)247 static inline void uart_xec_girq_clr(const struct device *dev)
248 {
249 struct uart_xec_device_config const *dev_cfg = dev->config;
250
251 mchp_soc_ecia_girq_src_clr(dev_cfg->girq_id, dev_cfg->girq_pos);
252 }
253
uart_xec_girq_en(uint8_t girq_idx,uint8_t girq_posn)254 static inline void uart_xec_girq_en(uint8_t girq_idx, uint8_t girq_posn)
255 {
256 mchp_xec_ecia_girq_src_en(girq_idx, girq_posn);
257 }
258
259 #else
260
uart_clr_slp_en(const struct device * dev)261 static void uart_clr_slp_en(const struct device *dev)
262 {
263 struct uart_xec_device_config const *dev_cfg = dev->config;
264
265 if (dev_cfg->pcr_bitpos == MCHP_PCR2_UART0_POS) {
266 mchp_pcr_periph_slp_ctrl(PCR_UART0, 0);
267 } else if (dev_cfg->pcr_bitpos == MCHP_PCR2_UART1_POS) {
268 mchp_pcr_periph_slp_ctrl(PCR_UART1, 0);
269 } else {
270 mchp_pcr_periph_slp_ctrl(PCR_UART2, 0);
271 }
272 }
273
uart_xec_girq_clr(const struct device * dev)274 static inline void uart_xec_girq_clr(const struct device *dev)
275 {
276 struct uart_xec_device_config const *dev_cfg = dev->config;
277
278 MCHP_GIRQ_SRC(dev_cfg->girq_id) = BIT(dev_cfg->girq_pos);
279 }
280
uart_xec_girq_en(uint8_t girq_idx,uint8_t girq_posn)281 static inline void uart_xec_girq_en(uint8_t girq_idx, uint8_t girq_posn)
282 {
283 MCHP_GIRQ_ENSET(girq_idx) = BIT(girq_posn);
284 }
285
286 #endif
287
set_baud_rate(const struct device * dev,uint32_t baud_rate)288 static void set_baud_rate(const struct device *dev, uint32_t baud_rate)
289 {
290 const struct uart_xec_device_config * const dev_cfg = dev->config;
291 struct uart_xec_dev_data * const dev_data = dev->data;
292 struct uart_regs *regs = dev_cfg->regs;
293 uint32_t divisor; /* baud rate divisor */
294 uint8_t lcr_cache;
295
296 if ((baud_rate != 0U) && (dev_cfg->sys_clk_freq != 0U)) {
297 /*
298 * calculate baud rate divisor. a variant of
299 * (uint32_t)(dev_cfg->sys_clk_freq / (16.0 * baud_rate) + 0.5)
300 */
301 divisor = ((dev_cfg->sys_clk_freq + (baud_rate << 3))
302 / baud_rate) >> 4;
303
304 /* set the DLAB to access the baud rate divisor registers */
305 lcr_cache = regs->LCR;
306 regs->LCR = LCR_DLAB | lcr_cache;
307 regs->RTXB = (unsigned char)(divisor & 0xff);
308 /* bit[7]=0 1.8MHz clock source, =1 48MHz clock source */
309 regs->IER = (unsigned char)((divisor >> 8) & 0x7f);
310
311 /* restore the DLAB to access the baud rate divisor registers */
312 regs->LCR = lcr_cache;
313
314 dev_data->uart_config.baudrate = baud_rate;
315 }
316 }
317
318 /*
319 * Configure UART.
320 * MCHP XEC UART defaults to reset if external Host VCC_PWRGD is inactive.
321 * We must change the UART reset signal to XEC VTR_PWRGD. Make sure UART
322 * clock source is an internal clock and UART pins are not inverted.
323 */
uart_xec_configure(const struct device * dev,const struct uart_config * cfg)324 static int uart_xec_configure(const struct device *dev,
325 const struct uart_config *cfg)
326 {
327 struct uart_xec_dev_data * const dev_data = dev->data;
328 const struct uart_xec_device_config * const dev_cfg = dev->config;
329 struct uart_regs *regs = dev_cfg->regs;
330 uint8_t lcr_cache;
331
332 /* temp for return value if error occurs in this locked region */
333 int ret = 0;
334
335 k_spinlock_key_t key = k_spin_lock(&dev_data->lock);
336
337 ARG_UNUSED(dev_data);
338
339 dev_data->fcr_cache = 0U;
340 dev_data->iir_cache = 0U;
341
342 /* XEC UART specific configuration and enable */
343 regs->CFG_SEL &= ~(MCHP_UART_LD_CFG_RESET_VCC |
344 MCHP_UART_LD_CFG_EXTCLK | MCHP_UART_LD_CFG_INVERT);
345 /* set activate to enable clocks */
346 regs->ACTV |= MCHP_UART_LD_ACTIVATE;
347
348 set_baud_rate(dev, cfg->baudrate);
349
350 /* Local structure to hold temporary values */
351 struct uart_config uart_cfg;
352
353 switch (cfg->data_bits) {
354 case UART_CFG_DATA_BITS_5:
355 uart_cfg.data_bits = LCR_CS5;
356 break;
357 case UART_CFG_DATA_BITS_6:
358 uart_cfg.data_bits = LCR_CS6;
359 break;
360 case UART_CFG_DATA_BITS_7:
361 uart_cfg.data_bits = LCR_CS7;
362 break;
363 case UART_CFG_DATA_BITS_8:
364 uart_cfg.data_bits = LCR_CS8;
365 break;
366 default:
367 ret = -ENOTSUP;
368 goto out;
369 }
370
371 switch (cfg->stop_bits) {
372 case UART_CFG_STOP_BITS_1:
373 uart_cfg.stop_bits = LCR_1_STB;
374 break;
375 case UART_CFG_STOP_BITS_2:
376 uart_cfg.stop_bits = LCR_2_STB;
377 break;
378 default:
379 ret = -ENOTSUP;
380 goto out;
381 }
382
383 switch (cfg->parity) {
384 case UART_CFG_PARITY_NONE:
385 uart_cfg.parity = LCR_PDIS;
386 break;
387 case UART_CFG_PARITY_EVEN:
388 uart_cfg.parity = LCR_EPS;
389 break;
390 default:
391 ret = -ENOTSUP;
392 goto out;
393 }
394
395 dev_data->uart_config = *cfg;
396
397 /* data bits, stop bits, parity, clear DLAB */
398 regs->LCR = uart_cfg.data_bits | uart_cfg.stop_bits | uart_cfg.parity;
399
400 regs->MCR = MCR_OUT2 | MCR_RTS | MCR_DTR;
401
402 /*
403 * Program FIFO: enabled, mode 0
404 * generate the interrupt at 8th byte
405 * Clear TX and RX FIFO
406 */
407 dev_data->fcr_cache = FCR_FIFO | FCR_MODE0 | FCR_FIFO_8 | FCR_RCVRCLR |
408 FCR_XMITCLR;
409 regs->IIR_FCR = dev_data->fcr_cache;
410
411 /* clear the port */
412 lcr_cache = regs->LCR;
413 regs->LCR = LCR_DLAB | lcr_cache;
414 regs->SCR = regs->RTXB;
415 regs->LCR = lcr_cache;
416
417 /* disable interrupts */
418 regs->IER = 0;
419
420 out:
421 k_spin_unlock(&dev_data->lock, key);
422 return ret;
423 };
424
425 #ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
uart_xec_config_get(const struct device * dev,struct uart_config * cfg)426 static int uart_xec_config_get(const struct device *dev,
427 struct uart_config *cfg)
428 {
429 struct uart_xec_dev_data *data = dev->data;
430
431 cfg->baudrate = data->uart_config.baudrate;
432 cfg->parity = data->uart_config.parity;
433 cfg->stop_bits = data->uart_config.stop_bits;
434 cfg->data_bits = data->uart_config.data_bits;
435 cfg->flow_ctrl = data->uart_config.flow_ctrl;
436
437 return 0;
438 }
439 #endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */
440
441 #ifdef CONFIG_PM_DEVICE
442
uart_xec_wake_handler(const struct device * gpio,struct gpio_callback * cb,uint32_t pins)443 static void uart_xec_wake_handler(const struct device *gpio, struct gpio_callback *cb,
444 uint32_t pins)
445 {
446 /* Disable interrupts on UART RX pin to avoid repeated interrupts. */
447 (void)gpio_pin_interrupt_configure(gpio, (find_msb_set(pins) - 1),
448 GPIO_INT_DISABLE);
449 /* Refresh console expired time */
450 #ifdef CONFIG_UART_CONSOLE_INPUT_EXPIRED
451 k_timeout_t delay = K_MSEC(CONFIG_UART_CONSOLE_INPUT_EXPIRED_TIMEOUT);
452
453 uart_xec_pm_policy_state_lock_get(UART_XEC_PM_POLICY_STATE_RX_FLAG);
454 k_work_reschedule(&rx_refresh_timeout_work, delay);
455 #endif
456 }
457
uart_xec_pm_action(const struct device * dev,enum pm_device_action action)458 static int uart_xec_pm_action(const struct device *dev,
459 enum pm_device_action action)
460 {
461 const struct uart_xec_device_config * const dev_cfg = dev->config;
462 struct uart_regs *regs = dev_cfg->regs;
463 int ret = 0;
464
465 switch (action) {
466 case PM_DEVICE_ACTION_RESUME:
467 regs->ACTV = MCHP_UART_LD_ACTIVATE;
468 break;
469 case PM_DEVICE_ACTION_SUSPEND:
470 /* Enable UART wake interrupt */
471 regs->ACTV = 0;
472 if ((dev_cfg->wakeup_source) && (dev_cfg->wakerx_gpio.port != NULL)) {
473 ret = gpio_pin_interrupt_configure_dt(&dev_cfg->wakerx_gpio,
474 GPIO_INT_MODE_EDGE | GPIO_INT_TRIG_LOW);
475 if (ret < 0) {
476 LOG_ERR("Failed to configure UART wake interrupt (ret %d)", ret);
477 return ret;
478 }
479 }
480 break;
481 default:
482 return -ENOTSUP;
483 }
484
485 return 0;
486 }
487
488 #ifdef CONFIG_UART_CONSOLE_INPUT_EXPIRED
uart_xec_rx_refresh_timeout(struct k_work * work)489 static void uart_xec_rx_refresh_timeout(struct k_work *work)
490 {
491 ARG_UNUSED(work);
492
493 uart_xec_pm_policy_state_lock_put(UART_XEC_PM_POLICY_STATE_RX_FLAG);
494 }
495 #endif
496 #endif /* CONFIG_PM_DEVICE */
497
498 /**
499 * @brief Initialize individual UART port
500 *
501 * This routine is called to reset the chip in a quiescent state.
502 *
503 * @param dev UART device struct
504 *
505 * @return 0 if successful, failed otherwise
506 */
uart_xec_init(const struct device * dev)507 static int uart_xec_init(const struct device *dev)
508 {
509 const struct uart_xec_device_config * const dev_cfg = dev->config;
510 struct uart_xec_dev_data *dev_data = dev->data;
511 int ret;
512
513 uart_clr_slp_en(dev);
514
515 ret = pinctrl_apply_state(dev_cfg->pcfg, PINCTRL_STATE_DEFAULT);
516 if (ret != 0) {
517 return ret;
518 }
519
520 ret = uart_xec_configure(dev, &dev_data->uart_config);
521 if (ret != 0) {
522 return ret;
523 }
524
525 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
526 dev_cfg->irq_config_func(dev);
527 #endif
528
529 #ifdef CONFIG_PM_DEVICE
530 #ifdef CONFIG_UART_CONSOLE_INPUT_EXPIRED
531 k_work_init_delayable(&rx_refresh_timeout_work, uart_xec_rx_refresh_timeout);
532 #endif
533 if ((dev_cfg->wakeup_source) && (dev_cfg->wakerx_gpio.port != NULL)) {
534 static struct gpio_callback uart_xec_wake_cb;
535
536 gpio_init_callback(&uart_xec_wake_cb, uart_xec_wake_handler,
537 BIT(dev_cfg->wakerx_gpio.pin));
538
539 ret = gpio_add_callback(dev_cfg->wakerx_gpio.port, &uart_xec_wake_cb);
540 if (ret < 0) {
541 LOG_ERR("Failed to add UART wake callback (err %d)", ret);
542 return ret;
543 }
544 }
545 #endif
546
547 return 0;
548 }
549
550 /**
551 * @brief Poll the device for input.
552 *
553 * @param dev UART device struct
554 * @param c Pointer to character
555 *
556 * @return 0 if a character arrived, -1 if the input buffer if empty.
557 */
uart_xec_poll_in(const struct device * dev,unsigned char * c)558 static int uart_xec_poll_in(const struct device *dev, unsigned char *c)
559 {
560 const struct uart_xec_device_config * const dev_cfg = dev->config;
561 struct uart_xec_dev_data *dev_data = dev->data;
562 struct uart_regs *regs = dev_cfg->regs;
563 int ret = -1;
564 k_spinlock_key_t key = k_spin_lock(&dev_data->lock);
565
566 if ((regs->LSR & LSR_RXRDY) != 0) {
567 /* got a character */
568 *c = regs->RTXB;
569 ret = 0;
570 }
571
572 k_spin_unlock(&dev_data->lock, key);
573
574 return ret;
575 }
576
577 /**
578 * @brief Output a character in polled mode.
579 *
580 * Checks if the transmitter is empty. If empty, a character is written to
581 * the data register.
582 *
583 * If the hardware flow control is enabled then the handshake signal CTS has to
584 * be asserted in order to send a character.
585 *
586 * @param dev UART device struct
587 * @param c Character to send
588 */
uart_xec_poll_out(const struct device * dev,unsigned char c)589 static void uart_xec_poll_out(const struct device *dev, unsigned char c)
590 {
591 const struct uart_xec_device_config * const dev_cfg = dev->config;
592 struct uart_xec_dev_data *dev_data = dev->data;
593 struct uart_regs *regs = dev_cfg->regs;
594 k_spinlock_key_t key = k_spin_lock(&dev_data->lock);
595
596 while ((regs->LSR & LSR_THRE) == 0) {
597 ;
598 }
599
600 regs->RTXB = c;
601
602 k_spin_unlock(&dev_data->lock, key);
603 }
604
605 /**
606 * @brief Check if an error was received
607 *
608 * @param dev UART device struct
609 *
610 * @return one of UART_ERROR_OVERRUN, UART_ERROR_PARITY, UART_ERROR_FRAMING,
611 * UART_BREAK if an error was detected, 0 otherwise.
612 */
uart_xec_err_check(const struct device * dev)613 static int uart_xec_err_check(const struct device *dev)
614 {
615 const struct uart_xec_device_config * const dev_cfg = dev->config;
616 struct uart_xec_dev_data *dev_data = dev->data;
617 struct uart_regs *regs = dev_cfg->regs;
618 k_spinlock_key_t key = k_spin_lock(&dev_data->lock);
619 int check = regs->LSR & LSR_EOB_MASK;
620
621 k_spin_unlock(&dev_data->lock, key);
622
623 return check >> 1;
624 }
625
626 #if CONFIG_UART_INTERRUPT_DRIVEN
627
628 /**
629 * @brief Fill FIFO with data
630 *
631 * @param dev UART device struct
632 * @param tx_data Data to transmit
633 * @param size Number of bytes to send
634 *
635 * @return Number of bytes sent
636 */
uart_xec_fifo_fill(const struct device * dev,const uint8_t * tx_data,int size)637 static int uart_xec_fifo_fill(const struct device *dev, const uint8_t *tx_data,
638 int size)
639 {
640 const struct uart_xec_device_config * const dev_cfg = dev->config;
641 struct uart_xec_dev_data *dev_data = dev->data;
642 struct uart_regs *regs = dev_cfg->regs;
643 int i;
644 k_spinlock_key_t key = k_spin_lock(&dev_data->lock);
645
646 for (i = 0; (i < size) && (regs->LSR & LSR_THRE) != 0; i++) {
647 #if defined(CONFIG_PM_DEVICE) && defined(CONFIG_UART_CONSOLE_INPUT_EXPIRED)
648 uart_xec_pm_policy_state_lock_get(UART_XEC_PM_POLICY_STATE_TX_FLAG);
649 #endif
650 regs->RTXB = tx_data[i];
651 }
652
653 k_spin_unlock(&dev_data->lock, key);
654
655 return i;
656 }
657
658 /**
659 * @brief Read data from FIFO
660 *
661 * @param dev UART device struct
662 * @param rxData Data container
663 * @param size Container size
664 *
665 * @return Number of bytes read
666 */
uart_xec_fifo_read(const struct device * dev,uint8_t * rx_data,const int size)667 static int uart_xec_fifo_read(const struct device *dev, uint8_t *rx_data,
668 const int size)
669 {
670 const struct uart_xec_device_config * const dev_cfg = dev->config;
671 struct uart_xec_dev_data *dev_data = dev->data;
672 struct uart_regs *regs = dev_cfg->regs;
673 int i;
674 k_spinlock_key_t key = k_spin_lock(&dev_data->lock);
675
676 for (i = 0; (i < size) && (regs->LSR & LSR_RXRDY) != 0; i++) {
677 rx_data[i] = regs->RTXB;
678 }
679
680 k_spin_unlock(&dev_data->lock, key);
681
682 return i;
683 }
684
685 /**
686 * @brief Enable TX interrupt in IER
687 *
688 * @param dev UART device struct
689 */
uart_xec_irq_tx_enable(const struct device * dev)690 static void uart_xec_irq_tx_enable(const struct device *dev)
691 {
692 const struct uart_xec_device_config * const dev_cfg = dev->config;
693 struct uart_xec_dev_data *dev_data = dev->data;
694 struct uart_regs *regs = dev_cfg->regs;
695 k_spinlock_key_t key = k_spin_lock(&dev_data->lock);
696
697 regs->IER |= IER_TBE;
698
699 k_spin_unlock(&dev_data->lock, key);
700 }
701
702 /**
703 * @brief Disable TX interrupt in IER
704 *
705 * @param dev UART device struct
706 */
uart_xec_irq_tx_disable(const struct device * dev)707 static void uart_xec_irq_tx_disable(const struct device *dev)
708 {
709 const struct uart_xec_device_config * const dev_cfg = dev->config;
710 struct uart_xec_dev_data *dev_data = dev->data;
711 struct uart_regs *regs = dev_cfg->regs;
712 k_spinlock_key_t key = k_spin_lock(&dev_data->lock);
713
714 regs->IER &= ~(IER_TBE);
715
716 k_spin_unlock(&dev_data->lock, key);
717 }
718
719 /**
720 * @brief Check if Tx IRQ has been raised
721 *
722 * @param dev UART device struct
723 *
724 * @return 1 if an IRQ is ready, 0 otherwise
725 */
uart_xec_irq_tx_ready(const struct device * dev)726 static int uart_xec_irq_tx_ready(const struct device *dev)
727 {
728 struct uart_xec_dev_data *dev_data = dev->data;
729 k_spinlock_key_t key = k_spin_lock(&dev_data->lock);
730
731 int ret = ((IIRC(dev) & IIR_ID) == IIR_THRE) ? 1 : 0;
732
733 k_spin_unlock(&dev_data->lock, key);
734
735 return ret;
736 }
737
738 /**
739 * @brief Check if nothing remains to be transmitted
740 *
741 * @param dev UART device struct
742 *
743 * @return 1 if nothing remains to be transmitted, 0 otherwise
744 */
uart_xec_irq_tx_complete(const struct device * dev)745 static int uart_xec_irq_tx_complete(const struct device *dev)
746 {
747 const struct uart_xec_device_config * const dev_cfg = dev->config;
748 struct uart_xec_dev_data *dev_data = dev->data;
749 struct uart_regs *regs = dev_cfg->regs;
750 k_spinlock_key_t key = k_spin_lock(&dev_data->lock);
751
752 int ret = ((regs->LSR & (LSR_TEMT | LSR_THRE))
753 == (LSR_TEMT | LSR_THRE)) ? 1 : 0;
754
755 k_spin_unlock(&dev_data->lock, key);
756
757 return ret;
758 }
759
760 /**
761 * @brief Enable RX interrupt in IER
762 *
763 * @param dev UART device struct
764 */
uart_xec_irq_rx_enable(const struct device * dev)765 static void uart_xec_irq_rx_enable(const struct device *dev)
766 {
767 const struct uart_xec_device_config * const dev_cfg = dev->config;
768 struct uart_xec_dev_data *dev_data = dev->data;
769 struct uart_regs *regs = dev_cfg->regs;
770 k_spinlock_key_t key = k_spin_lock(&dev_data->lock);
771
772 regs->IER |= IER_RXRDY;
773
774 k_spin_unlock(&dev_data->lock, key);
775 }
776
777 /**
778 * @brief Disable RX interrupt in IER
779 *
780 * @param dev UART device struct
781 */
uart_xec_irq_rx_disable(const struct device * dev)782 static void uart_xec_irq_rx_disable(const struct device *dev)
783 {
784 const struct uart_xec_device_config * const dev_cfg = dev->config;
785 struct uart_xec_dev_data *dev_data = dev->data;
786 struct uart_regs *regs = dev_cfg->regs;
787 k_spinlock_key_t key = k_spin_lock(&dev_data->lock);
788
789 regs->IER &= ~(IER_RXRDY);
790
791 k_spin_unlock(&dev_data->lock, key);
792 }
793
794 /**
795 * @brief Check if Rx IRQ has been raised
796 *
797 * @param dev UART device struct
798 *
799 * @return 1 if an IRQ is ready, 0 otherwise
800 */
uart_xec_irq_rx_ready(const struct device * dev)801 static int uart_xec_irq_rx_ready(const struct device *dev)
802 {
803 struct uart_xec_dev_data *dev_data = dev->data;
804 k_spinlock_key_t key = k_spin_lock(&dev_data->lock);
805
806 int ret = ((IIRC(dev) & IIR_ID) == IIR_RBRF) ? 1 : 0;
807
808 k_spin_unlock(&dev_data->lock, key);
809
810 return ret;
811 }
812
813 /**
814 * @brief Enable error interrupt in IER
815 *
816 * @param dev UART device struct
817 */
uart_xec_irq_err_enable(const struct device * dev)818 static void uart_xec_irq_err_enable(const struct device *dev)
819 {
820 const struct uart_xec_device_config * const dev_cfg = dev->config;
821 struct uart_xec_dev_data *dev_data = dev->data;
822 struct uart_regs *regs = dev_cfg->regs;
823 k_spinlock_key_t key = k_spin_lock(&dev_data->lock);
824
825 regs->IER |= IER_LSR;
826
827 k_spin_unlock(&dev_data->lock, key);
828 }
829
830 /**
831 * @brief Disable error interrupt in IER
832 *
833 * @param dev UART device struct
834 *
835 * @return 1 if an IRQ is ready, 0 otherwise
836 */
uart_xec_irq_err_disable(const struct device * dev)837 static void uart_xec_irq_err_disable(const struct device *dev)
838 {
839 const struct uart_xec_device_config * const dev_cfg = dev->config;
840 struct uart_xec_dev_data *dev_data = dev->data;
841 struct uart_regs *regs = dev_cfg->regs;
842 k_spinlock_key_t key = k_spin_lock(&dev_data->lock);
843
844 regs->IER &= ~(IER_LSR);
845
846 k_spin_unlock(&dev_data->lock, key);
847 }
848
849 /**
850 * @brief Check if any IRQ is pending
851 *
852 * @param dev UART device struct
853 *
854 * @return 1 if an IRQ is pending, 0 otherwise
855 */
uart_xec_irq_is_pending(const struct device * dev)856 static int uart_xec_irq_is_pending(const struct device *dev)
857 {
858 struct uart_xec_dev_data *dev_data = dev->data;
859 k_spinlock_key_t key = k_spin_lock(&dev_data->lock);
860
861 int ret = (!(IIRC(dev) & IIR_NIP)) ? 1 : 0;
862
863 k_spin_unlock(&dev_data->lock, key);
864
865 return ret;
866 }
867
868 /**
869 * @brief Update cached contents of IIR
870 *
871 * @param dev UART device struct
872 *
873 * @return Always 1
874 */
uart_xec_irq_update(const struct device * dev)875 static int uart_xec_irq_update(const struct device *dev)
876 {
877 const struct uart_xec_device_config * const dev_cfg = dev->config;
878 struct uart_xec_dev_data *dev_data = dev->data;
879 struct uart_regs *regs = dev_cfg->regs;
880 k_spinlock_key_t key = k_spin_lock(&dev_data->lock);
881
882 IIRC(dev) = regs->IIR_FCR;
883
884 k_spin_unlock(&dev_data->lock, key);
885
886 return 1;
887 }
888
889 /**
890 * @brief Set the callback function pointer for IRQ.
891 *
892 * @param dev UART device struct
893 * @param cb Callback function pointer.
894 */
uart_xec_irq_callback_set(const struct device * dev,uart_irq_callback_user_data_t cb,void * cb_data)895 static void uart_xec_irq_callback_set(const struct device *dev,
896 uart_irq_callback_user_data_t cb,
897 void *cb_data)
898 {
899 struct uart_xec_dev_data * const dev_data = dev->data;
900 k_spinlock_key_t key = k_spin_lock(&dev_data->lock);
901
902 dev_data->cb = cb;
903 dev_data->cb_data = cb_data;
904
905 k_spin_unlock(&dev_data->lock, key);
906 }
907
908 /**
909 * @brief Interrupt service routine.
910 *
911 * This simply calls the callback function, if one exists.
912 *
913 * @param arg Argument to ISR.
914 */
uart_xec_isr(const struct device * dev)915 static void uart_xec_isr(const struct device *dev)
916 {
917 struct uart_xec_dev_data * const dev_data = dev->data;
918 #if defined(CONFIG_PM_DEVICE) && defined(CONFIG_UART_CONSOLE_INPUT_EXPIRED)
919 const struct uart_xec_device_config * const dev_cfg = dev->config;
920 struct uart_regs *regs = dev_cfg->regs;
921 int rx_ready = 0;
922
923 rx_ready = ((regs->LSR & LSR_RXRDY) == LSR_RXRDY) ? 1 : 0;
924 if (rx_ready) {
925 k_timeout_t delay = K_MSEC(CONFIG_UART_CONSOLE_INPUT_EXPIRED_TIMEOUT);
926
927 uart_xec_pm_policy_state_lock_get(UART_XEC_PM_POLICY_STATE_RX_FLAG);
928 k_work_reschedule(&rx_refresh_timeout_work, delay);
929 }
930 #endif
931
932 if (dev_data->cb) {
933 dev_data->cb(dev, dev_data->cb_data);
934 }
935
936 #if defined(CONFIG_PM_DEVICE) && defined(CONFIG_UART_CONSOLE_INPUT_EXPIRED)
937 if (uart_xec_irq_tx_complete(dev)) {
938 uart_xec_pm_policy_state_lock_put(UART_XEC_PM_POLICY_STATE_TX_FLAG);
939 }
940 #endif /* CONFIG_PM */
941
942 /* clear ECIA GIRQ R/W1C status bit after UART status cleared */
943 uart_xec_girq_clr(dev);
944 }
945
946 #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
947
948 #ifdef CONFIG_UART_XEC_LINE_CTRL
949
950 /**
951 * @brief Manipulate line control for UART.
952 *
953 * @param dev UART device struct
954 * @param ctrl The line control to be manipulated
955 * @param val Value to set the line control
956 *
957 * @return 0 if successful, failed otherwise
958 */
uart_xec_line_ctrl_set(const struct device * dev,uint32_t ctrl,uint32_t val)959 static int uart_xec_line_ctrl_set(const struct device *dev,
960 uint32_t ctrl, uint32_t val)
961 {
962 const struct uart_xec_device_config * const dev_cfg = dev->config;
963 struct uart_xec_dev_data *dev_data = dev->data;
964 struct uart_regs *regs = dev_cfg->regs;
965 uint32_t mdc, chg;
966 k_spinlock_key_t key;
967
968 switch (ctrl) {
969 case UART_LINE_CTRL_BAUD_RATE:
970 set_baud_rate(dev, val);
971 return 0;
972
973 case UART_LINE_CTRL_RTS:
974 case UART_LINE_CTRL_DTR:
975 key = k_spin_lock(&dev_data->lock);
976 mdc = regs->MCR;
977
978 if (ctrl == UART_LINE_CTRL_RTS) {
979 chg = MCR_RTS;
980 } else {
981 chg = MCR_DTR;
982 }
983
984 if (val) {
985 mdc |= chg;
986 } else {
987 mdc &= ~(chg);
988 }
989 regs->MCR = mdc;
990 k_spin_unlock(&dev_data->lock, key);
991 return 0;
992 }
993
994 return -ENOTSUP;
995 }
996
997 #endif /* CONFIG_UART_XEC_LINE_CTRL */
998
999 static const struct uart_driver_api uart_xec_driver_api = {
1000 .poll_in = uart_xec_poll_in,
1001 .poll_out = uart_xec_poll_out,
1002 .err_check = uart_xec_err_check,
1003 #ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
1004 .configure = uart_xec_configure,
1005 .config_get = uart_xec_config_get,
1006 #endif
1007 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
1008
1009 .fifo_fill = uart_xec_fifo_fill,
1010 .fifo_read = uart_xec_fifo_read,
1011 .irq_tx_enable = uart_xec_irq_tx_enable,
1012 .irq_tx_disable = uart_xec_irq_tx_disable,
1013 .irq_tx_ready = uart_xec_irq_tx_ready,
1014 .irq_tx_complete = uart_xec_irq_tx_complete,
1015 .irq_rx_enable = uart_xec_irq_rx_enable,
1016 .irq_rx_disable = uart_xec_irq_rx_disable,
1017 .irq_rx_ready = uart_xec_irq_rx_ready,
1018 .irq_err_enable = uart_xec_irq_err_enable,
1019 .irq_err_disable = uart_xec_irq_err_disable,
1020 .irq_is_pending = uart_xec_irq_is_pending,
1021 .irq_update = uart_xec_irq_update,
1022 .irq_callback_set = uart_xec_irq_callback_set,
1023
1024 #endif
1025
1026 #ifdef CONFIG_UART_XEC_LINE_CTRL
1027 .line_ctrl_set = uart_xec_line_ctrl_set,
1028 #endif
1029 };
1030
1031 #define DEV_CONFIG_REG_INIT(n) \
1032 .regs = (struct uart_regs *)(DT_INST_REG_ADDR(n)),
1033
1034 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
1035 #define DEV_CONFIG_IRQ_FUNC_INIT(n) \
1036 .irq_config_func = irq_config_func##n,
1037 #define UART_XEC_IRQ_FUNC_DECLARE(n) \
1038 static void irq_config_func##n(const struct device *dev);
1039 #define UART_XEC_IRQ_FUNC_DEFINE(n) \
1040 static void irq_config_func##n(const struct device *dev) \
1041 { \
1042 ARG_UNUSED(dev); \
1043 IRQ_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority), \
1044 uart_xec_isr, DEVICE_DT_INST_GET(n), \
1045 0); \
1046 irq_enable(DT_INST_IRQN(n)); \
1047 uart_xec_girq_en(DT_INST_PROP_BY_IDX(n, girqs, 0), \
1048 DT_INST_PROP_BY_IDX(n, girqs, 1)); \
1049 }
1050 #else
1051 /* !CONFIG_UART_INTERRUPT_DRIVEN */
1052 #define DEV_CONFIG_IRQ_FUNC_INIT(n)
1053 #define UART_XEC_IRQ_FUNC_DECLARE(n)
1054 #define UART_XEC_IRQ_FUNC_DEFINE(n)
1055 #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
1056
1057 #define DEV_DATA_FLOW_CTRL(n) \
1058 DT_INST_PROP_OR(n, hw_flow_control, UART_CFG_FLOW_CTRL_NONE)
1059
1060 /* To enable wakeup on the UART, the DTS needs to have two entries defined
1061 * in the corresponding UART node in the DTS specifying it as a wake source
1062 * and specifying the UART_RX GPIO; example as below
1063 *
1064 * wakerx-gpios = <&gpio_140_176 25 GPIO_ACTIVE_HIGH>;
1065 * wakeup-source;
1066 */
1067 #ifdef CONFIG_PM_DEVICE
1068 #define XEC_UART_PM_WAKEUP(n) \
1069 .wakeup_source = (uint8_t)DT_INST_PROP_OR(n, wakeup_source, 0), \
1070 .wakerx_gpio = GPIO_DT_SPEC_INST_GET_OR(n, wakerx_gpios, {0}),
1071 #else
1072 #define XEC_UART_PM_WAKEUP(index) /* Not used */
1073 #endif
1074
1075 #define UART_XEC_DEVICE_INIT(n) \
1076 \
1077 PINCTRL_DT_INST_DEFINE(n); \
1078 \
1079 UART_XEC_IRQ_FUNC_DECLARE(n); \
1080 \
1081 static const struct uart_xec_device_config uart_xec_dev_cfg_##n = { \
1082 DEV_CONFIG_REG_INIT(n) \
1083 .sys_clk_freq = DT_INST_PROP(n, clock_frequency), \
1084 .girq_id = DT_INST_PROP_BY_IDX(n, girqs, 0), \
1085 .girq_pos = DT_INST_PROP_BY_IDX(n, girqs, 1), \
1086 .pcr_idx = DT_INST_PROP_BY_IDX(n, pcrs, 0), \
1087 .pcr_bitpos = DT_INST_PROP_BY_IDX(n, pcrs, 1), \
1088 .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
1089 XEC_UART_PM_WAKEUP(n) \
1090 DEV_CONFIG_IRQ_FUNC_INIT(n) \
1091 }; \
1092 static struct uart_xec_dev_data uart_xec_dev_data_##n = { \
1093 .uart_config.baudrate = DT_INST_PROP_OR(n, current_speed, 0), \
1094 .uart_config.parity = UART_CFG_PARITY_NONE, \
1095 .uart_config.stop_bits = UART_CFG_STOP_BITS_1, \
1096 .uart_config.data_bits = UART_CFG_DATA_BITS_8, \
1097 .uart_config.flow_ctrl = DEV_DATA_FLOW_CTRL(n), \
1098 }; \
1099 PM_DEVICE_DT_INST_DEFINE(n, uart_xec_pm_action); \
1100 DEVICE_DT_INST_DEFINE(n, &uart_xec_init, \
1101 PM_DEVICE_DT_INST_GET(n), \
1102 &uart_xec_dev_data_##n, \
1103 &uart_xec_dev_cfg_##n, \
1104 PRE_KERNEL_1, \
1105 CONFIG_SERIAL_INIT_PRIORITY, \
1106 &uart_xec_driver_api); \
1107 UART_XEC_IRQ_FUNC_DEFINE(n)
1108
1109 DT_INST_FOREACH_STATUS_OKAY(UART_XEC_DEVICE_INIT)
1110