blob: 3c462bd5835e9e4efae745791fdaaff80a4c2af5 [file] [log] [blame]
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +09001/*
Masahiro Yamadafa1f73f2016-07-19 21:56:13 +09002 * Copyright (C) 2016 Socionext Inc.
3 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +09004 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <clk.h>
10#include <fdtdec.h>
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +090011#include <mmc.h>
Simon Glass11c89f32017-05-17 17:18:03 -060012#include <dm.h>
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +090013#include <linux/compat.h>
14#include <linux/io.h>
Masahiro Yamadadc158ef2016-03-24 22:32:42 +090015#include <linux/sizes.h>
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +090016#include <asm/unaligned.h>
17#include <asm/dma-mapping.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
21#define UNIPHIER_SD_CMD 0x000 /* command */
22#define UNIPHIER_SD_CMD_NOSTOP BIT(14) /* No automatic CMD12 issue */
23#define UNIPHIER_SD_CMD_MULTI BIT(13) /* multiple block transfer */
24#define UNIPHIER_SD_CMD_RD BIT(12) /* 1: read, 0: write */
25#define UNIPHIER_SD_CMD_DATA BIT(11) /* data transfer */
26#define UNIPHIER_SD_CMD_APP BIT(6) /* ACMD preceded by CMD55 */
27#define UNIPHIER_SD_CMD_NORMAL (0 << 8)/* auto-detect of resp-type */
28#define UNIPHIER_SD_CMD_RSP_NONE (3 << 8)/* response: none */
29#define UNIPHIER_SD_CMD_RSP_R1 (4 << 8)/* response: R1, R5, R6, R7 */
30#define UNIPHIER_SD_CMD_RSP_R1B (5 << 8)/* response: R1b, R5b */
31#define UNIPHIER_SD_CMD_RSP_R2 (6 << 8)/* response: R2 */
32#define UNIPHIER_SD_CMD_RSP_R3 (7 << 8)/* response: R3, R4 */
33#define UNIPHIER_SD_ARG 0x008 /* command argument */
34#define UNIPHIER_SD_STOP 0x010 /* stop action control */
35#define UNIPHIER_SD_STOP_SEC BIT(8) /* use sector count */
36#define UNIPHIER_SD_STOP_STP BIT(0) /* issue CMD12 */
37#define UNIPHIER_SD_SECCNT 0x014 /* sector counter */
38#define UNIPHIER_SD_RSP10 0x018 /* response[39:8] */
39#define UNIPHIER_SD_RSP32 0x020 /* response[71:40] */
40#define UNIPHIER_SD_RSP54 0x028 /* response[103:72] */
41#define UNIPHIER_SD_RSP76 0x030 /* response[127:104] */
42#define UNIPHIER_SD_INFO1 0x038 /* IRQ status 1 */
43#define UNIPHIER_SD_INFO1_CD BIT(5) /* state of card detect */
44#define UNIPHIER_SD_INFO1_INSERT BIT(4) /* card inserted */
45#define UNIPHIER_SD_INFO1_REMOVE BIT(3) /* card removed */
46#define UNIPHIER_SD_INFO1_CMP BIT(2) /* data complete */
47#define UNIPHIER_SD_INFO1_RSP BIT(0) /* response complete */
48#define UNIPHIER_SD_INFO2 0x03c /* IRQ status 2 */
49#define UNIPHIER_SD_INFO2_ERR_ILA BIT(15) /* illegal access err */
50#define UNIPHIER_SD_INFO2_CBSY BIT(14) /* command busy */
51#define UNIPHIER_SD_INFO2_BWE BIT(9) /* write buffer ready */
52#define UNIPHIER_SD_INFO2_BRE BIT(8) /* read buffer ready */
53#define UNIPHIER_SD_INFO2_DAT0 BIT(7) /* SDDAT0 */
54#define UNIPHIER_SD_INFO2_ERR_RTO BIT(6) /* response time out */
55#define UNIPHIER_SD_INFO2_ERR_ILR BIT(5) /* illegal read err */
56#define UNIPHIER_SD_INFO2_ERR_ILW BIT(4) /* illegal write err */
57#define UNIPHIER_SD_INFO2_ERR_TO BIT(3) /* time out error */
58#define UNIPHIER_SD_INFO2_ERR_END BIT(2) /* END bit error */
59#define UNIPHIER_SD_INFO2_ERR_CRC BIT(1) /* CRC error */
60#define UNIPHIER_SD_INFO2_ERR_IDX BIT(0) /* cmd index error */
61#define UNIPHIER_SD_INFO1_MASK 0x040
62#define UNIPHIER_SD_INFO2_MASK 0x044
63#define UNIPHIER_SD_CLKCTL 0x048 /* clock divisor */
64#define UNIPHIER_SD_CLKCTL_DIV_MASK 0x104ff
65#define UNIPHIER_SD_CLKCTL_DIV1024 BIT(16) /* SDCLK = CLK / 1024 */
66#define UNIPHIER_SD_CLKCTL_DIV512 BIT(7) /* SDCLK = CLK / 512 */
67#define UNIPHIER_SD_CLKCTL_DIV256 BIT(6) /* SDCLK = CLK / 256 */
68#define UNIPHIER_SD_CLKCTL_DIV128 BIT(5) /* SDCLK = CLK / 128 */
69#define UNIPHIER_SD_CLKCTL_DIV64 BIT(4) /* SDCLK = CLK / 64 */
70#define UNIPHIER_SD_CLKCTL_DIV32 BIT(3) /* SDCLK = CLK / 32 */
71#define UNIPHIER_SD_CLKCTL_DIV16 BIT(2) /* SDCLK = CLK / 16 */
72#define UNIPHIER_SD_CLKCTL_DIV8 BIT(1) /* SDCLK = CLK / 8 */
73#define UNIPHIER_SD_CLKCTL_DIV4 BIT(0) /* SDCLK = CLK / 4 */
74#define UNIPHIER_SD_CLKCTL_DIV2 0 /* SDCLK = CLK / 2 */
75#define UNIPHIER_SD_CLKCTL_DIV1 BIT(10) /* SDCLK = CLK */
76#define UNIPHIER_SD_CLKCTL_OFFEN BIT(9) /* stop SDCLK when unused */
77#define UNIPHIER_SD_CLKCTL_SCLKEN BIT(8) /* SDCLK output enable */
78#define UNIPHIER_SD_SIZE 0x04c /* block size */
79#define UNIPHIER_SD_OPTION 0x050
80#define UNIPHIER_SD_OPTION_WIDTH_MASK (5 << 13)
81#define UNIPHIER_SD_OPTION_WIDTH_1 (4 << 13)
82#define UNIPHIER_SD_OPTION_WIDTH_4 (0 << 13)
83#define UNIPHIER_SD_OPTION_WIDTH_8 (1 << 13)
84#define UNIPHIER_SD_BUF 0x060 /* read/write buffer */
85#define UNIPHIER_SD_EXTMODE 0x1b0
86#define UNIPHIER_SD_EXTMODE_DMA_EN BIT(1) /* transfer 1: DMA, 0: pio */
87#define UNIPHIER_SD_SOFT_RST 0x1c0
88#define UNIPHIER_SD_SOFT_RST_RSTX BIT(0) /* reset deassert */
89#define UNIPHIER_SD_VERSION 0x1c4 /* version register */
90#define UNIPHIER_SD_VERSION_IP 0xff /* IP version */
91#define UNIPHIER_SD_HOST_MODE 0x1c8
92#define UNIPHIER_SD_IF_MODE 0x1cc
93#define UNIPHIER_SD_IF_MODE_DDR BIT(0) /* DDR mode */
94#define UNIPHIER_SD_VOLT 0x1e4 /* voltage switch */
95#define UNIPHIER_SD_VOLT_MASK (3 << 0)
96#define UNIPHIER_SD_VOLT_OFF (0 << 0)
97#define UNIPHIER_SD_VOLT_330 (1 << 0)/* 3.3V signal */
98#define UNIPHIER_SD_VOLT_180 (2 << 0)/* 1.8V signal */
99#define UNIPHIER_SD_DMA_MODE 0x410
100#define UNIPHIER_SD_DMA_MODE_DIR_RD BIT(16) /* 1: from device, 0: to dev */
101#define UNIPHIER_SD_DMA_MODE_ADDR_INC BIT(0) /* 1: address inc, 0: fixed */
102#define UNIPHIER_SD_DMA_CTL 0x414
103#define UNIPHIER_SD_DMA_CTL_START BIT(0) /* start DMA (auto cleared) */
104#define UNIPHIER_SD_DMA_RST 0x418
105#define UNIPHIER_SD_DMA_RST_RD BIT(9)
106#define UNIPHIER_SD_DMA_RST_WR BIT(8)
107#define UNIPHIER_SD_DMA_INFO1 0x420
108#define UNIPHIER_SD_DMA_INFO1_END_RD2 BIT(20) /* DMA from device is complete*/
109#define UNIPHIER_SD_DMA_INFO1_END_RD BIT(17) /* Don't use! Hardware bug */
110#define UNIPHIER_SD_DMA_INFO1_END_WR BIT(16) /* DMA to device is complete */
111#define UNIPHIER_SD_DMA_INFO1_MASK 0x424
112#define UNIPHIER_SD_DMA_INFO2 0x428
113#define UNIPHIER_SD_DMA_INFO2_ERR_RD BIT(17)
114#define UNIPHIER_SD_DMA_INFO2_ERR_WR BIT(16)
115#define UNIPHIER_SD_DMA_INFO2_MASK 0x42c
116#define UNIPHIER_SD_DMA_ADDR_L 0x440
117#define UNIPHIER_SD_DMA_ADDR_H 0x444
118
119/* alignment required by the DMA engine of this controller */
120#define UNIPHIER_SD_DMA_MINALIGN 0x10
121
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900122struct uniphier_sd_plat {
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900123 struct mmc_config cfg;
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900124 struct mmc mmc;
125};
126
127struct uniphier_sd_priv {
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900128 void __iomem *regbase;
129 unsigned long mclk;
130 unsigned int version;
131 u32 caps;
132#define UNIPHIER_SD_CAP_NONREMOVABLE BIT(0) /* Nonremovable e.g. eMMC */
133#define UNIPHIER_SD_CAP_DMA_INTERNAL BIT(1) /* have internal DMA engine */
134#define UNIPHIER_SD_CAP_DIV1024 BIT(2) /* divisor 1024 is available */
135};
136
137static dma_addr_t __dma_map_single(void *ptr, size_t size,
138 enum dma_data_direction dir)
139{
140 unsigned long addr = (unsigned long)ptr;
141
142 if (dir == DMA_FROM_DEVICE)
143 invalidate_dcache_range(addr, addr + size);
144 else
145 flush_dcache_range(addr, addr + size);
146
147 return addr;
148}
149
150static void __dma_unmap_single(dma_addr_t addr, size_t size,
151 enum dma_data_direction dir)
152{
153 if (dir != DMA_TO_DEVICE)
154 invalidate_dcache_range(addr, addr + size);
155}
156
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900157static int uniphier_sd_check_error(struct udevice *dev)
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900158{
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900159 struct uniphier_sd_priv *priv = dev_get_priv(dev);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900160 u32 info2 = readl(priv->regbase + UNIPHIER_SD_INFO2);
161
162 if (info2 & UNIPHIER_SD_INFO2_ERR_RTO) {
163 /*
164 * TIMEOUT must be returned for unsupported command. Do not
165 * display error log since this might be a part of sequence to
166 * distinguish between SD and MMC.
167 */
Jaehoon Chung7825d202016-07-19 16:33:36 +0900168 return -ETIMEDOUT;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900169 }
170
171 if (info2 & UNIPHIER_SD_INFO2_ERR_TO) {
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900172 dev_err(dev, "timeout error\n");
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900173 return -ETIMEDOUT;
174 }
175
176 if (info2 & (UNIPHIER_SD_INFO2_ERR_END | UNIPHIER_SD_INFO2_ERR_CRC |
177 UNIPHIER_SD_INFO2_ERR_IDX)) {
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900178 dev_err(dev, "communication out of sync\n");
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900179 return -EILSEQ;
180 }
181
182 if (info2 & (UNIPHIER_SD_INFO2_ERR_ILA | UNIPHIER_SD_INFO2_ERR_ILR |
183 UNIPHIER_SD_INFO2_ERR_ILW)) {
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900184 dev_err(dev, "illegal access\n");
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900185 return -EIO;
186 }
187
188 return 0;
189}
190
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900191static int uniphier_sd_wait_for_irq(struct udevice *dev, unsigned int reg,
192 u32 flag)
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900193{
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900194 struct uniphier_sd_priv *priv = dev_get_priv(dev);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900195 long wait = 1000000;
196 int ret;
197
198 while (!(readl(priv->regbase + reg) & flag)) {
199 if (wait-- < 0) {
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900200 dev_err(dev, "timeout\n");
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900201 return -ETIMEDOUT;
202 }
203
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900204 ret = uniphier_sd_check_error(dev);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900205 if (ret)
206 return ret;
207
208 udelay(1);
209 }
210
211 return 0;
212}
213
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900214static int uniphier_sd_pio_read_one_block(struct udevice *dev, u32 **pbuf,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900215 uint blocksize)
216{
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900217 struct uniphier_sd_priv *priv = dev_get_priv(dev);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900218 int i, ret;
219
220 /* wait until the buffer is filled with data */
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900221 ret = uniphier_sd_wait_for_irq(dev, UNIPHIER_SD_INFO2,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900222 UNIPHIER_SD_INFO2_BRE);
223 if (ret)
224 return ret;
225
226 /*
227 * Clear the status flag _before_ read the buffer out because
228 * UNIPHIER_SD_INFO2_BRE is edge-triggered, not level-triggered.
229 */
230 writel(0, priv->regbase + UNIPHIER_SD_INFO2);
231
232 if (likely(IS_ALIGNED((unsigned long)*pbuf, 4))) {
233 for (i = 0; i < blocksize / 4; i++)
234 *(*pbuf)++ = readl(priv->regbase + UNIPHIER_SD_BUF);
235 } else {
236 for (i = 0; i < blocksize / 4; i++)
237 put_unaligned(readl(priv->regbase + UNIPHIER_SD_BUF),
238 (*pbuf)++);
239 }
240
241 return 0;
242}
243
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900244static int uniphier_sd_pio_write_one_block(struct udevice *dev,
245 const u32 **pbuf, uint blocksize)
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900246{
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900247 struct uniphier_sd_priv *priv = dev_get_priv(dev);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900248 int i, ret;
249
250 /* wait until the buffer becomes empty */
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900251 ret = uniphier_sd_wait_for_irq(dev, UNIPHIER_SD_INFO2,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900252 UNIPHIER_SD_INFO2_BWE);
253 if (ret)
254 return ret;
255
256 writel(0, priv->regbase + UNIPHIER_SD_INFO2);
257
258 if (likely(IS_ALIGNED((unsigned long)*pbuf, 4))) {
259 for (i = 0; i < blocksize / 4; i++)
260 writel(*(*pbuf)++, priv->regbase + UNIPHIER_SD_BUF);
261 } else {
262 for (i = 0; i < blocksize / 4; i++)
263 writel(get_unaligned((*pbuf)++),
264 priv->regbase + UNIPHIER_SD_BUF);
265 }
266
267 return 0;
268}
269
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900270static int uniphier_sd_pio_xfer(struct udevice *dev, struct mmc_data *data)
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900271{
272 u32 *dest = (u32 *)data->dest;
273 const u32 *src = (const u32 *)data->src;
274 int i, ret;
275
276 for (i = 0; i < data->blocks; i++) {
277 if (data->flags & MMC_DATA_READ)
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900278 ret = uniphier_sd_pio_read_one_block(dev, &dest,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900279 data->blocksize);
280 else
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900281 ret = uniphier_sd_pio_write_one_block(dev, &src,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900282 data->blocksize);
283 if (ret)
284 return ret;
285 }
286
287 return 0;
288}
289
290static void uniphier_sd_dma_start(struct uniphier_sd_priv *priv,
291 dma_addr_t dma_addr)
292{
293 u32 tmp;
294
295 writel(0, priv->regbase + UNIPHIER_SD_DMA_INFO1);
296 writel(0, priv->regbase + UNIPHIER_SD_DMA_INFO2);
297
298 /* enable DMA */
299 tmp = readl(priv->regbase + UNIPHIER_SD_EXTMODE);
300 tmp |= UNIPHIER_SD_EXTMODE_DMA_EN;
301 writel(tmp, priv->regbase + UNIPHIER_SD_EXTMODE);
302
303 writel(dma_addr & U32_MAX, priv->regbase + UNIPHIER_SD_DMA_ADDR_L);
304
305 /* suppress the warning "right shift count >= width of type" */
306 dma_addr >>= min_t(int, 32, 8 * sizeof(dma_addr));
307
308 writel(dma_addr & U32_MAX, priv->regbase + UNIPHIER_SD_DMA_ADDR_H);
309
310 writel(UNIPHIER_SD_DMA_CTL_START, priv->regbase + UNIPHIER_SD_DMA_CTL);
311}
312
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900313static int uniphier_sd_dma_wait_for_irq(struct udevice *dev, u32 flag,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900314 unsigned int blocks)
315{
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900316 struct uniphier_sd_priv *priv = dev_get_priv(dev);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900317 long wait = 1000000 + 10 * blocks;
318
319 while (!(readl(priv->regbase + UNIPHIER_SD_DMA_INFO1) & flag)) {
320 if (wait-- < 0) {
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900321 dev_err(dev, "timeout during DMA\n");
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900322 return -ETIMEDOUT;
323 }
324
325 udelay(10);
326 }
327
328 if (readl(priv->regbase + UNIPHIER_SD_DMA_INFO2)) {
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900329 dev_err(dev, "error during DMA\n");
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900330 return -EIO;
331 }
332
333 return 0;
334}
335
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900336static int uniphier_sd_dma_xfer(struct udevice *dev, struct mmc_data *data)
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900337{
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900338 struct uniphier_sd_priv *priv = dev_get_priv(dev);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900339 size_t len = data->blocks * data->blocksize;
340 void *buf;
341 enum dma_data_direction dir;
342 dma_addr_t dma_addr;
343 u32 poll_flag, tmp;
344 int ret;
345
346 tmp = readl(priv->regbase + UNIPHIER_SD_DMA_MODE);
347
348 if (data->flags & MMC_DATA_READ) {
349 buf = data->dest;
350 dir = DMA_FROM_DEVICE;
351 poll_flag = UNIPHIER_SD_DMA_INFO1_END_RD2;
352 tmp |= UNIPHIER_SD_DMA_MODE_DIR_RD;
353 } else {
354 buf = (void *)data->src;
355 dir = DMA_TO_DEVICE;
356 poll_flag = UNIPHIER_SD_DMA_INFO1_END_WR;
357 tmp &= ~UNIPHIER_SD_DMA_MODE_DIR_RD;
358 }
359
360 writel(tmp, priv->regbase + UNIPHIER_SD_DMA_MODE);
361
362 dma_addr = __dma_map_single(buf, len, dir);
363
364 uniphier_sd_dma_start(priv, dma_addr);
365
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900366 ret = uniphier_sd_dma_wait_for_irq(dev, poll_flag, data->blocks);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900367
368 __dma_unmap_single(dma_addr, len, dir);
369
370 return ret;
371}
372
373/* check if the address is DMA'able */
374static bool uniphier_sd_addr_is_dmaable(unsigned long addr)
375{
376 if (!IS_ALIGNED(addr, UNIPHIER_SD_DMA_MINALIGN))
377 return false;
378
379#if defined(CONFIG_ARCH_UNIPHIER) && !defined(CONFIG_ARM64) && \
380 defined(CONFIG_SPL_BUILD)
381 /*
382 * For UniPhier ARMv7 SoCs, the stack is allocated in the locked ways
383 * of L2, which is unreachable from the DMA engine.
384 */
385 if (addr < CONFIG_SPL_STACK)
386 return false;
387#endif
388
389 return true;
390}
391
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900392static int uniphier_sd_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900393 struct mmc_data *data)
394{
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900395 struct uniphier_sd_priv *priv = dev_get_priv(dev);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900396 int ret;
397 u32 tmp;
398
399 if (readl(priv->regbase + UNIPHIER_SD_INFO2) & UNIPHIER_SD_INFO2_CBSY) {
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900400 dev_err(dev, "command busy\n");
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900401 return -EBUSY;
402 }
403
404 /* clear all status flags */
405 writel(0, priv->regbase + UNIPHIER_SD_INFO1);
406 writel(0, priv->regbase + UNIPHIER_SD_INFO2);
407
408 /* disable DMA once */
409 tmp = readl(priv->regbase + UNIPHIER_SD_EXTMODE);
410 tmp &= ~UNIPHIER_SD_EXTMODE_DMA_EN;
411 writel(tmp, priv->regbase + UNIPHIER_SD_EXTMODE);
412
413 writel(cmd->cmdarg, priv->regbase + UNIPHIER_SD_ARG);
414
415 tmp = cmd->cmdidx;
416
417 if (data) {
418 writel(data->blocksize, priv->regbase + UNIPHIER_SD_SIZE);
419 writel(data->blocks, priv->regbase + UNIPHIER_SD_SECCNT);
420
421 /* Do not send CMD12 automatically */
422 tmp |= UNIPHIER_SD_CMD_NOSTOP | UNIPHIER_SD_CMD_DATA;
423
424 if (data->blocks > 1)
425 tmp |= UNIPHIER_SD_CMD_MULTI;
426
427 if (data->flags & MMC_DATA_READ)
428 tmp |= UNIPHIER_SD_CMD_RD;
429 }
430
431 /*
432 * Do not use the response type auto-detection on this hardware.
433 * CMD8, for example, has different response types on SD and eMMC,
434 * while this controller always assumes the response type for SD.
435 * Set the response type manually.
436 */
437 switch (cmd->resp_type) {
438 case MMC_RSP_NONE:
439 tmp |= UNIPHIER_SD_CMD_RSP_NONE;
440 break;
441 case MMC_RSP_R1:
442 tmp |= UNIPHIER_SD_CMD_RSP_R1;
443 break;
444 case MMC_RSP_R1b:
445 tmp |= UNIPHIER_SD_CMD_RSP_R1B;
446 break;
447 case MMC_RSP_R2:
448 tmp |= UNIPHIER_SD_CMD_RSP_R2;
449 break;
450 case MMC_RSP_R3:
451 tmp |= UNIPHIER_SD_CMD_RSP_R3;
452 break;
453 default:
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900454 dev_err(dev, "unknown response type\n");
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900455 return -EINVAL;
456 }
457
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900458 dev_dbg(dev, "sending CMD%d (SD_CMD=%08x, SD_ARG=%08x)\n",
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900459 cmd->cmdidx, tmp, cmd->cmdarg);
460 writel(tmp, priv->regbase + UNIPHIER_SD_CMD);
461
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900462 ret = uniphier_sd_wait_for_irq(dev, UNIPHIER_SD_INFO1,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900463 UNIPHIER_SD_INFO1_RSP);
464 if (ret)
465 return ret;
466
467 if (cmd->resp_type & MMC_RSP_136) {
468 u32 rsp_127_104 = readl(priv->regbase + UNIPHIER_SD_RSP76);
469 u32 rsp_103_72 = readl(priv->regbase + UNIPHIER_SD_RSP54);
470 u32 rsp_71_40 = readl(priv->regbase + UNIPHIER_SD_RSP32);
471 u32 rsp_39_8 = readl(priv->regbase + UNIPHIER_SD_RSP10);
472
473 cmd->response[0] = (rsp_127_104 & 0xffffff) << 8 |
474 (rsp_103_72 & 0xff);
475 cmd->response[1] = (rsp_103_72 & 0xffffff) << 8 |
476 (rsp_71_40 & 0xff);
477 cmd->response[2] = (rsp_71_40 & 0xffffff) << 8 |
478 (rsp_39_8 & 0xff);
479 cmd->response[3] = (rsp_39_8 & 0xffffff) << 8;
480 } else {
481 /* bit 39-8 */
482 cmd->response[0] = readl(priv->regbase + UNIPHIER_SD_RSP10);
483 }
484
485 if (data) {
486 /* use DMA if the HW supports it and the buffer is aligned */
487 if (priv->caps & UNIPHIER_SD_CAP_DMA_INTERNAL &&
488 uniphier_sd_addr_is_dmaable((long)data->src))
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900489 ret = uniphier_sd_dma_xfer(dev, data);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900490 else
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900491 ret = uniphier_sd_pio_xfer(dev, data);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900492
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900493 ret = uniphier_sd_wait_for_irq(dev, UNIPHIER_SD_INFO1,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900494 UNIPHIER_SD_INFO1_CMP);
495 if (ret)
496 return ret;
497 }
498
499 return ret;
500}
501
Masahiro Yamada02051482016-08-25 14:52:37 +0900502static int uniphier_sd_set_bus_width(struct uniphier_sd_priv *priv,
503 struct mmc *mmc)
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900504{
505 u32 val, tmp;
506
507 switch (mmc->bus_width) {
508 case 1:
509 val = UNIPHIER_SD_OPTION_WIDTH_1;
510 break;
511 case 4:
512 val = UNIPHIER_SD_OPTION_WIDTH_4;
513 break;
514 case 8:
515 val = UNIPHIER_SD_OPTION_WIDTH_8;
516 break;
517 default:
Masahiro Yamada02051482016-08-25 14:52:37 +0900518 return -EINVAL;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900519 }
520
521 tmp = readl(priv->regbase + UNIPHIER_SD_OPTION);
522 tmp &= ~UNIPHIER_SD_OPTION_WIDTH_MASK;
523 tmp |= val;
524 writel(tmp, priv->regbase + UNIPHIER_SD_OPTION);
Masahiro Yamada02051482016-08-25 14:52:37 +0900525
526 return 0;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900527}
528
529static void uniphier_sd_set_ddr_mode(struct uniphier_sd_priv *priv,
530 struct mmc *mmc)
531{
532 u32 tmp;
533
534 tmp = readl(priv->regbase + UNIPHIER_SD_IF_MODE);
535 if (mmc->ddr_mode)
536 tmp |= UNIPHIER_SD_IF_MODE_DDR;
537 else
538 tmp &= ~UNIPHIER_SD_IF_MODE_DDR;
539 writel(tmp, priv->regbase + UNIPHIER_SD_IF_MODE);
540}
541
542static void uniphier_sd_set_clk_rate(struct uniphier_sd_priv *priv,
543 struct mmc *mmc)
544{
545 unsigned int divisor;
546 u32 val, tmp;
547
548 if (!mmc->clock)
549 return;
550
551 divisor = DIV_ROUND_UP(priv->mclk, mmc->clock);
552
553 if (divisor <= 1)
554 val = UNIPHIER_SD_CLKCTL_DIV1;
555 else if (divisor <= 2)
556 val = UNIPHIER_SD_CLKCTL_DIV2;
557 else if (divisor <= 4)
558 val = UNIPHIER_SD_CLKCTL_DIV4;
559 else if (divisor <= 8)
560 val = UNIPHIER_SD_CLKCTL_DIV8;
561 else if (divisor <= 16)
562 val = UNIPHIER_SD_CLKCTL_DIV16;
563 else if (divisor <= 32)
564 val = UNIPHIER_SD_CLKCTL_DIV32;
565 else if (divisor <= 64)
566 val = UNIPHIER_SD_CLKCTL_DIV64;
567 else if (divisor <= 128)
568 val = UNIPHIER_SD_CLKCTL_DIV128;
569 else if (divisor <= 256)
570 val = UNIPHIER_SD_CLKCTL_DIV256;
571 else if (divisor <= 512 || !(priv->caps & UNIPHIER_SD_CAP_DIV1024))
572 val = UNIPHIER_SD_CLKCTL_DIV512;
573 else
574 val = UNIPHIER_SD_CLKCTL_DIV1024;
575
576 tmp = readl(priv->regbase + UNIPHIER_SD_CLKCTL);
Masahiro Yamada25455532016-08-25 14:52:38 +0900577 if (tmp & UNIPHIER_SD_CLKCTL_SCLKEN &&
578 (tmp & UNIPHIER_SD_CLKCTL_DIV_MASK) == val)
579 return;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900580
581 /* stop the clock before changing its rate to avoid a glitch signal */
582 tmp &= ~UNIPHIER_SD_CLKCTL_SCLKEN;
583 writel(tmp, priv->regbase + UNIPHIER_SD_CLKCTL);
584
585 tmp &= ~UNIPHIER_SD_CLKCTL_DIV_MASK;
586 tmp |= val | UNIPHIER_SD_CLKCTL_OFFEN;
587 writel(tmp, priv->regbase + UNIPHIER_SD_CLKCTL);
588
589 tmp |= UNIPHIER_SD_CLKCTL_SCLKEN;
590 writel(tmp, priv->regbase + UNIPHIER_SD_CLKCTL);
Masahiro Yamada25455532016-08-25 14:52:38 +0900591
592 udelay(1000);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900593}
594
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900595static int uniphier_sd_set_ios(struct udevice *dev)
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900596{
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900597 struct uniphier_sd_priv *priv = dev_get_priv(dev);
598 struct mmc *mmc = mmc_get_mmc_dev(dev);
Masahiro Yamada02051482016-08-25 14:52:37 +0900599 int ret;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900600
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900601 dev_dbg(dev, "clock %uHz, DDRmode %d, width %u\n",
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900602 mmc->clock, mmc->ddr_mode, mmc->bus_width);
603
Masahiro Yamada02051482016-08-25 14:52:37 +0900604 ret = uniphier_sd_set_bus_width(priv, mmc);
605 if (ret)
606 return ret;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900607 uniphier_sd_set_ddr_mode(priv, mmc);
608 uniphier_sd_set_clk_rate(priv, mmc);
609
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900610 return 0;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900611}
612
Masahiro Yamadab29afb92016-08-25 14:52:36 +0900613static int uniphier_sd_get_cd(struct udevice *dev)
614{
615 struct uniphier_sd_priv *priv = dev_get_priv(dev);
616
617 if (priv->caps & UNIPHIER_SD_CAP_NONREMOVABLE)
618 return 1;
619
620 return !!(readl(priv->regbase + UNIPHIER_SD_INFO1) &
621 UNIPHIER_SD_INFO1_CD);
622}
623
624static const struct dm_mmc_ops uniphier_sd_ops = {
625 .send_cmd = uniphier_sd_send_cmd,
626 .set_ios = uniphier_sd_set_ios,
627 .get_cd = uniphier_sd_get_cd,
628};
629
630static void uniphier_sd_host_init(struct uniphier_sd_priv *priv)
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900631{
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900632 u32 tmp;
633
634 /* soft reset of the host */
635 tmp = readl(priv->regbase + UNIPHIER_SD_SOFT_RST);
636 tmp &= ~UNIPHIER_SD_SOFT_RST_RSTX;
637 writel(tmp, priv->regbase + UNIPHIER_SD_SOFT_RST);
638 tmp |= UNIPHIER_SD_SOFT_RST_RSTX;
639 writel(tmp, priv->regbase + UNIPHIER_SD_SOFT_RST);
640
641 /* FIXME: implement eMMC hw_reset */
642
643 writel(UNIPHIER_SD_STOP_SEC, priv->regbase + UNIPHIER_SD_STOP);
644
645 /*
646 * Connected to 32bit AXI.
647 * This register dropped backward compatibility at version 0x10.
648 * Write an appropriate value depending on the IP version.
649 */
650 writel(priv->version >= 0x10 ? 0x00000101 : 0x00000000,
651 priv->regbase + UNIPHIER_SD_HOST_MODE);
652
653 if (priv->caps & UNIPHIER_SD_CAP_DMA_INTERNAL) {
654 tmp = readl(priv->regbase + UNIPHIER_SD_DMA_MODE);
655 tmp |= UNIPHIER_SD_DMA_MODE_ADDR_INC;
656 writel(tmp, priv->regbase + UNIPHIER_SD_DMA_MODE);
657 }
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900658}
659
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900660static int uniphier_sd_bind(struct udevice *dev)
661{
662 struct uniphier_sd_plat *plat = dev_get_platdata(dev);
663
664 return mmc_bind(dev, &plat->mmc, &plat->cfg);
665}
666
Masahiro Yamadafe5428d2016-08-12 19:19:03 +0900667static int uniphier_sd_probe(struct udevice *dev)
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900668{
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900669 struct uniphier_sd_plat *plat = dev_get_platdata(dev);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900670 struct uniphier_sd_priv *priv = dev_get_priv(dev);
671 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
672 fdt_addr_t base;
Stephen Warrena9622432016-06-17 09:44:00 -0600673 struct clk clk;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900674 int ret;
675
Simon Glassba1dea42017-05-17 17:18:05 -0600676 base = devfdt_get_addr(dev);
Masahiro Yamadadc158ef2016-03-24 22:32:42 +0900677 if (base == FDT_ADDR_T_NONE)
678 return -EINVAL;
679
Masahiro Yamadafa1f73f2016-07-19 21:56:13 +0900680 priv->regbase = devm_ioremap(dev, base, SZ_2K);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900681 if (!priv->regbase)
682 return -ENOMEM;
683
Stephen Warrena9622432016-06-17 09:44:00 -0600684 ret = clk_get_by_index(dev, 0, &clk);
685 if (ret < 0) {
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900686 dev_err(dev, "failed to get host clock\n");
Stephen Warrena9622432016-06-17 09:44:00 -0600687 return ret;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900688 }
689
690 /* set to max rate */
Stephen Warrena9622432016-06-17 09:44:00 -0600691 priv->mclk = clk_set_rate(&clk, ULONG_MAX);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900692 if (IS_ERR_VALUE(priv->mclk)) {
693 dev_err(dev, "failed to set rate for host clock\n");
Stephen Warrena9622432016-06-17 09:44:00 -0600694 clk_free(&clk);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900695 return priv->mclk;
696 }
697
Stephen Warrena9622432016-06-17 09:44:00 -0600698 ret = clk_enable(&clk);
699 clk_free(&clk);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900700 if (ret) {
701 dev_err(dev, "failed to enable host clock\n");
702 return ret;
703 }
704
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900705 plat->cfg.name = dev->name;
706 plat->cfg.host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900707
Simon Glassdd79d6e2017-01-17 16:52:55 -0700708 switch (fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "bus-width",
709 1)) {
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900710 case 8:
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900711 plat->cfg.host_caps |= MMC_MODE_8BIT;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900712 break;
713 case 4:
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900714 plat->cfg.host_caps |= MMC_MODE_4BIT;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900715 break;
716 case 1:
717 break;
718 default:
719 dev_err(dev, "Invalid \"bus-width\" value\n");
720 return -EINVAL;
721 }
722
Simon Glassdd79d6e2017-01-17 16:52:55 -0700723 if (fdt_get_property(gd->fdt_blob, dev_of_offset(dev), "non-removable",
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900724 NULL))
725 priv->caps |= UNIPHIER_SD_CAP_NONREMOVABLE;
726
727 priv->version = readl(priv->regbase + UNIPHIER_SD_VERSION) &
728 UNIPHIER_SD_VERSION_IP;
729 dev_dbg(dev, "version %x\n", priv->version);
730 if (priv->version >= 0x10) {
731 priv->caps |= UNIPHIER_SD_CAP_DMA_INTERNAL;
732 priv->caps |= UNIPHIER_SD_CAP_DIV1024;
733 }
734
Masahiro Yamadab29afb92016-08-25 14:52:36 +0900735 uniphier_sd_host_init(priv);
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900736
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900737 plat->cfg.voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
738 plat->cfg.f_min = priv->mclk /
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900739 (priv->caps & UNIPHIER_SD_CAP_DIV1024 ? 1024 : 512);
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900740 plat->cfg.f_max = priv->mclk;
741 plat->cfg.b_max = U32_MAX; /* max value of UNIPHIER_SD_SECCNT */
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900742
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900743 upriv->mmc = &plat->mmc;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900744
745 return 0;
746}
747
748static const struct udevice_id uniphier_sd_match[] = {
749 { .compatible = "socionext,uniphier-sdhc" },
750 { /* sentinel */ }
751};
752
753U_BOOT_DRIVER(uniphier_mmc) = {
754 .name = "uniphier-mmc",
755 .id = UCLASS_MMC,
756 .of_match = uniphier_sd_match,
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900757 .bind = uniphier_sd_bind,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900758 .probe = uniphier_sd_probe,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900759 .priv_auto_alloc_size = sizeof(struct uniphier_sd_priv),
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900760 .platdata_auto_alloc_size = sizeof(struct uniphier_sd_plat),
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900761 .ops = &uniphier_sd_ops,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900762};