blob: 5a335e50aa642f9be4b96e19249ea660c3db3109 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: BSD-3-Clause
Mateusz Kulikowski5b472712016-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 Kulikowski5b472712016-03-31 23:12:29 +02008 */
9
10#include <common.h>
11#include <dm.h>
12#include <errno.h>
13#include <fdtdec.h>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
Mateusz Kulikowski5b472712016-03-31 23:12:29 +020015#include <asm/io.h>
Simon Glass336d4612020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Mateusz Kulikowski5b472712016-03-31 23:12:29 +020017#include <spmi/spmi.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
Jorge Ramirez-Ortiz210d9592018-01-10 11:33:28 +010021/* PMIC Arbiter configuration registers */
22#define PMIC_ARB_VERSION 0x0000
23#define PMIC_ARB_VERSION_V2_MIN 0x20010000
24
Mateusz Kulikowski5b472712016-03-31 23:12:29 +020025#define ARB_CHANNEL_OFFSET(n) (0x4 * (n))
26#define SPMI_CH_OFFSET(chnl) ((chnl) * 0x8000)
27
28#define SPMI_REG_CMD0 0x0
29#define SPMI_REG_CONFIG 0x4
30#define SPMI_REG_STATUS 0x8
31#define SPMI_REG_WDATA 0x10
32#define SPMI_REG_RDATA 0x18
33
34#define SPMI_CMD_OPCODE_SHIFT 27
35#define SPMI_CMD_SLAVE_ID_SHIFT 20
36#define SPMI_CMD_ADDR_SHIFT 12
37#define SPMI_CMD_ADDR_OFFSET_SHIFT 4
38#define SPMI_CMD_BYTE_CNT_SHIFT 0
39
40#define SPMI_CMD_EXT_REG_WRITE_LONG 0x00
41#define SPMI_CMD_EXT_REG_READ_LONG 0x01
42
43#define SPMI_STATUS_DONE 0x1
44
45#define SPMI_MAX_CHANNELS 128
46#define SPMI_MAX_SLAVES 16
47#define SPMI_MAX_PERIPH 256
48
49struct msm_spmi_priv {
50 phys_addr_t arb_chnl; /* ARB channel mapping base */
51 phys_addr_t spmi_core; /* SPMI core */
52 phys_addr_t spmi_obs; /* SPMI observer */
53 /* SPMI channel map */
54 uint8_t channel_map[SPMI_MAX_SLAVES][SPMI_MAX_PERIPH];
55};
56
57static int msm_spmi_write(struct udevice *dev, int usid, int pid, int off,
58 uint8_t val)
59{
60 struct msm_spmi_priv *priv = dev_get_priv(dev);
61 unsigned channel;
62 uint32_t reg = 0;
63
64 if (usid >= SPMI_MAX_SLAVES)
65 return -EIO;
66 if (pid >= SPMI_MAX_PERIPH)
67 return -EIO;
68
69 channel = priv->channel_map[usid][pid];
70
71 /* Disable IRQ mode for the current channel*/
72 writel(0x0, priv->spmi_core + SPMI_CH_OFFSET(channel) +
73 SPMI_REG_CONFIG);
74
75 /* Write single byte */
76 writel(val, priv->spmi_core + SPMI_CH_OFFSET(channel) + SPMI_REG_WDATA);
77
78 /* Prepare write command */
79 reg |= SPMI_CMD_EXT_REG_WRITE_LONG << SPMI_CMD_OPCODE_SHIFT;
80 reg |= (usid << SPMI_CMD_SLAVE_ID_SHIFT);
81 reg |= (pid << SPMI_CMD_ADDR_SHIFT);
82 reg |= (off << SPMI_CMD_ADDR_OFFSET_SHIFT);
83 reg |= 1; /* byte count */
84
85 /* Send write command */
86 writel(reg, priv->spmi_core + SPMI_CH_OFFSET(channel) + SPMI_REG_CMD0);
87
88 /* Wait till CMD DONE status */
89 reg = 0;
90 while (!reg) {
91 reg = readl(priv->spmi_core + SPMI_CH_OFFSET(channel) +
92 SPMI_REG_STATUS);
93 }
94
95 if (reg ^ SPMI_STATUS_DONE) {
96 printf("SPMI write failure.\n");
97 return -EIO;
98 }
99
100 return 0;
101}
102
103static int msm_spmi_read(struct udevice *dev, int usid, int pid, int off)
104{
105 struct msm_spmi_priv *priv = dev_get_priv(dev);
106 unsigned channel;
107 uint32_t reg = 0;
108
109 if (usid >= SPMI_MAX_SLAVES)
110 return -EIO;
111 if (pid >= SPMI_MAX_PERIPH)
112 return -EIO;
113
114 channel = priv->channel_map[usid][pid];
115
116 /* Disable IRQ mode for the current channel*/
117 writel(0x0, priv->spmi_obs + SPMI_CH_OFFSET(channel) + SPMI_REG_CONFIG);
118
119 /* Prepare read command */
120 reg |= SPMI_CMD_EXT_REG_READ_LONG << SPMI_CMD_OPCODE_SHIFT;
121 reg |= (usid << SPMI_CMD_SLAVE_ID_SHIFT);
122 reg |= (pid << SPMI_CMD_ADDR_SHIFT);
123 reg |= (off << SPMI_CMD_ADDR_OFFSET_SHIFT);
124 reg |= 1; /* byte count */
125
126 /* Request read */
127 writel(reg, priv->spmi_obs + SPMI_CH_OFFSET(channel) + SPMI_REG_CMD0);
128
129 /* Wait till CMD DONE status */
130 reg = 0;
131 while (!reg) {
132 reg = readl(priv->spmi_obs + SPMI_CH_OFFSET(channel) +
133 SPMI_REG_STATUS);
134 }
135
136 if (reg ^ SPMI_STATUS_DONE) {
137 printf("SPMI read failure.\n");
138 return -EIO;
139 }
140
141 /* Read the data */
142 return readl(priv->spmi_obs + SPMI_CH_OFFSET(channel) +
143 SPMI_REG_RDATA) & 0xFF;
144}
145
146static struct dm_spmi_ops msm_spmi_ops = {
147 .read = msm_spmi_read,
148 .write = msm_spmi_write,
149};
150
151static int msm_spmi_probe(struct udevice *dev)
152{
153 struct udevice *parent = dev->parent;
154 struct msm_spmi_priv *priv = dev_get_priv(dev);
Simon Glasse160f7d2017-01-17 16:52:55 -0700155 int node = dev_of_offset(dev);
Jorge Ramirez-Ortiz210d9592018-01-10 11:33:28 +0100156 u32 hw_ver;
157 bool is_v1;
Mateusz Kulikowski5b472712016-03-31 23:12:29 +0200158 int i;
159
Masahiro Yamada25484932020-07-17 14:36:48 +0900160 priv->arb_chnl = dev_read_addr(dev);
Mateusz Kulikowski5b472712016-03-31 23:12:29 +0200161 priv->spmi_core = fdtdec_get_addr_size_auto_parent(gd->fdt_blob,
Simon Glasse160f7d2017-01-17 16:52:55 -0700162 dev_of_offset(parent), node, "reg", 1, NULL, false);
Mateusz Kulikowski5b472712016-03-31 23:12:29 +0200163 priv->spmi_obs = fdtdec_get_addr_size_auto_parent(gd->fdt_blob,
Simon Glasse160f7d2017-01-17 16:52:55 -0700164 dev_of_offset(parent), node, "reg", 2, NULL, false);
Jorge Ramirez-Ortiz210d9592018-01-10 11:33:28 +0100165
166 hw_ver = readl(priv->arb_chnl + PMIC_ARB_VERSION - 0x800);
167 is_v1 = (hw_ver < PMIC_ARB_VERSION_V2_MIN);
168
169 dev_dbg(dev, "PMIC Arb Version-%d (0x%x)\n", (is_v1 ? 1 : 2), hw_ver);
170
Mateusz Kulikowski5b472712016-03-31 23:12:29 +0200171 if (priv->arb_chnl == FDT_ADDR_T_NONE ||
172 priv->spmi_core == FDT_ADDR_T_NONE ||
173 priv->spmi_obs == FDT_ADDR_T_NONE)
174 return -EINVAL;
175
176 /* Scan peripherals connected to each SPMI channel */
Jorge Ramirez-Ortizbd3006c2017-12-02 17:24:42 +0100177 for (i = 0; i < SPMI_MAX_PERIPH ; i++) {
Mateusz Kulikowski5b472712016-03-31 23:12:29 +0200178 uint32_t periph = readl(priv->arb_chnl + ARB_CHANNEL_OFFSET(i));
179 uint8_t slave_id = (periph & 0xf0000) >> 16;
180 uint8_t pid = (periph & 0xff00) >> 8;
181
182 priv->channel_map[slave_id][pid] = i;
183 }
184 return 0;
185}
186
187static const struct udevice_id msm_spmi_ids[] = {
188 { .compatible = "qcom,spmi-pmic-arb" },
189 { }
190};
191
192U_BOOT_DRIVER(msm_spmi) = {
193 .name = "msm_spmi",
194 .id = UCLASS_SPMI,
195 .of_match = msm_spmi_ids,
196 .ops = &msm_spmi_ops,
197 .probe = msm_spmi_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700198 .priv_auto = sizeof(struct msm_spmi_priv),
Mateusz Kulikowski5b472712016-03-31 23:12:29 +0200199};