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