blob: 0d1203cb766ff0c045fc8e4857afe3e4ba60daf7 [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>
Masahiro Yamadaef205ea2017-08-26 00:50:17 +090014#include <linux/dma-direction.h>
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +090015#include <linux/io.h>
Masahiro Yamadadc158ef2016-03-24 22:32:42 +090016#include <linux/sizes.h>
Marek Vasut10f0c3c2017-09-15 21:10:54 +020017#include <power/regulator.h>
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +090018#include <asm/unaligned.h>
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +090019
20DECLARE_GLOBAL_DATA_PTR;
21
22#define UNIPHIER_SD_CMD 0x000 /* command */
23#define UNIPHIER_SD_CMD_NOSTOP BIT(14) /* No automatic CMD12 issue */
24#define UNIPHIER_SD_CMD_MULTI BIT(13) /* multiple block transfer */
25#define UNIPHIER_SD_CMD_RD BIT(12) /* 1: read, 0: write */
26#define UNIPHIER_SD_CMD_DATA BIT(11) /* data transfer */
27#define UNIPHIER_SD_CMD_APP BIT(6) /* ACMD preceded by CMD55 */
28#define UNIPHIER_SD_CMD_NORMAL (0 << 8)/* auto-detect of resp-type */
29#define UNIPHIER_SD_CMD_RSP_NONE (3 << 8)/* response: none */
30#define UNIPHIER_SD_CMD_RSP_R1 (4 << 8)/* response: R1, R5, R6, R7 */
31#define UNIPHIER_SD_CMD_RSP_R1B (5 << 8)/* response: R1b, R5b */
32#define UNIPHIER_SD_CMD_RSP_R2 (6 << 8)/* response: R2 */
33#define UNIPHIER_SD_CMD_RSP_R3 (7 << 8)/* response: R3, R4 */
34#define UNIPHIER_SD_ARG 0x008 /* command argument */
35#define UNIPHIER_SD_STOP 0x010 /* stop action control */
36#define UNIPHIER_SD_STOP_SEC BIT(8) /* use sector count */
37#define UNIPHIER_SD_STOP_STP BIT(0) /* issue CMD12 */
38#define UNIPHIER_SD_SECCNT 0x014 /* sector counter */
39#define UNIPHIER_SD_RSP10 0x018 /* response[39:8] */
40#define UNIPHIER_SD_RSP32 0x020 /* response[71:40] */
41#define UNIPHIER_SD_RSP54 0x028 /* response[103:72] */
42#define UNIPHIER_SD_RSP76 0x030 /* response[127:104] */
43#define UNIPHIER_SD_INFO1 0x038 /* IRQ status 1 */
44#define UNIPHIER_SD_INFO1_CD BIT(5) /* state of card detect */
45#define UNIPHIER_SD_INFO1_INSERT BIT(4) /* card inserted */
46#define UNIPHIER_SD_INFO1_REMOVE BIT(3) /* card removed */
47#define UNIPHIER_SD_INFO1_CMP BIT(2) /* data complete */
48#define UNIPHIER_SD_INFO1_RSP BIT(0) /* response complete */
49#define UNIPHIER_SD_INFO2 0x03c /* IRQ status 2 */
50#define UNIPHIER_SD_INFO2_ERR_ILA BIT(15) /* illegal access err */
51#define UNIPHIER_SD_INFO2_CBSY BIT(14) /* command busy */
52#define UNIPHIER_SD_INFO2_BWE BIT(9) /* write buffer ready */
53#define UNIPHIER_SD_INFO2_BRE BIT(8) /* read buffer ready */
54#define UNIPHIER_SD_INFO2_DAT0 BIT(7) /* SDDAT0 */
55#define UNIPHIER_SD_INFO2_ERR_RTO BIT(6) /* response time out */
56#define UNIPHIER_SD_INFO2_ERR_ILR BIT(5) /* illegal read err */
57#define UNIPHIER_SD_INFO2_ERR_ILW BIT(4) /* illegal write err */
58#define UNIPHIER_SD_INFO2_ERR_TO BIT(3) /* time out error */
59#define UNIPHIER_SD_INFO2_ERR_END BIT(2) /* END bit error */
60#define UNIPHIER_SD_INFO2_ERR_CRC BIT(1) /* CRC error */
61#define UNIPHIER_SD_INFO2_ERR_IDX BIT(0) /* cmd index error */
62#define UNIPHIER_SD_INFO1_MASK 0x040
63#define UNIPHIER_SD_INFO2_MASK 0x044
64#define UNIPHIER_SD_CLKCTL 0x048 /* clock divisor */
65#define UNIPHIER_SD_CLKCTL_DIV_MASK 0x104ff
66#define UNIPHIER_SD_CLKCTL_DIV1024 BIT(16) /* SDCLK = CLK / 1024 */
67#define UNIPHIER_SD_CLKCTL_DIV512 BIT(7) /* SDCLK = CLK / 512 */
68#define UNIPHIER_SD_CLKCTL_DIV256 BIT(6) /* SDCLK = CLK / 256 */
69#define UNIPHIER_SD_CLKCTL_DIV128 BIT(5) /* SDCLK = CLK / 128 */
70#define UNIPHIER_SD_CLKCTL_DIV64 BIT(4) /* SDCLK = CLK / 64 */
71#define UNIPHIER_SD_CLKCTL_DIV32 BIT(3) /* SDCLK = CLK / 32 */
72#define UNIPHIER_SD_CLKCTL_DIV16 BIT(2) /* SDCLK = CLK / 16 */
73#define UNIPHIER_SD_CLKCTL_DIV8 BIT(1) /* SDCLK = CLK / 8 */
74#define UNIPHIER_SD_CLKCTL_DIV4 BIT(0) /* SDCLK = CLK / 4 */
75#define UNIPHIER_SD_CLKCTL_DIV2 0 /* SDCLK = CLK / 2 */
76#define UNIPHIER_SD_CLKCTL_DIV1 BIT(10) /* SDCLK = CLK */
77#define UNIPHIER_SD_CLKCTL_OFFEN BIT(9) /* stop SDCLK when unused */
78#define UNIPHIER_SD_CLKCTL_SCLKEN BIT(8) /* SDCLK output enable */
79#define UNIPHIER_SD_SIZE 0x04c /* block size */
80#define UNIPHIER_SD_OPTION 0x050
81#define UNIPHIER_SD_OPTION_WIDTH_MASK (5 << 13)
82#define UNIPHIER_SD_OPTION_WIDTH_1 (4 << 13)
83#define UNIPHIER_SD_OPTION_WIDTH_4 (0 << 13)
84#define UNIPHIER_SD_OPTION_WIDTH_8 (1 << 13)
85#define UNIPHIER_SD_BUF 0x060 /* read/write buffer */
86#define UNIPHIER_SD_EXTMODE 0x1b0
87#define UNIPHIER_SD_EXTMODE_DMA_EN BIT(1) /* transfer 1: DMA, 0: pio */
88#define UNIPHIER_SD_SOFT_RST 0x1c0
89#define UNIPHIER_SD_SOFT_RST_RSTX BIT(0) /* reset deassert */
90#define UNIPHIER_SD_VERSION 0x1c4 /* version register */
91#define UNIPHIER_SD_VERSION_IP 0xff /* IP version */
92#define UNIPHIER_SD_HOST_MODE 0x1c8
93#define UNIPHIER_SD_IF_MODE 0x1cc
94#define UNIPHIER_SD_IF_MODE_DDR BIT(0) /* DDR mode */
95#define UNIPHIER_SD_VOLT 0x1e4 /* voltage switch */
96#define UNIPHIER_SD_VOLT_MASK (3 << 0)
97#define UNIPHIER_SD_VOLT_OFF (0 << 0)
98#define UNIPHIER_SD_VOLT_330 (1 << 0)/* 3.3V signal */
99#define UNIPHIER_SD_VOLT_180 (2 << 0)/* 1.8V signal */
100#define UNIPHIER_SD_DMA_MODE 0x410
101#define UNIPHIER_SD_DMA_MODE_DIR_RD BIT(16) /* 1: from device, 0: to dev */
102#define UNIPHIER_SD_DMA_MODE_ADDR_INC BIT(0) /* 1: address inc, 0: fixed */
103#define UNIPHIER_SD_DMA_CTL 0x414
104#define UNIPHIER_SD_DMA_CTL_START BIT(0) /* start DMA (auto cleared) */
105#define UNIPHIER_SD_DMA_RST 0x418
106#define UNIPHIER_SD_DMA_RST_RD BIT(9)
107#define UNIPHIER_SD_DMA_RST_WR BIT(8)
108#define UNIPHIER_SD_DMA_INFO1 0x420
109#define UNIPHIER_SD_DMA_INFO1_END_RD2 BIT(20) /* DMA from device is complete*/
110#define UNIPHIER_SD_DMA_INFO1_END_RD BIT(17) /* Don't use! Hardware bug */
111#define UNIPHIER_SD_DMA_INFO1_END_WR BIT(16) /* DMA to device is complete */
112#define UNIPHIER_SD_DMA_INFO1_MASK 0x424
113#define UNIPHIER_SD_DMA_INFO2 0x428
114#define UNIPHIER_SD_DMA_INFO2_ERR_RD BIT(17)
115#define UNIPHIER_SD_DMA_INFO2_ERR_WR BIT(16)
116#define UNIPHIER_SD_DMA_INFO2_MASK 0x42c
117#define UNIPHIER_SD_DMA_ADDR_L 0x440
118#define UNIPHIER_SD_DMA_ADDR_H 0x444
119
120/* alignment required by the DMA engine of this controller */
121#define UNIPHIER_SD_DMA_MINALIGN 0x10
122
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900123struct uniphier_sd_plat {
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900124 struct mmc_config cfg;
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900125 struct mmc mmc;
126};
127
128struct uniphier_sd_priv {
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900129 void __iomem *regbase;
130 unsigned long mclk;
131 unsigned int version;
132 u32 caps;
133#define UNIPHIER_SD_CAP_NONREMOVABLE BIT(0) /* Nonremovable e.g. eMMC */
134#define UNIPHIER_SD_CAP_DMA_INTERNAL BIT(1) /* have internal DMA engine */
135#define UNIPHIER_SD_CAP_DIV1024 BIT(2) /* divisor 1024 is available */
Marek Vasutf4edfd02017-07-21 23:24:33 +0200136#define UNIPHIER_SD_CAP_64BIT BIT(3) /* Controller is 64bit */
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900137};
138
Marek Vasut2f4b7a62017-09-26 20:05:34 +0200139static u64 uniphier_sd_readq(struct uniphier_sd_priv *priv, unsigned int reg)
Marek Vasutaf633892017-07-21 23:24:34 +0200140{
141 if (priv->caps & UNIPHIER_SD_CAP_64BIT)
142 return readq(priv->regbase + (reg << 1));
143 else
144 return readq(priv->regbase + reg);
145}
146
147static void uniphier_sd_writeq(struct uniphier_sd_priv *priv,
Marek Vasut2f4b7a62017-09-26 20:05:34 +0200148 u64 val, unsigned int reg)
Marek Vasutaf633892017-07-21 23:24:34 +0200149{
150 if (priv->caps & UNIPHIER_SD_CAP_64BIT)
151 writeq(val, priv->regbase + (reg << 1));
152 else
153 writeq(val, priv->regbase + reg);
154}
155
Marek Vasut2f4b7a62017-09-26 20:05:34 +0200156static u32 uniphier_sd_readl(struct uniphier_sd_priv *priv, unsigned int reg)
Marek Vasuta965bba2017-07-21 23:24:32 +0200157{
Marek Vasutf4edfd02017-07-21 23:24:33 +0200158 if (priv->caps & UNIPHIER_SD_CAP_64BIT)
159 return readl(priv->regbase + (reg << 1));
160 else
161 return readl(priv->regbase + reg);
Marek Vasuta965bba2017-07-21 23:24:32 +0200162}
163
164static void uniphier_sd_writel(struct uniphier_sd_priv *priv,
Marek Vasut2f4b7a62017-09-26 20:05:34 +0200165 u32 val, unsigned int reg)
Marek Vasuta965bba2017-07-21 23:24:32 +0200166{
Marek Vasutf4edfd02017-07-21 23:24:33 +0200167 if (priv->caps & UNIPHIER_SD_CAP_64BIT)
168 writel(val, priv->regbase + (reg << 1));
169 else
170 writel(val, priv->regbase + reg);
Marek Vasuta965bba2017-07-21 23:24:32 +0200171}
172
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900173static dma_addr_t __dma_map_single(void *ptr, size_t size,
174 enum dma_data_direction dir)
175{
176 unsigned long addr = (unsigned long)ptr;
177
178 if (dir == DMA_FROM_DEVICE)
179 invalidate_dcache_range(addr, addr + size);
180 else
181 flush_dcache_range(addr, addr + size);
182
183 return addr;
184}
185
186static void __dma_unmap_single(dma_addr_t addr, size_t size,
187 enum dma_data_direction dir)
188{
189 if (dir != DMA_TO_DEVICE)
190 invalidate_dcache_range(addr, addr + size);
191}
192
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900193static int uniphier_sd_check_error(struct udevice *dev)
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900194{
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900195 struct uniphier_sd_priv *priv = dev_get_priv(dev);
Marek Vasuta965bba2017-07-21 23:24:32 +0200196 u32 info2 = uniphier_sd_readl(priv, UNIPHIER_SD_INFO2);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900197
198 if (info2 & UNIPHIER_SD_INFO2_ERR_RTO) {
199 /*
200 * TIMEOUT must be returned for unsupported command. Do not
201 * display error log since this might be a part of sequence to
202 * distinguish between SD and MMC.
203 */
Jaehoon Chung7825d202016-07-19 16:33:36 +0900204 return -ETIMEDOUT;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900205 }
206
207 if (info2 & UNIPHIER_SD_INFO2_ERR_TO) {
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900208 dev_err(dev, "timeout error\n");
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900209 return -ETIMEDOUT;
210 }
211
212 if (info2 & (UNIPHIER_SD_INFO2_ERR_END | UNIPHIER_SD_INFO2_ERR_CRC |
213 UNIPHIER_SD_INFO2_ERR_IDX)) {
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900214 dev_err(dev, "communication out of sync\n");
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900215 return -EILSEQ;
216 }
217
218 if (info2 & (UNIPHIER_SD_INFO2_ERR_ILA | UNIPHIER_SD_INFO2_ERR_ILR |
219 UNIPHIER_SD_INFO2_ERR_ILW)) {
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900220 dev_err(dev, "illegal access\n");
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900221 return -EIO;
222 }
223
224 return 0;
225}
226
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900227static int uniphier_sd_wait_for_irq(struct udevice *dev, unsigned int reg,
228 u32 flag)
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900229{
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900230 struct uniphier_sd_priv *priv = dev_get_priv(dev);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900231 long wait = 1000000;
232 int ret;
233
Marek Vasuta965bba2017-07-21 23:24:32 +0200234 while (!(uniphier_sd_readl(priv, reg) & flag)) {
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900235 if (wait-- < 0) {
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900236 dev_err(dev, "timeout\n");
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900237 return -ETIMEDOUT;
238 }
239
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900240 ret = uniphier_sd_check_error(dev);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900241 if (ret)
242 return ret;
243
244 udelay(1);
245 }
246
247 return 0;
248}
249
Marek Vasut2f4b7a62017-09-26 20:05:34 +0200250static int uniphier_sd_pio_read_one_block(struct udevice *dev, char *pbuf,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900251 uint blocksize)
252{
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900253 struct uniphier_sd_priv *priv = dev_get_priv(dev);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900254 int i, ret;
255
256 /* wait until the buffer is filled with data */
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900257 ret = uniphier_sd_wait_for_irq(dev, UNIPHIER_SD_INFO2,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900258 UNIPHIER_SD_INFO2_BRE);
259 if (ret)
260 return ret;
261
262 /*
263 * Clear the status flag _before_ read the buffer out because
264 * UNIPHIER_SD_INFO2_BRE is edge-triggered, not level-triggered.
265 */
Marek Vasuta965bba2017-07-21 23:24:32 +0200266 uniphier_sd_writel(priv, 0, UNIPHIER_SD_INFO2);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900267
Marek Vasut2f4b7a62017-09-26 20:05:34 +0200268 if (priv->caps & UNIPHIER_SD_CAP_64BIT) {
269 u64 *buf = (u64 *)pbuf;
270 if (likely(IS_ALIGNED((uintptr_t)buf, 8))) {
Marek Vasutaf633892017-07-21 23:24:34 +0200271 for (i = 0; i < blocksize / 8; i++) {
Marek Vasut2f4b7a62017-09-26 20:05:34 +0200272 *buf++ = uniphier_sd_readq(priv,
273 UNIPHIER_SD_BUF);
Marek Vasutaf633892017-07-21 23:24:34 +0200274 }
275 } else {
Marek Vasutaf633892017-07-21 23:24:34 +0200276 for (i = 0; i < blocksize / 8; i++) {
277 u64 data;
278 data = uniphier_sd_readq(priv,
279 UNIPHIER_SD_BUF);
Marek Vasut2f4b7a62017-09-26 20:05:34 +0200280 put_unaligned(data, buf++);
281 }
282 }
283 } else {
284 u32 *buf = (u32 *)pbuf;
285 if (likely(IS_ALIGNED((uintptr_t)buf, 4))) {
286 for (i = 0; i < blocksize / 4; i++) {
287 *buf++ = uniphier_sd_readl(priv,
288 UNIPHIER_SD_BUF);
Marek Vasutaf633892017-07-21 23:24:34 +0200289 }
290 } else {
291 for (i = 0; i < blocksize / 4; i++) {
292 u32 data;
293 data = uniphier_sd_readl(priv, UNIPHIER_SD_BUF);
Marek Vasut2f4b7a62017-09-26 20:05:34 +0200294 put_unaligned(data, buf++);
Marek Vasutaf633892017-07-21 23:24:34 +0200295 }
296 }
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900297 }
298
299 return 0;
300}
301
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900302static int uniphier_sd_pio_write_one_block(struct udevice *dev,
Marek Vasut2f4b7a62017-09-26 20:05:34 +0200303 const char *pbuf, uint blocksize)
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900304{
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900305 struct uniphier_sd_priv *priv = dev_get_priv(dev);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900306 int i, ret;
307
308 /* wait until the buffer becomes empty */
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900309 ret = uniphier_sd_wait_for_irq(dev, UNIPHIER_SD_INFO2,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900310 UNIPHIER_SD_INFO2_BWE);
311 if (ret)
312 return ret;
313
Marek Vasuta965bba2017-07-21 23:24:32 +0200314 uniphier_sd_writel(priv, 0, UNIPHIER_SD_INFO2);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900315
Marek Vasut2f4b7a62017-09-26 20:05:34 +0200316 if (priv->caps & UNIPHIER_SD_CAP_64BIT) {
317 const u64 *buf = (const u64 *)pbuf;
318 if (likely(IS_ALIGNED((uintptr_t)buf, 8))) {
Marek Vasutaf633892017-07-21 23:24:34 +0200319 for (i = 0; i < blocksize / 8; i++) {
Marek Vasut2f4b7a62017-09-26 20:05:34 +0200320 uniphier_sd_writeq(priv, *buf++,
Marek Vasutaf633892017-07-21 23:24:34 +0200321 UNIPHIER_SD_BUF);
322 }
323 } else {
Marek Vasut2f4b7a62017-09-26 20:05:34 +0200324 for (i = 0; i < blocksize / 8; i++) {
325 u64 data = get_unaligned(buf++);
326 uniphier_sd_writeq(priv, data,
Marek Vasutaf633892017-07-21 23:24:34 +0200327 UNIPHIER_SD_BUF);
328 }
329 }
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900330 } else {
Marek Vasut2f4b7a62017-09-26 20:05:34 +0200331 const u32 *buf = (const u32 *)pbuf;
332 if (likely(IS_ALIGNED((uintptr_t)buf, 4))) {
333 for (i = 0; i < blocksize / 4; i++) {
334 uniphier_sd_writel(priv, *buf++,
Marek Vasutaf633892017-07-21 23:24:34 +0200335 UNIPHIER_SD_BUF);
336 }
337 } else {
338 for (i = 0; i < blocksize / 4; i++) {
Marek Vasut2f4b7a62017-09-26 20:05:34 +0200339 u32 data = get_unaligned(buf++);
Marek Vasutaf633892017-07-21 23:24:34 +0200340 uniphier_sd_writel(priv, data,
341 UNIPHIER_SD_BUF);
342 }
343 }
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900344 }
345
346 return 0;
347}
348
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900349static int uniphier_sd_pio_xfer(struct udevice *dev, struct mmc_data *data)
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900350{
Marek Vasut2f4b7a62017-09-26 20:05:34 +0200351 const char *src = data->src;
352 char *dest = data->dest;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900353 int i, ret;
354
355 for (i = 0; i < data->blocks; i++) {
356 if (data->flags & MMC_DATA_READ)
Marek Vasut2f4b7a62017-09-26 20:05:34 +0200357 ret = uniphier_sd_pio_read_one_block(dev, dest,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900358 data->blocksize);
359 else
Marek Vasut2f4b7a62017-09-26 20:05:34 +0200360 ret = uniphier_sd_pio_write_one_block(dev, src,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900361 data->blocksize);
362 if (ret)
363 return ret;
Marek Vasut2f4b7a62017-09-26 20:05:34 +0200364
365 if (data->flags & MMC_DATA_READ)
366 dest += data->blocksize;
367 else
368 src += data->blocksize;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900369 }
370
371 return 0;
372}
373
374static void uniphier_sd_dma_start(struct uniphier_sd_priv *priv,
375 dma_addr_t dma_addr)
376{
377 u32 tmp;
378
Marek Vasuta965bba2017-07-21 23:24:32 +0200379 uniphier_sd_writel(priv, 0, UNIPHIER_SD_DMA_INFO1);
380 uniphier_sd_writel(priv, 0, UNIPHIER_SD_DMA_INFO2);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900381
382 /* enable DMA */
Marek Vasuta965bba2017-07-21 23:24:32 +0200383 tmp = uniphier_sd_readl(priv, UNIPHIER_SD_EXTMODE);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900384 tmp |= UNIPHIER_SD_EXTMODE_DMA_EN;
Marek Vasuta965bba2017-07-21 23:24:32 +0200385 uniphier_sd_writel(priv, tmp, UNIPHIER_SD_EXTMODE);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900386
Marek Vasuta965bba2017-07-21 23:24:32 +0200387 uniphier_sd_writel(priv, dma_addr & U32_MAX, UNIPHIER_SD_DMA_ADDR_L);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900388
389 /* suppress the warning "right shift count >= width of type" */
390 dma_addr >>= min_t(int, 32, 8 * sizeof(dma_addr));
391
Marek Vasuta965bba2017-07-21 23:24:32 +0200392 uniphier_sd_writel(priv, dma_addr & U32_MAX, UNIPHIER_SD_DMA_ADDR_H);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900393
Marek Vasuta965bba2017-07-21 23:24:32 +0200394 uniphier_sd_writel(priv, UNIPHIER_SD_DMA_CTL_START, UNIPHIER_SD_DMA_CTL);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900395}
396
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900397static int uniphier_sd_dma_wait_for_irq(struct udevice *dev, u32 flag,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900398 unsigned int blocks)
399{
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900400 struct uniphier_sd_priv *priv = dev_get_priv(dev);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900401 long wait = 1000000 + 10 * blocks;
402
Marek Vasuta965bba2017-07-21 23:24:32 +0200403 while (!(uniphier_sd_readl(priv, UNIPHIER_SD_DMA_INFO1) & flag)) {
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900404 if (wait-- < 0) {
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900405 dev_err(dev, "timeout during DMA\n");
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900406 return -ETIMEDOUT;
407 }
408
409 udelay(10);
410 }
411
Marek Vasuta965bba2017-07-21 23:24:32 +0200412 if (uniphier_sd_readl(priv, UNIPHIER_SD_DMA_INFO2)) {
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900413 dev_err(dev, "error during DMA\n");
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900414 return -EIO;
415 }
416
417 return 0;
418}
419
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900420static int uniphier_sd_dma_xfer(struct udevice *dev, struct mmc_data *data)
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900421{
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900422 struct uniphier_sd_priv *priv = dev_get_priv(dev);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900423 size_t len = data->blocks * data->blocksize;
424 void *buf;
425 enum dma_data_direction dir;
426 dma_addr_t dma_addr;
427 u32 poll_flag, tmp;
428 int ret;
429
Marek Vasuta965bba2017-07-21 23:24:32 +0200430 tmp = uniphier_sd_readl(priv, UNIPHIER_SD_DMA_MODE);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900431
432 if (data->flags & MMC_DATA_READ) {
433 buf = data->dest;
434 dir = DMA_FROM_DEVICE;
435 poll_flag = UNIPHIER_SD_DMA_INFO1_END_RD2;
436 tmp |= UNIPHIER_SD_DMA_MODE_DIR_RD;
437 } else {
438 buf = (void *)data->src;
439 dir = DMA_TO_DEVICE;
440 poll_flag = UNIPHIER_SD_DMA_INFO1_END_WR;
441 tmp &= ~UNIPHIER_SD_DMA_MODE_DIR_RD;
442 }
443
Marek Vasuta965bba2017-07-21 23:24:32 +0200444 uniphier_sd_writel(priv, tmp, UNIPHIER_SD_DMA_MODE);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900445
446 dma_addr = __dma_map_single(buf, len, dir);
447
448 uniphier_sd_dma_start(priv, dma_addr);
449
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900450 ret = uniphier_sd_dma_wait_for_irq(dev, poll_flag, data->blocks);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900451
452 __dma_unmap_single(dma_addr, len, dir);
453
454 return ret;
455}
456
457/* check if the address is DMA'able */
458static bool uniphier_sd_addr_is_dmaable(unsigned long addr)
459{
460 if (!IS_ALIGNED(addr, UNIPHIER_SD_DMA_MINALIGN))
461 return false;
462
463#if defined(CONFIG_ARCH_UNIPHIER) && !defined(CONFIG_ARM64) && \
464 defined(CONFIG_SPL_BUILD)
465 /*
466 * For UniPhier ARMv7 SoCs, the stack is allocated in the locked ways
467 * of L2, which is unreachable from the DMA engine.
468 */
469 if (addr < CONFIG_SPL_STACK)
470 return false;
471#endif
472
473 return true;
474}
475
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900476static int uniphier_sd_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900477 struct mmc_data *data)
478{
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900479 struct uniphier_sd_priv *priv = dev_get_priv(dev);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900480 int ret;
481 u32 tmp;
482
Marek Vasuta965bba2017-07-21 23:24:32 +0200483 if (uniphier_sd_readl(priv, UNIPHIER_SD_INFO2) & UNIPHIER_SD_INFO2_CBSY) {
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900484 dev_err(dev, "command busy\n");
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900485 return -EBUSY;
486 }
487
488 /* clear all status flags */
Marek Vasuta965bba2017-07-21 23:24:32 +0200489 uniphier_sd_writel(priv, 0, UNIPHIER_SD_INFO1);
490 uniphier_sd_writel(priv, 0, UNIPHIER_SD_INFO2);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900491
492 /* disable DMA once */
Marek Vasuta965bba2017-07-21 23:24:32 +0200493 tmp = uniphier_sd_readl(priv, UNIPHIER_SD_EXTMODE);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900494 tmp &= ~UNIPHIER_SD_EXTMODE_DMA_EN;
Marek Vasuta965bba2017-07-21 23:24:32 +0200495 uniphier_sd_writel(priv, tmp, UNIPHIER_SD_EXTMODE);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900496
Marek Vasuta965bba2017-07-21 23:24:32 +0200497 uniphier_sd_writel(priv, cmd->cmdarg, UNIPHIER_SD_ARG);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900498
499 tmp = cmd->cmdidx;
500
501 if (data) {
Marek Vasuta965bba2017-07-21 23:24:32 +0200502 uniphier_sd_writel(priv, data->blocksize, UNIPHIER_SD_SIZE);
503 uniphier_sd_writel(priv, data->blocks, UNIPHIER_SD_SECCNT);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900504
505 /* Do not send CMD12 automatically */
506 tmp |= UNIPHIER_SD_CMD_NOSTOP | UNIPHIER_SD_CMD_DATA;
507
508 if (data->blocks > 1)
509 tmp |= UNIPHIER_SD_CMD_MULTI;
510
511 if (data->flags & MMC_DATA_READ)
512 tmp |= UNIPHIER_SD_CMD_RD;
513 }
514
515 /*
516 * Do not use the response type auto-detection on this hardware.
517 * CMD8, for example, has different response types on SD and eMMC,
518 * while this controller always assumes the response type for SD.
519 * Set the response type manually.
520 */
521 switch (cmd->resp_type) {
522 case MMC_RSP_NONE:
523 tmp |= UNIPHIER_SD_CMD_RSP_NONE;
524 break;
525 case MMC_RSP_R1:
526 tmp |= UNIPHIER_SD_CMD_RSP_R1;
527 break;
528 case MMC_RSP_R1b:
529 tmp |= UNIPHIER_SD_CMD_RSP_R1B;
530 break;
531 case MMC_RSP_R2:
532 tmp |= UNIPHIER_SD_CMD_RSP_R2;
533 break;
534 case MMC_RSP_R3:
535 tmp |= UNIPHIER_SD_CMD_RSP_R3;
536 break;
537 default:
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900538 dev_err(dev, "unknown response type\n");
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900539 return -EINVAL;
540 }
541
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900542 dev_dbg(dev, "sending CMD%d (SD_CMD=%08x, SD_ARG=%08x)\n",
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900543 cmd->cmdidx, tmp, cmd->cmdarg);
Marek Vasuta965bba2017-07-21 23:24:32 +0200544 uniphier_sd_writel(priv, tmp, UNIPHIER_SD_CMD);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900545
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900546 ret = uniphier_sd_wait_for_irq(dev, UNIPHIER_SD_INFO1,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900547 UNIPHIER_SD_INFO1_RSP);
548 if (ret)
549 return ret;
550
551 if (cmd->resp_type & MMC_RSP_136) {
Marek Vasuta965bba2017-07-21 23:24:32 +0200552 u32 rsp_127_104 = uniphier_sd_readl(priv, UNIPHIER_SD_RSP76);
553 u32 rsp_103_72 = uniphier_sd_readl(priv, UNIPHIER_SD_RSP54);
554 u32 rsp_71_40 = uniphier_sd_readl(priv, UNIPHIER_SD_RSP32);
555 u32 rsp_39_8 = uniphier_sd_readl(priv, UNIPHIER_SD_RSP10);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900556
Marek Vasut9443f422017-07-21 23:11:59 +0200557 cmd->response[0] = ((rsp_127_104 & 0x00ffffff) << 8) |
558 ((rsp_103_72 & 0xff000000) >> 24);
559 cmd->response[1] = ((rsp_103_72 & 0x00ffffff) << 8) |
560 ((rsp_71_40 & 0xff000000) >> 24);
561 cmd->response[2] = ((rsp_71_40 & 0x00ffffff) << 8) |
562 ((rsp_39_8 & 0xff000000) >> 24);
563 cmd->response[3] = (rsp_39_8 & 0xffffff) << 8;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900564 } else {
565 /* bit 39-8 */
Marek Vasuta965bba2017-07-21 23:24:32 +0200566 cmd->response[0] = uniphier_sd_readl(priv, UNIPHIER_SD_RSP10);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900567 }
568
569 if (data) {
570 /* use DMA if the HW supports it and the buffer is aligned */
571 if (priv->caps & UNIPHIER_SD_CAP_DMA_INTERNAL &&
572 uniphier_sd_addr_is_dmaable((long)data->src))
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900573 ret = uniphier_sd_dma_xfer(dev, data);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900574 else
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900575 ret = uniphier_sd_pio_xfer(dev, data);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900576
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900577 ret = uniphier_sd_wait_for_irq(dev, UNIPHIER_SD_INFO1,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900578 UNIPHIER_SD_INFO1_CMP);
579 if (ret)
580 return ret;
581 }
582
583 return ret;
584}
585
Masahiro Yamada02051482016-08-25 14:52:37 +0900586static int uniphier_sd_set_bus_width(struct uniphier_sd_priv *priv,
587 struct mmc *mmc)
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900588{
589 u32 val, tmp;
590
591 switch (mmc->bus_width) {
592 case 1:
593 val = UNIPHIER_SD_OPTION_WIDTH_1;
594 break;
595 case 4:
596 val = UNIPHIER_SD_OPTION_WIDTH_4;
597 break;
598 case 8:
599 val = UNIPHIER_SD_OPTION_WIDTH_8;
600 break;
601 default:
Masahiro Yamada02051482016-08-25 14:52:37 +0900602 return -EINVAL;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900603 }
604
Marek Vasuta965bba2017-07-21 23:24:32 +0200605 tmp = uniphier_sd_readl(priv, UNIPHIER_SD_OPTION);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900606 tmp &= ~UNIPHIER_SD_OPTION_WIDTH_MASK;
607 tmp |= val;
Marek Vasuta965bba2017-07-21 23:24:32 +0200608 uniphier_sd_writel(priv, tmp, UNIPHIER_SD_OPTION);
Masahiro Yamada02051482016-08-25 14:52:37 +0900609
610 return 0;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900611}
612
613static void uniphier_sd_set_ddr_mode(struct uniphier_sd_priv *priv,
614 struct mmc *mmc)
615{
616 u32 tmp;
617
Marek Vasuta965bba2017-07-21 23:24:32 +0200618 tmp = uniphier_sd_readl(priv, UNIPHIER_SD_IF_MODE);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900619 if (mmc->ddr_mode)
620 tmp |= UNIPHIER_SD_IF_MODE_DDR;
621 else
622 tmp &= ~UNIPHIER_SD_IF_MODE_DDR;
Marek Vasuta965bba2017-07-21 23:24:32 +0200623 uniphier_sd_writel(priv, tmp, UNIPHIER_SD_IF_MODE);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900624}
625
626static void uniphier_sd_set_clk_rate(struct uniphier_sd_priv *priv,
627 struct mmc *mmc)
628{
629 unsigned int divisor;
630 u32 val, tmp;
631
632 if (!mmc->clock)
633 return;
634
635 divisor = DIV_ROUND_UP(priv->mclk, mmc->clock);
636
637 if (divisor <= 1)
638 val = UNIPHIER_SD_CLKCTL_DIV1;
639 else if (divisor <= 2)
640 val = UNIPHIER_SD_CLKCTL_DIV2;
641 else if (divisor <= 4)
642 val = UNIPHIER_SD_CLKCTL_DIV4;
643 else if (divisor <= 8)
644 val = UNIPHIER_SD_CLKCTL_DIV8;
645 else if (divisor <= 16)
646 val = UNIPHIER_SD_CLKCTL_DIV16;
647 else if (divisor <= 32)
648 val = UNIPHIER_SD_CLKCTL_DIV32;
649 else if (divisor <= 64)
650 val = UNIPHIER_SD_CLKCTL_DIV64;
651 else if (divisor <= 128)
652 val = UNIPHIER_SD_CLKCTL_DIV128;
653 else if (divisor <= 256)
654 val = UNIPHIER_SD_CLKCTL_DIV256;
655 else if (divisor <= 512 || !(priv->caps & UNIPHIER_SD_CAP_DIV1024))
656 val = UNIPHIER_SD_CLKCTL_DIV512;
657 else
658 val = UNIPHIER_SD_CLKCTL_DIV1024;
659
Marek Vasuta965bba2017-07-21 23:24:32 +0200660 tmp = uniphier_sd_readl(priv, UNIPHIER_SD_CLKCTL);
Masahiro Yamada25455532016-08-25 14:52:38 +0900661 if (tmp & UNIPHIER_SD_CLKCTL_SCLKEN &&
662 (tmp & UNIPHIER_SD_CLKCTL_DIV_MASK) == val)
663 return;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900664
665 /* stop the clock before changing its rate to avoid a glitch signal */
666 tmp &= ~UNIPHIER_SD_CLKCTL_SCLKEN;
Marek Vasuta965bba2017-07-21 23:24:32 +0200667 uniphier_sd_writel(priv, tmp, UNIPHIER_SD_CLKCTL);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900668
669 tmp &= ~UNIPHIER_SD_CLKCTL_DIV_MASK;
670 tmp |= val | UNIPHIER_SD_CLKCTL_OFFEN;
Marek Vasuta965bba2017-07-21 23:24:32 +0200671 uniphier_sd_writel(priv, tmp, UNIPHIER_SD_CLKCTL);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900672
673 tmp |= UNIPHIER_SD_CLKCTL_SCLKEN;
Marek Vasuta965bba2017-07-21 23:24:32 +0200674 uniphier_sd_writel(priv, tmp, UNIPHIER_SD_CLKCTL);
Masahiro Yamada25455532016-08-25 14:52:38 +0900675
676 udelay(1000);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900677}
678
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900679static int uniphier_sd_set_ios(struct udevice *dev)
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900680{
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900681 struct uniphier_sd_priv *priv = dev_get_priv(dev);
682 struct mmc *mmc = mmc_get_mmc_dev(dev);
Masahiro Yamada02051482016-08-25 14:52:37 +0900683 int ret;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900684
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900685 dev_dbg(dev, "clock %uHz, DDRmode %d, width %u\n",
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900686 mmc->clock, mmc->ddr_mode, mmc->bus_width);
687
Masahiro Yamada02051482016-08-25 14:52:37 +0900688 ret = uniphier_sd_set_bus_width(priv, mmc);
689 if (ret)
690 return ret;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900691 uniphier_sd_set_ddr_mode(priv, mmc);
692 uniphier_sd_set_clk_rate(priv, mmc);
693
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900694 return 0;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900695}
696
Masahiro Yamadab29afb92016-08-25 14:52:36 +0900697static int uniphier_sd_get_cd(struct udevice *dev)
698{
699 struct uniphier_sd_priv *priv = dev_get_priv(dev);
700
701 if (priv->caps & UNIPHIER_SD_CAP_NONREMOVABLE)
702 return 1;
703
Marek Vasuta965bba2017-07-21 23:24:32 +0200704 return !!(uniphier_sd_readl(priv, UNIPHIER_SD_INFO1) &
Masahiro Yamadab29afb92016-08-25 14:52:36 +0900705 UNIPHIER_SD_INFO1_CD);
706}
707
708static const struct dm_mmc_ops uniphier_sd_ops = {
709 .send_cmd = uniphier_sd_send_cmd,
710 .set_ios = uniphier_sd_set_ios,
711 .get_cd = uniphier_sd_get_cd,
712};
713
714static void uniphier_sd_host_init(struct uniphier_sd_priv *priv)
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900715{
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900716 u32 tmp;
717
718 /* soft reset of the host */
Marek Vasuta965bba2017-07-21 23:24:32 +0200719 tmp = uniphier_sd_readl(priv, UNIPHIER_SD_SOFT_RST);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900720 tmp &= ~UNIPHIER_SD_SOFT_RST_RSTX;
Marek Vasuta965bba2017-07-21 23:24:32 +0200721 uniphier_sd_writel(priv, tmp, UNIPHIER_SD_SOFT_RST);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900722 tmp |= UNIPHIER_SD_SOFT_RST_RSTX;
Marek Vasuta965bba2017-07-21 23:24:32 +0200723 uniphier_sd_writel(priv, tmp, UNIPHIER_SD_SOFT_RST);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900724
725 /* FIXME: implement eMMC hw_reset */
726
Marek Vasuta965bba2017-07-21 23:24:32 +0200727 uniphier_sd_writel(priv, UNIPHIER_SD_STOP_SEC, UNIPHIER_SD_STOP);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900728
729 /*
730 * Connected to 32bit AXI.
731 * This register dropped backward compatibility at version 0x10.
732 * Write an appropriate value depending on the IP version.
733 */
Marek Vasuta965bba2017-07-21 23:24:32 +0200734 uniphier_sd_writel(priv, priv->version >= 0x10 ? 0x00000101 : 0x00000000,
735 UNIPHIER_SD_HOST_MODE);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900736
737 if (priv->caps & UNIPHIER_SD_CAP_DMA_INTERNAL) {
Marek Vasuta965bba2017-07-21 23:24:32 +0200738 tmp = uniphier_sd_readl(priv, UNIPHIER_SD_DMA_MODE);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900739 tmp |= UNIPHIER_SD_DMA_MODE_ADDR_INC;
Marek Vasuta965bba2017-07-21 23:24:32 +0200740 uniphier_sd_writel(priv, tmp, UNIPHIER_SD_DMA_MODE);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900741 }
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900742}
743
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900744static int uniphier_sd_bind(struct udevice *dev)
745{
746 struct uniphier_sd_plat *plat = dev_get_platdata(dev);
747
748 return mmc_bind(dev, &plat->mmc, &plat->cfg);
749}
750
Masahiro Yamadafe5428d2016-08-12 19:19:03 +0900751static int uniphier_sd_probe(struct udevice *dev)
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900752{
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900753 struct uniphier_sd_plat *plat = dev_get_platdata(dev);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900754 struct uniphier_sd_priv *priv = dev_get_priv(dev);
755 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Marek Vasutcd36d412017-07-21 23:24:35 +0200756 const u32 quirks = dev_get_driver_data(dev);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900757 fdt_addr_t base;
Stephen Warrena9622432016-06-17 09:44:00 -0600758 struct clk clk;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900759 int ret;
Marek Vasut10f0c3c2017-09-15 21:10:54 +0200760#ifdef CONFIG_DM_REGULATOR
761 struct udevice *vqmmc_dev;
762#endif
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900763
Simon Glassba1dea42017-05-17 17:18:05 -0600764 base = devfdt_get_addr(dev);
Masahiro Yamadadc158ef2016-03-24 22:32:42 +0900765 if (base == FDT_ADDR_T_NONE)
766 return -EINVAL;
767
Masahiro Yamadafa1f73f2016-07-19 21:56:13 +0900768 priv->regbase = devm_ioremap(dev, base, SZ_2K);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900769 if (!priv->regbase)
770 return -ENOMEM;
771
Marek Vasut10f0c3c2017-09-15 21:10:54 +0200772#ifdef CONFIG_DM_REGULATOR
773 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
774 if (!ret) {
775 /* Set the regulator to 3.3V until we support 1.8V modes */
776 regulator_set_value(vqmmc_dev, 3300000);
777 regulator_set_enable(vqmmc_dev, true);
778 }
779#endif
780
Stephen Warrena9622432016-06-17 09:44:00 -0600781 ret = clk_get_by_index(dev, 0, &clk);
782 if (ret < 0) {
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900783 dev_err(dev, "failed to get host clock\n");
Stephen Warrena9622432016-06-17 09:44:00 -0600784 return ret;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900785 }
786
787 /* set to max rate */
Stephen Warrena9622432016-06-17 09:44:00 -0600788 priv->mclk = clk_set_rate(&clk, ULONG_MAX);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900789 if (IS_ERR_VALUE(priv->mclk)) {
790 dev_err(dev, "failed to set rate for host clock\n");
Stephen Warrena9622432016-06-17 09:44:00 -0600791 clk_free(&clk);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900792 return priv->mclk;
793 }
794
Stephen Warrena9622432016-06-17 09:44:00 -0600795 ret = clk_enable(&clk);
796 clk_free(&clk);
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900797 if (ret) {
798 dev_err(dev, "failed to enable host clock\n");
799 return ret;
800 }
801
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900802 plat->cfg.name = dev->name;
803 plat->cfg.host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900804
Simon Glassdd79d6e2017-01-17 16:52:55 -0700805 switch (fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "bus-width",
806 1)) {
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900807 case 8:
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900808 plat->cfg.host_caps |= MMC_MODE_8BIT;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900809 break;
810 case 4:
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900811 plat->cfg.host_caps |= MMC_MODE_4BIT;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900812 break;
813 case 1:
814 break;
815 default:
816 dev_err(dev, "Invalid \"bus-width\" value\n");
817 return -EINVAL;
818 }
819
Marek Vasutcd36d412017-07-21 23:24:35 +0200820 if (quirks) {
821 priv->caps = quirks;
822 } else {
823 priv->version = uniphier_sd_readl(priv, UNIPHIER_SD_VERSION) &
824 UNIPHIER_SD_VERSION_IP;
825 dev_dbg(dev, "version %x\n", priv->version);
826 if (priv->version >= 0x10) {
827 priv->caps |= UNIPHIER_SD_CAP_DMA_INTERNAL;
828 priv->caps |= UNIPHIER_SD_CAP_DIV1024;
829 }
830 }
831
Simon Glassdd79d6e2017-01-17 16:52:55 -0700832 if (fdt_get_property(gd->fdt_blob, dev_of_offset(dev), "non-removable",
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900833 NULL))
834 priv->caps |= UNIPHIER_SD_CAP_NONREMOVABLE;
835
Masahiro Yamadab29afb92016-08-25 14:52:36 +0900836 uniphier_sd_host_init(priv);
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900837
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900838 plat->cfg.voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
839 plat->cfg.f_min = priv->mclk /
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900840 (priv->caps & UNIPHIER_SD_CAP_DIV1024 ? 1024 : 512);
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900841 plat->cfg.f_max = priv->mclk;
842 plat->cfg.b_max = U32_MAX; /* max value of UNIPHIER_SD_SECCNT */
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900843
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900844 upriv->mmc = &plat->mmc;
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900845
846 return 0;
847}
848
849static const struct udevice_id uniphier_sd_match[] = {
Marek Vasut847ee0c2017-07-21 23:24:36 +0200850 { .compatible = "renesas,sdhi-r8a7795", .data = UNIPHIER_SD_CAP_64BIT },
851 { .compatible = "renesas,sdhi-r8a7796", .data = UNIPHIER_SD_CAP_64BIT },
Marek Vasutcd36d412017-07-21 23:24:35 +0200852 { .compatible = "socionext,uniphier-sdhc", .data = 0 },
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900853 { /* sentinel */ }
854};
855
856U_BOOT_DRIVER(uniphier_mmc) = {
857 .name = "uniphier-mmc",
858 .id = UCLASS_MMC,
859 .of_match = uniphier_sd_match,
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900860 .bind = uniphier_sd_bind,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900861 .probe = uniphier_sd_probe,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900862 .priv_auto_alloc_size = sizeof(struct uniphier_sd_priv),
Masahiro Yamadae67eac02016-09-14 01:06:03 +0900863 .platdata_auto_alloc_size = sizeof(struct uniphier_sd_plat),
Masahiro Yamadab26daaa2016-08-25 14:52:35 +0900864 .ops = &uniphier_sd_ops,
Masahiro Yamadaa0cfcc02016-02-18 19:52:48 +0900865};