Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 1 | /* |
Samuel Holland | f95b368 | 2019-10-20 15:12:20 -0500 | [diff] [blame] | 2 | * Copyright (c) 2017-2019, ARM Limited and Contributors. All rights reserved. |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 3 | * Copyright (c) 2018, Icenowy Zheng <icenowy@aosc.io> |
| 4 | * |
| 5 | * SPDX-License-Identifier: BSD-3-Clause |
| 6 | */ |
| 7 | |
Andre Przywara | a920a77 | 2018-10-02 00:21:49 +0100 | [diff] [blame] | 8 | #include <errno.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | |
Andre Przywara | a920a77 | 2018-10-02 00:21:49 +0100 | [diff] [blame] | 10 | #include <platform_def.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 11 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 12 | #include <common/debug.h> |
Samuel Holland | 5614789 | 2019-10-20 20:50:57 -0500 | [diff] [blame] | 13 | #include <drivers/allwinner/axp.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 14 | #include <drivers/allwinner/sunxi_rsb.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 15 | #include <lib/mmio.h> |
| 16 | |
Andre Przywara | a920a77 | 2018-10-02 00:21:49 +0100 | [diff] [blame] | 17 | #include <sunxi_def.h> |
| 18 | #include <sunxi_mmap.h> |
Andre Przywara | 456208a | 2018-10-14 12:02:02 +0100 | [diff] [blame] | 19 | #include <sunxi_private.h> |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 20 | |
Andre Przywara | a920a77 | 2018-10-02 00:21:49 +0100 | [diff] [blame] | 21 | static enum pmic_type { |
Samuel Holland | f95b368 | 2019-10-20 15:12:20 -0500 | [diff] [blame] | 22 | UNKNOWN, |
Andre Przywara | a920a77 | 2018-10-02 00:21:49 +0100 | [diff] [blame] | 23 | GENERIC_H5, |
| 24 | GENERIC_A64, |
Andre Przywara | 74f7a95 | 2018-10-02 00:21:53 +0100 | [diff] [blame] | 25 | REF_DESIGN_H5, /* regulators controlled by GPIO pins on port L */ |
Andre Przywara | 7f3c079 | 2018-09-15 01:18:49 +0100 | [diff] [blame] | 26 | AXP803_RSB, /* PMIC connected via RSB on most A64 boards */ |
Andre Przywara | a920a77 | 2018-10-02 00:21:49 +0100 | [diff] [blame] | 27 | } pmic; |
| 28 | |
Andre Przywara | 7f3c079 | 2018-09-15 01:18:49 +0100 | [diff] [blame] | 29 | #define AXP803_HW_ADDR 0x3a3 |
| 30 | #define AXP803_RT_ADDR 0x2d |
| 31 | |
Andre Przywara | a920a77 | 2018-10-02 00:21:49 +0100 | [diff] [blame] | 32 | /* |
| 33 | * On boards without a proper PMIC we struggle to turn off the system properly. |
| 34 | * Try to turn off as much off the system as we can, to reduce power |
| 35 | * consumption. This should be entered with only one core running and SMP |
| 36 | * disabled. |
| 37 | * This function only cares about peripherals. |
| 38 | */ |
Samuel Holland | 7678060 | 2019-10-20 20:00:27 -0500 | [diff] [blame] | 39 | static void sunxi_turn_off_soc(uint16_t socid) |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 40 | { |
Andre Przywara | a920a77 | 2018-10-02 00:21:49 +0100 | [diff] [blame] | 41 | int i; |
| 42 | |
| 43 | /** Turn off most peripherals, most importantly DRAM users. **/ |
| 44 | /* Keep DRAM controller running for now. */ |
| 45 | mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c0, ~BIT_32(14)); |
| 46 | mmio_clrbits_32(SUNXI_CCU_BASE + 0x60, ~BIT_32(14)); |
| 47 | /* Contains msgbox (bit 21) and spinlock (bit 22) */ |
| 48 | mmio_write_32(SUNXI_CCU_BASE + 0x2c4, 0); |
| 49 | mmio_write_32(SUNXI_CCU_BASE + 0x64, 0); |
| 50 | mmio_write_32(SUNXI_CCU_BASE + 0x2c8, 0); |
| 51 | /* Keep PIO controller running for now. */ |
| 52 | mmio_clrbits_32(SUNXI_CCU_BASE + 0x68, ~(BIT_32(5))); |
| 53 | mmio_write_32(SUNXI_CCU_BASE + 0x2d0, 0); |
| 54 | /* Contains UART0 (bit 16) */ |
| 55 | mmio_write_32(SUNXI_CCU_BASE + 0x2d8, 0); |
| 56 | mmio_write_32(SUNXI_CCU_BASE + 0x6c, 0); |
| 57 | mmio_write_32(SUNXI_CCU_BASE + 0x70, 0); |
| 58 | |
| 59 | /** Turn off DRAM controller. **/ |
| 60 | mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c0, BIT_32(14)); |
| 61 | mmio_clrbits_32(SUNXI_CCU_BASE + 0x60, BIT_32(14)); |
| 62 | |
| 63 | /** Migrate CPU and bus clocks away from the PLLs. **/ |
| 64 | /* AHB1: use OSC24M/1, APB1 = AHB1 / 2 */ |
| 65 | mmio_write_32(SUNXI_CCU_BASE + 0x54, 0x1000); |
| 66 | /* APB2: use OSC24M */ |
| 67 | mmio_write_32(SUNXI_CCU_BASE + 0x58, 0x1000000); |
| 68 | /* AHB2: use AHB1 clock */ |
| 69 | mmio_write_32(SUNXI_CCU_BASE + 0x5c, 0); |
| 70 | /* CPU: use OSC24M */ |
| 71 | mmio_write_32(SUNXI_CCU_BASE + 0x50, 0x10000); |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 72 | |
Andre Przywara | a920a77 | 2018-10-02 00:21:49 +0100 | [diff] [blame] | 73 | /** Turn off PLLs. **/ |
| 74 | for (i = 0; i < 6; i++) |
| 75 | mmio_clrbits_32(SUNXI_CCU_BASE + i * 8, BIT(31)); |
| 76 | switch (socid) { |
| 77 | case SUNXI_SOC_H5: |
| 78 | mmio_clrbits_32(SUNXI_CCU_BASE + 0x44, BIT(31)); |
| 79 | break; |
| 80 | case SUNXI_SOC_A64: |
| 81 | mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c, BIT(31)); |
| 82 | mmio_clrbits_32(SUNXI_CCU_BASE + 0x4c, BIT(31)); |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | |
Andre Przywara | 7f3c079 | 2018-09-15 01:18:49 +0100 | [diff] [blame] | 87 | static int rsb_init(void) |
| 88 | { |
| 89 | int ret; |
| 90 | |
| 91 | ret = rsb_init_controller(); |
| 92 | if (ret) |
| 93 | return ret; |
| 94 | |
Samuel Holland | 40b19ae | 2020-12-13 22:53:02 -0600 | [diff] [blame] | 95 | /* Switch to the recommended 3 MHz bus clock. */ |
| 96 | ret = rsb_set_bus_speed(SUNXI_OSC24M_CLK_IN_HZ, 3000000); |
Andre Przywara | 7f3c079 | 2018-09-15 01:18:49 +0100 | [diff] [blame] | 97 | if (ret) |
| 98 | return ret; |
| 99 | |
Samuel Holland | 2d47ab2 | 2020-12-13 22:43:15 -0600 | [diff] [blame] | 100 | /* Initiate an I2C transaction to switch the PMIC to RSB mode. */ |
| 101 | ret = rsb_set_device_mode(AXP20X_MODE_RSB << 16 | AXP20X_MODE_REG << 8); |
Andre Przywara | 7f3c079 | 2018-09-15 01:18:49 +0100 | [diff] [blame] | 102 | if (ret) |
| 103 | return ret; |
| 104 | |
Andre Przywara | 7f3c079 | 2018-09-15 01:18:49 +0100 | [diff] [blame] | 105 | /* Associate the 8-bit runtime address with the 12-bit bus address. */ |
Samuel Holland | 5614789 | 2019-10-20 20:50:57 -0500 | [diff] [blame] | 106 | ret = rsb_assign_runtime_address(AXP803_HW_ADDR, |
| 107 | AXP803_RT_ADDR); |
| 108 | if (ret) |
Andre Przywara | 7f3c079 | 2018-09-15 01:18:49 +0100 | [diff] [blame] | 109 | return ret; |
| 110 | |
Samuel Holland | 5614789 | 2019-10-20 20:50:57 -0500 | [diff] [blame] | 111 | return axp_check_id(); |
Andre Przywara | 6ec3dd5 | 2018-09-16 11:24:05 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Samuel Holland | 5614789 | 2019-10-20 20:50:57 -0500 | [diff] [blame] | 114 | int axp_read(uint8_t reg) |
Andre Przywara | e28d4ce | 2018-09-16 11:24:34 +0100 | [diff] [blame] | 115 | { |
Samuel Holland | 5614789 | 2019-10-20 20:50:57 -0500 | [diff] [blame] | 116 | return rsb_read(AXP803_RT_ADDR, reg); |
Andre Przywara | e28d4ce | 2018-09-16 11:24:34 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Samuel Holland | 5614789 | 2019-10-20 20:50:57 -0500 | [diff] [blame] | 119 | int axp_write(uint8_t reg, uint8_t val) |
Andre Przywara | e28d4ce | 2018-09-16 11:24:34 +0100 | [diff] [blame] | 120 | { |
Samuel Holland | 5614789 | 2019-10-20 20:50:57 -0500 | [diff] [blame] | 121 | return rsb_write(AXP803_RT_ADDR, reg, val); |
Andre Przywara | 6ec3dd5 | 2018-09-16 11:24:05 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Andre Przywara | 4e4b1e6 | 2018-09-08 19:18:37 +0100 | [diff] [blame] | 124 | int sunxi_pmic_setup(uint16_t socid, const void *fdt) |
Andre Przywara | a920a77 | 2018-10-02 00:21:49 +0100 | [diff] [blame] | 125 | { |
Andre Przywara | 7f3c079 | 2018-09-15 01:18:49 +0100 | [diff] [blame] | 126 | int ret; |
| 127 | |
Andre Przywara | a920a77 | 2018-10-02 00:21:49 +0100 | [diff] [blame] | 128 | switch (socid) { |
| 129 | case SUNXI_SOC_H5: |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame] | 130 | NOTICE("PMIC: Assuming H5 reference regulator design\n"); |
| 131 | |
Andre Przywara | 74f7a95 | 2018-10-02 00:21:53 +0100 | [diff] [blame] | 132 | pmic = REF_DESIGN_H5; |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame] | 133 | |
Andre Przywara | a920a77 | 2018-10-02 00:21:49 +0100 | [diff] [blame] | 134 | break; |
| 135 | case SUNXI_SOC_A64: |
| 136 | pmic = GENERIC_A64; |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame] | 137 | |
| 138 | INFO("PMIC: Probing AXP803 on RSB\n"); |
| 139 | |
Andre Przywara | 7f3c079 | 2018-09-15 01:18:49 +0100 | [diff] [blame] | 140 | ret = sunxi_init_platform_r_twi(socid, true); |
| 141 | if (ret) |
| 142 | return ret; |
| 143 | |
| 144 | ret = rsb_init(); |
| 145 | if (ret) |
| 146 | return ret; |
| 147 | |
| 148 | pmic = AXP803_RSB; |
Samuel Holland | 5614789 | 2019-10-20 20:50:57 -0500 | [diff] [blame] | 149 | axp_setup_regulators(fdt); |
Andre Przywara | 6ec3dd5 | 2018-09-16 11:24:05 +0100 | [diff] [blame] | 150 | |
Samuel Holland | 2d47ab2 | 2020-12-13 22:43:15 -0600 | [diff] [blame] | 151 | /* Switch the PMIC back to I2C mode. */ |
| 152 | ret = axp_write(AXP20X_MODE_REG, AXP20X_MODE_I2C); |
| 153 | if (ret) |
| 154 | return ret; |
| 155 | |
Andre Przywara | a920a77 | 2018-10-02 00:21:49 +0100 | [diff] [blame] | 156 | break; |
| 157 | default: |
Andre Przywara | a920a77 | 2018-10-02 00:21:49 +0100 | [diff] [blame] | 158 | return -ENODEV; |
| 159 | } |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 160 | return 0; |
| 161 | } |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 162 | |
Samuel Holland | fa4d935 | 2019-10-20 15:06:57 -0500 | [diff] [blame] | 163 | void sunxi_power_down(void) |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 164 | { |
Andre Przywara | a920a77 | 2018-10-02 00:21:49 +0100 | [diff] [blame] | 165 | switch (pmic) { |
| 166 | case GENERIC_H5: |
| 167 | /* Turn off as many peripherals and clocks as we can. */ |
| 168 | sunxi_turn_off_soc(SUNXI_SOC_H5); |
| 169 | /* Turn off the pin controller now. */ |
| 170 | mmio_write_32(SUNXI_CCU_BASE + 0x68, 0); |
| 171 | break; |
| 172 | case GENERIC_A64: |
| 173 | /* Turn off as many peripherals and clocks as we can. */ |
| 174 | sunxi_turn_off_soc(SUNXI_SOC_A64); |
| 175 | /* Turn off the pin controller now. */ |
| 176 | mmio_write_32(SUNXI_CCU_BASE + 0x68, 0); |
| 177 | break; |
Andre Przywara | 74f7a95 | 2018-10-02 00:21:53 +0100 | [diff] [blame] | 178 | case REF_DESIGN_H5: |
| 179 | sunxi_turn_off_soc(SUNXI_SOC_H5); |
| 180 | |
| 181 | /* |
| 182 | * Switch PL pins to power off the board: |
| 183 | * - PL5 (VCC_IO) -> high |
| 184 | * - PL8 (PWR-STB = CPU power supply) -> low |
| 185 | * - PL9 (PWR-DRAM) ->low |
| 186 | * - PL10 (power LED) -> low |
| 187 | * Note: Clearing PL8 will reset the board, so keep it up. |
| 188 | */ |
| 189 | sunxi_set_gpio_out('L', 5, 1); |
| 190 | sunxi_set_gpio_out('L', 9, 0); |
| 191 | sunxi_set_gpio_out('L', 10, 0); |
| 192 | |
| 193 | /* Turn off pin controller now. */ |
| 194 | mmio_write_32(SUNXI_CCU_BASE + 0x68, 0); |
| 195 | |
| 196 | break; |
Andre Przywara | 7f3c079 | 2018-09-15 01:18:49 +0100 | [diff] [blame] | 197 | case AXP803_RSB: |
| 198 | /* (Re-)init RSB in case the rich OS has disabled it. */ |
| 199 | sunxi_init_platform_r_twi(SUNXI_SOC_A64, true); |
| 200 | rsb_init(); |
Samuel Holland | 5614789 | 2019-10-20 20:50:57 -0500 | [diff] [blame] | 201 | axp_power_off(); |
Andre Przywara | 7f3c079 | 2018-09-15 01:18:49 +0100 | [diff] [blame] | 202 | break; |
Andre Przywara | a920a77 | 2018-10-02 00:21:49 +0100 | [diff] [blame] | 203 | default: |
| 204 | break; |
| 205 | } |
| 206 | |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 207 | } |