blob: fc0a1812318bb6af90a233c70c5dac74a1b259ae [file] [log] [blame]
Reinhard Meyerc718a562010-08-13 10:31:06 +02001/*
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>
Reinhard Meyer7f619cb2010-11-03 16:32:56 +010036#include <asm/arch/hardware.h>
Reinhard Meyerc718a562010-08-13 10:31:06 +020037#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
51static int initialized = 0;
52
Bo Shen644b4762013-04-26 00:27:06 +000053/* Read Atmel MCI IP version */
54static unsigned int atmel_mci_get_version(struct atmel_mci *mci)
55{
56 return readl(&mci->version) & 0x00000fff;
57}
58
Reinhard Meyerc718a562010-08-13 10:31:06 +020059/*
60 * Print command and status:
61 *
62 * - always when DEBUG is defined
63 * - on command errors
64 */
65static void dump_cmd(u32 cmdr, u32 arg, u32 status, const char* msg)
66{
67 printf("gen_atmel_mci: CMDR %08x (%2u) ARGR %08x (SR: %08x) %s\n",
68 cmdr, cmdr&0x3F, arg, status, msg);
69}
70
71/* Setup for MCI Clock and Block Size */
72static void mci_set_mode(struct mmc *mmc, u32 hz, u32 blklen)
73{
74 atmel_mci_t *mci = (atmel_mci_t *)mmc->priv;
75 u32 bus_hz = get_mci_clk_rate();
76 u32 clkdiv = 255;
77
78 debug("mci: bus_hz is %u, setting clock %u Hz, block size %u\n",
79 bus_hz, hz, blklen);
80 if (hz > 0) {
81 /* find lowest clkdiv yielding a rate <= than requested */
82 for (clkdiv=0; clkdiv<255; clkdiv++) {
83 if ((bus_hz / (clkdiv+1) / 2) <= hz)
84 break;
85 }
86 }
87 printf("mci: setting clock %u Hz, block size %u\n",
88 (bus_hz / (clkdiv+1)) / 2, blklen);
89
90 blklen &= 0xfffc;
91 /* On some platforms RDPROOF and WRPROOF are ignored */
92 writel((MMCI_BF(CLKDIV, clkdiv)
93 | MMCI_BF(BLKLEN, blklen)
94 | MMCI_BIT(RDPROOF)
95 | MMCI_BIT(WRPROOF)), &mci->mr);
Wu, Joshd1e486b2012-09-13 22:22:04 +000096 /*
97 * On some new platforms BLKLEN in mci->mr is ignored.
98 * Should use the BLKLEN in the block register.
99 */
100 writel(MMCI_BF(BLKLEN, blklen), &mci->blkr);
Reinhard Meyerc718a562010-08-13 10:31:06 +0200101 initialized = 1;
102}
103
104/* Return the CMDR with flags for a given command and data packet */
105static u32 mci_encode_cmd(
106 struct mmc_cmd *cmd, struct mmc_data *data, u32* error_flags)
107{
108 u32 cmdr = 0;
109
110 /* Default Flags for Errors */
111 *error_flags |= (MMCI_BIT(DTOE) | MMCI_BIT(RDIRE) | MMCI_BIT(RENDE) |
112 MMCI_BIT(RINDE) | MMCI_BIT(RTOE));
113
114 /* Default Flags for the Command */
115 cmdr |= MMCI_BIT(MAXLAT);
116
117 if (data) {
118 cmdr |= MMCI_BF(TRCMD, 1);
119 if (data->blocks > 1)
120 cmdr |= MMCI_BF(TRTYP, 1);
121 if (data->flags & MMC_DATA_READ)
122 cmdr |= MMCI_BIT(TRDIR);
123 }
124
125 if (cmd->resp_type & MMC_RSP_CRC)
126 *error_flags |= MMCI_BIT(RCRCE);
127 if (cmd->resp_type & MMC_RSP_136)
128 cmdr |= MMCI_BF(RSPTYP, 2);
129 else if (cmd->resp_type & MMC_RSP_BUSY)
130 cmdr |= MMCI_BF(RSPTYP, 3);
131 else if (cmd->resp_type & MMC_RSP_PRESENT)
132 cmdr |= MMCI_BF(RSPTYP, 1);
133
134 return cmdr | MMCI_BF(CMDNB, cmd->cmdidx);
135}
136
137/* Entered into function pointer in mci_send_cmd */
138static u32 mci_data_read(atmel_mci_t *mci, u32* data, u32 error_flags)
139{
140 u32 status;
141
142 do {
143 status = readl(&mci->sr);
144 if (status & (error_flags | MMCI_BIT(OVRE)))
145 goto io_fail;
146 } while (!(status & MMCI_BIT(RXRDY)));
147
148 if (status & MMCI_BIT(RXRDY)) {
149 *data = readl(&mci->rdr);
150 status = 0;
151 }
152io_fail:
153 return status;
154}
155
156/* Entered into function pointer in mci_send_cmd */
157static u32 mci_data_write(atmel_mci_t *mci, u32* data, u32 error_flags)
158{
159 u32 status;
160
161 do {
162 status = readl(&mci->sr);
163 if (status & (error_flags | MMCI_BIT(UNRE)))
164 goto io_fail;
165 } while (!(status & MMCI_BIT(TXRDY)));
166
167 if (status & MMCI_BIT(TXRDY)) {
168 writel(*data, &mci->tdr);
169 status = 0;
170 }
171io_fail:
172 return status;
173}
174
175/*
176 * Entered into mmc structure during driver init
177 *
178 * Sends a command out on the bus and deals with the block data.
179 * Takes the mmc pointer, a command pointer, and an optional data pointer.
180 */
181static int
182mci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
183{
184 atmel_mci_t *mci = (atmel_mci_t *)mmc->priv;
185 u32 cmdr;
186 u32 error_flags = 0;
187 u32 status;
188
189 if (!initialized) {
190 puts ("MCI not initialized!\n");
191 return COMM_ERR;
192 }
193
194 /* Figure out the transfer arguments */
195 cmdr = mci_encode_cmd(cmd, data, &error_flags);
196
Wu, Joshd1e486b2012-09-13 22:22:04 +0000197 /* For multi blocks read/write, set the block register */
198 if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK)
199 || (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK))
200 writel(data->blocks | MMCI_BF(BLKLEN, mmc->read_bl_len),
201 &mci->blkr);
202
Reinhard Meyerc718a562010-08-13 10:31:06 +0200203 /* Send the command */
204 writel(cmd->cmdarg, &mci->argr);
205 writel(cmdr, &mci->cmdr);
206
207#ifdef DEBUG
208 dump_cmd(cmdr, cmd->cmdarg, 0, "DEBUG");
209#endif
210
211 /* Wait for the command to complete */
212 while (!((status = readl(&mci->sr)) & MMCI_BIT(CMDRDY)));
213
214 if (status & error_flags) {
215 dump_cmd(cmdr, cmd->cmdarg, status, "Command Failed");
216 return COMM_ERR;
217 }
218
219 /* Copy the response to the response buffer */
220 if (cmd->resp_type & MMC_RSP_136) {
221 cmd->response[0] = readl(&mci->rspr);
222 cmd->response[1] = readl(&mci->rspr1);
223 cmd->response[2] = readl(&mci->rspr2);
224 cmd->response[3] = readl(&mci->rspr3);
225 } else
226 cmd->response[0] = readl(&mci->rspr);
227
228 /* transfer all of the blocks */
229 if (data) {
230 u32 word_count, block_count;
231 u32* ioptr;
232 u32 sys_blocksize, dummy, i;
233 u32 (*mci_data_op)
234 (atmel_mci_t *mci, u32* data, u32 error_flags);
235
236 if (data->flags & MMC_DATA_READ) {
237 mci_data_op = mci_data_read;
238 sys_blocksize = mmc->read_bl_len;
239 ioptr = (u32*)data->dest;
240 } else {
241 mci_data_op = mci_data_write;
242 sys_blocksize = mmc->write_bl_len;
243 ioptr = (u32*)data->src;
244 }
245
246 status = 0;
247 for (block_count = 0;
248 block_count < data->blocks && !status;
249 block_count++) {
250 word_count = 0;
251 do {
252 status = mci_data_op(mci, ioptr, error_flags);
253 word_count++;
254 ioptr++;
255 } while (!status && word_count < (data->blocksize/4));
256#ifdef DEBUG
257 if (data->flags & MMC_DATA_READ)
258 {
259 printf("Read Data:\n");
260 print_buffer(0, data->dest, 1,
261 word_count*4, 0);
262 }
263#endif
264#ifdef DEBUG
265 if (!status && word_count < (sys_blocksize / 4))
266 printf("filling rest of block...\n");
267#endif
268 /* fill the rest of a full block */
269 while (!status && word_count < (sys_blocksize / 4)) {
270 status = mci_data_op(mci, &dummy,
271 error_flags);
272 word_count++;
273 }
274 if (status) {
275 dump_cmd(cmdr, cmd->cmdarg, status,
276 "Data Transfer Failed");
277 return COMM_ERR;
278 }
279 }
280
281 /* Wait for Transfer End */
282 i = 0;
283 do {
284 status = readl(&mci->sr);
285
286 if (status & error_flags) {
287 dump_cmd(cmdr, cmd->cmdarg, status,
288 "DTIP Wait Failed");
289 return COMM_ERR;
290 }
291 i++;
292 } while ((status & MMCI_BIT(DTIP)) && i < 10000);
293 if (status & MMCI_BIT(DTIP)) {
294 dump_cmd(cmdr, cmd->cmdarg, status,
295 "XFER DTIP never unset, ignoring");
296 }
297 }
298
299 return 0;
300}
301
302/* Entered into mmc structure during driver init */
303static void mci_set_ios(struct mmc *mmc)
304{
305 atmel_mci_t *mci = (atmel_mci_t *)mmc->priv;
Bo Shen644b4762013-04-26 00:27:06 +0000306 int bus_width = mmc->bus_width;
307 unsigned int version = atmel_mci_get_version(mci);
308 int busw;
Reinhard Meyerc718a562010-08-13 10:31:06 +0200309
310 /* Set the clock speed */
311 mci_set_mode(mmc, mmc->clock, MMC_DEFAULT_BLKLEN);
312
313 /*
314 * set the bus width and select slot for this interface
315 * there is no capability for multiple slots on the same interface yet
Reinhard Meyerc718a562010-08-13 10:31:06 +0200316 */
Bo Shen644b4762013-04-26 00:27:06 +0000317 if ((version & 0xf00) >= 0x300) {
318 switch (bus_width) {
319 case 8:
320 busw = 3;
321 break;
322 case 4:
323 busw = 2;
324 break;
325 default:
326 busw = 0;
327 break;
328 }
329
330 writel(busw << 6 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
331 } else {
332 busw = (bus_width == 4) ? 1 : 0;
333
334 writel(busw << 7 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
335 }
Reinhard Meyerc718a562010-08-13 10:31:06 +0200336}
337
338/* Entered into mmc structure during driver init */
339static int mci_init(struct mmc *mmc)
340{
341 atmel_mci_t *mci = (atmel_mci_t *)mmc->priv;
342
343 /* Initialize controller */
344 writel(MMCI_BIT(SWRST), &mci->cr); /* soft reset */
345 writel(MMCI_BIT(PWSDIS), &mci->cr); /* disable power save */
346 writel(MMCI_BIT(MCIEN), &mci->cr); /* enable mci */
Reinhard Meyerf2675572010-11-16 09:24:41 +0100347 writel(MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr); /* select port */
Reinhard Meyerc718a562010-08-13 10:31:06 +0200348
Wu, Joshec35c912012-09-13 22:22:06 +0000349 /* This delay can be optimized, but stick with max value */
350 writel(0x7f, &mci->dtor);
Reinhard Meyerc718a562010-08-13 10:31:06 +0200351 /* Disable Interrupts */
352 writel(~0UL, &mci->idr);
353
354 /* Set default clocks and blocklen */
355 mci_set_mode(mmc, CONFIG_SYS_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
356
357 return 0;
358}
359
360/*
361 * This is the only exported function
362 *
363 * Call it with the MCI register base address
364 */
365int atmel_mci_init(void *regs)
366{
367 struct mmc *mmc = malloc(sizeof(struct mmc));
Bo Shen644b4762013-04-26 00:27:06 +0000368 struct atmel_mci *mci;
369 unsigned int version;
Reinhard Meyerc718a562010-08-13 10:31:06 +0200370
371 if (!mmc)
372 return -1;
Bo Shen644b4762013-04-26 00:27:06 +0000373
Reinhard Meyerc718a562010-08-13 10:31:06 +0200374 strcpy(mmc->name, "mci");
375 mmc->priv = regs;
376 mmc->send_cmd = mci_send_cmd;
377 mmc->set_ios = mci_set_ios;
378 mmc->init = mci_init;
Thierry Redingb9c8b772012-01-02 01:15:37 +0000379 mmc->getcd = NULL;
Nikita Kiryanov020f2612012-12-03 02:19:46 +0000380 mmc->getwp = NULL;
Reinhard Meyerc718a562010-08-13 10:31:06 +0200381
382 /* need to be able to pass these in on a board by board basis */
383 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Bo Shen644b4762013-04-26 00:27:06 +0000384 mci = (struct atmel_mci *)mmc->priv;
385 version = atmel_mci_get_version(mci);
386 if ((version & 0xf00) >= 0x300)
387 mmc->host_caps = MMC_MODE_8BIT;
388
389 mmc->host_caps |= MMC_MODE_4BIT;
390
Reinhard Meyerc718a562010-08-13 10:31:06 +0200391 /*
392 * min and max frequencies determined by
393 * max and min of clock divider
394 */
395 mmc->f_min = get_mci_clk_rate() / (2*256);
396 mmc->f_max = get_mci_clk_rate() / (2*1);
397
John Rigbyf2f43662011-04-18 05:50:08 +0000398 mmc->b_max = 0;
399
Reinhard Meyerc718a562010-08-13 10:31:06 +0200400 mmc_register(mmc);
401
402 return 0;
403}