78679702b20bd57324e6e9aba75737ddb2bad296
[openwrt/staging/ansuel.git] /
1 From 4bdb576667a976996f870d36c5bb841b9d0c1258 Mon Sep 17 00:00:00 2001
2 From: Jonathan Bell <jonathan@raspberrypi.com>
3 Date: Tue, 26 Mar 2024 13:25:01 +0000
4 Subject: [PATCH 0994/1085] drivers: mmc: export SD extension register
5 read/write functions
6
7 Certain status bits in these registers may need polling outside of
8 SD-specific code. Export in sd_ops.h
9
10 Signed-off-by: Jonathan Bell <jonathan@raspberrypi.com>
11 ---
12 drivers/mmc/core/sd.c | 97 ++++-----------------------------------
13 drivers/mmc/core/sd_ops.c | 83 +++++++++++++++++++++++++++++++--
14 drivers/mmc/core/sd_ops.h | 4 ++
15 3 files changed, 93 insertions(+), 91 deletions(-)
16
17 --- a/drivers/mmc/core/sd.c
18 +++ b/drivers/mmc/core/sd.c
19 @@ -1015,83 +1015,6 @@ static bool mmc_sd_card_using_v18(struct
20 (SD_MODE_UHS_SDR50 | SD_MODE_UHS_SDR104 | SD_MODE_UHS_DDR50);
21 }
22
23 -int sd_write_ext_reg(struct mmc_card *card, u8 fno, u8 page, u16 offset,
24 - u8 reg_data)
25 -{
26 - struct mmc_host *host = card->host;
27 - struct mmc_request mrq = {};
28 - struct mmc_command cmd = {};
29 - struct mmc_data data = {};
30 - struct scatterlist sg;
31 - u8 *reg_buf;
32 -
33 - reg_buf = card->ext_reg_buf;
34 - memset(reg_buf, 0, 512);
35 -
36 - mrq.cmd = &cmd;
37 - mrq.data = &data;
38 -
39 - /*
40 - * Arguments of CMD49:
41 - * [31:31] MIO (0 = memory).
42 - * [30:27] FNO (function number).
43 - * [26:26] MW - mask write mode (0 = disable).
44 - * [25:18] page number.
45 - * [17:9] offset address.
46 - * [8:0] length (0 = 1 byte).
47 - */
48 - cmd.arg = fno << 27 | page << 18 | offset << 9;
49 -
50 - /* The first byte in the buffer is the data to be written. */
51 - reg_buf[0] = reg_data;
52 -
53 - data.flags = MMC_DATA_WRITE;
54 - data.blksz = 512;
55 - data.blocks = 1;
56 - data.sg = &sg;
57 - data.sg_len = 1;
58 - sg_init_one(&sg, reg_buf, 512);
59 -
60 - cmd.opcode = SD_WRITE_EXTR_SINGLE;
61 - cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
62 -
63 - mmc_set_data_timeout(&data, card);
64 - mmc_wait_for_req(host, &mrq);
65 -
66 - /*
67 - * Note that, the SD card is allowed to signal busy on DAT0 up to 1s
68 - * after the CMD49. Although, let's leave this to be managed by the
69 - * caller.
70 - */
71 -
72 - if (cmd.error)
73 - return cmd.error;
74 - if (data.error)
75 - return data.error;
76 -
77 - return 0;
78 -}
79 -
80 -static int sd_read_ext_reg(struct mmc_card *card, u8 fno, u8 page,
81 - u16 offset, u16 len, u8 *reg_buf)
82 -{
83 - u32 cmd_args;
84 -
85 - /*
86 - * Command arguments of CMD48:
87 - * [31:31] MIO (0 = memory).
88 - * [30:27] FNO (function number).
89 - * [26:26] reserved (0).
90 - * [25:18] page number.
91 - * [17:9] offset address.
92 - * [8:0] length (0 = 1 byte, 1ff = 512 bytes).
93 - */
94 - cmd_args = fno << 27 | page << 18 | offset << 9 | (len -1);
95 -
96 - return mmc_send_adtc_data(card, card->host, SD_READ_EXTR_SINGLE,
97 - cmd_args, reg_buf, 512);
98 -}
99 -
100 static int sd_parse_ext_reg_power(struct mmc_card *card, u8 fno, u8 page,
101 u16 offset)
102 {
103 @@ -1101,7 +1024,7 @@ static int sd_parse_ext_reg_power(struct
104 reg_buf = card->ext_reg_buf;
105
106 /* Read the extension register for power management function. */
107 - err = sd_read_ext_reg(card, fno, page, offset, 512, reg_buf);
108 + err = mmc_sd_read_ext_reg(card, fno, page, offset, 512, reg_buf);
109 if (err) {
110 pr_warn("%s: error %d reading PM func of ext reg\n",
111 mmc_hostname(card->host), err);
112 @@ -1139,7 +1062,7 @@ static int sd_parse_ext_reg_perf(struct
113
114 reg_buf = card->ext_reg_buf;
115
116 - err = sd_read_ext_reg(card, fno, page, offset, 512, reg_buf);
117 + err = mmc_sd_read_ext_reg(card, fno, page, offset, 512, reg_buf);
118 if (err) {
119 pr_warn("%s: error %d reading PERF func of ext reg\n",
120 mmc_hostname(card->host), err);
121 @@ -1234,7 +1157,7 @@ static int sd_parse_ext_reg(struct mmc_c
122 return 0;
123 }
124
125 -static int sd_read_ext_regs(struct mmc_card *card)
126 +static int mmc_sd_read_ext_regs(struct mmc_card *card)
127 {
128 int err, i;
129 u8 num_ext, *gen_info_buf;
130 @@ -1260,7 +1183,7 @@ static int sd_read_ext_regs(struct mmc_c
131 * Read 512 bytes of general info, which is found at function number 0,
132 * at page 0 and with no offset.
133 */
134 - err = sd_read_ext_reg(card, 0, 0, 0, 512, gen_info_buf);
135 + err = mmc_sd_read_ext_reg(card, 0, 0, 0, 512, gen_info_buf);
136 if (err) {
137 pr_err("%s: error %d reading general info of SD ext reg\n",
138 mmc_hostname(card->host), err);
139 @@ -1332,7 +1255,7 @@ static int sd_flush_cache(struct mmc_hos
140 page = card->ext_perf.page;
141 offset = card->ext_perf.offset + 261;
142
143 - err = sd_write_ext_reg(card, fno, page, offset, BIT(0));
144 + err = mmc_sd_write_ext_reg(card, fno, page, offset, BIT(0));
145 if (err) {
146 pr_warn("%s: error %d writing Cache Flush bit\n",
147 mmc_hostname(host), err);
148 @@ -1348,7 +1271,7 @@ static int sd_flush_cache(struct mmc_hos
149 * Read the Flush Cache bit. The card shall reset it, to confirm that
150 * it's has completed the flushing of the cache.
151 */
152 - err = sd_read_ext_reg(card, fno, page, offset, 1, reg_buf);
153 + err = mmc_sd_read_ext_reg(card, fno, page, offset, 1, reg_buf);
154 if (err) {
155 pr_warn("%s: error %d reading Cache Flush bit\n",
156 mmc_hostname(host), err);
157 @@ -1371,7 +1294,7 @@ static int sd_enable_cache(struct mmc_ca
158 * Set Cache Enable at bit 0 in the performance enhancement register at
159 * 260 bytes offset.
160 */
161 - err = sd_write_ext_reg(card, card->ext_perf.fno, card->ext_perf.page,
162 + err = mmc_sd_write_ext_reg(card, card->ext_perf.fno, card->ext_perf.page,
163 card->ext_perf.offset + 260, BIT(0));
164 if (err) {
165 pr_warn("%s: error %d writing Cache Enable bit\n",
166 @@ -1541,7 +1464,7 @@ retry:
167 cont:
168 if (!oldcard) {
169 /* Read/parse the extension registers. */
170 - err = sd_read_ext_regs(card);
171 + err = mmc_sd_read_ext_regs(card);
172 if (err)
173 goto free_card;
174 }
175 @@ -1668,7 +1591,7 @@ static int sd_busy_poweroff_notify_cb(vo
176 * one byte offset and is one byte long. The Power Off Notification
177 * Ready is bit 0.
178 */
179 - err = sd_read_ext_reg(card, card->ext_power.fno, card->ext_power.page,
180 + err = mmc_sd_read_ext_reg(card, card->ext_power.fno, card->ext_power.page,
181 card->ext_power.offset + 1, 1, data->reg_buf);
182 if (err) {
183 pr_warn("%s: error %d reading status reg of PM func\n",
184 @@ -1694,7 +1617,7 @@ static int sd_poweroff_notify(struct mmc
185 * Set the Power Off Notification bit in the power management settings
186 * register at 2 bytes offset.
187 */
188 - err = sd_write_ext_reg(card, card->ext_power.fno, card->ext_power.page,
189 + err = mmc_sd_write_ext_reg(card, card->ext_power.fno, card->ext_power.page,
190 card->ext_power.offset + 2, BIT(0));
191 if (err) {
192 pr_warn("%s: error %d writing Power Off Notify bit\n",
193 --- a/drivers/mmc/core/sd_ops.c
194 +++ b/drivers/mmc/core/sd_ops.c
195 @@ -366,8 +366,83 @@ int mmc_app_sd_status(struct mmc_card *c
196 return 0;
197 }
198
199 -int sd_write_ext_reg(struct mmc_card *card, u8 fno, u8 page, u16 offset,
200 - u8 reg_data);
201 +
202 +int mmc_sd_write_ext_reg(struct mmc_card *card, u8 fno, u8 page, u16 offset,
203 + u8 reg_data)
204 +{
205 + struct mmc_host *host = card->host;
206 + struct mmc_request mrq = {};
207 + struct mmc_command cmd = {};
208 + struct mmc_data data = {};
209 + struct scatterlist sg;
210 + u8 *reg_buf;
211 +
212 + reg_buf = card->ext_reg_buf;
213 + memset(reg_buf, 0, 512);
214 +
215 + mrq.cmd = &cmd;
216 + mrq.data = &data;
217 +
218 + /*
219 + * Arguments of CMD49:
220 + * [31:31] MIO (0 = memory).
221 + * [30:27] FNO (function number).
222 + * [26:26] MW - mask write mode (0 = disable).
223 + * [25:18] page number.
224 + * [17:9] offset address.
225 + * [8:0] length (0 = 1 byte).
226 + */
227 + cmd.arg = fno << 27 | page << 18 | offset << 9;
228 +
229 + /* The first byte in the buffer is the data to be written. */
230 + reg_buf[0] = reg_data;
231 +
232 + data.flags = MMC_DATA_WRITE;
233 + data.blksz = 512;
234 + data.blocks = 1;
235 + data.sg = &sg;
236 + data.sg_len = 1;
237 + sg_init_one(&sg, reg_buf, 512);
238 +
239 + cmd.opcode = SD_WRITE_EXTR_SINGLE;
240 + cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
241 +
242 + mmc_set_data_timeout(&data, card);
243 + mmc_wait_for_req(host, &mrq);
244 +
245 + /*
246 + * Note that, the SD card is allowed to signal busy on DAT0 up to 1s
247 + * after the CMD49. Although, let's leave this to be managed by the
248 + * caller.
249 + */
250 +
251 + if (cmd.error)
252 + return cmd.error;
253 + if (data.error)
254 + return data.error;
255 +
256 + return 0;
257 +}
258 +
259 +int mmc_sd_read_ext_reg(struct mmc_card *card, u8 fno, u8 page,
260 + u16 offset, u16 len, u8 *reg_buf)
261 +{
262 + u32 cmd_args;
263 +
264 + /*
265 + * Command arguments of CMD48:
266 + * [31:31] MIO (0 = memory).
267 + * [30:27] FNO (function number).
268 + * [26:26] reserved (0).
269 + * [25:18] page number.
270 + * [17:9] offset address.
271 + * [8:0] length (0 = 1 byte, 1ff = 512 bytes).
272 + */
273 + cmd_args = fno << 27 | page << 18 | offset << 9 | (len - 1);
274 +
275 + return mmc_send_adtc_data(card, card->host, SD_READ_EXTR_SINGLE,
276 + cmd_args, reg_buf, 512);
277 +}
278
279 static int mmc_sd_cmdq_switch(struct mmc_card *card, bool enable)
280 {
281 @@ -383,8 +458,8 @@ static int mmc_sd_cmdq_switch(struct mmc
282 reg = BIT(0);
283
284 /* Performance enhancement register byte 262 controls command queueing */
285 - err = sd_write_ext_reg(card, card->ext_perf.fno, card->ext_perf.page,
286 - card->ext_perf.offset + 262, reg);
287 + err = mmc_sd_write_ext_reg(card, card->ext_perf.fno, card->ext_perf.page,
288 + card->ext_perf.offset + 262, reg);
289 if (!err)
290 card->ext_csd.cmdq_en = enable;
291
292 --- a/drivers/mmc/core/sd_ops.h
293 +++ b/drivers/mmc/core/sd_ops.h
294 @@ -23,6 +23,10 @@ int mmc_app_sd_status(struct mmc_card *c
295 int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card);
296 int mmc_sd_cmdq_enable(struct mmc_card *card);
297 int mmc_sd_cmdq_disable(struct mmc_card *card);
298 +int mmc_sd_write_ext_reg(struct mmc_card *card, u8 fno, u8 page, u16 offset,
299 + u8 reg_data);
300 +int mmc_sd_read_ext_reg(struct mmc_card *card, u8 fno, u8 page,
301 + u16 offset, u16 len, u8 *reg_buf);
302
303 #endif
304