xref: /openbmc/u-boot/drivers/mmc/gen_atmel_mci.c (revision d2397817f12d246cfd88caefd6f12dfd3e2d2c17)
1 /*
2  * Copyright (C) 2010
3  * Rob Emanuele <rob@emanuele.us>
4  * Reinhard Meyer, EMK Elektronik <reinhard.meyer@emk-elektronik.de>
5  *
6  * Original Driver:
7  * Copyright (C) 2004-2006 Atmel Corporation
8  *
9  * See file CREDITS for list of people who contributed to this
10  * project.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation; either version 2 of
15  * the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25  * MA 02111-1307 USA
26  */
27 
28 #include <common.h>
29 #include <mmc.h>
30 #include <part.h>
31 #include <malloc.h>
32 #include <asm/io.h>
33 #include <asm/errno.h>
34 #include <asm/byteorder.h>
35 #include <asm/arch/clk.h>
36 #include <asm/arch/hardware.h>
37 #include "atmel_mci.h"
38 
39 #ifndef CONFIG_SYS_MMC_CLK_OD
40 # define CONFIG_SYS_MMC_CLK_OD	150000
41 #endif
42 
43 #define MMC_DEFAULT_BLKLEN	512
44 
45 #if defined(CONFIG_ATMEL_MCI_PORTB)
46 # define MCI_BUS 1
47 #else
48 # define MCI_BUS 0
49 #endif
50 
51 static int initialized = 0;
52 
53 /*
54  * Print command and status:
55  *
56  * - always when DEBUG is defined
57  * - on command errors
58  */
59 static void dump_cmd(u32 cmdr, u32 arg, u32 status, const char* msg)
60 {
61 	printf("gen_atmel_mci: CMDR %08x (%2u) ARGR %08x (SR: %08x) %s\n",
62 		cmdr, cmdr&0x3F, arg, status, msg);
63 }
64 
65 /* Setup for MCI Clock and Block Size */
66 static void mci_set_mode(struct mmc *mmc, u32 hz, u32 blklen)
67 {
68 	atmel_mci_t *mci = (atmel_mci_t *)mmc->priv;
69 	u32 bus_hz = get_mci_clk_rate();
70 	u32 clkdiv = 255;
71 
72 	debug("mci: bus_hz is %u, setting clock %u Hz, block size %u\n",
73 		bus_hz, hz, blklen);
74 	if (hz > 0) {
75 		/* find lowest clkdiv yielding a rate <= than requested */
76 		for (clkdiv=0; clkdiv<255; clkdiv++) {
77 			if ((bus_hz / (clkdiv+1) / 2) <= hz)
78 				break;
79 		}
80 	}
81 	printf("mci: setting clock %u Hz, block size %u\n",
82 		(bus_hz / (clkdiv+1)) / 2, blklen);
83 
84 	blklen &= 0xfffc;
85 	/* On some platforms RDPROOF and WRPROOF are ignored */
86 	writel((MMCI_BF(CLKDIV, clkdiv)
87 		 | MMCI_BF(BLKLEN, blklen)
88 		 | MMCI_BIT(RDPROOF)
89 		 | MMCI_BIT(WRPROOF)), &mci->mr);
90 	initialized = 1;
91 }
92 
93 /* Return the CMDR with flags for a given command and data packet */
94 static u32 mci_encode_cmd(
95 	struct mmc_cmd *cmd, struct mmc_data *data, u32* error_flags)
96 {
97 	u32 cmdr = 0;
98 
99 	/* Default Flags for Errors */
100 	*error_flags |= (MMCI_BIT(DTOE) | MMCI_BIT(RDIRE) | MMCI_BIT(RENDE) |
101 		MMCI_BIT(RINDE) | MMCI_BIT(RTOE));
102 
103 	/* Default Flags for the Command */
104 	cmdr |= MMCI_BIT(MAXLAT);
105 
106 	if (data) {
107 		cmdr |= MMCI_BF(TRCMD, 1);
108 		if (data->blocks > 1)
109 			cmdr |= MMCI_BF(TRTYP, 1);
110 		if (data->flags & MMC_DATA_READ)
111 			cmdr |= MMCI_BIT(TRDIR);
112 	}
113 
114 	if (cmd->resp_type & MMC_RSP_CRC)
115 		*error_flags |= MMCI_BIT(RCRCE);
116 	if (cmd->resp_type & MMC_RSP_136)
117 		cmdr |= MMCI_BF(RSPTYP, 2);
118 	else if (cmd->resp_type & MMC_RSP_BUSY)
119 		cmdr |= MMCI_BF(RSPTYP, 3);
120 	else if (cmd->resp_type & MMC_RSP_PRESENT)
121 		cmdr |= MMCI_BF(RSPTYP, 1);
122 
123 	return cmdr | MMCI_BF(CMDNB, cmd->cmdidx);
124 }
125 
126 /* Entered into function pointer in mci_send_cmd */
127 static u32 mci_data_read(atmel_mci_t *mci, u32* data, u32 error_flags)
128 {
129 	u32 status;
130 
131 	do {
132 		status = readl(&mci->sr);
133 		if (status & (error_flags | MMCI_BIT(OVRE)))
134 			goto io_fail;
135 	} while (!(status & MMCI_BIT(RXRDY)));
136 
137 	if (status & MMCI_BIT(RXRDY)) {
138 		*data = readl(&mci->rdr);
139 		status = 0;
140 	}
141 io_fail:
142 	return status;
143 }
144 
145 /* Entered into function pointer in mci_send_cmd */
146 static u32 mci_data_write(atmel_mci_t *mci, u32* data, u32 error_flags)
147 {
148 	u32 status;
149 
150 	do {
151 		status = readl(&mci->sr);
152 		if (status & (error_flags | MMCI_BIT(UNRE)))
153 			goto io_fail;
154 	} while (!(status & MMCI_BIT(TXRDY)));
155 
156 	if (status & MMCI_BIT(TXRDY)) {
157 		writel(*data, &mci->tdr);
158 		status = 0;
159 	}
160 io_fail:
161 	return status;
162 }
163 
164 /*
165  * Entered into mmc structure during driver init
166  *
167  * Sends a command out on the bus and deals with the block data.
168  * Takes the mmc pointer, a command pointer, and an optional data pointer.
169  */
170 static int
171 mci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
172 {
173 	atmel_mci_t *mci = (atmel_mci_t *)mmc->priv;
174 	u32 cmdr;
175 	u32 error_flags = 0;
176 	u32 status;
177 
178 	if (!initialized) {
179 		puts ("MCI not initialized!\n");
180 		return COMM_ERR;
181 	}
182 
183 	/* Figure out the transfer arguments */
184 	cmdr = mci_encode_cmd(cmd, data, &error_flags);
185 
186 	if (data)
187 		writel(MMCI_BF(BCNT, data->blocks) |
188 			MMCI_BF(BLKLEN,	mmc->read_bl_len), &mci->blkr);
189 
190 	/* Send the command */
191 	writel(cmd->cmdarg, &mci->argr);
192 	writel(cmdr, &mci->cmdr);
193 
194 #ifdef DEBUG
195 	dump_cmd(cmdr, cmd->cmdarg, 0, "DEBUG");
196 #endif
197 
198 	/* Wait for the command to complete */
199 	while (!((status = readl(&mci->sr)) & MMCI_BIT(CMDRDY)));
200 
201 	if (status & error_flags) {
202 		dump_cmd(cmdr, cmd->cmdarg, status, "Command Failed");
203 		return COMM_ERR;
204 	}
205 
206 	/* Copy the response to the response buffer */
207 	if (cmd->resp_type & MMC_RSP_136) {
208 		cmd->response[0] = readl(&mci->rspr);
209 		cmd->response[1] = readl(&mci->rspr1);
210 		cmd->response[2] = readl(&mci->rspr2);
211 		cmd->response[3] = readl(&mci->rspr3);
212 	} else
213 		cmd->response[0] = readl(&mci->rspr);
214 
215 	/* transfer all of the blocks */
216 	if (data) {
217 		u32 word_count, block_count;
218 		u32* ioptr;
219 		u32 sys_blocksize, dummy, i;
220 		u32 (*mci_data_op)
221 			(atmel_mci_t *mci, u32* data, u32 error_flags);
222 
223 		if (data->flags & MMC_DATA_READ) {
224 			mci_data_op = mci_data_read;
225 			sys_blocksize = mmc->read_bl_len;
226 			ioptr = (u32*)data->dest;
227 		} else {
228 			mci_data_op = mci_data_write;
229 			sys_blocksize = mmc->write_bl_len;
230 			ioptr = (u32*)data->src;
231 		}
232 
233 		status = 0;
234 		for (block_count = 0;
235 				block_count < data->blocks && !status;
236 				block_count++) {
237 			word_count = 0;
238 			do {
239 				status = mci_data_op(mci, ioptr, error_flags);
240 				word_count++;
241 				ioptr++;
242 			} while (!status && word_count < (data->blocksize/4));
243 #ifdef DEBUG
244 			if (data->flags & MMC_DATA_READ)
245 			{
246 				printf("Read Data:\n");
247 				print_buffer(0, data->dest, 1,
248 					word_count*4, 0);
249 			}
250 #endif
251 #ifdef DEBUG
252 			if (!status && word_count < (sys_blocksize / 4))
253 				printf("filling rest of block...\n");
254 #endif
255 			/* fill the rest of a full block */
256 			while (!status && word_count < (sys_blocksize / 4)) {
257 				status = mci_data_op(mci, &dummy,
258 					error_flags);
259 				word_count++;
260 			}
261 			if (status) {
262 				dump_cmd(cmdr, cmd->cmdarg, status,
263 					"Data Transfer Failed");
264 				return COMM_ERR;
265 			}
266 		}
267 
268 		/* Wait for Transfer End */
269 		i = 0;
270 		do {
271 			status = readl(&mci->sr);
272 
273 			if (status & error_flags) {
274 				dump_cmd(cmdr, cmd->cmdarg, status,
275 					"DTIP Wait Failed");
276 				return COMM_ERR;
277 			}
278 			i++;
279 		} while ((status & MMCI_BIT(DTIP)) && i < 10000);
280 		if (status & MMCI_BIT(DTIP)) {
281 			dump_cmd(cmdr, cmd->cmdarg, status,
282 				"XFER DTIP never unset, ignoring");
283 		}
284 	}
285 
286 	return 0;
287 }
288 
289 /* Entered into mmc structure during driver init */
290 static void mci_set_ios(struct mmc *mmc)
291 {
292 	atmel_mci_t *mci = (atmel_mci_t *)mmc->priv;
293 	int busw = (mmc->bus_width == 4) ? 1 : 0;
294 
295 	/* Set the clock speed */
296 	mci_set_mode(mmc, mmc->clock, MMC_DEFAULT_BLKLEN);
297 
298 	/*
299 	 * set the bus width and select slot for this interface
300 	 * there is no capability for multiple slots on the same interface yet
301 	 * Bitfield SCDBUS needs to be expanded to 2 bits for 8-bit buses
302 	 */
303 	writel(MMCI_BF(SCDBUS, busw) | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
304 }
305 
306 /* Entered into mmc structure during driver init */
307 static int mci_init(struct mmc *mmc)
308 {
309 	atmel_mci_t *mci = (atmel_mci_t *)mmc->priv;
310 
311 	/* Initialize controller */
312 	writel(MMCI_BIT(SWRST), &mci->cr);	/* soft reset */
313 	writel(MMCI_BIT(PWSDIS), &mci->cr);	/* disable power save */
314 	writel(MMCI_BIT(MCIEN), &mci->cr);	/* enable mci */
315 	writel(MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);	/* select port */
316 
317 	/* Initial Time-outs */
318 	writel(0x5f, &mci->dtor);
319 	/* Disable Interrupts */
320 	writel(~0UL, &mci->idr);
321 
322 	/* Set default clocks and blocklen */
323 	mci_set_mode(mmc, CONFIG_SYS_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
324 
325 	return 0;
326 }
327 
328 /*
329  * This is the only exported function
330  *
331  * Call it with the MCI register base address
332  */
333 int atmel_mci_init(void *regs)
334 {
335 	struct mmc *mmc = malloc(sizeof(struct mmc));
336 
337 	if (!mmc)
338 		return -1;
339 	strcpy(mmc->name, "mci");
340 	mmc->priv = regs;
341 	mmc->send_cmd = mci_send_cmd;
342 	mmc->set_ios = mci_set_ios;
343 	mmc->init = mci_init;
344 
345 	/* need to be able to pass these in on a board by board basis */
346 	mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
347 	mmc->host_caps = MMC_MODE_4BIT;
348 	/*
349 	 * min and max frequencies determined by
350 	 * max and min of clock divider
351 	 */
352 	mmc->f_min = get_mci_clk_rate() / (2*256);
353 	mmc->f_max = get_mci_clk_rate() / (2*1);
354 
355 	mmc->b_max = 0;
356 
357 	mmc_register(mmc);
358 
359 	return 0;
360 }
361