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