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