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