158032586SSamuel Holland /* 2bc135624SSamuel Holland * Copyright (c) 2017-2020, 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 909d40e0eSAntonio Nino Diaz #include <common/debug.h> 1009d40e0eSAntonio Nino Diaz #include <lib/mmio.h> 11*436cd754SAndre Przywara #include <lib/smccc.h> 1209d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_v2.h> 13*436cd754SAndre Przywara #include <services/arm_arch_svc.h> 1409d40e0eSAntonio Nino Diaz 1558032586SSamuel Holland #include <sunxi_def.h> 1611480b90SAndre Przywara #include <sunxi_mmap.h> 174ec1a239SAndre Przywara #include <sunxi_private.h> 1858032586SSamuel Holland 19bc135624SSamuel Holland static const mmap_region_t sunxi_mmap[MAX_STATIC_MMAP_REGIONS + 1] = { 2058032586SSamuel Holland MAP_REGION_FLAT(SUNXI_SRAM_BASE, SUNXI_SRAM_SIZE, 2157b36632SSamuel Holland MT_DEVICE | MT_RW | MT_SECURE | MT_EXECUTE_NEVER), 2258032586SSamuel Holland MAP_REGION_FLAT(SUNXI_DEV_BASE, SUNXI_DEV_SIZE, 23ddb4c9e0SSamuel Holland MT_DEVICE | MT_RW | MT_SECURE | MT_EXECUTE_NEVER), 2401cec8f4SAndre Przywara MAP_REGION(PRELOADED_BL33_BASE, SUNXI_BL33_VIRT_BASE, 250be10ee3SAndre Przywara SUNXI_DRAM_MAP_SIZE, MT_RW_DATA | MT_NS), 2658032586SSamuel Holland {}, 2758032586SSamuel Holland }; 2858032586SSamuel Holland 2958032586SSamuel Holland unsigned int plat_get_syscnt_freq2(void) 3058032586SSamuel Holland { 3158032586SSamuel Holland return SUNXI_OSC24M_CLK_IN_HZ; 3258032586SSamuel Holland } 3358032586SSamuel Holland 3458032586SSamuel Holland void sunxi_configure_mmu_el3(int flags) 3558032586SSamuel Holland { 3658032586SSamuel Holland mmap_add_region(BL_CODE_BASE, BL_CODE_BASE, 3758032586SSamuel Holland BL_CODE_END - BL_CODE_BASE, 3858032586SSamuel Holland MT_CODE | MT_SECURE); 39ab74206bSSamuel Holland mmap_add_region(BL_CODE_END, BL_CODE_END, 40ab74206bSSamuel Holland BL_END - BL_CODE_END, 41ab74206bSSamuel Holland MT_RW_DATA | MT_SECURE); 42ab74206bSSamuel Holland #if SEPARATE_CODE_AND_RODATA 4358032586SSamuel Holland mmap_add_region(BL_RO_DATA_BASE, BL_RO_DATA_BASE, 4458032586SSamuel Holland BL_RO_DATA_END - BL_RO_DATA_BASE, 4558032586SSamuel Holland MT_RO_DATA | MT_SECURE); 46ab74206bSSamuel Holland #endif 47ab74206bSSamuel Holland #if SEPARATE_NOBITS_REGION 48ab74206bSSamuel Holland mmap_add_region(BL_NOBITS_BASE, BL_NOBITS_BASE, 49ab74206bSSamuel Holland BL_NOBITS_END - BL_NOBITS_BASE, 50ab74206bSSamuel Holland MT_RW_DATA | MT_SECURE); 51ab74206bSSamuel Holland #endif 52ab74206bSSamuel Holland #if USE_COHERENT_MEM 536c281cc3SSamuel Holland mmap_add_region(BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_BASE, 546c281cc3SSamuel Holland BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE, 556c281cc3SSamuel Holland MT_DEVICE | MT_RW | MT_SECURE | MT_EXECUTE_NEVER); 56ab74206bSSamuel Holland #endif 576c281cc3SSamuel Holland 5858032586SSamuel Holland mmap_add(sunxi_mmap); 5958032586SSamuel Holland init_xlat_tables(); 6058032586SSamuel Holland 6158032586SSamuel Holland enable_mmu_el3(0); 6258032586SSamuel Holland } 63c4143b74SAndre Przywara 64c4143b74SAndre Przywara #define SRAM_VER_REG (SUNXI_SYSCON_BASE + 0x24) 65c4143b74SAndre Przywara uint16_t sunxi_read_soc_id(void) 66c4143b74SAndre Przywara { 67c4143b74SAndre Przywara uint32_t reg = mmio_read_32(SRAM_VER_REG); 68c4143b74SAndre Przywara 69c4143b74SAndre Przywara /* Set bit 15 to prepare for the SOCID read. */ 70c4143b74SAndre Przywara mmio_write_32(SRAM_VER_REG, reg | BIT(15)); 71c4143b74SAndre Przywara 72c4143b74SAndre Przywara reg = mmio_read_32(SRAM_VER_REG); 73c4143b74SAndre Przywara 74c4143b74SAndre Przywara /* deactivate the SOCID access again */ 75c4143b74SAndre Przywara mmio_write_32(SRAM_VER_REG, reg & ~BIT(15)); 76c4143b74SAndre Przywara 77c4143b74SAndre Przywara return reg >> 16; 78c4143b74SAndre Przywara } 797020dca0SAndre Przywara 807020dca0SAndre Przywara /* 817020dca0SAndre Przywara * Configure a given pin to the GPIO-OUT function and sets its level. 827020dca0SAndre Przywara * The port is given as a capital letter, the pin is the number within 837020dca0SAndre Przywara * this port group. 847020dca0SAndre Przywara * So to set pin PC7 to high, use: sunxi_set_gpio_out('C', 7, true); 857020dca0SAndre Przywara */ 867020dca0SAndre Przywara void sunxi_set_gpio_out(char port, int pin, bool level_high) 877020dca0SAndre Przywara { 887020dca0SAndre Przywara uintptr_t port_base; 897020dca0SAndre Przywara 907020dca0SAndre Przywara if (port < 'A' || port > 'L') 917020dca0SAndre Przywara return; 927020dca0SAndre Przywara if (port == 'L') 937020dca0SAndre Przywara port_base = SUNXI_R_PIO_BASE; 947020dca0SAndre Przywara else 957020dca0SAndre Przywara port_base = SUNXI_PIO_BASE + (port - 'A') * 0x24; 967020dca0SAndre Przywara 977020dca0SAndre Przywara /* Set the new level first before configuring the pin. */ 987020dca0SAndre Przywara if (level_high) 997020dca0SAndre Przywara mmio_setbits_32(port_base + 0x10, BIT(pin)); 1007020dca0SAndre Przywara else 1017020dca0SAndre Przywara mmio_clrbits_32(port_base + 0x10, BIT(pin)); 1027020dca0SAndre Przywara 1037020dca0SAndre Przywara /* configure pin as GPIO out (4(3) bits per pin, 1: GPIO out */ 1047020dca0SAndre Przywara mmio_clrsetbits_32(port_base + (pin / 8) * 4, 1057020dca0SAndre Przywara 0x7 << ((pin % 8) * 4), 1067020dca0SAndre Przywara 0x1 << ((pin % 8) * 4)); 1077020dca0SAndre Przywara } 108d5ddf67aSAndre Przywara 109d5ddf67aSAndre Przywara int sunxi_init_platform_r_twi(uint16_t socid, bool use_rsb) 110d5ddf67aSAndre Przywara { 111d5ddf67aSAndre Przywara uint32_t pin_func = 0x77; 112d5ddf67aSAndre Przywara uint32_t device_bit; 113d5ddf67aSAndre Przywara unsigned int reset_offset = 0xb0; 114d5ddf67aSAndre Przywara 115d5ddf67aSAndre Przywara switch (socid) { 116d5ddf67aSAndre Przywara case SUNXI_SOC_H5: 117d5ddf67aSAndre Przywara if (use_rsb) 118d5ddf67aSAndre Przywara return -ENODEV; 119d5ddf67aSAndre Przywara pin_func = 0x22; 120d5ddf67aSAndre Przywara device_bit = BIT(6); 121d5ddf67aSAndre Przywara break; 122d5ddf67aSAndre Przywara case SUNXI_SOC_H6: 123bb104f27SAndre Przywara case SUNXI_SOC_H616: 1247060e0d8SSamuel Holland pin_func = use_rsb ? 0x22 : 0x33; 125d5ddf67aSAndre Przywara device_bit = BIT(16); 1267060e0d8SSamuel Holland reset_offset = use_rsb ? 0x1bc : 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 */ 13826123ca3SAndre Przywara if (socid != SUNXI_SOC_H6 && socid != SUNXI_SOC_H616) 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 /* un-gate clock */ 15126123ca3SAndre Przywara if (socid != SUNXI_SOC_H6 && socid != SUNXI_SOC_H616) 152d5ddf67aSAndre Przywara mmio_setbits_32(SUNXI_R_PRCM_BASE + 0x28, device_bit); 153d5ddf67aSAndre Przywara else 1547060e0d8SSamuel Holland mmio_setbits_32(SUNXI_R_PRCM_BASE + reset_offset, BIT(0)); 155d5ddf67aSAndre Przywara 156eb75518dSSamuel Holland /* assert, then de-assert reset of I2C/RSB controller */ 157eb75518dSSamuel Holland mmio_clrbits_32(SUNXI_R_PRCM_BASE + reset_offset, device_bit); 158eb75518dSSamuel Holland mmio_setbits_32(SUNXI_R_PRCM_BASE + reset_offset, device_bit); 159eb75518dSSamuel Holland 160d5ddf67aSAndre Przywara return 0; 161d5ddf67aSAndre Przywara } 162*436cd754SAndre Przywara 163*436cd754SAndre Przywara int32_t plat_is_smccc_feature_available(u_register_t fid) 164*436cd754SAndre Przywara { 165*436cd754SAndre Przywara switch (fid) { 166*436cd754SAndre Przywara case SMCCC_ARCH_SOC_ID: 167*436cd754SAndre Przywara return SMC_ARCH_CALL_SUCCESS; 168*436cd754SAndre Przywara default: 169*436cd754SAndre Przywara return SMC_ARCH_CALL_NOT_SUPPORTED; 170*436cd754SAndre Przywara } 171*436cd754SAndre Przywara } 172*436cd754SAndre Przywara 173*436cd754SAndre Przywara int32_t plat_get_soc_version(void) 174*436cd754SAndre Przywara { 175*436cd754SAndre Przywara int32_t ret; 176*436cd754SAndre Przywara 177*436cd754SAndre Przywara ret = SOC_ID_SET_JEP_106(JEDEC_ALLWINNER_BKID, JEDEC_ALLWINNER_MFID); 178*436cd754SAndre Przywara 179*436cd754SAndre Przywara return ret | (sunxi_read_soc_id() & SOC_ID_IMPL_DEF_MASK); 180*436cd754SAndre Przywara } 181*436cd754SAndre Przywara 182*436cd754SAndre Przywara int32_t plat_get_soc_revision(void) 183*436cd754SAndre Przywara { 184*436cd754SAndre Przywara uint32_t reg = mmio_read_32(SRAM_VER_REG); 185*436cd754SAndre Przywara 186*436cd754SAndre Przywara return reg & GENMASK_32(7, 0); 187*436cd754SAndre Przywara } 188