blob: a4f69f11d287b857c66c68ee5cce4de60abb3016 [file] [log] [blame]
Mayank Grover141c9bc2018-03-13 18:26:43 +05301/* Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
Unnati Gandhi4d07fac2014-07-04 17:38:25 +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 <mmc.h>
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053036#include <dev/keys.h>
37#include <spmi_v2.h>
38#include <pm8x41.h>
39#include <board.h>
40#include <baseband.h>
41#include <hsusb.h>
42#include <scm.h>
43#include <platform/gpio.h>
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053044#include <platform/irqs.h>
45#include <platform/clock.h>
46#include <crypto5_wrapper.h>
47#include <partition_parser.h>
48#include <stdlib.h>
Unnati Gandhi4d637e42014-07-11 14:47:25 +053049#include <gpio.h>
Unnati Gandhic24a86f2014-09-19 16:07:16 +053050#include <rpm-smd.h>
Unnati Gandhic43a2802014-09-19 17:27:25 +053051#include <qpic_nand.h>
lijuang3606df82015-09-02 21:14:43 +080052#include <smem.h>
Mayank Grover7e351832017-04-21 15:03:58 +053053#include <secapp_loader.h>
54#include <rpmb.h>
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053055
Unnati Gandhif4cb6622014-08-28 13:54:56 +053056#if LONG_PRESS_POWER_ON
57#include <shutdown_detect.h>
58#endif
59
60#if PON_VIB_SUPPORT
61#include <vibrator.h>
62#endif
63
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053064#define PMIC_ARB_CHANNEL_NUM 0
65#define PMIC_ARB_OWNER_ID 0
Unnati Gandhif4cb6622014-08-28 13:54:56 +053066#define TLMM_VOL_UP_BTN_GPIO 90
Matthew Qin7bd789f2015-07-15 15:31:20 +080067#define TLMM_VOL_DOWN_BTN_GPIO 91
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053068
Unnati Gandhif4cb6622014-08-28 13:54:56 +053069#if PON_VIB_SUPPORT
70#define VIBRATE_TIME 250
71#endif
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +053072#define HW_SUBTYPE_APQ_NOWGR 0xA
Unnati Gandhif4cb6622014-08-28 13:54:56 +053073
Unnati Gandhif4cb6622014-08-28 13:54:56 +053074#define CE1_INSTANCE 1
75#define CE_EE 1
76#define CE_FIFO_SIZE 64
77#define CE_READ_PIPE 3
78#define CE_WRITE_PIPE 2
79#define CE_READ_PIPE_LOCK_GRP 0
80#define CE_WRITE_PIPE_LOCK_GRP 0
81#define CE_ARRAY_SIZE 20
Matthew Qin7bd789f2015-07-15 15:31:20 +080082#define SUB_TYPE_SKUT 0x0A
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053083
Mayank Grover141c9bc2018-03-13 18:26:43 +053084/* Fastboot switch GPIO for Intrinsic board. */
85#define USB_SW_GPIO_INTRINSIC_SOM 3
86
Unnati Gandhic43a2802014-09-19 17:27:25 +053087extern void smem_ptable_init(void);
88extern void smem_add_modem_partitions(struct ptable *flash_ptable);
89void target_sdc_init();
90
91static struct ptable flash_ptable;
92
93/* NANDc BAM pipe numbers */
94#define DATA_CONSUMER_PIPE 0
95#define DATA_PRODUCER_PIPE 1
96#define CMD_PIPE 2
97
98/* NANDc BAM pipe groups */
99#define DATA_PRODUCER_PIPE_GRP 0
100#define DATA_CONSUMER_PIPE_GRP 0
101#define CMD_PIPE_GRP 1
102
103/* NANDc EE */
104#define QPIC_NAND_EE 0
105
106/* NANDc max desc length. */
107#define QPIC_NAND_MAX_DESC_LEN 0x7FFF
108
109#define LAST_NAND_PTN_LEN_PATTERN 0xFFFFFFFF
Mayank Groverc1190022017-02-23 17:28:46 +0530110#define UBI_CMDLINE " rootfstype=ubifs rootflags=bulk_read"
Unnati Gandhic43a2802014-09-19 17:27:25 +0530111
112struct qpic_nand_init_config config;
113
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530114struct mmc_device *dev;
115
116static uint32_t mmc_pwrctl_base[] =
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530117 { MSM_SDC1_BASE, MSM_SDC2_BASE };
118
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530119static uint32_t mmc_sdhci_base[] =
120 { MSM_SDC1_SDHCI_BASE, MSM_SDC2_SDHCI_BASE };
121
122static uint32_t mmc_sdc_pwrctl_irq[] =
123 { SDCC1_PWRCTL_IRQ, SDCC2_PWRCTL_IRQ };
124
125static void set_sdc_power_ctrl(void);
Aparna Mallavarapu5f80cbf2014-10-13 11:10:22 -0700126static void set_ebi2_config(void);
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530127
Unnati Gandhic43a2802014-09-19 17:27:25 +0530128void update_ptable_names(void)
129{
130 uint32_t ptn_index;
131 struct ptentry *ptentry_ptr = flash_ptable.parts;
132 struct ptentry *boot_ptn;
133 unsigned i;
134 uint32_t len;
135
136 /* Change all names to lower case. */
137 for (ptn_index = 0; ptn_index != (uint32_t)flash_ptable.count; ptn_index++)
138 {
139 len = strlen(ptentry_ptr[ptn_index].name);
140
141 for (i = 0; i < len; i++)
142 {
143 if (isupper(ptentry_ptr[ptn_index].name[i]))
144 {
145 ptentry_ptr[ptn_index].name[i] = tolower(ptentry_ptr[ptn_index].name[i]);
146 }
147 }
148
149 /* SBL fills in the last partition length as 0xFFFFFFFF.
150 * Update the length field based on the number of blocks on the flash.
151 */
152 if ((uint32_t)(ptentry_ptr[ptn_index].length) == LAST_NAND_PTN_LEN_PATTERN)
153 {
154 ptentry_ptr[ptn_index].length = flash_num_blocks() - ptentry_ptr[ptn_index].start;
155 }
156 }
157}
158
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530159void target_early_init(void)
160{
161#if WITH_DEBUG_UART
Unnati Gandhi1d7ca722015-03-12 16:51:09 +0530162 /* Do not intilaise UART in case the h/w
163 * is RCM.
164 */
Baochu Xu7a8327b2017-12-01 10:40:21 +0800165 uint32_t platform_subtype = board_hardware_subtype();
166
167 if( board_hardware_id() == HW_PLATFORM_RCM )
Unnati Gandhi1d7ca722015-03-12 16:51:09 +0530168 return;
Baochu Xu7a8327b2017-12-01 10:40:21 +0800169 else if ( platform_subtype == HW_PLATFORM_SUBTYPE_8909_PM660_V1)
170 uart_dm_init(2, 0, BLSP1_UART1_BASE);
171 else
172 uart_dm_init(1, 0, BLSP1_UART0_BASE);
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530173#endif
Unnati Gandhi1d7ca722015-03-12 16:51:09 +0530174
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530175}
176
Unnati Gandhic43a2802014-09-19 17:27:25 +0530177int target_is_emmc_boot(void)
178{
179 return platform_boot_dev_isemmc();
180}
181
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530182void target_sdc_init()
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530183{
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530184 struct mmc_config_data config;
185
186 /* Set drive strength & pull ctrl values */
187 set_sdc_power_ctrl();
188
189 config.bus_width = DATA_BUS_WIDTH_8BIT;
190 config.max_clk_rate = MMC_CLK_177MHZ;
191
192 /* Try slot 1*/
193 config.slot = 1;
194 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
195 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
196 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
197 config.hs400_support = 0;
198
199 if (!(dev = mmc_init(&config))) {
200 /* Try slot 2 */
201 config.slot = 2;
202 config.max_clk_rate = MMC_CLK_200MHZ;
203 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
204 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
205 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
206
207 if (!(dev = mmc_init(&config))) {
208 dprintf(CRITICAL, "mmc init failed!");
209 ASSERT(0);
210 }
211 }
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530212}
213
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530214void *target_mmc_device()
215{
216 return (void *) dev;
217}
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530218
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530219/* Return 1 if vol_up pressed */
lijuang2d2b8a02015-06-05 21:34:15 +0800220int target_volume_up()
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530221{
lijuang2d2b8a02015-06-05 21:34:15 +0800222 static uint8_t first_time = 0;
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530223 uint8_t status = 0;
224
lijuang2d2b8a02015-06-05 21:34:15 +0800225 if (!first_time) {
226 gpio_tlmm_config(TLMM_VOL_UP_BTN_GPIO, 0, GPIO_INPUT, GPIO_PULL_UP, GPIO_2MA, GPIO_ENABLE);
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530227
lijuang2d2b8a02015-06-05 21:34:15 +0800228 /* Wait for the gpio config to take effect - debounce time */
229 udelay(10000);
230
231 first_time = 1;
232 }
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530233
234 /* Get status of GPIO */
235 status = gpio_status(TLMM_VOL_UP_BTN_GPIO);
236
237 /* Active low signal. */
238 return !status;
239}
240
241/* Return 1 if vol_down pressed */
242uint32_t target_volume_down()
243{
Matthew Qin7bd789f2015-07-15 15:31:20 +0800244 if ((board_hardware_id() == HW_PLATFORM_QRD) &&
245 (board_hardware_subtype() == SUB_TYPE_SKUT)) {
246 uint32_t status = 0;
247
248 gpio_tlmm_config(TLMM_VOL_DOWN_BTN_GPIO, 0, GPIO_INPUT, GPIO_PULL_UP, GPIO_2MA, GPIO_ENABLE);
249
250 /* Wait for the gpio config to take effect - debounce time */
251 thread_sleep(10);
252
253 /* Get status of GPIO */
254 status = gpio_status(TLMM_VOL_DOWN_BTN_GPIO);
255
256 /* Active low signal. */
257 return !status;
258 } else {
259 /* Volume down button tied in with PMIC RESIN. */
260 return pm8x41_resin_status();
261 }
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530262}
263
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530264static void target_keystatus()
265{
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530266 keys_init();
267
268 if(target_volume_down())
269 keys_post_event(KEY_VOLUMEDOWN, 1);
270
271 if(target_volume_up())
272 keys_post_event(KEY_VOLUMEUP, 1);
273
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530274}
275
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530276static void set_sdc_power_ctrl()
277{
278 /* Drive strength configs for sdc pins */
279 struct tlmm_cfgs sdc1_hdrv_cfg[] =
280 {
Aparna Mallavarapu5f80cbf2014-10-13 11:10:22 -0700281 { SDC1_CLK_HDRV_CTL_OFF, TLMM_CUR_VAL_16MA, TLMM_HDRV_MASK, SDC1_HDRV_PULL_CTL },
282 { SDC1_CMD_HDRV_CTL_OFF, TLMM_CUR_VAL_10MA, TLMM_HDRV_MASK, SDC1_HDRV_PULL_CTL },
Unnati Gandhi9a6e6d42015-02-03 17:44:24 +0530283 { SDC1_DATA_HDRV_CTL_OFF, TLMM_CUR_VAL_10MA, TLMM_HDRV_MASK, SDC1_HDRV_PULL_CTL },
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530284 };
285
286 /* Pull configs for sdc pins */
287 struct tlmm_cfgs sdc1_pull_cfg[] =
288 {
Aparna Mallavarapu5f80cbf2014-10-13 11:10:22 -0700289 { SDC1_CLK_PULL_CTL_OFF, TLMM_NO_PULL, TLMM_PULL_MASK, SDC1_HDRV_PULL_CTL },
290 { SDC1_CMD_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, SDC1_HDRV_PULL_CTL },
291 { SDC1_DATA_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, SDC1_HDRV_PULL_CTL },
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530292 };
293
294 /* Set the drive strength & pull control values */
295 tlmm_set_hdrive_ctrl(sdc1_hdrv_cfg, ARRAY_SIZE(sdc1_hdrv_cfg));
296 tlmm_set_pull_ctrl(sdc1_pull_cfg, ARRAY_SIZE(sdc1_pull_cfg));
297}
298
Aparna Mallavarapu5f80cbf2014-10-13 11:10:22 -0700299static void set_ebi2_config()
300{
301 /* Drive strength configs for ebi2 pins */
302 struct tlmm_cfgs ebi2_hdrv_cfg[] =
303 {
304 { EBI2_BUSY_HDRV_CTL_OFF, TLMM_CUR_VAL_16MA, TLMM_HDRV_MASK, TLMM_EBI2_EMMC_GPIO_CFG },
305 { EBI2_WE_HDRV_CTL_OFF, TLMM_CUR_VAL_16MA, TLMM_HDRV_MASK, TLMM_EBI2_EMMC_GPIO_CFG },
306 { EBI2_OE_HDRV_CTL_OFF, TLMM_CUR_VAL_16MA, TLMM_HDRV_MASK, TLMM_EBI2_EMMC_GPIO_CFG },
307 { EBI2_CLE_HDRV_CTL_OFF, TLMM_CUR_VAL_16MA, TLMM_HDRV_MASK, TLMM_EBI2_EMMC_GPIO_CFG },
308 { EBI2_ALE_HDRV_CTL_OFF, TLMM_CUR_VAL_16MA, TLMM_HDRV_MASK, TLMM_EBI2_EMMC_GPIO_CFG },
309 { EBI2_CS_HDRV_CTL_OFF, TLMM_CUR_VAL_10MA, TLMM_HDRV_MASK, TLMM_EBI2_EMMC_GPIO_CFG },
310 { EBI2_DATA_HDRV_CTL_OFF, TLMM_CUR_VAL_6MA, TLMM_HDRV_MASK, SDC1_HDRV_PULL_CTL },
311 };
312
313 /* Pull configs for ebi2 pins */
314 struct tlmm_cfgs ebi2_pull_cfg[] =
315 {
316 { EBI2_BUSY_PULL_CTL_OFF, TLMM_NO_PULL, TLMM_PULL_MASK, TLMM_EBI2_EMMC_GPIO_CFG },
317 { EBI2_WE_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, TLMM_EBI2_EMMC_GPIO_CFG },
318 { EBI2_OE_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, TLMM_EBI2_EMMC_GPIO_CFG },
319 { EBI2_CLE_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, TLMM_EBI2_EMMC_GPIO_CFG },
320 { EBI2_ALE_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, TLMM_EBI2_EMMC_GPIO_CFG },
321 { EBI2_CS_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, TLMM_EBI2_EMMC_GPIO_CFG },
322 { EBI2_DATA_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, SDC1_HDRV_PULL_CTL },
323 };
324
325 /* Set the drive strength & pull control values */
326 tlmm_set_hdrive_ctrl(ebi2_hdrv_cfg, ARRAY_SIZE(ebi2_hdrv_cfg));
327 tlmm_set_pull_ctrl(ebi2_pull_cfg, ARRAY_SIZE(ebi2_pull_cfg));
328
329}
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530330void target_init(void)
331{
332 uint32_t base_addr;
333 uint8_t slot;
Jiten Patel8d56b462018-08-29 16:51:07 +0530334#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover7e351832017-04-21 15:03:58 +0530335 int ret = 0;
336#endif
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530337 dprintf(INFO, "target_init()\n");
338
339 spmi_init(PMIC_ARB_CHANNEL_NUM, PMIC_ARB_OWNER_ID);
340
341 target_keystatus();
342
Unnati Gandhi36ef2252014-11-04 18:45:14 +0530343#if BOOT_CONFIG_SUPPORT
Unnati Gandhic43a2802014-09-19 17:27:25 +0530344 platform_read_boot_config();
Unnati Gandhi8e4711b2014-10-13 05:03:00 +0530345#endif
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530346
Unnati Gandhic43a2802014-09-19 17:27:25 +0530347 if (platform_boot_dev_isemmc()) {
348 target_sdc_init();
349 if (partition_read_table())
350 {
351 dprintf(CRITICAL, "Error reading the partition table info\n");
352 ASSERT(0);
353 }
354
355 } else {
Aparna Mallavarapu5f80cbf2014-10-13 11:10:22 -0700356 set_ebi2_config();
Unnati Gandhic43a2802014-09-19 17:27:25 +0530357 config.pipes.read_pipe = DATA_PRODUCER_PIPE;
358 config.pipes.write_pipe = DATA_CONSUMER_PIPE;
359 config.pipes.cmd_pipe = CMD_PIPE;
360
361 config.pipes.read_pipe_grp = DATA_PRODUCER_PIPE_GRP;
362 config.pipes.write_pipe_grp = DATA_CONSUMER_PIPE_GRP;
363 config.pipes.cmd_pipe_grp = CMD_PIPE_GRP;
364
365 config.bam_base = MSM_NAND_BAM_BASE;
366 config.nand_base = MSM_NAND_BASE;
367 config.ee = QPIC_NAND_EE;
368 config.max_desc_len = QPIC_NAND_MAX_DESC_LEN;
369
370 qpic_nand_init(&config);
371
372 ptable_init(&flash_ptable);
373 smem_ptable_init();
374 smem_add_modem_partitions(&flash_ptable);
375
376 update_ptable_names();
377 flash_set_ptable(&flash_ptable);
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530378 }
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530379
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530380#if LONG_PRESS_POWER_ON
381 shutdown_detect();
382#endif
383
384#if PON_VIB_SUPPORT
385
386 /* turn on vibrator to indicate that phone is booting up to end user */
Mayank Grover7e351832017-04-21 15:03:58 +0530387 vib_timed_turn_on(VIBRATE_TIME);
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530388#endif
389
390 if (target_use_signed_kernel())
391 target_crypto_init_params();
Unnati Gandhic24a86f2014-09-19 16:07:16 +0530392
Jiten Patel8d56b462018-08-29 16:51:07 +0530393#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover509301a2017-10-26 12:10:11 +0530394 if (VB_M <= target_get_vb_version())
Mayank Grover7e351832017-04-21 15:03:58 +0530395 {
396 clock_ce_enable(CE1_INSTANCE);
397
398 /* Initialize Qseecom */
399 ret = qseecom_init();
400
401 if (ret < 0)
402 {
403 dprintf(CRITICAL, "Failed to initialize qseecom, error: %d\n", ret);
404 ASSERT(0);
405 }
406
407 /* Start Qseecom */
408 ret = qseecom_tz_init();
409
410 if (ret < 0)
411 {
412 dprintf(CRITICAL, "Failed to start qseecom, error: %d\n", ret);
413 ASSERT(0);
414 }
415
416 if (rpmb_init() < 0)
417 {
418 dprintf(CRITICAL, "RPMB init failed\n");
419 ASSERT(0);
420 }
421
422 /*
423 * Load the sec app for first time
424 */
425 if (load_sec_app() < 0)
426 {
427 dprintf(CRITICAL, "Failed to load App for verified\n");
428 ASSERT(0);
429 }
430 }
431#endif
432
Unnati Gandhi36ef2252014-11-04 18:45:14 +0530433#if SMD_SUPPORT
Unnati Gandhic24a86f2014-09-19 16:07:16 +0530434 rpm_smd_init();
Aparna Mallavarapu96fe9d92014-10-19 12:48:01 -0700435#endif
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530436}
437
438void target_serialno(unsigned char *buf)
439{
440 uint32_t serialno;
441 if (target_is_emmc_boot()) {
442 serialno = mmc_get_psn();
443 snprintf((char *)buf, 13, "%x", serialno);
444 }
445}
446
447unsigned board_machtype(void)
448{
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530449 return LINUX_MACHTYPE_UNKNOWN;
450}
451
Unnati Gandhia556a4d2014-08-12 10:42:21 +0530452/* Detect the target type */
453void target_detect(struct board_data *board)
454{
455 /*
456 * already fill the board->target on board.c
457 */
458}
459
460void target_baseband_detect(struct board_data *board)
461{
462 uint32_t platform;
463
464 platform = board->platform;
465 switch(platform)
466 {
467 case MSM8909:
468 case MSM8209:
469 case MSM8208:
Unnati Gandhi917c8612015-02-06 16:50:32 +0530470 case MSM8609:
vijay kumarba95efb2015-09-08 15:24:12 +0530471 case MSM8909W:
Unnati Gandhia556a4d2014-08-12 10:42:21 +0530472 board->baseband = BASEBAND_MSM;
473 break;
474
475 case MDM9209:
476 case MDM9309:
477 case MDM9609:
478 board->baseband = BASEBAND_MDM;
479 break;
480
Unnati Gandhi36e40472014-12-16 12:00:04 +0530481 case APQ8009:
vijay kumarba95efb2015-09-08 15:24:12 +0530482 case APQ8009W:
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530483 if ((board->platform_hw == HW_PLATFORM_MTP) &&
Parth Dixit102ac212016-10-21 20:38:14 +0530484 ((board->platform_subtype == HW_SUBTYPE_APQ_NOWGR) ||
485 (board->platform_subtype == HW_PLATFORM_SUBTYPE_SWOC_NOWGR_CIRC)))
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530486 board->baseband = BASEBAND_APQ_NOWGR;
487 else
488 board->baseband = BASEBAND_APQ;
Unnati Gandhi36e40472014-12-16 12:00:04 +0530489 break;
490
Unnati Gandhia556a4d2014-08-12 10:42:21 +0530491 default:
492 dprintf(CRITICAL, "Platform type: %u is not supported\n", platform);
493 ASSERT(0);
494 };
495}
Shivaraj Shettyf9e10c42014-09-17 04:21:15 +0530496uint8_t target_panel_auto_detect_enabled()
497{
498 uint8_t ret = 0;
499
500 switch(board_hardware_id()) {
501 default:
502 ret = 0;
503 break;
504 }
505 return ret;
506}
507
508static uint8_t splash_override;
509/* Returns 1 if target supports continuous splash screen. */
510int target_cont_splash_screen()
511{
512 uint8_t splash_screen = 0;
513 if (!splash_override) {
514 switch (board_hardware_id()) {
Shivaraj Shetty7db7eec2014-11-05 20:48:35 +0530515 case HW_PLATFORM_SURF:
516 case HW_PLATFORM_MTP:
Ray Zhang17a13112014-11-07 14:07:23 +0800517 case HW_PLATFORM_QRD:
Sandeep Panda8ede6502014-12-02 10:56:16 +0530518 case HW_PLATFORM_RCM:
Shivaraj Shetty7db7eec2014-11-05 20:48:35 +0530519 splash_screen = 1;
520 break;
Shivaraj Shettyf9e10c42014-09-17 04:21:15 +0530521 default:
522 splash_screen = 0;
523 break;
524 }
525 dprintf(SPEW, "Target_cont_splash=%d\n", splash_screen);
526 }
527 return splash_screen;
528}
529
530void target_force_cont_splash_disable(uint8_t override)
531{
532 splash_override = override;
533}
Unnati Gandhia556a4d2014-08-12 10:42:21 +0530534
Vijay Kumar Pendoti92688632016-05-27 11:35:32 +0530535/*Update this command line only for LE based builds*/
vijay kumar58d779b2015-08-31 17:25:49 +0530536int get_target_boot_params(const char *cmdline, const char *part, char **buf)
Unnati Gandhic43a2802014-09-19 17:27:25 +0530537{
538 struct ptable *ptable;
539 int system_ptn_index = -1;
Vijay Kumar Pendoti92688632016-05-27 11:35:32 +0530540 int le_based = -1;
P.V. Phani Kumar3de93a02016-08-05 13:05:08 +0530541 uint32_t buflen = 0;
542
543 if (!cmdline || !part ) {
544 dprintf(CRITICAL, "WARN: Invalid input param\n");
545 return -1;
546 }
Unnati Gandhic43a2802014-09-19 17:27:25 +0530547
Vijay Kumar Pendoti92688632016-05-27 11:35:32 +0530548 /*LE partition.xml will have recoveryfs partition*/
549 if (target_is_emmc_boot())
550 le_based = partition_get_index("recoveryfs");
551 else
552 /*Nand targets by default have this*/
553 le_based = 1;
Unnati Gandhic43a2802014-09-19 17:27:25 +0530554
Vijay Kumar Pendoti92688632016-05-27 11:35:32 +0530555 if (le_based != -1)
556 {
557 if (!target_is_emmc_boot())
558 {
559 ptable = flash_get_ptable();
560 if (!ptable)
561 {
562 dprintf(CRITICAL,
563 "WARN: Cannot get flash partition table\n");
564 return -1;
565 }
566 system_ptn_index = ptable_get_index(ptable, part);
Unnati Gandhic43a2802014-09-19 17:27:25 +0530567 }
Vijay Kumar Pendoti92688632016-05-27 11:35:32 +0530568 else
569 system_ptn_index = partition_get_index(part);
Unnati Gandhic43a2802014-09-19 17:27:25 +0530570 if (system_ptn_index < 0) {
571 dprintf(CRITICAL,
572 "WARN: Cannot get partition index for %s\n", part);
573 return -1;
574 }
Unnati Gandhic43a2802014-09-19 17:27:25 +0530575 /*
576 * check if cmdline contains "root=" at the beginning of buffer or
577 * " root=" in the middle of buffer.
578 */
579 if (((!strncmp(cmdline, "root=", strlen("root="))) ||
P.V. Phani Kumard9a08422016-08-30 13:35:29 +0530580 (strstr(cmdline, " root=")))) {
Unnati Gandhic43a2802014-09-19 17:27:25 +0530581 dprintf(DEBUG, "DEBUG: cmdline has root=\n");
P.V. Phani Kumard9a08422016-08-30 13:35:29 +0530582 return -1;
583 }
Unnati Gandhic43a2802014-09-19 17:27:25 +0530584 else
vijay kumar58d779b2015-08-31 17:25:49 +0530585 /*in success case buf will be freed in the calling function of this*/
Vijay Kumar Pendoti92688632016-05-27 11:35:32 +0530586 {
587 if (!target_is_emmc_boot())
P.V. Phani Kumar3de93a02016-08-05 13:05:08 +0530588 {
Mayank Groverc1190022017-02-23 17:28:46 +0530589 buflen = strlen(UBI_CMDLINE) + strlen(" root=ubi0:rootfs ubi.mtd=") + sizeof(int) + 1; /* 1 byte for null character*/
P.V. Phani Kumar3de93a02016-08-05 13:05:08 +0530590
591 /* In success case, this memory is freed in calling function */
592 *buf = (char *)malloc(buflen);
593 if(!(*buf)) {
594 dprintf(CRITICAL,"Unable to allocate memory for boot params \n");
595 return -1;
596 }
597
Mayank Groverc1190022017-02-23 17:28:46 +0530598 /* Adding command line parameters according to target boot type */
599 snprintf(*buf, buflen, UBI_CMDLINE);
600 snprintf(*buf+strlen(*buf), buflen, " root=ubi0:rootfs ubi.mtd=%d", system_ptn_index);
P.V. Phani Kumar3de93a02016-08-05 13:05:08 +0530601 }
Vijay Kumar Pendoti92688632016-05-27 11:35:32 +0530602 else
P.V. Phani Kumar3de93a02016-08-05 13:05:08 +0530603 {
604 /* Extra character is for Null termination */
605 buflen = strlen(" root=/dev/mmcblk0p") + sizeof(int) + 1;
606
607 /* In success case, this memory is freed in calling function */
608 *buf = (char *)malloc(buflen);
609 if(!(*buf)) {
610 dprintf(CRITICAL,"Unable to allocate memory for boot params \n");
611 return -1;
612 }
613
Vijay Kumar Pendoti92688632016-05-27 11:35:32 +0530614 /*For Emmc case increase the ptn_index by 1*/
P.V. Phani Kumard9a08422016-08-30 13:35:29 +0530615 snprintf(*buf, buflen, " root=/dev/mmcblk0p%d",system_ptn_index + 1);
P.V. Phani Kumar3de93a02016-08-05 13:05:08 +0530616 }
Vijay Kumar Pendoti92688632016-05-27 11:35:32 +0530617 }
P.V. Phani Kumar3de93a02016-08-05 13:05:08 +0530618
619 /*Return for LE based Targets.*/
620 return 0;
Unnati Gandhic43a2802014-09-19 17:27:25 +0530621 }
P.V. Phani Kumar3de93a02016-08-05 13:05:08 +0530622 return -1;
Unnati Gandhic43a2802014-09-19 17:27:25 +0530623}
624
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530625unsigned target_baseband()
626{
627 return board_baseband();
628}
629
630int emmc_recovery_init(void)
631{
632 return _emmc_recovery_init();
633}
634
635void target_usb_init(void)
636{
637 uint32_t val;
638
639 /* Select and enable external configuration with USB PHY */
640 ulpi_write(ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT, ULPI_MISC_A_SET);
641
642 /* Enable sess_vld */
643 val = readl(USB_GENCONFIG_2) | GEN2_SESS_VLD_CTRL_EN;
644 writel(val, USB_GENCONFIG_2);
645
646 /* Enable external vbus configuration in the LINK */
647 val = readl(USB_USBCMD);
648 val |= SESS_VLD_CTRL;
649 writel(val, USB_USBCMD);
650}
651
652unsigned target_pause_for_battery_charge(void)
653{
Mayank Grover6f125882017-12-05 10:28:32 +0530654 uint32_t pmic = target_get_pmic();
655 uint8_t pon_reason = 0;
656 uint8_t is_cold_boot = 0;
657
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530658 /* In case of fastboot reboot,adb reboot or if we see the power key
659 * pressed we do not want go into charger mode.
660 * fastboot reboot is warm boot with PON hard reset bit not set
661 * adb reboot is a cold boot with PON hard reset bit set
662 */
Mayank Grover6f125882017-12-05 10:28:32 +0530663 if (pmic == PMIC_IS_PM660)
664 {
665 pon_reason = pm660_get_pon_reason();
666 is_cold_boot = pm660_get_is_cold_boot();
667 }
668 else
669 {
670 pon_reason = pm8x41_get_pon_reason();
671 is_cold_boot = pm8x41_get_is_cold_boot();
672 }
673 dprintf(INFO, "%s : pon_reason is %d cold_boot:%d\n", __func__,
674 pon_reason, is_cold_boot);
675
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530676 if (is_cold_boot &&
677 (!(pon_reason & HARD_RST)) &&
678 (!(pon_reason & KPDPWR_N)) &&
Chunmei Cai6eb22fe2015-08-20 15:39:06 +0800679 ((pon_reason & USB_CHG) || (pon_reason & DC_CHG) || (pon_reason & CBLPWR_N)))
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530680 return 1;
681 else
682 return 0;
683}
684
685void target_usb_stop(void)
686{
687 /* Disable VBUS mimicing in the controller. */
688 ulpi_write(ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT, ULPI_MISC_A_CLEAR);
689}
690
691
692void target_uninit(void)
693{
694#if PON_VIB_SUPPORT
695 /* wait for the vibrator timer is expried */
696 wait_vib_timeout();
697#endif
698
Unnati Gandhic43a2802014-09-19 17:27:25 +0530699 if (platform_boot_dev_isemmc())
700 {
701 mmc_put_card_to_sleep(dev);
702 sdhci_mode_disable(&dev->host);
703 }
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530704
705 if (crypto_initialized())
706 crypto_eng_cleanup();
707
708 if (target_is_ssd_enabled())
709 clock_ce_disable(CE1_INSTANCE);
Unnati Gandhic24a86f2014-09-19 16:07:16 +0530710
Jiten Patel8d56b462018-08-29 16:51:07 +0530711#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover509301a2017-10-26 12:10:11 +0530712 if(VB_M <= target_get_vb_version())
Mayank Grover7e351832017-04-21 15:03:58 +0530713 {
714 if (is_sec_app_loaded())
715 {
716 if (send_milestone_call_to_tz() < 0)
717 {
718 dprintf(CRITICAL, "Failed to unload App for rpmb\n");
719 ASSERT(0);
720 }
721 }
722
723 if (rpmb_uninit() < 0)
724 {
725 dprintf(CRITICAL, "RPMB uninit failed\n");
726 ASSERT(0);
727 }
728
729 clock_ce_disable(CE1_INSTANCE);
730 }
731#endif
732
Unnati Gandhi36ef2252014-11-04 18:45:14 +0530733#if SMD_SUPPORT
Unnati Gandhic24a86f2014-09-19 16:07:16 +0530734 rpm_smd_uninit();
Aparna Mallavarapu96fe9d92014-10-19 12:48:01 -0700735#endif
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530736}
737
738/* Do any target specific intialization needed before entering fastboot mode */
739void target_fastboot_init(void)
740{
Mayank Grover141c9bc2018-03-13 18:26:43 +0530741 uint32_t hw_id = board_hardware_id();
742 uint32_t platform_subtype = board_hardware_subtype();
743
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530744 /* Set the BOOT_DONE flag in PM8916 */
745 pm8x41_set_boot_done();
746
747 if (target_is_ssd_enabled()) {
748 clock_ce_enable(CE1_INSTANCE);
749 target_load_ssd_keystore();
750 }
Mayank Grover141c9bc2018-03-13 18:26:43 +0530751
752 if ((HW_PLATFORM_MTP == hw_id) &&
753 (HW_PLATFORM_SUBTYPE_INTRINSIC_SOM == platform_subtype))
754 {
755 dprintf(SPEW, "Enabling PMIC GPIO for USB detection\n");
756
757 struct pm8x41_gpio usbgpio_param = {
758 .direction = PM_GPIO_DIR_OUT,
759 .vin_sel = 0,
760 .out_strength = PM_GPIO_OUT_DRIVE_MED,
761 .function = PM_GPIO_FUNC_HIGH,
762 .pull = PM_GPIO_PULLDOWN_10,
763 .inv_int_pol = PM_GPIO_INVERT,
764 };
765
766 pm8x41_gpio_config(USB_SW_GPIO_INTRINSIC_SOM, &usbgpio_param);
767 pm8x41_gpio_set(USB_SW_GPIO_INTRINSIC_SOM, 0);
768 }
769
770 return;
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530771}
772
lijuang395b5e62015-11-19 17:39:44 +0800773int set_download_mode(enum reboot_reason mode)
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530774{
775 int ret = 0;
776 ret = scm_dload_mode(mode);
777
778 pm8x41_clear_pmic_watchdog();
779
780 return ret;
781}
782
783void target_load_ssd_keystore(void)
784{
785 uint64_t ptn;
786 int index;
787 uint64_t size;
788 uint32_t *buffer = NULL;
789
790 if (!target_is_ssd_enabled())
791 return;
792
793 index = partition_get_index("ssd");
794
795 ptn = partition_get_offset(index);
796 if (ptn == 0){
797 dprintf(CRITICAL, "Error: ssd partition not found\n");
798 return;
799 }
800
801 size = partition_get_size(index);
802 if (size == 0) {
803 dprintf(CRITICAL, "Error: invalid ssd partition size\n");
804 return;
805 }
806
807 buffer = memalign(CACHE_LINE, ROUNDUP(size, CACHE_LINE));
808 if (!buffer) {
809 dprintf(CRITICAL, "Error: allocating memory for ssd buffer\n");
810 return;
811 }
812 if (mmc_read(ptn, buffer, size)) {
813 dprintf(CRITICAL, "Error: cannot read data\n");
814 free(buffer);
815 return;
816 }
817
818 clock_ce_enable(CE1_INSTANCE);
819 scm_protect_keystore(buffer, size);
820 clock_ce_disable(CE1_INSTANCE);
821 free(buffer);
822}
823
824crypto_engine_type board_ce_type(void)
825{
826 return CRYPTO_ENGINE_TYPE_HW;
827}
828
829/* Set up params for h/w CE. */
830void target_crypto_init_params()
831{
832 struct crypto_init_params ce_params;
833
834 /* Set up base addresses and instance. */
835 ce_params.crypto_instance = CE1_INSTANCE;
836 ce_params.crypto_base = MSM_CE1_BASE;
837 ce_params.bam_base = MSM_CE1_BAM_BASE;
838
839 /* Set up BAM config. */
840 ce_params.bam_ee = CE_EE;
841 ce_params.pipes.read_pipe = CE_READ_PIPE;
842 ce_params.pipes.write_pipe = CE_WRITE_PIPE;
843 ce_params.pipes.read_pipe_grp = CE_READ_PIPE_LOCK_GRP;
844 ce_params.pipes.write_pipe_grp = CE_WRITE_PIPE_LOCK_GRP;
845
846 /* Assign buffer sizes. */
847 ce_params.num_ce = CE_ARRAY_SIZE;
848 ce_params.read_fifo_size = CE_FIFO_SIZE;
849 ce_params.write_fifo_size = CE_FIFO_SIZE;
850
851 /* BAM is initialized by TZ for this platform.
852 * Do not do it again as the initialization address space
853 * is locked.
854 */
855 ce_params.do_bam_init = 0;
856
857 crypto_init_params(&ce_params);
858}
859
860uint32_t target_get_hlos_subtype()
861{
862 return board_hlos_subtype();
863}
Channagoud Kadabi400bd112015-08-10 15:38:10 -0700864
865void pmic_reset_configure(uint8_t reset_type)
866{
867 pm8x41_reset_configure(reset_type);
868}
lijuang3606df82015-09-02 21:14:43 +0800869
870uint32_t target_get_pmic()
871{
Baochu Xu34bb8e62017-12-05 14:22:06 +0800872 if ((board_hardware_subtype() == HW_PLATFORM_SUBTYPE_8909_PM660) ||
Umang Agrawalc7e597a2018-07-05 18:01:52 +0530873 (board_hardware_subtype() == HW_PLATFORM_SUBTYPE_8909_PM660_V1) ||
874 (board_hardware_subtype() == HW_PLATFORM_SUBTYPE_8909_COMPAL_ALPHA))
Mayank Grover3f20adf2017-07-03 16:33:22 +0530875 return PMIC_IS_PM660;
876 else
877 return PMIC_IS_PM8909;
lijuang3606df82015-09-02 21:14:43 +0800878}