blob: 0b3b0cdc45428653668ae1ceec833006f2a504b9 [file] [log] [blame]
Chandan Uddaraju8adde5a2009-11-17 11:31:28 -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.
Chandan Uddaraju8adde5a2009-11-17 11:31:28 -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>
Chandan Uddaraju8adde5a2009-11-17 11:31:28 -080039
Chandan Uddaraju885e4db2009-12-03 22:45:26 -080040#define LINUX_MACHTYPE 1008000
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
Chandan Uddaraju8adde5a2009-11-17 11:31:28 -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 */,
Chandan Uddaraju8adde5a2009-11-17 11:31:28 -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 */,
Chandan Uddaraju8adde5a2009-11-17 11:31:28 -080065 .name = "system",
66 },
67 {
Ajay Dudanid43419e2010-02-10 13:17:39 -080068 .start = 800,
Chandan Uddarajuaeb979e2009-12-14 18:04:26 -080069 .length = 40 /* 5MB */,
Chandan Uddaraju8adde5a2009-11-17 11:31:28 -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",
Chandan Uddaraju8adde5a2009-11-17 11:31:28 -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);
Chandan Uddaraju8adde5a2009-11-17 11:31:28 -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;
Chandan Uddaraju8adde5a2009-11-17 11:31:28 -080099 int i;
100
101 dprintf(INFO, "target_init()\n");
102
Chandan Uddarajuaeb979e2009-12-14 18:04:26 -0800103#if (!ENABLE_NANDWRITE)
Chandan Uddaraju8adde5a2009-11-17 11:31:28 -0800104 keys_init();
105 keypad_init();
Chandan Uddarajuaeb979e2009-12-14 18:04:26 -0800106#endif
Chandan Uddaraju8adde5a2009-11-17 11:31:28 -0800107 ptable_init(&flash_ptable);
108 smem_ptable_init();
109
110 flash_init();
111 flash_info = flash_get_info();
112 ASSERT(flash_info);
113
114 offset = smem_get_apps_flash_start();
115 if (offset == 0xffffffff)
116 while(1);
117
Chandan Uddarajucc400c22010-01-22 17:19:09 -0800118 total_num_of_blocks = (flash_info->block_size)/NUM_PAGES_PER_BLOCK;
119
Chandan Uddaraju8adde5a2009-11-17 11:31:28 -0800120 for (i = 0; i < num_parts; i++) {
121 struct ptentry *ptn = &board_part_list[i];
122 unsigned len = ptn->length;
123
Chandan Uddarajucc400c22010-01-22 17:19:09 -0800124 if(len == VARIABLE_LENGTH)
125 {
126 start_addr_changed = true;
127 unsigned length_for_prt = 0;
128 unsigned j;
129 for (j = i+1; j < num_parts; j++)
130 {
131 struct ptentry *temp_ptn = &board_part_list[j];
132 ASSERT(temp_ptn->length != VARIABLE_LENGTH);
133 length_for_prt += temp_ptn->length;
134 }
135 len = (total_num_of_blocks - 1) - (offset + ptn->start + length_for_prt);
136 ASSERT(len >= 0);
137 next_ptr_start_adr = ptn->start + len;
138 }
139 if((ptn->start == DIFF_START_ADDR) && (start_addr_changed))
140 {
141 ASSERT(next_ptr_start_adr);
142 ptn->start = next_ptr_start_adr;
143 next_ptr_start_adr = ptn->start + ptn->length;
144 }
Chandan Uddaraju8adde5a2009-11-17 11:31:28 -0800145 ptable_add(&flash_ptable, ptn->name, offset + ptn->start,
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800146 len, ptn->flags, TYPE_APPS_PARTITION, PERM_WRITEABLE);
Chandan Uddaraju8adde5a2009-11-17 11:31:28 -0800147 }
148
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800149 smem_add_modem_partitions(&flash_ptable);
150
Chandan Uddaraju8adde5a2009-11-17 11:31:28 -0800151 ptable_dump(&flash_ptable);
152 flash_set_ptable(&flash_ptable);
153}
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800154
155unsigned board_machtype(void)
156{
157 return LINUX_MACHTYPE;
158}
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800159
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800160void reboot_device(unsigned reboot_reason)
161{
162 reboot(reboot_reason);
163}
164
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800165unsigned check_reboot_mode(void)
166{
167 unsigned mode[2] = {0, 0};
168 unsigned int mode_len = sizeof(mode);
169 unsigned smem_status;
170
171 smem_status = smem_read_alloc_entry(SMEM_APPS_BOOT_MODE,
172 &mode, mode_len );
173 if(smem_status)
174 {
175 dprintf(CRITICAL, "ERROR: unable to read shared memory for reboot mode\n");
176 return 0;
177 }
178 return mode[0];
179}
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800180
181void target_battery_charging_enable(unsigned enable, unsigned disconnect)
182{
183 if(disconnect){
184 usb_charger_reset();
185 return;
186 }
187 else
188 usb_stop_charging(!enable);
189
190 for(;;)
191 {
192 thread_sleep(10);
193 usb_charger_change_state();
194 }
195}