Skip to content

Commit

Permalink
drivers: Add board uclass
Browse files Browse the repository at this point in the history
Since there is no canonical "board device" that can be used in board
files, it is difficult to use DM function for board initialization in
these cases.

Hence, add a uclass that implements a simple "board device", which can
hold devices not suitable anywhere else in the device tree, and is also
able to read encoded information, e.g. hard-wired GPIOs on a GPIO
expander, read-only memory ICs, etc. that carry information about the
hardware.

The devices of this uclass expose methods to read generic data types
(integers, strings, booleans) to encode the information provided by the
hardware.

Reviewed-by: Simon Glass <[email protected]>
Signed-off-by: Mario Six <[email protected]>
  • Loading branch information
si-gdsys authored and sjg20 committed Sep 29, 2018
1 parent 172942a commit 5381c28
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 0 deletions.
2 changes: 2 additions & 0 deletions drivers/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ source "drivers/ddr/Kconfig"

source "drivers/demo/Kconfig"

source "drivers/board/Kconfig"

source "drivers/ddr/fsl/Kconfig"

source "drivers/dfu/Kconfig"
Expand Down
1 change: 1 addition & 0 deletions drivers/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ obj-y += ata/
obj-$(CONFIG_DM_DEMO) += demo/
obj-$(CONFIG_BIOSEMU) += bios_emulator/
obj-y += block/
obj-y += board/
obj-$(CONFIG_BOOTCOUNT_LIMIT) += bootcount/
obj-$(CONFIG_CPU) += cpu/
obj-y += crypto/
Expand Down
7 changes: 7 additions & 0 deletions drivers/board/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
menuconfig BOARD
bool "Device Information"
help
Support methods to query hardware configurations from internal
mechanisms (e.g. reading GPIO values, determining the presence of
devices on busses, etc.). This enables the usage of U-Boot with
modular board architectures.
6 changes: 6 additions & 0 deletions drivers/board/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SPDX-License-Identifier: GPL-2.0+
#
# (C) Copyright 2017
# Mario Six, Guntermann & Drunck GmbH, [email protected]
obj-$(CONFIG_BOARD) += board-uclass.o
obj-$(CONFIG_BOARD_GAZERBEAM) += gazerbeam.o
60 changes: 60 additions & 0 deletions drivers/board/board-uclass.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* (C) Copyright 2017
* Mario Six, Guntermann & Drunck GmbH, [email protected]
*/

#include <common.h>
#include <dm.h>
#include <board.h>

int board_get(struct udevice **devp)
{
return uclass_first_device_err(UCLASS_BOARD, devp);
}

int board_detect(struct udevice *dev)
{
struct board_ops *ops = board_get_ops(dev);

if (!ops->detect)
return -ENOSYS;

return ops->detect(dev);
}

int board_get_bool(struct udevice *dev, int id, bool *val)
{
struct board_ops *ops = board_get_ops(dev);

if (!ops->get_bool)
return -ENOSYS;

return ops->get_bool(dev, id, val);
}

int board_get_int(struct udevice *dev, int id, int *val)
{
struct board_ops *ops = board_get_ops(dev);

if (!ops->get_int)
return -ENOSYS;

return ops->get_int(dev, id, val);
}

int board_get_str(struct udevice *dev, int id, size_t size, char *val)
{
struct board_ops *ops = board_get_ops(dev);

if (!ops->get_str)
return -ENOSYS;

return ops->get_str(dev, id, size, val);
}

UCLASS_DRIVER(board) = {
.id = UCLASS_BOARD,
.name = "board",
.post_bind = dm_scan_fdt_dev,
};
139 changes: 139 additions & 0 deletions include/board.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* (C) Copyright 2017
* Mario Six, Guntermann & Drunck GmbH, [email protected]
*/

/*
* This uclass encapsulates hardware methods to gather information about a
* board or a specific device such as hard-wired GPIOs on GPIO expanders,
* read-only data in flash ICs, or similar.
*
* The interface offers functions to read the usual standard data types (bool,
* int, string) from the device, each of which is identified by a static
* numeric ID (which will usually be defined as a enum in a header file).
*
* If for example the board had a read-only serial number flash IC, we could
* call
*
* ret = board_detect(dev);
* if (ret) {
* debug("board device not found.");
* return ret;
* }
*
* ret = board_get_int(dev, ID_SERIAL_NUMBER, &serial);
* if (ret) {
* debug("Error when reading serial number from device.");
* return ret;
* }
*
* to read the serial number.
*/

struct board_ops {
/**
* detect() - Run the hardware info detection procedure for this
* device.
* @dev: The device containing the information
*
* This operation might take a long time (e.g. read from EEPROM,
* check the presence of a device on a bus etc.), hence this is not
* done in the probe() method, but later during operation in this
* dedicated method.
*
* Return: 0 if OK, -ve on error.
*/
int (*detect)(struct udevice *dev);

/**
* get_bool() - Read a specific bool data value that describes the
* hardware setup.
* @dev: The board instance to gather the data.
* @id: A unique identifier for the bool value to be read.
* @val: Pointer to a buffer that receives the value read.
*
* Return: 0 if OK, -ve on error.
*/
int (*get_bool)(struct udevice *dev, int id, bool *val);

/**
* get_int() - Read a specific int data value that describes the
* hardware setup.
* @dev: The board instance to gather the data.
* @id: A unique identifier for the int value to be read.
* @val: Pointer to a buffer that receives the value read.
*
* Return: 0 if OK, -ve on error.
*/
int (*get_int)(struct udevice *dev, int id, int *val);

/**
* get_str() - Read a specific string data value that describes the
* hardware setup.
* @dev: The board instance to gather the data.
* @id: A unique identifier for the string value to be read.
* @size: The size of the buffer to receive the string data.
* @val: Pointer to a buffer that receives the value read.
*
* Return: 0 if OK, -ve on error.
*/
int (*get_str)(struct udevice *dev, int id, size_t size, char *val);
};

#define board_get_ops(dev) ((struct board_ops *)(dev)->driver->ops)

/**
* board_detect() - Run the hardware info detection procedure for this device.
*
* @dev: The device containing the information
*
* Return: 0 if OK, -ve on error.
*/
int board_detect(struct udevice *dev);

/**
* board_get_bool() - Read a specific bool data value that describes the
* hardware setup.
* @dev: The board instance to gather the data.
* @id: A unique identifier for the bool value to be read.
* @val: Pointer to a buffer that receives the value read.
*
* Return: 0 if OK, -ve on error.
*/
int board_get_bool(struct udevice *dev, int id, bool *val);

/**
* board_get_int() - Read a specific int data value that describes the
* hardware setup.
* @dev: The board instance to gather the data.
* @id: A unique identifier for the int value to be read.
* @val: Pointer to a buffer that receives the value read.
*
* Return: 0 if OK, -ve on error.
*/
int board_get_int(struct udevice *dev, int id, int *val);

/**
* board_get_str() - Read a specific string data value that describes the
* hardware setup.
* @dev: The board instance to gather the data.
* @id: A unique identifier for the string value to be read.
* @size: The size of the buffer to receive the string data.
* @val: Pointer to a buffer that receives the value read.
*
* Return: 0 if OK, -ve on error.
*/
int board_get_str(struct udevice *dev, int id, size_t size, char *val);

/**
* board_get() - Return the board device for the board in question.
* @devp: Pointer to structure to receive the board device.
*
* Since there can only be at most one board instance, the API can supply a
* function that returns the unique device. This is especially useful for use
* in board files.
*
* Return: 0 if OK, -ve on error.
*/
int board_get(struct udevice **devp);
1 change: 1 addition & 0 deletions include/dm/uclass-id.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ enum uclass_id {
UCLASS_ADC, /* Analog-to-digital converter */
UCLASS_AHCI, /* SATA disk controller */
UCLASS_BLK, /* Block device */
UCLASS_BOARD, /* Device information from hardware */
UCLASS_CLK, /* Clock source, e.g. used by peripherals */
UCLASS_CPU, /* CPU, typically part of an SoC */
UCLASS_CROS_EC, /* Chrome OS EC */
Expand Down

0 comments on commit 5381c28

Please sign in to comment.