s5pc11x charger: avoid flickering, unexpected wakeup
[kernel/u-boot.git] / board / samsung / universal / universal.c
1 /*
2  * Copyright (C) 2009 Samsung Electronics
3  * Kyungmin Park <kyungmin.park@samsung.com>
4  * Minkyu Kang <mk7.kang@samsung.com>
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA
23  */
24
25 #include <common.h>
26 #include <i2c.h>
27 #include <malloc.h>
28 #include <asm/io.h>
29 #include <asm/arch/clk.h>
30 #include <asm/arch/clock.h>
31 #include <asm/arch/gpio.h>
32 #include <asm/arch/keypad.h>
33 #include <asm/arch/mmc.h>
34 #include <asm/arch/power.h>
35 #include <asm/arch/mem.h>
36 #include <asm/arch/hs_otg.h>
37 #include <asm/arch/regs-otg.h>
38 #include <asm/arch/rtc.h>
39 #include <asm/arch/adc.h>
40 #include <asm/errno.h>
41 #include <fbutils.h>
42 #include <lcd.h>
43 #include <bmp_layout.h>
44
45 #include "animation_frames.h"
46 #include "gpio_setting.h"
47
48 DECLARE_GLOBAL_DATA_PTR;
49
50 #define C100_MACH_START                 3000
51 #define C110_MACH_START                 3100
52
53 static unsigned int board_rev;
54 static unsigned int battery_soc;
55 static struct s5pc110_gpio *s5pc110_gpio;
56
57 enum {
58         I2C_2,
59         I2C_GPIO3,
60         I2C_PMIC,
61         I2C_GPIO5,
62         I2C_GPIO6,
63         I2C_GPIO7,
64         I2C_GPIO10,
65 };
66
67 /*
68  * i2c 2
69  * SDA: GPD1[4]
70  * SCL: GPD1[5]
71  */
72 static struct i2c_gpio_bus_data i2c_2 = {
73         .sda_pin        = 4,
74         .scl_pin        = 5,
75 };
76
77 /*
78  * i2c gpio3
79  * SDA: GPJ3[6]
80  * SCL: GPJ3[7]
81  */
82 static struct i2c_gpio_bus_data i2c_gpio3 = {
83         .sda_pin        = 6,
84         .scl_pin        = 7,
85 };
86
87 /*
88  * i2c pmic
89  * SDA: GPJ4[0]
90  * SCL: GPJ4[3]
91  */
92 static struct i2c_gpio_bus_data i2c_pmic = {
93         .sda_pin        = 0,
94         .scl_pin        = 3,
95 };
96
97 /*
98  * i2c gpio5
99  * SDA: MP05[3]
100  * SCL: MP05[2]
101  */
102 static struct i2c_gpio_bus_data i2c_gpio5 = {
103         .sda_pin        = 3,
104         .scl_pin        = 2,
105 };
106
107 /*
108  * i2c gpio6
109  * SDA: GPJ3[4]
110  * SCL: GPJ3[5]
111  */
112 static struct i2c_gpio_bus_data i2c_gpio6 = {
113         .sda_pin        = 4,
114         .scl_pin        = 5,
115 };
116
117 /*
118  * i2c gpio7 - kessler
119  * SDA: MP05[1]
120  * SCL: MP05[0]
121  */
122 static struct i2c_gpio_bus_data i2c_gpio7 = {
123         .sda_pin        = 1,
124         .scl_pin        = 0,
125 };
126
127 /*
128  * i2c gpio7 - cypress
129  * SDA: MP05[6]
130  * SCL: MP05[4]
131  */
132 static struct i2c_gpio_bus_data i2c_cypress_gpio7 = {
133         .sda_pin        = 6,
134         .scl_pin        = 4,
135 };
136
137 /*
138  * i2c gpio10
139  * SDA: GPJ3[0]
140  * SCL: GPJ3[1]
141  */
142 static struct i2c_gpio_bus_data i2c_gpio10 = {
143         .sda_pin        = 0,
144         .scl_pin        = 1,
145 };
146
147
148 static struct i2c_gpio_bus i2c_gpio[] = {
149         {
150                 .bus    = &i2c_2,
151         }, {
152                 .bus    = &i2c_gpio3,
153         }, {
154                 .bus    = &i2c_pmic,
155         }, {
156                 .bus    = &i2c_gpio5,
157         }, {
158                 .bus    = &i2c_gpio6,
159         }, {
160                 .bus    = &i2c_gpio7,
161         }, {
162                 .bus    = &i2c_gpio10,
163         },
164 };
165
166 u32 get_board_rev(void)
167 {
168         return board_rev;
169 }
170
171 static int hwrevision(int rev)
172 {
173         return (board_rev & 0xf) == rev;
174 }
175
176 enum {
177         MACH_UNIVERSAL,
178         MACH_TICKERTAPE,
179         MACH_CHANGED,
180         MACH_P1P2,      /* Don't remove it */
181         MACH_GEMINUS,
182         MACH_CYPRESS,
183
184         MACH_WMG160 = 160,
185
186         MACH_PSEUDO_END,
187
188         MACH_AQUILA     = 2646,
189         MACH_KESSLER    = 3102,
190 };
191
192 #define SPLIT_SCREEN_FEATURE    0x100
193
194 /* board is MACH_AQUILA and board is like below. */
195 #define J1_B2_BOARD             0x0200
196 #define LIMO_UNIVERSAL_BOARD    0x0400
197 #define LIMO_REAL_BOARD         0x0800
198 #define MEDIA_BOARD             0x1000
199 #define BAMBOO_BOARD            0x2000
200
201 /* board is MACH_KESSLER and board is like below */
202 #define ARIES_BOARD             0x4000
203 #define NEPTUNE_BOARD           0x8000
204
205 #define BOARD_MASK              0xFF00
206
207 static int c110_machine_id(void)
208 {
209         return gd->bd->bi_arch_number - C110_MACH_START;
210 }
211
212 static int machine_is_aquila(void)
213 {
214         return (gd->bd->bi_arch_number == MACH_AQUILA);
215 }
216
217 static int machine_is_tickertape(void)
218 {
219         return c110_machine_id() == MACH_TICKERTAPE;
220 }
221
222 static int machine_is_geminus(void)
223 {
224         return c110_machine_id() == MACH_GEMINUS;
225 }
226
227 static int machine_is_cypress(void)
228 {
229         return c110_machine_id() == MACH_CYPRESS;
230 }
231
232 static int board_is_limo_universal(void)
233 {
234         return machine_is_aquila() && (board_rev & LIMO_UNIVERSAL_BOARD);
235 }
236
237 static int board_is_limo_real(void)
238 {
239         return machine_is_aquila() && (board_rev & LIMO_REAL_BOARD);
240 }
241
242 static int board_is_media(void)
243 {
244         return machine_is_aquila() && (board_rev & MEDIA_BOARD);
245 }
246
247 static int board_is_j1b2(void)
248 {
249         return machine_is_aquila() && (board_rev & J1_B2_BOARD);
250 }
251
252 /* Kessler */
253 static int machine_is_kessler(void)
254 {
255         return gd->bd->bi_arch_number == MACH_KESSLER;
256 }
257
258 static int board_is_neptune(void)
259 {
260         return machine_is_kessler() && (board_rev & NEPTUNE_BOARD);
261 }
262
263 /* DLNA Dongle */
264 static int machine_is_wmg160(void)
265 {
266         return c110_machine_id() == MACH_WMG160;
267 }
268
269 static void check_battery(int mode);
270
271 void i2c_init_board(void)
272 {
273         struct s5pc110_gpio *gpio = (struct s5pc110_gpio *)S5PC110_GPIO_BASE;
274         int num_bus;
275
276         if (cpu_is_s5pc100())
277                 return;
278
279         num_bus = ARRAY_SIZE(i2c_gpio);
280
281         if (machine_is_aquila()) {
282                 i2c_gpio[I2C_GPIO6].bus->gpio_base = 0;
283                 i2c_gpio[I2C_GPIO7].bus->gpio_base = 0;
284         } else if (machine_is_kessler()) {
285                 i2c_gpio[I2C_GPIO7].bus->gpio_base =
286                         (unsigned int)&gpio->gpio_mp0_5;
287         } else if (machine_is_cypress()) {
288                 i2c_gpio[I2C_GPIO7].bus = &i2c_cypress_gpio7;
289                 i2c_gpio[I2C_GPIO7].bus->gpio_base =
290                         (unsigned int)&gpio->gpio_mp0_5;
291         } else {
292                 i2c_gpio[I2C_GPIO7].bus->gpio_base = 0;
293         }
294
295         i2c_gpio[I2C_2].bus->gpio_base = (unsigned int)&gpio->gpio_d1;
296         i2c_gpio[I2C_GPIO3].bus->gpio_base = (unsigned int)&gpio->gpio_j3;
297         i2c_gpio[I2C_PMIC].bus->gpio_base = (unsigned int)&gpio->gpio_j4;
298         i2c_gpio[I2C_GPIO5].bus->gpio_base = (unsigned int)&gpio->gpio_mp0_5;
299         i2c_gpio[I2C_GPIO6].bus->gpio_base = (unsigned int)&gpio->gpio_j3;
300
301         i2c_gpio_init(i2c_gpio, num_bus, I2C_PMIC);
302
303         /* Reset on max17040 early */
304         if (battery_soc == 0)
305                 check_battery(1);
306 }
307
308 #ifdef CONFIG_MISC_INIT_R
309 #define DEV_INFO_LEN            512
310 static char device_info[DEV_INFO_LEN];
311 static int display_info;
312
313 static void empty_device_info_buffer(void)
314 {
315         memset(device_info, 0x0, DEV_INFO_LEN);
316 }
317
318 static void dprintf(const char *fmt, ...)
319 {
320         va_list args;
321         uint i;
322         char buf[128];
323
324         va_start(args, fmt);
325         i = vsprintf(buf, fmt, args);
326         va_end(args);
327
328         buf[127] = 0;
329
330         if ((strlen(device_info) + strlen(buf)) > (DEV_INFO_LEN - 1)) {
331                 puts("Flushing device info...\n");
332                 puts(device_info);
333                 empty_device_info_buffer();
334         }
335         strcat(device_info, buf);
336         puts(buf);
337 }
338
339 #ifdef CONFIG_S5PC1XXFB
340 static void display_device_info(void)
341 {
342         if (!display_info)
343                 return;
344
345         init_font();
346         set_font_xy(0, 450);
347         set_font_color(FONT_WHITE);
348         fb_printf(device_info);
349         exit_font();
350
351         memset(device_info, 0x0, DEV_INFO_LEN);
352
353         udelay(5 * 1000 * 1000);
354 }
355 #endif
356
357 static const char *board_name[] = {
358         "Universal",
359         "TickerTape",
360         "Kessler",
361         "P1P2",         /* Don't remove it */
362         "Geminus",
363         "Cypress",
364         "Neptune",
365 };
366
367 enum {
368         MEM_4G1G1G,
369         MEM_4G2G1G,
370         MEM_4G3G1G,
371         MEM_4G4G1G,
372 };
373
374 static char feature_buffer[32];
375
376 static char *display_features(int board, int board_rev)
377 {
378         int count = 0;
379         char *buf = feature_buffer;
380         char *name = NULL;
381
382         if (board == MACH_AQUILA) {
383                 if (board_rev & SPLIT_SCREEN_FEATURE)
384                         name = "SplitScreen";
385                 if (board_rev & J1_B2_BOARD)
386                         name = "J1 B2";
387                 /* Limo Real or Universal */
388                 if (board_rev & LIMO_REAL_BOARD)
389                         name = "Limo Real";
390                 else if (board_rev & LIMO_UNIVERSAL_BOARD)
391                         name = "Limo Universal";
392                 if (board_rev & MEDIA_BOARD)
393                         name = "Media";
394                 if (board_rev & BAMBOO_BOARD)
395                         name = "Bamboo";
396         } else if (board == MACH_KESSLER) {
397                 if (board_rev & ARIES_BOARD)
398                         name = "Aries";
399                 if (board_rev & NEPTUNE_BOARD)
400                         name = "Neptune";
401         }
402         if (name)
403                 count += sprintf(buf + count, " - %s", name);
404
405         return buf;
406 }
407
408 static char *get_board_name(int board)
409 {
410         if (board == MACH_AQUILA)
411                 return "Aquila";
412         else if (board == MACH_KESSLER)
413                 return "Kessler";
414         else if (board == MACH_WMG160)
415                 return "WMG160";
416         return (char *) board_name[board];
417 }
418
419 static void check_board_revision(int board, int rev)
420 {
421         if (board == MACH_AQUILA) {
422                 /* Limo Real or Universal */
423                 if (rev & LIMO_UNIVERSAL_BOARD)
424                         board_rev &= ~J1_B2_BOARD;
425                 if (rev & LIMO_REAL_BOARD)
426                         board_rev &= ~(J1_B2_BOARD |
427                                         LIMO_UNIVERSAL_BOARD);
428                 if (rev & MEDIA_BOARD)
429                         board_rev &= ~(J1_B2_BOARD |
430                                         LIMO_UNIVERSAL_BOARD);
431                 if (rev & BAMBOO_BOARD)
432                         board_rev &= ~(J1_B2_BOARD |
433                                         LIMO_UNIVERSAL_BOARD |
434                                         LIMO_REAL_BOARD |
435                                         MEDIA_BOARD);
436         } else if (board == MACH_KESSLER) {
437                 if (rev & ARIES_BOARD)
438                         board_rev &= ~(J1_B2_BOARD |
439                                         LIMO_UNIVERSAL_BOARD);
440                 if (rev & NEPTUNE_BOARD)
441                         board_rev &= ~(J1_B2_BOARD |
442                                         LIMO_UNIVERSAL_BOARD);
443         } else
444                 board_rev &= ~BOARD_MASK;
445 }
446
447 static unsigned int get_hw_revision(struct s5pc1xx_gpio_bank *bank, int hwrev3)
448 {
449         unsigned int rev;
450         int mode3 = 1;
451
452         if (hwrev3)
453                 mode3 = 7;
454
455         gpio_direction_input(bank, 2);
456         gpio_direction_input(bank, 3);
457         gpio_direction_input(bank, 4);
458         gpio_direction_input(bank, mode3);
459
460         gpio_set_pull(bank, 2, GPIO_PULL_NONE);         /* HWREV_MODE0 */
461         gpio_set_pull(bank, 3, GPIO_PULL_NONE);         /* HWREV_MODE1 */
462         gpio_set_pull(bank, 4, GPIO_PULL_NONE);         /* HWREV_MODE2 */
463         gpio_set_pull(bank, mode3, GPIO_PULL_NONE);     /* HWREV_MODE3 */
464
465         rev = gpio_get_value(bank, 2);
466         rev |= (gpio_get_value(bank, 3) << 1);
467         rev |= (gpio_get_value(bank, 4) << 2);
468         rev |= (gpio_get_value(bank, mode3) << 3);
469
470         return rev;
471 }
472
473 static void check_hw_revision(void)
474 {
475         unsigned int board = MACH_UNIVERSAL;    /* Default is Universal */
476
477         if (cpu_is_s5pc100()) {
478                 struct s5pc100_gpio *gpio =
479                         (struct s5pc100_gpio *)S5PC100_GPIO_BASE;
480
481                 board_rev = get_hw_revision(&gpio->gpio_j0, 0);
482
483                 /* C100 TickerTape */
484                 if (board_rev == 3)
485                         board = MACH_TICKERTAPE;
486         } else {
487                 struct s5pc110_gpio *gpio =
488                         (struct s5pc110_gpio *)S5PC110_GPIO_BASE;
489                 int hwrev3 = 0;
490
491                 board_rev = 0;
492
493                 /*
494                  * Note Check 'Aquila' board first
495                  *
496                  * TT: TickerTape
497                  * SS: SplitScreen
498                  * LRA: Limo Real Aquila
499                  * LUA: Limo Universal Aquila
500                  * OA: Old Aquila
501                  * CYP: Cypress
502                  * BB: Bamboo
503                  *
504                  * ADDR = 0xE0200000 + OFF
505                  *
506                  *       OFF    Universal BB   LRA  LUA  OA   TT   SS        CYP
507                  *   J1: 0x0264 0x10      0x10 0x00 0x00 0x00 0x00 0x00     
508                  *   J2: 0x0284           0x01 0x10 0x00 
509                  *   H1: 0x0C24    W           0x28 0xA8 0x1C                0x0F
510                  *   H3: 0x0C64                0x03 0x07 0x0F               
511                  *   D1: 0x00C4 0x0F           0x3F 0x3F 0x0F 0xXC 0x3F
512                  *    I: 0x0224                          0x02 0x00 0x08
513                  * MP03: 0x0324                          0x9x      0xbx 0x9x
514                  * MP05: 0x0364                          0x80      0x88
515                  */
516
517                 /* C110 Aquila */
518                 if (gpio_get_value(&gpio->gpio_j1, 4) == 0) {
519                         board = MACH_AQUILA;
520                         board_rev |= J1_B2_BOARD;
521
522                         gpio_set_pull(&gpio->gpio_j2, 6, GPIO_PULL_NONE);
523                         gpio_direction_input(&gpio->gpio_j2, 6);
524
525                         /* Check board */
526                         if (gpio_get_value(&gpio->gpio_h1, 2) == 0)
527                                 board_rev |= LIMO_UNIVERSAL_BOARD;
528
529                         if (gpio_get_value(&gpio->gpio_h3, 2) == 0)
530                                 board_rev |= LIMO_REAL_BOARD;
531
532                         if (gpio_get_value(&gpio->gpio_j2, 6) == 1)
533                                 board_rev |= MEDIA_BOARD;
534
535                         /* set gpio to default value. */
536                         gpio_set_pull(&gpio->gpio_j2, 6, GPIO_PULL_DOWN);
537                         gpio_direction_output(&gpio->gpio_j2, 6, 0);
538                 }
539
540                 /* Workaround: C110 Aquila Rev0.6 */
541                 if (board_rev == 6) {
542                         board = MACH_AQUILA;
543                         board_rev |= LIMO_REAL_BOARD;
544                 }
545
546                 /* C110 Aquila Bamboo */
547                 if (gpio_get_value(&gpio->gpio_j2, 0) == 1) {
548                         board = MACH_AQUILA;
549                         board_rev |= BAMBOO_BOARD;
550                 }
551
552                 /* C110 TickerTape */
553                 if (gpio_get_value(&gpio->gpio_d1, 0) == 0 &&
554                                 gpio_get_value(&gpio->gpio_d1, 1) == 0)
555                         board = MACH_TICKERTAPE;
556
557                 /* WMG160 - GPH3[0:4] = 0x00 */
558                 if (board == MACH_TICKERTAPE) {
559                         int i, wmg160 = 1;
560
561                         for (i = 0; i < 4; i++) {
562                                 if (gpio_get_value(&gpio->gpio_h3, i) != 0) {
563                                         wmg160 = 0;
564                                         break;
565                                 }
566                         }
567                         if (wmg160) {
568                                 board = MACH_WMG160;
569                                 hwrev3 = 1;
570                         }
571                 }
572
573                 /* C110 Geminus for rev0.0 */
574                 gpio_set_pull(&gpio->gpio_j1, 2, GPIO_PULL_NONE);
575                 gpio_direction_input(&gpio->gpio_j1, 2);
576                 if (gpio_get_value(&gpio->gpio_j1, 2) == 1) {
577                         board = MACH_GEMINUS;
578                         if ((board_rev & ~BOARD_MASK) == 3)
579                                 board_rev &= ~0xff;
580                 }
581                 gpio_set_pull(&gpio->gpio_j1, 2, GPIO_PULL_DOWN);
582                 gpio_direction_output(&gpio->gpio_j1, 2, 0);
583
584                 /* C110 Geminus for rev0.1 ~ */
585                 gpio_set_pull(&gpio->gpio_j0, 6, GPIO_PULL_NONE);
586                 gpio_direction_input(&gpio->gpio_j0, 6);
587                 if (gpio_get_value(&gpio->gpio_j0, 6) == 1) {
588                         board = MACH_GEMINUS;
589                         hwrev3 = 1;
590                 }
591                 gpio_set_pull(&gpio->gpio_j0, 6, GPIO_PULL_DOWN);
592
593                 /* Kessler MP0_5[6] == 1 */
594                 gpio_direction_input(&gpio->gpio_mp0_5, 6);
595                 if (gpio_get_value(&gpio->gpio_mp0_5, 6) == 1) {
596                         /* Cypress: Do this for cypress */
597                         gpio_set_pull(&gpio->gpio_j2, 2, GPIO_PULL_NONE);
598                         gpio_direction_input(&gpio->gpio_j2, 2);
599                         if (gpio_get_value(&gpio->gpio_j2, 2) == 1) {
600                                 board = MACH_CYPRESS;
601                                 gpio_direction_output(&gpio->gpio_mp0_5, 6, 0);
602                         } else {
603                                 board = MACH_KESSLER;
604                                 board_rev |= ARIES_BOARD;
605
606                                 /* Neptune MP0_5[4] == 1 */
607                                 gpio_direction_input(&gpio->gpio_mp0_5, 4);
608                                 if (gpio_get_value(&gpio->gpio_mp0_5, 4) == 1) {
609                                         board_rev &= ~ARIES_BOARD;
610                                         board_rev |= NEPTUNE_BOARD;
611                                 }
612                         }
613                         gpio_set_pull(&gpio->gpio_j2, 2, GPIO_PULL_DOWN);
614                         hwrev3 = 1;
615                 } else
616                         gpio_direction_output(&gpio->gpio_mp0_5, 6, 0);
617
618                 board_rev |= get_hw_revision(&gpio->gpio_j0, hwrev3);
619         }
620
621         /* Set machine id */
622         if (board < MACH_PSEUDO_END) {
623                 if (cpu_is_s5pc110())
624                         gd->bd->bi_arch_number = C110_MACH_START + board;
625                 else
626                         gd->bd->bi_arch_number = C100_MACH_START + board;
627         } else
628                 gd->bd->bi_arch_number = board;
629
630         /* Architecture Common settings */
631         if (cpu_is_s5pc110()) {
632                 setenv("mtdparts", MTDPARTS_DEFAULT_4KB);
633         } else {
634                 setenv("bootk", "onenand read 0x30007FC0 0x60000 0x300000; "
635                                 "bootm 0x30007FC0");
636                 setenv("updatek", "onenand erase 0x60000 0x300000; "
637                                   "onenand write 0x31008000 0x60000 0x300000");
638         }
639 }
640
641 static void show_hw_revision(void)
642 {
643         int board;
644
645         /*
646          * Workaround for Rev 0.3 + CP Ver ES 3.1
647          * it's Rev 0.4
648          */
649         if (board_is_limo_real()) {
650                 if (hwrevision(0)) {
651                         /* default is Rev 0.4 */
652                         board_rev &= ~0xf;
653                         board_rev |= 0x4;
654                 }
655         }
656
657         if (machine_is_kessler() || machine_is_aquila())
658                 board = gd->bd->bi_arch_number;
659         else if (cpu_is_s5pc110())
660                 board = gd->bd->bi_arch_number - C110_MACH_START;
661         else
662                 board = gd->bd->bi_arch_number - C100_MACH_START;
663
664
665         check_board_revision(board, board_rev);
666
667         /* Set CPU Revision */
668         if (machine_is_aquila()) {
669                 if (board_is_limo_real()) {
670                         if ((board_rev & 0xf) < 8)
671                                 s5pc1xx_set_cpu_rev(0);
672                 }
673         } else if (machine_is_kessler())
674                 s5pc1xx_set_cpu_rev(1);
675         else if (machine_is_geminus()) {
676                 if ((board_rev & 0xf) < 1)
677                         s5pc1xx_set_cpu_rev(0);
678         } else if (machine_is_cypress())
679                 s5pc1xx_set_cpu_rev(1);
680         else
681                 s5pc1xx_set_cpu_rev(0);
682
683         dprintf("HW Revision:\t%x (%s%s) %s\n",
684                 board_rev, get_board_name(board),
685                 display_features(board, board_rev),
686                 s5pc1xx_get_cpu_rev() ? "" : "EVT0");
687 }
688
689 static void check_auto_burn(void)
690 {
691         unsigned long magic_base = CONFIG_SYS_SDRAM_BASE + 0x02000000;
692         unsigned int count = 0;
693         char buf[64];
694
695         if (readl(magic_base) == 0x426f6f74) {  /* ASICC: Boot */
696                 printf("Auto burning bootloader\n");
697                 count += sprintf(buf + count, "run updateb; ");
698         }
699         if (readl(magic_base + 0x04) == 0x4b65726e) {   /* ASICC: Kern */
700                 printf("Auto burning kernel\n");
701                 count += sprintf(buf + count, "run updatek; ");
702         }
703
704         if (count) {
705                 count += sprintf(buf + count, "reset");
706                 setenv("bootcmd", buf);
707         }
708
709         /* Clear the magic value */
710         writel(0xa5a55a5a, magic_base);
711         writel(0xa5a55a5a, magic_base + 0x4);
712 }
713
714 static void pmic_pin_init(void)
715 {
716         unsigned int reg, value;
717
718         if (cpu_is_s5pc100())
719                 return;
720
721         /* AP_PS_HOLD: XEINT_0: GPH0[0]
722          * Note: Don't use GPIO PS_HOLD it doesn't work
723          */
724         reg = S5PC110_PS_HOLD_CONTROL;
725         value = readl(reg);
726         value |= S5PC110_PS_HOLD_DIR_OUTPUT |
727                 S5PC110_PS_HOLD_DATA_HIGH |
728                 S5PC110_PS_HOLD_OUT_EN;
729         writel(value, reg);
730
731         /* nPOWER: XEINT_22: GPH2[6] interrupt mode */
732         gpio_cfg_pin(&s5pc110_gpio->gpio_h2, 6, GPIO_IRQ);
733         gpio_set_pull(&s5pc110_gpio->gpio_h2, 6, GPIO_PULL_UP);
734 }
735
736 static void enable_ldos(void)
737 {
738         if (cpu_is_s5pc100())
739                 return;
740
741         /* TOUCH_EN: XMMC3DATA_3: GPG3[6] output high */
742         gpio_direction_output(&s5pc110_gpio->gpio_g3, 6, 1);
743 }
744
745 static void enable_t_flash(void)
746 {
747         if (!(board_is_limo_universal() || board_is_limo_real()))
748                 return;
749
750         /* T_FLASH_EN : XM0ADDR_13: MP0_5[4] output high */
751         gpio_direction_output(&s5pc110_gpio->gpio_mp0_5, 4, 1);
752 }
753
754 static void setup_limo_real_gpios(void)
755 {
756         if (!board_is_limo_real())
757                 return;
758
759         /*
760          * Note: Please write GPIO alphabet order
761          */
762         /* CODEC_LDO_EN: XVVSYNC_LDI: GPF3[4] output high */
763         gpio_direction_output(&s5pc110_gpio->gpio_f3, 4, 1);
764
765         if (hwrevision(0))
766                 /* RESET_REQ_N: XM0BEN_1: MP0_2[1] output high */
767                 gpio_direction_output(&s5pc110_gpio->gpio_mp0_2, 1, 1);
768         else
769                 /* RESET_REQ_N: XM0CSn_2: MP0_1[2] output high */
770                 gpio_direction_output(&s5pc110_gpio->gpio_mp0_1, 2, 1);
771
772         /* T_FLASH_DETECT: EINT28: GPH3[4] interrupt mode */
773         gpio_cfg_pin(&s5pc110_gpio->gpio_h3, 4, GPIO_IRQ);
774         gpio_set_pull(&s5pc110_gpio->gpio_h3, 4, GPIO_PULL_UP);
775 }
776
777 static void setup_media_gpios(void)
778 {
779         if (!board_is_media())
780                 return;
781
782         /*
783          * Note: Please write GPIO alphabet order
784          */
785         /* RESET_REQ_N: XM0CSn_2: MP0_1[2] output high */
786         gpio_direction_output(&s5pc110_gpio->gpio_mp0_1, 2, 1);
787
788         /* T_FLASH_DETECT: EINT28: GPH3[4] interrupt mode */
789         gpio_cfg_pin(&s5pc110_gpio->gpio_h3, 4, GPIO_IRQ);
790         gpio_set_pull(&s5pc110_gpio->gpio_h3, 4, GPIO_PULL_UP);
791 }
792
793 #define KBR3            (1 << 3)
794 #define KBR2            (1 << 2)
795 #define KBR1            (1 << 1)
796 #define KBR0            (1 << 0)
797
798 static void check_keypad(void)
799 {
800         unsigned int reg, value;
801         unsigned int col_num, row_num;
802         unsigned int col_mask;
803         unsigned int col_mask_shift;
804         unsigned int row_state[4];
805         unsigned int i;
806         unsigned int auto_download = 0;
807
808         if (machine_is_wmg160())
809                 return;
810
811         if (cpu_is_s5pc100()) {
812                 struct s5pc100_gpio *gpio =
813                         (struct s5pc100_gpio *)S5PC100_GPIO_BASE;
814
815                 row_num = 3;
816                 col_num = 3;
817
818                 /* Set GPH2[2:0] to KP_COL[2:0] */
819                 gpio_cfg_pin(&gpio->gpio_h2, 0, 0x3);
820                 gpio_cfg_pin(&gpio->gpio_h2, 1, 0x3);
821                 gpio_cfg_pin(&gpio->gpio_h2, 2, 0x3);
822
823                 /* Set GPH3[2:0] to KP_ROW[2:0] */
824                 gpio_cfg_pin(&gpio->gpio_h3, 0, 0x3);
825                 gpio_cfg_pin(&gpio->gpio_h3, 1, 0x3);
826                 gpio_cfg_pin(&gpio->gpio_h3, 2, 0x3);
827
828                 reg = S5PC100_KEYPAD_BASE;
829                 col_mask = S5PC1XX_KEYIFCOL_MASK;
830                 col_mask_shift = 0;
831         } else {
832                 if (board_is_limo_real() || board_is_limo_universal()) {
833                         row_num = 2;
834                         col_num = 3;
835                 } else {
836                         row_num = 4;
837                         col_num = 4;
838                 }
839
840                 for (i = 0; i < row_num; i++) {
841                         /* Set GPH3[3:0] to KP_ROW[3:0] */
842                         gpio_cfg_pin(&s5pc110_gpio->gpio_h3, i, 0x3);
843                         gpio_set_pull(&s5pc110_gpio->gpio_h3, i, GPIO_PULL_UP);
844                 }
845
846                 for (i = 0; i < col_num; i++)
847                         /* Set GPH2[3:0] to KP_COL[3:0] */
848                         gpio_cfg_pin(&s5pc110_gpio->gpio_h2, i, 0x3);
849
850                 reg = S5PC110_KEYPAD_BASE;
851                 col_mask = S5PC110_KEYIFCOLEN_MASK;
852                 col_mask_shift = 8;
853         }
854
855         /* KEYIFCOL reg clear */
856         writel(0, reg + S5PC1XX_KEYIFCOL_OFFSET);
857
858         /* key_scan */
859         for (i = 0; i < col_num; i++) {
860                 value = col_mask;
861                 value &= ~(1 << i) << col_mask_shift;
862
863                 writel(value, reg + S5PC1XX_KEYIFCOL_OFFSET);
864                 udelay(1000);
865
866                 value = readl(reg + S5PC1XX_KEYIFROW_OFFSET);
867                 row_state[i] = ~value & ((1 << row_num) - 1);
868                 if (row_state[i])
869                         printf("[%d col] row_state: 0x%x\n", i, row_state[i]);
870         }
871
872         /* KEYIFCOL reg clear */
873         writel(0, reg + S5PC1XX_KEYIFCOL_OFFSET);
874
875         if (machine_is_aquila() || machine_is_kessler()) {
876                 /* cam full shot & volume down */
877                 if ((row_state[0] & 0x1) && (row_state[1] & 0x2))
878                         auto_download = 1;
879                 /* volume down */
880                 else if ((row_state[1] & 0x2))
881                         display_info = 1;
882         } else if (machine_is_geminus())
883                 /* volume down & home */
884                 if ((row_state[1] & 0x2) && (row_state[2] & 0x1))
885                         auto_download = 1;
886
887         if (auto_download)
888                 setenv("bootcmd", "usbdown");
889 }
890
891 static void check_battery(int mode)
892 {
893         unsigned char val[2];
894         unsigned char addr = 0x36;      /* max17040 fuel gauge */
895
896         i2c_set_bus_num(I2C_GPIO3);
897
898         if (machine_is_aquila()) {
899                 if (board_is_j1b2())
900                         return;
901         } else if (machine_is_kessler())
902                 i2c_set_bus_num(I2C_GPIO7);
903         else if (machine_is_cypress()) {
904                 i2c_set_bus_num(I2C_GPIO7);
905         } else if (machine_is_geminus()) {
906                 if (hwrevision(1))
907                         i2c_set_bus_num(I2C_GPIO7);
908         } else
909                 return;
910
911         if (i2c_probe(addr)) {
912                 printf("Can't found max17040 fuel gauge\n");
913                 return;
914         }
915
916         /* mode 0: check mode / 1: enable mode */
917         if (mode) {
918                 val[0] = 0x54;
919                 val[1] = 0x00;
920                 i2c_write(addr, 0xfe, 1, val, 2);
921         } else {
922                 i2c_read(addr, 0x04, 1, val, 1);
923                 dprintf("battery:\t%d%%\n", val[0]);
924                 battery_soc = val[0];
925         }
926 }
927
928 static void check_mhl(void)
929 {
930         unsigned char val[2];
931         unsigned char addr = 0x39;      /* SIL9230 */
932
933         /* MHL Power enable */
934         /* HDMI_EN : GPJ2[2] XMSMDATA_2 output mode */
935         gpio_direction_output(&s5pc110_gpio->gpio_j2, 2, 1);
936
937         /* MHL_RST : MP0_4[7] XM0ADDR_7 output mode */
938         gpio_direction_output(&s5pc110_gpio->gpio_mp0_4, 7, 0);
939
940         /* 10ms required after reset */
941         udelay(10000);
942
943         /* output enable */
944         gpio_set_value(&s5pc110_gpio->gpio_mp0_4, 7, 1);
945
946         i2c_set_bus_num(I2C_GPIO5);
947
948         /* set usb path */
949         if (i2c_probe(addr)) {
950                 printf("Can't found MHL Chip\n");
951                 return;
952         }
953
954         /*
955          * System Control #1
956          * set to Normal operation
957          */
958         val[0] = 0x35;
959         i2c_write((0x72 >> 1), 0x08, 1, val, 1);
960         i2c_read((0x72 >> 1), 0x08, 1, val, 1);
961
962         /*
963          * MHL TX Control #1
964          * TERM_MODE [7:6]
965          * 00 = MHL termination ON
966          * 11 = MHL termination OFF
967          */
968         val[0] = 0xd0;
969         i2c_write((0x72 >> 1), 0xa0, 1, val, 1);
970         i2c_read((0x72 >> 1), 0xa0, 1, val, 1);
971 }
972
973 #define CHARGER_ANIMATION_FRAME         6
974 static void max8998_clear_interrupt(void)
975 {
976         unsigned char addr, val[2];
977         i2c_set_bus_num(I2C_PMIC);
978         addr = 0xCC >> 1;
979         if (i2c_probe(addr)) {
980                 printf("Can't found max8998\n");
981                 return;
982         }
983
984         i2c_read(addr, 0x00, 1, val, 1);
985         i2c_read(addr, 0x01, 1, val, 1);
986         i2c_read(addr, 0x02, 1, val, 1);
987         i2c_read(addr, 0x03, 1, val, 1);
988 }
989
990 static int max8998_power_key(void)
991 {
992         unsigned char addr, val[2];
993         i2c_set_bus_num(I2C_PMIC);
994         addr = 0xCC >> 1;
995         if (i2c_probe(addr)) {
996                 printf("Can't found max8998\n");
997                 return 0;
998         }
999
1000         /* Accessing IRQ1 register */
1001         i2c_read(addr, 0x00, 1, val, 1);
1002         if (val[0] & (1 << 6))
1003                 return 1;
1004
1005         return 0;
1006 }
1007
1008 static int max8998_has_ext_power_source(void)
1009 {
1010         unsigned char addr, val[2];
1011         i2c_set_bus_num(I2C_PMIC);
1012         addr = 0xCC >> 1;
1013         if (i2c_probe(addr)) {
1014                 printf("Can't found max8998\n");
1015                 return 0;
1016         }
1017
1018         /* Accessing STATUS2 register */
1019         i2c_read(addr, 0x09, 1, val, 1);
1020         if (val[0] & (1 << 5))
1021                 return 1;
1022
1023         return 0;
1024 }
1025
1026 struct thermister_stat {
1027         short centigrade;
1028         unsigned short adc;
1029 };
1030
1031 static struct thermister_stat adc_to_temperature_data[] = {
1032         { .centigrade = -20,    .adc = 1856, },
1033         { .centigrade = -15,    .adc = 1799, },
1034         { .centigrade = -10,    .adc = 1730, },
1035         { .centigrade = -5,     .adc = 1649, },
1036         { .centigrade = 0,      .adc = 1556, },
1037         { .centigrade = 5,      .adc = 1454, },
1038         { .centigrade = 10,     .adc = 1343, },
1039         { .centigrade = 15,     .adc = 1227, },
1040         { .centigrade = 20,     .adc = 1109, },
1041         { .centigrade = 25,     .adc = 992, },
1042         { .centigrade = 30,     .adc = 880, },
1043         { .centigrade = 35,     .adc = 773, },
1044         { .centigrade = 40,     .adc = 675, },
1045         { .centigrade = 45,     .adc = 586, },
1046         { .centigrade = 50,     .adc = 507, },
1047         { .centigrade = 55,     .adc = 436, },
1048         { .centigrade = 58,     .adc = 399, },
1049         { .centigrade = 63,     .adc = 343, },
1050         { .centigrade = 65,     .adc = 322, },
1051 };
1052
1053 #ifndef USHRT_MAX
1054 #define USHRT_MAX       0xFFFFU
1055 #endif
1056 static int adc_to_temperature_centigrade(unsigned short adc)
1057 {
1058         int i;
1059         int approximation;
1060         /* low_*: Greatest Lower Bound,
1061          *          *          *          * high_*: Smallest Upper Bound */
1062         int low_temp, high_temp;
1063         unsigned short low_adc = 0, high_adc = USHRT_MAX;
1064         for (i = 0; i < ARRAY_SIZE(adc_to_temperature_data); i++) {
1065                 if (adc_to_temperature_data[i].adc <= adc &&
1066                                 adc_to_temperature_data[i].adc >= low_adc) {
1067                         low_temp = adc_to_temperature_data[i].centigrade;
1068                         low_adc = adc_to_temperature_data[i].adc;
1069                 }
1070                 if (adc_to_temperature_data[i].adc >= adc &&
1071                                 adc_to_temperature_data[i].adc <= high_adc) {
1072                         high_temp = adc_to_temperature_data[i].centigrade;
1073                         high_adc = adc_to_temperature_data[i].adc;
1074                 }
1075         }
1076
1077         /* Linear approximation between cloest low and high,
1078          * which is the weighted average of the two. */
1079
1080         /* The following equation is correct only when the two are different */
1081         if (low_adc == high_adc)
1082                 return low_temp;
1083         if (ARRAY_SIZE(adc_to_temperature_data) < 2)
1084                 return 20; /* The room temperature */
1085         if (low_adc == 0)
1086                 return high_temp;
1087         if (high_adc == USHRT_MAX)
1088                 return low_temp;
1089         approximation = low_temp * (adc - low_adc) +
1090                 high_temp * (high_adc - adc);
1091         approximation /= high_adc - low_adc;
1092         return approximation;
1093 }
1094
1095 static unsigned short get_adc_value(int channel)
1096 {
1097         struct s5pc110_adc *adc = (struct s5pc110_adc *) S5PC110_ADC_BASE;
1098         unsigned short ret = 0;
1099         unsigned int reg;
1100         int ldonum = 8;
1101         char buf[64];
1102         unsigned int loop = 0;
1103
1104         if (machine_is_kessler())
1105                 ldonum = 4;
1106         else if (machine_is_geminus())
1107                 ldonum = 4;
1108         else if (machine_is_wmg160())
1109                 ldonum = 4;
1110         else if (machine_is_cypress())
1111                 ldonum = 8;
1112         else if (machine_is_tickertape())
1113                 ldonum = 8;
1114         else if (machine_is_aquila())
1115                 ldonum = 8;
1116         /*
1117         else if (machine_is_p1p2())
1118                 ldonum = 4;
1119         */
1120
1121         sprintf(buf, "pmic ldo %d on", ldonum);
1122         run_command(buf, 0);
1123         writel(channel & 0xF, &adc->adcmux);
1124         writel((1 << 14) | (49 << 6), &adc->adccon);
1125         writel(1000 & 0xffff, &adc->adcdly);
1126         writel(readl(&adc->adccon) | (1 << 16), &adc->adccon); /* 12 bit */
1127         udelay(10);
1128         writel(readl(&adc->adccon) | (1 << 0), &adc->adccon); /* Enable */
1129         udelay(10);
1130         do {
1131                 udelay(1);
1132                 reg = readl(&adc->adccon);
1133         } while (!reg & (1 << 15) && loop++ < 1000);
1134         ret = readl(&adc->adcdat0) & 0xFFF;
1135         sprintf(buf, "pmic ldo %d off", ldonum);
1136         run_command(buf, 0);
1137
1138         return ret;
1139 }
1140
1141 static int adc_get_average_ambient_temperature(void)
1142 {
1143         if (machine_is_kessler()) {
1144                 unsigned short min = USHRT_MAX;
1145                 unsigned short max = 0;
1146                 unsigned int sum = 0;
1147                 unsigned int measured = 0;
1148                 int i;
1149
1150                 for (i = 0; i < 7; i++) {
1151                         unsigned short measurement = get_adc_value(6);
1152                         sum += measurement;
1153                         measured ++;
1154                         if (min > measurement)
1155                                 min = measurement;
1156                         if (max < measurement)
1157                                 max = measurement;
1158                 }
1159                 if (measured >= 3) {
1160                         measured -= 2;
1161                         sum -= min;
1162                         sum -= max;
1163                 }
1164                 sum /= measured;
1165                 printf("Average Ambient Temperature = %d(ADC=%d)\n",
1166                                 adc_to_temperature_centigrade(sum), sum);
1167                 return adc_to_temperature_centigrade(sum);
1168         }
1169         else
1170                 return 20; /* 20 Centigrade */
1171 }
1172
1173 enum temperature_level {
1174         _TEMP_OK,
1175         _TEMP_OK_HIGH,
1176         _TEMP_OK_LOW,
1177         _TEMP_TOO_HIGH,
1178         _TEMP_TOO_LOW,
1179 };
1180
1181 static enum temperature_level temperature_check(void)
1182 {
1183         int temp = adc_get_average_ambient_temperature();
1184         if (temp < -5)
1185                 return _TEMP_TOO_LOW;
1186         if (temp < 0)
1187                 return _TEMP_OK_LOW;
1188         if (temp > 63)
1189                 return _TEMP_TOO_HIGH;
1190         if (temp > 58)
1191                 return _TEMP_OK_HIGH;
1192         return _TEMP_OK;
1193 }
1194
1195 extern void lcd_display_clear(void);
1196 extern int lcd_display_bitmap(ulong bmp_image, int x, int y);
1197
1198 static void charger_en(int enable)
1199 {
1200         /* 0: disable
1201          * 600: 600mA
1202          * 475: 475mA
1203          */
1204         unsigned char addr = 0xCC >> 1; /* max8998 */
1205         unsigned char val[2];
1206
1207         i2c_set_bus_num(I2C_PMIC);
1208         if (i2c_probe(addr)) {
1209                 printf("Can't found max8998 (%s:%d)\n", __func__, __LINE__);
1210                 return;
1211         }
1212
1213         if (!enable) {
1214                 printf("Disable the charger.\n");
1215                 i2c_read(addr, 0x0D, 1, val, 1);
1216                 val[0] &= ~(0x1);
1217                 val[0] |= 0x1;
1218                 i2c_write(addr, 0x0D, 1, val, 1);
1219         } else {
1220                 i2c_read(addr, 0x0C, 1, val, 1);
1221                 val[0] &= ~(0x7 << 0);
1222                 val[0] &= ~(0x7 << 5);
1223                 if (enable == 600) {
1224                         val[0] |= 5; /* 600mA */
1225                         val[0] |= (3 << 5); /* Stop at 150mA (25%) */
1226                 } else { /* Assume 475 mA */
1227                         enable = 475;
1228                         val[0] |= 2; /* 475mA */
1229                         val[0] |= (4 << 5); /* Stop at 142.5mA (30%) */
1230                 }
1231                 i2c_write(addr, 0x0C, 1, val, 1);
1232
1233                 i2c_read(addr, 0x0D, 1, val, 1);
1234                 val[0] &= ~(0x1);
1235                 i2c_write(addr, 0x0D, 1, val, 1);
1236                 printf("Enable the charger @ %dmA\n", enable);
1237         }
1238 }
1239
1240 void lcd_power_on(unsigned int onoff);
1241
1242 static void into_charge_mode(void)
1243 {
1244         unsigned int level;
1245         int i, j;
1246         bmp_image_t *bmp;
1247         unsigned long len;
1248         ulong bmp_addr[CHARGER_ANIMATION_FRAME];
1249         unsigned int reg, wakeup_stat;
1250         int charger_speed = 600;
1251
1252         max8998_clear_interrupt();
1253
1254         printf("Charge Mode\n");
1255         charger_en(charger_speed);
1256
1257 #ifdef CONFIG_S5PC1XXFB
1258         init_font();
1259
1260         /* TODO: write the image-text for the charger */
1261
1262         level = battery_soc * CHARGER_ANIMATION_FRAME / 100;
1263         if (level >= CHARGER_ANIMATION_FRAME)
1264                 level = CHARGER_ANIMATION_FRAME - 1;
1265
1266         for (i = 0; i < CHARGER_ANIMATION_FRAME; i++)
1267                 bmp_addr[i] = (ulong)battery_charging_animation[i];
1268
1269         lcd_display_clear();
1270         for (i = 0; i < 3; i++) {
1271                 for (j = level; j < CHARGER_ANIMATION_FRAME; j++) {
1272                         int k;
1273
1274                         bmp = gunzip_bmp(bmp_addr[j], &len);
1275                         lcd_display_bitmap((ulong) bmp, 140, 202);
1276                         free(bmp);
1277
1278                         for (k = 0; k < 10; k++)
1279                                 if (max8998_power_key()) {
1280                                         lcd_display_clear();
1281                                         goto restore_screen;
1282                                 } else if (!max8998_has_ext_power_source()) {
1283                                         lcd_display_clear();
1284                                         goto restore_screen;
1285                                 } else
1286                                         udelay(100 * 1000);
1287                 }
1288         }
1289         exit_font();
1290 #endif
1291         /* Disable the display to prevent flickering */
1292         /* TODO: how to reenable the display later? */
1293         lcd_power_on(0);
1294
1295         do {
1296                 struct s5pc110_rtc *rtc = (struct s5pc110_rtc *) S5PC110_RTC_BASE;
1297                 unsigned int org, org_ip3, org_tcfg0;
1298                 enum temperature_level  previous_state = _TEMP_OK;
1299
1300                 empty_device_info_buffer();
1301                 if (max8998_power_key())
1302                         break;
1303                 else if (!max8998_has_ext_power_source())
1304                         break;
1305
1306                 /* Enable RTC, SYSTIMER, ADC at CLKGATE IP3 */
1307                 org_ip3 = readl(0xE010046C);
1308                 writel(org_ip3 | (1 << 15) | (1 << 16) | (1 << 24), 0xE010046C);
1309
1310                 reg = org = readl(&rtc->rtccon);
1311                 reg |= (1 << 0);
1312                 writel(reg, &rtc->rtccon);
1313
1314                 reg = readl(&rtc->rtccon);
1315                 writel(reg | (1 << 3), &rtc->rtccon);
1316                 udelay(10);
1317                 writel(reg & ~(1 << 3), &rtc->rtccon);
1318                 udelay(10);
1319
1320                 reg = readl(&rtc->rtccon);
1321                 reg &= ~( (1 << 8) | (0xF << 4));
1322                 reg |= (1 << 8) | (0xD << 4); /* D: 4 Hz, 9: 64 Hz */
1323                 writel(reg, &rtc->rtccon);
1324
1325                 reg = 15 * 4 - 1; /* 15 sec */
1326                 writel(reg, &rtc->ticcnt);
1327
1328                 /* EVT0: sleep 1, EVT1: sleep */
1329                 if (cpu_is_s5pc110()) {
1330                         char *name = "dummy";
1331                         char *usage = "N/A";
1332                         char *help = NULL;
1333                         cmd_tbl_t ctt;
1334                         ctt.name = name;
1335                         ctt.usage = usage;
1336                         ctt.help = help;
1337
1338                         if (s5pc1xx_get_cpu_rev() == 0) {
1339                                 char *argv[] = {"1", "1"};
1340                                 wakeup_stat = do_sleep(&ctt, 0, 2, argv);
1341                         } else {
1342                                 char *argv[] = {"0", "0"};
1343                                 wakeup_stat = do_sleep(&ctt, 0, 1, argv);
1344                         }
1345                 } else {
1346                         printf("\n\n\nERROR: this is not S5PC110.\n\n\n");
1347                         return;
1348                 }
1349
1350                 /* Check TEMP HIGH/LOW */
1351                 switch (temperature_check()) {
1352                 case _TEMP_OK:
1353                         charger_en(charger_speed);
1354                         previous_state = _TEMP_OK;
1355                         break;
1356                 case _TEMP_TOO_LOW:
1357                         charger_en(0);
1358                         previous_state = _TEMP_TOO_LOW;
1359                         break;
1360                 case _TEMP_TOO_HIGH:
1361                         charger_en(0);
1362                         previous_state = _TEMP_TOO_HIGH;
1363                         break;
1364                 case _TEMP_OK_LOW:
1365                         if (previous_state == _TEMP_TOO_LOW)
1366                                 charger_en(0);
1367                         else
1368                         {
1369                                 charger_en(charger_speed);
1370                                 previous_state = _TEMP_OK;
1371                         }
1372                         break;
1373                 case _TEMP_OK_HIGH:
1374                         if (previous_state == _TEMP_TOO_HIGH)
1375                                 charger_en(0);
1376                         else
1377                         {
1378                                 charger_en(charger_speed);
1379                                 previous_state = _TEMP_OK;
1380                         }
1381                         break;
1382                 }
1383
1384                 writel(org, &rtc->rtccon);
1385                 writel(org_tcfg0, 0xE2600000);
1386                 writel(org_ip3, 0xE010046C);
1387
1388         } while (wakeup_stat == 0x04); /* RTC TICK */
1389
1390 restore_screen:
1391         /* TODO: Reenable logo display (not working yet) */
1392         lcd_power_on(1);
1393         drv_lcd_init_resume();
1394 }
1395
1396 static void check_micro_usb(int intr)
1397 {
1398         unsigned char addr;
1399         unsigned char val[2];
1400         static int started_charging_once = 0;
1401         char *path;
1402
1403         if (cpu_is_s5pc100())
1404                 return;
1405
1406         if (board_is_limo_real()) {
1407                 if (hwrevision(0) || hwrevision(1))
1408                         return;
1409         }
1410
1411         i2c_set_bus_num(I2C_PMIC);
1412
1413         if (machine_is_kessler())
1414                 i2c_set_bus_num(I2C_GPIO6);
1415         else if (machine_is_cypress()) {
1416                 i2c_set_bus_num(I2C_GPIO6);
1417         } else if (machine_is_geminus()) {
1418                 if (hwrevision(1))
1419                         i2c_set_bus_num(I2C_GPIO6);
1420         } else if (machine_is_wmg160())
1421                 i2c_set_bus_num(I2C_GPIO6);
1422
1423         addr = 0x25;            /* fsa9480 */
1424         if (i2c_probe(addr)) {
1425                 printf("Can't found fsa9480\n");
1426                 return;
1427         }
1428
1429         /* Clear Interrupt */
1430         if (intr) {
1431                 i2c_read(addr, 0x03, 1, val, 2);
1432                 udelay(500 * 1000);
1433         }
1434
1435         /* Read Device Type 1 */
1436         i2c_read(addr, 0x0a, 1, val, 1);
1437
1438 #define FSA_DEV1_CHARGER        (1 << 6)
1439 #define FSA_DEV1_UART           (1 << 3)
1440 #define FSA_DEV1_USB            (1 << 2)
1441 #define FSA_DEV2_JIG_USB_OFF    (1 << 1)
1442 #define FSA_DEV2_JIG_USB_ON     (1 << 0)
1443
1444         /*
1445          * If USB, use default 475mA
1446          * If Charger, use 600mA and go to charge mode
1447          */
1448         if ((val[0] & FSA_DEV1_CHARGER) && !started_charging_once) {
1449                 started_charging_once = 1;
1450
1451                 /* If it's full, do not charge. */
1452                 if (battery_soc < 100)
1453                         into_charge_mode();
1454                 else
1455                         charger_en(0);
1456         }
1457         else if (val[0] & FSA_DEV1_USB) {
1458                 if (battery_soc < 100)
1459                         charger_en(475); /* enable charger and keep booting */
1460                 else
1461                         charger_en(0);
1462         }
1463
1464         /* If Factory Mode is Boot ON-USB, go to download mode */
1465         i2c_read(addr, 0x07, 1, val, 1);
1466
1467 #define FSA_ADC_FAC_USB_OFF     0x18
1468 #define FSA_ADC_FAC_USB_ON      0x19
1469 #define FSA_ADC_FAC_UART        0x1d
1470
1471         if (val[0] == FSA_ADC_FAC_USB_ON || val[0] == FSA_ADC_FAC_USB_OFF)
1472                 setenv("bootcmd", "usbdown");
1473
1474         path = getenv("usb");
1475
1476         if (!strncmp(path, "cp", 2))
1477                 run_command("microusb cp", 0);
1478 }
1479
1480 static void micro_usb_switch(int path)
1481 {
1482         unsigned char addr;
1483         unsigned char val[2];
1484
1485         i2c_set_bus_num(I2C_PMIC);
1486
1487         if (machine_is_kessler())
1488                 i2c_set_bus_num(I2C_GPIO6);
1489         else if (machine_is_cypress()) {
1490                 i2c_set_bus_num(I2C_GPIO6);
1491         } else if (machine_is_geminus()) {
1492                 if (hwrevision(1))
1493                         i2c_set_bus_num(I2C_GPIO6);
1494         } else if (machine_is_wmg160()) {
1495                 i2c_set_bus_num(I2C_GPIO6);
1496                 return;
1497         }
1498
1499         addr = 0x25;            /* fsa9480 */
1500         if (i2c_probe(addr)) {
1501                 printf("Can't found fsa9480\n");
1502                 return;
1503         }
1504
1505         if (path)
1506                 val[0] = 0x90;  /* VAUDIO */
1507         else
1508                 val[0] = (1 << 5) | (1 << 2);   /* DHOST */
1509
1510         i2c_write(addr, 0x13, 1, val, 1);       /* MANSW1 */
1511
1512         i2c_read(addr, 0x2, 1, val, 1);
1513         val[0] &= ~(1 << 2);                    /* Manual switching */
1514         i2c_write(addr, 0x2, 1, val, 1);
1515 }
1516
1517 #define MAX8998_REG_ONOFF1      0x11
1518 #define MAX8998_REG_ONOFF2      0x12
1519 #define MAX8998_REG_ONOFF3      0x13
1520 #define MAX8998_REG_LDO7        0x21
1521 #define MAX8998_REG_LDO17       0x29
1522 /* ONOFF1 */
1523 #define MAX8998_LDO3            (1 << 2)
1524 /* ONOFF2 */
1525 #define MAX8998_LDO6            (1 << 7)
1526 #define MAX8998_LDO7            (1 << 6)
1527 #define MAX8998_LDO8            (1 << 5)
1528 #define MAX8998_LDO9            (1 << 4)
1529 #define MAX8998_LDO10           (1 << 3)
1530 #define MAX8998_LDO11           (1 << 2)
1531 #define MAX8998_LDO12           (1 << 1)
1532 #define MAX8998_LDO13           (1 << 0)
1533 /* ONOFF3 */
1534 #define MAX8998_LDO14           (1 << 7)
1535 #define MAX8998_LDO15           (1 << 6)
1536 #define MAX8998_LDO16           (1 << 5)
1537 #define MAX8998_LDO17           (1 << 4)
1538
1539
1540 static void init_pmic(void)
1541 {
1542         unsigned char addr;
1543         unsigned char val[2];
1544
1545         if (cpu_is_s5pc100())
1546                 return;
1547
1548         i2c_set_bus_num(I2C_PMIC);
1549
1550         addr = 0xCC >> 1;       /* max8998 */
1551         if (i2c_probe(addr)) {
1552                 printf("Can't found max8998\n");
1553                 return;
1554         }
1555
1556         /* ONOFF1 */
1557         i2c_read(addr, MAX8998_REG_ONOFF1, 1, val, 1);
1558         val[0] &= ~MAX8998_LDO3;
1559         i2c_write(addr, MAX8998_REG_ONOFF1, 1, val, 1);
1560
1561         /* ONOFF2 */
1562         i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
1563         /*
1564          * Disable LDO10(VPLL_1.1V), LDO11(CAM_IO_2.8V),
1565          * LDO12(CAM_ISP_1.2V), LDO13(CAM_A_2.8V)
1566          */
1567         val[0] &= ~(MAX8998_LDO10 | MAX8998_LDO11 |
1568                         MAX8998_LDO12 | MAX8998_LDO13);
1569
1570         if (machine_is_kessler())
1571                 val[0] |= MAX8998_LDO7;         /* LDO7: VLCD_1.8V */
1572
1573         i2c_write(addr, MAX8998_REG_ONOFF2, 1, val, 1);
1574         i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
1575         /* ONOFF3 */
1576         i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
1577         /*
1578          * Disable LDO14(CAM_CIF_1.8), LDO15(CAM_AF_3.3V),
1579          * LDO16(VMIPI_1.8V), LDO17(CAM_8M_1.8V)
1580          */
1581         val[0] &= ~(MAX8998_LDO14 | MAX8998_LDO15 |
1582                         MAX8998_LDO16 | MAX8998_LDO17);
1583
1584         if (machine_is_kessler())
1585                 val[0] |= MAX8998_LDO17;        /* LDO17: VCC_3.0V_LCD */
1586
1587         i2c_write(addr, MAX8998_REG_ONOFF3, 1, val, 1);
1588         i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
1589 }
1590
1591 static void setup_power_down_mode_registers(void)
1592 {
1593         struct s5pc110_gpio *gpio = (struct s5pc110_gpio *)S5PC110_GPIO_BASE;
1594         struct s5pc1xx_gpio_bank *bank;
1595         struct gpio_powermode *p;
1596         int n_p;
1597         struct gpio_external *ge;
1598         int n_ge;
1599         struct s5pc1xx_gpio_item *mr;
1600         int n_mr;
1601         int i;
1602
1603         if (cpu_is_s5pc100())
1604                 return;
1605
1606         /* Only Limo real and kessler supports worked for sleep currnet */
1607         if (machine_is_aquila()) {
1608                 if (board_is_limo_real())
1609                         /* Support */;
1610                 else
1611                         return;
1612         } else if (machine_is_kessler()) {
1613                 /* Support */;
1614         } else if (machine_is_geminus()) {
1615                 /* Support */;
1616         } else
1617                 return;
1618
1619         if (machine_is_aquila()) {
1620                 /* Aquila rev 0.8 or lower */
1621                 p = aquila_powerdown_modes;
1622                 ge = aquila_external_powerdown_modes;
1623                 mr = aquila_mirror_powerdown_mode;
1624                 n_p = ARRAY_SIZE(aquila_powerdown_modes);
1625                 n_ge = ARRAY_SIZE(aquila_external_powerdown_modes);
1626                 n_mr = ARRAY_SIZE(aquila_mirror_powerdown_mode);
1627         } else if (machine_is_kessler()) {
1628                 /* Aquila rev 0.9 */
1629                 p = kessler_powerdown_modes;
1630                 ge = kessler_external_powerdown_modes;
1631                 mr = kessler_mirror_powerdown_mode;
1632                 n_p = ARRAY_SIZE(kessler_powerdown_modes);
1633                 n_ge = ARRAY_SIZE(kessler_external_powerdown_modes);
1634                 n_mr = ARRAY_SIZE(kessler_mirror_powerdown_mode);
1635         } else if (machine_is_geminus()) {
1636                 if (hwrevision(1)) {
1637                         /* Same as Aquila rev 0.9 */
1638 #if 0
1639                         p = kessler_powerdown_modes;
1640                         ge = kessler_external_powerdown_modes;
1641                         mr = kessler_mirror_powerdown_mode;
1642                         n_p = ARRAY_SIZE(kessler_powerdown_modes);
1643                         n_ge = ARRAY_SIZE(kessler_external_powerdown_modes);
1644                         n_mr = ARRAY_SIZE(kessler_mirror_powerdown_mode);
1645 #else
1646                         p = aquila_powerdown_modes;
1647                         ge = aquila_external_powerdown_modes;
1648                         mr = aquila_mirror_powerdown_mode;
1649                         n_p = ARRAY_SIZE(aquila_powerdown_modes);
1650                         n_ge = ARRAY_SIZE(aquila_external_powerdown_modes);
1651                         n_mr = ARRAY_SIZE(aquila_mirror_powerdown_mode);
1652 #endif
1653                 } else if (hwrevision(0)) {
1654                         /* Same as Aquila rev 0.8 or lower */
1655                         p = aquila_powerdown_modes;
1656                         ge = aquila_external_powerdown_modes;
1657                         mr = aquila_mirror_powerdown_mode;
1658                         n_p = ARRAY_SIZE(aquila_powerdown_modes);
1659                         n_ge = ARRAY_SIZE(aquila_external_powerdown_modes);
1660                         n_mr = ARRAY_SIZE(aquila_mirror_powerdown_mode);
1661                 } else {
1662                         return; /* Not supported */
1663                 }
1664         }
1665
1666         bank = &gpio->gpio_a0;
1667
1668         for (i = 0; i < n_p; i++, p++, bank++) {
1669                 writel(p->conpdn, &bank->pdn_con);
1670                 writel(p->pudpdn, &bank->pdn_pull);
1671         }
1672         /* M299 */
1673         writel(0xff0022b0, (unsigned int *)0xF0000000);
1674         writel(0xff0022b0, (unsigned int *)0xF1400000);
1675
1676         bank = &gpio->gpio_h0;
1677
1678         for (i = 0; i < n_ge; i++) {
1679                 writel(ge->con, &bank->con);
1680                 writel(ge->dat, &bank->dat);
1681                 writel(ge->pud, &bank->pull);
1682
1683                 bank++;
1684                 ge++;
1685         }
1686
1687         for (i = 0; i < n_mr; i++) {
1688                 unsigned int reg = readl(&mr->bank->pdn_con);
1689                 reg &= ~(1 << mr->number);
1690                 if (readl(&mr->bank->dat) & (1 << mr->number))
1691                         reg |= 1 << mr->number;
1692                 writel(reg, &mr->bank->pdn_con);
1693                 mr++;
1694         }
1695 }
1696
1697 #ifdef CONFIG_LCD
1698 #include "../../../drivers/video/s5p-spi.h"
1699
1700 extern void s6e63m0_set_platform_data(struct spi_platform_data *pd);
1701 extern void s6d16a0x_set_platform_data(struct spi_platform_data *pd);
1702
1703 struct spi_platform_data spi_pd;
1704
1705 struct s5pc110_gpio *gpio_base = (struct s5pc110_gpio *) S5PC110_GPIO_BASE;
1706
1707 void lcd_cfg_gpio(void)
1708 {
1709         unsigned int i, f3_end = 4;
1710
1711         for (i = 0; i < 8; i++) {
1712                 /* set GPF0,1,2[0:7] for RGB Interface and Data lines (32bit) */
1713                 gpio_cfg_pin(&gpio_base->gpio_f0, i, GPIO_FUNC(2));
1714                 gpio_cfg_pin(&gpio_base->gpio_f1, i, GPIO_FUNC(2));
1715                 gpio_cfg_pin(&gpio_base->gpio_f2, i, GPIO_FUNC(2));
1716                 /* pull-up/down disable */
1717                 gpio_set_pull(&gpio_base->gpio_f0, i, GPIO_PULL_NONE);
1718                 gpio_set_pull(&gpio_base->gpio_f1, i, GPIO_PULL_NONE);
1719                 gpio_set_pull(&gpio_base->gpio_f2, i, GPIO_PULL_NONE);
1720
1721                 /* drive strength to max (24bit) */
1722                 gpio_set_drv(&gpio_base->gpio_f0, i, GPIO_DRV_4X);
1723                 gpio_set_rate(&gpio_base->gpio_f0, i, GPIO_DRV_SLOW);
1724                 gpio_set_drv(&gpio_base->gpio_f1, i, GPIO_DRV_4X);
1725                 gpio_set_rate(&gpio_base->gpio_f1, i, GPIO_DRV_SLOW);
1726                 gpio_set_drv(&gpio_base->gpio_f2, i, GPIO_DRV_4X);
1727                 gpio_set_rate(&gpio_base->gpio_f2, i, GPIO_DRV_SLOW);
1728         }
1729
1730         /* set DISPLAY_DE_B pin for dual rgb mode. */
1731         if (board_is_media())
1732                 f3_end = 5;
1733
1734         for (i = 0; i < f3_end; i++) {
1735                 /* set GPF3[0:3] for RGB Interface and Data lines (32bit) */
1736                 gpio_cfg_pin(&gpio_base->gpio_f3, i, GPIO_PULL_UP);
1737                 /* pull-up/down disable */
1738                 gpio_set_pull(&gpio_base->gpio_f3, i, GPIO_PULL_NONE);
1739                 /* drive strength to max (24bit) */
1740                 gpio_set_drv(&gpio_base->gpio_f3, i, GPIO_DRV_4X);
1741                 gpio_set_rate(&gpio_base->gpio_f3, i, GPIO_DRV_SLOW);
1742         }
1743         /* display output path selection (only [1:0] valid) */
1744         writel(0x2, 0xE0107008);
1745
1746         /* gpio pad configuration for LCD reset. */
1747         gpio_cfg_pin(&gpio_base->gpio_mp0_5, 5, GPIO_OUTPUT);
1748
1749         /* gpio pad configuration for LCD ON. */
1750         gpio_cfg_pin(&gpio_base->gpio_j1, 3, GPIO_OUTPUT);
1751
1752         /* LCD_BACKLIGHT_EN */
1753         if (machine_is_geminus())
1754                 gpio_cfg_pin(&gpio_base->gpio_mp0_5, 0, GPIO_OUTPUT);
1755         if (board_is_neptune()) {
1756                 gpio_cfg_pin(&gpio_base->gpio_mp0_4, 4, GPIO_OUTPUT);
1757                 gpio_direction_output(&gpio_base->gpio_mp0_4, 4, 0);
1758         }
1759
1760         /*
1761          * gpio pad configuration for
1762          * DISPLAY_CS, DISPLAY_CLK, DISPLAY_SO, DISPLAY_SI.
1763          */
1764         gpio_cfg_pin(&gpio_base->gpio_mp0_1, 1, GPIO_OUTPUT);
1765         gpio_cfg_pin(&gpio_base->gpio_mp0_4, 1, GPIO_OUTPUT);
1766         gpio_cfg_pin(&gpio_base->gpio_mp0_4, 2, GPIO_INPUT);
1767         gpio_cfg_pin(&gpio_base->gpio_mp0_4, 3, GPIO_OUTPUT);
1768
1769         if (machine_is_aquila() || machine_is_kessler()) {
1770                 spi_pd.cs_bank = &gpio_base->gpio_mp0_1;
1771                 spi_pd.cs_num = 1;
1772                 spi_pd.clk_bank = &gpio_base->gpio_mp0_4;
1773                 spi_pd.clk_num = 1;
1774                 spi_pd.si_bank = &gpio_base->gpio_mp0_4;
1775                 spi_pd.si_num = 3;
1776                 spi_pd.so_bank = &gpio_base->gpio_mp0_4;
1777                 spi_pd.so_num = 2;
1778
1779                 if (board_is_neptune())
1780                         s6d16a0x_set_platform_data(&spi_pd);
1781                 else {
1782                         s6e63m0_set_platform_data(&spi_pd);
1783                         if (board_is_media())
1784                                 spi_pd.set_rev = 1;
1785                 }
1786         }
1787
1788         if (machine_is_cypress()) {
1789 #if 0           /* universal cypress */
1790                 /* FLCD_CS */
1791                 gpio_cfg_pin(&gpio_base->gpio_mp0_1, 0, GPIO_OUTPUT);
1792 #endif
1793                 /* FLCD_CS_S */
1794                 gpio_cfg_pin(&gpio_base->gpio_mp0_5, 1, GPIO_OUTPUT);
1795                 /* FLCD_CLK */
1796                 gpio_cfg_pin(&gpio_base->gpio_mp0_4, 0, GPIO_OUTPUT);
1797                 /* FLCD_SDI */
1798                 gpio_cfg_pin(&gpio_base->gpio_mp0_4, 2, GPIO_OUTPUT);
1799                 /* FLCD_RST_S */
1800                 gpio_cfg_pin(&gpio_base->gpio_mp0_4, 5, GPIO_OUTPUT);
1801                 /* FLCD_ON_S */
1802                 gpio_cfg_pin(&gpio_base->gpio_g2, 2, GPIO_OUTPUT);
1803 #if 0           /* universal cypress */
1804                 pd_cs.bank = &gpio_base->gpio_mp0_1;
1805                 pd_cs.num = 0;
1806 #endif
1807                 spi_pd.cs_bank = &gpio_base->gpio_mp0_5;
1808                 spi_pd.cs_num = 1;
1809                 spi_pd.clk_bank = &gpio_base->gpio_mp0_4;
1810                 spi_pd.clk_num = 0;
1811                 spi_pd.si_bank = &gpio_base->gpio_mp0_4;
1812                 spi_pd.si_num = 2;
1813
1814                 spi_pd.set_rev = 1;
1815
1816                 /* these data would be sent to s6e63m0 lcd panel driver. */
1817                 s6e63m0_set_platform_data(&spi_pd);
1818         }
1819
1820         return;
1821 }
1822
1823 #define SWRST_REG               0x00
1824 #define LEDCON_REG              0x01
1825 #define LED_CUR_SET_REG         0x03
1826 #define LED_CUR_TR_REG          0x08
1827
1828 #define SWRST                   0x01
1829 #define NORMAL_MODE             0x01
1830 #define CUR_SET                 0x63
1831 #define TR_SET                  0x00
1832
1833 void backlight_on(unsigned int onoff)
1834 {
1835
1836         unsigned char addr;
1837         unsigned char val[2];
1838         struct s5pc110_gpio *gpio = (struct s5pc110_gpio *) S5PC110_GPIO_BASE;
1839
1840         if (onoff) {
1841                 if (machine_is_geminus())
1842                         gpio_set_value(&gpio->gpio_mp0_5, 0, 1);
1843         } else {
1844                 if (machine_is_geminus())
1845                         gpio_set_value(&gpio->gpio_mp0_5, 0, 0);
1846         }
1847
1848         if (machine_is_kessler() && board_is_neptune()) {
1849                 gpio_set_value(&gpio->gpio_mp0_4, 4, 1);
1850                 udelay(6);
1851
1852                 i2c_set_bus_num(I2C_GPIO5);
1853
1854                 addr = 0x76;
1855                 if (i2c_probe(addr)) {
1856                         if (i2c_probe(addr)) {
1857                                 printf("Can't found s6d16a0x backlight i2c\n");
1858                                 return;
1859                         }
1860                 }
1861                 val[0] = SWRST;
1862                 i2c_write(addr, SWRST_REG, 1, val, 1);
1863                 /* NORMAL MODE */
1864                 val[0] = CUR_SET;
1865                 i2c_write(addr, LED_CUR_SET_REG, 1, val, 1);
1866                 val[0] = TR_SET;
1867                 i2c_write(addr, LED_CUR_TR_REG, 1, val, 1);
1868                 val[0] = NORMAL_MODE;
1869                 i2c_write(addr, LEDCON_REG, 1, val, 1);
1870                 udelay(5000);
1871         }
1872 }
1873
1874 void reset_lcd(void)
1875 {
1876         struct s5pc110_gpio *gpio = (struct s5pc110_gpio *) S5PC110_GPIO_BASE;
1877
1878         if (machine_is_aquila() || machine_is_kessler() || machine_is_geminus())
1879                 gpio_set_value(&gpio->gpio_mp0_5, 5, 1);
1880         if (machine_is_cypress())
1881                 gpio_set_value(&gpio->gpio_mp0_4, 5, 1);
1882 }
1883
1884 void lcd_power_on(unsigned int onoff)
1885 {
1886         struct s5pc110_gpio *gpio = (struct s5pc110_gpio *) S5PC110_GPIO_BASE;
1887         if (onoff) {
1888                 /* TSP_LDO_ON */
1889                 if (machine_is_aquila() || machine_is_geminus())
1890                         gpio_set_value(&gpio->gpio_j1, 3, 1);
1891
1892                 if (machine_is_cypress())
1893                         gpio_set_value(&gpio->gpio_g2, 2, 1);
1894
1895                 if (machine_is_kessler()) {
1896                         unsigned char addr;
1897                         unsigned char val[2];
1898                         unsigned char val2[2];
1899
1900                         gpio_set_value(&gpio->gpio_j1, 3, 1);
1901
1902                         i2c_set_bus_num(I2C_PMIC);
1903                         addr = 0xCC >> 1;       /* max8998 */
1904                         if (i2c_probe(addr)) {
1905                                 printf("Can't found max8998\n");
1906                                 return;
1907                         }
1908                         i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
1909                         val[0] &= ~(MAX8998_LDO17);
1910                         val[0] |= MAX8998_LDO17;        /* LDO17: VCC_3.0V_LCD */
1911                         i2c_write(addr, MAX8998_REG_ONOFF3, 1, val, 1);
1912
1913                         i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
1914                         val[0] |= MAX8998_LDO17;
1915                         val2[0] = 0xE;
1916                         i2c_write(addr, MAX8998_REG_LDO17, 1, val2, 1);
1917                         i2c_write(addr, MAX8998_REG_ONOFF3, 1, val, 1);
1918
1919                         i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
1920                         val[0] |= MAX8998_LDO7;
1921                         val2[0] = 0x2;
1922                         i2c_write(addr, MAX8998_REG_LDO7, 1, val2, 1);
1923                         i2c_write(addr, MAX8998_REG_ONOFF2, 1, val, 1);
1924                 }
1925         } else {
1926                 if (machine_is_aquila() || machine_is_geminus())
1927                         gpio_set_value(&gpio->gpio_j1, 3, 0);
1928
1929                 if (machine_is_cypress())
1930                         gpio_set_value(&gpio->gpio_g2, 2, 0);
1931
1932                 if (machine_is_kessler()) {
1933                         unsigned char addr;
1934                         unsigned char val[2];
1935
1936                         gpio_set_value(&gpio->gpio_j1, 3, 0);
1937
1938                         i2c_set_bus_num(I2C_PMIC);
1939                         addr = 0xCC >> 1;       /* max8998 */
1940                         if (i2c_probe(addr)) {
1941                                 printf("Can't found max8998\n");
1942                                 return;
1943                         }
1944
1945                         i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
1946                         val[0] &= ~(MAX8998_LDO7);
1947                         i2c_write(addr, MAX8998_REG_ONOFF2, 1, val, 1);
1948                         i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
1949
1950                         i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
1951                         val[0] &= ~(MAX8998_LDO17);
1952                         i2c_write(addr, MAX8998_REG_ONOFF3, 1, val, 1);
1953                         i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
1954                 }
1955         }
1956 }
1957
1958 extern void s6e63m0_cfg_ldo(void);
1959 extern void s6e63m0_enable_ldo(unsigned int onoff);
1960 extern void s6d16a0x_cfg_ldo(void);
1961 extern void s6d16a0x_enable_ldo(unsigned int onoff);
1962
1963 int s5p_no_lcd_support(void)
1964 {
1965         if (machine_is_wmg160())
1966                 return 1;
1967         return 0;
1968 }
1969
1970 void init_panel_info(vidinfo_t *vid)
1971 {
1972         vid->cfg_gpio = NULL;
1973         vid->reset_lcd = NULL;
1974         vid->backlight_on = NULL;
1975         vid->lcd_power_on = NULL;
1976
1977         vid->cfg_ldo = NULL;
1978         vid->enable_ldo = NULL;
1979
1980         vid->init_delay = 0;
1981         vid->reset_delay = 0;
1982         vid->power_on_delay = 0;
1983
1984         vid->vl_freq    = 60;
1985         vid->vl_col     = 480;
1986         vid->vl_row     = 800;
1987         vid->vl_width   = 480;
1988         vid->vl_height  = 800;
1989
1990         vid->dual_lcd_enabled = 0;
1991
1992         if (board_is_media()) {
1993                 vid->vl_col     = 960;
1994                 vid->vl_row     = 800;
1995                 vid->vl_width   = 960;
1996                 vid->vl_height  = 800;
1997
1998                 /* enable dual lcd mode. */
1999                 vid->dual_lcd_enabled = 1;
2000         }
2001
2002         vid->vl_clkp    = CONFIG_SYS_HIGH;
2003         vid->vl_hsp     = CONFIG_SYS_LOW;
2004         vid->vl_vsp     = CONFIG_SYS_LOW;
2005         vid->vl_dp      = CONFIG_SYS_HIGH;
2006         vid->vl_bpix    = 32;
2007
2008         /* S6E63M0 LCD Panel */
2009         vid->vl_hspw    = 2;
2010         vid->vl_hbpd    = 16;
2011         vid->vl_hfpd    = 16;
2012
2013         vid->vl_vspw    = 2;
2014         vid->vl_vbpd    = 3;
2015         vid->vl_vfpd    = 28;
2016
2017         if (machine_is_aquila() || machine_is_kessler() || machine_is_cypress()) {
2018                 vid->cfg_gpio = lcd_cfg_gpio;
2019                 vid->reset_lcd = reset_lcd;
2020                 vid->backlight_on = backlight_on;
2021                 vid->lcd_power_on = lcd_power_on;
2022                 vid->cfg_ldo = s6e63m0_cfg_ldo;
2023                 vid->enable_ldo = s6e63m0_enable_ldo;
2024
2025                 vid->init_delay = 25000;
2026                 vid->reset_delay = 120000;
2027         }
2028
2029         if (board_is_neptune()) {
2030                 vid->vl_freq    = 100;
2031                 vid->vl_col     = 320;
2032                 vid->vl_row     = 480;
2033                 vid->vl_width   = 320;
2034                 vid->vl_height  = 480;
2035
2036                 vid->vl_clkp    = CONFIG_SYS_HIGH;
2037                 vid->vl_hsp     = CONFIG_SYS_HIGH;
2038                 vid->vl_vsp     = CONFIG_SYS_HIGH;
2039                 vid->vl_dp      = CONFIG_SYS_LOW;
2040                 vid->vl_bpix    = 32;
2041
2042                 /* disable dual lcd mode. */
2043                 vid->dual_lcd_enabled = 0;
2044
2045                 /* S6D16A0X LCD Panel */
2046                 vid->vl_hspw    = 16;
2047                 vid->vl_hbpd    = 24;
2048                 vid->vl_hfpd    = 16;
2049
2050                 vid->vl_vspw    = 2;
2051                 vid->vl_vbpd    = 2;
2052                 vid->vl_vfpd    = 4;
2053
2054                 vid->cfg_gpio = lcd_cfg_gpio;
2055                 vid->backlight_on = backlight_on;
2056                 vid->lcd_power_on = lcd_power_on;
2057                 vid->reset_lcd = reset_lcd;
2058                 vid->cfg_ldo = s6d16a0x_cfg_ldo;
2059                 vid->enable_ldo = s6d16a0x_enable_ldo;
2060
2061                 vid->init_delay = 10000;
2062                 vid->power_on_delay = 10000;
2063                 vid->reset_delay = 1000;
2064
2065         }
2066
2067         if (machine_is_geminus()) {
2068                 vid->vl_freq    = 60;
2069                 vid->vl_col     = 1024,
2070                 vid->vl_row     = 600,
2071                 vid->vl_width   = 1024,
2072                 vid->vl_height  = 600,
2073                 vid->vl_clkp    = CONFIG_SYS_LOW,
2074                 vid->vl_hsp     = CONFIG_SYS_HIGH,
2075                 vid->vl_vsp     = CONFIG_SYS_HIGH,
2076                 vid->vl_dp      = CONFIG_SYS_LOW,
2077                 vid->vl_bpix    = 32,
2078
2079                 vid->vl_hspw    = 32,
2080                 vid->vl_hfpd    = 48,
2081                 vid->vl_hbpd    = 80,
2082
2083                 vid->vl_vspw    = 1,
2084                 vid->vl_vfpd    = 3,
2085                 vid->vl_vbpd    = 4,
2086
2087                 vid->cfg_gpio = lcd_cfg_gpio;
2088                 vid->reset_lcd = reset_lcd;
2089                 vid->backlight_on = backlight_on;
2090                 vid->lcd_power_on = lcd_power_on;
2091         }
2092 #if 0
2093         vid->vl_freq    = 60;
2094         vid->vl_col     = 480,
2095         vid->vl_row     = 800,
2096         vid->vl_width   = 480,
2097         vid->vl_height  = 800,
2098         vid->vl_clkp    = CONFIG_SYS_HIGH,
2099         vid->vl_hsp     = CONFIG_SYS_LOW,
2100         vid->vl_vsp     = CONFIG_SYS_LOW,
2101         vid->vl_dp      = CONFIG_SYS_HIGH,
2102         vid->vl_bpix    = 32,
2103
2104         /* tl2796 panel. */
2105         vid->vl_hpw     = 4,
2106         vid->vl_blw     = 8,
2107         vid->vl_elw     = 8,
2108
2109         vid->vl_vpw     = 4,
2110         vid->vl_bfw     = 8,
2111         vid->vl_efw     = 8,
2112 #endif
2113 #if 0
2114         vid->vl_freq    = 60;
2115         vid->vl_col     = 1024,
2116         vid->vl_row     = 600,
2117         vid->vl_width   = 1024,
2118         vid->vl_height  = 600,
2119         vid->vl_clkp    = CONFIG_SYS_HIGH,
2120         vid->vl_hsp     = CONFIG_SYS_HIGH,
2121         vid->vl_vsp     = CONFIG_SYS_HIGH,
2122         vid->vl_dp      = CONFIG_SYS_LOW,
2123         vid->vl_bpix    = 32,
2124
2125         /* AMS701KA AMOLED Panel. */
2126         vid->vl_hpw     = 30,
2127         vid->vl_blw     = 114,
2128         vid->vl_elw     = 48,
2129
2130         vid->vl_vpw     = 2,
2131         vid->vl_bfw     = 6,
2132         vid->vl_efw     = 8,
2133 #endif
2134 }
2135 #endif
2136
2137 static void setup_meminfo(void)
2138 {
2139         char meminfo[64] = {0, };
2140         int count = 0, size, real;
2141
2142         size = gd->bd->bi_dram[0].size >> 20;
2143         count += sprintf(meminfo + count, "mem=%dM", size);
2144
2145         /* Each Chip Select can't exceed the 256MiB */
2146         size = gd->bd->bi_dram[1].size >> 20;
2147         real = min(size, 256);
2148         count += sprintf(meminfo + count, " mem=%dM@0x%x",
2149                 real, (unsigned int)gd->bd->bi_dram[1].start);
2150
2151         size -= real;
2152         if (size > 0) {
2153                 count += sprintf(meminfo + count, " mem=%dM@0x%x", size,
2154                         (unsigned int)gd->bd->bi_dram[1].start + (real << 20));
2155         }
2156
2157         setenv("meminfo", meminfo);
2158 }
2159
2160 int misc_init_r(void)
2161 {
2162 #ifdef CONFIG_LCD
2163         /* It should be located at first */
2164         lcd_is_enabled = 0;
2165
2166         if (machine_is_aquila() || machine_is_kessler()) {
2167                 if (board_is_neptune())
2168                         setenv("lcdinfo", "lcd=s6d16a0x");
2169                 else if (board_is_media())
2170                         setenv("lcdinfo", "lcd=s6e63m0");
2171                 else
2172                         setenv("lcdinfo", "lcd=s6e63m0");
2173         }
2174         if (machine_is_geminus())
2175                 setenv("lcdinfo", "lcd=lms480jc01");
2176         if (machine_is_cypress())
2177                 setenv("lcdinfo", "lcd=s6e63m0");
2178 #endif
2179         setup_meminfo();
2180
2181         show_hw_revision();
2182
2183         /* Set proper PMIC pins */
2184         pmic_pin_init();
2185
2186         /* Check auto burning */
2187         check_auto_burn();
2188
2189         /* To power up I2C2 */
2190         enable_ldos();
2191
2192         /* Enable T-Flash at Limo Real or Limo Universal */
2193         enable_t_flash();
2194
2195         /* Setup Limo Real board GPIOs */
2196         setup_limo_real_gpios();
2197
2198         /* Setup Media board GPIOs */
2199         setup_media_gpios();
2200
2201         /* To usbdown automatically */
2202         check_keypad();
2203
2204         /* check max8998 */
2205         init_pmic();
2206
2207 #ifdef CONFIG_S5PC1XXFB
2208         display_device_info();
2209 #endif
2210
2211         setup_power_down_mode_registers();
2212
2213         /* check max17040 */
2214         check_battery(0);
2215
2216         /* check fsa9480 */
2217         check_micro_usb(0);
2218
2219         return 0;
2220 }
2221 #endif
2222
2223 int board_init(void)
2224 {
2225         /* Set Initial global variables */
2226         s5pc110_gpio = (struct s5pc110_gpio *) S5PC110_GPIO_BASE;
2227
2228         gd->bd->bi_arch_number = MACH_AQUILA;
2229         gd->bd->bi_boot_params = PHYS_SDRAM_1 + 0x100;
2230
2231         /* Check H/W Revision */
2232         check_hw_revision();
2233
2234         return 0;
2235 }
2236
2237 int dram_init(void)
2238 {
2239         unsigned int base, memconfig0, size;
2240         unsigned int memconfig1, sz = 0;
2241
2242         if (cpu_is_s5pc100()) {
2243                 /* In mem setup, we swap the bank. So below size is correct */
2244                 gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
2245                 gd->bd->bi_dram[0].size = PHYS_SDRAM_2_SIZE;
2246                 gd->bd->bi_dram[1].start = S5PC100_PHYS_SDRAM_2;
2247                 size = 128;
2248         } else {
2249                 /* In S5PC110, we can't swap the DMC0/1 */
2250                 gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
2251                 gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;
2252
2253                 base = S5PC110_DMC1_BASE;
2254                 /* DMC configuration */
2255                 memconfig0 = readl(base + MEMCONFIG0_OFFSET);
2256                 gd->bd->bi_dram[1].start = memconfig0 & 0xFF000000;
2257
2258                 size = (memconfig0 >> 16) & 0xFF;
2259                 size = ((unsigned char) ~size) + 1;
2260
2261                 /*
2262                  * (0x07 + 1) * 16 = 128 MiB
2263                  * (0x0f + 1) * 16 = 256 MiB
2264                  */
2265                 size = size << 4;
2266
2267                 /*
2268                  * Aquila Rev0.5 4G3G1G
2269                  * Aquila Rev0.8 4G3G1G
2270                  * Aquila Rev0.9 4G3G1G
2271                  */
2272                 if (machine_is_aquila() || machine_is_kessler())
2273                         if (hwrevision(5) || hwrevision(8) || hwrevision(9)) {
2274                                 memconfig1 = readl(base + MEMCONFIG1_OFFSET);
2275
2276                                 sz = (memconfig1 >> 16) & 0xFF;
2277                                 sz = ((unsigned char) ~sz) + 1;
2278                                 sz = sz << 4;
2279                         }
2280
2281         }
2282         /*
2283          * bi_dram[1].size contains all DMC1 memory size
2284          */
2285         gd->bd->bi_dram[1].size = (size + sz) << 20;
2286
2287         return 0;
2288 }
2289
2290 /* Used for sleep test */
2291 static unsigned char saved_val[4][2];
2292 void board_sleep_init_late(void)
2293 {
2294         /* CODEC_LDO_EN: GPF3[4] */
2295         gpio_direction_output(&s5pc110_gpio->gpio_f3, 4, 0);
2296         /* CODEC_XTAL_EN: GPH3[2] */
2297         gpio_direction_output(&s5pc110_gpio->gpio_h3, 2, 0);
2298
2299         /* MMC T_FLASH off */
2300         gpio_direction_output(&s5pc110_gpio->gpio_mp0_5, 4, 0);
2301         /* MHL off */
2302         gpio_direction_output(&s5pc110_gpio->gpio_j2, 2, 0);
2303         gpio_direction_output(&s5pc110_gpio->gpio_mp0_4, 7, 0);
2304         gpio_direction_output(&s5pc110_gpio->gpio_j2, 3, 0); /* MHL_ON for REV02 or higher */
2305
2306
2307 }
2308 void board_sleep_init(void)
2309 {
2310         unsigned char addr;
2311         unsigned char val[2];
2312
2313         i2c_set_bus_num(I2C_PMIC);
2314         addr = 0xCC >> 1;
2315         if (i2c_probe(addr)) {
2316                 printf("Can't find max8998\n");
2317                 return;
2318         }
2319
2320         if (machine_is_kessler()) {
2321
2322                 /* Set ONOFF1 */
2323                 i2c_read(addr, MAX8998_REG_ONOFF1, 1, val, 1);
2324                 saved_val[0][0] = val[0];
2325                 saved_val[0][1] = val[1];
2326                 val[0] &= ~((1 << 7) | (1 << 6) | (1 << 4) | (1 << 2) |
2327                                 (1 << 1) | (1 << 0));
2328                 i2c_write(addr, MAX8998_REG_ONOFF1, 1, val, 1);
2329                 i2c_read(addr, MAX8998_REG_ONOFF1, 1, val, 1);
2330                 /* Set ONOFF2 */
2331                 i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
2332                 saved_val[1][0] = val[0];
2333                 saved_val[1][1] = val[1];
2334                 val[0] &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 3) |
2335                                 (1 << 2) | (1 << 1) | (1 << 0));
2336                 val[0] |= (1 << 7);
2337                 i2c_write(addr, MAX8998_REG_ONOFF2, 1, val, 1);
2338                 i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
2339                 /* Set ONOFF3 */
2340                 i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
2341                 saved_val[2][0] = val[0];
2342                 saved_val[2][1] = val[1];
2343                 val[0] &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 4));
2344                 i2c_write(addr, MAX8998_REG_ONOFF3, 1, val, 1);
2345                 i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
2346                 /* Set ONOFF4 */
2347                 i2c_read(addr, MAX8998_REG_ONOFF3+1, 1, val, 1);
2348                 saved_val[3][0] = val[0];
2349                 saved_val[3][1] = val[1];
2350                 val[0] &= ~((1 << 6) | (1 << 4));
2351                 i2c_write(addr, MAX8998_REG_ONOFF3+1, 1, val, 1);
2352                 i2c_read(addr, MAX8998_REG_ONOFF3+1, 1, val, 1);
2353                 printf("Turned off regulators with Kessler setting."
2354                                " Preparing to sleep. [%s:%d]\n",
2355                                 __FILE__, __LINE__);
2356         } else { /* Default */
2357
2358                 /* Set ONOFF1 */
2359                 i2c_read(addr, MAX8998_REG_ONOFF1, 1, val, 1);
2360                 saved_val[0][0] = val[0];
2361                 saved_val[0][1] = val[1];
2362                 val[0] &= ~((1 << 7) | (1 << 6) | (1 << 4) | (1 << 2) |
2363                                 (1 << 1) | (1 << 0));
2364                 i2c_write(addr, MAX8998_REG_ONOFF1, 1, val, 1);
2365                 i2c_read(addr, MAX8998_REG_ONOFF1, 1, val, 1);
2366                 /* Set ONOFF2 */
2367                 i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
2368                 saved_val[1][0] = val[0];
2369                 saved_val[1][1] = val[1];
2370                 val[0] &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 3) |
2371                                 (1 << 2) | (1 << 1) | (1 << 0));
2372                 val[0] |= (1 << 7);
2373                 i2c_write(addr, MAX8998_REG_ONOFF2, 1, val, 1);
2374                 i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
2375                 /* Set ONOFF3 */
2376                 i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
2377                 saved_val[2][0] = val[0];
2378                 saved_val[2][1] = val[1];
2379                 val[0] &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 4));
2380                 i2c_write(addr, MAX8998_REG_ONOFF3, 1, val, 1);
2381                 i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
2382                 /* Set ONOFF4 */
2383                 i2c_read(addr, MAX8998_REG_ONOFF3+1, 1, val, 1);
2384                 saved_val[3][0] = val[0];
2385                 saved_val[3][1] = val[1];
2386                 val[0] &= ~((1 << 7) | (1 << 6) | (1 << 4));
2387                 i2c_write(addr, MAX8998_REG_ONOFF3+1, 1, val, 1);
2388                 i2c_read(addr, MAX8998_REG_ONOFF3+1, 1, val, 1);
2389                 printf("Turned off regulators with default(Aquila) setting."
2390                                " Preparing to sleep. [%s:%d]\n",
2391                                 __FILE__, __LINE__);
2392         }
2393 }
2394
2395 void board_sleep_resume(void)
2396 {
2397         unsigned char addr;
2398         unsigned char val[2];
2399
2400         show_hw_revision();
2401
2402         i2c_set_bus_num(I2C_PMIC);
2403         addr = 0xCC >> 1;
2404         if (i2c_probe(addr)) {
2405                 printf("Can't find max8998\n");
2406                 return;
2407         }
2408
2409         /* Set ONOFF1 */
2410         i2c_write(addr, MAX8998_REG_ONOFF1, 1, saved_val[0], 1);
2411         i2c_read(addr, MAX8998_REG_ONOFF1, 1, val, 1);
2412         /* Set ONOFF2 */
2413         i2c_write(addr, MAX8998_REG_ONOFF2, 1, saved_val[1], 1);
2414         i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
2415         /* Set ONOFF3 */
2416         i2c_write(addr, MAX8998_REG_ONOFF3, 1, saved_val[2], 1);
2417         i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
2418         /* Set ONOFF4 */
2419         i2c_write(addr, MAX8998_REG_ONOFF3+1, 1, saved_val[3], 1);
2420         i2c_read(addr, MAX8998_REG_ONOFF3+1, 1, val, 1);
2421         printf("Waked up.\n");
2422
2423         /* check max17040 */
2424         check_battery(0);
2425
2426         /* check fsa9480 */
2427         check_micro_usb(1);
2428 }
2429
2430 #if defined(CONFIG_USB_GADGET_S3C_UDC_OTG)
2431
2432 static int s5pc1xx_phy_control(int on)
2433 {
2434         static int status;
2435
2436         if (on && !status) {
2437 //              printf("turning USB power on\n");
2438 #ifdef CONFIG_CMD_PMIC
2439                 run_command("pmic ldo 3 on", 0);
2440 #endif
2441                 /* S5PC110 */
2442                 if (board_is_limo_universal() ||
2443                         board_is_limo_real() ||
2444                         board_is_media()) {
2445                         /* check usb path */
2446                         if (board_is_limo_real() && !hwrevision(6))
2447                                 check_mhl();
2448                 }
2449
2450                 if (machine_is_tickertape())
2451                         /* USB_SEL: XM0ADDR_0: MP04[0] output mode */
2452                         gpio_direction_output(&s5pc110_gpio->gpio_mp0_4, 0, 0);
2453
2454                 /* USB Path to AP */
2455                 micro_usb_switch(0);
2456                 status = 1;
2457         } else if (!on && status) {
2458 //              printf("turning USB power off\n");
2459 #ifdef CONFIG_CMD_PMIC
2460                 run_command("pmic ldo 3 off", 0);
2461 #endif
2462                 status = 0;
2463         }
2464         udelay(10000);
2465
2466         return 0;
2467 }
2468
2469 struct s3c_plat_otg_data s5pc110_otg_data = {
2470         .phy_control = s5pc1xx_phy_control,
2471         .regs_phy = S5PC110_PHY_BASE,
2472         .regs_otg = S5PC110_OTG_BASE,
2473 };
2474
2475 int board_eth_init(bd_t *bis)
2476 {
2477         int res = -1;
2478
2479         if (cpu_is_s5pc100())
2480                 return -1;
2481
2482         s3c_udc_probe(&s5pc110_otg_data);
2483         if (usb_eth_initialize(bis) >= 0)
2484                 res = 0;
2485         return res;
2486 }
2487 #endif
2488
2489 #ifdef CONFIG_CMD_USBDOWN
2490 int usb_board_init(void)
2491 {
2492 #ifdef CONFIG_CMD_PMIC
2493         run_command("pmic ldo 3 on", 0);
2494 #endif
2495
2496         if (cpu_is_s5pc100()) {
2497 #ifdef CONFIG_HARD_I2C
2498                 uchar val[2] = {0,};
2499
2500                 /* PMIC */
2501                 if (i2c_read(0x66, 0, 1, val, 2)) {
2502                         printf("i2c_read error\n");
2503                         return 1;
2504                 }
2505
2506                 val[0] |= (1 << 3);
2507                 val[1] |= (1 << 5);
2508
2509                 if (i2c_write(0x66, 0, 1, val, 2)) {
2510                         printf("i2c_write error\n");
2511                         return 1;
2512                 }
2513                 i2c_read(0x66, 0, 1, val, 2);
2514 #endif
2515                 return 0;
2516         }
2517
2518         /* S5PC110 */
2519         if (board_is_limo_universal() ||
2520                 board_is_limo_real() ||
2521                 board_is_media()) {
2522                 /* check usb path */
2523                 if (board_is_limo_real() && !hwrevision(6))
2524                         check_mhl();
2525         }
2526
2527         if (machine_is_tickertape())
2528                 /* USB_SEL: XM0ADDR_0: MP04[0] output mode */
2529                 gpio_direction_output(&s5pc110_gpio->gpio_mp0_4, 0, 0);
2530
2531         /* USB Path to AP */
2532         micro_usb_switch(0);
2533
2534         return 0;
2535 }
2536 #endif
2537
2538 #ifdef CONFIG_GENERIC_MMC
2539 int s5p_no_mmc_support(void)
2540 {
2541         if (machine_is_wmg160())
2542                 return 1;
2543         return 0;
2544 }
2545
2546 int board_mmc_init(bd_t *bis)
2547 {
2548         unsigned int reg;
2549         unsigned int clock;
2550         struct s5pc110_clock *clk = (struct s5pc110_clock *)S5PC1XX_CLOCK_BASE;
2551         int i;
2552
2553         /* MASSMEMORY_EN: XMSMDATA7: GPJ2[7] output high */
2554         if (machine_is_kessler())
2555                 gpio_direction_output(&s5pc110_gpio->gpio_j2, 7, 1);
2556
2557         if (machine_is_wmg160())
2558                 return -1;
2559
2560         /* MMC0 Clock source = SCLKMPLL */
2561         reg = readl(&clk->src4);
2562         reg &= ~0xf;
2563         reg |= 0x6;
2564         writel(reg, &clk->src4);
2565
2566         reg = readl(&clk->div4);
2567         reg &= ~0xf;
2568
2569         /* set div value near 50MHz */
2570         clock = get_pll_clk(MPLL) / 1000000;
2571         for (i = 0; i < 0xf; i++) {
2572                 if ((clock / (i + 1)) <= 50) {
2573                         reg |= i << 0;
2574                         break;
2575                 }
2576         }
2577
2578         writel(reg, &clk->div4);
2579
2580         /*
2581          * MMC0 GPIO
2582          * GPG0[0]      SD_0_CLK
2583          * GPG0[1]      SD_0_CMD
2584          * GPG0[2]      SD_0_CDn        -> Not used
2585          * GPG0[3:6]    SD_0_DATA[0:3]
2586          */
2587         for (i = 0; i < 7; i++) {
2588                 if (i == 2)
2589                         continue;
2590                 /* GPG0[0:6] special function 2 */
2591                 gpio_cfg_pin(&s5pc110_gpio->gpio_g0, i, 0x2);
2592                 /* GPG0[0:6] pull disable */
2593                 gpio_set_pull(&s5pc110_gpio->gpio_g0, i, GPIO_PULL_NONE);
2594                 /* GPG0[0:6] drv 4x */
2595                 gpio_set_drv(&s5pc110_gpio->gpio_g0, i, GPIO_DRV_4X);
2596         }
2597
2598         return s5pc1xx_mmc_init(0);
2599 }
2600 #endif
2601
2602 #ifdef CONFIG_CMD_PMIC
2603 static int pmic_status(void)
2604 {
2605         unsigned char addr, val[2];
2606         int reg, i;
2607
2608         i2c_set_bus_num(I2C_PMIC);
2609         addr = 0xCC >> 1;
2610         if (i2c_probe(addr)) {
2611                 printf("Can't found max8998\n");
2612                 return -1;
2613         }
2614
2615         reg = 0x11;
2616         i2c_read(addr, reg, 1, val, 1);
2617         for (i = 7; i >= 4; i--)
2618                 printf("BUCK%d %s\n", 7 - i + 1,
2619                         val[0] & (1 << i) ? "on" : "off");
2620         for (; i >= 0; i--)
2621                 printf("LDO%d %s\n", 5 - i,
2622                         val[0] & (1 << i) ? "on" : "off");
2623         reg = 0x12;
2624         i2c_read(addr, reg, 1, val, 1);
2625         for (i = 7; i >= 0; i--)
2626                 printf("LDO%d %s\n", 7 - i + 6,
2627                         val[0] & (1 << i) ? "on" : "off");
2628         reg = 0x13;
2629         i2c_read(addr, reg, 1, val, 1);
2630         for (i = 7; i >= 4; i--)
2631                 printf("LDO%d %s\n", 7 - i + 14,
2632                         val[0] & (1 << i) ? "on" : "off");
2633
2634         reg = 0xd;
2635         i2c_read(addr, reg, 1, val, 1);
2636         for (i = 7; i >= 6; i--)
2637                 printf("SAFEOUT%d %s\n", 7 - i + 1,
2638                         val[0] & (1 << i) ? "on" : "off");
2639         return 0;
2640 }
2641
2642 static int pmic_ldo_control(int buck, int ldo, int safeout, int on)
2643 {
2644         unsigned char addr, val[2];
2645         unsigned int reg, shift;
2646
2647         if (ldo) {
2648                 if (ldo < 2)
2649                         return -1;
2650                 if (ldo <= 5) {
2651                         reg = 0x11;
2652                         shift = 5 - ldo;
2653                 } else if (ldo <= 13) {
2654                         reg = 0x12;
2655                         shift = 13 - ldo;
2656                 } else if (ldo <= 17) {
2657                         reg = 0x13;
2658                         shift = 17 - ldo + 4;
2659                 } else
2660                         return -1;
2661         } else if (buck) {
2662                 if (buck > 4)
2663                         return -1;
2664                 reg = 0x11;
2665                 shift = 4 - buck + 4;
2666         } else if (safeout) {
2667                 if (safeout > 3)
2668                         return -1;
2669                 reg = 0xd;
2670                 shift = 8 - safeout;
2671         } else
2672                 return -1;
2673
2674         i2c_set_bus_num(I2C_PMIC);
2675         addr = 0xCC >> 1;
2676         if (i2c_probe(addr)) {
2677                 printf("Can't found max8998\n");
2678                 return -1;
2679         }
2680
2681         i2c_read(addr, reg, 1, val, 1);
2682         if (on)
2683                 val[0] |= (1 << shift);
2684         else
2685                 val[0] &= ~(1 << shift);
2686         i2c_write(addr, reg, 1, val, 1);
2687         i2c_read(addr, reg, 1, val, 1);
2688
2689         if (ldo)
2690                 printf("ldo %d value 0x%x, %s\n", ldo, val[0],
2691                         val[0] & (1 << shift) ? "on" : "off");
2692         else if (buck)
2693                 printf("buck %d value 0x%x, %s\n", buck, val[0],
2694                         val[0] & (1 << shift) ? "on" : "off");
2695         else if (safeout)
2696                 printf("safeout %d value 0x%x, %s\n", safeout, val[0],
2697                         val[0] & (1 << shift) ? "on" : "off");
2698
2699         return 0;
2700 }
2701
2702 static int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
2703 {
2704         int buck = 0, ldo = 0, safeout = 0, on = -1;
2705
2706         switch (argc) {
2707         case 2:
2708                 if (strncmp(argv[1], "status", 6) == 0)
2709                         return pmic_status();
2710                 break;
2711         case 4:
2712                 if (strncmp(argv[1], "ldo", 3) == 0)
2713                         ldo = simple_strtoul(argv[2], NULL, 10);
2714                 else if (strncmp(argv[1], "buck", 4) == 0)
2715                         buck = simple_strtoul(argv[2], NULL, 10);
2716                 else if (strncmp(argv[1], "safeout", 7) == 0)
2717                         safeout = simple_strtoul(argv[2], NULL, 10);
2718                 else
2719                         break;
2720
2721                 if (strncmp(argv[3], "on", 2) == 0)
2722                         on = 1;
2723                 else if (strncmp(argv[3], "off", 3) == 0)
2724                         on = 0;
2725                 else
2726                         break;
2727
2728                 return pmic_ldo_control(buck, ldo, safeout, on);
2729
2730         default:
2731                 break;
2732         }
2733
2734         cmd_usage(cmdtp);
2735         return 1;
2736 }
2737
2738 U_BOOT_CMD(
2739         pmic,           CONFIG_SYS_MAXARGS,     1, do_pmic,
2740         "PMIC LDO & BUCK control",
2741         "status - Display PMIC LDO & BUCK status\n"
2742         "pmic ldo num on/off - Turn on/off the LDO\n"
2743         "pmic buck num on/off - Turn on/off the BUCK\n"
2744         "pmic safeout num on/off - Turn on/off the SAFEOUT\n"
2745 );
2746 #endif
2747
2748 #ifdef CONFIG_CMD_DEVICE_POWER
2749
2750 enum {
2751         POWER_NONE,
2752         POWER_TOUCH,
2753         POWER_3_TOUCHKEY,
2754         POWER_LCD,
2755         POWER_HAPTIC,
2756         POWER_AUDIO_CODEC,
2757         POWER_FM_RADIO,
2758         POWER_BT_WIFI,
2759         POWER_HDMI,
2760 };
2761
2762 static void power_display_devices(void)
2763 {
2764         printf("devices:\n");
2765         printf("\t%d - touch\n", POWER_TOUCH);
2766         printf("\t%d - 3 touchkey\n", POWER_3_TOUCHKEY);
2767         printf("\t%d - LCD\n", POWER_LCD);
2768         printf("\t%d - Haptic\n", POWER_HAPTIC);
2769         printf("\t%d - Audio Codec\n", POWER_AUDIO_CODEC);
2770         printf("\t%d - FM Radio\n", POWER_FM_RADIO);
2771         printf("\t%d - BT/WiFi\n", POWER_BT_WIFI);
2772         printf("\t%d - HDMI\n", POWER_HDMI);
2773 }
2774
2775 static int power_hdmi(int on)
2776 {
2777         /* HDMI_EN1: GPJ2[2] */
2778         gpio_direction_output(&s5pc110_gpio->gpio_j2, 2, on);
2779         /* MHL_ON: GPJ2[3] */
2780         gpio_direction_output(&s5pc110_gpio->gpio_j2, 3, on);
2781         return 0;
2782 }
2783
2784 static int power_bt_wifi(int on)
2785 {
2786         /* WLAN_BT_EN: GPB[5] */
2787         gpio_direction_output(&s5pc110_gpio->gpio_b, 5, on);
2788         return 0;
2789 }
2790
2791 static int power_fm_radio(int on)
2792 {
2793         /* FM_BUS_nRST: GPJ2[5] */
2794         gpio_direction_output(&s5pc110_gpio->gpio_j2, 5, !on);
2795         return 0;
2796 }
2797
2798 static int power_audio_codec(int on)
2799 {
2800         /* CODEC_LDO_EN: GPF3[4] */
2801         gpio_direction_output(&s5pc110_gpio->gpio_f3, 4, on);
2802         /* CODEC_XTAL_EN: GPH3[2] */
2803         gpio_direction_output(&s5pc110_gpio->gpio_h3, 2, on);
2804         return 0;
2805 }
2806
2807 static int power_haptic(int on)
2808 {
2809         /* HAPTIC_ON: GPJ1[1] */
2810         gpio_direction_output(&s5pc110_gpio->gpio_j1, 1, on);
2811         return 0;
2812 }
2813
2814 static int power_lcd(int on)
2815 {
2816         /* MLCD_ON: GPJ1[3] */
2817         gpio_direction_output(&s5pc110_gpio->gpio_j1, 3, on);
2818         return 0;
2819 }
2820
2821 static int power_touch(int on)
2822 {
2823         /* TOUCH_EN: GPG3[6] */
2824         gpio_direction_output(&s5pc110_gpio->gpio_g3, 6, on);
2825         return 0;
2826 }
2827
2828 static int power_3_touchkey(int on)
2829 {
2830         if (on) {
2831                 /* 3_TOUCH_EN - GPJ3[0] : (J1B2) */
2832                 /* 3_TOUCH_EN - GPJ3[5] : (not J1B2) */
2833                 if (board_rev & J1_B2_BOARD)
2834                         gpio_direction_output(&s5pc110_gpio->gpio_j3, 0, on);
2835                 else
2836                         gpio_direction_output(&s5pc110_gpio->gpio_j3, 5, on);
2837
2838                 /* 3_TOUCH_CE - GPJ2[6] */
2839                 gpio_direction_output(&s5pc110_gpio->gpio_j2, 6, on);   /* TOUCH_CE */
2840         } else {
2841                 /* 3_TOUCH_CE - GPJ2[6] */
2842                 gpio_direction_output(&s5pc110_gpio->gpio_j2, 6, on);   /* TOUCH_CE */
2843         }
2844
2845         if (on) {
2846                 unsigned int reg;
2847                 unsigned char val[2];
2848                 unsigned char addr = 0x20;              /* mcs5000 3-touchkey */
2849
2850                 /* Require 100ms */
2851                 udelay(80 * 1000);
2852
2853                 /* 3 touchkey */
2854                 i2c_set_bus_num(I2C_GPIO10);
2855
2856                 /* Workaround to probe */
2857                 if (i2c_probe(addr)) {
2858                         if (i2c_probe(addr)) {
2859                                 printf("Can't found 3 touchkey\n");
2860                                 return -ENODEV;
2861                         }
2862                 }
2863
2864 #define MCS5000_TK_HW_VERSION  0x06
2865 #define MCS5000_TK_FW_VERSION  0x0A
2866 #define MCS5000_TK_MI_VERSION  0x0B
2867
2868                 reg = MCS5000_TK_MI_VERSION;
2869                 i2c_read(addr, reg, 1, val, 1);
2870                 printf("3-touchkey:\tM/I 0x%x, ", val[0]);
2871                 reg = MCS5000_TK_HW_VERSION;
2872                 i2c_read(addr, reg, 1, val, 1);
2873                 printf("H/W 0x%x, ", val[0]);
2874                 reg = MCS5000_TK_FW_VERSION;
2875                 i2c_read(addr, reg, 1, val, 1);
2876                 printf("F/W 0x%x\n", val[0]);
2877         }
2878         return 0;
2879 }
2880
2881 static int power_control(int device, int on)
2882 {
2883         switch (device) {
2884         case POWER_TOUCH:
2885                 return power_touch(on);
2886         case POWER_3_TOUCHKEY:
2887                 return power_3_touchkey(on);
2888         case POWER_LCD:
2889                 return power_lcd(on);
2890         case POWER_HAPTIC:
2891                 return power_haptic(on);
2892         case POWER_AUDIO_CODEC:
2893                 return power_audio_codec(on);
2894         case POWER_FM_RADIO:
2895                 return power_fm_radio(on);
2896         case POWER_BT_WIFI:
2897                 return power_bt_wifi(on);
2898         case POWER_HDMI:
2899                 return power_hdmi(on);
2900         default:
2901                 printf("I don't know device %d\n", device);
2902                 break;
2903         }
2904         return 0;
2905 }
2906
2907 static int power_on(int on)
2908 {
2909         power_touch(on);
2910         power_3_touchkey(on);
2911         power_lcd(on);
2912         power_haptic(on);
2913         power_audio_codec(on);
2914         power_fm_radio(on);
2915         power_bt_wifi(on);
2916         power_hdmi(on);
2917
2918         return 0;
2919 }
2920
2921 static int do_power(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
2922 {
2923         int device, on;
2924
2925         if (!machine_is_aquila() && !machine_is_kessler())
2926                 goto out;
2927
2928         switch (argc) {
2929         case 2:
2930                 if (strncmp(argv[1], "on", 2) == 0)
2931                         return power_on(1);
2932                 if (strncmp(argv[1], "off", 3) == 0)
2933                         return power_on(0);
2934                 break;
2935         case 3:
2936                 device = simple_strtoul(argv[1], NULL, 10);
2937                 if (device < 0)
2938                         break;
2939
2940                 if (strncmp(argv[2], "on", 2) == 0)
2941                         on = 1;
2942                 else if (strncmp(argv[2], "off", 3) == 0)
2943                         on = 0;
2944                 else
2945                         break;
2946                 return power_control(device, on);
2947         default:
2948                 break;
2949         }
2950 out:
2951         cmd_usage(cmdtp);
2952         power_display_devices();
2953         return 1;
2954 }
2955
2956 U_BOOT_CMD(
2957         power,          CONFIG_SYS_MAXARGS,     1, do_power,
2958         "Device Power Management control",
2959         "device on/off - Turn on/off the device\n"
2960 );
2961
2962 static int do_microusb(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
2963 {
2964         switch (argc) {
2965         case 2:
2966                 if (strncmp(argv[1], "cp", 2) == 0) {
2967                         micro_usb_switch(1);
2968                         pmic_ldo_control(0, 0, 2, 1);
2969                         setenv("usb", "cp");
2970                 } else if (strncmp(argv[1], "ap", 2) == 0) {
2971                         micro_usb_switch(0);
2972                         pmic_ldo_control(0, 0, 2, 0);
2973                         setenv("usb", "ap");
2974                 }
2975                 break;
2976         default:
2977                 cmd_usage(cmdtp);
2978                 return 1;
2979         }
2980
2981         saveenv();
2982
2983         printf("USB Path is set to %s\n", getenv("usb"));
2984
2985         return 0;
2986 }
2987
2988 U_BOOT_CMD(
2989         microusb,               CONFIG_SYS_MAXARGS,     1, do_microusb,
2990         "Micro USB Switch",
2991         "cp - switch to CP\n"
2992         "microusb ap - switch to AP\n"
2993 );
2994 #endif