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 2334e4a2ecSChris 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 3933fcd1bbSMasahiro Yamada make sandbox_defconfig 4065c70539SSimon Glass make 4133fcd1bbSMasahiro Yamada ./u-boot -d u-boot.dtb 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 93e57f9c8eSJagan Teki ./test/py/test.py --bd sandbox --build -k ut_dm -v 9465c70539SSimon Glass 9565c70539SSimon GlassYou should see something like this: 9665c70539SSimon Glass 97e57f9c8eSJagan Teki(venv)$ ./test/py/test.py --bd sandbox --build -k ut_dm -v 98e57f9c8eSJagan Teki+make O=/root/u-boot/build-sandbox -s sandbox_defconfig 99e57f9c8eSJagan Teki+make O=/root/u-boot/build-sandbox -s -j8 100e57f9c8eSJagan Teki============================= test session starts ============================== 101e57f9c8eSJagan Tekiplatform linux2 -- Python 2.7.5, pytest-2.9.0, py-1.4.31, pluggy-0.3.1 -- /root/u-boot/venv/bin/python 102e57f9c8eSJagan Tekicachedir: .cache 103e57f9c8eSJagan Tekirootdir: /root/u-boot, inifile: 104e57f9c8eSJagan Tekicollected 199 items 10598a16053SSimon Glass 106e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut_dm_init PASSED 107e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_adc_bind] PASSED 108e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_adc_multi_channel_conversion] PASSED 109e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_adc_multi_channel_shot] PASSED 110e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_adc_single_channel_conversion] PASSED 111e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_adc_single_channel_shot] PASSED 112e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_adc_supply] PASSED 113e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_adc_wrong_channel_selection] PASSED 114e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_autobind] PASSED 115e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_autobind_uclass_pdata_alloc] PASSED 116e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_autobind_uclass_pdata_valid] PASSED 117e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_autoprobe] PASSED 118e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_bus_child_post_bind] PASSED 119e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_bus_child_post_bind_uclass] PASSED 120e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_bus_child_pre_probe_uclass] PASSED 121e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_bus_children] PASSED 122e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_bus_children_funcs] PASSED 123e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_bus_children_iterators] PASSED 124e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_bus_parent_data] PASSED 125e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_bus_parent_data_uclass] PASSED 126e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_bus_parent_ops] PASSED 127e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_bus_parent_platdata] PASSED 128e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_bus_parent_platdata_uclass] PASSED 129e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_children] PASSED 130e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_clk_base] PASSED 131e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_clk_periph] PASSED 132e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_device_get_uclass_id] PASSED 133e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_eth] PASSED 134e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_eth_act] PASSED 135e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_eth_alias] PASSED 136e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_eth_prime] PASSED 137e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_eth_rotate] PASSED 138e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_fdt] PASSED 139e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_fdt_offset] PASSED 140e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_fdt_pre_reloc] PASSED 141e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_fdt_uclass_seq] PASSED 142e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_gpio] PASSED 143e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_gpio_anon] PASSED 144e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_gpio_copy] PASSED 145e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_gpio_leak] PASSED 146e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_gpio_phandles] PASSED 147e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_gpio_requestf] PASSED 148e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_i2c_bytewise] PASSED 149e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_i2c_find] PASSED 150e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_i2c_offset] PASSED 151e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_i2c_offset_len] PASSED 152e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_i2c_probe_empty] PASSED 153e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_i2c_read_write] PASSED 154e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_i2c_speed] PASSED 155e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_leak] PASSED 156e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_led_base] PASSED 157e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_led_gpio] PASSED 158e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_led_label] PASSED 159e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_lifecycle] PASSED 160e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_mmc_base] PASSED 161e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_net_retry] PASSED 162e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_operations] PASSED 163e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_ordering] PASSED 164e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_pci_base] PASSED 165e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_pci_busnum] PASSED 166e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_pci_swapcase] PASSED 167e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_platdata] PASSED 168e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_power_pmic_get] PASSED 169e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_power_pmic_io] PASSED 170e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_power_regulator_autoset] PASSED 171e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_power_regulator_autoset_list] PASSED 172e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_power_regulator_get] PASSED 173e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_power_regulator_set_get_current] PASSED 174e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_power_regulator_set_get_enable] PASSED 175e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_power_regulator_set_get_mode] PASSED 176e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_power_regulator_set_get_voltage] PASSED 177e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_pre_reloc] PASSED 178e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_ram_base] PASSED 179e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_regmap_base] PASSED 180e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_regmap_syscon] PASSED 181e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_remoteproc_base] PASSED 182e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_remove] PASSED 183e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_reset_base] PASSED 184e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_reset_walk] PASSED 185e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_rtc_base] PASSED 186e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_rtc_dual] PASSED 187e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_rtc_reset] PASSED 188e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_rtc_set_get] PASSED 189e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_spi_find] PASSED 190e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_spi_flash] PASSED 191e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_spi_xfer] PASSED 192e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_syscon_base] PASSED 193e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_syscon_by_driver_data] PASSED 194e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_timer_base] PASSED 195e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_uclass] PASSED 196e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_uclass_before_ready] PASSED 197e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_uclass_devices_find] PASSED 198e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_uclass_devices_find_by_name] PASSED 199e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_uclass_devices_get] PASSED 200e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_uclass_devices_get_by_name] PASSED 201e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_usb_base] PASSED 202e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_usb_flash] PASSED 203e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_usb_keyb] PASSED 204e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_usb_multi] PASSED 205e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_usb_remove] PASSED 206e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_usb_tree] PASSED 207e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_usb_tree_remove] PASSED 208e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_usb_tree_reorder] PASSED 209e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_video_base] PASSED 210e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_video_bmp] PASSED 211e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_video_bmp_comp] PASSED 212e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_video_chars] PASSED 213e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_video_context] PASSED 214e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_video_rotation1] PASSED 215e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_video_rotation2] PASSED 216e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_video_rotation3] PASSED 217e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_video_text] PASSED 218e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_video_truetype] PASSED 219e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_video_truetype_bs] PASSED 220e57f9c8eSJagan Tekitest/py/tests/test_ut.py::test_ut[ut_dm_video_truetype_scroll] PASSED 22198a16053SSimon Glass 222e57f9c8eSJagan Teki======================= 84 tests deselected by '-kut_dm' ======================= 223e57f9c8eSJagan Teki================== 115 passed, 84 deselected in 3.77 seconds =================== 22465c70539SSimon Glass 22565c70539SSimon GlassWhat is going on? 22665c70539SSimon Glass----------------- 22765c70539SSimon Glass 22865c70539SSimon GlassLet's start at the top. The demo command is in common/cmd_demo.c. It does 22934e4a2ecSChris Packhamthe usual command processing and then: 23065c70539SSimon Glass 23154c5d08aSHeiko Schocher struct udevice *demo_dev; 23265c70539SSimon Glass 23365c70539SSimon Glass ret = uclass_get_device(UCLASS_DEMO, devnum, &demo_dev); 23465c70539SSimon Glass 23565c70539SSimon GlassUCLASS_DEMO means the class of devices which implement 'demo'. Other 23665c70539SSimon Glassclasses might be MMC, or GPIO, hashing or serial. The idea is that the 23765c70539SSimon Glassdevices in the class all share a particular way of working. The class 23865c70539SSimon Glasspresents a unified view of all these devices to U-Boot. 23965c70539SSimon Glass 24065c70539SSimon GlassThis function looks up a device for the demo uclass. Given a device 24165c70539SSimon Glassnumber we can find the device because all devices have registered with 24265c70539SSimon Glassthe UCLASS_DEMO uclass. 24365c70539SSimon Glass 24465c70539SSimon GlassThe device is automatically activated ready for use by uclass_get_device(). 24565c70539SSimon Glass 24665c70539SSimon GlassNow that we have the device we can do things like: 24765c70539SSimon Glass 24865c70539SSimon Glass return demo_hello(demo_dev, ch); 24965c70539SSimon Glass 25065c70539SSimon GlassThis function is in the demo uclass. It takes care of calling the 'hello' 25165c70539SSimon Glassmethod of the relevant driver. Bearing in mind that there are two drivers, 25265c70539SSimon Glassthis particular device may use one or other of them. 25365c70539SSimon Glass 25465c70539SSimon GlassThe code for demo_hello() is in drivers/demo/demo-uclass.c: 25565c70539SSimon Glass 25654c5d08aSHeiko Schocherint demo_hello(struct udevice *dev, int ch) 25765c70539SSimon Glass{ 25865c70539SSimon Glass const struct demo_ops *ops = device_get_ops(dev); 25965c70539SSimon Glass 26065c70539SSimon Glass if (!ops->hello) 26165c70539SSimon Glass return -ENOSYS; 26265c70539SSimon Glass 26365c70539SSimon Glass return ops->hello(dev, ch); 26465c70539SSimon Glass} 26565c70539SSimon Glass 26665c70539SSimon GlassAs you can see it just calls the relevant driver method. One of these is 26765c70539SSimon Glassin drivers/demo/demo-simple.c: 26865c70539SSimon Glass 26954c5d08aSHeiko Schocherstatic int simple_hello(struct udevice *dev, int ch) 27065c70539SSimon Glass{ 27165c70539SSimon Glass const struct dm_demo_pdata *pdata = dev_get_platdata(dev); 27265c70539SSimon Glass 27365c70539SSimon Glass printf("Hello from %08x: %s %d\n", map_to_sysmem(dev), 27465c70539SSimon Glass pdata->colour, pdata->sides); 27565c70539SSimon Glass 27665c70539SSimon Glass return 0; 27765c70539SSimon Glass} 27865c70539SSimon Glass 27965c70539SSimon Glass 28065c70539SSimon GlassSo that is a trip from top (command execution) to bottom (driver action) 28165c70539SSimon Glassbut it leaves a lot of topics to address. 28265c70539SSimon Glass 28365c70539SSimon Glass 28465c70539SSimon GlassDeclaring Drivers 28565c70539SSimon Glass----------------- 28665c70539SSimon Glass 28765c70539SSimon GlassA driver declaration looks something like this (see 28865c70539SSimon Glassdrivers/demo/demo-shape.c): 28965c70539SSimon Glass 29065c70539SSimon Glassstatic const struct demo_ops shape_ops = { 29165c70539SSimon Glass .hello = shape_hello, 29265c70539SSimon Glass .status = shape_status, 29365c70539SSimon Glass}; 29465c70539SSimon Glass 29565c70539SSimon GlassU_BOOT_DRIVER(demo_shape_drv) = { 29665c70539SSimon Glass .name = "demo_shape_drv", 29765c70539SSimon Glass .id = UCLASS_DEMO, 29865c70539SSimon Glass .ops = &shape_ops, 29965c70539SSimon Glass .priv_data_size = sizeof(struct shape_data), 30065c70539SSimon Glass}; 30165c70539SSimon Glass 30265c70539SSimon Glass 30365c70539SSimon GlassThis driver has two methods (hello and status) and requires a bit of 30465c70539SSimon Glassprivate data (accessible through dev_get_priv(dev) once the driver has 30565c70539SSimon Glassbeen probed). It is a member of UCLASS_DEMO so will register itself 30665c70539SSimon Glassthere. 30765c70539SSimon Glass 30865c70539SSimon GlassIn U_BOOT_DRIVER it is also possible to specify special methods for bind 30965c70539SSimon Glassand unbind, and these are called at appropriate times. For many drivers 31065c70539SSimon Glassit is hoped that only 'probe' and 'remove' will be needed. 31165c70539SSimon Glass 31265c70539SSimon GlassThe U_BOOT_DRIVER macro creates a data structure accessible from C, 31365c70539SSimon Glassso driver model can find the drivers that are available. 31465c70539SSimon Glass 31565c70539SSimon GlassThe methods a device can provide are documented in the device.h header. 31665c70539SSimon GlassBriefly, they are: 31765c70539SSimon Glass 31865c70539SSimon Glass bind - make the driver model aware of a device (bind it to its driver) 31965c70539SSimon Glass unbind - make the driver model forget the device 32065c70539SSimon Glass ofdata_to_platdata - convert device tree data to platdata - see later 32165c70539SSimon Glass probe - make a device ready for use 32265c70539SSimon Glass remove - remove a device so it cannot be used until probed again 32365c70539SSimon Glass 32465c70539SSimon GlassThe sequence to get a device to work is bind, ofdata_to_platdata (if using 32565c70539SSimon Glassdevice tree) and probe. 32665c70539SSimon Glass 32765c70539SSimon Glass 32865c70539SSimon GlassPlatform Data 32965c70539SSimon Glass------------- 33065c70539SSimon Glass 33197f3ee34SSimon Glass*** Note: platform data is the old way of doing things. It is 33297f3ee34SSimon Glass*** basically a C structure which is passed to drivers to tell them about 33397f3ee34SSimon Glass*** platform-specific settings like the address of its registers, bus 33497f3ee34SSimon Glass*** speed, etc. Device tree is now the preferred way of handling this. 33597f3ee34SSimon Glass*** Unless you have a good reason not to use device tree (the main one 33697f3ee34SSimon Glass*** being you need serial support in SPL and don't have enough SRAM for 33797f3ee34SSimon Glass*** the cut-down device tree and libfdt libraries) you should stay away 33897f3ee34SSimon Glass*** from platform data. 33997f3ee34SSimon Glass 34022ec1363SSimon GlassPlatform data is like Linux platform data, if you are familiar with that. 34122ec1363SSimon GlassIt provides the board-specific information to start up a device. 34222ec1363SSimon Glass 34322ec1363SSimon GlassWhy is this information not just stored in the device driver itself? The 34422ec1363SSimon Glassidea is that the device driver is generic, and can in principle operate on 34522ec1363SSimon Glassany board that has that type of device. For example, with modern 34622ec1363SSimon Glasshighly-complex SoCs it is common for the IP to come from an IP vendor, and 34722ec1363SSimon Glasstherefore (for example) the MMC controller may be the same on chips from 34822ec1363SSimon Glassdifferent vendors. It makes no sense to write independent drivers for the 34922ec1363SSimon GlassMMC controller on each vendor's SoC, when they are all almost the same. 35022ec1363SSimon GlassSimilarly, we may have 6 UARTs in an SoC, all of which are mostly the same, 35122ec1363SSimon Glassbut lie at different addresses in the address space. 35222ec1363SSimon Glass 35322ec1363SSimon GlassUsing the UART example, we have a single driver and it is instantiated 6 35422ec1363SSimon Glasstimes by supplying 6 lots of platform data. Each lot of platform data 35522ec1363SSimon Glassgives the driver name and a pointer to a structure containing information 35622ec1363SSimon Glassabout this instance - e.g. the address of the register space. It may be that 35722ec1363SSimon Glassone of the UARTS supports RS-485 operation - this can be added as a flag in 35822ec1363SSimon Glassthe platform data, which is set for this one port and clear for the rest. 35922ec1363SSimon Glass 36022ec1363SSimon GlassThink of your driver as a generic piece of code which knows how to talk to 36122ec1363SSimon Glassa device, but needs to know where it is, any variant/option information and 36222ec1363SSimon Glassso on. Platform data provides this link between the generic piece of code 36322ec1363SSimon Glassand the specific way it is bound on a particular board. 36422ec1363SSimon Glass 36522ec1363SSimon GlassExamples of platform data include: 36622ec1363SSimon Glass 36722ec1363SSimon Glass - The base address of the IP block's register space 36822ec1363SSimon Glass - Configuration options, like: 36922ec1363SSimon Glass - the SPI polarity and maximum speed for a SPI controller 37022ec1363SSimon Glass - the I2C speed to use for an I2C device 37122ec1363SSimon Glass - the number of GPIOs available in a GPIO device 37222ec1363SSimon Glass 37322ec1363SSimon GlassWhere does the platform data come from? It is either held in a structure 37422ec1363SSimon Glasswhich is compiled into U-Boot, or it can be parsed from the Device Tree 37522ec1363SSimon Glass(see 'Device Tree' below). 37622ec1363SSimon Glass 37722ec1363SSimon GlassFor an example of how it can be compiled in, see demo-pdata.c which 37865c70539SSimon Glasssets up a table of driver names and their associated platform data. 37965c70539SSimon GlassThe data can be interpreted by the drivers however they like - it is 38065c70539SSimon Glassbasically a communication scheme between the board-specific code and 38165c70539SSimon Glassthe generic drivers, which are intended to work on any board. 38265c70539SSimon Glass 38334e4a2ecSChris PackhamDrivers can access their data via dev->info->platdata. Here is 38465c70539SSimon Glassthe declaration for the platform data, which would normally appear 38565c70539SSimon Glassin the board file. 38665c70539SSimon Glass 38765c70539SSimon Glass static const struct dm_demo_cdata red_square = { 38865c70539SSimon Glass .colour = "red", 38965c70539SSimon Glass .sides = 4. 39065c70539SSimon Glass }; 39165c70539SSimon Glass static const struct driver_info info[] = { 39265c70539SSimon Glass { 39365c70539SSimon Glass .name = "demo_shape_drv", 39465c70539SSimon Glass .platdata = &red_square, 39565c70539SSimon Glass }, 39665c70539SSimon Glass }; 39765c70539SSimon Glass 39865c70539SSimon Glass demo1 = driver_bind(root, &info[0]); 39965c70539SSimon Glass 40065c70539SSimon Glass 40165c70539SSimon GlassDevice Tree 40265c70539SSimon Glass----------- 40365c70539SSimon Glass 40465c70539SSimon GlassWhile platdata is useful, a more flexible way of providing device data is 40597f3ee34SSimon Glassby using device tree. In U-Boot you should use this where possible. Avoid 40697f3ee34SSimon Glasssending patches which make use of the U_BOOT_DEVICE() macro unless strictly 40797f3ee34SSimon Glassnecessary. 40897f3ee34SSimon Glass 40997f3ee34SSimon GlassWith device tree we replace the above code with the following device tree 41097f3ee34SSimon Glassfragment: 41165c70539SSimon Glass 41265c70539SSimon Glass red-square { 41365c70539SSimon Glass compatible = "demo-shape"; 41465c70539SSimon Glass colour = "red"; 41565c70539SSimon Glass sides = <4>; 41665c70539SSimon Glass }; 41765c70539SSimon Glass 41822ec1363SSimon GlassThis means that instead of having lots of U_BOOT_DEVICE() declarations in 41922ec1363SSimon Glassthe board file, we put these in the device tree. This approach allows a lot 42022ec1363SSimon Glassmore generality, since the same board file can support many types of boards 42122ec1363SSimon Glass(e,g. with the same SoC) just by using different device trees. An added 42222ec1363SSimon Glassbenefit is that the Linux device tree can be used, thus further simplifying 42322ec1363SSimon Glassthe task of board-bring up either for U-Boot or Linux devs (whoever gets to 42422ec1363SSimon Glassthe board first!). 42565c70539SSimon Glass 42665c70539SSimon GlassThe easiest way to make this work it to add a few members to the driver: 42765c70539SSimon Glass 42865c70539SSimon Glass .platdata_auto_alloc_size = sizeof(struct dm_test_pdata), 42965c70539SSimon Glass .ofdata_to_platdata = testfdt_ofdata_to_platdata, 43065c70539SSimon Glass 43165c70539SSimon GlassThe 'auto_alloc' feature allowed space for the platdata to be allocated 43222ec1363SSimon Glassand zeroed before the driver's ofdata_to_platdata() method is called. The 43322ec1363SSimon Glassofdata_to_platdata() method, which the driver write supplies, should parse 43422ec1363SSimon Glassthe device tree node for this device and place it in dev->platdata. Thus 43522ec1363SSimon Glasswhen the probe method is called later (to set up the device ready for use) 43622ec1363SSimon Glassthe platform data will be present. 43765c70539SSimon Glass 43865c70539SSimon GlassNote that both methods are optional. If you provide an ofdata_to_platdata 43922ec1363SSimon Glassmethod then it will be called first (during activation). If you provide a 44022ec1363SSimon Glassprobe method it will be called next. See Driver Lifecycle below for more 44122ec1363SSimon Glassdetails. 44265c70539SSimon Glass 44365c70539SSimon GlassIf you don't want to have the platdata automatically allocated then you 44465c70539SSimon Glasscan leave out platdata_auto_alloc_size. In this case you can use malloc 44565c70539SSimon Glassin your ofdata_to_platdata (or probe) method to allocate the required memory, 44665c70539SSimon Glassand you should free it in the remove method. 44765c70539SSimon Glass 4482f3b95dbSSimon GlassThe driver model tree is intended to mirror that of the device tree. The 4492f3b95dbSSimon Glassroot driver is at device tree offset 0 (the root node, '/'), and its 4502f3b95dbSSimon Glasschildren are the children of the root node. 4512f3b95dbSSimon Glass 45265c70539SSimon Glass 45365c70539SSimon GlassDeclaring Uclasses 45465c70539SSimon Glass------------------ 45565c70539SSimon Glass 45665c70539SSimon GlassThe demo uclass is declared like this: 45765c70539SSimon Glass 45865c70539SSimon GlassU_BOOT_CLASS(demo) = { 45965c70539SSimon Glass .id = UCLASS_DEMO, 46065c70539SSimon Glass}; 46165c70539SSimon Glass 46265c70539SSimon GlassIt is also possible to specify special methods for probe, etc. The uclass 46365c70539SSimon Glassnumbering comes from include/dm/uclass.h. To add a new uclass, add to the 46465c70539SSimon Glassend of the enum there, then declare your uclass as above. 46565c70539SSimon Glass 46665c70539SSimon Glass 4675a66a8ffSSimon GlassDevice Sequence Numbers 4685a66a8ffSSimon Glass----------------------- 4695a66a8ffSSimon Glass 4705a66a8ffSSimon GlassU-Boot numbers devices from 0 in many situations, such as in the command 4715a66a8ffSSimon Glassline for I2C and SPI buses, and the device names for serial ports (serial0, 4725a66a8ffSSimon Glassserial1, ...). Driver model supports this numbering and permits devices 4739cc36a2bSSimon Glassto be locating by their 'sequence'. This numbering uniquely identifies a 474547cea19SSimon Glassdevice in its uclass, so no two devices within a particular uclass can have 475547cea19SSimon Glassthe same sequence number. 4765a66a8ffSSimon Glass 4775a66a8ffSSimon GlassSequence numbers start from 0 but gaps are permitted. For example, a board 4789cc36a2bSSimon Glassmay have I2C buses 1, 4, 5 but no 0, 2 or 3. The choice of how devices are 4795a66a8ffSSimon Glassnumbered is up to a particular board, and may be set by the SoC in some 4805a66a8ffSSimon Glasscases. While it might be tempting to automatically renumber the devices 4815a66a8ffSSimon Glasswhere there are gaps in the sequence, this can lead to confusion and is 4825a66a8ffSSimon Glassnot the way that U-Boot works. 4835a66a8ffSSimon Glass 4845a66a8ffSSimon GlassEach device can request a sequence number. If none is required then the 4855a66a8ffSSimon Glassdevice will be automatically allocated the next available sequence number. 4865a66a8ffSSimon Glass 4875a66a8ffSSimon GlassTo specify the sequence number in the device tree an alias is typically 4889cc36a2bSSimon Glassused. Make sure that the uclass has the DM_UC_FLAG_SEQ_ALIAS flag set. 4895a66a8ffSSimon Glass 4905a66a8ffSSimon Glassaliases { 4915a66a8ffSSimon Glass serial2 = "/serial@22230000"; 4925a66a8ffSSimon Glass}; 4935a66a8ffSSimon Glass 4945a66a8ffSSimon GlassThis indicates that in the uclass called "serial", the named node 4955a66a8ffSSimon Glass("/serial@22230000") will be given sequence number 2. Any command or driver 4965a66a8ffSSimon Glasswhich requests serial device 2 will obtain this device. 4975a66a8ffSSimon Glass 4989cc36a2bSSimon GlassMore commonly you can use node references, which expand to the full path: 4995a66a8ffSSimon Glass 5005a66a8ffSSimon Glassaliases { 5019cc36a2bSSimon Glass serial2 = &serial_2; 5025a66a8ffSSimon Glass}; 5035a66a8ffSSimon Glass... 5049cc36a2bSSimon Glassserial_2: serial@22230000 { 5059cc36a2bSSimon Glass... 5065a66a8ffSSimon Glass}; 5075a66a8ffSSimon Glass 5089cc36a2bSSimon GlassThe alias resolves to the same string in this case, but this version is 5099cc36a2bSSimon Glasseasier to read. 5105a66a8ffSSimon Glass 5115a66a8ffSSimon GlassDevice sequence numbers are resolved when a device is probed. Before then 5125a66a8ffSSimon Glassthe sequence number is only a request which may or may not be honoured, 5135a66a8ffSSimon Glassdepending on what other devices have been probed. However the numbering is 5145a66a8ffSSimon Glassentirely under the control of the board author so a conflict is generally 5155a66a8ffSSimon Glassan error. 5165a66a8ffSSimon Glass 5175a66a8ffSSimon Glass 518a327dee0SSimon GlassBus Drivers 519a327dee0SSimon Glass----------- 520a327dee0SSimon Glass 521a327dee0SSimon GlassA common use of driver model is to implement a bus, a device which provides 522a327dee0SSimon Glassaccess to other devices. Example of buses include SPI and I2C. Typically 523a327dee0SSimon Glassthe bus provides some sort of transport or translation that makes it 524a327dee0SSimon Glasspossible to talk to the devices on the bus. 525a327dee0SSimon Glass 5262017aaefSSimon GlassDriver model provides some useful features to help with implementing buses. 5272017aaefSSimon GlassFirstly, a bus can request that its children store some 'parent data' which 5282017aaefSSimon Glasscan be used to keep track of child state. Secondly, the bus can define 5292017aaefSSimon Glassmethods which are called when a child is probed or removed. This is similar 5302017aaefSSimon Glassto the methods the uclass driver provides. Thirdly, per-child platform data 5312017aaefSSimon Glasscan be provided to specify things like the child's address on the bus. This 5322017aaefSSimon Glasspersists across child probe()/remove() cycles. 5332017aaefSSimon Glass 5342017aaefSSimon GlassFor consistency and ease of implementation, the bus uclass can specify the 5352017aaefSSimon Glassper-child platform data, so that it can be the same for all children of buses 5362017aaefSSimon Glassin that uclass. There are also uclass methods which can be called when 5372017aaefSSimon Glasschildren are bound and probed. 538a327dee0SSimon Glass 539a327dee0SSimon GlassHere an explanation of how a bus fits with a uclass may be useful. Consider 540a327dee0SSimon Glassa USB bus with several devices attached to it, each from a different (made 541a327dee0SSimon Glassup) uclass: 542a327dee0SSimon Glass 543a327dee0SSimon Glass xhci_usb (UCLASS_USB) 544a327dee0SSimon Glass eth (UCLASS_ETHERNET) 545a327dee0SSimon Glass camera (UCLASS_CAMERA) 546a327dee0SSimon Glass flash (UCLASS_FLASH_STORAGE) 547a327dee0SSimon Glass 548a327dee0SSimon GlassEach of the devices is connected to a different address on the USB bus. 549a327dee0SSimon GlassThe bus device wants to store this address and some other information such 550a327dee0SSimon Glassas the bus speed for each device. 551a327dee0SSimon Glass 5522017aaefSSimon GlassTo achieve this, the bus device can use dev->parent_platdata in each of its 5532017aaefSSimon Glassthree children. This can be auto-allocated if the bus driver (or bus uclass) 5542017aaefSSimon Glasshas a non-zero value for per_child_platdata_auto_alloc_size. If not, then 5552017aaefSSimon Glassthe bus device or uclass can allocate the space itself before the child 5562017aaefSSimon Glassdevice is probed. 557a327dee0SSimon Glass 558a327dee0SSimon GlassAlso the bus driver can define the child_pre_probe() and child_post_remove() 559a327dee0SSimon Glassmethods to allow it to do some processing before the child is activated or 560a327dee0SSimon Glassafter it is deactivated. 561a327dee0SSimon Glass 5622017aaefSSimon GlassSimilarly the bus uclass can define the child_post_bind() method to obtain 5632017aaefSSimon Glassthe per-child platform data from the device tree and set it up for the child. 5642017aaefSSimon GlassThe bus uclass can also provide a child_pre_probe() method. Very often it is 5652017aaefSSimon Glassthe bus uclass that controls these features, since it avoids each driver 5662017aaefSSimon Glasshaving to do the same processing. Of course the driver can still tweak and 5672017aaefSSimon Glassoverride these activities. 5682017aaefSSimon Glass 569a327dee0SSimon GlassNote that the information that controls this behaviour is in the bus's 570a327dee0SSimon Glassdriver, not the child's. In fact it is possible that child has no knowledge 571a327dee0SSimon Glassthat it is connected to a bus. The same child device may even be used on two 572a327dee0SSimon Glassdifferent bus types. As an example. the 'flash' device shown above may also 573a327dee0SSimon Glassbe connected on a SATA bus or standalone with no bus: 574a327dee0SSimon Glass 575a327dee0SSimon Glass xhci_usb (UCLASS_USB) 576a327dee0SSimon Glass flash (UCLASS_FLASH_STORAGE) - parent data/methods defined by USB bus 577a327dee0SSimon Glass 578a327dee0SSimon Glass sata (UCLASS_SATA) 579a327dee0SSimon Glass flash (UCLASS_FLASH_STORAGE) - parent data/methods defined by SATA bus 580a327dee0SSimon Glass 581a327dee0SSimon Glass flash (UCLASS_FLASH_STORAGE) - no parent data/methods (not on a bus) 582a327dee0SSimon Glass 583a327dee0SSimon GlassAbove you can see that the driver for xhci_usb/sata controls the child's 584a327dee0SSimon Glassbus methods. In the third example the device is not on a bus, and therefore 585a327dee0SSimon Glasswill not have these methods at all. Consider the case where the flash 586a327dee0SSimon Glassdevice defines child methods. These would be used for *its* children, and 587a327dee0SSimon Glasswould be quite separate from the methods defined by the driver for the bus 588a327dee0SSimon Glassthat the flash device is connetced to. The act of attaching a device to a 589a327dee0SSimon Glassparent device which is a bus, causes the device to start behaving like a 590a327dee0SSimon Glassbus device, regardless of its own views on the matter. 591a327dee0SSimon Glass 592a327dee0SSimon GlassThe uclass for the device can also contain data private to that uclass. 593a327dee0SSimon GlassBut note that each device on the bus may be a memeber of a different 594a327dee0SSimon Glassuclass, and this data has nothing to do with the child data for each child 5952017aaefSSimon Glasson the bus. It is the bus' uclass that controls the child with respect to 5962017aaefSSimon Glassthe bus. 597a327dee0SSimon Glass 598a327dee0SSimon Glass 59922ec1363SSimon GlassDriver Lifecycle 60022ec1363SSimon Glass---------------- 60122ec1363SSimon Glass 60222ec1363SSimon GlassHere are the stages that a device goes through in driver model. Note that all 60322ec1363SSimon Glassmethods mentioned here are optional - e.g. if there is no probe() method for 60422ec1363SSimon Glassa device then it will not be called. A simple device may have very few 60522ec1363SSimon Glassmethods actually defined. 60622ec1363SSimon Glass 60722ec1363SSimon Glass1. Bind stage 60822ec1363SSimon Glass 609daac3bfeSStephen WarrenU-Boot discovers devices using one of these two methods: 61022ec1363SSimon Glass 611daac3bfeSStephen Warren - Scan the U_BOOT_DEVICE() definitions. U-Boot looks up the name specified 612daac3bfeSStephen Warrenby each, to find the appropriate U_BOOT_DRIVER() definition. In this case, 613daac3bfeSStephen Warrenthere is no path by which driver_data may be provided, but the U_BOOT_DEVICE() 614daac3bfeSStephen Warrenmay provide platdata. 61522ec1363SSimon Glass 61622ec1363SSimon Glass - Scan through the device tree definitions. U-Boot looks at top-level 61722ec1363SSimon Glassnodes in the the device tree. It looks at the compatible string in each node 618daac3bfeSStephen Warrenand uses the of_match table of the U_BOOT_DRIVER() structure to find the 619daac3bfeSStephen Warrenright driver for each node. In this case, the of_match table may provide a 620daac3bfeSStephen Warrendriver_data value, but platdata cannot be provided until later. 621daac3bfeSStephen Warren 622daac3bfeSStephen WarrenFor each device that is discovered, U-Boot then calls device_bind() to create a 623daac3bfeSStephen Warrennew device, initializes various core fields of the device object such as name, 624daac3bfeSStephen Warrenuclass & driver, initializes any optional fields of the device object that are 625daac3bfeSStephen Warrenapplicable such as of_offset, driver_data & platdata, and finally calls the 626daac3bfeSStephen Warrendriver's bind() method if one is defined. 62722ec1363SSimon Glass 62822ec1363SSimon GlassAt this point all the devices are known, and bound to their drivers. There 62922ec1363SSimon Glassis a 'struct udevice' allocated for all devices. However, nothing has been 63022ec1363SSimon Glassactivated (except for the root device). Each bound device that was created 63122ec1363SSimon Glassfrom a U_BOOT_DEVICE() declaration will hold the platdata pointer specified 63222ec1363SSimon Glassin that declaration. For a bound device created from the device tree, 63322ec1363SSimon Glassplatdata will be NULL, but of_offset will be the offset of the device tree 63422ec1363SSimon Glassnode that caused the device to be created. The uclass is set correctly for 63522ec1363SSimon Glassthe device. 63622ec1363SSimon Glass 63722ec1363SSimon GlassThe device's bind() method is permitted to perform simple actions, but 63822ec1363SSimon Glassshould not scan the device tree node, not initialise hardware, nor set up 63922ec1363SSimon Glassstructures or allocate memory. All of these tasks should be left for 64022ec1363SSimon Glassthe probe() method. 64122ec1363SSimon Glass 64222ec1363SSimon GlassNote that compared to Linux, U-Boot's driver model has a separate step of 64322ec1363SSimon Glassprobe/remove which is independent of bind/unbind. This is partly because in 64422ec1363SSimon GlassU-Boot it may be expensive to probe devices and we don't want to do it until 64522ec1363SSimon Glassthey are needed, or perhaps until after relocation. 64622ec1363SSimon Glass 64722ec1363SSimon Glass2. Activation/probe 64822ec1363SSimon Glass 64922ec1363SSimon GlassWhen a device needs to be used, U-Boot activates it, by following these 65022ec1363SSimon Glasssteps (see device_probe()): 65122ec1363SSimon Glass 65222ec1363SSimon Glass a. If priv_auto_alloc_size is non-zero, then the device-private space 65322ec1363SSimon Glass is allocated for the device and zeroed. It will be accessible as 65422ec1363SSimon Glass dev->priv. The driver can put anything it likes in there, but should use 65522ec1363SSimon Glass it for run-time information, not platform data (which should be static 65622ec1363SSimon Glass and known before the device is probed). 65722ec1363SSimon Glass 65822ec1363SSimon Glass b. If platdata_auto_alloc_size is non-zero, then the platform data space 65922ec1363SSimon Glass is allocated. This is only useful for device tree operation, since 66022ec1363SSimon Glass otherwise you would have to specific the platform data in the 66122ec1363SSimon Glass U_BOOT_DEVICE() declaration. The space is allocated for the device and 66222ec1363SSimon Glass zeroed. It will be accessible as dev->platdata. 66322ec1363SSimon Glass 66422ec1363SSimon Glass c. If the device's uclass specifies a non-zero per_device_auto_alloc_size, 66522ec1363SSimon Glass then this space is allocated and zeroed also. It is allocated for and 66622ec1363SSimon Glass stored in the device, but it is uclass data. owned by the uclass driver. 66722ec1363SSimon Glass It is possible for the device to access it. 66822ec1363SSimon Glass 669e59f458dSSimon Glass d. If the device's immediate parent specifies a per_child_auto_alloc_size 670e59f458dSSimon Glass then this space is allocated. This is intended for use by the parent 671e59f458dSSimon Glass device to keep track of things related to the child. For example a USB 672e59f458dSSimon Glass flash stick attached to a USB host controller would likely use this 673e59f458dSSimon Glass space. The controller can hold information about the USB state of each 674e59f458dSSimon Glass of its children. 675e59f458dSSimon Glass 676e59f458dSSimon Glass e. All parent devices are probed. It is not possible to activate a device 67722ec1363SSimon Glass unless its predecessors (all the way up to the root device) are activated. 67822ec1363SSimon Glass This means (for example) that an I2C driver will require that its bus 67922ec1363SSimon Glass be activated. 68022ec1363SSimon Glass 681e59f458dSSimon Glass f. The device's sequence number is assigned, either the requested one 6825a66a8ffSSimon Glass (assuming no conflicts) or the next available one if there is a conflict 6835a66a8ffSSimon Glass or nothing particular is requested. 6845a66a8ffSSimon Glass 685e59f458dSSimon Glass g. If the driver provides an ofdata_to_platdata() method, then this is 68622ec1363SSimon Glass called to convert the device tree data into platform data. This should 687e160f7d4SSimon Glass do various calls like fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), ...) 68822ec1363SSimon Glass to access the node and store the resulting information into dev->platdata. 68922ec1363SSimon Glass After this point, the device works the same way whether it was bound 69022ec1363SSimon Glass using a device tree node or U_BOOT_DEVICE() structure. In either case, 69122ec1363SSimon Glass the platform data is now stored in the platdata structure. Typically you 69222ec1363SSimon Glass will use the platdata_auto_alloc_size feature to specify the size of the 69322ec1363SSimon Glass platform data structure, and U-Boot will automatically allocate and zero 69422ec1363SSimon Glass it for you before entry to ofdata_to_platdata(). But if not, you can 69522ec1363SSimon Glass allocate it yourself in ofdata_to_platdata(). Note that it is preferable 69622ec1363SSimon Glass to do all the device tree decoding in ofdata_to_platdata() rather than 69722ec1363SSimon Glass in probe(). (Apart from the ugliness of mixing configuration and run-time 69822ec1363SSimon Glass data, one day it is possible that U-Boot will cache platformat data for 69922ec1363SSimon Glass devices which are regularly de/activated). 70022ec1363SSimon Glass 701e59f458dSSimon Glass h. The device's probe() method is called. This should do anything that 70222ec1363SSimon Glass is required by the device to get it going. This could include checking 70322ec1363SSimon Glass that the hardware is actually present, setting up clocks for the 70422ec1363SSimon Glass hardware and setting up hardware registers to initial values. The code 70522ec1363SSimon Glass in probe() can access: 70622ec1363SSimon Glass 70722ec1363SSimon Glass - platform data in dev->platdata (for configuration) 70822ec1363SSimon Glass - private data in dev->priv (for run-time state) 70922ec1363SSimon Glass - uclass data in dev->uclass_priv (for things the uclass stores 71022ec1363SSimon Glass about this device) 71122ec1363SSimon Glass 71222ec1363SSimon Glass Note: If you don't use priv_auto_alloc_size then you will need to 71322ec1363SSimon Glass allocate the priv space here yourself. The same applies also to 71422ec1363SSimon Glass platdata_auto_alloc_size. Remember to free them in the remove() method. 71522ec1363SSimon Glass 716e59f458dSSimon Glass i. The device is marked 'activated' 71722ec1363SSimon Glass 718e59f458dSSimon Glass j. The uclass's post_probe() method is called, if one exists. This may 71922ec1363SSimon Glass cause the uclass to do some housekeeping to record the device as 72022ec1363SSimon Glass activated and 'known' by the uclass. 72122ec1363SSimon Glass 72222ec1363SSimon Glass3. Running stage 72322ec1363SSimon Glass 72422ec1363SSimon GlassThe device is now activated and can be used. From now until it is removed 72522ec1363SSimon Glassall of the above structures are accessible. The device appears in the 72622ec1363SSimon Glassuclass's list of devices (so if the device is in UCLASS_GPIO it will appear 72722ec1363SSimon Glassas a device in the GPIO uclass). This is the 'running' state of the device. 72822ec1363SSimon Glass 72922ec1363SSimon Glass4. Removal stage 73022ec1363SSimon Glass 73122ec1363SSimon GlassWhen the device is no-longer required, you can call device_remove() to 73222ec1363SSimon Glassremove it. This performs the probe steps in reverse: 73322ec1363SSimon Glass 73422ec1363SSimon Glass a. The uclass's pre_remove() method is called, if one exists. This may 73522ec1363SSimon Glass cause the uclass to do some housekeeping to record the device as 73622ec1363SSimon Glass deactivated and no-longer 'known' by the uclass. 73722ec1363SSimon Glass 73822ec1363SSimon Glass b. All the device's children are removed. It is not permitted to have 73922ec1363SSimon Glass an active child device with a non-active parent. This means that 74022ec1363SSimon Glass device_remove() is called for all the children recursively at this point. 74122ec1363SSimon Glass 74222ec1363SSimon Glass c. The device's remove() method is called. At this stage nothing has been 74322ec1363SSimon Glass deallocated so platform data, private data and the uclass data will all 74422ec1363SSimon Glass still be present. This is where the hardware can be shut down. It is 74522ec1363SSimon Glass intended that the device be completely inactive at this point, For U-Boot 74622ec1363SSimon Glass to be sure that no hardware is running, it should be enough to remove 74722ec1363SSimon Glass all devices. 74822ec1363SSimon Glass 749e59f458dSSimon Glass d. The device memory is freed (platform data, private data, uclass data, 750e59f458dSSimon Glass parent data). 75122ec1363SSimon Glass 75222ec1363SSimon Glass Note: Because the platform data for a U_BOOT_DEVICE() is defined with a 75322ec1363SSimon Glass static pointer, it is not de-allocated during the remove() method. For 75422ec1363SSimon Glass a device instantiated using the device tree data, the platform data will 75522ec1363SSimon Glass be dynamically allocated, and thus needs to be deallocated during the 75622ec1363SSimon Glass remove() method, either: 75722ec1363SSimon Glass 75822ec1363SSimon Glass 1. if the platdata_auto_alloc_size is non-zero, the deallocation 75922ec1363SSimon Glass happens automatically within the driver model core; or 76022ec1363SSimon Glass 76122ec1363SSimon Glass 2. when platdata_auto_alloc_size is 0, both the allocation (in probe() 76222ec1363SSimon Glass or preferably ofdata_to_platdata()) and the deallocation in remove() 76322ec1363SSimon Glass are the responsibility of the driver author. 76422ec1363SSimon Glass 7655a66a8ffSSimon Glass e. The device sequence number is set to -1, meaning that it no longer 7665a66a8ffSSimon Glass has an allocated sequence. If the device is later reactivated and that 7675a66a8ffSSimon Glass sequence number is still free, it may well receive the name sequence 7685a66a8ffSSimon Glass number again. But from this point, the sequence number previously used 7695a66a8ffSSimon Glass by this device will no longer exist (think of SPI bus 2 being removed 7705a66a8ffSSimon Glass and bus 2 is no longer available for use). 7715a66a8ffSSimon Glass 7725a66a8ffSSimon Glass f. The device is marked inactive. Note that it is still bound, so the 77322ec1363SSimon Glass device structure itself is not freed at this point. Should the device be 77422ec1363SSimon Glass activated again, then the cycle starts again at step 2 above. 77522ec1363SSimon Glass 77622ec1363SSimon Glass5. Unbind stage 77722ec1363SSimon Glass 77822ec1363SSimon GlassThe device is unbound. This is the step that actually destroys the device. 77922ec1363SSimon GlassIf a parent has children these will be destroyed first. After this point 78022ec1363SSimon Glassthe device does not exist and its memory has be deallocated. 78122ec1363SSimon Glass 78222ec1363SSimon Glass 78365c70539SSimon GlassData Structures 78465c70539SSimon Glass--------------- 78565c70539SSimon Glass 78665c70539SSimon GlassDriver model uses a doubly-linked list as the basic data structure. Some 78765c70539SSimon Glassnodes have several lists running through them. Creating a more efficient 78865c70539SSimon Glassdata structure might be worthwhile in some rare cases, once we understand 78965c70539SSimon Glasswhat the bottlenecks are. 79065c70539SSimon Glass 79165c70539SSimon Glass 79265c70539SSimon GlassChanges since v1 79365c70539SSimon Glass---------------- 79465c70539SSimon Glass 79565c70539SSimon GlassFor the record, this implementation uses a very similar approach to the 79665c70539SSimon Glassoriginal patches, but makes at least the following changes: 79765c70539SSimon Glass 79834e4a2ecSChris Packham- Tried to aggressively remove boilerplate, so that for most drivers there 79965c70539SSimon Glassis little or no 'driver model' code to write. 80065c70539SSimon Glass- Moved some data from code into data structure - e.g. store a pointer to 80165c70539SSimon Glassthe driver operations structure in the driver, rather than passing it 80265c70539SSimon Glassto the driver bind function. 803ae7f4513SSimon Glass- Rename some structures to make them more similar to Linux (struct udevice 80465c70539SSimon Glassinstead of struct instance, struct platdata, etc.) 80565c70539SSimon Glass- Change the name 'core' to 'uclass', meaning U-Boot class. It seems that 80665c70539SSimon Glassthis concept relates to a class of drivers (or a subsystem). We shouldn't 80765c70539SSimon Glassuse 'class' since it is a C++ reserved word, so U-Boot class (uclass) seems 80865c70539SSimon Glassbetter than 'core'. 80954c5d08aSHeiko Schocher- Remove 'struct driver_instance' and just use a single 'struct udevice'. 81065c70539SSimon GlassThis removes a level of indirection that doesn't seem necessary. 81165c70539SSimon Glass- Built in device tree support, to avoid the need for platdata 81265c70539SSimon Glass- Removed the concept of driver relocation, and just make it possible for 81365c70539SSimon Glassthe new driver (created after relocation) to access the old driver data. 81465c70539SSimon GlassI feel that relocation is a very special case and will only apply to a few 81565c70539SSimon Glassdrivers, many of which can/will just re-init anyway. So the overhead of 81665c70539SSimon Glassdealing with this might not be worth it. 81765c70539SSimon Glass- Implemented a GPIO system, trying to keep it simple 81865c70539SSimon Glass 81965c70539SSimon Glass 82000606d7eSSimon GlassPre-Relocation Support 82100606d7eSSimon Glass---------------------- 82200606d7eSSimon Glass 82300606d7eSSimon GlassFor pre-relocation we simply call the driver model init function. Only 82400606d7eSSimon Glassdrivers marked with DM_FLAG_PRE_RELOC or the device tree 82500606d7eSSimon Glass'u-boot,dm-pre-reloc' flag are initialised prior to relocation. This helps 82600606d7eSSimon Glassto reduce the driver model overhead. 82700606d7eSSimon Glass 828*27326c7eSHeiko StübnerIt is possible to limit this to specific relocation steps, by using 829*27326c7eSHeiko Stübnerthe more specialized 'u-boot,dm-spl' and 'u-boot,dm-tpl' flags 830*27326c7eSHeiko Stübnerin the devicetree. 831*27326c7eSHeiko Stübner 83200606d7eSSimon GlassThen post relocation we throw that away and re-init driver model again. 83300606d7eSSimon GlassFor drivers which require some sort of continuity between pre- and 83400606d7eSSimon Glasspost-relocation devices, we can provide access to the pre-relocation 83500606d7eSSimon Glassdevice pointers, but this is not currently implemented (the root device 83600606d7eSSimon Glasspointer is saved but not made available through the driver model API). 83700606d7eSSimon Glass 83800606d7eSSimon Glass 83938687ae6SSimon GlassSPL Support 84038687ae6SSimon Glass----------- 84138687ae6SSimon Glass 84238687ae6SSimon GlassDriver model can operate in SPL. Its efficient implementation and small code 84338687ae6SSimon Glasssize provide for a small overhead which is acceptable for all but the most 84438687ae6SSimon Glassconstrained systems. 84538687ae6SSimon Glass 84638687ae6SSimon GlassTo enable driver model in SPL, define CONFIG_SPL_DM. You might want to 84738687ae6SSimon Glassconsider the following option also. See the main README for more details. 84838687ae6SSimon Glass 84938687ae6SSimon Glass - CONFIG_SYS_MALLOC_SIMPLE 85038687ae6SSimon Glass - CONFIG_DM_WARN 85138687ae6SSimon Glass - CONFIG_DM_DEVICE_REMOVE 85238687ae6SSimon Glass - CONFIG_DM_STDIO 85338687ae6SSimon Glass 85438687ae6SSimon Glass 85538687ae6SSimon GlassEnabling Driver Model 85638687ae6SSimon Glass--------------------- 85738687ae6SSimon Glass 85838687ae6SSimon GlassDriver model is being brought into U-Boot gradually. As each subsystems gets 85938687ae6SSimon Glasssupport, a uclass is created and a CONFIG to enable use of driver model for 86038687ae6SSimon Glassthat subsystem. 86138687ae6SSimon Glass 86238687ae6SSimon GlassFor example CONFIG_DM_SERIAL enables driver model for serial. With that 86338687ae6SSimon Glassdefined, the old serial support is not enabled, and your serial driver must 86438687ae6SSimon Glassconform to driver model. With that undefined, the old serial support is 86538687ae6SSimon Glassenabled and driver model is not available for serial. This means that when 86638687ae6SSimon Glassyou convert a driver, you must either convert all its boards, or provide for 86738687ae6SSimon Glassthe driver to be compiled both with and without driver model (generally this 86838687ae6SSimon Glassis not very hard). 86938687ae6SSimon Glass 87038687ae6SSimon GlassSee the main README for full details of the available driver model CONFIG 87138687ae6SSimon Glassoptions. 87238687ae6SSimon Glass 87338687ae6SSimon Glass 87465c70539SSimon GlassThings to punt for later 87565c70539SSimon Glass------------------------ 87665c70539SSimon Glass 87765c70539SSimon GlassUclasses are statically numbered at compile time. It would be possible to 87865c70539SSimon Glasschange this to dynamic numbering, but then we would require some sort of 87965c70539SSimon Glasslookup service, perhaps searching by name. This is slightly less efficient 88065c70539SSimon Glassso has been left out for now. One small advantage of dynamic numbering might 88165c70539SSimon Glassbe fewer merge conflicts in uclass-id.h. 88265c70539SSimon Glass 88365c70539SSimon Glass 88465c70539SSimon GlassSimon Glass 88565c70539SSimon Glasssjg@chromium.org 88665c70539SSimon GlassApril 2013 88765c70539SSimon GlassUpdated 7-May-13 88865c70539SSimon GlassUpdated 14-Jun-13 88965c70539SSimon GlassUpdated 18-Oct-13 89065c70539SSimon GlassUpdated 5-Nov-13 891