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 | |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 8 | #include <errno.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | #include <common/debug.h> |
Andre Przywara | 4f6b807 | 2023-02-01 22:28:37 +0000 | [diff] [blame] | 11 | #include <common/fdt_wrappers.h> |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 12 | #include <drivers/allwinner/axp.h> |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 13 | #include <drivers/allwinner/sunxi_rsb.h> |
Andre Przywara | 4f6b807 | 2023-02-01 22:28:37 +0000 | [diff] [blame] | 14 | #include <libfdt.h> |
Andre Przywara | 5176075 | 2021-02-14 23:56:04 +0000 | [diff] [blame] | 15 | #include <lib/mmio.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 16 | |
Andre Przywara | 5176075 | 2021-02-14 23:56:04 +0000 | [diff] [blame] | 17 | #include <sunxi_cpucfg.h> |
Andre Przywara | 6753776 | 2018-10-14 22:13:53 +0100 | [diff] [blame] | 18 | #include <sunxi_def.h> |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 19 | #include <sunxi_mmap.h> |
Andre Przywara | 456208a | 2018-10-14 12:02:02 +0100 | [diff] [blame] | 20 | #include <sunxi_private.h> |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 21 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 22 | #define AXP805_HW_ADDR 0x745 |
| 23 | #define AXP805_RT_ADDR 0x3a |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 24 | |
Samuel Holland | f95b368 | 2019-10-20 15:12:20 -0500 | [diff] [blame] | 25 | static enum pmic_type { |
| 26 | UNKNOWN, |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 27 | AXP805, |
Samuel Holland | f95b368 | 2019-10-20 15:12:20 -0500 | [diff] [blame] | 28 | } pmic; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 29 | |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 30 | int axp_read(uint8_t reg) |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 31 | { |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 32 | return rsb_read(AXP805_RT_ADDR, reg); |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 33 | } |
| 34 | |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 35 | int axp_write(uint8_t reg, uint8_t val) |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 36 | { |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 37 | return rsb_write(AXP805_RT_ADDR, reg, val); |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 38 | } |
| 39 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 40 | static int rsb_init(void) |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 41 | { |
| 42 | int ret; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 43 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 44 | ret = rsb_init_controller(); |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame] | 45 | if (ret) |
| 46 | return ret; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 47 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 48 | /* Switch to the recommended 3 MHz bus clock. */ |
| 49 | ret = rsb_set_bus_speed(SUNXI_OSC24M_CLK_IN_HZ, 3000000); |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame] | 50 | if (ret) |
| 51 | return ret; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 52 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 53 | /* Initiate an I2C transaction to switch the PMIC to RSB mode. */ |
| 54 | ret = rsb_set_device_mode(AXP20X_MODE_RSB << 16 | AXP20X_MODE_REG << 8); |
| 55 | if (ret) |
| 56 | return ret; |
| 57 | |
| 58 | /* Associate the 8-bit runtime address with the 12-bit bus address. */ |
| 59 | ret = rsb_assign_runtime_address(AXP805_HW_ADDR, AXP805_RT_ADDR); |
| 60 | if (ret) |
| 61 | return ret; |
| 62 | |
| 63 | return axp_check_id(); |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 64 | } |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 65 | |
Andre Przywara | 4e4b1e6 | 2018-09-08 19:18:37 +0100 | [diff] [blame] | 66 | int sunxi_pmic_setup(uint16_t socid, const void *fdt) |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 67 | { |
Andre Przywara | 4f6b807 | 2023-02-01 22:28:37 +0000 | [diff] [blame] | 68 | int node, ret; |
| 69 | |
| 70 | node = fdt_node_offset_by_compatible(fdt, 0, "allwinner,sun8i-a23-rsb"); |
| 71 | if ((node < 0) || !fdt_node_is_enabled(fdt, node)) { |
| 72 | return -ENODEV; |
| 73 | } |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 74 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 75 | INFO("PMIC: Probing AXP805 on RSB\n"); |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame] | 76 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 77 | ret = sunxi_init_platform_r_twi(socid, true); |
Samuel Holland | f39fd86 | 2019-10-20 15:28:14 -0500 | [diff] [blame] | 78 | if (ret) |
| 79 | return ret; |
| 80 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 81 | ret = rsb_init(); |
| 82 | if (ret) |
| 83 | return ret; |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 84 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 85 | /* Switch the AXP805 to master/single-PMIC mode. */ |
| 86 | ret = axp_write(0xff, 0x0); |
Icenowy Zheng | 8d76982 | 2018-07-22 21:30:14 +0800 | [diff] [blame] | 87 | if (ret) |
Samuel Holland | f95b368 | 2019-10-20 15:12:20 -0500 | [diff] [blame] | 88 | return ret; |
| 89 | |
| 90 | pmic = AXP805; |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 91 | axp_setup_regulators(fdt); |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 92 | |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 93 | /* Switch the PMIC back to I2C mode. */ |
| 94 | ret = axp_write(AXP20X_MODE_REG, AXP20X_MODE_I2C); |
| 95 | if (ret) |
| 96 | return ret; |
| 97 | |
Icenowy Zheng | 7508bef | 2018-07-21 20:41:12 +0800 | [diff] [blame] | 98 | return 0; |
| 99 | } |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 100 | |
Samuel Holland | fa4d935 | 2019-10-20 15:06:57 -0500 | [diff] [blame] | 101 | void sunxi_power_down(void) |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 102 | { |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 103 | switch (pmic) { |
| 104 | case AXP805: |
Samuel Holland | cb093f2 | 2020-12-13 22:34:10 -0600 | [diff] [blame] | 105 | /* (Re-)init RSB in case the rich OS has disabled it. */ |
| 106 | sunxi_init_platform_r_twi(SUNXI_SOC_H6, true); |
| 107 | rsb_init(); |
Samuel Holland | 1dad265 | 2019-10-20 21:34:38 -0500 | [diff] [blame] | 108 | axp_power_off(); |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 109 | break; |
| 110 | default: |
| 111 | break; |
| 112 | } |
Icenowy Zheng | bd57eb5 | 2018-07-22 21:52:50 +0800 | [diff] [blame] | 113 | } |
Andre Przywara | 5176075 | 2021-02-14 23:56:04 +0000 | [diff] [blame] | 114 | |
| 115 | void sunxi_cpu_power_off_self(void) |
| 116 | { |
| 117 | u_register_t mpidr = read_mpidr(); |
| 118 | unsigned int core = MPIDR_AFFLVL0_VAL(mpidr); |
| 119 | |
| 120 | /* Enable the CPUIDLE hardware (only really needs to be done once). */ |
| 121 | mmio_write_32(SUNXI_CPUIDLE_EN_REG, 0x16aa0000); |
| 122 | mmio_write_32(SUNXI_CPUIDLE_EN_REG, 0xaa160001); |
| 123 | |
| 124 | /* Trigger power off for this core. */ |
| 125 | mmio_write_32(SUNXI_CORE_CLOSE_REG, BIT_32(core)); |
| 126 | } |