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