blob: e9d60a03daa73e4f0695b1bc7b0250d9b74c2b51 [file] [log] [blame]
Unnati Gandhi4d07fac2014-07-04 17:38:25 +05301/* Copyright (c) 2014, The Linux Foundation. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <debug.h>
30#include <platform/iomap.h>
31#include <reg.h>
32#include <target.h>
33#include <platform.h>
34#include <uart_dm.h>
35#include <mmc.h>
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053036#include <dev/keys.h>
37#include <spmi_v2.h>
38#include <pm8x41.h>
39#include <board.h>
40#include <baseband.h>
41#include <hsusb.h>
42#include <scm.h>
43#include <platform/gpio.h>
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053044#include <platform/irqs.h>
45#include <platform/clock.h>
46#include <crypto5_wrapper.h>
47#include <partition_parser.h>
48#include <stdlib.h>
Unnati Gandhi4d637e42014-07-11 14:47:25 +053049#include <gpio.h>
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053050
Unnati Gandhif4cb6622014-08-28 13:54:56 +053051#if LONG_PRESS_POWER_ON
52#include <shutdown_detect.h>
53#endif
54
55#if PON_VIB_SUPPORT
56#include <vibrator.h>
57#endif
58
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053059#define PMIC_ARB_CHANNEL_NUM 0
60#define PMIC_ARB_OWNER_ID 0
Unnati Gandhif4cb6622014-08-28 13:54:56 +053061#define TLMM_VOL_UP_BTN_GPIO 90
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053062
Unnati Gandhif4cb6622014-08-28 13:54:56 +053063#if PON_VIB_SUPPORT
64#define VIBRATE_TIME 250
65#endif
66
67#define FASTBOOT_MODE 0x77665500
68
69#define CE1_INSTANCE 1
70#define CE_EE 1
71#define CE_FIFO_SIZE 64
72#define CE_READ_PIPE 3
73#define CE_WRITE_PIPE 2
74#define CE_READ_PIPE_LOCK_GRP 0
75#define CE_WRITE_PIPE_LOCK_GRP 0
76#define CE_ARRAY_SIZE 20
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053077
Unnati Gandhi4d637e42014-07-11 14:47:25 +053078struct mmc_device *dev;
79
80static uint32_t mmc_pwrctl_base[] =
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053081 { MSM_SDC1_BASE, MSM_SDC2_BASE };
82
Unnati Gandhi4d637e42014-07-11 14:47:25 +053083static 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
89static void set_sdc_power_ctrl(void);
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053090
91void target_early_init(void)
92{
93#if WITH_DEBUG_UART
94 uart_dm_init(1, 0, BLSP1_UART1_BASE);
95#endif
96}
97
Unnati Gandhi4d637e42014-07-11 14:47:25 +053098void target_sdc_init()
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053099{
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530100 struct mmc_config_data config;
101
102 /* Set drive strength & pull ctrl values */
103 set_sdc_power_ctrl();
104
105 config.bus_width = DATA_BUS_WIDTH_8BIT;
106 config.max_clk_rate = MMC_CLK_177MHZ;
107
108 /* Try slot 1*/
109 config.slot = 1;
110 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
111 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
112 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
113 config.hs400_support = 0;
114
115 if (!(dev = mmc_init(&config))) {
116 /* Try slot 2 */
117 config.slot = 2;
118 config.max_clk_rate = MMC_CLK_200MHZ;
119 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
120 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
121 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
122
123 if (!(dev = mmc_init(&config))) {
124 dprintf(CRITICAL, "mmc init failed!");
125 ASSERT(0);
126 }
127 }
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530128}
129
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530130void *target_mmc_device()
131{
132 return (void *) dev;
133}
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530134
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530135/* Return 1 if vol_up pressed */
136static int target_volume_up()
137{
138 uint8_t status = 0;
139
140 gpio_tlmm_config(TLMM_VOL_UP_BTN_GPIO, 0, GPIO_INPUT, GPIO_PULL_UP, GPIO_2MA, GPIO_ENABLE);
141
142 /* Wait for the gpio config to take effect - debounce time */
143 thread_sleep(10);
144
145 /* Get status of GPIO */
146 status = gpio_status(TLMM_VOL_UP_BTN_GPIO);
147
148 /* Active low signal. */
149 return !status;
150}
151
152/* Return 1 if vol_down pressed */
153uint32_t target_volume_down()
154{
155 /* Volume down button tied in with PMIC RESIN. */
156 return pm8x41_resin_status();
157}
158
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530159static void target_keystatus()
160{
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530161 keys_init();
162
163 if(target_volume_down())
164 keys_post_event(KEY_VOLUMEDOWN, 1);
165
166 if(target_volume_up())
167 keys_post_event(KEY_VOLUMEUP, 1);
168
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530169}
170
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530171static void set_sdc_power_ctrl()
172{
173 /* Drive strength configs for sdc pins */
174 struct tlmm_cfgs sdc1_hdrv_cfg[] =
175 {
176 { SDC1_CLK_HDRV_CTL_OFF, TLMM_CUR_VAL_16MA, TLMM_HDRV_MASK },
177 { SDC1_CMD_HDRV_CTL_OFF, TLMM_CUR_VAL_10MA, TLMM_HDRV_MASK },
178 { SDC1_DATA_HDRV_CTL_OFF, TLMM_CUR_VAL_6MA, TLMM_HDRV_MASK },
179 };
180
181 /* Pull configs for sdc pins */
182 struct tlmm_cfgs sdc1_pull_cfg[] =
183 {
184 { SDC1_CLK_PULL_CTL_OFF, TLMM_NO_PULL, TLMM_PULL_MASK },
185 { SDC1_CMD_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK },
186 { SDC1_DATA_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK },
187 };
188
189 /* Set the drive strength & pull control values */
190 tlmm_set_hdrive_ctrl(sdc1_hdrv_cfg, ARRAY_SIZE(sdc1_hdrv_cfg));
191 tlmm_set_pull_ctrl(sdc1_pull_cfg, ARRAY_SIZE(sdc1_pull_cfg));
192}
193
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530194void target_init(void)
195{
196 uint32_t base_addr;
197 uint8_t slot;
198
199 dprintf(INFO, "target_init()\n");
200
201 spmi_init(PMIC_ARB_CHANNEL_NUM, PMIC_ARB_OWNER_ID);
202
203 target_keystatus();
204
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530205 target_sdc_init();
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530206
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530207 if (partition_read_table())
208 {
209 dprintf(CRITICAL, "Error reading the partition table info\n");
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530210 ASSERT(0);
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530211 }
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530212
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530213#if LONG_PRESS_POWER_ON
214 shutdown_detect();
215#endif
216
217#if PON_VIB_SUPPORT
218
219 /* turn on vibrator to indicate that phone is booting up to end user */
220 vib_timed_turn_on(VIBRATE_TIME);
221#endif
222
223 if (target_use_signed_kernel())
224 target_crypto_init_params();
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530225}
226
227void target_serialno(unsigned char *buf)
228{
229 uint32_t serialno;
230 if (target_is_emmc_boot()) {
231 serialno = mmc_get_psn();
232 snprintf((char *)buf, 13, "%x", serialno);
233 }
234}
235
236unsigned board_machtype(void)
237{
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530238 return LINUX_MACHTYPE_UNKNOWN;
239}
240
241unsigned check_reboot_mode(void)
242{
243 uint32_t restart_reason = 0;
244
245 /* Read reboot reason and scrub it */
246 restart_reason = readl(RESTART_REASON_ADDR);
247 writel(0x00, RESTART_REASON_ADDR);
248
249 return restart_reason;
250}
251
252static int scm_dload_mode(int mode)
253{
254 int ret = 0;
255 uint32_t dload_type;
256
257 dprintf(SPEW, "DLOAD mode: %d\n", mode);
258 if (mode == NORMAL_DLOAD)
259 dload_type = SCM_DLOAD_MODE;
260 else if(mode == EMERGENCY_DLOAD)
261 dload_type = SCM_EDLOAD_MODE;
262 else
263 dload_type = 0;
264
265 ret = scm_call_atomic2(SCM_SVC_BOOT, SCM_DLOAD_CMD, dload_type, 0);
266 if (ret)
267 dprintf(CRITICAL, "Failed to write to boot misc: %d\n", ret);
268
269 ret = scm_call_atomic2(SCM_SVC_BOOT, WDOG_DEBUG_DISABLE, 1, 0);
270 if (ret)
271 dprintf(CRITICAL, "Failed to disable the wdog debug \n");
272
273 return ret;
274}
275
276/* Configure PMIC and Drop PS_HOLD for shutdown */
277void shutdown_device()
278{
279 dprintf(CRITICAL, "Going down for shutdown.\n");
280
281 /* Configure PMIC for shutdown */
282 pm8x41_reset_configure(PON_PSHOLD_SHUTDOWN);
283
284 /* Drop PS_HOLD for MSM */
285 writel(0x00, MPM2_MPM_PS_HOLD);
286
287 mdelay(5000);
288
289}
290
291void reboot_device(unsigned reboot_reason)
292{
293 uint8_t reset_type = 0;
294 uint32_t ret = 0;
295
296 /* Need to clear the SW_RESET_ENTRY register and
297 * write to the BOOT_MISC_REG for known reset cases
298 */
299 if(reboot_reason != DLOAD)
300 scm_dload_mode(NORMAL_MODE);
301
302 writel(reboot_reason, RESTART_REASON_ADDR);
303
304 /* For Reboot-bootloader and Dload cases do a warm reset
305 * For Reboot cases do a hard reset
306 */
307 if((reboot_reason == FASTBOOT_MODE) || (reboot_reason == DLOAD))
308 reset_type = PON_PSHOLD_WARM_RESET;
309 else
310 reset_type = PON_PSHOLD_HARD_RESET;
311
312 pm8x41_reset_configure(reset_type);
313
314 ret = scm_halt_pmic_arbiter();
315
316 if (ret)
317 dprintf(CRITICAL , "Failed to halt pmic arbiter: %d\n", ret);
318
319 /* Drop PS_HOLD for MSM */
320 writel(0x00, MPM2_MPM_PS_HOLD);
321
322 mdelay(5000);
323
324 dprintf(CRITICAL, "Rebooting failed\n");
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530325}
326
Unnati Gandhia556a4d2014-08-12 10:42:21 +0530327/* Detect the target type */
328void target_detect(struct board_data *board)
329{
330 /*
331 * already fill the board->target on board.c
332 */
333}
334
335void target_baseband_detect(struct board_data *board)
336{
337 uint32_t platform;
338
339 platform = board->platform;
340 switch(platform)
341 {
342 case MSM8909:
343 case MSM8209:
344 case MSM8208:
345 board->baseband = BASEBAND_MSM;
346 break;
347
348 case MDM9209:
349 case MDM9309:
350 case MDM9609:
351 board->baseband = BASEBAND_MDM;
352 break;
353
354 default:
355 dprintf(CRITICAL, "Platform type: %u is not supported\n", platform);
356 ASSERT(0);
357 };
358}
Shivaraj Shettyf9e10c42014-09-17 04:21:15 +0530359uint8_t target_panel_auto_detect_enabled()
360{
361 uint8_t ret = 0;
362
363 switch(board_hardware_id()) {
364 default:
365 ret = 0;
366 break;
367 }
368 return ret;
369}
370
371static uint8_t splash_override;
372/* Returns 1 if target supports continuous splash screen. */
373int target_cont_splash_screen()
374{
375 uint8_t splash_screen = 0;
376 if (!splash_override) {
377 switch (board_hardware_id()) {
378 default:
379 splash_screen = 0;
380 break;
381 }
382 dprintf(SPEW, "Target_cont_splash=%d\n", splash_screen);
383 }
384 return splash_screen;
385}
386
387void target_force_cont_splash_disable(uint8_t override)
388{
389 splash_override = override;
390}
Unnati Gandhia556a4d2014-08-12 10:42:21 +0530391
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530392unsigned target_baseband()
393{
394 return board_baseband();
395}
396
397int emmc_recovery_init(void)
398{
399 return _emmc_recovery_init();
400}
401
402void target_usb_init(void)
403{
404 uint32_t val;
405
406 /* Select and enable external configuration with USB PHY */
407 ulpi_write(ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT, ULPI_MISC_A_SET);
408
409 /* Enable sess_vld */
410 val = readl(USB_GENCONFIG_2) | GEN2_SESS_VLD_CTRL_EN;
411 writel(val, USB_GENCONFIG_2);
412
413 /* Enable external vbus configuration in the LINK */
414 val = readl(USB_USBCMD);
415 val |= SESS_VLD_CTRL;
416 writel(val, USB_USBCMD);
417}
418
419unsigned target_pause_for_battery_charge(void)
420{
421 uint8_t pon_reason = pm8x41_get_pon_reason();
422 uint8_t is_cold_boot = pm8x41_get_is_cold_boot();
423 dprintf(INFO, "%s : pon_reason is %d cold_boot:%d\n", __func__,
424 pon_reason, is_cold_boot);
425 /* In case of fastboot reboot,adb reboot or if we see the power key
426 * pressed we do not want go into charger mode.
427 * fastboot reboot is warm boot with PON hard reset bit not set
428 * adb reboot is a cold boot with PON hard reset bit set
429 */
430 if (is_cold_boot &&
431 (!(pon_reason & HARD_RST)) &&
432 (!(pon_reason & KPDPWR_N)) &&
433 ((pon_reason & USB_CHG) || (pon_reason & DC_CHG)))
434 return 1;
435 else
436 return 0;
437}
438
439void target_usb_stop(void)
440{
441 /* Disable VBUS mimicing in the controller. */
442 ulpi_write(ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT, ULPI_MISC_A_CLEAR);
443}
444
445
446void target_uninit(void)
447{
448#if PON_VIB_SUPPORT
449 /* wait for the vibrator timer is expried */
450 wait_vib_timeout();
451#endif
452
453 mmc_put_card_to_sleep(dev);
454 sdhci_mode_disable(&dev->host);
455
456 if (crypto_initialized())
457 crypto_eng_cleanup();
458
459 if (target_is_ssd_enabled())
460 clock_ce_disable(CE1_INSTANCE);
461}
462
463/* Do any target specific intialization needed before entering fastboot mode */
464void target_fastboot_init(void)
465{
466 /* Set the BOOT_DONE flag in PM8916 */
467 pm8x41_set_boot_done();
468
469 if (target_is_ssd_enabled()) {
470 clock_ce_enable(CE1_INSTANCE);
471 target_load_ssd_keystore();
472 }
473}
474
475int set_download_mode(enum dload_mode mode)
476{
477 int ret = 0;
478 ret = scm_dload_mode(mode);
479
480 pm8x41_clear_pmic_watchdog();
481
482 return ret;
483}
484
485void target_load_ssd_keystore(void)
486{
487 uint64_t ptn;
488 int index;
489 uint64_t size;
490 uint32_t *buffer = NULL;
491
492 if (!target_is_ssd_enabled())
493 return;
494
495 index = partition_get_index("ssd");
496
497 ptn = partition_get_offset(index);
498 if (ptn == 0){
499 dprintf(CRITICAL, "Error: ssd partition not found\n");
500 return;
501 }
502
503 size = partition_get_size(index);
504 if (size == 0) {
505 dprintf(CRITICAL, "Error: invalid ssd partition size\n");
506 return;
507 }
508
509 buffer = memalign(CACHE_LINE, ROUNDUP(size, CACHE_LINE));
510 if (!buffer) {
511 dprintf(CRITICAL, "Error: allocating memory for ssd buffer\n");
512 return;
513 }
514 if (mmc_read(ptn, buffer, size)) {
515 dprintf(CRITICAL, "Error: cannot read data\n");
516 free(buffer);
517 return;
518 }
519
520 clock_ce_enable(CE1_INSTANCE);
521 scm_protect_keystore(buffer, size);
522 clock_ce_disable(CE1_INSTANCE);
523 free(buffer);
524}
525
526crypto_engine_type board_ce_type(void)
527{
528 return CRYPTO_ENGINE_TYPE_HW;
529}
530
531/* Set up params for h/w CE. */
532void target_crypto_init_params()
533{
534 struct crypto_init_params ce_params;
535
536 /* Set up base addresses and instance. */
537 ce_params.crypto_instance = CE1_INSTANCE;
538 ce_params.crypto_base = MSM_CE1_BASE;
539 ce_params.bam_base = MSM_CE1_BAM_BASE;
540
541 /* Set up BAM config. */
542 ce_params.bam_ee = CE_EE;
543 ce_params.pipes.read_pipe = CE_READ_PIPE;
544 ce_params.pipes.write_pipe = CE_WRITE_PIPE;
545 ce_params.pipes.read_pipe_grp = CE_READ_PIPE_LOCK_GRP;
546 ce_params.pipes.write_pipe_grp = CE_WRITE_PIPE_LOCK_GRP;
547
548 /* Assign buffer sizes. */
549 ce_params.num_ce = CE_ARRAY_SIZE;
550 ce_params.read_fifo_size = CE_FIFO_SIZE;
551 ce_params.write_fifo_size = CE_FIFO_SIZE;
552
553 /* BAM is initialized by TZ for this platform.
554 * Do not do it again as the initialization address space
555 * is locked.
556 */
557 ce_params.do_bam_init = 0;
558
559 crypto_init_params(&ce_params);
560}
561
562uint32_t target_get_hlos_subtype()
563{
564 return board_hlos_subtype();
565}