0dacb5334983115c4172c844fe87c10eabd3dae6
[kernel/u-boot.git] / board / samsung / universal_c210 / universal.c
1 /*
2  *  Copyright (C) 2010 Samsung Electronics
3  *  Minkyu Kang <mk7.kang@samsung.com>
4  *  Kyungmin Park <kyungmin.park@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 <lcd.h>
28 #include <spi.h>
29 #include <asm/io.h>
30 #include <asm/arch/adc.h>
31 #include <asm/arch/clock.h>
32 #include <asm/arch/gpio.h>
33 #include <asm/arch/mmc.h>
34 #include <asm/arch/power.h>
35 #include <asm/arch/clk.h>
36 #include <ramoops.h>
37 #include <info_action.h>
38 #include <pmic.h>
39
40 DECLARE_GLOBAL_DATA_PTR;
41
42 static struct s5pc210_gpio_part1 *gpio1;
43 static struct s5pc210_gpio_part2 *gpio2;
44
45 static unsigned int battery_soc;
46 static unsigned int board_rev;
47
48 u32 get_board_rev(void)
49 {
50         return board_rev;
51 }
52
53 static int get_hwrev(void)
54 {
55         return board_rev & 0xFF;
56 }
57
58 enum {
59         I2C_0, I2C_1, I2C_2, I2C_3,
60         I2C_4, I2C_5, I2C_6, I2C_7,
61         I2C_8, I2C_9, I2C_10, I2C_11,
62         I2C_12, I2C_13, I2C_NUM,
63 };
64
65 /* i2c0 (CAM)   SDA: GPD1[0] SCL: GPD1[1] */
66 static struct i2c_gpio_bus_data i2c_0 = {
67         .sda_pin        = 0,
68         .scl_pin        = 1,
69 };
70
71 /* i2c1 (Gryo)  SDA: GPD1[2] SCL: GPD1[3] */
72 static struct i2c_gpio_bus_data i2c_1 = {
73         .sda_pin        = 2,
74         .scl_pin        = 3,
75 };
76
77 /* i2c3 (TSP)   SDA: GPA1[2] SCL: GPA1[3] */
78 static struct i2c_gpio_bus_data i2c_3 = {
79         .sda_pin        = 2,
80         .scl_pin        = 3,
81 };
82
83 /* i2c4         SDA: GPB[2] SCL: GPB[3] */
84 static struct i2c_gpio_bus_data i2c_4 = {
85         .sda_pin        = 2,
86         .scl_pin        = 3,
87 };
88
89 /* i2c5 (PMIC)  SDA: GPB[6] SCL: GPB[7] */
90 static struct i2c_gpio_bus_data i2c_5 = {
91         .sda_pin        = 6,
92         .scl_pin        = 7,
93 };
94
95 /* i2c6 (CODEC) SDA: GPC1[3] SCL: GPC1[4] */
96 static struct i2c_gpio_bus_data i2c_6 = {
97         .sda_pin        = 3,
98         .scl_pin        = 4,
99 };
100
101 /* i2c7         SDA: GPD0[2] SCL: GPD0[3] */
102 static struct i2c_gpio_bus_data i2c_7 = {
103         .sda_pin        = 2,
104         .scl_pin        = 3,
105 };
106
107 /* i2c9         SDA: SPY4[0] SCL: SPY4[1] */
108 static struct i2c_gpio_bus_data i2c_9 = {
109         .sda_pin        = 0,
110         .scl_pin        = 1,
111 };
112
113 /* i2c10        SDA: SPE1[0] SCL: SPE1[1] */
114 static struct i2c_gpio_bus_data i2c_10 = {
115         .sda_pin        = 0,
116         .scl_pin        = 1,
117 };
118
119 /* i2c12        SDA: SPE4[0] SCL: SPE4[1] */
120 static struct i2c_gpio_bus_data i2c_12 = {
121         .sda_pin        = 0,
122         .scl_pin        = 1,
123 };
124
125 /* i2c13        SDA: SPE4[2] SCL: SPE4[3] */
126 static struct i2c_gpio_bus_data i2c_13 = {
127         .sda_pin        = 2,
128         .scl_pin        = 3,
129 };
130
131 static struct i2c_gpio_bus i2c_gpio[I2C_NUM];
132
133 static void check_battery(int mode);
134 static void check_micro_usb(int intr);
135 static void init_pmic_lp3974(void);
136 static void init_pmic_max8952(void);
137
138 void i2c_init_board(void)
139 {
140         gpio1 = (struct s5pc210_gpio_part1 *) S5PC210_GPIO_PART1_BASE;
141         gpio2 = (struct s5pc210_gpio_part2 *) S5PC210_GPIO_PART2_BASE;
142
143         i2c_gpio[I2C_0].bus = &i2c_0;
144         i2c_gpio[I2C_1].bus = &i2c_1;
145         i2c_gpio[I2C_2].bus = NULL;
146         i2c_gpio[I2C_3].bus = &i2c_3;
147         i2c_gpio[I2C_4].bus = &i2c_4;
148         i2c_gpio[I2C_5].bus = &i2c_5;
149         i2c_gpio[I2C_6].bus = &i2c_6;
150         i2c_gpio[I2C_7].bus = &i2c_7;
151         i2c_gpio[I2C_8].bus = NULL;
152         i2c_gpio[I2C_9].bus = &i2c_9;
153         i2c_gpio[I2C_10].bus = &i2c_10;
154         i2c_gpio[I2C_11].bus = NULL;
155         i2c_gpio[I2C_12].bus = &i2c_12;
156         i2c_gpio[I2C_13].bus = &i2c_13;
157
158         i2c_gpio[I2C_0].bus->gpio_base = (unsigned int)&gpio1->d1;
159         i2c_gpio[I2C_1].bus->gpio_base = (unsigned int)&gpio1->d1;
160         i2c_gpio[I2C_3].bus->gpio_base = (unsigned int)&gpio1->a1;
161         i2c_gpio[I2C_4].bus->gpio_base = (unsigned int)&gpio1->b;
162         i2c_gpio[I2C_5].bus->gpio_base = (unsigned int)&gpio1->b;
163         i2c_gpio[I2C_6].bus->gpio_base = (unsigned int)&gpio1->c1;
164         i2c_gpio[I2C_7].bus->gpio_base = (unsigned int)&gpio1->d0;
165         i2c_gpio[I2C_9].bus->gpio_base = (unsigned int)&gpio2->y4;
166         i2c_gpio[I2C_10].bus->gpio_base = (unsigned int)&gpio1->e1;
167         i2c_gpio[I2C_12].bus->gpio_base = (unsigned int)&gpio1->e4;
168         i2c_gpio[I2C_13].bus->gpio_base = (unsigned int)&gpio1->e4;
169
170         i2c_gpio_init(i2c_gpio, I2C_NUM, I2C_5);
171 }
172
173 static void check_hw_revision(void);
174
175 int board_init(void)
176 {
177         gpio1 = (struct s5pc210_gpio_part1 *) S5PC210_GPIO_PART1_BASE;
178         gpio2 = (struct s5pc210_gpio_part2 *) S5PC210_GPIO_PART2_BASE;
179
180         gd->bd->bi_arch_number = MACH_TYPE;
181         gd->bd->bi_boot_params = PHYS_SDRAM_1 + 0x100;
182
183         check_hw_revision();
184
185         return 0;
186 }
187
188 int dram_init(void)
189 {
190         gd->ram_size = PHYS_SDRAM_1_SIZE + PHYS_SDRAM_2_SIZE;
191
192         /* Early init for i2c devices - Where these funcions should go?? */
193
194         /* Reset on max17040 */
195         check_battery(1);
196
197         /* pmic init */
198         init_pmic_lp3974();
199         init_pmic_max8952();
200
201         /* Reset on fsa9480 */
202         check_micro_usb(1);
203
204         return 0;
205 }
206
207 void dram_init_banksize(void)
208 {
209         gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
210         gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;
211         gd->bd->bi_dram[1].start = PHYS_SDRAM_2;
212         gd->bd->bi_dram[1].size = PHYS_SDRAM_2_SIZE;
213
214         gd->ram_size = gd->bd->bi_dram[0].size + gd->bd->bi_dram[1].size;
215 }
216
217 static void check_auto_burn(void)
218 {
219         unsigned int magic_base = CONFIG_SYS_SDRAM_BASE + 0x02000000;
220         unsigned int count = 0;
221         char buf[64];
222
223         /* OneNAND */
224         if (readl(magic_base) == 0x426f6f74) {          /* ASICC: Boot */
225                 puts("Auto buring bootloader\n");
226                 count += sprintf(buf + count, "run updateb; ");
227         }
228         /* MMC */
229         if (readl(magic_base) == 0x654D4D43) {          /* ASICC: eMMC */
230                 puts("Auto buring bootloader (eMMC)\n");
231                 count += sprintf(buf + count, "run updatemmc; ");
232         }
233         if (readl(magic_base + 0x4) == 0x4b65726e) {    /* ASICC: Kern */
234                 puts("Auto buring kernel\n");
235                 count += sprintf(buf + count, "run updatek; ");
236         }
237         /* Backup u-boot in eMMC */
238         if (readl(magic_base + 0x8) == 0x4261636B) {    /* ASICC: Back */
239                 puts("Auto buring u-boot image (boot partition2 in eMMC)\n");
240                 count += sprintf(buf + count, "run updatebackup; ");
241         }
242
243         if (count) {
244                 count += sprintf(buf + count, "reset");
245                 setenv("bootcmd", buf);
246         }
247
248         /* Clear the magic value */
249         memset((void *)magic_base, 0, 2);
250 }
251
252 static void check_battery(int mode)
253 {
254         unsigned char val[2];
255         unsigned char addr = 0x36;      /* max17040 fuel gauge */
256
257         i2c_set_bus_num(I2C_9);
258
259         if (i2c_probe(addr)) {
260                 puts("Can't found max17040 fuel gauge\n");
261                 return;
262         }
263
264         /* mode 0: check mode / 1: enable mode */
265         if (mode) {
266                 val[0] = 0x40;
267                 val[1] = 0x00;
268                 i2c_write(addr, 0xfe, 1, val, 2);
269         } else {
270                 i2c_read(addr, 0x04, 1, val, 1);
271                 printf("battery:\t%d%%\n", val[0]);
272                 battery_soc = val[0];
273         }
274 }
275
276 static int fsa9480_probe(void)
277 {
278         unsigned char addr = 0x25;
279
280         i2c_set_bus_num(I2C_10);
281
282         if (i2c_probe(addr)) {
283                 puts("Can't found fsa9480\n");
284                 return 1;
285         }
286
287         return 0;
288 }
289
290 static void charger_en(int enable);
291 static void into_charge_mode(int charger_speed);
292 static void check_micro_usb(int intr)
293 {
294         unsigned char addr;
295         unsigned char val[2];
296         static int started_charging_once = 0;
297         char *path;
298
299         if (fsa9480_probe())
300                 return;
301
302         addr = 0x25;    /* fsa9480 */
303
304         /* Clear Interrupt */
305         if (intr) {
306                 i2c_read(addr, 0x03, 1, val, 2);
307                 return;
308         }
309
310         /* Read Device Type 1 */
311         i2c_read(addr, 0x0a, 1, val, 1);
312
313 #define FSA_DEV1_CHARGER        (1 << 6)
314 #define FSA_DEV1_UART           (1 << 3)
315 #define FSA_DEV1_USB            (1 << 2)
316 #define FSA_DEV2_JIG_USB_OFF    (1 << 1)
317 #define FSA_DEV2_JIG_USB_ON     (1 << 0)
318
319         /* disable the charger related feature */
320         /*
321          * If USB, use default 475mA
322          * If Charger, use 600mA and go to charge mode
323          */
324         if ((val[0] & FSA_DEV1_CHARGER) && !started_charging_once) {
325                 started_charging_once = 1;
326
327                 /* If it's full, do not charge. */
328                 if (battery_soc < 100) {
329                         charger_en(600);
330                         into_charge_mode(600);
331                 } else
332                         charger_en(0);
333         } else if (val[0] & FSA_DEV1_USB) {
334                 if (battery_soc < 100)
335                         charger_en(500); /* enable charger and keep booting */
336                 else
337                         charger_en(0);
338         }
339
340         /* If reset status is watchdog reset then skip it */
341         if (get_reset_status() != SWRESET) {
342                 /* If Factory Mode is Boot ON-USB, go to download mode */
343                 i2c_read(addr, 0x07, 1, val, 1);
344
345 #define FSA_ADC_FAC_USB_OFF     0x18
346 #define FSA_ADC_FAC_USB_ON      0x19
347 #define FSA_ADC_FAC_UART        0x1d
348
349                 if (val[0] == FSA_ADC_FAC_USB_ON ||
350                         val[0] == FSA_ADC_FAC_USB_OFF)
351                         setenv("bootcmd", "usbdown");
352         }
353
354         path = getenv("usb");
355
356         if (!strncmp(path, "cp", 2))
357                 run_command("microusb cp", 0);
358 }
359
360 static void micro_usb_switch(int path)
361 {
362         unsigned char addr;
363         unsigned char val[2];
364
365         if (fsa9480_probe())
366                 return;
367
368         addr = 0x25;            /* fsa9480 */
369
370         if (path)
371                 val[0] = 0x90;  /* VAUDIO */
372         else
373                 val[0] = (1 << 5) | (1 << 2);   /* DHOST */
374
375         i2c_write(addr, 0x13, 1, val, 1);       /* MANSW1 */
376
377         i2c_read(addr, 0x2, 1, val, 1);
378         val[0] &= ~(1 << 2);                    /* Manual switching */
379         i2c_write(addr, 0x2, 1, val, 1);
380 }
381
382 #define LP3974_REG_IRQ1         0x00
383 #define LP3974_REG_IRQ2         0x01
384 #define LP3974_REG_IRQ3         0x02
385 #define LP3974_REG_ONOFF1       0x11
386 #define LP3974_REG_ONOFF2       0x12
387 #define LP3974_REG_ONOFF3       0x13
388 #define LP3974_REG_ONOFF4       0x14
389 #define LP3974_REG_LDO7         0x21
390 #define LP3974_REG_LDO17        0x29
391 #define LP3974_REG_UVLO         0xB9
392 #define LP3974_REG_MODCHG       0xEF
393 /* ONOFF1 */
394 #define LP3974_LDO3             (1 << 2)
395 /* ONOFF2 */
396 #define LP3974_LDO6             (1 << 7)
397 #define LP3974_LDO7             (1 << 6)
398 #define LP3974_LDO8             (1 << 5)
399 #define LP3974_LDO9             (1 << 4)
400 #define LP3974_LDO10            (1 << 3)
401 #define LP3974_LDO11            (1 << 2)
402 #define LP3974_LDO12            (1 << 1)
403 #define LP3974_LDO13            (1 << 0)
404 /* ONOFF3 */
405 #define LP3974_LDO14            (1 << 7)
406 #define LP3974_LDO15            (1 << 6)
407 #define LP3974_LDO16            (1 << 5)
408 #define LP3974_LDO17            (1 << 4)
409
410 static int lp3974_probe(void)
411 {
412         unsigned char addr = 0xCC >> 1;
413
414         i2c_set_bus_num(I2C_5);
415
416         if (i2c_probe(addr)) {
417                 puts("Can't found lp3974\n");
418                 return 1;
419         }
420
421         return 0;
422 }
423
424 static int max8952_probe(void)
425 {
426         unsigned char addr = 0xC0 >> 1;
427
428         i2c_set_bus_num(I2C_5);
429
430         if (i2c_probe(addr)) {
431                 puts("Cannot find MAX8952\n");
432                 return 1;
433         }
434
435         return 0;
436 }
437
438 static void init_pmic_lp3974(void)
439 {
440         unsigned char addr;
441         unsigned char val[2];
442
443         addr = 0xCC >> 1; /* LP3974 */
444         if (lp3974_probe())
445                 return;
446
447         /* LDO2 1.2V LDO3 1.1V */
448         val[0] = 0x86; /* (((1200 - 800) / 50) << 4) | (((1100 - 800) / 50)) */
449         i2c_write(addr, 0x1D, 1, val, 1);
450
451         /* LDO4 3.3V */
452         val[0] = 0x11; /* (3300 - 1600) / 100; */
453         i2c_write(addr, 0x1E, 1, val, 1);
454
455         /* LDO5 2.8V */
456         val[0] = 0x0c; /* (2800 - 1600) / 100; */
457         i2c_write(addr, 0x1F, 1, val, 1);
458
459         /* LDO6 not used: minimum */
460         val[0] = 0;
461         i2c_write(addr, 0x20, 1, val, 1);
462
463         /* LDO7 1.8V */
464         val[0] = 0x02; /* (1800 - 1600) / 100; */
465         i2c_write(addr, 0x21, 1, val, 1);
466
467         /* LDO8 3.3V LDO9 2.8V*/
468         val[0] = 0x30; /* (((3300 - 3000) / 100) << 4) | (((2800 - 2800) / 100) << 0); */
469         i2c_write(addr, 0x22, 1, val, 1);
470
471         /* LDO10 1.1V LDO11 3.3V */
472         val[0] = 0x71; /* (((1100 - 950) / 50) << 5) | (((3300 - 1600) / 100) << 0); */
473         i2c_write(addr, 0x23, 1, val, 1);
474
475         /* LDO12 2.8V */
476         val[0] = 0x14; /* (2800 - 1200) / 100 + 4; */
477         i2c_write(addr, 0x24, 1, val, 1);
478
479         /* LDO13 1.2V */
480         val[0] = 0x4; /* (1200 - 1200) / 100 + 4; */
481         i2c_write(addr, 0x25, 1, val, 1);
482
483         /* LDO14 1.8V */
484         val[0] = 0x6; /* (1800 - 1200) / 100; */
485         i2c_write(addr, 0x26, 1, val, 1);
486
487         /* LDO15 1.2V */
488         val[0] = 0; /* (1200 - 1200) / 100; */
489         i2c_write(addr, 0x27, 1, val, 1);
490
491         /* LDO16 2.8V */
492         val[0] = 0xc; /* (2800 - 1600) / 100; */
493         i2c_write(addr, 0x28, 1, val, 1);
494
495         /* LDO17 3.0V */
496         val[0] = 0xe; /* (3000 - 1600) / 100; */
497         i2c_write(addr, 0x29, 1, val, 1);
498
499         /*
500          * Because the data sheet of LP3974 does NOT mention default
501          * register values of ONOFF1~4 (ENABLE1~4), we ignore the given
502          * default values and set as we want
503          */
504
505         /* Note: To remove USB detect warning, Turn off LDO 8 first */
506
507         /*
508          * ONOFF2
509          * LDO6 OFF, LDO7 ON, LDO8 OFF, LDO9 ON,
510          * LDO10 OFF, LDO11 OFF, LDO12 OFF, LDO13 OFF
511          */
512         val[0] = 0x50;
513         i2c_write(addr, LP3974_REG_ONOFF2, 1, val, 1);
514
515         /*
516          * ONOFF1
517          * Buck1 ON, Buck2 OFF, Buck3 ON, Buck4 ON
518          * LDO2 ON, LDO3 OFF, LDO4 ON, LDO5 ON
519          */
520         val[0] = 0xBB;
521         i2c_write(addr, LP3974_REG_ONOFF1, 1, val, 1);
522
523         /*
524          * ONOFF3
525          * LDO14 OFF, LDO15 OFF, LGO16 OFF, LDO17 ON,
526          * EPWRHOLD OFF, EBATTMON OFF, ELBCNFG2 OFF, ELBCNFG1 OFF
527          */
528         val[0] = 0x10;
529         i2c_write(addr, LP3974_REG_ONOFF3, 1, val, 1);
530
531         /*
532          * ONOFF4
533          * EN32kAP ON, EN32kCP ON, ENVICHG ON, ENRAMP ON,
534          * RAMP 12mV/us (fastest)
535          */
536         val[0] = 0xFB;
537         i2c_write(addr, LP3974_REG_ONOFF4, 1, val, 1);
538
539         /*
540          * CHGCNTL1
541          * ICHG: 500mA (0x3) / 600mA (0x5)
542          * RESTART LEVEL: 100mA (0x1)
543          * EOC LEVEL: 30% (0x4) / 25% (0x3) : both 150mA of ICHG
544          * Let's start with slower charging mode and let micro usb driver
545          * determine whether we can do it fast or not. Thus, using the slower
546          * setting...
547          */
548         val[0] = 0x8B;
549         i2c_write(addr, 0xC, 1, val, 1);
550
551         /*
552          * CHGCNTL2
553          * CHARGER DISABLE: Enable (0x0)
554          * TEMP CONTROL: 105C (0x0)
555          * BATT SEL: 4.2V (0x0)
556          * FULL TIMEOUT: 5hr (0x0)
557          * ESAFEOUT2: ON (0x1)
558          * ESAFEOUT1: OFF (0x0)
559          */
560         val[0] = 0x40;
561         i2c_write(addr, 0xD, 1, val, 1);
562
563         val[0] = 0x0E; /* 1.1V @ DVSARM1(VINT) */
564         i2c_write(addr, 0x15, 1, val, 1);
565         val[0] = 0x0E; /* 1.1V @ DVSARM2(VINT) */
566         i2c_write(addr, 0x16, 1, val, 1);
567         val[0] = 0x0E; /* 1.1V @ DVSARM3(VINT) */
568         i2c_write(addr, 0x17, 1, val, 1);
569         val[0] = 0x0A; /* 1.0V @ DVSARM4(VINT) */
570         i2c_write(addr, 0x18, 1, val, 1);
571         val[0] = 0x12; /* 1.2V @ DVSINT1(VG3D) */
572         i2c_write(addr, 0x19, 1, val, 1);
573         val[0] = 0x0E; /* 1.1V @ DVSINT2(VG3D) */
574         i2c_write(addr, 0x1A, 1, val, 1);
575
576         val[0] = 0x2; /* 1.8V for BUCK3 VCC 1.8V PDA */
577         i2c_write(addr, 0x1B, 1, val, 1);
578         val[0] = 0x4; /* 1.2V for BUCK4 VMEM 1.2V C210 */
579         i2c_write(addr, 0x1C, 1, val, 1);
580
581         /* Use DVSARM1 for VINT */
582         gpio_direction_output(&gpio2->x0, 5, 0);
583         gpio_direction_output(&gpio2->x0, 6, 0);
584         /* Use DVSINT2 for VG3D */
585         gpio_direction_output(&gpio1->e2, 0, 1);
586
587         /*
588          * Default level of UVLO.
589          * UVLOf = 2.7V (0x3 << 4), UVLOr = 3.1V (0xB)
590          * set UVLOf to 2.55V (0 << 4).
591          */
592         val[0] = 0x2C;
593         i2c_write(addr, LP3974_REG_MODCHG, 1, val, 1);
594         val[0] = 0x58;
595         i2c_write(addr, LP3974_REG_MODCHG, 1, val, 1);
596         val[0] = 0xB1;
597         i2c_write(addr, LP3974_REG_MODCHG, 1, val, 1);
598
599         i2c_read_r(addr, LP3974_REG_UVLO, 1, val, 1);
600         val[0] = (val[0] & 0xf) | (0 << 4);
601         i2c_write(addr, LP3974_REG_UVLO, 1, val, 1);
602
603         val[0] = 0x00;
604         i2c_write(addr, LP3974_REG_MODCHG, 1, val, 1);
605 }
606
607 static int poweron_key_check(void)
608 {
609         unsigned char addr, val[2];
610
611         addr = 0xCC >> 1;
612         if (lp3974_probe())
613                 return 0;
614
615         i2c_read_r(addr, LP3974_REG_IRQ3, 1, val, 1);
616         return val[0] & 0x1;
617 }
618
619 int check_exit_key(void)
620 {
621         return poweron_key_check();
622 }
623
624 static int power_key_check(void)
625 {
626         unsigned char addr, val[4];
627         int tmp;
628
629         addr = 0xCC >> 1;
630         if (lp3974_probe())
631                 return -1;
632
633         /* power_key check */
634         i2c_read_r(addr, LP3974_REG_IRQ1, 1, val, 4);
635
636         tmp = ((val[0] & (1 << 7)) >> 7);
637
638         return tmp;
639 }
640
641 static void check_keypad(void)
642 {
643         unsigned int val = 0;
644         unsigned int power_key, auto_download = 0;
645
646         val = ~(gpio_get_value(&gpio2->x2, 1));
647
648         power_key = power_key_check();
649
650         if (power_key && (val & 0x1))
651                 auto_download = 1;
652
653         if (auto_download)
654                 setenv("bootcmd", "usbdown");
655 }
656
657 /*
658  * charger_en(): set lp3974 pmic's charger mode
659  * enable 0: disable charger
660  * 600: 600mA
661  * 500: 500mA
662  */
663 static void charger_en(int enable)
664 {
665         unsigned char addr = 0xCC >> 1; /* LP3974 */
666         unsigned char val[2];
667
668         if (lp3974_probe())
669                 return;
670
671         switch (enable) {
672         case 0:
673                 puts("Disable the charger.\n");
674                 i2c_read(addr, 0x0D, 1, val, 1);
675                 val[0] |= 0x1;
676                 i2c_write(addr, 0xD, 1, val, 1);
677                 break;
678         case 500:
679                 puts("Enable the charger @ 500mA\n");
680                 /*
681                  * CHGCNTL1
682                  * ICHG: 500mA (0x3) / 600mA (0x5)
683                  * RESTART LEVEL: 100mA (0x1)
684                  * EOC LEVEL: 30% (0x4) / 25% (0x3) : both 150mA of ICHG
685                  * Let's start with slower charging mode and
686                  * let micro usb driver determine whether we can do it
687                  * fast or not. Thus, using the slower setting...
688                  */
689                 val[0] = 0x8B;
690                 i2c_write(addr, 0x0C, 1, val, 1);
691                 i2c_read(addr, 0x0D, 1, val, 1);
692                 val[0] &= ~(0x1);
693                 i2c_write(addr, 0x0D, 1, val, 1);
694                 break;
695         case 600:
696                 puts("Enable the charger @ 600mA\n");
697                 val[0] = 0x6D;
698                 i2c_write(addr, 0x0C, 1, val, 1);
699                 i2c_read(addr, 0x0D, 1, val, 1);
700                 val[0] &= ~(0x1);
701                 i2c_write(addr, 0x0D, 1, val, 1);
702                 break;
703         default:
704                 puts("Incorrect charger setting.\n");
705         }
706 }
707
708 struct thermister_stat {
709         short centigrade;
710         unsigned short adc;
711 };
712
713 static struct thermister_stat adc_to_temperature_data[] = {
714         { .centigrade = -20,    .adc = 1856, },
715         { .centigrade = -15,    .adc = 1799, },
716         { .centigrade = -10,    .adc = 1730, },
717         { .centigrade = -5,     .adc = 1649, },
718         { .centigrade = 0,      .adc = 1556, },
719         { .centigrade = 5,      .adc = 1454, },
720         { .centigrade = 10,     .adc = 1343, },
721         { .centigrade = 15,     .adc = 1227, },
722         { .centigrade = 20,     .adc = 1109, },
723         { .centigrade = 25,     .adc = 992, },
724         { .centigrade = 30,     .adc = 880, },
725         { .centigrade = 35,     .adc = 773, },
726         { .centigrade = 40,     .adc = 675, },
727         { .centigrade = 45,     .adc = 586, },
728         { .centigrade = 50,     .adc = 507, },
729         { .centigrade = 55,     .adc = 436, },
730         { .centigrade = 58,     .adc = 399, },
731         { .centigrade = 63,     .adc = 343, },
732         { .centigrade = 65,     .adc = 322, },
733 };
734
735 #ifndef USHRT_MAX
736 #define USHRT_MAX       0xFFFFU
737 #endif
738
739 static int adc_to_temperature_centigrade(unsigned short adc)
740 {
741         int i;
742         int approximation;
743         /* low_*: Greatest Lower Bound,
744          *          *          *          * high_*: Smallest Upper Bound */
745         int low_temp = 0, high_temp = 0;
746         unsigned short low_adc = 0, high_adc = USHRT_MAX;
747         for (i = 0; i < ARRAY_SIZE(adc_to_temperature_data); i++) {
748                 if (adc_to_temperature_data[i].adc <= adc &&
749                                 adc_to_temperature_data[i].adc >= low_adc) {
750                         low_temp = adc_to_temperature_data[i].centigrade;
751                         low_adc = adc_to_temperature_data[i].adc;
752                 }
753                 if (adc_to_temperature_data[i].adc >= adc &&
754                                 adc_to_temperature_data[i].adc <= high_adc) {
755                         high_temp = adc_to_temperature_data[i].centigrade;
756                         high_adc = adc_to_temperature_data[i].adc;
757                 }
758         }
759
760         /* Linear approximation between cloest low and high,
761          * which is the weighted average of the two. */
762
763         /* The following equation is correct only when the two are different */
764         if (low_adc == high_adc)
765                 return low_temp;
766         if (ARRAY_SIZE(adc_to_temperature_data) < 2)
767                 return 20; /* The room temperature */
768         if (low_adc == 0)
769                 return high_temp;
770         if (high_adc == USHRT_MAX)
771                 return low_temp;
772
773         approximation = low_temp * (adc - low_adc) +
774                 high_temp * (high_adc - adc);
775         approximation /= high_adc - low_adc;
776
777         return approximation;
778 }
779
780 static int adc_get_average_ambient_temperature(void)
781 {
782         unsigned short min = USHRT_MAX;
783         unsigned short max = 0;
784         unsigned int sum = 0;
785         unsigned int measured = 0;
786         int i;
787
788         for (i = 0; i < 7; i++) {
789                 /* XADCAIN6 */
790                 unsigned short measurement = get_adc_value(6);
791                 sum += measurement;
792                 measured++;
793                 if (min > measurement)
794                         min = measurement;
795                 if (max < measurement)
796                         max = measurement;
797         }
798         if (measured >= 3) {
799                 measured -= 2;
800                 sum -= min;
801                 sum -= max;
802         }
803         sum /= measured;
804         printf("Average Ambient Temperature = %d(ADC=%d)\n",
805                         adc_to_temperature_centigrade(sum), sum);
806         return adc_to_temperature_centigrade(sum);
807 }
808
809 enum temperature_level {
810         _TEMP_OK,
811         _TEMP_OK_HIGH,
812         _TEMP_OK_LOW,
813         _TEMP_TOO_HIGH,
814         _TEMP_TOO_LOW,
815 };
816
817 static enum temperature_level temperature_check(void)
818 {
819         int temp = adc_get_average_ambient_temperature();
820         if (temp < -5)
821                 return _TEMP_TOO_LOW;
822         if (temp < 0)
823                 return _TEMP_OK_LOW;
824         if (temp > 63)
825                 return _TEMP_TOO_HIGH;
826         if (temp > 58)
827                 return _TEMP_OK_HIGH;
828         return _TEMP_OK;
829 }
830
831 /*
832  * into_charge_mode()
833  * Run a charge loop with animation and temperature check with sleep
834 **/
835 static void into_charge_mode(int charger_speed)
836 {
837         int i, j, delay;
838         enum temperature_level previous_state = _TEMP_OK;
839         unsigned int wakeup_stat = 0;
840
841         /* 1. Show Animation */
842         for (i = 0; i < 5; i++) {
843                 for (j = 0; j < 5; j++) {
844                         printf(".");
845                         for (delay = 0; delay < 1000; delay++)
846                                 udelay(1000);
847                 }
848                 printf("\n");
849         }
850
851         /* 2. Loop with temperature check and sleep */
852         do {
853                 /* TODO: 2.A. Setup wakeup source and rtc tick */
854
855                 /* TODO: 2.B. Go to sleep */
856                 for (delay = 0; delay < 4000; delay++)
857                         udelay(1000);
858
859                 /* 2.C. Check the temperature */
860                 switch (temperature_check()) {
861                 case _TEMP_OK:
862                         charger_en(charger_speed);
863                         previous_state = _TEMP_OK;
864                         break;
865                 case _TEMP_TOO_LOW:
866                         charger_en(0);
867                         previous_state = _TEMP_TOO_LOW;
868                         break;
869                 case _TEMP_TOO_HIGH:
870                         charger_en(0);
871                         previous_state = _TEMP_TOO_HIGH;
872                         break;
873                 case _TEMP_OK_LOW:
874                         if (previous_state == _TEMP_TOO_LOW) {
875                                 charger_en(0);
876                         } else {
877                                 charger_en(charger_speed);
878                                 previous_state = _TEMP_OK;
879                         }
880                         break;
881                 case _TEMP_OK_HIGH:
882                         if (previous_state == _TEMP_TOO_HIGH) {
883                                 charger_en(0);
884                         } else {
885                                 charger_en(charger_speed);
886                                 previous_state = _TEMP_OK;
887                         }
888                         break;
889                 }
890         } while (wakeup_stat == 0x04);
891 }
892
893 static void init_pmic_max8952(void)
894 {
895         unsigned char addr;
896         unsigned char val[2];
897
898         addr = 0xC0 >> 1; /* MAX8952 */
899         if (max8952_probe())
900                 return;
901
902         /* MODE0: 1.10V: Default */
903         val[0] = 33;
904         i2c_write(addr, 0x00, 1, val, 1);
905         /* MODE1: 1.20V */
906         val[0] = 43;
907         i2c_write(addr, 0x01, 1, val, 1);
908         /* MODE2: 1.05V */
909         val[0] = 28;
910         i2c_write(addr, 0x02, 1, val, 1);
911         /* MODE3: 0.95V */
912         val[0] = 18;
913         i2c_write(addr, 0x03, 1, val, 1);
914
915         /*
916          * Note: use the default setting and configure pins high
917          * to generate the 1.1V
918          */
919         /* VARM_OUTPUT_SEL_A / VID_0 / XEINT_3 (GPX0[3]) = default 0 */
920         gpio_direction_output(&gpio2->x0, 3, 0);
921         /* VARM_OUTPUT_SEL_B / VID_1 / XEINT_4 (GPX0[4]) = default 0 */
922         gpio_direction_output(&gpio2->x0, 4, 0);
923
924         /* CONTROL: Disable PULL_DOWN */
925         val[0] = 0;
926         i2c_write(addr, 0x04, 1, val, 1);
927
928         /* SYNC: Do Nothing */
929         /* RAMP: As Fast As Possible: Default: Do Nothing */
930 }
931
932 #ifdef CONFIG_LCD
933
934 void fimd_clk_set(void)
935 {
936         struct s5pc210_clock *clk =
937                 (struct s5pc210_clock *)samsung_get_base_clock();
938
939         /* workaround */
940         unsigned long display_ctrl = 0x10010210;
941         unsigned int cfg = 0;
942
943         /* LCD0_BLK FIFO S/W reset */
944         cfg = readl(display_ctrl);
945         cfg |= (1 << 9);
946         writel(cfg, display_ctrl);
947
948         cfg = 0;
949
950         /* FIMD of LBLK0 Bypass Selection */
951         cfg = readl(display_ctrl);
952         cfg &= ~(1 << 9);
953         cfg |= (1 << 1);
954         writel(cfg, display_ctrl);
955
956         /* set lcd src clock */
957         cfg = readl(&clk->src_lcd0);
958         cfg &= ~(0xf);
959         cfg |= 0x6;
960         writel(cfg, &clk->src_lcd0);
961
962         /* set fimd ratio */
963         cfg = readl(&clk->div_lcd0);
964         cfg &= ~(0xf);
965         cfg |= 0x1;
966         writel(cfg, &clk->div_lcd0);
967 }
968
969 extern void ld9040_set_platform_data(struct spi_platform_data *pd);
970
971 struct spi_platform_data spi_pd;
972
973 static void lcd_cfg_gpio(void)
974 {
975         unsigned int i;
976
977         for (i = 0; i < 8; i++) {
978                 /* set GPF0,1,2[0:7] for RGB Interface and Data lines (32bit) */
979                 gpio_cfg_pin(&gpio1->f0, i, GPIO_FUNC(2));
980                 gpio_cfg_pin(&gpio1->f1, i, GPIO_FUNC(2));
981                 gpio_cfg_pin(&gpio1->f2, i, GPIO_FUNC(2));
982                 /* pull-up/down disable */
983                 gpio_set_pull(&gpio1->f0, i, GPIO_PULL_NONE);
984                 gpio_set_pull(&gpio1->f1, i, GPIO_PULL_NONE);
985                 gpio_set_pull(&gpio1->f2, i, GPIO_PULL_NONE);
986
987                 /* drive strength to max (24bit) */
988                 gpio_set_drv(&gpio1->f0, i, GPIO_DRV_4X);
989                 gpio_set_rate(&gpio1->f0, i, GPIO_DRV_SLOW);
990                 gpio_set_drv(&gpio1->f1, i, GPIO_DRV_4X);
991                 gpio_set_rate(&gpio1->f1, i, GPIO_DRV_SLOW);
992                 gpio_set_drv(&gpio1->f2, i, GPIO_DRV_4X);
993                 gpio_set_rate(&gpio1->f2, i, GPIO_DRV_SLOW);
994         }
995
996         for (i = 0; i < 4; i++) {
997                 /* set GPF3[0:3] for RGB Interface and Data lines (32bit) */
998                 gpio_cfg_pin(&gpio1->f3, i, GPIO_PULL_UP);
999                 /* pull-up/down disable */
1000                 gpio_set_pull(&gpio1->f3, i, GPIO_PULL_NONE);
1001                 /* drive strength to max (24bit) */
1002                 gpio_set_drv(&gpio1->f3, i, GPIO_DRV_4X);
1003                 gpio_set_rate(&gpio1->f3, i, GPIO_DRV_SLOW);
1004         }
1005
1006         /* gpio pad configuration for LCD reset. */
1007         gpio_direction_output(&gpio2->y4, 5, 1);
1008
1009         /*
1010          * gpio pad configuration for
1011          * DISPLAY_CS, DISPLAY_CLK, DISPLAY_SO, DISPLAY_SI.
1012          */
1013         gpio_cfg_pin(&gpio2->y4, 3, GPIO_OUTPUT);
1014         gpio_cfg_pin(&gpio2->y3, 1, GPIO_OUTPUT);
1015         gpio_cfg_pin(&gpio2->y3, 3, GPIO_OUTPUT);
1016
1017         spi_pd.cs_bank = &gpio2->y4;
1018         spi_pd.cs_num = 3;
1019         spi_pd.clk_bank = &gpio2->y3;
1020         spi_pd.clk_num = 1;
1021         spi_pd.si_bank = &gpio2->y3;
1022         spi_pd.si_num = 3;
1023
1024         spi_pd.mode = SPI_MODE_3;
1025
1026         spi_pd.cs_active = ACTIVE_LOW;
1027         spi_pd.word_len = 8;
1028
1029         ld9040_set_platform_data(&spi_pd);
1030
1031         return;
1032 }
1033
1034 extern void ld9040_cfg_ldo(void);
1035 extern void ld9040_enable_ldo(unsigned int onoff);
1036
1037 int s5p_no_lcd_support(void)
1038 {
1039         return 0;
1040 }
1041
1042 void init_panel_info(vidinfo_t *vid)
1043 {
1044         vid->vl_freq    = 60;
1045         vid->vl_col     = 480;
1046         vid->vl_row     = 800;
1047         vid->vl_width   = 480;
1048         vid->vl_height  = 800;
1049         vid->vl_clkp    = CONFIG_SYS_HIGH;
1050         vid->vl_hsp     = CONFIG_SYS_HIGH;
1051         vid->vl_vsp     = CONFIG_SYS_HIGH;
1052         vid->vl_dp      = CONFIG_SYS_HIGH;
1053
1054         vid->vl_bpix    = 32;
1055         /* disable dual lcd mode. */
1056         vid->dual_lcd_enabled = 0;
1057
1058         /* LD9040 LCD Panel */
1059         vid->vl_hspw    = 2;
1060         vid->vl_hbpd    = 16;
1061         vid->vl_hfpd    = 16;
1062
1063         vid->vl_vspw    = 2;
1064         vid->vl_vbpd    = 6;
1065         vid->vl_vfpd    = 4;
1066
1067         vid->cfg_gpio = lcd_cfg_gpio;
1068         vid->backlight_on = NULL;
1069         vid->lcd_power_on = NULL;       /* Don't need the poweron squence */
1070         vid->reset_lcd = NULL;          /* Don't need the reset squence */
1071
1072         vid->cfg_ldo = ld9040_cfg_ldo;
1073         vid->enable_ldo = ld9040_enable_ldo;
1074
1075         vid->init_delay = 0;
1076         vid->power_on_delay = 0;
1077         vid->reset_delay = 0;
1078         vid->interface_mode = FIMD_RGB_INTERFACE;
1079
1080         /* board should be detected at here. */
1081
1082         /* for LD8040. */
1083         vid->pclk_name = MPLL;
1084         vid->sclk_div = 1;
1085
1086         setenv("lcdinfo", "lcd=ld9040");
1087 }
1088 #endif
1089
1090 static unsigned int get_hw_revision(void)
1091 {
1092         int hwrev, mode0, mode1;
1093
1094         mode0 = get_adc_value(1);               /* HWREV_MODE0 */
1095         mode1 = get_adc_value(2);               /* HWREV_MODE1 */
1096
1097         /*
1098          * XXX Always set the default hwrev as the latest board
1099          * ADC = (voltage) / 3.3 * 4096
1100          */
1101         hwrev = 3;
1102
1103 #define IS_RANGE(x, min, max)   ((x) > (min) && (x) < (max))
1104         if (IS_RANGE(mode0, 80, 200) && IS_RANGE(mode1, 80, 200))
1105                 hwrev = 0x0;            /* 0.01V        0.01V */
1106         if (IS_RANGE(mode0, 750, 1000) && IS_RANGE(mode1, 80, 200))
1107                 hwrev = 0x1;            /* 610mV        0.01V */
1108         if (IS_RANGE(mode0, 1300, 1700) && IS_RANGE(mode1, 80, 200))
1109                 hwrev = 0x2;            /* 1.16V        0.01V */
1110         if (IS_RANGE(mode0, 2000, 2400) && IS_RANGE(mode1, 80, 200))
1111                 hwrev = 0x3;            /* 1.79V        0.01V */
1112 #undef IS_RANGE
1113
1114         debug("mode0: %d, mode1: %d, hwrev 0x%x\n", mode0, mode1, hwrev);
1115
1116         return hwrev;
1117 }
1118
1119 static const char * const pcb_rev[] = {
1120         "UNIV_0.0",
1121         "UNIV_0.1",
1122         "AQUILA_1.7",
1123         "AQUILA_1.9",
1124 };
1125
1126 static void check_hw_revision(void)
1127 {
1128         int hwrev;
1129
1130         hwrev = get_hw_revision();
1131
1132         board_rev |= hwrev;
1133 }
1134
1135 static void show_hw_revision(void)
1136 {
1137         printf("HW Revision:\t0x%x\n", board_rev);
1138         printf("PCB Revision:\t%s\n", pcb_rev[board_rev & 0xf]);
1139 }
1140
1141 void get_rev_info(char *rev_info)
1142 {
1143         sprintf(rev_info, "HW Revision: 0x%x (%s)\n",
1144                         board_rev, pcb_rev[board_rev & 0xf]);
1145 }
1146
1147 static void check_reset_status(void)
1148 {
1149         int status = get_reset_status();
1150
1151         puts("Reset Status: ");
1152
1153         switch (status) {
1154         case EXTRESET:
1155                 puts("Pin(Ext) Reset\n");
1156                 break;
1157         case WARMRESET:
1158                 puts("Warm Reset\n");
1159                 break;
1160         case WDTRESET:
1161                 puts("Watchdog Reset\n");
1162                 break;
1163         case SWRESET:
1164                 puts("S/W Reset\n");
1165                 break;
1166         default:
1167                 printf("Unknown (0x%x)\n", status);
1168         }
1169 }
1170
1171 #ifdef CONFIG_CMD_RAMOOPS
1172 static void show_dump_msg(void)
1173 {
1174         int ret;
1175
1176         ret = ramoops_init(samsung_get_base_modem());
1177
1178         if (!ret)
1179                 setenv("bootdelay", "-1");
1180 }
1181 #endif
1182
1183 #ifdef CONFIG_MISC_INIT_R
1184 int misc_init_r(void)
1185 {
1186         check_reset_status();
1187 #ifdef CONFIG_CMD_RAMOOPS
1188         show_dump_msg();
1189 #endif
1190
1191         show_hw_revision();
1192         check_keypad();
1193
1194         check_auto_burn();
1195
1196         /* check max17040 */
1197         check_battery(0);
1198
1199         /* check fsa9480 */
1200         check_micro_usb(0);
1201
1202 #ifdef CONFIG_INFO_ACTION
1203         info_action_check();
1204 #endif
1205
1206 #ifdef CONFIG_CMD_PMIC
1207         pmic_bus_init(I2C_5);
1208         run_command("pmic ldo 4 off", 0);       /* adc off */
1209 #endif
1210
1211         return 0;
1212 }
1213 #endif
1214
1215 #ifdef CONFIG_CMD_USBDOWN
1216 int usb_board_init(void)
1217 {
1218         /* interrupt clear */
1219         poweron_key_check();
1220
1221 #ifdef CONFIG_CMD_PMIC
1222         run_command("pmic ldo 8 on", 0);
1223         run_command("pmic ldo 3 on", 0);
1224         run_command("pmic safeout 1 on", 0);
1225 #endif
1226         return 0;
1227 }
1228 #endif
1229
1230 #ifdef CONFIG_GENERIC_MMC
1231 int s5p_no_mmc_support(void)
1232 {
1233         return 0;
1234 }
1235
1236 int board_mmc_init(bd_t *bis)
1237 {
1238         int i, err;
1239
1240         switch (get_hwrev()) {
1241         case 0:
1242                 /*
1243                  * Set the low to enable LDO_EN
1244                  * But when you use the test board for eMMC booting
1245                  * you should set it HIGH since it removes the inverter
1246                  */
1247                 /* MASSMEMORY_EN: XMDMDATA_6: GPE3[6] */
1248                 gpio_direction_output(&gpio1->e3, 6, 0);
1249                 break;
1250         default:
1251                 /*
1252                  * Default reset state is High and there's no inverter
1253                  * But set it as HIGH to ensure
1254                  */
1255                 /* MASSMEMORY_EN: XMDMADDR_3: GPE1[3] */
1256                 gpio_direction_output(&gpio1->e1, 3, 1);
1257                 break;
1258         }
1259
1260 #ifdef CONFIG_MMC_BOOT_EVT0
1261         /*
1262          * In MMC boot test board don't have inverter
1263          */
1264         gpio_direction_output(&gpio1->e3, 6, 1);
1265 #endif
1266
1267         /*
1268          * eMMC GPIO:
1269          * SDR 8-bit@48MHz at MMC0
1270          * GPK0[0]      SD_0_CLK(2)
1271          * GPK0[1]      SD_0_CMD(2)
1272          * GPK0[2]      SD_0_CDn        -> Not used
1273          * GPK0[3:6]    SD_0_DATA[0:3](2)
1274          * GPK1[3:6]    SD_0_DATA[0:3](3)
1275          *
1276          * DDR 4-bit@26MHz at MMC4
1277          * GPK0[0]      SD_4_CLK(3)
1278          * GPK0[1]      SD_4_CMD(3)
1279          * GPK0[2]      SD_4_CDn        -> Not used
1280          * GPK0[3:6]    SD_4_DATA[0:3](3)
1281          * GPK1[3:6]    SD_4_DATA[4:7](4)
1282          */
1283         for (i = 0; i < 7; i++) {
1284                 if (i == 2)
1285                         continue;
1286                 /* GPK0[0:6] special function 2 */
1287                 gpio_cfg_pin(&gpio2->k0, i, 0x2);
1288                 /* GPK0[0:6] pull disable */
1289                 gpio_set_pull(&gpio2->k0, i, GPIO_PULL_NONE);
1290                 /* GPK0[0:6] drv 4x */
1291                 gpio_set_drv(&gpio2->k0, i, GPIO_DRV_4X);
1292         }
1293
1294         for (i = 3; i < 7; i++) {
1295                 /* GPK1[3:6] special function 3 */
1296                 gpio_cfg_pin(&gpio2->k1, i, 0x3);
1297                 /* GPK1[3:6] pull disable */
1298                 gpio_set_pull(&gpio2->k1, i, GPIO_PULL_NONE);
1299                 /* GPK1[3:6] drv 4x */
1300                 gpio_set_drv(&gpio2->k1, i, GPIO_DRV_4X);
1301         }
1302
1303         /* T-flash detect */
1304         gpio_cfg_pin(&gpio2->x3, 4, 0xf);
1305         gpio_set_pull(&gpio2->x3, 4, GPIO_PULL_UP);
1306
1307         /*
1308          * MMC device init
1309          * mmc0  : eMMC (8-bit buswidth)
1310          * mmc2  : SD card (4-bit buswidth)
1311          */
1312         err = s5p_mmc_init(0, 8);
1313
1314         /*
1315          * Check the T-flash  detect pin
1316          * GPX3[4] T-flash detect pin
1317          */
1318         if (!gpio_get_value(&gpio2->x3, 4)) {
1319                 /*
1320                  * SD card GPIO:
1321                  * GPK2[0]      SD_2_CLK(2)
1322                  * GPK2[1]      SD_2_CMD(2)
1323                  * GPK2[2]      SD_2_CDn        -> Not used
1324                  * GPK2[3:6]    SD_2_DATA[0:3](2)
1325                  */
1326                 for (i = 0; i < 7; i++) {
1327                         if (i == 2)
1328                                 continue;
1329                         /* GPK2[0:6] special function 2 */
1330                         gpio_cfg_pin(&gpio2->k2, i, 0x2);
1331                         /* GPK2[0:6] pull disable */
1332                         gpio_set_pull(&gpio2->k2, i, GPIO_PULL_NONE);
1333                         /* GPK2[0:6] drv 4x */
1334                         gpio_set_drv(&gpio2->k2, i, GPIO_DRV_4X);
1335                 }
1336                 err = s5p_mmc_init(2, 4);
1337         }
1338
1339         return err;
1340
1341 }
1342 #endif
1343
1344 #ifdef CONFIG_CMD_DEVICE_POWER
1345 static int do_microusb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1346 {
1347         switch (argc) {
1348         case 2:
1349                 if (strncmp(argv[1], "cp", 2) == 0) {
1350                         micro_usb_switch(1);
1351                         run_command("pmic safeout 2 on", 0);
1352                         setenv("usb", "cp");
1353                 } else if (strncmp(argv[1], "ap", 2) == 0) {
1354                         micro_usb_switch(0);
1355                         run_command("pmic safeout 2 off", 0);
1356                         setenv("usb", "ap");
1357                 }
1358                 break;
1359         default:
1360                 cmd_usage(cmdtp);
1361                 return 1;
1362         }
1363
1364         saveenv();
1365
1366         printf("USB Path is set to %s\n", getenv("usb"));
1367
1368         return 0;
1369 }
1370
1371 U_BOOT_CMD(
1372         microusb,               CONFIG_SYS_MAXARGS,     1, do_microusb,
1373         "Micro USB Switch",
1374         "cp - switch to CP\n"
1375         "microusb ap - switch to AP\n"
1376 );
1377 #endif