blob: 89a0ea1681c93543c9852bb58ab5e1f7a3e5e3d2 [file] [log] [blame]
Ajay Dudani513fb742010-05-24 20:39:26 -07001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
Duy Truongf3ac7b32013-02-13 01:07:28 -08004 * Copyright (c) 2009-2011, The Linux Foundation. All rights reserved.
Ajay Dudani513fb742010-05-24 20:39:26 -07005 *
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>
38#include <smem.h>
Greg Griscod6250552011-06-29 14:40:23 -070039#include <platform.h>
Ajay Dudani513fb742010-05-24 20:39:26 -070040
41#define LINUX_MACHTYPE 2627
42
43#define VARIABLE_LENGTH 0x10101010
44#define DIFF_START_ADDR 0xF0F0F0F0
45#define NUM_PAGES_PER_BLOCK 0x40
46
47static struct ptable flash_ptable;
48
49/* for these partitions, start will be offset by either what we get from
50 * smem, or from the above offset if smem is not useful. Also, we should
51 * probably have smem_ptable code populate our flash_ptable.
52 *
53 * When smem provides us with a full partition table, we can get rid of
54 * this altogether.
55 *
56 */
57static struct ptentry board_part_list[] = {
58 {
59 .start = 0,
60 .length = 40 /* 5MB */,
61 .name = "boot",
62 },
63 {
64 .start = 40,
David Ng3755bb82010-10-12 18:12:06 -070065 .length = 800 /* 100MB */,
Ajay Dudani513fb742010-05-24 20:39:26 -070066 .name = "system",
67 },
68 {
David Ng3755bb82010-10-12 18:12:06 -070069 .start = 840,
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070070 .length = 240 /* 30MB */,
Ajay Dudani513fb742010-05-24 20:39:26 -070071 .name = "cache",
72 },
73 {
David Ng3755bb82010-10-12 18:12:06 -070074 .start = 1080,
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070075 .length = 3 /* 384KB */,
76 .name = "misc",
77 },
78 {
David Ng3755bb82010-10-12 18:12:06 -070079 .start = 1083,
Ajay Dudani513fb742010-05-24 20:39:26 -070080 .length = VARIABLE_LENGTH,
81 .name = "userdata",
82 },
83 {
84 .start = DIFF_START_ADDR,
85 .length = 40 /* 5MB */,
86 .name = "recovery",
87 },
88};
89static int num_parts = sizeof(board_part_list)/sizeof(struct ptentry);
90
91void smem_ptable_init(void);
92unsigned smem_get_apps_flash_start(void);
93void usb_charger_change_state(void);
94void usb_charger_reset(void);
95void usb_stop_charging(unsigned);
96void keypad_init(void);
97
98void target_init(void)
99{
100 unsigned offset;
101 struct flash_info *flash_info;
102 unsigned total_num_of_blocks;
103 bool start_addr_changed = false;
104 unsigned next_ptr_start_adr = 0;
105 int i;
106
107 dprintf(INFO, "target_init()\n");
108
109#if (!ENABLE_NANDWRITE)
110 keys_init();
111 keypad_init();
112#endif
113 ptable_init(&flash_ptable);
114 smem_ptable_init();
115
116 flash_init();
117 flash_info = flash_get_info();
118 ASSERT(flash_info);
119
120 offset = smem_get_apps_flash_start();
121 if (offset == 0xffffffff)
122 while(1);
123
Chandan Uddaraju40605112010-08-09 14:25:08 -0700124 total_num_of_blocks = flash_info->num_blocks;
Ajay Dudani513fb742010-05-24 20:39:26 -0700125
126 for (i = 0; i < num_parts; i++) {
127 struct ptentry *ptn = &board_part_list[i];
128 unsigned len = ptn->length;
129
130 if(len == VARIABLE_LENGTH)
131 {
132 start_addr_changed = true;
133 unsigned length_for_prt = 0;
134 unsigned j;
135 for (j = i+1; j < num_parts; j++)
136 {
137 struct ptentry *temp_ptn = &board_part_list[j];
138 ASSERT(temp_ptn->length != VARIABLE_LENGTH);
139 length_for_prt += temp_ptn->length;
140 }
141 len = (total_num_of_blocks - 1) - (offset + ptn->start + length_for_prt);
142 ASSERT(len >= 0);
143 next_ptr_start_adr = ptn->start + len;
144 }
145 if((ptn->start == DIFF_START_ADDR) && (start_addr_changed))
146 {
147 ASSERT(next_ptr_start_adr);
148 ptn->start = next_ptr_start_adr;
149 next_ptr_start_adr = ptn->start + ptn->length;
150 }
151 ptable_add(&flash_ptable, ptn->name, offset + ptn->start,
152 len, ptn->flags, TYPE_APPS_PARTITION, PERM_WRITEABLE);
153 }
154
155 smem_add_modem_partitions(&flash_ptable);
156
157 ptable_dump(&flash_ptable);
158 flash_set_ptable(&flash_ptable);
159}
160
161unsigned board_machtype(void)
162{
163 return LINUX_MACHTYPE;
164}
165
166void reboot_device(unsigned reboot_reason)
167{
168 reboot(reboot_reason);
169}
170
171unsigned check_reboot_mode(void)
172{
173 unsigned mode[2] = {0, 0};
174 unsigned int mode_len = sizeof(mode);
175 unsigned smem_status;
176
177 smem_status = smem_read_alloc_entry(SMEM_APPS_BOOT_MODE,
178 &mode, mode_len );
179 if(smem_status)
180 {
181 dprintf(CRITICAL, "ERROR: unable to read shared memory for reboot mode\n");
182 return 0;
183 }
184 return mode[0];
185}
186
187void target_battery_charging_enable(unsigned enable, unsigned disconnect)
188{
189 if(disconnect){
190 usb_charger_reset();
191 return;
192 }
193 else
194 usb_stop_charging(!enable);
195
196 for(;;)
197 {
198 thread_sleep(10);
199 usb_charger_change_state();
200 }
201}