U-Boot通過devices_init函數創建設備鏈表,然後在devices_init函數中初始化設備並將設備添加到設備鏈表中。U-Boot使用devices_t結構體來管理設備,設備鏈表也就是devices_t結構體的鏈表。通過i2c_init、drv_lcd_init、drv_video_init、drv_keyboard_init、drv_logbuff_init、drv_system_init、serial_devices_init、drv_usbtty_init和drv_nc_init函數初始化設備(這些函數是否執行是通過宏來決定的),並通過device_register函數注冊設備。
相關閱讀:
圖解U-Boot:第一階段源碼分析 http://www.linuxidc.com/Linux/2012-03/55963.htm
圖解U-Boot:第二階段源碼分析 http://www.linuxidc.com/Linux/2012-03/55964.htm
圖解U-Boot:引導內核分析 http://www.linuxidc.com/Linux/2012-03/55965.htm
一、初始設備鏈表、初始化設備和注冊設備
U-Boot在第二階段中通過devices_init函數創建設備鏈表,初始化設備並將其注冊到設備鏈表中。該函數在common/devices.c文件中,其對應的頭文件是nclude/devices.h。
1.1 devices_init函數
- int devices_init (void)
- {
- #ifndef CONFIG_ARM /* already relocated for current ARM implementation */
- ulong relocation_offset = gd->reloc_off;
- int i;
-
- /* relocate device name pointers */
- for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
- stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
- relocation_offset);
- }
- #endif
-
- /* Initialize the list */
- devlist = ListCreate (sizeof (device_t));//創建設備列表
-
- if (devlist == NULL) {
- eputs ("Cannot initialize the list of devices!\n");
- return -1;
- }
- #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
- i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);//初始化i2c接口,i2c沒有注冊到devlist中去
- #endif
- #ifdef CONFIG_LCD
- drv_lcd_init ();
- #endif
- #if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE)
- drv_video_init ();
- #endif
- #ifdef CONFIG_KEYBOARD
- drv_keyboard_init ();
- #endif
- #ifdef CONFIG_LOGBUFFER
- drv_logbuff_init ();
- #endif
- drv_system_init ();//這裡其實是定義了一個串口設備,並且注冊到devlist中
- #ifdef CONFIG_SERIAL_MULTI
- serial_devices_init ();
- #endif
- #ifdef CONFIG_USB_TTY
- drv_usbtty_init ();
- #endif
- #ifdef CONFIG_NETCONSOLE
- drv_nc_init ();
- #endif
-
- return (0);
- }
經過devices_init(),創建了devlist,但是只有一個串口設備注冊在內。
1.2 devices結構的定義
- /* Device information */
- typedef struct {
- int flags; /* Device flags: input/output/system */
- int ext; /* Supported extensions */
- char name[16]; /* Device name 設備名稱 */
-
- /* GENERAL functions 啟動和停止函數 */
-
- int (*start) (void); /* To start the device */
- int (*stop) (void); /* To stop the device */
-
- /* OUTPUT functions 輸出函數 */
-
- void (*putc) (const char c); /* To put a char */
- void (*puts) (const char *s); /* To put a string (accelerator) */
-
- /* INPUT functions 輸入函數*/
-
- int (*tstc) (void); /* To test if a char is ready... */
- int (*getc) (void); /* To get that char */
-
- /* Other functions */
-
- void *priv; /* Private extensions */
- } device_t;
1.3 drv_system_init 函數
drv_system_init 函數初始化串口設備,源碼如下:
- static void drv_system_init (void)
- {
- device_t dev;//定義一個結構體
-
- memset (&dev, 0, sizeof (dev));//為剛剛定義的結構體分配內存
-
- strcpy (dev.name, "serial");//名稱
- dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
-
- #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
- dev.putc = serial_buffered_putc;
- dev.puts = serial_buffered_puts;
- dev.getc = serial_buffered_getc;
- dev.tstc = serial_buffered_tstc;
- #else
- dev.putc = serial_putc;
- dev.puts = serial_puts;
- dev.getc = serial_getc;
- dev.tstc = serial_tstc;
- #endif
-
- device_register (&dev);//注冊函數
-
- #ifdef CFG_DEVICE_NULLDEV
- memset (&dev, 0, sizeof (dev));
-
- strcpy (dev.name, "nulldev");
- dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
- dev.putc = nulldev_putc;
- dev.puts = nulldev_puts;
- dev.getc = nulldev_input;
- dev.tstc = nulldev_input;
-
- device_register (&dev);
- #endif
- }