blob: a3e9c0bfdafd84bfd07d3dea8674712330e4de3d [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 Gandhic24a86f2014-09-19 16:07:16 +053050#include <rpm-smd.h>
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053051
Unnati Gandhif4cb6622014-08-28 13:54:56 +053052#if LONG_PRESS_POWER_ON
53#include <shutdown_detect.h>
54#endif
55
56#if PON_VIB_SUPPORT
57#include <vibrator.h>
58#endif
59
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053060#define PMIC_ARB_CHANNEL_NUM 0
61#define PMIC_ARB_OWNER_ID 0
Unnati Gandhif4cb6622014-08-28 13:54:56 +053062#define TLMM_VOL_UP_BTN_GPIO 90
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053063
Unnati Gandhif4cb6622014-08-28 13:54:56 +053064#if PON_VIB_SUPPORT
65#define VIBRATE_TIME 250
66#endif
67
68#define FASTBOOT_MODE 0x77665500
69
70#define CE1_INSTANCE 1
71#define CE_EE 1
72#define CE_FIFO_SIZE 64
73#define CE_READ_PIPE 3
74#define CE_WRITE_PIPE 2
75#define CE_READ_PIPE_LOCK_GRP 0
76#define CE_WRITE_PIPE_LOCK_GRP 0
77#define CE_ARRAY_SIZE 20
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053078
Unnati Gandhi4d637e42014-07-11 14:47:25 +053079struct mmc_device *dev;
80
81static uint32_t mmc_pwrctl_base[] =
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053082 { MSM_SDC1_BASE, MSM_SDC2_BASE };
83
Unnati Gandhi4d637e42014-07-11 14:47:25 +053084static 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
90static void set_sdc_power_ctrl(void);
Unnati Gandhi4d07fac2014-07-04 17:38:25 +053091
92void target_early_init(void)
93{
94#if WITH_DEBUG_UART
95 uart_dm_init(1, 0, BLSP1_UART1_BASE);
96#endif
97}
98
Unnati Gandhi4d637e42014-07-11 14:47:25 +053099void target_sdc_init()
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530100{
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530101 struct mmc_config_data config;
102
103 /* Set drive strength & pull ctrl values */
104 set_sdc_power_ctrl();
105
106 config.bus_width = DATA_BUS_WIDTH_8BIT;
107 config.max_clk_rate = MMC_CLK_177MHZ;
108
109 /* Try slot 1*/
110 config.slot = 1;
111 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
112 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
113 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
114 config.hs400_support = 0;
115
116 if (!(dev = mmc_init(&config))) {
117 /* Try slot 2 */
118 config.slot = 2;
119 config.max_clk_rate = MMC_CLK_200MHZ;
120 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
121 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
122 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
123
124 if (!(dev = mmc_init(&config))) {
125 dprintf(CRITICAL, "mmc init failed!");
126 ASSERT(0);
127 }
128 }
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530129}
130
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530131void *target_mmc_device()
132{
133 return (void *) dev;
134}
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530135
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530136/* Return 1 if vol_up pressed */
137static int target_volume_up()
138{
139 uint8_t status = 0;
140
141 gpio_tlmm_config(TLMM_VOL_UP_BTN_GPIO, 0, GPIO_INPUT, GPIO_PULL_UP, GPIO_2MA, GPIO_ENABLE);
142
143 /* Wait for the gpio config to take effect - debounce time */
144 thread_sleep(10);
145
146 /* Get status of GPIO */
147 status = gpio_status(TLMM_VOL_UP_BTN_GPIO);
148
149 /* Active low signal. */
150 return !status;
151}
152
153/* Return 1 if vol_down pressed */
154uint32_t target_volume_down()
155{
156 /* Volume down button tied in with PMIC RESIN. */
157 return pm8x41_resin_status();
158}
159
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530160static void target_keystatus()
161{
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530162 keys_init();
163
164 if(target_volume_down())
165 keys_post_event(KEY_VOLUMEDOWN, 1);
166
167 if(target_volume_up())
168 keys_post_event(KEY_VOLUMEUP, 1);
169
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530170}
171
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530172static void set_sdc_power_ctrl()
173{
174 /* Drive strength configs for sdc pins */
175 struct tlmm_cfgs sdc1_hdrv_cfg[] =
176 {
177 { SDC1_CLK_HDRV_CTL_OFF, TLMM_CUR_VAL_16MA, TLMM_HDRV_MASK },
178 { SDC1_CMD_HDRV_CTL_OFF, TLMM_CUR_VAL_10MA, TLMM_HDRV_MASK },
179 { SDC1_DATA_HDRV_CTL_OFF, TLMM_CUR_VAL_6MA, TLMM_HDRV_MASK },
180 };
181
182 /* Pull configs for sdc pins */
183 struct tlmm_cfgs sdc1_pull_cfg[] =
184 {
185 { SDC1_CLK_PULL_CTL_OFF, TLMM_NO_PULL, TLMM_PULL_MASK },
186 { SDC1_CMD_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK },
187 { SDC1_DATA_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK },
188 };
189
190 /* Set the drive strength & pull control values */
191 tlmm_set_hdrive_ctrl(sdc1_hdrv_cfg, ARRAY_SIZE(sdc1_hdrv_cfg));
192 tlmm_set_pull_ctrl(sdc1_pull_cfg, ARRAY_SIZE(sdc1_pull_cfg));
193}
194
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530195void target_init(void)
196{
197 uint32_t base_addr;
198 uint8_t slot;
199
200 dprintf(INFO, "target_init()\n");
201
202 spmi_init(PMIC_ARB_CHANNEL_NUM, PMIC_ARB_OWNER_ID);
203
204 target_keystatus();
205
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530206 target_sdc_init();
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530207
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530208 if (partition_read_table())
209 {
210 dprintf(CRITICAL, "Error reading the partition table info\n");
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530211 ASSERT(0);
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530212 }
Unnati Gandhi4d637e42014-07-11 14:47:25 +0530213
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530214#if LONG_PRESS_POWER_ON
215 shutdown_detect();
216#endif
217
218#if PON_VIB_SUPPORT
219
220 /* turn on vibrator to indicate that phone is booting up to end user */
221 vib_timed_turn_on(VIBRATE_TIME);
222#endif
223
224 if (target_use_signed_kernel())
225 target_crypto_init_params();
Unnati Gandhic24a86f2014-09-19 16:07:16 +0530226
227 rpm_smd_init();
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530228}
229
230void target_serialno(unsigned char *buf)
231{
232 uint32_t serialno;
233 if (target_is_emmc_boot()) {
234 serialno = mmc_get_psn();
235 snprintf((char *)buf, 13, "%x", serialno);
236 }
237}
238
239unsigned board_machtype(void)
240{
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530241 return LINUX_MACHTYPE_UNKNOWN;
242}
243
244unsigned check_reboot_mode(void)
245{
246 uint32_t restart_reason = 0;
247
248 /* Read reboot reason and scrub it */
249 restart_reason = readl(RESTART_REASON_ADDR);
250 writel(0x00, RESTART_REASON_ADDR);
251
252 return restart_reason;
253}
254
255static int scm_dload_mode(int mode)
256{
257 int ret = 0;
258 uint32_t dload_type;
259
260 dprintf(SPEW, "DLOAD mode: %d\n", mode);
261 if (mode == NORMAL_DLOAD)
262 dload_type = SCM_DLOAD_MODE;
263 else if(mode == EMERGENCY_DLOAD)
264 dload_type = SCM_EDLOAD_MODE;
265 else
266 dload_type = 0;
267
268 ret = scm_call_atomic2(SCM_SVC_BOOT, SCM_DLOAD_CMD, dload_type, 0);
269 if (ret)
270 dprintf(CRITICAL, "Failed to write to boot misc: %d\n", ret);
271
272 ret = scm_call_atomic2(SCM_SVC_BOOT, WDOG_DEBUG_DISABLE, 1, 0);
273 if (ret)
274 dprintf(CRITICAL, "Failed to disable the wdog debug \n");
275
276 return ret;
277}
278
279/* Configure PMIC and Drop PS_HOLD for shutdown */
280void shutdown_device()
281{
282 dprintf(CRITICAL, "Going down for shutdown.\n");
283
284 /* Configure PMIC for shutdown */
285 pm8x41_reset_configure(PON_PSHOLD_SHUTDOWN);
286
287 /* Drop PS_HOLD for MSM */
288 writel(0x00, MPM2_MPM_PS_HOLD);
289
290 mdelay(5000);
291
292}
293
294void reboot_device(unsigned reboot_reason)
295{
296 uint8_t reset_type = 0;
297 uint32_t ret = 0;
298
299 /* Need to clear the SW_RESET_ENTRY register and
300 * write to the BOOT_MISC_REG for known reset cases
301 */
302 if(reboot_reason != DLOAD)
303 scm_dload_mode(NORMAL_MODE);
304
305 writel(reboot_reason, RESTART_REASON_ADDR);
306
307 /* For Reboot-bootloader and Dload cases do a warm reset
308 * For Reboot cases do a hard reset
309 */
310 if((reboot_reason == FASTBOOT_MODE) || (reboot_reason == DLOAD))
311 reset_type = PON_PSHOLD_WARM_RESET;
312 else
313 reset_type = PON_PSHOLD_HARD_RESET;
314
315 pm8x41_reset_configure(reset_type);
316
317 ret = scm_halt_pmic_arbiter();
318
319 if (ret)
320 dprintf(CRITICAL , "Failed to halt pmic arbiter: %d\n", ret);
321
322 /* Drop PS_HOLD for MSM */
323 writel(0x00, MPM2_MPM_PS_HOLD);
324
325 mdelay(5000);
326
327 dprintf(CRITICAL, "Rebooting failed\n");
Unnati Gandhi4d07fac2014-07-04 17:38:25 +0530328}
329
Unnati Gandhia556a4d2014-08-12 10:42:21 +0530330/* Detect the target type */
331void target_detect(struct board_data *board)
332{
333 /*
334 * already fill the board->target on board.c
335 */
336}
337
338void target_baseband_detect(struct board_data *board)
339{
340 uint32_t platform;
341
342 platform = board->platform;
343 switch(platform)
344 {
345 case MSM8909:
346 case MSM8209:
347 case MSM8208:
348 board->baseband = BASEBAND_MSM;
349 break;
350
351 case MDM9209:
352 case MDM9309:
353 case MDM9609:
354 board->baseband = BASEBAND_MDM;
355 break;
356
357 default:
358 dprintf(CRITICAL, "Platform type: %u is not supported\n", platform);
359 ASSERT(0);
360 };
361}
Shivaraj Shettyf9e10c42014-09-17 04:21:15 +0530362uint8_t target_panel_auto_detect_enabled()
363{
364 uint8_t ret = 0;
365
366 switch(board_hardware_id()) {
367 default:
368 ret = 0;
369 break;
370 }
371 return ret;
372}
373
374static uint8_t splash_override;
375/* Returns 1 if target supports continuous splash screen. */
376int target_cont_splash_screen()
377{
378 uint8_t splash_screen = 0;
379 if (!splash_override) {
380 switch (board_hardware_id()) {
381 default:
382 splash_screen = 0;
383 break;
384 }
385 dprintf(SPEW, "Target_cont_splash=%d\n", splash_screen);
386 }
387 return splash_screen;
388}
389
390void target_force_cont_splash_disable(uint8_t override)
391{
392 splash_override = override;
393}
Unnati Gandhia556a4d2014-08-12 10:42:21 +0530394
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530395unsigned target_baseband()
396{
397 return board_baseband();
398}
399
400int emmc_recovery_init(void)
401{
402 return _emmc_recovery_init();
403}
404
405void target_usb_init(void)
406{
407 uint32_t val;
408
409 /* Select and enable external configuration with USB PHY */
410 ulpi_write(ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT, ULPI_MISC_A_SET);
411
412 /* Enable sess_vld */
413 val = readl(USB_GENCONFIG_2) | GEN2_SESS_VLD_CTRL_EN;
414 writel(val, USB_GENCONFIG_2);
415
416 /* Enable external vbus configuration in the LINK */
417 val = readl(USB_USBCMD);
418 val |= SESS_VLD_CTRL;
419 writel(val, USB_USBCMD);
420}
421
422unsigned target_pause_for_battery_charge(void)
423{
424 uint8_t pon_reason = pm8x41_get_pon_reason();
425 uint8_t is_cold_boot = pm8x41_get_is_cold_boot();
426 dprintf(INFO, "%s : pon_reason is %d cold_boot:%d\n", __func__,
427 pon_reason, is_cold_boot);
428 /* In case of fastboot reboot,adb reboot or if we see the power key
429 * pressed we do not want go into charger mode.
430 * fastboot reboot is warm boot with PON hard reset bit not set
431 * adb reboot is a cold boot with PON hard reset bit set
432 */
433 if (is_cold_boot &&
434 (!(pon_reason & HARD_RST)) &&
435 (!(pon_reason & KPDPWR_N)) &&
436 ((pon_reason & USB_CHG) || (pon_reason & DC_CHG)))
437 return 1;
438 else
439 return 0;
440}
441
442void target_usb_stop(void)
443{
444 /* Disable VBUS mimicing in the controller. */
445 ulpi_write(ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT, ULPI_MISC_A_CLEAR);
446}
447
448
449void target_uninit(void)
450{
451#if PON_VIB_SUPPORT
452 /* wait for the vibrator timer is expried */
453 wait_vib_timeout();
454#endif
455
456 mmc_put_card_to_sleep(dev);
457 sdhci_mode_disable(&dev->host);
458
459 if (crypto_initialized())
460 crypto_eng_cleanup();
461
462 if (target_is_ssd_enabled())
463 clock_ce_disable(CE1_INSTANCE);
Unnati Gandhic24a86f2014-09-19 16:07:16 +0530464
465 rpm_smd_uninit();
Unnati Gandhif4cb6622014-08-28 13:54:56 +0530466}
467
468/* Do any target specific intialization needed before entering fastboot mode */
469void target_fastboot_init(void)
470{
471 /* Set the BOOT_DONE flag in PM8916 */
472 pm8x41_set_boot_done();
473
474 if (target_is_ssd_enabled()) {
475 clock_ce_enable(CE1_INSTANCE);
476 target_load_ssd_keystore();
477 }
478}
479
480int set_download_mode(enum dload_mode mode)
481{
482 int ret = 0;
483 ret = scm_dload_mode(mode);
484
485 pm8x41_clear_pmic_watchdog();
486
487 return ret;
488}
489
490void target_load_ssd_keystore(void)
491{
492 uint64_t ptn;
493 int index;
494 uint64_t size;
495 uint32_t *buffer = NULL;
496
497 if (!target_is_ssd_enabled())
498 return;
499
500 index = partition_get_index("ssd");
501
502 ptn = partition_get_offset(index);
503 if (ptn == 0){
504 dprintf(CRITICAL, "Error: ssd partition not found\n");
505 return;
506 }
507
508 size = partition_get_size(index);
509 if (size == 0) {
510 dprintf(CRITICAL, "Error: invalid ssd partition size\n");
511 return;
512 }
513
514 buffer = memalign(CACHE_LINE, ROUNDUP(size, CACHE_LINE));
515 if (!buffer) {
516 dprintf(CRITICAL, "Error: allocating memory for ssd buffer\n");
517 return;
518 }
519 if (mmc_read(ptn, buffer, size)) {
520 dprintf(CRITICAL, "Error: cannot read data\n");
521 free(buffer);
522 return;
523 }
524
525 clock_ce_enable(CE1_INSTANCE);
526 scm_protect_keystore(buffer, size);
527 clock_ce_disable(CE1_INSTANCE);
528 free(buffer);
529}
530
531crypto_engine_type board_ce_type(void)
532{
533 return CRYPTO_ENGINE_TYPE_HW;
534}
535
536/* Set up params for h/w CE. */
537void target_crypto_init_params()
538{
539 struct crypto_init_params ce_params;
540
541 /* Set up base addresses and instance. */
542 ce_params.crypto_instance = CE1_INSTANCE;
543 ce_params.crypto_base = MSM_CE1_BASE;
544 ce_params.bam_base = MSM_CE1_BAM_BASE;
545
546 /* Set up BAM config. */
547 ce_params.bam_ee = CE_EE;
548 ce_params.pipes.read_pipe = CE_READ_PIPE;
549 ce_params.pipes.write_pipe = CE_WRITE_PIPE;
550 ce_params.pipes.read_pipe_grp = CE_READ_PIPE_LOCK_GRP;
551 ce_params.pipes.write_pipe_grp = CE_WRITE_PIPE_LOCK_GRP;
552
553 /* Assign buffer sizes. */
554 ce_params.num_ce = CE_ARRAY_SIZE;
555 ce_params.read_fifo_size = CE_FIFO_SIZE;
556 ce_params.write_fifo_size = CE_FIFO_SIZE;
557
558 /* BAM is initialized by TZ for this platform.
559 * Do not do it again as the initialization address space
560 * is locked.
561 */
562 ce_params.do_bam_init = 0;
563
564 crypto_init_params(&ce_params);
565}
566
567uint32_t target_get_hlos_subtype()
568{
569 return board_hlos_subtype();
570}