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