xref: /rk3399_ARM-atf/plat/allwinner/common/sunxi_common.c (revision ddb4c9e04cc5849dc035c9db382fc2d113df928f)
158032586SSamuel Holland /*
25cffedceSSamuel Holland  * Copyright (c) 2017-2019, ARM Limited and Contributors. All rights reserved.
358032586SSamuel Holland  *
458032586SSamuel Holland  * SPDX-License-Identifier: BSD-3-Clause
558032586SSamuel Holland  */
658032586SSamuel Holland 
7d5ddf67aSAndre Przywara #include <errno.h>
809d40e0eSAntonio Nino Diaz 
958032586SSamuel Holland #include <platform_def.h>
1009d40e0eSAntonio Nino Diaz 
1109d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
1209d40e0eSAntonio Nino Diaz #include <common/debug.h>
1309d40e0eSAntonio Nino Diaz #include <lib/mmio.h>
1409d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_v2.h>
1509d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
1609d40e0eSAntonio Nino Diaz 
1758032586SSamuel Holland #include <sunxi_def.h>
1811480b90SAndre Przywara #include <sunxi_mmap.h>
194ec1a239SAndre Przywara #include <sunxi_private.h>
2058032586SSamuel Holland 
2150811682SSamuel Holland static const mmap_region_t sunxi_mmap[PLATFORM_MMAP_REGIONS + 1] = {
2258032586SSamuel Holland 	MAP_REGION_FLAT(SUNXI_SRAM_BASE, SUNXI_SRAM_SIZE,
23*ddb4c9e0SSamuel Holland 			MT_RW_DATA | MT_SECURE),
2458032586SSamuel Holland 	MAP_REGION_FLAT(SUNXI_DEV_BASE, SUNXI_DEV_SIZE,
25*ddb4c9e0SSamuel Holland 			MT_DEVICE | MT_RW | MT_SECURE | MT_EXECUTE_NEVER),
26c3af6b00SAndre Przywara 	MAP_REGION(SUNXI_DRAM_BASE, SUNXI_DRAM_VIRT_BASE, SUNXI_DRAM_SEC_SIZE,
27*ddb4c9e0SSamuel Holland 		   MT_RW_DATA | MT_SECURE),
28c3af6b00SAndre Przywara 	MAP_REGION(PLAT_SUNXI_NS_IMAGE_OFFSET,
29c3af6b00SAndre Przywara 		   SUNXI_DRAM_VIRT_BASE + SUNXI_DRAM_SEC_SIZE,
30c3af6b00SAndre Przywara 		   SUNXI_DRAM_MAP_SIZE,
31*ddb4c9e0SSamuel Holland 		   MT_RO_DATA | MT_NS),
3258032586SSamuel Holland 	{},
3358032586SSamuel Holland };
3458032586SSamuel Holland 
3558032586SSamuel Holland unsigned int plat_get_syscnt_freq2(void)
3658032586SSamuel Holland {
3758032586SSamuel Holland 	return SUNXI_OSC24M_CLK_IN_HZ;
3858032586SSamuel Holland }
3958032586SSamuel Holland 
4058032586SSamuel Holland uintptr_t plat_get_ns_image_entrypoint(void)
4158032586SSamuel Holland {
4258032586SSamuel Holland #ifdef PRELOADED_BL33_BASE
4358032586SSamuel Holland 	return PRELOADED_BL33_BASE;
4458032586SSamuel Holland #else
4558032586SSamuel Holland 	return PLAT_SUNXI_NS_IMAGE_OFFSET;
4658032586SSamuel Holland #endif
4758032586SSamuel Holland }
4858032586SSamuel Holland 
4958032586SSamuel Holland void sunxi_configure_mmu_el3(int flags)
5058032586SSamuel Holland {
5158032586SSamuel Holland 	mmap_add_region(BL_CODE_BASE, BL_CODE_BASE,
5258032586SSamuel Holland 			BL_CODE_END - BL_CODE_BASE,
5358032586SSamuel Holland 			MT_CODE | MT_SECURE);
5458032586SSamuel Holland 	mmap_add_region(BL_RO_DATA_BASE, BL_RO_DATA_BASE,
5558032586SSamuel Holland 			BL_RO_DATA_END - BL_RO_DATA_BASE,
5658032586SSamuel Holland 			MT_RO_DATA | MT_SECURE);
576c281cc3SSamuel Holland 	mmap_add_region(BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_BASE,
586c281cc3SSamuel Holland 			BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE,
596c281cc3SSamuel Holland 			MT_DEVICE | MT_RW | MT_SECURE | MT_EXECUTE_NEVER);
606c281cc3SSamuel Holland 
6158032586SSamuel Holland 	mmap_add(sunxi_mmap);
6258032586SSamuel Holland 	init_xlat_tables();
6358032586SSamuel Holland 
6458032586SSamuel Holland 	enable_mmu_el3(0);
6558032586SSamuel Holland }
66c4143b74SAndre Przywara 
67c4143b74SAndre Przywara #define SRAM_VER_REG (SUNXI_SYSCON_BASE + 0x24)
68c4143b74SAndre Przywara uint16_t sunxi_read_soc_id(void)
69c4143b74SAndre Przywara {
70c4143b74SAndre Przywara 	uint32_t reg = mmio_read_32(SRAM_VER_REG);
71c4143b74SAndre Przywara 
72c4143b74SAndre Przywara 	/* Set bit 15 to prepare for the SOCID read. */
73c4143b74SAndre Przywara 	mmio_write_32(SRAM_VER_REG, reg | BIT(15));
74c4143b74SAndre Przywara 
75c4143b74SAndre Przywara 	reg = mmio_read_32(SRAM_VER_REG);
76c4143b74SAndre Przywara 
77c4143b74SAndre Przywara 	/* deactivate the SOCID access again */
78c4143b74SAndre Przywara 	mmio_write_32(SRAM_VER_REG, reg & ~BIT(15));
79c4143b74SAndre Przywara 
80c4143b74SAndre Przywara 	return reg >> 16;
81c4143b74SAndre Przywara }
827020dca0SAndre Przywara 
837020dca0SAndre Przywara /*
847020dca0SAndre Przywara  * Configure a given pin to the GPIO-OUT function and sets its level.
857020dca0SAndre Przywara  * The port is given as a capital letter, the pin is the number within
867020dca0SAndre Przywara  * this port group.
877020dca0SAndre Przywara  * So to set pin PC7 to high, use: sunxi_set_gpio_out('C', 7, true);
887020dca0SAndre Przywara  */
897020dca0SAndre Przywara void sunxi_set_gpio_out(char port, int pin, bool level_high)
907020dca0SAndre Przywara {
917020dca0SAndre Przywara 	uintptr_t port_base;
927020dca0SAndre Przywara 
937020dca0SAndre Przywara 	if (port < 'A' || port > 'L')
947020dca0SAndre Przywara 		return;
957020dca0SAndre Przywara 	if (port == 'L')
967020dca0SAndre Przywara 		port_base = SUNXI_R_PIO_BASE;
977020dca0SAndre Przywara 	else
987020dca0SAndre Przywara 		port_base = SUNXI_PIO_BASE + (port - 'A') * 0x24;
997020dca0SAndre Przywara 
1007020dca0SAndre Przywara 	/* Set the new level first before configuring the pin. */
1017020dca0SAndre Przywara 	if (level_high)
1027020dca0SAndre Przywara 		mmio_setbits_32(port_base + 0x10, BIT(pin));
1037020dca0SAndre Przywara 	else
1047020dca0SAndre Przywara 		mmio_clrbits_32(port_base + 0x10, BIT(pin));
1057020dca0SAndre Przywara 
1067020dca0SAndre Przywara 	/* configure pin as GPIO out (4(3) bits per pin, 1: GPIO out */
1077020dca0SAndre Przywara 	mmio_clrsetbits_32(port_base + (pin / 8) * 4,
1087020dca0SAndre Przywara 			   0x7 << ((pin % 8) * 4),
1097020dca0SAndre Przywara 			   0x1 << ((pin % 8) * 4));
1107020dca0SAndre Przywara }
111d5ddf67aSAndre Przywara 
112d5ddf67aSAndre Przywara int sunxi_init_platform_r_twi(uint16_t socid, bool use_rsb)
113d5ddf67aSAndre Przywara {
114d5ddf67aSAndre Przywara 	uint32_t pin_func = 0x77;
115d5ddf67aSAndre Przywara 	uint32_t device_bit;
116d5ddf67aSAndre Przywara 	unsigned int reset_offset = 0xb0;
117d5ddf67aSAndre Przywara 
118d5ddf67aSAndre Przywara 	switch (socid) {
119d5ddf67aSAndre Przywara 	case SUNXI_SOC_H5:
120d5ddf67aSAndre Przywara 		if (use_rsb)
121d5ddf67aSAndre Przywara 			return -ENODEV;
122d5ddf67aSAndre Przywara 		pin_func = 0x22;
123d5ddf67aSAndre Przywara 		device_bit = BIT(6);
124d5ddf67aSAndre Przywara 		break;
125d5ddf67aSAndre Przywara 	case SUNXI_SOC_H6:
126d5ddf67aSAndre Przywara 		if (use_rsb)
127d5ddf67aSAndre Przywara 			return -ENODEV;
128d5ddf67aSAndre Przywara 		pin_func = 0x33;
129d5ddf67aSAndre Przywara 		device_bit = BIT(16);
130d5ddf67aSAndre Przywara 		reset_offset = 0x19c;
131d5ddf67aSAndre Przywara 		break;
132d5ddf67aSAndre Przywara 	case SUNXI_SOC_A64:
133d5ddf67aSAndre Przywara 		pin_func = use_rsb ? 0x22 : 0x33;
134d5ddf67aSAndre Przywara 		device_bit = use_rsb ? BIT(3) : BIT(6);
135d5ddf67aSAndre Przywara 		break;
136d5ddf67aSAndre Przywara 	default:
137d5ddf67aSAndre Przywara 		INFO("R_I2C/RSB on Allwinner 0x%x SoC not supported\n", socid);
138d5ddf67aSAndre Przywara 		return -ENODEV;
139d5ddf67aSAndre Przywara 	}
140d5ddf67aSAndre Przywara 
141d5ddf67aSAndre Przywara 	/* un-gate R_PIO clock */
142d5ddf67aSAndre Przywara 	if (socid != SUNXI_SOC_H6)
143d5ddf67aSAndre Przywara 		mmio_setbits_32(SUNXI_R_PRCM_BASE + 0x28, BIT(0));
144d5ddf67aSAndre Przywara 
145d5ddf67aSAndre Przywara 	/* switch pins PL0 and PL1 to the desired function */
146d5ddf67aSAndre Przywara 	mmio_clrsetbits_32(SUNXI_R_PIO_BASE + 0x00, 0xffU, pin_func);
147d5ddf67aSAndre Przywara 
148d5ddf67aSAndre Przywara 	/* level 2 drive strength */
149d5ddf67aSAndre Przywara 	mmio_clrsetbits_32(SUNXI_R_PIO_BASE + 0x14, 0x0fU, 0xaU);
150d5ddf67aSAndre Przywara 
151d5ddf67aSAndre Przywara 	/* set both pins to pull-up */
152d5ddf67aSAndre Przywara 	mmio_clrsetbits_32(SUNXI_R_PIO_BASE + 0x1c, 0x0fU, 0x5U);
153d5ddf67aSAndre Przywara 
154d5ddf67aSAndre Przywara 	/* un-gate clock */
155d5ddf67aSAndre Przywara 	if (socid != SUNXI_SOC_H6)
156d5ddf67aSAndre Przywara 		mmio_setbits_32(SUNXI_R_PRCM_BASE + 0x28, device_bit);
157d5ddf67aSAndre Przywara 	else
158d5ddf67aSAndre Przywara 		mmio_setbits_32(SUNXI_R_PRCM_BASE + 0x19c, device_bit | BIT(0));
159d5ddf67aSAndre Przywara 
160eb75518dSSamuel Holland 	/* assert, then de-assert reset of I2C/RSB controller */
161eb75518dSSamuel Holland 	mmio_clrbits_32(SUNXI_R_PRCM_BASE + reset_offset, device_bit);
162eb75518dSSamuel Holland 	mmio_setbits_32(SUNXI_R_PRCM_BASE + reset_offset, device_bit);
163eb75518dSSamuel Holland 
164d5ddf67aSAndre Przywara 	return 0;
165d5ddf67aSAndre Przywara }
16611480b90SAndre Przywara 
16711480b90SAndre Przywara /* This lock synchronises access to the arisc management processor. */
16811480b90SAndre Przywara DEFINE_BAKERY_LOCK(arisc_lock);
16911480b90SAndre Przywara 
17011480b90SAndre Przywara /*
17111480b90SAndre Przywara  * Tell the "arisc" SCP core (an OpenRISC core) to execute some code.
17211480b90SAndre Przywara  * We don't have any service running there, so we place some OpenRISC code
17311480b90SAndre Przywara  * in SRAM, put the address of that into the reset vector and release the
17411480b90SAndre Przywara  * arisc reset line. The SCP will execute that code and pull the line up again.
17511480b90SAndre Przywara  */
1765cffedceSSamuel Holland void sunxi_execute_arisc_code(uint32_t *code, size_t size, uint16_t param)
17711480b90SAndre Przywara {
17811480b90SAndre Przywara 	uintptr_t arisc_reset_vec = SUNXI_SRAM_A2_BASE - 0x4000 + 0x100;
17911480b90SAndre Przywara 
18011480b90SAndre Przywara 	do {
18111480b90SAndre Przywara 		bakery_lock_get(&arisc_lock);
18211480b90SAndre Przywara 		/* Wait until the arisc is in reset state. */
18311480b90SAndre Przywara 		if (!(mmio_read_32(SUNXI_R_CPUCFG_BASE) & BIT(0)))
18411480b90SAndre Przywara 			break;
18511480b90SAndre Przywara 
18611480b90SAndre Przywara 		bakery_lock_release(&arisc_lock);
18711480b90SAndre Przywara 	} while (1);
18811480b90SAndre Przywara 
18911480b90SAndre Przywara 	/* Patch up the code to feed in an input parameter. */
1905cffedceSSamuel Holland 	code[0] = (code[0] & ~0xffff) | param;
19111480b90SAndre Przywara 	clean_dcache_range((uintptr_t)code, size);
19211480b90SAndre Przywara 
19311480b90SAndre Przywara 	/*
19411480b90SAndre Przywara 	 * The OpenRISC unconditional branch has opcode 0, the branch offset
19511480b90SAndre Przywara 	 * is in the lower 26 bits, containing the distance to the target,
19611480b90SAndre Przywara 	 * in instruction granularity (32 bits).
19711480b90SAndre Przywara 	 */
19811480b90SAndre Przywara 	mmio_write_32(arisc_reset_vec, ((uintptr_t)code - arisc_reset_vec) / 4);
19911480b90SAndre Przywara 	clean_dcache_range(arisc_reset_vec, 4);
20011480b90SAndre Przywara 
20111480b90SAndre Przywara 	/* De-assert the arisc reset line to let it run. */
20211480b90SAndre Przywara 	mmio_setbits_32(SUNXI_R_CPUCFG_BASE, BIT(0));
20311480b90SAndre Przywara 
20411480b90SAndre Przywara 	/*
20511480b90SAndre Przywara 	 * We release the lock here, although the arisc is still busy.
20611480b90SAndre Przywara 	 * But as long as it runs, the reset line is high, so other users
20711480b90SAndre Przywara 	 * won't leave the loop above.
20811480b90SAndre Przywara 	 * Once it has finished, the code is supposed to clear the reset line,
20911480b90SAndre Przywara 	 * to signal this to other users.
21011480b90SAndre Przywara 	 */
21111480b90SAndre Przywara 	bakery_lock_release(&arisc_lock);
21211480b90SAndre Przywara }
213