Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: BSD-3-Clause |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Qualcomm SPMI bus driver |
| 4 | * |
| 5 | * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com> |
| 6 | * |
| 7 | * Loosely based on Little Kernel driver |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <dm.h> |
| 12 | #include <errno.h> |
| 13 | #include <fdtdec.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 15 | #include <asm/io.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <dm/device_compat.h> |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 17 | #include <spmi/spmi.h> |
| 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Jorge Ramirez-Ortiz | 210d959 | 2018-01-10 11:33:28 +0100 | [diff] [blame] | 21 | /* PMIC Arbiter configuration registers */ |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 22 | #define PMIC_ARB_VERSION 0x0000 |
| 23 | #define PMIC_ARB_VERSION_V2_MIN 0x20010000 |
| 24 | #define PMIC_ARB_VERSION_V3_MIN 0x30000000 |
| 25 | #define PMIC_ARB_VERSION_V5_MIN 0x50000000 |
Jorge Ramirez-Ortiz | 210d959 | 2018-01-10 11:33:28 +0100 | [diff] [blame] | 26 | |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 27 | #define APID_MAP_OFFSET_V1_V2_V3 (0x800) |
| 28 | #define APID_MAP_OFFSET_V5 (0x900) |
| 29 | #define ARB_CHANNEL_OFFSET(n) (0x4 * (n)) |
| 30 | #define SPMI_CH_OFFSET(chnl) ((chnl) * 0x8000) |
| 31 | #define SPMI_V5_OBS_CH_OFFSET(chnl) ((chnl) * 0x80) |
| 32 | #define SPMI_V5_RW_CH_OFFSET(chnl) ((chnl) * 0x10000) |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 33 | |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 34 | #define SPMI_REG_CMD0 0x0 |
| 35 | #define SPMI_REG_CONFIG 0x4 |
| 36 | #define SPMI_REG_STATUS 0x8 |
| 37 | #define SPMI_REG_WDATA 0x10 |
| 38 | #define SPMI_REG_RDATA 0x18 |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 39 | |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 40 | #define SPMI_CMD_OPCODE_SHIFT 27 |
| 41 | #define SPMI_CMD_SLAVE_ID_SHIFT 20 |
| 42 | #define SPMI_CMD_ADDR_SHIFT 12 |
| 43 | #define SPMI_CMD_ADDR_OFFSET_SHIFT 4 |
| 44 | #define SPMI_CMD_BYTE_CNT_SHIFT 0 |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 45 | |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 46 | #define SPMI_CMD_EXT_REG_WRITE_LONG 0x00 |
| 47 | #define SPMI_CMD_EXT_REG_READ_LONG 0x01 |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 48 | |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 49 | #define SPMI_STATUS_DONE 0x1 |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 50 | |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 51 | #define SPMI_MAX_CHANNELS 128 |
| 52 | #define SPMI_MAX_SLAVES 16 |
| 53 | #define SPMI_MAX_PERIPH 256 |
| 54 | |
| 55 | enum arb_ver { |
| 56 | V1 = 1, |
| 57 | V2, |
| 58 | V3, |
| 59 | V5 = 5 |
| 60 | }; |
| 61 | |
| 62 | /* |
| 63 | * PMIC arbiter version 5 uses different register offsets for read/write vs |
| 64 | * observer channels. |
| 65 | */ |
| 66 | enum pmic_arb_channel { |
| 67 | PMIC_ARB_CHANNEL_RW, |
| 68 | PMIC_ARB_CHANNEL_OBS, |
| 69 | }; |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 70 | |
| 71 | struct msm_spmi_priv { |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 72 | phys_addr_t arb_chnl; /* ARB channel mapping base */ |
Caleb Connolly | 92fe089 | 2023-12-05 13:46:53 +0000 | [diff] [blame] | 73 | phys_addr_t spmi_chnls; /* SPMI channels */ |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 74 | phys_addr_t spmi_obs; /* SPMI observer */ |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 75 | /* SPMI channel map */ |
| 76 | uint8_t channel_map[SPMI_MAX_SLAVES][SPMI_MAX_PERIPH]; |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 77 | /* SPMI bus arbiter version */ |
| 78 | u32 arb_ver; |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | static int msm_spmi_write(struct udevice *dev, int usid, int pid, int off, |
| 82 | uint8_t val) |
| 83 | { |
| 84 | struct msm_spmi_priv *priv = dev_get_priv(dev); |
| 85 | unsigned channel; |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 86 | unsigned int ch_offset; |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 87 | uint32_t reg = 0; |
| 88 | |
| 89 | if (usid >= SPMI_MAX_SLAVES) |
| 90 | return -EIO; |
| 91 | if (pid >= SPMI_MAX_PERIPH) |
| 92 | return -EIO; |
| 93 | |
| 94 | channel = priv->channel_map[usid][pid]; |
| 95 | |
| 96 | /* Disable IRQ mode for the current channel*/ |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 97 | writel(0x0, |
Caleb Connolly | 92fe089 | 2023-12-05 13:46:53 +0000 | [diff] [blame] | 98 | priv->spmi_chnls + SPMI_CH_OFFSET(channel) + SPMI_REG_CONFIG); |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 99 | |
| 100 | /* Write single byte */ |
Caleb Connolly | 92fe089 | 2023-12-05 13:46:53 +0000 | [diff] [blame] | 101 | writel(val, priv->spmi_chnls + SPMI_CH_OFFSET(channel) + SPMI_REG_WDATA); |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 102 | |
| 103 | /* Prepare write command */ |
| 104 | reg |= SPMI_CMD_EXT_REG_WRITE_LONG << SPMI_CMD_OPCODE_SHIFT; |
| 105 | reg |= (usid << SPMI_CMD_SLAVE_ID_SHIFT); |
| 106 | reg |= (pid << SPMI_CMD_ADDR_SHIFT); |
| 107 | reg |= (off << SPMI_CMD_ADDR_OFFSET_SHIFT); |
| 108 | reg |= 1; /* byte count */ |
| 109 | |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 110 | if (priv->arb_ver == V5) |
| 111 | ch_offset = SPMI_V5_RW_CH_OFFSET(channel); |
| 112 | else |
| 113 | ch_offset = SPMI_CH_OFFSET(channel); |
| 114 | |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 115 | /* Send write command */ |
Caleb Connolly | 92fe089 | 2023-12-05 13:46:53 +0000 | [diff] [blame] | 116 | writel(reg, priv->spmi_chnls + SPMI_CH_OFFSET(channel) + SPMI_REG_CMD0); |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 117 | |
| 118 | /* Wait till CMD DONE status */ |
| 119 | reg = 0; |
| 120 | while (!reg) { |
Caleb Connolly | 92fe089 | 2023-12-05 13:46:53 +0000 | [diff] [blame] | 121 | reg = readl(priv->spmi_chnls + SPMI_CH_OFFSET(channel) + |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 122 | SPMI_REG_STATUS); |
| 123 | } |
| 124 | |
| 125 | if (reg ^ SPMI_STATUS_DONE) { |
| 126 | printf("SPMI write failure.\n"); |
| 127 | return -EIO; |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static int msm_spmi_read(struct udevice *dev, int usid, int pid, int off) |
| 134 | { |
| 135 | struct msm_spmi_priv *priv = dev_get_priv(dev); |
| 136 | unsigned channel; |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 137 | unsigned int ch_offset; |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 138 | uint32_t reg = 0; |
| 139 | |
| 140 | if (usid >= SPMI_MAX_SLAVES) |
| 141 | return -EIO; |
| 142 | if (pid >= SPMI_MAX_PERIPH) |
| 143 | return -EIO; |
| 144 | |
| 145 | channel = priv->channel_map[usid][pid]; |
| 146 | |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 147 | if (priv->arb_ver == V5) |
| 148 | ch_offset = SPMI_V5_OBS_CH_OFFSET(channel); |
| 149 | else |
| 150 | ch_offset = SPMI_CH_OFFSET(channel); |
| 151 | |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 152 | /* Disable IRQ mode for the current channel*/ |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 153 | writel(0x0, priv->spmi_obs + ch_offset + SPMI_REG_CONFIG); |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 154 | |
| 155 | /* Prepare read command */ |
| 156 | reg |= SPMI_CMD_EXT_REG_READ_LONG << SPMI_CMD_OPCODE_SHIFT; |
| 157 | reg |= (usid << SPMI_CMD_SLAVE_ID_SHIFT); |
| 158 | reg |= (pid << SPMI_CMD_ADDR_SHIFT); |
| 159 | reg |= (off << SPMI_CMD_ADDR_OFFSET_SHIFT); |
| 160 | reg |= 1; /* byte count */ |
| 161 | |
| 162 | /* Request read */ |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 163 | writel(reg, priv->spmi_obs + ch_offset + SPMI_REG_CMD0); |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 164 | |
| 165 | /* Wait till CMD DONE status */ |
| 166 | reg = 0; |
| 167 | while (!reg) { |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 168 | reg = readl(priv->spmi_obs + ch_offset + SPMI_REG_STATUS); |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | if (reg ^ SPMI_STATUS_DONE) { |
| 172 | printf("SPMI read failure.\n"); |
| 173 | return -EIO; |
| 174 | } |
| 175 | |
| 176 | /* Read the data */ |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 177 | return readl(priv->spmi_obs + ch_offset + |
| 178 | SPMI_REG_RDATA) & 0xFF; |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static struct dm_spmi_ops msm_spmi_ops = { |
| 182 | .read = msm_spmi_read, |
| 183 | .write = msm_spmi_write, |
| 184 | }; |
| 185 | |
| 186 | static int msm_spmi_probe(struct udevice *dev) |
| 187 | { |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 188 | struct msm_spmi_priv *priv = dev_get_priv(dev); |
Caleb Connolly | 92fe089 | 2023-12-05 13:46:53 +0000 | [diff] [blame] | 189 | phys_addr_t core_addr; |
Jorge Ramirez-Ortiz | 210d959 | 2018-01-10 11:33:28 +0100 | [diff] [blame] | 190 | u32 hw_ver; |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 191 | int i; |
| 192 | |
Caleb Connolly | 92fe089 | 2023-12-05 13:46:53 +0000 | [diff] [blame] | 193 | core_addr = dev_read_addr_name(dev, "core"); |
| 194 | priv->spmi_chnls = dev_read_addr_name(dev, "chnls"); |
| 195 | priv->spmi_obs = dev_read_addr_name(dev, "obsrvr"); |
Jorge Ramirez-Ortiz | 210d959 | 2018-01-10 11:33:28 +0100 | [diff] [blame] | 196 | |
Caleb Connolly | 92fe089 | 2023-12-05 13:46:53 +0000 | [diff] [blame] | 197 | hw_ver = readl(core_addr + PMIC_ARB_VERSION); |
Jorge Ramirez-Ortiz | 210d959 | 2018-01-10 11:33:28 +0100 | [diff] [blame] | 198 | |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 199 | if (hw_ver < PMIC_ARB_VERSION_V3_MIN) { |
| 200 | priv->arb_ver = V2; |
Caleb Connolly | 92fe089 | 2023-12-05 13:46:53 +0000 | [diff] [blame] | 201 | priv->arb_chnl = core_addr + APID_MAP_OFFSET_V1_V2_V3; |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 202 | } else if (hw_ver < PMIC_ARB_VERSION_V5_MIN) { |
| 203 | priv->arb_ver = V3; |
Caleb Connolly | 92fe089 | 2023-12-05 13:46:53 +0000 | [diff] [blame] | 204 | priv->arb_chnl = core_addr + APID_MAP_OFFSET_V1_V2_V3; |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 205 | } else { |
| 206 | priv->arb_ver = V5; |
Caleb Connolly | 92fe089 | 2023-12-05 13:46:53 +0000 | [diff] [blame] | 207 | priv->arb_chnl = core_addr + APID_MAP_OFFSET_V5; |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 208 | } |
| 209 | |
Caleb Connolly | 92fe089 | 2023-12-05 13:46:53 +0000 | [diff] [blame] | 210 | dev_dbg(dev, "PMIC Arb Version-%d (%#x)\n", hw_ver >> 28, hw_ver); |
Jorge Ramirez-Ortiz | 210d959 | 2018-01-10 11:33:28 +0100 | [diff] [blame] | 211 | |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 212 | if (priv->arb_chnl == FDT_ADDR_T_NONE || |
Caleb Connolly | 92fe089 | 2023-12-05 13:46:53 +0000 | [diff] [blame] | 213 | priv->spmi_chnls == FDT_ADDR_T_NONE || |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 214 | priv->spmi_obs == FDT_ADDR_T_NONE) |
| 215 | return -EINVAL; |
| 216 | |
Caleb Connolly | 92fe089 | 2023-12-05 13:46:53 +0000 | [diff] [blame] | 217 | dev_dbg(dev, "priv->arb_chnl address (%#08llx)\n", priv->arb_chnl); |
| 218 | dev_dbg(dev, "priv->spmi_chnls address (%#08llx)\n", priv->spmi_chnls); |
| 219 | dev_dbg(dev, "priv->spmi_obs address (%#08llx)\n", priv->spmi_obs); |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 220 | /* Scan peripherals connected to each SPMI channel */ |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 221 | for (i = 0; i < SPMI_MAX_PERIPH; i++) { |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 222 | uint32_t periph = readl(priv->arb_chnl + ARB_CHANNEL_OFFSET(i)); |
| 223 | uint8_t slave_id = (periph & 0xf0000) >> 16; |
| 224 | uint8_t pid = (periph & 0xff00) >> 8; |
| 225 | |
| 226 | priv->channel_map[slave_id][pid] = i; |
| 227 | } |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static const struct udevice_id msm_spmi_ids[] = { |
| 232 | { .compatible = "qcom,spmi-pmic-arb" }, |
| 233 | { } |
| 234 | }; |
| 235 | |
| 236 | U_BOOT_DRIVER(msm_spmi) = { |
| 237 | .name = "msm_spmi", |
| 238 | .id = UCLASS_SPMI, |
| 239 | .of_match = msm_spmi_ids, |
| 240 | .ops = &msm_spmi_ops, |
| 241 | .probe = msm_spmi_probe, |
Dzmitry Sankouski | f5a2d6b | 2021-10-17 13:44:28 +0300 | [diff] [blame] | 242 | .priv_auto = sizeof(struct msm_spmi_priv), |
Mateusz Kulikowski | 5b47271 | 2016-03-31 23:12:29 +0200 | [diff] [blame] | 243 | }; |