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