Peng Fan | ac2cb4d | 2021-08-07 16:01:10 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: BSD-3-Clause |
| 2 | /* |
| 3 | * Copyright 2021 NXP |
| 4 | */ |
| 5 | |
| 6 | #include <log.h> |
| 7 | #include <asm/io.h> |
| 8 | #include <asm/arch/imx-regs.h> |
| 9 | #include <linux/delay.h> |
| 10 | |
| 11 | #include "upower_api.h" |
| 12 | |
| 13 | #define UPOWER_AP_MU1_ADDR 0x29280000 |
| 14 | static struct mu_type *muptr = (struct mu_type *)UPOWER_AP_MU1_ADDR; |
| 15 | |
| 16 | void upower_wait_resp(void) |
| 17 | { |
| 18 | while (!(readl(&muptr->rsr) & BIT(0))) { |
| 19 | debug("%s: poll the mu:%x\n", __func__, readl(&muptr->rsr)); |
| 20 | udelay(100); |
| 21 | } |
| 22 | |
| 23 | upwr_txrx_isr(); |
| 24 | } |
| 25 | |
| 26 | u32 upower_status(int status) |
| 27 | { |
| 28 | u32 ret = -1; |
| 29 | |
| 30 | switch (status) { |
| 31 | case 0: |
| 32 | debug("%s: finished successfully!\n", __func__); |
| 33 | ret = 0; |
| 34 | break; |
| 35 | case -1: |
| 36 | printf("%s: memory allocation or resource failed!\n", __func__); |
| 37 | break; |
| 38 | case -2: |
| 39 | printf("%s: invalid argument!\n", __func__); |
| 40 | break; |
| 41 | case -3: |
| 42 | printf("%s: called in an invalid API state!\n", __func__); |
| 43 | break; |
| 44 | default: |
| 45 | printf("%s: invalid return status\n", __func__); |
| 46 | break; |
| 47 | } |
| 48 | return ret; |
| 49 | } |
| 50 | |
| 51 | void user_upwr_rdy_callb(u32 soc, u32 vmajor, u32 vminor) |
| 52 | { |
| 53 | printf("%s: soc=%x\n", __func__, soc); |
| 54 | printf("%s: RAM version:%d.%d\n", __func__, vmajor, vminor); |
| 55 | } |
| 56 | |
| 57 | int upower_pmic_i2c_write(u32 reg_addr, u32 reg_val) |
| 58 | { |
| 59 | int ret, ret_val; |
| 60 | enum upwr_resp err_code; |
| 61 | |
| 62 | ret = upwr_xcp_i2c_access(0x32, 1, 1, reg_addr, reg_val, NULL); |
| 63 | if (ret) { |
| 64 | printf("pmic i2c write failed ret %d\n", ret); |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | upower_wait_resp(); |
| 69 | ret = upwr_poll_req_status(UPWR_SG_EXCEPT, NULL, &err_code, &ret_val, 1000); |
| 70 | if (ret != UPWR_REQ_OK) { |
| 71 | printf("i2c poll Failure %d, err_code %d, ret_val 0x%x\n", ret, err_code, ret_val); |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | debug("PMIC write reg[0x%x], val[0x%x]\n", reg_addr, reg_val); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | int upower_pmic_i2c_read(u32 reg_addr, u32 *reg_val) |
| 81 | { |
| 82 | int ret, ret_val; |
| 83 | enum upwr_resp err_code; |
| 84 | |
| 85 | if (!reg_val) |
| 86 | return -1; |
| 87 | |
| 88 | ret = upwr_xcp_i2c_access(0x32, -1, 1, reg_addr, 0, NULL); |
| 89 | if (ret) { |
| 90 | printf("pmic i2c read failed ret %d\n", ret); |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | upower_wait_resp(); |
| 95 | ret = upwr_poll_req_status(UPWR_SG_EXCEPT, NULL, &err_code, &ret_val, 1000); |
| 96 | if (ret != UPWR_REQ_OK) { |
| 97 | printf("i2c poll Failure %d, err_code %d, ret_val 0x%x\n", ret, err_code, ret_val); |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | *reg_val = ret_val; |
| 102 | |
| 103 | debug("PMIC read reg[0x%x], val[0x%x]\n", reg_addr, *reg_val); |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | int upower_init(void) |
| 109 | { |
| 110 | u32 fw_major, fw_minor, fw_vfixes; |
| 111 | u32 soc_id; |
| 112 | int status; |
| 113 | |
| 114 | u32 swton; |
| 115 | u64 memon; |
| 116 | int ret, ret_val; |
| 117 | |
| 118 | do { |
| 119 | status = upwr_init(1, muptr); |
| 120 | if (upower_status(status)) { |
| 121 | printf("%s: upower init failure\n", __func__); |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | soc_id = upwr_rom_version(&fw_major, &fw_minor, &fw_vfixes); |
| 126 | if (!soc_id) { |
| 127 | printf("%s:, soc_id not initialized\n", __func__); |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | printf("%s: soc_id=%d\n", __func__, soc_id); |
| 132 | printf("%s: version:%d.%d.%d\n", __func__, fw_major, fw_minor, fw_vfixes); |
| 133 | |
| 134 | printf("%s: start uPower RAM service\n", __func__); |
| 135 | status = upwr_start(1, user_upwr_rdy_callb); |
| 136 | upower_wait_resp(); |
| 137 | if (upower_status(status)) { |
| 138 | printf("%s: upower init failure\n", __func__); |
| 139 | break; |
| 140 | } |
| 141 | } while (0); |
| 142 | |
| 143 | swton = 0xfff80; |
| 144 | ret = upwr_pwm_power_on(&swton, NULL, NULL); |
| 145 | if (ret) |
| 146 | printf("Turn on switches fail %d\n", ret); |
| 147 | else |
| 148 | printf("Turn on switches ok\n"); |
| 149 | upower_wait_resp(); |
| 150 | ret = upwr_poll_req_status(UPWR_SG_PWRMGMT, NULL, NULL, &ret_val, 1000); |
| 151 | if (ret != UPWR_REQ_OK) |
| 152 | printf("Failure %d\n", ret); |
| 153 | |
| 154 | memon = 0x3FFFFFFFFFFFFCUL; |
| 155 | ret = upwr_pwm_power_on(NULL, (const u32 *)&memon, NULL); |
| 156 | if (ret) |
| 157 | printf("Turn on memories fail %d\n", ret); |
| 158 | else |
| 159 | printf("Turn on memories ok\n"); |
| 160 | upower_wait_resp(); |
| 161 | ret = upwr_poll_req_status(UPWR_SG_PWRMGMT, NULL, NULL, &ret_val, 1000); |
| 162 | if (ret != UPWR_REQ_OK) |
| 163 | printf("Failure %d\n", ret); |
| 164 | |
| 165 | mdelay(1); |
| 166 | |
| 167 | ret = upwr_xcp_set_ddr_retention(APD_DOMAIN, 0, NULL); |
| 168 | if (ret) |
| 169 | printf("Clear DDR retention fail %d\n", ret); |
| 170 | else |
| 171 | printf("Clear DDR retention ok\n"); |
| 172 | |
| 173 | upower_wait_resp(); |
| 174 | |
| 175 | ret = upwr_poll_req_status(UPWR_SG_EXCEPT, NULL, NULL, &ret_val, 1000); |
| 176 | if (ret != UPWR_REQ_OK) |
| 177 | printf("Failure %d\n", ret); |
| 178 | |
| 179 | return 0; |
| 180 | } |