blob: 2659ee224b8c9f2f6d5658ccfe75316d85f2a872 [file] [log] [blame]
Channagoud Kadabif8ad8e72015-01-06 15:10:13 -08001/* Copyright (c) 2014-2015 The Linux Foundation. All rights reserved.
Channagoud Kadabied60a8b2014-06-27 15:35:09 -07002 *
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 <platform/irqs.h>
32#include <platform/gpio.h>
33#include <reg.h>
34#include <target.h>
35#include <platform.h>
36#include <dload_util.h>
37#include <uart_dm.h>
38#include <mmc.h>
39#include <spmi.h>
40#include <board.h>
41#include <smem.h>
42#include <baseband.h>
Sridhar Parasuramd75ade52015-03-09 15:45:16 -070043#include <regulator.h>
Channagoud Kadabied60a8b2014-06-27 15:35:09 -070044#include <dev/keys.h>
45#include <pm8x41.h>
46#include <crypto5_wrapper.h>
47#include <clock.h>
48#include <partition_parser.h>
49#include <scm.h>
50#include <platform/clock.h>
51#include <platform/gpio.h>
52#include <platform/timer.h>
53#include <stdlib.h>
54#include <ufs.h>
55#include <boot_device.h>
56#include <qmp_phy.h>
Channagoud Kadabi7d308202014-12-22 12:07:04 -080057#include <sdhci_msm.h>
58#include <qusb2_phy.h>
Channagoud Kadabi2bab29b2015-02-11 13:26:03 -080059#include <rpmb.h>
Sridhar Parasuram9f28f672015-03-17 15:40:47 -070060#include <rpm-glink.h>
Channagoud Kadabied60a8b2014-06-27 15:35:09 -070061
Channagoud Kadabi4a870cf2015-01-21 10:39:01 -080062#define CE_INSTANCE 1
63#define CE_EE 1
64#define CE_FIFO_SIZE 64
65#define CE_READ_PIPE 3
66#define CE_WRITE_PIPE 2
67#define CE_READ_PIPE_LOCK_GRP 0
68#define CE_WRITE_PIPE_LOCK_GRP 0
69#define CE_ARRAY_SIZE 20
70
Channagoud Kadabied60a8b2014-06-27 15:35:09 -070071#define PMIC_ARB_CHANNEL_NUM 0
72#define PMIC_ARB_OWNER_ID 0
73
74static void set_sdc_power_ctrl(void);
75static uint32_t mmc_pwrctl_base[] =
76 { MSM_SDC1_BASE, MSM_SDC2_BASE };
77
78static 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 };
83
84struct mmc_device *dev;
85struct ufs_dev ufs_device;
86
Channagoud Kadabied60a8b2014-06-27 15:35:09 -070087void target_early_init(void)
88{
89#if WITH_DEBUG_UART
Channagoud Kadabi35503c42014-11-14 16:22:43 -080090 uart_dm_init(8, 0, BLSP2_UART1_BASE);
Channagoud Kadabied60a8b2014-06-27 15:35:09 -070091#endif
92}
93
94/* Return 1 if vol_up pressed */
95static int target_volume_up()
96{
97 uint8_t status = 0;
98 struct pm8x41_gpio gpio;
99
100 /* Configure the GPIO */
101 gpio.direction = PM_GPIO_DIR_IN;
102 gpio.function = 0;
103 gpio.pull = PM_GPIO_PULL_UP_30;
104 gpio.vin_sel = 2;
105
106 pm8x41_gpio_config(2, &gpio);
107
108 /* Wait for the pmic gpio config to take effect */
109 thread_sleep(1);
110
111 /* Get status of P_GPIO_5 */
Channagoud Kadabi99d23702015-02-02 20:52:17 -0800112 pm8x41_gpio_get(2, &status);
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700113
114 return !status; /* active low */
115}
116
117/* Return 1 if vol_down pressed */
118uint32_t target_volume_down()
119{
120 return pm8x41_resin_status();
121}
122
123static void target_keystatus()
124{
125 keys_init();
126
127 if(target_volume_down())
128 keys_post_event(KEY_VOLUMEDOWN, 1);
129
130 if(target_volume_up())
131 keys_post_event(KEY_VOLUMEUP, 1);
132}
133
134void target_uninit(void)
135{
136 if (platform_boot_dev_isemmc())
137 {
138 mmc_put_card_to_sleep(dev);
139 /* Disable HC mode before jumping to kernel */
140 sdhci_mode_disable(&dev->host);
141 }
Channagoud Kadabi2bab29b2015-02-11 13:26:03 -0800142
143 if (is_sec_app_loaded())
144 {
145 if (unload_sec_app() < 0)
146 {
147 dprintf(CRITICAL, "Failed to unload App for rpmb\n");
148 ASSERT(0);
149 }
150 }
151
Sridhar Parasuram9f28f672015-03-17 15:40:47 -0700152 /* Tear down glink channels */
153 rpm_glink_uninit();
154
Channagoud Kadabi2bab29b2015-02-11 13:26:03 -0800155 if (rpmb_uninit() < 0)
156 {
157 dprintf(CRITICAL, "RPMB uninit failed\n");
158 ASSERT(0);
159 }
160
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700161}
162
163static void set_sdc_power_ctrl()
164{
165 /* Drive strength configs for sdc pins */
166 struct tlmm_cfgs sdc1_hdrv_cfg[] =
167 {
168 { SDC1_CLK_HDRV_CTL_OFF, TLMM_CUR_VAL_16MA, TLMM_HDRV_MASK, SDC1_HDRV_PULL_CTL },
169 { SDC1_CMD_HDRV_CTL_OFF, TLMM_CUR_VAL_10MA, TLMM_HDRV_MASK, SDC1_HDRV_PULL_CTL },
170 { SDC1_DATA_HDRV_CTL_OFF, TLMM_CUR_VAL_10MA, TLMM_HDRV_MASK, SDC1_HDRV_PULL_CTL },
171 };
172
173 /* Pull configs for sdc pins */
174 struct tlmm_cfgs sdc1_pull_cfg[] =
175 {
176 { SDC1_CLK_PULL_CTL_OFF, TLMM_NO_PULL, TLMM_PULL_MASK, SDC1_HDRV_PULL_CTL },
177 { SDC1_CMD_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, SDC1_HDRV_PULL_CTL },
178 { SDC1_DATA_PULL_CTL_OFF, TLMM_PULL_UP, TLMM_PULL_MASK, SDC1_HDRV_PULL_CTL },
179 };
180
181 struct tlmm_cfgs sdc1_rclk_cfg[] =
182 {
183 { SDC1_RCLK_PULL_CTL_OFF, TLMM_PULL_DOWN, TLMM_PULL_MASK, SDC1_HDRV_PULL_CTL },
184 };
185
186 /* Set the drive strength & pull control values */
187 tlmm_set_hdrive_ctrl(sdc1_hdrv_cfg, ARRAY_SIZE(sdc1_hdrv_cfg));
188 tlmm_set_pull_ctrl(sdc1_pull_cfg, ARRAY_SIZE(sdc1_pull_cfg));
189 tlmm_set_pull_ctrl(sdc1_rclk_cfg, ARRAY_SIZE(sdc1_rclk_cfg));
190}
191
192void target_sdc_init()
193{
194 struct mmc_config_data config = {0};
195
196 /* Set drive strength & pull ctrl values */
197 set_sdc_power_ctrl();
198
199 config.bus_width = DATA_BUS_WIDTH_8BIT;
200 config.max_clk_rate = MMC_CLK_192MHZ;
Channagoud Kadabi99d23702015-02-02 20:52:17 -0800201 config.hs400_support = 1;
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700202
203 /* Try slot 1*/
204 config.slot = 1;
205 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
206 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
207 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
208
209 if (!(dev = mmc_init(&config)))
210 {
211 /* Try slot 2 */
212 config.slot = 2;
213 config.max_clk_rate = MMC_CLK_200MHZ;
214 config.sdhc_base = mmc_sdhci_base[config.slot - 1];
215 config.pwrctl_base = mmc_pwrctl_base[config.slot - 1];
216 config.pwr_irq = mmc_sdc_pwrctl_irq[config.slot - 1];
217
218 if (!(dev = mmc_init(&config)))
219 {
220 dprintf(CRITICAL, "mmc init failed!");
221 ASSERT(0);
222 }
223 }
224}
225
226void *target_mmc_device()
227{
228 if (platform_boot_dev_isemmc())
229 return (void *) dev;
230 else
231 return (void *) &ufs_device;
232}
233
234void target_init(void)
235{
236 dprintf(INFO, "target_init()\n");
237
Sridhar Parasuram1d224322015-06-15 11:03:40 -0700238 pmic_info_populate();
239
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700240 spmi_init(PMIC_ARB_CHANNEL_NUM, PMIC_ARB_OWNER_ID);
241
242 target_keystatus();
243
244 if (target_use_signed_kernel())
245 target_crypto_init_params();
246
247 platform_read_boot_config();
248
Sridhar Parasuram50b9d962015-02-12 11:28:09 -0800249#ifdef MMC_SDHCI_SUPPORT
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700250 if (platform_boot_dev_isemmc())
251 {
252 target_sdc_init();
253 }
Sridhar Parasuram50b9d962015-02-12 11:28:09 -0800254#endif
255#ifdef UFS_SUPPORT
256 if (!platform_boot_dev_isemmc())
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700257 {
258 ufs_device.base = UFS_BASE;
259 ufs_init(&ufs_device);
260 }
Sridhar Parasuram50b9d962015-02-12 11:28:09 -0800261#endif
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700262
263 /* Storage initialization is complete, read the partition table info */
Channagoud Kadabi58a273b2015-02-10 12:56:22 -0800264 mmc_read_partition_table(0);
Channagoud Kadabi2bab29b2015-02-11 13:26:03 -0800265
266 if (rpmb_init() < 0)
267 {
268 dprintf(CRITICAL, "RPMB init failed\n");
269 ASSERT(0);
270 }
Sridhar Parasuram9f28f672015-03-17 15:40:47 -0700271 /* Initialize Glink */
272 rpm_glink_init();
273
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700274}
275
276unsigned board_machtype(void)
277{
278 return LINUX_MACHTYPE_UNKNOWN;
279}
280
281/* Detect the target type */
282void target_detect(struct board_data *board)
283{
284 /* This is filled from board.c */
285}
286
Dhaval Patelb95039c2015-03-16 11:14:06 -0700287static uint8_t splash_override;
288/* Returns 1 if target supports continuous splash screen. */
289int target_cont_splash_screen()
290{
291 uint8_t splash_screen = 0;
292 if(!splash_override) {
293 switch(board_hardware_id())
294 {
295 case HW_PLATFORM_SURF:
296 case HW_PLATFORM_MTP:
297 case HW_PLATFORM_FLUID:
298 dprintf(SPEW, "Target_cont_splash=1\n");
299 splash_screen = 1;
300 break;
301 default:
302 dprintf(SPEW, "Target_cont_splash=0\n");
303 splash_screen = 0;
304 }
305 }
306 return splash_screen;
307}
308
309void target_force_cont_splash_disable(uint8_t override)
310{
311 splash_override = override;
312}
313
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700314/* Detect the modem type */
315void target_baseband_detect(struct board_data *board)
316{
317 uint32_t platform;
318
319 platform = board->platform;
320
321 switch(platform) {
Channagoud Kadabi439df822015-05-26 11:14:16 -0700322 case APQ8096:
323 board->baseband = BASEBAND_APQ;
324 break;
Channagoud Kadabi4a4c05e2015-03-30 15:18:58 -0700325 case MSM8996:
Channagoud Kadabi99d23702015-02-02 20:52:17 -0800326 if (board->platform_version == 0x10000)
327 board->baseband = BASEBAND_APQ;
328 else
329 board->baseband = BASEBAND_MSM;
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700330 break;
331 default:
332 dprintf(CRITICAL, "Platform type: %u is not supported\n",platform);
333 ASSERT(0);
334 };
335}
336unsigned target_baseband()
337{
338 return board_baseband();
339}
340
341void target_serialno(unsigned char *buf)
342{
343 unsigned int serialno;
344 if (target_is_emmc_boot()) {
345 serialno = mmc_get_psn();
346 snprintf((char *)buf, 13, "%x", serialno);
347 }
348}
349
350unsigned check_reboot_mode(void)
351{
352 uint32_t restart_reason = 0;
353 uint32_t restart_reason_addr;
354
355 restart_reason_addr = RESTART_REASON_ADDR;
356
357 /* Read reboot reason and scrub it */
358 restart_reason = readl(restart_reason_addr);
359 writel(0x00, restart_reason_addr);
360
361 return restart_reason;
362}
363
364void reboot_device(unsigned reboot_reason)
365{
366 uint8_t reset_type = 0;
367
368 /* Write the reboot reason */
369 writel(reboot_reason, RESTART_REASON_ADDR);
370
371 if(reboot_reason)
372 reset_type = PON_PSHOLD_WARM_RESET;
373 else
374 reset_type = PON_PSHOLD_HARD_RESET;
375
376 pm8x41_reset_configure(reset_type);
377
378 /* Drop PS_HOLD for MSM */
379 writel(0x00, MPM2_MPM_PS_HOLD);
380
381 mdelay(5000);
382
383 dprintf(CRITICAL, "Rebooting failed\n");
384}
385
386int emmc_recovery_init(void)
387{
388 return _emmc_recovery_init();
389}
390
391void target_usb_phy_reset()
392{
393 usb30_qmp_phy_reset();
394 qusb2_phy_reset();
395}
396
397target_usb_iface_t* target_usb30_init()
398{
399 target_usb_iface_t *t_usb_iface;
400
401 t_usb_iface = calloc(1, sizeof(target_usb_iface_t));
402 ASSERT(t_usb_iface);
403
404 t_usb_iface->phy_init = usb30_qmp_phy_init;
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700405 t_usb_iface->phy_reset = target_usb_phy_reset;
406 t_usb_iface->clock_init = clock_usb30_init;
407 t_usb_iface->vbus_override = 1;
408
409 return t_usb_iface;
410}
411
412/* identify the usb controller to be used for the target */
413const char * target_usb_controller()
414{
415 return "dwc";
416}
417
418uint32_t target_override_pll()
419{
Channagoud Kadabi1e5144b2015-04-28 17:15:05 -0700420 if (board_soc_version() >= 0x20000)
421 return 0;
422 else
423 return 1;
Channagoud Kadabied60a8b2014-06-27 15:35:09 -0700424}
425
Channagoud Kadabi4a870cf2015-01-21 10:39:01 -0800426crypto_engine_type board_ce_type(void)
427{
428 return CRYPTO_ENGINE_TYPE_SW;
429}
430
431/* Set up params for h/w CE. */
432void target_crypto_init_params()
433{
434 struct crypto_init_params ce_params;
435
436 /* Set up base addresses and instance. */
437 ce_params.crypto_instance = CE_INSTANCE;
438 ce_params.crypto_base = MSM_CE_BASE;
439 ce_params.bam_base = MSM_CE_BAM_BASE;
440
441 /* Set up BAM config. */
442 ce_params.bam_ee = CE_EE;
443 ce_params.pipes.read_pipe = CE_READ_PIPE;
444 ce_params.pipes.write_pipe = CE_WRITE_PIPE;
445 ce_params.pipes.read_pipe_grp = CE_READ_PIPE_LOCK_GRP;
446 ce_params.pipes.write_pipe_grp = CE_WRITE_PIPE_LOCK_GRP;
447
448 /* Assign buffer sizes. */
449 ce_params.num_ce = CE_ARRAY_SIZE;
450 ce_params.read_fifo_size = CE_FIFO_SIZE;
451 ce_params.write_fifo_size = CE_FIFO_SIZE;
452
453 /* BAM is initialized by TZ for this platform.
454 * Do not do it again as the initialization address space
455 * is locked.
456 */
457 ce_params.do_bam_init = 0;
458
459 crypto_init_params(&ce_params);
460}
Channagoud Kadabi083290f2015-03-13 14:18:38 -0700461
462unsigned target_pause_for_battery_charge(void)
463{
464 uint8_t pon_reason = pm8x41_get_pon_reason();
465 uint8_t is_cold_boot = pm8x41_get_is_cold_boot();
466 dprintf(INFO, "%s : pon_reason is %d cold_boot:%d\n", __func__,
467 pon_reason, is_cold_boot);
468 /* In case of fastboot reboot,adb reboot or if we see the power key
469 * pressed we do not want go into charger mode.
470 * fastboot reboot is warm boot with PON hard reset bit not set
471 * adb reboot is a cold boot with PON hard reset bit set
472 */
473 if (is_cold_boot &&
474 (!(pon_reason & HARD_RST)) &&
475 (!(pon_reason & KPDPWR_N)) &&
476 ((pon_reason & PON1)))
477 return 1;
478 else
479 return 0;
480}
Channagoud Kadabi23edc0c2015-03-27 18:31:32 -0700481
482int set_download_mode(enum dload_mode mode)
483{
484 int ret = 0;
485 ret = scm_dload_mode(mode);
486
487 return ret;
488}