01611322afcab90ecee85197450d2946cb8f1a72
[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                 puts("Auto burning bootloader\n");
721                 count += sprintf(buf + count, "run updateb; ");
722         }
723         if (readl(magic_base + 0x04) == 0x4b65726e) {   /* ASICC: Kern */
724                 puts("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() && hwrevision(0)) {
904                         /* home & volume down */
905                         if ((row_state[1] & 0x1) && (row_state[1] & 0x2))
906                                 auto_download = 1;
907                 } else if (board_is_neptune() && hwrevision(2)) {
908                         /* cam full shot & volume down */
909                         if ((row_state[1] & 0x6) && (row_state[2] & 0x4))
910                                 auto_download = 1;
911                 } else {
912                         /* cam full shot & volume down */
913                         if ((row_state[0] & 0x1) && (row_state[1] & 0x2))
914                                 auto_download = 1;
915                 }
916         } else if (mach_is_geminus())
917                 /* volume down & home */
918                 if ((row_state[1] & 0x2) && (row_state[2] & 0x1))
919                         auto_download = 1;
920
921         if (auto_download)
922                 setenv("bootcmd", "usbdown");
923 }
924
925 static void check_battery(int mode)
926 {
927         unsigned char val[2];
928         unsigned char addr = 0x36;      /* max17040 fuel gauge */
929
930         i2c_set_bus_num(I2C_GPIO3);
931
932         if (mach_is_aquila()) {
933                 if (board_is_j1b2())
934                         return;
935         } else if (mach_is_kessler()) {
936                 i2c_set_bus_num(I2C_GPIO7);
937         } else if (mach_is_cypress()) {
938                 i2c_set_bus_num(I2C_GPIO7);
939         } else if (mach_is_geminus()) {
940                 if (hwrevision(1))
941                         i2c_set_bus_num(I2C_GPIO7);
942         } else
943                 return;
944
945         if (i2c_probe(addr)) {
946                 puts("Can't found max17040 fuel gauge\n");
947                 return;
948         }
949
950         /* mode 0: check mode / 1: enable mode */
951         if (mode) {
952                 val[0] = 0x54;
953                 val[1] = 0x00;
954                 i2c_write(addr, 0xfe, 1, val, 2);
955         } else {
956                 i2c_read(addr, 0x04, 1, val, 1);
957                 dprintf("battery:\t%d%%\n", val[0]);
958                 battery_soc = val[0];
959         }
960 }
961
962 static void check_mhl(void)
963 {
964         unsigned char val[2];
965         unsigned char addr = 0x39;      /* SIL9230 */
966
967         /* MHL Power enable */
968         /* HDMI_EN : GPJ2[2] XMSMDATA_2 output mode */
969         gpio_direction_output(&s5pc110_gpio->gpio_j2, 2, 1);
970
971         /* MHL_RST : MP0_4[7] XM0ADDR_7 output mode */
972         gpio_direction_output(&s5pc110_gpio->gpio_mp0_4, 7, 0);
973
974         /* 10ms required after reset */
975         udelay(10000);
976
977         /* output enable */
978         gpio_set_value(&s5pc110_gpio->gpio_mp0_4, 7, 1);
979
980         i2c_set_bus_num(I2C_GPIO5);
981
982         /* set usb path */
983         if (i2c_probe(addr)) {
984                 puts("Can't found MHL Chip\n");
985                 return;
986         }
987
988         /*
989          * System Control #1
990          * set to Normal operation
991          */
992         val[0] = 0x35;
993         i2c_write((0x72 >> 1), 0x08, 1, val, 1);
994         i2c_read((0x72 >> 1), 0x08, 1, val, 1);
995
996         /*
997          * MHL TX Control #1
998          * TERM_MODE [7:6]
999          * 00 = MHL termination ON
1000          * 11 = MHL termination OFF
1001          */
1002         val[0] = 0xd0;
1003         i2c_write((0x72 >> 1), 0xa0, 1, val, 1);
1004         i2c_read((0x72 >> 1), 0xa0, 1, val, 1);
1005 }
1006
1007 static int max8998_probe(void)
1008 {
1009         unsigned char addr = 0xCC >> 1;
1010
1011         i2c_set_bus_num(I2C_PMIC);
1012
1013         if (i2c_probe(addr)) {
1014                 puts("Can't found max8998\n");
1015                 return 1;
1016         }
1017
1018         return 0;
1019 }
1020
1021 #define CHARGER_ANIMATION_FRAME         6
1022 static void max8998_clear_interrupt(void)
1023 {
1024         unsigned char addr, val[2];
1025         addr = 0xCC >> 1;
1026
1027         if (max8998_probe())
1028                 return;
1029
1030         i2c_read(addr, 0x00, 1, val, 1);
1031         i2c_read(addr, 0x01, 1, val, 1);
1032         i2c_read(addr, 0x02, 1, val, 1);
1033         i2c_read(addr, 0x03, 1, val, 1);
1034 }
1035
1036 static int max8998_power_key(void)
1037 {
1038         unsigned char addr, val[2];
1039         addr = 0xCC >> 1;
1040
1041         if (max8998_probe())
1042                 return 0;
1043
1044         /* Accessing IRQ1 register */
1045         i2c_read(addr, 0x00, 1, val, 1);
1046         if (val[0] & (1 << 6))
1047                 return 1;
1048
1049         return 0;
1050 }
1051
1052 static int max8998_has_ext_power_source(void)
1053 {
1054         unsigned char addr, val[2];
1055         addr = 0xCC >> 1;
1056
1057         if (max8998_probe())
1058                 return 0;
1059
1060         /* Accessing STATUS2 register */
1061         i2c_read(addr, 0x09, 1, val, 1);
1062         if (val[0] & (1 << 5))
1063                 return 1;
1064
1065         return 0;
1066 }
1067
1068 struct thermister_stat {
1069         short centigrade;
1070         unsigned short adc;
1071 };
1072
1073 static struct thermister_stat adc_to_temperature_data[] = {
1074         { .centigrade = -20,    .adc = 1856, },
1075         { .centigrade = -15,    .adc = 1799, },
1076         { .centigrade = -10,    .adc = 1730, },
1077         { .centigrade = -5,     .adc = 1649, },
1078         { .centigrade = 0,      .adc = 1556, },
1079         { .centigrade = 5,      .adc = 1454, },
1080         { .centigrade = 10,     .adc = 1343, },
1081         { .centigrade = 15,     .adc = 1227, },
1082         { .centigrade = 20,     .adc = 1109, },
1083         { .centigrade = 25,     .adc = 992, },
1084         { .centigrade = 30,     .adc = 880, },
1085         { .centigrade = 35,     .adc = 773, },
1086         { .centigrade = 40,     .adc = 675, },
1087         { .centigrade = 45,     .adc = 586, },
1088         { .centigrade = 50,     .adc = 507, },
1089         { .centigrade = 55,     .adc = 436, },
1090         { .centigrade = 58,     .adc = 399, },
1091         { .centigrade = 63,     .adc = 343, },
1092         { .centigrade = 65,     .adc = 322, },
1093 };
1094
1095 #ifndef USHRT_MAX
1096 #define USHRT_MAX       0xFFFFU
1097 #endif
1098 static int adc_to_temperature_centigrade(unsigned short adc)
1099 {
1100         int i;
1101         int approximation;
1102         /* low_*: Greatest Lower Bound,
1103          *          *          *          * high_*: Smallest Upper Bound */
1104         int low_temp = 0, high_temp = 0;
1105         unsigned short low_adc = 0, high_adc = USHRT_MAX;
1106         for (i = 0; i < ARRAY_SIZE(adc_to_temperature_data); i++) {
1107                 if (adc_to_temperature_data[i].adc <= adc &&
1108                                 adc_to_temperature_data[i].adc >= low_adc) {
1109                         low_temp = adc_to_temperature_data[i].centigrade;
1110                         low_adc = adc_to_temperature_data[i].adc;
1111                 }
1112                 if (adc_to_temperature_data[i].adc >= adc &&
1113                                 adc_to_temperature_data[i].adc <= high_adc) {
1114                         high_temp = adc_to_temperature_data[i].centigrade;
1115                         high_adc = adc_to_temperature_data[i].adc;
1116                 }
1117         }
1118
1119         /* Linear approximation between cloest low and high,
1120          * which is the weighted average of the two. */
1121
1122         /* The following equation is correct only when the two are different */
1123         if (low_adc == high_adc)
1124                 return low_temp;
1125         if (ARRAY_SIZE(adc_to_temperature_data) < 2)
1126                 return 20; /* The room temperature */
1127         if (low_adc == 0)
1128                 return high_temp;
1129         if (high_adc == USHRT_MAX)
1130                 return low_temp;
1131
1132         approximation = low_temp * (adc - low_adc) +
1133                 high_temp * (high_adc - adc);
1134         approximation /= high_adc - low_adc;
1135
1136         return approximation;
1137 }
1138
1139 static unsigned short get_adc_value(int channel)
1140 {
1141         struct s5pc110_adc *adc = (struct s5pc110_adc *) S5PC110_ADC_BASE;
1142         unsigned short ret = 0;
1143         unsigned int reg;
1144         int ldonum = 8;
1145         char buf[64];
1146         unsigned int loop = 0;
1147
1148         if (mach_is_kessler())
1149                 ldonum = 4;
1150         else if (mach_is_geminus())
1151                 ldonum = 4;
1152         else if (mach_is_wmg160())
1153                 ldonum = 4;
1154         else if (mach_is_cypress())
1155                 ldonum = 8;
1156         else if (mach_is_tickertape())
1157                 ldonum = 8;
1158         else if (mach_is_aquila())
1159                 ldonum = 8;
1160         /*
1161         else if (mach_is_p1p2())
1162                 ldonum = 4;
1163         */
1164
1165         sprintf(buf, "pmic ldo %d on", ldonum);
1166         run_command(buf, 0);
1167
1168         writel(channel & 0xF, &adc->adcmux);
1169         writel((1 << 14) | (49 << 6), &adc->adccon);
1170         writel(1000 & 0xffff, &adc->adcdly);
1171         writel(readl(&adc->adccon) | (1 << 16), &adc->adccon); /* 12 bit */
1172         udelay(10);
1173         writel(readl(&adc->adccon) | (1 << 0), &adc->adccon); /* Enable */
1174         udelay(10);
1175
1176         do {
1177                 udelay(1);
1178                 reg = readl(&adc->adccon);
1179         } while (!(reg & (1 << 15)) && (loop++ < 1000));
1180
1181         ret = readl(&adc->adcdat0) & 0xFFF;
1182         sprintf(buf, "pmic ldo %d off", ldonum);
1183         run_command(buf, 0);
1184
1185         return ret;
1186 }
1187
1188 static int adc_get_average_ambient_temperature(void)
1189 {
1190         if (mach_is_kessler()) {
1191                 unsigned short min = USHRT_MAX;
1192                 unsigned short max = 0;
1193                 unsigned int sum = 0;
1194                 unsigned int measured = 0;
1195                 int i;
1196
1197                 for (i = 0; i < 7; i++) {
1198                         unsigned short measurement = get_adc_value(6);
1199                         sum += measurement;
1200                         measured++;
1201                         if (min > measurement)
1202                                 min = measurement;
1203                         if (max < measurement)
1204                                 max = measurement;
1205                 }
1206                 if (measured >= 3) {
1207                         measured -= 2;
1208                         sum -= min;
1209                         sum -= max;
1210                 }
1211                 sum /= measured;
1212                 printf("Average Ambient Temperature = %d(ADC=%d)\n",
1213                                 adc_to_temperature_centigrade(sum), sum);
1214                 return adc_to_temperature_centigrade(sum);
1215         }
1216
1217         return 20; /* 20 Centigrade */
1218 }
1219
1220 enum temperature_level {
1221         _TEMP_OK,
1222         _TEMP_OK_HIGH,
1223         _TEMP_OK_LOW,
1224         _TEMP_TOO_HIGH,
1225         _TEMP_TOO_LOW,
1226 };
1227
1228 static enum temperature_level temperature_check(void)
1229 {
1230         int temp = adc_get_average_ambient_temperature();
1231         if (temp < -5)
1232                 return _TEMP_TOO_LOW;
1233         if (temp < 0)
1234                 return _TEMP_OK_LOW;
1235         if (temp > 63)
1236                 return _TEMP_TOO_HIGH;
1237         if (temp > 58)
1238                 return _TEMP_OK_HIGH;
1239         return _TEMP_OK;
1240 }
1241
1242 extern void lcd_display_clear(void);
1243 extern int lcd_display_bitmap(ulong bmp_image, int x, int y);
1244
1245 static void charger_en(int enable)
1246 {
1247         /* 0: disable
1248          * 600: 600mA
1249          * 475: 475mA
1250          */
1251         unsigned char addr = 0xCC >> 1; /* max8998 */
1252         unsigned char val[2];
1253
1254         if (max8998_probe())
1255                 return;
1256
1257         if (!enable) {
1258                 puts("Disable the charger.\n");
1259                 i2c_read(addr, 0x0D, 1, val, 1);
1260                 val[0] &= ~(0x1);
1261                 val[0] |= 0x1;
1262                 i2c_write(addr, 0x0D, 1, val, 1);
1263         } else {
1264                 i2c_read(addr, 0x0C, 1, val, 1);
1265                 val[0] &= ~(0x7 << 0);
1266                 val[0] &= ~(0x7 << 5);
1267                 if (enable == 600) {
1268                         val[0] |= 5; /* 600mA */
1269                         val[0] |= (3 << 5); /* Stop at 150mA (25%) */
1270                 } else { /* Assume 475 mA */
1271                         enable = 475;
1272                         val[0] |= 2; /* 475mA */
1273                         val[0] |= (4 << 5); /* Stop at 142.5mA (30%) */
1274                 }
1275                 i2c_write(addr, 0x0C, 1, val, 1);
1276
1277                 i2c_read(addr, 0x0D, 1, val, 1);
1278                 val[0] &= ~(0x1);
1279                 i2c_write(addr, 0x0D, 1, val, 1);
1280                 printf("Enable the charger @ %dmA\n", enable);
1281         }
1282 }
1283
1284 void lcd_power_on(unsigned int onoff);
1285 extern int do_sleep (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
1286 extern int drv_lcd_init_resume (void);
1287
1288 static void into_charge_mode(void)
1289 {
1290         unsigned int level;
1291         int i, j;
1292         bmp_image_t *bmp;
1293         unsigned long len;
1294         ulong bmp_addr[CHARGER_ANIMATION_FRAME];
1295         unsigned int reg, wakeup_stat;
1296         int charger_speed = 600;
1297
1298         max8998_clear_interrupt();
1299
1300         puts("Charge Mode\n");
1301         charger_en(charger_speed);
1302
1303 #ifdef CONFIG_S5PC1XXFB
1304         init_font();
1305
1306         /* TODO: write the image-text for the charger */
1307
1308         level = battery_soc * CHARGER_ANIMATION_FRAME / 100;
1309         if (level >= CHARGER_ANIMATION_FRAME)
1310                 level = CHARGER_ANIMATION_FRAME - 1;
1311
1312         for (i = 0; i < CHARGER_ANIMATION_FRAME; i++)
1313                 bmp_addr[i] = (ulong)battery_charging_animation[i];
1314
1315         lcd_display_clear();
1316         for (i = 0; i < 3; i++) {
1317                 for (j = level; j < CHARGER_ANIMATION_FRAME; j++) {
1318                         int k;
1319
1320                         bmp = gunzip_bmp(bmp_addr[j], &len);
1321                         lcd_display_bitmap((ulong)bmp, 140, 202);
1322                         free(bmp);
1323
1324                         for (k = 0; k < 10; k++) {
1325                                 if (max8998_power_key()) {
1326                                         lcd_display_clear();
1327                                         goto restore_screen;
1328                                 } else if (!max8998_has_ext_power_source()) {
1329                                         lcd_display_clear();
1330                                         goto restore_screen;
1331                                 } else {
1332                                         udelay(100 * 1000);
1333                                 }
1334                         }
1335                 }
1336         }
1337         exit_font();
1338
1339         /* Disable the display to prevent flickering */
1340         /* TODO: how to reenable the display later? */
1341         lcd_power_on(0);
1342 #endif
1343
1344         do {
1345                 struct s5pc110_rtc *rtc = (struct s5pc110_rtc *)S5PC110_RTC_BASE;
1346                 unsigned int org, org_ip3;
1347                 enum temperature_level  previous_state = _TEMP_OK;
1348
1349                 empty_device_info_buffer();
1350                 if (max8998_power_key())
1351                         break;
1352                 else if (!max8998_has_ext_power_source())
1353                         break;
1354
1355                 /* Enable RTC, SYSTIMER, ADC at CLKGATE IP3 */
1356                 org_ip3 = readl(0xE010046C);
1357                 writel(org_ip3 | (1 << 15) | (1 << 16) | (1 << 24), 0xE010046C);
1358
1359                 reg = org = readl(&rtc->rtccon);
1360                 reg |= (1 << 0);
1361                 writel(reg, &rtc->rtccon);
1362
1363                 reg = readl(&rtc->rtccon);
1364                 writel(reg | (1 << 3), &rtc->rtccon);
1365                 udelay(10);
1366                 writel(reg & ~(1 << 3), &rtc->rtccon);
1367                 udelay(10);
1368
1369                 reg = readl(&rtc->rtccon);
1370                 reg &= ~((1 << 8) | (0xF << 4));
1371                 reg |= (1 << 8) | (0xD << 4); /* D: 4 Hz, 9: 64 Hz */
1372                 writel(reg, &rtc->rtccon);
1373
1374                 reg = 15 * 4 - 1; /* 15 sec */
1375                 writel(reg, &rtc->ticcnt);
1376
1377                 /* EVT0: sleep 1, EVT1: sleep */
1378                 if (cpu_is_s5pc110()) {
1379                         char *name = "dummy";
1380                         char *usage = "N/A";
1381                         char *help = NULL;
1382                         cmd_tbl_t ctt;
1383                         ctt.name = name;
1384                         ctt.usage = usage;
1385                         ctt.help = help;
1386
1387                         if (s5pc1xx_get_cpu_rev() == 0) {
1388                                 char *argv[] = {"1", "1"};
1389                                 wakeup_stat = do_sleep(&ctt, 0, 2, argv);
1390                         } else {
1391                                 char *argv[] = {"0", "0"};
1392                                 wakeup_stat = do_sleep(&ctt, 0, 1, argv);
1393                         }
1394                 } else {
1395                         puts("\n\n\nERROR: this is not S5PC110.\n\n\n");
1396                         return;
1397                 }
1398
1399                 /* Check TEMP HIGH/LOW */
1400                 switch (temperature_check()) {
1401                 case _TEMP_OK:
1402                         charger_en(charger_speed);
1403                         previous_state = _TEMP_OK;
1404                         break;
1405                 case _TEMP_TOO_LOW:
1406                         charger_en(0);
1407                         previous_state = _TEMP_TOO_LOW;
1408                         break;
1409                 case _TEMP_TOO_HIGH:
1410                         charger_en(0);
1411                         previous_state = _TEMP_TOO_HIGH;
1412                         break;
1413                 case _TEMP_OK_LOW:
1414                         if (previous_state == _TEMP_TOO_LOW) {
1415                                 charger_en(0);
1416                         } else {
1417                                 charger_en(charger_speed);
1418                                 previous_state = _TEMP_OK;
1419                         }
1420                         break;
1421                 case _TEMP_OK_HIGH:
1422                         if (previous_state == _TEMP_TOO_HIGH) {
1423                                 charger_en(0);
1424                         } else {
1425                                 charger_en(charger_speed);
1426                                 previous_state = _TEMP_OK;
1427                         }
1428                         break;
1429                 }
1430
1431                 writel(org, &rtc->rtccon);
1432                 writel(org_ip3, 0xE010046C);
1433
1434         } while (wakeup_stat == 0x04); /* RTC TICK */
1435
1436 #ifdef CONFIG_S5PC1XXFB
1437 restore_screen:
1438         /* TODO: Reenable logo display (not working yet) */
1439         lcd_power_on(1);
1440         drv_lcd_init_resume();
1441 #endif
1442 }
1443
1444 #define S5PC110_RST_STAT        0xE010A000
1445
1446 #define SWRESET                 (1 << 3)
1447 #define WDTRESET                (1 << 2)
1448 #define WARMRESET               (1 << 1)
1449 #define EXTRESET                (1 << 0)
1450
1451 static int get_reset_status(void)
1452 {
1453         return readl(S5PC110_RST_STAT) & 0xf;
1454 }
1455
1456 static int fsa9480_probe(void)
1457 {
1458         unsigned char addr = 0x25;
1459
1460         if (cpu_is_s5pc100())
1461                 return 1;
1462
1463         if (board_is_limo_real()) {
1464                 if (hwrevision(0) || hwrevision(1))
1465                         return 1;
1466         }
1467
1468         i2c_set_bus_num(I2C_PMIC);
1469
1470         if (mach_is_kessler()) {
1471                 i2c_set_bus_num(I2C_GPIO6);
1472         } else if (mach_is_cypress()) {
1473                 i2c_set_bus_num(I2C_GPIO6);
1474         } else if (mach_is_geminus()) {
1475                 if (hwrevision(1))
1476                         i2c_set_bus_num(I2C_GPIO6);
1477         } else if (mach_is_wmg160()) {
1478                 i2c_set_bus_num(I2C_GPIO6);
1479         }
1480
1481         if (i2c_probe(addr)) {
1482                 puts("Can't found fsa9480\n");
1483                 return 1;
1484         }
1485
1486         return 0;
1487 }
1488
1489 static void check_micro_usb(int intr)
1490 {
1491         unsigned char addr;
1492         unsigned char val[2];
1493         static int started_charging_once = 0;
1494         char *path;
1495
1496         if (fsa9480_probe())
1497                 return;
1498
1499         addr = 0x25;            /* fsa9480 */
1500
1501         /* Clear Interrupt */
1502         if (intr) {
1503                 i2c_read(addr, 0x03, 1, val, 2);
1504                 return;
1505         }
1506
1507         /* Read Device Type 1 */
1508         i2c_read(addr, 0x0a, 1, val, 1);
1509
1510 #define FSA_DEV1_CHARGER        (1 << 6)
1511 #define FSA_DEV1_UART           (1 << 3)
1512 #define FSA_DEV1_USB            (1 << 2)
1513 #define FSA_DEV2_JIG_USB_OFF    (1 << 1)
1514 #define FSA_DEV2_JIG_USB_ON     (1 << 0)
1515
1516         /*
1517          * If USB, use default 475mA
1518          * If Charger, use 600mA and go to charge mode
1519          */
1520         if ((val[0] & FSA_DEV1_CHARGER) && !started_charging_once) {
1521                 started_charging_once = 1;
1522
1523                 /* If it's full, do not charge. */
1524                 if (battery_soc < 100)
1525                         into_charge_mode();
1526                 else
1527                         charger_en(0);
1528         } else if (val[0] & FSA_DEV1_USB) {
1529                 if (battery_soc < 100)
1530                         charger_en(475); /* enable charger and keep booting */
1531                 else
1532                         charger_en(0);
1533         }
1534
1535         /* If reset status is watchdog reset then skip it */
1536         if (!(get_reset_status() & WDTRESET)) {
1537                 /* If Factory Mode is Boot ON-USB, go to download mode */
1538                 i2c_read(addr, 0x07, 1, val, 1);
1539
1540 #define FSA_ADC_FAC_USB_OFF     0x18
1541 #define FSA_ADC_FAC_USB_ON      0x19
1542 #define FSA_ADC_FAC_UART        0x1d
1543
1544                 if (val[0] == FSA_ADC_FAC_USB_ON ||
1545                         val[0] == FSA_ADC_FAC_USB_OFF)
1546                         setenv("bootcmd", "usbdown");
1547         }
1548
1549         path = getenv("usb");
1550
1551         if (!strncmp(path, "cp", 2))
1552                 run_command("microusb cp", 0);
1553 }
1554
1555 static void micro_usb_switch(int path)
1556 {
1557         unsigned char addr;
1558         unsigned char val[2];
1559
1560         if (fsa9480_probe())
1561                 return;
1562
1563         addr = 0x25;            /* fsa9480 */
1564
1565         if (path)
1566                 val[0] = 0x90;  /* VAUDIO */
1567         else
1568                 val[0] = (1 << 5) | (1 << 2);   /* DHOST */
1569
1570         i2c_write(addr, 0x13, 1, val, 1);       /* MANSW1 */
1571
1572         i2c_read(addr, 0x2, 1, val, 1);
1573         val[0] &= ~(1 << 2);                    /* Manual switching */
1574         i2c_write(addr, 0x2, 1, val, 1);
1575 }
1576
1577 #define MAX8998_REG_ONOFF1      0x11
1578 #define MAX8998_REG_ONOFF2      0x12
1579 #define MAX8998_REG_ONOFF3      0x13
1580 #define MAX8998_REG_LDO7        0x21
1581 #define MAX8998_REG_LDO17       0x29
1582 /* ONOFF1 */
1583 #define MAX8998_LDO3            (1 << 2)
1584 /* ONOFF2 */
1585 #define MAX8998_LDO6            (1 << 7)
1586 #define MAX8998_LDO7            (1 << 6)
1587 #define MAX8998_LDO8            (1 << 5)
1588 #define MAX8998_LDO9            (1 << 4)
1589 #define MAX8998_LDO10           (1 << 3)
1590 #define MAX8998_LDO11           (1 << 2)
1591 #define MAX8998_LDO12           (1 << 1)
1592 #define MAX8998_LDO13           (1 << 0)
1593 /* ONOFF3 */
1594 #define MAX8998_LDO14           (1 << 7)
1595 #define MAX8998_LDO15           (1 << 6)
1596 #define MAX8998_LDO16           (1 << 5)
1597 #define MAX8998_LDO17           (1 << 4)
1598
1599
1600 static void init_pmic(void)
1601 {
1602         unsigned char addr;
1603         unsigned char val[2];
1604
1605         if (cpu_is_s5pc100())
1606                 return;
1607
1608         addr = 0xCC >> 1;       /* max8998 */
1609         if (max8998_probe())
1610                 return;
1611
1612         /* ONOFF1 */
1613         i2c_read(addr, MAX8998_REG_ONOFF1, 1, val, 1);
1614         val[0] &= ~MAX8998_LDO3;
1615         i2c_write(addr, MAX8998_REG_ONOFF1, 1, val, 1);
1616
1617         /* ONOFF2 */
1618         i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
1619         /*
1620          * Disable LDO10(VPLL_1.1V), LDO11(CAM_IO_2.8V),
1621          * LDO12(CAM_ISP_1.2V), LDO13(CAM_A_2.8V)
1622          */
1623         val[0] &= ~(MAX8998_LDO10 | MAX8998_LDO11 |
1624                         MAX8998_LDO12 | MAX8998_LDO13);
1625
1626         if (mach_is_kessler())
1627                 val[0] |= MAX8998_LDO7;         /* LDO7: VLCD_1.8V */
1628
1629         i2c_write(addr, MAX8998_REG_ONOFF2, 1, val, 1);
1630         i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
1631         /* ONOFF3 */
1632         i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
1633         /*
1634          * Disable LDO14(CAM_CIF_1.8), LDO15(CAM_AF_3.3V),
1635          * LDO16(VMIPI_1.8V), LDO17(CAM_8M_1.8V)
1636          */
1637         val[0] &= ~(MAX8998_LDO14 | MAX8998_LDO15 |
1638                         MAX8998_LDO16 | MAX8998_LDO17);
1639
1640         if (mach_is_kessler())
1641                 val[0] |= MAX8998_LDO17;        /* LDO17: VCC_3.0V_LCD */
1642
1643         i2c_write(addr, MAX8998_REG_ONOFF3, 1, val, 1);
1644         i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
1645 }
1646
1647 static void setup_power_down_mode_registers(void)
1648 {
1649         struct s5pc110_gpio *gpio = (struct s5pc110_gpio *)S5PC110_GPIO_BASE;
1650         struct s5pc1xx_gpio_bank *bank;
1651         struct gpio_powermode *p;
1652         int n_p;
1653         struct gpio_external *ge;
1654         int n_ge;
1655         struct s5pc1xx_gpio_item *mr;
1656         int n_mr;
1657         int i;
1658
1659         if (cpu_is_s5pc100())
1660                 return;
1661
1662         /* Only Limo real and kessler supports worked for sleep currnet */
1663         if (mach_is_aquila()) {
1664                 if (board_is_limo_real())
1665                         /* Support */;
1666                 else
1667                         return;
1668         } else if (mach_is_kessler()) {
1669                 /* Support */;
1670         } else if (mach_is_geminus()) {
1671                 /* Support */;
1672         } else {
1673                 return;
1674         }
1675
1676         if (mach_is_aquila()) {
1677                 /* Aquila rev 0.8 or lower */
1678                 p = aquila_powerdown_modes;
1679                 ge = aquila_external_powerdown_modes;
1680                 mr = aquila_mirror_powerdown_mode;
1681                 n_p = ARRAY_SIZE(aquila_powerdown_modes);
1682                 n_ge = ARRAY_SIZE(aquila_external_powerdown_modes);
1683                 n_mr = ARRAY_SIZE(aquila_mirror_powerdown_mode);
1684         } else if (mach_is_kessler()) {
1685                 /* Aquila rev 0.9 */
1686                 p = kessler_powerdown_modes;
1687                 ge = kessler_external_powerdown_modes;
1688                 mr = kessler_mirror_powerdown_mode;
1689                 n_p = ARRAY_SIZE(kessler_powerdown_modes);
1690                 n_ge = ARRAY_SIZE(kessler_external_powerdown_modes);
1691                 n_mr = ARRAY_SIZE(kessler_mirror_powerdown_mode);
1692         } else if (mach_is_geminus()) {
1693                 if (hwrevision(1)) {
1694                         /* Same as Aquila rev 0.9 */
1695 #if 0
1696                         p = kessler_powerdown_modes;
1697                         ge = kessler_external_powerdown_modes;
1698                         mr = kessler_mirror_powerdown_mode;
1699                         n_p = ARRAY_SIZE(kessler_powerdown_modes);
1700                         n_ge = ARRAY_SIZE(kessler_external_powerdown_modes);
1701                         n_mr = ARRAY_SIZE(kessler_mirror_powerdown_mode);
1702 #else
1703                         p = aquila_powerdown_modes;
1704                         ge = aquila_external_powerdown_modes;
1705                         mr = aquila_mirror_powerdown_mode;
1706                         n_p = ARRAY_SIZE(aquila_powerdown_modes);
1707                         n_ge = ARRAY_SIZE(aquila_external_powerdown_modes);
1708                         n_mr = ARRAY_SIZE(aquila_mirror_powerdown_mode);
1709 #endif
1710                 } else if (hwrevision(0)) {
1711                         /* Same as Aquila rev 0.8 or lower */
1712                         p = aquila_powerdown_modes;
1713                         ge = aquila_external_powerdown_modes;
1714                         mr = aquila_mirror_powerdown_mode;
1715                         n_p = ARRAY_SIZE(aquila_powerdown_modes);
1716                         n_ge = ARRAY_SIZE(aquila_external_powerdown_modes);
1717                         n_mr = ARRAY_SIZE(aquila_mirror_powerdown_mode);
1718                 } else {
1719                         return; /* Not supported */
1720                 }
1721         }
1722
1723         bank = &gpio->gpio_a0;
1724
1725         for (i = 0; i < n_p; i++, p++, bank++) {
1726                 writel(p->conpdn, &bank->pdn_con);
1727                 writel(p->pudpdn, &bank->pdn_pull);
1728         }
1729         /* M299 */
1730         writel(0xff0022b0, (unsigned int *)0xF0000000);
1731         writel(0xff0022b0, (unsigned int *)0xF1400000);
1732
1733         bank = &gpio->gpio_h0;
1734
1735         for (i = 0; i < n_ge; i++) {
1736                 writel(ge->con, &bank->con);
1737                 writel(ge->dat, &bank->dat);
1738                 writel(ge->pud, &bank->pull);
1739
1740                 bank++;
1741                 ge++;
1742         }
1743
1744         for (i = 0; i < n_mr; i++) {
1745                 unsigned int reg = readl(&mr->bank->pdn_con);
1746                 reg &= ~(0x3 << (mr->number << 1));
1747                 if (readl(&mr->bank->dat) & (1 << mr->number))
1748                         reg |= 0x1 << (mr->number << 1);
1749                 writel(reg, &mr->bank->pdn_con);
1750                 mr++;
1751         }
1752 }
1753
1754 #ifdef CONFIG_LCD
1755 #include "../../../drivers/video/s5p-spi.h"
1756
1757 extern void s6e63m0_set_platform_data(struct spi_platform_data *pd);
1758 extern void s6d16a0x_set_platform_data(struct spi_platform_data *pd);
1759
1760 struct spi_platform_data spi_pd;
1761
1762 struct s5pc110_gpio *gpio_base = (struct s5pc110_gpio *) S5PC110_GPIO_BASE;
1763
1764 void lcd_cfg_gpio(void)
1765 {
1766         unsigned int i, f3_end = 4;
1767
1768         for (i = 0; i < 8; i++) {
1769                 /* set GPF0,1,2[0:7] for RGB Interface and Data lines (32bit) */
1770                 gpio_cfg_pin(&gpio_base->gpio_f0, i, GPIO_FUNC(2));
1771                 gpio_cfg_pin(&gpio_base->gpio_f1, i, GPIO_FUNC(2));
1772                 gpio_cfg_pin(&gpio_base->gpio_f2, i, GPIO_FUNC(2));
1773                 /* pull-up/down disable */
1774                 gpio_set_pull(&gpio_base->gpio_f0, i, GPIO_PULL_NONE);
1775                 gpio_set_pull(&gpio_base->gpio_f1, i, GPIO_PULL_NONE);
1776                 gpio_set_pull(&gpio_base->gpio_f2, i, GPIO_PULL_NONE);
1777
1778                 /* drive strength to max (24bit) */
1779                 gpio_set_drv(&gpio_base->gpio_f0, i, GPIO_DRV_4X);
1780                 gpio_set_rate(&gpio_base->gpio_f0, i, GPIO_DRV_SLOW);
1781                 gpio_set_drv(&gpio_base->gpio_f1, i, GPIO_DRV_4X);
1782                 gpio_set_rate(&gpio_base->gpio_f1, i, GPIO_DRV_SLOW);
1783                 gpio_set_drv(&gpio_base->gpio_f2, i, GPIO_DRV_4X);
1784                 gpio_set_rate(&gpio_base->gpio_f2, i, GPIO_DRV_SLOW);
1785         }
1786
1787         /* set DISPLAY_DE_B pin for dual rgb mode. */
1788         if (board_is_media())
1789                 f3_end = 5;
1790
1791         for (i = 0; i < f3_end; i++) {
1792                 /* set GPF3[0:3] for RGB Interface and Data lines (32bit) */
1793                 gpio_cfg_pin(&gpio_base->gpio_f3, i, GPIO_PULL_UP);
1794                 /* pull-up/down disable */
1795                 gpio_set_pull(&gpio_base->gpio_f3, i, GPIO_PULL_NONE);
1796                 /* drive strength to max (24bit) */
1797                 gpio_set_drv(&gpio_base->gpio_f3, i, GPIO_DRV_4X);
1798                 gpio_set_rate(&gpio_base->gpio_f3, i, GPIO_DRV_SLOW);
1799         }
1800         /* display output path selection (only [1:0] valid) */
1801         writel(0x2, 0xE0107008);
1802
1803         /* gpio pad configuration for LCD reset. */
1804         gpio_cfg_pin(&gpio_base->gpio_mp0_5, 5, GPIO_OUTPUT);
1805
1806         /* gpio pad configuration for LCD ON. */
1807         gpio_cfg_pin(&gpio_base->gpio_j1, 3, GPIO_OUTPUT);
1808
1809         /* LCD_BACKLIGHT_EN */
1810         if (mach_is_geminus())
1811                 gpio_cfg_pin(&gpio_base->gpio_mp0_5, 0, GPIO_OUTPUT);
1812         if (board_is_neptune() && hwrevision(0)) {
1813                 gpio_cfg_pin(&gpio_base->gpio_mp0_4, 4, GPIO_OUTPUT);
1814                 gpio_direction_output(&gpio_base->gpio_mp0_4, 4, 0);
1815         }
1816
1817         /*
1818          * gpio pad configuration for
1819          * DISPLAY_CS, DISPLAY_CLK, DISPLAY_SO, DISPLAY_SI.
1820          */
1821         gpio_cfg_pin(&gpio_base->gpio_mp0_1, 1, GPIO_OUTPUT);
1822         gpio_cfg_pin(&gpio_base->gpio_mp0_4, 1, GPIO_OUTPUT);
1823         gpio_cfg_pin(&gpio_base->gpio_mp0_4, 2, GPIO_INPUT);
1824         gpio_cfg_pin(&gpio_base->gpio_mp0_4, 3, GPIO_OUTPUT);
1825
1826         if (mach_is_aquila() || mach_is_kessler()) {
1827                 spi_pd.cs_bank = &gpio_base->gpio_mp0_1;
1828                 spi_pd.cs_num = 1;
1829                 spi_pd.clk_bank = &gpio_base->gpio_mp0_4;
1830                 spi_pd.clk_num = 1;
1831                 spi_pd.si_bank = &gpio_base->gpio_mp0_4;
1832                 spi_pd.si_num = 3;
1833                 spi_pd.so_bank = &gpio_base->gpio_mp0_4;
1834                 spi_pd.so_num = 2;
1835
1836                 if (board_is_neptune() && hwrevision(0))
1837                         s6d16a0x_set_platform_data(&spi_pd);
1838                 else {
1839                         s6e63m0_set_platform_data(&spi_pd);
1840                         if (board_is_media())
1841                                 spi_pd.set_rev = 1;
1842                 }
1843         }
1844
1845         if (mach_is_cypress()) {
1846 #if 0           /* universal cypress */
1847                 /* FLCD_CS */
1848                 gpio_cfg_pin(&gpio_base->gpio_mp0_1, 0, GPIO_OUTPUT);
1849 #endif
1850                 /* FLCD_CS_S */
1851                 gpio_cfg_pin(&gpio_base->gpio_mp0_5, 1, GPIO_OUTPUT);
1852                 /* FLCD_CLK */
1853                 gpio_cfg_pin(&gpio_base->gpio_mp0_4, 0, GPIO_OUTPUT);
1854                 /* FLCD_SDI */
1855                 gpio_cfg_pin(&gpio_base->gpio_mp0_4, 2, GPIO_OUTPUT);
1856                 /* FLCD_RST_S */
1857                 gpio_cfg_pin(&gpio_base->gpio_mp0_4, 5, GPIO_OUTPUT);
1858                 /* FLCD_ON_S */
1859                 gpio_cfg_pin(&gpio_base->gpio_g2, 2, GPIO_OUTPUT);
1860 #if 0           /* universal cypress */
1861                 pd_cs.bank = &gpio_base->gpio_mp0_1;
1862                 pd_cs.num = 0;
1863 #endif
1864                 spi_pd.cs_bank = &gpio_base->gpio_mp0_5;
1865                 spi_pd.cs_num = 1;
1866                 spi_pd.clk_bank = &gpio_base->gpio_mp0_4;
1867                 spi_pd.clk_num = 0;
1868                 spi_pd.si_bank = &gpio_base->gpio_mp0_4;
1869                 spi_pd.si_num = 2;
1870
1871                 spi_pd.set_rev = 1;
1872
1873                 /* these data would be sent to s6e63m0 lcd panel driver. */
1874                 s6e63m0_set_platform_data(&spi_pd);
1875         }
1876
1877         return;
1878 }
1879
1880 #define SWRST_REG               0x00
1881 #define LEDCON_REG              0x01
1882 #define LED_CUR_SET_REG         0x03
1883 #define LED_CUR_TR_REG          0x08
1884
1885 #define SWRST                   0x01
1886 #define NORMAL_MODE             0x09
1887 #define CUR_SET                 0x63
1888 #define TR_SET                  0x00
1889 void backlight_on(unsigned int onoff)
1890 {
1891
1892         unsigned char addr;
1893         unsigned char val[2];
1894         struct s5pc110_gpio *gpio = (struct s5pc110_gpio *) S5PC110_GPIO_BASE;
1895
1896         if (onoff) {
1897                 if (mach_is_geminus())
1898                         gpio_set_value(&gpio->gpio_mp0_5, 0, 1);
1899         } else {
1900                 if (mach_is_geminus())
1901                         gpio_set_value(&gpio->gpio_mp0_5, 0, 0);
1902         }
1903
1904         if (mach_is_kessler() && board_is_neptune() && hwrevision(0)) {
1905                 gpio_set_value(&gpio->gpio_mp0_4, 4, 1);
1906                 udelay(6);
1907
1908                 i2c_set_bus_num(I2C_GPIO5);
1909
1910                 addr = 0x76;
1911                 if (i2c_probe(addr)) {
1912                         if (i2c_probe(addr)) {
1913                                 puts("Can't found s6d16a0x backlight i2c\n");
1914                                 return;
1915                         }
1916                 }
1917                 val[0] = SWRST;
1918                 i2c_write(addr, SWRST_REG, 1, val, 1);
1919                 /* NORMAL MODE */
1920                 val[0] = CUR_SET;
1921                 i2c_write(addr, LED_CUR_SET_REG, 1, val, 1);
1922                 val[0] = TR_SET;
1923                 i2c_write(addr, LED_CUR_TR_REG, 1, val, 1);
1924                 val[0] = NORMAL_MODE;
1925                 i2c_write(addr, LEDCON_REG, 1, val, 1);
1926                 udelay(5000);
1927         }
1928 }
1929
1930 void reset_lcd(void)
1931 {
1932         struct s5pc110_gpio *gpio = (struct s5pc110_gpio *) S5PC110_GPIO_BASE;
1933
1934         if (mach_is_aquila() || mach_is_kessler() || mach_is_geminus())
1935                 gpio_set_value(&gpio->gpio_mp0_5, 5, 1);
1936         if (mach_is_cypress())
1937                 gpio_set_value(&gpio->gpio_mp0_4, 5, 1);
1938 }
1939
1940 void lcd_power_on(unsigned int onoff)
1941 {
1942         struct s5pc110_gpio *gpio = (struct s5pc110_gpio *) S5PC110_GPIO_BASE;
1943         if (onoff) {
1944                 /* TSP_LDO_ON */
1945                 if (mach_is_aquila() || mach_is_geminus())
1946                         gpio_set_value(&gpio->gpio_j1, 3, 1);
1947
1948                 if (mach_is_cypress())
1949                         gpio_set_value(&gpio->gpio_g2, 2, 1);
1950
1951                 if (mach_is_kessler()) {
1952                         unsigned char addr;
1953                         unsigned char val[2];
1954                         unsigned char val2[2];
1955
1956                         gpio_set_value(&gpio->gpio_j1, 3, 1);
1957
1958                         addr = 0xCC >> 1;       /* max8998 */
1959                         if (max8998_probe())
1960                                 return;
1961
1962                         i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
1963                         val[0] &= ~(MAX8998_LDO17);
1964                         val[0] |= MAX8998_LDO17; /* LDO17: VCC_3.0V_LCD */
1965                         i2c_write(addr, MAX8998_REG_ONOFF3, 1, val, 1);
1966
1967                         i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
1968                         val[0] |= MAX8998_LDO17;
1969                         val2[0] = 0xE;
1970                         i2c_write(addr, MAX8998_REG_LDO17, 1, val2, 1);
1971                         i2c_write(addr, MAX8998_REG_ONOFF3, 1, val, 1);
1972
1973                         i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
1974                         val[0] |= MAX8998_LDO7;
1975                         val2[0] = 0x2;
1976                         i2c_write(addr, MAX8998_REG_LDO7, 1, val2, 1);
1977                         i2c_write(addr, MAX8998_REG_ONOFF2, 1, val, 1);
1978                 }
1979         } else {
1980                 if (mach_is_aquila() || mach_is_geminus())
1981                         gpio_set_value(&gpio->gpio_j1, 3, 0);
1982
1983                 if (mach_is_cypress())
1984                         gpio_set_value(&gpio->gpio_g2, 2, 0);
1985
1986                 if (mach_is_kessler()) {
1987                         unsigned char addr;
1988                         unsigned char val[2];
1989
1990                         gpio_set_value(&gpio->gpio_j1, 3, 0);
1991
1992                         addr = 0xCC >> 1;       /* max8998 */
1993                         if (max8998_probe())
1994                                 return;
1995
1996                         i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
1997                         val[0] &= ~(MAX8998_LDO7);
1998                         i2c_write(addr, MAX8998_REG_ONOFF2, 1, val, 1);
1999                         i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
2000
2001                         i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
2002                         val[0] &= ~(MAX8998_LDO17);
2003                         i2c_write(addr, MAX8998_REG_ONOFF3, 1, val, 1);
2004                         i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
2005                 }
2006         }
2007 }
2008
2009 extern void s6e63m0_cfg_ldo(void);
2010 extern void s6e63m0_enable_ldo(unsigned int onoff);
2011 extern void s6d16a0x_cfg_ldo(void);
2012 extern void s6d16a0x_enable_ldo(unsigned int onoff);
2013
2014 int s5p_no_lcd_support(void)
2015 {
2016         if (mach_is_wmg160())
2017                 return 1;
2018         return 0;
2019 }
2020
2021 void init_panel_info(vidinfo_t *vid)
2022 {
2023         vid->cfg_gpio = NULL;
2024         vid->reset_lcd = NULL;
2025         vid->backlight_on = NULL;
2026         vid->lcd_power_on = NULL;
2027
2028         vid->cfg_ldo = NULL;
2029         vid->enable_ldo = NULL;
2030
2031         vid->init_delay = 0;
2032         vid->reset_delay = 0;
2033         vid->power_on_delay = 0;
2034
2035         vid->vl_freq    = 60;
2036         vid->vl_col     = 480;
2037         vid->vl_row     = 800;
2038         vid->vl_width   = 480;
2039         vid->vl_height  = 800;
2040
2041         vid->dual_lcd_enabled = 0;
2042
2043         if (board_is_media()) {
2044                 vid->vl_col     = 960;
2045                 vid->vl_row     = 800;
2046                 vid->vl_width   = 960;
2047                 vid->vl_height  = 800;
2048
2049                 /* enable dual lcd mode. */
2050                 vid->dual_lcd_enabled = 1;
2051         }
2052
2053         vid->vl_clkp    = CONFIG_SYS_HIGH;
2054         vid->vl_hsp     = CONFIG_SYS_LOW;
2055         vid->vl_vsp     = CONFIG_SYS_LOW;
2056         vid->vl_dp      = CONFIG_SYS_HIGH;
2057         vid->vl_bpix    = 32;
2058
2059         /* S6E63M0 LCD Panel */
2060         vid->vl_hspw    = 2;
2061         vid->vl_hbpd    = 16;
2062         vid->vl_hfpd    = 16;
2063
2064         vid->vl_vspw    = 2;
2065         vid->vl_vbpd    = 3;
2066         vid->vl_vfpd    = 28;
2067
2068         if (mach_is_aquila() || mach_is_kessler()
2069                         || mach_is_cypress()) {
2070                 vid->cfg_gpio = lcd_cfg_gpio;
2071                 vid->reset_lcd = reset_lcd;
2072                 vid->backlight_on = backlight_on;
2073                 vid->lcd_power_on = lcd_power_on;
2074                 vid->cfg_ldo = s6e63m0_cfg_ldo;
2075                 vid->enable_ldo = s6e63m0_enable_ldo;
2076
2077                 vid->init_delay = 25000;
2078                 vid->reset_delay = 120000;
2079         }
2080
2081         if (board_is_neptune() && hwrevision(0)) {
2082                 vid->vl_freq    = 100;
2083                 vid->vl_col     = 320;
2084                 vid->vl_row     = 480;
2085                 vid->vl_width   = 320;
2086                 vid->vl_height  = 480;
2087
2088                 vid->vl_clkp    = CONFIG_SYS_HIGH;
2089                 vid->vl_hsp     = CONFIG_SYS_HIGH;
2090                 vid->vl_vsp     = CONFIG_SYS_HIGH;
2091                 vid->vl_dp      = CONFIG_SYS_LOW;
2092                 vid->vl_bpix    = 32;
2093
2094                 /* disable dual lcd mode. */
2095                 vid->dual_lcd_enabled = 0;
2096
2097                 /* S6D16A0X LCD Panel */
2098                 vid->vl_hspw    = 16;
2099                 vid->vl_hbpd    = 24;
2100                 vid->vl_hfpd    = 16;
2101
2102                 vid->vl_vspw    = 2;
2103                 vid->vl_vbpd    = 2;
2104                 vid->vl_vfpd    = 4;
2105
2106                 vid->cfg_gpio = lcd_cfg_gpio;
2107                 vid->backlight_on = backlight_on;
2108                 vid->lcd_power_on = lcd_power_on;
2109                 vid->reset_lcd = reset_lcd;
2110                 vid->cfg_ldo = s6d16a0x_cfg_ldo;
2111                 vid->enable_ldo = s6d16a0x_enable_ldo;
2112
2113                 vid->init_delay = 10000;
2114                 vid->power_on_delay = 10000;
2115                 vid->reset_delay = 1000;
2116
2117         }
2118
2119         if (mach_is_geminus()) {
2120                 vid->vl_freq    = 60;
2121                 vid->vl_col     = 1024,
2122                 vid->vl_row     = 600,
2123                 vid->vl_width   = 1024,
2124                 vid->vl_height  = 600,
2125                 vid->vl_clkp    = CONFIG_SYS_LOW,
2126                 vid->vl_hsp     = CONFIG_SYS_HIGH,
2127                 vid->vl_vsp     = CONFIG_SYS_HIGH,
2128                 vid->vl_dp      = CONFIG_SYS_LOW,
2129                 vid->vl_bpix    = 32,
2130
2131                 vid->vl_hspw    = 32,
2132                 vid->vl_hfpd    = 48,
2133                 vid->vl_hbpd    = 80,
2134
2135                 vid->vl_vspw    = 1,
2136                 vid->vl_vfpd    = 3,
2137                 vid->vl_vbpd    = 4,
2138
2139                 vid->cfg_gpio = lcd_cfg_gpio;
2140                 vid->reset_lcd = reset_lcd;
2141                 vid->backlight_on = backlight_on;
2142                 vid->lcd_power_on = lcd_power_on;
2143         }
2144 #if 0
2145         vid->vl_freq    = 60;
2146         vid->vl_col     = 480,
2147         vid->vl_row     = 800,
2148         vid->vl_width   = 480,
2149         vid->vl_height  = 800,
2150         vid->vl_clkp    = CONFIG_SYS_HIGH,
2151         vid->vl_hsp     = CONFIG_SYS_LOW,
2152         vid->vl_vsp     = CONFIG_SYS_LOW,
2153         vid->vl_dp      = CONFIG_SYS_HIGH,
2154         vid->vl_bpix    = 32,
2155
2156         /* tl2796 panel. */
2157         vid->vl_hpw     = 4,
2158         vid->vl_blw     = 8,
2159         vid->vl_elw     = 8,
2160
2161         vid->vl_vpw     = 4,
2162         vid->vl_bfw     = 8,
2163         vid->vl_efw     = 8,
2164 #endif
2165 #if 0
2166         vid->vl_freq    = 60;
2167         vid->vl_col     = 1024,
2168         vid->vl_row     = 600,
2169         vid->vl_width   = 1024,
2170         vid->vl_height  = 600,
2171         vid->vl_clkp    = CONFIG_SYS_HIGH,
2172         vid->vl_hsp     = CONFIG_SYS_HIGH,
2173         vid->vl_vsp     = CONFIG_SYS_HIGH,
2174         vid->vl_dp      = CONFIG_SYS_LOW,
2175         vid->vl_bpix    = 32,
2176
2177         /* AMS701KA AMOLED Panel. */
2178         vid->vl_hpw     = 30,
2179         vid->vl_blw     = 114,
2180         vid->vl_elw     = 48,
2181
2182         vid->vl_vpw     = 2,
2183         vid->vl_bfw     = 6,
2184         vid->vl_efw     = 8,
2185 #endif
2186 }
2187 #endif
2188
2189 static void setup_meminfo(void)
2190 {
2191         char meminfo[64] = {0, };
2192         int count = 0, size, real;
2193
2194         size = gd->bd->bi_dram[0].size >> 20;
2195         count += sprintf(meminfo + count, "mem=%dM", size);
2196
2197         /* Each Chip Select can't exceed the 256MiB */
2198         size = gd->bd->bi_dram[1].size >> 20;
2199         real = min(size, 256);
2200         count += sprintf(meminfo + count, " mem=%dM@0x%x",
2201                 real, (unsigned int)gd->bd->bi_dram[1].start);
2202
2203         size -= real;
2204         if (size > 0) {
2205                 count += sprintf(meminfo + count, " mem=%dM@0x%x", size,
2206                         (unsigned int)gd->bd->bi_dram[1].start + (real << 20));
2207         }
2208
2209         setenv("meminfo", meminfo);
2210 }
2211
2212 int misc_init_r(void)
2213 {
2214 #ifdef CONFIG_LCD
2215         /* It should be located at first */
2216         lcd_is_enabled = 0;
2217
2218         if (mach_is_aquila() || mach_is_kessler()) {
2219                 if (board_is_neptune() && hwrevision(0))
2220                         setenv("lcdinfo", "lcd=s6d16a0x");
2221                 else if (board_is_media())
2222                         setenv("lcdinfo", "lcd=s6e63m0");
2223                 else
2224                         setenv("lcdinfo", "lcd=s6e63m0");
2225         }
2226         if (mach_is_geminus())
2227                 setenv("lcdinfo", "lcd=lms480jc01");
2228         if (mach_is_cypress())
2229                 setenv("lcdinfo", "lcd=s6e63m0");
2230 #endif
2231         setup_meminfo();
2232
2233         show_hw_revision();
2234
2235         /* Set proper PMIC pins */
2236         pmic_pin_init();
2237
2238         /* Check auto burning */
2239         check_auto_burn();
2240
2241         /* To power up I2C2 */
2242         enable_ldos();
2243
2244         /* Enable T-Flash at Limo Real or Limo Universal */
2245         enable_t_flash();
2246
2247         /* Setup Limo Real board GPIOs */
2248         setup_limo_real_gpios();
2249
2250         /* Setup Media board GPIOs */
2251         setup_media_gpios();
2252
2253         /* To usbdown automatically */
2254         check_keypad();
2255
2256         /* check max8998 */
2257         init_pmic();
2258
2259 #ifdef CONFIG_S5PC1XXFB
2260         display_device_info();
2261 #endif
2262
2263         setup_power_down_mode_registers();
2264
2265         /* check max17040 */
2266         check_battery(0);
2267
2268         /* check fsa9480 */
2269         check_micro_usb(0);
2270
2271         return 0;
2272 }
2273 #endif
2274
2275 int board_init(void)
2276 {
2277         /* Set Initial global variables */
2278         s5pc110_gpio = (struct s5pc110_gpio *) S5PC110_GPIO_BASE;
2279
2280         gd->bd->bi_arch_number = MACH_TYPE_AQUILA;
2281         gd->bd->bi_boot_params = PHYS_SDRAM_1 + 0x100;
2282
2283 #ifdef CONFIG_LCD
2284         /* 
2285          * set reserved memory region for framebuffer.
2286          *
2287          * this region wouldn't be rewrited by kernel so
2288          * could avoid nosie screen filled by garbages
2289          * after hibernation resume has been completed.
2290          */
2291         gd->fb_base = CONFIG_FB_RESERVED_MEM;
2292 #endif
2293
2294         /* Check H/W Revision */
2295         check_hw_revision();
2296
2297         return 0;
2298 }
2299
2300 int dram_init(void)
2301 {
2302         unsigned int base, memconfig0, size;
2303         unsigned int memconfig1, sz = 0;
2304
2305         if (cpu_is_s5pc100()) {
2306                 /* In mem setup, we swap the bank. So below size is correct */
2307                 gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
2308                 gd->bd->bi_dram[0].size = PHYS_SDRAM_2_SIZE;
2309                 gd->bd->bi_dram[1].start = S5PC100_PHYS_SDRAM_2;
2310                 size = 128;
2311         } else {
2312                 /* In S5PC110, we can't swap the DMC0/1 */
2313                 gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
2314                 gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;
2315
2316                 base = S5PC110_DMC1_BASE;
2317                 /* DMC configuration */
2318                 memconfig0 = readl(base + MEMCONFIG0_OFFSET);
2319                 gd->bd->bi_dram[1].start = memconfig0 & 0xFF000000;
2320
2321                 size = (memconfig0 >> 16) & 0xFF;
2322                 size = ((unsigned char) ~size) + 1;
2323
2324                 /*
2325                  * (0x07 + 1) * 16 = 128 MiB
2326                  * (0x0f + 1) * 16 = 256 MiB
2327                  */
2328                 size = size << 4;
2329
2330                 /*
2331                  * Aquila Rev0.5 4G3G1G
2332                  * Aquila Rev0.8 4G3G1G
2333                  * Aquila Rev0.9 4G3G1G
2334                  */
2335                 if (mach_is_aquila() || mach_is_kessler()) {
2336                         if (hwrevision(5) || hwrevision(8) || hwrevision(9)) {
2337                                 memconfig1 = readl(base + MEMCONFIG1_OFFSET);
2338
2339                                 sz = (memconfig1 >> 16) & 0xFF;
2340                                 sz = ((unsigned char) ~sz) + 1;
2341                                 sz = sz << 4;
2342                         }
2343                         if (mach_is_kessler() && board_is_s1())
2344                                 sz = 0;
2345                 }
2346         }
2347         /*
2348          * bi_dram[1].size contains all DMC1 memory size
2349          */
2350         gd->bd->bi_dram[1].size = (size + sz) << 20;
2351
2352         return 0;
2353 }
2354
2355 /* Used for sleep test */
2356 static unsigned char saved_val[4][2];
2357 void board_sleep_init_late(void)
2358 {
2359         /* CODEC_LDO_EN: GPF3[4] */
2360         gpio_direction_output(&s5pc110_gpio->gpio_f3, 4, 0);
2361         /* CODEC_XTAL_EN: GPH3[2] */
2362         gpio_direction_output(&s5pc110_gpio->gpio_h3, 2, 0);
2363
2364         /* MMC T_FLASH off */
2365         gpio_direction_output(&s5pc110_gpio->gpio_mp0_5, 4, 0);
2366         /* MHL off */
2367         gpio_direction_output(&s5pc110_gpio->gpio_j2, 2, 0);
2368         gpio_direction_output(&s5pc110_gpio->gpio_mp0_4, 7, 0);
2369         /* MHL_ON for REV02 or higher */
2370         gpio_direction_output(&s5pc110_gpio->gpio_j2, 3, 0);
2371 }
2372
2373 void board_sleep_init(void)
2374 {
2375         unsigned char addr;
2376         unsigned char val[2];
2377
2378         addr = 0xCC >> 1;
2379         if (max8998_probe())
2380                 return;
2381
2382         if (mach_is_kessler()) {
2383                 /* Set ONOFF1 */
2384                 i2c_read(addr, MAX8998_REG_ONOFF1, 1, val, 1);
2385                 saved_val[0][0] = val[0];
2386                 saved_val[0][1] = val[1];
2387                 val[0] &= ~((1 << 7) | (1 << 6) | (1 << 4) | (1 << 2) |
2388                                 (1 << 1) | (1 << 0));
2389                 i2c_write(addr, MAX8998_REG_ONOFF1, 1, val, 1);
2390                 i2c_read(addr, MAX8998_REG_ONOFF1, 1, val, 1);
2391
2392                 /* Set ONOFF2 */
2393                 i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
2394                 saved_val[1][0] = val[0];
2395                 saved_val[1][1] = val[1];
2396                 val[0] &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 3) |
2397                                 (1 << 2) | (1 << 1) | (1 << 0));
2398                 val[0] |= (1 << 7);
2399                 i2c_write(addr, MAX8998_REG_ONOFF2, 1, val, 1);
2400                 i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
2401
2402                 /* Set ONOFF3 */
2403                 i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
2404                 saved_val[2][0] = val[0];
2405                 saved_val[2][1] = val[1];
2406                 val[0] &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 4));
2407                 i2c_write(addr, MAX8998_REG_ONOFF3, 1, val, 1);
2408                 i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
2409
2410                 /* Set ONOFF4 */
2411                 i2c_read(addr, MAX8998_REG_ONOFF3+1, 1, val, 1);
2412                 saved_val[3][0] = val[0];
2413                 saved_val[3][1] = val[1];
2414                 val[0] &= ~((1 << 6) | (1 << 4));
2415                 i2c_write(addr, MAX8998_REG_ONOFF3+1, 1, val, 1);
2416                 i2c_read(addr, MAX8998_REG_ONOFF3+1, 1, val, 1);
2417
2418                 printf("Turned off regulators with Kessler setting."
2419                                " Preparing to sleep. [%s:%d]\n",
2420                                 __FILE__, __LINE__);
2421         } else { /* Default */
2422                 /* Set ONOFF1 */
2423                 i2c_read(addr, MAX8998_REG_ONOFF1, 1, val, 1);
2424                 saved_val[0][0] = val[0];
2425                 saved_val[0][1] = val[1];
2426                 val[0] &= ~((1 << 7) | (1 << 6) | (1 << 4) | (1 << 2) |
2427                                 (1 << 1) | (1 << 0));
2428                 i2c_write(addr, MAX8998_REG_ONOFF1, 1, val, 1);
2429                 i2c_read(addr, MAX8998_REG_ONOFF1, 1, val, 1);
2430
2431                 /* Set ONOFF2 */
2432                 i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
2433                 saved_val[1][0] = val[0];
2434                 saved_val[1][1] = val[1];
2435                 val[0] &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 3) |
2436                                 (1 << 2) | (1 << 1) | (1 << 0));
2437                 val[0] |= (1 << 7);
2438                 i2c_write(addr, MAX8998_REG_ONOFF2, 1, val, 1);
2439                 i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
2440
2441                 /* Set ONOFF3 */
2442                 i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
2443                 saved_val[2][0] = val[0];
2444                 saved_val[2][1] = val[1];
2445                 val[0] &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 4));
2446                 i2c_write(addr, MAX8998_REG_ONOFF3, 1, val, 1);
2447                 i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
2448
2449                 /* Set ONOFF4 */
2450                 i2c_read(addr, MAX8998_REG_ONOFF3+1, 1, val, 1);
2451                 saved_val[3][0] = val[0];
2452                 saved_val[3][1] = val[1];
2453                 val[0] &= ~((1 << 7) | (1 << 6) | (1 << 4));
2454                 i2c_write(addr, MAX8998_REG_ONOFF3+1, 1, val, 1);
2455                 i2c_read(addr, MAX8998_REG_ONOFF3+1, 1, val, 1);
2456
2457                 printf("Turned off regulators with default(Aquila) setting."
2458                                " Preparing to sleep. [%s:%d]\n",
2459                                 __FILE__, __LINE__);
2460         }
2461 }
2462
2463 void board_sleep_resume(void)
2464 {
2465         unsigned char addr;
2466         unsigned char val[2];
2467
2468         show_hw_revision();
2469
2470         addr = 0xCC >> 1;
2471         if (max8998_probe())
2472                 return;
2473
2474         /* Set ONOFF1 */
2475         i2c_write(addr, MAX8998_REG_ONOFF1, 1, saved_val[0], 1);
2476         i2c_read(addr, MAX8998_REG_ONOFF1, 1, val, 1);
2477         /* Set ONOFF2 */
2478         i2c_write(addr, MAX8998_REG_ONOFF2, 1, saved_val[1], 1);
2479         i2c_read(addr, MAX8998_REG_ONOFF2, 1, val, 1);
2480         /* Set ONOFF3 */
2481         i2c_write(addr, MAX8998_REG_ONOFF3, 1, saved_val[2], 1);
2482         i2c_read(addr, MAX8998_REG_ONOFF3, 1, val, 1);
2483         /* Set ONOFF4 */
2484         i2c_write(addr, MAX8998_REG_ONOFF3+1, 1, saved_val[3], 1);
2485         i2c_read(addr, MAX8998_REG_ONOFF3+1, 1, val, 1);
2486         puts("Waked up.\n");
2487
2488         /* check max17040 */
2489         check_battery(0);
2490
2491         /* check fsa9480 */
2492         check_micro_usb(0);
2493 }
2494
2495 #if defined(CONFIG_USB_GADGET_S3C_UDC_OTG)
2496
2497 static int s5pc1xx_phy_control(int on)
2498 {
2499         static int status;
2500
2501         if (on && !status) {
2502 #ifdef CONFIG_CMD_PMIC
2503                 run_command("pmic ldo 3 on", 0);
2504 #endif
2505                 /* S5PC110 */
2506                 if (board_is_limo_universal() ||
2507                         board_is_limo_real() ||
2508                         board_is_media()) {
2509                         /* check usb path */
2510                         if (board_is_limo_real() && !hwrevision(6))
2511                                 check_mhl();
2512                 }
2513
2514                 if (mach_is_tickertape())
2515                         /* USB_SEL: XM0ADDR_0: MP04[0] output mode */
2516                         gpio_direction_output(&s5pc110_gpio->gpio_mp0_4, 0, 0);
2517
2518                 /* USB Path to AP */
2519                 micro_usb_switch(0);
2520                 status = 1;
2521         } else if (!on && status) {
2522 #ifdef CONFIG_CMD_PMIC
2523                 run_command("pmic ldo 3 off", 0);
2524 #endif
2525                 status = 0;
2526         }
2527         udelay(10000);
2528
2529         return 0;
2530 }
2531
2532 struct s3c_plat_otg_data s5pc110_otg_data = {
2533         .phy_control = s5pc1xx_phy_control,
2534         .regs_phy = S5PC110_PHY_BASE,
2535         .regs_otg = S5PC110_OTG_BASE,
2536 };
2537
2538 int board_eth_init(bd_t *bis)
2539 {
2540         int res = -1;
2541
2542         if (cpu_is_s5pc100())
2543                 return -1;
2544
2545         s3c_udc_probe(&s5pc110_otg_data);
2546         if (usb_eth_initialize(bis) >= 0)
2547                 res = 0;
2548         return res;
2549 }
2550 #endif
2551
2552 #ifdef CONFIG_CMD_USBDOWN
2553 int usb_board_init(void)
2554 {
2555 #ifdef CONFIG_CMD_PMIC
2556         run_command("pmic ldo 3 on", 0);
2557 #endif
2558
2559         if (cpu_is_s5pc100()) {
2560 #ifdef CONFIG_HARD_I2C
2561                 uchar val[2] = {0,};
2562
2563                 /* PMIC */
2564                 if (i2c_read(0x66, 0, 1, val, 2)) {
2565                         puts("i2c_read error\n");
2566                         return 1;
2567                 }
2568
2569                 val[0] |= (1 << 3);
2570                 val[1] |= (1 << 5);
2571
2572                 if (i2c_write(0x66, 0, 1, val, 2)) {
2573                         puts("i2c_write error\n");
2574                         return 1;
2575                 }
2576                 i2c_read(0x66, 0, 1, val, 2);
2577 #endif
2578                 return 0;
2579         }
2580
2581         /* S5PC110 */
2582         if (board_is_limo_universal() ||
2583                 board_is_limo_real() ||
2584                 board_is_media()) {
2585                 /* check usb path */
2586                 if (board_is_limo_real() && !hwrevision(6))
2587                         check_mhl();
2588         }
2589
2590         if (mach_is_tickertape())
2591                 /* USB_SEL: XM0ADDR_0: MP04[0] output mode */
2592                 gpio_direction_output(&s5pc110_gpio->gpio_mp0_4, 0, 0);
2593
2594         /* USB Path to AP */
2595         micro_usb_switch(0);
2596
2597         return 0;
2598 }
2599 #endif
2600
2601 #ifdef CONFIG_GENERIC_MMC
2602 int s5p_no_mmc_support(void)
2603 {
2604         if (mach_is_wmg160())
2605                 return 1;
2606         return 0;
2607 }
2608
2609 int board_mmc_init(bd_t *bis)
2610 {
2611         unsigned int reg;
2612         unsigned int clock;
2613         struct s5pc110_clock *clk = (struct s5pc110_clock *)S5PC1XX_CLOCK_BASE;
2614         int i;
2615
2616         /* MASSMEMORY_EN: XMSMDATA7: GPJ2[7] output high */
2617         if (mach_is_kessler())
2618                 gpio_direction_output(&s5pc110_gpio->gpio_j2, 7, 1);
2619
2620         if (mach_is_wmg160())
2621                 return -1;
2622
2623         /* MMC0 Clock source = SCLKMPLL */
2624         reg = readl(&clk->src4);
2625         reg &= ~0xf;
2626         reg |= 0x6;
2627         writel(reg, &clk->src4);
2628
2629         reg = readl(&clk->div4);
2630         reg &= ~0xf;
2631
2632         /* set div value near 50MHz */
2633         clock = get_pll_clk(MPLL) / 1000000;
2634         for (i = 0; i < 0xf; i++) {
2635                 if ((clock / (i + 1)) <= 50) {
2636                         reg |= i << 0;
2637                         break;
2638                 }
2639         }
2640
2641         writel(reg, &clk->div4);
2642
2643         /*
2644          * MMC0 GPIO
2645          * GPG0[0]      SD_0_CLK
2646          * GPG0[1]      SD_0_CMD
2647          * GPG0[2]      SD_0_CDn        -> Not used
2648          * GPG0[3:6]    SD_0_DATA[0:3]
2649          */
2650         for (i = 0; i < 7; i++) {
2651                 if (i == 2)
2652                         continue;
2653                 /* GPG0[0:6] special function 2 */
2654                 gpio_cfg_pin(&s5pc110_gpio->gpio_g0, i, 0x2);
2655                 /* GPG0[0:6] pull disable */
2656                 gpio_set_pull(&s5pc110_gpio->gpio_g0, i, GPIO_PULL_NONE);
2657                 /* GPG0[0:6] drv 4x */
2658                 gpio_set_drv(&s5pc110_gpio->gpio_g0, i, GPIO_DRV_4X);
2659         }
2660
2661         if (mach_is_geminus()) {
2662                 gpio_direction_output(&s5pc110_gpio->gpio_j2, 7, 1);
2663                 gpio_set_pull(&s5pc110_gpio->gpio_j2, 7, GPIO_PULL_NONE);
2664         }
2665
2666         return s5pc1xx_mmc_init(0);
2667 }
2668 #endif
2669
2670 #ifdef CONFIG_CMD_PMIC
2671 static int pmic_status(void)
2672 {
2673         unsigned char addr, val[2];
2674         int reg, i;
2675
2676         addr = 0xCC >> 1;
2677
2678         if (max8998_probe())
2679                 return -1;
2680
2681         reg = 0x11;
2682         i2c_read(addr, reg, 1, val, 1);
2683         for (i = 7; i >= 4; i--)
2684                 printf("BUCK%d %s\n", 7 - i + 1,
2685                         val[0] & (1 << i) ? "on" : "off");
2686         for (; i >= 0; i--)
2687                 printf("LDO%d %s\n", 5 - i,
2688                         val[0] & (1 << i) ? "on" : "off");
2689         reg = 0x12;
2690         i2c_read(addr, reg, 1, val, 1);
2691         for (i = 7; i >= 0; i--)
2692                 printf("LDO%d %s\n", 7 - i + 6,
2693                         val[0] & (1 << i) ? "on" : "off");
2694         reg = 0x13;
2695         i2c_read(addr, reg, 1, val, 1);
2696         for (i = 7; i >= 4; i--)
2697                 printf("LDO%d %s\n", 7 - i + 14,
2698                         val[0] & (1 << i) ? "on" : "off");
2699
2700         reg = 0xd;
2701         i2c_read(addr, reg, 1, val, 1);
2702         for (i = 7; i >= 6; i--)
2703                 printf("SAFEOUT%d %s\n", 7 - i + 1,
2704                         val[0] & (1 << i) ? "on" : "off");
2705         return 0;
2706 }
2707
2708 static int pmic_ldo_control(int buck, int ldo, int safeout, int on)
2709 {
2710         unsigned char addr, val[2];
2711         unsigned int reg, shift;
2712
2713         if (ldo) {
2714                 if (ldo < 2)
2715                         return -1;
2716                 if (ldo <= 5) {
2717                         reg = 0x11;
2718                         shift = 5 - ldo;
2719                 } else if (ldo <= 13) {
2720                         reg = 0x12;
2721                         shift = 13 - ldo;
2722                 } else if (ldo <= 17) {
2723                         reg = 0x13;
2724                         shift = 17 - ldo + 4;
2725                 } else
2726                         return -1;
2727         } else if (buck) {
2728                 if (buck > 4)
2729                         return -1;
2730                 reg = 0x11;
2731                 shift = 4 - buck + 4;
2732         } else if (safeout) {
2733                 if (safeout > 3)
2734                         return -1;
2735                 reg = 0xd;
2736                 shift = 8 - safeout;
2737         } else
2738                 return -1;
2739
2740         addr = 0xCC >> 1;
2741
2742         if (max8998_probe())
2743                 return -1;
2744
2745         i2c_read(addr, reg, 1, val, 1);
2746         if (on)
2747                 val[0] |= (1 << shift);
2748         else
2749                 val[0] &= ~(1 << shift);
2750         i2c_write(addr, reg, 1, val, 1);
2751         i2c_read(addr, reg, 1, val, 1);
2752
2753         if (ldo)
2754                 printf("ldo %d value 0x%x, %s\n", ldo, val[0],
2755                         val[0] & (1 << shift) ? "on" : "off");
2756         else if (buck)
2757                 printf("buck %d value 0x%x, %s\n", buck, val[0],
2758                         val[0] & (1 << shift) ? "on" : "off");
2759         else if (safeout)
2760                 printf("safeout %d value 0x%x, %s\n", safeout, val[0],
2761                         val[0] & (1 << shift) ? "on" : "off");
2762
2763         return 0;
2764 }
2765
2766 static int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
2767 {
2768         int buck = 0, ldo = 0, safeout = 0, on = -1;
2769
2770         switch (argc) {
2771         case 2:
2772                 if (strncmp(argv[1], "status", 6) == 0)
2773                         return pmic_status();
2774                 break;
2775         case 4:
2776                 if (strncmp(argv[1], "ldo", 3) == 0)
2777                         ldo = simple_strtoul(argv[2], NULL, 10);
2778                 else if (strncmp(argv[1], "buck", 4) == 0)
2779                         buck = simple_strtoul(argv[2], NULL, 10);
2780                 else if (strncmp(argv[1], "safeout", 7) == 0)
2781                         safeout = simple_strtoul(argv[2], NULL, 10);
2782                 else
2783                         break;
2784
2785                 if (strncmp(argv[3], "on", 2) == 0)
2786                         on = 1;
2787                 else if (strncmp(argv[3], "off", 3) == 0)
2788                         on = 0;
2789                 else
2790                         break;
2791
2792                 return pmic_ldo_control(buck, ldo, safeout, on);
2793
2794         default:
2795                 break;
2796         }
2797
2798         cmd_usage(cmdtp);
2799         return 1;
2800 }
2801
2802 U_BOOT_CMD(
2803         pmic,           CONFIG_SYS_MAXARGS,     1, do_pmic,
2804         "PMIC LDO & BUCK control",
2805         "status - Display PMIC LDO & BUCK status\n"
2806         "pmic ldo num on/off - Turn on/off the LDO\n"
2807         "pmic buck num on/off - Turn on/off the BUCK\n"
2808         "pmic safeout num on/off - Turn on/off the SAFEOUT\n"
2809 );
2810 #endif
2811
2812 #ifdef CONFIG_CMD_DEVICE_POWER
2813 static int do_microusb(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
2814 {
2815         switch (argc) {
2816         case 2:
2817                 if (strncmp(argv[1], "cp", 2) == 0) {
2818                         micro_usb_switch(1);
2819                         pmic_ldo_control(0, 0, 2, 1);
2820                         setenv("usb", "cp");
2821                 } else if (strncmp(argv[1], "ap", 2) == 0) {
2822                         micro_usb_switch(0);
2823                         pmic_ldo_control(0, 0, 2, 0);
2824                         setenv("usb", "ap");
2825                 }
2826                 break;
2827         default:
2828                 cmd_usage(cmdtp);
2829                 return 1;
2830         }
2831
2832         saveenv();
2833
2834         printf("USB Path is set to %s\n", getenv("usb"));
2835
2836         return 0;
2837 }
2838
2839 U_BOOT_CMD(
2840         microusb,               CONFIG_SYS_MAXARGS,     1, do_microusb,
2841         "Micro USB Switch",
2842         "cp - switch to CP\n"
2843         "microusb ap - switch to AP\n"
2844 );
2845 #endif