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
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-11-09 GuEe-GUI first version
*/
#include <rtthread.h>
#include "8250.h"
struct ofw_platform_8250
{
struct serial8250 parent;
struct rt_reset_control *rstc;
};
#define to_ofw_platform_8250(serial8250) rt_container_of(serial8250, struct ofw_platform_8250, parent)
static void ofw_platform_8250_free_resource(struct ofw_platform_8250 *op8250)
{
struct serial8250 *serial = &op8250->parent;
if (serial->base)
{
rt_iounmap(serial->base);
}
if (!rt_is_err_or_null(serial->clk))
{
rt_clk_disable_unprepare(serial->clk);
rt_clk_put(serial->clk);
}
if (!rt_is_err_or_null(op8250->rstc))
{
rt_reset_control_put(op8250->rstc);
}
rt_free(op8250);
}
static void op8250_remove(struct serial8250 *serial)
{
struct ofw_platform_8250 *op8250 = to_ofw_platform_8250(serial);
ofw_platform_8250_free_resource(op8250);
}
static rt_err_t ofw_platform_8250_probe(struct rt_platform_device *pdev)
{
rt_err_t err;
rt_uint32_t val;
struct serial8250 *serial;
struct ofw_platform_8250 *op8250;
struct rt_ofw_node *np = pdev->parent.ofw_node;
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
if (rt_ofw_prop_read_bool(np, "used-by-rtas"))
{
return -RT_EBUSY;
}
op8250 = serial8250_alloc(op8250);
if (!op8250)
{
return -RT_ENOMEM;
}
serial = &op8250->parent;
serial->base = rt_ofw_iomap(np, 0);
if (!serial->base)
{
err = -RT_EIO;
goto _fail;
}
serial->irq = rt_ofw_get_irq(np, 0);
if (serial->irq < 0)
{
err = serial->irq;
goto _fail;
}
if (!rt_ofw_prop_read_u32(np, "clock-frequency", &val))
{
serial->freq = val;
}
else
{
serial->clk = rt_ofw_get_clk(np, 0);
if (rt_is_err(serial->clk))
{
err = rt_ptr_err(serial->clk);
goto _fail;
}
if ((err = rt_clk_prepare_enable(serial->clk)))
{
goto _fail;
}
serial->freq = rt_clk_get_rate(serial->clk);
}
if (!rt_ofw_prop_read_u32(np, "reg-shift", &val))
{
serial->regshift = val;
}
serial->iotype = PORT_MMIO;
if (!rt_ofw_prop_read_u32(np, "reg-io-width", &val))
{
switch (val)
{
case 1:
serial->iotype = PORT_MMIO;
break;
case 2:
serial->iotype = PORT_MMIO16;
break;
case 4:
serial->iotype = rt_ofw_prop_read_bool(np, "big-endian") ?
PORT_MMIO32BE : PORT_MMIO32;
break;
}
}
op8250->rstc = rt_ofw_get_reset_control_by_index(np, 0);
if (rt_is_err(op8250->rstc))
{
err = rt_ptr_err(op8250->rstc);
goto _fail;
}
if (op8250->rstc && (err = rt_reset_control_deassert(op8250->rstc)))
{
goto _fail;
}
rt_dm_dev_bind_fwdata(&serial->parent.parent, pdev->parent.ofw_node, &serial->parent);
pdev->parent.user_data = serial;
serial->parent.ops = &serial8250_uart_ops;
serial->parent.config = config;
serial->remove = &op8250_remove;
serial->data = op8250;
if ((err = serial8250_setup(serial)))
{
goto _fail;
}
return RT_EOK;
_fail:
ofw_platform_8250_free_resource(op8250);
return err;
}
static rt_err_t ofw_platform_8250_remove(struct rt_platform_device *pdev)
{
struct rt_device *dev = &pdev->parent;
struct serial8250 *serial = dev->user_data;
rt_dm_dev_unbind_fwdata(dev, RT_NULL);
return serial8250_remove(serial);
}
static const struct rt_ofw_node_id ofw_platform_8250_ofw_ids[] =
{
{ .type = "ttyS", .compatible = "ns16550a" },
{ .type = "ttyS", .compatible = "ns16550" },
#ifndef RT_SERIAL_8250_BCM7271
{ .type = "ttyS", .compatible = "brcm,bcm7271-uart" },
#endif
{ /* sentinel */ }
};
static struct rt_platform_driver ofw_platform_8250_driver =
{
.name = "8250-ofw",
.ids = ofw_platform_8250_ofw_ids,
.probe = ofw_platform_8250_probe,
.remove = ofw_platform_8250_remove,
};
static int ofw_platform_8250_drv_register(void)
{
rt_platform_driver_register(&ofw_platform_8250_driver);
return 0;
}
INIT_PLATFORM_EXPORT(ofw_platform_8250_drv_register);
|