Merge branch 'mpc86xx'
[platform/kernel/u-boot.git] / board / delta / delta.c
1 /*
2  * (C) Copyright 2002
3  * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
4  *
5  * (C) Copyright 2002
6  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7  * Marius Groeger <mgroeger@sysgo.de>
8  *
9  * See file CREDITS for list of people who contributed to this
10  * project.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation; either version 2 of
15  * the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25  * MA 02111-1307 USA
26  */
27
28 #include <common.h>
29 #include <i2c.h>
30 #include <da9030.h>
31 #include <malloc.h>
32 #include <command.h>
33 #include <asm/arch/pxa-regs.h>
34
35 DECLARE_GLOBAL_DATA_PTR;
36
37 /* ------------------------------------------------------------------------- */
38
39 static void init_DA9030(void);
40 static void keys_init(void);
41 static void get_pressed_keys(uchar *s);
42 static uchar *key_match(uchar *kbd_data);
43
44 /*
45  * Miscelaneous platform dependent initialisations
46  */
47
48 int board_init (void)
49 {
50         /* memory and cpu-speed are setup before relocation */
51         /* so we do _nothing_ here */
52
53         /* arch number of Lubbock-Board mk@tbd: fix this! */
54         gd->bd->bi_arch_number = MACH_TYPE_LUBBOCK;
55
56         /* adress of boot parameters */
57         gd->bd->bi_boot_params = 0xa0000100;
58
59         return 0;
60 }
61
62 int board_late_init(void)
63 {
64 #ifdef DELTA_CHECK_KEYBD
65         uchar kbd_data[KEYBD_DATALEN];
66         char keybd_env[2 * KEYBD_DATALEN + 1];
67         char *str;
68         int i;
69 #endif /* DELTA_CHECK_KEYBD */
70
71         setenv("stdout", "serial");
72         setenv("stderr", "serial");
73
74 #ifdef DELTA_CHECK_KEYBD
75         keys_init();
76
77         memset(kbd_data, '\0', KEYBD_DATALEN);
78
79         /* check for pressed keys and setup keybd_env */
80         get_pressed_keys(kbd_data);
81
82         for (i = 0; i < KEYBD_DATALEN; ++i) {
83                 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
84         }
85         setenv ("keybd", keybd_env);
86
87         str = strdup ((char *)key_match (kbd_data));    /* decode keys */
88
89 # ifdef CONFIG_PREBOOT  /* automatically configure "preboot" command on key match */
90         setenv ("preboot", str);        /* set or delete definition */
91 # endif /* CONFIG_PREBOOT */
92         if (str != NULL) {
93                 free (str);
94         }
95 #endif /* DELTA_CHECK_KEYBD */
96
97         init_DA9030();
98         return 0;
99 }
100
101
102 /*
103  * Magic Key Handling, mainly copied from board/lwmon/lwmon.c
104  */
105 #ifdef DELTA_CHECK_KEYBD
106
107 static uchar kbd_magic_prefix[] = "key_magic";
108 static uchar kbd_command_prefix[] = "key_cmd";
109
110 /*
111  * Get pressed keys
112  * s is a buffer of size KEYBD_DATALEN-1
113  */
114 static void get_pressed_keys(uchar *s)
115 {
116         unsigned long val;
117         val = GPLR3;
118
119         if(val & (1<<31))
120                 *s++ = KEYBD_KP_DKIN0;
121         if(val & (1<<18))
122                 *s++ = KEYBD_KP_DKIN1;
123         if(val & (1<<29))
124                 *s++ = KEYBD_KP_DKIN2;
125         if(val & (1<<22))
126                 *s++ = KEYBD_KP_DKIN5;
127 }
128
129 static void keys_init()
130 {
131         CKENB |= CKENB_7_GPIO;
132         udelay(100);
133
134         /* Configure GPIOs */
135         GPIO127 = 0xa840;       /* KP_DKIN0 */
136         GPIO114 = 0xa840;       /* KP_DKIN1 */
137         GPIO125 = 0xa840;       /* KP_DKIN2 */
138         GPIO118 = 0xa840;       /* KP_DKIN5 */
139
140         /* Configure GPIOs as inputs */
141         GPDR3 &= ~(1<<31 | 1<<18 | 1<<29 | 1<<22);
142         GCDR3 = (1<<31 | 1<<18 | 1<<29 | 1<<22);
143
144         udelay(100);
145 }
146
147 static int compare_magic (uchar *kbd_data, uchar *str)
148 {
149         /* uchar compare[KEYBD_DATALEN-1]; */
150         uchar compare[KEYBD_DATALEN];
151         char *nxt;
152         int i;
153
154         /* Don't include modifier byte */
155         /* memcpy (compare, kbd_data+1, KEYBD_DATALEN-1); */
156         memcpy (compare, kbd_data, KEYBD_DATALEN);
157
158         for (; str != NULL; str = (*nxt) ? (uchar *)(nxt+1) : (uchar *)nxt) {
159                 uchar c;
160                 int k;
161
162                 c = (uchar) simple_strtoul ((char *)str, (char **) (&nxt), 16);
163
164                 if (str == (uchar *)nxt) {      /* invalid character */
165                         break;
166                 }
167
168                 /*
169                  * Check if this key matches the input.
170                  * Set matches to zero, so they match only once
171                  * and we can find duplicates or extra keys
172                  */
173                 for (k = 0; k < sizeof(compare); ++k) {
174                         if (compare[k] == '\0') /* only non-zero entries */
175                                 continue;
176                         if (c == compare[k]) {  /* found matching key */
177                                 compare[k] = '\0';
178                                 break;
179                         }
180                 }
181                 if (k == sizeof(compare)) {
182                         return -1;              /* unmatched key */
183                 }
184         }
185
186         /*
187          * A full match leaves no keys in the `compare' array,
188          */
189         for (i = 0; i < sizeof(compare); ++i) {
190                 if (compare[i])
191                 {
192                         return -1;
193                 }
194         }
195
196         return 0;
197 }
198
199
200 static uchar *key_match (uchar *kbd_data)
201 {
202         char magic[sizeof (kbd_magic_prefix) + 1];
203         uchar *suffix;
204         char *kbd_magic_keys;
205
206         /*
207          * The following string defines the characters that can pe appended
208          * to "key_magic" to form the names of environment variables that
209          * hold "magic" key codes, i. e. such key codes that can cause
210          * pre-boot actions. If the string is empty (""), then only
211          * "key_magic" is checked (old behaviour); the string "125" causes
212          * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
213          */
214         if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
215                 kbd_magic_keys = "";
216
217         /* loop over all magic keys;
218          * use '\0' suffix in case of empty string
219          */
220         for (suffix=(uchar *)kbd_magic_keys; *suffix || suffix==(uchar *)kbd_magic_keys; ++suffix) {
221                 sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
222 #if 0
223                 printf ("### Check magic \"%s\"\n", magic);
224 #endif
225                 if (compare_magic(kbd_data, (uchar *)getenv(magic)) == 0) {
226                         char cmd_name[sizeof (kbd_command_prefix) + 1];
227                         char *cmd;
228
229                         sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
230
231                         cmd = getenv (cmd_name);
232 #if 0
233                         printf ("### Set PREBOOT to $(%s): \"%s\"\n",
234                                 cmd_name, cmd ? cmd : "<<NULL>>");
235 #endif
236                         *kbd_data = *suffix;
237                         return ((uchar *)cmd);
238                 }
239         }
240 #if 0
241         printf ("### Delete PREBOOT\n");
242 #endif
243         *kbd_data = '\0';
244         return (NULL);
245 }
246
247 int do_kbd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
248 {
249         uchar kbd_data[KEYBD_DATALEN];
250         char keybd_env[2 * KEYBD_DATALEN + 1];
251         int i;
252
253         /* Read keys */
254         get_pressed_keys(kbd_data);
255         puts ("Keys:");
256         for (i = 0; i < KEYBD_DATALEN; ++i) {
257                 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
258                 printf (" %02x", kbd_data[i]);
259         }
260         putc ('\n');
261         setenv ("keybd", keybd_env);
262         return 0;
263 }
264
265 U_BOOT_CMD(
266            kbd, 1,      1,      do_kbd,
267            "kbd     - read keyboard status\n",
268            NULL
269 );
270
271 #endif /* DELTA_CHECK_KEYBD */
272
273
274 int dram_init (void)
275 {
276         gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
277         gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;
278         gd->bd->bi_dram[1].start = PHYS_SDRAM_2;
279         gd->bd->bi_dram[1].size = PHYS_SDRAM_2_SIZE;
280         gd->bd->bi_dram[2].start = PHYS_SDRAM_3;
281         gd->bd->bi_dram[2].size = PHYS_SDRAM_3_SIZE;
282         gd->bd->bi_dram[3].start = PHYS_SDRAM_4;
283         gd->bd->bi_dram[3].size = PHYS_SDRAM_4_SIZE;
284
285         return 0;
286 }
287
288 void i2c_init_board()
289 {
290         CKENB |= (CKENB_4_I2C);
291
292         /* setup I2C GPIO's */
293         GPIO32 = 0x801;         /* SCL = Alt. Fkt. 1 */
294         GPIO33 = 0x801;         /* SDA = Alt. Fkt. 1 */
295 }
296
297 /* initialize the DA9030 Power Controller */
298 static void init_DA9030()
299 {
300         uchar addr = (uchar) DA9030_I2C_ADDR, val = 0;
301
302         CKENB |= CKENB_7_GPIO;
303         udelay(100);
304
305         /* Rising Edge on EXTON to reset DA9030 */
306         GPIO17 = 0x8800;        /* configure GPIO17, no pullup, -down */
307         GPDR0 |= (1<<17);       /* GPIO17 is output */
308         GSDR0 = (1<<17);
309         GPCR0 = (1<<17);        /* drive GPIO17 low */
310         GPSR0 = (1<<17);        /* drive GPIO17 high */
311
312 #if CFG_DA9030_EXTON_DELAY
313         udelay((unsigned long) CFG_DA9030_EXTON_DELAY); /* wait for DA9030 */
314 #endif
315         GPCR0 = (1<<17);        /* drive GPIO17 low */
316
317         /* reset the watchdog and go active (0xec) */
318         val = (SYS_CONTROL_A_HWRES_ENABLE |
319                (0x6<<4) |
320                SYS_CONTROL_A_WDOG_ACTION |
321                SYS_CONTROL_A_WATCHDOG);
322         if(i2c_write(addr, SYS_CONTROL_A, 1, &val, 1)) {
323                 printf("Error accessing DA9030 via i2c.\n");
324                 return;
325         }
326
327         i2c_reg_write(addr, REG_CONTROL_1_97, 0xfd); /* disable LDO1, enable LDO6 */
328         i2c_reg_write(addr, LDO2_3, 0xd1);      /* LDO2 =1,9V, LDO3=3,1V */
329         i2c_reg_write(addr, LDO4_5, 0xcc);      /* LDO2 =1,9V, LDO3=3,1V */
330         i2c_reg_write(addr, LDO6_SIMCP, 0x3e);  /* LDO6=3,2V, SIMCP = 5V support */
331         i2c_reg_write(addr, LDO7_8, 0xc9);      /* LDO7=2,7V, LDO8=3,0V */
332         i2c_reg_write(addr, LDO9_12, 0xec);     /* LDO9=3,0V, LDO12=3,2V */
333         i2c_reg_write(addr, BUCK, 0x0c);        /* Buck=1.2V */
334         i2c_reg_write(addr, REG_CONTROL_2_98, 0x7f); /* All LDO'S on 8,9,10,11,12,14 */
335         i2c_reg_write(addr, LDO_10_11, 0xcc);   /* LDO10=3.0V  LDO11=3.0V */
336         i2c_reg_write(addr, LDO_15, 0xae);      /* LDO15=1.8V, dislock first 3bit */
337         i2c_reg_write(addr, LDO_14_16, 0x05);   /* LDO14=2.8V, LDO16=NB */
338         i2c_reg_write(addr, LDO_18_19, 0x9c);   /* LDO18=3.0V, LDO19=2.7V */
339         i2c_reg_write(addr, LDO_17_SIMCP0, 0x2c); /* LDO17=3.0V, SIMCP=3V support */
340         i2c_reg_write(addr, BUCK2_DVC1, 0x9a);  /* Buck2=1.5V plus Update support of 520 MHz */
341         i2c_reg_write(addr, REG_CONTROL_2_18, 0x43); /* Ball on */
342         i2c_reg_write(addr, MISC_CONTROLB, 0x08); /* session valid enable */
343         i2c_reg_write(addr, USBPUMP, 0xc1);     /* start pump, ignore HW signals */
344
345         val = i2c_reg_read(addr, STATUS);
346         if(val & STATUS_CHDET)
347                 printf("Charger detected, turning on LED.\n");
348         else {
349                 printf("No charger detetected.\n");
350                 /* undervoltage? print error and power down */
351         }
352 }
353
354
355 #if 0
356 /* reset the DA9030 watchdog */
357 void hw_watchdog_reset(void)
358 {
359         uchar addr = (uchar) DA9030_I2C_ADDR, val = 0;
360         val = i2c_reg_read(addr, SYS_CONTROL_A);
361         val |= SYS_CONTROL_A_WATCHDOG;
362         i2c_reg_write(addr, SYS_CONTROL_A, val);
363 }
364 #endif