blob: 82410b1edf367ab5ceb81e43f0e134af66a98714 [file] [log] [blame]
Samuel Hollandb8566642017-08-12 04:07:39 -05001/*
Samuel Hollandfde9e1c2020-12-13 21:26:36 -06002 * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved.
Samuel Hollandb8566642017-08-12 04:07:39 -05003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Andre Przywara67537762018-10-14 22:13:53 +01007#include <errno.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009#include <common/debug.h>
10#include <lib/mmio.h>
11#include <lib/xlat_tables/xlat_tables_v2.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012
Samuel Hollandb8566642017-08-12 04:07:39 -050013#include <sunxi_def.h>
Andre Przywara9b490722018-10-14 11:45:41 +010014#include <sunxi_mmap.h>
Andre Przywara456208a2018-10-14 12:02:02 +010015#include <sunxi_private.h>
Samuel Hollandb8566642017-08-12 04:07:39 -050016
Samuel Hollandfde9e1c2020-12-13 21:26:36 -060017static const mmap_region_t sunxi_mmap[MAX_STATIC_MMAP_REGIONS + 1] = {
Samuel Hollandb8566642017-08-12 04:07:39 -050018 MAP_REGION_FLAT(SUNXI_SRAM_BASE, SUNXI_SRAM_SIZE,
Samuel Hollandd002f3b2019-12-29 12:22:55 -060019 MT_DEVICE | MT_RW | MT_SECURE | MT_EXECUTE_NEVER),
Samuel Hollandb8566642017-08-12 04:07:39 -050020 MAP_REGION_FLAT(SUNXI_DEV_BASE, SUNXI_DEV_SIZE,
Samuel Hollanda4fbdfa2019-10-27 17:30:15 -050021 MT_DEVICE | MT_RW | MT_SECURE | MT_EXECUTE_NEVER),
Andre Przywaracd1c67e2020-11-28 01:38:15 +000022 MAP_REGION(PRELOADED_BL33_BASE, SUNXI_BL33_VIRT_BASE,
Andre Przywarafb838332020-12-14 12:06:24 +000023 SUNXI_DRAM_MAP_SIZE, MT_RW_DATA | MT_NS),
Samuel Hollandb8566642017-08-12 04:07:39 -050024 {},
25};
26
27unsigned int plat_get_syscnt_freq2(void)
28{
29 return SUNXI_OSC24M_CLK_IN_HZ;
30}
31
Samuel Hollandb8566642017-08-12 04:07:39 -050032void sunxi_configure_mmu_el3(int flags)
33{
Samuel Hollandb8566642017-08-12 04:07:39 -050034 mmap_add_region(BL_CODE_BASE, BL_CODE_BASE,
35 BL_CODE_END - BL_CODE_BASE,
36 MT_CODE | MT_SECURE);
Samuel Holland3683f3e2020-12-13 20:45:49 -060037 mmap_add_region(BL_CODE_END, BL_CODE_END,
38 BL_END - BL_CODE_END,
39 MT_RW_DATA | MT_SECURE);
40#if SEPARATE_CODE_AND_RODATA
Samuel Hollandb8566642017-08-12 04:07:39 -050041 mmap_add_region(BL_RO_DATA_BASE, BL_RO_DATA_BASE,
42 BL_RO_DATA_END - BL_RO_DATA_BASE,
43 MT_RO_DATA | MT_SECURE);
Samuel Holland3683f3e2020-12-13 20:45:49 -060044#endif
45#if SEPARATE_NOBITS_REGION
46 mmap_add_region(BL_NOBITS_BASE, BL_NOBITS_BASE,
47 BL_NOBITS_END - BL_NOBITS_BASE,
48 MT_RW_DATA | MT_SECURE);
49#endif
50#if USE_COHERENT_MEM
Samuel Hollandf4bfcac2019-10-27 17:21:24 -050051 mmap_add_region(BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_BASE,
52 BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE,
53 MT_DEVICE | MT_RW | MT_SECURE | MT_EXECUTE_NEVER);
Samuel Holland3683f3e2020-12-13 20:45:49 -060054#endif
Samuel Hollandf4bfcac2019-10-27 17:21:24 -050055
Samuel Hollandb8566642017-08-12 04:07:39 -050056 mmap_add(sunxi_mmap);
57 init_xlat_tables();
58
59 enable_mmu_el3(0);
60}
Andre Przywarac2366b92018-06-22 00:47:08 +010061
62#define SRAM_VER_REG (SUNXI_SYSCON_BASE + 0x24)
63uint16_t sunxi_read_soc_id(void)
64{
65 uint32_t reg = mmio_read_32(SRAM_VER_REG);
66
67 /* Set bit 15 to prepare for the SOCID read. */
68 mmio_write_32(SRAM_VER_REG, reg | BIT(15));
69
70 reg = mmio_read_32(SRAM_VER_REG);
71
72 /* deactivate the SOCID access again */
73 mmio_write_32(SRAM_VER_REG, reg & ~BIT(15));
74
75 return reg >> 16;
76}
Andre Przywara435464d2018-10-14 12:03:23 +010077
78/*
79 * Configure a given pin to the GPIO-OUT function and sets its level.
80 * The port is given as a capital letter, the pin is the number within
81 * this port group.
82 * So to set pin PC7 to high, use: sunxi_set_gpio_out('C', 7, true);
83 */
84void sunxi_set_gpio_out(char port, int pin, bool level_high)
85{
86 uintptr_t port_base;
87
88 if (port < 'A' || port > 'L')
89 return;
90 if (port == 'L')
91 port_base = SUNXI_R_PIO_BASE;
92 else
93 port_base = SUNXI_PIO_BASE + (port - 'A') * 0x24;
94
95 /* Set the new level first before configuring the pin. */
96 if (level_high)
97 mmio_setbits_32(port_base + 0x10, BIT(pin));
98 else
99 mmio_clrbits_32(port_base + 0x10, BIT(pin));
100
101 /* configure pin as GPIO out (4(3) bits per pin, 1: GPIO out */
102 mmio_clrsetbits_32(port_base + (pin / 8) * 4,
103 0x7 << ((pin % 8) * 4),
104 0x1 << ((pin % 8) * 4));
105}
Andre Przywara67537762018-10-14 22:13:53 +0100106
107int sunxi_init_platform_r_twi(uint16_t socid, bool use_rsb)
108{
109 uint32_t pin_func = 0x77;
110 uint32_t device_bit;
111 unsigned int reset_offset = 0xb0;
112
113 switch (socid) {
114 case SUNXI_SOC_H5:
115 if (use_rsb)
116 return -ENODEV;
117 pin_func = 0x22;
118 device_bit = BIT(6);
119 break;
120 case SUNXI_SOC_H6:
Andre Przywarabafb5612020-11-24 11:07:10 +0000121 case SUNXI_SOC_H616:
Samuel Hollandcb093f22020-12-13 22:34:10 -0600122 pin_func = use_rsb ? 0x22 : 0x33;
Andre Przywara67537762018-10-14 22:13:53 +0100123 device_bit = BIT(16);
Samuel Hollandcb093f22020-12-13 22:34:10 -0600124 reset_offset = use_rsb ? 0x1bc : 0x19c;
Andre Przywara67537762018-10-14 22:13:53 +0100125 break;
126 case SUNXI_SOC_A64:
127 pin_func = use_rsb ? 0x22 : 0x33;
128 device_bit = use_rsb ? BIT(3) : BIT(6);
129 break;
130 default:
131 INFO("R_I2C/RSB on Allwinner 0x%x SoC not supported\n", socid);
132 return -ENODEV;
133 }
134
135 /* un-gate R_PIO clock */
Andre Przywara2d42e5f2020-11-28 01:39:17 +0000136 if (socid != SUNXI_SOC_H6 && socid != SUNXI_SOC_H616)
Andre Przywara67537762018-10-14 22:13:53 +0100137 mmio_setbits_32(SUNXI_R_PRCM_BASE + 0x28, BIT(0));
138
139 /* switch pins PL0 and PL1 to the desired function */
140 mmio_clrsetbits_32(SUNXI_R_PIO_BASE + 0x00, 0xffU, pin_func);
141
142 /* level 2 drive strength */
143 mmio_clrsetbits_32(SUNXI_R_PIO_BASE + 0x14, 0x0fU, 0xaU);
144
145 /* set both pins to pull-up */
146 mmio_clrsetbits_32(SUNXI_R_PIO_BASE + 0x1c, 0x0fU, 0x5U);
147
Andre Przywara67537762018-10-14 22:13:53 +0100148 /* un-gate clock */
Andre Przywara2d42e5f2020-11-28 01:39:17 +0000149 if (socid != SUNXI_SOC_H6 && socid != SUNXI_SOC_H616)
Andre Przywara67537762018-10-14 22:13:53 +0100150 mmio_setbits_32(SUNXI_R_PRCM_BASE + 0x28, device_bit);
151 else
Samuel Hollandcb093f22020-12-13 22:34:10 -0600152 mmio_setbits_32(SUNXI_R_PRCM_BASE + reset_offset, BIT(0));
Andre Przywara67537762018-10-14 22:13:53 +0100153
Samuel Hollandf9da1342019-10-20 14:17:30 -0500154 /* assert, then de-assert reset of I2C/RSB controller */
155 mmio_clrbits_32(SUNXI_R_PRCM_BASE + reset_offset, device_bit);
156 mmio_setbits_32(SUNXI_R_PRCM_BASE + reset_offset, device_bit);
157
Andre Przywara67537762018-10-14 22:13:53 +0100158 return 0;
159}