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