blob: 4851198571c561cff44caaa2443ece3b60abc406 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +09002/*
3 * drivers/mmc/sh_sdhi.c
4 *
5 * SD/MMC driver for Renesas rmobile ARM SoCs.
6 *
Kouei Abe5eada1d2017-05-13 15:51:16 +02007 * Copyright (C) 2011,2013-2017 Renesas Electronics Corporation
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +09008 * Copyright (C) 2014 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
9 * Copyright (C) 2008-2009 Renesas Solutions Corp.
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +090010 */
11
12#include <common.h>
13#include <malloc.h>
14#include <mmc.h>
Marek Vasutd1c18ca2017-07-21 23:22:54 +020015#include <dm.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060016#include <part.h>
Simon Glass336d4612020-02-03 07:36:16 -070017#include <dm/device_compat.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090018#include <linux/errno.h>
Marek Vasutd1c18ca2017-07-21 23:22:54 +020019#include <linux/compat.h>
20#include <linux/io.h>
21#include <linux/sizes.h>
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +090022#include <asm/arch/rmobile.h>
23#include <asm/arch/sh_sdhi.h>
Marek Vasut8cd46cb2017-07-21 23:22:56 +020024#include <clk.h>
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +090025
26#define DRIVER_NAME "sh-sdhi"
27
28struct sh_sdhi_host {
Marek Vasutd1c18ca2017-07-21 23:22:54 +020029 void __iomem *addr;
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +090030 int ch;
31 int bus_shift;
32 unsigned long quirks;
33 unsigned char wait_int;
34 unsigned char sd_error;
35 unsigned char detect_waiting;
Marek Vasuta3f0a7d2017-07-21 23:22:55 +020036 unsigned char app_cmd;
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +090037};
Kouei Abe5eada1d2017-05-13 15:51:16 +020038
39static inline void sh_sdhi_writeq(struct sh_sdhi_host *host, int reg, u64 val)
40{
41 writeq(val, host->addr + (reg << host->bus_shift));
42}
43
44static inline u64 sh_sdhi_readq(struct sh_sdhi_host *host, int reg)
45{
46 return readq(host->addr + (reg << host->bus_shift));
47}
48
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +090049static inline void sh_sdhi_writew(struct sh_sdhi_host *host, int reg, u16 val)
50{
51 writew(val, host->addr + (reg << host->bus_shift));
52}
53
54static inline u16 sh_sdhi_readw(struct sh_sdhi_host *host, int reg)
55{
56 return readw(host->addr + (reg << host->bus_shift));
57}
58
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +090059static void sh_sdhi_detect(struct sh_sdhi_host *host)
60{
61 sh_sdhi_writew(host, SDHI_OPTION,
62 OPT_BUS_WIDTH_1 | sh_sdhi_readw(host, SDHI_OPTION));
63
64 host->detect_waiting = 0;
65}
66
67static int sh_sdhi_intr(void *dev_id)
68{
69 struct sh_sdhi_host *host = dev_id;
70 int state1 = 0, state2 = 0;
71
72 state1 = sh_sdhi_readw(host, SDHI_INFO1);
73 state2 = sh_sdhi_readw(host, SDHI_INFO2);
74
75 debug("%s: state1 = %x, state2 = %x\n", __func__, state1, state2);
76
77 /* CARD Insert */
78 if (state1 & INFO1_CARD_IN) {
79 sh_sdhi_writew(host, SDHI_INFO1, ~INFO1_CARD_IN);
80 if (!host->detect_waiting) {
81 host->detect_waiting = 1;
82 sh_sdhi_detect(host);
83 }
84 sh_sdhi_writew(host, SDHI_INFO1_MASK, INFO1M_RESP_END |
85 INFO1M_ACCESS_END | INFO1M_CARD_IN |
86 INFO1M_DATA3_CARD_RE | INFO1M_DATA3_CARD_IN);
87 return -EAGAIN;
88 }
89 /* CARD Removal */
90 if (state1 & INFO1_CARD_RE) {
91 sh_sdhi_writew(host, SDHI_INFO1, ~INFO1_CARD_RE);
92 if (!host->detect_waiting) {
93 host->detect_waiting = 1;
94 sh_sdhi_detect(host);
95 }
96 sh_sdhi_writew(host, SDHI_INFO1_MASK, INFO1M_RESP_END |
97 INFO1M_ACCESS_END | INFO1M_CARD_RE |
98 INFO1M_DATA3_CARD_RE | INFO1M_DATA3_CARD_IN);
99 sh_sdhi_writew(host, SDHI_SDIO_INFO1_MASK, SDIO_INFO1M_ON);
100 sh_sdhi_writew(host, SDHI_SDIO_MODE, SDIO_MODE_OFF);
101 return -EAGAIN;
102 }
103
104 if (state2 & INFO2_ALL_ERR) {
105 sh_sdhi_writew(host, SDHI_INFO2,
106 (unsigned short)~(INFO2_ALL_ERR));
107 sh_sdhi_writew(host, SDHI_INFO2_MASK,
108 INFO2M_ALL_ERR |
109 sh_sdhi_readw(host, SDHI_INFO2_MASK));
110 host->sd_error = 1;
111 host->wait_int = 1;
112 return 0;
113 }
114 /* Respons End */
115 if (state1 & INFO1_RESP_END) {
116 sh_sdhi_writew(host, SDHI_INFO1, ~INFO1_RESP_END);
117 sh_sdhi_writew(host, SDHI_INFO1_MASK,
118 INFO1M_RESP_END |
119 sh_sdhi_readw(host, SDHI_INFO1_MASK));
120 host->wait_int = 1;
121 return 0;
122 }
123 /* SD_BUF Read Enable */
124 if (state2 & INFO2_BRE_ENABLE) {
125 sh_sdhi_writew(host, SDHI_INFO2, ~INFO2_BRE_ENABLE);
126 sh_sdhi_writew(host, SDHI_INFO2_MASK,
127 INFO2M_BRE_ENABLE | INFO2M_BUF_ILL_READ |
128 sh_sdhi_readw(host, SDHI_INFO2_MASK));
129 host->wait_int = 1;
130 return 0;
131 }
132 /* SD_BUF Write Enable */
133 if (state2 & INFO2_BWE_ENABLE) {
134 sh_sdhi_writew(host, SDHI_INFO2, ~INFO2_BWE_ENABLE);
135 sh_sdhi_writew(host, SDHI_INFO2_MASK,
136 INFO2_BWE_ENABLE | INFO2M_BUF_ILL_WRITE |
137 sh_sdhi_readw(host, SDHI_INFO2_MASK));
138 host->wait_int = 1;
139 return 0;
140 }
141 /* Access End */
142 if (state1 & INFO1_ACCESS_END) {
143 sh_sdhi_writew(host, SDHI_INFO1, ~INFO1_ACCESS_END);
144 sh_sdhi_writew(host, SDHI_INFO1_MASK,
145 INFO1_ACCESS_END |
146 sh_sdhi_readw(host, SDHI_INFO1_MASK));
147 host->wait_int = 1;
148 return 0;
149 }
150 return -EAGAIN;
151}
152
153static int sh_sdhi_wait_interrupt_flag(struct sh_sdhi_host *host)
154{
155 int timeout = 10000000;
156
157 while (1) {
158 timeout--;
159 if (timeout < 0) {
160 debug(DRIVER_NAME": %s timeout\n", __func__);
161 return 0;
162 }
163
164 if (!sh_sdhi_intr(host))
165 break;
166
167 udelay(1); /* 1 usec */
168 }
169
170 return 1; /* Return value: NOT 0 = complete waiting */
171}
172
173static int sh_sdhi_clock_control(struct sh_sdhi_host *host, unsigned long clk)
174{
175 u32 clkdiv, i, timeout;
176
177 if (sh_sdhi_readw(host, SDHI_INFO2) & (1 << 14)) {
178 printf(DRIVER_NAME": Busy state ! Cannot change the clock\n");
179 return -EBUSY;
180 }
181
182 sh_sdhi_writew(host, SDHI_CLK_CTRL,
183 ~CLK_ENABLE & sh_sdhi_readw(host, SDHI_CLK_CTRL));
184
185 if (clk == 0)
186 return -EIO;
187
188 clkdiv = 0x80;
189 i = CONFIG_SH_SDHI_FREQ >> (0x8 + 1);
190 for (; clkdiv && clk >= (i << 1); (clkdiv >>= 1))
191 i <<= 1;
192
193 sh_sdhi_writew(host, SDHI_CLK_CTRL, clkdiv);
194
195 timeout = 100000;
196 /* Waiting for SD Bus busy to be cleared */
197 while (timeout--) {
198 if ((sh_sdhi_readw(host, SDHI_INFO2) & 0x2000))
199 break;
200 }
201
202 if (timeout)
203 sh_sdhi_writew(host, SDHI_CLK_CTRL,
204 CLK_ENABLE | sh_sdhi_readw(host, SDHI_CLK_CTRL));
205 else
206 return -EBUSY;
207
208 return 0;
209}
210
211static int sh_sdhi_sync_reset(struct sh_sdhi_host *host)
212{
213 u32 timeout;
214 sh_sdhi_writew(host, SDHI_SOFT_RST, SOFT_RST_ON);
215 sh_sdhi_writew(host, SDHI_SOFT_RST, SOFT_RST_OFF);
216 sh_sdhi_writew(host, SDHI_CLK_CTRL,
217 CLK_ENABLE | sh_sdhi_readw(host, SDHI_CLK_CTRL));
218
219 timeout = 100000;
220 while (timeout--) {
221 if (!(sh_sdhi_readw(host, SDHI_INFO2) & INFO2_CBUSY))
222 break;
223 udelay(100);
224 }
225
226 if (!timeout)
227 return -EBUSY;
228
229 if (host->quirks & SH_SDHI_QUIRK_16BIT_BUF)
230 sh_sdhi_writew(host, SDHI_HOST_MODE, 1);
231
232 return 0;
233}
234
235static int sh_sdhi_error_manage(struct sh_sdhi_host *host)
236{
237 unsigned short e_state1, e_state2;
238 int ret;
239
240 host->sd_error = 0;
241 host->wait_int = 0;
242
243 e_state1 = sh_sdhi_readw(host, SDHI_ERR_STS1);
244 e_state2 = sh_sdhi_readw(host, SDHI_ERR_STS2);
245 if (e_state2 & ERR_STS2_SYS_ERROR) {
246 if (e_state2 & ERR_STS2_RES_STOP_TIMEOUT)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900247 ret = -ETIMEDOUT;
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900248 else
249 ret = -EILSEQ;
250 debug("%s: ERR_STS2 = %04x\n",
251 DRIVER_NAME, sh_sdhi_readw(host, SDHI_ERR_STS2));
252 sh_sdhi_sync_reset(host);
253
254 sh_sdhi_writew(host, SDHI_INFO1_MASK,
255 INFO1M_DATA3_CARD_RE | INFO1M_DATA3_CARD_IN);
256 return ret;
257 }
258 if (e_state1 & ERR_STS1_CRC_ERROR || e_state1 & ERR_STS1_CMD_ERROR)
259 ret = -EILSEQ;
260 else
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900261 ret = -ETIMEDOUT;
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900262
263 debug("%s: ERR_STS1 = %04x\n",
264 DRIVER_NAME, sh_sdhi_readw(host, SDHI_ERR_STS1));
265 sh_sdhi_sync_reset(host);
266 sh_sdhi_writew(host, SDHI_INFO1_MASK,
267 INFO1M_DATA3_CARD_RE | INFO1M_DATA3_CARD_IN);
268 return ret;
269}
270
271static int sh_sdhi_single_read(struct sh_sdhi_host *host, struct mmc_data *data)
272{
273 long time;
274 unsigned short blocksize, i;
275 unsigned short *p = (unsigned short *)data->dest;
Kouei Abe5eada1d2017-05-13 15:51:16 +0200276 u64 *q = (u64 *)data->dest;
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900277
278 if ((unsigned long)p & 0x00000001) {
279 debug(DRIVER_NAME": %s: The data pointer is unaligned.",
280 __func__);
281 return -EIO;
282 }
283
284 host->wait_int = 0;
285 sh_sdhi_writew(host, SDHI_INFO2_MASK,
286 ~(INFO2M_BRE_ENABLE | INFO2M_BUF_ILL_READ) &
287 sh_sdhi_readw(host, SDHI_INFO2_MASK));
288 sh_sdhi_writew(host, SDHI_INFO1_MASK,
289 ~INFO1M_ACCESS_END &
290 sh_sdhi_readw(host, SDHI_INFO1_MASK));
291 time = sh_sdhi_wait_interrupt_flag(host);
292 if (time == 0 || host->sd_error != 0)
293 return sh_sdhi_error_manage(host);
294
295 host->wait_int = 0;
296 blocksize = sh_sdhi_readw(host, SDHI_SIZE);
Kouei Abe5eada1d2017-05-13 15:51:16 +0200297 if (host->quirks & SH_SDHI_QUIRK_64BIT_BUF)
298 for (i = 0; i < blocksize / 8; i++)
299 *q++ = sh_sdhi_readq(host, SDHI_BUF0);
300 else
301 for (i = 0; i < blocksize / 2; i++)
302 *p++ = sh_sdhi_readw(host, SDHI_BUF0);
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900303
304 time = sh_sdhi_wait_interrupt_flag(host);
305 if (time == 0 || host->sd_error != 0)
306 return sh_sdhi_error_manage(host);
307
308 host->wait_int = 0;
309 return 0;
310}
311
312static int sh_sdhi_multi_read(struct sh_sdhi_host *host, struct mmc_data *data)
313{
314 long time;
315 unsigned short blocksize, i, sec;
316 unsigned short *p = (unsigned short *)data->dest;
Kouei Abe5eada1d2017-05-13 15:51:16 +0200317 u64 *q = (u64 *)data->dest;
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900318
319 if ((unsigned long)p & 0x00000001) {
320 debug(DRIVER_NAME": %s: The data pointer is unaligned.",
321 __func__);
322 return -EIO;
323 }
324
325 debug("%s: blocks = %d, blocksize = %d\n",
326 __func__, data->blocks, data->blocksize);
327
328 host->wait_int = 0;
329 for (sec = 0; sec < data->blocks; sec++) {
330 sh_sdhi_writew(host, SDHI_INFO2_MASK,
331 ~(INFO2M_BRE_ENABLE | INFO2M_BUF_ILL_READ) &
332 sh_sdhi_readw(host, SDHI_INFO2_MASK));
333
334 time = sh_sdhi_wait_interrupt_flag(host);
335 if (time == 0 || host->sd_error != 0)
336 return sh_sdhi_error_manage(host);
337
338 host->wait_int = 0;
339 blocksize = sh_sdhi_readw(host, SDHI_SIZE);
Kouei Abe5eada1d2017-05-13 15:51:16 +0200340 if (host->quirks & SH_SDHI_QUIRK_64BIT_BUF)
341 for (i = 0; i < blocksize / 8; i++)
342 *q++ = sh_sdhi_readq(host, SDHI_BUF0);
343 else
344 for (i = 0; i < blocksize / 2; i++)
345 *p++ = sh_sdhi_readw(host, SDHI_BUF0);
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900346 }
347
348 return 0;
349}
350
351static int sh_sdhi_single_write(struct sh_sdhi_host *host,
352 struct mmc_data *data)
353{
354 long time;
355 unsigned short blocksize, i;
356 const unsigned short *p = (const unsigned short *)data->src;
Kouei Abe5eada1d2017-05-13 15:51:16 +0200357 const u64 *q = (const u64 *)data->src;
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900358
359 if ((unsigned long)p & 0x00000001) {
360 debug(DRIVER_NAME": %s: The data pointer is unaligned.",
361 __func__);
362 return -EIO;
363 }
364
365 debug("%s: blocks = %d, blocksize = %d\n",
366 __func__, data->blocks, data->blocksize);
367
368 host->wait_int = 0;
369 sh_sdhi_writew(host, SDHI_INFO2_MASK,
370 ~(INFO2M_BWE_ENABLE | INFO2M_BUF_ILL_WRITE) &
371 sh_sdhi_readw(host, SDHI_INFO2_MASK));
372 sh_sdhi_writew(host, SDHI_INFO1_MASK,
373 ~INFO1M_ACCESS_END &
374 sh_sdhi_readw(host, SDHI_INFO1_MASK));
375
376 time = sh_sdhi_wait_interrupt_flag(host);
377 if (time == 0 || host->sd_error != 0)
378 return sh_sdhi_error_manage(host);
379
380 host->wait_int = 0;
381 blocksize = sh_sdhi_readw(host, SDHI_SIZE);
Kouei Abe5eada1d2017-05-13 15:51:16 +0200382 if (host->quirks & SH_SDHI_QUIRK_64BIT_BUF)
383 for (i = 0; i < blocksize / 8; i++)
384 sh_sdhi_writeq(host, SDHI_BUF0, *q++);
385 else
386 for (i = 0; i < blocksize / 2; i++)
387 sh_sdhi_writew(host, SDHI_BUF0, *p++);
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900388
389 time = sh_sdhi_wait_interrupt_flag(host);
390 if (time == 0 || host->sd_error != 0)
391 return sh_sdhi_error_manage(host);
392
393 host->wait_int = 0;
394 return 0;
395}
396
397static int sh_sdhi_multi_write(struct sh_sdhi_host *host, struct mmc_data *data)
398{
399 long time;
400 unsigned short i, sec, blocksize;
401 const unsigned short *p = (const unsigned short *)data->src;
Kouei Abe5eada1d2017-05-13 15:51:16 +0200402 const u64 *q = (const u64 *)data->src;
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900403
404 debug("%s: blocks = %d, blocksize = %d\n",
405 __func__, data->blocks, data->blocksize);
406
407 host->wait_int = 0;
408 for (sec = 0; sec < data->blocks; sec++) {
409 sh_sdhi_writew(host, SDHI_INFO2_MASK,
410 ~(INFO2M_BWE_ENABLE | INFO2M_BUF_ILL_WRITE) &
411 sh_sdhi_readw(host, SDHI_INFO2_MASK));
412
413 time = sh_sdhi_wait_interrupt_flag(host);
414 if (time == 0 || host->sd_error != 0)
415 return sh_sdhi_error_manage(host);
416
417 host->wait_int = 0;
418 blocksize = sh_sdhi_readw(host, SDHI_SIZE);
Kouei Abe5eada1d2017-05-13 15:51:16 +0200419 if (host->quirks & SH_SDHI_QUIRK_64BIT_BUF)
420 for (i = 0; i < blocksize / 8; i++)
421 sh_sdhi_writeq(host, SDHI_BUF0, *q++);
422 else
423 for (i = 0; i < blocksize / 2; i++)
424 sh_sdhi_writew(host, SDHI_BUF0, *p++);
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900425 }
426
427 return 0;
428}
429
430static void sh_sdhi_get_response(struct sh_sdhi_host *host, struct mmc_cmd *cmd)
431{
432 unsigned short i, j, cnt = 1;
433 unsigned short resp[8];
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900434
435 if (cmd->resp_type & MMC_RSP_136) {
436 cnt = 4;
437 resp[0] = sh_sdhi_readw(host, SDHI_RSP00);
438 resp[1] = sh_sdhi_readw(host, SDHI_RSP01);
439 resp[2] = sh_sdhi_readw(host, SDHI_RSP02);
440 resp[3] = sh_sdhi_readw(host, SDHI_RSP03);
441 resp[4] = sh_sdhi_readw(host, SDHI_RSP04);
442 resp[5] = sh_sdhi_readw(host, SDHI_RSP05);
443 resp[6] = sh_sdhi_readw(host, SDHI_RSP06);
444 resp[7] = sh_sdhi_readw(host, SDHI_RSP07);
445
446 /* SDHI REGISTER SPECIFICATION */
447 for (i = 7, j = 6; i > 0; i--) {
448 resp[i] = (resp[i] << 8) & 0xff00;
449 resp[i] |= (resp[j--] >> 8) & 0x00ff;
450 }
451 resp[0] = (resp[0] << 8) & 0xff00;
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900452 } else {
453 resp[0] = sh_sdhi_readw(host, SDHI_RSP00);
454 resp[1] = sh_sdhi_readw(host, SDHI_RSP01);
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900455 }
456
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900457#if defined(__BIG_ENDIAN_BITFIELD)
masakazu.mochizuki.wd@hitachi.com6f107e42016-04-12 17:11:41 +0900458 if (cnt == 4) {
459 cmd->response[0] = (resp[6] << 16) | resp[7];
460 cmd->response[1] = (resp[4] << 16) | resp[5];
461 cmd->response[2] = (resp[2] << 16) | resp[3];
462 cmd->response[3] = (resp[0] << 16) | resp[1];
463 } else {
464 cmd->response[0] = (resp[0] << 16) | resp[1];
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900465 }
466#else
masakazu.mochizuki.wd@hitachi.com6f107e42016-04-12 17:11:41 +0900467 if (cnt == 4) {
468 cmd->response[0] = (resp[7] << 16) | resp[6];
469 cmd->response[1] = (resp[5] << 16) | resp[4];
470 cmd->response[2] = (resp[3] << 16) | resp[2];
471 cmd->response[3] = (resp[1] << 16) | resp[0];
472 } else {
473 cmd->response[0] = (resp[1] << 16) | resp[0];
474 }
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900475#endif /* __BIG_ENDIAN_BITFIELD */
476}
477
478static unsigned short sh_sdhi_set_cmd(struct sh_sdhi_host *host,
479 struct mmc_data *data, unsigned short opc)
480{
Marek Vasuta3f0a7d2017-07-21 23:22:55 +0200481 if (host->app_cmd) {
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900482 if (!data)
Marek Vasuta3f0a7d2017-07-21 23:22:55 +0200483 host->app_cmd = 0;
484 return opc | BIT(6);
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900485 }
Marek Vasuta3f0a7d2017-07-21 23:22:55 +0200486
487 switch (opc) {
488 case MMC_CMD_SWITCH:
489 return opc | (data ? 0x1c00 : 0x40);
490 case MMC_CMD_SEND_EXT_CSD:
491 return opc | (data ? 0x1c00 : 0);
492 case MMC_CMD_SEND_OP_COND:
493 return opc | 0x0700;
494 case MMC_CMD_APP_CMD:
495 host->app_cmd = 1;
496 default:
497 return opc;
498 }
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900499}
500
501static unsigned short sh_sdhi_data_trans(struct sh_sdhi_host *host,
502 struct mmc_data *data, unsigned short opc)
503{
Marek Vasuta3f0a7d2017-07-21 23:22:55 +0200504 if (host->app_cmd) {
505 host->app_cmd = 0;
506 switch (opc) {
507 case SD_CMD_APP_SEND_SCR:
508 case SD_CMD_APP_SD_STATUS:
509 return sh_sdhi_single_read(host, data);
510 default:
511 printf(DRIVER_NAME": SD: NOT SUPPORT APP CMD = d'%04d\n",
512 opc);
513 return -EINVAL;
514 }
515 } else {
516 switch (opc) {
517 case MMC_CMD_WRITE_MULTIPLE_BLOCK:
518 return sh_sdhi_multi_write(host, data);
519 case MMC_CMD_READ_MULTIPLE_BLOCK:
520 return sh_sdhi_multi_read(host, data);
521 case MMC_CMD_WRITE_SINGLE_BLOCK:
522 return sh_sdhi_single_write(host, data);
523 case MMC_CMD_READ_SINGLE_BLOCK:
524 case MMC_CMD_SWITCH:
525 case MMC_CMD_SEND_EXT_CSD:;
526 return sh_sdhi_single_read(host, data);
527 default:
528 printf(DRIVER_NAME": SD: NOT SUPPORT CMD = d'%04d\n", opc);
529 return -EINVAL;
530 }
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900531 }
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900532}
533
534static int sh_sdhi_start_cmd(struct sh_sdhi_host *host,
535 struct mmc_data *data, struct mmc_cmd *cmd)
536{
537 long time;
Marek Vasuta3f0a7d2017-07-21 23:22:55 +0200538 unsigned short shcmd, opc = cmd->cmdidx;
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900539 int ret = 0;
540 unsigned long timeout;
541
542 debug("opc = %d, arg = %x, resp_type = %x\n",
543 opc, cmd->cmdarg, cmd->resp_type);
544
545 if (opc == MMC_CMD_STOP_TRANSMISSION) {
546 /* SDHI sends the STOP command automatically by STOP reg */
547 sh_sdhi_writew(host, SDHI_INFO1_MASK, ~INFO1M_ACCESS_END &
548 sh_sdhi_readw(host, SDHI_INFO1_MASK));
549
550 time = sh_sdhi_wait_interrupt_flag(host);
551 if (time == 0 || host->sd_error != 0)
552 return sh_sdhi_error_manage(host);
553
554 sh_sdhi_get_response(host, cmd);
555 return 0;
556 }
557
558 if (data) {
559 if ((opc == MMC_CMD_READ_MULTIPLE_BLOCK) ||
560 opc == MMC_CMD_WRITE_MULTIPLE_BLOCK) {
561 sh_sdhi_writew(host, SDHI_STOP, STOP_SEC_ENABLE);
562 sh_sdhi_writew(host, SDHI_SECCNT, data->blocks);
563 }
564 sh_sdhi_writew(host, SDHI_SIZE, data->blocksize);
565 }
Marek Vasuta3f0a7d2017-07-21 23:22:55 +0200566
567 shcmd = sh_sdhi_set_cmd(host, data, opc);
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900568
569 /*
Bin Menga1875592016-02-05 19:30:11 -0800570 * U-Boot cannot use interrupt.
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900571 * So this flag may not be clear by timing
572 */
573 sh_sdhi_writew(host, SDHI_INFO1, ~INFO1_RESP_END);
574
575 sh_sdhi_writew(host, SDHI_INFO1_MASK,
576 INFO1M_RESP_END | sh_sdhi_readw(host, SDHI_INFO1_MASK));
577 sh_sdhi_writew(host, SDHI_ARG0,
578 (unsigned short)(cmd->cmdarg & ARG0_MASK));
579 sh_sdhi_writew(host, SDHI_ARG1,
580 (unsigned short)((cmd->cmdarg >> 16) & ARG1_MASK));
581
582 timeout = 100000;
583 /* Waiting for SD Bus busy to be cleared */
584 while (timeout--) {
585 if ((sh_sdhi_readw(host, SDHI_INFO2) & 0x2000))
586 break;
587 }
588
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900589 host->wait_int = 0;
590 sh_sdhi_writew(host, SDHI_INFO1_MASK,
591 ~INFO1M_RESP_END & sh_sdhi_readw(host, SDHI_INFO1_MASK));
592 sh_sdhi_writew(host, SDHI_INFO2_MASK,
593 ~(INFO2M_CMD_ERROR | INFO2M_CRC_ERROR |
594 INFO2M_END_ERROR | INFO2M_TIMEOUT |
595 INFO2M_RESP_TIMEOUT | INFO2M_ILA) &
596 sh_sdhi_readw(host, SDHI_INFO2_MASK));
597
Marek Vasuta3f0a7d2017-07-21 23:22:55 +0200598 sh_sdhi_writew(host, SDHI_CMD, (unsigned short)(shcmd & CMD_MASK));
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900599 time = sh_sdhi_wait_interrupt_flag(host);
Marek Vasuta3f0a7d2017-07-21 23:22:55 +0200600 if (!time) {
601 host->app_cmd = 0;
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900602 return sh_sdhi_error_manage(host);
Marek Vasuta3f0a7d2017-07-21 23:22:55 +0200603 }
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900604
605 if (host->sd_error) {
606 switch (cmd->cmdidx) {
607 case MMC_CMD_ALL_SEND_CID:
608 case MMC_CMD_SELECT_CARD:
609 case SD_CMD_SEND_IF_COND:
610 case MMC_CMD_APP_CMD:
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900611 ret = -ETIMEDOUT;
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900612 break;
613 default:
614 debug(DRIVER_NAME": Cmd(d'%d) err\n", opc);
615 debug(DRIVER_NAME": cmdidx = %d\n", cmd->cmdidx);
616 ret = sh_sdhi_error_manage(host);
617 break;
618 }
619 host->sd_error = 0;
620 host->wait_int = 0;
Marek Vasuta3f0a7d2017-07-21 23:22:55 +0200621 host->app_cmd = 0;
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900622 return ret;
623 }
Marek Vasuta3f0a7d2017-07-21 23:22:55 +0200624
625 if (sh_sdhi_readw(host, SDHI_INFO1) & INFO1_RESP_END) {
626 host->app_cmd = 0;
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900627 return -EINVAL;
Marek Vasuta3f0a7d2017-07-21 23:22:55 +0200628 }
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900629
630 if (host->wait_int) {
631 sh_sdhi_get_response(host, cmd);
632 host->wait_int = 0;
633 }
Marek Vasuta3f0a7d2017-07-21 23:22:55 +0200634
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900635 if (data)
636 ret = sh_sdhi_data_trans(host, data, opc);
637
638 debug("ret = %d, resp = %08x, %08x, %08x, %08x\n",
639 ret, cmd->response[0], cmd->response[1],
640 cmd->response[2], cmd->response[3]);
641 return ret;
642}
643
Marek Vasutd1c18ca2017-07-21 23:22:54 +0200644static int sh_sdhi_send_cmd_common(struct sh_sdhi_host *host,
645 struct mmc_cmd *cmd, struct mmc_data *data)
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900646{
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900647 host->sd_error = 0;
648
Marek Vasutd1c18ca2017-07-21 23:22:54 +0200649 return sh_sdhi_start_cmd(host, data, cmd);
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900650}
651
Marek Vasutd1c18ca2017-07-21 23:22:54 +0200652static int sh_sdhi_set_ios_common(struct sh_sdhi_host *host, struct mmc *mmc)
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900653{
654 int ret;
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900655
656 ret = sh_sdhi_clock_control(host, mmc->clock);
657 if (ret)
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900658 return -EINVAL;
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900659
Kouei Abe91a16c32017-05-13 15:51:17 +0200660 if (mmc->bus_width == 8)
661 sh_sdhi_writew(host, SDHI_OPTION,
662 OPT_BUS_WIDTH_8 | (~OPT_BUS_WIDTH_M &
663 sh_sdhi_readw(host, SDHI_OPTION)));
664 else if (mmc->bus_width == 4)
665 sh_sdhi_writew(host, SDHI_OPTION,
666 OPT_BUS_WIDTH_4 | (~OPT_BUS_WIDTH_M &
667 sh_sdhi_readw(host, SDHI_OPTION)));
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900668 else
Kouei Abe91a16c32017-05-13 15:51:17 +0200669 sh_sdhi_writew(host, SDHI_OPTION,
670 OPT_BUS_WIDTH_1 | (~OPT_BUS_WIDTH_M &
671 sh_sdhi_readw(host, SDHI_OPTION)));
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900672
673 debug("clock = %d, buswidth = %d\n", mmc->clock, mmc->bus_width);
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900674
675 return 0;
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900676}
677
Marek Vasutd1c18ca2017-07-21 23:22:54 +0200678static int sh_sdhi_initialize_common(struct sh_sdhi_host *host)
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900679{
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900680 int ret = sh_sdhi_sync_reset(host);
681
682 sh_sdhi_writew(host, SDHI_PORTSEL, USE_1PORT);
683
684#if defined(__BIG_ENDIAN_BITFIELD)
685 sh_sdhi_writew(host, SDHI_EXT_SWAP, SET_SWAP);
686#endif
687
688 sh_sdhi_writew(host, SDHI_INFO1_MASK, INFO1M_RESP_END |
689 INFO1M_ACCESS_END | INFO1M_CARD_RE |
690 INFO1M_DATA3_CARD_RE | INFO1M_DATA3_CARD_IN);
691
692 return ret;
693}
694
Marek Vasutd1c18ca2017-07-21 23:22:54 +0200695#ifndef CONFIG_DM_MMC
696static void *mmc_priv(struct mmc *mmc)
697{
698 return (void *)mmc->priv;
699}
700
701static int sh_sdhi_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
702 struct mmc_data *data)
703{
704 struct sh_sdhi_host *host = mmc_priv(mmc);
705
706 return sh_sdhi_send_cmd_common(host, cmd, data);
707}
708
709static int sh_sdhi_set_ios(struct mmc *mmc)
710{
711 struct sh_sdhi_host *host = mmc_priv(mmc);
712
713 return sh_sdhi_set_ios_common(host, mmc);
714}
715
716static int sh_sdhi_initialize(struct mmc *mmc)
717{
718 struct sh_sdhi_host *host = mmc_priv(mmc);
719
720 return sh_sdhi_initialize_common(host);
721}
722
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900723static const struct mmc_ops sh_sdhi_ops = {
724 .send_cmd = sh_sdhi_send_cmd,
725 .set_ios = sh_sdhi_set_ios,
726 .init = sh_sdhi_initialize,
727};
728
Kouei Abea5950f82017-05-13 15:51:18 +0200729#ifdef CONFIG_RCAR_GEN3
730static struct mmc_config sh_sdhi_cfg = {
731 .name = DRIVER_NAME,
732 .ops = &sh_sdhi_ops,
733 .f_min = CLKDEV_INIT,
734 .f_max = CLKDEV_HS_DATA,
735 .voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34,
736 .host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | MMC_MODE_HS |
737 MMC_MODE_HS_52MHz,
738 .part_type = PART_TYPE_DOS,
739 .b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT,
740};
741#else
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900742static struct mmc_config sh_sdhi_cfg = {
743 .name = DRIVER_NAME,
744 .ops = &sh_sdhi_ops,
745 .f_min = CLKDEV_INIT,
746 .f_max = CLKDEV_HS_DATA,
747 .voltages = MMC_VDD_32_33 | MMC_VDD_33_34,
748 .host_caps = MMC_MODE_4BIT | MMC_MODE_HS,
749 .part_type = PART_TYPE_DOS,
750 .b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT,
751};
Kouei Abea5950f82017-05-13 15:51:18 +0200752#endif
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900753
754int sh_sdhi_init(unsigned long addr, int ch, unsigned long quirks)
755{
756 int ret = 0;
757 struct mmc *mmc;
758 struct sh_sdhi_host *host = NULL;
759
760 if (ch >= CONFIG_SYS_SH_SDHI_NR_CHANNEL)
761 return -ENODEV;
762
763 host = malloc(sizeof(struct sh_sdhi_host));
764 if (!host)
765 return -ENOMEM;
766
767 mmc = mmc_create(&sh_sdhi_cfg, host);
768 if (!mmc) {
769 ret = -1;
770 goto error;
771 }
772
773 host->ch = ch;
Marek Vasutd1c18ca2017-07-21 23:22:54 +0200774 host->addr = (void __iomem *)addr;
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900775 host->quirks = quirks;
776
Kouei Abe5eada1d2017-05-13 15:51:16 +0200777 if (host->quirks & SH_SDHI_QUIRK_64BIT_BUF)
778 host->bus_shift = 2;
779 else if (host->quirks & SH_SDHI_QUIRK_16BIT_BUF)
Nobuhiro Iwamatsu72d42ba2014-12-17 08:03:00 +0900780 host->bus_shift = 1;
781
782 return ret;
783error:
784 if (host)
785 free(host);
786 return ret;
787}
Marek Vasutd1c18ca2017-07-21 23:22:54 +0200788
789#else
790
791struct sh_sdhi_plat {
792 struct mmc_config cfg;
793 struct mmc mmc;
794};
795
796int sh_sdhi_dm_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
797 struct mmc_data *data)
798{
799 struct sh_sdhi_host *host = dev_get_priv(dev);
800
801 return sh_sdhi_send_cmd_common(host, cmd, data);
802}
803
804int sh_sdhi_dm_set_ios(struct udevice *dev)
805{
806 struct sh_sdhi_host *host = dev_get_priv(dev);
807 struct mmc *mmc = mmc_get_mmc_dev(dev);
808
809 return sh_sdhi_set_ios_common(host, mmc);
810}
811
812static const struct dm_mmc_ops sh_sdhi_dm_ops = {
813 .send_cmd = sh_sdhi_dm_send_cmd,
814 .set_ios = sh_sdhi_dm_set_ios,
815};
816
817static int sh_sdhi_dm_bind(struct udevice *dev)
818{
819 struct sh_sdhi_plat *plat = dev_get_platdata(dev);
820
821 return mmc_bind(dev, &plat->mmc, &plat->cfg);
822}
823
824static int sh_sdhi_dm_probe(struct udevice *dev)
825{
826 struct sh_sdhi_plat *plat = dev_get_platdata(dev);
827 struct sh_sdhi_host *host = dev_get_priv(dev);
828 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Marek Vasut8cd46cb2017-07-21 23:22:56 +0200829 struct clk sh_sdhi_clk;
Marek Vasutd1c18ca2017-07-21 23:22:54 +0200830 const u32 quirks = dev_get_driver_data(dev);
831 fdt_addr_t base;
Marek Vasut8cd46cb2017-07-21 23:22:56 +0200832 int ret;
Marek Vasutd1c18ca2017-07-21 23:22:54 +0200833
834 base = devfdt_get_addr(dev);
835 if (base == FDT_ADDR_T_NONE)
836 return -EINVAL;
837
838 host->addr = devm_ioremap(dev, base, SZ_2K);
839 if (!host->addr)
840 return -ENOMEM;
841
Marek Vasut8cd46cb2017-07-21 23:22:56 +0200842 ret = clk_get_by_index(dev, 0, &sh_sdhi_clk);
843 if (ret) {
844 debug("failed to get clock, ret=%d\n", ret);
845 return ret;
846 }
847
848 ret = clk_enable(&sh_sdhi_clk);
849 if (ret) {
850 debug("failed to enable clock, ret=%d\n", ret);
851 return ret;
852 }
853
Marek Vasutd1c18ca2017-07-21 23:22:54 +0200854 host->quirks = quirks;
855
856 if (host->quirks & SH_SDHI_QUIRK_64BIT_BUF)
857 host->bus_shift = 2;
858 else if (host->quirks & SH_SDHI_QUIRK_16BIT_BUF)
859 host->bus_shift = 1;
860
861 plat->cfg.name = dev->name;
862 plat->cfg.host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
863
864 switch (fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "bus-width",
865 1)) {
866 case 8:
867 plat->cfg.host_caps |= MMC_MODE_8BIT;
868 break;
869 case 4:
870 plat->cfg.host_caps |= MMC_MODE_4BIT;
871 break;
872 case 1:
873 break;
874 default:
875 dev_err(dev, "Invalid \"bus-width\" value\n");
876 return -EINVAL;
877 }
878
879 sh_sdhi_initialize_common(host);
880
881 plat->cfg.voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
882 plat->cfg.f_min = CLKDEV_INIT;
883 plat->cfg.f_max = CLKDEV_HS_DATA;
884 plat->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
885
886 upriv->mmc = &plat->mmc;
887
888 return 0;
889}
890
891static const struct udevice_id sh_sdhi_sd_match[] = {
892 { .compatible = "renesas,sdhi-r8a7795", .data = SH_SDHI_QUIRK_64BIT_BUF },
893 { .compatible = "renesas,sdhi-r8a7796", .data = SH_SDHI_QUIRK_64BIT_BUF },
894 { /* sentinel */ }
895};
896
897U_BOOT_DRIVER(sh_sdhi_mmc) = {
898 .name = "sh-sdhi-mmc",
899 .id = UCLASS_MMC,
900 .of_match = sh_sdhi_sd_match,
901 .bind = sh_sdhi_dm_bind,
902 .probe = sh_sdhi_dm_probe,
903 .priv_auto_alloc_size = sizeof(struct sh_sdhi_host),
904 .platdata_auto_alloc_size = sizeof(struct sh_sdhi_plat),
905 .ops = &sh_sdhi_dm_ops,
906};
907#endif