blob: 51172fdceb2d15373e8629f575cf0bf0b414e37b [file] [log] [blame]
Mayank Grover42664672017-03-06 17:57:26 +05301/* Copyright (c) 2015,2017, The Linux Foundation. All rights reserved.
vijay kumarca1672a2015-04-09 16:45:40 +05302 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <debug.h>
30#include <platform/iomap.h>
31#include <reg.h>
32#include <target.h>
33#include <platform.h>
34#include <uart_dm.h>
35#include <platform/gpio.h>
36#include <lib/ptable.h>
37#include <qpic_nand.h>
38#include <dev/keys.h>
39#include <spmi_v2.h>
40#include <pm8x41.h>
41#include <board.h>
42#include <baseband.h>
43#include <hsusb.h>
44#include <scm.h>
45#include <platform/gpio.h>
46#include <platform/gpio.h>
47#include <platform/irqs.h>
48#include <platform/clock.h>
49#include <crypto5_wrapper.h>
50#include <partition_parser.h>
51#include <stdlib.h>
Mayank Grover6372b6b2017-03-03 17:55:24 +053052#include <regulator.h>
vijay kumarca1672a2015-04-09 16:45:40 +053053
54#if LONG_PRESS_POWER_ON
55#include <shutdown_detect.h>
56#endif
57
58#define FASTBOOT_MODE 0x77665500
59#define PON_SOFT_RB_SPARE 0x88F
60
61extern void smem_ptable_init(void);
62extern void smem_add_modem_partitions(struct ptable *flash_ptable);
63
64static struct ptable flash_ptable;
65
66/* PMIC config data */
67#define PMIC_ARB_CHANNEL_NUM 0
68#define PMIC_ARB_OWNER_ID 0
Mayank Grovereb3fb552017-06-07 14:18:12 +053069#define PMIC_MAJOR_V1 1
vijay kumarca1672a2015-04-09 16:45:40 +053070
71/* NANDc BAM pipe numbers */
72#define DATA_CONSUMER_PIPE 0
73#define DATA_PRODUCER_PIPE 1
74#define CMD_PIPE 2
75
76/* NANDc BAM pipe groups */
77#define DATA_PRODUCER_PIPE_GRP 0
78#define DATA_CONSUMER_PIPE_GRP 0
79#define CMD_PIPE_GRP 1
80
81/* NANDc EE */
82#define QPIC_NAND_EE 0
83
84/* NANDc max desc length. */
85#define QPIC_NAND_MAX_DESC_LEN 0x7FFF
86
87#define LAST_NAND_PTN_LEN_PATTERN 0xFFFFFFFF
vijay kumarfc07e142015-08-31 16:46:41 +053088#define UBI_CMDLINE " rootfstype=ubifs rootflags=bulk_read"
vijay kumarca1672a2015-04-09 16:45:40 +053089
vijay kumar70dbe3e2015-11-20 13:07:30 +053090#define CE1_INSTANCE 1
91#define CE_EE 1
92#define CE_FIFO_SIZE 64
93#define CE_READ_PIPE 3
94#define CE_WRITE_PIPE 2
95#define CE_READ_PIPE_LOCK_GRP 0
96#define CE_WRITE_PIPE_LOCK_GRP 0
97#define CE_ARRAY_SIZE 20
98#define SUB_TYPE_SKUT 0x0A
99
Monika Singh2e0e39a2018-04-12 12:01:55 +0530100__WEAK unsigned int qseecom_get_version()
101{
102 return 0;
103}
104
vijay kumarca1672a2015-04-09 16:45:40 +0530105struct qpic_nand_init_config config;
106
107void update_ptable_names(void)
108{
109 uint32_t ptn_index;
110 struct ptentry *ptentry_ptr = flash_ptable.parts;
111 struct ptentry *boot_ptn;
112 unsigned i;
113 uint32_t len;
114
115 /* Change all names to lower case. */
116 for (ptn_index = 0; ptn_index != (uint32_t)flash_ptable.count; ptn_index++)
117 {
118 len = strlen(ptentry_ptr[ptn_index].name);
119 for (i = 0; i < len; i++)
120 {
121 if (isupper(ptentry_ptr[ptn_index].name[i]))
122 {
123 ptentry_ptr[ptn_index].name[i] = tolower(ptentry_ptr[ptn_index].name[i]);
124 }
125 }
126 /* SBL fills in the last partition length as 0xFFFFFFFF.
127 * Update the length field based on the number of blocks on the flash.*/
128 if ((uint32_t)(ptentry_ptr[ptn_index].length) == LAST_NAND_PTN_LEN_PATTERN)
129 {
130 ptentry_ptr[ptn_index].length = flash_num_blocks() - ptentry_ptr[ptn_index].start;
131 }
132 }
133}
134
vijay kumar84e606d2015-10-15 18:36:36 +0530135/* Return Non zero (i.e 0x2) if vol_down pressed */
136uint32_t target_volume_down()
137{
138 /* Volume down button tied in with PMIC RESIN. */
139 return pm8x41_resin_status();
140}
141
142static void target_keystatus()
143{
144 keys_init();
145
146 if(target_volume_down())
147 keys_post_event(KEY_VOLUMEDOWN, 1);
148}
149
vijay kumarca1672a2015-04-09 16:45:40 +0530150void target_early_init(void)
151{
152#if WITH_DEBUG_UART
vijay kumar20627da2015-10-01 13:28:58 +0530153 /*BLSP1 and UART5*/
154 uart_dm_init(5, 0, BLSP1_UART5_BASE);
vijay kumarca1672a2015-04-09 16:45:40 +0530155#endif
156}
157
158/* Configure PMIC and Drop PS_HOLD for shutdown */
159void shutdown_device()
160{
161 dprintf(CRITICAL, "Going down for shutdown.\n");
162
163 /* Configure PMIC for shutdown */
164 pm8x41_reset_configure(PON_PSHOLD_SHUTDOWN);
165
166 /* Drop PS_HOLD for MSM */
167 writel(0x00, MPM2_MPM_PS_HOLD);
168
169 mdelay(5000);
170
171 dprintf(CRITICAL, "shutdown failed\n");
172
173 ASSERT(0);
174}
175
176
177void target_init(void)
178{
179 uint32_t base_addr;
180 uint8_t slot;
181
182 dprintf(INFO, "target_init()\n");
183
184 spmi_init(PMIC_ARB_CHANNEL_NUM, PMIC_ARB_OWNER_ID);
185
Mayank Grover6372b6b2017-03-03 17:55:24 +0530186#if SMD_SUPPORT
187 rpm_smd_init();
188#endif
vijay kumar84e606d2015-10-15 18:36:36 +0530189 target_keystatus();
vijay kumarca1672a2015-04-09 16:45:40 +0530190 config.pipes.read_pipe = DATA_PRODUCER_PIPE;
191 config.pipes.write_pipe = DATA_CONSUMER_PIPE;
192 config.pipes.cmd_pipe = CMD_PIPE;
193
194 config.pipes.read_pipe_grp = DATA_PRODUCER_PIPE_GRP;
195 config.pipes.write_pipe_grp = DATA_CONSUMER_PIPE_GRP;
196 config.pipes.cmd_pipe_grp = CMD_PIPE_GRP;
197
198 config.bam_base = MSM_NAND_BAM_BASE;
199 config.nand_base = MSM_NAND_BASE;
200 config.ee = QPIC_NAND_EE;
201 config.max_desc_len = QPIC_NAND_MAX_DESC_LEN;
202
203 qpic_nand_init(&config);
204
205 ptable_init(&flash_ptable);
vijay kumarec490942015-07-23 12:43:54 +0530206 smem_ptable_init();
207 smem_add_modem_partitions(&flash_ptable);
vijay kumarca1672a2015-04-09 16:45:40 +0530208
209 update_ptable_names();
210 flash_set_ptable(&flash_ptable);
vijay kumar70dbe3e2015-11-20 13:07:30 +0530211
212 if (target_use_signed_kernel())
213 target_crypto_init_params();
vijay kumarca1672a2015-04-09 16:45:40 +0530214}
215
216/* Identify the current target */
217void target_detect(struct board_data *board)
218{
219 /* This property is filled as part of board.c */
220}
221
222unsigned board_machtype(void)
223{
224}
225
226/* Identify the baseband being used */
227void target_baseband_detect(struct board_data *board)
228{
229 uint32_t platform = board->platform;
230
231 switch(platform)
232 {
vijay kumardb062a32015-10-30 12:32:15 +0530233 case MDM9607:
234 case MDM8207:
235 case MDM9207:
Mayank Grover42664672017-03-06 17:57:26 +0530236 case MDM9206:
vijay kumardb062a32015-10-30 12:32:15 +0530237 case MDM9307:
238 case MDM9628:
vijay kumarca1672a2015-04-09 16:45:40 +0530239 board->baseband = BASEBAND_MDM;
240 break;
241 default:
242 dprintf(CRITICAL, "Platform type: %u is not supported\n", platform);
243 ASSERT(0);
244 };
245}
246
vijay kumar00c1e792015-09-28 14:48:48 +0530247void target_serialno(unsigned char *buf)
248{
249 uint32_t serialno;
250 serialno = board_chip_serial();
vijay kumara1260412015-11-12 17:39:07 +0530251 snprintf((char *)buf, 13, "%x", serialno);
vijay kumar00c1e792015-09-28 14:48:48 +0530252}
253
vijay kumarca1672a2015-04-09 16:45:40 +0530254unsigned check_reboot_mode(void)
255{
256 uint32_t restart_reason = 0;
257
258 /* Read reboot reason and scrub it */
259 restart_reason = readl(RESTART_REASON_ADDR);
260 writel(0x00, RESTART_REASON_ADDR);
261
262 return restart_reason;
263}
264
vijay kumarfc07e142015-08-31 16:46:41 +0530265int get_target_boot_params(const char *cmdline, const char *part, char **buf)
vijay kumarca1672a2015-04-09 16:45:40 +0530266{
267 struct ptable *ptable;
268 int system_ptn_index = -1;
vijay kumarfc07e142015-08-31 16:46:41 +0530269 uint32_t buflen = strlen(UBI_CMDLINE) + strlen(" root=ubi0:rootfs ubi.mtd=") + sizeof(int) + 1; /* 1 byte for null character*/
270
Mayank Groveraea8b102017-01-04 18:20:49 +0530271 if (!cmdline || !part ) {
272 dprintf(CRITICAL, "WARN: Invalid input param\n");
273 return -1;
274 }
275
vijay kumarfc07e142015-08-31 16:46:41 +0530276 *buf = (char *)malloc(buflen);
277 if(!(*buf)) {
278 dprintf(CRITICAL,"Unable to allocate memory for boot params\n");
279 return -1;
280 }
vijay kumarca1672a2015-04-09 16:45:40 +0530281
282 ptable = flash_get_ptable();
283 if (!ptable) {
284 dprintf(CRITICAL,"WARN: Cannot get flash partition table\n");
vijay kumarfc07e142015-08-31 16:46:41 +0530285 free(*buf);
vijay kumarca1672a2015-04-09 16:45:40 +0530286 return -1;
287 }
288
289 system_ptn_index = ptable_get_index(ptable, part);
290 if (system_ptn_index < 0) {
291 dprintf(CRITICAL,"WARN: Cannot get partition index for %s\n", part);
vijay kumarfc07e142015-08-31 16:46:41 +0530292 free(*buf);
vijay kumarca1672a2015-04-09 16:45:40 +0530293 return -1;
294 }
vijay kumarfc07e142015-08-31 16:46:41 +0530295 /* Adding command line parameters according to target boot type */
296 snprintf(*buf, buflen, UBI_CMDLINE);
297
vijay kumarca1672a2015-04-09 16:45:40 +0530298 /*check if cmdline contains "root=" at the beginning of buffer or
299 * " root=" in the middle of buffer.
300 */
301 if (((!strncmp(cmdline, "root=", strlen("root="))) ||
302 (strstr(cmdline, " root="))))
303 dprintf(DEBUG, "DEBUG: cmdline has root=\n");
304 else
vijay kumarfc07e142015-08-31 16:46:41 +0530305 snprintf(*buf+strlen(*buf), buflen, " root=ubi0:rootfs ubi.mtd=%d", system_ptn_index);
306 /*in success case buf will be freed in the calling function of this*/
vijay kumarca1672a2015-04-09 16:45:40 +0530307 return 0;
308}
309
310void target_usb_init(void)
311{
312 uint32_t val;
313
314 /* Select and enable external configuration with USB PHY */
vijay kumarec490942015-07-23 12:43:54 +0530315 ulpi_write(ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT, ULPI_MISC_A_SET);
vijay kumarca1672a2015-04-09 16:45:40 +0530316
317 /* Enable sess_vld */
318 val = readl(USB_GENCONFIG_2) | GEN2_SESS_VLD_CTRL_EN;
319 writel(val, USB_GENCONFIG_2);
320
321 /* Enable external vbus configuration in the LINK */
322 val = readl(USB_USBCMD);
323 val |= SESS_VLD_CTRL;
324 writel(val, USB_USBCMD);
325}
326
327void target_uninit(void)
328{
vijay kumar70dbe3e2015-11-20 13:07:30 +0530329 if (crypto_initialized())
330 crypto_eng_cleanup();
Mayank Grover7881d022017-04-13 16:50:13 +0530331
332 if(platform_is_mdm9206())
333 regulator_disable(REG_LDO4);
334
Mayank Grover6372b6b2017-03-03 17:55:24 +0530335#if SMD_SUPPORT
336 rpm_smd_uninit();
337#endif
vijay kumarca1672a2015-04-09 16:45:40 +0530338}
339
340void reboot_device(unsigned reboot_reason)
341{
vijay kumarcefa8512015-10-16 12:54:30 +0530342 uint8_t reset_type = 0;
Mayank Grovereb3fb552017-06-07 14:18:12 +0530343 struct board_pmic_data pmic_info;
344
vijay kumarca1672a2015-04-09 16:45:40 +0530345 /* Write the reboot reason */
346 writel(reboot_reason, RESTART_REASON_ADDR);
347
348 /* Configure PMIC for warm reset */
349 /* PM 8019 v1 aligns with PM8941 v2.
350 * This call should be based on the pmic version
351 * when PM8019 v2 is available.
352 */
vijay kumarcefa8512015-10-16 12:54:30 +0530353 if(reboot_reason)
354 reset_type = PON_PSHOLD_WARM_RESET;
355 else
356 reset_type = PON_PSHOLD_HARD_RESET;
357
Mayank Grovereb3fb552017-06-07 14:18:12 +0530358 if (board_pmic_info(&pmic_info, SMEM_V7_SMEM_MAX_PMIC_DEVICES))
359 {
360 /* make decision based on pmic major version */
361 switch (pmic_info.pmic_version >>16)
362 {
363 case PMIC_MAJOR_V1:
364 pm8x41_v2_reset_configure(reset_type);
365 break;
366 default:
367 pm8x41_reset_configure(reset_type);
368 }
369 }
vijay kumarca1672a2015-04-09 16:45:40 +0530370
371 /* Drop PS_HOLD for MSM */
372 writel(0x00, MPM2_MPM_PS_HOLD);
373
374 mdelay(5000);
375
376 dprintf(CRITICAL, "Rebooting failed\n");
377 return;
378}
vijay kumar70dbe3e2015-11-20 13:07:30 +0530379
380crypto_engine_type board_ce_type(void)
381{
382 return CRYPTO_ENGINE_TYPE_HW;
383}
384
385/* Set up params for h/w CE. */
386void target_crypto_init_params()
387{
388 struct crypto_init_params ce_params;
389
390 /* Set up base addresses and instance. */
391 ce_params.crypto_instance = CE1_INSTANCE;
392 ce_params.crypto_base = MSM_CE1_BASE;
393 ce_params.bam_base = MSM_CE1_BAM_BASE;
394
395 /* Set up BAM config. */
396 ce_params.bam_ee = CE_EE;
397 ce_params.pipes.read_pipe = CE_READ_PIPE;
398 ce_params.pipes.write_pipe = CE_WRITE_PIPE;
399 ce_params.pipes.read_pipe_grp = CE_READ_PIPE_LOCK_GRP;
400 ce_params.pipes.write_pipe_grp = CE_WRITE_PIPE_LOCK_GRP;
401
402 /* Assign buffer sizes. */
403 ce_params.num_ce = CE_ARRAY_SIZE;
404 ce_params.read_fifo_size = CE_FIFO_SIZE;
405 ce_params.write_fifo_size = CE_FIFO_SIZE;
406
407 /* BAM is initialized by TZ for this platform.
408 * Do not do it again as the initialization address space
409 * is locked.
410 */
411 ce_params.do_bam_init = 0;
412 crypto_init_params(&ce_params);
413}
Mayank Grover6372b6b2017-03-03 17:55:24 +0530414
415void target_fastboot_init()
416{
417 /* Enable LDO4 for processing fastboot commands.*/
418 if(platform_is_mdm9206()){
419 regulator_enable(REG_LDO4);
420 dprintf(SPEW, "Enable LDO4\n");
421 }
422}