summaryrefslogtreecommitdiff
path: root/components/drivers/core/platform_ofw.c
blob: 7bcfd4ea6ccdd858715b559b6d53b8239909bfc8 (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * Copyright (c) 2006-2024, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-06-04     GuEe-GUI     the first version
 * 2025-12-25     lhxj         fix OFW bus conflict and prevent duplicate device creation
 */

#include <rtthread.h>

#define DBG_TAG "drv.platform"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#include <drivers/ofw_io.h>
#include <drivers/ofw_fdt.h>
#include <drivers/platform.h>
#include <drivers/core/bus.h>
#include <drivers/core/dm.h>
#include <drivers/misc.h>

#include "../ofw/ofw_internal.h"

static const struct rt_ofw_node_id platform_ofw_ids[] =
{
    { .compatible = "simple-bus", },
#ifdef RT_USING_MFD
    { .compatible = "simple-mfd", },
#endif
#ifdef RT_USING_ISA
    { .compatible = "isa", },
#endif
#ifdef RT_USING_AMBA_BUS
    /*
     * Maybe ARM has replaced it with compatible: "arm,primecell" and will not
     * used anymore in the future.
     */
    { .compatible = "arm,amba-bus", },
#endif
    { /* sentinel */ }
};

static void ofw_address_to_string(char *buffer, rt_size_t size, rt_uint64_t address)
{
    rt_uint32_t address_hi = rt_upper_32_bits(address);

    if (address_hi)
    {
        rt_snprintf(buffer, size, "%x%08x", address_hi, rt_lower_32_bits(address));
    }
    else
    {
        rt_snprintf(buffer, size, "%x", rt_lower_32_bits(address));
    }
}

static void ofw_device_rename(struct rt_device *dev)
{
    rt_uint32_t mask;
    rt_uint64_t addr;
    const char *dev_name = dev->parent.name;
    struct rt_ofw_node *np = dev->ofw_node;

#if RT_NAME_MAX > 0
    if (dev_name[0] == '\0')
    {
        dev_name = RT_NULL;
    }
#endif

    while (np->parent)
    {
        if (!rt_ofw_get_address(np, 0, &addr, RT_NULL))
        {
            char addr_str[sizeof(addr) * 2 + 1];
            const char *node_name = rt_fdt_node_name(np->full_name);
            rt_size_t tag_len = strchrnul(node_name, '@') - node_name;

            ofw_address_to_string(addr_str, sizeof(addr_str), addr);

            if (!rt_ofw_prop_read_u32(np, "mask", &mask))
            {
                rt_dm_dev_set_name(dev, dev_name ? "%s.%x.%.*s:%s" : "%s.%x.%.*s",
                    addr_str, __rt_ffs(mask) - 1, tag_len, node_name, dev_name);
            }
            else
            {
                rt_dm_dev_set_name(dev, dev_name ? "%s.%.*s:%s" : "%s.%.*s",
                    addr_str, tag_len, node_name, dev_name);
            }

            return;
        }

        rt_dm_dev_set_name(dev, dev_name ? "%s:%s" : "%s",
                rt_fdt_node_name(np->full_name), dev_name);

        np = np->parent;
    }
}

static struct rt_platform_device *alloc_ofw_platform_device(struct rt_ofw_node *np)
{
    struct rt_platform_device *pdev = rt_platform_device_alloc("");

    if (pdev)
    {
        /* inc reference of dt-node */
        rt_ofw_node_get(np);
        rt_ofw_node_set_flag(np, RT_OFW_F_PLATFORM);

        pdev->parent.ofw_node = np;

        ofw_device_rename(&pdev->parent);
    }
    else
    {
        LOG_E("Alloc device fail for %s", rt_ofw_node_full_name(np));
    }

    return pdev;
}

static rt_err_t platform_ofw_device_probe_once(struct rt_ofw_node *parent_np)
{
    rt_err_t err = RT_EOK;
    struct rt_ofw_node *np;
    struct rt_platform_device *pdev;

    rt_ofw_foreach_available_child_node(parent_np, np)
    {
        const char *name;
        struct rt_ofw_node_id *id;
        struct rt_ofw_prop *compat_prop = RT_NULL;

        if (np->dev)
        {
            /* Check first */
            continue;
        }

        LOG_D("%s found in %s", np->full_name, parent_np->full_name);

        /* Is system node or have driver */
        if (rt_ofw_node_test_flag(np, RT_OFW_F_SYSTEM) ||
            rt_ofw_node_test_flag(np, RT_OFW_F_READLY))
        {
            continue;
        }

        compat_prop = rt_ofw_get_prop(np, "compatible", RT_NULL);
        name = rt_ofw_node_name(np);

        /* Not have name and compatible */
        if (!compat_prop && (name == (const char *)"<NULL>" || !rt_strcmp(name, "<NULL>")))
        {
            continue;
        }

        id = rt_ofw_prop_match(compat_prop, platform_ofw_ids);

        if (id && np->child)
        {
            /* scan next level */
            err = platform_ofw_device_probe_once(np);

            if (err)
            {
                rt_ofw_node_put(np);
                LOG_E("%s bus probe fail", np->full_name);

                break;
            }
        }

        if (np->dev)
        {
            /* Maybe the childs have requested this node */
            continue;
        }

        pdev = alloc_ofw_platform_device(np);

        if (!pdev)
        {
            rt_ofw_node_put(np);
            err = -RT_ENOMEM;

            break;
        }

        pdev->dev_id = ofw_alias_node_id(np);
        np->dev = &pdev->parent;
        LOG_D("%s register to bus", np->full_name);

        rt_platform_device_register(pdev);
    }

    return err;
}

rt_err_t rt_platform_ofw_device_probe_child(struct rt_ofw_node *np)
{
    rt_err_t err;
    struct rt_ofw_node *parent = rt_ofw_get_parent(np);

    if (parent && rt_strcmp(parent->name, "/") &&
        rt_ofw_get_prop(np, "compatible", RT_NULL) &&
        !rt_ofw_node_test_flag(np, RT_OFW_F_PLATFORM))
    {
        struct rt_platform_device *pdev = alloc_ofw_platform_device(np);

        if (pdev)
        {
            err = rt_platform_device_register(pdev);
        }
        else
        {
            err = -RT_ENOMEM;
        }
    }
    else
    {
        err = -RT_EINVAL;
    }

    rt_ofw_node_put(parent);

    return err;
}

rt_err_t rt_platform_ofw_request(struct rt_ofw_node *np)
{
    rt_err_t err;

    if (np)
    {
        struct rt_device *dev = np->dev;

        if (dev)
        {
            /*
             * Device was already created (np->dev != NULL).
             * - If it's already probed (dev->drv != NULL), nothing to do.
             * - Retry only devices created by the OFW platform scanner. Other
             *   devices belong to their native bus (e.g. I2C/SPI) and must not
             *   be transferred to or probed through the platform bus.
             */
            if (!dev->drv && rt_ofw_node_test_flag(np, RT_OFW_F_PLATFORM))
            {
                err = rt_bus_probe_device(dev);
            }
            else
            {
                err = RT_EOK;
            }
        }
        else
        {
            struct rt_ofw_node_id *id;
            struct rt_platform_device *pdev = alloc_ofw_platform_device(np);

            if (pdev)
            {
                pdev->dev_id = ofw_alias_node_id(np);
                np->dev = &pdev->parent;
                LOG_D("%s register to bus", np->full_name);

                err = rt_platform_device_register(pdev);

                if (!err && np->child && (id = rt_ofw_node_match(np, platform_ofw_ids)))
                {
                    rt_err_t sub_err;

                    if ((sub_err = platform_ofw_device_probe_once(np)))
                    {
                        LOG_W("%s child platform device probe failed", np->full_name);
                    }
                }
            }
            else
            {
                err = -RT_ENOMEM;
            }
        }
    }
    else
    {
        err = -RT_EINVAL;
    }

    return err;
}

static int platform_ofw_device_probe(void)
{
    rt_err_t err = RT_EOK;
    struct rt_ofw_node *node;

    if (ofw_node_root)
    {
        if ((node = rt_ofw_find_node_by_path("/clocks")))
        {
            platform_ofw_device_probe_once(node);
            rt_ofw_node_put(node);
        }

        rt_ofw_node_get(ofw_node_root);

        err = platform_ofw_device_probe_once(ofw_node_root);

        rt_ofw_node_put(ofw_node_root);

        if ((node = rt_ofw_find_node_by_path("/firmware")))
        {
            platform_ofw_device_probe_once(node);
            rt_ofw_node_put(node);
        }

        rt_ofw_node_get(ofw_node_chosen);
        if ((node = rt_ofw_get_child_by_compatible(ofw_node_chosen, "simple-framebuffer")))
        {
            platform_ofw_device_probe_once(node);
            rt_ofw_node_put(node);
        }
        rt_ofw_node_get(ofw_node_chosen);
    }
    else
    {
        err = -RT_ENOSYS;
    }

    return (int)err;
}
INIT_PLATFORM_EXPORT(platform_ofw_device_probe);

rt_err_t rt_platform_ofw_free(struct rt_platform_device *pdev)
{
    rt_err_t err = RT_EOK;

    if (pdev)
    {
        struct rt_ofw_node *np = pdev->parent.ofw_node;

        if (np)
        {
            rt_ofw_node_clear_flag(np, RT_OFW_F_PLATFORM);
            rt_ofw_node_put(np);

            rt_free(pdev);
        }
    }
    else
    {
        err = -RT_EINVAL;
    }

    return err;
}