blob: 46e2e09dc26e11d5ab4c32a00f86737157ffc5ab [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: BSD-3-Clause
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +02002/*
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 Kulikowski73bec9c2016-03-31 23:12:29 +02008 */
9
10#include <common.h>
11#include <dm.h>
12#include <errno.h>
13#include <fdtdec.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +020015#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +020017#include <spmi/spmi.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
Jorge Ramirez-Ortiz4bcef682018-01-10 11:33:28 +010021/* PMIC Arbiter configuration registers */
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030022#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-Ortiz4bcef682018-01-10 11:33:28 +010026
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030027#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 Kulikowski73bec9c2016-03-31 23:12:29 +020033
Neil Armstrong3fe83672024-04-05 10:21:55 +020034#define SPMI_OWNERSHIP_PERIPH2OWNER(x) ((x) & 0x7)
35
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030036#define SPMI_REG_CMD0 0x0
37#define SPMI_REG_CONFIG 0x4
38#define SPMI_REG_STATUS 0x8
39#define SPMI_REG_WDATA 0x10
40#define SPMI_REG_RDATA 0x18
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +020041
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030042#define SPMI_CMD_OPCODE_SHIFT 27
43#define SPMI_CMD_SLAVE_ID_SHIFT 20
44#define SPMI_CMD_ADDR_SHIFT 12
45#define SPMI_CMD_ADDR_OFFSET_SHIFT 4
46#define SPMI_CMD_BYTE_CNT_SHIFT 0
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +020047
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030048#define SPMI_CMD_EXT_REG_WRITE_LONG 0x00
49#define SPMI_CMD_EXT_REG_READ_LONG 0x01
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +020050
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030051#define SPMI_STATUS_DONE 0x1
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +020052
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030053#define SPMI_MAX_CHANNELS 128
Neil Armstrong3fe83672024-04-05 10:21:55 +020054#define SPMI_MAX_CHANNELS_V5 512
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030055#define SPMI_MAX_SLAVES 16
56#define SPMI_MAX_PERIPH 256
57
Neil Armstrong3fe83672024-04-05 10:21:55 +020058#define SPMI_CHANNEL_READ_ONLY BIT(31)
59#define SPMI_CHANNEL_MASK 0xffff
60
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030061enum arb_ver {
62 V1 = 1,
63 V2,
64 V3,
65 V5 = 5
66};
67
68/*
69 * PMIC arbiter version 5 uses different register offsets for read/write vs
70 * observer channels.
71 */
72enum pmic_arb_channel {
73 PMIC_ARB_CHANNEL_RW,
74 PMIC_ARB_CHANNEL_OBS,
75};
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +020076
77struct msm_spmi_priv {
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030078 phys_addr_t arb_chnl; /* ARB channel mapping base */
Caleb Connolly99f591c2023-12-05 13:46:53 +000079 phys_addr_t spmi_chnls; /* SPMI channels */
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030080 phys_addr_t spmi_obs; /* SPMI observer */
Neil Armstrong3fe83672024-04-05 10:21:55 +020081 phys_addr_t spmi_cnfg; /* SPMI config */
82 u32 owner; /* Current owner */
83 unsigned int max_channels; /* Max channels */
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +020084 /* SPMI channel map */
Neil Armstrong3fe83672024-04-05 10:21:55 +020085 uint32_t channel_map[SPMI_MAX_SLAVES][SPMI_MAX_PERIPH];
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030086 /* SPMI bus arbiter version */
87 u32 arb_ver;
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +020088};
89
Neil Armstrongdde6d552024-04-05 10:21:54 +020090static u32 pmic_arb_fmt_cmd_v1(u8 opc, u8 sid, u8 pid, u8 off)
91{
92 return (opc << 27) | (sid << 20) | (pid << 12) | (off << 4) | 1;
93}
94
95static u32 pmic_arb_fmt_cmd_v2(u8 opc, u8 off)
96{
97 return (opc << 27) | (off << 4) | 1;
98}
99
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200100static int msm_spmi_write(struct udevice *dev, int usid, int pid, int off,
101 uint8_t val)
102{
103 struct msm_spmi_priv *priv = dev_get_priv(dev);
104 unsigned channel;
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300105 unsigned int ch_offset;
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200106 uint32_t reg = 0;
107
108 if (usid >= SPMI_MAX_SLAVES)
109 return -EIO;
110 if (pid >= SPMI_MAX_PERIPH)
111 return -EIO;
Neil Armstrong3fe83672024-04-05 10:21:55 +0200112 if (priv->channel_map[usid][pid] & SPMI_CHANNEL_READ_ONLY)
113 return -EPERM;
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200114
Neil Armstrong3fe83672024-04-05 10:21:55 +0200115 channel = priv->channel_map[usid][pid] & SPMI_CHANNEL_MASK;
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200116
Neil Armstrongdde6d552024-04-05 10:21:54 +0200117 dev_dbg(dev, "[%d:%d] %s: channel %d\n", usid, pid, __func__, channel);
118
119 switch (priv->arb_ver) {
120 case V1:
121 ch_offset = SPMI_CH_OFFSET(channel);
122
123 reg = pmic_arb_fmt_cmd_v1(SPMI_CMD_EXT_REG_WRITE_LONG,
124 usid, pid, off);
125 break;
126
127 case V2:
Neil Armstrong62f34392024-04-05 10:21:53 +0200128 ch_offset = SPMI_CH_OFFSET(channel);
129
Neil Armstrongdde6d552024-04-05 10:21:54 +0200130 reg = pmic_arb_fmt_cmd_v2(SPMI_CMD_EXT_REG_WRITE_LONG, off);
131 break;
132
133 case V5:
134 ch_offset = SPMI_V5_RW_CH_OFFSET(channel);
135
136 reg = pmic_arb_fmt_cmd_v2(SPMI_CMD_EXT_REG_WRITE_LONG, off);
137 break;
138 }
139
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200140 /* Disable IRQ mode for the current channel*/
Neil Armstrong62f34392024-04-05 10:21:53 +0200141 writel(0x0, priv->spmi_chnls + ch_offset + SPMI_REG_CONFIG);
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200142
143 /* Write single byte */
Neil Armstrong62f34392024-04-05 10:21:53 +0200144 writel(val, priv->spmi_chnls + ch_offset + SPMI_REG_WDATA);
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200145
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200146 /* Send write command */
Neil Armstrong62f34392024-04-05 10:21:53 +0200147 writel(reg, priv->spmi_chnls + ch_offset + SPMI_REG_CMD0);
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200148
149 /* Wait till CMD DONE status */
150 reg = 0;
151 while (!reg) {
Neil Armstrong62f34392024-04-05 10:21:53 +0200152 reg = readl(priv->spmi_chnls + ch_offset +
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200153 SPMI_REG_STATUS);
154 }
155
156 if (reg ^ SPMI_STATUS_DONE) {
157 printf("SPMI write failure.\n");
158 return -EIO;
159 }
160
161 return 0;
162}
163
164static int msm_spmi_read(struct udevice *dev, int usid, int pid, int off)
165{
166 struct msm_spmi_priv *priv = dev_get_priv(dev);
167 unsigned channel;
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300168 unsigned int ch_offset;
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200169 uint32_t reg = 0;
170
171 if (usid >= SPMI_MAX_SLAVES)
172 return -EIO;
173 if (pid >= SPMI_MAX_PERIPH)
174 return -EIO;
175
Neil Armstrong3fe83672024-04-05 10:21:55 +0200176 channel = priv->channel_map[usid][pid] & SPMI_CHANNEL_MASK;
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200177
Neil Armstrongdde6d552024-04-05 10:21:54 +0200178 dev_dbg(dev, "[%d:%d] %s: channel %d\n", usid, pid, __func__, channel);
179
180 switch (priv->arb_ver) {
181 case V1:
182 ch_offset = SPMI_CH_OFFSET(channel);
183
184 /* Prepare read command */
185 reg = pmic_arb_fmt_cmd_v1(SPMI_CMD_EXT_REG_READ_LONG,
186 usid, pid, off);
187 break;
188
189 case V2:
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300190 ch_offset = SPMI_CH_OFFSET(channel);
191
Neil Armstrongdde6d552024-04-05 10:21:54 +0200192 /* Prepare read command */
193 reg = pmic_arb_fmt_cmd_v2(SPMI_CMD_EXT_REG_READ_LONG, off);
194 break;
195
196 case V5:
197 ch_offset = SPMI_V5_OBS_CH_OFFSET(channel);
198
199 /* Prepare read command */
200 reg = pmic_arb_fmt_cmd_v2(SPMI_CMD_EXT_REG_READ_LONG, off);
201 break;
202 }
203
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200204 /* Disable IRQ mode for the current channel*/
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300205 writel(0x0, priv->spmi_obs + ch_offset + SPMI_REG_CONFIG);
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200206
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200207 /* Request read */
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300208 writel(reg, priv->spmi_obs + ch_offset + SPMI_REG_CMD0);
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200209
210 /* Wait till CMD DONE status */
211 reg = 0;
212 while (!reg) {
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300213 reg = readl(priv->spmi_obs + ch_offset + SPMI_REG_STATUS);
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200214 }
215
216 if (reg ^ SPMI_STATUS_DONE) {
217 printf("SPMI read failure.\n");
218 return -EIO;
219 }
220
221 /* Read the data */
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300222 return readl(priv->spmi_obs + ch_offset +
223 SPMI_REG_RDATA) & 0xFF;
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200224}
225
226static struct dm_spmi_ops msm_spmi_ops = {
227 .read = msm_spmi_read,
228 .write = msm_spmi_write,
229};
230
231static int msm_spmi_probe(struct udevice *dev)
232{
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200233 struct msm_spmi_priv *priv = dev_get_priv(dev);
Caleb Connolly99f591c2023-12-05 13:46:53 +0000234 phys_addr_t core_addr;
Jorge Ramirez-Ortiz4bcef682018-01-10 11:33:28 +0100235 u32 hw_ver;
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200236 int i;
237
Caleb Connolly99f591c2023-12-05 13:46:53 +0000238 core_addr = dev_read_addr_name(dev, "core");
239 priv->spmi_chnls = dev_read_addr_name(dev, "chnls");
240 priv->spmi_obs = dev_read_addr_name(dev, "obsrvr");
Neil Armstrong3fe83672024-04-05 10:21:55 +0200241 dev_read_u32(dev, "qcom,ee", &priv->owner);
Jorge Ramirez-Ortiz4bcef682018-01-10 11:33:28 +0100242
Caleb Connolly99f591c2023-12-05 13:46:53 +0000243 hw_ver = readl(core_addr + PMIC_ARB_VERSION);
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300244
245 if (hw_ver < PMIC_ARB_VERSION_V3_MIN) {
246 priv->arb_ver = V2;
Caleb Connolly99f591c2023-12-05 13:46:53 +0000247 priv->arb_chnl = core_addr + APID_MAP_OFFSET_V1_V2_V3;
Neil Armstrong3fe83672024-04-05 10:21:55 +0200248 priv->max_channels = SPMI_MAX_CHANNELS;
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300249 } else if (hw_ver < PMIC_ARB_VERSION_V5_MIN) {
250 priv->arb_ver = V3;
Caleb Connolly99f591c2023-12-05 13:46:53 +0000251 priv->arb_chnl = core_addr + APID_MAP_OFFSET_V1_V2_V3;
Neil Armstrong3fe83672024-04-05 10:21:55 +0200252 priv->max_channels = SPMI_MAX_CHANNELS;
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300253 } else {
254 priv->arb_ver = V5;
Caleb Connolly99f591c2023-12-05 13:46:53 +0000255 priv->arb_chnl = core_addr + APID_MAP_OFFSET_V5;
Neil Armstrong3fe83672024-04-05 10:21:55 +0200256 priv->max_channels = SPMI_MAX_CHANNELS_V5;
257 priv->spmi_cnfg = dev_read_addr_name(dev, "cnfg");
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300258 }
259
Caleb Connolly99f591c2023-12-05 13:46:53 +0000260 dev_dbg(dev, "PMIC Arb Version-%d (%#x)\n", hw_ver >> 28, hw_ver);
Jorge Ramirez-Ortiz4bcef682018-01-10 11:33:28 +0100261
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200262 if (priv->arb_chnl == FDT_ADDR_T_NONE ||
Caleb Connolly99f591c2023-12-05 13:46:53 +0000263 priv->spmi_chnls == FDT_ADDR_T_NONE ||
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200264 priv->spmi_obs == FDT_ADDR_T_NONE)
265 return -EINVAL;
266
Caleb Connolly99f591c2023-12-05 13:46:53 +0000267 dev_dbg(dev, "priv->arb_chnl address (%#08llx)\n", priv->arb_chnl);
268 dev_dbg(dev, "priv->spmi_chnls address (%#08llx)\n", priv->spmi_chnls);
269 dev_dbg(dev, "priv->spmi_obs address (%#08llx)\n", priv->spmi_obs);
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200270 /* Scan peripherals connected to each SPMI channel */
Neil Armstrong3fe83672024-04-05 10:21:55 +0200271 for (i = 0; i < priv->max_channels; i++) {
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200272 uint32_t periph = readl(priv->arb_chnl + ARB_CHANNEL_OFFSET(i));
273 uint8_t slave_id = (periph & 0xf0000) >> 16;
274 uint8_t pid = (periph & 0xff00) >> 8;
275
276 priv->channel_map[slave_id][pid] = i;
Neil Armstrong3fe83672024-04-05 10:21:55 +0200277
278 /* Mark channels read-only when from different owner */
279 if (priv->arb_ver == V5) {
280 uint32_t cnfg = readl(priv->spmi_cnfg + ARB_CHANNEL_OFFSET(i));
281 uint8_t owner = SPMI_OWNERSHIP_PERIPH2OWNER(cnfg);
282
283 if (owner != priv->owner)
284 priv->channel_map[slave_id][pid] |= SPMI_CHANNEL_READ_ONLY;
285 }
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200286 }
287 return 0;
288}
289
290static const struct udevice_id msm_spmi_ids[] = {
291 { .compatible = "qcom,spmi-pmic-arb" },
292 { }
293};
294
295U_BOOT_DRIVER(msm_spmi) = {
296 .name = "msm_spmi",
297 .id = UCLASS_SPMI,
298 .of_match = msm_spmi_ids,
299 .ops = &msm_spmi_ops,
300 .probe = msm_spmi_probe,
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300301 .priv_auto = sizeof(struct msm_spmi_priv),
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200302};