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