xref: /rk3399_rockchip-uboot/doc/driver-model/README.txt (revision 34e4a2ec0ad95cea910094e33761bddf56ad7fc0)
165c70539SSimon GlassDriver Model
265c70539SSimon Glass============
365c70539SSimon Glass
465c70539SSimon GlassThis README contains high-level information about driver model, a unified
565c70539SSimon Glassway of declaring and accessing drivers in U-Boot. The original work was done
665c70539SSimon Glassby:
765c70539SSimon Glass
865c70539SSimon Glass   Marek Vasut <marex@denx.de>
965c70539SSimon Glass   Pavel Herrmann <morpheus.ibis@gmail.com>
1065c70539SSimon Glass   Viktor Křivák <viktor.krivak@gmail.com>
1165c70539SSimon Glass   Tomas Hlavacek <tmshlvck@gmail.com>
1265c70539SSimon Glass
1365c70539SSimon GlassThis has been both simplified and extended into the current implementation
1465c70539SSimon Glassby:
1565c70539SSimon Glass
1665c70539SSimon Glass   Simon Glass <sjg@chromium.org>
1765c70539SSimon Glass
1865c70539SSimon Glass
1965c70539SSimon GlassTerminology
2065c70539SSimon Glass-----------
2165c70539SSimon Glass
2265c70539SSimon GlassUclass - a group of devices which operate in the same way. A uclass provides
23*34e4a2ecSChris Packham	a way of accessing individual devices within the group, but always
2465c70539SSimon Glass	using the same interface. For example a GPIO uclass provides
2565c70539SSimon Glass	operations for get/set value. An I2C uclass may have 10 I2C ports,
2665c70539SSimon Glass	4 with one driver, and 6 with another.
2765c70539SSimon Glass
2865c70539SSimon GlassDriver - some code which talks to a peripheral and presents a higher-level
2965c70539SSimon Glass	interface to it.
3065c70539SSimon Glass
3165c70539SSimon GlassDevice - an instance of a driver, tied to a particular port or peripheral.
3265c70539SSimon Glass
3365c70539SSimon Glass
3465c70539SSimon GlassHow to try it
3565c70539SSimon Glass-------------
3665c70539SSimon Glass
3765c70539SSimon GlassBuild U-Boot sandbox and run it:
3865c70539SSimon Glass
3965c70539SSimon Glass   make sandbox_config
4065c70539SSimon Glass   make
4165c70539SSimon Glass   ./u-boot
4265c70539SSimon Glass
4365c70539SSimon Glass   (type 'reset' to exit U-Boot)
4465c70539SSimon Glass
4565c70539SSimon Glass
4665c70539SSimon GlassThere is a uclass called 'demo'. This uclass handles
4765c70539SSimon Glasssaying hello, and reporting its status. There are two drivers in this
4865c70539SSimon Glassuclass:
4965c70539SSimon Glass
5065c70539SSimon Glass   - simple: Just prints a message for hello, doesn't implement status
5165c70539SSimon Glass   - shape: Prints shapes and reports number of characters printed as status
5265c70539SSimon Glass
5365c70539SSimon GlassThe demo class is pretty simple, but not trivial. The intention is that it
5465c70539SSimon Glasscan be used for testing, so it will implement all driver model features and
5565c70539SSimon Glassprovide good code coverage of them. It does have multiple drivers, it
5665c70539SSimon Glasshandles parameter data and platdata (data which tells the driver how
5765c70539SSimon Glassto operate on a particular platform) and it uses private driver data.
5865c70539SSimon Glass
5965c70539SSimon GlassTo try it, see the example session below:
6065c70539SSimon Glass
6165c70539SSimon Glass=>demo hello 1
6265c70539SSimon GlassHello '@' from 07981110: red 4
6365c70539SSimon Glass=>demo status 2
6465c70539SSimon GlassStatus: 0
6565c70539SSimon Glass=>demo hello 2
6665c70539SSimon Glassg
6765c70539SSimon Glassr@
6865c70539SSimon Glasse@@
6965c70539SSimon Glasse@@@
7065c70539SSimon Glassn@@@@
7165c70539SSimon Glassg@@@@@
7265c70539SSimon Glass=>demo status 2
7365c70539SSimon GlassStatus: 21
7465c70539SSimon Glass=>demo hello 4 ^
7565c70539SSimon Glass  y^^^
7665c70539SSimon Glass e^^^^^
7765c70539SSimon Glassl^^^^^^^
7865c70539SSimon Glassl^^^^^^^
7965c70539SSimon Glass o^^^^^
8065c70539SSimon Glass  w^^^
8165c70539SSimon Glass=>demo status 4
8265c70539SSimon GlassStatus: 36
8365c70539SSimon Glass=>
8465c70539SSimon Glass
8565c70539SSimon Glass
8665c70539SSimon GlassRunning the tests
8765c70539SSimon Glass-----------------
8865c70539SSimon Glass
8965c70539SSimon GlassThe intent with driver model is that the core portion has 100% test coverage
9065c70539SSimon Glassin sandbox, and every uclass has its own test. As a move towards this, tests
9165c70539SSimon Glassare provided in test/dm. To run them, try:
9265c70539SSimon Glass
9365c70539SSimon Glass   ./test/dm/test-dm.sh
9465c70539SSimon Glass
9565c70539SSimon GlassYou should see something like this:
9665c70539SSimon Glass
9765c70539SSimon Glass    <...U-Boot banner...>
9865c70539SSimon Glass    Running 12 driver model tests
9965c70539SSimon Glass    Test: dm_test_autobind
10065c70539SSimon Glass    Test: dm_test_autoprobe
10165c70539SSimon Glass    Test: dm_test_children
10265c70539SSimon Glass    Test: dm_test_fdt
10365c70539SSimon Glass    Test: dm_test_gpio
10465c70539SSimon Glass    sandbox_gpio: sb_gpio_get_value: error: offset 4 not reserved
10565c70539SSimon Glass    Test: dm_test_leak
10665c70539SSimon Glass    Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c
10765c70539SSimon Glass    Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c
10865c70539SSimon Glass    Test: dm_test_lifecycle
10965c70539SSimon Glass    Test: dm_test_operations
11065c70539SSimon Glass    Test: dm_test_ordering
11165c70539SSimon Glass    Test: dm_test_platdata
11265c70539SSimon Glass    Test: dm_test_remove
11365c70539SSimon Glass    Test: dm_test_uclass
11465c70539SSimon Glass    Failures: 0
11565c70539SSimon Glass
11665c70539SSimon Glass(You can add '#define DEBUG' as suggested to check for memory leaks)
11765c70539SSimon Glass
11865c70539SSimon Glass
11965c70539SSimon GlassWhat is going on?
12065c70539SSimon Glass-----------------
12165c70539SSimon Glass
12265c70539SSimon GlassLet's start at the top. The demo command is in common/cmd_demo.c. It does
123*34e4a2ecSChris Packhamthe usual command processing and then:
12465c70539SSimon Glass
12554c5d08aSHeiko Schocher	struct udevice *demo_dev;
12665c70539SSimon Glass
12765c70539SSimon Glass	ret = uclass_get_device(UCLASS_DEMO, devnum, &demo_dev);
12865c70539SSimon Glass
12965c70539SSimon GlassUCLASS_DEMO means the class of devices which implement 'demo'. Other
13065c70539SSimon Glassclasses might be MMC, or GPIO, hashing or serial. The idea is that the
13165c70539SSimon Glassdevices in the class all share a particular way of working. The class
13265c70539SSimon Glasspresents a unified view of all these devices to U-Boot.
13365c70539SSimon Glass
13465c70539SSimon GlassThis function looks up a device for the demo uclass. Given a device
13565c70539SSimon Glassnumber we can find the device because all devices have registered with
13665c70539SSimon Glassthe UCLASS_DEMO uclass.
13765c70539SSimon Glass
13865c70539SSimon GlassThe device is automatically activated ready for use by uclass_get_device().
13965c70539SSimon Glass
14065c70539SSimon GlassNow that we have the device we can do things like:
14165c70539SSimon Glass
14265c70539SSimon Glass	return demo_hello(demo_dev, ch);
14365c70539SSimon Glass
14465c70539SSimon GlassThis function is in the demo uclass. It takes care of calling the 'hello'
14565c70539SSimon Glassmethod of the relevant driver. Bearing in mind that there are two drivers,
14665c70539SSimon Glassthis particular device may use one or other of them.
14765c70539SSimon Glass
14865c70539SSimon GlassThe code for demo_hello() is in drivers/demo/demo-uclass.c:
14965c70539SSimon Glass
15054c5d08aSHeiko Schocherint demo_hello(struct udevice *dev, int ch)
15165c70539SSimon Glass{
15265c70539SSimon Glass	const struct demo_ops *ops = device_get_ops(dev);
15365c70539SSimon Glass
15465c70539SSimon Glass	if (!ops->hello)
15565c70539SSimon Glass		return -ENOSYS;
15665c70539SSimon Glass
15765c70539SSimon Glass	return ops->hello(dev, ch);
15865c70539SSimon Glass}
15965c70539SSimon Glass
16065c70539SSimon GlassAs you can see it just calls the relevant driver method. One of these is
16165c70539SSimon Glassin drivers/demo/demo-simple.c:
16265c70539SSimon Glass
16354c5d08aSHeiko Schocherstatic int simple_hello(struct udevice *dev, int ch)
16465c70539SSimon Glass{
16565c70539SSimon Glass	const struct dm_demo_pdata *pdata = dev_get_platdata(dev);
16665c70539SSimon Glass
16765c70539SSimon Glass	printf("Hello from %08x: %s %d\n", map_to_sysmem(dev),
16865c70539SSimon Glass	       pdata->colour, pdata->sides);
16965c70539SSimon Glass
17065c70539SSimon Glass	return 0;
17165c70539SSimon Glass}
17265c70539SSimon Glass
17365c70539SSimon Glass
17465c70539SSimon GlassSo that is a trip from top (command execution) to bottom (driver action)
17565c70539SSimon Glassbut it leaves a lot of topics to address.
17665c70539SSimon Glass
17765c70539SSimon Glass
17865c70539SSimon GlassDeclaring Drivers
17965c70539SSimon Glass-----------------
18065c70539SSimon Glass
18165c70539SSimon GlassA driver declaration looks something like this (see
18265c70539SSimon Glassdrivers/demo/demo-shape.c):
18365c70539SSimon Glass
18465c70539SSimon Glassstatic const struct demo_ops shape_ops = {
18565c70539SSimon Glass	.hello = shape_hello,
18665c70539SSimon Glass	.status = shape_status,
18765c70539SSimon Glass};
18865c70539SSimon Glass
18965c70539SSimon GlassU_BOOT_DRIVER(demo_shape_drv) = {
19065c70539SSimon Glass	.name	= "demo_shape_drv",
19165c70539SSimon Glass	.id	= UCLASS_DEMO,
19265c70539SSimon Glass	.ops	= &shape_ops,
19365c70539SSimon Glass	.priv_data_size = sizeof(struct shape_data),
19465c70539SSimon Glass};
19565c70539SSimon Glass
19665c70539SSimon Glass
19765c70539SSimon GlassThis driver has two methods (hello and status) and requires a bit of
19865c70539SSimon Glassprivate data (accessible through dev_get_priv(dev) once the driver has
19965c70539SSimon Glassbeen probed). It is a member of UCLASS_DEMO so will register itself
20065c70539SSimon Glassthere.
20165c70539SSimon Glass
20265c70539SSimon GlassIn U_BOOT_DRIVER it is also possible to specify special methods for bind
20365c70539SSimon Glassand unbind, and these are called at appropriate times. For many drivers
20465c70539SSimon Glassit is hoped that only 'probe' and 'remove' will be needed.
20565c70539SSimon Glass
20665c70539SSimon GlassThe U_BOOT_DRIVER macro creates a data structure accessible from C,
20765c70539SSimon Glassso driver model can find the drivers that are available.
20865c70539SSimon Glass
20965c70539SSimon GlassThe methods a device can provide are documented in the device.h header.
21065c70539SSimon GlassBriefly, they are:
21165c70539SSimon Glass
21265c70539SSimon Glass    bind - make the driver model aware of a device (bind it to its driver)
21365c70539SSimon Glass    unbind - make the driver model forget the device
21465c70539SSimon Glass    ofdata_to_platdata - convert device tree data to platdata - see later
21565c70539SSimon Glass    probe - make a device ready for use
21665c70539SSimon Glass    remove - remove a device so it cannot be used until probed again
21765c70539SSimon Glass
21865c70539SSimon GlassThe sequence to get a device to work is bind, ofdata_to_platdata (if using
21965c70539SSimon Glassdevice tree) and probe.
22065c70539SSimon Glass
22165c70539SSimon Glass
22265c70539SSimon GlassPlatform Data
22365c70539SSimon Glass-------------
22465c70539SSimon Glass
22565c70539SSimon GlassWhere does the platform data come from? See demo-pdata.c which
22665c70539SSimon Glasssets up a table of driver names and their associated platform data.
22765c70539SSimon GlassThe data can be interpreted by the drivers however they like - it is
22865c70539SSimon Glassbasically a communication scheme between the board-specific code and
22965c70539SSimon Glassthe generic drivers, which are intended to work on any board.
23065c70539SSimon Glass
231*34e4a2ecSChris PackhamDrivers can access their data via dev->info->platdata. Here is
23265c70539SSimon Glassthe declaration for the platform data, which would normally appear
23365c70539SSimon Glassin the board file.
23465c70539SSimon Glass
23565c70539SSimon Glass	static const struct dm_demo_cdata red_square = {
23665c70539SSimon Glass		.colour = "red",
23765c70539SSimon Glass		.sides = 4.
23865c70539SSimon Glass	};
23965c70539SSimon Glass	static const struct driver_info info[] = {
24065c70539SSimon Glass		{
24165c70539SSimon Glass			.name = "demo_shape_drv",
24265c70539SSimon Glass			.platdata = &red_square,
24365c70539SSimon Glass		},
24465c70539SSimon Glass	};
24565c70539SSimon Glass
24665c70539SSimon Glass	demo1 = driver_bind(root, &info[0]);
24765c70539SSimon Glass
24865c70539SSimon Glass
24965c70539SSimon GlassDevice Tree
25065c70539SSimon Glass-----------
25165c70539SSimon Glass
25265c70539SSimon GlassWhile platdata is useful, a more flexible way of providing device data is
25365c70539SSimon Glassby using device tree. With device tree we replace the above code with the
25465c70539SSimon Glassfollowing device tree fragment:
25565c70539SSimon Glass
25665c70539SSimon Glass	red-square {
25765c70539SSimon Glass		compatible = "demo-shape";
25865c70539SSimon Glass		colour = "red";
25965c70539SSimon Glass		sides = <4>;
26065c70539SSimon Glass	};
26165c70539SSimon Glass
26265c70539SSimon Glass
26365c70539SSimon GlassThe easiest way to make this work it to add a few members to the driver:
26465c70539SSimon Glass
26565c70539SSimon Glass	.platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
26665c70539SSimon Glass	.ofdata_to_platdata = testfdt_ofdata_to_platdata,
26765c70539SSimon Glass	.probe	= testfdt_drv_probe,
26865c70539SSimon Glass
26965c70539SSimon GlassThe 'auto_alloc' feature allowed space for the platdata to be allocated
27065c70539SSimon Glassand zeroed before the driver's ofdata_to_platdata method is called. This
27165c70539SSimon Glassmethod reads the information out of the device tree and puts it in
27265c70539SSimon Glassdev->platdata. Then the probe method is called to set up the device.
27365c70539SSimon Glass
27465c70539SSimon GlassNote that both methods are optional. If you provide an ofdata_to_platdata
275*34e4a2ecSChris Packhammethod then it will be called first (after bind). If you provide a probe
27665c70539SSimon Glassmethod it will be called next.
27765c70539SSimon Glass
27865c70539SSimon GlassIf you don't want to have the platdata automatically allocated then you
27965c70539SSimon Glasscan leave out platdata_auto_alloc_size. In this case you can use malloc
28065c70539SSimon Glassin your ofdata_to_platdata (or probe) method to allocate the required memory,
28165c70539SSimon Glassand you should free it in the remove method.
28265c70539SSimon Glass
28365c70539SSimon Glass
28465c70539SSimon GlassDeclaring Uclasses
28565c70539SSimon Glass------------------
28665c70539SSimon Glass
28765c70539SSimon GlassThe demo uclass is declared like this:
28865c70539SSimon Glass
28965c70539SSimon GlassU_BOOT_CLASS(demo) = {
29065c70539SSimon Glass	.id		= UCLASS_DEMO,
29165c70539SSimon Glass};
29265c70539SSimon Glass
29365c70539SSimon GlassIt is also possible to specify special methods for probe, etc. The uclass
29465c70539SSimon Glassnumbering comes from include/dm/uclass.h. To add a new uclass, add to the
29565c70539SSimon Glassend of the enum there, then declare your uclass as above.
29665c70539SSimon Glass
29765c70539SSimon Glass
29865c70539SSimon GlassData Structures
29965c70539SSimon Glass---------------
30065c70539SSimon Glass
30165c70539SSimon GlassDriver model uses a doubly-linked list as the basic data structure. Some
30265c70539SSimon Glassnodes have several lists running through them. Creating a more efficient
30365c70539SSimon Glassdata structure might be worthwhile in some rare cases, once we understand
30465c70539SSimon Glasswhat the bottlenecks are.
30565c70539SSimon Glass
30665c70539SSimon Glass
30765c70539SSimon GlassChanges since v1
30865c70539SSimon Glass----------------
30965c70539SSimon Glass
31065c70539SSimon GlassFor the record, this implementation uses a very similar approach to the
31165c70539SSimon Glassoriginal patches, but makes at least the following changes:
31265c70539SSimon Glass
313*34e4a2ecSChris Packham- Tried to aggressively remove boilerplate, so that for most drivers there
31465c70539SSimon Glassis little or no 'driver model' code to write.
31565c70539SSimon Glass- Moved some data from code into data structure - e.g. store a pointer to
31665c70539SSimon Glassthe driver operations structure in the driver, rather than passing it
31765c70539SSimon Glassto the driver bind function.
31865c70539SSimon Glass- Rename some structures to make them more similar to Linux (struct device
31965c70539SSimon Glassinstead of struct instance, struct platdata, etc.)
32065c70539SSimon Glass- Change the name 'core' to 'uclass', meaning U-Boot class. It seems that
32165c70539SSimon Glassthis concept relates to a class of drivers (or a subsystem). We shouldn't
32265c70539SSimon Glassuse 'class' since it is a C++ reserved word, so U-Boot class (uclass) seems
32365c70539SSimon Glassbetter than 'core'.
32454c5d08aSHeiko Schocher- Remove 'struct driver_instance' and just use a single 'struct udevice'.
32565c70539SSimon GlassThis removes a level of indirection that doesn't seem necessary.
32665c70539SSimon Glass- Built in device tree support, to avoid the need for platdata
32765c70539SSimon Glass- Removed the concept of driver relocation, and just make it possible for
32865c70539SSimon Glassthe new driver (created after relocation) to access the old driver data.
32965c70539SSimon GlassI feel that relocation is a very special case and will only apply to a few
33065c70539SSimon Glassdrivers, many of which can/will just re-init anyway. So the overhead of
33165c70539SSimon Glassdealing with this might not be worth it.
33265c70539SSimon Glass- Implemented a GPIO system, trying to keep it simple
33365c70539SSimon Glass
33465c70539SSimon Glass
33565c70539SSimon GlassThings to punt for later
33665c70539SSimon Glass------------------------
33765c70539SSimon Glass
33865c70539SSimon Glass- SPL support - this will have to be present before many drivers can be
33965c70539SSimon Glassconverted, but it seems like we can add it once we are happy with the
34065c70539SSimon Glasscore implementation.
34165c70539SSimon Glass- Pre-relocation support - similar story
34265c70539SSimon Glass
34365c70539SSimon GlassThat is not to say that no thinking has gone into these - in fact there
34465c70539SSimon Glassis quite a lot there. However, getting these right is non-trivial and
34565c70539SSimon Glassthere is a high cost associated with going down the wrong path.
34665c70539SSimon Glass
34765c70539SSimon GlassFor SPL, it may be possible to fit in a simplified driver model with only
34865c70539SSimon Glassbind and probe methods, to reduce size.
34965c70539SSimon Glass
35065c70539SSimon GlassFor pre-relocation we can simply call the driver model init function. Then
35165c70539SSimon Glasspost relocation we throw that away and re-init driver model again. For drivers
35265c70539SSimon Glasswhich require some sort of continuity between pre- and post-relocation
35365c70539SSimon Glassdevices, we can provide access to the pre-relocation device pointers.
35465c70539SSimon Glass
35565c70539SSimon GlassUclasses are statically numbered at compile time. It would be possible to
35665c70539SSimon Glasschange this to dynamic numbering, but then we would require some sort of
35765c70539SSimon Glasslookup service, perhaps searching by name. This is slightly less efficient
35865c70539SSimon Glassso has been left out for now. One small advantage of dynamic numbering might
35965c70539SSimon Glassbe fewer merge conflicts in uclass-id.h.
36065c70539SSimon Glass
36165c70539SSimon Glass
36265c70539SSimon GlassSimon Glass
36365c70539SSimon Glasssjg@chromium.org
36465c70539SSimon GlassApril 2013
36565c70539SSimon GlassUpdated 7-May-13
36665c70539SSimon GlassUpdated 14-Jun-13
36765c70539SSimon GlassUpdated 18-Oct-13
36865c70539SSimon GlassUpdated 5-Nov-13
369