lib: utils/serial: Use fdt_driver for initialization

The serial driver subsystem does not need any extra data, so it can use
`struct fdt_driver` directly. The generic fdt_serial_init() first
attempts to match the chosen stdout device, and upon failure matches the
first available serial device in the DT. It is a fatal error if no such
device is found. This matches the behavior of fdt_driver_init_one().

Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
This commit is contained in:
Samuel Holland
2024-11-11 14:02:54 -08:00
committed by Anup Patel
parent 6d9ad492db
commit a524f0a507
12 changed files with 18 additions and 60 deletions

View File

@@ -9,19 +9,16 @@
#include <libfdt.h>
#include <sbi/sbi_error.h>
#include <sbi/sbi_scratch.h>
#include <sbi_utils/fdt/fdt_helper.h>
#include <sbi_utils/serial/fdt_serial.h>
/* List of FDT serial drivers generated at compile time */
extern struct fdt_serial *const fdt_serial_drivers[];
extern const struct fdt_driver *const fdt_serial_drivers[];
int fdt_serial_init(const void *fdt)
{
const void *prop;
struct fdt_serial *drv;
const struct fdt_match *match;
int pos, noff = -1, len, coff, rc;
int noff = -1, len, coff, rc;
/* Find offset of node pointed to by stdout-path */
coff = fdt_path_offset(fdt, "/chosen");
@@ -38,50 +35,15 @@ int fdt_serial_init(const void *fdt)
else
noff = fdt_path_offset(fdt, prop);
}
if (-1 < noff) {
if (!fdt_node_is_enabled(fdt, noff))
noff = -1;
}
}
/* First check DT node pointed by stdout-path */
for (pos = 0; fdt_serial_drivers[pos] && -1 < noff; pos++) {
drv = fdt_serial_drivers[pos];
match = fdt_match_node(fdt, noff, drv->match_table);
if (!match)
continue;
/* drv->init must not be NULL */
if (drv->init == NULL)
return SBI_EFAIL;
rc = drv->init(fdt, noff, match);
if (rc == SBI_ENODEV)
continue;
return rc;
if (-1 < noff) {
rc = fdt_driver_init_by_offset(fdt, noff, fdt_serial_drivers);
if (rc != SBI_ENODEV)
return rc;
}
/* Lastly check all DT nodes */
for (pos = 0; fdt_serial_drivers[pos]; pos++) {
drv = fdt_serial_drivers[pos];
noff = -1;
while ((noff = fdt_find_match(fdt, noff,
drv->match_table, &match)) >= 0) {
if (!fdt_node_is_enabled(fdt, noff))
continue;
/* drv->init must not be NULL */
if (drv->init == NULL)
return SBI_EFAIL;
rc = drv->init(fdt, noff, match);
if (rc == SBI_ENODEV)
continue;
return rc;
}
}
return SBI_ENODEV;
return fdt_driver_init_one(fdt, fdt_serial_drivers);
}

View File

@@ -30,7 +30,7 @@ static const struct fdt_match serial_cadence_match[] = {
{ },
};
struct fdt_serial fdt_serial_cadence = {
const struct fdt_driver fdt_serial_cadence = {
.match_table = serial_cadence_match,
.init = serial_cadence_init
};

View File

@@ -1,3 +1,3 @@
HEADER: sbi_utils/serial/fdt_serial.h
TYPE: struct fdt_serial
TYPE: const struct fdt_driver
NAME: fdt_serial_drivers

View File

@@ -29,7 +29,7 @@ static const struct fdt_match serial_gaisler_match[] = {
{},
};
struct fdt_serial fdt_serial_gaisler = {
const struct fdt_driver fdt_serial_gaisler = {
.match_table = serial_gaisler_match,
.init = serial_gaisler_init
};

View File

@@ -41,7 +41,7 @@ static int serial_htif_init(const void *fdt, int nodeoff,
return htif_serial_init(custom, fromhost_addr, tohost_addr);
}
struct fdt_serial fdt_serial_htif = {
const struct fdt_driver fdt_serial_htif = {
.match_table = serial_htif_match,
.init = serial_htif_init
};

View File

@@ -33,7 +33,7 @@ static const struct fdt_match serial_litex_match[] = {
{ },
};
struct fdt_serial fdt_serial_litex = {
const struct fdt_driver fdt_serial_litex = {
.match_table = serial_litex_match,
.init = serial_litex_init
};

View File

@@ -25,7 +25,7 @@ static const struct fdt_match serial_renesas_scif_match[] = {
{ /* sentinel */ }
};
struct fdt_serial fdt_serial_renesas_scif = {
const struct fdt_driver fdt_serial_renesas_scif = {
.match_table = serial_renesas_scif_match,
.init = serial_renesas_scif_init
};

View File

@@ -27,7 +27,7 @@ static const struct fdt_match serial_shakti_match[] = {
{ },
};
struct fdt_serial fdt_serial_shakti = {
const struct fdt_driver fdt_serial_shakti = {
.match_table = serial_shakti_match,
.init = serial_shakti_init
};

View File

@@ -30,7 +30,7 @@ static const struct fdt_match serial_sifive_match[] = {
{ },
};
struct fdt_serial fdt_serial_sifive = {
const struct fdt_driver fdt_serial_sifive = {
.match_table = serial_sifive_match,
.init = serial_sifive_init
};

View File

@@ -33,7 +33,7 @@ static const struct fdt_match serial_uart8250_match[] = {
{ },
};
struct fdt_serial fdt_serial_uart8250 = {
const struct fdt_driver fdt_serial_uart8250 = {
.match_table = serial_uart8250_match,
.init = serial_uart8250_init,
};

View File

@@ -29,7 +29,7 @@ static const struct fdt_match serial_xlnx_uartlite_match[] = {
{ },
};
struct fdt_serial fdt_serial_xlnx_uartlite = {
const struct fdt_driver fdt_serial_xlnx_uartlite = {
.match_table = serial_xlnx_uartlite_match,
.init = serial_xlnx_uartlite_init,
};