blob: dba1ed3cc90c6a5b98858ee45c7d84b2d49af08d [file] [log] [blame]
lijuangc518f5d2015-02-10 10:22:53 +08001/* Copyright (c) 2014-2015, The Linux Foundation. All rights reserved.
Aparna Mallavarapufe1f3d12013-10-19 15:05:30 +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>
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -080036#include <platform/gpio.h>
37#include <dev/keys.h>
38#include <spmi_v2.h>
39#include <pm8x41.h>
Aparna Mallavarapufe1f3d12013-10-19 15:05:30 +053040#include <board.h>
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -080041#include <baseband.h>
42#include <hsusb.h>
Aparna Mallavarapuacb6ede2014-03-21 19:22:00 +053043#include <scm.h>
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -080044#include <platform/gpio.h>
45#include <platform/gpio.h>
46#include <platform/irqs.h>
Aparna Mallavarapu7c8e75f2014-04-22 20:20:28 +053047#include <platform/clock.h>
48#include <crypto5_wrapper.h>
49#include <partition_parser.h>
50#include <stdlib.h>
Aparna Mallavarapufe1f3d12013-10-19 15:05:30 +053051
Matthew Qinf3ebf182014-04-08 11:38:14 +080052#if LONG_PRESS_POWER_ON
53#include <shutdown_detect.h>
54#endif
55
Matthew Qin7f5ab932014-04-08 15:25:54 +080056#if PON_VIB_SUPPORT
57#include <vibrator.h>
58#endif
59
Aparna Mallavarapufe1f3d12013-10-19 15:05:30 +053060#define PMIC_ARB_CHANNEL_NUM 0
61#define PMIC_ARB_OWNER_ID 0
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -080062#define TLMM_VOL_UP_BTN_GPIO 107
Aparna Mallavarapufe1f3d12013-10-19 15:05:30 +053063
Matthew Qin7f5ab932014-04-08 15:25:54 +080064#if PON_VIB_SUPPORT
65#define VIBRATE_TIME 250
66#endif
67
Aparna Mallavarapu7c8e75f2014-04-22 20:20:28 +053068#define CE1_INSTANCE 1
69#define CE_EE 1
70#define CE_FIFO_SIZE 64
71#define CE_READ_PIPE 3
72#define CE_WRITE_PIPE 2
73#define CE_READ_PIPE_LOCK_GRP 0
74#define CE_WRITE_PIPE_LOCK_GRP 0
75#define CE_ARRAY_SIZE 20
76
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -080077static void set_sdc_power_ctrl(void);
78
79struct mmc_device *dev;
80
81static uint32_t mmc_pwrctl_base[] =
Aparna Mallavarapufe1f3d12013-10-19 15:05:30 +053082 { MSM_SDC1_BASE, MSM_SDC2_BASE };
83
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -080084static uint32_t mmc_sdhci_base[] =
85 { MSM_SDC1_SDHCI_BASE, MSM_SDC2_SDHCI_BASE };
86
87static uint32_t mmc_sdc_pwrctl_irq[] =
88 { SDCC1_PWRCTL_IRQ, SDCC2_PWRCTL_IRQ };
89
Aparna Mallavarapufe1f3d12013-10-19 15:05:30 +053090void target_early_init(void)
91{
92#if WITH_DEBUG_UART
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -080093 uart_dm_init(2, 0, BLSP1_UART1_BASE);
Aparna Mallavarapufe1f3d12013-10-19 15:05:30 +053094#endif
95}
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -080096
97void target_sdc_init()
Aparna Mallavarapufe1f3d12013-10-19 15:05:30 +053098{
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -080099 struct mmc_config_data config;
100
101 /* Set drive strength & pull ctrl values */
102 set_sdc_power_ctrl();
103
104 config.bus_width = DATA_BUS_WIDTH_8BIT;
Aparna Mallavarapu53b09402014-03-26 14:46:43 +0530105 config.max_clk_rate = MMC_CLK_177MHZ;
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -0800106
107 /* Try slot 1*/
108 config.slot = 1;
109 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
110 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
111 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
112 config.hs400_support = 0;
113
114 if (!(dev = mmc_init(&config))) {
115 /* Try slot 2 */
116 config.slot = 2;
Aparna Mallavarapu53b09402014-03-26 14:46:43 +0530117 config.max_clk_rate = MMC_CLK_200MHZ;
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -0800118 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
119 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
120 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
121
122 if (!(dev = mmc_init(&config))) {
123 dprintf(CRITICAL, "mmc init failed!");
124 ASSERT(0);
125 }
126 }
Aparna Mallavarapufe1f3d12013-10-19 15:05:30 +0530127}
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -0800128
129void *target_mmc_device()
130{
131 return (void *) dev;
132}
133
134/* Return 1 if vol_up pressed */
135static int target_volume_up()
136{
137 uint8_t status = 0;
138
139 gpio_tlmm_config(TLMM_VOL_UP_BTN_GPIO, 0, GPIO_INPUT, GPIO_PULL_UP, GPIO_2MA, GPIO_ENABLE);
140
141 /* Wait for the gpio config to take effect - debounce time */
142 thread_sleep(10);
143
144 /* Get status of GPIO */
145 status = gpio_status(TLMM_VOL_UP_BTN_GPIO);
146
147 /* Active low signal. */
148 return !status;
149}
150
151/* Return 1 if vol_down pressed */
152uint32_t target_volume_down()
153{
154 /* Volume down button tied in with PMIC RESIN. */
155 return pm8x41_resin_status();
156}
157
Aparna Mallavarapufe1f3d12013-10-19 15:05:30 +0530158static void target_keystatus()
159{
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -0800160 keys_init();
161
162 if(target_volume_down())
163 keys_post_event(KEY_VOLUMEDOWN, 1);
164
165 if(target_volume_up())
166 keys_post_event(KEY_VOLUMEUP, 1);
Aparna Mallavarapufe1f3d12013-10-19 15:05:30 +0530167}
168
169void target_init(void)
170{
171 uint32_t base_addr;
172 uint8_t slot;
173
174 dprintf(INFO, "target_init()\n");
175
176 spmi_init(PMIC_ARB_CHANNEL_NUM, PMIC_ARB_OWNER_ID);
177
178 target_keystatus();
179
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -0800180 target_sdc_init();
181 if (partition_read_table())
Aparna Mallavarapufe1f3d12013-10-19 15:05:30 +0530182 {
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -0800183 dprintf(CRITICAL, "Error reading the partition table info\n");
Aparna Mallavarapufe1f3d12013-10-19 15:05:30 +0530184 ASSERT(0);
185 }
Matthew Qinf3ebf182014-04-08 11:38:14 +0800186
187#if LONG_PRESS_POWER_ON
188 shutdown_detect();
189#endif
Matthew Qin7f5ab932014-04-08 15:25:54 +0800190
191#if PON_VIB_SUPPORT
192 /* turn on vibrator to indicate that phone is booting up to end user */
193 vib_timed_turn_on(VIBRATE_TIME);
194#endif
Aparna Mallavarapu7c8e75f2014-04-22 20:20:28 +0530195
196 if (target_use_signed_kernel())
197 target_crypto_init_params();
Aparna Mallavarapufe1f3d12013-10-19 15:05:30 +0530198}
199
200void target_serialno(unsigned char *buf)
201{
202 uint32_t serialno;
203 if (target_is_emmc_boot()) {
204 serialno = mmc_get_psn();
205 snprintf((char *)buf, 13, "%x", serialno);
206 }
207}
208
209unsigned board_machtype(void)
210{
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -0800211 return LINUX_MACHTYPE_UNKNOWN;
212}
213
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -0800214/* Detect the target type */
215void target_detect(struct board_data *board)
216{
217 /*
218 * already fill the board->target on board.c
219 */
220}
221
222void target_baseband_detect(struct board_data *board)
223{
224 uint32_t platform;
225
226 platform = board->platform;
227 switch(platform)
228 {
229 case MSM8916:
Aparna Mallavarapu9b482a82014-06-02 21:18:34 +0530230 case MSM8116:
231 case MSM8216:
232 case MSM8616:
Aparna Mallavarapud81c99e2014-04-20 23:32:51 +0530233 case MSM8939:
234 case MSM8236:
235 case MSM8636:
Unnati Gandhiad17b722014-06-11 23:04:54 +0530236 case MSM8936:
Aparna Mallavarapu36cae9d2014-08-04 12:51:10 +0530237 case MSM8239:
vijay kumardd51c592015-01-05 12:46:28 +0530238 case MSM8929:
239 case MSM8629:
240 case MSM8229:
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -0800241 board->baseband = BASEBAND_MSM;
242 break;
Aparna Mallavarapu9b482a82014-06-02 21:18:34 +0530243 case APQ8016:
Aparna Mallavarapud81c99e2014-04-20 23:32:51 +0530244 case APQ8039:
245 case APQ8036:
vijay kumardd51c592015-01-05 12:46:28 +0530246 case APQ8029:
Aparna Mallavarapud81c99e2014-04-20 23:32:51 +0530247 board->baseband = BASEBAND_APQ;
248 break;
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -0800249 default:
250 dprintf(CRITICAL, "Platform type: %u is not supported\n", platform);
251 ASSERT(0);
252 };
253}
254
255unsigned target_baseband()
256{
257 return board_baseband();
258}
259
260int emmc_recovery_init(void)
261{
262 return _emmc_recovery_init();
263}
264
265static void set_sdc_power_ctrl()
266{
267 /* Drive strength configs for sdc pins */
268 struct tlmm_cfgs sdc1_hdrv_cfg[] =
269 {
vijay kumar34f54102014-10-27 21:55:47 +0530270 { SDC1_CLK_HDRV_CTL_OFF, TLMM_CUR_VAL_16MA, TLMM_HDRV_MASK, 0},
271 { SDC1_CMD_HDRV_CTL_OFF, TLMM_CUR_VAL_10MA, TLMM_HDRV_MASK, 0},
vijay kumarf1d659c2015-03-17 19:54:14 +0530272 { SDC1_DATA_HDRV_CTL_OFF, TLMM_CUR_VAL_10MA, TLMM_HDRV_MASK , 0},
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -0800273 };
274
275 /* Pull configs for sdc pins */
276 struct tlmm_cfgs sdc1_pull_cfg[] =
277 {
vijay kumar34f54102014-10-27 21:55:47 +0530278 { SDC1_CLK_PULL_CTL_OFF, TLMM_NO_PULL, TLMM_PULL_MASK, 0},
279 { SDC1_CMD_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, 0},
280 { SDC1_DATA_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, 0},
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -0800281 };
282
283 /* Set the drive strength & pull control values */
284 tlmm_set_hdrive_ctrl(sdc1_hdrv_cfg, ARRAY_SIZE(sdc1_hdrv_cfg));
285 tlmm_set_pull_ctrl(sdc1_pull_cfg, ARRAY_SIZE(sdc1_pull_cfg));
286}
287
288void target_usb_init(void)
289{
290 uint32_t val;
291
292 /* Select and enable external configuration with USB PHY */
293 ulpi_write(ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT, ULPI_MISC_A_SET);
294
295 /* Enable sess_vld */
296 val = readl(USB_GENCONFIG_2) | GEN2_SESS_VLD_CTRL_EN;
297 writel(val, USB_GENCONFIG_2);
298
299 /* Enable external vbus configuration in the LINK */
300 val = readl(USB_USBCMD);
301 val |= SESS_VLD_CTRL;
302 writel(val, USB_USBCMD);
303}
304
Padmanabhan Komanduru1869a762014-04-01 20:12:05 +0530305uint8_t target_panel_auto_detect_enabled()
306{
307 uint8_t ret = 0;
308 uint32_t hw_subtype = board_hardware_subtype();
309
310 switch(board_hardware_id()) {
311 case HW_PLATFORM_SURF:
Vineet Bajaj5cd5b112015-02-09 16:13:30 +0530312 case HW_PLATFORM_MTP:
Padmanabhan Komanduru1869a762014-04-01 20:12:05 +0530313 ret = 1;
314 break;
315 default:
316 ret = 0;
317 break;
318 }
319 return ret;
320}
321
322static uint8_t splash_override;
Padmanabhan Komandurucd5645e2014-03-25 20:34:18 +0530323/* Returns 1 if target supports continuous splash screen. */
324int target_cont_splash_screen()
325{
326 uint8_t splash_screen = 0;
Padmanabhan Komanduru1869a762014-04-01 20:12:05 +0530327 if (!splash_override) {
328 switch (board_hardware_id()) {
Padmanabhan Komandurucd5645e2014-03-25 20:34:18 +0530329 case HW_PLATFORM_MTP:
330 case HW_PLATFORM_SURF:
Mao Flynn81409472014-04-10 15:01:30 +0800331 case HW_PLATFORM_QRD:
Padmanabhan Komandurucd5645e2014-03-25 20:34:18 +0530332 splash_screen = 1;
333 break;
334 default:
Padmanabhan Komandurucd5645e2014-03-25 20:34:18 +0530335 splash_screen = 0;
336 break;
Padmanabhan Komanduru1869a762014-04-01 20:12:05 +0530337 }
338 dprintf(SPEW, "Target_cont_splash=%d\n", splash_screen);
Padmanabhan Komandurucd5645e2014-03-25 20:34:18 +0530339 }
Padmanabhan Komanduru1869a762014-04-01 20:12:05 +0530340 return splash_screen;
341}
342
343void target_force_cont_splash_disable(uint8_t override)
344{
345 splash_override = override;
Padmanabhan Komandurucd5645e2014-03-25 20:34:18 +0530346}
347
Zhenhua Huang9b8cb1c2014-04-11 15:23:05 +0800348unsigned target_pause_for_battery_charge(void)
349{
350 uint8_t pon_reason = pm8x41_get_pon_reason();
351 uint8_t is_cold_boot = pm8x41_get_is_cold_boot();
352 dprintf(INFO, "%s : pon_reason is %d cold_boot:%d\n", __func__,
353 pon_reason, is_cold_boot);
354 /* In case of fastboot reboot,adb reboot or if we see the power key
355 * pressed we do not want go into charger mode.
356 * fastboot reboot is warm boot with PON hard reset bit not set
357 * adb reboot is a cold boot with PON hard reset bit set
358 */
359 if (is_cold_boot &&
360 (!(pon_reason & HARD_RST)) &&
361 (!(pon_reason & KPDPWR_N)) &&
Zhenhua Huangd01ef5f2015-02-16 11:22:10 +0800362 ((pon_reason & USB_CHG) || (pon_reason & DC_CHG) || (pon_reason & CBLPWR_N)))
Zhenhua Huang9b8cb1c2014-04-11 15:23:05 +0800363 return 1;
364 else
365 return 0;
366}
367
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -0800368void target_usb_stop(void)
369{
370 /* Disable VBUS mimicing in the controller. */
371 ulpi_write(ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT, ULPI_MISC_A_CLEAR);
372}
373
374
375void target_uninit(void)
376{
Matthew Qin7f5ab932014-04-08 15:25:54 +0800377#if PON_VIB_SUPPORT
378 /* wait for the vibrator timer is expried */
379 wait_vib_timeout();
380#endif
381
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -0800382 mmc_put_card_to_sleep(dev);
383 sdhci_mode_disable(&dev->host);
Aparna Mallavarapu7c8e75f2014-04-22 20:20:28 +0530384
Aparna Mallavarapu7c8e75f2014-04-22 20:20:28 +0530385 if (crypto_initialized())
386 crypto_eng_cleanup();
Aparna Mallavarapu25aff012014-05-08 12:21:44 +0530387
388 if (target_is_ssd_enabled())
389 clock_ce_disable(CE1_INSTANCE);
Aparna Mallavarapu7cc5a792014-02-27 21:49:59 -0800390}
391
392/* Do any target specific intialization needed before entering fastboot mode */
393void target_fastboot_init(void)
394{
395 /* Set the BOOT_DONE flag in PM8916 */
396 pm8x41_set_boot_done();
Aparna Mallavarapu7c8e75f2014-04-22 20:20:28 +0530397
398 if (target_is_ssd_enabled()) {
399 clock_ce_enable(CE1_INSTANCE);
400 target_load_ssd_keystore();
401 }
Aparna Mallavarapufe1f3d12013-10-19 15:05:30 +0530402}
Aparna Mallavarapuacb6ede2014-03-21 19:22:00 +0530403
404int set_download_mode(enum dload_mode mode)
405{
406 int ret = 0;
407 ret = scm_dload_mode(mode);
408
409 pm8x41_clear_pmic_watchdog();
410
411 return ret;
412}
Aparna Mallavarapu7c8e75f2014-04-22 20:20:28 +0530413
414void target_load_ssd_keystore(void)
415{
416 uint64_t ptn;
417 int index;
418 uint64_t size;
419 uint32_t *buffer = NULL;
420
421 if (!target_is_ssd_enabled())
422 return;
423
424 index = partition_get_index("ssd");
425
426 ptn = partition_get_offset(index);
427 if (ptn == 0){
428 dprintf(CRITICAL, "Error: ssd partition not found\n");
429 return;
430 }
431
432 size = partition_get_size(index);
433 if (size == 0) {
434 dprintf(CRITICAL, "Error: invalid ssd partition size\n");
435 return;
436 }
437
438 buffer = memalign(CACHE_LINE, ROUNDUP(size, CACHE_LINE));
439 if (!buffer) {
440 dprintf(CRITICAL, "Error: allocating memory for ssd buffer\n");
441 return;
442 }
443
444 if (mmc_read(ptn, buffer, size)) {
445 dprintf(CRITICAL, "Error: cannot read data\n");
446 free(buffer);
447 return;
448 }
449
450 clock_ce_enable(CE1_INSTANCE);
451 scm_protect_keystore(buffer, size);
452 clock_ce_disable(CE1_INSTANCE);
453 free(buffer);
454}
455
456crypto_engine_type board_ce_type(void)
457{
458 return CRYPTO_ENGINE_TYPE_HW;
459}
460
461/* Set up params for h/w CE. */
462void target_crypto_init_params()
463{
464 struct crypto_init_params ce_params;
465
466 /* Set up base addresses and instance. */
467 ce_params.crypto_instance = CE1_INSTANCE;
468 ce_params.crypto_base = MSM_CE1_BASE;
469 ce_params.bam_base = MSM_CE1_BAM_BASE;
470
471 /* Set up BAM config. */
472 ce_params.bam_ee = CE_EE;
473 ce_params.pipes.read_pipe = CE_READ_PIPE;
474 ce_params.pipes.write_pipe = CE_WRITE_PIPE;
475 ce_params.pipes.read_pipe_grp = CE_READ_PIPE_LOCK_GRP;
476 ce_params.pipes.write_pipe_grp = CE_WRITE_PIPE_LOCK_GRP;
477
478 /* Assign buffer sizes. */
479 ce_params.num_ce = CE_ARRAY_SIZE;
480 ce_params.read_fifo_size = CE_FIFO_SIZE;
481 ce_params.write_fifo_size = CE_FIFO_SIZE;
482
483 /* BAM is initialized by TZ for this platform.
484 * Do not do it again as the initialization address space
485 * is locked.
486 */
487 ce_params.do_bam_init = 0;
488
489 crypto_init_params(&ce_params);
490}
Aparna Mallavarapua1158242014-05-23 14:47:44 +0530491
492uint32_t target_get_hlos_subtype()
493{
494 return board_hlos_subtype();
495}
Channagoud Kadabib7dabeb2015-08-05 16:18:27 -0700496
497void pmic_reset_configure(uint8_t reset_type)
498{
499 pm8x41_reset_configure(reset_type);
500}