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