blob: 97383d8c7b8e43273a7546af5094f4770653c051 [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
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030034#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 Kulikowski73bec9c2016-03-31 23:12:29 +020039
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030040#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 Kulikowski73bec9c2016-03-31 23:12:29 +020045
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030046#define SPMI_CMD_EXT_REG_WRITE_LONG 0x00
47#define SPMI_CMD_EXT_REG_READ_LONG 0x01
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +020048
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030049#define SPMI_STATUS_DONE 0x1
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +020050
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030051#define SPMI_MAX_CHANNELS 128
52#define SPMI_MAX_SLAVES 16
53#define SPMI_MAX_PERIPH 256
54
55enum 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 */
66enum pmic_arb_channel {
67 PMIC_ARB_CHANNEL_RW,
68 PMIC_ARB_CHANNEL_OBS,
69};
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +020070
71struct msm_spmi_priv {
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030072 phys_addr_t arb_chnl; /* ARB channel mapping base */
Caleb Connolly99f591c2023-12-05 13:46:53 +000073 phys_addr_t spmi_chnls; /* SPMI channels */
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030074 phys_addr_t spmi_obs; /* SPMI observer */
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +020075 /* SPMI channel map */
76 uint8_t channel_map[SPMI_MAX_SLAVES][SPMI_MAX_PERIPH];
Dzmitry Sankouski682351a2021-10-17 13:44:28 +030077 /* SPMI bus arbiter version */
78 u32 arb_ver;
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +020079};
80
81static 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 Sankouski682351a2021-10-17 13:44:28 +030086 unsigned int ch_offset;
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +020087 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
Neil Armstrong62f34392024-04-05 10:21:53 +020096 if (priv->arb_ver == V5)
97 ch_offset = SPMI_V5_RW_CH_OFFSET(channel);
98 else
99 ch_offset = SPMI_CH_OFFSET(channel);
100
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200101 /* Disable IRQ mode for the current channel*/
Neil Armstrong62f34392024-04-05 10:21:53 +0200102 writel(0x0, priv->spmi_chnls + ch_offset + SPMI_REG_CONFIG);
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200103
104 /* Write single byte */
Neil Armstrong62f34392024-04-05 10:21:53 +0200105 writel(val, priv->spmi_chnls + ch_offset + SPMI_REG_WDATA);
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200106
107 /* Prepare write command */
108 reg |= SPMI_CMD_EXT_REG_WRITE_LONG << SPMI_CMD_OPCODE_SHIFT;
109 reg |= (usid << SPMI_CMD_SLAVE_ID_SHIFT);
110 reg |= (pid << SPMI_CMD_ADDR_SHIFT);
111 reg |= (off << SPMI_CMD_ADDR_OFFSET_SHIFT);
112 reg |= 1; /* byte count */
113
114 /* Send write command */
Neil Armstrong62f34392024-04-05 10:21:53 +0200115 writel(reg, priv->spmi_chnls + ch_offset + SPMI_REG_CMD0);
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200116
117 /* Wait till CMD DONE status */
118 reg = 0;
119 while (!reg) {
Neil Armstrong62f34392024-04-05 10:21:53 +0200120 reg = readl(priv->spmi_chnls + ch_offset +
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200121 SPMI_REG_STATUS);
122 }
123
124 if (reg ^ SPMI_STATUS_DONE) {
125 printf("SPMI write failure.\n");
126 return -EIO;
127 }
128
129 return 0;
130}
131
132static int msm_spmi_read(struct udevice *dev, int usid, int pid, int off)
133{
134 struct msm_spmi_priv *priv = dev_get_priv(dev);
135 unsigned channel;
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300136 unsigned int ch_offset;
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200137 uint32_t reg = 0;
138
139 if (usid >= SPMI_MAX_SLAVES)
140 return -EIO;
141 if (pid >= SPMI_MAX_PERIPH)
142 return -EIO;
143
144 channel = priv->channel_map[usid][pid];
145
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300146 if (priv->arb_ver == V5)
147 ch_offset = SPMI_V5_OBS_CH_OFFSET(channel);
148 else
149 ch_offset = SPMI_CH_OFFSET(channel);
150
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200151 /* Disable IRQ mode for the current channel*/
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300152 writel(0x0, priv->spmi_obs + ch_offset + SPMI_REG_CONFIG);
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200153
154 /* Prepare read command */
155 reg |= SPMI_CMD_EXT_REG_READ_LONG << SPMI_CMD_OPCODE_SHIFT;
156 reg |= (usid << SPMI_CMD_SLAVE_ID_SHIFT);
157 reg |= (pid << SPMI_CMD_ADDR_SHIFT);
158 reg |= (off << SPMI_CMD_ADDR_OFFSET_SHIFT);
159 reg |= 1; /* byte count */
160
161 /* Request read */
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300162 writel(reg, priv->spmi_obs + ch_offset + SPMI_REG_CMD0);
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200163
164 /* Wait till CMD DONE status */
165 reg = 0;
166 while (!reg) {
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300167 reg = readl(priv->spmi_obs + ch_offset + SPMI_REG_STATUS);
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200168 }
169
170 if (reg ^ SPMI_STATUS_DONE) {
171 printf("SPMI read failure.\n");
172 return -EIO;
173 }
174
175 /* Read the data */
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300176 return readl(priv->spmi_obs + ch_offset +
177 SPMI_REG_RDATA) & 0xFF;
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200178}
179
180static struct dm_spmi_ops msm_spmi_ops = {
181 .read = msm_spmi_read,
182 .write = msm_spmi_write,
183};
184
185static int msm_spmi_probe(struct udevice *dev)
186{
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200187 struct msm_spmi_priv *priv = dev_get_priv(dev);
Caleb Connolly99f591c2023-12-05 13:46:53 +0000188 phys_addr_t core_addr;
Jorge Ramirez-Ortiz4bcef682018-01-10 11:33:28 +0100189 u32 hw_ver;
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200190 int i;
191
Caleb Connolly99f591c2023-12-05 13:46:53 +0000192 core_addr = dev_read_addr_name(dev, "core");
193 priv->spmi_chnls = dev_read_addr_name(dev, "chnls");
194 priv->spmi_obs = dev_read_addr_name(dev, "obsrvr");
Jorge Ramirez-Ortiz4bcef682018-01-10 11:33:28 +0100195
Caleb Connolly99f591c2023-12-05 13:46:53 +0000196 hw_ver = readl(core_addr + PMIC_ARB_VERSION);
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300197
198 if (hw_ver < PMIC_ARB_VERSION_V3_MIN) {
199 priv->arb_ver = V2;
Caleb Connolly99f591c2023-12-05 13:46:53 +0000200 priv->arb_chnl = core_addr + APID_MAP_OFFSET_V1_V2_V3;
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300201 } else if (hw_ver < PMIC_ARB_VERSION_V5_MIN) {
202 priv->arb_ver = V3;
Caleb Connolly99f591c2023-12-05 13:46:53 +0000203 priv->arb_chnl = core_addr + APID_MAP_OFFSET_V1_V2_V3;
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300204 } else {
205 priv->arb_ver = V5;
Caleb Connolly99f591c2023-12-05 13:46:53 +0000206 priv->arb_chnl = core_addr + APID_MAP_OFFSET_V5;
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300207 }
208
Caleb Connolly99f591c2023-12-05 13:46:53 +0000209 dev_dbg(dev, "PMIC Arb Version-%d (%#x)\n", hw_ver >> 28, hw_ver);
Jorge Ramirez-Ortiz4bcef682018-01-10 11:33:28 +0100210
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200211 if (priv->arb_chnl == FDT_ADDR_T_NONE ||
Caleb Connolly99f591c2023-12-05 13:46:53 +0000212 priv->spmi_chnls == FDT_ADDR_T_NONE ||
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200213 priv->spmi_obs == FDT_ADDR_T_NONE)
214 return -EINVAL;
215
Caleb Connolly99f591c2023-12-05 13:46:53 +0000216 dev_dbg(dev, "priv->arb_chnl address (%#08llx)\n", priv->arb_chnl);
217 dev_dbg(dev, "priv->spmi_chnls address (%#08llx)\n", priv->spmi_chnls);
218 dev_dbg(dev, "priv->spmi_obs address (%#08llx)\n", priv->spmi_obs);
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200219 /* Scan peripherals connected to each SPMI channel */
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300220 for (i = 0; i < SPMI_MAX_PERIPH; i++) {
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200221 uint32_t periph = readl(priv->arb_chnl + ARB_CHANNEL_OFFSET(i));
222 uint8_t slave_id = (periph & 0xf0000) >> 16;
223 uint8_t pid = (periph & 0xff00) >> 8;
224
225 priv->channel_map[slave_id][pid] = i;
226 }
227 return 0;
228}
229
230static const struct udevice_id msm_spmi_ids[] = {
231 { .compatible = "qcom,spmi-pmic-arb" },
232 { }
233};
234
235U_BOOT_DRIVER(msm_spmi) = {
236 .name = "msm_spmi",
237 .id = UCLASS_SPMI,
238 .of_match = msm_spmi_ids,
239 .ops = &msm_spmi_ops,
240 .probe = msm_spmi_probe,
Dzmitry Sankouski682351a2021-10-17 13:44:28 +0300241 .priv_auto = sizeof(struct msm_spmi_priv),
Mateusz Kulikowski73bec9c2016-03-31 23:12:29 +0200242};