blob: 9f10bd3fd38cd1930c6dc672777b51869bbaa649 [file] [log] [blame]
Channagoud Kadabif8ad8e72015-01-06 15:10:13 -08001/* Copyright (c) 2014-2015 The Linux Foundation. All rights reserved.
Channagoud Kadabied60a8b2014-06-27 15:35:09 -07002 *
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
Sridhar Parasuram568e7a62015-08-06 13:16:02 -070021 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, nit
22 * PROCUREMENT OF
Channagoud Kadabied60a8b2014-06-27 15:35:09 -070023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <debug.h>
31#include <platform/iomap.h>
32#include <platform/irqs.h>
33#include <platform/gpio.h>
34#include <reg.h>
35#include <target.h>
36#include <platform.h>
37#include <dload_util.h>
38#include <uart_dm.h>
39#include <mmc.h>
40#include <spmi.h>
41#include <board.h>
42#include <smem.h>
43#include <baseband.h>
Sridhar Parasuramd75ade52015-03-09 15:45:16 -070044#include <regulator.h>
Channagoud Kadabied60a8b2014-06-27 15:35:09 -070045#include <dev/keys.h>
46#include <pm8x41.h>
47#include <crypto5_wrapper.h>
48#include <clock.h>
49#include <partition_parser.h>
50#include <scm.h>
51#include <platform/clock.h>
52#include <platform/gpio.h>
53#include <platform/timer.h>
54#include <stdlib.h>
55#include <ufs.h>
56#include <boot_device.h>
57#include <qmp_phy.h>
Channagoud Kadabi7d308202014-12-22 12:07:04 -080058#include <sdhci_msm.h>
59#include <qusb2_phy.h>
Sridhar Parasuram568e7a62015-08-06 13:16:02 -070060#include <secapp_loader.h>
Channagoud Kadabi2bab29b2015-02-11 13:26:03 -080061#include <rpmb.h>
Sridhar Parasuram9f28f672015-03-17 15:40:47 -070062#include <rpm-glink.h>
Channagoud Kadabibb8f1f92015-04-27 11:14:45 -070063#if ENABLE_WBC
64#include <pm_app_smbchg.h>
65#endif
Channagoud Kadabied60a8b2014-06-27 15:35:09 -070066
Channagoud Kadabi4a870cf2015-01-21 10:39:01 -080067#define CE_INSTANCE 1
Channagoud Kadabi7edb9b42015-08-11 23:45:31 -070068#define CE_EE 0
Channagoud Kadabi4a870cf2015-01-21 10:39:01 -080069#define CE_FIFO_SIZE 64
70#define CE_READ_PIPE 3
71#define CE_WRITE_PIPE 2
72#define CE_READ_PIPE_LOCK_GRP 0
73#define CE_WRITE_PIPE_LOCK_GRP 0
74#define CE_ARRAY_SIZE 20
75
Channagoud Kadabied60a8b2014-06-27 15:35:09 -070076#define PMIC_ARB_CHANNEL_NUM 0
77#define PMIC_ARB_OWNER_ID 0
78
79static void set_sdc_power_ctrl(void);
80static uint32_t mmc_pwrctl_base[] =
81 { MSM_SDC1_BASE, MSM_SDC2_BASE };
82
83static uint32_t mmc_sdhci_base[] =
84 { MSM_SDC1_SDHCI_BASE, MSM_SDC2_SDHCI_BASE };
85
86static uint32_t mmc_sdc_pwrctl_irq[] =
87 { SDCC1_PWRCTL_IRQ, SDCC2_PWRCTL_IRQ };
88
89struct mmc_device *dev;
90struct ufs_dev ufs_device;
91
Channagoud Kadabied60a8b2014-06-27 15:35:09 -070092void target_early_init(void)
93{
94#if WITH_DEBUG_UART
Channagoud Kadabi35503c42014-11-14 16:22:43 -080095 uart_dm_init(8, 0, BLSP2_UART1_BASE);
Channagoud Kadabied60a8b2014-06-27 15:35:09 -070096#endif
97}
98
99/* Return 1 if vol_up pressed */
Amit Blay6a3e88b2015-06-23 22:25:06 +0300100int target_volume_up()
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700101{
lijuang2d2b8a02015-06-05 21:34:15 +0800102 static uint8_t first_time = 0;
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700103 uint8_t status = 0;
104 struct pm8x41_gpio gpio;
105
lijuang2d2b8a02015-06-05 21:34:15 +0800106 if (!first_time) {
107 /* Configure the GPIO */
108 gpio.direction = PM_GPIO_DIR_IN;
109 gpio.function = 0;
110 gpio.pull = PM_GPIO_PULL_UP_30;
111 gpio.vin_sel = 2;
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700112
lijuang2d2b8a02015-06-05 21:34:15 +0800113 pm8x41_gpio_config(2, &gpio);
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700114
lijuang2d2b8a02015-06-05 21:34:15 +0800115 /* Wait for the pmic gpio config to take effect */
116 udelay(10000);
117
118 first_time = 1;
119 }
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700120
121 /* Get status of P_GPIO_5 */
Channagoud Kadabi99d23702015-02-02 20:52:17 -0800122 pm8x41_gpio_get(2, &status);
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700123
124 return !status; /* active low */
125}
126
127/* Return 1 if vol_down pressed */
128uint32_t target_volume_down()
129{
130 return pm8x41_resin_status();
131}
132
133static void target_keystatus()
134{
135 keys_init();
136
137 if(target_volume_down())
138 keys_post_event(KEY_VOLUMEDOWN, 1);
139
140 if(target_volume_up())
141 keys_post_event(KEY_VOLUMEUP, 1);
142}
143
144void target_uninit(void)
145{
146 if (platform_boot_dev_isemmc())
147 {
148 mmc_put_card_to_sleep(dev);
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700149 }
Channagoud Kadabi2bab29b2015-02-11 13:26:03 -0800150
151 if (is_sec_app_loaded())
152 {
Sridhar Parasuram568e7a62015-08-06 13:16:02 -0700153 if (send_milestone_call_to_tz() < 0)
Channagoud Kadabi2bab29b2015-02-11 13:26:03 -0800154 {
155 dprintf(CRITICAL, "Failed to unload App for rpmb\n");
156 ASSERT(0);
157 }
158 }
159
Channagoud Kadabibb8f1f92015-04-27 11:14:45 -0700160#if ENABLE_WBC
Channagoud Kadabi22165612015-07-22 14:04:37 -0700161 if (board_hardware_id() == HW_PLATFORM_MTP)
162 pm_appsbl_set_dcin_suspend(1);
Channagoud Kadabibb8f1f92015-04-27 11:14:45 -0700163#endif
164
Channagoud Kadabi7edb9b42015-08-11 23:45:31 -0700165
166 if (crypto_initialized())
167 {
168 crypto_eng_cleanup();
169 clock_ce_disable(CE_INSTANCE);
170 }
171
Sridhar Parasuram9f28f672015-03-17 15:40:47 -0700172 /* Tear down glink channels */
173 rpm_glink_uninit();
174
Channagoud Kadabi2bab29b2015-02-11 13:26:03 -0800175 if (rpmb_uninit() < 0)
176 {
177 dprintf(CRITICAL, "RPMB uninit failed\n");
178 ASSERT(0);
179 }
180
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700181}
182
183static void set_sdc_power_ctrl()
184{
185 /* Drive strength configs for sdc pins */
186 struct tlmm_cfgs sdc1_hdrv_cfg[] =
187 {
188 { SDC1_CLK_HDRV_CTL_OFF, TLMM_CUR_VAL_16MA, TLMM_HDRV_MASK, SDC1_HDRV_PULL_CTL },
189 { SDC1_CMD_HDRV_CTL_OFF, TLMM_CUR_VAL_10MA, TLMM_HDRV_MASK, SDC1_HDRV_PULL_CTL },
190 { SDC1_DATA_HDRV_CTL_OFF, TLMM_CUR_VAL_10MA, TLMM_HDRV_MASK, SDC1_HDRV_PULL_CTL },
191 };
192
193 /* Pull configs for sdc pins */
194 struct tlmm_cfgs sdc1_pull_cfg[] =
195 {
196 { SDC1_CLK_PULL_CTL_OFF, TLMM_NO_PULL, TLMM_PULL_MASK, SDC1_HDRV_PULL_CTL },
197 { SDC1_CMD_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, SDC1_HDRV_PULL_CTL },
198 { SDC1_DATA_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, SDC1_HDRV_PULL_CTL },
199 };
200
201 struct tlmm_cfgs sdc1_rclk_cfg[] =
202 {
203 { SDC1_RCLK_PULL_CTL_OFF, TLMM_PULL_DOWN, TLMM_PULL_MASK, SDC1_HDRV_PULL_CTL },
204 };
205
206 /* Set the drive strength & pull control values */
207 tlmm_set_hdrive_ctrl(sdc1_hdrv_cfg, ARRAY_SIZE(sdc1_hdrv_cfg));
208 tlmm_set_pull_ctrl(sdc1_pull_cfg, ARRAY_SIZE(sdc1_pull_cfg));
209 tlmm_set_pull_ctrl(sdc1_rclk_cfg, ARRAY_SIZE(sdc1_rclk_cfg));
210}
211
212void target_sdc_init()
213{
214 struct mmc_config_data config = {0};
215
216 /* Set drive strength & pull ctrl values */
217 set_sdc_power_ctrl();
218
219 config.bus_width = DATA_BUS_WIDTH_8BIT;
220 config.max_clk_rate = MMC_CLK_192MHZ;
Channagoud Kadabi99d23702015-02-02 20:52:17 -0800221 config.hs400_support = 1;
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700222
223 /* Try slot 1*/
224 config.slot = 1;
225 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
226 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
227 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
228
229 if (!(dev = mmc_init(&config)))
230 {
231 /* Try slot 2 */
232 config.slot = 2;
233 config.max_clk_rate = MMC_CLK_200MHZ;
234 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
235 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
236 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
237
238 if (!(dev = mmc_init(&config)))
239 {
240 dprintf(CRITICAL, "mmc init failed!");
241 ASSERT(0);
242 }
243 }
244}
245
246void *target_mmc_device()
247{
248 if (platform_boot_dev_isemmc())
249 return (void *) dev;
250 else
251 return (void *) &ufs_device;
252}
253
254void target_init(void)
255{
Sridhar Parasuram568e7a62015-08-06 13:16:02 -0700256 int ret = 0;
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700257 dprintf(INFO, "target_init()\n");
258
Sridhar Parasuram1d224322015-06-15 11:03:40 -0700259 pmic_info_populate();
260
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700261 spmi_init(PMIC_ARB_CHANNEL_NUM, PMIC_ARB_OWNER_ID);
262
Channagoud Kadabibb8f1f92015-04-27 11:14:45 -0700263 /* Initialize Glink */
264 rpm_glink_init();
265
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700266 target_keystatus();
267
268 if (target_use_signed_kernel())
269 target_crypto_init_params();
270
271 platform_read_boot_config();
272
Sridhar Parasuram50b9d962015-02-12 11:28:09 -0800273#ifdef MMC_SDHCI_SUPPORT
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700274 if (platform_boot_dev_isemmc())
275 {
276 target_sdc_init();
277 }
Sridhar Parasuram50b9d962015-02-12 11:28:09 -0800278#endif
279#ifdef UFS_SUPPORT
280 if (!platform_boot_dev_isemmc())
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700281 {
282 ufs_device.base = UFS_BASE;
283 ufs_init(&ufs_device);
284 }
Sridhar Parasuram50b9d962015-02-12 11:28:09 -0800285#endif
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700286
287 /* Storage initialization is complete, read the partition table info */
Channagoud Kadabi58a273b2015-02-10 12:56:22 -0800288 mmc_read_partition_table(0);
Channagoud Kadabi2bab29b2015-02-11 13:26:03 -0800289
Channagoud Kadabi1171d8d2015-07-30 19:12:44 -0700290#if ENABLE_WBC
291 /* Look for battery voltage and make sure we have enough to bootup
292 * Otherwise initiate battery charging
293 * Charging should happen as early as possible, any other driver
294 * initialization before this should consider the power impact
295 */
Channagoud Kadabi25fd8ce2015-08-19 14:45:08 -0700296 switch(board_hardware_id())
297 {
298 case HW_PLATFORM_MTP:
299 case HW_PLATFORM_FLUID:
lijuang0e98add2015-11-10 10:40:27 +0800300 case HW_PLATFORM_QRD:
Channagoud Kadabi25fd8ce2015-08-19 14:45:08 -0700301 pm_appsbl_chg_check_weak_battery_status(1);
302 break;
303 default:
304 /* Charging not supported */
305 break;
306 };
Channagoud Kadabi1171d8d2015-07-30 19:12:44 -0700307#endif
308
Sridhar Parasuram568e7a62015-08-06 13:16:02 -0700309 /* Initialize Qseecom */
310 ret = qseecom_init();
311
312 if (ret < 0)
313 {
314 dprintf(CRITICAL, "Failed to initialize qseecom, error: %d\n", ret);
315 ASSERT(0);
316 }
317
318 /* Start Qseecom */
319 ret = qseecom_tz_init();
320
321 if (ret < 0)
322 {
323 dprintf(CRITICAL, "Failed to start qseecom, error: %d\n", ret);
324 ASSERT(0);
325 }
326
Sridhar Parasuramc61ecc22015-09-22 13:53:31 -0700327 if (rpmb_init() < 0)
328 {
329 dprintf(CRITICAL, "RPMB init failed\n");
330 ASSERT(0);
331 }
332
Sridhar Parasuram568e7a62015-08-06 13:16:02 -0700333 /*
334 * Load the sec app for first time
335 */
336 if (load_sec_app() < 0)
337 {
338 dprintf(CRITICAL, "Failed to load App for verified\n");
339 ASSERT(0);
340 }
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700341}
342
343unsigned board_machtype(void)
344{
345 return LINUX_MACHTYPE_UNKNOWN;
346}
347
348/* Detect the target type */
349void target_detect(struct board_data *board)
350{
351 /* This is filled from board.c */
352}
353
Dhaval Patelb95039c2015-03-16 11:14:06 -0700354static uint8_t splash_override;
355/* Returns 1 if target supports continuous splash screen. */
356int target_cont_splash_screen()
357{
358 uint8_t splash_screen = 0;
Channagoud Kadabi25fd8ce2015-08-19 14:45:08 -0700359 if(!splash_override && !pm_appsbl_charging_in_progress()) {
Dhaval Patelb95039c2015-03-16 11:14:06 -0700360 switch(board_hardware_id())
361 {
362 case HW_PLATFORM_SURF:
363 case HW_PLATFORM_MTP:
364 case HW_PLATFORM_FLUID:
feifanz76fe6482015-09-02 15:25:16 +0800365 case HW_PLATFORM_QRD:
Kuogee Hsiehb976dfc2015-08-28 13:21:30 -0700366 case HW_PLATFORM_LIQUID:
Dhaval Patelb95039c2015-03-16 11:14:06 -0700367 dprintf(SPEW, "Target_cont_splash=1\n");
368 splash_screen = 1;
369 break;
370 default:
371 dprintf(SPEW, "Target_cont_splash=0\n");
372 splash_screen = 0;
373 }
374 }
375 return splash_screen;
376}
377
378void target_force_cont_splash_disable(uint8_t override)
379{
380 splash_override = override;
381}
382
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700383/* Detect the modem type */
384void target_baseband_detect(struct board_data *board)
385{
386 uint32_t platform;
387
388 platform = board->platform;
389
390 switch(platform) {
Channagoud Kadabi439df822015-05-26 11:14:16 -0700391 case APQ8096:
392 board->baseband = BASEBAND_APQ;
393 break;
Channagoud Kadabi4a4c05e2015-03-30 15:18:58 -0700394 case MSM8996:
Channagoud Kadabi99d23702015-02-02 20:52:17 -0800395 if (board->platform_version == 0x10000)
396 board->baseband = BASEBAND_APQ;
397 else
398 board->baseband = BASEBAND_MSM;
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700399 break;
400 default:
401 dprintf(CRITICAL, "Platform type: %u is not supported\n",platform);
402 ASSERT(0);
403 };
404}
405unsigned target_baseband()
406{
407 return board_baseband();
408}
409
410void target_serialno(unsigned char *buf)
411{
412 unsigned int serialno;
413 if (target_is_emmc_boot()) {
414 serialno = mmc_get_psn();
415 snprintf((char *)buf, 13, "%x", serialno);
416 }
417}
418
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700419int emmc_recovery_init(void)
420{
421 return _emmc_recovery_init();
422}
423
424void target_usb_phy_reset()
425{
426 usb30_qmp_phy_reset();
427 qusb2_phy_reset();
428}
429
430target_usb_iface_t* target_usb30_init()
431{
432 target_usb_iface_t *t_usb_iface;
433
434 t_usb_iface = calloc(1, sizeof(target_usb_iface_t));
435 ASSERT(t_usb_iface);
436
437 t_usb_iface->phy_init = usb30_qmp_phy_init;
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700438 t_usb_iface->phy_reset = target_usb_phy_reset;
439 t_usb_iface->clock_init = clock_usb30_init;
440 t_usb_iface->vbus_override = 1;
441
442 return t_usb_iface;
443}
444
445/* identify the usb controller to be used for the target */
446const char * target_usb_controller()
447{
448 return "dwc";
449}
450
451uint32_t target_override_pll()
452{
Channagoud Kadabi1e5144b2015-04-28 17:15:05 -0700453 if (board_soc_version() >= 0x20000)
454 return 0;
455 else
456 return 1;
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700457}
458
Channagoud Kadabi4a870cf2015-01-21 10:39:01 -0800459crypto_engine_type board_ce_type(void)
460{
Channagoud Kadabi7edb9b42015-08-11 23:45:31 -0700461 return CRYPTO_ENGINE_TYPE_HW;
Channagoud Kadabi4a870cf2015-01-21 10:39:01 -0800462}
463
464/* Set up params for h/w CE. */
465void target_crypto_init_params()
466{
467 struct crypto_init_params ce_params;
468
469 /* Set up base addresses and instance. */
470 ce_params.crypto_instance = CE_INSTANCE;
471 ce_params.crypto_base = MSM_CE_BASE;
472 ce_params.bam_base = MSM_CE_BAM_BASE;
473
474 /* Set up BAM config. */
475 ce_params.bam_ee = CE_EE;
476 ce_params.pipes.read_pipe = CE_READ_PIPE;
477 ce_params.pipes.write_pipe = CE_WRITE_PIPE;
478 ce_params.pipes.read_pipe_grp = CE_READ_PIPE_LOCK_GRP;
479 ce_params.pipes.write_pipe_grp = CE_WRITE_PIPE_LOCK_GRP;
480
481 /* Assign buffer sizes. */
482 ce_params.num_ce = CE_ARRAY_SIZE;
483 ce_params.read_fifo_size = CE_FIFO_SIZE;
484 ce_params.write_fifo_size = CE_FIFO_SIZE;
485
486 /* BAM is initialized by TZ for this platform.
487 * Do not do it again as the initialization address space
488 * is locked.
489 */
490 ce_params.do_bam_init = 0;
491
492 crypto_init_params(&ce_params);
493}
Channagoud Kadabi083290f2015-03-13 14:18:38 -0700494
495unsigned target_pause_for_battery_charge(void)
496{
497 uint8_t pon_reason = pm8x41_get_pon_reason();
498 uint8_t is_cold_boot = pm8x41_get_is_cold_boot();
499 dprintf(INFO, "%s : pon_reason is %d cold_boot:%d\n", __func__,
500 pon_reason, is_cold_boot);
501 /* 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)) &&
509 ((pon_reason & PON1)))
510 return 1;
511 else
512 return 0;
513}
Channagoud Kadabi23edc0c2015-03-27 18:31:32 -0700514
515int set_download_mode(enum dload_mode mode)
516{
517 int ret = 0;
518 ret = scm_dload_mode(mode);
519
520 return ret;
521}
Channagoud Kadabiffc4b902015-06-25 23:14:27 -0700522
Channagoud Kadabi65b518d2015-08-05 16:17:14 -0700523void pmic_reset_configure(uint8_t reset_type)
Channagoud Kadabiffc4b902015-06-25 23:14:27 -0700524{
Channagoud Kadabi65b518d2015-08-05 16:17:14 -0700525 pm8994_reset_configure(reset_type);
Channagoud Kadabiffc4b902015-06-25 23:14:27 -0700526}
lijuang3606df82015-09-02 21:14:43 +0800527
528uint32_t target_get_pmic()
529{
530 return PMIC_IS_PMI8996;
531}