max8998: move power key check routines
[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 int check_exit_key(void)
608 {
609         return pmic_get_irq(PWRON1S);
610 }
611
612 static void check_keypad(void)
613 {
614         unsigned int val = 0;
615         unsigned int power_key, auto_download = 0;
616
617         val = ~(gpio_get_value(&gpio2->x2, 1));
618
619         power_key = pmic_get_irq(PWRONR);
620
621         if (power_key && (val & 0x1))
622                 auto_download = 1;
623
624         if (auto_download)
625                 setenv("bootcmd", "usbdown");
626 }
627
628 /*
629  * charger_en(): set lp3974 pmic's charger mode
630  * enable 0: disable charger
631  * 600: 600mA
632  * 500: 500mA
633  */
634 static void charger_en(int enable)
635 {
636         unsigned char addr = 0xCC >> 1; /* LP3974 */
637         unsigned char val[2];
638
639         if (lp3974_probe())
640                 return;
641
642         switch (enable) {
643         case 0:
644                 puts("Disable the charger.\n");
645                 i2c_read(addr, 0x0D, 1, val, 1);
646                 val[0] |= 0x1;
647                 i2c_write(addr, 0xD, 1, val, 1);
648                 break;
649         case 500:
650                 puts("Enable the charger @ 500mA\n");
651                 /*
652                  * CHGCNTL1
653                  * ICHG: 500mA (0x3) / 600mA (0x5)
654                  * RESTART LEVEL: 100mA (0x1)
655                  * EOC LEVEL: 30% (0x4) / 25% (0x3) : both 150mA of ICHG
656                  * Let's start with slower charging mode and
657                  * let micro usb driver determine whether we can do it
658                  * fast or not. Thus, using the slower setting...
659                  */
660                 val[0] = 0x8B;
661                 i2c_write(addr, 0x0C, 1, val, 1);
662                 i2c_read(addr, 0x0D, 1, val, 1);
663                 val[0] &= ~(0x1);
664                 i2c_write(addr, 0x0D, 1, val, 1);
665                 break;
666         case 600:
667                 puts("Enable the charger @ 600mA\n");
668                 val[0] = 0x6D;
669                 i2c_write(addr, 0x0C, 1, val, 1);
670                 i2c_read(addr, 0x0D, 1, val, 1);
671                 val[0] &= ~(0x1);
672                 i2c_write(addr, 0x0D, 1, val, 1);
673                 break;
674         default:
675                 puts("Incorrect charger setting.\n");
676         }
677 }
678
679 struct thermister_stat {
680         short centigrade;
681         unsigned short adc;
682 };
683
684 static struct thermister_stat adc_to_temperature_data[] = {
685         { .centigrade = -20,    .adc = 1856, },
686         { .centigrade = -15,    .adc = 1799, },
687         { .centigrade = -10,    .adc = 1730, },
688         { .centigrade = -5,     .adc = 1649, },
689         { .centigrade = 0,      .adc = 1556, },
690         { .centigrade = 5,      .adc = 1454, },
691         { .centigrade = 10,     .adc = 1343, },
692         { .centigrade = 15,     .adc = 1227, },
693         { .centigrade = 20,     .adc = 1109, },
694         { .centigrade = 25,     .adc = 992, },
695         { .centigrade = 30,     .adc = 880, },
696         { .centigrade = 35,     .adc = 773, },
697         { .centigrade = 40,     .adc = 675, },
698         { .centigrade = 45,     .adc = 586, },
699         { .centigrade = 50,     .adc = 507, },
700         { .centigrade = 55,     .adc = 436, },
701         { .centigrade = 58,     .adc = 399, },
702         { .centigrade = 63,     .adc = 343, },
703         { .centigrade = 65,     .adc = 322, },
704 };
705
706 #ifndef USHRT_MAX
707 #define USHRT_MAX       0xFFFFU
708 #endif
709
710 static int adc_to_temperature_centigrade(unsigned short adc)
711 {
712         int i;
713         int approximation;
714         /* low_*: Greatest Lower Bound,
715          *          *          *          * high_*: Smallest Upper Bound */
716         int low_temp = 0, high_temp = 0;
717         unsigned short low_adc = 0, high_adc = USHRT_MAX;
718         for (i = 0; i < ARRAY_SIZE(adc_to_temperature_data); i++) {
719                 if (adc_to_temperature_data[i].adc <= adc &&
720                                 adc_to_temperature_data[i].adc >= low_adc) {
721                         low_temp = adc_to_temperature_data[i].centigrade;
722                         low_adc = adc_to_temperature_data[i].adc;
723                 }
724                 if (adc_to_temperature_data[i].adc >= adc &&
725                                 adc_to_temperature_data[i].adc <= high_adc) {
726                         high_temp = adc_to_temperature_data[i].centigrade;
727                         high_adc = adc_to_temperature_data[i].adc;
728                 }
729         }
730
731         /* Linear approximation between cloest low and high,
732          * which is the weighted average of the two. */
733
734         /* The following equation is correct only when the two are different */
735         if (low_adc == high_adc)
736                 return low_temp;
737         if (ARRAY_SIZE(adc_to_temperature_data) < 2)
738                 return 20; /* The room temperature */
739         if (low_adc == 0)
740                 return high_temp;
741         if (high_adc == USHRT_MAX)
742                 return low_temp;
743
744         approximation = low_temp * (adc - low_adc) +
745                 high_temp * (high_adc - adc);
746         approximation /= high_adc - low_adc;
747
748         return approximation;
749 }
750
751 static int adc_get_average_ambient_temperature(void)
752 {
753         unsigned short min = USHRT_MAX;
754         unsigned short max = 0;
755         unsigned int sum = 0;
756         unsigned int measured = 0;
757         int i;
758
759         for (i = 0; i < 7; i++) {
760                 /* XADCAIN6 */
761                 unsigned short measurement = get_adc_value(6);
762                 sum += measurement;
763                 measured++;
764                 if (min > measurement)
765                         min = measurement;
766                 if (max < measurement)
767                         max = measurement;
768         }
769         if (measured >= 3) {
770                 measured -= 2;
771                 sum -= min;
772                 sum -= max;
773         }
774         sum /= measured;
775         printf("Average Ambient Temperature = %d(ADC=%d)\n",
776                         adc_to_temperature_centigrade(sum), sum);
777         return adc_to_temperature_centigrade(sum);
778 }
779
780 enum temperature_level {
781         _TEMP_OK,
782         _TEMP_OK_HIGH,
783         _TEMP_OK_LOW,
784         _TEMP_TOO_HIGH,
785         _TEMP_TOO_LOW,
786 };
787
788 static enum temperature_level temperature_check(void)
789 {
790         int temp = adc_get_average_ambient_temperature();
791         if (temp < -5)
792                 return _TEMP_TOO_LOW;
793         if (temp < 0)
794                 return _TEMP_OK_LOW;
795         if (temp > 63)
796                 return _TEMP_TOO_HIGH;
797         if (temp > 58)
798                 return _TEMP_OK_HIGH;
799         return _TEMP_OK;
800 }
801
802 /*
803  * into_charge_mode()
804  * Run a charge loop with animation and temperature check with sleep
805 **/
806 static void into_charge_mode(int charger_speed)
807 {
808         int i, j, delay;
809         enum temperature_level previous_state = _TEMP_OK;
810         unsigned int wakeup_stat = 0;
811
812         /* 1. Show Animation */
813         for (i = 0; i < 5; i++) {
814                 for (j = 0; j < 5; j++) {
815                         printf(".");
816                         for (delay = 0; delay < 1000; delay++)
817                                 udelay(1000);
818                 }
819                 printf("\n");
820         }
821
822         /* 2. Loop with temperature check and sleep */
823         do {
824                 /* TODO: 2.A. Setup wakeup source and rtc tick */
825
826                 /* TODO: 2.B. Go to sleep */
827                 for (delay = 0; delay < 4000; delay++)
828                         udelay(1000);
829
830                 /* 2.C. Check the temperature */
831                 switch (temperature_check()) {
832                 case _TEMP_OK:
833                         charger_en(charger_speed);
834                         previous_state = _TEMP_OK;
835                         break;
836                 case _TEMP_TOO_LOW:
837                         charger_en(0);
838                         previous_state = _TEMP_TOO_LOW;
839                         break;
840                 case _TEMP_TOO_HIGH:
841                         charger_en(0);
842                         previous_state = _TEMP_TOO_HIGH;
843                         break;
844                 case _TEMP_OK_LOW:
845                         if (previous_state == _TEMP_TOO_LOW) {
846                                 charger_en(0);
847                         } else {
848                                 charger_en(charger_speed);
849                                 previous_state = _TEMP_OK;
850                         }
851                         break;
852                 case _TEMP_OK_HIGH:
853                         if (previous_state == _TEMP_TOO_HIGH) {
854                                 charger_en(0);
855                         } else {
856                                 charger_en(charger_speed);
857                                 previous_state = _TEMP_OK;
858                         }
859                         break;
860                 }
861         } while (wakeup_stat == 0x04);
862 }
863
864 static void init_pmic_max8952(void)
865 {
866         unsigned char addr;
867         unsigned char val[2];
868
869         addr = 0xC0 >> 1; /* MAX8952 */
870         if (max8952_probe())
871                 return;
872
873         /* MODE0: 1.10V: Default */
874         val[0] = 33;
875         i2c_write(addr, 0x00, 1, val, 1);
876         /* MODE1: 1.20V */
877         val[0] = 43;
878         i2c_write(addr, 0x01, 1, val, 1);
879         /* MODE2: 1.05V */
880         val[0] = 28;
881         i2c_write(addr, 0x02, 1, val, 1);
882         /* MODE3: 0.95V */
883         val[0] = 18;
884         i2c_write(addr, 0x03, 1, val, 1);
885
886         /*
887          * Note: use the default setting and configure pins high
888          * to generate the 1.1V
889          */
890         /* VARM_OUTPUT_SEL_A / VID_0 / XEINT_3 (GPX0[3]) = default 0 */
891         gpio_direction_output(&gpio2->x0, 3, 0);
892         /* VARM_OUTPUT_SEL_B / VID_1 / XEINT_4 (GPX0[4]) = default 0 */
893         gpio_direction_output(&gpio2->x0, 4, 0);
894
895         /* CONTROL: Disable PULL_DOWN */
896         val[0] = 0;
897         i2c_write(addr, 0x04, 1, val, 1);
898
899         /* SYNC: Do Nothing */
900         /* RAMP: As Fast As Possible: Default: Do Nothing */
901 }
902
903 #ifdef CONFIG_LCD
904
905 void fimd_clk_set(void)
906 {
907         struct s5pc210_clock *clk =
908                 (struct s5pc210_clock *)samsung_get_base_clock();
909
910         /* workaround */
911         unsigned long display_ctrl = 0x10010210;
912         unsigned int cfg = 0;
913
914         /* LCD0_BLK FIFO S/W reset */
915         cfg = readl(display_ctrl);
916         cfg |= (1 << 9);
917         writel(cfg, display_ctrl);
918
919         cfg = 0;
920
921         /* FIMD of LBLK0 Bypass Selection */
922         cfg = readl(display_ctrl);
923         cfg &= ~(1 << 9);
924         cfg |= (1 << 1);
925         writel(cfg, display_ctrl);
926
927         /* set lcd src clock */
928         cfg = readl(&clk->src_lcd0);
929         cfg &= ~(0xf);
930         cfg |= 0x6;
931         writel(cfg, &clk->src_lcd0);
932
933         /* set fimd ratio */
934         cfg = readl(&clk->div_lcd0);
935         cfg &= ~(0xf);
936         cfg |= 0x1;
937         writel(cfg, &clk->div_lcd0);
938 }
939
940 extern void ld9040_set_platform_data(struct spi_platform_data *pd);
941
942 struct spi_platform_data spi_pd;
943
944 static void lcd_cfg_gpio(void)
945 {
946         unsigned int i;
947
948         for (i = 0; i < 8; i++) {
949                 /* set GPF0,1,2[0:7] for RGB Interface and Data lines (32bit) */
950                 gpio_cfg_pin(&gpio1->f0, i, GPIO_FUNC(2));
951                 gpio_cfg_pin(&gpio1->f1, i, GPIO_FUNC(2));
952                 gpio_cfg_pin(&gpio1->f2, i, GPIO_FUNC(2));
953                 /* pull-up/down disable */
954                 gpio_set_pull(&gpio1->f0, i, GPIO_PULL_NONE);
955                 gpio_set_pull(&gpio1->f1, i, GPIO_PULL_NONE);
956                 gpio_set_pull(&gpio1->f2, i, GPIO_PULL_NONE);
957
958                 /* drive strength to max (24bit) */
959                 gpio_set_drv(&gpio1->f0, i, GPIO_DRV_4X);
960                 gpio_set_rate(&gpio1->f0, i, GPIO_DRV_SLOW);
961                 gpio_set_drv(&gpio1->f1, i, GPIO_DRV_4X);
962                 gpio_set_rate(&gpio1->f1, i, GPIO_DRV_SLOW);
963                 gpio_set_drv(&gpio1->f2, i, GPIO_DRV_4X);
964                 gpio_set_rate(&gpio1->f2, i, GPIO_DRV_SLOW);
965         }
966
967         for (i = 0; i < 4; i++) {
968                 /* set GPF3[0:3] for RGB Interface and Data lines (32bit) */
969                 gpio_cfg_pin(&gpio1->f3, i, GPIO_PULL_UP);
970                 /* pull-up/down disable */
971                 gpio_set_pull(&gpio1->f3, i, GPIO_PULL_NONE);
972                 /* drive strength to max (24bit) */
973                 gpio_set_drv(&gpio1->f3, i, GPIO_DRV_4X);
974                 gpio_set_rate(&gpio1->f3, i, GPIO_DRV_SLOW);
975         }
976
977         /* gpio pad configuration for LCD reset. */
978         gpio_direction_output(&gpio2->y4, 5, 1);
979
980         /*
981          * gpio pad configuration for
982          * DISPLAY_CS, DISPLAY_CLK, DISPLAY_SO, DISPLAY_SI.
983          */
984         gpio_cfg_pin(&gpio2->y4, 3, GPIO_OUTPUT);
985         gpio_cfg_pin(&gpio2->y3, 1, GPIO_OUTPUT);
986         gpio_cfg_pin(&gpio2->y3, 3, GPIO_OUTPUT);
987
988         spi_pd.cs_bank = &gpio2->y4;
989         spi_pd.cs_num = 3;
990         spi_pd.clk_bank = &gpio2->y3;
991         spi_pd.clk_num = 1;
992         spi_pd.si_bank = &gpio2->y3;
993         spi_pd.si_num = 3;
994
995         spi_pd.mode = SPI_MODE_3;
996
997         spi_pd.cs_active = ACTIVE_LOW;
998         spi_pd.word_len = 8;
999
1000         ld9040_set_platform_data(&spi_pd);
1001
1002         return;
1003 }
1004
1005 extern void ld9040_cfg_ldo(void);
1006 extern void ld9040_enable_ldo(unsigned int onoff);
1007
1008 int s5p_no_lcd_support(void)
1009 {
1010         return 0;
1011 }
1012
1013 void init_panel_info(vidinfo_t *vid)
1014 {
1015         vid->vl_freq    = 60;
1016         vid->vl_col     = 480;
1017         vid->vl_row     = 800;
1018         vid->vl_width   = 480;
1019         vid->vl_height  = 800;
1020         vid->vl_clkp    = CONFIG_SYS_HIGH;
1021         vid->vl_hsp     = CONFIG_SYS_HIGH;
1022         vid->vl_vsp     = CONFIG_SYS_HIGH;
1023         vid->vl_dp      = CONFIG_SYS_HIGH;
1024
1025         vid->vl_bpix    = 32;
1026         /* disable dual lcd mode. */
1027         vid->dual_lcd_enabled = 0;
1028
1029         /* LD9040 LCD Panel */
1030         vid->vl_hspw    = 2;
1031         vid->vl_hbpd    = 16;
1032         vid->vl_hfpd    = 16;
1033
1034         vid->vl_vspw    = 2;
1035         vid->vl_vbpd    = 6;
1036         vid->vl_vfpd    = 4;
1037
1038         vid->cfg_gpio = lcd_cfg_gpio;
1039         vid->backlight_on = NULL;
1040         vid->lcd_power_on = NULL;       /* Don't need the poweron squence */
1041         vid->reset_lcd = NULL;          /* Don't need the reset squence */
1042
1043         vid->cfg_ldo = ld9040_cfg_ldo;
1044         vid->enable_ldo = ld9040_enable_ldo;
1045
1046         vid->init_delay = 0;
1047         vid->power_on_delay = 0;
1048         vid->reset_delay = 0;
1049         vid->interface_mode = FIMD_RGB_INTERFACE;
1050
1051         /* board should be detected at here. */
1052
1053         /* for LD8040. */
1054         vid->pclk_name = MPLL;
1055         vid->sclk_div = 1;
1056
1057         setenv("lcdinfo", "lcd=ld9040");
1058 }
1059 #endif
1060
1061 static unsigned int get_hw_revision(void)
1062 {
1063         int hwrev, mode0, mode1;
1064
1065         mode0 = get_adc_value(1);               /* HWREV_MODE0 */
1066         mode1 = get_adc_value(2);               /* HWREV_MODE1 */
1067
1068         /*
1069          * XXX Always set the default hwrev as the latest board
1070          * ADC = (voltage) / 3.3 * 4096
1071          */
1072         hwrev = 3;
1073
1074 #define IS_RANGE(x, min, max)   ((x) > (min) && (x) < (max))
1075         if (IS_RANGE(mode0, 80, 200) && IS_RANGE(mode1, 80, 200))
1076                 hwrev = 0x0;            /* 0.01V        0.01V */
1077         if (IS_RANGE(mode0, 750, 1000) && IS_RANGE(mode1, 80, 200))
1078                 hwrev = 0x1;            /* 610mV        0.01V */
1079         if (IS_RANGE(mode0, 1300, 1700) && IS_RANGE(mode1, 80, 200))
1080                 hwrev = 0x2;            /* 1.16V        0.01V */
1081         if (IS_RANGE(mode0, 2000, 2400) && IS_RANGE(mode1, 80, 200))
1082                 hwrev = 0x3;            /* 1.79V        0.01V */
1083 #undef IS_RANGE
1084
1085         debug("mode0: %d, mode1: %d, hwrev 0x%x\n", mode0, mode1, hwrev);
1086
1087         return hwrev;
1088 }
1089
1090 static const char * const pcb_rev[] = {
1091         "UNIV_0.0",
1092         "UNIV_0.1",
1093         "AQUILA_1.7",
1094         "AQUILA_1.9",
1095 };
1096
1097 static void check_hw_revision(void)
1098 {
1099         int hwrev;
1100
1101         hwrev = get_hw_revision();
1102
1103         board_rev |= hwrev;
1104 }
1105
1106 static void show_hw_revision(void)
1107 {
1108         printf("HW Revision:\t0x%x\n", board_rev);
1109         printf("PCB Revision:\t%s\n", pcb_rev[board_rev & 0xf]);
1110 }
1111
1112 void get_rev_info(char *rev_info)
1113 {
1114         sprintf(rev_info, "HW Revision: 0x%x (%s)\n",
1115                         board_rev, pcb_rev[board_rev & 0xf]);
1116 }
1117
1118 static void check_reset_status(void)
1119 {
1120         int status = get_reset_status();
1121
1122         puts("Reset Status: ");
1123
1124         switch (status) {
1125         case EXTRESET:
1126                 puts("Pin(Ext) Reset\n");
1127                 break;
1128         case WARMRESET:
1129                 puts("Warm Reset\n");
1130                 break;
1131         case WDTRESET:
1132                 puts("Watchdog Reset\n");
1133                 break;
1134         case SWRESET:
1135                 puts("S/W Reset\n");
1136                 break;
1137         default:
1138                 printf("Unknown (0x%x)\n", status);
1139         }
1140 }
1141
1142 #ifdef CONFIG_CMD_RAMOOPS
1143 static void show_dump_msg(void)
1144 {
1145         int ret;
1146
1147         ret = ramoops_init(samsung_get_base_modem());
1148
1149         if (!ret)
1150                 setenv("bootdelay", "-1");
1151 }
1152 #endif
1153
1154 #ifdef CONFIG_MISC_INIT_R
1155 int misc_init_r(void)
1156 {
1157         check_reset_status();
1158 #ifdef CONFIG_CMD_RAMOOPS
1159         show_dump_msg();
1160 #endif
1161
1162         show_hw_revision();
1163
1164         pmic_bus_init(I2C_5);
1165
1166         check_keypad();
1167
1168         check_auto_burn();
1169
1170         /* check max17040 */
1171         check_battery(0);
1172
1173         /* check fsa9480 */
1174         check_micro_usb(0);
1175
1176 #ifdef CONFIG_INFO_ACTION
1177         info_action_check();
1178 #endif
1179
1180 #ifdef CONFIG_CMD_PMIC
1181         run_command("pmic ldo 4 off", 0);       /* adc off */
1182 #endif
1183
1184         return 0;
1185 }
1186 #endif
1187
1188 #ifdef CONFIG_CMD_USBDOWN
1189 int usb_board_init(void)
1190 {
1191         /* interrupt clear */
1192         pmic_get_irq(PWRON1S);
1193
1194 #ifdef CONFIG_CMD_PMIC
1195         run_command("pmic ldo 8 on", 0);
1196         run_command("pmic ldo 3 on", 0);
1197         run_command("pmic safeout 1 on", 0);
1198 #endif
1199         return 0;
1200 }
1201 #endif
1202
1203 #ifdef CONFIG_GENERIC_MMC
1204 int s5p_no_mmc_support(void)
1205 {
1206         return 0;
1207 }
1208
1209 int board_mmc_init(bd_t *bis)
1210 {
1211         int i, err;
1212
1213         switch (get_hwrev()) {
1214         case 0:
1215                 /*
1216                  * Set the low to enable LDO_EN
1217                  * But when you use the test board for eMMC booting
1218                  * you should set it HIGH since it removes the inverter
1219                  */
1220                 /* MASSMEMORY_EN: XMDMDATA_6: GPE3[6] */
1221                 gpio_direction_output(&gpio1->e3, 6, 0);
1222                 break;
1223         default:
1224                 /*
1225                  * Default reset state is High and there's no inverter
1226                  * But set it as HIGH to ensure
1227                  */
1228                 /* MASSMEMORY_EN: XMDMADDR_3: GPE1[3] */
1229                 gpio_direction_output(&gpio1->e1, 3, 1);
1230                 break;
1231         }
1232
1233 #ifdef CONFIG_MMC_BOOT_EVT0
1234         /*
1235          * In MMC boot test board don't have inverter
1236          */
1237         gpio_direction_output(&gpio1->e3, 6, 1);
1238 #endif
1239
1240         /*
1241          * eMMC GPIO:
1242          * SDR 8-bit@48MHz at MMC0
1243          * GPK0[0]      SD_0_CLK(2)
1244          * GPK0[1]      SD_0_CMD(2)
1245          * GPK0[2]      SD_0_CDn        -> Not used
1246          * GPK0[3:6]    SD_0_DATA[0:3](2)
1247          * GPK1[3:6]    SD_0_DATA[0:3](3)
1248          *
1249          * DDR 4-bit@26MHz at MMC4
1250          * GPK0[0]      SD_4_CLK(3)
1251          * GPK0[1]      SD_4_CMD(3)
1252          * GPK0[2]      SD_4_CDn        -> Not used
1253          * GPK0[3:6]    SD_4_DATA[0:3](3)
1254          * GPK1[3:6]    SD_4_DATA[4:7](4)
1255          */
1256         for (i = 0; i < 7; i++) {
1257                 if (i == 2)
1258                         continue;
1259                 /* GPK0[0:6] special function 2 */
1260                 gpio_cfg_pin(&gpio2->k0, i, 0x2);
1261                 /* GPK0[0:6] pull disable */
1262                 gpio_set_pull(&gpio2->k0, i, GPIO_PULL_NONE);
1263                 /* GPK0[0:6] drv 4x */
1264                 gpio_set_drv(&gpio2->k0, i, GPIO_DRV_4X);
1265         }
1266
1267         for (i = 3; i < 7; i++) {
1268                 /* GPK1[3:6] special function 3 */
1269                 gpio_cfg_pin(&gpio2->k1, i, 0x3);
1270                 /* GPK1[3:6] pull disable */
1271                 gpio_set_pull(&gpio2->k1, i, GPIO_PULL_NONE);
1272                 /* GPK1[3:6] drv 4x */
1273                 gpio_set_drv(&gpio2->k1, i, GPIO_DRV_4X);
1274         }
1275
1276         /* T-flash detect */
1277         gpio_cfg_pin(&gpio2->x3, 4, 0xf);
1278         gpio_set_pull(&gpio2->x3, 4, GPIO_PULL_UP);
1279
1280         /*
1281          * MMC device init
1282          * mmc0  : eMMC (8-bit buswidth)
1283          * mmc2  : SD card (4-bit buswidth)
1284          */
1285         err = s5p_mmc_init(0, 8);
1286
1287         /*
1288          * Check the T-flash  detect pin
1289          * GPX3[4] T-flash detect pin
1290          */
1291         if (!gpio_get_value(&gpio2->x3, 4)) {
1292                 /*
1293                  * SD card GPIO:
1294                  * GPK2[0]      SD_2_CLK(2)
1295                  * GPK2[1]      SD_2_CMD(2)
1296                  * GPK2[2]      SD_2_CDn        -> Not used
1297                  * GPK2[3:6]    SD_2_DATA[0:3](2)
1298                  */
1299                 for (i = 0; i < 7; i++) {
1300                         if (i == 2)
1301                                 continue;
1302                         /* GPK2[0:6] special function 2 */
1303                         gpio_cfg_pin(&gpio2->k2, i, 0x2);
1304                         /* GPK2[0:6] pull disable */
1305                         gpio_set_pull(&gpio2->k2, i, GPIO_PULL_NONE);
1306                         /* GPK2[0:6] drv 4x */
1307                         gpio_set_drv(&gpio2->k2, i, GPIO_DRV_4X);
1308                 }
1309                 err = s5p_mmc_init(2, 4);
1310         }
1311
1312         return err;
1313
1314 }
1315 #endif
1316
1317 #ifdef CONFIG_CMD_DEVICE_POWER
1318 static int do_microusb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1319 {
1320         switch (argc) {
1321         case 2:
1322                 if (strncmp(argv[1], "cp", 2) == 0) {
1323                         micro_usb_switch(1);
1324                         run_command("pmic safeout 2 on", 0);
1325                         setenv("usb", "cp");
1326                 } else if (strncmp(argv[1], "ap", 2) == 0) {
1327                         micro_usb_switch(0);
1328                         run_command("pmic safeout 2 off", 0);
1329                         setenv("usb", "ap");
1330                 }
1331                 break;
1332         default:
1333                 cmd_usage(cmdtp);
1334                 return 1;
1335         }
1336
1337         saveenv();
1338
1339         printf("USB Path is set to %s\n", getenv("usb"));
1340
1341         return 0;
1342 }
1343
1344 U_BOOT_CMD(
1345         microusb,               CONFIG_SYS_MAXARGS,     1, do_microusb,
1346         "Micro USB Switch",
1347         "cp - switch to CP\n"
1348         "microusb ap - switch to AP\n"
1349 );
1350 #endif