1 /*
2 * Copyright (c) 2021 Henrik Brix Andersen <henrik@brixandersen.dk>
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT neorv32_uart
8
9 #include <zephyr/device.h>
10 #include <zephyr/drivers/syscon.h>
11 #include <zephyr/drivers/uart.h>
12 #include <zephyr/pm/device.h>
13 #include <zephyr/sys/sys_io.h>
14
15 #include <zephyr/logging/log.h>
16 #include <zephyr/irq.h>
17 LOG_MODULE_REGISTER(uart_neorv32, CONFIG_UART_LOG_LEVEL);
18
19 /* NEORV32 UART registers offsets */
20 #define NEORV32_UART_CTRL_OFFSET 0x00
21 #define NEORV32_UART_DATA_OFFSET 0x04
22
23 /* UART_CTRL register bits */
24 #define NEORV32_UART_CTRL_EN BIT(0)
25 #define NEORV32_UART_CTRL_SIM_MODE BIT(1)
26 #define NEORV32_UART_CTRL_HWFC_EN BIT(2)
27 #define NEORV32_UART_CTRL_PRSC_POS 3U
28 #define NEORV32_UART_CTRL_PRSC_MASK BIT_MASK(3)
29 #define NEORV32_UART_CTRL_BAUD_POS 6U
30 #define NEORV32_UART_CTRL_BAUD_MASK BIT_MASK(10)
31 #define NEORV32_UART_CTRL_RX_NEMPTY BIT(16)
32 #define NEORV32_UART_CTRL_RX_HALF BIT(17)
33 #define NEORV32_UART_CTRL_RX_FULL BIT(18)
34 #define NEORV32_UART_CTRL_TX_NEMPTY BIT(19)
35 #define NEORV32_UART_CTRL_TX_HALF BIT(20)
36 #define NEORV32_UART_CTRL_TX_FULL BIT(21)
37 #define NEORV32_UART_CTRL_IRQ_RX_NEMPTY BIT(22)
38 #define NEORV32_UART_CTRL_IRQ_RX_HALF BIT(23)
39 #define NEORV32_UART_CTRL_IRQ_RX_FULL BIT(24)
40 #define NEORV32_UART_CTRL_IRQ_TX_EMPTY BIT(25)
41 #define NEORV32_UART_CTRL_IRQ_TX_NHALF BIT(26)
42 #define NEORV32_UART_CTRL_RX_OVER BIT(30)
43 #define NEORV32_UART_CTRL_TX_BUSY BIT(31)
44
45 struct neorv32_uart_config {
46 const struct device *syscon;
47 uint32_t feature_mask;
48 mm_reg_t base;
49 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
50 void (*irq_config_func)(const struct device *dev);
51 unsigned int tx_irq;
52 unsigned int rx_irq;
53 #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
54 };
55
56 struct neorv32_uart_data {
57 struct uart_config uart_cfg;
58 uint32_t last_data;
59 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
60 struct k_timer timer;
61 uart_irq_callback_user_data_t callback;
62 void *callback_data;
63 #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
64 };
65
neorv32_uart_read_ctrl(const struct device * dev)66 static inline uint32_t neorv32_uart_read_ctrl(const struct device *dev)
67 {
68 const struct neorv32_uart_config *config = dev->config;
69
70 return sys_read32(config->base + NEORV32_UART_CTRL_OFFSET);
71 }
72
neorv32_uart_write_ctrl(const struct device * dev,uint32_t ctrl)73 static inline void neorv32_uart_write_ctrl(const struct device *dev, uint32_t ctrl)
74 {
75 const struct neorv32_uart_config *config = dev->config;
76
77 sys_write32(ctrl, config->base + NEORV32_UART_CTRL_OFFSET);
78 }
79
neorv32_uart_read_data(const struct device * dev)80 static inline uint32_t neorv32_uart_read_data(const struct device *dev)
81 {
82 const struct neorv32_uart_config *config = dev->config;
83 struct neorv32_uart_data *data = dev->data;
84 uint32_t reg;
85
86 /* Cache status bits as they are cleared upon read */
87 reg = sys_read32(config->base + NEORV32_UART_DATA_OFFSET);
88 data->last_data = reg;
89
90 return reg;
91 }
92
neorv32_uart_write_data(const struct device * dev,uint32_t data)93 static inline void neorv32_uart_write_data(const struct device *dev, uint32_t data)
94 {
95 const struct neorv32_uart_config *config = dev->config;
96
97 sys_write32(data, config->base + NEORV32_UART_DATA_OFFSET);
98 }
99
neorv32_uart_poll_in(const struct device * dev,unsigned char * c)100 static int neorv32_uart_poll_in(const struct device *dev, unsigned char *c)
101 {
102 uint32_t data;
103
104 data = neorv32_uart_read_data(dev);
105
106 if ((data & NEORV32_UART_CTRL_RX_NEMPTY) != 0) {
107 *c = data & BIT_MASK(8);
108 return 0;
109 }
110
111 return -1;
112 }
113
neorv32_uart_poll_out(const struct device * dev,unsigned char c)114 static void neorv32_uart_poll_out(const struct device *dev, unsigned char c)
115 {
116 while ((neorv32_uart_read_ctrl(dev) & NEORV32_UART_CTRL_TX_BUSY) != 0) {
117 }
118
119 neorv32_uart_write_data(dev, c);
120 }
121
neorv32_uart_configure(const struct device * dev,const struct uart_config * cfg)122 static int neorv32_uart_configure(const struct device *dev, const struct uart_config *cfg)
123 {
124 const struct neorv32_uart_config *config = dev->config;
125 struct neorv32_uart_data *data = dev->data;
126 uint32_t ctrl = NEORV32_UART_CTRL_EN;
127 uint16_t baudxx = 0;
128 uint8_t prscx = 0;
129 uint32_t clk;
130 int err;
131
132 __ASSERT_NO_MSG(cfg != NULL);
133
134 if (cfg->stop_bits != UART_CFG_STOP_BITS_1) {
135 LOG_ERR("hardware only supports one stop bit");
136 return -ENOTSUP;
137 }
138
139 if (cfg->data_bits != UART_CFG_DATA_BITS_8) {
140 LOG_ERR("hardware only supports 8 data bits");
141 return -ENOTSUP;
142 }
143
144 switch (cfg->parity) {
145 case UART_CFG_PARITY_NONE:
146 break;
147 default:
148 LOG_ERR("unsupported parity mode %d", cfg->parity);
149 return -ENOTSUP;
150 }
151
152 switch (cfg->flow_ctrl) {
153 case UART_CFG_FLOW_CTRL_NONE:
154 ctrl |= 0;
155 break;
156 case UART_CFG_FLOW_CTRL_RTS_CTS:
157 ctrl |= NEORV32_UART_CTRL_HWFC_EN;
158 break;
159 default:
160 LOG_ERR("unsupported flow control mode %d", cfg->flow_ctrl);
161 return -ENOTSUP;
162 }
163
164 err = syscon_read_reg(config->syscon, NEORV32_SYSINFO_CLK, &clk);
165 if (err < 0) {
166 LOG_ERR("failed to determine clock rate (err %d)", err);
167 return -EIO;
168 }
169
170 if (cfg->baudrate == 0) {
171 LOG_ERR("invalid baud rate 0");
172 return -EINVAL;
173 }
174
175 /*
176 * Calculate clock prescaler and baud prescaler. Initial prscx = 0 is
177 * clock / 2.
178 */
179 baudxx = clk / (2 * cfg->baudrate);
180 while (baudxx >= NEORV32_UART_CTRL_BAUD_MASK) {
181 if ((prscx == 2) || (prscx == 4)) {
182 baudxx >>= 3;
183 } else {
184 baudxx >>= 1;
185 }
186
187 prscx++;
188 }
189
190 if (prscx > NEORV32_UART_CTRL_PRSC_MASK) {
191 LOG_ERR("unsupported baud rate %d", cfg->baudrate);
192 return -ENOTSUP;
193 }
194
195 ctrl |= (baudxx - 1) << NEORV32_UART_CTRL_BAUD_POS;
196 ctrl |= prscx << NEORV32_UART_CTRL_PRSC_POS;
197
198 data->uart_cfg = *cfg;
199 neorv32_uart_write_ctrl(dev, ctrl);
200
201 return 0;
202 }
203
neorv32_uart_config_get(const struct device * dev,struct uart_config * cfg)204 static int neorv32_uart_config_get(const struct device *dev, struct uart_config *cfg)
205 {
206 struct neorv32_uart_data *data = dev->data;
207
208 __ASSERT_NO_MSG(cfg != NULL);
209
210 *cfg = data->uart_cfg;
211
212 return 0;
213 }
214
215 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
neorv32_uart_fifo_fill(const struct device * dev,const uint8_t * tx_data,int len)216 static int neorv32_uart_fifo_fill(const struct device *dev, const uint8_t *tx_data, int len)
217 {
218 uint32_t ctrl;
219
220 if (len <= 0) {
221 return 0;
222 }
223
224 __ASSERT_NO_MSG(tx_data != NULL);
225
226 ctrl = neorv32_uart_read_ctrl(dev);
227 if ((ctrl & NEORV32_UART_CTRL_TX_BUSY) == 0) {
228 neorv32_uart_write_data(dev, *tx_data);
229 return 1;
230 }
231
232 return 0;
233 }
234
neorv32_uart_fifo_read(const struct device * dev,uint8_t * rx_data,const int size)235 static int neorv32_uart_fifo_read(const struct device *dev, uint8_t *rx_data, const int size)
236 {
237 struct neorv32_uart_data *data = dev->data;
238 int count = 0;
239
240 if (size <= 0) {
241 return 0;
242 }
243
244 __ASSERT_NO_MSG(rx_data != NULL);
245
246 while ((data->last_data & NEORV32_UART_CTRL_RX_NEMPTY) != 0) {
247 rx_data[count++] = data->last_data & BIT_MASK(8);
248 data->last_data &= ~(NEORV32_UART_CTRL_RX_NEMPTY);
249
250 if (count >= size) {
251 break;
252 }
253
254 (void)neorv32_uart_read_data(dev);
255 }
256
257 return count;
258 }
259
neorv32_uart_tx_soft_isr(struct k_timer * timer)260 static void neorv32_uart_tx_soft_isr(struct k_timer *timer)
261 {
262 const struct device *dev = k_timer_user_data_get(timer);
263 struct neorv32_uart_data *data = dev->data;
264 uart_irq_callback_user_data_t callback = data->callback;
265
266 if (callback) {
267 callback(dev, data->callback_data);
268 }
269 }
270
neorv32_uart_irq_tx_enable(const struct device * dev)271 static void neorv32_uart_irq_tx_enable(const struct device *dev)
272 {
273 const struct neorv32_uart_config *config = dev->config;
274 struct neorv32_uart_data *data = dev->data;
275 uint32_t ctrl;
276
277 irq_enable(config->tx_irq);
278
279 ctrl = neorv32_uart_read_ctrl(dev);
280 if ((ctrl & NEORV32_UART_CTRL_TX_BUSY) == 0) {
281 /*
282 * TX done event already generated an edge interrupt. Generate a
283 * soft interrupt and have it call the callback function in
284 * timer isr context.
285 */
286 k_timer_start(&data->timer, K_NO_WAIT, K_NO_WAIT);
287 }
288 }
289
neorv32_uart_irq_tx_disable(const struct device * dev)290 static void neorv32_uart_irq_tx_disable(const struct device *dev)
291 {
292 const struct neorv32_uart_config *config = dev->config;
293
294 irq_disable(config->tx_irq);
295 }
296
neorv32_uart_irq_tx_ready(const struct device * dev)297 static int neorv32_uart_irq_tx_ready(const struct device *dev)
298 {
299 const struct neorv32_uart_config *config = dev->config;
300 uint32_t ctrl;
301
302 if (!irq_is_enabled(config->tx_irq)) {
303 return 0;
304 }
305
306 ctrl = neorv32_uart_read_ctrl(dev);
307
308 return (ctrl & NEORV32_UART_CTRL_TX_BUSY) == 0;
309 }
310
neorv32_uart_irq_rx_enable(const struct device * dev)311 static void neorv32_uart_irq_rx_enable(const struct device *dev)
312 {
313 const struct neorv32_uart_config *config = dev->config;
314
315 irq_enable(config->rx_irq);
316 }
317
neorv32_uart_irq_rx_disable(const struct device * dev)318 static void neorv32_uart_irq_rx_disable(const struct device *dev)
319 {
320 const struct neorv32_uart_config *config = dev->config;
321
322 irq_disable(config->rx_irq);
323 }
324
neorv32_uart_irq_tx_complete(const struct device * dev)325 static int neorv32_uart_irq_tx_complete(const struct device *dev)
326 {
327 uint32_t ctrl;
328
329 ctrl = neorv32_uart_read_ctrl(dev);
330
331 return (ctrl & NEORV32_UART_CTRL_TX_BUSY) == 0;
332 }
333
neorv32_uart_irq_rx_ready(const struct device * dev)334 static int neorv32_uart_irq_rx_ready(const struct device *dev)
335 {
336 const struct neorv32_uart_config *config = dev->config;
337 struct neorv32_uart_data *data = dev->data;
338
339 if (!irq_is_enabled(config->rx_irq)) {
340 return 0;
341 }
342
343 return (data->last_data & NEORV32_UART_CTRL_RX_NEMPTY) != 0;
344 }
345
neorv32_uart_irq_is_pending(const struct device * dev)346 static int neorv32_uart_irq_is_pending(const struct device *dev)
347 {
348 return (neorv32_uart_irq_tx_ready(dev) ||
349 neorv32_uart_irq_rx_ready(dev));
350 }
351
neorv32_uart_irq_update(const struct device * dev)352 static int neorv32_uart_irq_update(const struct device *dev)
353 {
354 const struct neorv32_uart_config *config = dev->config;
355
356 if (irq_is_enabled(config->rx_irq)) {
357 /* Cache data for use by rx_ready() and fifo_read() */
358 (void)neorv32_uart_read_data(dev);
359 }
360
361 return 1;
362 }
363
neorv32_uart_irq_callback_set(const struct device * dev,uart_irq_callback_user_data_t cb,void * user_data)364 static void neorv32_uart_irq_callback_set(const struct device *dev,
365 uart_irq_callback_user_data_t cb, void *user_data)
366 {
367 struct neorv32_uart_data *data = dev->data;
368
369 data->callback = cb;
370 data->callback_data = user_data;
371 }
372
neorv32_uart_isr(const struct device * dev)373 static void neorv32_uart_isr(const struct device *dev)
374 {
375 struct neorv32_uart_data *data = dev->data;
376 uart_irq_callback_user_data_t callback = data->callback;
377
378 if (callback) {
379 callback(dev, data->callback_data);
380 }
381 }
382 #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
383
neorv32_uart_init(const struct device * dev)384 static int neorv32_uart_init(const struct device *dev)
385 {
386 const struct neorv32_uart_config *config = dev->config;
387 struct neorv32_uart_data *data = dev->data;
388 uint32_t features;
389 int err;
390
391 if (!device_is_ready(config->syscon)) {
392 LOG_ERR("syscon device not ready");
393 return -EINVAL;
394 }
395
396 err = syscon_read_reg(config->syscon, NEORV32_SYSINFO_FEATURES, &features);
397 if (err < 0) {
398 LOG_ERR("failed to determine implemented features (err %d)", err);
399 return -EIO;
400 }
401
402 if ((features & config->feature_mask) == 0) {
403 LOG_ERR("neorv32 uart instance not supported");
404 return -ENODEV;
405 }
406
407 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
408 k_timer_init(&data->timer, &neorv32_uart_tx_soft_isr, NULL);
409 k_timer_user_data_set(&data->timer, (void *)dev);
410
411 config->irq_config_func(dev);
412 #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
413
414 return neorv32_uart_configure(dev, &data->uart_cfg);
415 }
416
417 #ifdef CONFIG_PM_DEVICE
neorv32_uart_pm_action(const struct device * dev,enum pm_device_action action)418 static int neorv32_uart_pm_action(const struct device *dev,
419 enum pm_device_action action)
420 {
421 uint32_t ctrl = neorv32_uart_read_ctrl(dev);
422
423 switch (action) {
424 case PM_DEVICE_ACTION_SUSPEND:
425 ctrl &= ~(NEORV32_UART_CTRL_EN);
426 break;
427 case PM_DEVICE_ACTION_RESUME:
428 ctrl |= NEORV32_UART_CTRL_EN;
429 break;
430 default:
431 return -ENOTSUP;
432 }
433
434 neorv32_uart_write_ctrl(dev, ctrl);
435
436 return 0;
437 }
438 #endif /* CONFIG_PM_DEVICE */
439
440 static const struct uart_driver_api neorv32_uart_driver_api = {
441 .poll_in = neorv32_uart_poll_in,
442 .poll_out = neorv32_uart_poll_out,
443 #ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
444 .configure = neorv32_uart_configure,
445 .config_get = neorv32_uart_config_get,
446 #endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */
447 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
448 .fifo_fill = neorv32_uart_fifo_fill,
449 .fifo_read = neorv32_uart_fifo_read,
450 .irq_tx_enable = neorv32_uart_irq_tx_enable,
451 .irq_tx_disable = neorv32_uart_irq_tx_disable,
452 .irq_tx_ready = neorv32_uart_irq_tx_ready,
453 .irq_rx_enable = neorv32_uart_irq_rx_enable,
454 .irq_rx_disable = neorv32_uart_irq_rx_disable,
455 .irq_tx_complete = neorv32_uart_irq_tx_complete,
456 .irq_rx_ready = neorv32_uart_irq_rx_ready,
457 .irq_is_pending = neorv32_uart_irq_is_pending,
458 .irq_update = neorv32_uart_irq_update,
459 .irq_callback_set = neorv32_uart_irq_callback_set,
460 #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
461 };
462
463 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
464 #define NEORV32_UART_CONFIG_FUNC(node_id, n) \
465 static void neorv32_uart_config_func_##n(const struct device *dev) \
466 { \
467 IRQ_CONNECT(DT_IRQ_BY_NAME(node_id, tx, irq), \
468 DT_IRQ_BY_NAME(node_id, tx, priority), \
469 neorv32_uart_isr, \
470 DEVICE_DT_GET(node_id), 0); \
471 \
472 IRQ_CONNECT(DT_IRQ_BY_NAME(node_id, rx, irq), \
473 DT_IRQ_BY_NAME(node_id, rx, priority), \
474 neorv32_uart_isr, \
475 DEVICE_DT_GET(node_id), 0); \
476 }
477 #define NEORV32_UART_CONFIG_INIT(node_id, n) \
478 .irq_config_func = neorv32_uart_config_func_##n, \
479 .tx_irq = DT_IRQ_BY_NAME(node_id, tx, irq), \
480 .rx_irq = DT_IRQ_BY_NAME(node_id, rx, irq),
481 #else
482 #define NEORV32_UART_CONFIG_FUNC(node_id, n)
483 #define NEORV32_UART_CONFIG_INIT(node_id, n)
484 #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
485
486 #define NEORV32_UART_INIT(node_id, n) \
487 NEORV32_UART_CONFIG_FUNC(node_id, n) \
488 \
489 static struct neorv32_uart_data neorv32_uart_##n##_data = { \
490 .uart_cfg = { \
491 .baudrate = DT_PROP(node_id, current_speed), \
492 .parity = DT_ENUM_IDX_OR(node_id, parity, \
493 UART_CFG_PARITY_NONE), \
494 .stop_bits = UART_CFG_STOP_BITS_1, \
495 .data_bits = UART_CFG_DATA_BITS_8, \
496 .flow_ctrl = DT_PROP(node_id, hw_flow_control) ? \
497 UART_CFG_FLOW_CTRL_RTS_CTS : \
498 UART_CFG_FLOW_CTRL_NONE, \
499 }, \
500 }; \
501 \
502 static const struct neorv32_uart_config neorv32_uart_##n##_config = { \
503 .syscon = DEVICE_DT_GET(DT_PHANDLE(node_id, syscon)), \
504 .feature_mask = NEORV32_SYSINFO_FEATURES_IO_UART##n, \
505 .base = DT_REG_ADDR(node_id), \
506 NEORV32_UART_CONFIG_INIT(node_id, n) \
507 }; \
508 \
509 PM_DEVICE_DT_DEFINE(node_id, neorv32_uart_pm_action); \
510 \
511 DEVICE_DT_DEFINE(node_id, &neorv32_uart_init, \
512 PM_DEVICE_DT_GET(node_id), \
513 &neorv32_uart_##n##_data, \
514 &neorv32_uart_##n##_config, \
515 PRE_KERNEL_1, \
516 CONFIG_SERIAL_INIT_PRIORITY, \
517 &neorv32_uart_driver_api)
518
519 #if DT_NODE_HAS_COMPAT_STATUS(DT_NODELABEL(uart0), DT_DRV_COMPAT, okay)
520 NEORV32_UART_INIT(DT_NODELABEL(uart0), 0);
521 #endif
522
523 #if DT_NODE_HAS_COMPAT_STATUS(DT_NODELABEL(uart1), DT_DRV_COMPAT, okay)
524 NEORV32_UART_INIT(DT_NODELABEL(uart1), 1);
525 #endif
526