Browse Source

add dtb to lua

Signed-off-by: surenyi <surenyi82@163.com>
master
surenyi 6 years ago
parent
commit
61d51e69b0
  1. 191
      packages/vsky/libdsp/driver/of/base.c
  2. 10
      packages/vsky/libdsp/driver/of/fdt.c
  3. 126
      packages/vsky/libdsp/inc/of.h
  4. 19
      packages/vsky/libdsp/lua/lualib/kit.c

191
packages/vsky/libdsp/driver/of/base.c

@ -567,3 +567,194 @@ struct device_node *of_get_next_parent(struct device_node *node)
return parent;
}
/**
* of_find_property_value_of_size
*
* @np: device node from which the property value is to be read.
* @propname: name of the property to be searched.
* @min: minimum allowed length of property value
* @max: maximum allowed length of property value (0 means unlimited)
* @len: if !=NULL, actual length is written to here
*
* Search for a property in a device node and valid the requested size.
* Returns the property value on success, -EINVAL if the property does not
* exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
* property data is too small or too large.
*
*/
#define ERR_PTR(x) ((void *)(x))
static void *of_find_property_value_of_size(const struct device_node *np,
const char *propname, unsigned int min, unsigned int max, size_t *len)
{
struct property *prop = of_find_property(np, propname, NULL);
if (!prop)
return ERR_PTR(-22);
if (!prop->value)
return ERR_PTR(-61);
if (prop->length < min)
return ERR_PTR(-75);
if (max && prop->length > max)
return ERR_PTR(-75);
if (len)
*len = prop->length;
return prop->value;
}
#define MAX_ERRNO 4095
#define IS_ERR_VALUE(x) ((unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO)
static inline int IS_ERR(const void *ptr)
{
return IS_ERR_VALUE((unsigned long)ptr);
}
/**
* of_property_read_variable_u32_array - Find and read an array of 32 bit
* integers from a property, with bounds on the minimum and maximum array size.
*
* @np: device node from which the property value is to be read.
* @propname: name of the property to be searched.
* @out_values: pointer to return value, modified only if return value is 0.
* @sz_min: minimum number of array elements to read
* @sz_max: maximum number of array elements to read, if zero there is no
* upper limit on the number of elements in the dts entry but only
* sz_min will be read.
*
* Search for a property in a device node and read 32-bit value(s) from
* it. Returns number of elements read on success, -EINVAL if the property
* does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
* if the property data is smaller than sz_min or longer than sz_max.
*
* The out_values is modified only if a valid u32 value can be decoded.
*/
int of_property_read_variable_u32_array(const struct device_node *np, const char *propname, unsigned int *out_values,
size_t sz_min, size_t sz_max)
{
size_t sz, count;
const int *val = of_find_property_value_of_size(np, propname,
(sz_min * sizeof(*out_values)),
(sz_max * sizeof(*out_values)),
&sz);
if (IS_ERR(val))
return (int)(val);
if (!sz_max)
sz = sz_min;
else
sz /= sizeof(*out_values);
count = sz;
while (count--) {
unsigned int v = *val;
++val;
#ifdef _LITTLE_ENDIAN
*out_values++ = swap32(v);
#else
*out_values++ = v;
#endif
}
return sz;
}
/**
* of_property_read_variable_u8_array - Find and read an array of u8 from a
* property, with bounds on the minimum and maximum array size.
*
* @np: device node from which the property value is to be read.
* @propname: name of the property to be searched.
* @out_values: pointer to return value, modified only if return value is 0.
* @sz_min: minimum number of array elements to read
* @sz_max: maximum number of array elements to read, if zero there is no
* upper limit on the number of elements in the dts entry but only
* sz_min will be read.
*
* Search for a property in a device node and read 8-bit value(s) from
* it. Returns number of elements read on success, -EINVAL if the property
* does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
* if the property data is smaller than sz_min or longer than sz_max.
*
* dts entry of array should be like:
* property = /bits/ 8 <0x50 0x60 0x70>;
*
* The out_values is modified only if a valid u8 value can be decoded.
*/
int of_property_read_variable_u8_array(const struct device_node *np, const char *propname, unsigned char *out_values, size_t sz_min, size_t sz_max)
{
size_t sz, count;
const unsigned char *val = of_find_property_value_of_size(np, propname,
(sz_min * sizeof(*out_values)),
(sz_max * sizeof(*out_values)),
&sz);
if (IS_ERR(val))
return (int)(val);
if (!sz_max)
sz = sz_min;
else
sz /= sizeof(*out_values);
count = sz;
while (count--)
*out_values++ = *val++;
return sz;
}
/**
* of_property_read_variable_u16_array - Find and read an array of u16 from a
* property, with bounds on the minimum and maximum array size.
*
* @np: device node from which the property value is to be read.
* @propname: name of the property to be searched.
* @out_values: pointer to return value, modified only if return value is 0.
* @sz_min: minimum number of array elements to read
* @sz_max: maximum number of array elements to read, if zero there is no
* upper limit on the number of elements in the dts entry but only
* sz_min will be read.
*
* Search for a property in a device node and read 16-bit value(s) from
* it. Returns number of elements read on success, -EINVAL if the property
* does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
* if the property data is smaller than sz_min or longer than sz_max.
*
* dts entry of array should be like:
* property = /bits/ 16 <0x5000 0x6000 0x7000>;
*
* The out_values is modified only if a valid u16 value can be decoded.
*/
int of_property_read_variable_u16_array(const struct device_node *np, const char *propname, unsigned short *out_values, size_t sz_min, size_t sz_max)
{
size_t sz, count;
const short *val = of_find_property_value_of_size(np, propname,
(sz_min * sizeof(*out_values)),
(sz_max * sizeof(*out_values)),
&sz);
if (IS_ERR(val))
return (int)(val);
if (!sz_max)
sz = sz_min;
else
sz /= sizeof(*out_values);
count = sz;
while (count--) {
short v = *val;
++val;
#ifdef _LITTLE_ENDIAN
*out_values++ = ((v & 0xff) << 8) | ((v >> 8) & 0xff);
#else
*out_values++ = v;
#endif
}
return sz;
}

10
packages/vsky/libdsp/driver/of/fdt.c

@ -428,3 +428,13 @@ int unflatten_device_tree(void *mempos)
return of_root ? 0 : -1;
}
const char * of_fdt_get_machine_name(void *mempos)
{
const char *name;
name = fdt_getprop(mempos, 0, "model", NULL);
if (!name)
name = fdt_getprop(mempos, 0, "compatible", NULL);
return name;
}

126
packages/vsky/libdsp/inc/of.h

@ -148,23 +148,104 @@ static inline const char *of_node_full_name(const struct device_node *np)
const char *of_prop_next_string(struct property *prop, const char *cur);
int of_device_is_compatible(const struct device_node *device,
const char *compat);
int of_device_is_compatible(const struct device_node *device, const char *compat);
const void *__of_get_property(const struct device_node *np, const char *name, int *lenp);
int of_n_addr_cells(struct device_node *np);
int of_n_size_cells(struct device_node *np);
unsigned long long of_read_number(const int *cell, int size);
struct device_node *of_get_parent(const struct device_node *node);
int of_property_read_string_helper(const struct device_node *np,
const char *propname, const char **out_strs,
size_t sz, int skip);
int of_property_read_string_helper(const struct device_node *np, const char *propname, const char **out_strs, size_t sz, int skip);
struct property *of_find_property(const struct device_node *np, const char *name, int *lenp);
int of_property_read_variable_u8_array(const struct device_node *np, const char *propname, unsigned char *out_values, size_t sz_min, size_t sz_max);
int of_property_read_variable_u16_array(const struct device_node *np, const char *propname, unsigned short *out_values, size_t sz_min, size_t sz_max);
int of_property_read_variable_u32_array(const struct device_node *np, const char *propname, unsigned int *out_values, size_t sz_min, size_t sz_max);
/**
* of_property_read_u32_array - Find and read an array of 32 bit integers
* from a property.
*
* @np: device node from which the property value is to be read.
* @propname: name of the property to be searched.
* @out_values: pointer to return value, modified only if return value is 0.
* @sz: number of array elements to read
*
* Search for a property in a device node and read 32-bit value(s) from
* it. Returns 0 on success, -EINVAL if the property does not exist,
* -ENODATA if property does not have a value, and -EOVERFLOW if the
* property data isn't large enough.
*
* The out_values is modified only if a valid u32 value can be decoded.
*/
static inline int of_property_read_u32_array(const struct device_node *np, const char *propname, unsigned int *out_values, size_t sz)
{
int ret = of_property_read_variable_u32_array(np, propname, out_values, sz, 0);
if (ret >= 0)
return 0;
else
return ret;
}
struct property *of_find_property(const struct device_node *np,
const char *name,
int *lenp);
/**
* of_property_read_u16_array - Find and read an array of u16 from a property.
*
* @np: device node from which the property value is to be read.
* @propname: name of the property to be searched.
* @out_values: pointer to return value, modified only if return value is 0.
* @sz: number of array elements to read
*
* Search for a property in a device node and read 16-bit value(s) from
* it. Returns 0 on success, -EINVAL if the property does not exist,
* -ENODATA if property does not have a value, and -EOVERFLOW if the
* property data isn't large enough.
*
* dts entry of array should be like:
* property = /bits/ 16 <0x5000 0x6000 0x7000>;
*
* The out_values is modified only if a valid u16 value can be decoded.
*/
static inline int of_property_read_u16_array(const struct device_node *np,
const char *propname,
unsigned short *out_values, size_t sz)
{
int ret = of_property_read_variable_u16_array(np, propname, out_values,
sz, 0);
if (ret >= 0)
return 0;
else
return ret;
}
/**
* of_property_read_u8_array - Find and read an array of u8 from a property.
*
* @np: device node from which the property value is to be read.
* @propname: name of the property to be searched.
* @out_values: pointer to return value, modified only if return value is 0.
* @sz: number of array elements to read
*
* Search for a property in a device node and read 8-bit value(s) from
* it. Returns 0 on success, -EINVAL if the property does not exist,
* -ENODATA if property does not have a value, and -EOVERFLOW if the
* property data isn't large enough.
*
* dts entry of array should be like:
* property = /bits/ 8 <0x50 0x60 0x70>;
*
* The out_values is modified only if a valid u8 value can be decoded.
*/
static inline int of_property_read_u8_array(const struct device_node *np, const char *propname, unsigned char *out_values, size_t sz)
{
int ret = of_property_read_variable_u8_array(np, propname, out_values, sz, 0);
if (ret >= 0)
return 0;
else
return ret;
}
/**
* of_property_read_string_index() - Find and read a string from a multiple
@ -184,9 +265,7 @@ struct property *of_find_property(const struct device_node *np,
*
* The out_string pointer is modified only if a valid string can be decoded.
*/
static inline int of_property_read_string_index(const struct device_node *np,
const char *propname,
int index, const char **output)
static inline int of_property_read_string_index(const struct device_node *np, const char *propname, int index, const char **output)
{
int rc = of_property_read_string_helper(np, propname, output, 1, index);
return rc < 0 ? rc : 0;
@ -200,14 +279,33 @@ static inline int of_property_read_string_index(const struct device_node *np,
* Search for a property in a device node.
* Returns true if the property exists false otherwise.
*/
static inline int of_property_read_bool(const struct device_node *np,
const char *propname)
static inline int of_property_read_bool(const struct device_node *np, const char *propname)
{
struct property *prop = of_find_property(np, propname, NULL);
return prop ? 1 : 0;
}
static inline int of_property_read_u8(const struct device_node *np, const char *propname, unsigned char *out_value)
{
return of_property_read_u8_array(np, propname, out_value, 1);
}
static inline int of_property_read_u16(const struct device_node *np, const char *propname, unsigned short *out_value)
{
return of_property_read_u16_array(np, propname, out_value, 1);
}
static inline int of_property_read_u32(const struct device_node *np, const char *propname, unsigned int *out_value)
{
return of_property_read_u32_array(np, propname, out_value, 1);
}
static inline int of_property_read_s32(const struct device_node *np, const char *propname, signed int *out_value)
{
return of_property_read_u32(np, propname, (unsigned int*) out_value);
}
struct device_node *__of_find_all_nodes(struct device_node *prev);
#define for_each_of_allnodes_from(from, dn) \
@ -232,6 +330,6 @@ void of_alias_scan(void * (*dt_alloc)(uint32_t size, uint32_t align));
struct device_node *of_get_next_parent(struct device_node *node);
int unflatten_device_tree(void *mempos);
const char * of_fdt_get_machine_name(void *mempos);
#endif

19
packages/vsky/libdsp/lua/lualib/kit.c

@ -501,6 +501,23 @@ static int __unflatten(lua_State *L)
return 1;
}
static int __of_machine(lua_State *L)
{
unsigned int addr = luaL_checkinteger(L, 1);
const char *s = of_fdt_get_machine_name((void *)addr);
if (s) {
lua_pushstring(L, s);
return 1;
}
return 0;
}
static struct luaL_Reg __of[] = {
{"unflatten", __unflatten},
{"machine", __of_machine},
{NULL, NULL},
};
static struct luaL_Reg utils[] = {
{"coreId", __get_core_id},
{"l2Address",__to_l2_address},
@ -529,7 +546,6 @@ static struct luaL_Reg board[] = {
{"flash", __flash_open},
{"uart", __uart_open},
{"power", __powerup},
{"unflatten", __unflatten},
{NULL, NULL}
};
@ -540,6 +556,7 @@ int luaopen_dspkits(struct lua_State *L)
__add_funcs(L, utils, "utils");
__add_funcs(L, __elf, "elf");
__add_funcs(L, mem, "memory");
__add_funcs(L, __of, "dtb");
flash_init(L);
uart_init(L);

Loading…
Cancel
Save