Consolidate FDT [find "names" index by string] logic.

This commit is contained in:
jakllsch 2019-02-27 16:56:00 +00:00
parent 215056ede3
commit 21aceb1091
7 changed files with 74 additions and 153 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: fdt_clock.c,v 1.7 2019/02/27 16:30:40 jakllsch Exp $ */
/* $NetBSD: fdt_clock.c,v 1.8 2019/02/27 16:56:00 jakllsch Exp $ */
/*-
* Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
@ -27,7 +27,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: fdt_clock.c,v 1.7 2019/02/27 16:30:40 jakllsch Exp $");
__KERNEL_RCSID(0, "$NetBSD: fdt_clock.c,v 1.8 2019/02/27 16:56:00 jakllsch Exp $");
#include <sys/param.h>
#include <sys/bus.h>
@ -123,25 +123,14 @@ fdtbus_clock_get_index(int phandle, u_int index)
static struct clk *
fdtbus_clock_get_prop(int phandle, const char *clkname, const char *prop)
{
struct clk *clk = NULL;
const char *p;
u_int index;
int len, resid;
int err;
p = fdtbus_get_prop(phandle, prop, &len);
if (p == NULL)
err = fdtbus_get_index(phandle, prop, clkname, &index);
if (err != 0)
return NULL;
for (index = 0, resid = len; resid > 0; index++) {
if (strcmp(p, clkname) == 0) {
clk = fdtbus_clock_get_index(phandle, index);
break;
}
resid -= strlen(p) + 1;
p += strlen(p) + 1;
}
return clk;
return fdtbus_clock_get_index(phandle, index);
}
static u_int
@ -182,27 +171,21 @@ struct clk *
fdtbus_clock_byname(const char *clkname)
{
struct fdtbus_clock_controller *cc;
u_int len, resid, index, clock_cells;
const char *p;
u_int index, clock_cells;
int err;
LIST_FOREACH(cc, &fdtbus_clock_controllers, cc_next) {
if (!of_hasprop(cc->cc_phandle, "clock-output-names"))
err = fdtbus_get_index(cc->cc_phandle, "clock-output-names", clkname, &index);
if (err != 0)
continue;
p = fdtbus_get_prop(cc->cc_phandle, "clock-output-names", &len);
for (index = 0, resid = len; resid > 0; index++) {
if (strcmp(p, clkname) == 0) {
if (of_getprop_uint32(cc->cc_phandle, "#clock-cells", &clock_cells))
break;
continue;
const u_int index_raw = htobe32(index);
return cc->cc_funcs->decode(cc->cc_dev,
cc->cc_phandle,
clock_cells > 0 ? &index_raw : NULL,
clock_cells > 0 ? 4 : 0);
}
resid -= strlen(p) + 1;
p += strlen(p) + 1;
}
}
return NULL;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: fdt_dma.c,v 1.3 2019/02/27 16:30:40 jakllsch Exp $ */
/* $NetBSD: fdt_dma.c,v 1.4 2019/02/27 16:56:00 jakllsch Exp $ */
/*-
* Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
@ -27,7 +27,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: fdt_dma.c,v 1.3 2019/02/27 16:30:40 jakllsch Exp $");
__KERNEL_RCSID(0, "$NetBSD: fdt_dma.c,v 1.4 2019/02/27 16:56:00 jakllsch Exp $");
#include <sys/param.h>
#include <sys/bus.h>
@ -132,36 +132,14 @@ done:
struct fdtbus_dma *
fdtbus_dma_get(int phandle, const char *name, void (*cb)(void *), void *cbarg)
{
struct fdtbus_dma *dma = NULL;
char *dma_names = NULL;
const char *p;
u_int index;
int len, resid;
int err;
len = OF_getproplen(phandle, "dma-names");
if (len <= 0)
err = fdtbus_get_index(phandle, "dma-names", name, &index);
if (err != 0)
return NULL;
dma_names = kmem_alloc(len, KM_SLEEP);
if (OF_getprop(phandle, "dma-names", dma_names, len) != len) {
kmem_free(dma_names, len);
return NULL;
}
p = dma_names;
for (index = 0, resid = len; resid > 0; index++) {
if (strcmp(p, name) == 0) {
dma = fdtbus_dma_get_index(phandle, index, cb, cbarg);
break;
}
resid -= strlen(p) + 1;
p += strlen(p) + 1;
}
if (dma_names)
kmem_free(dma_names, len);
return dma;
return fdtbus_dma_get_index(phandle, index, cb, cbarg);
}
void

View File

@ -1,4 +1,4 @@
/* $NetBSD: fdt_phy.c,v 1.4 2019/02/27 16:30:40 jakllsch Exp $ */
/* $NetBSD: fdt_phy.c,v 1.5 2019/02/27 16:56:00 jakllsch Exp $ */
/*-
* Copyright (c) 2015-2017 Jared McNeill <jmcneill@invisible.ca>
@ -27,7 +27,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: fdt_phy.c,v 1.4 2019/02/27 16:30:40 jakllsch Exp $");
__KERNEL_RCSID(0, "$NetBSD: fdt_phy.c,v 1.5 2019/02/27 16:56:00 jakllsch Exp $");
#include <sys/param.h>
#include <sys/bus.h>
@ -131,36 +131,14 @@ done:
struct fdtbus_phy *
fdtbus_phy_get(int phandle, const char *phyname)
{
struct fdtbus_phy *phy = NULL;
char *phy_names = NULL;
const char *p;
u_int index;
int len, resid;
int err;
len = OF_getproplen(phandle, "phy-names");
if (len <= 0)
err = fdtbus_get_index(phandle, "phy-names", phyname, &index);
if (err != 0)
return NULL;
phy_names = kmem_alloc(len, KM_SLEEP);
if (OF_getprop(phandle, "phy-names", phy_names, len) != len) {
kmem_free(phy_names, len);
return NULL;
}
p = phy_names;
for (index = 0, resid = len; resid > 0; index++) {
if (strcmp(p, phyname) == 0) {
phy = fdtbus_phy_get_index(phandle, index);
break;
}
resid -= strlen(p) + 1;
p += strlen(p) + 1;
}
if (phy_names)
kmem_free(phy_names, len);
return phy;
return fdtbus_phy_get_index(phandle, index);
}
void

View File

@ -1,4 +1,4 @@
/* $NetBSD: fdt_pinctrl.c,v 1.7 2019/01/23 04:23:01 thorpej Exp $ */
/* $NetBSD: fdt_pinctrl.c,v 1.8 2019/02/27 16:56:00 jakllsch Exp $ */
/*-
* Copyright (c) 2019 Jason R. Thorpe
@ -29,7 +29,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: fdt_pinctrl.c,v 1.7 2019/01/23 04:23:01 thorpej Exp $");
__KERNEL_RCSID(0, "$NetBSD: fdt_pinctrl.c,v 1.8 2019/02/27 16:56:00 jakllsch Exp $");
#include <sys/param.h>
#include <sys/bus.h>
@ -118,24 +118,16 @@ fdtbus_pinctrl_set_config_index(int phandle, u_int index)
int
fdtbus_pinctrl_set_config(int phandle, const char *cfgname)
{
const char *pinctrl_names, *name;
int len, index;
u_int index;
int err;
if ((len = OF_getproplen(phandle, "pinctrl-names")) < 0)
err = fdtbus_get_index(phandle, "pinctrl-names", cfgname, &index);
if (err != 0)
return -1;
pinctrl_names = fdtbus_get_string(phandle, "pinctrl-names");
for (name = pinctrl_names, index = 0; len > 0;
len -= strlen(name) + 1, name += strlen(name) + 1, index++) {
if (strcmp(name, cfgname) == 0)
return fdtbus_pinctrl_set_config_index(phandle, index);
}
/* Not found */
return -1;
}
static void
fdtbus_pinctrl_configure_node(int phandle)
{

View File

@ -1,4 +1,4 @@
/* $NetBSD: fdt_reset.c,v 1.3 2019/02/27 16:30:40 jakllsch Exp $ */
/* $NetBSD: fdt_reset.c,v 1.4 2019/02/27 16:56:00 jakllsch Exp $ */
/*-
* Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
@ -27,7 +27,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: fdt_reset.c,v 1.3 2019/02/27 16:30:40 jakllsch Exp $");
__KERNEL_RCSID(0, "$NetBSD: fdt_reset.c,v 1.4 2019/02/27 16:56:00 jakllsch Exp $");
#include <sys/param.h>
#include <sys/bus.h>
@ -131,36 +131,14 @@ done:
struct fdtbus_reset *
fdtbus_reset_get(int phandle, const char *rstname)
{
struct fdtbus_reset *rst = NULL;
char *reset_names = NULL;
const char *p;
u_int index;
int len, resid;
int err;
len = OF_getproplen(phandle, "reset-names");
if (len <= 0)
err = fdtbus_get_index(phandle, "reset-names", rstname, &index);
if (err != 0)
return NULL;
reset_names = kmem_alloc(len, KM_SLEEP);
if (OF_getprop(phandle, "reset-names", reset_names, len) != len) {
kmem_free(reset_names, len);
return NULL;
}
p = reset_names;
for (index = 0, resid = len; resid > 0; index++) {
if (strcmp(p, rstname) == 0) {
rst = fdtbus_reset_get_index(phandle, index);
break;
}
resid -= strlen(p) + 1;
p += strlen(p) + 1;
}
if (reset_names)
kmem_free(reset_names, len);
return rst;
return fdtbus_reset_get_index(phandle, index);
}
void

View File

@ -1,4 +1,4 @@
/* $NetBSD: fdt_subr.c,v 1.28 2019/02/27 16:30:40 jakllsch Exp $ */
/* $NetBSD: fdt_subr.c,v 1.29 2019/02/27 16:56:00 jakllsch Exp $ */
/*-
* Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
@ -27,7 +27,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: fdt_subr.c,v 1.28 2019/02/27 16:30:40 jakllsch Exp $");
__KERNEL_RCSID(0, "$NetBSD: fdt_subr.c,v 1.29 2019/02/27 16:56:00 jakllsch Exp $");
#include "opt_fdt.h"
@ -215,26 +215,14 @@ int
fdtbus_get_reg_byname(int phandle, const char *name, bus_addr_t *paddr,
bus_size_t *psize)
{
const char *reg_names, *p;
u_int index;
int len, resid;
int error = ENOENT;
int error;
reg_names = fdtbus_get_prop(phandle, "reg-names", &len);
if (len <= 0)
return error;
error = fdtbus_get_index(phandle, "reg-names", name, &index);
if (error != 0)
return ENOENT;
p = reg_names;
for (index = 0, resid = len; resid > 0; index++) {
if (strcmp(p, name) == 0) {
error = fdtbus_get_reg(phandle, index, paddr, psize);
break;
}
resid -= strlen(p) + 1;
p += strlen(p) + 1;
}
return error;
return fdtbus_get_reg(phandle, index, paddr, psize);
}
int
@ -480,3 +468,26 @@ fdtbus_get_string_index(int phandle, const char *prop, u_int index)
return NULL;
}
int
fdtbus_get_index(int phandle, const char *prop, const char *name, u_int *idx)
{
const char *p;
size_t pl;
u_int index;
int len;
p = fdtbus_get_prop(phandle, prop, &len);
if (p == NULL || len <= 0)
return -1;
for (index = 0; len > 0;
pl = strlen(p) + 1, len -= pl, p += pl, index++) {
if (strcmp(p, name) == 0) {
*idx = index;
return 0;
}
}
return -1;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: fdtvar.h,v 1.48 2019/01/30 01:24:00 jmcneill Exp $ */
/* $NetBSD: fdtvar.h,v 1.49 2019/02/27 16:56:00 jakllsch Exp $ */
/*-
* Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
@ -379,6 +379,7 @@ bool fdtbus_status_okay(int);
const void * fdtbus_get_prop(int, const char *, int *);
const char * fdtbus_get_string(int, const char *);
const char * fdtbus_get_string_index(int, const char *, u_int);
int fdtbus_get_index(int, const char *, const char *, u_int *);
void fdt_add_bus(device_t, int, struct fdt_attach_args *);
void fdt_add_bus_match(device_t, int, struct fdt_attach_args *,