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