blob: 88d685494a5a6c8bed17f538cd3ea5f62b544b0c [file] [log] [blame]
Parth Dixit80bb5232016-01-05 15:26:22 +05301/* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
Aparna Mallavarapuca676882015-01-19 20:39:06 +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>
Aparna Mallavarapubc6315e2015-04-11 04:00:43 +053040#include <pm8x41_hw.h>
Aparna Mallavarapuca676882015-01-19 20:39:06 +053041#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>
Aparna Mallavarapubc6315e2015-04-11 04:00:43 +053049#include <platform/timer.h>
Aparna Mallavarapuca676882015-01-19 20:39:06 +053050#include <crypto5_wrapper.h>
51#include <partition_parser.h>
52#include <stdlib.h>
Aparna Mallavarapufa5f8a72015-03-31 06:21:36 +053053#include <rpm-smd.h>
Aparna Mallavarapubc6315e2015-04-11 04:00:43 +053054#include <spmi.h>
55#include <sdhci_msm.h>
56#include <clock.h>
Parth Dixit6e6bad52015-07-30 19:02:38 +053057#include <boot_device.h>
58#include <secapp_loader.h>
59#include <rpmb.h>
lijuang3606df82015-09-02 21:14:43 +080060#include <smem.h>
Aparna Mallavarapuca676882015-01-19 20:39:06 +053061
Padmanabhan Komanduru9d49f892015-04-10 12:58:46 -070062#include "target/display.h"
63
Aparna Mallavarapuca676882015-01-19 20:39:06 +053064#if LONG_PRESS_POWER_ON
65#include <shutdown_detect.h>
66#endif
67
Matthew Qin47dfdb72015-06-10 21:29:11 +080068#if PON_VIB_SUPPORT
69#include <vibrator.h>
70#endif
71
72#if PON_VIB_SUPPORT
73#define VIBRATE_TIME 250
74#endif
75
Aparna Mallavarapuca676882015-01-19 20:39:06 +053076#define PMIC_ARB_CHANNEL_NUM 0
77#define PMIC_ARB_OWNER_ID 0
78#define TLMM_VOL_UP_BTN_GPIO 85
Unnati Gandhife004a92015-06-01 13:06:06 +053079#define TLMM_VOL_UP_BTN_GPIO_8956 113
Parth Dixit720d3b92015-10-30 01:21:34 +053080#define TLMM_VOL_UP_BTN_GPIO_8937 91
Wufengf2e37312016-04-12 16:09:47 +080081#define TLMM_VOL_DOWN_BTN_GPIO 128
Aparna Mallavarapuca676882015-01-19 20:39:06 +053082
83#define FASTBOOT_MODE 0x77665500
Aparna Mallavarapu680a1332015-04-29 19:14:09 +053084#define RECOVERY_MODE 0x77665502
Aparna Mallavarapuca676882015-01-19 20:39:06 +053085#define PON_SOFT_RB_SPARE 0x88F
86
Parth Dixit17b85192016-12-28 15:51:33 +053087#define EXT4_CMDLINE " rootfstype=ext4 root=/dev/mmcblk0p"
88
Aparna Mallavarapu1e8b0932015-03-29 23:38:13 +053089#define CE1_INSTANCE 1
90#define CE_EE 1
91#define CE_FIFO_SIZE 64
92#define CE_READ_PIPE 3
93#define CE_WRITE_PIPE 2
94#define CE_READ_PIPE_LOCK_GRP 0
95#define CE_WRITE_PIPE_LOCK_GRP 0
96#define CE_ARRAY_SIZE 20
Wufengf2e37312016-04-12 16:09:47 +080097#define SUB_TYPE_SKUT 0x0A
Vijay Kumar Pendoti0d698842016-09-23 16:23:16 +053098#define SMBCHG_USB_RT_STS 0x21310
99#define USBIN_UV_RT_STS BIT(0)
Aparna Mallavarapu1e8b0932015-03-29 23:38:13 +0530100
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530101struct mmc_device *dev;
102
103static uint32_t mmc_pwrctl_base[] =
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530104 { MSM_SDC1_BASE, MSM_SDC2_BASE };
105
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530106static uint32_t mmc_sdhci_base[] =
107 { MSM_SDC1_SDHCI_BASE, MSM_SDC2_SDHCI_BASE };
108
109static uint32_t mmc_sdc_pwrctl_irq[] =
110 { SDCC1_PWRCTL_IRQ, SDCC2_PWRCTL_IRQ };
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530111
112void target_early_init(void)
113{
114#if WITH_DEBUG_UART
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530115 uart_dm_init(2, 0, BLSP1_UART1_BASE);
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530116#endif
117}
118
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530119static void set_sdc_power_ctrl()
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530120{
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530121 /* Drive strength configs for sdc pins */
122 struct tlmm_cfgs sdc1_hdrv_cfg[] =
123 {
124 { SDC1_CLK_HDRV_CTL_OFF, TLMM_CUR_VAL_16MA, TLMM_HDRV_MASK, 0},
125 { SDC1_CMD_HDRV_CTL_OFF, TLMM_CUR_VAL_10MA, TLMM_HDRV_MASK, 0},
126 { SDC1_DATA_HDRV_CTL_OFF, TLMM_CUR_VAL_10MA, TLMM_HDRV_MASK , 0},
127 };
128
129 /* Pull configs for sdc pins */
130 struct tlmm_cfgs sdc1_pull_cfg[] =
131 {
132 { SDC1_CLK_PULL_CTL_OFF, TLMM_NO_PULL, TLMM_PULL_MASK, 0},
133 { SDC1_CMD_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, 0},
134 { SDC1_DATA_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, 0},
135 };
136
Aparna Mallavarapu29138912015-04-13 23:45:35 +0530137 struct tlmm_cfgs sdc1_rclk_cfg[] =
138 {
139 { SDC1_RCLK_PULL_CTL_OFF, TLMM_PULL_DOWN, TLMM_PULL_MASK, 0},
140 };
141
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530142 /* Set the drive strength & pull control values */
143 tlmm_set_hdrive_ctrl(sdc1_hdrv_cfg, ARRAY_SIZE(sdc1_hdrv_cfg));
144 tlmm_set_pull_ctrl(sdc1_pull_cfg, ARRAY_SIZE(sdc1_pull_cfg));
Aparna Mallavarapu29138912015-04-13 23:45:35 +0530145 tlmm_set_pull_ctrl(sdc1_rclk_cfg, ARRAY_SIZE(sdc1_rclk_cfg));
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530146}
147
148void target_sdc_init()
149{
150 struct mmc_config_data config;
151
152 /* Set drive strength & pull ctrl values */
153 set_sdc_power_ctrl();
154
155 /* Try slot 1*/
156 config.slot = 1;
157 config.bus_width = DATA_BUS_WIDTH_8BIT;
Aparna Mallavarapu680a1332015-04-29 19:14:09 +0530158 config.max_clk_rate = MMC_CLK_192MHZ;
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530159 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
160 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
161 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
162 config.hs400_support = 1;
163
164 if (!(dev = mmc_init(&config))) {
165 /* Try slot 2 */
166 config.slot = 2;
167 config.max_clk_rate = MMC_CLK_200MHZ;
168 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
169 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
170 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
171 config.hs400_support = 0;
172
173 if (!(dev = mmc_init(&config))) {
174 dprintf(CRITICAL, "mmc init failed!");
175 ASSERT(0);
176 }
177 }
178}
179
180void *target_mmc_device()
181{
182 return (void *) dev;
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530183}
184
185/* Return 1 if vol_up pressed */
Rami Butsteine51318a2015-05-27 16:23:17 +0300186int target_volume_up()
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530187{
lijuang2d2b8a02015-06-05 21:34:15 +0800188 static uint8_t first_time = 0;
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530189 uint8_t status = 0;
Unnati Gandhie3a5c0e2015-06-14 17:31:07 +0530190 uint32_t vol_up_gpio;
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530191
Unnati Gandhife004a92015-06-01 13:06:06 +0530192 if(platform_is_msm8956())
Unnati Gandhie3a5c0e2015-06-14 17:31:07 +0530193 vol_up_gpio = TLMM_VOL_UP_BTN_GPIO_8956;
Parth Dixit05f3c9f2016-03-18 17:14:57 +0530194 else if(platform_is_msm8937() || platform_is_msm8917())
Parth Dixit720d3b92015-10-30 01:21:34 +0530195 vol_up_gpio = TLMM_VOL_UP_BTN_GPIO_8937;
Unnati Gandhife004a92015-06-01 13:06:06 +0530196 else
Unnati Gandhie3a5c0e2015-06-14 17:31:07 +0530197 vol_up_gpio = TLMM_VOL_UP_BTN_GPIO;
198
lijuang2d2b8a02015-06-05 21:34:15 +0800199 if (!first_time) {
200 gpio_tlmm_config(vol_up_gpio, 0, GPIO_INPUT, GPIO_PULL_UP, GPIO_2MA, GPIO_ENABLE);
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530201
lijuang2d2b8a02015-06-05 21:34:15 +0800202 /* Wait for the gpio config to take effect - debounce time */
203 udelay(10000);
204
205 first_time = 1;
206 }
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530207
208 /* Get status of GPIO */
Unnati Gandhie3a5c0e2015-06-14 17:31:07 +0530209 status = gpio_status(vol_up_gpio);
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530210
Aparna Mallavarapufa5f8a72015-03-31 06:21:36 +0530211 /* Active low signal. */
Aparna Mallavarapudb938b62015-04-09 01:00:55 +0530212 return !status;
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530213}
214
215/* Return 1 if vol_down pressed */
216uint32_t target_volume_down()
217{
Wufengf2e37312016-04-12 16:09:47 +0800218 static bool vol_down_key_init = false;
219
220 if ((board_hardware_id() == HW_PLATFORM_QRD) &&
221 (board_hardware_subtype() == SUB_TYPE_SKUT)) {
222 uint32_t status = 0;
223
224 if (!vol_down_key_init) {
225 gpio_tlmm_config(TLMM_VOL_DOWN_BTN_GPIO, 0, GPIO_INPUT, GPIO_PULL_UP,
226 GPIO_2MA, GPIO_ENABLE);
227 /* Wait for the gpio config to take effect - debounce time */
228 thread_sleep(10);
229 vol_down_key_init = true;
230 }
231
232 /* Get status of GPIO */
233 status = gpio_status(TLMM_VOL_DOWN_BTN_GPIO);
234
235 /* Active low signal. */
236 return !status;
237 } else {
238 /* Volume down button tied in with PMIC RESIN. */
239 return pm8x41_resin_status();
240 }
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530241}
242
Parth Dixit300a3b92015-06-19 16:38:12 +0530243uint32_t target_is_pwrkey_pon_reason()
244{
245 uint8_t pon_reason = pm8950_get_pon_reason();
Vijay Kumar Pendoti0d698842016-09-23 16:23:16 +0530246 bool usb_present_sts = !(USBIN_UV_RT_STS &
247 pm8x41_reg_read(SMBCHG_USB_RT_STS));
Parth Dixit300a3b92015-06-19 16:38:12 +0530248 if (pm8x41_get_is_cold_boot() && ((pon_reason == KPDPWR_N) || (pon_reason == (KPDPWR_N|PON1))))
249 return 1;
Vijay Kumar Pendoti0d698842016-09-23 16:23:16 +0530250 else if ((pon_reason == PON1) && (!usb_present_sts))
251 return 1;
Parth Dixit300a3b92015-06-19 16:38:12 +0530252 else
253 return 0;
254}
255
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530256static void target_keystatus()
257{
258 keys_init();
259
260 if(target_volume_down())
261 keys_post_event(KEY_VOLUMEDOWN, 1);
262
263 if(target_volume_up())
264 keys_post_event(KEY_VOLUMEUP, 1);
265}
266
267/* Configure PMIC and Drop PS_HOLD for shutdown */
268void shutdown_device()
269{
270 dprintf(CRITICAL, "Going down for shutdown.\n");
271
272 /* Configure PMIC for shutdown */
273 pm8x41_reset_configure(PON_PSHOLD_SHUTDOWN);
274
275 /* Drop PS_HOLD for MSM */
276 writel(0x00, MPM2_MPM_PS_HOLD);
277
278 mdelay(5000);
279
280 dprintf(CRITICAL, "shutdown failed\n");
281
282 ASSERT(0);
283}
284
285
286void target_init(void)
287{
Parth Dixit5b954e02015-10-17 22:20:31 +0530288#if VERIFIED_BOOT
Parth Dixitb73ff342015-10-27 17:34:08 +0530289#if !VBOOT_MOTA
Parth Dixit6e6bad52015-07-30 19:02:38 +0530290 int ret = 0;
Parth Dixit5b954e02015-10-17 22:20:31 +0530291#endif
Parth Dixitb73ff342015-10-27 17:34:08 +0530292#endif
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530293 dprintf(INFO, "target_init()\n");
294
295 spmi_init(PMIC_ARB_CHANNEL_NUM, PMIC_ARB_OWNER_ID);
296
Parth Dixit550ddf32016-11-28 17:00:29 +0530297 if(target_is_pmi_enabled())
Parth Dixit3e6dead2015-12-08 15:04:54 +0530298 {
Parth Dixit550ddf32016-11-28 17:00:29 +0530299 if(platform_is_msm8937() || platform_is_msm8917())
Parth Dixit3e6dead2015-12-08 15:04:54 +0530300 {
Parth Dixit550ddf32016-11-28 17:00:29 +0530301 uint8_t pmi_rev = 0;
302 uint32_t pmi_type = 0;
303
304 pmi_type = board_pmic_target(1) & 0xffff;
305 if(pmi_type == PMIC_IS_PMI8950)
306 {
307 /* read pmic spare register for rev */
308 pmi_rev = pmi8950_get_pmi_subtype();
309 if(pmi_rev)
310 board_pmi_target_set(1,pmi_rev);
311 }
Parth Dixit3e6dead2015-12-08 15:04:54 +0530312 }
313 }
314
Parth Dixit550ddf32016-11-28 17:00:29 +0530315
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530316 target_keystatus();
317
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530318 target_sdc_init();
319 if (partition_read_table())
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530320 {
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530321 dprintf(CRITICAL, "Error reading the partition table info\n");
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530322 ASSERT(0);
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530323 }
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530324
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530325#if LONG_PRESS_POWER_ON
Parth Dixit550ddf32016-11-28 17:00:29 +0530326 if(target_is_pmi_enabled())
327 shutdown_detect();
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530328#endif
Matthew Qin47dfdb72015-06-10 21:29:11 +0800329
330#if PON_VIB_SUPPORT
331 /* turn on vibrator to indicate that phone is booting up to end user */
Parth Dixit550ddf32016-11-28 17:00:29 +0530332 if(target_is_pmi_enabled())
333 vib_timed_turn_on(VIBRATE_TIME);
Matthew Qin47dfdb72015-06-10 21:29:11 +0800334#endif
335
Aparna Mallavarapu1e8b0932015-03-29 23:38:13 +0530336 if (target_use_signed_kernel())
337 target_crypto_init_params();
Aparna Mallavarapufa5f8a72015-03-31 06:21:36 +0530338
Parth Dixit5b954e02015-10-17 22:20:31 +0530339#if VERIFIED_BOOT
Parth Dixitb73ff342015-10-27 17:34:08 +0530340#if !VBOOT_MOTA
Parth Dixit0eb73692015-08-09 17:32:27 +0530341 clock_ce_enable(CE1_INSTANCE);
342
Parth Dixit6e6bad52015-07-30 19:02:38 +0530343 /* Initialize Qseecom */
344 ret = qseecom_init();
345
346 if (ret < 0)
347 {
348 dprintf(CRITICAL, "Failed to initialize qseecom, error: %d\n", ret);
349 ASSERT(0);
350 }
351
352 /* Start Qseecom */
353 ret = qseecom_tz_init();
354
355 if (ret < 0)
356 {
357 dprintf(CRITICAL, "Failed to start qseecom, error: %d\n", ret);
358 ASSERT(0);
359 }
360
Parth Dixitb4b2ffa2015-10-09 15:31:14 +0530361 if (rpmb_init() < 0)
362 {
363 dprintf(CRITICAL, "RPMB init failed\n");
364 ASSERT(0);
365 }
366
Parth Dixit6e6bad52015-07-30 19:02:38 +0530367 /*
368 * Load the sec app for first time
369 */
370 if (load_sec_app() < 0)
371 {
372 dprintf(CRITICAL, "Failed to load App for verified\n");
373 ASSERT(0);
374 }
Parth Dixit5b954e02015-10-17 22:20:31 +0530375#endif
Parth Dixitb73ff342015-10-27 17:34:08 +0530376#endif
Parth Dixit5b954e02015-10-17 22:20:31 +0530377
Aparna Mallavarapufa5f8a72015-03-31 06:21:36 +0530378#if SMD_SUPPORT
379 rpm_smd_init();
380#endif
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530381}
382
383void target_serialno(unsigned char *buf)
384{
385 uint32_t serialno;
386 if (target_is_emmc_boot()) {
387 serialno = mmc_get_psn();
388 snprintf((char *)buf, 13, "%x", serialno);
389 }
390}
391
392unsigned board_machtype(void)
393{
Aparna Mallavarapue9bdacd2015-03-15 14:24:21 +0530394 return LINUX_MACHTYPE_UNKNOWN;
395}
396
397/* Detect the target type */
398void target_detect(struct board_data *board)
399{
400 /* This is already filled as part of board.c */
401}
402
403/* Detect the modem type */
404void target_baseband_detect(struct board_data *board)
405{
406 uint32_t platform;
407
408 platform = board->platform;
409
410 switch(platform) {
411 case MSM8952:
412 case MSM8956:
413 case MSM8976:
Parth Dixit4ec3fe22015-10-30 00:44:33 +0530414 case MSM8937:
Parth Dixit660369e2016-05-12 09:53:15 +0530415 case MSM8940:
Parth Dixit05f3c9f2016-03-18 17:14:57 +0530416 case MSM8917:
Mayank Grovercd5f0ff2016-10-03 18:08:52 +0530417 case MSM8920:
Parth Dixit05f3c9f2016-03-18 17:14:57 +0530418 case MSM8217:
419 case MSM8617:
Aparna Mallavarapue9bdacd2015-03-15 14:24:21 +0530420 board->baseband = BASEBAND_MSM;
421 break;
Aparna Mallavarapu815b3242015-04-29 11:08:14 +0530422 case APQ8052:
423 case APQ8056:
424 case APQ8076:
Parth Dixit4ec3fe22015-10-30 00:44:33 +0530425 case APQ8037:
Parth Dixit05f3c9f2016-03-18 17:14:57 +0530426 case APQ8017:
Aparna Mallavarapu815b3242015-04-29 11:08:14 +0530427 board->baseband = BASEBAND_APQ;
428 break;
Aparna Mallavarapue9bdacd2015-03-15 14:24:21 +0530429 default:
430 dprintf(CRITICAL, "Platform type: %u is not supported\n",platform);
431 ASSERT(0);
432 };
433}
434
435unsigned target_baseband()
436{
437 return board_baseband();
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530438}
439
440unsigned check_reboot_mode(void)
441{
442 uint32_t restart_reason = 0;
443
444 /* Read reboot reason and scrub it */
445 restart_reason = readl(RESTART_REASON_ADDR);
446 writel(0x00, RESTART_REASON_ADDR);
447
448 return restart_reason;
449}
450
451unsigned check_hard_reboot_mode(void)
452{
453 uint8_t hard_restart_reason = 0;
454 uint8_t value = 0;
455
456 /* Read reboot reason and scrub it
457 * Bit-5, bit-6 and bit-7 of SOFT_RB_SPARE for hard reset reason
458 */
459 value = pm8x41_reg_read(PON_SOFT_RB_SPARE);
460 hard_restart_reason = value >> 5;
461 pm8x41_reg_write(PON_SOFT_RB_SPARE, value & 0x1f);
462
463 return hard_restart_reason;
464}
465
lijuang395b5e62015-11-19 17:39:44 +0800466int set_download_mode(enum reboot_reason mode)
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530467{
468 int ret = 0;
469 ret = scm_dload_mode(mode);
470
471 pm8x41_clear_pmic_watchdog();
472
473 return ret;
474}
475
476int emmc_recovery_init(void)
477{
478 return _emmc_recovery_init();
479}
480
481void reboot_device(unsigned reboot_reason)
482{
483 uint8_t reset_type = 0;
484 uint32_t ret = 0;
485
lijuang395b5e62015-11-19 17:39:44 +0800486 /* Set cookie for dload mode */
487 if(set_download_mode(reboot_reason)) {
488 dprintf(CRITICAL, "HALT: set_download_mode not supported\n");
489 return;
490 }
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530491
492 writel(reboot_reason, RESTART_REASON_ADDR);
493
494 /* For Reboot-bootloader and Dload cases do a warm reset
495 * For Reboot cases do a hard reset
496 */
lijuang395b5e62015-11-19 17:39:44 +0800497 if((reboot_reason == FASTBOOT_MODE) || (reboot_reason == NORMAL_DLOAD) ||
498 (reboot_reason == EMERGENCY_DLOAD) || (reboot_reason == RECOVERY_MODE))
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530499 reset_type = PON_PSHOLD_WARM_RESET;
500 else
501 reset_type = PON_PSHOLD_HARD_RESET;
502
Parth Dixit550ddf32016-11-28 17:00:29 +0530503 if(target_is_pmi_enabled())
504 pm8994_reset_configure(reset_type);
505 else
506 pm8x41_reset_configure(reset_type);
507
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530508
509 ret = scm_halt_pmic_arbiter();
510 if (ret)
511 dprintf(CRITICAL , "Failed to halt pmic arbiter: %d\n", ret);
512
513 /* Drop PS_HOLD for MSM */
514 writel(0x00, MPM2_MPM_PS_HOLD);
515
516 mdelay(5000);
517
518 dprintf(CRITICAL, "Rebooting failed\n");
519}
520
521#if USER_FORCE_RESET_SUPPORT
522/* Return 1 if it is a force resin triggered by user. */
523uint32_t is_user_force_reset(void)
524{
525 uint8_t poff_reason1 = pm8x41_get_pon_poff_reason1();
526 uint8_t poff_reason2 = pm8x41_get_pon_poff_reason2();
527
528 dprintf(SPEW, "poff_reason1: %d\n", poff_reason1);
529 dprintf(SPEW, "poff_reason2: %d\n", poff_reason2);
530 if (pm8x41_get_is_cold_boot() && (poff_reason1 == KPDPWR_AND_RESIN ||
531 poff_reason2 == STAGE3))
532 return 1;
533 else
534 return 0;
535}
536#endif
537
538unsigned target_pause_for_battery_charge(void)
539{
540 uint8_t pon_reason = pm8x41_get_pon_reason();
541 uint8_t is_cold_boot = pm8x41_get_is_cold_boot();
Parth Dixit550ddf32016-11-28 17:00:29 +0530542 bool usb_present_sts = 1; /* don't care by default */
543
544 if(target_is_pmi_enabled())
545 usb_present_sts = (!(USBIN_UV_RT_STS &
546 pm8x41_reg_read(SMBCHG_USB_RT_STS)));
547
Zhenhua Huangb46b9b52015-04-21 19:53:09 +0800548 dprintf(INFO, "%s : pon_reason is:0x%x cold_boot:%d usb_sts:%d\n", __func__,
549 pon_reason, is_cold_boot, usb_present_sts);
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530550 /* In case of fastboot reboot,adb reboot or if we see the power key
551 * pressed we do not want go into charger mode.
552 * fastboot reboot is warm boot with PON hard reset bit not set
553 * adb reboot is a cold boot with PON hard reset bit set
554 */
555 if (is_cold_boot &&
556 (!(pon_reason & HARD_RST)) &&
557 (!(pon_reason & KPDPWR_N)) &&
Zhenhua Huangb46b9b52015-04-21 19:53:09 +0800558 usb_present_sts)
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530559 return 1;
560 else
561 return 0;
562}
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530563
564void target_uninit(void)
565{
c_wufeng8324c042016-01-25 10:37:37 +0800566#if PON_VIB_SUPPORT
Parth Dixit550ddf32016-11-28 17:00:29 +0530567 if(target_is_pmi_enabled())
568 turn_off_vib_early();
c_wufeng8324c042016-01-25 10:37:37 +0800569#endif
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530570 mmc_put_card_to_sleep(dev);
571 sdhci_mode_disable(&dev->host);
Aparna Mallavarapu1e8b0932015-03-29 23:38:13 +0530572 if (crypto_initialized())
Vijay Kumar Pendotib0f9ba32016-04-15 16:55:30 +0530573 {
Aparna Mallavarapu1e8b0932015-03-29 23:38:13 +0530574 crypto_eng_cleanup();
Vijay Kumar Pendotib0f9ba32016-04-15 16:55:30 +0530575 clock_ce_disable(CE1_INSTANCE);
576 }
Aparna Mallavarapu1e8b0932015-03-29 23:38:13 +0530577
578 if (target_is_ssd_enabled())
579 clock_ce_disable(CE1_INSTANCE);
Aparna Mallavarapufa5f8a72015-03-31 06:21:36 +0530580
Parth Dixit5b954e02015-10-17 22:20:31 +0530581#if VERIFIED_BOOT
Parth Dixitb73ff342015-10-27 17:34:08 +0530582#if !VBOOT_MOTA
Parth Dixit6e6bad52015-07-30 19:02:38 +0530583 if (is_sec_app_loaded())
584 {
585 if (send_milestone_call_to_tz() < 0)
586 {
587 dprintf(CRITICAL, "Failed to unload App for rpmb\n");
588 ASSERT(0);
589 }
590 }
591
592 if (rpmb_uninit() < 0)
593 {
594 dprintf(CRITICAL, "RPMB uninit failed\n");
595 ASSERT(0);
596 }
597
Parth Dixit0eb73692015-08-09 17:32:27 +0530598 clock_ce_disable(CE1_INSTANCE);
Parth Dixit5b954e02015-10-17 22:20:31 +0530599#endif
Parth Dixitb73ff342015-10-27 17:34:08 +0530600#endif
Parth Dixit5b954e02015-10-17 22:20:31 +0530601
Aparna Mallavarapufa5f8a72015-03-31 06:21:36 +0530602#if SMD_SUPPORT
603 rpm_smd_uninit();
604#endif
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530605}
606
607void target_usb_init(void)
608{
609 uint32_t val;
610
611 /* Select and enable external configuration with USB PHY */
612 ulpi_write(ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT, ULPI_MISC_A_SET);
613
614 /* Enable sess_vld */
615 val = readl(USB_GENCONFIG_2) | GEN2_SESS_VLD_CTRL_EN;
616 writel(val, USB_GENCONFIG_2);
617
618 /* Enable external vbus configuration in the LINK */
619 val = readl(USB_USBCMD);
620 val |= SESS_VLD_CTRL;
621 writel(val, USB_USBCMD);
622}
623
624void target_usb_stop(void)
625{
626 /* Disable VBUS mimicing in the controller. */
627 ulpi_write(ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT, ULPI_MISC_A_CLEAR);
628}
Aparna Mallavarapu1e8b0932015-03-29 23:38:13 +0530629
Padmanabhan Komanduru9d49f892015-04-10 12:58:46 -0700630static uint8_t splash_override;
631/* Returns 1 if target supports continuous splash screen. */
632int target_cont_splash_screen()
633{
634 uint8_t splash_screen = 0;
635 if (!splash_override) {
636 switch (board_hardware_id()) {
637 case HW_PLATFORM_MTP:
638 case HW_PLATFORM_SURF:
Vishnuvardhan Prodduturie116c002015-07-14 17:14:25 +0530639 case HW_PLATFORM_RCM:
feifanz174c82c2015-04-15 18:57:07 +0800640 case HW_PLATFORM_QRD:
Padmanabhan Komanduru9d49f892015-04-10 12:58:46 -0700641 splash_screen = 1;
642 break;
643 default:
644 splash_screen = 0;
645 break;
646 }
647 dprintf(SPEW, "Target_cont_splash=%d\n", splash_screen);
648 }
649 return splash_screen;
650}
651
652void target_force_cont_splash_disable(uint8_t override)
653{
654 splash_override = override;
655}
656
Ray Zhangf95f5b92015-06-25 15:34:29 +0800657uint8_t target_panel_auto_detect_enabled()
658{
659 uint8_t ret = 0;
660
661 switch(board_hardware_id())
662 {
663 case HW_PLATFORM_QRD:
664 ret = platform_is_msm8956() ? 1 : 0;
665 break;
666 case HW_PLATFORM_SURF:
667 case HW_PLATFORM_MTP:
668 default:
669 ret = 0;
670 }
671 return ret;
672}
673
Aparna Mallavarapu1e8b0932015-03-29 23:38:13 +0530674/* Do any target specific intialization needed before entering fastboot mode */
675void target_fastboot_init(void)
676{
677 if (target_is_ssd_enabled()) {
678 clock_ce_enable(CE1_INSTANCE);
679 target_load_ssd_keystore();
680 }
681}
682
683void target_load_ssd_keystore(void)
684{
685 uint64_t ptn;
686 int index;
687 uint64_t size;
688 uint32_t *buffer = NULL;
689
690 if (!target_is_ssd_enabled())
691 return;
692
693 index = partition_get_index("ssd");
694
695 ptn = partition_get_offset(index);
696 if (ptn == 0){
697 dprintf(CRITICAL, "Error: ssd partition not found\n");
698 return;
699 }
700
701 size = partition_get_size(index);
702 if (size == 0) {
703 dprintf(CRITICAL, "Error: invalid ssd partition size\n");
704 return;
705 }
706
707 buffer = memalign(CACHE_LINE, ROUNDUP(size, CACHE_LINE));
708 if (!buffer) {
709 dprintf(CRITICAL, "Error: allocating memory for ssd buffer\n");
710 return;
711 }
712
713 if (mmc_read(ptn, buffer, size)) {
714 dprintf(CRITICAL, "Error: cannot read data\n");
715 free(buffer);
716 return;
717 }
718
719 clock_ce_enable(CE1_INSTANCE);
720 scm_protect_keystore(buffer, size);
721 clock_ce_disable(CE1_INSTANCE);
722 free(buffer);
723}
724
725crypto_engine_type board_ce_type(void)
726{
727 return CRYPTO_ENGINE_TYPE_HW;
728}
729
730/* Set up params for h/w CE. */
731void target_crypto_init_params()
732{
733 struct crypto_init_params ce_params;
734
735 /* Set up base addresses and instance. */
736 ce_params.crypto_instance = CE1_INSTANCE;
737 ce_params.crypto_base = MSM_CE1_BASE;
738 ce_params.bam_base = MSM_CE1_BAM_BASE;
739
740 /* Set up BAM config. */
741 ce_params.bam_ee = CE_EE;
742 ce_params.pipes.read_pipe = CE_READ_PIPE;
743 ce_params.pipes.write_pipe = CE_WRITE_PIPE;
744 ce_params.pipes.read_pipe_grp = CE_READ_PIPE_LOCK_GRP;
745 ce_params.pipes.write_pipe_grp = CE_WRITE_PIPE_LOCK_GRP;
746
747 /* Assign buffer sizes. */
748 ce_params.num_ce = CE_ARRAY_SIZE;
749 ce_params.read_fifo_size = CE_FIFO_SIZE;
750 ce_params.write_fifo_size = CE_FIFO_SIZE;
751
752 /* BAM is initialized by TZ for this platform.
753 * Do not do it again as the initialization address space
754 * is locked.
755 */
756 ce_params.do_bam_init = 0;
757
758 crypto_init_params(&ce_params);
759}
lijuang3606df82015-09-02 21:14:43 +0800760
Parth Dixit550ddf32016-11-28 17:00:29 +0530761bool target_is_pmi_enabled(void)
762{
763 if(platform_is_msm8917() &&
Parth Dixitacec6bf2017-02-27 19:06:39 +0530764 (board_hardware_subtype() == HW_PLATFORM_SUBTYPE_SAP_NOPMI))
Parth Dixit550ddf32016-11-28 17:00:29 +0530765 return 0;
766 else
767 return 1;
768}
769
Parth Dixit17b85192016-12-28 15:51:33 +0530770#if _APPEND_CMDLINE
771int get_target_boot_params(const char *cmdline, const char *part, char **buf)
772{
773 int system_ptn_index = -1;
774 uint32_t buflen;
775 int ret = -1;
776
777 if (!cmdline || !part ) {
778 dprintf(CRITICAL, "WARN: Invalid input param\n");
779 return -1;
780 }
781
782 if (!strstr(cmdline, "root=/dev/ram")) /* This check is to handle kdev boot */
783 {
784 if (target_is_emmc_boot()) {
785 buflen = strlen(EXT4_CMDLINE) + sizeof(int) +1;
786 *buf = (char *)malloc(buflen);
787 if(!(*buf)) {
788 dprintf(CRITICAL,"Unable to allocate memory for boot params\n");
789 return -1;
790 }
791 /* Below is for emmc boot */
792 system_ptn_index = partition_get_index(part) + 1; /* Adding +1 as offsets for eMMC start at 1 and NAND at 0 */
793 if (system_ptn_index < 0) {
794 dprintf(CRITICAL,
795 "WARN: Cannot get partition index for %s\n", part);
796 free(*buf);
797 return -1;
798 }
799 snprintf(*buf, buflen, EXT4_CMDLINE"%d", system_ptn_index);
800 ret = 0;
801 }
802 }
803 /*in success case buf will be freed in the calling function of this*/
804 return ret;
805}
806#endif
807
lijuang3606df82015-09-02 21:14:43 +0800808uint32_t target_get_pmic()
809{
810 return PMIC_IS_PMI8950;
811}