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