blob: 83ae1e516996c8aef775dced41471d23a23a39ea [file] [log] [blame]
Paul Burtond7727132018-12-16 19:25:20 -03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Ingenic JZ MMC driver
4 *
5 * Copyright (c) 2013 Imagination Technologies
6 * Author: Paul Burton <paul.burton@imgtec.com>
7 */
8
9#include <common.h>
10#include <malloc.h>
11#include <mmc.h>
12#include <asm/io.h>
13#include <asm/unaligned.h>
14#include <errno.h>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Simon Glassc05ed002020-05-10 11:40:11 -060016#include <linux/delay.h>
Paul Burtond7727132018-12-16 19:25:20 -030017#include <mach/jz4780.h>
18#include <wait_bit.h>
19
20/* Registers */
21#define MSC_STRPCL 0x000
22#define MSC_STAT 0x004
23#define MSC_CLKRT 0x008
24#define MSC_CMDAT 0x00c
25#define MSC_RESTO 0x010
26#define MSC_RDTO 0x014
27#define MSC_BLKLEN 0x018
28#define MSC_NOB 0x01c
29#define MSC_SNOB 0x020
30#define MSC_IMASK 0x024
31#define MSC_IREG 0x028
32#define MSC_CMD 0x02c
33#define MSC_ARG 0x030
34#define MSC_RES 0x034
35#define MSC_RXFIFO 0x038
36#define MSC_TXFIFO 0x03c
37#define MSC_LPM 0x040
38#define MSC_DMAC 0x044
39#define MSC_DMANDA 0x048
40#define MSC_DMADA 0x04c
41#define MSC_DMALEN 0x050
42#define MSC_DMACMD 0x054
43#define MSC_CTRL2 0x058
44#define MSC_RTCNT 0x05c
45#define MSC_DBG 0x0fc
46
47/* MSC Clock and Control Register (MSC_STRPCL) */
48#define MSC_STRPCL_EXIT_MULTIPLE BIT(7)
49#define MSC_STRPCL_EXIT_TRANSFER BIT(6)
50#define MSC_STRPCL_START_READWAIT BIT(5)
51#define MSC_STRPCL_STOP_READWAIT BIT(4)
52#define MSC_STRPCL_RESET BIT(3)
53#define MSC_STRPCL_START_OP BIT(2)
54#define MSC_STRPCL_CLOCK_CONTROL_STOP BIT(0)
55#define MSC_STRPCL_CLOCK_CONTROL_START BIT(1)
56
57/* MSC Status Register (MSC_STAT) */
58#define MSC_STAT_AUTO_CMD_DONE BIT(31)
59#define MSC_STAT_IS_RESETTING BIT(15)
60#define MSC_STAT_SDIO_INT_ACTIVE BIT(14)
61#define MSC_STAT_PRG_DONE BIT(13)
62#define MSC_STAT_DATA_TRAN_DONE BIT(12)
63#define MSC_STAT_END_CMD_RES BIT(11)
64#define MSC_STAT_DATA_FIFO_AFULL BIT(10)
65#define MSC_STAT_IS_READWAIT BIT(9)
66#define MSC_STAT_CLK_EN BIT(8)
67#define MSC_STAT_DATA_FIFO_FULL BIT(7)
68#define MSC_STAT_DATA_FIFO_EMPTY BIT(6)
69#define MSC_STAT_CRC_RES_ERR BIT(5)
70#define MSC_STAT_CRC_READ_ERROR BIT(4)
71#define MSC_STAT_CRC_WRITE_ERROR BIT(2)
72#define MSC_STAT_CRC_WRITE_ERROR_NOSTS BIT(4)
73#define MSC_STAT_TIME_OUT_RES BIT(1)
74#define MSC_STAT_TIME_OUT_READ BIT(0)
75
76/* MSC Bus Clock Control Register (MSC_CLKRT) */
77#define MSC_CLKRT_CLK_RATE_MASK 0x7
78
79/* MSC Command Sequence Control Register (MSC_CMDAT) */
80#define MSC_CMDAT_IO_ABORT BIT(11)
81#define MSC_CMDAT_BUS_WIDTH_1BIT (0x0 << 9)
82#define MSC_CMDAT_BUS_WIDTH_4BIT (0x2 << 9)
83#define MSC_CMDAT_DMA_EN BIT(8)
84#define MSC_CMDAT_INIT BIT(7)
85#define MSC_CMDAT_BUSY BIT(6)
86#define MSC_CMDAT_STREAM_BLOCK BIT(5)
87#define MSC_CMDAT_WRITE BIT(4)
88#define MSC_CMDAT_DATA_EN BIT(3)
89#define MSC_CMDAT_RESPONSE_MASK (0x7 << 0)
90#define MSC_CMDAT_RESPONSE_NONE (0x0 << 0) /* No response */
91#define MSC_CMDAT_RESPONSE_R1 (0x1 << 0) /* Format R1 and R1b */
92#define MSC_CMDAT_RESPONSE_R2 (0x2 << 0) /* Format R2 */
93#define MSC_CMDAT_RESPONSE_R3 (0x3 << 0) /* Format R3 */
94#define MSC_CMDAT_RESPONSE_R4 (0x4 << 0) /* Format R4 */
95#define MSC_CMDAT_RESPONSE_R5 (0x5 << 0) /* Format R5 */
96#define MSC_CMDAT_RESPONSE_R6 (0x6 << 0) /* Format R6 */
97
98/* MSC Interrupts Mask Register (MSC_IMASK) */
99#define MSC_IMASK_TIME_OUT_RES BIT(9)
100#define MSC_IMASK_TIME_OUT_READ BIT(8)
101#define MSC_IMASK_SDIO BIT(7)
102#define MSC_IMASK_TXFIFO_WR_REQ BIT(6)
103#define MSC_IMASK_RXFIFO_RD_REQ BIT(5)
104#define MSC_IMASK_END_CMD_RES BIT(2)
105#define MSC_IMASK_PRG_DONE BIT(1)
106#define MSC_IMASK_DATA_TRAN_DONE BIT(0)
107
108/* MSC Interrupts Status Register (MSC_IREG) */
109#define MSC_IREG_TIME_OUT_RES BIT(9)
110#define MSC_IREG_TIME_OUT_READ BIT(8)
111#define MSC_IREG_SDIO BIT(7)
112#define MSC_IREG_TXFIFO_WR_REQ BIT(6)
113#define MSC_IREG_RXFIFO_RD_REQ BIT(5)
114#define MSC_IREG_END_CMD_RES BIT(2)
115#define MSC_IREG_PRG_DONE BIT(1)
116#define MSC_IREG_DATA_TRAN_DONE BIT(0)
117
118struct jz_mmc_plat {
119 struct mmc_config cfg;
120 struct mmc mmc;
121};
122
123struct jz_mmc_priv {
124 void __iomem *regs;
125 u32 flags;
126/* priv flags */
127#define JZ_MMC_BUS_WIDTH_MASK 0x3
128#define JZ_MMC_BUS_WIDTH_1 0x0
129#define JZ_MMC_BUS_WIDTH_4 0x2
130#define JZ_MMC_BUS_WIDTH_8 0x3
131#define JZ_MMC_SENT_INIT BIT(2)
132};
133
134static int jz_mmc_clock_rate(void)
135{
136 return 24000000;
137}
138
Ezequiel Garcia82d54642019-01-07 18:13:25 -0300139#if CONFIG_IS_ENABLED(MMC_WRITE)
140static inline void jz_mmc_write_data(struct jz_mmc_priv *priv, struct mmc_data *data)
141{
142 int sz = DIV_ROUND_UP(data->blocks * data->blocksize, 4);
143 const void *buf = data->src;
144
145 while (sz--) {
146 u32 val = get_unaligned_le32(buf);
147
148 wait_for_bit_le32(priv->regs + MSC_IREG,
149 MSC_IREG_TXFIFO_WR_REQ,
150 true, 10000, false);
151 writel(val, priv->regs + MSC_TXFIFO);
152 buf += 4;
153 }
154}
155#else
156static void jz_mmc_write_data(struct jz_mmc_priv *priv, struct mmc_data *data)
157{}
158#endif
159
160static inline int jz_mmc_read_data(struct jz_mmc_priv *priv, struct mmc_data *data)
161{
162 int sz = data->blocks * data->blocksize;
163 void *buf = data->dest;
164 u32 stat, val;
165
166 do {
167 stat = readl(priv->regs + MSC_STAT);
168
169 if (stat & MSC_STAT_TIME_OUT_READ)
170 return -ETIMEDOUT;
171 if (stat & MSC_STAT_CRC_READ_ERROR)
172 return -EINVAL;
173 if (stat & MSC_STAT_DATA_FIFO_EMPTY) {
174 udelay(10);
175 continue;
176 }
177 do {
178 val = readl(priv->regs + MSC_RXFIFO);
179 if (sz == 1)
180 *(u8 *)buf = (u8)val;
181 else if (sz == 2)
182 put_unaligned_le16(val, buf);
183 else if (sz >= 4)
184 put_unaligned_le32(val, buf);
185 buf += 4;
186 sz -= 4;
187 stat = readl(priv->regs + MSC_STAT);
188 } while (!(stat & MSC_STAT_DATA_FIFO_EMPTY));
189 } while (!(stat & MSC_STAT_DATA_TRAN_DONE));
190 return 0;
191}
192
Paul Burtond7727132018-12-16 19:25:20 -0300193static int jz_mmc_send_cmd(struct mmc *mmc, struct jz_mmc_priv *priv,
194 struct mmc_cmd *cmd, struct mmc_data *data)
195{
196 u32 stat, mask, cmdat = 0;
197 int i, ret;
198
199 /* stop the clock */
200 writel(MSC_STRPCL_CLOCK_CONTROL_STOP, priv->regs + MSC_STRPCL);
201 ret = wait_for_bit_le32(priv->regs + MSC_STAT,
202 MSC_STAT_CLK_EN, false, 10000, false);
203 if (ret)
204 return ret;
205
206 writel(0, priv->regs + MSC_DMAC);
207
208 /* setup command */
209 writel(cmd->cmdidx, priv->regs + MSC_CMD);
210 writel(cmd->cmdarg, priv->regs + MSC_ARG);
211
212 if (data) {
213 /* setup data */
214 cmdat |= MSC_CMDAT_DATA_EN;
215 if (data->flags & MMC_DATA_WRITE)
216 cmdat |= MSC_CMDAT_WRITE;
217
218 writel(data->blocks, priv->regs + MSC_NOB);
219 writel(data->blocksize, priv->regs + MSC_BLKLEN);
220 } else {
221 writel(0, priv->regs + MSC_NOB);
222 writel(0, priv->regs + MSC_BLKLEN);
223 }
224
225 /* setup response */
226 switch (cmd->resp_type) {
227 case MMC_RSP_NONE:
228 break;
229 case MMC_RSP_R1:
230 case MMC_RSP_R1b:
231 cmdat |= MSC_CMDAT_RESPONSE_R1;
232 break;
233 case MMC_RSP_R2:
234 cmdat |= MSC_CMDAT_RESPONSE_R2;
235 break;
236 case MMC_RSP_R3:
237 cmdat |= MSC_CMDAT_RESPONSE_R3;
238 break;
239 default:
240 break;
241 }
242
243 if (cmd->resp_type & MMC_RSP_BUSY)
244 cmdat |= MSC_CMDAT_BUSY;
245
246 /* set init for the first command only */
247 if (!(priv->flags & JZ_MMC_SENT_INIT)) {
248 cmdat |= MSC_CMDAT_INIT;
249 priv->flags |= JZ_MMC_SENT_INIT;
250 }
251
252 cmdat |= (priv->flags & JZ_MMC_BUS_WIDTH_MASK) << 9;
253
254 /* write the data setup */
255 writel(cmdat, priv->regs + MSC_CMDAT);
256
257 /* unmask interrupts */
258 mask = 0xffffffff & ~(MSC_IMASK_END_CMD_RES | MSC_IMASK_TIME_OUT_RES);
259 if (data) {
260 mask &= ~MSC_IMASK_DATA_TRAN_DONE;
261 if (data->flags & MMC_DATA_WRITE) {
262 mask &= ~MSC_IMASK_TXFIFO_WR_REQ;
263 } else {
264 mask &= ~(MSC_IMASK_RXFIFO_RD_REQ |
265 MSC_IMASK_TIME_OUT_READ);
266 }
267 }
268 writel(mask, priv->regs + MSC_IMASK);
269
270 /* clear interrupts */
271 writel(0xffffffff, priv->regs + MSC_IREG);
272
273 /* start the command (& the clock) */
274 writel(MSC_STRPCL_START_OP | MSC_STRPCL_CLOCK_CONTROL_START,
275 priv->regs + MSC_STRPCL);
276
277 /* wait for completion */
278 for (i = 0; i < 100; i++) {
279 stat = readl(priv->regs + MSC_IREG);
280 stat &= MSC_IREG_END_CMD_RES | MSC_IREG_TIME_OUT_RES;
281 if (stat)
282 break;
283 mdelay(1);
284 }
285 writel(stat, priv->regs + MSC_IREG);
286 if (stat & MSC_IREG_TIME_OUT_RES)
287 return -ETIMEDOUT;
288
289 if (cmd->resp_type & MMC_RSP_PRESENT) {
290 /* read the response */
291 if (cmd->resp_type & MMC_RSP_136) {
292 u16 a, b, c, i;
293
294 a = readw(priv->regs + MSC_RES);
295 for (i = 0; i < 4; i++) {
296 b = readw(priv->regs + MSC_RES);
297 c = readw(priv->regs + MSC_RES);
298 cmd->response[i] =
299 (a << 24) | (b << 8) | (c >> 8);
300 a = c;
301 }
302 } else {
303 cmd->response[0] = readw(priv->regs + MSC_RES) << 24;
304 cmd->response[0] |= readw(priv->regs + MSC_RES) << 8;
305 cmd->response[0] |= readw(priv->regs + MSC_RES) & 0xff;
306 }
307 }
Ezequiel Garcia82d54642019-01-07 18:13:25 -0300308 if (data) {
309 if (data->flags & MMC_DATA_WRITE)
310 jz_mmc_write_data(priv, data);
311 else if (data->flags & MMC_DATA_READ) {
312 ret = jz_mmc_read_data(priv, data);
313 if (ret)
314 return ret;
Paul Burtond7727132018-12-16 19:25:20 -0300315 }
Paul Burtond7727132018-12-16 19:25:20 -0300316 }
317
318 return 0;
319}
320
321static int jz_mmc_set_ios(struct mmc *mmc, struct jz_mmc_priv *priv)
322{
323 u32 real_rate = jz_mmc_clock_rate();
324 u8 clk_div = 0;
325
326 /* calculate clock divide */
327 while ((real_rate > mmc->clock) && (clk_div < 7)) {
328 real_rate >>= 1;
329 clk_div++;
330 }
331 writel(clk_div & MSC_CLKRT_CLK_RATE_MASK, priv->regs + MSC_CLKRT);
332
333 /* set the bus width for the next command */
334 priv->flags &= ~JZ_MMC_BUS_WIDTH_MASK;
335 if (mmc->bus_width == 8)
336 priv->flags |= JZ_MMC_BUS_WIDTH_8;
337 else if (mmc->bus_width == 4)
338 priv->flags |= JZ_MMC_BUS_WIDTH_4;
339 else
340 priv->flags |= JZ_MMC_BUS_WIDTH_1;
341
342 return 0;
343}
344
345static int jz_mmc_core_init(struct mmc *mmc)
346{
347 struct jz_mmc_priv *priv = mmc->priv;
348 int ret;
349
350 /* Reset */
351 writel(MSC_STRPCL_RESET, priv->regs + MSC_STRPCL);
352 ret = wait_for_bit_le32(priv->regs + MSC_STAT,
353 MSC_STAT_IS_RESETTING, false, 10000, false);
354 if (ret)
355 return ret;
356
357 /* Maximum timeouts */
358 writel(0xffff, priv->regs + MSC_RESTO);
359 writel(0xffffffff, priv->regs + MSC_RDTO);
360
361 /* Enable low power mode */
362 writel(0x1, priv->regs + MSC_LPM);
363
364 return 0;
365}
366
367#if !CONFIG_IS_ENABLED(DM_MMC)
368
369static int jz_mmc_legacy_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
370 struct mmc_data *data)
371{
372 struct jz_mmc_priv *priv = mmc->priv;
373
374 return jz_mmc_send_cmd(mmc, priv, cmd, data);
375}
376
377static int jz_mmc_legacy_set_ios(struct mmc *mmc)
378{
379 struct jz_mmc_priv *priv = mmc->priv;
380
381 return jz_mmc_set_ios(mmc, priv);
382};
383
384static const struct mmc_ops jz_msc_ops = {
385 .send_cmd = jz_mmc_legacy_send_cmd,
386 .set_ios = jz_mmc_legacy_set_ios,
387 .init = jz_mmc_core_init,
388};
389
390static struct jz_mmc_priv jz_mmc_priv_static;
391static struct jz_mmc_plat jz_mmc_plat_static = {
392 .cfg = {
393 .name = "MSC",
394 .ops = &jz_msc_ops,
395
396 .voltages = MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 |
397 MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 |
398 MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36,
399 .host_caps = MMC_MODE_4BIT | MMC_MODE_HS_52MHz | MMC_MODE_HS,
400
401 .f_min = 375000,
402 .f_max = 48000000,
403 .b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT,
404 },
405};
406
407int jz_mmc_init(void __iomem *base)
408{
409 struct mmc *mmc;
410
411 jz_mmc_priv_static.regs = base;
412
413 mmc = mmc_create(&jz_mmc_plat_static.cfg, &jz_mmc_priv_static);
414
415 return mmc ? 0 : -ENODEV;
416}
417
418#else /* CONFIG_DM_MMC */
419
420#include <dm.h>
421DECLARE_GLOBAL_DATA_PTR;
422
423static int jz_mmc_dm_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
424 struct mmc_data *data)
425{
426 struct jz_mmc_priv *priv = dev_get_priv(dev);
427 struct mmc *mmc = mmc_get_mmc_dev(dev);
428
429 return jz_mmc_send_cmd(mmc, priv, cmd, data);
430}
431
432static int jz_mmc_dm_set_ios(struct udevice *dev)
433{
434 struct jz_mmc_priv *priv = dev_get_priv(dev);
435 struct mmc *mmc = mmc_get_mmc_dev(dev);
436
437 return jz_mmc_set_ios(mmc, priv);
438};
439
440static const struct dm_mmc_ops jz_msc_ops = {
441 .send_cmd = jz_mmc_dm_send_cmd,
442 .set_ios = jz_mmc_dm_set_ios,
443};
444
445static int jz_mmc_ofdata_to_platdata(struct udevice *dev)
446{
447 struct jz_mmc_priv *priv = dev_get_priv(dev);
448 struct jz_mmc_plat *plat = dev_get_platdata(dev);
449 struct mmc_config *cfg;
450 int ret;
451
452 priv->regs = map_physmem(devfdt_get_addr(dev), 0x100, MAP_NOCACHE);
453 cfg = &plat->cfg;
454
455 cfg->name = "MSC";
456 cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
457
458 ret = mmc_of_parse(dev, cfg);
459 if (ret < 0) {
460 dev_err(dev, "failed to parse host caps\n");
461 return ret;
462 }
463
464 cfg->f_min = 400000;
465 cfg->f_max = 52000000;
466
467 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
468 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
469
470 return 0;
471}
472
473static int jz_mmc_bind(struct udevice *dev)
474{
475 struct jz_mmc_plat *plat = dev_get_platdata(dev);
476
477 return mmc_bind(dev, &plat->mmc, &plat->cfg);
478}
479
480static int jz_mmc_probe(struct udevice *dev)
481{
482 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
483 struct jz_mmc_priv *priv = dev_get_priv(dev);
484 struct jz_mmc_plat *plat = dev_get_platdata(dev);
485
486 plat->mmc.priv = priv;
487 upriv->mmc = &plat->mmc;
488 return jz_mmc_core_init(&plat->mmc);
489}
490
491static const struct udevice_id jz_mmc_ids[] = {
492 { .compatible = "ingenic,jz4780-mmc" },
493 { }
494};
495
496U_BOOT_DRIVER(jz_mmc_drv) = {
497 .name = "jz_mmc",
498 .id = UCLASS_MMC,
499 .of_match = jz_mmc_ids,
500 .ofdata_to_platdata = jz_mmc_ofdata_to_platdata,
501 .bind = jz_mmc_bind,
502 .probe = jz_mmc_probe,
503 .priv_auto_alloc_size = sizeof(struct jz_mmc_priv),
504 .platdata_auto_alloc_size = sizeof(struct jz_mmc_plat),
505 .ops = &jz_msc_ops,
506};
507#endif /* CONFIG_DM_MMC */