]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/devices.c
* Add support for log buffer which can be passed to Linux kernel's
[karo-tx-uboot.git] / common / devices.c
1 /*
2  * (C) Copyright 2000
3  * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <config.h>
25 #include <common.h>
26 #include <stdarg.h>
27 #include <malloc.h>
28 #include <devices.h>
29 #include <i2c.h>
30
31 list_t devlist = 0;
32 device_t *stdio_devices[] = { NULL, NULL, NULL };
33 char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
34
35 #ifdef CFG_DEVICE_NULLDEV
36 void nulldev_putc(const char c)
37 {
38   /* nulldev is empty! */
39 }
40
41 void nulldev_puts(const char *s)
42 {
43   /* nulldev is empty! */
44 }
45
46 int nulldev_input(void)
47 {
48   /* nulldev is empty! */
49   return 0;
50 }
51 #endif
52
53 /**************************************************************************
54  * SYSTEM DRIVERS
55  **************************************************************************
56  */
57
58 static void drv_system_init (void)
59 {
60         device_t dev;
61
62         memset (&dev, 0, sizeof (dev));
63
64         strcpy (dev.name, "serial");
65         dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
66 #if CONFIG_SERIAL_SOFTWARE_FIFO
67         dev.putc = serial_buffered_putc;
68         dev.puts = serial_buffered_puts;
69         dev.getc = serial_buffered_getc;
70         dev.tstc = serial_buffered_tstc;
71 #else
72         dev.putc = serial_putc;
73         dev.puts = serial_puts;
74         dev.getc = serial_getc;
75         dev.tstc = serial_tstc;
76 #endif
77
78         device_register (&dev);
79
80 #ifdef CFG_DEVICE_NULLDEV
81         memset (&dev, 0, sizeof (dev));
82
83         strcpy (dev.name, "nulldev");
84         dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
85         dev.putc = nulldev_putc;
86         dev.puts = nulldev_puts;
87         dev.getc = nulldev_input;
88         dev.tstc = nulldev_input;
89
90         device_register (&dev);
91 #endif
92 }
93
94 /**************************************************************************
95  * DEVICES
96  **************************************************************************
97  */
98
99 int device_register (device_t * dev)
100 {
101         ListInsertItem (devlist, dev, LIST_END);
102         return 0;
103 }
104
105 /* deregister the device "devname".
106  * returns 0 if success, -1 if device is assigned and 1 if devname not found
107  */
108 #ifdef  CFG_DEVICE_DEREGISTER
109 int device_deregister(char *devname)
110 {
111         int i,l,dev_index;
112         device_t *dev = NULL;
113         char temp_names[3][8];
114
115         dev_index=-1;
116         for (i=1; i<=ListNumItems(devlist); i++) {
117                 dev = ListGetPtrToItem (devlist, i);
118                 if(strcmp(dev->name,devname)==0) {
119                         dev_index=i;
120                         break;
121                 }
122         }
123         if(dev_index<0) /* device not found */
124                 return 0;
125         /* get stdio devices (ListRemoveItem changes the dev list) */
126         for (l=0 ; l< MAX_FILES; l++) {
127                 if (stdio_devices[l] == dev) {
128                         /* Device is assigned -> report error */
129                         return -1;
130                 }
131                 memcpy (&temp_names[l][0],
132                         stdio_devices[l]->name,
133                         sizeof(stdio_devices[l]->name));
134         }
135         ListRemoveItem(devlist,NULL,dev_index);
136         /* reassign Device list */
137         for (i=1; i<=ListNumItems(devlist); i++) {
138                 dev = ListGetPtrToItem (devlist, i);
139                 for (l=0 ; l< MAX_FILES; l++) {
140                         if(strcmp(dev->name,temp_names[l])==0) {
141                                 stdio_devices[l] = dev;
142                         }
143                 }
144         }
145         return 0;
146 }
147 #endif  /* CFG_DEVICE_DEREGISTER */
148
149 int devices_init (void)
150 {
151         DECLARE_GLOBAL_DATA_PTR;
152
153         int i;
154         ulong relocation_offset = gd->reloc_off;
155
156         /* relocate device name pointers */
157         for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
158                 stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
159                                                 relocation_offset);
160         }
161
162         /* Initialize the list */
163         devlist = ListCreate (sizeof (device_t));
164
165         if (devlist == NULL) {
166                 eputs ("Cannot initialize the list of devices!\n");
167                 return -1;
168         }
169 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
170         i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
171 #endif
172 #ifdef CONFIG_LCD
173         drv_lcd_init ();
174 #endif
175 #ifdef CONFIG_VIDEO
176         drv_video_init ();
177 #endif
178 #ifdef CONFIG_WL_4PPM_KEYBOARD
179         drv_wlkbd_init ();
180 #endif
181 #ifdef CONFIG_LOGBUFFER
182         drv_logbuff_init ();
183 #endif
184         drv_system_init ();
185
186         gd-> flags |= GD_FLG_DEVINIT;   /* device initialization done */
187
188         return (0);
189 }
190
191 int devices_done (void)
192 {
193         ListDispose (devlist);
194
195         return 0;
196 }