1 /*
2  * Copyright (c) 2006-2024 RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2022-11-16     GuEe-GUI     first version
9  */
10 
11 #ifndef __SERIAL_8250_H__
12 #define __SERIAL_8250_H__
13 
14 #include <rthw.h>
15 #include <rtthread.h>
16 #include <rtdevice.h>
17 #include <ioremap.h>
18 #include "regs.h"
19 #include "serial_dm.h"
20 
21 enum
22 {
23     PORT_IO,
24     PORT_MMIO,
25     PORT_MMIO16,
26     PORT_MMIO32,
27     PORT_MMIO32BE,
28 };
29 
30 struct serial8250
31 {
32     struct rt_serial_device parent;
33     struct rt_clk *clk;
34 
35     int irq;
36     void *base;
37     rt_size_t size;
38     rt_uint32_t freq;       /* frequency */
39     rt_uint32_t regshift;   /* reg offset shift */
40     rt_uint8_t iotype;      /* io access style */
41 
42     struct rt_spinlock spinlock;
43 
44     rt_uint32_t (*serial_in)(struct serial8250 *, int offset);
45     void (*serial_out)(struct serial8250 *, int offset, int value);
46     rt_err_t (*handle_irq)(struct serial8250 *, int irq);
47 
48     /* Free all resource (and parent) by child */
49     void (*remove)(struct serial8250 *);
50     void *data;
51 };
52 
53 #define serial8250_alloc(obj) rt_calloc(1, sizeof(typeof(*obj)))
54 #define raw_to_serial8250(raw_serial) rt_container_of(raw_serial, struct serial8250, parent)
55 
56 rt_err_t serial8250_config(struct serial8250 *serial, const char *options);
57 rt_err_t serial8250_setup(struct serial8250 *serial);
58 rt_err_t serial8250_remove(struct serial8250 *serial);
59 
60 rt_uint32_t serial8250_in(struct serial8250 *serial, int offset);
61 void serial8250_out(struct serial8250 *serial, int offset, int value);
62 
63 rt_err_t serial8250_uart_configure(struct rt_serial_device *raw_serial, struct serial_configure *cfg);
64 rt_err_t serial8250_uart_control(struct rt_serial_device *raw_serial, int cmd, void *arg);
65 int serial8250_uart_putc(struct rt_serial_device *raw_serial, char c);
66 int serial8250_uart_getc(struct rt_serial_device *raw_serial);
67 
68 int serial8250_early_putc(struct rt_serial_device *raw_serial, char c);
69 rt_err_t serial8250_early_fdt_setup(struct serial8250 *serial, struct rt_fdt_earlycon *con, const char *options);
70 
71 extern struct serial8250 early_serial8250;
72 extern const struct rt_uart_ops serial8250_uart_ops;
73 
74 #endif /* __SERIAL_8250_H__ */
75