blob: 2e743c2118c3b1679057db021ff051bacbb46521 [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>
40#include <board.h>
41#include <baseband.h>
42#include <hsusb.h>
43#include <scm.h>
44#include <platform/gpio.h>
45#include <platform/gpio.h>
46#include <platform/irqs.h>
47#include <platform/clock.h>
48#include <crypto5_wrapper.h>
49#include <partition_parser.h>
50#include <stdlib.h>
51
52#if LONG_PRESS_POWER_ON
53#include <shutdown_detect.h>
54#endif
55
56#define PMIC_ARB_CHANNEL_NUM 0
57#define PMIC_ARB_OWNER_ID 0
58#define TLMM_VOL_UP_BTN_GPIO 85
59
60#define FASTBOOT_MODE 0x77665500
61#define PON_SOFT_RB_SPARE 0x88F
62
Aparna Mallavarapu1e8b0932015-03-29 23:38:13 +053063#define CE1_INSTANCE 1
64#define CE_EE 1
65#define CE_FIFO_SIZE 64
66#define CE_READ_PIPE 3
67#define CE_WRITE_PIPE 2
68#define CE_READ_PIPE_LOCK_GRP 0
69#define CE_WRITE_PIPE_LOCK_GRP 0
70#define CE_ARRAY_SIZE 20
71
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +053072struct mmc_device *dev;
73
74static uint32_t mmc_pwrctl_base[] =
Aparna Mallavarapuca676882015-01-19 20:39:06 +053075 { MSM_SDC1_BASE, MSM_SDC2_BASE };
76
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +053077static uint32_t mmc_sdhci_base[] =
78 { MSM_SDC1_SDHCI_BASE, MSM_SDC2_SDHCI_BASE };
79
80static uint32_t mmc_sdc_pwrctl_irq[] =
81 { SDCC1_PWRCTL_IRQ, SDCC2_PWRCTL_IRQ };
Aparna Mallavarapuca676882015-01-19 20:39:06 +053082
83void target_early_init(void)
84{
85#if WITH_DEBUG_UART
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +053086 uart_dm_init(2, 0, BLSP1_UART1_BASE);
Aparna Mallavarapuca676882015-01-19 20:39:06 +053087#endif
88}
89
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +053090static void set_sdc_power_ctrl()
Aparna Mallavarapuca676882015-01-19 20:39:06 +053091{
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +053092 /* Drive strength configs for sdc pins */
93 struct tlmm_cfgs sdc1_hdrv_cfg[] =
94 {
95 { SDC1_CLK_HDRV_CTL_OFF, TLMM_CUR_VAL_16MA, TLMM_HDRV_MASK, 0},
96 { SDC1_CMD_HDRV_CTL_OFF, TLMM_CUR_VAL_10MA, TLMM_HDRV_MASK, 0},
97 { SDC1_DATA_HDRV_CTL_OFF, TLMM_CUR_VAL_10MA, TLMM_HDRV_MASK , 0},
98 };
99
100 /* Pull configs for sdc pins */
101 struct tlmm_cfgs sdc1_pull_cfg[] =
102 {
103 { SDC1_CLK_PULL_CTL_OFF, TLMM_NO_PULL, TLMM_PULL_MASK, 0},
104 { SDC1_CMD_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, 0},
105 { SDC1_DATA_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, 0},
106 };
107
108 /* Set the drive strength & pull control values */
109 tlmm_set_hdrive_ctrl(sdc1_hdrv_cfg, ARRAY_SIZE(sdc1_hdrv_cfg));
110 tlmm_set_pull_ctrl(sdc1_pull_cfg, ARRAY_SIZE(sdc1_pull_cfg));
111}
112
113void target_sdc_init()
114{
115 struct mmc_config_data config;
116
117 /* Set drive strength & pull ctrl values */
118 set_sdc_power_ctrl();
119
120 /* Try slot 1*/
121 config.slot = 1;
122 config.bus_width = DATA_BUS_WIDTH_8BIT;
123 config.max_clk_rate = MMC_CLK_177MHZ;
124 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
125 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
126 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
127 config.hs400_support = 1;
128
129 if (!(dev = mmc_init(&config))) {
130 /* Try slot 2 */
131 config.slot = 2;
132 config.max_clk_rate = MMC_CLK_200MHZ;
133 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
134 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
135 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
136 config.hs400_support = 0;
137
138 if (!(dev = mmc_init(&config))) {
139 dprintf(CRITICAL, "mmc init failed!");
140 ASSERT(0);
141 }
142 }
143}
144
145void *target_mmc_device()
146{
147 return (void *) dev;
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530148}
149
150/* Return 1 if vol_up pressed */
151static int target_volume_up()
152{
153 uint8_t status = 0;
154
155 gpio_tlmm_config(TLMM_VOL_UP_BTN_GPIO, 0, GPIO_INPUT, GPIO_PULL_UP, GPIO_2MA, GPIO_ENABLE);
156
157 /* Wait for the gpio config to take effect - debounce time */
158 thread_sleep(10);
159
160 /* Get status of GPIO */
161 status = gpio_status(TLMM_VOL_UP_BTN_GPIO);
162
163 /* Active high signal. */
Aparna Mallavarapudb938b62015-04-09 01:00:55 +0530164 return !status;
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530165}
166
167/* Return 1 if vol_down pressed */
168uint32_t target_volume_down()
169{
170 /* Volume down button tied in with PMIC RESIN. */
171 return pm8x41_resin_status();
172}
173
174static void target_keystatus()
175{
176 keys_init();
177
178 if(target_volume_down())
179 keys_post_event(KEY_VOLUMEDOWN, 1);
180
181 if(target_volume_up())
182 keys_post_event(KEY_VOLUMEUP, 1);
183}
184
185/* Configure PMIC and Drop PS_HOLD for shutdown */
186void shutdown_device()
187{
188 dprintf(CRITICAL, "Going down for shutdown.\n");
189
190 /* Configure PMIC for shutdown */
191 pm8x41_reset_configure(PON_PSHOLD_SHUTDOWN);
192
193 /* Drop PS_HOLD for MSM */
194 writel(0x00, MPM2_MPM_PS_HOLD);
195
196 mdelay(5000);
197
198 dprintf(CRITICAL, "shutdown failed\n");
199
200 ASSERT(0);
201}
202
203
204void target_init(void)
205{
206 uint32_t base_addr;
207 uint8_t slot;
208
209 dprintf(INFO, "target_init()\n");
210
211 spmi_init(PMIC_ARB_CHANNEL_NUM, PMIC_ARB_OWNER_ID);
212
213 target_keystatus();
214
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530215 target_sdc_init();
216 if (partition_read_table())
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530217 {
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530218 dprintf(CRITICAL, "Error reading the partition table info\n");
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530219 ASSERT(0);
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530220 }
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530221
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530222#if LONG_PRESS_POWER_ON
223 shutdown_detect();
224#endif
Aparna Mallavarapu1e8b0932015-03-29 23:38:13 +0530225 if (target_use_signed_kernel())
226 target_crypto_init_params();
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530227}
228
229void target_serialno(unsigned char *buf)
230{
231 uint32_t serialno;
232 if (target_is_emmc_boot()) {
233 serialno = mmc_get_psn();
234 snprintf((char *)buf, 13, "%x", serialno);
235 }
236}
237
238unsigned board_machtype(void)
239{
Aparna Mallavarapue9bdacd2015-03-15 14:24:21 +0530240 return LINUX_MACHTYPE_UNKNOWN;
241}
242
243/* Detect the target type */
244void target_detect(struct board_data *board)
245{
246 /* This is already filled as part of board.c */
247}
248
249/* Detect the modem type */
250void target_baseband_detect(struct board_data *board)
251{
252 uint32_t platform;
253
254 platform = board->platform;
255
256 switch(platform) {
257 case MSM8952:
258 case MSM8956:
259 case MSM8976:
260 board->baseband = BASEBAND_MSM;
261 break;
262 default:
263 dprintf(CRITICAL, "Platform type: %u is not supported\n",platform);
264 ASSERT(0);
265 };
266}
267
268unsigned target_baseband()
269{
270 return board_baseband();
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530271}
272
273unsigned check_reboot_mode(void)
274{
275 uint32_t restart_reason = 0;
276
277 /* Read reboot reason and scrub it */
278 restart_reason = readl(RESTART_REASON_ADDR);
279 writel(0x00, RESTART_REASON_ADDR);
280
281 return restart_reason;
282}
283
284unsigned check_hard_reboot_mode(void)
285{
286 uint8_t hard_restart_reason = 0;
287 uint8_t value = 0;
288
289 /* Read reboot reason and scrub it
290 * Bit-5, bit-6 and bit-7 of SOFT_RB_SPARE for hard reset reason
291 */
292 value = pm8x41_reg_read(PON_SOFT_RB_SPARE);
293 hard_restart_reason = value >> 5;
294 pm8x41_reg_write(PON_SOFT_RB_SPARE, value & 0x1f);
295
296 return hard_restart_reason;
297}
298
299int set_download_mode(enum dload_mode mode)
300{
301 int ret = 0;
302 ret = scm_dload_mode(mode);
303
304 pm8x41_clear_pmic_watchdog();
305
306 return ret;
307}
308
309int emmc_recovery_init(void)
310{
311 return _emmc_recovery_init();
312}
313
314void reboot_device(unsigned reboot_reason)
315{
316 uint8_t reset_type = 0;
317 uint32_t ret = 0;
318
319 /* Need to clear the SW_RESET_ENTRY register and
320 * write to the BOOT_MISC_REG for known reset cases
321 */
322 if(reboot_reason != DLOAD)
323 scm_dload_mode(NORMAL_MODE);
324
325 writel(reboot_reason, RESTART_REASON_ADDR);
326
327 /* For Reboot-bootloader and Dload cases do a warm reset
328 * For Reboot cases do a hard reset
329 */
330 if((reboot_reason == FASTBOOT_MODE) || (reboot_reason == DLOAD))
331 reset_type = PON_PSHOLD_WARM_RESET;
332 else
333 reset_type = PON_PSHOLD_HARD_RESET;
334
335 pm8x41_reset_configure(reset_type);
336
337 ret = scm_halt_pmic_arbiter();
338 if (ret)
339 dprintf(CRITICAL , "Failed to halt pmic arbiter: %d\n", ret);
340
341 /* Drop PS_HOLD for MSM */
342 writel(0x00, MPM2_MPM_PS_HOLD);
343
344 mdelay(5000);
345
346 dprintf(CRITICAL, "Rebooting failed\n");
347}
348
349#if USER_FORCE_RESET_SUPPORT
350/* Return 1 if it is a force resin triggered by user. */
351uint32_t is_user_force_reset(void)
352{
353 uint8_t poff_reason1 = pm8x41_get_pon_poff_reason1();
354 uint8_t poff_reason2 = pm8x41_get_pon_poff_reason2();
355
356 dprintf(SPEW, "poff_reason1: %d\n", poff_reason1);
357 dprintf(SPEW, "poff_reason2: %d\n", poff_reason2);
358 if (pm8x41_get_is_cold_boot() && (poff_reason1 == KPDPWR_AND_RESIN ||
359 poff_reason2 == STAGE3))
360 return 1;
361 else
362 return 0;
363}
364#endif
365
366unsigned target_pause_for_battery_charge(void)
367{
368 uint8_t pon_reason = pm8x41_get_pon_reason();
369 uint8_t is_cold_boot = pm8x41_get_is_cold_boot();
370 dprintf(INFO, "%s : pon_reason is %d cold_boot:%d\n", __func__,
371 pon_reason, is_cold_boot);
372 /* In case of fastboot reboot,adb reboot or if we see the power key
373 * pressed we do not want go into charger mode.
374 * fastboot reboot is warm boot with PON hard reset bit not set
375 * adb reboot is a cold boot with PON hard reset bit set
376 */
377 if (is_cold_boot &&
378 (!(pon_reason & HARD_RST)) &&
379 (!(pon_reason & KPDPWR_N)) &&
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530380 ((pon_reason & USB_CHG)))
Aparna Mallavarapuca676882015-01-19 20:39:06 +0530381 return 1;
382 else
383 return 0;
384}
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530385
386void target_uninit(void)
387{
388 mmc_put_card_to_sleep(dev);
389 sdhci_mode_disable(&dev->host);
Aparna Mallavarapu1e8b0932015-03-29 23:38:13 +0530390 if (crypto_initialized())
391 crypto_eng_cleanup();
392
393 if (target_is_ssd_enabled())
394 clock_ce_disable(CE1_INSTANCE);
Aparna Mallavarapu7b638e62015-03-26 05:51:57 +0530395}
396
397void target_usb_init(void)
398{
399 uint32_t val;
400
401 /* Select and enable external configuration with USB PHY */
402 ulpi_write(ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT, ULPI_MISC_A_SET);
403
404 /* Enable sess_vld */
405 val = readl(USB_GENCONFIG_2) | GEN2_SESS_VLD_CTRL_EN;
406 writel(val, USB_GENCONFIG_2);
407
408 /* Enable external vbus configuration in the LINK */
409 val = readl(USB_USBCMD);
410 val |= SESS_VLD_CTRL;
411 writel(val, USB_USBCMD);
412}
413
414void target_usb_stop(void)
415{
416 /* Disable VBUS mimicing in the controller. */
417 ulpi_write(ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT, ULPI_MISC_A_CLEAR);
418}
Aparna Mallavarapu1e8b0932015-03-29 23:38:13 +0530419
420/* Do any target specific intialization needed before entering fastboot mode */
421void target_fastboot_init(void)
422{
423 if (target_is_ssd_enabled()) {
424 clock_ce_enable(CE1_INSTANCE);
425 target_load_ssd_keystore();
426 }
427}
428
429void target_load_ssd_keystore(void)
430{
431 uint64_t ptn;
432 int index;
433 uint64_t size;
434 uint32_t *buffer = NULL;
435
436 if (!target_is_ssd_enabled())
437 return;
438
439 index = partition_get_index("ssd");
440
441 ptn = partition_get_offset(index);
442 if (ptn == 0){
443 dprintf(CRITICAL, "Error: ssd partition not found\n");
444 return;
445 }
446
447 size = partition_get_size(index);
448 if (size == 0) {
449 dprintf(CRITICAL, "Error: invalid ssd partition size\n");
450 return;
451 }
452
453 buffer = memalign(CACHE_LINE, ROUNDUP(size, CACHE_LINE));
454 if (!buffer) {
455 dprintf(CRITICAL, "Error: allocating memory for ssd buffer\n");
456 return;
457 }
458
459 if (mmc_read(ptn, buffer, size)) {
460 dprintf(CRITICAL, "Error: cannot read data\n");
461 free(buffer);
462 return;
463 }
464
465 clock_ce_enable(CE1_INSTANCE);
466 scm_protect_keystore(buffer, size);
467 clock_ce_disable(CE1_INSTANCE);
468 free(buffer);
469}
470
471crypto_engine_type board_ce_type(void)
472{
473 return CRYPTO_ENGINE_TYPE_HW;
474}
475
476/* Set up params for h/w CE. */
477void target_crypto_init_params()
478{
479 struct crypto_init_params ce_params;
480
481 /* Set up base addresses and instance. */
482 ce_params.crypto_instance = CE1_INSTANCE;
483 ce_params.crypto_base = MSM_CE1_BASE;
484 ce_params.bam_base = MSM_CE1_BAM_BASE;
485
486 /* Set up BAM config. */
487 ce_params.bam_ee = CE_EE;
488 ce_params.pipes.read_pipe = CE_READ_PIPE;
489 ce_params.pipes.write_pipe = CE_WRITE_PIPE;
490 ce_params.pipes.read_pipe_grp = CE_READ_PIPE_LOCK_GRP;
491 ce_params.pipes.write_pipe_grp = CE_WRITE_PIPE_LOCK_GRP;
492
493 /* Assign buffer sizes. */
494 ce_params.num_ce = CE_ARRAY_SIZE;
495 ce_params.read_fifo_size = CE_FIFO_SIZE;
496 ce_params.write_fifo_size = CE_FIFO_SIZE;
497
498 /* BAM is initialized by TZ for this platform.
499 * Do not do it again as the initialization address space
500 * is locked.
501 */
502 ce_params.do_bam_init = 0;
503
504 crypto_init_params(&ce_params);
505}