1*4882a593SmuzhiyunDriver Model 2*4882a593Smuzhiyun============ 3*4882a593Smuzhiyun 4*4882a593SmuzhiyunThis README contains high-level information about driver model, a unified 5*4882a593Smuzhiyunway of declaring and accessing drivers in U-Boot. The original work was done 6*4882a593Smuzhiyunby: 7*4882a593Smuzhiyun 8*4882a593Smuzhiyun Marek Vasut <marex@denx.de> 9*4882a593Smuzhiyun Pavel Herrmann <morpheus.ibis@gmail.com> 10*4882a593Smuzhiyun Viktor Křivák <viktor.krivak@gmail.com> 11*4882a593Smuzhiyun Tomas Hlavacek <tmshlvck@gmail.com> 12*4882a593Smuzhiyun 13*4882a593SmuzhiyunThis has been both simplified and extended into the current implementation 14*4882a593Smuzhiyunby: 15*4882a593Smuzhiyun 16*4882a593Smuzhiyun Simon Glass <sjg@chromium.org> 17*4882a593Smuzhiyun 18*4882a593Smuzhiyun 19*4882a593SmuzhiyunTerminology 20*4882a593Smuzhiyun----------- 21*4882a593Smuzhiyun 22*4882a593SmuzhiyunUclass - a group of devices which operate in the same way. A uclass provides 23*4882a593Smuzhiyun a way of accessing individual devices within the group, but always 24*4882a593Smuzhiyun using the same interface. For example a GPIO uclass provides 25*4882a593Smuzhiyun operations for get/set value. An I2C uclass may have 10 I2C ports, 26*4882a593Smuzhiyun 4 with one driver, and 6 with another. 27*4882a593Smuzhiyun 28*4882a593SmuzhiyunDriver - some code which talks to a peripheral and presents a higher-level 29*4882a593Smuzhiyun interface to it. 30*4882a593Smuzhiyun 31*4882a593SmuzhiyunDevice - an instance of a driver, tied to a particular port or peripheral. 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun 34*4882a593SmuzhiyunHow to try it 35*4882a593Smuzhiyun------------- 36*4882a593Smuzhiyun 37*4882a593SmuzhiyunBuild U-Boot sandbox and run it: 38*4882a593Smuzhiyun 39*4882a593Smuzhiyun make sandbox_defconfig 40*4882a593Smuzhiyun make 41*4882a593Smuzhiyun ./u-boot -d u-boot.dtb 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun (type 'reset' to exit U-Boot) 44*4882a593Smuzhiyun 45*4882a593Smuzhiyun 46*4882a593SmuzhiyunThere is a uclass called 'demo'. This uclass handles 47*4882a593Smuzhiyunsaying hello, and reporting its status. There are two drivers in this 48*4882a593Smuzhiyunuclass: 49*4882a593Smuzhiyun 50*4882a593Smuzhiyun - simple: Just prints a message for hello, doesn't implement status 51*4882a593Smuzhiyun - shape: Prints shapes and reports number of characters printed as status 52*4882a593Smuzhiyun 53*4882a593SmuzhiyunThe demo class is pretty simple, but not trivial. The intention is that it 54*4882a593Smuzhiyuncan be used for testing, so it will implement all driver model features and 55*4882a593Smuzhiyunprovide good code coverage of them. It does have multiple drivers, it 56*4882a593Smuzhiyunhandles parameter data and platdata (data which tells the driver how 57*4882a593Smuzhiyunto operate on a particular platform) and it uses private driver data. 58*4882a593Smuzhiyun 59*4882a593SmuzhiyunTo try it, see the example session below: 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun=>demo hello 1 62*4882a593SmuzhiyunHello '@' from 07981110: red 4 63*4882a593Smuzhiyun=>demo status 2 64*4882a593SmuzhiyunStatus: 0 65*4882a593Smuzhiyun=>demo hello 2 66*4882a593Smuzhiyung 67*4882a593Smuzhiyunr@ 68*4882a593Smuzhiyune@@ 69*4882a593Smuzhiyune@@@ 70*4882a593Smuzhiyunn@@@@ 71*4882a593Smuzhiyung@@@@@ 72*4882a593Smuzhiyun=>demo status 2 73*4882a593SmuzhiyunStatus: 21 74*4882a593Smuzhiyun=>demo hello 4 ^ 75*4882a593Smuzhiyun y^^^ 76*4882a593Smuzhiyun e^^^^^ 77*4882a593Smuzhiyunl^^^^^^^ 78*4882a593Smuzhiyunl^^^^^^^ 79*4882a593Smuzhiyun o^^^^^ 80*4882a593Smuzhiyun w^^^ 81*4882a593Smuzhiyun=>demo status 4 82*4882a593SmuzhiyunStatus: 36 83*4882a593Smuzhiyun=> 84*4882a593Smuzhiyun 85*4882a593Smuzhiyun 86*4882a593SmuzhiyunRunning the tests 87*4882a593Smuzhiyun----------------- 88*4882a593Smuzhiyun 89*4882a593SmuzhiyunThe intent with driver model is that the core portion has 100% test coverage 90*4882a593Smuzhiyunin sandbox, and every uclass has its own test. As a move towards this, tests 91*4882a593Smuzhiyunare provided in test/dm. To run them, try: 92*4882a593Smuzhiyun 93*4882a593Smuzhiyun ./test/py/test.py --bd sandbox --build -k ut_dm -v 94*4882a593Smuzhiyun 95*4882a593SmuzhiyunYou should see something like this: 96*4882a593Smuzhiyun 97*4882a593Smuzhiyun(venv)$ ./test/py/test.py --bd sandbox --build -k ut_dm -v 98*4882a593Smuzhiyun+make O=/root/u-boot/build-sandbox -s sandbox_defconfig 99*4882a593Smuzhiyun+make O=/root/u-boot/build-sandbox -s -j8 100*4882a593Smuzhiyun============================= test session starts ============================== 101*4882a593Smuzhiyunplatform linux2 -- Python 2.7.5, pytest-2.9.0, py-1.4.31, pluggy-0.3.1 -- /root/u-boot/venv/bin/python 102*4882a593Smuzhiyuncachedir: .cache 103*4882a593Smuzhiyunrootdir: /root/u-boot, inifile: 104*4882a593Smuzhiyuncollected 199 items 105*4882a593Smuzhiyun 106*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut_dm_init PASSED 107*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_adc_bind] PASSED 108*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_adc_multi_channel_conversion] PASSED 109*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_adc_multi_channel_shot] PASSED 110*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_adc_single_channel_conversion] PASSED 111*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_adc_single_channel_shot] PASSED 112*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_adc_supply] PASSED 113*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_adc_wrong_channel_selection] PASSED 114*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_autobind] PASSED 115*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_autobind_uclass_pdata_alloc] PASSED 116*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_autobind_uclass_pdata_valid] PASSED 117*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_autoprobe] PASSED 118*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_bus_child_post_bind] PASSED 119*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_bus_child_post_bind_uclass] PASSED 120*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_bus_child_pre_probe_uclass] PASSED 121*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_bus_children] PASSED 122*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_bus_children_funcs] PASSED 123*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_bus_children_iterators] PASSED 124*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_bus_parent_data] PASSED 125*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_bus_parent_data_uclass] PASSED 126*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_bus_parent_ops] PASSED 127*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_bus_parent_platdata] PASSED 128*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_bus_parent_platdata_uclass] PASSED 129*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_children] PASSED 130*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_clk_base] PASSED 131*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_clk_periph] PASSED 132*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_device_get_uclass_id] PASSED 133*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_eth] PASSED 134*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_eth_act] PASSED 135*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_eth_alias] PASSED 136*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_eth_prime] PASSED 137*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_eth_rotate] PASSED 138*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_fdt] PASSED 139*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_fdt_offset] PASSED 140*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_fdt_pre_reloc] PASSED 141*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_fdt_uclass_seq] PASSED 142*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_gpio] PASSED 143*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_gpio_anon] PASSED 144*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_gpio_copy] PASSED 145*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_gpio_leak] PASSED 146*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_gpio_phandles] PASSED 147*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_gpio_requestf] PASSED 148*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_i2c_bytewise] PASSED 149*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_i2c_find] PASSED 150*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_i2c_offset] PASSED 151*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_i2c_offset_len] PASSED 152*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_i2c_probe_empty] PASSED 153*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_i2c_read_write] PASSED 154*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_i2c_speed] PASSED 155*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_leak] PASSED 156*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_led_base] PASSED 157*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_led_gpio] PASSED 158*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_led_label] PASSED 159*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_lifecycle] PASSED 160*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_mmc_base] PASSED 161*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_net_retry] PASSED 162*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_operations] PASSED 163*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_ordering] PASSED 164*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_pci_base] PASSED 165*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_pci_busnum] PASSED 166*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_pci_swapcase] PASSED 167*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_platdata] PASSED 168*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_power_pmic_get] PASSED 169*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_power_pmic_io] PASSED 170*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_power_regulator_autoset] PASSED 171*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_power_regulator_autoset_list] PASSED 172*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_power_regulator_get] PASSED 173*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_power_regulator_set_get_current] PASSED 174*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_power_regulator_set_get_enable] PASSED 175*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_power_regulator_set_get_mode] PASSED 176*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_power_regulator_set_get_voltage] PASSED 177*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_pre_reloc] PASSED 178*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_ram_base] PASSED 179*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_regmap_base] PASSED 180*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_regmap_syscon] PASSED 181*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_remoteproc_base] PASSED 182*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_remove] PASSED 183*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_reset_base] PASSED 184*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_reset_walk] PASSED 185*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_rtc_base] PASSED 186*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_rtc_dual] PASSED 187*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_rtc_reset] PASSED 188*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_rtc_set_get] PASSED 189*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_spi_find] PASSED 190*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_spi_flash] PASSED 191*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_spi_xfer] PASSED 192*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_syscon_base] PASSED 193*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_syscon_by_driver_data] PASSED 194*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_timer_base] PASSED 195*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_uclass] PASSED 196*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_uclass_before_ready] PASSED 197*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_uclass_devices_find] PASSED 198*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_uclass_devices_find_by_name] PASSED 199*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_uclass_devices_get] PASSED 200*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_uclass_devices_get_by_name] PASSED 201*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_usb_base] PASSED 202*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_usb_flash] PASSED 203*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_usb_keyb] PASSED 204*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_usb_multi] PASSED 205*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_usb_remove] PASSED 206*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_usb_tree] PASSED 207*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_usb_tree_remove] PASSED 208*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_usb_tree_reorder] PASSED 209*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_video_base] PASSED 210*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_video_bmp] PASSED 211*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_video_bmp_comp] PASSED 212*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_video_chars] PASSED 213*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_video_context] PASSED 214*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_video_rotation1] PASSED 215*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_video_rotation2] PASSED 216*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_video_rotation3] PASSED 217*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_video_text] PASSED 218*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_video_truetype] PASSED 219*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_video_truetype_bs] PASSED 220*4882a593Smuzhiyuntest/py/tests/test_ut.py::test_ut[ut_dm_video_truetype_scroll] PASSED 221*4882a593Smuzhiyun 222*4882a593Smuzhiyun======================= 84 tests deselected by '-kut_dm' ======================= 223*4882a593Smuzhiyun================== 115 passed, 84 deselected in 3.77 seconds =================== 224*4882a593Smuzhiyun 225*4882a593SmuzhiyunWhat is going on? 226*4882a593Smuzhiyun----------------- 227*4882a593Smuzhiyun 228*4882a593SmuzhiyunLet's start at the top. The demo command is in common/cmd_demo.c. It does 229*4882a593Smuzhiyunthe usual command processing and then: 230*4882a593Smuzhiyun 231*4882a593Smuzhiyun struct udevice *demo_dev; 232*4882a593Smuzhiyun 233*4882a593Smuzhiyun ret = uclass_get_device(UCLASS_DEMO, devnum, &demo_dev); 234*4882a593Smuzhiyun 235*4882a593SmuzhiyunUCLASS_DEMO means the class of devices which implement 'demo'. Other 236*4882a593Smuzhiyunclasses might be MMC, or GPIO, hashing or serial. The idea is that the 237*4882a593Smuzhiyundevices in the class all share a particular way of working. The class 238*4882a593Smuzhiyunpresents a unified view of all these devices to U-Boot. 239*4882a593Smuzhiyun 240*4882a593SmuzhiyunThis function looks up a device for the demo uclass. Given a device 241*4882a593Smuzhiyunnumber we can find the device because all devices have registered with 242*4882a593Smuzhiyunthe UCLASS_DEMO uclass. 243*4882a593Smuzhiyun 244*4882a593SmuzhiyunThe device is automatically activated ready for use by uclass_get_device(). 245*4882a593Smuzhiyun 246*4882a593SmuzhiyunNow that we have the device we can do things like: 247*4882a593Smuzhiyun 248*4882a593Smuzhiyun return demo_hello(demo_dev, ch); 249*4882a593Smuzhiyun 250*4882a593SmuzhiyunThis function is in the demo uclass. It takes care of calling the 'hello' 251*4882a593Smuzhiyunmethod of the relevant driver. Bearing in mind that there are two drivers, 252*4882a593Smuzhiyunthis particular device may use one or other of them. 253*4882a593Smuzhiyun 254*4882a593SmuzhiyunThe code for demo_hello() is in drivers/demo/demo-uclass.c: 255*4882a593Smuzhiyun 256*4882a593Smuzhiyunint demo_hello(struct udevice *dev, int ch) 257*4882a593Smuzhiyun{ 258*4882a593Smuzhiyun const struct demo_ops *ops = device_get_ops(dev); 259*4882a593Smuzhiyun 260*4882a593Smuzhiyun if (!ops->hello) 261*4882a593Smuzhiyun return -ENOSYS; 262*4882a593Smuzhiyun 263*4882a593Smuzhiyun return ops->hello(dev, ch); 264*4882a593Smuzhiyun} 265*4882a593Smuzhiyun 266*4882a593SmuzhiyunAs you can see it just calls the relevant driver method. One of these is 267*4882a593Smuzhiyunin drivers/demo/demo-simple.c: 268*4882a593Smuzhiyun 269*4882a593Smuzhiyunstatic int simple_hello(struct udevice *dev, int ch) 270*4882a593Smuzhiyun{ 271*4882a593Smuzhiyun const struct dm_demo_pdata *pdata = dev_get_platdata(dev); 272*4882a593Smuzhiyun 273*4882a593Smuzhiyun printf("Hello from %08x: %s %d\n", map_to_sysmem(dev), 274*4882a593Smuzhiyun pdata->colour, pdata->sides); 275*4882a593Smuzhiyun 276*4882a593Smuzhiyun return 0; 277*4882a593Smuzhiyun} 278*4882a593Smuzhiyun 279*4882a593Smuzhiyun 280*4882a593SmuzhiyunSo that is a trip from top (command execution) to bottom (driver action) 281*4882a593Smuzhiyunbut it leaves a lot of topics to address. 282*4882a593Smuzhiyun 283*4882a593Smuzhiyun 284*4882a593SmuzhiyunDeclaring Drivers 285*4882a593Smuzhiyun----------------- 286*4882a593Smuzhiyun 287*4882a593SmuzhiyunA driver declaration looks something like this (see 288*4882a593Smuzhiyundrivers/demo/demo-shape.c): 289*4882a593Smuzhiyun 290*4882a593Smuzhiyunstatic const struct demo_ops shape_ops = { 291*4882a593Smuzhiyun .hello = shape_hello, 292*4882a593Smuzhiyun .status = shape_status, 293*4882a593Smuzhiyun}; 294*4882a593Smuzhiyun 295*4882a593SmuzhiyunU_BOOT_DRIVER(demo_shape_drv) = { 296*4882a593Smuzhiyun .name = "demo_shape_drv", 297*4882a593Smuzhiyun .id = UCLASS_DEMO, 298*4882a593Smuzhiyun .ops = &shape_ops, 299*4882a593Smuzhiyun .priv_data_size = sizeof(struct shape_data), 300*4882a593Smuzhiyun}; 301*4882a593Smuzhiyun 302*4882a593Smuzhiyun 303*4882a593SmuzhiyunThis driver has two methods (hello and status) and requires a bit of 304*4882a593Smuzhiyunprivate data (accessible through dev_get_priv(dev) once the driver has 305*4882a593Smuzhiyunbeen probed). It is a member of UCLASS_DEMO so will register itself 306*4882a593Smuzhiyunthere. 307*4882a593Smuzhiyun 308*4882a593SmuzhiyunIn U_BOOT_DRIVER it is also possible to specify special methods for bind 309*4882a593Smuzhiyunand unbind, and these are called at appropriate times. For many drivers 310*4882a593Smuzhiyunit is hoped that only 'probe' and 'remove' will be needed. 311*4882a593Smuzhiyun 312*4882a593SmuzhiyunThe U_BOOT_DRIVER macro creates a data structure accessible from C, 313*4882a593Smuzhiyunso driver model can find the drivers that are available. 314*4882a593Smuzhiyun 315*4882a593SmuzhiyunThe methods a device can provide are documented in the device.h header. 316*4882a593SmuzhiyunBriefly, they are: 317*4882a593Smuzhiyun 318*4882a593Smuzhiyun bind - make the driver model aware of a device (bind it to its driver) 319*4882a593Smuzhiyun unbind - make the driver model forget the device 320*4882a593Smuzhiyun ofdata_to_platdata - convert device tree data to platdata - see later 321*4882a593Smuzhiyun probe - make a device ready for use 322*4882a593Smuzhiyun remove - remove a device so it cannot be used until probed again 323*4882a593Smuzhiyun 324*4882a593SmuzhiyunThe sequence to get a device to work is bind, ofdata_to_platdata (if using 325*4882a593Smuzhiyundevice tree) and probe. 326*4882a593Smuzhiyun 327*4882a593Smuzhiyun 328*4882a593SmuzhiyunPlatform Data 329*4882a593Smuzhiyun------------- 330*4882a593Smuzhiyun 331*4882a593Smuzhiyun*** Note: platform data is the old way of doing things. It is 332*4882a593Smuzhiyun*** basically a C structure which is passed to drivers to tell them about 333*4882a593Smuzhiyun*** platform-specific settings like the address of its registers, bus 334*4882a593Smuzhiyun*** speed, etc. Device tree is now the preferred way of handling this. 335*4882a593Smuzhiyun*** Unless you have a good reason not to use device tree (the main one 336*4882a593Smuzhiyun*** being you need serial support in SPL and don't have enough SRAM for 337*4882a593Smuzhiyun*** the cut-down device tree and libfdt libraries) you should stay away 338*4882a593Smuzhiyun*** from platform data. 339*4882a593Smuzhiyun 340*4882a593SmuzhiyunPlatform data is like Linux platform data, if you are familiar with that. 341*4882a593SmuzhiyunIt provides the board-specific information to start up a device. 342*4882a593Smuzhiyun 343*4882a593SmuzhiyunWhy is this information not just stored in the device driver itself? The 344*4882a593Smuzhiyunidea is that the device driver is generic, and can in principle operate on 345*4882a593Smuzhiyunany board that has that type of device. For example, with modern 346*4882a593Smuzhiyunhighly-complex SoCs it is common for the IP to come from an IP vendor, and 347*4882a593Smuzhiyuntherefore (for example) the MMC controller may be the same on chips from 348*4882a593Smuzhiyundifferent vendors. It makes no sense to write independent drivers for the 349*4882a593SmuzhiyunMMC controller on each vendor's SoC, when they are all almost the same. 350*4882a593SmuzhiyunSimilarly, we may have 6 UARTs in an SoC, all of which are mostly the same, 351*4882a593Smuzhiyunbut lie at different addresses in the address space. 352*4882a593Smuzhiyun 353*4882a593SmuzhiyunUsing the UART example, we have a single driver and it is instantiated 6 354*4882a593Smuzhiyuntimes by supplying 6 lots of platform data. Each lot of platform data 355*4882a593Smuzhiyungives the driver name and a pointer to a structure containing information 356*4882a593Smuzhiyunabout this instance - e.g. the address of the register space. It may be that 357*4882a593Smuzhiyunone of the UARTS supports RS-485 operation - this can be added as a flag in 358*4882a593Smuzhiyunthe platform data, which is set for this one port and clear for the rest. 359*4882a593Smuzhiyun 360*4882a593SmuzhiyunThink of your driver as a generic piece of code which knows how to talk to 361*4882a593Smuzhiyuna device, but needs to know where it is, any variant/option information and 362*4882a593Smuzhiyunso on. Platform data provides this link between the generic piece of code 363*4882a593Smuzhiyunand the specific way it is bound on a particular board. 364*4882a593Smuzhiyun 365*4882a593SmuzhiyunExamples of platform data include: 366*4882a593Smuzhiyun 367*4882a593Smuzhiyun - The base address of the IP block's register space 368*4882a593Smuzhiyun - Configuration options, like: 369*4882a593Smuzhiyun - the SPI polarity and maximum speed for a SPI controller 370*4882a593Smuzhiyun - the I2C speed to use for an I2C device 371*4882a593Smuzhiyun - the number of GPIOs available in a GPIO device 372*4882a593Smuzhiyun 373*4882a593SmuzhiyunWhere does the platform data come from? It is either held in a structure 374*4882a593Smuzhiyunwhich is compiled into U-Boot, or it can be parsed from the Device Tree 375*4882a593Smuzhiyun(see 'Device Tree' below). 376*4882a593Smuzhiyun 377*4882a593SmuzhiyunFor an example of how it can be compiled in, see demo-pdata.c which 378*4882a593Smuzhiyunsets up a table of driver names and their associated platform data. 379*4882a593SmuzhiyunThe data can be interpreted by the drivers however they like - it is 380*4882a593Smuzhiyunbasically a communication scheme between the board-specific code and 381*4882a593Smuzhiyunthe generic drivers, which are intended to work on any board. 382*4882a593Smuzhiyun 383*4882a593SmuzhiyunDrivers can access their data via dev->info->platdata. Here is 384*4882a593Smuzhiyunthe declaration for the platform data, which would normally appear 385*4882a593Smuzhiyunin the board file. 386*4882a593Smuzhiyun 387*4882a593Smuzhiyun static const struct dm_demo_cdata red_square = { 388*4882a593Smuzhiyun .colour = "red", 389*4882a593Smuzhiyun .sides = 4. 390*4882a593Smuzhiyun }; 391*4882a593Smuzhiyun static const struct driver_info info[] = { 392*4882a593Smuzhiyun { 393*4882a593Smuzhiyun .name = "demo_shape_drv", 394*4882a593Smuzhiyun .platdata = &red_square, 395*4882a593Smuzhiyun }, 396*4882a593Smuzhiyun }; 397*4882a593Smuzhiyun 398*4882a593Smuzhiyun demo1 = driver_bind(root, &info[0]); 399*4882a593Smuzhiyun 400*4882a593Smuzhiyun 401*4882a593SmuzhiyunDevice Tree 402*4882a593Smuzhiyun----------- 403*4882a593Smuzhiyun 404*4882a593SmuzhiyunWhile platdata is useful, a more flexible way of providing device data is 405*4882a593Smuzhiyunby using device tree. In U-Boot you should use this where possible. Avoid 406*4882a593Smuzhiyunsending patches which make use of the U_BOOT_DEVICE() macro unless strictly 407*4882a593Smuzhiyunnecessary. 408*4882a593Smuzhiyun 409*4882a593SmuzhiyunWith device tree we replace the above code with the following device tree 410*4882a593Smuzhiyunfragment: 411*4882a593Smuzhiyun 412*4882a593Smuzhiyun red-square { 413*4882a593Smuzhiyun compatible = "demo-shape"; 414*4882a593Smuzhiyun colour = "red"; 415*4882a593Smuzhiyun sides = <4>; 416*4882a593Smuzhiyun }; 417*4882a593Smuzhiyun 418*4882a593SmuzhiyunThis means that instead of having lots of U_BOOT_DEVICE() declarations in 419*4882a593Smuzhiyunthe board file, we put these in the device tree. This approach allows a lot 420*4882a593Smuzhiyunmore generality, since the same board file can support many types of boards 421*4882a593Smuzhiyun(e,g. with the same SoC) just by using different device trees. An added 422*4882a593Smuzhiyunbenefit is that the Linux device tree can be used, thus further simplifying 423*4882a593Smuzhiyunthe task of board-bring up either for U-Boot or Linux devs (whoever gets to 424*4882a593Smuzhiyunthe board first!). 425*4882a593Smuzhiyun 426*4882a593SmuzhiyunThe easiest way to make this work it to add a few members to the driver: 427*4882a593Smuzhiyun 428*4882a593Smuzhiyun .platdata_auto_alloc_size = sizeof(struct dm_test_pdata), 429*4882a593Smuzhiyun .ofdata_to_platdata = testfdt_ofdata_to_platdata, 430*4882a593Smuzhiyun 431*4882a593SmuzhiyunThe 'auto_alloc' feature allowed space for the platdata to be allocated 432*4882a593Smuzhiyunand zeroed before the driver's ofdata_to_platdata() method is called. The 433*4882a593Smuzhiyunofdata_to_platdata() method, which the driver write supplies, should parse 434*4882a593Smuzhiyunthe device tree node for this device and place it in dev->platdata. Thus 435*4882a593Smuzhiyunwhen the probe method is called later (to set up the device ready for use) 436*4882a593Smuzhiyunthe platform data will be present. 437*4882a593Smuzhiyun 438*4882a593SmuzhiyunNote that both methods are optional. If you provide an ofdata_to_platdata 439*4882a593Smuzhiyunmethod then it will be called first (during activation). If you provide a 440*4882a593Smuzhiyunprobe method it will be called next. See Driver Lifecycle below for more 441*4882a593Smuzhiyundetails. 442*4882a593Smuzhiyun 443*4882a593SmuzhiyunIf you don't want to have the platdata automatically allocated then you 444*4882a593Smuzhiyuncan leave out platdata_auto_alloc_size. In this case you can use malloc 445*4882a593Smuzhiyunin your ofdata_to_platdata (or probe) method to allocate the required memory, 446*4882a593Smuzhiyunand you should free it in the remove method. 447*4882a593Smuzhiyun 448*4882a593SmuzhiyunThe driver model tree is intended to mirror that of the device tree. The 449*4882a593Smuzhiyunroot driver is at device tree offset 0 (the root node, '/'), and its 450*4882a593Smuzhiyunchildren are the children of the root node. 451*4882a593Smuzhiyun 452*4882a593Smuzhiyun 453*4882a593SmuzhiyunDeclaring Uclasses 454*4882a593Smuzhiyun------------------ 455*4882a593Smuzhiyun 456*4882a593SmuzhiyunThe demo uclass is declared like this: 457*4882a593Smuzhiyun 458*4882a593SmuzhiyunU_BOOT_CLASS(demo) = { 459*4882a593Smuzhiyun .id = UCLASS_DEMO, 460*4882a593Smuzhiyun}; 461*4882a593Smuzhiyun 462*4882a593SmuzhiyunIt is also possible to specify special methods for probe, etc. The uclass 463*4882a593Smuzhiyunnumbering comes from include/dm/uclass.h. To add a new uclass, add to the 464*4882a593Smuzhiyunend of the enum there, then declare your uclass as above. 465*4882a593Smuzhiyun 466*4882a593Smuzhiyun 467*4882a593SmuzhiyunDevice Sequence Numbers 468*4882a593Smuzhiyun----------------------- 469*4882a593Smuzhiyun 470*4882a593SmuzhiyunU-Boot numbers devices from 0 in many situations, such as in the command 471*4882a593Smuzhiyunline for I2C and SPI buses, and the device names for serial ports (serial0, 472*4882a593Smuzhiyunserial1, ...). Driver model supports this numbering and permits devices 473*4882a593Smuzhiyunto be locating by their 'sequence'. This numbering uniquely identifies a 474*4882a593Smuzhiyundevice in its uclass, so no two devices within a particular uclass can have 475*4882a593Smuzhiyunthe same sequence number. 476*4882a593Smuzhiyun 477*4882a593SmuzhiyunSequence numbers start from 0 but gaps are permitted. For example, a board 478*4882a593Smuzhiyunmay have I2C buses 1, 4, 5 but no 0, 2 or 3. The choice of how devices are 479*4882a593Smuzhiyunnumbered is up to a particular board, and may be set by the SoC in some 480*4882a593Smuzhiyuncases. While it might be tempting to automatically renumber the devices 481*4882a593Smuzhiyunwhere there are gaps in the sequence, this can lead to confusion and is 482*4882a593Smuzhiyunnot the way that U-Boot works. 483*4882a593Smuzhiyun 484*4882a593SmuzhiyunEach device can request a sequence number. If none is required then the 485*4882a593Smuzhiyundevice will be automatically allocated the next available sequence number. 486*4882a593Smuzhiyun 487*4882a593SmuzhiyunTo specify the sequence number in the device tree an alias is typically 488*4882a593Smuzhiyunused. Make sure that the uclass has the DM_UC_FLAG_SEQ_ALIAS flag set. 489*4882a593Smuzhiyun 490*4882a593Smuzhiyunaliases { 491*4882a593Smuzhiyun serial2 = "/serial@22230000"; 492*4882a593Smuzhiyun}; 493*4882a593Smuzhiyun 494*4882a593SmuzhiyunThis indicates that in the uclass called "serial", the named node 495*4882a593Smuzhiyun("/serial@22230000") will be given sequence number 2. Any command or driver 496*4882a593Smuzhiyunwhich requests serial device 2 will obtain this device. 497*4882a593Smuzhiyun 498*4882a593SmuzhiyunMore commonly you can use node references, which expand to the full path: 499*4882a593Smuzhiyun 500*4882a593Smuzhiyunaliases { 501*4882a593Smuzhiyun serial2 = &serial_2; 502*4882a593Smuzhiyun}; 503*4882a593Smuzhiyun... 504*4882a593Smuzhiyunserial_2: serial@22230000 { 505*4882a593Smuzhiyun... 506*4882a593Smuzhiyun}; 507*4882a593Smuzhiyun 508*4882a593SmuzhiyunThe alias resolves to the same string in this case, but this version is 509*4882a593Smuzhiyuneasier to read. 510*4882a593Smuzhiyun 511*4882a593SmuzhiyunDevice sequence numbers are resolved when a device is probed. Before then 512*4882a593Smuzhiyunthe sequence number is only a request which may or may not be honoured, 513*4882a593Smuzhiyundepending on what other devices have been probed. However the numbering is 514*4882a593Smuzhiyunentirely under the control of the board author so a conflict is generally 515*4882a593Smuzhiyunan error. 516*4882a593Smuzhiyun 517*4882a593Smuzhiyun 518*4882a593SmuzhiyunBus Drivers 519*4882a593Smuzhiyun----------- 520*4882a593Smuzhiyun 521*4882a593SmuzhiyunA common use of driver model is to implement a bus, a device which provides 522*4882a593Smuzhiyunaccess to other devices. Example of buses include SPI and I2C. Typically 523*4882a593Smuzhiyunthe bus provides some sort of transport or translation that makes it 524*4882a593Smuzhiyunpossible to talk to the devices on the bus. 525*4882a593Smuzhiyun 526*4882a593SmuzhiyunDriver model provides some useful features to help with implementing buses. 527*4882a593SmuzhiyunFirstly, a bus can request that its children store some 'parent data' which 528*4882a593Smuzhiyuncan be used to keep track of child state. Secondly, the bus can define 529*4882a593Smuzhiyunmethods which are called when a child is probed or removed. This is similar 530*4882a593Smuzhiyunto the methods the uclass driver provides. Thirdly, per-child platform data 531*4882a593Smuzhiyuncan be provided to specify things like the child's address on the bus. This 532*4882a593Smuzhiyunpersists across child probe()/remove() cycles. 533*4882a593Smuzhiyun 534*4882a593SmuzhiyunFor consistency and ease of implementation, the bus uclass can specify the 535*4882a593Smuzhiyunper-child platform data, so that it can be the same for all children of buses 536*4882a593Smuzhiyunin that uclass. There are also uclass methods which can be called when 537*4882a593Smuzhiyunchildren are bound and probed. 538*4882a593Smuzhiyun 539*4882a593SmuzhiyunHere an explanation of how a bus fits with a uclass may be useful. Consider 540*4882a593Smuzhiyuna USB bus with several devices attached to it, each from a different (made 541*4882a593Smuzhiyunup) uclass: 542*4882a593Smuzhiyun 543*4882a593Smuzhiyun xhci_usb (UCLASS_USB) 544*4882a593Smuzhiyun eth (UCLASS_ETHERNET) 545*4882a593Smuzhiyun camera (UCLASS_CAMERA) 546*4882a593Smuzhiyun flash (UCLASS_FLASH_STORAGE) 547*4882a593Smuzhiyun 548*4882a593SmuzhiyunEach of the devices is connected to a different address on the USB bus. 549*4882a593SmuzhiyunThe bus device wants to store this address and some other information such 550*4882a593Smuzhiyunas the bus speed for each device. 551*4882a593Smuzhiyun 552*4882a593SmuzhiyunTo achieve this, the bus device can use dev->parent_platdata in each of its 553*4882a593Smuzhiyunthree children. This can be auto-allocated if the bus driver (or bus uclass) 554*4882a593Smuzhiyunhas a non-zero value for per_child_platdata_auto_alloc_size. If not, then 555*4882a593Smuzhiyunthe bus device or uclass can allocate the space itself before the child 556*4882a593Smuzhiyundevice is probed. 557*4882a593Smuzhiyun 558*4882a593SmuzhiyunAlso the bus driver can define the child_pre_probe() and child_post_remove() 559*4882a593Smuzhiyunmethods to allow it to do some processing before the child is activated or 560*4882a593Smuzhiyunafter it is deactivated. 561*4882a593Smuzhiyun 562*4882a593SmuzhiyunSimilarly the bus uclass can define the child_post_bind() method to obtain 563*4882a593Smuzhiyunthe per-child platform data from the device tree and set it up for the child. 564*4882a593SmuzhiyunThe bus uclass can also provide a child_pre_probe() method. Very often it is 565*4882a593Smuzhiyunthe bus uclass that controls these features, since it avoids each driver 566*4882a593Smuzhiyunhaving to do the same processing. Of course the driver can still tweak and 567*4882a593Smuzhiyunoverride these activities. 568*4882a593Smuzhiyun 569*4882a593SmuzhiyunNote that the information that controls this behaviour is in the bus's 570*4882a593Smuzhiyundriver, not the child's. In fact it is possible that child has no knowledge 571*4882a593Smuzhiyunthat it is connected to a bus. The same child device may even be used on two 572*4882a593Smuzhiyundifferent bus types. As an example. the 'flash' device shown above may also 573*4882a593Smuzhiyunbe connected on a SATA bus or standalone with no bus: 574*4882a593Smuzhiyun 575*4882a593Smuzhiyun xhci_usb (UCLASS_USB) 576*4882a593Smuzhiyun flash (UCLASS_FLASH_STORAGE) - parent data/methods defined by USB bus 577*4882a593Smuzhiyun 578*4882a593Smuzhiyun sata (UCLASS_SATA) 579*4882a593Smuzhiyun flash (UCLASS_FLASH_STORAGE) - parent data/methods defined by SATA bus 580*4882a593Smuzhiyun 581*4882a593Smuzhiyun flash (UCLASS_FLASH_STORAGE) - no parent data/methods (not on a bus) 582*4882a593Smuzhiyun 583*4882a593SmuzhiyunAbove you can see that the driver for xhci_usb/sata controls the child's 584*4882a593Smuzhiyunbus methods. In the third example the device is not on a bus, and therefore 585*4882a593Smuzhiyunwill not have these methods at all. Consider the case where the flash 586*4882a593Smuzhiyundevice defines child methods. These would be used for *its* children, and 587*4882a593Smuzhiyunwould be quite separate from the methods defined by the driver for the bus 588*4882a593Smuzhiyunthat the flash device is connetced to. The act of attaching a device to a 589*4882a593Smuzhiyunparent device which is a bus, causes the device to start behaving like a 590*4882a593Smuzhiyunbus device, regardless of its own views on the matter. 591*4882a593Smuzhiyun 592*4882a593SmuzhiyunThe uclass for the device can also contain data private to that uclass. 593*4882a593SmuzhiyunBut note that each device on the bus may be a memeber of a different 594*4882a593Smuzhiyunuclass, and this data has nothing to do with the child data for each child 595*4882a593Smuzhiyunon the bus. It is the bus' uclass that controls the child with respect to 596*4882a593Smuzhiyunthe bus. 597*4882a593Smuzhiyun 598*4882a593Smuzhiyun 599*4882a593SmuzhiyunDriver Lifecycle 600*4882a593Smuzhiyun---------------- 601*4882a593Smuzhiyun 602*4882a593SmuzhiyunHere are the stages that a device goes through in driver model. Note that all 603*4882a593Smuzhiyunmethods mentioned here are optional - e.g. if there is no probe() method for 604*4882a593Smuzhiyuna device then it will not be called. A simple device may have very few 605*4882a593Smuzhiyunmethods actually defined. 606*4882a593Smuzhiyun 607*4882a593Smuzhiyun1. Bind stage 608*4882a593Smuzhiyun 609*4882a593SmuzhiyunU-Boot discovers devices using one of these two methods: 610*4882a593Smuzhiyun 611*4882a593Smuzhiyun - Scan the U_BOOT_DEVICE() definitions. U-Boot looks up the name specified 612*4882a593Smuzhiyunby each, to find the appropriate U_BOOT_DRIVER() definition. In this case, 613*4882a593Smuzhiyunthere is no path by which driver_data may be provided, but the U_BOOT_DEVICE() 614*4882a593Smuzhiyunmay provide platdata. 615*4882a593Smuzhiyun 616*4882a593Smuzhiyun - Scan through the device tree definitions. U-Boot looks at top-level 617*4882a593Smuzhiyunnodes in the the device tree. It looks at the compatible string in each node 618*4882a593Smuzhiyunand uses the of_match table of the U_BOOT_DRIVER() structure to find the 619*4882a593Smuzhiyunright driver for each node. In this case, the of_match table may provide a 620*4882a593Smuzhiyundriver_data value, but platdata cannot be provided until later. 621*4882a593Smuzhiyun 622*4882a593SmuzhiyunFor each device that is discovered, U-Boot then calls device_bind() to create a 623*4882a593Smuzhiyunnew device, initializes various core fields of the device object such as name, 624*4882a593Smuzhiyunuclass & driver, initializes any optional fields of the device object that are 625*4882a593Smuzhiyunapplicable such as of_offset, driver_data & platdata, and finally calls the 626*4882a593Smuzhiyundriver's bind() method if one is defined. 627*4882a593Smuzhiyun 628*4882a593SmuzhiyunAt this point all the devices are known, and bound to their drivers. There 629*4882a593Smuzhiyunis a 'struct udevice' allocated for all devices. However, nothing has been 630*4882a593Smuzhiyunactivated (except for the root device). Each bound device that was created 631*4882a593Smuzhiyunfrom a U_BOOT_DEVICE() declaration will hold the platdata pointer specified 632*4882a593Smuzhiyunin that declaration. For a bound device created from the device tree, 633*4882a593Smuzhiyunplatdata will be NULL, but of_offset will be the offset of the device tree 634*4882a593Smuzhiyunnode that caused the device to be created. The uclass is set correctly for 635*4882a593Smuzhiyunthe device. 636*4882a593Smuzhiyun 637*4882a593SmuzhiyunThe device's bind() method is permitted to perform simple actions, but 638*4882a593Smuzhiyunshould not scan the device tree node, not initialise hardware, nor set up 639*4882a593Smuzhiyunstructures or allocate memory. All of these tasks should be left for 640*4882a593Smuzhiyunthe probe() method. 641*4882a593Smuzhiyun 642*4882a593SmuzhiyunNote that compared to Linux, U-Boot's driver model has a separate step of 643*4882a593Smuzhiyunprobe/remove which is independent of bind/unbind. This is partly because in 644*4882a593SmuzhiyunU-Boot it may be expensive to probe devices and we don't want to do it until 645*4882a593Smuzhiyunthey are needed, or perhaps until after relocation. 646*4882a593Smuzhiyun 647*4882a593Smuzhiyun2. Activation/probe 648*4882a593Smuzhiyun 649*4882a593SmuzhiyunWhen a device needs to be used, U-Boot activates it, by following these 650*4882a593Smuzhiyunsteps (see device_probe()): 651*4882a593Smuzhiyun 652*4882a593Smuzhiyun a. If priv_auto_alloc_size is non-zero, then the device-private space 653*4882a593Smuzhiyun is allocated for the device and zeroed. It will be accessible as 654*4882a593Smuzhiyun dev->priv. The driver can put anything it likes in there, but should use 655*4882a593Smuzhiyun it for run-time information, not platform data (which should be static 656*4882a593Smuzhiyun and known before the device is probed). 657*4882a593Smuzhiyun 658*4882a593Smuzhiyun b. If platdata_auto_alloc_size is non-zero, then the platform data space 659*4882a593Smuzhiyun is allocated. This is only useful for device tree operation, since 660*4882a593Smuzhiyun otherwise you would have to specific the platform data in the 661*4882a593Smuzhiyun U_BOOT_DEVICE() declaration. The space is allocated for the device and 662*4882a593Smuzhiyun zeroed. It will be accessible as dev->platdata. 663*4882a593Smuzhiyun 664*4882a593Smuzhiyun c. If the device's uclass specifies a non-zero per_device_auto_alloc_size, 665*4882a593Smuzhiyun then this space is allocated and zeroed also. It is allocated for and 666*4882a593Smuzhiyun stored in the device, but it is uclass data. owned by the uclass driver. 667*4882a593Smuzhiyun It is possible for the device to access it. 668*4882a593Smuzhiyun 669*4882a593Smuzhiyun d. If the device's immediate parent specifies a per_child_auto_alloc_size 670*4882a593Smuzhiyun then this space is allocated. This is intended for use by the parent 671*4882a593Smuzhiyun device to keep track of things related to the child. For example a USB 672*4882a593Smuzhiyun flash stick attached to a USB host controller would likely use this 673*4882a593Smuzhiyun space. The controller can hold information about the USB state of each 674*4882a593Smuzhiyun of its children. 675*4882a593Smuzhiyun 676*4882a593Smuzhiyun e. All parent devices are probed. It is not possible to activate a device 677*4882a593Smuzhiyun unless its predecessors (all the way up to the root device) are activated. 678*4882a593Smuzhiyun This means (for example) that an I2C driver will require that its bus 679*4882a593Smuzhiyun be activated. 680*4882a593Smuzhiyun 681*4882a593Smuzhiyun f. The device's sequence number is assigned, either the requested one 682*4882a593Smuzhiyun (assuming no conflicts) or the next available one if there is a conflict 683*4882a593Smuzhiyun or nothing particular is requested. 684*4882a593Smuzhiyun 685*4882a593Smuzhiyun g. If the driver provides an ofdata_to_platdata() method, then this is 686*4882a593Smuzhiyun called to convert the device tree data into platform data. This should 687*4882a593Smuzhiyun do various calls like fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), ...) 688*4882a593Smuzhiyun to access the node and store the resulting information into dev->platdata. 689*4882a593Smuzhiyun After this point, the device works the same way whether it was bound 690*4882a593Smuzhiyun using a device tree node or U_BOOT_DEVICE() structure. In either case, 691*4882a593Smuzhiyun the platform data is now stored in the platdata structure. Typically you 692*4882a593Smuzhiyun will use the platdata_auto_alloc_size feature to specify the size of the 693*4882a593Smuzhiyun platform data structure, and U-Boot will automatically allocate and zero 694*4882a593Smuzhiyun it for you before entry to ofdata_to_platdata(). But if not, you can 695*4882a593Smuzhiyun allocate it yourself in ofdata_to_platdata(). Note that it is preferable 696*4882a593Smuzhiyun to do all the device tree decoding in ofdata_to_platdata() rather than 697*4882a593Smuzhiyun in probe(). (Apart from the ugliness of mixing configuration and run-time 698*4882a593Smuzhiyun data, one day it is possible that U-Boot will cache platformat data for 699*4882a593Smuzhiyun devices which are regularly de/activated). 700*4882a593Smuzhiyun 701*4882a593Smuzhiyun h. The device's probe() method is called. This should do anything that 702*4882a593Smuzhiyun is required by the device to get it going. This could include checking 703*4882a593Smuzhiyun that the hardware is actually present, setting up clocks for the 704*4882a593Smuzhiyun hardware and setting up hardware registers to initial values. The code 705*4882a593Smuzhiyun in probe() can access: 706*4882a593Smuzhiyun 707*4882a593Smuzhiyun - platform data in dev->platdata (for configuration) 708*4882a593Smuzhiyun - private data in dev->priv (for run-time state) 709*4882a593Smuzhiyun - uclass data in dev->uclass_priv (for things the uclass stores 710*4882a593Smuzhiyun about this device) 711*4882a593Smuzhiyun 712*4882a593Smuzhiyun Note: If you don't use priv_auto_alloc_size then you will need to 713*4882a593Smuzhiyun allocate the priv space here yourself. The same applies also to 714*4882a593Smuzhiyun platdata_auto_alloc_size. Remember to free them in the remove() method. 715*4882a593Smuzhiyun 716*4882a593Smuzhiyun i. The device is marked 'activated' 717*4882a593Smuzhiyun 718*4882a593Smuzhiyun j. The uclass's post_probe() method is called, if one exists. This may 719*4882a593Smuzhiyun cause the uclass to do some housekeeping to record the device as 720*4882a593Smuzhiyun activated and 'known' by the uclass. 721*4882a593Smuzhiyun 722*4882a593Smuzhiyun3. Running stage 723*4882a593Smuzhiyun 724*4882a593SmuzhiyunThe device is now activated and can be used. From now until it is removed 725*4882a593Smuzhiyunall of the above structures are accessible. The device appears in the 726*4882a593Smuzhiyunuclass's list of devices (so if the device is in UCLASS_GPIO it will appear 727*4882a593Smuzhiyunas a device in the GPIO uclass). This is the 'running' state of the device. 728*4882a593Smuzhiyun 729*4882a593Smuzhiyun4. Removal stage 730*4882a593Smuzhiyun 731*4882a593SmuzhiyunWhen the device is no-longer required, you can call device_remove() to 732*4882a593Smuzhiyunremove it. This performs the probe steps in reverse: 733*4882a593Smuzhiyun 734*4882a593Smuzhiyun a. The uclass's pre_remove() method is called, if one exists. This may 735*4882a593Smuzhiyun cause the uclass to do some housekeeping to record the device as 736*4882a593Smuzhiyun deactivated and no-longer 'known' by the uclass. 737*4882a593Smuzhiyun 738*4882a593Smuzhiyun b. All the device's children are removed. It is not permitted to have 739*4882a593Smuzhiyun an active child device with a non-active parent. This means that 740*4882a593Smuzhiyun device_remove() is called for all the children recursively at this point. 741*4882a593Smuzhiyun 742*4882a593Smuzhiyun c. The device's remove() method is called. At this stage nothing has been 743*4882a593Smuzhiyun deallocated so platform data, private data and the uclass data will all 744*4882a593Smuzhiyun still be present. This is where the hardware can be shut down. It is 745*4882a593Smuzhiyun intended that the device be completely inactive at this point, For U-Boot 746*4882a593Smuzhiyun to be sure that no hardware is running, it should be enough to remove 747*4882a593Smuzhiyun all devices. 748*4882a593Smuzhiyun 749*4882a593Smuzhiyun d. The device memory is freed (platform data, private data, uclass data, 750*4882a593Smuzhiyun parent data). 751*4882a593Smuzhiyun 752*4882a593Smuzhiyun Note: Because the platform data for a U_BOOT_DEVICE() is defined with a 753*4882a593Smuzhiyun static pointer, it is not de-allocated during the remove() method. For 754*4882a593Smuzhiyun a device instantiated using the device tree data, the platform data will 755*4882a593Smuzhiyun be dynamically allocated, and thus needs to be deallocated during the 756*4882a593Smuzhiyun remove() method, either: 757*4882a593Smuzhiyun 758*4882a593Smuzhiyun 1. if the platdata_auto_alloc_size is non-zero, the deallocation 759*4882a593Smuzhiyun happens automatically within the driver model core; or 760*4882a593Smuzhiyun 761*4882a593Smuzhiyun 2. when platdata_auto_alloc_size is 0, both the allocation (in probe() 762*4882a593Smuzhiyun or preferably ofdata_to_platdata()) and the deallocation in remove() 763*4882a593Smuzhiyun are the responsibility of the driver author. 764*4882a593Smuzhiyun 765*4882a593Smuzhiyun e. The device sequence number is set to -1, meaning that it no longer 766*4882a593Smuzhiyun has an allocated sequence. If the device is later reactivated and that 767*4882a593Smuzhiyun sequence number is still free, it may well receive the name sequence 768*4882a593Smuzhiyun number again. But from this point, the sequence number previously used 769*4882a593Smuzhiyun by this device will no longer exist (think of SPI bus 2 being removed 770*4882a593Smuzhiyun and bus 2 is no longer available for use). 771*4882a593Smuzhiyun 772*4882a593Smuzhiyun f. The device is marked inactive. Note that it is still bound, so the 773*4882a593Smuzhiyun device structure itself is not freed at this point. Should the device be 774*4882a593Smuzhiyun activated again, then the cycle starts again at step 2 above. 775*4882a593Smuzhiyun 776*4882a593Smuzhiyun5. Unbind stage 777*4882a593Smuzhiyun 778*4882a593SmuzhiyunThe device is unbound. This is the step that actually destroys the device. 779*4882a593SmuzhiyunIf a parent has children these will be destroyed first. After this point 780*4882a593Smuzhiyunthe device does not exist and its memory has be deallocated. 781*4882a593Smuzhiyun 782*4882a593Smuzhiyun 783*4882a593SmuzhiyunData Structures 784*4882a593Smuzhiyun--------------- 785*4882a593Smuzhiyun 786*4882a593SmuzhiyunDriver model uses a doubly-linked list as the basic data structure. Some 787*4882a593Smuzhiyunnodes have several lists running through them. Creating a more efficient 788*4882a593Smuzhiyundata structure might be worthwhile in some rare cases, once we understand 789*4882a593Smuzhiyunwhat the bottlenecks are. 790*4882a593Smuzhiyun 791*4882a593Smuzhiyun 792*4882a593SmuzhiyunChanges since v1 793*4882a593Smuzhiyun---------------- 794*4882a593Smuzhiyun 795*4882a593SmuzhiyunFor the record, this implementation uses a very similar approach to the 796*4882a593Smuzhiyunoriginal patches, but makes at least the following changes: 797*4882a593Smuzhiyun 798*4882a593Smuzhiyun- Tried to aggressively remove boilerplate, so that for most drivers there 799*4882a593Smuzhiyunis little or no 'driver model' code to write. 800*4882a593Smuzhiyun- Moved some data from code into data structure - e.g. store a pointer to 801*4882a593Smuzhiyunthe driver operations structure in the driver, rather than passing it 802*4882a593Smuzhiyunto the driver bind function. 803*4882a593Smuzhiyun- Rename some structures to make them more similar to Linux (struct udevice 804*4882a593Smuzhiyuninstead of struct instance, struct platdata, etc.) 805*4882a593Smuzhiyun- Change the name 'core' to 'uclass', meaning U-Boot class. It seems that 806*4882a593Smuzhiyunthis concept relates to a class of drivers (or a subsystem). We shouldn't 807*4882a593Smuzhiyunuse 'class' since it is a C++ reserved word, so U-Boot class (uclass) seems 808*4882a593Smuzhiyunbetter than 'core'. 809*4882a593Smuzhiyun- Remove 'struct driver_instance' and just use a single 'struct udevice'. 810*4882a593SmuzhiyunThis removes a level of indirection that doesn't seem necessary. 811*4882a593Smuzhiyun- Built in device tree support, to avoid the need for platdata 812*4882a593Smuzhiyun- Removed the concept of driver relocation, and just make it possible for 813*4882a593Smuzhiyunthe new driver (created after relocation) to access the old driver data. 814*4882a593SmuzhiyunI feel that relocation is a very special case and will only apply to a few 815*4882a593Smuzhiyundrivers, many of which can/will just re-init anyway. So the overhead of 816*4882a593Smuzhiyundealing with this might not be worth it. 817*4882a593Smuzhiyun- Implemented a GPIO system, trying to keep it simple 818*4882a593Smuzhiyun 819*4882a593Smuzhiyun 820*4882a593SmuzhiyunPre-Relocation Support 821*4882a593Smuzhiyun---------------------- 822*4882a593Smuzhiyun 823*4882a593SmuzhiyunFor pre-relocation we simply call the driver model init function. Only 824*4882a593Smuzhiyundrivers marked with DM_FLAG_PRE_RELOC or the device tree 825*4882a593Smuzhiyun'u-boot,dm-pre-reloc' flag are initialised prior to relocation. This helps 826*4882a593Smuzhiyunto reduce the driver model overhead. 827*4882a593Smuzhiyun 828*4882a593SmuzhiyunIt is possible to limit this to specific relocation steps, by using 829*4882a593Smuzhiyunthe more specialized 'u-boot,dm-spl' and 'u-boot,dm-tpl' flags 830*4882a593Smuzhiyunin the devicetree. 831*4882a593Smuzhiyun 832*4882a593SmuzhiyunThen post relocation we throw that away and re-init driver model again. 833*4882a593SmuzhiyunFor drivers which require some sort of continuity between pre- and 834*4882a593Smuzhiyunpost-relocation devices, we can provide access to the pre-relocation 835*4882a593Smuzhiyundevice pointers, but this is not currently implemented (the root device 836*4882a593Smuzhiyunpointer is saved but not made available through the driver model API). 837*4882a593Smuzhiyun 838*4882a593Smuzhiyun 839*4882a593SmuzhiyunSPL Support 840*4882a593Smuzhiyun----------- 841*4882a593Smuzhiyun 842*4882a593SmuzhiyunDriver model can operate in SPL. Its efficient implementation and small code 843*4882a593Smuzhiyunsize provide for a small overhead which is acceptable for all but the most 844*4882a593Smuzhiyunconstrained systems. 845*4882a593Smuzhiyun 846*4882a593SmuzhiyunTo enable driver model in SPL, define CONFIG_SPL_DM. You might want to 847*4882a593Smuzhiyunconsider the following option also. See the main README for more details. 848*4882a593Smuzhiyun 849*4882a593Smuzhiyun - CONFIG_SYS_MALLOC_SIMPLE 850*4882a593Smuzhiyun - CONFIG_DM_WARN 851*4882a593Smuzhiyun - CONFIG_DM_DEVICE_REMOVE 852*4882a593Smuzhiyun - CONFIG_DM_STDIO 853*4882a593Smuzhiyun 854*4882a593Smuzhiyun 855*4882a593SmuzhiyunEnabling Driver Model 856*4882a593Smuzhiyun--------------------- 857*4882a593Smuzhiyun 858*4882a593SmuzhiyunDriver model is being brought into U-Boot gradually. As each subsystems gets 859*4882a593Smuzhiyunsupport, a uclass is created and a CONFIG to enable use of driver model for 860*4882a593Smuzhiyunthat subsystem. 861*4882a593Smuzhiyun 862*4882a593SmuzhiyunFor example CONFIG_DM_SERIAL enables driver model for serial. With that 863*4882a593Smuzhiyundefined, the old serial support is not enabled, and your serial driver must 864*4882a593Smuzhiyunconform to driver model. With that undefined, the old serial support is 865*4882a593Smuzhiyunenabled and driver model is not available for serial. This means that when 866*4882a593Smuzhiyunyou convert a driver, you must either convert all its boards, or provide for 867*4882a593Smuzhiyunthe driver to be compiled both with and without driver model (generally this 868*4882a593Smuzhiyunis not very hard). 869*4882a593Smuzhiyun 870*4882a593SmuzhiyunSee the main README for full details of the available driver model CONFIG 871*4882a593Smuzhiyunoptions. 872*4882a593Smuzhiyun 873*4882a593Smuzhiyun 874*4882a593SmuzhiyunThings to punt for later 875*4882a593Smuzhiyun------------------------ 876*4882a593Smuzhiyun 877*4882a593SmuzhiyunUclasses are statically numbered at compile time. It would be possible to 878*4882a593Smuzhiyunchange this to dynamic numbering, but then we would require some sort of 879*4882a593Smuzhiyunlookup service, perhaps searching by name. This is slightly less efficient 880*4882a593Smuzhiyunso has been left out for now. One small advantage of dynamic numbering might 881*4882a593Smuzhiyunbe fewer merge conflicts in uclass-id.h. 882*4882a593Smuzhiyun 883*4882a593Smuzhiyun 884*4882a593SmuzhiyunSimon Glass 885*4882a593Smuzhiyunsjg@chromium.org 886*4882a593SmuzhiyunApril 2013 887*4882a593SmuzhiyunUpdated 7-May-13 888*4882a593SmuzhiyunUpdated 14-Jun-13 889*4882a593SmuzhiyunUpdated 18-Oct-13 890*4882a593SmuzhiyunUpdated 5-Nov-13 891