blob: b44014d88a5bd606f5af1584c9e23e796bf2d4ca [file] [log] [blame]
Ajay Dudania66de4f2009-11-22 08:57:39 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
Shashank Mittal8e49dec2010-03-01 15:19:04 -08004 * Copyright (c) 2009-2010, Code Aurora Forum. All rights reserved.
Ajay Dudania66de4f2009-11-22 08:57:39 -08005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google, Inc. nor the names of its contributors
16 * may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <debug.h>
34#include <dev/keys.h>
35#include <dev/gpio_keypad.h>
36#include <lib/ptable.h>
37#include <dev/flash.h>
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080038#include <smem.h>
Ajay Dudania66de4f2009-11-22 08:57:39 -080039
Chandan Uddaraju885e4db2009-12-03 22:45:26 -080040#define LINUX_MACHTYPE 1008002
41
Chandan Uddarajucc400c22010-01-22 17:19:09 -080042#define VARIABLE_LENGTH 0x10101010
43#define DIFF_START_ADDR 0xF0F0F0F0
44#define NUM_PAGES_PER_BLOCK 0x40
45
Ajay Dudania66de4f2009-11-22 08:57:39 -080046static struct ptable flash_ptable;
47
48/* for these partitions, start will be offset by either what we get from
49 * smem, or from the above offset if smem is not useful. Also, we should
50 * probably have smem_ptable code populate our flash_ptable.
51 *
52 * When smem provides us with a full partition table, we can get rid of
53 * this altogether.
54 *
55 */
56static struct ptentry board_part_list[] = {
57 {
58 .start = 0,
Chandan Uddarajucc400c22010-01-22 17:19:09 -080059 .length = 40 /* 5MB */,
Ajay Dudania66de4f2009-11-22 08:57:39 -080060 .name = "boot",
61 },
62 {
Chandan Uddarajucc400c22010-01-22 17:19:09 -080063 .start = 40,
Ajay Dudanid43419e2010-02-10 13:17:39 -080064 .length = 760 /* 95MB */,
Ajay Dudania66de4f2009-11-22 08:57:39 -080065 .name = "system",
66 },
67 {
Ajay Dudanid43419e2010-02-10 13:17:39 -080068 .start = 800,
Chandan Uddarajucc400c22010-01-22 17:19:09 -080069 .length = 40 /* 5MB */,
Ajay Dudania66de4f2009-11-22 08:57:39 -080070 .name = "cache",
71 },
72 {
Ajay Dudanid43419e2010-02-10 13:17:39 -080073 .start = 840,
Chandan Uddarajucc400c22010-01-22 17:19:09 -080074 .length = VARIABLE_LENGTH,
75 .name = "userdata",
Chandan Uddarajuaeb979e2009-12-14 18:04:26 -080076 },
77 {
Chandan Uddarajucc400c22010-01-22 17:19:09 -080078 .start = DIFF_START_ADDR,
79 .length = 40 /* 5MB */,
80 .name = "recovery",
Ajay Dudania66de4f2009-11-22 08:57:39 -080081 },
82};
83static int num_parts = sizeof(board_part_list)/sizeof(struct ptentry);
84
85void smem_ptable_init(void);
86unsigned smem_get_apps_flash_start(void);
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -080087void usb_charger_change_state(void);
88void usb_charger_reset(void);
89void usb_stop_charging(unsigned);
Ajay Dudania66de4f2009-11-22 08:57:39 -080090void keypad_init(void);
91
92void target_init(void)
93{
94 unsigned offset;
95 struct flash_info *flash_info;
Chandan Uddarajucc400c22010-01-22 17:19:09 -080096 unsigned total_num_of_blocks;
97 bool start_addr_changed = false;
98 unsigned next_ptr_start_adr = 0;
Ajay Dudania66de4f2009-11-22 08:57:39 -080099 int i;
100
101 dprintf(INFO, "target_init()\n");
102
Chandan Uddarajuaeb979e2009-12-14 18:04:26 -0800103#if (!ENABLE_NANDWRITE)
Ajay Dudania66de4f2009-11-22 08:57:39 -0800104 keys_init();
105 keypad_init();
Chandan Uddarajuaeb979e2009-12-14 18:04:26 -0800106#endif
Ajay Dudania66de4f2009-11-22 08:57:39 -0800107
108 ptable_init(&flash_ptable);
109 smem_ptable_init();
110
111 flash_init();
112 flash_info = flash_get_info();
113 ASSERT(flash_info);
114
115 offset = smem_get_apps_flash_start();
116 if (offset == 0xffffffff)
117 while(1);
118
Chandan Uddarajucc400c22010-01-22 17:19:09 -0800119 total_num_of_blocks = (flash_info->block_size)/NUM_PAGES_PER_BLOCK;
120
Ajay Dudania66de4f2009-11-22 08:57:39 -0800121 for (i = 0; i < num_parts; i++) {
122 struct ptentry *ptn = &board_part_list[i];
123 unsigned len = ptn->length;
124
Chandan Uddarajucc400c22010-01-22 17:19:09 -0800125 if(len == VARIABLE_LENGTH)
126 {
127 start_addr_changed = true;
128 unsigned length_for_prt = 0;
129 unsigned j;
130 for (j = i+1; j < num_parts; j++)
131 {
132 struct ptentry *temp_ptn = &board_part_list[j];
133 ASSERT(temp_ptn->length != VARIABLE_LENGTH);
134 length_for_prt += temp_ptn->length;
135 }
136 len = (total_num_of_blocks - 1) - (offset + ptn->start + length_for_prt);
137 ASSERT(len >= 0);
138 next_ptr_start_adr = ptn->start + len;
139 }
140 if((ptn->start == DIFF_START_ADDR) && (start_addr_changed))
141 {
142 ASSERT(next_ptr_start_adr);
143 ptn->start = next_ptr_start_adr;
144 next_ptr_start_adr = ptn->start + ptn->length;
145 }
Ajay Dudania66de4f2009-11-22 08:57:39 -0800146 ptable_add(&flash_ptable, ptn->name, offset + ptn->start,
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800147 len, ptn->flags, TYPE_APPS_PARTITION, PERM_WRITEABLE);
Ajay Dudania66de4f2009-11-22 08:57:39 -0800148 }
149
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800150 smem_add_modem_partitions(&flash_ptable);
151
Ajay Dudania66de4f2009-11-22 08:57:39 -0800152 ptable_dump(&flash_ptable);
153 flash_set_ptable(&flash_ptable);
154}
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800155
156unsigned board_machtype(void)
157{
158 return LINUX_MACHTYPE;
159}
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800160
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800161void reboot_device(unsigned reboot_reason)
162{
163 reboot(reboot_reason);
164}
165
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800166unsigned check_reboot_mode(void)
167{
168 unsigned mode[2] = {0, 0};
169 unsigned int mode_len = sizeof(mode);
170 unsigned smem_status;
171
172 smem_status = smem_read_alloc_entry(SMEM_APPS_BOOT_MODE,
173 &mode, mode_len );
174 if(smem_status)
175 {
176 dprintf(CRITICAL, "ERROR: unable to read shared memory for reboot mode\n");
177 return 0;
178 }
179 return mode[0];
180}
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800181
182void target_battery_charging_enable(unsigned enable, unsigned disconnect)
183{
184 if(disconnect){
185 usb_charger_reset();
186 return;
187 }
188 else
189 usb_stop_charging(!enable);
190
191 for(;;)
192 {
193 thread_sleep(10);
194 usb_charger_change_state();
195 }
196}