Driver model implementation, tests, demo and GPIO
Subsystems (4)
v2 cover Driver model implementation, tests, demo and GPIO
Note: If you are reviewing this code, but don't have a lot of time, please consider starting with the 'demo' driver (patch 'dm: Add a demonstration/example driver') since it clearly shows how devices and uclasses work. Much of this series consists of test code and plumbing, so is of less interest to driver authors.
This patch adds a driver model implementation. It is taken from the driver model code developed by:
Marek Vasut <[email protected]>
Pavel Herrmann <[email protected]>
Viktor K?iv?k <[email protected]>
Tomas Hlavacek <[email protected]>
Please see doc/driver-model/README.txt for details of how to run this and what to look for. So far the documentation in doc/driver-model has not been updated.
You can find a test version of the code used here in branch dm2 at:
http://git.denx.de/u-boot-x86.git
(Branch dm contains the original implementation)
Changes in v2:
- Removed pointer return values in favour of integer
- Use driver_bind() in dm_init() instead of writing new code
- Allow driver_bind() to support a NULL parent
- Add dm_warn() to warn about impending doom
- Standardise variable names (e.g. uclass instead of class)
- Remove relocation functions
- Add new header file for lists
- Add new util file to hold utility functions
- Allow a driver to bind to only one uclass
- Remove unneeded arguments to uclass_bind(), uclass_unbind()
- Rename struct device's 'bus' to 'parent'
- Rename data structures to hopefully be clearer
- Put platform_data definitions in their own header file
- Add U_BOOT_DEVICE to declare platform_data
- Add auto-probing feature for platform_data to avoid driver_bind() calls
- Add simple unit test functions
- Add test infrastructure for driver model
- Add integration tests for driver model
- Add device tree support in driver model
- Add automatic allocation of platform_data for FDT
- Add automatic allocation of priv data for devices
- Add automatic allocation of device-specific priv data for uclasses
- Add GPIO uclass and tests
- Add sandbox GPIO driver
- Update gpio command to use driver model
- Add tests for core code
- Add script to run tests
- Add a single include/dm.h to bring in driver model code
Pavel Herrmann (1):
dm: Add README for driver model
Simon Glass (13):
sandbox: Make map_to_sysmem() use a constant pointer
sandbox: Correct data sizes and printf() strings in fdtdec.c
sandbox: config: Don't use 64-bit physical memory Add cmd_process_error() to report and process errors
sandbox: config: Enable driver model
dm: Add base driver model support
dm: Set up driver model after relocation
dm: Add basic tests
dm: Add a 'dm' command for testing
dm: Add a demonstration/example driver
dm: Add GPIO support and tests
sandbox: Convert GPIOs to use driver model
dm: Enable gpio command to support driver model
Makefile | 4 + arch/sandbox/include/asm/gpio.h | 14 +- arch/sandbox/include/asm/io.h | 2 +- arch/sandbox/include/asm/types.h | 4 +- board/sandbox/sandbox/sandbox.c | 7 +- common/Makefile | 1 + common/board_r.c | 33 +++ common/cmd_demo.c | 118 ++++++++ common/cmd_gpio.c | 127 ++++++++- common/command.c | 10 + common/dm/Makefile | 40 +++ common/dm/device.c | 370 +++++++++++++++++++++++++ common/dm/lists.c | 183 +++++++++++++ common/dm/root.c | 126 +++++++++ common/dm/uclass.c | 362 +++++++++++++++++++++++++ common/dm/util.c | 50 ++++ doc/driver-model/README.txt | 329 ++++++++++++++++++++++ drivers/demo/Makefile | 44 +++ drivers/demo/demo-pdata.c | 60 ++++ drivers/demo/demo-shape.c | 107 ++++++++ drivers/demo/demo-simple.c | 46 ++++ drivers/demo/demo-uclass.c | 54 ++++ drivers/gpio/Makefile | 2 + drivers/gpio/gpio-uclass.c | 281 +++++++++++++++++++ drivers/gpio/sandbox.c | 216 +++++++++------ include/asm-generic/global_data.h | 9 + include/asm-generic/gpio.h | 50 ++++ include/command.h | 9 + include/common.h | 2 +- include/configs/sandbox.h | 10 +- include/dm-demo.h | 47 ++++ include/dm.h | 27 ++ include/dm/device-internal.h | 38 +++ include/dm/device.h | 72 +++++ include/dm/lists.h | 37 +++ include/dm/platform_data.h | 38 +++ include/dm/root.h | 38 +++ include/dm/test.h | 132 +++++++++ include/dm/uclass-id.h | 42 +++ include/dm/uclass-internal.h | 36 +++ include/dm/uclass.h | 74 +++++ include/dm/ut.h | 83 ++++++ include/dm/util.h | 42 +++ lib/fdtdec.c | 8 +- test/dm/Makefile | 53 ++++ test/dm/cmd_dm.c | 147 ++++++++++ test/dm/core.c | 557 ++++++++++++++++++++++++++++++++++++++ test/dm/gpio.c | 124 +++++++++ test/dm/test-driver.c | 159 +++++++++++ test/dm/test-fdt.c | 148 ++++++++++ test/dm/test-main.c | 120 ++++++++ test/dm/test-uclass.c | 118 ++++++++ test/dm/test.dts | 59 ++++ test/dm/ut.c | 46 ++++ 54 files changed, 4805 insertions(+), 110 deletions(-) create mode 100644 common/cmd_demo.c create mode 100644 common/dm/Makefile create mode 100644 common/dm/device.c create mode 100644 common/dm/lists.c create mode 100644 common/dm/root.c create mode 100644 common/dm/uclass.c create mode 100644 common/dm/util.c create mode 100644 doc/driver-model/README.txt create mode 100644 drivers/demo/Makefile create mode 100644 drivers/demo/demo-pdata.c create mode 100644 drivers/demo/demo-shape.c create mode 100644 drivers/demo/demo-simple.c create mode 100644 drivers/demo/demo-uclass.c create mode 100644 drivers/gpio/gpio-uclass.c create mode 100644 include/dm-demo.h create mode 100644 include/dm.h create mode 100644 include/dm/device-internal.h create mode 100644 include/dm/device.h create mode 100644 include/dm/lists.h create mode 100644 include/dm/platform_data.h create mode 100644 include/dm/root.h create mode 100644 include/dm/test.h create mode 100644 include/dm/uclass-id.h create mode 100644 include/dm/uclass-internal.h create mode 100644 include/dm/uclass.h create mode 100644 include/dm/ut.h create mode 100644 include/dm/util.h create mode 100644 test/dm/Makefile create mode 100644 test/dm/cmd_dm.c create mode 100644 test/dm/core.c create mode 100644 test/dm/gpio.c create mode 100644 test/dm/test-driver.c create mode 100644 test/dm/test-fdt.c create mode 100644 test/dm/test-main.c create mode 100644 test/dm/test-uclass.c create mode 100644 test/dm/test.dts create mode 100644 test/dm/ut.c
Signature · 1 line
1.8.2.1
RFC PATCH v2 1/14 sandbox: Make map_to_sysmem() use a constant pointer
Very often a constant pointer is passed to this function, so we should declare this, since map_to_sysmem() does not change the pointer.
---
Changes in v2: None
arch/sandbox/include/asm/io.h | 2 +- include/common.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
arch/sandbox/include/asm/io.h +1 -1
@@ -54,6 +54,6 @@ static inline void unmap_sysmem(const void *vaddr) }/* Map from a pointer to our RAM buffer */ -phys_addr_t map_to_sysmem (void *ptr); +phys_addr_t map_to_sysmem (const void *ptr);#endif
include/common.h +1 -1
@@ -925,7 +925,7 @@ static inline void unmap_sysmem(const void *vaddr) { } -static inline phys_addr_t map_to_sysmem (void *ptr) +static inline phys_addr_t map_to_sysmem (const void *ptr) {return (phys_addr_t )(uintptr_t )ptr; }
Signature · 1 line
1.8.2.1
RFC PATCH v2 2/14 sandbox: Correct data sizes and printf() strings in fdtdec.c
In reply to Simon GlassThere are a few wwrnings in this file when building for sandbox. Addresses coming from the device tree need to be treated as ulong as elsewhere in U-Boot and we must use map_sysmem() to convert to a pointer when needed.
---
Changes in v2: None
lib/fdtdec.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
lib/fdtdec.c +4 -4
@@ -92,10 +92,10 @@ fdt_addr_t fdtdec_get_addr_size(const void *blob, int node, size = (fdt_size_t *)((char *)cell +sizeof (fdt_addr_t)); *sizep =fdt_size_to_cpu (*size); -debug ("addr=%p, size=%p\n" , (void *)addr, - (void *)*sizep); +debug ("addr=%08lx, size=%08x\n" , + (ulong )addr, *sizep); }else { -debug ("%p\n" , (void *)addr); +debug ("%08lx\n" , (ulong )addr); }return addr; }
@@ -617,7 +617,7 @@ int fdtdec_decode_region(const void *blob, int node,if (!cell || (len !=sizeof (fdt_addr_t) *2 ))return -1 ; - *ptrp = (void *)fdt_addr_to_cpu (*cell); + *ptrp =map_sysmem (fdt_addr_to_cpu (*cell), *size); *size =fdt_size_to_cpu (cell[1 ]);debug ("%s: size=%zx\n" , __func__, *size);return 0 ;
Signature · 1 line
1.8.2.1
RFC PATCH v2 3/14 sandbox: config: Don't use 64-bit physical memory
In reply to Simon GlassSandbox uses an emulated memory map which is quite small. We don't need the CONFIG_PHYS_64BIT option since we can address memory with a 32-bit offset into our ram_buf.
Adjust the phys_addr_t and phys_size_t types accordingly.
---
Changes in v2: None
arch/sandbox/include/asm/types.h | 4 ++-- include/configs/sandbox.h | 1 - 2 files changed, 2 insertions(+), 3 deletions(-)
arch/sandbox/include/asm/types.h +2 -2
@@ -64,8 +64,8 @@ typedef unsigned long long u64;#define BITS_PER_LONG CONFIG_SANDBOX_BITS_PER_LONGtypedef unsigned long dma_addr_t ; -typedef unsigned long phys_addr_t ; -typedef unsigned long phys_size_t ; +typedef u32 phys_addr_t ; +typedef u32 phys_size_t ;#endif /* __KERNEL__ */
include/configs/sandbox.h +0 -1
@@ -73,7 +73,6 @@#define CONFIG_SYS_LOAD_ADDR 0x00000000#define CONFIG_SYS_MEMTEST_START 0x00100000#define CONFIG_SYS_MEMTEST_END (CONFIG_SYS_MEMTEST_START + 0x1000) -#define CONFIG_PHYS_64BIT #define CONFIG_SYS_FDT_LOAD_ADDR 0x1000000/* Size of our emulated memory */
Signature · 1 line
1.8.2.1
RFC PATCH v2 4/14 Add cmd_process_error() to report and process errors
In reply to Simon GlassU-Boot now uses errors defined in include/errno.h which are negative integers. Command which fail need to report the error and return 1 to indicate failure. Add this functionality in cmd_process_error().
For now this merely reports the error number. It would be possible also to produce a helpful error message by storing the error strings in U-Boot.
---
Changes in v2: None
common/command.c | 10 ++++++++++ include/command.h | 9 +++++++++ 2 files changed, 19 insertions(+)
common/command.c +10 -0
@@ -554,3 +554,13 @@ enum command_ret_t cmd_process(int flag, int argc, char * const argv[], rc =cmd_usage (cmdtp);return rc; } + +int cmd_process_error (cmd_tbl_t *cmdtp,int err) +{ +if (err) { +printf ("Command '%s' failed: Error %d\n" , cmdtp->name, err); +return 1 ; + } + +return 0 ; +}
include/command.h +9 -0
@@ -80,6 +80,15 @@ extern int var_complete(int argc, char * const argv[], char last_char, int maxv,extern int cmd_auto_complete (const char *const prompt,char *buf,int *np,int *colp);#endif +/** +* cmd_process_error() - report and process a possible error +* +* @cmdtp: Command which caused the error +* @err: Error code (0 if none, -ve for error, like -EIO) +* @return 0 if there is not error, 1 (CMD_RET_FAILURE) if an error is found +*/ +int cmd_process_error (cmd_tbl_t *cmdtp,int err); +/* * Monitor Command *
Signature · 1 line
1.8.2.1
RFC PATCH v2 5/14 sandbox: config: Enable driver model
In reply to Simon GlassUse driver model in sandbox to permit running of driver model unit test.
---
Changes in v2: None
include/configs/sandbox.h | 2 ++ 1 file changed, 2 insertions(+)
include/configs/sandbox.h +2 -0
@@ -22,6 +22,8 @@#ifndef __CONFIG_H#define __CONFIG_H +#define CONFIG_DM +/* Number of bits in a C 'long' on this architecture */ #define CONFIG_SANDBOX_BITS_PER_LONG 64
Signature · 1 line
1.8.2.1
RFC PATCH v2 6/14 dm: Add base driver model support
In reply to Simon GlassAdd driver model functionality for generic board.
This includes data structures and base code for registering devices and uclasses (groups of devices with the same purpose, e.g. all I2C ports will be in the same uclass).
The feature is enabled with CONFIG_DM.
---
Changes in v2: None
Makefile | 2 + common/dm/Makefile | 40 +++++ common/dm/device.c | 370 ++++++++++++++++++++++++++++++++++++++ common/dm/lists.c | 181 +++++++++++++++++++ common/dm/root.c | 124 +++++++++++++ common/dm/uclass.c | 362 +++++++++++++++++++++++++++++++++++++ common/dm/util.c | 50 ++++++ include/asm-generic/global_data.h | 9 + include/dm.h | 27 +++ include/dm/device-internal.h | 38 ++++ include/dm/device.h | 72 ++++++++ include/dm/lists.h | 37 ++++ include/dm/platform_data.h | 38 ++++ include/dm/root.h | 38 ++++ include/dm/uclass-id.h | 42 +++++ include/dm/uclass-internal.h | 36 ++++ include/dm/uclass.h | 74 ++++++++ include/dm/util.h | 42 +++++ 18 files changed, 1582 insertions(+) create mode 100644 common/dm/Makefile create mode 100644 common/dm/device.c create mode 100644 common/dm/lists.c create mode 100644 common/dm/root.c create mode 100644 common/dm/uclass.c create mode 100644 common/dm/util.c create mode 100644 include/dm.h create mode 100644 include/dm/device-internal.h create mode 100644 include/dm/device.h create mode 100644 include/dm/lists.h create mode 100644 include/dm/platform_data.h create mode 100644 include/dm/root.h create mode 100644 include/dm/uclass-id.h create mode 100644 include/dm/uclass-internal.h create mode 100644 include/dm/uclass.h create mode 100644 include/dm/util.h
Makefile +2 -0
@@ -337,6 +337,8 @@ LIBS-y += api/libapi.o LIBS-y += post/libpost.o LIBS-y += test/libtest.o +LIBS-$(CONFIG_DM) += common/dm/libdm.o + ifneq ($(CONFIG_AM33XX)$(CONFIG_OMAP34XX)$(CONFIG_OMAP44XX)$(CONFIG_OMAP54XX)$(CONFIG_TI814X),) LIBS-y += $(CPUDIR)/omap-common/libomap-common.o endif
common/dm/Makefile new file +40 -0
@@ -0,0 +1,40 @@ +# See file CREDITS for list of people who contributed to this +# project. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 2 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, +# MA 02111-1307 USA +# + +include $(TOPDIR)/config.mk + +LIB := $(obj)libdm.o + +COBJS := device.o lists.o root.o uclass.o util.o +SRCS := $(COBJS:.o=.c) +OBJS := $(addprefix $(obj),$(COBJS)) + +all: $(LIB) + +$(LIB): $(obj).depend $(OBJS) + $(call cmd_link_o_target, $(OBJS)) + +######################################################################### + +# defines $(obj).depend target +include $(SRCTREE)/rules.mk + +sinclude $(obj).depend + +#########################################################################
common/dm/device.c new file +370 -0
370 lines not on this page · raw message
common/dm/lists.c new file +181 -0
181 lines not on this page · raw message
common/dm/root.c new file +124 -0
124 lines not on this page · raw message
common/dm/uclass.c new file +362 -0
362 lines not on this page · raw message
common/dm/util.c new file +50 -0
50 lines not on this page · raw message
include/asm-generic/global_data.h +9 -0
@@ -24,6 +24,7 @@#ifndef __ASM_GENERIC_GBL_DATA_H#define __ASM_GENERIC_GBL_DATA_H +/* * The following data structure is placed in some memory which is * available very early after boot (like DPRAM on MPC8xx/MPC82xx, or
@@ -37,6 +38,8 @@*/ #ifndef __ASSEMBLY__ +#include <linux/list.h> + typedef struct global_data { bd_t *bd; unsigned long flags;
@@ -80,6 +83,12 @@ typedef struct global_data {unsigned long start_addr_sp;/* start_addr_stackpointer */ unsigned long reloc_off;struct global_data *new_gd;/* relocated global data */ + +#ifdef CONFIG_DM +struct device *dm_root;/* Root instance for Driver Model */ +struct list_head uclass_root;/* Head of core tree */ +#endif +const void *fdt_blob;/* Our device tree, NULL if none */ void *new_fdt;/* Relocated FDT */ unsigned long fdt_size;/* Space reserved for relocated FDT */
include/dm.h new file +27 -0
@@ -0,0 +1,27 @@ +/* +* Copyright (C) 2013 Google, Inc +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, +* MA 02111-1307 USA +*/ + +#ifndef _DM_H_ +#define _DM_H + +#include <dm/device.h> +#include <dm/platform_data.h> +#include <dm/uclass.h> + +#endif
include/dm/device-internal.h new file +38 -0
@@ -0,0 +1,38 @@ +/* +* (C) Copyright 2012 +* Pavel Herrmann <[email protected]> +* Marek Vasut <[email protected]> +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, +* MA 02111-1307 USA +*/ + +#ifndef _DM_DEVICE_INTERNAL_H +#define _DM_DEVICE_INTERNAL_H + +struct device ; + +/* driver manager API */ +int device_bind (struct device *parent,struct driver *drv, +const char *name,void *platform_data,int of_offset, +struct device **devp); +int device_bind_by_name (struct device *parent,const struct driver_info *info, +struct device **devp); + +int device_probe (struct device *dev); +int device_remove (struct device *dev); +int device_unbind (struct device *dev); + +#endif
include/dm/device.h new file +72 -0
72 lines not on this page · raw message
include/dm/lists.h new file +37 -0
@@ -0,0 +1,37 @@ +/* +* (C) Copyright 2012 +* Pavel Herrmann <[email protected]> +* +* See file CREDITS for list of people who contributed to this +* project. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, +* MA 02111-1307 USA +*/ + +#ifndef _DM_LISTS_H_ +#define _DM_LISTS_H_ + +#include <dm/uclass-id.h> + +struct driver *lists_driver_lookup_name (const char *name); + +struct uclass_driver *lists_uclass_lookup (enum uclass_id id); + +int lists_bind_drivers (struct device *parent); + +int lists_bind_fdt (struct device *parent,const void *blob,int offset); + +#endif
include/dm/platform_data.h new file +38 -0
@@ -0,0 +1,38 @@ +/* +* Copyright (C) 2013 Google, Inc +* +* (C) Copyright 2012 +* Pavel Herrmann <[email protected]> +* Marek Vasut <[email protected]> +* +* See file CREDITS for list of people who contributed to this +* project. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, +* MA 02111-1307 USA +*/ + +#ifndef _DM_PLATFORM_DATA_H +#define _DM_PLATFORM_DATA_H + +struct driver_info { +const char *name; +const void *platform_data; +}; + +#define U_BOOT_DEVICE (__name) \ + ll_entry_declare(struct driver_info, __name, driver_info) + +#endif
include/dm/root.h new file +38 -0
@@ -0,0 +1,38 @@ +/* +* (C) Copyright 2012 +* Pavel Herrmann <[email protected]> +* +* See file CREDITS for list of people who contributed to this +* project. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, +* MA 02111-1307 USA +*/ + +#ifndef _DM_ROOT_H_ +#define _DM_ROOT_H_ + +struct device ; + +/* tree creation helpers */ +struct device *dm_root (void ); + +int dm_scan_platform_data (void ); + +int dm_scan_fdt (const void *blob); + +int dm_init (void ); + +#endif
include/dm/uclass-id.h new file +42 -0
42 lines not on this page · raw message
include/dm/uclass-internal.h new file +36 -0
@@ -0,0 +1,36 @@ +/* +* (C) Copyright 2012 +* Pavel Herrmann <[email protected]> +* +* See file CREDITS for list of people who contributed to this +* project. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, +* MA 02111-1307 USA +*/ + +#ifndef _DM_UCLASS_INTERNAL_H +#define _DM_UCLASS_INTERNAL_H + +int uclass_find_device (enum uclass_id id,int index,struct device **devp); + +int uclass_bind_device (struct device *dev); +int uclass_unbind_device (struct device *dev); +int uclass_post_probe_device (struct device *dev); +int uclass_pre_remove_device (struct device *dev); +struct uclass *uclass_find (enum uclass_id key); +int uclass_destroy (struct uclass *uc); + +#endif
include/dm/uclass.h new file +74 -0
74 lines not on this page · raw message
include/dm/util.h new file +42 -0
42 lines not on this page · raw message
Signature · 1 line
1.8.2.1
RFC PATCH v2 7/14 dm: Set up driver model after relocation
In reply to Simon GlassMake driver model available after relocation, by setting up data structures and scanning for devices using compiled-in platform_data and (when available) the device tree.
---
Changes in v2: None
common/board_r.c | 33 +++++++++++++++++++++++++++++++++ common/dm/lists.c | 2 ++ common/dm/root.c | 2 ++ 3 files changed, 37 insertions(+)
common/board_r.c +33 -0
@@ -34,6 +34,7 @@#ifdef CONFIG_HAS_DATAFLASH #include <dataflash.h> #endif +#include <dm.h> #include <environment.h> #include <fdtdec.h> #if defined(CONFIG_CMD_IDE )
@@ -66,7 +67,9 @@#ifdef CONFIG_X86 #include <asm/init_helpers.h> #endif +#include <dm/root.h> #include <linux/compiler.h> +#include <linux/err.h> DECLARE_GLOBAL_DATA_PTR ;
@@ -269,6 +272,33 @@ static int initr_malloc(void)return 0 ; } +#ifdef CONFIG_DM +static int initr_dm (void ) +{ +int ret; + + ret =dm_init (); +if (ret) { +debug ("dm_init() failed: %d\n" , ret); +return ret; + } + ret =dm_scan_platform_data (); +if (ret) { +debug ("dm_scan_platform_data() failed: %d\n" , ret); +return ret; + } +#ifdef CONFIG_OF_CONTROL + ret =dm_scan_fdt (gd->fdt_blob); +if (ret) { +debug ("dm_scan_fdt() failed: %d\n" , ret); +return ret; + } +#endif + +return 0 ; +} +#endif +__weak int power_init_board (void ) {return 0 ;
@@ -765,6 +795,9 @@ init_fnc_t init_sequence_r[] = {#endif initr_barrier, initr_malloc, +#ifdef CONFIG_DM + initr_dm, +#endif #ifdef CONFIG_ARCH_EARLY_INIT_R arch_early_init_r,#endif
common/dm/lists.c +2 -0
@@ -110,6 +110,7 @@ int lists_bind_drivers(struct device *parent)return result; } +#ifdef CONFIG_OF_CONTROL /** * driver_check_compatible() - Check if a driver is compatible with this node *
@@ -179,3 +180,4 @@ int lists_bind_fdt(struct device *parent, const void *blob, int offset) return result; } +#endif
common/dm/root.c +2 -0
@@ -92,6 +92,7 @@ int dm_scan_platform_data(void)return 0 ; } +#ifdef CONFIG_OF_CONTROL int dm_scan_fdt (const void *blob) {int offset =0 ;
@@ -112,6 +113,7 @@ int dm_scan_fdt(const void *blob)return ret; } +#endif U_BOOT_DRIVER (root_driver) = { .name ="root_driver" ,
Signature · 1 line
1.8.2.1
RFC PATCH v2 8/14 dm: Add basic tests
In reply to Simon GlassAdd some tests of driver model functionality. Coverage includes:
- basic init
- binding of drivers to devices using platform_data
- automatic probing of devices when referenced
- availability of platform data to devices
- lifecycle from bind to probe to remove to unbind
- renumbering within a uclass when devices are probed/removed
- calling driver-defined operations
- deactivation of drivers when removed
- memory leak across creation and destruction of drivers/uclasses
- uclass init/destroy methods
- automatic probe/remove of children/parents when needed
This function is enabled for sandbox, using CONFIG_DM_TEST.
---
Changes in v2: None
Makefile | 1 + include/configs/sandbox.h | 1 + include/dm/test.h | 132 +++++++++++ include/dm/ut.h | 83 +++++++ test/dm/Makefile | 52 +++++ test/dm/core.c | 557 ++++++++++++++++++++++++++++++++++++++++++++++ test/dm/test-driver.c | 159 +++++++++++++ test/dm/test-fdt.c | 148 ++++++++++++ test/dm/test-main.c | 120 ++++++++++ test/dm/test-uclass.c | 118 ++++++++++ test/dm/test.dts | 59 +++++ test/dm/ut.c | 46 ++++ 12 files changed, 1476 insertions(+) create mode 100644 include/dm/test.h create mode 100644 include/dm/ut.h create mode 100644 test/dm/Makefile create mode 100644 test/dm/core.c create mode 100644 test/dm/test-driver.c create mode 100644 test/dm/test-fdt.c create mode 100644 test/dm/test-main.c create mode 100644 test/dm/test-uclass.c create mode 100644 test/dm/test.dts create mode 100644 test/dm/ut.c
Makefile +1 -0
@@ -336,6 +336,7 @@ LIBS-y += lib/libfdt/libfdt.o LIBS-y += api/libapi.o LIBS-y += post/libpost.o LIBS-y += test/libtest.o +LIBS-y += test/dm/libtestdm.o LIBS-$(CONFIG_DM) += common/dm/libdm.o
include/configs/sandbox.h +1 -0
@@ -23,6 +23,7 @@#define __CONFIG_H#define CONFIG_DM +#define CONFIG_DM_TEST /* Number of bits in a C 'long' on this architecture */ #define CONFIG_SANDBOX_BITS_PER_LONG 64
include/dm/test.h new file +132 -0
132 lines not on this page · raw message
include/dm/ut.h new file +83 -0
83 lines not on this page · raw message
test/dm/Makefile new file +52 -0
52 lines not on this page · raw message
test/dm/core.c new file +557 -0
557 lines not on this page · raw message
test/dm/test-driver.c new file +159 -0
159 lines not on this page · raw message
test/dm/test-fdt.c new file +148 -0
148 lines not on this page · raw message
test/dm/test-main.c new file +120 -0
120 lines not on this page · raw message
test/dm/test-uclass.c new file +118 -0
118 lines not on this page · raw message
test/dm/test.dts new file +59 -0
59 lines not on this page · raw message
test/dm/ut.c new file +46 -0
46 lines not on this page · raw message
Signature · 1 line
1.8.2.1
RFC PATCH v2 9/14 dm: Add a 'dm' command for testing
In reply to Simon GlassThis command is not required for driver model operation, but can be useful for testing. It provides simple dumps of internal data structures.
---
Changes in v2: None
include/configs/sandbox.h | 1 + test/dm/Makefile | 1 + test/dm/cmd_dm.c | 147 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 test/dm/cmd_dm.c
include/configs/sandbox.h +1 -0
@@ -23,6 +23,7 @@#define __CONFIG_H#define CONFIG_DM +#define CONFIG_CMD_DM #define CONFIG_DM_TEST /* Number of bits in a C 'long' on this architecture */
test/dm/Makefile +1 -0
@@ -21,6 +21,7 @@ include $(TOPDIR)/config.mk LIB = $(obj)libtestdm.o +COBJS-$(CONFIG_CMD_DM) += cmd_dm.o COBJS-$(CONFIG_DM_TEST) += test-driver.o COBJS-$(CONFIG_DM_TEST) += test-fdt.o COBJS-$(CONFIG_DM_TEST) += test-main.o
test/dm/cmd_dm.c new file +147 -0
147 lines not on this page · raw message
Signature · 1 line
1.8.2.1
RFC PATCH v2 10/14 dm: Add a demonstration/example driver
In reply to Simon GlassAs an example of how to write a uclass and a driver, provide a demo version of each, accessible through the 'demo' command.
To use these with driver model, define CONFIG_CMD_DEMO and CONFIG_DM_DEMO.
The two demo drivers are enabled with CONFIG_DM_DEMO_SIMPLE and CONFIG_DM_DEMO_SHAPE.
---
Changes in v2: None
Makefile | 1 + common/Makefile | 1 + common/cmd_demo.c | 118 +++++++++++++++++++++++++++++++++++++++++++++ drivers/demo/Makefile | 44 +++++++++++++++++ drivers/demo/demo-pdata.c | 60 +++++++++++++++++++++++ drivers/demo/demo-shape.c | 107 ++++++++++++++++++++++++++++++++++++++++ drivers/demo/demo-simple.c | 46 ++++++++++++++++++ drivers/demo/demo-uclass.c | 54 +++++++++++++++++++++ include/configs/sandbox.h | 4 ++ include/dm-demo.h | 47 ++++++++++++++++++ 10 files changed, 482 insertions(+) create mode 100644 common/cmd_demo.c create mode 100644 drivers/demo/Makefile create mode 100644 drivers/demo/demo-pdata.c create mode 100644 drivers/demo/demo-shape.c create mode 100644 drivers/demo/demo-simple.c create mode 100644 drivers/demo/demo-uclass.c create mode 100644 include/dm-demo.h
Makefile +1 -0
@@ -338,6 +338,7 @@ LIBS-y += post/libpost.o LIBS-y += test/libtest.o LIBS-y += test/dm/libtestdm.o +LIBS-$(CONFIG_DM_DEMO) += drivers/demo/libdemo.o LIBS-$(CONFIG_DM) += common/dm/libdm.o ifneq ($(CONFIG_AM33XX)$(CONFIG_OMAP34XX)$(CONFIG_OMAP44XX)$(CONFIG_OMAP54XX)$(CONFIG_TI814X),)
common/Makefile +1 -0
@@ -85,6 +85,7 @@ COBJS-$(CONFIG_CMD_CONSOLE) += cmd_console.o COBJS-$(CONFIG_CMD_CPLBINFO) += cmd_cplbinfo.o COBJS-$(CONFIG_DATAFLASH_MMC_SELECT) += cmd_dataflash_mmc_mux.o COBJS-$(CONFIG_CMD_DATE) += cmd_date.o +COBJS-$(CONFIG_CMD_DEMO) += cmd_demo.o COBJS-$(CONFIG_CMD_SOUND) += cmd_sound.o ifdef CONFIG_4xx COBJS-$(CONFIG_CMD_SETGETDCR) += cmd_dcr.o
common/cmd_demo.c new file +118 -0
118 lines not on this page · raw message
drivers/demo/Makefile new file +44 -0
44 lines not on this page · raw message
drivers/demo/demo-pdata.c new file +60 -0
60 lines not on this page · raw message
drivers/demo/demo-shape.c new file +107 -0
107 lines not on this page · raw message
drivers/demo/demo-simple.c new file +46 -0
46 lines not on this page · raw message
drivers/demo/demo-uclass.c new file +54 -0
54 lines not on this page · raw message
include/configs/sandbox.h +4 -0
@@ -23,7 +23,11 @@#define __CONFIG_H#define CONFIG_DM +#define CONFIG_CMD_DEMO #define CONFIG_CMD_DM +#define CONFIG_DM_DEMO +#define CONFIG_DM_DEMO_SIMPLE +#define CONFIG_DM_DEMO_SHAPE #define CONFIG_DM_TEST /* Number of bits in a C 'long' on this architecture */
include/dm-demo.h new file +47 -0
47 lines not on this page · raw message
Signature · 1 line
1.8.2.1
RFC PATCH v2 11/14 dm: Add GPIO support and tests
In reply to Simon GlassAdd driver model support for GPIOs. Since existing GPIO drivers do not use driver model, this feature must be enabled by CONFIG_DM_GPIO. After drivers are converted over
Tests are provided for the sandbox implementation, and are a sufficient sanity check for basic operation.
The GPIO uclass understands the concept of named banks of GPIOs, which each GPIO device pricing a single bank. Within each bank the GPIOs are numbered using an offset from 0 to n-1. For example a bank named 'b' with 20 offsets will provide GPIOs named b0 to b19.
Anonymous GPIO banks are also supported, and are just numbered without any prefix.
Each time a GPIO driver is added to the uclass, the GPIOs are renumbered accordinging, so there is always a global GPIO numbering order.
---
Changes in v2: None
drivers/gpio/Makefile | 2 + drivers/gpio/gpio-uclass.c | 281 +++++++++++++++++++++++++++++++++++++++++++++ include/asm-generic/gpio.h | 50 ++++++++ test/dm/gpio.c | 124 ++++++++++++++++++++ 4 files changed, 457 insertions(+) create mode 100644 drivers/gpio/gpio-uclass.c create mode 100644 test/dm/gpio.c
drivers/gpio/Makefile +2 -0
@@ -25,6 +25,8 @@ include $(TOPDIR)/config.mk LIB := $(obj)libgpio.o +COBJS-$(CONFIG_DM_GPIO) += gpio-uclass.o + COBJS-$(CONFIG_AT91_GPIO) += at91_gpio.o COBJS-$(CONFIG_INTEL_ICH6_GPIO) += intel_ich6_gpio.o COBJS-$(CONFIG_KIRKWOOD_GPIO) += kw_gpio.o
drivers/gpio/gpio-uclass.c new file +281 -0
281 lines not on this page · raw message
include/asm-generic/gpio.h +50 -0
54 lines not on this page · raw message
test/dm/gpio.c new file +124 -0
124 lines not on this page · raw message
Signature · 1 line
1.8.2.1
RFC PATCH v2 12/14 sandbox: Convert GPIOs to use driver model
In reply to Simon GlassConvert sandbox over to use driver model GPIOs.
---
Changes in v2: None
arch/sandbox/include/asm/gpio.h | 14 +-- board/sandbox/sandbox/sandbox.c | 7 +- drivers/gpio/sandbox.c | 216 +++++++++++++++++++++++++--------------- include/configs/sandbox.h | 1 + 4 files changed, 150 insertions(+), 88 deletions(-)
arch/sandbox/include/asm/gpio.h +5 -9
@@ -45,7 +45,7 @@* @param gp GPIO number * @return -1 on error, 0 if GPIO is low, >0 if high */ -int sandbox_gpio_get_value (unsigned gp); +int sandbox_gpio_get_value (struct device *dev,unsigned int offset);/** * Set the simulated value of a GPIO (used only in sandbox test code)
@@ -54,7 +54,7 @@ int sandbox_gpio_get_value(unsigned gp);* @param value value to set (0 for low, non-zero for high) * @return -1 on error, 0 if ok */ -int sandbox_gpio_set_value (unsigned gp,int value); +int sandbox_gpio_set_value (struct device *dev,unsigned int offset,int value);/** * Return the simulated direction of a GPIO (used only in sandbox test code)
@@ -62,7 +62,7 @@ int sandbox_gpio_set_value(unsigned gp, int value);* @param gp GPIO number * @return -1 on error, 0 if GPIO is input, >0 if output */ -int sandbox_gpio_get_direction (unsigned gp); +int sandbox_gpio_get_direction (struct device *dev,unsigned int offset);/** * Set the simulated direction of a GPIO (used only in sandbox test code)
@@ -71,11 +71,7 @@ int sandbox_gpio_get_direction(unsigned gp);* @param output 0 to set as input, 1 to set as output * @return -1 on error, 0 if ok */ -int sandbox_gpio_set_direction (unsigned gp,int output); - -/* Display information about each GPIO */ -void gpio_info (void ); - -#define gpio_status () gpio_info() +int sandbox_gpio_set_direction (struct device *dev,unsigned int offset, +int output);#endif
board/sandbox/sandbox/sandbox.c +6 -1
@@ -20,7 +20,7 @@*/ #include <common.h> - +#include <dm.h> #include <os.h> /*
@@ -30,6 +30,11 @@*/ gd_t *gd; +/* Add a simple GPIO device */ +U_BOOT_DEVICE (gpio_sandbox) = { + .name ="gpio_sandbox" , +}; +void flush_cache (unsigned long start,unsigned long size) { }
drivers/gpio/sandbox.c +138 -78
317 lines not on this page · raw message
include/configs/sandbox.h +1 -0
@@ -28,6 +28,7 @@#define CONFIG_DM_DEMO #define CONFIG_DM_DEMO_SIMPLE #define CONFIG_DM_DEMO_SHAPE +#define CONFIG_DM_GPIO #define CONFIG_DM_TEST /* Number of bits in a C 'long' on this architecture */
Signature · 1 line
1.8.2.1
RFC PATCH v2 13/14 dm: Enable gpio command to support driver model
In reply to Simon GlassNow that named GPIO banks are supported, along with a way of obtaining the status of a GPIO (input or output), we can provide an enhanced GPIO command for driver model. Where the driver provides its own operation for obtaining the GPIO state, this is used, otherwise a generic version is sufficient.
---
Changes in v2: None
common/cmd_gpio.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 114 insertions(+), 13 deletions(-)
common/cmd_gpio.c +114 -13
@@ -8,7 +8,7 @@#include <common.h> #include <command.h> - +#include <dm.h> #include <asm/gpio.h> #ifndef name_to_gpio
@@ -22,25 +22,115 @@ enum gpio_cmd { GPIO_TOGGLE, }; +#if defined(CONFIG_DM_GPIO ) && !defined(gpio_status) +static const char *const gpio_function[] = { +"input" , +"output" , +"unknown" , +}; + +static void show_gpio (struct device *dev,const char *bank_name,int offset) +{ +struct dm_gpio_ops *ops =gpio_get_ops (dev); +char buf[80 ]; +int ret; + + *buf ='\0' ; +if (ops->get_state) { + ret = ops->get_state (dev, offset, buf,sizeof (buf)); +if (ret) { +puts ("<unknown>" ); +return ; + } + }else { +int func =GPIOF_UNKNOWN ; +int ret; + +if (ops->get_function) { + ret = ops->get_function (dev, offset); +if (ret >=0 && ret <ARRAY_SIZE (gpio_function)) + func = ret; + } +sprintf (buf,"%s%u: %8s %d" , bank_name, offset, + gpio_function[func], ops->get_value (dev, offset)); + } + +puts (buf); +puts ("\n" ); +} + +static int do_gpio_status (const char *gpio_name) +{ +struct device *dev; +int newline =0 ; +int ret; + +if (gpio_name && !*gpio_name) + gpio_name =NULL ; +for (ret =uclass_first_device (UCLASS_GPIO , &dev); + dev; + ret =uclass_next_device (&dev)) { +const char *bank_name; +int num_bits; + + bank_name =gpio_get_bank_info (dev, &num_bits); + +if (!gpio_name || !bank_name || + !strncmp (gpio_name, bank_name,strlen (bank_name))) { +const char *p =NULL ; +int offset; + +if (bank_name) { +if (newline) +putc ('\n' ); +printf ("Bank %s:\n" , bank_name); + } + newline =1 ; +if (gpio_name && bank_name) { + p = gpio_name +strlen (bank_name); + offset =simple_strtoul (p,NULL ,10 ); +show_gpio (dev, bank_name, offset); + }else { +for (offset =0 ; offset < num_bits; offset++) +show_gpio (dev, bank_name, offset); + } + } + } + +return ret; +} +#endif +static int do_gpio (cmd_tbl_t *cmdtp,int flag,int argc,char *const argv[]) { -int gpio; +unsigned int gpio;enum gpio_cmd sub_cmd;ulong value; -const char *str_cmd, *str_gpio; +const char *str_cmd, *str_gpio =NULL ; +#ifdef CONFIG_DM_GPIO +int ret; +#endif +if (argc <2 ) + show_usage: +return CMD_RET_USAGE ; + str_cmd = argv[1 ]; +if (argc >2 ) + str_gpio = argv[2 ]; +if (!strcmp (str_cmd,"status" )) { +/* Support deprecated gpio_status() */ #ifdef gpio_status -if (argc ==2 && !strcmp (argv[1 ],"status" )) {gpio_status ();return 0 ; - } +#elif defined(CONFIG_DM_GPIO ) +return cmd_process_error (cmdtp,do_gpio_status (str_gpio)); +#else + goto show_usage;#endif + } -if (argc !=3 ) - show_usage: -return CMD_RET_USAGE ; - str_cmd = argv[1 ]; - str_gpio = argv[2 ]; +if (!str_gpio) + goto show_usage;/* parse the behavior */ switch (*str_cmd) {
@@ -51,11 +141,21 @@ static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])default : goto show_usage; } +#if defined(CONFIG_DM_GPIO ) +/* +* TODO(sjg at chromium.org): Convert this code over to use the GPIO +* uclass interface instead of the numbered GPIO compatibility +* layer. +*/ + ret =gpio_lookup_name (str_gpio,NULL ,NULL , &gpio); +if (ret) +return cmd_process_error (cmdtp, ret); +#else /* turn the gpio name into a gpio number */ gpio =name_to_gpio (str_gpio);if (gpio <0 ) goto show_usage; - +#endif /* grab the pin before we tweak it */ if (gpio_request (gpio,"cmd_gpio" )) {printf ("gpio: requesting pin %u failed\n" , gpio);
@@ -84,6 +184,7 @@ static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) }U_BOOT_CMD (gpio,3 ,0 , do_gpio, -"input/set/clear/toggle gpio pins" , +"query and control gpio pins" ,"<input|set|clear|toggle> <pin>\n" -" - input/set/clear/toggle the specified pin" ); +" - input/set/clear/toggle the specified pin\n" +"gpio status [<bank> | <pin>]" );
Signature · 1 line
1.8.2.1
RFC PATCH v2 14/14 dm: Add README for driver model
In reply to Simon GlassFrom: Pavel Herrmann <[email protected]>
This adds a README to help with understanding of this series.
---
Changes in v2:
- Removed pointer return values in favour of integer
- Use driver_bind() in dm_init() instead of writing new code
- Allow driver_bind() to support a NULL parent
- Add dm_warn() to warn about impending doom
- Standardise variable names (e.g. uclass instead of class)
- Remove relocation functions
- Add new header file for lists
- Add new util file to hold utility functions
- Allow a driver to bind to only one uclass
- Remove unneeded arguments to uclass_bind(), uclass_unbind()
- Rename struct device's 'bus' to 'parent'
- Rename data structures to hopefully be clearer
- Put platform_data definitions in their own header file
- Add U_BOOT_DEVICE to declare platform_data
- Add auto-probing feature for platform_data to avoid driver_bind() calls
- Add simple unit test functions
- Add test infrastructure for driver model
- Add integration tests for driver model
- Add device tree support in driver model
- Add automatic allocation of platform_data for FDT
- Add automatic allocation of priv data for devices
- Add automatic allocation of device-specific priv data for uclasses
- Add GPIO uclass and tests
- Add sandbox GPIO driver
- Update gpio command to use driver model
- Add tests for core code
- Add script to run tests
- Add a single include/dm.h to bring in driver model code
doc/driver-model/README.txt | 329 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 329 insertions(+) create mode 100644 doc/driver-model/README.txt
doc/driver-model/README.txt new file +329 -0
@@ -0,0 +1,329 @@ +Driver Model RFC +================ + +This README contains information about an early RFC implementation of +driver model. The original work was done by: + + Marek Vasut <[email protected]> + Pavel Herrmann <[email protected]> + Viktor K?iv?k <[email protected]> + Tomas Hlavacek <[email protected]> + +The code was taken from branch dm at: + + git://git.denx.de/u-boot-marex.git + +and heavily modified. You can find a test version of the code used here in +branch dm2 at: + + http://git.denx.de/u-boot-x86.git + +(Branch dm contains the original implementation) + + +The goal of this effort is to get a very basic implementation into +the U-Boot source tree quickly - something which people can build on, +rather than something that solves all of the problems immediately. +The only driver support available is GPIO, because it is simple, +but non-trivial. + + +Terminology +----------- + +Uclass - a group of device which operate in the same way. A uclass provides + a way of accessing invidual devices within the group, but always + using the same interface. For example a GPIO uclass provides + operations for get/set value. An I2C uclass may have 10 I2C ports, + 4 with one driver, and 6 with another. + +Driver - some code which talks to a peripheral and presents a unified + interface to it. + +Device - an instance of a driver, tied to a particular port or peripheral. + + +How to try it +------------- + +Build U-Boot sandbox and run it: + + make sandbox_config + make + ./u-boot + + (type 'reset' to exit U-Boot) + + +There is a uclass called 'demo'. This uclass handles +saying hello, and reporting its status. There are two drivers in this +uclass: + + - simple: Just prints a message for hello, doesn't implement status + - shape: Prints shapes and reports number of characters printed as status + +The demo class is pretty simple, but not trivial. The intention is that it +can be used for testing, so it will implement all driver model features and +provide 100% code coverage of them. It does have multiple drivers, it +handles parameter data and platform_data (data which tells the driver how +to operate on a particular platform) and it uses private driver data. + +To try it, see the example session below: + +=>demo hello 1 +Hello '@' from 07981110: red 4 +=>demo status 2 +Status: 0 +=>demo hello 2 +g +r@ +e@@ +e@@@ +n@@@@ +g@@@@@ +=>demo status 2 +Status: 21 +=>demo hello 4 ^ + y^^^ + e^^^^^ +l^^^^^^^ +l^^^^^^^ + o^^^^^ + w^^^ +=>demo status 4 +Status: 36 +=> + + +Running the tests +----------------- + +The intent with driver model is that the core portion has 100% test coverage +in sandbox, and every uclass has its own test. As a move towards this, tests +are provided in test/dm. To run them, try: + + ./test/dm/test-dm.sh + + +What is going on? +----------------- + +Let's start at the top. The demo command is in common/cmd_demo.c. It does +the usual command procesing and then: + + struct device *demo_dev; + + ret = uclass_get_device(UCLASS_DEMO, &demo_dev); + +UCLASS_DEMO means the class of devices which implement 'demo'. Other +classes might be MMC, or GPIO, hashing or serial. The idea is that the +devices in the class all share a particular way of working. The class +presents a unified view of all these devices to U-Boot. + +This function looks up the device for the demo uclass. Given a device +number we can find the device because all devices have registered with +the UCLASS_DEMO uclass. + +Having found the device, we activate it with: + + ret = driver_activate(demo_dev); + +This is because all devices are inactive until used in U-Boot. The exact +mechanism of activating a driver will hopefully change. For example, i +may be possible to combine uclass_get_child() and driver_activate(). + +Now that we have the device we can do things like: + + return demo_hello(demo_dev, ch); + +This function is in the demo uclass. It takes care of calling the 'hello' +method of the relevant driver. Bearing in mind that there are two drivers, +this particular device may use one or other of them. + +The code for demo_hello() is in drivers/demo/demo-uclass.c: + +int demo_hello(struct device *dev, int ch) +{ + const struct demo_ops *ops = device_get_ops(dev); + + if (!ops->hello) + return -ENOSYS; + + return ops->hello(dev, ch); +} + +As you can see it just calls the relevant driver method. One of these is +in drivers/demo/demo-simple.c: + +static int simple_hello(struct device *dev, int ch) +{ + const struct dm_demo_pdata *pdata = dev->platform_data; + + printf("Hello '%c' from %08x: %s %d\n", ch, map_to_sysmem(dev), + pdata->colour, pdata->sides); + + return 0; +} + + +So that is a trip from top to bottom but it leaves a lot of topics to +address. + + +Declaring Drivers +----------------- + +A driver declaration looks something like this (see +drivers/demo/demo-shape.c): + +static const struct demo_ops simple_ops = { + .hello = shape_hello, + .status = shape_status, +}; + +U_BOOT_DRIVER(demo_shape_drv) = { + .name = "demo_shape_drv", + .id = UCLASS_DEMO, + .ops = &simple_ops, + .priv_data_size = sizeof(struct shape_data), +}; + + +This driver has two methods (hello and status) and requires a bit +of private data (accessible through dev->priv once the driver has +been probed). It is a member of UCLASS_DEMO so will register itself +there. + +In U_BOOT_DRIVER it is also possible to specify special methods for probe, +and bind, and these are called at appropriate times. For many drivers +it is hoped that only 'probe' and 'remove' will be needed. + +The U_BOOT_DRIVER macro creates a data structure accessible from C, +so driver model can find the drivers that are available. + + +Platform Data +------------- + +Where does the platform data come from? See common/board_r.c which +sets up a table of driver names and their associated platform data. +The data can be interpreted by the drivers however they like - it is +basically a communication scheme between the board-specific code and +the generic drivers, which are intended to work on any board. + +Drivers can acceess their data via dev->info->platform_data. Here is +the declaration for the platform data, which would normally appear +in the board file. + + static const struct dm_demo_cdata red_square = { + .colour = "red", + .sides = 4. + }; + static const struct driver_info info[] = { + { + .name = "demo_shape_drv", + .platform_data = &red_square, + }, + }; + + demo1 = driver_bind(root, &info[0]); + + +Device Tree +----------- + +This is now implemented. You can use device tree to specify the platform data +and activated drivers. In other words we replace the above code with the +following device treefragment: + + red-square { + compatible = "demo-shape"; + colour = "red"; + sides = <4>; + }; + + +The easiest way to make this work it to add a platform_data_size member to +the driver: + + .platform_data_size = sizeof(struct dm_test_pdata), + +This will be allocated and zeroed before the driver's probe method is called. +The driver can then read the information out of the device tree and put it +in dev->priv. + + +Declaring Uclasses +------------------ + +The demo uclass is declared like this: + +U_BOOT_CLASS(demo) = { + .id = UCLASS_DEMO, +}; + +It is also possible to specify special methods for probe, etc. The uclass +numbering comes from include/dm/uclass.h. + + +Data Structures +--------------- + +Driver model uses a doubly-linked list as the basic data structure. Some +nodes have several lists running through them. Creating a more efficient +data structure might be worthwhile in some rare cases, once we understand +what the bottlenecks are. + + +Changes so far +-------------- + +The documenation in this direction is out of data and until the patch +is in more solid form I have avoided updating it. This implementation +uses a very similar approach, but makes at least the following changes: + +- Tried to agressively remove boilerplate, so that for most drivers there +is little or no 'driver model' code to write. +- Moved some data from code into data structure - e.g. store a pointer to +the driver operations structure in the driver, rather than passing it +to the driver bind function. +- Rename some structures to make them more similar to Linux (struct device +instead of struct instance, struct platform_data, etc.) +- Change the name 'core' to 'uclass', meaning U-Boot class. It seems that +this concept relates to a class of drivers (or a subsystem). We shouldn't +use 'class' since it is a C++ reserved word, so U-Boot class (uclass) seems +better than 'core'. +- Remove 'struct driver_instance' and just use a single 'struct device'. +This removes a level of indirection that doesn't seem necessary. +- Built in device tree support, to avoid the need for platform_data +- Removed the concept of driver relocation, and just make it possible for +the new driver (created after relocation) to access the old driver data. +I feel that relocation is a very special case and will only apply to a few +drivers, many of which can/will just re-init anyway. So the overhead of +dealing with this might not be worth it. +- Implemented a GPIO system, trying to keep it simple + +Proposed additional changes +--------------------------- + +- Maybe add device tree support to the demo driver +- Update the documenation to match the new implementation + + +Things to punt for later +------------------------ + +- SPL support - this will have to be present before many drivers can be +converted, but it seems like we can add it once we are happy with the +core implementation. +- Pre-relocation support - similar story + +That is not to say that no thinking has gone into these - in fact there +is quite a lot there. However, getting these right is non-trivial and +there is a high cost associated with going down the wrong path. + + +Simon Glass +sjg at chromium.org +April 2013 +Updated 7-May-13
Signature · 1 line
1.8.2.1
v2 cover Driver model implementation, tests, demo and GPIO
In reply to Simon GlassHi,
Quoted 19 lines · Simon Glass
Note: If you are reviewing this code, but don't have a lot of time, please consider starting with the 'demo' driver (patch 'dm: Add a demonstration/example driver') since it clearly shows how devices and uclasses work. Much of this series consists of test code and plumbing, so is of less interest to driver authors.
This patch adds a driver model implementation. It is taken from the driver model code developed by:
Marek Vasut <[email protected]>
Pavel Herrmann <[email protected]>
Viktor K?iv?k <[email protected]>
Tomas Hlavacek <[email protected]>Please see
doc/driver-model/README.txtfor details of how to run this and what to look for. So far the documentation in doc/driver-model has not been updated.
Quoted 5 lines
You can find a test version of the code used here in branch dm2 at:
http://git.denx.de/u-boot-x86.git(Branch dm contains the original implementation)
Does anyone have any comments on this series please?
Regards,
Simon
Quoted 145 lines
Changes in v2:
- Removed pointer return values in favour of integer
- Usedriver_bind()indm_init()instead of writing new code
- Allowdriver_bind()to support a NULL parent
- Adddm_warn()to warn about impending doom
- Standardise variable names (e.g. uclass instead of class)
- Remove relocation functions
- Add new header file for lists
- Add new util file to hold utility functions
- Allow a driver to bind to only one uclass
- Remove unneeded arguments touclass_bind(),uclass_unbind()
- Rename struct device's 'bus' to 'parent'
- Rename data structures to hopefully be clearer
- Putplatform_datadefinitions in their own header file
- AddU_BOOT_DEVICEto declareplatform_data
- Add auto-probing feature forplatform_datato avoiddriver_bind()calls
- Add simple unit test functions
- Add test infrastructure for driver model
- Add integration tests for driver model
- Add device tree support in driver model
- Add automatic allocation ofplatform_datafor FDT
- Add automatic allocation of priv data for devices
- Add automatic allocation of device-specific priv data for uclasses
- Add GPIO uclass and tests
- Add sandbox GPIO driver
- Update gpio command to use driver model
- Add tests for core code
- Add script to run tests
- Add a singleinclude/dm.hto bring in driver model codePavel Herrmann (1):
dm: Add README for driver modelSimon Glass (13):
sandbox: Makemap_to_sysmem()use a constant pointer
sandbox: Correct data sizes andprintf()strings infdtdec.c
sandbox: config: Don't use 64-bit physical memory Addcmd_process_error()to report and process errors
sandbox: config: Enable driver model
dm: Add base driver model support
dm: Set up driver model after relocation
dm: Add basic tests
dm: Add a 'dm' command for testing
dm: Add a demonstration/example driver
dm: Add GPIO support and tests
sandbox: Convert GPIOs to use driver model
dm: Enable gpio command to support driver modelMakefile | 4 + arch/sandbox/include/asm/gpio.h | 14 +- arch/sandbox/include/asm/io.h | 2 +- arch/sandbox/include/asm/types.h | 4 +- board/sandbox/sandbox/sandbox.c | 7 +- common/Makefile | 1 + common/board_r.c | 33 +++ common/cmd_demo.c | 118 ++++++++ common/cmd_gpio.c | 127 ++++++++- common/command.c | 10 + common/dm/Makefile | 40 +++ common/dm/device.c | 370 +++++++++++++++++++++++++ common/dm/lists.c | 183 +++++++++++++ common/dm/root.c | 126 +++++++++ common/dm/uclass.c | 362 +++++++++++++++++++++++++ common/dm/util.c | 50 ++++ doc/driver-model/README.txt | 329 ++++++++++++++++++++++ drivers/demo/Makefile | 44 +++ drivers/demo/demo-pdata.c | 60 ++++ drivers/demo/demo-shape.c | 107 ++++++++ drivers/demo/demo-simple.c | 46 ++++ drivers/demo/demo-uclass.c | 54 ++++ drivers/gpio/Makefile | 2 + drivers/gpio/gpio-uclass.c | 281 +++++++++++++++++++ drivers/gpio/sandbox.c | 216 +++++++++------ include/asm-generic/global_data.h | 9 + include/asm-generic/gpio.h | 50 ++++ include/command.h | 9 + include/common.h | 2 +- include/configs/sandbox.h | 10 +- include/dm-demo.h | 47 ++++ include/dm.h | 27 ++ include/dm/device-internal.h | 38 +++ include/dm/device.h | 72 +++++ include/dm/lists.h | 37 +++ include/dm/platform_data.h | 38 +++ include/dm/root.h | 38 +++ include/dm/test.h | 132 +++++++++ include/dm/uclass-id.h | 42 +++ include/dm/uclass-internal.h | 36 +++ include/dm/uclass.h | 74 +++++ include/dm/ut.h | 83 ++++++ include/dm/util.h | 42 +++ lib/fdtdec.c | 8 +- test/dm/Makefile | 53 ++++ test/dm/cmd_dm.c | 147 ++++++++++ test/dm/core.c | 557 ++++++++++++++++++++++++++++++++++++++ test/dm/gpio.c | 124 +++++++++ test/dm/test-driver.c | 159 +++++++++++ test/dm/test-fdt.c | 148 ++++++++++ test/dm/test-main.c | 120 ++++++++ test/dm/test-uclass.c | 118 ++++++++ test/dm/test.dts | 59 ++++ test/dm/ut.c | 46 ++++ 54 files changed, 4805 insertions(+), 110 deletions(-) create mode 100644 common/cmd_demo.c create mode 100644 common/dm/Makefile create mode 100644 common/dm/device.c create mode 100644 common/dm/lists.c create mode 100644 common/dm/root.c create mode 100644 common/dm/uclass.c create mode 100644 common/dm/util.c create mode 100644 doc/driver-model/README.txt create mode 100644 drivers/demo/Makefile create mode 100644 drivers/demo/demo-pdata.c create mode 100644 drivers/demo/demo-shape.c create mode 100644 drivers/demo/demo-simple.c create mode 100644 drivers/demo/demo-uclass.c create mode 100644 drivers/gpio/gpio-uclass.c create mode 100644 include/dm-demo.h create mode 100644 include/dm.h create mode 100644 include/dm/device-internal.h create mode 100644 include/dm/device.h create mode 100644 include/dm/lists.h create mode 100644 include/dm/platform_data.h create mode 100644 include/dm/root.h create mode 100644 include/dm/test.h create mode 100644 include/dm/uclass-id.h create mode 100644 include/dm/uclass-internal.h create mode 100644 include/dm/uclass.h create mode 100644 include/dm/ut.h create mode 100644 include/dm/util.h create mode 100644 test/dm/Makefile create mode 100644 test/dm/cmd_dm.c create mode 100644 test/dm/core.c create mode 100644 test/dm/gpio.c create mode 100644 test/dm/test-driver.c create mode 100644 test/dm/test-fdt.c create mode 100644 test/dm/test-main.c create mode 100644 test/dm/test-uclass.c create mode 100644 test/dm/test.dts create mode 100644 test/dm/ut.c--
1.8.2.1
v2 cover Driver model implementation, tests, demo and GPIO
Dear Simon Glass,
Quoted 21 lines
Hi,
Quoted 18 lines · Simon Glass
Note: If you are reviewing this code, but don't have a lot of time, please consider starting with the 'demo' driver (patch 'dm: Add a demonstration/example driver') since it clearly shows how devices and uclasses work. Much of this series consists of test code and plumbing, so is of less interest to driver authors.
This patch adds a driver model implementation. It is taken from
the driver model code developed by:
Marek Vasut <[email protected]>
Pavel Herrmann <[email protected]>
Viktor K?iv?k <[email protected]>
Tomas Hlavacek <[email protected]>Please see
doc/driver-model/README.txtfor details of how to run this and what to look for. So far the documentation in doc/driver-model has not been updated.
Quoted 6 lines
Quoted 4 lines
You can find a test version of the code used here in branch dm2 at:
http://git.denx.de/u-boot-x86.git(Branch dm contains the original implementation)
Does anyone have any comments on this series please?
I really cannot focus on much until 11th next month, sorry.
I should be able to review it after then ...
Best regards,
Marek Vasut