Skip to content

Commit 4609da1

Browse files
committed
modify platform_ofw
1 parent 04b2c81 commit 4609da1

File tree

4 files changed

+110
-17
lines changed

4 files changed

+110
-17
lines changed

components/drivers/core/platform_ofw.c

+79-17
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <drivers/platform.h>
1818
#include <drivers/core/dm.h>
1919

20+
#include "../ofw/ofw_internal.h"
21+
2022
static const struct rt_ofw_node_id platform_ofw_ids[] =
2123
{
2224
{ .compatible = "simple-bus", },
@@ -35,11 +37,32 @@ static const struct rt_ofw_node_id platform_ofw_ids[] =
3537
#endif
3638
{ /* sentinel */ }
3739
};
40+
static struct rt_platform_device *alloc_ofw_platform_device(struct rt_ofw_node *np)
41+
{
42+
struct rt_platform_device *pdev = rt_platform_device_alloc(np->name);
43+
44+
if (pdev)
45+
{
46+
/* inc reference of dt-node */
47+
rt_ofw_node_get(np);
48+
rt_ofw_node_set_flag(np, RT_OFW_F_PLATFORM);
49+
50+
#ifdef RT_USING_OFW
51+
pdev->parent.ofw_node = np;
52+
#endif
53+
}
54+
else
55+
{
56+
LOG_E("Alloc device fail for %s", rt_ofw_node_full_name(np));
57+
}
58+
59+
return pdev;
60+
}
3861

3962
static rt_err_t platform_ofw_device_probe_once(struct rt_ofw_node *parent_np)
4063
{
4164
rt_err_t err = RT_EOK;
42-
struct rt_ofw_node *np, *child;
65+
struct rt_ofw_node *np;
4366
struct rt_platform_device *pdev;
4467

4568
rt_ofw_foreach_available_child_node(parent_np, np)
@@ -48,6 +71,8 @@ static rt_err_t platform_ofw_device_probe_once(struct rt_ofw_node *parent_np)
4871
struct rt_ofw_node_id *id;
4972
struct rt_ofw_prop *compat_prop = RT_NULL;
5073

74+
LOG_D("%s found in %s", np->full_name, parent_np->full_name);
75+
5176
/* Is system node or have driver */
5277
if (rt_ofw_node_test_flag(np, RT_OFW_F_SYSTEM) ||
5378
rt_ofw_node_test_flag(np, RT_OFW_F_READLY))
@@ -66,54 +91,91 @@ static rt_err_t platform_ofw_device_probe_once(struct rt_ofw_node *parent_np)
6691

6792
id = rt_ofw_prop_match(compat_prop, platform_ofw_ids);
6893

69-
if (id && (child = rt_ofw_get_next_child(np, RT_NULL)))
94+
if (id && np->child)
7095
{
7196
/* scan next level */
72-
err = platform_ofw_device_probe_once(child);
73-
74-
rt_ofw_node_put(child);
97+
err = platform_ofw_device_probe_once(np);
7598

7699
if (err)
77100
{
101+
rt_ofw_node_put(np);
78102
LOG_E("%s bus probe fail", np->full_name);
79103

80104
break;
81105
}
82106
}
83107

84-
pdev = rt_platform_device_alloc(np->name);
108+
pdev = alloc_ofw_platform_device(np);
85109

86110
if (!pdev)
87111
{
112+
rt_ofw_node_put(np);
88113
err = -RT_ENOMEM;
89114

90115
break;
91116
}
92117

93-
/* inc reference of dt-node */
94-
rt_ofw_node_get(np);
95-
rt_ofw_node_set_flag(np, RT_OFW_F_PLATFORM);
96-
97-
pdev->parent.ofw_node = np;
118+
pdev->dev_id = ofw_alias_node_id(np);
119+
LOG_D("%s register to bus", np->full_name);
98120

99121
rt_platform_device_register(pdev);
100122
}
101123

102124
return err;
103125
}
104126

127+
rt_err_t rt_platform_ofw_device_probe_child(struct rt_ofw_node *np)
128+
{
129+
rt_err_t err;
130+
struct rt_ofw_node *parent = rt_ofw_get_parent(np);
131+
132+
if (parent && rt_strcmp(parent->name, "/") &&
133+
rt_ofw_get_prop(np, "compatible", RT_NULL) &&
134+
!rt_ofw_node_test_flag(np, RT_OFW_F_PLATFORM))
135+
{
136+
struct rt_platform_device *pdev = alloc_ofw_platform_device(np);
137+
138+
if (pdev)
139+
{
140+
err = rt_platform_device_register(pdev);
141+
}
142+
else
143+
{
144+
err = -RT_ENOMEM;
145+
}
146+
}
147+
else
148+
{
149+
err = -RT_EINVAL;
150+
}
151+
152+
rt_ofw_node_put(parent);
153+
154+
return err;
155+
}
156+
105157
static int platform_ofw_device_probe(void)
106158
{
107159
rt_err_t err = RT_EOK;
108-
struct rt_ofw_node *root_np;
109-
110-
root_np = rt_ofw_find_node_by_path("/");
160+
struct rt_ofw_node *node;
111161

112-
if (root_np)
162+
if (ofw_node_root)
113163
{
114-
err = platform_ofw_device_probe_once(root_np);
164+
err = platform_ofw_device_probe_once(ofw_node_root);
115165

116-
rt_ofw_node_put(root_np);
166+
rt_ofw_node_put(ofw_node_root);
167+
168+
if ((node = rt_ofw_find_node_by_path("/firmware")))
169+
{
170+
platform_ofw_device_probe_once(node);
171+
rt_ofw_node_put(node);
172+
}
173+
174+
if ((node = rt_ofw_get_child_by_compatible(ofw_node_chosen, "simple-framebuffer")))
175+
{
176+
platform_ofw_device_probe_once(node);
177+
rt_ofw_node_put(node);
178+
}
117179
}
118180
else
119181
{

components/drivers/include/drivers/platform.h

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ struct rt_platform_device
2222
{
2323
struct rt_device parent;
2424

25+
int dev_id;
26+
2527
const char *name;
2628

2729
#ifdef RT_USING_OFW
@@ -49,6 +51,8 @@ struct rt_platform_device *rt_platform_device_alloc(const char *name);
4951
rt_err_t rt_platform_driver_register(struct rt_platform_driver *pdrv);
5052
rt_err_t rt_platform_device_register(struct rt_platform_device *pdev);
5153

54+
rt_err_t rt_platform_ofw_device_probe_child(struct rt_ofw_node *np);
55+
5256
#define RT_PLATFORM_DRIVER_EXPORT(driver) RT_DRIVER_EXPORT(driver, platform, BUILIN)
5357

5458
#endif /* __PLATFORM_H__ */

components/drivers/ofw/base.c

+26
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,32 @@ struct rt_ofw_node *rt_ofw_get_alias_node(const char *tag, int id)
10921092
return np;
10931093
}
10941094

1095+
int ofw_alias_node_id(struct rt_ofw_node *np)
1096+
{
1097+
int id;
1098+
struct alias_info *info;
1099+
1100+
if (np)
1101+
{
1102+
id = -1;
1103+
1104+
rt_list_for_each_entry(info, &_aliases_nodes, list)
1105+
{
1106+
if (info->np == np)
1107+
{
1108+
id = info->id;
1109+
break;
1110+
}
1111+
}
1112+
}
1113+
else
1114+
{
1115+
id = -RT_EINVAL;
1116+
}
1117+
1118+
return id;
1119+
}
1120+
10951121
int rt_ofw_get_alias_id(struct rt_ofw_node *np, const char *tag)
10961122
{
10971123
int id;

components/drivers/ofw/ofw_internal.h

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ extern struct rt_fdt_earlycon fdt_earlycon;
6868
(to_type)(((value) >> ((sizeof(value) - sizeof(to_type)) * 8)))
6969

7070
rt_err_t ofw_alias_scan(void);
71+
int ofw_alias_node_id(struct rt_ofw_node *np);
7172
rt_err_t ofw_phandle_hash_reset(rt_phandle min, rt_phandle max);
7273

7374
#endif /* __OFW_INTERNAL_H__ */

0 commit comments

Comments
 (0)