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