recovery: code clean up
[kernel/u-boot.git] / recovery / board / samsung / universal / universal.c
1 /*
2  * (C) Copyright 2010 Samsung Electronics
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307 USA
21  */
22
23 #include <common.h>
24 #include <linux/mtd/compat.h>
25 #include <linux/mtd/mtd.h>
26 #include <linux/mtd/onenand.h>
27 #include <asm/io.h>
28 #include <asm/arch/gpio.h>
29 #include <asm/arch/keypad.h>
30 #include "../../../onenand.h"
31
32 typedef int (init_fnc_t) (void);
33
34 DECLARE_GLOBAL_DATA_PTR;
35
36 static struct s5pc110_gpio *s5pc110_gpio;
37
38 static void sdelay(unsigned long usec)
39 {
40         ulong kv;
41
42         do {
43                 kv = 0x100000;
44                 while (kv)
45                         kv--;
46                 usec--;
47         } while (usec);
48 }
49
50 static int check_keypad(void)
51 {
52         unsigned int condition = 0;
53         unsigned int reg, value;
54         unsigned int col_num, row_num;
55         unsigned int col_mask;
56         unsigned int col_mask_shift;
57         unsigned int row_state[4] = {0, 0, 0, 0};
58         unsigned int i;
59
60         /* board is limo universal */
61
62         row_num = 2;
63         col_num = 3;
64
65         for (i = 0; i < sizeof(row_state)/sizeof(int); i++)
66                 row_state[i] = 0;
67
68         for (i = 0; i < row_num; i++) {
69                 /* Set GPH3[3:0] to KP_ROW[3:0] */
70                 gpio_cfg_pin(&s5pc110_gpio->gpio_h3, i, 0x3);
71                 gpio_set_pull(&s5pc110_gpio->gpio_h3, i, GPIO_PULL_UP);
72         }
73
74         for (i = 0; i < col_num; i++)
75                 /* Set GPH2[3:0] to KP_COL[3:0] */
76                 gpio_cfg_pin(&s5pc110_gpio->gpio_h2, i, 0x3);
77
78         reg = S5PC110_KEYPAD_BASE;
79         col_mask = S5PC110_KEYIFCOLEN_MASK;
80         col_mask_shift = 8;
81
82         /* KEYIFCOL reg clear */
83         writel(0, reg + S5PC1XX_KEYIFCOL_OFFSET);
84
85         /* key_scan */
86         for (i = 0; i < col_num; i++) {
87                 value = col_mask;
88                 value &= ~(1 << i) << col_mask_shift;
89
90                 writel(value, reg + S5PC1XX_KEYIFCOL_OFFSET);
91                 sdelay(1000);
92
93                 value = readl(reg + S5PC1XX_KEYIFROW_OFFSET);
94                 row_state[i] = ~value & ((1 << row_num) - 1);
95         }
96
97         /* KEYIFCOL reg clear */
98         writel(0, reg + S5PC1XX_KEYIFCOL_OFFSET);
99
100         /* check volume up/down */
101         if ((row_state[1] & 0x3) == 0x3)
102                 condition = 1;
103
104         return condition;
105 }
106
107 static int check_block(void)
108 {
109         struct onenand_op *onenand_ops = onenand_get_interface();
110         struct mtd_info *mtd = onenand_ops->mtd;
111         struct onenand_chip *this = mtd->priv;
112         int i;
113         int page_to_check = 4;
114         int ret, retlen = 0;
115         ulong blocksize = 1 << this->erase_shift;
116         ulong pagesize = 1 << this->page_shift;
117         u_char *down_ram_addr;
118         ulong uboot_addr;
119         u_char verify_buf[0x10];
120
121         down_ram_addr = (unsigned char *)CONFIG_SYS_DOWN_ADDR;
122         uboot_addr = CONFIG_RECOVERY_UBOOT_BLOCK << this->erase_shift;
123
124         onenand_ops->read(uboot_addr, blocksize, &retlen, down_ram_addr, 0);
125         if (retlen != blocksize)
126                 return 1;
127
128         memset(verify_buf, 0xFF, sizeof(verify_buf));
129
130         for (i = 0; i < page_to_check; i++) {
131                 ret = memcmp(down_ram_addr + pagesize*i, verify_buf,
132                                 sizeof(verify_buf));
133                 if (ret)
134                         break;
135         }
136
137         if (i == page_to_check)
138                 return 1;
139
140         return 0;
141 }
142
143 int board_check_condition(void)
144 {
145         if (check_keypad())
146                 return 1;
147
148         if (check_block())
149                 return 1;
150
151         return 0;
152 }
153
154 int board_load_uboot(unsigned char *buf)
155 {
156         struct mtd_info *mtd = &onenand_mtd;
157         struct onenand_chip *this = mtd->priv;
158         int offset;
159         size_t size;
160         size_t ret;
161
162         offset = CONFIG_RECOVERY_UBOOT_BLOCK << this->erase_shift;
163         size = CONFIG_SYS_MONITOR_LEN;
164
165         mtd->read(mtd, offset, size, &ret, buf);
166
167         if (size != ret)
168                 return -1;
169
170         return 0;
171 }
172
173 void board_recovery_init(void)
174 {
175         /* Set Initial global variables */
176         s5pc110_gpio = (struct s5pc110_gpio *) S5PC110_GPIO_BASE;
177 }