blob: fbdaf06edf49329bb343fee26e17830455c47e0a [file] [log] [blame]
Umang Agrawalabccfc92017-12-19 12:05:27 +05301/* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +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>
36#include <platform/gpio.h>
37#include <dev/keys.h>
38#include <spmi_v2.h>
39#include <pm8x41.h>
P.V. Phani Kumara053a322015-08-13 18:36:05 +053040#include <pm8x41_hw.h>
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +053041#include <board.h>
42#include <baseband.h>
43#include <hsusb.h>
44#include <scm.h>
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +053045#include <platform/irqs.h>
46#include <platform/clock.h>
P.V. Phani Kumara053a322015-08-13 18:36:05 +053047#include <platform/timer.h>
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +053048#include <crypto5_wrapper.h>
49#include <partition_parser.h>
50#include <stdlib.h>
P.V. Phani Kumara053a322015-08-13 18:36:05 +053051#include <rpm-smd.h>
52#include <spmi.h>
53#include <sdhci_msm.h>
54#include <clock.h>
P.V. Phani Kumar77826d32015-12-26 20:56:35 +053055#include <boot_device.h>
56#include <secapp_loader.h>
57#include <rpmb.h>
58#include <smem.h>
59#include <qmp_phy.h>
60#include <qusb2_phy.h>
Padmanabhan Komanduru0104a892016-01-22 16:58:10 +053061#include "target/display.h"
Sourabh Banerjee51695cc2018-02-27 09:40:30 +053062#include "recovery.h"
63#include <ab_partition_parser.h>
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +053064
65#if LONG_PRESS_POWER_ON
66#include <shutdown_detect.h>
67#endif
68
c_wufeng41310ae2016-01-14 17:59:22 +080069#if PON_VIB_SUPPORT
70#include <vibrator.h>
71#define VIBRATE_TIME 250
72#endif
73
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +053074#define PMIC_ARB_CHANNEL_NUM 0
75#define PMIC_ARB_OWNER_ID 0
76#define TLMM_VOL_UP_BTN_GPIO 85
77
78#define FASTBOOT_MODE 0x77665500
P.V. Phani Kumar77826d32015-12-26 20:56:35 +053079#define RECOVERY_MODE 0x77665502
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +053080#define PON_SOFT_RB_SPARE 0x88F
Sourabh Banerjee51695cc2018-02-27 09:40:30 +053081
82#if VERITY_LE
83#define ROOTDEV_CMDLINE " root=/dev/dm-0 dm=\"system none ro,0 1 android-verity /dev/mmcblk0p"
84#else
85#define ROOTDEV_CMDLINE " root=/dev/mmcblk0p"
86#endif
87
88#define RECOVERY_ROOTDEV_CMDLINE " root=/dev/mmcblk0p"
89#define ROOTDEV_FSTYPE_CMDLINE (" rootfstype=ext4 ")
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +053090
P.V. Phani Kumar77826d32015-12-26 20:56:35 +053091#define CE1_INSTANCE 1
92#define CE_EE 1
93#define CE_FIFO_SIZE 64
94#define CE_READ_PIPE 3
95#define CE_WRITE_PIPE 2
96#define CE_READ_PIPE_LOCK_GRP 0
97#define CE_WRITE_PIPE_LOCK_GRP 0
98#define CE_ARRAY_SIZE 20
99
Vijay Kumar Pendoti7e9226c2016-09-21 20:49:21 +0530100#define SMBCHG_USB_RT_STS 0x21310
101#define USBIN_UV_RT_STS BIT(0)
Umang Agrawalabccfc92017-12-19 12:05:27 +0530102#define USBIN_UV_RT_STS_PMI632 BIT(2)
Vijay Kumar Pendoti7e9226c2016-09-21 20:49:21 +0530103
P.V. Phani Kumara053a322015-08-13 18:36:05 +0530104struct mmc_device *dev;
105
106static uint32_t mmc_pwrctl_base[] =
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530107 { MSM_SDC1_BASE, MSM_SDC2_BASE };
108
P.V. Phani Kumara053a322015-08-13 18:36:05 +0530109static uint32_t mmc_sdhci_base[] =
110 { MSM_SDC1_SDHCI_BASE, MSM_SDC2_SDHCI_BASE };
111
112static uint32_t mmc_sdc_pwrctl_irq[] =
113 { SDCC1_PWRCTL_IRQ, SDCC2_PWRCTL_IRQ };
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530114
115void target_early_init(void)
116{
117#if WITH_DEBUG_UART
P.V. Phani Kumar2e4eeae2015-12-31 16:52:54 +0530118 uart_dm_init(1, 0, BLSP1_UART0_BASE);
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530119#endif
120}
121
anisha agarwalebc52bc2016-07-08 15:50:00 -0700122#if _APPEND_CMDLINE
Sourabh Banerjee51695cc2018-02-27 09:40:30 +0530123/*
124 get_target_boot_params: appends bootparam as per following conditions:
125
126 1. Always appends "rootfstype=ext4", if it is emmc boot path.
127
128 2. Appends more bootparams only if multi-slot is not supported
129 2.1 If booting into recovery:
130 rootfstype=ext4 root=/dev/mmcblk0p<NN>
131 where: root=/dev/mmcblk0p<NN> is block device to "recoveryfs" partition
132
133 2.2 If booting into normal boot path:
134 2.2.1 If verity is enabled:
135 root=/dev/dm-0 dm=\"system none ro,0 1 android-verity /dev/mmcblk0p<NN>
136 where: root=/dev/mmcblk0p<NN> is block device to "system" partition
137
138 2.2.2 If verity is not enabled
139 rootfstype=ext4 root=/dev/mmcblk0p<NN>
140 where: root=/dev/mmcblk0p<NN> is block device to "system" partition
141*/
anisha agarwalebc52bc2016-07-08 15:50:00 -0700142int get_target_boot_params(const char *cmdline, const char *part, char **buf)
143{
144 int system_ptn_index = -1;
145 uint32_t buflen;
146 int ret = -1;
147
148 if (!cmdline || !part ) {
149 dprintf(CRITICAL, "WARN: Invalid input param\n");
150 return -1;
151 }
152
153 if (!strstr(cmdline, "root=/dev/ram")) /* This check is to handle kdev boot */
154 {
155 if (target_is_emmc_boot()) {
Sourabh Banerjee51695cc2018-02-27 09:40:30 +0530156 /*
157 Calculate length for "rootfstype=ext4"
158 The "rootfstype=ext4" is appended to kernel commandline in all conditions
159 The conditions are subsequently documented.
160 */
161 buflen = sizeof(ROOTDEV_FSTYPE_CMDLINE);
162
163 /*
164 Append other bootparams to command line
165 only if multi-slot is not supported.
166 */
167 if(!partition_multislot_is_supported()) {
168 /*
169 When booting into recovery append
170 block device number for "recoveryfs"
171 Eventual command line looks like:
172 ...rootfstype=ext4 root=/dev/mmcblk0p<NN>...
173 */
174 if(boot_into_recovery == true) {
175 buflen += strlen(RECOVERY_ROOTDEV_CMDLINE) + sizeof(int) + 1;
176 } else {
177 /*
178 When booting normally append command line
179 with verity bootparam only if VERITY_LE is
180 defined. The command line is as follows:
181 ...root=/dev/dm-0 dm=\"system none ro,0 1 android-verity /dev/mmcblk0p<NN>...
182 OR
183 ...root=/dev/mmcblk0p<NN>...
184 */
185 buflen += strlen(ROOTDEV_CMDLINE) + sizeof(int) + 1;
186 }
187 }
188
anisha agarwalebc52bc2016-07-08 15:50:00 -0700189 *buf = (char *)malloc(buflen);
190 if(!(*buf)) {
191 dprintf(CRITICAL,"Unable to allocate memory for boot params\n");
192 return -1;
193 }
194 /* Below is for emmc boot */
195 system_ptn_index = partition_get_index(part) + 1; /* Adding +1 as offsets for eMMC start at 1 and NAND at 0 */
196 if (system_ptn_index < 0) {
197 dprintf(CRITICAL,
Sourabh Banerjee51695cc2018-02-27 09:40:30 +0530198 "WARN: Cannot get partition index for %s\n", part);
anisha agarwalebc52bc2016-07-08 15:50:00 -0700199 free(*buf);
200 return -1;
201 }
Sourabh Banerjee51695cc2018-02-27 09:40:30 +0530202
203 if(!partition_multislot_is_supported()) {
204 if(boot_into_recovery == true) {
205 snprintf(*buf, buflen, "%s %s%d", ROOTDEV_FSTYPE_CMDLINE,
206 RECOVERY_ROOTDEV_CMDLINE, system_ptn_index);
207 } else {
208 snprintf(*buf, buflen, "%s %s%d", ROOTDEV_FSTYPE_CMDLINE,
209 ROOTDEV_CMDLINE, system_ptn_index);
210 }
211 }
212
anisha agarwalebc52bc2016-07-08 15:50:00 -0700213 ret = 0;
214 }
215 }
216 /*in success case buf will be freed in the calling function of this*/
217 return ret;
218}
219#endif
220
P.V. Phani Kumara053a322015-08-13 18:36:05 +0530221static void set_sdc_power_ctrl()
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530222{
P.V. Phani Kumara053a322015-08-13 18:36:05 +0530223 /* Drive strength configs for sdc pins */
224 struct tlmm_cfgs sdc1_hdrv_cfg[] =
225 {
226 { SDC1_CLK_HDRV_CTL_OFF, TLMM_CUR_VAL_16MA, TLMM_HDRV_MASK, 0},
227 { SDC1_CMD_HDRV_CTL_OFF, TLMM_CUR_VAL_10MA, TLMM_HDRV_MASK, 0},
228 { SDC1_DATA_HDRV_CTL_OFF, TLMM_CUR_VAL_10MA, TLMM_HDRV_MASK , 0},
229 };
230
231 /* Pull configs for sdc pins */
232 struct tlmm_cfgs sdc1_pull_cfg[] =
233 {
234 { SDC1_CLK_PULL_CTL_OFF, TLMM_NO_PULL, TLMM_PULL_MASK, 0},
235 { SDC1_CMD_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, 0},
236 { SDC1_DATA_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, 0},
237 };
238
239 struct tlmm_cfgs sdc1_rclk_cfg[] =
240 {
241 { SDC1_RCLK_PULL_CTL_OFF, TLMM_PULL_DOWN, TLMM_PULL_MASK, 0},
242 };
243
244 /* Set the drive strength & pull control values */
245 tlmm_set_hdrive_ctrl(sdc1_hdrv_cfg, ARRAY_SIZE(sdc1_hdrv_cfg));
246 tlmm_set_pull_ctrl(sdc1_pull_cfg, ARRAY_SIZE(sdc1_pull_cfg));
247 tlmm_set_pull_ctrl(sdc1_rclk_cfg, ARRAY_SIZE(sdc1_rclk_cfg));
248}
249
250void target_sdc_init()
251{
252 struct mmc_config_data config;
253
254 /* Set drive strength & pull ctrl values */
255 set_sdc_power_ctrl();
256
257 config.slot = MMC_SLOT;
258 config.bus_width = DATA_BUS_WIDTH_8BIT;
259 config.max_clk_rate = MMC_CLK_192MHZ;
260 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
261 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
262 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
263 config.hs400_support = 1;
264
265 if (!(dev = mmc_init(&config))) {
266 /* Try different config. values */
267 config.max_clk_rate = MMC_CLK_200MHZ;
268 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
269 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
270 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
271 config.hs400_support = 0;
272
273 if (!(dev = mmc_init(&config))) {
274 dprintf(CRITICAL, "mmc init failed!");
275 ASSERT(0);
276 }
277 }
278}
279
280void *target_mmc_device()
281{
282 return (void *) dev;
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530283}
284
285/* Return 1 if vol_up pressed */
Gaurav Nebhwanid9dd0342016-01-28 16:35:55 +0530286int target_volume_up()
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530287{
288 uint8_t status = 0;
289
290 gpio_tlmm_config(TLMM_VOL_UP_BTN_GPIO, 0, GPIO_INPUT, GPIO_PULL_UP, GPIO_2MA, GPIO_ENABLE);
291
292 /* Wait for the gpio config to take effect - debounce time */
293 thread_sleep(10);
294
295 /* Get status of GPIO */
296 status = gpio_status(TLMM_VOL_UP_BTN_GPIO);
297
298 /* Active high signal. */
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530299 return !status;
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530300}
301
302/* Return 1 if vol_down pressed */
303uint32_t target_volume_down()
304{
305 /* Volume down button tied in with PMIC RESIN. */
306 return pm8x41_resin_status();
307}
308
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530309uint32_t target_is_pwrkey_pon_reason()
310{
Umang Agrawalabccfc92017-12-19 12:05:27 +0530311 uint32_t pmic = target_get_pmic();
312 uint8_t pon_reason = 0;
Umang Agrawalabccfc92017-12-19 12:05:27 +0530313 bool usb_present_sts = 1;
314
315 if (pmic == PMIC_IS_PMI632)
316 {
Umang Agrawal5f14ae42018-02-21 15:51:18 +0530317 pon_reason = pmi632_get_pon_reason();
Umang Agrawalabccfc92017-12-19 12:05:27 +0530318 usb_present_sts = !(USBIN_UV_RT_STS_PMI632 &
Vijay Kumar Pendoti7e9226c2016-09-21 20:49:21 +0530319 pm8x41_reg_read(SMBCHG_USB_RT_STS));
Umang Agrawalabccfc92017-12-19 12:05:27 +0530320 }
321 else
322 {
323 pon_reason = pm8950_get_pon_reason();
Umang Agrawal5f14ae42018-02-21 15:51:18 +0530324 usb_present_sts = !(USBIN_UV_RT_STS &
Umang Agrawalabccfc92017-12-19 12:05:27 +0530325 pm8x41_reg_read(SMBCHG_USB_RT_STS));
326 }
327
Umang Agrawal5f14ae42018-02-21 15:51:18 +0530328 if (pm8x41_get_is_cold_boot() && ((pon_reason == KPDPWR_N) ||
Umang Agrawalabccfc92017-12-19 12:05:27 +0530329 (pon_reason == (KPDPWR_N|PON1))))
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530330 return 1;
Vijay Kumar Pendoti7e9226c2016-09-21 20:49:21 +0530331 else if ((pon_reason == PON1) && (!usb_present_sts))
332 return 1;
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530333 else
334 return 0;
335}
336
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530337static void target_keystatus()
338{
339 keys_init();
340
341 if(target_volume_down())
342 keys_post_event(KEY_VOLUMEDOWN, 1);
343
344 if(target_volume_up())
345 keys_post_event(KEY_VOLUMEUP, 1);
346}
347
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530348void target_init(void)
349{
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530350 dprintf(INFO, "target_init()\n");
351
352 spmi_init(PMIC_ARB_CHANNEL_NUM, PMIC_ARB_OWNER_ID);
353
354 target_keystatus();
355
P.V. Phani Kumara053a322015-08-13 18:36:05 +0530356 target_sdc_init();
357 if (partition_read_table())
358 {
359 dprintf(CRITICAL, "Error reading the partition table info\n");
360 ASSERT(0);
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530361 }
P.V. Phani Kumara053a322015-08-13 18:36:05 +0530362
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530363#if LONG_PRESS_POWER_ON
Umang Agrawalabccfc92017-12-19 12:05:27 +0530364 if (target_is_pmi_enabled())
365 shutdown_detect();
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530366#endif
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530367
c_wufeng41310ae2016-01-14 17:59:22 +0800368#if PON_VIB_SUPPORT
Umang Agrawalabccfc92017-12-19 12:05:27 +0530369 if (target_is_pmi_enabled())
370 vib_timed_turn_on(VIBRATE_TIME);
c_wufeng41310ae2016-01-14 17:59:22 +0800371#endif
372
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530373
374 if (target_use_signed_kernel())
375 target_crypto_init_params();
376
377#if VERIFIED_BOOT
Mayank Grover8b2f19a2017-10-26 12:12:17 +0530378 if (VB_M <= target_get_vb_version())
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530379 {
Mayank Grover6878e012017-09-06 11:04:03 +0530380 clock_ce_enable(CE1_INSTANCE);
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530381
Mayank Grover6878e012017-09-06 11:04:03 +0530382 /* Initialize Qseecom */
383 if (qseecom_init() < 0)
384 {
385 dprintf(CRITICAL, "Failed to initialize qseecom\n");
386 ASSERT(0);
387 }
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530388
Mayank Grover6878e012017-09-06 11:04:03 +0530389 /* Start Qseecom */
390 if (qseecom_tz_init() < 0)
391 {
392 dprintf(CRITICAL, "Failed to start qseecom\n");
393 ASSERT(0);
394 }
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530395
Mayank Grover6878e012017-09-06 11:04:03 +0530396 if (rpmb_init() < 0)
397 {
398 dprintf(CRITICAL, "RPMB init failed\n");
399 ASSERT(0);
400 }
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530401
Mayank Grover6878e012017-09-06 11:04:03 +0530402 /*
403 * Load the sec app for first time
404 */
405 if (load_sec_app() < 0)
406 {
407 dprintf(CRITICAL, "Failed to load App for verified\n");
408 ASSERT(0);
409 }
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530410 }
411#endif
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530412
413#if SMD_SUPPORT
414 rpm_smd_init();
415#endif
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530416}
417
418void target_serialno(unsigned char *buf)
419{
420 uint32_t serialno;
421 if (target_is_emmc_boot()) {
422 serialno = mmc_get_psn();
423 snprintf((char *)buf, 13, "%x", serialno);
424 }
425}
426
427unsigned board_machtype(void)
428{
P.V. Phani Kumara053a322015-08-13 18:36:05 +0530429 return LINUX_MACHTYPE_UNKNOWN;
430}
431
432/* Detect the target type */
433void target_detect(struct board_data *board)
434{
435 /* This is already filled as part of board.c */
436}
437
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530438/* Detect the modem type */
P.V. Phani Kumara053a322015-08-13 18:36:05 +0530439void target_baseband_detect(struct board_data *board)
440{
441 uint32_t platform;
442
443 platform = board->platform;
444
445 switch(platform) {
Gaurav Nebhwani6c945a42016-02-16 17:26:51 +0530446 case MSM8953:
Mayank Grover759e0b02017-04-11 11:59:06 +0530447 case SDM450:
lijuang2f1c1f52017-12-12 14:44:32 +0800448 case SDM632:
P.V. Phani Kumara053a322015-08-13 18:36:05 +0530449 board->baseband = BASEBAND_MSM;
450 break;
Gaurav Nebhwani6c945a42016-02-16 17:26:51 +0530451 case APQ8053:
Mayank Grover3dc285c2017-12-26 12:47:09 +0530452 case SDA450:
lijuang2f1c1f52017-12-12 14:44:32 +0800453 case SDA632:
Gaurav Nebhwani22a0d9f2015-12-29 13:49:26 +0530454 board->baseband = BASEBAND_APQ;
455 break;
P.V. Phani Kumara053a322015-08-13 18:36:05 +0530456 default:
457 dprintf(CRITICAL, "Platform type: %u is not supported\n",platform);
458 ASSERT(0);
459 };
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530460}
461
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530462unsigned target_baseband()
463{
464 return board_baseband();
465}
lijuang395b5e62015-11-19 17:39:44 +0800466
467int set_download_mode(enum reboot_reason mode)
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530468{
469 int ret = 0;
470 ret = scm_dload_mode(mode);
471
472 pm8x41_clear_pmic_watchdog();
473
474 return ret;
475}
476
477int emmc_recovery_init(void)
478{
479 return _emmc_recovery_init();
480}
481
482unsigned target_pause_for_battery_charge(void)
483{
Umang Agrawalabccfc92017-12-19 12:05:27 +0530484 uint32_t pmic = target_get_pmic();
Umang Agrawal5f14ae42018-02-21 15:51:18 +0530485 uint8_t pon_reason = pm8x41_get_pon_reason();
486 uint8_t is_cold_boot = pm8x41_get_is_cold_boot();
Umang Agrawalabccfc92017-12-19 12:05:27 +0530487 bool usb_present_sts = 1;
488
Umang Agrawal5f14ae42018-02-21 15:51:18 +0530489 if (target_is_pmi_enabled())
Umang Agrawalabccfc92017-12-19 12:05:27 +0530490 {
Umang Agrawal5f14ae42018-02-21 15:51:18 +0530491 if (pmic == PMIC_IS_PMI632)
492 usb_present_sts = !(USBIN_UV_RT_STS_PMI632 &
Zhenhua Huangcf812d72016-01-27 17:27:47 +0800493 pm8x41_reg_read(SMBCHG_USB_RT_STS));
Umang Agrawal5f14ae42018-02-21 15:51:18 +0530494 else
Umang Agrawalabccfc92017-12-19 12:05:27 +0530495 usb_present_sts = !(USBIN_UV_RT_STS &
496 pm8x41_reg_read(SMBCHG_USB_RT_STS));
497 }
498
Zhenhua Huangcf812d72016-01-27 17:27:47 +0800499 dprintf(INFO, "%s : pon_reason is:0x%x cold_boot:%d usb_sts:%d\n", __func__,
500 pon_reason, is_cold_boot, usb_present_sts);
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530501 /* In case of fastboot reboot,adb reboot or if we see the power key
502 * pressed we do not want go into charger mode.
503 * fastboot reboot is warm boot with PON hard reset bit not set
504 * adb reboot is a cold boot with PON hard reset bit set
505 */
506 if (is_cold_boot &&
507 (!(pon_reason & HARD_RST)) &&
508 (!(pon_reason & KPDPWR_N)) &&
Zhenhua Huangcf812d72016-01-27 17:27:47 +0800509 usb_present_sts)
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530510 return 1;
511 else
512 return 0;
513}
514
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530515void target_uninit(void)
516{
Mayank Grover92e55972018-03-23 17:58:16 +0530517#if PON_VIB_SUPPORT
518 if(target_is_pmi_enabled())
519 turn_off_vib_early();
520#endif
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530521 mmc_put_card_to_sleep(dev);
522 sdhci_mode_disable(&dev->host);
523 if (crypto_initialized())
524 crypto_eng_cleanup();
525
526 if (target_is_ssd_enabled())
527 clock_ce_disable(CE1_INSTANCE);
528
529#if VERIFIED_BOOT
Mayank Grover8b2f19a2017-10-26 12:12:17 +0530530 if (VB_M <= target_get_vb_version())
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530531 {
Mayank Grover6878e012017-09-06 11:04:03 +0530532 if (is_sec_app_loaded())
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530533 {
Mayank Grover6878e012017-09-06 11:04:03 +0530534 if (send_milestone_call_to_tz() < 0)
535 {
536 dprintf(CRITICAL, "Failed to unload App for rpmb\n");
537 ASSERT(0);
538 }
539 }
540
541 if (rpmb_uninit() < 0)
542 {
543 dprintf(CRITICAL, "RPMB uninit failed\n");
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530544 ASSERT(0);
545 }
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530546
Mayank Grover6878e012017-09-06 11:04:03 +0530547 clock_ce_disable(CE1_INSTANCE);
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530548 }
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530549#endif
550
551#if SMD_SUPPORT
552 rpm_smd_uninit();
553#endif
554}
555
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530556/* UTMI MUX configuration to connect PHY to SNPS controller:
557 * Configure primary HS phy mux to use UTMI interface
558 * (connected to usb30 controller).
559 */
560static void tcsr_hs_phy_mux_configure(void)
561{
562 uint32_t reg;
563
564 reg = readl(USB2_PHY_SEL);
565
566 writel(reg | 0x1, USB2_PHY_SEL);
567}
568
569/* configure hs phy mux if using dwc controller */
570void target_usb_phy_mux_configure(void)
571{
572 if(!strcmp(target_usb_controller(), "dwc"))
573 {
574 tcsr_hs_phy_mux_configure();
575 }
576}
577
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530578void target_usb_phy_reset()
579{
580
581 usb30_qmp_phy_reset();
582 qusb2_phy_reset();
583}
584
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530585/* Initialize target specific USB handlers */
586target_usb_iface_t* target_usb30_init()
587{
588 target_usb_iface_t *t_usb_iface;
589
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530590 t_usb_iface = (target_usb_iface_t *) calloc(1, sizeof(target_usb_iface_t));
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530591 ASSERT(t_usb_iface);
592
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530593 t_usb_iface->mux_config = NULL;
594 t_usb_iface->phy_init = usb30_qmp_phy_init;
595 t_usb_iface->phy_reset = target_usb_phy_reset;
596 t_usb_iface->clock_init = clock_usb30_init;
597 t_usb_iface->vbus_override = 1;
Aparna Mallavarapu01fc00a2015-06-01 20:37:05 +0530598
599 return t_usb_iface;
600}
601
602/* identify the usb controller to be used for the target */
603const char * target_usb_controller()
604{
605 return "dwc";
606}
P.V. Phani Kumara053a322015-08-13 18:36:05 +0530607
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530608/* Do any target specific intialization needed before entering fastboot mode */
609void target_fastboot_init(void)
610{
611 if (target_is_ssd_enabled()) {
612 clock_ce_enable(CE1_INSTANCE);
613 target_load_ssd_keystore();
614 }
615}
616
617void target_load_ssd_keystore(void)
618{
619 uint64_t ptn;
620 int index;
621 uint64_t size;
622 uint32_t *buffer = NULL;
623
624 if (!target_is_ssd_enabled())
625 return;
626
627 index = partition_get_index("ssd");
628
629 ptn = partition_get_offset(index);
630 if (ptn == 0){
631 dprintf(CRITICAL, "Error: ssd partition not found\n");
632 return;
633 }
634
635 size = partition_get_size(index);
636 if (size == 0) {
637 dprintf(CRITICAL, "Error: invalid ssd partition size\n");
638 return;
639 }
640
641 buffer = memalign(CACHE_LINE, ROUNDUP(size, CACHE_LINE));
642 if (!buffer) {
643 dprintf(CRITICAL, "Error: allocating memory for ssd buffer\n");
644 return;
645 }
646
647 if (mmc_read(ptn, buffer, size)) {
648 dprintf(CRITICAL, "Error: cannot read data\n");
649 free(buffer);
650 return;
651 }
652
653 clock_ce_enable(CE1_INSTANCE);
654 scm_protect_keystore(buffer, size);
655 clock_ce_disable(CE1_INSTANCE);
656 free(buffer);
657}
P.V. Phani Kumara053a322015-08-13 18:36:05 +0530658
659crypto_engine_type board_ce_type(void)
660{
661 return CRYPTO_ENGINE_TYPE_HW;
662}
663
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530664/* Set up params for h/w CE. */
665void target_crypto_init_params()
666{
667 struct crypto_init_params ce_params;
668
669 /* Set up base addresses and instance. */
670 ce_params.crypto_instance = CE1_INSTANCE;
671 ce_params.crypto_base = MSM_CE1_BASE;
672 ce_params.bam_base = MSM_CE1_BAM_BASE;
673
674 /* Set up BAM config. */
675 ce_params.bam_ee = CE_EE;
676 ce_params.pipes.read_pipe = CE_READ_PIPE;
677 ce_params.pipes.write_pipe = CE_WRITE_PIPE;
678 ce_params.pipes.read_pipe_grp = CE_READ_PIPE_LOCK_GRP;
679 ce_params.pipes.write_pipe_grp = CE_WRITE_PIPE_LOCK_GRP;
680
681 /* Assign buffer sizes. */
682 ce_params.num_ce = CE_ARRAY_SIZE;
683 ce_params.read_fifo_size = CE_FIFO_SIZE;
684 ce_params.write_fifo_size = CE_FIFO_SIZE;
685
686 /* BAM is initialized by TZ for this platform.
687 * Do not do it again as the initialization address space
688 * is locked.
689 */
690 ce_params.do_bam_init = 0;
691
692 crypto_init_params(&ce_params);
693}
P.V. Phani Kumara053a322015-08-13 18:36:05 +0530694
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530695uint32_t target_get_pmic()
696{
Umang Agrawalabccfc92017-12-19 12:05:27 +0530697 if (target_is_pmi_enabled()) {
698 uint32_t pmi_type = board_pmic_target(1) & 0xffff;
699 if (pmi_type == PMIC_IS_PMI632)
700 return PMIC_IS_PMI632;
701 else
702 return PMIC_IS_PMI8950;
703 }
704 else {
705 return PMIC_IS_UNKNOWN;
706 }
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530707}
708
Umang Agrawal89f6dcb2018-01-03 19:07:47 +0530709void pmic_reset_configure(uint8_t reset_type)
710{
711 uint32_t pmi_type;
712
713 pmi_type = target_get_pmic();
714 if (pmi_type == PMIC_IS_PMI632)
715 pmi632_reset_configure(reset_type);
716 else
717 pm8994_reset_configure(reset_type);
718}
719
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530720struct qmp_reg qmp_settings[] =
721{
Mayank Grovere55fe622016-10-13 18:39:05 +0530722 {0x804, 0x01}, /* USB3PHY_PCIE_USB3_PCS_POWER_DOWN_CONTROL */
723
724 /* Common block settings */
725 {0xAC, 0x14}, /* QSERDES_COM_SYSCLK_EN_SEL */
726 {0x34, 0x08}, /* QSERDES_COM_BIAS_EN_CLKBUFLR_EN */
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530727 {0x174, 0x30}, /* QSERDES_COM_CLK_SELECT */
Mayank Grovere55fe622016-10-13 18:39:05 +0530728 {0x70, 0x0F}, /* USB3PHY_QSERDES_COM_BG_TRIM */
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530729 {0x19c, 0x01}, /* QSERDES_COM_SVS_MODE_CLK_SEL */
730 {0x178, 0x00}, /* QSERDES_COM_HSCLK_SEL */
Mayank Grovere55fe622016-10-13 18:39:05 +0530731 {0x194, 0x06}, /* QSERDES_COM_CMN_CONFIG */
732 {0x48, 0x0F}, /* USB3PHY_QSERDES_COM_PLL_IVCO */
733 {0x3C, 0x02}, /* QSERDES_COM_SYS_CLK_CTRL */
734
735 /* PLL & Loop filter settings */
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530736 {0xd0, 0x82}, /* QSERDES_COM_DEC_START_MODE0 */
737 {0xdc, 0x55}, /* QSERDES_COM_DIV_FRAC_START1_MODE0 */
738 {0xe0, 0x55}, /* QSERDES_COM_DIV_FRAC_START2_MODE0 */
739 {0xe4, 0x03}, /* QSERDES_COM_DIV_FRAC_START3_MODE0 */
740 {0x78, 0x0b}, /* QSERDES_COM_CP_CTRL_MODE0 */
741 {0x84, 0x16}, /* QSERDES_COM_PLL_RCTRL_MODE0 */
742 {0x90, 0x28}, /* QSERDES_COM_PLL_CCTRL_MODE0 */
743 {0x108, 0x80}, /* QSERDES_COM_INTEGLOOP_GAIN0_MODE0 */
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530744 {0x4c, 0x15}, /* QSERDES_COM_LOCK_CMP1_MODE0 */
745 {0x50, 0x34}, /* QSERDES_COM_LOCK_CMP2_MODE0 */
746 {0x54, 0x00}, /* QSERDES_COM_LOCK_CMP3_MODE0 */
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530747 {0x18c, 0x00}, /* QSERDES_COM_CORE_CLK_EN */
748 {0xcc, 0x00}, /* QSERDES_COM_LOCK_CMP_CFG */
749 {0x128, 0x00}, /* QSERDES_COM_VCO_TUNE_MAP */
750 {0x0C, 0x0A}, /* QSERDES_COM_BG_TIMER */
Mayank Grovere55fe622016-10-13 18:39:05 +0530751
752 /* SSC Settings */
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530753 {0x10, 0x01}, /* QSERDES_COM_SSC_EN_CENTER */
754 {0x1c, 0x31}, /* QSERDES_COM_SSC_PER1 */
755 {0x20, 0x01}, /* QSERDES_COM_SSC_PER2 */
756 {0x14, 0x00}, /* QSERDES_COM_SSC_ADJ_PER1 */
757 {0x18, 0x00}, /* QSERDES_COM_SSC_ADJ_PER2 */
758 {0x24, 0xde}, /* QSERDES_COM_SSC_STEP_SIZE1 */
759 {0x28, 0x07}, /* QSERDES_COM_SSC_STEP_SIZE2 */
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530760
761 /* Rx Settings */
Mayank Grovere55fe622016-10-13 18:39:05 +0530762 {0x41C, 0x06}, /* QSERDES_RX_UCDR_SO_GAIN */
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530763 {0x4d8, 0x02}, /* QSERDES_RX_RX_EQU_ADAPTOR_CNTRL2 */
Mayank Grovere55fe622016-10-13 18:39:05 +0530764 {0x4dc, 0x4c}, /* QSERDES_RX_RX_EQU_ADAPTOR_CNTRL3 */
765 {0x4e0, 0xb8}, /* QSERDES_RX_RX_EQU_ADAPTOR_CNTRL4 */
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530766 {0x508, 0x77}, /* QSERDES_RX_RX_EQ_OFFSET_ADAPTOR_CNTRL1 */
767 {0x50c, 0x80}, /* QSERDES_RX_RX_OFFSET_ADAPTOR_CNTRL2 */
768 {0x514, 0x03}, /* QSERDES_RX_SIGDET_CNTRL */
769 {0x51c, 0x16}, /* QSERDES_RX_SIGDET_DEGLITCH_CNTRL */
Mayank Grovere55fe622016-10-13 18:39:05 +0530770 {0x510, 0x0C}, /* QSERDES_RX_SIGDET_ENABLES */
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530771
772 /* Tx settings */
773 {0x268, 0x45}, /* QSERDES_TX_HIGHZ_TRANSCEIVEREN_BIAS_DRVR_EN */
774 {0x2ac, 0x12}, /* QSERDES_TX_RCV_DETECT_LVL_2 */
775 {0x294, 0x06}, /* QSERDES_TX_LANE_MODE */
Mayank Grovere55fe622016-10-13 18:39:05 +0530776 {0x824, 0x15}, /* PCIE_USB3_PCS_TXDEEMPH_M6DB_V0 */
777 {0x828, 0x0E}, /* PCIE_USB3_PCS_TXDEEMPH_M3P5DB_V0 */
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530778
779 /* FLL settings */
780 {0x8c8, 0x83}, /* PCIE_USB3_PCS_FLL_CNTRL2 */
781 {0x8c4, 0x02}, /* PCIE_USB3_PCS_FLL_CNTRL1 */
782 {0x8cc, 0x09}, /* PCIE_USB3_PCS_FLL_CNT_VAL_L */
783 {0x8D0, 0xA2}, /* PCIE_USB3_PCS_FLL_CNT_VAL_H_TOL */
784 {0x8D4, 0x85}, /* PCIE_USB3_PCS_FLL_MAN_CODE */
785
786 /* PCS Settings */
787 {0x880, 0xD1}, /* PCIE_USB3_PCS_LOCK_DETECT_CONFIG1 */
788 {0x884, 0x1F}, /* PCIE_USB3_PCS_LOCK_DETECT_CONFIG2 */
789 {0x888, 0x47}, /* PCIE_USB3_PCS_LOCK_DETECT_CONFIG3 */
Mayank Grovere55fe622016-10-13 18:39:05 +0530790 {0x864, 0x1B}, /* PCIE_USB3_PCS_POWER_STATE_CONFIG2 */
P.V. Phani Kumar77826d32015-12-26 20:56:35 +0530791 {0x8B8, 0x75}, /* PCIE_USB3_PCS_RXEQTRAINING_WAIT_TIME */
792 {0x8BC, 0x13}, /* PCIE_USB3_PCS_RXEQTRAINING_RUN_TIME */
793 {0x8B0, 0x86}, /* PCIE_USB3_PCS_LFPS_TX_ECSTART_EQTLOCK */
794 {0x8A0, 0x04}, /* PCIE_USB3_PCS_PWRUP_RESET_DLY_TIME_AUXCLK */
795 {0x88C, 0x44}, /* PCIE_USB3_PCS_TSYNC_RSYNC_TIME */
796 {0x870, 0xE7}, /* PCIE_USB3_PCS_RCVR_DTCT_DLY_P1U2_L */
797 {0x874, 0x03}, /* PCIE_USB3_PCS_RCVR_DTCT_DLY_P1U2_H */
798 {0x878, 0x40}, /* PCIE_USB3_PCS_RCVR_DTCT_DLY_U3_L */
799 {0x87c, 0x00}, /* PCIE_USB3_PCS_RCVR_DTCT_DLY_U3_H */
800 {0x9D8, 0x88}, /* PCIE_USB3_PCS_RX_SIGDET_LVL */
801 {0x808, 0x03}, /* PCIE_USB3_PCS_START_CONTROL */
802 {0x800, 0x00}, /* PCIE_USB3_PCS_SW_RESET */
803};
804
805struct qmp_reg *target_get_qmp_settings()
806{
807 return qmp_settings;
808}
809
810int target_get_qmp_regsize()
811{
812 return ARRAY_SIZE(qmp_settings);
813}
Padmanabhan Komanduru0104a892016-01-22 16:58:10 +0530814static uint8_t splash_override;
815/* Returns 1 if target supports continuous splash screen. */
816int target_cont_splash_screen()
817{
818 uint8_t splash_screen = 0;
819 if (!splash_override) {
820 switch (board_hardware_id()) {
821 case HW_PLATFORM_MTP:
822 case HW_PLATFORM_SURF:
823 case HW_PLATFORM_RCM:
824 case HW_PLATFORM_QRD:
825 splash_screen = 1;
826 break;
827 default:
828 splash_screen = 0;
829 break;
830 }
831 dprintf(SPEW, "Target_cont_splash=%d\n", splash_screen);
832 }
833 return splash_screen;
834}
835
836void target_force_cont_splash_disable(uint8_t override)
837{
838 splash_override = override;
839}