xref: /optee_os/core/arch/arm/plat-hikey/main.c (revision 1bb929836182ecb96d2d9d268daa807c67596396)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2015, Linaro Limited
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * 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 BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <console.h>
30 #include <drivers/pl011.h>
31 #ifdef CFG_SPI
32 #include <drivers/pl022_spi.h>
33 #include <drivers/pl061_gpio.h>
34 #endif
35 #if defined(PLATFORM_FLAVOR_hikey)
36 #include <hikey_peripherals.h>
37 #endif
38 #include <initcall.h>
39 #include <io.h>
40 #include <kernel/generic_boot.h>
41 #include <kernel/panic.h>
42 #include <kernel/pm_stubs.h>
43 #include <mm/tee_pager.h>
44 #include <mm/core_memprot.h>
45 #include <platform_config.h>
46 #include <stdint.h>
47 #include <tee/entry_std.h>
48 #include <tee/entry_fast.h>
49 
50 static void main_fiq(void);
51 
52 static const struct thread_handlers handlers = {
53 	.std_smc = tee_entry_std,
54 	.fast_smc = tee_entry_fast,
55 	.nintr = main_fiq,
56 	.cpu_on = cpu_on_handler,
57 	.cpu_off = pm_do_nothing,
58 	.cpu_suspend = pm_do_nothing,
59 	.cpu_resume = pm_do_nothing,
60 	.system_off = pm_do_nothing,
61 	.system_reset = pm_do_nothing,
62 };
63 
64 static struct pl011_data console_data;
65 
66 register_phys_mem(MEM_AREA_IO_NSEC, CONSOLE_UART_BASE, PL011_REG_SIZE);
67 #if defined(PLATFORM_FLAVOR_hikey)
68 register_phys_mem(MEM_AREA_IO_NSEC, PMUSSI_BASE, PMUSSI_REG_SIZE);
69 #endif
70 #if defined(CFG_SPI) && defined(PLATFORM_FLAVOR_hikey)
71 register_phys_mem(MEM_AREA_IO_NSEC, PERI_BASE, PERI_BASE_REG_SIZE);
72 register_phys_mem(MEM_AREA_IO_NSEC, PMX0_BASE, PMX0_REG_SIZE);
73 register_phys_mem(MEM_AREA_IO_NSEC, PMX1_BASE, PMX1_REG_SIZE);
74 register_phys_mem(MEM_AREA_IO_NSEC, GPIO6_BASE, PL061_REG_SIZE);
75 register_phys_mem(MEM_AREA_IO_NSEC, SPI_BASE, PL022_REG_SIZE);
76 #endif
77 register_nsec_ddr(DRAM0_BASE, DRAM0_SIZE_NSEC);
78 
79 const struct thread_handlers *generic_boot_get_handlers(void)
80 {
81 	return &handlers;
82 }
83 
84 static void main_fiq(void)
85 {
86 	panic();
87 }
88 
89 void console_init(void)
90 {
91 	pl011_init(&console_data, CONSOLE_UART_BASE, CONSOLE_UART_CLK_IN_HZ,
92 		   CONSOLE_BAUDRATE);
93 	register_serial_console(&console_data.chip);
94 }
95 
96 #if defined(PLATFORM_FLAVOR_hikey)
97 #ifdef CFG_SPI
98 void spi_init(void)
99 {
100 	uint32_t shifted_val, read_val;
101 	vaddr_t peri_base = core_mmu_get_va(PERI_BASE, MEM_AREA_IO_NSEC);
102 	vaddr_t pmx0_base = core_mmu_get_va(PMX0_BASE, MEM_AREA_IO_NSEC);
103 	vaddr_t pmx1_base = core_mmu_get_va(PMX1_BASE, MEM_AREA_IO_NSEC);
104 
105 	DMSG("take SPI0 out of reset\n");
106 	shifted_val = PERI_RST3_SSP;
107 	/*
108 	 * no need to read PERI_SC_PERIPH_RSTDIS3 first
109 	 * as all the bits are processed and cleared after writing
110 	 */
111 	write32(shifted_val, peri_base + PERI_SC_PERIPH_RSTDIS3);
112 	DMSG("PERI_SC_PERIPH_RSTDIS3: 0x%x\n",
113 		read32(peri_base + PERI_SC_PERIPH_RSTDIS3));
114 
115 	/*
116 	 * wait until the requested device is out of reset
117 	 * and ready to be used
118 	 */
119 	do {
120 		read_val = read32(peri_base + PERI_SC_PERIPH_RSTSTAT3);
121 	} while (read_val & shifted_val);
122 	DMSG("PERI_SC_PERIPH_RSTSTAT3: 0x%x\n", read_val);
123 
124 	DMSG("enable SPI clock\n");
125 	/*
126 	 * no need to read PERI_SC_PERIPH_CLKEN3 first
127 	 * as all the bits are processed and cleared after writing
128 	 */
129 	shifted_val = PERI_CLK3_SSP;
130 	write32(shifted_val, peri_base + PERI_SC_PERIPH_CLKEN3);
131 	DMSG("PERI_SC_PERIPH_CLKEN3: 0x%x\n",
132 		read32(peri_base + PERI_SC_PERIPH_CLKEN3));
133 
134 	DMSG("PERI_SC_PERIPH_CLKSTAT3: 0x%x\n",
135 		read32(peri_base + PERI_SC_PERIPH_CLKSTAT3));
136 
137 	/*
138 	 * GPIO6_2 can be configured as PINMUX_GPIO, but as PINMUX_SPI, HW IP
139 	 * will control the chip select pin so we don't have to manually do it.
140 	 * The only concern is that the IP will pulse it between each packet,
141 	 * which might not work with certain clients. There seems to be no
142 	 * option to configure it to stay enabled for the total duration of the
143 	 * transfer.
144 	 * ref: http://infocenter.arm.com/help/topic/com.arm.doc.ddi0194h/CJACFAFG.html
145 	 */
146 	DMSG("configure gpio6 pins 0-3 as SPI\n");
147 	write32(PINMUX_SPI, pmx0_base + PMX0_IOMG104);
148 	write32(PINMUX_SPI, pmx0_base + PMX0_IOMG105);
149 	write32(PINMUX_SPI, pmx0_base + PMX0_IOMG106);
150 	write32(PINMUX_SPI, pmx0_base + PMX0_IOMG107);
151 
152 	DMSG("configure gpio6 pins 0-3 as nopull\n");
153 	write32(PINCFG_NOPULL, pmx1_base + PMX1_IOCG104);
154 	write32(PINCFG_NOPULL, pmx1_base + PMX1_IOCG105);
155 	write32(PINCFG_NOPULL, pmx1_base + PMX1_IOCG106);
156 	write32(PINCFG_NOPULL, pmx1_base + PMX1_IOCG107);
157 
158 #ifdef CFG_SPI_TEST
159 	spi_test();
160 #endif
161 }
162 #endif
163 
164 static TEE_Result peripherals_init(void)
165 {
166 	vaddr_t pmussi_base = core_mmu_get_va(PMUSSI_BASE, MEM_AREA_IO_NSEC);
167 
168 	DMSG("enable LD021_1V8 source (pin 35) on LS connector\n");
169 	/*
170 	 * Mezzanine cards usually use this to source level shifters for
171 	 * UART, GPIO, SPI, I2C, etc so if not enabled, connected
172 	 * peripherals will not work either (during bootloader stage)
173 	 * until linux is booted.
174 	 */
175 	io_mask8(pmussi_base + PMUSSI_LDO21_REG_ADJ, PMUSSI_LDO21_REG_VL_1V8,
176 		PMUSSI_LDO21_REG_VL_MASK);
177 	write8(PMUSSI_ENA_LDO21, pmussi_base + PMUSSI_ENA_LDO17_22);
178 
179 #ifdef CFG_SPI
180 	spi_init();
181 #endif
182 	return TEE_SUCCESS;
183 }
184 
185 driver_init(peripherals_init);
186 #endif /* PLATFORM_FLAVOR_hikey */
187