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