blob: 40776f72b3579ab991dc9d24f35564672c7e6c08 [file] [log] [blame]
Mayank Grover42664672017-03-06 17:57:26 +05301/* Copyright (c) 2015,2017, The Linux Foundation. All rights reserved.
vijay kumarca1672a2015-04-09 16:45:40 +05302 *
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 <platform/gpio.h>
36#include <lib/ptable.h>
37#include <qpic_nand.h>
38#include <dev/keys.h>
39#include <spmi_v2.h>
40#include <pm8x41.h>
41#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>
49#include <crypto5_wrapper.h>
50#include <partition_parser.h>
51#include <stdlib.h>
Mayank Grover6372b6b2017-03-03 17:55:24 +053052#include <regulator.h>
vijay kumarca1672a2015-04-09 16:45:40 +053053
54#if LONG_PRESS_POWER_ON
55#include <shutdown_detect.h>
56#endif
57
58#define FASTBOOT_MODE 0x77665500
59#define PON_SOFT_RB_SPARE 0x88F
60
61extern void smem_ptable_init(void);
62extern void smem_add_modem_partitions(struct ptable *flash_ptable);
63
64static struct ptable flash_ptable;
65
66/* PMIC config data */
67#define PMIC_ARB_CHANNEL_NUM 0
68#define PMIC_ARB_OWNER_ID 0
Mayank Grovereb3fb552017-06-07 14:18:12 +053069#define PMIC_MAJOR_V1 1
vijay kumarca1672a2015-04-09 16:45:40 +053070
71/* NANDc BAM pipe numbers */
72#define DATA_CONSUMER_PIPE 0
73#define DATA_PRODUCER_PIPE 1
74#define CMD_PIPE 2
75
76/* NANDc BAM pipe groups */
77#define DATA_PRODUCER_PIPE_GRP 0
78#define DATA_CONSUMER_PIPE_GRP 0
79#define CMD_PIPE_GRP 1
80
81/* NANDc EE */
82#define QPIC_NAND_EE 0
83
84/* NANDc max desc length. */
85#define QPIC_NAND_MAX_DESC_LEN 0x7FFF
86
87#define LAST_NAND_PTN_LEN_PATTERN 0xFFFFFFFF
vijay kumarfc07e142015-08-31 16:46:41 +053088#define UBI_CMDLINE " rootfstype=ubifs rootflags=bulk_read"
vijay kumarca1672a2015-04-09 16:45:40 +053089
vijay kumar70dbe3e2015-11-20 13:07:30 +053090#define CE1_INSTANCE 1
91#define CE_EE 1
92#define CE_FIFO_SIZE 64
93#define CE_READ_PIPE 3
94#define CE_WRITE_PIPE 2
95#define CE_READ_PIPE_LOCK_GRP 0
96#define CE_WRITE_PIPE_LOCK_GRP 0
97#define CE_ARRAY_SIZE 20
98#define SUB_TYPE_SKUT 0x0A
99
vijay kumarca1672a2015-04-09 16:45:40 +0530100struct qpic_nand_init_config config;
101
102void update_ptable_names(void)
103{
104 uint32_t ptn_index;
105 struct ptentry *ptentry_ptr = flash_ptable.parts;
106 struct ptentry *boot_ptn;
107 unsigned i;
108 uint32_t len;
109
110 /* Change all names to lower case. */
111 for (ptn_index = 0; ptn_index != (uint32_t)flash_ptable.count; ptn_index++)
112 {
113 len = strlen(ptentry_ptr[ptn_index].name);
114 for (i = 0; i < len; i++)
115 {
116 if (isupper(ptentry_ptr[ptn_index].name[i]))
117 {
118 ptentry_ptr[ptn_index].name[i] = tolower(ptentry_ptr[ptn_index].name[i]);
119 }
120 }
121 /* SBL fills in the last partition length as 0xFFFFFFFF.
122 * Update the length field based on the number of blocks on the flash.*/
123 if ((uint32_t)(ptentry_ptr[ptn_index].length) == LAST_NAND_PTN_LEN_PATTERN)
124 {
125 ptentry_ptr[ptn_index].length = flash_num_blocks() - ptentry_ptr[ptn_index].start;
126 }
127 }
128}
129
vijay kumar84e606d2015-10-15 18:36:36 +0530130/* Return Non zero (i.e 0x2) if vol_down pressed */
131uint32_t target_volume_down()
132{
133 /* Volume down button tied in with PMIC RESIN. */
134 return pm8x41_resin_status();
135}
136
137static void target_keystatus()
138{
139 keys_init();
140
141 if(target_volume_down())
142 keys_post_event(KEY_VOLUMEDOWN, 1);
143}
144
vijay kumarca1672a2015-04-09 16:45:40 +0530145void target_early_init(void)
146{
147#if WITH_DEBUG_UART
vijay kumar20627da2015-10-01 13:28:58 +0530148 /*BLSP1 and UART5*/
149 uart_dm_init(5, 0, BLSP1_UART5_BASE);
vijay kumarca1672a2015-04-09 16:45:40 +0530150#endif
151}
152
153/* Configure PMIC and Drop PS_HOLD for shutdown */
154void shutdown_device()
155{
156 dprintf(CRITICAL, "Going down for shutdown.\n");
157
158 /* Configure PMIC for shutdown */
159 pm8x41_reset_configure(PON_PSHOLD_SHUTDOWN);
160
161 /* Drop PS_HOLD for MSM */
162 writel(0x00, MPM2_MPM_PS_HOLD);
163
164 mdelay(5000);
165
166 dprintf(CRITICAL, "shutdown failed\n");
167
168 ASSERT(0);
169}
170
171
172void target_init(void)
173{
174 uint32_t base_addr;
175 uint8_t slot;
176
177 dprintf(INFO, "target_init()\n");
178
179 spmi_init(PMIC_ARB_CHANNEL_NUM, PMIC_ARB_OWNER_ID);
180
Mayank Grover6372b6b2017-03-03 17:55:24 +0530181#if SMD_SUPPORT
182 rpm_smd_init();
183#endif
vijay kumar84e606d2015-10-15 18:36:36 +0530184 target_keystatus();
vijay kumarca1672a2015-04-09 16:45:40 +0530185 config.pipes.read_pipe = DATA_PRODUCER_PIPE;
186 config.pipes.write_pipe = DATA_CONSUMER_PIPE;
187 config.pipes.cmd_pipe = CMD_PIPE;
188
189 config.pipes.read_pipe_grp = DATA_PRODUCER_PIPE_GRP;
190 config.pipes.write_pipe_grp = DATA_CONSUMER_PIPE_GRP;
191 config.pipes.cmd_pipe_grp = CMD_PIPE_GRP;
192
193 config.bam_base = MSM_NAND_BAM_BASE;
194 config.nand_base = MSM_NAND_BASE;
195 config.ee = QPIC_NAND_EE;
196 config.max_desc_len = QPIC_NAND_MAX_DESC_LEN;
197
198 qpic_nand_init(&config);
199
200 ptable_init(&flash_ptable);
vijay kumarec490942015-07-23 12:43:54 +0530201 smem_ptable_init();
202 smem_add_modem_partitions(&flash_ptable);
vijay kumarca1672a2015-04-09 16:45:40 +0530203
204 update_ptable_names();
205 flash_set_ptable(&flash_ptable);
vijay kumar70dbe3e2015-11-20 13:07:30 +0530206
207 if (target_use_signed_kernel())
208 target_crypto_init_params();
vijay kumarca1672a2015-04-09 16:45:40 +0530209}
210
211/* Identify the current target */
212void target_detect(struct board_data *board)
213{
214 /* This property is filled as part of board.c */
215}
216
217unsigned board_machtype(void)
218{
219}
220
221/* Identify the baseband being used */
222void target_baseband_detect(struct board_data *board)
223{
224 uint32_t platform = board->platform;
225
226 switch(platform)
227 {
vijay kumardb062a32015-10-30 12:32:15 +0530228 case MDM9607:
229 case MDM8207:
230 case MDM9207:
Mayank Grover42664672017-03-06 17:57:26 +0530231 case MDM9206:
vijay kumardb062a32015-10-30 12:32:15 +0530232 case MDM9307:
233 case MDM9628:
vijay kumarca1672a2015-04-09 16:45:40 +0530234 board->baseband = BASEBAND_MDM;
235 break;
236 default:
237 dprintf(CRITICAL, "Platform type: %u is not supported\n", platform);
238 ASSERT(0);
239 };
240}
241
vijay kumar00c1e792015-09-28 14:48:48 +0530242void target_serialno(unsigned char *buf)
243{
244 uint32_t serialno;
245 serialno = board_chip_serial();
vijay kumara1260412015-11-12 17:39:07 +0530246 snprintf((char *)buf, 13, "%x", serialno);
vijay kumar00c1e792015-09-28 14:48:48 +0530247}
248
vijay kumarca1672a2015-04-09 16:45:40 +0530249unsigned check_reboot_mode(void)
250{
251 uint32_t restart_reason = 0;
252
253 /* Read reboot reason and scrub it */
254 restart_reason = readl(RESTART_REASON_ADDR);
255 writel(0x00, RESTART_REASON_ADDR);
256
257 return restart_reason;
258}
259
vijay kumarfc07e142015-08-31 16:46:41 +0530260int get_target_boot_params(const char *cmdline, const char *part, char **buf)
vijay kumarca1672a2015-04-09 16:45:40 +0530261{
262 struct ptable *ptable;
263 int system_ptn_index = -1;
vijay kumarfc07e142015-08-31 16:46:41 +0530264 uint32_t buflen = strlen(UBI_CMDLINE) + strlen(" root=ubi0:rootfs ubi.mtd=") + sizeof(int) + 1; /* 1 byte for null character*/
265
Mayank Groveraea8b102017-01-04 18:20:49 +0530266 if (!cmdline || !part ) {
267 dprintf(CRITICAL, "WARN: Invalid input param\n");
268 return -1;
269 }
270
vijay kumarfc07e142015-08-31 16:46:41 +0530271 *buf = (char *)malloc(buflen);
272 if(!(*buf)) {
273 dprintf(CRITICAL,"Unable to allocate memory for boot params\n");
274 return -1;
275 }
vijay kumarca1672a2015-04-09 16:45:40 +0530276
277 ptable = flash_get_ptable();
278 if (!ptable) {
279 dprintf(CRITICAL,"WARN: Cannot get flash partition table\n");
vijay kumarfc07e142015-08-31 16:46:41 +0530280 free(*buf);
vijay kumarca1672a2015-04-09 16:45:40 +0530281 return -1;
282 }
283
284 system_ptn_index = ptable_get_index(ptable, part);
285 if (system_ptn_index < 0) {
286 dprintf(CRITICAL,"WARN: Cannot get partition index for %s\n", part);
vijay kumarfc07e142015-08-31 16:46:41 +0530287 free(*buf);
vijay kumarca1672a2015-04-09 16:45:40 +0530288 return -1;
289 }
vijay kumarfc07e142015-08-31 16:46:41 +0530290 /* Adding command line parameters according to target boot type */
291 snprintf(*buf, buflen, UBI_CMDLINE);
292
vijay kumarca1672a2015-04-09 16:45:40 +0530293 /*check if cmdline contains "root=" at the beginning of buffer or
294 * " root=" in the middle of buffer.
295 */
296 if (((!strncmp(cmdline, "root=", strlen("root="))) ||
297 (strstr(cmdline, " root="))))
298 dprintf(DEBUG, "DEBUG: cmdline has root=\n");
299 else
vijay kumarfc07e142015-08-31 16:46:41 +0530300 snprintf(*buf+strlen(*buf), buflen, " root=ubi0:rootfs ubi.mtd=%d", system_ptn_index);
301 /*in success case buf will be freed in the calling function of this*/
vijay kumarca1672a2015-04-09 16:45:40 +0530302 return 0;
303}
304
305void target_usb_init(void)
306{
307 uint32_t val;
308
309 /* Select and enable external configuration with USB PHY */
vijay kumarec490942015-07-23 12:43:54 +0530310 ulpi_write(ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT, ULPI_MISC_A_SET);
vijay kumarca1672a2015-04-09 16:45:40 +0530311
312 /* Enable sess_vld */
313 val = readl(USB_GENCONFIG_2) | GEN2_SESS_VLD_CTRL_EN;
314 writel(val, USB_GENCONFIG_2);
315
316 /* Enable external vbus configuration in the LINK */
317 val = readl(USB_USBCMD);
318 val |= SESS_VLD_CTRL;
319 writel(val, USB_USBCMD);
320}
321
322void target_uninit(void)
323{
vijay kumar70dbe3e2015-11-20 13:07:30 +0530324 if (crypto_initialized())
325 crypto_eng_cleanup();
Mayank Grover7881d022017-04-13 16:50:13 +0530326
327 if(platform_is_mdm9206())
328 regulator_disable(REG_LDO4);
329
Mayank Grover6372b6b2017-03-03 17:55:24 +0530330#if SMD_SUPPORT
331 rpm_smd_uninit();
332#endif
vijay kumarca1672a2015-04-09 16:45:40 +0530333}
334
335void reboot_device(unsigned reboot_reason)
336{
vijay kumarcefa8512015-10-16 12:54:30 +0530337 uint8_t reset_type = 0;
Mayank Grovereb3fb552017-06-07 14:18:12 +0530338 struct board_pmic_data pmic_info;
339
vijay kumarca1672a2015-04-09 16:45:40 +0530340 /* Write the reboot reason */
341 writel(reboot_reason, RESTART_REASON_ADDR);
342
343 /* Configure PMIC for warm reset */
344 /* PM 8019 v1 aligns with PM8941 v2.
345 * This call should be based on the pmic version
346 * when PM8019 v2 is available.
347 */
vijay kumarcefa8512015-10-16 12:54:30 +0530348 if(reboot_reason)
349 reset_type = PON_PSHOLD_WARM_RESET;
350 else
351 reset_type = PON_PSHOLD_HARD_RESET;
352
Mayank Grovereb3fb552017-06-07 14:18:12 +0530353 if (board_pmic_info(&pmic_info, SMEM_V7_SMEM_MAX_PMIC_DEVICES))
354 {
355 /* make decision based on pmic major version */
356 switch (pmic_info.pmic_version >>16)
357 {
358 case PMIC_MAJOR_V1:
359 pm8x41_v2_reset_configure(reset_type);
360 break;
361 default:
362 pm8x41_reset_configure(reset_type);
363 }
364 }
vijay kumarca1672a2015-04-09 16:45:40 +0530365
366 /* Drop PS_HOLD for MSM */
367 writel(0x00, MPM2_MPM_PS_HOLD);
368
369 mdelay(5000);
370
371 dprintf(CRITICAL, "Rebooting failed\n");
372 return;
373}
vijay kumar70dbe3e2015-11-20 13:07:30 +0530374
375crypto_engine_type board_ce_type(void)
376{
377 return CRYPTO_ENGINE_TYPE_HW;
378}
379
380/* Set up params for h/w CE. */
381void target_crypto_init_params()
382{
383 struct crypto_init_params ce_params;
384
385 /* Set up base addresses and instance. */
386 ce_params.crypto_instance = CE1_INSTANCE;
387 ce_params.crypto_base = MSM_CE1_BASE;
388 ce_params.bam_base = MSM_CE1_BAM_BASE;
389
390 /* Set up BAM config. */
391 ce_params.bam_ee = CE_EE;
392 ce_params.pipes.read_pipe = CE_READ_PIPE;
393 ce_params.pipes.write_pipe = CE_WRITE_PIPE;
394 ce_params.pipes.read_pipe_grp = CE_READ_PIPE_LOCK_GRP;
395 ce_params.pipes.write_pipe_grp = CE_WRITE_PIPE_LOCK_GRP;
396
397 /* Assign buffer sizes. */
398 ce_params.num_ce = CE_ARRAY_SIZE;
399 ce_params.read_fifo_size = CE_FIFO_SIZE;
400 ce_params.write_fifo_size = CE_FIFO_SIZE;
401
402 /* BAM is initialized by TZ for this platform.
403 * Do not do it again as the initialization address space
404 * is locked.
405 */
406 ce_params.do_bam_init = 0;
407 crypto_init_params(&ce_params);
408}
Mayank Grover6372b6b2017-03-03 17:55:24 +0530409
410void target_fastboot_init()
411{
412 /* Enable LDO4 for processing fastboot commands.*/
413 if(platform_is_mdm9206()){
414 regulator_enable(REG_LDO4);
415 dprintf(SPEW, "Enable LDO4\n");
416 }
417}