xref: /openbmc/u-boot/drivers/mmc/sh_mmcif.c (revision 66c433ed)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0
2afb35666SYoshihiro Shimoda /*
3afb35666SYoshihiro Shimoda  * MMCIF driver.
4afb35666SYoshihiro Shimoda  *
5afb35666SYoshihiro Shimoda  * Copyright (C)  2011 Renesas Solutions Corp.
6afb35666SYoshihiro Shimoda  */
7afb35666SYoshihiro Shimoda 
8afb35666SYoshihiro Shimoda #include <config.h>
9afb35666SYoshihiro Shimoda #include <common.h>
10afb35666SYoshihiro Shimoda #include <watchdog.h>
11afb35666SYoshihiro Shimoda #include <command.h>
12afb35666SYoshihiro Shimoda #include <mmc.h>
1348f54a2dSMarek Vasut #include <clk.h>
1448f54a2dSMarek Vasut #include <dm.h>
15afb35666SYoshihiro Shimoda #include <malloc.h>
161221ce45SMasahiro Yamada #include <linux/errno.h>
1748f54a2dSMarek Vasut #include <linux/compat.h>
1848f54a2dSMarek Vasut #include <linux/io.h>
1948f54a2dSMarek Vasut #include <linux/sizes.h>
20afb35666SYoshihiro Shimoda #include "sh_mmcif.h"
21afb35666SYoshihiro Shimoda 
22afb35666SYoshihiro Shimoda #define DRIVER_NAME	"sh_mmcif"
23afb35666SYoshihiro Shimoda 
sh_mmcif_intr(void * dev_id)24afb35666SYoshihiro Shimoda static int sh_mmcif_intr(void *dev_id)
25afb35666SYoshihiro Shimoda {
26afb35666SYoshihiro Shimoda 	struct sh_mmcif_host *host = dev_id;
27afb35666SYoshihiro Shimoda 	u32 state = 0;
28afb35666SYoshihiro Shimoda 
29afb35666SYoshihiro Shimoda 	state = sh_mmcif_read(&host->regs->ce_int);
30afb35666SYoshihiro Shimoda 	state &= sh_mmcif_read(&host->regs->ce_int_mask);
31afb35666SYoshihiro Shimoda 
32afb35666SYoshihiro Shimoda 	if (state & INT_RBSYE) {
33afb35666SYoshihiro Shimoda 		sh_mmcif_write(~(INT_RBSYE | INT_CRSPE), &host->regs->ce_int);
34afb35666SYoshihiro Shimoda 		sh_mmcif_bitclr(MASK_MRBSYE, &host->regs->ce_int_mask);
35afb35666SYoshihiro Shimoda 		goto end;
36afb35666SYoshihiro Shimoda 	} else if (state & INT_CRSPE) {
37afb35666SYoshihiro Shimoda 		sh_mmcif_write(~INT_CRSPE, &host->regs->ce_int);
38afb35666SYoshihiro Shimoda 		sh_mmcif_bitclr(MASK_MCRSPE, &host->regs->ce_int_mask);
39afb35666SYoshihiro Shimoda 		/* one more interrupt (INT_RBSYE) */
40afb35666SYoshihiro Shimoda 		if (sh_mmcif_read(&host->regs->ce_cmd_set) & CMD_SET_RBSY)
41afb35666SYoshihiro Shimoda 			return -EAGAIN;
42afb35666SYoshihiro Shimoda 		goto end;
43afb35666SYoshihiro Shimoda 	} else if (state & INT_BUFREN) {
44afb35666SYoshihiro Shimoda 		sh_mmcif_write(~INT_BUFREN, &host->regs->ce_int);
45afb35666SYoshihiro Shimoda 		sh_mmcif_bitclr(MASK_MBUFREN, &host->regs->ce_int_mask);
46afb35666SYoshihiro Shimoda 		goto end;
47afb35666SYoshihiro Shimoda 	} else if (state & INT_BUFWEN) {
48afb35666SYoshihiro Shimoda 		sh_mmcif_write(~INT_BUFWEN, &host->regs->ce_int);
49afb35666SYoshihiro Shimoda 		sh_mmcif_bitclr(MASK_MBUFWEN, &host->regs->ce_int_mask);
50afb35666SYoshihiro Shimoda 		goto end;
51afb35666SYoshihiro Shimoda 	} else if (state & INT_CMD12DRE) {
52afb35666SYoshihiro Shimoda 		sh_mmcif_write(~(INT_CMD12DRE | INT_CMD12RBE | INT_CMD12CRE |
53afb35666SYoshihiro Shimoda 				  INT_BUFRE), &host->regs->ce_int);
54afb35666SYoshihiro Shimoda 		sh_mmcif_bitclr(MASK_MCMD12DRE, &host->regs->ce_int_mask);
55afb35666SYoshihiro Shimoda 		goto end;
56afb35666SYoshihiro Shimoda 	} else if (state & INT_BUFRE) {
57afb35666SYoshihiro Shimoda 		sh_mmcif_write(~INT_BUFRE, &host->regs->ce_int);
58afb35666SYoshihiro Shimoda 		sh_mmcif_bitclr(MASK_MBUFRE, &host->regs->ce_int_mask);
59afb35666SYoshihiro Shimoda 		goto end;
60afb35666SYoshihiro Shimoda 	} else if (state & INT_DTRANE) {
61afb35666SYoshihiro Shimoda 		sh_mmcif_write(~INT_DTRANE, &host->regs->ce_int);
62afb35666SYoshihiro Shimoda 		sh_mmcif_bitclr(MASK_MDTRANE, &host->regs->ce_int_mask);
63afb35666SYoshihiro Shimoda 		goto end;
64afb35666SYoshihiro Shimoda 	} else if (state & INT_CMD12RBE) {
65afb35666SYoshihiro Shimoda 		sh_mmcif_write(~(INT_CMD12RBE | INT_CMD12CRE),
66afb35666SYoshihiro Shimoda 				&host->regs->ce_int);
67afb35666SYoshihiro Shimoda 		sh_mmcif_bitclr(MASK_MCMD12RBE, &host->regs->ce_int_mask);
68afb35666SYoshihiro Shimoda 		goto end;
69afb35666SYoshihiro Shimoda 	} else if (state & INT_ERR_STS) {
70afb35666SYoshihiro Shimoda 		/* err interrupts */
71afb35666SYoshihiro Shimoda 		sh_mmcif_write(~state, &host->regs->ce_int);
72afb35666SYoshihiro Shimoda 		sh_mmcif_bitclr(state, &host->regs->ce_int_mask);
73afb35666SYoshihiro Shimoda 		goto err;
74afb35666SYoshihiro Shimoda 	} else
75afb35666SYoshihiro Shimoda 		return -EAGAIN;
76afb35666SYoshihiro Shimoda 
77afb35666SYoshihiro Shimoda err:
78afb35666SYoshihiro Shimoda 	host->sd_error = 1;
79afb35666SYoshihiro Shimoda 	debug("%s: int err state = %08x\n", DRIVER_NAME, state);
80afb35666SYoshihiro Shimoda end:
81afb35666SYoshihiro Shimoda 	host->wait_int = 1;
82afb35666SYoshihiro Shimoda 	return 0;
83afb35666SYoshihiro Shimoda }
84afb35666SYoshihiro Shimoda 
mmcif_wait_interrupt_flag(struct sh_mmcif_host * host)85afb35666SYoshihiro Shimoda static int mmcif_wait_interrupt_flag(struct sh_mmcif_host *host)
86afb35666SYoshihiro Shimoda {
87afb35666SYoshihiro Shimoda 	int timeout = 10000000;
88afb35666SYoshihiro Shimoda 
89afb35666SYoshihiro Shimoda 	while (1) {
90afb35666SYoshihiro Shimoda 		timeout--;
91afb35666SYoshihiro Shimoda 		if (timeout < 0) {
92afb35666SYoshihiro Shimoda 			printf("timeout\n");
93afb35666SYoshihiro Shimoda 			return 0;
94afb35666SYoshihiro Shimoda 		}
95afb35666SYoshihiro Shimoda 
96afb35666SYoshihiro Shimoda 		if (!sh_mmcif_intr(host))
97afb35666SYoshihiro Shimoda 			break;
98afb35666SYoshihiro Shimoda 
99afb35666SYoshihiro Shimoda 		udelay(1);	/* 1 usec */
100afb35666SYoshihiro Shimoda 	}
101afb35666SYoshihiro Shimoda 
102afb35666SYoshihiro Shimoda 	return 1;	/* Return value: NOT 0 = complete waiting */
103afb35666SYoshihiro Shimoda }
104afb35666SYoshihiro Shimoda 
sh_mmcif_clock_control(struct sh_mmcif_host * host,unsigned int clk)105afb35666SYoshihiro Shimoda static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk)
106afb35666SYoshihiro Shimoda {
107afb35666SYoshihiro Shimoda 	sh_mmcif_bitclr(CLK_ENABLE, &host->regs->ce_clk_ctrl);
108afb35666SYoshihiro Shimoda 	sh_mmcif_bitclr(CLK_CLEAR, &host->regs->ce_clk_ctrl);
109afb35666SYoshihiro Shimoda 
110afb35666SYoshihiro Shimoda 	if (!clk)
111afb35666SYoshihiro Shimoda 		return;
11221ea3503SNobuhiro Iwamatsu 
11321ea3503SNobuhiro Iwamatsu 	if (clk == CLKDEV_EMMC_DATA)
114afb35666SYoshihiro Shimoda 		sh_mmcif_bitset(CLK_PCLK, &host->regs->ce_clk_ctrl);
11521ea3503SNobuhiro Iwamatsu 	else
11621ea3503SNobuhiro Iwamatsu 		sh_mmcif_bitset((fls(DIV_ROUND_UP(host->clk,
11721ea3503SNobuhiro Iwamatsu 						  clk) - 1) - 1) << 16,
11821ea3503SNobuhiro Iwamatsu 				&host->regs->ce_clk_ctrl);
119afb35666SYoshihiro Shimoda 	sh_mmcif_bitset(CLK_ENABLE, &host->regs->ce_clk_ctrl);
120afb35666SYoshihiro Shimoda }
121afb35666SYoshihiro Shimoda 
sh_mmcif_sync_reset(struct sh_mmcif_host * host)122afb35666SYoshihiro Shimoda static void sh_mmcif_sync_reset(struct sh_mmcif_host *host)
123afb35666SYoshihiro Shimoda {
124afb35666SYoshihiro Shimoda 	u32 tmp;
125afb35666SYoshihiro Shimoda 
126afb35666SYoshihiro Shimoda 	tmp = sh_mmcif_read(&host->regs->ce_clk_ctrl) & (CLK_ENABLE |
127afb35666SYoshihiro Shimoda 							 CLK_CLEAR);
128afb35666SYoshihiro Shimoda 
129afb35666SYoshihiro Shimoda 	sh_mmcif_write(SOFT_RST_ON, &host->regs->ce_version);
130afb35666SYoshihiro Shimoda 	sh_mmcif_write(SOFT_RST_OFF, &host->regs->ce_version);
131afb35666SYoshihiro Shimoda 	sh_mmcif_bitset(tmp | SRSPTO_256 | SRBSYTO_29 | SRWDTO_29 | SCCSTO_29,
132afb35666SYoshihiro Shimoda 			&host->regs->ce_clk_ctrl);
133afb35666SYoshihiro Shimoda 	/* byte swap on */
134afb35666SYoshihiro Shimoda 	sh_mmcif_bitset(BUF_ACC_ATYP, &host->regs->ce_buf_acc);
135afb35666SYoshihiro Shimoda }
136afb35666SYoshihiro Shimoda 
sh_mmcif_error_manage(struct sh_mmcif_host * host)137afb35666SYoshihiro Shimoda static int sh_mmcif_error_manage(struct sh_mmcif_host *host)
138afb35666SYoshihiro Shimoda {
139afb35666SYoshihiro Shimoda 	u32 state1, state2;
140afb35666SYoshihiro Shimoda 	int ret, timeout = 10000000;
141afb35666SYoshihiro Shimoda 
142afb35666SYoshihiro Shimoda 	host->sd_error = 0;
143afb35666SYoshihiro Shimoda 	host->wait_int = 0;
144afb35666SYoshihiro Shimoda 
145afb35666SYoshihiro Shimoda 	state1 = sh_mmcif_read(&host->regs->ce_host_sts1);
146afb35666SYoshihiro Shimoda 	state2 = sh_mmcif_read(&host->regs->ce_host_sts2);
147afb35666SYoshihiro Shimoda 	debug("%s: ERR HOST_STS1 = %08x\n", \
148afb35666SYoshihiro Shimoda 			DRIVER_NAME, sh_mmcif_read(&host->regs->ce_host_sts1));
149afb35666SYoshihiro Shimoda 	debug("%s: ERR HOST_STS2 = %08x\n", \
150afb35666SYoshihiro Shimoda 			DRIVER_NAME, sh_mmcif_read(&host->regs->ce_host_sts2));
151afb35666SYoshihiro Shimoda 
152afb35666SYoshihiro Shimoda 	if (state1 & STS1_CMDSEQ) {
153afb35666SYoshihiro Shimoda 		debug("%s: Forced end of command sequence\n", DRIVER_NAME);
154afb35666SYoshihiro Shimoda 		sh_mmcif_bitset(CMD_CTRL_BREAK, &host->regs->ce_cmd_ctrl);
155afb35666SYoshihiro Shimoda 		sh_mmcif_bitset(~CMD_CTRL_BREAK, &host->regs->ce_cmd_ctrl);
156afb35666SYoshihiro Shimoda 		while (1) {
157afb35666SYoshihiro Shimoda 			timeout--;
158afb35666SYoshihiro Shimoda 			if (timeout < 0) {
159afb35666SYoshihiro Shimoda 				printf(DRIVER_NAME": Forceed end of " \
160afb35666SYoshihiro Shimoda 					"command sequence timeout err\n");
161afb35666SYoshihiro Shimoda 				return -EILSEQ;
162afb35666SYoshihiro Shimoda 			}
163afb35666SYoshihiro Shimoda 			if (!(sh_mmcif_read(&host->regs->ce_host_sts1)
164afb35666SYoshihiro Shimoda 								& STS1_CMDSEQ))
165afb35666SYoshihiro Shimoda 				break;
166afb35666SYoshihiro Shimoda 		}
167afb35666SYoshihiro Shimoda 		sh_mmcif_sync_reset(host);
168afb35666SYoshihiro Shimoda 		return -EILSEQ;
169afb35666SYoshihiro Shimoda 	}
170afb35666SYoshihiro Shimoda 
171afb35666SYoshihiro Shimoda 	if (state2 & STS2_CRC_ERR)
172afb35666SYoshihiro Shimoda 		ret = -EILSEQ;
173afb35666SYoshihiro Shimoda 	else if (state2 & STS2_TIMEOUT_ERR)
174915ffa52SJaehoon Chung 		ret = -ETIMEDOUT;
175afb35666SYoshihiro Shimoda 	else
176afb35666SYoshihiro Shimoda 		ret = -EILSEQ;
177afb35666SYoshihiro Shimoda 	return ret;
178afb35666SYoshihiro Shimoda }
179afb35666SYoshihiro Shimoda 
sh_mmcif_single_read(struct sh_mmcif_host * host,struct mmc_data * data)180afb35666SYoshihiro Shimoda static int sh_mmcif_single_read(struct sh_mmcif_host *host,
181afb35666SYoshihiro Shimoda 				struct mmc_data *data)
182afb35666SYoshihiro Shimoda {
183afb35666SYoshihiro Shimoda 	long time;
184afb35666SYoshihiro Shimoda 	u32 blocksize, i;
185afb35666SYoshihiro Shimoda 	unsigned long *p = (unsigned long *)data->dest;
186afb35666SYoshihiro Shimoda 
187afb35666SYoshihiro Shimoda 	if ((unsigned long)p & 0x00000001) {
188afb35666SYoshihiro Shimoda 		printf("%s: The data pointer is unaligned.", __func__);
189afb35666SYoshihiro Shimoda 		return -EIO;
190afb35666SYoshihiro Shimoda 	}
191afb35666SYoshihiro Shimoda 
192afb35666SYoshihiro Shimoda 	host->wait_int = 0;
193afb35666SYoshihiro Shimoda 
194afb35666SYoshihiro Shimoda 	/* buf read enable */
195afb35666SYoshihiro Shimoda 	sh_mmcif_bitset(MASK_MBUFREN, &host->regs->ce_int_mask);
196afb35666SYoshihiro Shimoda 	time = mmcif_wait_interrupt_flag(host);
197afb35666SYoshihiro Shimoda 	if (time == 0 || host->sd_error != 0)
198afb35666SYoshihiro Shimoda 		return sh_mmcif_error_manage(host);
199afb35666SYoshihiro Shimoda 
200afb35666SYoshihiro Shimoda 	host->wait_int = 0;
201afb35666SYoshihiro Shimoda 	blocksize = (BLOCK_SIZE_MASK &
202afb35666SYoshihiro Shimoda 			sh_mmcif_read(&host->regs->ce_block_set)) + 3;
203afb35666SYoshihiro Shimoda 	for (i = 0; i < blocksize / 4; i++)
204afb35666SYoshihiro Shimoda 		*p++ = sh_mmcif_read(&host->regs->ce_data);
205afb35666SYoshihiro Shimoda 
206afb35666SYoshihiro Shimoda 	/* buffer read end */
207afb35666SYoshihiro Shimoda 	sh_mmcif_bitset(MASK_MBUFRE, &host->regs->ce_int_mask);
208afb35666SYoshihiro Shimoda 	time = mmcif_wait_interrupt_flag(host);
209afb35666SYoshihiro Shimoda 	if (time == 0 || host->sd_error != 0)
210afb35666SYoshihiro Shimoda 		return sh_mmcif_error_manage(host);
211afb35666SYoshihiro Shimoda 
212afb35666SYoshihiro Shimoda 	host->wait_int = 0;
213afb35666SYoshihiro Shimoda 	return 0;
214afb35666SYoshihiro Shimoda }
215afb35666SYoshihiro Shimoda 
sh_mmcif_multi_read(struct sh_mmcif_host * host,struct mmc_data * data)216afb35666SYoshihiro Shimoda static int sh_mmcif_multi_read(struct sh_mmcif_host *host,
217afb35666SYoshihiro Shimoda 				struct mmc_data *data)
218afb35666SYoshihiro Shimoda {
219afb35666SYoshihiro Shimoda 	long time;
220afb35666SYoshihiro Shimoda 	u32 blocksize, i, j;
221afb35666SYoshihiro Shimoda 	unsigned long *p = (unsigned long *)data->dest;
222afb35666SYoshihiro Shimoda 
223afb35666SYoshihiro Shimoda 	if ((unsigned long)p & 0x00000001) {
224afb35666SYoshihiro Shimoda 		printf("%s: The data pointer is unaligned.", __func__);
225afb35666SYoshihiro Shimoda 		return -EIO;
226afb35666SYoshihiro Shimoda 	}
227afb35666SYoshihiro Shimoda 
228afb35666SYoshihiro Shimoda 	host->wait_int = 0;
229afb35666SYoshihiro Shimoda 	blocksize = BLOCK_SIZE_MASK & sh_mmcif_read(&host->regs->ce_block_set);
230afb35666SYoshihiro Shimoda 	for (j = 0; j < data->blocks; j++) {
231afb35666SYoshihiro Shimoda 		sh_mmcif_bitset(MASK_MBUFREN, &host->regs->ce_int_mask);
232afb35666SYoshihiro Shimoda 		time = mmcif_wait_interrupt_flag(host);
233afb35666SYoshihiro Shimoda 		if (time == 0 || host->sd_error != 0)
234afb35666SYoshihiro Shimoda 			return sh_mmcif_error_manage(host);
235afb35666SYoshihiro Shimoda 
236afb35666SYoshihiro Shimoda 		host->wait_int = 0;
237afb35666SYoshihiro Shimoda 		for (i = 0; i < blocksize / 4; i++)
238afb35666SYoshihiro Shimoda 			*p++ = sh_mmcif_read(&host->regs->ce_data);
239afb35666SYoshihiro Shimoda 
240afb35666SYoshihiro Shimoda 		WATCHDOG_RESET();
241afb35666SYoshihiro Shimoda 	}
242afb35666SYoshihiro Shimoda 	return 0;
243afb35666SYoshihiro Shimoda }
244afb35666SYoshihiro Shimoda 
sh_mmcif_single_write(struct sh_mmcif_host * host,struct mmc_data * data)245afb35666SYoshihiro Shimoda static int sh_mmcif_single_write(struct sh_mmcif_host *host,
246afb35666SYoshihiro Shimoda 				 struct mmc_data *data)
247afb35666SYoshihiro Shimoda {
248afb35666SYoshihiro Shimoda 	long time;
249afb35666SYoshihiro Shimoda 	u32 blocksize, i;
250afb35666SYoshihiro Shimoda 	const unsigned long *p = (unsigned long *)data->dest;
251afb35666SYoshihiro Shimoda 
252afb35666SYoshihiro Shimoda 	if ((unsigned long)p & 0x00000001) {
253afb35666SYoshihiro Shimoda 		printf("%s: The data pointer is unaligned.", __func__);
254afb35666SYoshihiro Shimoda 		return -EIO;
255afb35666SYoshihiro Shimoda 	}
256afb35666SYoshihiro Shimoda 
257afb35666SYoshihiro Shimoda 	host->wait_int = 0;
258afb35666SYoshihiro Shimoda 	sh_mmcif_bitset(MASK_MBUFWEN, &host->regs->ce_int_mask);
259afb35666SYoshihiro Shimoda 
260afb35666SYoshihiro Shimoda 	time = mmcif_wait_interrupt_flag(host);
261afb35666SYoshihiro Shimoda 	if (time == 0 || host->sd_error != 0)
262afb35666SYoshihiro Shimoda 		return sh_mmcif_error_manage(host);
263afb35666SYoshihiro Shimoda 
264afb35666SYoshihiro Shimoda 	host->wait_int = 0;
265afb35666SYoshihiro Shimoda 	blocksize = (BLOCK_SIZE_MASK &
266afb35666SYoshihiro Shimoda 			sh_mmcif_read(&host->regs->ce_block_set)) + 3;
267afb35666SYoshihiro Shimoda 	for (i = 0; i < blocksize / 4; i++)
268afb35666SYoshihiro Shimoda 		sh_mmcif_write(*p++, &host->regs->ce_data);
269afb35666SYoshihiro Shimoda 
270afb35666SYoshihiro Shimoda 	/* buffer write end */
271afb35666SYoshihiro Shimoda 	sh_mmcif_bitset(MASK_MDTRANE, &host->regs->ce_int_mask);
272afb35666SYoshihiro Shimoda 
273afb35666SYoshihiro Shimoda 	time = mmcif_wait_interrupt_flag(host);
274afb35666SYoshihiro Shimoda 	if (time == 0 || host->sd_error != 0)
275afb35666SYoshihiro Shimoda 		return sh_mmcif_error_manage(host);
276afb35666SYoshihiro Shimoda 
277afb35666SYoshihiro Shimoda 	host->wait_int = 0;
278afb35666SYoshihiro Shimoda 	return 0;
279afb35666SYoshihiro Shimoda }
280afb35666SYoshihiro Shimoda 
sh_mmcif_multi_write(struct sh_mmcif_host * host,struct mmc_data * data)281afb35666SYoshihiro Shimoda static int sh_mmcif_multi_write(struct sh_mmcif_host *host,
282afb35666SYoshihiro Shimoda 				struct mmc_data *data)
283afb35666SYoshihiro Shimoda {
284afb35666SYoshihiro Shimoda 	long time;
285afb35666SYoshihiro Shimoda 	u32 i, j, blocksize;
286afb35666SYoshihiro Shimoda 	const unsigned long *p = (unsigned long *)data->dest;
287afb35666SYoshihiro Shimoda 
288afb35666SYoshihiro Shimoda 	if ((unsigned long)p & 0x00000001) {
289afb35666SYoshihiro Shimoda 		printf("%s: The data pointer is unaligned.", __func__);
290afb35666SYoshihiro Shimoda 		return -EIO;
291afb35666SYoshihiro Shimoda 	}
292afb35666SYoshihiro Shimoda 
293afb35666SYoshihiro Shimoda 	host->wait_int = 0;
294afb35666SYoshihiro Shimoda 	blocksize = BLOCK_SIZE_MASK & sh_mmcif_read(&host->regs->ce_block_set);
295afb35666SYoshihiro Shimoda 	for (j = 0; j < data->blocks; j++) {
296afb35666SYoshihiro Shimoda 		sh_mmcif_bitset(MASK_MBUFWEN, &host->regs->ce_int_mask);
297afb35666SYoshihiro Shimoda 
298afb35666SYoshihiro Shimoda 		time = mmcif_wait_interrupt_flag(host);
299afb35666SYoshihiro Shimoda 
300afb35666SYoshihiro Shimoda 		if (time == 0 || host->sd_error != 0)
301afb35666SYoshihiro Shimoda 			return sh_mmcif_error_manage(host);
302afb35666SYoshihiro Shimoda 
303afb35666SYoshihiro Shimoda 		host->wait_int = 0;
304afb35666SYoshihiro Shimoda 		for (i = 0; i < blocksize / 4; i++)
305afb35666SYoshihiro Shimoda 			sh_mmcif_write(*p++, &host->regs->ce_data);
306afb35666SYoshihiro Shimoda 
307afb35666SYoshihiro Shimoda 		WATCHDOG_RESET();
308afb35666SYoshihiro Shimoda 	}
309afb35666SYoshihiro Shimoda 	return 0;
310afb35666SYoshihiro Shimoda }
311afb35666SYoshihiro Shimoda 
sh_mmcif_get_response(struct sh_mmcif_host * host,struct mmc_cmd * cmd)312afb35666SYoshihiro Shimoda static void sh_mmcif_get_response(struct sh_mmcif_host *host,
313afb35666SYoshihiro Shimoda 					struct mmc_cmd *cmd)
314afb35666SYoshihiro Shimoda {
315afb35666SYoshihiro Shimoda 	if (cmd->resp_type & MMC_RSP_136) {
316afb35666SYoshihiro Shimoda 		cmd->response[0] = sh_mmcif_read(&host->regs->ce_resp3);
317afb35666SYoshihiro Shimoda 		cmd->response[1] = sh_mmcif_read(&host->regs->ce_resp2);
318afb35666SYoshihiro Shimoda 		cmd->response[2] = sh_mmcif_read(&host->regs->ce_resp1);
319afb35666SYoshihiro Shimoda 		cmd->response[3] = sh_mmcif_read(&host->regs->ce_resp0);
320afb35666SYoshihiro Shimoda 		debug(" RESP %08x, %08x, %08x, %08x\n", cmd->response[0],
321afb35666SYoshihiro Shimoda 			 cmd->response[1], cmd->response[2], cmd->response[3]);
322afb35666SYoshihiro Shimoda 	} else {
323afb35666SYoshihiro Shimoda 		cmd->response[0] = sh_mmcif_read(&host->regs->ce_resp0);
324afb35666SYoshihiro Shimoda 	}
325afb35666SYoshihiro Shimoda }
326afb35666SYoshihiro Shimoda 
sh_mmcif_get_cmd12response(struct sh_mmcif_host * host,struct mmc_cmd * cmd)327afb35666SYoshihiro Shimoda static void sh_mmcif_get_cmd12response(struct sh_mmcif_host *host,
328afb35666SYoshihiro Shimoda 					struct mmc_cmd *cmd)
329afb35666SYoshihiro Shimoda {
330afb35666SYoshihiro Shimoda 	cmd->response[0] = sh_mmcif_read(&host->regs->ce_resp_cmd12);
331afb35666SYoshihiro Shimoda }
332afb35666SYoshihiro Shimoda 
sh_mmcif_set_cmd(struct sh_mmcif_host * host,struct mmc_data * data,struct mmc_cmd * cmd)333afb35666SYoshihiro Shimoda static u32 sh_mmcif_set_cmd(struct sh_mmcif_host *host,
334afb35666SYoshihiro Shimoda 				struct mmc_data *data, struct mmc_cmd *cmd)
335afb35666SYoshihiro Shimoda {
336afb35666SYoshihiro Shimoda 	u32 tmp = 0;
337afb35666SYoshihiro Shimoda 	u32 opc = cmd->cmdidx;
338afb35666SYoshihiro Shimoda 
339afb35666SYoshihiro Shimoda 	/* Response Type check */
340afb35666SYoshihiro Shimoda 	switch (cmd->resp_type) {
341afb35666SYoshihiro Shimoda 	case MMC_RSP_NONE:
342afb35666SYoshihiro Shimoda 		tmp |= CMD_SET_RTYP_NO;
343afb35666SYoshihiro Shimoda 		break;
344afb35666SYoshihiro Shimoda 	case MMC_RSP_R1:
345afb35666SYoshihiro Shimoda 	case MMC_RSP_R1b:
346afb35666SYoshihiro Shimoda 	case MMC_RSP_R3:
347afb35666SYoshihiro Shimoda 		tmp |= CMD_SET_RTYP_6B;
348afb35666SYoshihiro Shimoda 		break;
349afb35666SYoshihiro Shimoda 	case MMC_RSP_R2:
350afb35666SYoshihiro Shimoda 		tmp |= CMD_SET_RTYP_17B;
351afb35666SYoshihiro Shimoda 		break;
352afb35666SYoshihiro Shimoda 	default:
353afb35666SYoshihiro Shimoda 		printf(DRIVER_NAME": Not support type response.\n");
354afb35666SYoshihiro Shimoda 		break;
355afb35666SYoshihiro Shimoda 	}
356afb35666SYoshihiro Shimoda 
357afb35666SYoshihiro Shimoda 	/* RBSY */
358afb35666SYoshihiro Shimoda 	if (opc == MMC_CMD_SWITCH)
359afb35666SYoshihiro Shimoda 		tmp |= CMD_SET_RBSY;
360afb35666SYoshihiro Shimoda 
361afb35666SYoshihiro Shimoda 	/* WDAT / DATW */
362afb35666SYoshihiro Shimoda 	if (host->data) {
363afb35666SYoshihiro Shimoda 		tmp |= CMD_SET_WDAT;
364afb35666SYoshihiro Shimoda 		switch (host->bus_width) {
365afb35666SYoshihiro Shimoda 		case MMC_BUS_WIDTH_1:
366afb35666SYoshihiro Shimoda 			tmp |= CMD_SET_DATW_1;
367afb35666SYoshihiro Shimoda 			break;
368afb35666SYoshihiro Shimoda 		case MMC_BUS_WIDTH_4:
369afb35666SYoshihiro Shimoda 			tmp |= CMD_SET_DATW_4;
370afb35666SYoshihiro Shimoda 			break;
371afb35666SYoshihiro Shimoda 		case MMC_BUS_WIDTH_8:
372afb35666SYoshihiro Shimoda 			tmp |= CMD_SET_DATW_8;
373afb35666SYoshihiro Shimoda 			break;
374afb35666SYoshihiro Shimoda 		default:
375afb35666SYoshihiro Shimoda 			printf(DRIVER_NAME": Not support bus width.\n");
376afb35666SYoshihiro Shimoda 			break;
377afb35666SYoshihiro Shimoda 		}
378afb35666SYoshihiro Shimoda 	}
379afb35666SYoshihiro Shimoda 	/* DWEN */
380afb35666SYoshihiro Shimoda 	if (opc == MMC_CMD_WRITE_SINGLE_BLOCK ||
381afb35666SYoshihiro Shimoda 	    opc == MMC_CMD_WRITE_MULTIPLE_BLOCK)
382afb35666SYoshihiro Shimoda 		tmp |= CMD_SET_DWEN;
383afb35666SYoshihiro Shimoda 	/* CMLTE/CMD12EN */
384afb35666SYoshihiro Shimoda 	if (opc == MMC_CMD_READ_MULTIPLE_BLOCK ||
385afb35666SYoshihiro Shimoda 	    opc == MMC_CMD_WRITE_MULTIPLE_BLOCK) {
386afb35666SYoshihiro Shimoda 		tmp |= CMD_SET_CMLTE | CMD_SET_CMD12EN;
387afb35666SYoshihiro Shimoda 		sh_mmcif_bitset(data->blocks << 16, &host->regs->ce_block_set);
388afb35666SYoshihiro Shimoda 	}
389afb35666SYoshihiro Shimoda 	/* RIDXC[1:0] check bits */
390afb35666SYoshihiro Shimoda 	if (opc == MMC_CMD_SEND_OP_COND || opc == MMC_CMD_ALL_SEND_CID ||
391afb35666SYoshihiro Shimoda 	    opc == MMC_CMD_SEND_CSD || opc == MMC_CMD_SEND_CID)
392afb35666SYoshihiro Shimoda 		tmp |= CMD_SET_RIDXC_BITS;
393afb35666SYoshihiro Shimoda 	/* RCRC7C[1:0] check bits */
394afb35666SYoshihiro Shimoda 	if (opc == MMC_CMD_SEND_OP_COND)
395afb35666SYoshihiro Shimoda 		tmp |= CMD_SET_CRC7C_BITS;
396afb35666SYoshihiro Shimoda 	/* RCRC7C[1:0] internal CRC7 */
397afb35666SYoshihiro Shimoda 	if (opc == MMC_CMD_ALL_SEND_CID ||
398afb35666SYoshihiro Shimoda 		opc == MMC_CMD_SEND_CSD || opc == MMC_CMD_SEND_CID)
399afb35666SYoshihiro Shimoda 		tmp |= CMD_SET_CRC7C_INTERNAL;
400afb35666SYoshihiro Shimoda 
401afb35666SYoshihiro Shimoda 	return opc = ((opc << 24) | tmp);
402afb35666SYoshihiro Shimoda }
403afb35666SYoshihiro Shimoda 
sh_mmcif_data_trans(struct sh_mmcif_host * host,struct mmc_data * data,u16 opc)404afb35666SYoshihiro Shimoda static u32 sh_mmcif_data_trans(struct sh_mmcif_host *host,
405afb35666SYoshihiro Shimoda 				struct mmc_data *data, u16 opc)
406afb35666SYoshihiro Shimoda {
407afb35666SYoshihiro Shimoda 	u32 ret;
408afb35666SYoshihiro Shimoda 
409afb35666SYoshihiro Shimoda 	switch (opc) {
410afb35666SYoshihiro Shimoda 	case MMC_CMD_READ_MULTIPLE_BLOCK:
411afb35666SYoshihiro Shimoda 		ret = sh_mmcif_multi_read(host, data);
412afb35666SYoshihiro Shimoda 		break;
413afb35666SYoshihiro Shimoda 	case MMC_CMD_WRITE_MULTIPLE_BLOCK:
414afb35666SYoshihiro Shimoda 		ret = sh_mmcif_multi_write(host, data);
415afb35666SYoshihiro Shimoda 		break;
416afb35666SYoshihiro Shimoda 	case MMC_CMD_WRITE_SINGLE_BLOCK:
417afb35666SYoshihiro Shimoda 		ret = sh_mmcif_single_write(host, data);
418afb35666SYoshihiro Shimoda 		break;
419afb35666SYoshihiro Shimoda 	case MMC_CMD_READ_SINGLE_BLOCK:
420afb35666SYoshihiro Shimoda 	case MMC_CMD_SEND_EXT_CSD:
421afb35666SYoshihiro Shimoda 		ret = sh_mmcif_single_read(host, data);
422afb35666SYoshihiro Shimoda 		break;
423afb35666SYoshihiro Shimoda 	default:
424afb35666SYoshihiro Shimoda 		printf(DRIVER_NAME": NOT SUPPORT CMD = d'%08d\n", opc);
425afb35666SYoshihiro Shimoda 		ret = -EINVAL;
426afb35666SYoshihiro Shimoda 		break;
427afb35666SYoshihiro Shimoda 	}
428afb35666SYoshihiro Shimoda 	return ret;
429afb35666SYoshihiro Shimoda }
430afb35666SYoshihiro Shimoda 
sh_mmcif_start_cmd(struct sh_mmcif_host * host,struct mmc_data * data,struct mmc_cmd * cmd)431afb35666SYoshihiro Shimoda static int sh_mmcif_start_cmd(struct sh_mmcif_host *host,
432afb35666SYoshihiro Shimoda 				struct mmc_data *data, struct mmc_cmd *cmd)
433afb35666SYoshihiro Shimoda {
434afb35666SYoshihiro Shimoda 	long time;
435afb35666SYoshihiro Shimoda 	int ret = 0, mask = 0;
436afb35666SYoshihiro Shimoda 	u32 opc = cmd->cmdidx;
437afb35666SYoshihiro Shimoda 
438afb35666SYoshihiro Shimoda 	if (opc == MMC_CMD_STOP_TRANSMISSION) {
439afb35666SYoshihiro Shimoda 		/* MMCIF sends the STOP command automatically */
440afb35666SYoshihiro Shimoda 		if (host->last_cmd == MMC_CMD_READ_MULTIPLE_BLOCK)
441afb35666SYoshihiro Shimoda 			sh_mmcif_bitset(MASK_MCMD12DRE,
442afb35666SYoshihiro Shimoda 					&host->regs->ce_int_mask);
443afb35666SYoshihiro Shimoda 		else
444afb35666SYoshihiro Shimoda 			sh_mmcif_bitset(MASK_MCMD12RBE,
445afb35666SYoshihiro Shimoda 					&host->regs->ce_int_mask);
446afb35666SYoshihiro Shimoda 
447afb35666SYoshihiro Shimoda 		time = mmcif_wait_interrupt_flag(host);
448afb35666SYoshihiro Shimoda 		if (time == 0 || host->sd_error != 0)
449afb35666SYoshihiro Shimoda 			return sh_mmcif_error_manage(host);
450afb35666SYoshihiro Shimoda 
451afb35666SYoshihiro Shimoda 		sh_mmcif_get_cmd12response(host, cmd);
452afb35666SYoshihiro Shimoda 		return 0;
453afb35666SYoshihiro Shimoda 	}
454afb35666SYoshihiro Shimoda 	if (opc == MMC_CMD_SWITCH)
455afb35666SYoshihiro Shimoda 		mask = MASK_MRBSYE;
456afb35666SYoshihiro Shimoda 	else
457afb35666SYoshihiro Shimoda 		mask = MASK_MCRSPE;
458afb35666SYoshihiro Shimoda 
459afb35666SYoshihiro Shimoda 	mask |=	MASK_MCMDVIO | MASK_MBUFVIO | MASK_MWDATERR |
460afb35666SYoshihiro Shimoda 		MASK_MRDATERR | MASK_MRIDXERR | MASK_MRSPERR |
461afb35666SYoshihiro Shimoda 		MASK_MCCSTO | MASK_MCRCSTO | MASK_MWDATTO |
462afb35666SYoshihiro Shimoda 		MASK_MRDATTO | MASK_MRBSYTO | MASK_MRSPTO;
463afb35666SYoshihiro Shimoda 
464afb35666SYoshihiro Shimoda 	if (host->data) {
465afb35666SYoshihiro Shimoda 		sh_mmcif_write(0, &host->regs->ce_block_set);
466afb35666SYoshihiro Shimoda 		sh_mmcif_write(data->blocksize, &host->regs->ce_block_set);
467afb35666SYoshihiro Shimoda 	}
468afb35666SYoshihiro Shimoda 	opc = sh_mmcif_set_cmd(host, data, cmd);
469afb35666SYoshihiro Shimoda 
470afb35666SYoshihiro Shimoda 	sh_mmcif_write(INT_START_MAGIC, &host->regs->ce_int);
471afb35666SYoshihiro Shimoda 	sh_mmcif_write(mask, &host->regs->ce_int_mask);
472afb35666SYoshihiro Shimoda 
473afb35666SYoshihiro Shimoda 	debug("CMD%d ARG:%08x\n", cmd->cmdidx, cmd->cmdarg);
474afb35666SYoshihiro Shimoda 	/* set arg */
475afb35666SYoshihiro Shimoda 	sh_mmcif_write(cmd->cmdarg, &host->regs->ce_arg);
476afb35666SYoshihiro Shimoda 	host->wait_int = 0;
477afb35666SYoshihiro Shimoda 	/* set cmd */
478afb35666SYoshihiro Shimoda 	sh_mmcif_write(opc, &host->regs->ce_cmd_set);
479afb35666SYoshihiro Shimoda 
480afb35666SYoshihiro Shimoda 	time = mmcif_wait_interrupt_flag(host);
481afb35666SYoshihiro Shimoda 	if (time == 0)
482afb35666SYoshihiro Shimoda 		return sh_mmcif_error_manage(host);
483afb35666SYoshihiro Shimoda 
484afb35666SYoshihiro Shimoda 	if (host->sd_error) {
485afb35666SYoshihiro Shimoda 		switch (cmd->cmdidx) {
486afb35666SYoshihiro Shimoda 		case MMC_CMD_ALL_SEND_CID:
487afb35666SYoshihiro Shimoda 		case MMC_CMD_SELECT_CARD:
488afb35666SYoshihiro Shimoda 		case MMC_CMD_APP_CMD:
489915ffa52SJaehoon Chung 			ret = -ETIMEDOUT;
490afb35666SYoshihiro Shimoda 			break;
491afb35666SYoshihiro Shimoda 		default:
492afb35666SYoshihiro Shimoda 			printf(DRIVER_NAME": Cmd(d'%d) err\n", cmd->cmdidx);
493afb35666SYoshihiro Shimoda 			ret = sh_mmcif_error_manage(host);
494afb35666SYoshihiro Shimoda 			break;
495afb35666SYoshihiro Shimoda 		}
496afb35666SYoshihiro Shimoda 		host->sd_error = 0;
497afb35666SYoshihiro Shimoda 		host->wait_int = 0;
498afb35666SYoshihiro Shimoda 		return ret;
499afb35666SYoshihiro Shimoda 	}
500afb35666SYoshihiro Shimoda 
501afb35666SYoshihiro Shimoda 	/* if no response */
502afb35666SYoshihiro Shimoda 	if (!(opc & 0x00C00000))
503afb35666SYoshihiro Shimoda 		return 0;
504afb35666SYoshihiro Shimoda 
505afb35666SYoshihiro Shimoda 	if (host->wait_int == 1) {
506afb35666SYoshihiro Shimoda 		sh_mmcif_get_response(host, cmd);
507afb35666SYoshihiro Shimoda 		host->wait_int = 0;
508afb35666SYoshihiro Shimoda 	}
509afb35666SYoshihiro Shimoda 	if (host->data)
510afb35666SYoshihiro Shimoda 		ret = sh_mmcif_data_trans(host, data, cmd->cmdidx);
511afb35666SYoshihiro Shimoda 	host->last_cmd = cmd->cmdidx;
512afb35666SYoshihiro Shimoda 
513afb35666SYoshihiro Shimoda 	return ret;
514afb35666SYoshihiro Shimoda }
515afb35666SYoshihiro Shimoda 
sh_mmcif_send_cmd_common(struct sh_mmcif_host * host,struct mmc_cmd * cmd,struct mmc_data * data)51648f54a2dSMarek Vasut static int sh_mmcif_send_cmd_common(struct sh_mmcif_host *host,
51748f54a2dSMarek Vasut 				    struct mmc_cmd *cmd, struct mmc_data *data)
518afb35666SYoshihiro Shimoda {
519afb35666SYoshihiro Shimoda 	int ret;
520afb35666SYoshihiro Shimoda 
521afb35666SYoshihiro Shimoda 	WATCHDOG_RESET();
522afb35666SYoshihiro Shimoda 
523afb35666SYoshihiro Shimoda 	switch (cmd->cmdidx) {
524afb35666SYoshihiro Shimoda 	case MMC_CMD_APP_CMD:
525915ffa52SJaehoon Chung 		return -ETIMEDOUT;
526afb35666SYoshihiro Shimoda 	case MMC_CMD_SEND_EXT_CSD: /* = SD_SEND_IF_COND (8) */
527afb35666SYoshihiro Shimoda 		if (data)
528afb35666SYoshihiro Shimoda 			/* ext_csd */
529afb35666SYoshihiro Shimoda 			break;
530afb35666SYoshihiro Shimoda 		else
531afb35666SYoshihiro Shimoda 			/* send_if_cond cmd (not support) */
532915ffa52SJaehoon Chung 			return -ETIMEDOUT;
533afb35666SYoshihiro Shimoda 	default:
534afb35666SYoshihiro Shimoda 		break;
535afb35666SYoshihiro Shimoda 	}
536afb35666SYoshihiro Shimoda 	host->sd_error = 0;
537afb35666SYoshihiro Shimoda 	host->data = data;
538afb35666SYoshihiro Shimoda 	ret = sh_mmcif_start_cmd(host, data, cmd);
539afb35666SYoshihiro Shimoda 	host->data = NULL;
540afb35666SYoshihiro Shimoda 
541afb35666SYoshihiro Shimoda 	return ret;
542afb35666SYoshihiro Shimoda }
543afb35666SYoshihiro Shimoda 
sh_mmcif_set_ios_common(struct sh_mmcif_host * host,struct mmc * mmc)54448f54a2dSMarek Vasut static int sh_mmcif_set_ios_common(struct sh_mmcif_host *host, struct mmc *mmc)
545afb35666SYoshihiro Shimoda {
546afb35666SYoshihiro Shimoda 	if (mmc->clock)
547afb35666SYoshihiro Shimoda 		sh_mmcif_clock_control(host, mmc->clock);
548afb35666SYoshihiro Shimoda 
549afb35666SYoshihiro Shimoda 	if (mmc->bus_width == 8)
550afb35666SYoshihiro Shimoda 		host->bus_width = MMC_BUS_WIDTH_8;
551afb35666SYoshihiro Shimoda 	else if (mmc->bus_width == 4)
552afb35666SYoshihiro Shimoda 		host->bus_width = MMC_BUS_WIDTH_4;
553afb35666SYoshihiro Shimoda 	else
554afb35666SYoshihiro Shimoda 		host->bus_width = MMC_BUS_WIDTH_1;
555afb35666SYoshihiro Shimoda 
556afb35666SYoshihiro Shimoda 	debug("clock = %d, buswidth = %d\n", mmc->clock, mmc->bus_width);
55707b0b9c0SJaehoon Chung 
55807b0b9c0SJaehoon Chung 	return 0;
559afb35666SYoshihiro Shimoda }
560afb35666SYoshihiro Shimoda 
sh_mmcif_initialize_common(struct sh_mmcif_host * host)56148f54a2dSMarek Vasut static int sh_mmcif_initialize_common(struct sh_mmcif_host *host)
562afb35666SYoshihiro Shimoda {
563afb35666SYoshihiro Shimoda 	sh_mmcif_sync_reset(host);
564afb35666SYoshihiro Shimoda 	sh_mmcif_write(MASK_ALL, &host->regs->ce_int_mask);
565afb35666SYoshihiro Shimoda 	return 0;
566afb35666SYoshihiro Shimoda }
567afb35666SYoshihiro Shimoda 
56848f54a2dSMarek Vasut #ifndef CONFIG_DM_MMC
mmc_priv(struct mmc * mmc)56948f54a2dSMarek Vasut static void *mmc_priv(struct mmc *mmc)
57048f54a2dSMarek Vasut {
57148f54a2dSMarek Vasut 	return (void *)mmc->priv;
57248f54a2dSMarek Vasut }
57348f54a2dSMarek Vasut 
sh_mmcif_send_cmd(struct mmc * mmc,struct mmc_cmd * cmd,struct mmc_data * data)57448f54a2dSMarek Vasut static int sh_mmcif_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
57548f54a2dSMarek Vasut 			    struct mmc_data *data)
57648f54a2dSMarek Vasut {
57748f54a2dSMarek Vasut 	struct sh_mmcif_host *host = mmc_priv(mmc);
57848f54a2dSMarek Vasut 
57948f54a2dSMarek Vasut 	return sh_mmcif_send_cmd_common(host, cmd, data);
58048f54a2dSMarek Vasut }
58148f54a2dSMarek Vasut 
sh_mmcif_set_ios(struct mmc * mmc)58248f54a2dSMarek Vasut static int sh_mmcif_set_ios(struct mmc *mmc)
58348f54a2dSMarek Vasut {
58448f54a2dSMarek Vasut 	struct sh_mmcif_host *host = mmc_priv(mmc);
58548f54a2dSMarek Vasut 
58648f54a2dSMarek Vasut 	return sh_mmcif_set_ios_common(host, mmc);
58748f54a2dSMarek Vasut }
58848f54a2dSMarek Vasut 
sh_mmcif_initialize(struct mmc * mmc)58948f54a2dSMarek Vasut static int sh_mmcif_initialize(struct mmc *mmc)
59048f54a2dSMarek Vasut {
59148f54a2dSMarek Vasut 	struct sh_mmcif_host *host = mmc_priv(mmc);
59248f54a2dSMarek Vasut 
59348f54a2dSMarek Vasut 	return sh_mmcif_initialize_common(host);
59448f54a2dSMarek Vasut }
59548f54a2dSMarek Vasut 
596ab769f22SPantelis Antoniou static const struct mmc_ops sh_mmcif_ops = {
59748f54a2dSMarek Vasut 	.send_cmd       = sh_mmcif_send_cmd,
598ab769f22SPantelis Antoniou 	.set_ios        = sh_mmcif_set_ios,
59948f54a2dSMarek Vasut 	.init           = sh_mmcif_initialize,
600ab769f22SPantelis Antoniou };
601ab769f22SPantelis Antoniou 
60293bfd616SPantelis Antoniou static struct mmc_config sh_mmcif_cfg = {
60393bfd616SPantelis Antoniou 	.name		= DRIVER_NAME,
60493bfd616SPantelis Antoniou 	.ops		= &sh_mmcif_ops,
60593bfd616SPantelis Antoniou 	.host_caps	= MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT |
6065a20397bSRob Herring 			  MMC_MODE_8BIT,
607cd2bf484SNobuhiro Iwamatsu 	.voltages	= MMC_VDD_32_33 | MMC_VDD_33_34,
60893bfd616SPantelis Antoniou 	.b_max		= CONFIG_SYS_MMC_MAX_BLK_COUNT,
60993bfd616SPantelis Antoniou };
61093bfd616SPantelis Antoniou 
mmcif_mmc_init(void)611afb35666SYoshihiro Shimoda int mmcif_mmc_init(void)
612afb35666SYoshihiro Shimoda {
613afb35666SYoshihiro Shimoda 	struct mmc *mmc;
614afb35666SYoshihiro Shimoda 	struct sh_mmcif_host *host = NULL;
615afb35666SYoshihiro Shimoda 
616afb35666SYoshihiro Shimoda 	host = malloc(sizeof(struct sh_mmcif_host));
617afb35666SYoshihiro Shimoda 	if (!host)
61874c32ef5SNobuhiro Iwamatsu 		return -ENOMEM;
619afb35666SYoshihiro Shimoda 	memset(host, 0, sizeof(*host));
620afb35666SYoshihiro Shimoda 
621afb35666SYoshihiro Shimoda 	host->regs = (struct sh_mmcif_regs *)CONFIG_SH_MMCIF_ADDR;
622afb35666SYoshihiro Shimoda 	host->clk = CONFIG_SH_MMCIF_CLK;
623afb35666SYoshihiro Shimoda 
6247a7eb983SNobuhiro Iwamatsu 	sh_mmcif_cfg.f_min = MMC_CLK_DIV_MIN(host->clk);
6259675f610SNobuhiro Iwamatsu 	sh_mmcif_cfg.f_max = MMC_CLK_DIV_MAX(host->clk);
6267a7eb983SNobuhiro Iwamatsu 
62793bfd616SPantelis Antoniou 	mmc = mmc_create(&sh_mmcif_cfg, host);
62893bfd616SPantelis Antoniou 	if (mmc == NULL) {
62993bfd616SPantelis Antoniou 		free(host);
63093bfd616SPantelis Antoniou 		return -ENOMEM;
63193bfd616SPantelis Antoniou 	}
632afb35666SYoshihiro Shimoda 
63393bfd616SPantelis Antoniou 	return 0;
634afb35666SYoshihiro Shimoda }
63548f54a2dSMarek Vasut 
63648f54a2dSMarek Vasut #else
63748f54a2dSMarek Vasut struct sh_mmcif_plat {
63848f54a2dSMarek Vasut 	struct mmc_config cfg;
63948f54a2dSMarek Vasut 	struct mmc mmc;
64048f54a2dSMarek Vasut };
64148f54a2dSMarek Vasut 
sh_mmcif_dm_send_cmd(struct udevice * dev,struct mmc_cmd * cmd,struct mmc_data * data)64248f54a2dSMarek Vasut int sh_mmcif_dm_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
64348f54a2dSMarek Vasut 			struct mmc_data *data)
64448f54a2dSMarek Vasut {
64548f54a2dSMarek Vasut 	struct sh_mmcif_host *host = dev_get_priv(dev);
64648f54a2dSMarek Vasut 
64748f54a2dSMarek Vasut 	return sh_mmcif_send_cmd_common(host, cmd, data);
64848f54a2dSMarek Vasut }
64948f54a2dSMarek Vasut 
sh_mmcif_dm_set_ios(struct udevice * dev)65048f54a2dSMarek Vasut int sh_mmcif_dm_set_ios(struct udevice *dev)
65148f54a2dSMarek Vasut {
65248f54a2dSMarek Vasut 	struct sh_mmcif_host *host = dev_get_priv(dev);
65348f54a2dSMarek Vasut 	struct mmc *mmc = mmc_get_mmc_dev(dev);
65448f54a2dSMarek Vasut 
65548f54a2dSMarek Vasut 	return sh_mmcif_set_ios_common(host, mmc);
65648f54a2dSMarek Vasut }
65748f54a2dSMarek Vasut 
65848f54a2dSMarek Vasut static const struct dm_mmc_ops sh_mmcif_dm_ops = {
65948f54a2dSMarek Vasut 	.send_cmd	= sh_mmcif_dm_send_cmd,
66048f54a2dSMarek Vasut 	.set_ios	= sh_mmcif_dm_set_ios,
66148f54a2dSMarek Vasut };
66248f54a2dSMarek Vasut 
sh_mmcif_dm_bind(struct udevice * dev)66348f54a2dSMarek Vasut static int sh_mmcif_dm_bind(struct udevice *dev)
66448f54a2dSMarek Vasut {
66548f54a2dSMarek Vasut 	struct sh_mmcif_plat *plat = dev_get_platdata(dev);
66648f54a2dSMarek Vasut 
66748f54a2dSMarek Vasut 	return mmc_bind(dev, &plat->mmc, &plat->cfg);
66848f54a2dSMarek Vasut }
66948f54a2dSMarek Vasut 
sh_mmcif_dm_probe(struct udevice * dev)67048f54a2dSMarek Vasut static int sh_mmcif_dm_probe(struct udevice *dev)
67148f54a2dSMarek Vasut {
67248f54a2dSMarek Vasut 	struct sh_mmcif_plat *plat = dev_get_platdata(dev);
67348f54a2dSMarek Vasut 	struct sh_mmcif_host *host = dev_get_priv(dev);
67448f54a2dSMarek Vasut 	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
67548f54a2dSMarek Vasut 	struct clk sh_mmcif_clk;
67648f54a2dSMarek Vasut 	fdt_addr_t base;
67748f54a2dSMarek Vasut 	int ret;
67848f54a2dSMarek Vasut 
67948f54a2dSMarek Vasut 	base = devfdt_get_addr(dev);
68048f54a2dSMarek Vasut 	if (base == FDT_ADDR_T_NONE)
68148f54a2dSMarek Vasut 		return -EINVAL;
68248f54a2dSMarek Vasut 
68348f54a2dSMarek Vasut 	host->regs = (struct sh_mmcif_regs *)devm_ioremap(dev, base, SZ_2K);
68448f54a2dSMarek Vasut 	if (!host->regs)
68548f54a2dSMarek Vasut 		return -ENOMEM;
68648f54a2dSMarek Vasut 
68748f54a2dSMarek Vasut 	ret = clk_get_by_index(dev, 0, &sh_mmcif_clk);
68848f54a2dSMarek Vasut 	if (ret) {
68948f54a2dSMarek Vasut 		debug("failed to get clock, ret=%d\n", ret);
69048f54a2dSMarek Vasut 		return ret;
69148f54a2dSMarek Vasut 	}
69248f54a2dSMarek Vasut 
69348f54a2dSMarek Vasut 	ret = clk_enable(&sh_mmcif_clk);
69448f54a2dSMarek Vasut 	if (ret) {
69548f54a2dSMarek Vasut 		debug("failed to enable clock, ret=%d\n", ret);
69648f54a2dSMarek Vasut 		return ret;
69748f54a2dSMarek Vasut 	}
69848f54a2dSMarek Vasut 
699*f4eaa56aSMarek Vasut 	host->clk = clk_set_rate(&sh_mmcif_clk, 97500000);
70048f54a2dSMarek Vasut 
70148f54a2dSMarek Vasut 	plat->cfg.name = dev->name;
70248f54a2dSMarek Vasut 	plat->cfg.host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
70348f54a2dSMarek Vasut 
70448f54a2dSMarek Vasut 	switch (fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "bus-width",
70548f54a2dSMarek Vasut 			       1)) {
70648f54a2dSMarek Vasut 	case 8:
70748f54a2dSMarek Vasut 		plat->cfg.host_caps |= MMC_MODE_8BIT;
70848f54a2dSMarek Vasut 		break;
70948f54a2dSMarek Vasut 	case 4:
71048f54a2dSMarek Vasut 		plat->cfg.host_caps |= MMC_MODE_4BIT;
71148f54a2dSMarek Vasut 		break;
71248f54a2dSMarek Vasut 	case 1:
71348f54a2dSMarek Vasut 		break;
71448f54a2dSMarek Vasut 	default:
71548f54a2dSMarek Vasut 		dev_err(dev, "Invalid \"bus-width\" value\n");
71648f54a2dSMarek Vasut 		return -EINVAL;
71748f54a2dSMarek Vasut 	}
71848f54a2dSMarek Vasut 
71948f54a2dSMarek Vasut 	sh_mmcif_initialize_common(host);
72048f54a2dSMarek Vasut 
72148f54a2dSMarek Vasut 	plat->cfg.voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
72248f54a2dSMarek Vasut 	plat->cfg.f_min = MMC_CLK_DIV_MIN(host->clk);
72348f54a2dSMarek Vasut 	plat->cfg.f_max = MMC_CLK_DIV_MAX(host->clk);
72448f54a2dSMarek Vasut 	plat->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
72548f54a2dSMarek Vasut 
72648f54a2dSMarek Vasut 	upriv->mmc = &plat->mmc;
72748f54a2dSMarek Vasut 
72848f54a2dSMarek Vasut 	return 0;
72948f54a2dSMarek Vasut }
73048f54a2dSMarek Vasut 
73148f54a2dSMarek Vasut static const struct udevice_id sh_mmcif_sd_match[] = {
73248f54a2dSMarek Vasut 	{ .compatible = "renesas,sh-mmcif" },
73348f54a2dSMarek Vasut 	{ /* sentinel */ }
73448f54a2dSMarek Vasut };
73548f54a2dSMarek Vasut 
73648f54a2dSMarek Vasut U_BOOT_DRIVER(sh_mmcif_mmc) = {
73748f54a2dSMarek Vasut 	.name			= "sh-mmcif",
73848f54a2dSMarek Vasut 	.id			= UCLASS_MMC,
73948f54a2dSMarek Vasut 	.of_match		= sh_mmcif_sd_match,
74048f54a2dSMarek Vasut 	.bind			= sh_mmcif_dm_bind,
74148f54a2dSMarek Vasut 	.probe			= sh_mmcif_dm_probe,
74248f54a2dSMarek Vasut 	.priv_auto_alloc_size	= sizeof(struct sh_mmcif_host),
74348f54a2dSMarek Vasut 	.platdata_auto_alloc_size = sizeof(struct sh_mmcif_plat),
74448f54a2dSMarek Vasut 	.ops			= &sh_mmcif_dm_ops,
74548f54a2dSMarek Vasut };
74648f54a2dSMarek Vasut #endif
747