Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 1 | /* |
Samuel Holland | fde9e1c | 2020-12-13 21:26:36 -0600 | [diff] [blame] | 2 | * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved. |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Andre Przywara | 6753776 | 2018-10-14 22:13:53 +0100 | [diff] [blame] | 7 | #include <errno.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 8 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | #include <common/debug.h> |
| 10 | #include <lib/mmio.h> |
Andre Przywara | abb7ce1 | 2020-09-25 16:42:06 +0100 | [diff] [blame] | 11 | #include <lib/smccc.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 12 | #include <lib/xlat_tables/xlat_tables_v2.h> |
Andre Przywara | abb7ce1 | 2020-09-25 16:42:06 +0100 | [diff] [blame] | 13 | #include <services/arm_arch_svc.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 14 | |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 15 | #include <sunxi_def.h> |
Andre Przywara | 9b49072 | 2018-10-14 11:45:41 +0100 | [diff] [blame] | 16 | #include <sunxi_mmap.h> |
Andre Przywara | 456208a | 2018-10-14 12:02:02 +0100 | [diff] [blame] | 17 | #include <sunxi_private.h> |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 18 | |
Samuel Holland | fde9e1c | 2020-12-13 21:26:36 -0600 | [diff] [blame] | 19 | static const mmap_region_t sunxi_mmap[MAX_STATIC_MMAP_REGIONS + 1] = { |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 20 | MAP_REGION_FLAT(SUNXI_SRAM_BASE, SUNXI_SRAM_SIZE, |
Samuel Holland | d002f3b | 2019-12-29 12:22:55 -0600 | [diff] [blame] | 21 | MT_DEVICE | MT_RW | MT_SECURE | MT_EXECUTE_NEVER), |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 22 | MAP_REGION_FLAT(SUNXI_DEV_BASE, SUNXI_DEV_SIZE, |
Samuel Holland | a4fbdfa | 2019-10-27 17:30:15 -0500 | [diff] [blame] | 23 | MT_DEVICE | MT_RW | MT_SECURE | MT_EXECUTE_NEVER), |
Andre Przywara | cd1c67e | 2020-11-28 01:38:15 +0000 | [diff] [blame] | 24 | MAP_REGION(PRELOADED_BL33_BASE, SUNXI_BL33_VIRT_BASE, |
Andre Przywara | fb83833 | 2020-12-14 12:06:24 +0000 | [diff] [blame] | 25 | SUNXI_DRAM_MAP_SIZE, MT_RW_DATA | MT_NS), |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 26 | {}, |
| 27 | }; |
| 28 | |
| 29 | unsigned int plat_get_syscnt_freq2(void) |
| 30 | { |
| 31 | return SUNXI_OSC24M_CLK_IN_HZ; |
| 32 | } |
| 33 | |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 34 | void sunxi_configure_mmu_el3(int flags) |
| 35 | { |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 36 | mmap_add_region(BL_CODE_BASE, BL_CODE_BASE, |
| 37 | BL_CODE_END - BL_CODE_BASE, |
| 38 | MT_CODE | MT_SECURE); |
Samuel Holland | 3683f3e | 2020-12-13 20:45:49 -0600 | [diff] [blame] | 39 | mmap_add_region(BL_CODE_END, BL_CODE_END, |
| 40 | BL_END - BL_CODE_END, |
| 41 | MT_RW_DATA | MT_SECURE); |
| 42 | #if SEPARATE_CODE_AND_RODATA |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 43 | mmap_add_region(BL_RO_DATA_BASE, BL_RO_DATA_BASE, |
| 44 | BL_RO_DATA_END - BL_RO_DATA_BASE, |
| 45 | MT_RO_DATA | MT_SECURE); |
Samuel Holland | 3683f3e | 2020-12-13 20:45:49 -0600 | [diff] [blame] | 46 | #endif |
| 47 | #if SEPARATE_NOBITS_REGION |
| 48 | mmap_add_region(BL_NOBITS_BASE, BL_NOBITS_BASE, |
| 49 | BL_NOBITS_END - BL_NOBITS_BASE, |
| 50 | MT_RW_DATA | MT_SECURE); |
| 51 | #endif |
| 52 | #if USE_COHERENT_MEM |
Samuel Holland | f4bfcac | 2019-10-27 17:21:24 -0500 | [diff] [blame] | 53 | mmap_add_region(BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_BASE, |
| 54 | BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE, |
| 55 | MT_DEVICE | MT_RW | MT_SECURE | MT_EXECUTE_NEVER); |
Samuel Holland | 3683f3e | 2020-12-13 20:45:49 -0600 | [diff] [blame] | 56 | #endif |
Samuel Holland | f4bfcac | 2019-10-27 17:21:24 -0500 | [diff] [blame] | 57 | |
Samuel Holland | b856664 | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 58 | mmap_add(sunxi_mmap); |
| 59 | init_xlat_tables(); |
| 60 | |
| 61 | enable_mmu_el3(0); |
| 62 | } |
Andre Przywara | c2366b9 | 2018-06-22 00:47:08 +0100 | [diff] [blame] | 63 | |
| 64 | #define SRAM_VER_REG (SUNXI_SYSCON_BASE + 0x24) |
| 65 | uint16_t sunxi_read_soc_id(void) |
| 66 | { |
| 67 | uint32_t reg = mmio_read_32(SRAM_VER_REG); |
| 68 | |
| 69 | /* Set bit 15 to prepare for the SOCID read. */ |
| 70 | mmio_write_32(SRAM_VER_REG, reg | BIT(15)); |
| 71 | |
| 72 | reg = mmio_read_32(SRAM_VER_REG); |
| 73 | |
| 74 | /* deactivate the SOCID access again */ |
| 75 | mmio_write_32(SRAM_VER_REG, reg & ~BIT(15)); |
| 76 | |
| 77 | return reg >> 16; |
| 78 | } |
Andre Przywara | 435464d | 2018-10-14 12:03:23 +0100 | [diff] [blame] | 79 | |
| 80 | /* |
| 81 | * Configure a given pin to the GPIO-OUT function and sets its level. |
| 82 | * The port is given as a capital letter, the pin is the number within |
| 83 | * this port group. |
| 84 | * So to set pin PC7 to high, use: sunxi_set_gpio_out('C', 7, true); |
| 85 | */ |
| 86 | void sunxi_set_gpio_out(char port, int pin, bool level_high) |
| 87 | { |
| 88 | uintptr_t port_base; |
| 89 | |
| 90 | if (port < 'A' || port > 'L') |
| 91 | return; |
| 92 | if (port == 'L') |
| 93 | port_base = SUNXI_R_PIO_BASE; |
| 94 | else |
| 95 | port_base = SUNXI_PIO_BASE + (port - 'A') * 0x24; |
| 96 | |
| 97 | /* Set the new level first before configuring the pin. */ |
| 98 | if (level_high) |
| 99 | mmio_setbits_32(port_base + 0x10, BIT(pin)); |
| 100 | else |
| 101 | mmio_clrbits_32(port_base + 0x10, BIT(pin)); |
| 102 | |
| 103 | /* configure pin as GPIO out (4(3) bits per pin, 1: GPIO out */ |
| 104 | mmio_clrsetbits_32(port_base + (pin / 8) * 4, |
| 105 | 0x7 << ((pin % 8) * 4), |
| 106 | 0x1 << ((pin % 8) * 4)); |
| 107 | } |
Andre Przywara | 6753776 | 2018-10-14 22:13:53 +0100 | [diff] [blame] | 108 | |
| 109 | int sunxi_init_platform_r_twi(uint16_t socid, bool use_rsb) |
| 110 | { |
| 111 | uint32_t pin_func = 0x77; |
| 112 | uint32_t device_bit; |
| 113 | unsigned int reset_offset = 0xb0; |
| 114 | |
| 115 | switch (socid) { |
| 116 | case SUNXI_SOC_H5: |
| 117 | if (use_rsb) |
| 118 | return -ENODEV; |
| 119 | pin_func = 0x22; |
| 120 | device_bit = BIT(6); |
| 121 | break; |
| 122 | case SUNXI_SOC_H6: |
Andre Przywara | bafb561 | 2020-11-24 11:07:10 +0000 | [diff] [blame] | 123 | case SUNXI_SOC_H616: |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 124 | pin_func = use_rsb ? 0x22 : 0x33; |
Andre Przywara | 6753776 | 2018-10-14 22:13:53 +0100 | [diff] [blame] | 125 | device_bit = BIT(16); |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 126 | reset_offset = use_rsb ? 0x1bc : 0x19c; |
Andre Przywara | 6753776 | 2018-10-14 22:13:53 +0100 | [diff] [blame] | 127 | break; |
| 128 | case SUNXI_SOC_A64: |
| 129 | pin_func = use_rsb ? 0x22 : 0x33; |
| 130 | device_bit = use_rsb ? BIT(3) : BIT(6); |
| 131 | break; |
| 132 | default: |
| 133 | INFO("R_I2C/RSB on Allwinner 0x%x SoC not supported\n", socid); |
| 134 | return -ENODEV; |
| 135 | } |
| 136 | |
| 137 | /* un-gate R_PIO clock */ |
Andre Przywara | 2d42e5f | 2020-11-28 01:39:17 +0000 | [diff] [blame] | 138 | if (socid != SUNXI_SOC_H6 && socid != SUNXI_SOC_H616) |
Andre Przywara | 6753776 | 2018-10-14 22:13:53 +0100 | [diff] [blame] | 139 | mmio_setbits_32(SUNXI_R_PRCM_BASE + 0x28, BIT(0)); |
| 140 | |
| 141 | /* switch pins PL0 and PL1 to the desired function */ |
| 142 | mmio_clrsetbits_32(SUNXI_R_PIO_BASE + 0x00, 0xffU, pin_func); |
| 143 | |
| 144 | /* level 2 drive strength */ |
| 145 | mmio_clrsetbits_32(SUNXI_R_PIO_BASE + 0x14, 0x0fU, 0xaU); |
| 146 | |
| 147 | /* set both pins to pull-up */ |
| 148 | mmio_clrsetbits_32(SUNXI_R_PIO_BASE + 0x1c, 0x0fU, 0x5U); |
| 149 | |
Andre Przywara | 6753776 | 2018-10-14 22:13:53 +0100 | [diff] [blame] | 150 | /* un-gate clock */ |
Andre Przywara | 2d42e5f | 2020-11-28 01:39:17 +0000 | [diff] [blame] | 151 | if (socid != SUNXI_SOC_H6 && socid != SUNXI_SOC_H616) |
Andre Przywara | 6753776 | 2018-10-14 22:13:53 +0100 | [diff] [blame] | 152 | mmio_setbits_32(SUNXI_R_PRCM_BASE + 0x28, device_bit); |
| 153 | else |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 154 | mmio_setbits_32(SUNXI_R_PRCM_BASE + reset_offset, BIT(0)); |
Andre Przywara | 6753776 | 2018-10-14 22:13:53 +0100 | [diff] [blame] | 155 | |
Samuel Holland | f9da134 | 2019-10-20 14:17:30 -0500 | [diff] [blame] | 156 | /* assert, then de-assert reset of I2C/RSB controller */ |
| 157 | mmio_clrbits_32(SUNXI_R_PRCM_BASE + reset_offset, device_bit); |
| 158 | mmio_setbits_32(SUNXI_R_PRCM_BASE + reset_offset, device_bit); |
| 159 | |
Andre Przywara | 6753776 | 2018-10-14 22:13:53 +0100 | [diff] [blame] | 160 | return 0; |
| 161 | } |
Andre Przywara | abb7ce1 | 2020-09-25 16:42:06 +0100 | [diff] [blame] | 162 | |
| 163 | int32_t plat_is_smccc_feature_available(u_register_t fid) |
| 164 | { |
| 165 | switch (fid) { |
| 166 | case SMCCC_ARCH_SOC_ID: |
| 167 | return SMC_ARCH_CALL_SUCCESS; |
| 168 | default: |
| 169 | return SMC_ARCH_CALL_NOT_SUPPORTED; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | int32_t plat_get_soc_version(void) |
| 174 | { |
| 175 | int32_t ret; |
| 176 | |
| 177 | ret = SOC_ID_SET_JEP_106(JEDEC_ALLWINNER_BKID, JEDEC_ALLWINNER_MFID); |
| 178 | |
| 179 | return ret | (sunxi_read_soc_id() & SOC_ID_IMPL_DEF_MASK); |
| 180 | } |
| 181 | |
| 182 | int32_t plat_get_soc_revision(void) |
| 183 | { |
| 184 | uint32_t reg = mmio_read_32(SRAM_VER_REG); |
| 185 | |
| 186 | return reg & GENMASK_32(7, 0); |
| 187 | } |