blob: cbcc13feed7d4532b83ad665193323860e75cbc2 [file] [log] [blame]
Aparna Mallavarapuca676882015-01-19 20:39:06 +05301/* Copyright (c) 2015, 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>
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>
Aparna Mallavarapuca676882015-01-19 20:39:06 +053057
Padmanabhan Komanduru9d49f892015-04-10 12:58:46 -070058#include "target/display.h"
59
Aparna Mallavarapuca676882015-01-19 20:39:06 +053060#if LONG_PRESS_POWER_ON
61#include <shutdown_detect.h>
62#endif
63
64#define PMIC_ARB_CHANNEL_NUM 0
65#define PMIC_ARB_OWNER_ID 0
66#define TLMM_VOL_UP_BTN_GPIO 85
67
68#define FASTBOOT_MODE 0x77665500
Aparna Mallavarapu680a1332015-04-29 19:14:09 +053069#define RECOVERY_MODE 0x77665502
Aparna Mallavarapuca676882015-01-19 20:39:06 +053070#define PON_SOFT_RB_SPARE 0x88F
71
Aparna Mallavarapu1e8b0932015-03-29 23:38:13 +053072#define CE1_INSTANCE 1
73#define CE_EE 1
74#define CE_FIFO_SIZE 64
75#define CE_READ_PIPE 3
76#define CE_WRITE_PIPE 2
77#define CE_READ_PIPE_LOCK_GRP 0
78#define CE_WRITE_PIPE_LOCK_GRP 0
79#define CE_ARRAY_SIZE 20
80
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +053081struct mmc_device *dev;
82
83static uint32_t mmc_pwrctl_base[] =
Aparna Mallavarapuca676882015-01-19 20:39:06 +053084 { MSM_SDC1_BASE, MSM_SDC2_BASE };
85
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +053086static uint32_t mmc_sdhci_base[] =
87 { MSM_SDC1_SDHCI_BASE, MSM_SDC2_SDHCI_BASE };
88
89static uint32_t mmc_sdc_pwrctl_irq[] =
90 { SDCC1_PWRCTL_IRQ, SDCC2_PWRCTL_IRQ };
Aparna Mallavarapuca676882015-01-19 20:39:06 +053091
92void target_early_init(void)
93{
94#if WITH_DEBUG_UART
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +053095 uart_dm_init(2, 0, BLSP1_UART1_BASE);
Aparna Mallavarapuca676882015-01-19 20:39:06 +053096#endif
97}
98
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +053099static void set_sdc_power_ctrl()
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530100{
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530101 /* Drive strength configs for sdc pins */
102 struct tlmm_cfgs sdc1_hdrv_cfg[] =
103 {
104 { SDC1_CLK_HDRV_CTL_OFF, TLMM_CUR_VAL_16MA, TLMM_HDRV_MASK, 0},
105 { SDC1_CMD_HDRV_CTL_OFF, TLMM_CUR_VAL_10MA, TLMM_HDRV_MASK, 0},
106 { SDC1_DATA_HDRV_CTL_OFF, TLMM_CUR_VAL_10MA, TLMM_HDRV_MASK , 0},
107 };
108
109 /* Pull configs for sdc pins */
110 struct tlmm_cfgs sdc1_pull_cfg[] =
111 {
112 { SDC1_CLK_PULL_CTL_OFF, TLMM_NO_PULL, TLMM_PULL_MASK, 0},
113 { SDC1_CMD_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, 0},
114 { SDC1_DATA_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, 0},
115 };
116
Aparna Mallavarapu29138912015-04-13 23:45:35 +0530117 struct tlmm_cfgs sdc1_rclk_cfg[] =
118 {
119 { SDC1_RCLK_PULL_CTL_OFF, TLMM_PULL_DOWN, TLMM_PULL_MASK, 0},
120 };
121
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530122 /* Set the drive strength & pull control values */
123 tlmm_set_hdrive_ctrl(sdc1_hdrv_cfg, ARRAY_SIZE(sdc1_hdrv_cfg));
124 tlmm_set_pull_ctrl(sdc1_pull_cfg, ARRAY_SIZE(sdc1_pull_cfg));
Aparna Mallavarapu29138912015-04-13 23:45:35 +0530125 tlmm_set_pull_ctrl(sdc1_rclk_cfg, ARRAY_SIZE(sdc1_rclk_cfg));
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530126}
127
128void target_sdc_init()
129{
130 struct mmc_config_data config;
131
132 /* Set drive strength & pull ctrl values */
133 set_sdc_power_ctrl();
134
135 /* Try slot 1*/
136 config.slot = 1;
137 config.bus_width = DATA_BUS_WIDTH_8BIT;
Aparna Mallavarapu680a1332015-04-29 19:14:09 +0530138 config.max_clk_rate = MMC_CLK_192MHZ;
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530139 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
140 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
141 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
142 config.hs400_support = 1;
143
144 if (!(dev = mmc_init(&config))) {
145 /* Try slot 2 */
146 config.slot = 2;
147 config.max_clk_rate = MMC_CLK_200MHZ;
148 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
149 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
150 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
151 config.hs400_support = 0;
152
153 if (!(dev = mmc_init(&config))) {
154 dprintf(CRITICAL, "mmc init failed!");
155 ASSERT(0);
156 }
157 }
158}
159
160void *target_mmc_device()
161{
162 return (void *) dev;
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530163}
164
165/* Return 1 if vol_up pressed */
166static int target_volume_up()
167{
168 uint8_t status = 0;
169
170 gpio_tlmm_config(TLMM_VOL_UP_BTN_GPIO, 0, GPIO_INPUT, GPIO_PULL_UP, GPIO_2MA, GPIO_ENABLE);
171
172 /* Wait for the gpio config to take effect - debounce time */
173 thread_sleep(10);
174
175 /* Get status of GPIO */
176 status = gpio_status(TLMM_VOL_UP_BTN_GPIO);
177
Aparna Mallavarapufa5f8a72015-03-31 06:21:36 +0530178 /* Active low signal. */
Aparna Mallavarapudb938b62015-04-09 01:00:55 +0530179 return !status;
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530180}
181
182/* Return 1 if vol_down pressed */
183uint32_t target_volume_down()
184{
185 /* Volume down button tied in with PMIC RESIN. */
186 return pm8x41_resin_status();
187}
188
189static void target_keystatus()
190{
191 keys_init();
192
193 if(target_volume_down())
194 keys_post_event(KEY_VOLUMEDOWN, 1);
195
196 if(target_volume_up())
197 keys_post_event(KEY_VOLUMEUP, 1);
198}
199
200/* Configure PMIC and Drop PS_HOLD for shutdown */
201void shutdown_device()
202{
203 dprintf(CRITICAL, "Going down for shutdown.\n");
204
205 /* Configure PMIC for shutdown */
206 pm8x41_reset_configure(PON_PSHOLD_SHUTDOWN);
207
208 /* Drop PS_HOLD for MSM */
209 writel(0x00, MPM2_MPM_PS_HOLD);
210
211 mdelay(5000);
212
213 dprintf(CRITICAL, "shutdown failed\n");
214
215 ASSERT(0);
216}
217
218
219void target_init(void)
220{
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530221 dprintf(INFO, "target_init()\n");
222
223 spmi_init(PMIC_ARB_CHANNEL_NUM, PMIC_ARB_OWNER_ID);
224
225 target_keystatus();
226
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530227 target_sdc_init();
228 if (partition_read_table())
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530229 {
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530230 dprintf(CRITICAL, "Error reading the partition table info\n");
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530231 ASSERT(0);
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530232 }
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530233
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530234#if LONG_PRESS_POWER_ON
235 shutdown_detect();
236#endif
Aparna Mallavarapu1e8b0932015-03-29 23:38:13 +0530237 if (target_use_signed_kernel())
238 target_crypto_init_params();
Aparna Mallavarapufa5f8a72015-03-31 06:21:36 +0530239
240#if SMD_SUPPORT
241 rpm_smd_init();
242#endif
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530243}
244
245void target_serialno(unsigned char *buf)
246{
247 uint32_t serialno;
248 if (target_is_emmc_boot()) {
249 serialno = mmc_get_psn();
250 snprintf((char *)buf, 13, "%x", serialno);
251 }
252}
253
254unsigned board_machtype(void)
255{
Aparna Mallavarapue9bdacd2015-03-15 14:24:21 +0530256 return LINUX_MACHTYPE_UNKNOWN;
257}
258
259/* Detect the target type */
260void target_detect(struct board_data *board)
261{
262 /* This is already filled as part of board.c */
263}
264
265/* Detect the modem type */
266void target_baseband_detect(struct board_data *board)
267{
268 uint32_t platform;
269
270 platform = board->platform;
271
272 switch(platform) {
273 case MSM8952:
274 case MSM8956:
275 case MSM8976:
276 board->baseband = BASEBAND_MSM;
277 break;
278 default:
279 dprintf(CRITICAL, "Platform type: %u is not supported\n",platform);
280 ASSERT(0);
281 };
282}
283
284unsigned target_baseband()
285{
286 return board_baseband();
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530287}
288
289unsigned check_reboot_mode(void)
290{
291 uint32_t restart_reason = 0;
292
293 /* Read reboot reason and scrub it */
294 restart_reason = readl(RESTART_REASON_ADDR);
295 writel(0x00, RESTART_REASON_ADDR);
296
297 return restart_reason;
298}
299
300unsigned check_hard_reboot_mode(void)
301{
302 uint8_t hard_restart_reason = 0;
303 uint8_t value = 0;
304
305 /* Read reboot reason and scrub it
306 * Bit-5, bit-6 and bit-7 of SOFT_RB_SPARE for hard reset reason
307 */
308 value = pm8x41_reg_read(PON_SOFT_RB_SPARE);
309 hard_restart_reason = value >> 5;
310 pm8x41_reg_write(PON_SOFT_RB_SPARE, value & 0x1f);
311
312 return hard_restart_reason;
313}
314
315int set_download_mode(enum dload_mode mode)
316{
317 int ret = 0;
318 ret = scm_dload_mode(mode);
319
320 pm8x41_clear_pmic_watchdog();
321
322 return ret;
323}
324
325int emmc_recovery_init(void)
326{
327 return _emmc_recovery_init();
328}
329
330void reboot_device(unsigned reboot_reason)
331{
332 uint8_t reset_type = 0;
333 uint32_t ret = 0;
334
335 /* Need to clear the SW_RESET_ENTRY register and
336 * write to the BOOT_MISC_REG for known reset cases
337 */
338 if(reboot_reason != DLOAD)
339 scm_dload_mode(NORMAL_MODE);
340
341 writel(reboot_reason, RESTART_REASON_ADDR);
342
343 /* For Reboot-bootloader and Dload cases do a warm reset
344 * For Reboot cases do a hard reset
345 */
Aparna Mallavarapu680a1332015-04-29 19:14:09 +0530346 if((reboot_reason == FASTBOOT_MODE) || (reboot_reason == DLOAD) || (reboot_reason == RECOVERY_MODE))
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530347 reset_type = PON_PSHOLD_WARM_RESET;
348 else
349 reset_type = PON_PSHOLD_HARD_RESET;
350
351 pm8x41_reset_configure(reset_type);
352
353 ret = scm_halt_pmic_arbiter();
354 if (ret)
355 dprintf(CRITICAL , "Failed to halt pmic arbiter: %d\n", ret);
356
357 /* Drop PS_HOLD for MSM */
358 writel(0x00, MPM2_MPM_PS_HOLD);
359
360 mdelay(5000);
361
362 dprintf(CRITICAL, "Rebooting failed\n");
363}
364
365#if USER_FORCE_RESET_SUPPORT
366/* Return 1 if it is a force resin triggered by user. */
367uint32_t is_user_force_reset(void)
368{
369 uint8_t poff_reason1 = pm8x41_get_pon_poff_reason1();
370 uint8_t poff_reason2 = pm8x41_get_pon_poff_reason2();
371
372 dprintf(SPEW, "poff_reason1: %d\n", poff_reason1);
373 dprintf(SPEW, "poff_reason2: %d\n", poff_reason2);
374 if (pm8x41_get_is_cold_boot() && (poff_reason1 == KPDPWR_AND_RESIN ||
375 poff_reason2 == STAGE3))
376 return 1;
377 else
378 return 0;
379}
380#endif
381
382unsigned target_pause_for_battery_charge(void)
383{
384 uint8_t pon_reason = pm8x41_get_pon_reason();
385 uint8_t is_cold_boot = pm8x41_get_is_cold_boot();
386 dprintf(INFO, "%s : pon_reason is %d cold_boot:%d\n", __func__,
387 pon_reason, is_cold_boot);
388 /* In case of fastboot reboot,adb reboot or if we see the power key
389 * pressed we do not want go into charger mode.
390 * fastboot reboot is warm boot with PON hard reset bit not set
391 * adb reboot is a cold boot with PON hard reset bit set
392 */
393 if (is_cold_boot &&
394 (!(pon_reason & HARD_RST)) &&
395 (!(pon_reason & KPDPWR_N)) &&
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530396 ((pon_reason & USB_CHG)))
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530397 return 1;
398 else
399 return 0;
400}
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530401
402void target_uninit(void)
403{
404 mmc_put_card_to_sleep(dev);
405 sdhci_mode_disable(&dev->host);
Aparna Mallavarapu1e8b0932015-03-29 23:38:13 +0530406 if (crypto_initialized())
407 crypto_eng_cleanup();
408
409 if (target_is_ssd_enabled())
410 clock_ce_disable(CE1_INSTANCE);
Aparna Mallavarapufa5f8a72015-03-31 06:21:36 +0530411
412#if SMD_SUPPORT
413 rpm_smd_uninit();
414#endif
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530415}
416
417void target_usb_init(void)
418{
419 uint32_t val;
420
421 /* Select and enable external configuration with USB PHY */
422 ulpi_write(ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT, ULPI_MISC_A_SET);
423
424 /* Enable sess_vld */
425 val = readl(USB_GENCONFIG_2) | GEN2_SESS_VLD_CTRL_EN;
426 writel(val, USB_GENCONFIG_2);
427
428 /* Enable external vbus configuration in the LINK */
429 val = readl(USB_USBCMD);
430 val |= SESS_VLD_CTRL;
431 writel(val, USB_USBCMD);
432}
433
434void target_usb_stop(void)
435{
436 /* Disable VBUS mimicing in the controller. */
437 ulpi_write(ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT, ULPI_MISC_A_CLEAR);
438}
Aparna Mallavarapu1e8b0932015-03-29 23:38:13 +0530439
Padmanabhan Komanduru9d49f892015-04-10 12:58:46 -0700440static uint8_t splash_override;
441/* Returns 1 if target supports continuous splash screen. */
442int target_cont_splash_screen()
443{
444 uint8_t splash_screen = 0;
445 if (!splash_override) {
446 switch (board_hardware_id()) {
447 case HW_PLATFORM_MTP:
448 case HW_PLATFORM_SURF:
449 splash_screen = 1;
450 break;
451 default:
452 splash_screen = 0;
453 break;
454 }
455 dprintf(SPEW, "Target_cont_splash=%d\n", splash_screen);
456 }
457 return splash_screen;
458}
459
460void target_force_cont_splash_disable(uint8_t override)
461{
462 splash_override = override;
463}
464
Aparna Mallavarapu1e8b0932015-03-29 23:38:13 +0530465/* Do any target specific intialization needed before entering fastboot mode */
466void target_fastboot_init(void)
467{
468 if (target_is_ssd_enabled()) {
469 clock_ce_enable(CE1_INSTANCE);
470 target_load_ssd_keystore();
471 }
472}
473
474void target_load_ssd_keystore(void)
475{
476 uint64_t ptn;
477 int index;
478 uint64_t size;
479 uint32_t *buffer = NULL;
480
481 if (!target_is_ssd_enabled())
482 return;
483
484 index = partition_get_index("ssd");
485
486 ptn = partition_get_offset(index);
487 if (ptn == 0){
488 dprintf(CRITICAL, "Error: ssd partition not found\n");
489 return;
490 }
491
492 size = partition_get_size(index);
493 if (size == 0) {
494 dprintf(CRITICAL, "Error: invalid ssd partition size\n");
495 return;
496 }
497
498 buffer = memalign(CACHE_LINE, ROUNDUP(size, CACHE_LINE));
499 if (!buffer) {
500 dprintf(CRITICAL, "Error: allocating memory for ssd buffer\n");
501 return;
502 }
503
504 if (mmc_read(ptn, buffer, size)) {
505 dprintf(CRITICAL, "Error: cannot read data\n");
506 free(buffer);
507 return;
508 }
509
510 clock_ce_enable(CE1_INSTANCE);
511 scm_protect_keystore(buffer, size);
512 clock_ce_disable(CE1_INSTANCE);
513 free(buffer);
514}
515
516crypto_engine_type board_ce_type(void)
517{
518 return CRYPTO_ENGINE_TYPE_HW;
519}
520
521/* Set up params for h/w CE. */
522void target_crypto_init_params()
523{
524 struct crypto_init_params ce_params;
525
526 /* Set up base addresses and instance. */
527 ce_params.crypto_instance = CE1_INSTANCE;
528 ce_params.crypto_base = MSM_CE1_BASE;
529 ce_params.bam_base = MSM_CE1_BAM_BASE;
530
531 /* Set up BAM config. */
532 ce_params.bam_ee = CE_EE;
533 ce_params.pipes.read_pipe = CE_READ_PIPE;
534 ce_params.pipes.write_pipe = CE_WRITE_PIPE;
535 ce_params.pipes.read_pipe_grp = CE_READ_PIPE_LOCK_GRP;
536 ce_params.pipes.write_pipe_grp = CE_WRITE_PIPE_LOCK_GRP;
537
538 /* Assign buffer sizes. */
539 ce_params.num_ce = CE_ARRAY_SIZE;
540 ce_params.read_fifo_size = CE_FIFO_SIZE;
541 ce_params.write_fifo_size = CE_FIFO_SIZE;
542
543 /* BAM is initialized by TZ for this platform.
544 * Do not do it again as the initialization address space
545 * is locked.
546 */
547 ce_params.do_bam_init = 0;
548
549 crypto_init_params(&ce_params);
550}