12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511 |
- /*
- * Copyright (c) 2006-2022, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2022-08-25 GuEe-GUI first version
- */
- #include <rtthread.h>
- #include <drivers/ofw.h>
- #include <drivers/ofw_io.h>
- #include <drivers/ofw_fdt.h>
- #include <drivers/ofw_raw.h>
- #define DBG_TAG "rtdm.ofw"
- #define DBG_LVL DBG_INFO
- #include <rtdbg.h>
- #include "ofw_internal.h"
- struct rt_ofw_node *ofw_node_root = RT_NULL;
- struct rt_ofw_node *ofw_node_cpus = RT_NULL;
- struct rt_ofw_node *ofw_node_chosen = RT_NULL;
- struct rt_ofw_node *ofw_node_aliases = RT_NULL;
- struct rt_ofw_node *ofw_node_reserved_memory = RT_NULL;
- static rt_phandle _phandle_range[2] = { 1, 1 };
- static struct rt_ofw_node **_phandle_hash = RT_NULL;
- static rt_list_t _aliases_nodes = RT_LIST_OBJECT_INIT(_aliases_nodes);
- rt_err_t ofw_phandle_hash_reset(rt_phandle min, rt_phandle max)
- {
- rt_err_t err = RT_EOK;
- struct rt_ofw_node **hash_ptr = RT_NULL;
- max = RT_ALIGN(max, OFW_NODE_MIN_HASH);
- if (max > _phandle_range[1])
- {
- rt_size_t size = sizeof(*_phandle_hash) * (max - min);
- if (!_phandle_hash)
- {
- hash_ptr = rt_calloc(1, size);
- }
- else
- {
- hash_ptr = rt_realloc(_phandle_hash, size);
- if (hash_ptr)
- {
- rt_size_t old_max = _phandle_range[1];
- rt_memset(&hash_ptr[old_max], 0, sizeof(_phandle_hash) * (max - old_max));
- }
- }
- }
- if (hash_ptr)
- {
- /* We always reset min value only once */
- if (min)
- {
- _phandle_range[0] = min;
- }
- _phandle_range[1] = max;
- _phandle_hash = hash_ptr;
- }
- else
- {
- err = -RT_ENOMEM;
- }
- return err;
- }
- static void ofw_prop_destroy(struct rt_ofw_prop *prop)
- {
- struct rt_ofw_prop *next;
- while (prop)
- {
- next = prop->next;
- rt_free(prop);
- prop = next;
- }
- }
- static struct rt_ofw_node *ofw_get_next_node(struct rt_ofw_node *prev)
- {
- struct rt_ofw_node *np;
- /*
- * Walk:
- *
- * / { ------------------------ [0] (START) has child, goto child.
- *
- * node0 { ---------------- [1] has child, goto child.
- *
- * node0_0 { ---------- [2] no child, has sibling, goto sibling.
- * };
- *
- * node0_1 { ---------- [3] no sibling now.
- * upward while the parent has sibling.
- * };
- * };
- *
- * node1 { ---------------- [4] come from node0 who find the sibling:
- * node1, node1 has child, goto child.
- *
- * node1_0 { ---------- [5] has child, goto child.
- *
- * node1_0_0 { ---- [6] no sibling now.
- * upward while the parent has sibling.
- * (END) in the root.
- * };
- * };
- * };
- * };
- */
- if (!prev)
- {
- np = ofw_node_root;
- }
- else if (prev->child)
- {
- np = prev->child;
- }
- else
- {
- np = prev;
- while (np->parent && !np->sibling)
- {
- np = np->parent;
- }
- np = np->sibling;
- }
- return np;
- }
- static void ofw_node_destroy(struct rt_ofw_node *np)
- {
- struct rt_ofw_node *prev;
- if (np->parent)
- {
- /* Ask parent and prev sibling we are destroy. */
- prev = np->parent->child;
- if (prev == np)
- {
- np->parent->child = RT_NULL;
- }
- else
- {
- while (prev->sibling != np)
- {
- prev = prev->sibling;
- }
- prev->sibling = np->sibling;
- }
- }
- while (np)
- {
- if (rt_ofw_node_test_flag(np, RT_OFW_F_SYSTEM) == RT_FALSE)
- {
- LOG_E("%s is system node", np->full_name);
- RT_ASSERT(0);
- }
- prev = np;
- np = ofw_get_next_node(np);
- ofw_prop_destroy(prev->props);
- rt_free(prev);
- }
- }
- rt_err_t rt_ofw_node_destroy(struct rt_ofw_node *np)
- {
- rt_err_t err = RT_EOK;
- if (np)
- {
- if (rt_ref_read(&np->ref) <= 1)
- {
- ofw_node_destroy(np);
- }
- else
- {
- err = -RT_EBUSY;
- }
- }
- else
- {
- err = -RT_EINVAL;
- }
- return err;
- }
- struct rt_ofw_node *rt_ofw_node_get(struct rt_ofw_node *np)
- {
- if (np)
- {
- LOG_D("%s get ref = %d", np->full_name, rt_ref_read(&np->ref));
- rt_ref_get(&np->ref);
- }
- return np;
- }
- static void ofw_node_release(struct rt_ref *r)
- {
- struct rt_ofw_node *np = rt_container_of(r, struct rt_ofw_node, ref);
- LOG_E("%s is release", np->full_name);
- RT_ASSERT(0);
- }
- void rt_ofw_node_put(struct rt_ofw_node *np)
- {
- if (np)
- {
- LOG_D("%s put ref = %d", np->full_name, rt_ref_read(&np->ref));
- rt_ref_put(&np->ref, &ofw_node_release);
- }
- }
- rt_bool_t rt_ofw_node_tag_equ(const struct rt_ofw_node *np, const char *tag)
- {
- rt_bool_t ret = RT_FALSE;
- if (np && tag)
- {
- const char *node_name = rt_fdt_node_name(np->full_name);
- rt_size_t tag_len = rt_strchrnul(node_name, '@') - node_name;
- ret = (rt_strlen(tag) == tag_len && !rt_strncmp(node_name, tag, tag_len));
- }
- return ret;
- }
- rt_bool_t rt_ofw_node_tag_prefix(const struct rt_ofw_node *np, const char *prefix)
- {
- rt_bool_t ret = RT_FALSE;
- if (np && prefix)
- {
- ret = !rt_strncmp(rt_fdt_node_name(np->full_name), prefix, rt_strlen(prefix));
- }
- return ret;
- }
- static int ofw_prop_index_of_string(struct rt_ofw_prop *prop, const char *string,
- rt_int32_t (*cmp)(const char *cs, const char *ct))
- {
- int index = -1;
- rt_size_t len = prop->length, slen = 0;
- const char *value = prop->value;
- for (int idx = 0; len > 0; ++idx)
- {
- /* Add '\0' */
- slen = rt_strlen(value) + 1;
- if (!cmp(value, string))
- {
- index = idx;
- break;
- }
- len -= slen;
- value += slen;
- }
- return index;
- }
- static rt_int32_t ofw_strcasecmp(const char *cs, const char *ct)
- {
- extern rt_int32_t strcasecmp(const char *cs, const char *ct);
- return rt_strcasecmp(cs, ct);
- }
- static int ofw_prop_index_of_compatible(struct rt_ofw_prop *prop, const char *compatible)
- {
- return ofw_prop_index_of_string(prop, compatible, ofw_strcasecmp);
- }
- static int ofw_node_index_of_compatible(const struct rt_ofw_node *np, const char *compatible)
- {
- int idx = -1;
- struct rt_ofw_prop *prop = rt_ofw_get_prop(np, "compatible", RT_NULL);
- if (prop)
- {
- idx = ofw_prop_index_of_compatible(prop, compatible);
- }
- return idx;
- }
- rt_bool_t rt_ofw_machine_is_compatible(const char *compatible)
- {
- return ofw_node_index_of_compatible(ofw_node_root, compatible) >= 0;
- }
- /*
- * Property status:
- *
- * "okay" or "ok":
- * Indicates the device is operational.
- *
- * "disabled":
- * Indicates that the device is not presently operational, but it might
- * become operational in the future (for example, something is not
- * plugged in, or switched off).
- * Refer to the device binding for details on what disabled means for a
- * given device.
- *
- * "reserved":
- * Indicates that the device is operational, but should not be used.
- * Typically this is used for devices that are controlled by another
- * software component, such as platform firmware.
- *
- * "fail":
- * Indicates that the device is not operational. A serious error was
- * detected in the device, and it is unlikely to become operational
- * without repair.
- *
- * "fail-sss":
- * Indicates that the device is not operational. A serious error was
- * detected in the device and it is unlikely to become operational
- * without repair. The sss portion of the value is specific to the
- * device and indicates the error condition detected.
- */
- static rt_bool_t ofw_node_is_fail(const struct rt_ofw_node *np)
- {
- rt_bool_t res = RT_FALSE;
- const char *status = rt_ofw_prop_read_raw(np, "status", RT_NULL);
- if (status)
- {
- res = !rt_strcmp(status, "fail") || !rt_strncmp(status, "fail-", 5);
- }
- return res;
- }
- static rt_bool_t ofw_node_is_available(const struct rt_ofw_node *np)
- {
- rt_bool_t res = RT_TRUE;
- const char *status = rt_ofw_prop_read_raw(np, "status", RT_NULL);
- if (status)
- {
- res = !rt_strcmp(status, "okay") || !rt_strcmp(status, "ok");
- }
- return res;
- }
- rt_bool_t rt_ofw_node_is_available(const struct rt_ofw_node *np)
- {
- return np ? ofw_node_is_available(np) : RT_FALSE;
- }
- rt_bool_t rt_ofw_node_is_compatible(const struct rt_ofw_node *np, const char *compatible)
- {
- rt_bool_t res = RT_FALSE;
- if (np)
- {
- res = ofw_node_index_of_compatible(np, compatible) >= 0;
- }
- return res;
- }
- static struct rt_ofw_node_id *ofw_prop_match(struct rt_ofw_prop *prop, const struct rt_ofw_node_id *ids)
- {
- int best_index = RT_UINT32_MAX >> 1, index;
- struct rt_ofw_node_id *found_id = RT_NULL, *id;
- for (id = (struct rt_ofw_node_id *)ids; id->compatible[0]; ++id)
- {
- index = ofw_prop_index_of_compatible(prop, id->compatible);
- if (index >= 0 && index < best_index)
- {
- found_id = id;
- best_index = index;
- }
- }
- return found_id;
- }
- struct rt_ofw_node_id *rt_ofw_prop_match(struct rt_ofw_prop *prop, const struct rt_ofw_node_id *ids)
- {
- struct rt_ofw_node_id *id = RT_NULL;
- if (prop && ids && !rt_strcmp(prop->name, "compatible"))
- {
- id = ofw_prop_match(prop, ids);
- }
- return id;
- }
- struct rt_ofw_node_id *rt_ofw_node_match(struct rt_ofw_node *np, const struct rt_ofw_node_id *ids)
- {
- struct rt_ofw_prop *prop;
- struct rt_ofw_node_id *id = RT_NULL;
- if (np && ids && (prop = rt_ofw_get_prop(np, "compatible", RT_NULL)))
- {
- id = ofw_prop_match(prop, ids);
- }
- return id;
- }
- struct rt_ofw_node *rt_ofw_find_node_by_tag(struct rt_ofw_node *from, const char *tag)
- {
- struct rt_ofw_node *np = RT_NULL;
- if (tag)
- {
- rt_ofw_foreach_nodes(from, np)
- {
- if (rt_ofw_node_tag_equ(np, tag))
- {
- break;
- }
- }
- }
- return np;
- }
- struct rt_ofw_node *rt_ofw_find_node_by_prop_r(struct rt_ofw_node *from, const char *propname,
- const struct rt_ofw_prop **out_prop)
- {
- struct rt_ofw_node *np = RT_NULL;
- if (propname)
- {
- rt_ofw_foreach_nodes(from, np)
- {
- struct rt_ofw_prop *prop = rt_ofw_get_prop(np, propname, RT_NULL);
- if (prop)
- {
- if (out_prop)
- {
- *out_prop = prop;
- }
- break;
- }
- }
- }
- return np;
- }
- struct rt_ofw_node *rt_ofw_find_node_by_name(struct rt_ofw_node *from, const char *name)
- {
- struct rt_ofw_node *np = RT_NULL;
- if (name)
- {
- rt_ofw_foreach_nodes(from, np)
- {
- if (np->name && !rt_strcmp(np->name, name))
- {
- np = rt_ofw_node_get(np);
- break;
- }
- }
- }
- return np;
- }
- struct rt_ofw_node *rt_ofw_find_node_by_type(struct rt_ofw_node *from, const char *type)
- {
- struct rt_ofw_node *np = RT_NULL;
- if (type)
- {
- rt_ofw_foreach_nodes(from, np)
- {
- if (rt_ofw_node_is_type(np, type))
- {
- break;
- }
- }
- }
- return np;
- }
- struct rt_ofw_node *rt_ofw_find_node_by_compatible(struct rt_ofw_node *from, const char *compatible)
- {
- struct rt_ofw_node *np = RT_NULL;
- if (compatible)
- {
- rt_ofw_foreach_nodes(from, np)
- {
- if (ofw_node_index_of_compatible(np, compatible) >= 0)
- {
- break;
- }
- }
- }
- return np;
- }
- struct rt_ofw_node *rt_ofw_find_node_by_ids_r(struct rt_ofw_node *from, const struct rt_ofw_node_id *ids,
- const struct rt_ofw_node_id **out_id)
- {
- struct rt_ofw_node *np = RT_NULL;
- if (ids)
- {
- rt_ofw_foreach_nodes(from, np)
- {
- struct rt_ofw_node_id *id = rt_ofw_node_match(np, ids);
- if (id)
- {
- if (out_id)
- {
- *out_id = id;
- }
- break;
- }
- }
- }
- return np;
- }
- struct rt_ofw_node *rt_ofw_find_node_by_path(const char *path)
- {
- struct rt_ofw_node *np, *parent, *tmp;
- if (path)
- {
- if (!rt_strcmp(path, "/"))
- {
- np = ofw_node_root;
- }
- else
- {
- ++path;
- parent = rt_ofw_node_get(ofw_node_root);
- while (*path)
- {
- const char *next = rt_strchrnul(path, '/');
- rt_size_t len = next - path;
- tmp = RT_NULL;
- rt_ofw_foreach_child_node(parent, np)
- {
- if (!rt_strncmp(np->full_name, path, len))
- {
- rt_ofw_node_put(parent);
- parent = np;
- tmp = np;
- break;
- }
- }
- if (!tmp)
- {
- rt_ofw_node_put(parent);
- break;
- }
- path += len;
- }
- np = tmp;
- }
- rt_ofw_node_get(np);
- }
- return np;
- }
- struct rt_ofw_node *rt_ofw_find_node_by_phandle(rt_phandle phandle)
- {
- struct rt_ofw_node *np = RT_NULL;
- if (phandle >= OFW_PHANDLE_MIN && phandle <= OFW_PHANDLE_MAX)
- {
- /* rebase from zero */
- rt_phandle poff = phandle - _phandle_range[0];
- np = _phandle_hash[poff];
- if (!np)
- {
- rt_ofw_foreach_allnodes(np)
- {
- if (np->phandle == phandle)
- {
- _phandle_hash[poff] = np;
- break;
- }
- }
- }
- else
- {
- rt_ofw_node_get(np);
- }
- }
- return np;
- }
- struct rt_ofw_node *rt_ofw_get_parent(const struct rt_ofw_node *np)
- {
- if (np)
- {
- np = rt_ofw_node_get(np->parent);
- }
- return (struct rt_ofw_node *)np;
- }
- struct rt_ofw_node *rt_ofw_get_child_by_tag(const struct rt_ofw_node *parent, const char *tag)
- {
- struct rt_ofw_node *child = RT_NULL;
- if (parent && tag)
- {
- rt_ofw_foreach_child_node(parent, child)
- {
- if (rt_ofw_node_tag_equ(child, tag))
- {
- break;
- }
- }
- }
- return child;
- }
- struct rt_ofw_node *rt_ofw_get_child_by_compatible(const struct rt_ofw_node *parent, const char *compatible)
- {
- struct rt_ofw_node *child = RT_NULL;
- if (parent && compatible)
- {
- rt_ofw_foreach_child_node(parent, child)
- {
- if (ofw_node_index_of_compatible(child, compatible) >= 0)
- {
- break;
- }
- }
- }
- return child;
- }
- int rt_ofw_get_child_count(const struct rt_ofw_node *np)
- {
- int nr;
- if (np)
- {
- struct rt_ofw_node *child;
- nr = 0;
- rt_ofw_foreach_child_node(np, child)
- {
- ++nr;
- }
- }
- else
- {
- nr = -RT_EINVAL;
- }
- return nr;
- }
- int rt_ofw_get_available_child_count(const struct rt_ofw_node *np)
- {
- int nr;
- if (np)
- {
- struct rt_ofw_node *child;
- nr = 0;
- rt_ofw_foreach_available_child_node(np, child)
- {
- ++nr;
- }
- }
- else
- {
- nr = -RT_EINVAL;
- }
- return nr;
- }
- struct rt_ofw_node *rt_ofw_get_next_node(struct rt_ofw_node *prev)
- {
- struct rt_ofw_node *np;
- np = rt_ofw_node_get(ofw_get_next_node(prev));
- rt_ofw_node_put(prev);
- return np;
- }
- struct rt_ofw_node *rt_ofw_get_next_parent(struct rt_ofw_node *prev)
- {
- struct rt_ofw_node *next = RT_NULL;
- if (prev)
- {
- next = rt_ofw_node_get(prev->parent);
- rt_ofw_node_put(prev);
- }
- return next;
- }
- struct rt_ofw_node *rt_ofw_get_next_child(const struct rt_ofw_node *parent, struct rt_ofw_node *prev)
- {
- struct rt_ofw_node *next = RT_NULL;
- if (parent)
- {
- next = prev ? prev->sibling : parent->child;
- rt_ofw_node_put(prev);
- rt_ofw_node_get(next);
- }
- return next;
- }
- struct rt_ofw_node *rt_ofw_get_next_available_child(const struct rt_ofw_node *parent, struct rt_ofw_node *prev)
- {
- struct rt_ofw_node *next = RT_NULL;
- if (parent)
- {
- next = prev;
- do {
- next = rt_ofw_get_next_child(parent, next);
- } while (next && !ofw_node_is_available(next));
- }
- return next;
- }
- struct rt_ofw_node *rt_ofw_get_cpu_node(int cpu, int *thread, rt_bool_t (*match_cpu_hwid)(int cpu, rt_uint64_t hwid))
- {
- const char *propname = "reg";
- struct rt_ofw_node *cpu_np = RT_NULL;
- /*
- * "reg" (some of the obsolete arch may be other names):
- * The value of reg is a <prop-encoded-array> that defines a unique
- * CPU/thread id for the CPU/threads represented by the CPU node.
- *
- * If a CPU supports more than one thread (i.e. multiple streams of
- * execution) the reg property is an array with 1 element per thread. The
- * #address-cells on the /cpus node specifies how many cells each element
- * of the array takes. Software can determine the number of threads by
- * dividing the size of reg by the parent node’s #address-cells:
- *
- * thread-number = reg-cells / address-cells
- *
- * If a CPU/thread can be the target of an external interrupt the reg
- * property value must be a unique CPU/thread id that is addressable by the
- * interrupt controller.
- *
- * If a CPU/thread cannot be the target of an external interrupt, then reg
- * must be unique and out of bounds of the range addressed by the interrupt
- * controller
- *
- * If a CPU/thread’s PIR (pending interrupt register) is modifiable, a
- * client program should modify PIR to match the reg property value. If PIR
- * cannot be modified and the PIR value is distinct from the interrupt
- * controller number space, the CPUs binding may define a binding-specific
- * representation of PIR values if desired.
- */
- rt_ofw_foreach_cpu_node(cpu_np)
- {
- rt_ssize_t prop_len;
- rt_bool_t is_end = RT_FALSE;
- int tid, addr_cells = rt_ofw_io_addr_cells(cpu_np);
- const fdt32_t *cell = rt_ofw_prop_read_raw(cpu_np, propname, &prop_len);
- if (!cell && !addr_cells)
- {
- if (match_cpu_hwid && match_cpu_hwid(cpu, 0))
- {
- break;
- }
- continue;
- }
- if (!match_cpu_hwid)
- {
- continue;
- }
- prop_len /= sizeof(*cell) * addr_cells;
- for (tid = 0; tid < prop_len; ++tid)
- {
- rt_uint64_t hwid = rt_fdt_read_number(cell, addr_cells);
- if (match_cpu_hwid(cpu, hwid))
- {
- if (thread)
- {
- *thread = tid;
- }
- is_end = RT_TRUE;
- break;
- }
- cell += addr_cells;
- }
- if (is_end)
- {
- break;
- }
- }
- return cpu_np;
- }
- struct rt_ofw_node *rt_ofw_get_next_cpu_node(struct rt_ofw_node *prev)
- {
- struct rt_ofw_node *cpu_np;
- if (prev)
- {
- cpu_np = prev->sibling;
- rt_ofw_node_put(prev);
- }
- else
- {
- cpu_np = ofw_node_cpus->child;
- }
- for (; cpu_np; cpu_np = cpu_np->sibling)
- {
- if (ofw_node_is_fail(cpu_np))
- {
- continue;
- }
- if (!(rt_ofw_node_tag_equ(cpu_np, "cpu") || rt_ofw_node_is_type(cpu_np, "cpu")))
- {
- continue;
- }
- if (rt_ofw_node_get(cpu_np))
- {
- break;
- }
- }
- return cpu_np;
- }
- struct rt_ofw_node *rt_ofw_get_cpu_state_node(struct rt_ofw_node *cpu_np, int index)
- {
- struct rt_ofw_cell_args args;
- struct rt_ofw_node *np = RT_NULL, *state_np;
- rt_err_t err = rt_ofw_parse_phandle_cells(cpu_np, "power-domains", "#power-domain-cells", 0, &args);
- if (!err)
- {
- state_np = rt_ofw_parse_phandle(args.data, "domain-idle-states", index);
- rt_ofw_node_put(args.data);
- if (state_np)
- {
- np = state_np;
- }
- }
- if (!np)
- {
- int count = 0;
- rt_uint32_t phandle;
- const fdt32_t *cell;
- struct rt_ofw_prop *prop;
- rt_ofw_foreach_prop_u32(cpu_np, "cpu-idle-states", prop, cell, phandle)
- {
- if (count == index)
- {
- np = rt_ofw_find_node_by_phandle((rt_phandle)phandle);
- break;
- }
- ++count;
- }
- }
- return np;
- }
- rt_uint64_t rt_ofw_get_cpu_id(struct rt_ofw_node *cpu_np)
- {
- rt_uint64_t cpuid = ~0ULL;
- if (cpu_np)
- {
- rt_uint64_t idx = 0;
- struct rt_ofw_node *np = ofw_node_cpus->child;
- for (; np; np = np->sibling)
- {
- if (!(rt_ofw_node_tag_equ(cpu_np, "cpu") || rt_ofw_node_is_type(cpu_np, "cpu")))
- {
- continue;
- }
- if (cpu_np == np)
- {
- cpuid = idx;
- break;
- }
- ++idx;
- }
- if ((rt_int64_t)cpuid < 0 && !rt_ofw_prop_read_u64(cpu_np, "rt-thread,cpuid", &idx))
- {
- cpuid = idx;
- }
- }
- return cpuid;
- }
- rt_uint64_t rt_ofw_get_cpu_hwid(struct rt_ofw_node *cpu_np, unsigned int thread)
- {
- rt_uint64_t thread_id, hwid = ~0ULL;
- if (cpu_np && thread >= 0 && !rt_ofw_get_address(cpu_np, thread, &thread_id, RT_NULL))
- {
- hwid = thread_id;
- }
- return hwid;
- }
- rt_err_t ofw_alias_scan(void)
- {
- rt_err_t err = RT_EOK;
- struct rt_ofw_prop *prop;
- struct rt_ofw_node *np = ofw_node_aliases, *tmp;
- rt_ofw_foreach_prop(np, prop)
- {
- int id = 0, rate = 1;
- struct alias_info *info;
- const char *name = prop->name, *end;
- /* Maybe the bootloader will set the name, or other nodes reference the aliases */
- if (!rt_strcmp(name, "name") || !rt_strcmp(name, "phandle"))
- {
- continue;
- }
- if (!(tmp = rt_ofw_find_node_by_path(prop->value)))
- {
- continue;
- }
- end = name + rt_strlen(name);
- while (*end && !(*end >= '0' && *end <= '9'))
- {
- --end;
- }
- while (*end && (*end >= '0' && *end <= '9'))
- {
- id += (*end - '0') * rate;
- rate *= 10;
- --end;
- }
- info = rt_malloc(sizeof(*info));
- if (!info)
- {
- err = -RT_ENOMEM;
- break;
- }
- rt_list_init(&info->list);
- info->id = id;
- info->tag = name;
- info->tag_len = end - name;
- info->np = tmp;
- rt_list_insert_after(&_aliases_nodes, &info->list);
- }
- return err;
- }
- struct rt_ofw_node *rt_ofw_get_alias_node(const char *tag, int id)
- {
- struct alias_info *info;
- struct rt_ofw_node *np = RT_NULL;
- if (tag && id >= 0)
- {
- rt_list_for_each_entry(info, &_aliases_nodes, list)
- {
- if (rt_strncmp(info->tag, tag, info->tag_len))
- {
- continue;
- }
- if (info->id == id)
- {
- np = info->np;
- break;
- }
- }
- }
- return np;
- }
- int rt_ofw_get_alias_id(struct rt_ofw_node *np, const char *tag)
- {
- int id;
- struct alias_info *info;
- if (np && tag)
- {
- id = -1;
- rt_list_for_each_entry(info, &_aliases_nodes, list)
- {
- if (rt_strncmp(info->tag, tag, info->tag_len))
- {
- continue;
- }
- if (info->np == np)
- {
- id = info->id;
- break;
- }
- }
- }
- else
- {
- id = -RT_EINVAL;
- }
- return id;
- }
- int rt_ofw_get_alias_last_id(const char *tag)
- {
- int id;
- struct alias_info *info;
- if (tag)
- {
- id = -1;
- rt_list_for_each_entry(info, &_aliases_nodes, list)
- {
- if (rt_strncmp(info->tag, tag, info->tag_len))
- {
- continue;
- }
- if (info->id > id)
- {
- id = info->id;
- }
- }
- }
- else
- {
- id = -RT_EINVAL;
- }
- return id;
- }
- struct rt_ofw_node *rt_ofw_parse_phandle(const struct rt_ofw_node *np, const char *phandle_name, int index)
- {
- struct rt_ofw_cell_args args;
- struct rt_ofw_node *ref_np = RT_NULL;
- if (!rt_ofw_parse_phandle_cells(np, phandle_name, RT_NULL, index, &args))
- {
- ref_np = args.data;
- }
- return ref_np;
- }
- static rt_err_t ofw_parse_phandle_cells(const struct rt_ofw_node *np, const char *list_name, const char *cells_name,
- int index, struct rt_ofw_cell_args *out_args)
- {
- rt_err_t err = -RT_EEMPTY;
- rt_uint32_t value;
- rt_size_t count = 0;
- const fdt32_t *cell;
- struct rt_ofw_prop *prop;
- /*
- * List:
- *
- * phandle1: node1 {
- * #list-cells = <2>;
- * };
- *
- * phandle2: node2 {
- * #list-cells = <1>;
- * };
- *
- * node3 {
- * list = <&phandle1 0xaa 0xbb>, <&phandle2 0xcc>;
- * };
- *
- * if call:
- * rt_ofw_parse_phandle_cells(node3, "list", "#list-cells", 0, &args):
- *
- * args.data = node1;
- * args.args_count = 2;
- * args.args[0] = 0xaa;
- * args.args[1] = 0xbb;
- *
- * rt_ofw_parse_phandle_cells(node3, "list", "#list-cells", 1, &args):
- *
- * args.data = node2;
- * args.args_count = 1;
- * args.args[0] = 0xcc;
- */
- rt_ofw_foreach_prop_u32(np, list_name, prop, cell, value)
- {
- rt_uint32_t cells_count = 0;
- struct rt_ofw_node *phandle_np = rt_ofw_find_node_by_phandle((rt_phandle)value);
- /* if phandle node is undefined, we assume that the cels_count is 0 */
- if (cells_name && phandle_np)
- {
- rt_ofw_prop_read_u32(phandle_np, cells_name, &cells_count);
- }
- if (count++ == index)
- {
- for (int idx = 0; idx < cells_count; ++idx)
- {
- cell = rt_ofw_prop_next_u32(prop, cell, &value);
- out_args->args[idx] = value;
- }
- out_args->args_count = cells_count;
- out_args->data = phandle_np;
- if (out_args->data)
- {
- err = RT_EOK;
- }
- break;
- }
- cell += cells_count;
- }
- return err;
- }
- rt_err_t rt_ofw_parse_phandle_cells(const struct rt_ofw_node *np, const char *list_name, const char *cells_name,
- int index, struct rt_ofw_cell_args *out_args)
- {
- rt_err_t err;
- if (np && list_name && index >= 0 && out_args)
- {
- err = ofw_parse_phandle_cells(np, list_name, cells_name, index, out_args);
- }
- else
- {
- err = -RT_EINVAL;
- }
- return err;
- }
- int rt_ofw_count_phandle_cells(const struct rt_ofw_node *np, const char *list_name, const char *cells_name)
- {
- int count;
- if (np && list_name)
- {
- count = -1;
- if (!cells_name)
- {
- rt_ssize_t length;
- if (rt_ofw_get_prop(np, list_name, &length))
- {
- count = length / sizeof(fdt32_t);
- }
- }
- else
- {
- int index = count = 0;
- struct rt_ofw_cell_args args;
- while (!ofw_parse_phandle_cells(np, list_name, cells_name, index, &args))
- {
- ++index;
- ++count;
- }
- }
- }
- else
- {
- count = -RT_EINVAL;
- }
- return count;
- }
- struct rt_ofw_prop *rt_ofw_get_prop(const struct rt_ofw_node *np, const char *name, rt_ssize_t *out_length)
- {
- struct rt_ofw_prop *prop = RT_NULL;
- if (np && name)
- {
- rt_ofw_foreach_prop(np, prop)
- {
- if (!rt_strcmp(prop->name, name))
- {
- if (out_length)
- {
- *out_length = prop->length;
- }
- break;
- }
- }
- }
- return prop;
- }
- #define OFW_PROP_READ_UXX_ARRAY_INDEX(bit) \
- int rt_ofw_prop_read_u##bit##_array_index( \
- const struct rt_ofw_node *np, const char *propname, \
- int index, int nr, rt_uint##bit##_t *out_values) \
- { \
- int res, max_nr; \
- if (np && propname && index >= 0 && nr >= 0 && out_values) \
- { \
- rt_ssize_t len; \
- const fdt##bit##_t *elm; \
- elm = rt_ofw_prop_read_raw(np, propname, &len); \
- max_nr = len / sizeof(*elm); \
- if (elm && index < max_nr) \
- { \
- elm += index; \
- max_nr -= index; \
- res = nr > max_nr ? max_nr : nr; \
- for (nr = 0; nr < res; ++nr) \
- { \
- *out_values++ = fdt##bit##_to_cpu(*elm++); \
- } \
- } \
- else \
- { \
- res = -RT_EEMPTY; \
- } \
- } \
- else \
- { \
- res = -RT_EINVAL; \
- } \
- return res; \
- }
- OFW_PROP_READ_UXX_ARRAY_INDEX(8)
- OFW_PROP_READ_UXX_ARRAY_INDEX(16)
- OFW_PROP_READ_UXX_ARRAY_INDEX(32)
- OFW_PROP_READ_UXX_ARRAY_INDEX(64)
- #undef OFW_PROP_READ_UXX_ARRAY_INDEX
- int rt_ofw_prop_read_string_array_index(const struct rt_ofw_node *np, const char *propname,
- int index, int nr, const char **out_strings)
- {
- int res = 0;
- if (np && propname && index >= 0 && nr >= 0 && out_strings)
- {
- rt_ssize_t len, slen = 0;
- const char *value = rt_ofw_prop_read_raw(np, propname, &len);
- if (value)
- {
- nr += index;
- for (int idx = 0; idx < nr && len > 0; ++idx)
- {
- /* Add '\0' */
- slen = rt_strlen(value) + 1;
- if (idx >= index)
- {
- *out_strings++ = value;
- ++res;
- }
- len -= slen;
- value += slen;
- }
- }
- else
- {
- res = -RT_EEMPTY;
- }
- }
- else
- {
- res = -RT_EINVAL;
- }
- return res;
- }
- int rt_ofw_prop_count_of_size(const struct rt_ofw_node *np, const char *propname, int size)
- {
- int count;
- if (np && propname && size > 0)
- {
- rt_ssize_t len;
- count = -RT_EEMPTY;
- if (rt_ofw_get_prop(np, propname, &len))
- {
- count = len / size;
- }
- }
- else
- {
- count = -RT_EINVAL;
- }
- return count;
- }
- static rt_int32_t ofw_strcmp(const char *cs, const char *ct)
- {
- return rt_strcmp(cs, ct);
- }
- int rt_ofw_prop_index_of_string(const struct rt_ofw_node *np, const char *propname, const char *string)
- {
- int idx;
- if (np && propname && string)
- {
- struct rt_ofw_prop *prop = rt_ofw_get_prop(np, propname, RT_NULL);
- idx = -1;
- if (prop)
- {
- idx = ofw_prop_index_of_string(prop, string, ofw_strcmp);
- }
- }
- else
- {
- idx = -RT_EINVAL;
- }
- return idx;
- }
- const fdt32_t *rt_ofw_prop_next_u32(struct rt_ofw_prop *prop, const fdt32_t *cur, rt_uint32_t *out_value)
- {
- if (prop && out_value)
- {
- if (cur)
- {
- ++cur;
- if ((void *)cur >= prop->value + prop->length)
- {
- cur = RT_NULL;
- }
- }
- else
- {
- cur = prop->value;
- }
- if (cur)
- {
- *out_value = fdt32_to_cpu(*cur);
- }
- }
- else
- {
- cur = RT_NULL;
- }
- return cur;
- }
- const char *rt_ofw_prop_next_string(struct rt_ofw_prop *prop, const char *cur)
- {
- if (prop)
- {
- if (cur)
- {
- cur += rt_strlen(cur) + 1;
- if ((void *)cur >= prop->value + prop->length)
- {
- cur = RT_NULL;
- }
- }
- else
- {
- cur = prop->value;
- }
- }
- else
- {
- cur = RT_NULL;
- }
- return cur;
- }
|