blob: 74d53a3140bada62e8f5d2309a45924ca7b4c693 [file] [log] [blame]
Reinhard Meyer1592ef82010-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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Reinhard Meyer1592ef82010-08-13 10:31:06 +020010 */
11
12#include <common.h>
13#include <mmc.h>
14#include <part.h>
15#include <malloc.h>
16#include <asm/io.h>
17#include <asm/errno.h>
18#include <asm/byteorder.h>
19#include <asm/arch/clk.h>
Reinhard Meyer329f0f52010-11-03 16:32:56 +010020#include <asm/arch/hardware.h>
Reinhard Meyer1592ef82010-08-13 10:31:06 +020021#include "atmel_mci.h"
22
23#ifndef CONFIG_SYS_MMC_CLK_OD
24# define CONFIG_SYS_MMC_CLK_OD 150000
25#endif
26
27#define MMC_DEFAULT_BLKLEN 512
28
29#if defined(CONFIG_ATMEL_MCI_PORTB)
30# define MCI_BUS 1
31#else
32# define MCI_BUS 0
33#endif
34
Marek Vasut6b75d352015-10-23 20:46:30 +020035struct atmel_mci_priv {
36 struct mmc_config cfg;
37 struct atmel_mci *mci;
38};
39
Reinhard Meyer1592ef82010-08-13 10:31:06 +020040static int initialized = 0;
41
Bo Shenaac4b692013-04-26 00:27:06 +000042/* Read Atmel MCI IP version */
43static unsigned int atmel_mci_get_version(struct atmel_mci *mci)
44{
45 return readl(&mci->version) & 0x00000fff;
46}
47
Reinhard Meyer1592ef82010-08-13 10:31:06 +020048/*
49 * Print command and status:
50 *
51 * - always when DEBUG is defined
52 * - on command errors
53 */
54static void dump_cmd(u32 cmdr, u32 arg, u32 status, const char* msg)
55{
Marek Vasutb84c9c92015-10-23 20:46:28 +020056 debug("gen_atmel_mci: CMDR %08x (%2u) ARGR %08x (SR: %08x) %s\n",
57 cmdr, cmdr & 0x3F, arg, status, msg);
Reinhard Meyer1592ef82010-08-13 10:31:06 +020058}
59
60/* Setup for MCI Clock and Block Size */
61static void mci_set_mode(struct mmc *mmc, u32 hz, u32 blklen)
62{
Marek Vasut6b75d352015-10-23 20:46:30 +020063 struct atmel_mci_priv *priv = mmc->priv;
64 atmel_mci_t *mci = priv->mci;
Reinhard Meyer1592ef82010-08-13 10:31:06 +020065 u32 bus_hz = get_mci_clk_rate();
66 u32 clkdiv = 255;
Bo Shencd60ebd42014-07-31 14:39:30 +080067 unsigned int version = atmel_mci_get_version(mci);
68 u32 clkodd = 0;
69 u32 mr;
Reinhard Meyer1592ef82010-08-13 10:31:06 +020070
71 debug("mci: bus_hz is %u, setting clock %u Hz, block size %u\n",
72 bus_hz, hz, blklen);
73 if (hz > 0) {
Bo Shencd60ebd42014-07-31 14:39:30 +080074 if (version >= 0x500) {
75 clkdiv = DIV_ROUND_UP(bus_hz, hz) - 2;
76 if (clkdiv > 511)
77 clkdiv = 511;
78
79 clkodd = clkdiv & 1;
80 clkdiv >>= 1;
81
Marek Vasutb84c9c92015-10-23 20:46:28 +020082 debug("mci: setting clock %u Hz, block size %u\n",
83 bus_hz / (clkdiv * 2 + clkodd + 2), blklen);
Bo Shencd60ebd42014-07-31 14:39:30 +080084 } else {
85 /* find clkdiv yielding a rate <= than requested */
86 for (clkdiv = 0; clkdiv < 255; clkdiv++) {
87 if ((bus_hz / (clkdiv + 1) / 2) <= hz)
88 break;
89 }
Marek Vasutb84c9c92015-10-23 20:46:28 +020090 debug("mci: setting clock %u Hz, block size %u\n",
91 (bus_hz / (clkdiv + 1)) / 2, blklen);
Bo Shencd60ebd42014-07-31 14:39:30 +080092
Reinhard Meyer1592ef82010-08-13 10:31:06 +020093 }
94 }
Reinhard Meyer1592ef82010-08-13 10:31:06 +020095
96 blklen &= 0xfffc;
Bo Shencd60ebd42014-07-31 14:39:30 +080097
98 mr = MMCI_BF(CLKDIV, clkdiv);
99
100 /* MCI IP version >= 0x200 has R/WPROOF */
101 if (version >= 0x200)
102 mr |= MMCI_BIT(RDPROOF) | MMCI_BIT(WRPROOF);
103
Wu, Josh1db73772012-09-13 22:22:04 +0000104 /*
Bo Shencd60ebd42014-07-31 14:39:30 +0800105 * MCI IP version >= 0x500 use bit 16 as clkodd.
106 * MCI IP version < 0x500 use upper 16 bits for blklen.
Wu, Josh1db73772012-09-13 22:22:04 +0000107 */
Bo Shencd60ebd42014-07-31 14:39:30 +0800108 if (version >= 0x500)
109 mr |= MMCI_BF(CLKODD, clkodd);
110 else
111 mr |= MMCI_BF(BLKLEN, blklen);
112
113 writel(mr, &mci->mr);
114
115 /* MCI IP version >= 0x200 has blkr */
116 if (version >= 0x200)
117 writel(MMCI_BF(BLKLEN, blklen), &mci->blkr);
118
Bo Shenda55c662014-07-31 14:39:32 +0800119 if (mmc->card_caps & mmc->cfg->host_caps & MMC_MODE_HS)
120 writel(MMCI_BIT(HSMODE), &mci->cfg);
121
Marek Vasutecfb0ff2015-10-23 20:46:29 +0200122 udelay(50);
123
Reinhard Meyer1592ef82010-08-13 10:31:06 +0200124 initialized = 1;
125}
126
127/* Return the CMDR with flags for a given command and data packet */
128static u32 mci_encode_cmd(
129 struct mmc_cmd *cmd, struct mmc_data *data, u32* error_flags)
130{
131 u32 cmdr = 0;
132
133 /* Default Flags for Errors */
134 *error_flags |= (MMCI_BIT(DTOE) | MMCI_BIT(RDIRE) | MMCI_BIT(RENDE) |
135 MMCI_BIT(RINDE) | MMCI_BIT(RTOE));
136
137 /* Default Flags for the Command */
138 cmdr |= MMCI_BIT(MAXLAT);
139
140 if (data) {
141 cmdr |= MMCI_BF(TRCMD, 1);
142 if (data->blocks > 1)
143 cmdr |= MMCI_BF(TRTYP, 1);
144 if (data->flags & MMC_DATA_READ)
145 cmdr |= MMCI_BIT(TRDIR);
146 }
147
148 if (cmd->resp_type & MMC_RSP_CRC)
149 *error_flags |= MMCI_BIT(RCRCE);
150 if (cmd->resp_type & MMC_RSP_136)
151 cmdr |= MMCI_BF(RSPTYP, 2);
152 else if (cmd->resp_type & MMC_RSP_BUSY)
153 cmdr |= MMCI_BF(RSPTYP, 3);
154 else if (cmd->resp_type & MMC_RSP_PRESENT)
155 cmdr |= MMCI_BF(RSPTYP, 1);
156
157 return cmdr | MMCI_BF(CMDNB, cmd->cmdidx);
158}
159
160/* Entered into function pointer in mci_send_cmd */
161static u32 mci_data_read(atmel_mci_t *mci, u32* data, u32 error_flags)
162{
163 u32 status;
164
165 do {
166 status = readl(&mci->sr);
167 if (status & (error_flags | MMCI_BIT(OVRE)))
168 goto io_fail;
169 } while (!(status & MMCI_BIT(RXRDY)));
170
171 if (status & MMCI_BIT(RXRDY)) {
172 *data = readl(&mci->rdr);
173 status = 0;
174 }
175io_fail:
176 return status;
177}
178
179/* Entered into function pointer in mci_send_cmd */
180static u32 mci_data_write(atmel_mci_t *mci, u32* data, u32 error_flags)
181{
182 u32 status;
183
184 do {
185 status = readl(&mci->sr);
186 if (status & (error_flags | MMCI_BIT(UNRE)))
187 goto io_fail;
188 } while (!(status & MMCI_BIT(TXRDY)));
189
190 if (status & MMCI_BIT(TXRDY)) {
191 writel(*data, &mci->tdr);
192 status = 0;
193 }
194io_fail:
195 return status;
196}
197
198/*
199 * Entered into mmc structure during driver init
200 *
201 * Sends a command out on the bus and deals with the block data.
202 * Takes the mmc pointer, a command pointer, and an optional data pointer.
203 */
204static int
205mci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
206{
Marek Vasut6b75d352015-10-23 20:46:30 +0200207 struct atmel_mci_priv *priv = mmc->priv;
208 atmel_mci_t *mci = priv->mci;
Reinhard Meyer1592ef82010-08-13 10:31:06 +0200209 u32 cmdr;
210 u32 error_flags = 0;
211 u32 status;
212
213 if (!initialized) {
214 puts ("MCI not initialized!\n");
215 return COMM_ERR;
216 }
217
218 /* Figure out the transfer arguments */
219 cmdr = mci_encode_cmd(cmd, data, &error_flags);
220
Wu, Josh1db73772012-09-13 22:22:04 +0000221 /* For multi blocks read/write, set the block register */
222 if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK)
223 || (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK))
224 writel(data->blocks | MMCI_BF(BLKLEN, mmc->read_bl_len),
225 &mci->blkr);
226
Reinhard Meyer1592ef82010-08-13 10:31:06 +0200227 /* Send the command */
228 writel(cmd->cmdarg, &mci->argr);
229 writel(cmdr, &mci->cmdr);
230
231#ifdef DEBUG
232 dump_cmd(cmdr, cmd->cmdarg, 0, "DEBUG");
233#endif
234
235 /* Wait for the command to complete */
236 while (!((status = readl(&mci->sr)) & MMCI_BIT(CMDRDY)));
237
Bo Shen93e32362013-04-26 00:27:07 +0000238 if ((status & error_flags) & MMCI_BIT(RTOE)) {
239 dump_cmd(cmdr, cmd->cmdarg, status, "Command Time Out");
240 return TIMEOUT;
241 } else if (status & error_flags) {
Reinhard Meyer1592ef82010-08-13 10:31:06 +0200242 dump_cmd(cmdr, cmd->cmdarg, status, "Command Failed");
243 return COMM_ERR;
244 }
245
246 /* Copy the response to the response buffer */
247 if (cmd->resp_type & MMC_RSP_136) {
248 cmd->response[0] = readl(&mci->rspr);
249 cmd->response[1] = readl(&mci->rspr1);
250 cmd->response[2] = readl(&mci->rspr2);
251 cmd->response[3] = readl(&mci->rspr3);
252 } else
253 cmd->response[0] = readl(&mci->rspr);
254
255 /* transfer all of the blocks */
256 if (data) {
257 u32 word_count, block_count;
258 u32* ioptr;
259 u32 sys_blocksize, dummy, i;
260 u32 (*mci_data_op)
261 (atmel_mci_t *mci, u32* data, u32 error_flags);
262
263 if (data->flags & MMC_DATA_READ) {
264 mci_data_op = mci_data_read;
265 sys_blocksize = mmc->read_bl_len;
266 ioptr = (u32*)data->dest;
267 } else {
268 mci_data_op = mci_data_write;
269 sys_blocksize = mmc->write_bl_len;
270 ioptr = (u32*)data->src;
271 }
272
273 status = 0;
274 for (block_count = 0;
275 block_count < data->blocks && !status;
276 block_count++) {
277 word_count = 0;
278 do {
279 status = mci_data_op(mci, ioptr, error_flags);
280 word_count++;
281 ioptr++;
282 } while (!status && word_count < (data->blocksize/4));
283#ifdef DEBUG
284 if (data->flags & MMC_DATA_READ)
285 {
Wu, Josh9902c7b2014-05-07 17:06:08 +0800286 u32 cnt = word_count * 4;
Reinhard Meyer1592ef82010-08-13 10:31:06 +0200287 printf("Read Data:\n");
Wu, Josh9902c7b2014-05-07 17:06:08 +0800288 print_buffer(0, data->dest + cnt * block_count,
289 1, cnt, 0);
Reinhard Meyer1592ef82010-08-13 10:31:06 +0200290 }
291#endif
292#ifdef DEBUG
293 if (!status && word_count < (sys_blocksize / 4))
294 printf("filling rest of block...\n");
295#endif
296 /* fill the rest of a full block */
297 while (!status && word_count < (sys_blocksize / 4)) {
298 status = mci_data_op(mci, &dummy,
299 error_flags);
300 word_count++;
301 }
302 if (status) {
303 dump_cmd(cmdr, cmd->cmdarg, status,
304 "Data Transfer Failed");
305 return COMM_ERR;
306 }
307 }
308
309 /* Wait for Transfer End */
310 i = 0;
311 do {
312 status = readl(&mci->sr);
313
314 if (status & error_flags) {
315 dump_cmd(cmdr, cmd->cmdarg, status,
316 "DTIP Wait Failed");
317 return COMM_ERR;
318 }
319 i++;
320 } while ((status & MMCI_BIT(DTIP)) && i < 10000);
321 if (status & MMCI_BIT(DTIP)) {
322 dump_cmd(cmdr, cmd->cmdarg, status,
323 "XFER DTIP never unset, ignoring");
324 }
325 }
326
327 return 0;
328}
329
330/* Entered into mmc structure during driver init */
331static void mci_set_ios(struct mmc *mmc)
332{
Marek Vasut6b75d352015-10-23 20:46:30 +0200333 struct atmel_mci_priv *priv = mmc->priv;
334 atmel_mci_t *mci = priv->mci;
Bo Shenaac4b692013-04-26 00:27:06 +0000335 int bus_width = mmc->bus_width;
336 unsigned int version = atmel_mci_get_version(mci);
337 int busw;
Reinhard Meyer1592ef82010-08-13 10:31:06 +0200338
339 /* Set the clock speed */
340 mci_set_mode(mmc, mmc->clock, MMC_DEFAULT_BLKLEN);
341
342 /*
343 * set the bus width and select slot for this interface
344 * there is no capability for multiple slots on the same interface yet
Reinhard Meyer1592ef82010-08-13 10:31:06 +0200345 */
Bo Shenaac4b692013-04-26 00:27:06 +0000346 if ((version & 0xf00) >= 0x300) {
347 switch (bus_width) {
348 case 8:
349 busw = 3;
350 break;
351 case 4:
352 busw = 2;
353 break;
354 default:
355 busw = 0;
356 break;
357 }
358
359 writel(busw << 6 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
360 } else {
361 busw = (bus_width == 4) ? 1 : 0;
362
363 writel(busw << 7 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
364 }
Reinhard Meyer1592ef82010-08-13 10:31:06 +0200365}
366
367/* Entered into mmc structure during driver init */
368static int mci_init(struct mmc *mmc)
369{
Marek Vasut6b75d352015-10-23 20:46:30 +0200370 struct atmel_mci_priv *priv = mmc->priv;
371 atmel_mci_t *mci = priv->mci;
Reinhard Meyer1592ef82010-08-13 10:31:06 +0200372
373 /* Initialize controller */
374 writel(MMCI_BIT(SWRST), &mci->cr); /* soft reset */
375 writel(MMCI_BIT(PWSDIS), &mci->cr); /* disable power save */
376 writel(MMCI_BIT(MCIEN), &mci->cr); /* enable mci */
Reinhard Meyer2aed9d12010-11-16 09:24:41 +0100377 writel(MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr); /* select port */
Reinhard Meyer1592ef82010-08-13 10:31:06 +0200378
Wu, Josh9924ca62012-09-13 22:22:06 +0000379 /* This delay can be optimized, but stick with max value */
380 writel(0x7f, &mci->dtor);
Reinhard Meyer1592ef82010-08-13 10:31:06 +0200381 /* Disable Interrupts */
382 writel(~0UL, &mci->idr);
383
384 /* Set default clocks and blocklen */
385 mci_set_mode(mmc, CONFIG_SYS_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
386
387 return 0;
388}
389
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200390static const struct mmc_ops atmel_mci_ops = {
391 .send_cmd = mci_send_cmd,
392 .set_ios = mci_set_ios,
393 .init = mci_init,
394};
395
Reinhard Meyer1592ef82010-08-13 10:31:06 +0200396/*
397 * This is the only exported function
398 *
399 * Call it with the MCI register base address
400 */
401int atmel_mci_init(void *regs)
402{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200403 struct mmc *mmc;
404 struct mmc_config *cfg;
Marek Vasut6b75d352015-10-23 20:46:30 +0200405 struct atmel_mci_priv *priv;
Bo Shenaac4b692013-04-26 00:27:06 +0000406 unsigned int version;
Reinhard Meyer1592ef82010-08-13 10:31:06 +0200407
Marek Vasut6b75d352015-10-23 20:46:30 +0200408 priv = calloc(1, sizeof(*priv));
409 if (!priv)
410 return -ENOMEM;
Bo Shenaac4b692013-04-26 00:27:06 +0000411
Marek Vasut6b75d352015-10-23 20:46:30 +0200412 cfg = &priv->cfg;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200413
414 cfg->name = "mci";
415 cfg->ops = &atmel_mci_ops;
Reinhard Meyer1592ef82010-08-13 10:31:06 +0200416
Marek Vasut6b75d352015-10-23 20:46:30 +0200417 priv->mci = (struct atmel_mci *)regs;
418
Reinhard Meyer1592ef82010-08-13 10:31:06 +0200419 /* need to be able to pass these in on a board by board basis */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200420 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Marek Vasut6b75d352015-10-23 20:46:30 +0200421 version = atmel_mci_get_version(priv->mci);
Bo Shenda55c662014-07-31 14:39:32 +0800422 if ((version & 0xf00) >= 0x300) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200423 cfg->host_caps = MMC_MODE_8BIT;
Bo Shenda55c662014-07-31 14:39:32 +0800424 cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
425 }
Bo Shenaac4b692013-04-26 00:27:06 +0000426
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200427 cfg->host_caps |= MMC_MODE_4BIT;
Bo Shenaac4b692013-04-26 00:27:06 +0000428
Reinhard Meyer1592ef82010-08-13 10:31:06 +0200429 /*
430 * min and max frequencies determined by
431 * max and min of clock divider
432 */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200433 cfg->f_min = get_mci_clk_rate() / (2*256);
434 cfg->f_max = get_mci_clk_rate() / (2*1);
Reinhard Meyer1592ef82010-08-13 10:31:06 +0200435
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200436 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
John Rigby8feafcc2011-04-18 05:50:08 +0000437
Marek Vasut6b75d352015-10-23 20:46:30 +0200438 mmc = mmc_create(cfg, priv);
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200439
440 if (mmc == NULL) {
Marek Vasut6b75d352015-10-23 20:46:30 +0200441 free(priv);
442 return -ENODEV;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200443 }
Marek Vasut6b75d352015-10-23 20:46:30 +0200444 /* NOTE: possibly leaking the priv structure */
Reinhard Meyer1592ef82010-08-13 10:31:06 +0200445
446 return 0;
447}