blob: 35b31ba80556ad850891bbbf9a294449c3b88b05 [file] [log] [blame]
Dima Zavin9caac252009-01-26 12:37:15 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Channagoud Kadabibd50bea2015-03-19 15:17:04 -07005 * Copyright (c) 2009-2012,2015 The Linux Foundation. All rights reserved.
Shashank Mittal8e49dec2010-03-01 15:19:04 -08006 *
Dima Zavin9caac252009-01-26 12:37:15 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
Amol Jadi6133e8d2012-10-07 22:15:02 -070014 * the documentation and/or other materials provided with the
Dima Zavin9caac252009-01-26 12:37:15 -080015 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Amol Jadi6133e8d2012-10-07 22:15:02 -070024 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
Dima Zavin9caac252009-01-26 12:37:15 -080025 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
27 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <debug.h>
32#include <reg.h>
33#include <string.h>
34#include <sys/types.h>
35#include <platform/iomap.h>
Shashank Mittal8e49dec2010-03-01 15:19:04 -080036#include <lib/ptable.h>
Dima Zavin9caac252009-01-26 12:37:15 -080037
Aparna Mallavarapu1f62fdd2015-03-08 12:50:57 +053038#include "board.h"
Dima Zavin9caac252009-01-26 12:37:15 -080039#include "smem.h"
40
Dima Zavin9caac252009-01-26 12:37:15 -080041
42/* partition table from SMEM */
43static struct smem_ptable smem_ptable;
Amol Jadi6133e8d2012-10-07 22:15:02 -070044static unsigned smem_apps_flash_start = 0xFFFFFFFF;
Dima Zavin9caac252009-01-26 12:37:15 -080045
Sundarajan Srinivasanbd8a5712013-07-09 17:09:09 -070046static ram_partition_table ptable;
47
Dima Zavin9caac252009-01-26 12:37:15 -080048static void dump_smem_ptable(void)
49{
Amol Jadi6133e8d2012-10-07 22:15:02 -070050 unsigned i;
Dima Zavin9caac252009-01-26 12:37:15 -080051
Amol Jadi6133e8d2012-10-07 22:15:02 -070052 for (i = 0; i < smem_ptable.len; i++) {
Dima Zavin9caac252009-01-26 12:37:15 -080053 struct smem_ptn *p = &smem_ptable.parts[i];
54 if (p->name[0] == '\0')
55 continue;
56 dprintf(SPEW, "%d: %s offs=0x%08x size=0x%08x attr: 0x%08x\n",
57 i, p->name, p->start, p->size, p->attr);
58 }
59}
60
61void smem_ptable_init(void)
62{
63 unsigned i;
Amol Jadi6133e8d2012-10-07 22:15:02 -070064 unsigned ret;
vijay kumar4f4405f2014-08-08 11:49:53 +053065 unsigned len = 0;
Dima Zavin9caac252009-01-26 12:37:15 -080066
Amol Jadi6133e8d2012-10-07 22:15:02 -070067 /* Read only the header portion of ptable */
68 ret = smem_read_alloc_entry_offset(SMEM_AARM_PARTITION_TABLE,
69 &smem_ptable,
70 SMEM_PTABLE_HDR_LEN,
71 0);
72 if (ret)
73 {
74 dprintf(CRITICAL, "Failed to read ptable hdr (%d)", ret);
75 ASSERT(0);
76 }
Dima Zavin9caac252009-01-26 12:37:15 -080077
Amol Jadi6133e8d2012-10-07 22:15:02 -070078 /* Verify ptable magic */
Dima Zavin9caac252009-01-26 12:37:15 -080079 if (smem_ptable.magic[0] != _SMEM_PTABLE_MAGIC_1 ||
80 smem_ptable.magic[1] != _SMEM_PTABLE_MAGIC_2)
81 return;
82
Amol Jadi6133e8d2012-10-07 22:15:02 -070083 /* Ensure that # of partitions is less than the max we have allocated. */
84 ASSERT(smem_ptable.len <= SMEM_PTABLE_MAX_PARTS);
85
86 /* Find out length of partition data based on table version. */
87 if (smem_ptable.version <= 3)
88 {
89 len = SMEM_PTABLE_HDR_LEN + SMEM_PTABLE_MAX_PARTS_V3*sizeof(struct smem_ptn);
90 }
91 else if (smem_ptable.version == 4)
92 {
93 len = SMEM_PTABLE_HDR_LEN + SMEM_PTABLE_MAX_PARTS_V4*sizeof(struct smem_ptn);
94 }
95 else
96 {
97 dprintf(CRITICAL, "Unknown ptable version (%d)", smem_ptable.version);
98 ASSERT(0);
99 }
100
101 ret = smem_read_alloc_entry(SMEM_AARM_PARTITION_TABLE,
102 &smem_ptable,
103 len);
104 if (ret)
105 {
106 dprintf(CRITICAL, "Failed to read ptable (%d)", ret);
107 ASSERT(0);
108 }
109
Dima Zavin9caac252009-01-26 12:37:15 -0800110 dump_smem_ptable();
111 dprintf(INFO, "smem ptable found: ver: %d len: %d\n",
Amol Jadi6133e8d2012-10-07 22:15:02 -0700112 smem_ptable.version, smem_ptable.len);
Dima Zavin9caac252009-01-26 12:37:15 -0800113
114 for (i = 0; i < smem_ptable.len; i++) {
115 if (!strcmp(smem_ptable.parts[i].name, "0:APPS"))
Ajay Dudanib01e5062011-12-03 23:23:42 -0800116 break;
Dima Zavin9caac252009-01-26 12:37:15 -0800117 }
118 if (i == smem_ptable.len)
119 return;
120
121 smem_apps_flash_start = smem_ptable.parts[i].start;
122}
123
124unsigned smem_get_apps_flash_start(void)
125{
126 return smem_apps_flash_start;
127}
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800128
129void smem_add_modem_partitions(struct ptable *flash_ptable)
130{
Amol Jadi6133e8d2012-10-07 22:15:02 -0700131 unsigned i;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800132
133 if (smem_ptable.magic[0] != _SMEM_PTABLE_MAGIC_1 ||
134 smem_ptable.magic[1] != _SMEM_PTABLE_MAGIC_2)
135 return;
136
Amol Jadi6133e8d2012-10-07 22:15:02 -0700137 for (i = 0; i < smem_ptable.len; i++) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800138 char *token;
139 char *pname = NULL;
Sridhar Parasuram131ad2e2015-06-15 16:22:54 -0700140 char *sp;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800141 struct smem_ptn *p = &smem_ptable.parts[i];
Ajay Dudanib01e5062011-12-03 23:23:42 -0800142 if (p->name[0] == '\0')
143 continue;
Sridhar Parasuram131ad2e2015-06-15 16:22:54 -0700144 token = strtok_r(p->name, ":", &sp);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800145 while (token) {
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800146 pname = token;
Sridhar Parasuram131ad2e2015-06-15 16:22:54 -0700147 token = strtok_r(NULL, ":", &sp);
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800148 }
Ajay Dudanib01e5062011-12-03 23:23:42 -0800149 if (pname) {
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800150 ptable_add(flash_ptable, pname, p->start,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800151 p->size, 0, TYPE_MODEM_PARTITION,
152 PERM_WRITEABLE);
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800153 }
154 }
155}
156
Sundarajan Srinivasanbd8a5712013-07-09 17:09:09 -0700157static void smem_copy_ram_ptable(void *buf)
158{
Channagoud Kadabibd50bea2015-03-19 15:17:04 -0700159 struct smem_ram_ptable *table_v0 = NULL;
160 struct smem_ram_ptable_v1 *table_v1 = NULL;
161 struct smem_ram_ptable_v2 *table_v2 = NULL;
Sundarajan Srinivasanbd8a5712013-07-09 17:09:09 -0700162 uint32_t pentry = 0;
163
164 ptable.hdr = *(struct smem_ram_ptable_hdr*)buf;
165
166 /* Perform member to member copy from smem_ram_ptable to wrapper struct ram_ptable */
Channagoud Kadabibd50bea2015-03-19 15:17:04 -0700167 if(ptable.hdr.version == SMEM_RAM_PTABLE_VERSION_2)
168 {
169 table_v2 = (struct smem_ram_ptable_v2*)buf;
170
171 memcpy(&ptable, table_v2, sizeof(ram_partition_table));
172 }
Channagoud Kadabi626a1d92015-03-25 15:20:31 -0700173 else if(ptable.hdr.version == SMEM_RAM_PTABLE_VERSION_1)
Sundarajan Srinivasanbd8a5712013-07-09 17:09:09 -0700174 {
175 table_v1 = (struct smem_ram_ptable_v1*)buf;
176
Channagoud Kadabibd50bea2015-03-19 15:17:04 -0700177 for(pentry = 0; pentry < ((struct smem_ram_ptable_hdr*)buf)->len; pentry++)
178 {
179 ptable.parts[pentry].start = table_v1->parts[pentry].start;
180 ptable.parts[pentry].size = table_v1->parts[pentry].size;
181 ptable.parts[pentry].attr = table_v1->parts[pentry].attr;
182 ptable.parts[pentry].category = table_v1->parts[pentry].category;
183 ptable.parts[pentry].domain = table_v1->parts[pentry].domain;
184 ptable.parts[pentry].type = table_v1->parts[pentry].type;
185 ptable.parts[pentry].num_partitions = table_v1->parts[pentry].num_partitions;
186 }
Sundarajan Srinivasanbd8a5712013-07-09 17:09:09 -0700187 }
188 else if(ptable.hdr.version == SMEM_RAM_PTABLE_VERSION_0)
189 {
190 table_v0 = (struct smem_ram_ptable*)buf;
191
192 for(pentry = 0; pentry < ((struct smem_ram_ptable_hdr*)buf)->len; pentry++)
193 {
194 ptable.parts[pentry].start = table_v0->parts[pentry].start;
195 ptable.parts[pentry].size = table_v0->parts[pentry].size;
196 ptable.parts[pentry].attr = table_v0->parts[pentry].attr;
197 ptable.parts[pentry].category = table_v0->parts[pentry].category;
198 ptable.parts[pentry].domain = table_v0->parts[pentry].domain;
199 ptable.parts[pentry].type = table_v0->parts[pentry].type;
200 ptable.parts[pentry].num_partitions = table_v0->parts[pentry].num_partitions;
201 }
202
203 }
204 else
205 {
206 dprintf(CRITICAL,"ERROR: Unknown smem ram ptable version: %u", ptable.hdr.version);
207 ASSERT(0);
208 }
209}
210
Ajay Dudania1eafc42010-04-21 19:48:11 -0700211/* RAM Partition table from SMEM */
212int smem_ram_ptable_init(struct smem_ram_ptable *smem_ram_ptable)
213{
214 unsigned i;
215
216 i = smem_read_alloc_entry(SMEM_USABLE_RAM_PARTITION_TABLE,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800217 smem_ram_ptable,
218 sizeof(struct smem_ram_ptable));
Ajay Dudania1eafc42010-04-21 19:48:11 -0700219 if (i != 0)
220 return 0;
221
222 if (smem_ram_ptable->magic[0] != _SMEM_RAM_PTABLE_MAGIC_1 ||
223 smem_ram_ptable->magic[1] != _SMEM_RAM_PTABLE_MAGIC_2)
224 return 0;
225
Ajay Dudaniebb0b5b2011-08-02 14:35:55 -0700226 dprintf(SPEW, "smem ram ptable found: ver: %d len: %d\n",
Ajay Dudania1eafc42010-04-21 19:48:11 -0700227 smem_ram_ptable->version, smem_ram_ptable->len);
228
Sundarajan Srinivasanbd8a5712013-07-09 17:09:09 -0700229 smem_copy_ram_ptable((void*)smem_ram_ptable);
230
Ajay Dudania1eafc42010-04-21 19:48:11 -0700231 return 1;
232}
Sundarajan Srinivasanbd8a5712013-07-09 17:09:09 -0700233
234/* RAM Partition table from SMEM */
Channagoud Kadabibd50bea2015-03-19 15:17:04 -0700235static uint32_t buffer[sizeof(struct smem_ram_ptable_v2)];
Sundarajan Srinivasanbd8a5712013-07-09 17:09:09 -0700236int smem_ram_ptable_init_v1()
237{
238 uint32_t i;
239 uint32_t ret;
240 uint32_t version;
vijay kumar4f4405f2014-08-08 11:49:53 +0530241 uint32_t smem_ram_ptable_size = 0;
Sundarajan Srinivasanbd8a5712013-07-09 17:09:09 -0700242 struct smem_ram_ptable_hdr *ram_ptable_hdr;
243
244 /* Check smem ram partition table version and decide on length of ram_ptable */
245 ret = smem_read_alloc_entry_offset(SMEM_USABLE_RAM_PARTITION_TABLE,
246 &version,
247 sizeof(version),
248 SMEM_RAM_PTABLE_VERSION_OFFSET);
249
250 if(ret)
251 return 0;
252
Channagoud Kadabibd50bea2015-03-19 15:17:04 -0700253 if(version == SMEM_RAM_PTABLE_VERSION_2)
254 smem_ram_ptable_size = sizeof(struct smem_ram_ptable_v2);
Channagoud Kadabi626a1d92015-03-25 15:20:31 -0700255 else if(version == SMEM_RAM_PTABLE_VERSION_1)
Sundarajan Srinivasanbd8a5712013-07-09 17:09:09 -0700256 smem_ram_ptable_size = sizeof(struct smem_ram_ptable_v1);
257 else if(version == SMEM_RAM_PTABLE_VERSION_0)
258 smem_ram_ptable_size = sizeof(struct smem_ram_ptable);
259 else
260 {
261 dprintf(CRITICAL,"ERROR: Wrong smem_ram_ptable version: %u", version);
262 ASSERT(0);
263 }
264
265 i = smem_read_alloc_entry(SMEM_USABLE_RAM_PARTITION_TABLE,
266 (void*)buffer,
267 smem_ram_ptable_size);
268 if (i != 0)
269 return 0;
270
271 ram_ptable_hdr = (struct smem_ram_ptable_hdr *)buffer;
272
273 if (ram_ptable_hdr->magic[0] != _SMEM_RAM_PTABLE_MAGIC_1 ||
274 ram_ptable_hdr->magic[1] != _SMEM_RAM_PTABLE_MAGIC_2)
275 return 0;
276
277 smem_copy_ram_ptable((void*)buffer);
278
279 dprintf(SPEW, "smem ram ptable found: ver: %u len: %u\n",
280 ram_ptable_hdr->version, ram_ptable_hdr->len);
281
282 return 1;
283}
284
285void smem_get_ram_ptable_entry(ram_partition *ptn, uint32_t entry)
286{
287 memcpy(ptn, &(ptable.parts[entry]), sizeof(ram_partition));
288}
289
290uint32_t smem_get_ram_ptable_len(void)
291{
292 return ptable.hdr.len;
293}
294
295uint32_t smem_get_ram_ptable_version(void)
296{
297 return ptable.hdr.version;
298}
Aparna Mallavarapu1f62fdd2015-03-08 12:50:57 +0530299
300uint32_t get_ddr_start()
301{
302 uint32_t i;
303 ram_partition ptn_entry;
304 uint32_t len = 0;
305
306 ASSERT(smem_ram_ptable_init_v1());
307
308 len = smem_get_ram_ptable_len();
309
310 /* Determine the Start addr of the DDR RAM */
311 for(i = 0; i < len; i++)
312 {
313 smem_get_ram_ptable_entry(&ptn_entry, i);
314 if(ptn_entry.type == SYS_MEMORY)
315 {
316 if((ptn_entry.category == SDRAM) ||
317 (ptn_entry.category == IMEM))
318 {
319 /* Check to ensure that start address is 1MB aligned */
320 ASSERT((ptn_entry.start & (MB-1)) == 0);
321 return ptn_entry.start;
322 }
323 }
324 }
325 ASSERT("DDR Start Mem Not found\n");
326 return 0;
327}
Channagoud Kadabib22da982015-07-13 15:03:41 -0700328
329uint64_t smem_get_ddr_size()
330{
331 uint32_t i;
332 ram_partition ptn_entry;
333 uint32_t len = 0;
334 uint64_t size = 0;
335
336 ASSERT(smem_ram_ptable_init_v1());
337
338 len = smem_get_ram_ptable_len();
339
340 /* Determine the Start addr of the DDR RAM */
341 for(i = 0; i < len; i++)
342 {
343 smem_get_ram_ptable_entry(&ptn_entry, i);
344 if(ptn_entry.type == SYS_MEMORY && ptn_entry.category == SDRAM)
345 size += ptn_entry.size;
346 }
347
348 return size;
349}
tracychuid19990d2020-06-12 13:22:29 +0800350
351/*[TracyChui]Show memory information on fastboot screen 20200612 start */
352//BOOT.BF.3.3.2\boot_images\core\api\boot\ddr_common.h
353/** DDR manufacturers. */
354typedef enum
355{
356 RESERVED_0, /**< Reserved for future use. */
357 SAMSUNG, /**< Samsung. */
358 QIMONDA, /**< Qimonda. */
359 ELPIDA, /**< Elpida Memory, Inc. */
360 ETRON, /**< Etron Technology, Inc. */
361 NANYA, /**< Nanya Technology Corporation. */
362 HYNIX, /**< Hynix Semiconductor Inc. */
363 MOSEL, /**< Mosel Vitelic Corporation. */
364 WINBOND, /**< Winbond Electronics Corp. */
365 ESMT, /**< Elite Semiconductor Memory Technology Inc. */
366 RESERVED_1, /**< Reserved for future use. */
367 SPANSION, /**< Spansion Inc. */
368 SST, /**< Silicon Storage Technology, Inc. */
369 ZMOS, /**< ZMOS Technology, Inc. */
370 INTEL, /**< Intel Corporation. */
371 NUMONYX = 254, /**< Numonyx, acquired by Micron Technology, Inc. */
372 MICRON = 255, /**< Micron Technology, Inc. */
373 DDR_MANUFACTURES_MAX = 0x7FFFFFFF /**< Forces the enumerator to 32 bits. */
374} DDR_MANUFACTURES;
375
376//uint32_t smem_get_ddr_manufacturer_id()
377void smem_get_ddr_manufacturer_id(unsigned char *buf)
378{
379 uint32_t manufacturer_id = -1;
380 uint32_t manufacturer_id_len = sizeof(manufacturer_id);
381 unsigned ret;
382
383 ret = smem_read_alloc_entry(SMEM_ID_VENDOR2,
384 &manufacturer_id,
385 manufacturer_id_len);
386 if (ret)
387 {
388 dprintf(CRITICAL, "Failed to read SMEM_ID_VENDOR2 (%d)", ret);
389 }
390
391 switch(manufacturer_id)
392 {
393 case SAMSUNG:
394 snprintf((char *)buf, 64, "SAMSUNG");
395 break;
396 case ELPIDA:
397 snprintf((char *)buf, 64, "ELPIDA");
398 break;
399 case HYNIX:
400 snprintf((char *)buf, 64, "HYNIX");
401 break;
402 case MICRON:
403 snprintf((char *)buf, 64, "MICRON");
404 break;
405 default:
406 snprintf((char *)buf, 64, "OTHERS");
407 break;
408 }
409
410 dprintf(CRITICAL,"manufacturer_id=%d, %s\n", manufacturer_id, buf);
411 /*[TracyChui]Show memory information on fastboot screen 20200612 end */
412}