158032586SSamuel Holland /* 258032586SSamuel Holland * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved. 358032586SSamuel Holland * 458032586SSamuel Holland * SPDX-License-Identifier: BSD-3-Clause 558032586SSamuel Holland */ 658032586SSamuel Holland 7*11480b90SAndre Przywara #include <arch_helpers.h> 8d5ddf67aSAndre Przywara #include <debug.h> 9d5ddf67aSAndre Przywara #include <errno.h> 10c4143b74SAndre Przywara #include <mmio.h> 1158032586SSamuel Holland #include <platform.h> 1258032586SSamuel Holland #include <platform_def.h> 1358032586SSamuel Holland #include <sunxi_def.h> 14*11480b90SAndre Przywara #include <sunxi_mmap.h> 154ec1a239SAndre Przywara #include <sunxi_private.h> 1658032586SSamuel Holland #include <xlat_tables_v2.h> 1758032586SSamuel Holland 1858032586SSamuel Holland static mmap_region_t sunxi_mmap[PLATFORM_MMAP_REGIONS + 1] = { 1958032586SSamuel Holland MAP_REGION_FLAT(SUNXI_SRAM_BASE, SUNXI_SRAM_SIZE, 2058032586SSamuel Holland MT_MEMORY | MT_RW | MT_SECURE), 2158032586SSamuel Holland MAP_REGION_FLAT(SUNXI_DEV_BASE, SUNXI_DEV_SIZE, 2258032586SSamuel Holland MT_DEVICE | MT_RW | MT_SECURE), 23c3af6b00SAndre Przywara MAP_REGION(SUNXI_DRAM_BASE, SUNXI_DRAM_VIRT_BASE, SUNXI_DRAM_SEC_SIZE, 24c3af6b00SAndre Przywara MT_MEMORY | MT_RW | MT_SECURE), 25c3af6b00SAndre Przywara MAP_REGION(PLAT_SUNXI_NS_IMAGE_OFFSET, 26c3af6b00SAndre Przywara SUNXI_DRAM_VIRT_BASE + SUNXI_DRAM_SEC_SIZE, 27c3af6b00SAndre Przywara SUNXI_DRAM_MAP_SIZE, 28c3af6b00SAndre Przywara MT_MEMORY | MT_RO | MT_NS), 2958032586SSamuel Holland {}, 3058032586SSamuel Holland }; 3158032586SSamuel Holland 3258032586SSamuel Holland unsigned int plat_get_syscnt_freq2(void) 3358032586SSamuel Holland { 3458032586SSamuel Holland return SUNXI_OSC24M_CLK_IN_HZ; 3558032586SSamuel Holland } 3658032586SSamuel Holland 3758032586SSamuel Holland uintptr_t plat_get_ns_image_entrypoint(void) 3858032586SSamuel Holland { 3958032586SSamuel Holland #ifdef PRELOADED_BL33_BASE 4058032586SSamuel Holland return PRELOADED_BL33_BASE; 4158032586SSamuel Holland #else 4258032586SSamuel Holland return PLAT_SUNXI_NS_IMAGE_OFFSET; 4358032586SSamuel Holland #endif 4458032586SSamuel Holland } 4558032586SSamuel Holland 4658032586SSamuel Holland void sunxi_configure_mmu_el3(int flags) 4758032586SSamuel Holland { 4858032586SSamuel Holland mmap_add_region(BL31_BASE, BL31_BASE, 4958032586SSamuel Holland BL31_LIMIT - BL31_BASE, 5058032586SSamuel Holland MT_MEMORY | MT_RW | MT_SECURE); 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); 5758032586SSamuel Holland mmap_add(sunxi_mmap); 5858032586SSamuel Holland init_xlat_tables(); 5958032586SSamuel Holland 6058032586SSamuel Holland enable_mmu_el3(0); 6158032586SSamuel Holland } 62c4143b74SAndre Przywara 63c4143b74SAndre Przywara #define SRAM_VER_REG (SUNXI_SYSCON_BASE + 0x24) 64c4143b74SAndre Przywara uint16_t sunxi_read_soc_id(void) 65c4143b74SAndre Przywara { 66c4143b74SAndre Przywara uint32_t reg = mmio_read_32(SRAM_VER_REG); 67c4143b74SAndre Przywara 68c4143b74SAndre Przywara /* Set bit 15 to prepare for the SOCID read. */ 69c4143b74SAndre Przywara mmio_write_32(SRAM_VER_REG, reg | BIT(15)); 70c4143b74SAndre Przywara 71c4143b74SAndre Przywara reg = mmio_read_32(SRAM_VER_REG); 72c4143b74SAndre Przywara 73c4143b74SAndre Przywara /* deactivate the SOCID access again */ 74c4143b74SAndre Przywara mmio_write_32(SRAM_VER_REG, reg & ~BIT(15)); 75c4143b74SAndre Przywara 76c4143b74SAndre Przywara return reg >> 16; 77c4143b74SAndre Przywara } 787020dca0SAndre Przywara 797020dca0SAndre Przywara /* 807020dca0SAndre Przywara * Configure a given pin to the GPIO-OUT function and sets its level. 817020dca0SAndre Przywara * The port is given as a capital letter, the pin is the number within 827020dca0SAndre Przywara * this port group. 837020dca0SAndre Przywara * So to set pin PC7 to high, use: sunxi_set_gpio_out('C', 7, true); 847020dca0SAndre Przywara */ 857020dca0SAndre Przywara void sunxi_set_gpio_out(char port, int pin, bool level_high) 867020dca0SAndre Przywara { 877020dca0SAndre Przywara uintptr_t port_base; 887020dca0SAndre Przywara 897020dca0SAndre Przywara if (port < 'A' || port > 'L') 907020dca0SAndre Przywara return; 917020dca0SAndre Przywara if (port == 'L') 927020dca0SAndre Przywara port_base = SUNXI_R_PIO_BASE; 937020dca0SAndre Przywara else 947020dca0SAndre Przywara port_base = SUNXI_PIO_BASE + (port - 'A') * 0x24; 957020dca0SAndre Przywara 967020dca0SAndre Przywara /* Set the new level first before configuring the pin. */ 977020dca0SAndre Przywara if (level_high) 987020dca0SAndre Przywara mmio_setbits_32(port_base + 0x10, BIT(pin)); 997020dca0SAndre Przywara else 1007020dca0SAndre Przywara mmio_clrbits_32(port_base + 0x10, BIT(pin)); 1017020dca0SAndre Przywara 1027020dca0SAndre Przywara /* configure pin as GPIO out (4(3) bits per pin, 1: GPIO out */ 1037020dca0SAndre Przywara mmio_clrsetbits_32(port_base + (pin / 8) * 4, 1047020dca0SAndre Przywara 0x7 << ((pin % 8) * 4), 1057020dca0SAndre Przywara 0x1 << ((pin % 8) * 4)); 1067020dca0SAndre Przywara } 107d5ddf67aSAndre Przywara 108d5ddf67aSAndre Przywara int sunxi_init_platform_r_twi(uint16_t socid, bool use_rsb) 109d5ddf67aSAndre Przywara { 110d5ddf67aSAndre Przywara uint32_t pin_func = 0x77; 111d5ddf67aSAndre Przywara uint32_t device_bit; 112d5ddf67aSAndre Przywara unsigned int reset_offset = 0xb0; 113d5ddf67aSAndre Przywara 114d5ddf67aSAndre Przywara switch (socid) { 115d5ddf67aSAndre Przywara case SUNXI_SOC_H5: 116d5ddf67aSAndre Przywara if (use_rsb) 117d5ddf67aSAndre Przywara return -ENODEV; 118d5ddf67aSAndre Przywara pin_func = 0x22; 119d5ddf67aSAndre Przywara device_bit = BIT(6); 120d5ddf67aSAndre Przywara break; 121d5ddf67aSAndre Przywara case SUNXI_SOC_H6: 122d5ddf67aSAndre Przywara if (use_rsb) 123d5ddf67aSAndre Przywara return -ENODEV; 124d5ddf67aSAndre Przywara pin_func = 0x33; 125d5ddf67aSAndre Przywara device_bit = BIT(16); 126d5ddf67aSAndre Przywara reset_offset = 0x19c; 127d5ddf67aSAndre Przywara break; 128d5ddf67aSAndre Przywara case SUNXI_SOC_A64: 129d5ddf67aSAndre Przywara pin_func = use_rsb ? 0x22 : 0x33; 130d5ddf67aSAndre Przywara device_bit = use_rsb ? BIT(3) : BIT(6); 131d5ddf67aSAndre Przywara break; 132d5ddf67aSAndre Przywara default: 133d5ddf67aSAndre Przywara INFO("R_I2C/RSB on Allwinner 0x%x SoC not supported\n", socid); 134d5ddf67aSAndre Przywara return -ENODEV; 135d5ddf67aSAndre Przywara } 136d5ddf67aSAndre Przywara 137d5ddf67aSAndre Przywara /* un-gate R_PIO clock */ 138d5ddf67aSAndre Przywara if (socid != SUNXI_SOC_H6) 139d5ddf67aSAndre Przywara mmio_setbits_32(SUNXI_R_PRCM_BASE + 0x28, BIT(0)); 140d5ddf67aSAndre Przywara 141d5ddf67aSAndre Przywara /* switch pins PL0 and PL1 to the desired function */ 142d5ddf67aSAndre Przywara mmio_clrsetbits_32(SUNXI_R_PIO_BASE + 0x00, 0xffU, pin_func); 143d5ddf67aSAndre Przywara 144d5ddf67aSAndre Przywara /* level 2 drive strength */ 145d5ddf67aSAndre Przywara mmio_clrsetbits_32(SUNXI_R_PIO_BASE + 0x14, 0x0fU, 0xaU); 146d5ddf67aSAndre Przywara 147d5ddf67aSAndre Przywara /* set both pins to pull-up */ 148d5ddf67aSAndre Przywara mmio_clrsetbits_32(SUNXI_R_PIO_BASE + 0x1c, 0x0fU, 0x5U); 149d5ddf67aSAndre Przywara 150d5ddf67aSAndre Przywara /* assert, then de-assert reset of I2C/RSB controller */ 151d5ddf67aSAndre Przywara mmio_clrbits_32(SUNXI_R_PRCM_BASE + reset_offset, device_bit); 152d5ddf67aSAndre Przywara mmio_setbits_32(SUNXI_R_PRCM_BASE + reset_offset, device_bit); 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 160d5ddf67aSAndre Przywara return 0; 161d5ddf67aSAndre Przywara } 162*11480b90SAndre Przywara 163*11480b90SAndre Przywara /* This lock synchronises access to the arisc management processor. */ 164*11480b90SAndre Przywara DEFINE_BAKERY_LOCK(arisc_lock); 165*11480b90SAndre Przywara 166*11480b90SAndre Przywara /* 167*11480b90SAndre Przywara * Tell the "arisc" SCP core (an OpenRISC core) to execute some code. 168*11480b90SAndre Przywara * We don't have any service running there, so we place some OpenRISC code 169*11480b90SAndre Przywara * in SRAM, put the address of that into the reset vector and release the 170*11480b90SAndre Przywara * arisc reset line. The SCP will execute that code and pull the line up again. 171*11480b90SAndre Przywara */ 172*11480b90SAndre Przywara void sunxi_execute_arisc_code(uint32_t *code, size_t size, 173*11480b90SAndre Przywara int patch_offset, uint16_t param) 174*11480b90SAndre Przywara { 175*11480b90SAndre Przywara uintptr_t arisc_reset_vec = SUNXI_SRAM_A2_BASE - 0x4000 + 0x100; 176*11480b90SAndre Przywara 177*11480b90SAndre Przywara do { 178*11480b90SAndre Przywara bakery_lock_get(&arisc_lock); 179*11480b90SAndre Przywara /* Wait until the arisc is in reset state. */ 180*11480b90SAndre Przywara if (!(mmio_read_32(SUNXI_R_CPUCFG_BASE) & BIT(0))) 181*11480b90SAndre Przywara break; 182*11480b90SAndre Przywara 183*11480b90SAndre Przywara bakery_lock_release(&arisc_lock); 184*11480b90SAndre Przywara } while (1); 185*11480b90SAndre Przywara 186*11480b90SAndre Przywara /* Patch up the code to feed in an input parameter. */ 187*11480b90SAndre Przywara if (patch_offset >= 0 && patch_offset <= (size - 4)) 188*11480b90SAndre Przywara code[patch_offset] = (code[patch_offset] & ~0xffff) | param; 189*11480b90SAndre Przywara clean_dcache_range((uintptr_t)code, size); 190*11480b90SAndre Przywara 191*11480b90SAndre Przywara /* 192*11480b90SAndre Przywara * The OpenRISC unconditional branch has opcode 0, the branch offset 193*11480b90SAndre Przywara * is in the lower 26 bits, containing the distance to the target, 194*11480b90SAndre Przywara * in instruction granularity (32 bits). 195*11480b90SAndre Przywara */ 196*11480b90SAndre Przywara mmio_write_32(arisc_reset_vec, ((uintptr_t)code - arisc_reset_vec) / 4); 197*11480b90SAndre Przywara clean_dcache_range(arisc_reset_vec, 4); 198*11480b90SAndre Przywara 199*11480b90SAndre Przywara /* De-assert the arisc reset line to let it run. */ 200*11480b90SAndre Przywara mmio_setbits_32(SUNXI_R_CPUCFG_BASE, BIT(0)); 201*11480b90SAndre Przywara 202*11480b90SAndre Przywara /* 203*11480b90SAndre Przywara * We release the lock here, although the arisc is still busy. 204*11480b90SAndre Przywara * But as long as it runs, the reset line is high, so other users 205*11480b90SAndre Przywara * won't leave the loop above. 206*11480b90SAndre Przywara * Once it has finished, the code is supposed to clear the reset line, 207*11480b90SAndre Przywara * to signal this to other users. 208*11480b90SAndre Przywara */ 209*11480b90SAndre Przywara bakery_lock_release(&arisc_lock); 210*11480b90SAndre Przywara } 211