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