UDM-design.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. The U-Boot Driver Model Project
  2. ===============================
  3. Design document
  4. ===============
  5. Marek Vasut <marek.vasut@gmail.com>
  6. Pavel Herrmann <morpheus.ibis@gmail.com>
  7. 2012-05-17
  8. I) The modular concept
  9. ----------------------
  10. The driver core design is done with modularity in mind. The long-term plan is to
  11. extend this modularity to allow loading not only drivers, but various other
  12. objects into U-Boot at runtime -- like commands, support for other boards etc.
  13. II) Driver core initialization stages
  14. -------------------------------------
  15. The drivers have to be initialized in two stages, since the U-Boot bootloader
  16. runs in two stages itself. The first stage is the one which is executed before
  17. the bootloader itself is relocated. The second stage then happens after
  18. relocation.
  19. 1) First stage
  20. --------------
  21. The first stage runs after the bootloader did very basic hardware init. This
  22. means the stack pointer was configured, caches disabled and that's about it.
  23. The problem with this part is the memory management isn't running at all. To
  24. make things even worse, at this point, the RAM is still likely uninitialized
  25. and therefore unavailable.
  26. 2) Second stage
  27. ---------------
  28. At this stage, the bootloader has initialized RAM and is running from it's
  29. final location. Dynamic memory allocations are working at this point. Most of
  30. the driver initialization is executed here.
  31. III) The drivers
  32. ----------------
  33. 1) The structure of a driver
  34. ----------------------------
  35. The driver will contain a structure located in a separate section, which
  36. will allow linker to create a list of compiled-in drivers at compile time.
  37. Let's call this list "driver_list".
  38. struct driver __attribute__((section(driver_list))) {
  39. /* The name of the driver */
  40. char name[STATIC_CONFIG_DRIVER_NAME_LENGTH];
  41. /*
  42. * This function should connect this driver with cores it depends on and
  43. * with other drivers, likely bus drivers
  44. */
  45. int (*bind)(struct instance *i);
  46. /* This function actually initializes the hardware. */
  47. int (*probe)(struct instance *i);
  48. /*
  49. * The function of the driver called when U-Boot finished relocation.
  50. * This is particularly important to eg. move pointers to DMA buffers
  51. * and such from the location before relocation to their final location.
  52. */
  53. int (*reloc)(struct instance *i);
  54. /*
  55. * This is called when the driver is shuting down, to deinitialize the
  56. * hardware.
  57. */
  58. int (*remove)(struct instance *i);
  59. /* This is called to remove the driver from the driver tree */
  60. int (*unbind)(struct instance *i);
  61. /* This is a list of cores this driver depends on */
  62. struct driver *cores[];
  63. };
  64. The cores[] array in here is very important. It allows u-boot to figure out,
  65. in compile-time, which possible cores can be activated at runtime. Therefore
  66. if there are cores that won't be ever activated, GCC LTO might remove them
  67. from the final binary. Actually, this information might be used to drive build
  68. of the cores.
  69. FIXME: Should *cores[] be really struct driver, pointing to drivers that
  70. represent the cores? Shouldn't it be core instance pointer?
  71. 2) Instantiation of a driver
  72. ----------------------------
  73. The driver is instantiated by calling:
  74. driver_bind(struct instance *bus, const struct driver_info *di)
  75. The "struct instance *bus" is a pointer to a bus with which this driver should
  76. be registered with. The "root" bus pointer is supplied to the board init
  77. functions.
  78. FIXME: We need some functions that will return list of busses of certain type
  79. registered with the system so the user can find proper instance even if
  80. he has no bus pointer (this will come handy if the user isn't
  81. registering the driver from board init function, but somewhere else).
  82. The "const struct driver_info *di" pointer points to a structure defining the
  83. driver to be registered. The structure is defined as follows:
  84. struct driver_info {
  85. char name[STATIC_CONFIG_DRIVER_NAME_LENGTH];
  86. void *platform_data;
  87. }
  88. The instantiation of a driver by calling driver_bind() creates an instance
  89. of the driver by allocating "struct driver_instance". Note that only struct
  90. instance is passed to the driver. The wrapping struct driver_instance is there
  91. for purposes of the driver core:
  92. struct driver_instance {
  93. uint32_t flags;
  94. struct instance i;
  95. };
  96. struct instance {
  97. /* Pointer to a driver information passed by driver_register() */
  98. const struct driver_info *info;
  99. /* Pointer to a bus this driver is bound with */
  100. struct instance *bus;
  101. /* Pointer to this driver's own private data */
  102. void *private_data;
  103. /* Pointer to the first block of successor nodes (optional) */
  104. struct successor_block *succ;
  105. }
  106. The instantiation of a driver does not mean the hardware is initialized. The
  107. driver_bind() call only creates the instance of the driver, fills in the "bus"
  108. pointer and calls the drivers' .bind() function. The .bind() function of the
  109. driver should hook the driver with the remaining cores and/or drivers it
  110. depends on.
  111. It's important to note here, that in case the driver instance has multiple
  112. parents, such parent can be connected with this instance by calling:
  113. driver_link(struct instance *parent, struct instance *dev);
  114. This will connect the other parent driver with the newly instantiated driver.
  115. Note that this must be called after driver_bind() and before driver_acticate()
  116. (driver_activate() will be explained below). To allow struct instance to have
  117. multiple parent pointer, the struct instance *bus will utilize it's last bit
  118. to indicate if this is a pointer to struct instance or to an array if
  119. instances, struct successor block. The approach is similar as the approach to
  120. *succ in struct instance, described in the following paragraph.
  121. The last pointer of the struct instance, the pointer to successor nodes, is
  122. used only in case of a bus driver. Otherwise the pointer contains NULL value.
  123. The last bit of this field indicates if this is a bus having a single child
  124. node (so the last bit is 0) or if this bus has multiple child nodes (the last
  125. bit is 1). In the former case, the driver core should clear the last bit and
  126. this pointer points directly to the child node. In the later case of a bus
  127. driver, the pointer points to an instance of structure:
  128. struct successor_block {
  129. /* Array of pointers to instances of devices attached to this bus */
  130. struct instance *dev[BLOCKING_FACTOR];
  131. /* Pointer to next block of successors */
  132. struct successor_block *next;
  133. }
  134. Some of the *dev[] array members might be NULL in case there are no more
  135. devices attached. The *next is NULL in case the list of attached devices
  136. doesn't continue anymore. The BLOCKING_FACTOR is used to allocate multiple
  137. slots for successor devices at once to avoid fragmentation of memory.
  138. 3) The bind() function of a driver
  139. ----------------------------------
  140. The bind function of a driver connects the driver with various cores the
  141. driver provides functions for. The driver model related part will look like
  142. the following example for a bus driver:
  143. int driver_bind(struct instance *in)
  144. {
  145. ...
  146. core_bind(&core_i2c_static_instance, in, i2c_bus_funcs);
  147. ...
  148. }
  149. FIXME: What if we need to run-time determine, depending on some hardware
  150. register, what kind of i2c_bus_funcs to pass?
  151. This makes the i2c core aware of a new bus. The i2c_bus_funcs is a constant
  152. structure of functions any i2c bus driver must provide to work. This will
  153. allow the i2c command operate with the bus. The core_i2c_static_instance is
  154. the pointer to the instance of a core this driver provides function to.
  155. FIXME: Maybe replace "core-i2c" with CORE_I2C global pointer to an instance of
  156. the core?
  157. 4) The instantiation of a core driver
  158. -------------------------------------
  159. The core driver is special in the way that it's single-instance driver. It is
  160. always present in the system, though it might not be activated. The fact that
  161. it's single instance allows it to be instantiated at compile time.
  162. Therefore, all possible structures of this driver can be in read-only memory,
  163. especially struct driver and struct driver_instance. But the successor list,
  164. which needs special treatment.
  165. To solve the problem with a successor list and the core driver flags, a new
  166. entry in struct gd (global data) will be introduced. This entry will point to
  167. runtime allocated array of struct driver_instance. It will be possible to
  168. allocate the exact amount of struct driver_instance necessary, as the number
  169. of cores that might be activated will be known at compile time. The cores will
  170. then behave like any usual driver.
  171. Pointers to the struct instance of cores can be computed at compile time,
  172. therefore allowing the resulting u-boot binary to save some overhead.
  173. 5) The probe() function of a driver
  174. -----------------------------------
  175. The probe function of a driver allocates necessary resources and does required
  176. initialization of the hardware itself. This is usually called only when the
  177. driver is needed, as a part of the defered probe mechanism.
  178. The driver core should implement a function called
  179. int driver_activate(struct instance *in);
  180. which should call the .probe() function of the driver and then configure the
  181. state of the driver instance to "ACTIVATED". This state of a driver instance
  182. should be stored in a wrap-around structure for the structure instance, the
  183. struct driver_instance.
  184. 6) The command side interface to a driver
  185. -----------------------------------------
  186. The U-Boot command shall communicate only with the specific driver core. The
  187. driver core in turn exports necessary API towards the command.
  188. 7) Demonstration imaginary board
  189. --------------------------------
  190. Consider the following computer:
  191. *
  192. |
  193. +-- System power management logic
  194. |
  195. +-- CPU clock controlling logc
  196. |
  197. +-- NAND controller
  198. | |
  199. | +-- NAND flash chip
  200. |
  201. +-- 128MB of DDR DRAM
  202. |
  203. +-- I2C bus #0
  204. | |
  205. | +-- RTC
  206. | |
  207. | +-- EEPROM #0
  208. | |
  209. | +-- EEPROM #1
  210. |
  211. +-- USB host-only IP core
  212. | |
  213. | +-- USB storage device
  214. |
  215. +-- USB OTG-capable IP core
  216. | |
  217. | +-- connection to the host PC
  218. |
  219. +-- GPIO
  220. | |
  221. | +-- User LED #0
  222. | |
  223. | +-- User LED #1
  224. |
  225. +-- UART0
  226. |
  227. +-- UART1
  228. |
  229. +-- Ethernet controller #0
  230. |
  231. +-- Ethernet controller #1
  232. |
  233. +-- Audio codec
  234. |
  235. +-- PCI bridge
  236. | |
  237. | +-- Ethernet controller #2
  238. | |
  239. | +-- SPI host card
  240. | | |
  241. | | +-- Audio amplifier (must be operational before codec)
  242. | |
  243. | +-- GPIO host card
  244. | |
  245. | +-- User LED #2
  246. |
  247. +-- LCD controller
  248. |
  249. +-- PWM controller (must be enabled after LCD controller)
  250. |
  251. +-- SPI host controller
  252. | |
  253. | +-- SD/MMC connected via SPI
  254. | |
  255. | +-- SPI flash
  256. |
  257. +-- CPLD/FPGA with stored configuration of the board