blob: 37efe6b3f12ddcdcb0c8403c321ac186a45c3dbb [file] [log] [blame]
Andrew Victor65dbf342006-04-02 19:18:51 +01001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/at91_mci.c - ATMEL AT91 MCI Driver
Andrew Victor65dbf342006-04-02 19:18:51 +01003 *
4 * Copyright (C) 2005 Cougar Creek Computing Devices Ltd, All Rights Reserved
5 *
6 * Copyright (C) 2006 Malcolm Noyes
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13/*
Andrew Victor99eeb8d2006-12-11 12:40:23 +010014 This is the AT91 MCI driver that has been tested with both MMC cards
Andrew Victor65dbf342006-04-02 19:18:51 +010015 and SD-cards. Boards that support write protect are now supported.
16 The CCAT91SBC001 board does not support SD cards.
17
18 The three entry points are at91_mci_request, at91_mci_set_ios
19 and at91_mci_get_ro.
20
21 SET IOS
22 This configures the device to put it into the correct mode and clock speed
23 required.
24
25 MCI REQUEST
26 MCI request processes the commands sent in the mmc_request structure. This
27 can consist of a processing command and a stop command in the case of
28 multiple block transfers.
29
30 There are three main types of request, commands, reads and writes.
31
32 Commands are straight forward. The command is submitted to the controller and
33 the request function returns. When the controller generates an interrupt to indicate
34 the command is finished, the response to the command are read and the mmc_request_done
35 function called to end the request.
36
37 Reads and writes work in a similar manner to normal commands but involve the PDC (DMA)
38 controller to manage the transfers.
39
40 A read is done from the controller directly to the scatterlist passed in from the request.
Andrew Victor99eeb8d2006-12-11 12:40:23 +010041 Due to a bug in the AT91RM9200 controller, when a read is completed, all the words are byte
42 swapped in the scatterlist buffers. AT91SAM926x are not affected by this bug.
Andrew Victor65dbf342006-04-02 19:18:51 +010043
44 The sequence of read interrupts is: ENDRX, RXBUFF, CMDRDY
45
46 A write is slightly different in that the bytes to write are read from the scatterlist
47 into a dma memory buffer (this is in case the source buffer should be read only). The
48 entire write buffer is then done from this single dma memory buffer.
49
50 The sequence of write interrupts is: ENDTX, TXBUFE, NOTBUSY, CMDRDY
51
52 GET RO
53 Gets the status of the write protect pin, if available.
54*/
55
Andrew Victor65dbf342006-04-02 19:18:51 +010056#include <linux/module.h>
57#include <linux/moduleparam.h>
58#include <linux/init.h>
59#include <linux/ioport.h>
60#include <linux/platform_device.h>
61#include <linux/interrupt.h>
62#include <linux/blkdev.h>
63#include <linux/delay.h>
64#include <linux/err.h>
65#include <linux/dma-mapping.h>
66#include <linux/clk.h>
Andrew Victor93a3ddc2007-02-08 11:31:22 +010067#include <linux/atmel_pdc.h>
Andrew Victor65dbf342006-04-02 19:18:51 +010068
69#include <linux/mmc/host.h>
Andrew Victor65dbf342006-04-02 19:18:51 +010070
71#include <asm/io.h>
72#include <asm/irq.h>
David Brownell6e996ee2008-02-04 18:12:48 +010073#include <asm/gpio.h>
74
Russell Kinga09e64f2008-08-05 16:14:15 +010075#include <mach/board.h>
76#include <mach/cpu.h>
77#include <mach/at91_mci.h>
Andrew Victor65dbf342006-04-02 19:18:51 +010078
79#define DRIVER_NAME "at91_mci"
80
Andrew Victordf05a302006-10-23 14:50:09 +020081#define FL_SENT_COMMAND (1 << 0)
82#define FL_SENT_STOP (1 << 1)
Andrew Victor65dbf342006-04-02 19:18:51 +010083
Andrew Victordf05a302006-10-23 14:50:09 +020084#define AT91_MCI_ERRORS (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE \
85 | AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE \
Nicolas Ferre37b758e82007-08-08 12:01:44 +020086 | AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)
Andrew Victor65dbf342006-04-02 19:18:51 +010087
Andrew Victore0b19b82006-10-25 19:42:38 +020088#define at91_mci_read(host, reg) __raw_readl((host)->baseaddr + (reg))
89#define at91_mci_write(host, reg, val) __raw_writel((val), (host)->baseaddr + (reg))
Andrew Victor65dbf342006-04-02 19:18:51 +010090
Andrew Victor65dbf342006-04-02 19:18:51 +010091
92/*
93 * Low level type for this driver
94 */
95struct at91mci_host
96{
97 struct mmc_host *mmc;
98 struct mmc_command *cmd;
99 struct mmc_request *request;
100
Andrew Victore0b19b82006-10-25 19:42:38 +0200101 void __iomem *baseaddr;
Andrew Victor17ea0592006-10-23 14:44:40 +0200102 int irq;
Andrew Victore0b19b82006-10-25 19:42:38 +0200103
Andrew Victor65dbf342006-04-02 19:18:51 +0100104 struct at91_mmc_data *board;
105 int present;
106
Andrew Victor3dd3b032006-10-23 14:46:54 +0200107 struct clk *mci_clk;
108
Andrew Victor65dbf342006-04-02 19:18:51 +0100109 /*
110 * Flag indicating when the command has been sent. This is used to
111 * work out whether or not to send the stop
112 */
113 unsigned int flags;
114 /* flag for current bus settings */
115 u32 bus_mode;
116
117 /* DMA buffer used for transmitting */
118 unsigned int* buffer;
119 dma_addr_t physical_address;
120 unsigned int total_length;
121
122 /* Latest in the scatterlist that has been enabled for transfer, but not freed */
123 int in_use_index;
124
125 /* Latest in the scatterlist that has been enabled for transfer */
126 int transfer_index;
Marc Pignate181dce2008-05-30 14:06:32 +0200127
128 /* Timer for timeouts */
129 struct timer_list timer;
Andrew Victor65dbf342006-04-02 19:18:51 +0100130};
131
Marc Pignatc5a89c62008-05-30 14:07:47 +0200132/*
133 * Reset the controller and restore most of the state
134 */
135static void at91_reset_host(struct at91mci_host *host)
136{
137 unsigned long flags;
138 u32 mr;
139 u32 sdcr;
140 u32 dtor;
141 u32 imr;
142
143 local_irq_save(flags);
144 imr = at91_mci_read(host, AT91_MCI_IMR);
145
146 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
147
148 /* save current state */
149 mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff;
150 sdcr = at91_mci_read(host, AT91_MCI_SDCR);
151 dtor = at91_mci_read(host, AT91_MCI_DTOR);
152
153 /* reset the controller */
154 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
155
156 /* restore state */
157 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
158 at91_mci_write(host, AT91_MCI_MR, mr);
159 at91_mci_write(host, AT91_MCI_SDCR, sdcr);
160 at91_mci_write(host, AT91_MCI_DTOR, dtor);
161 at91_mci_write(host, AT91_MCI_IER, imr);
162
163 /* make sure sdio interrupts will fire */
164 at91_mci_read(host, AT91_MCI_SR);
165
166 local_irq_restore(flags);
167}
168
Marc Pignate181dce2008-05-30 14:06:32 +0200169static void at91_timeout_timer(unsigned long data)
170{
171 struct at91mci_host *host;
172
173 host = (struct at91mci_host *)data;
174
175 if (host->request) {
176 dev_err(host->mmc->parent, "Timeout waiting end of packet\n");
177
178 if (host->cmd && host->cmd->data) {
179 host->cmd->data->error = -ETIMEDOUT;
180 } else {
181 if (host->cmd)
182 host->cmd->error = -ETIMEDOUT;
183 else
184 host->request->cmd->error = -ETIMEDOUT;
185 }
186
Marc Pignatc5a89c62008-05-30 14:07:47 +0200187 at91_reset_host(host);
Marc Pignate181dce2008-05-30 14:06:32 +0200188 mmc_request_done(host->mmc, host->request);
189 }
190}
191
Andrew Victor65dbf342006-04-02 19:18:51 +0100192/*
193 * Copy from sg to a dma block - used for transfers
194 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200195static inline void at91_mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data)
Andrew Victor65dbf342006-04-02 19:18:51 +0100196{
197 unsigned int len, i, size;
198 unsigned *dmabuf = host->buffer;
199
Ville Syrjala5385edc2008-06-14 20:27:20 +0300200 size = data->blksz * data->blocks;
Andrew Victor65dbf342006-04-02 19:18:51 +0100201 len = data->sg_len;
202
Ville Syrjala5385edc2008-06-14 20:27:20 +0300203 /* AT91SAM926[0/3] Data Write Operation and number of bytes erratum */
204 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
205 if (host->total_length == 12)
206 memset(dmabuf, 0, 12);
207
Andrew Victor65dbf342006-04-02 19:18:51 +0100208 /*
209 * Just loop through all entries. Size might not
210 * be the entire list though so make sure that
211 * we do not transfer too much.
212 */
213 for (i = 0; i < len; i++) {
214 struct scatterlist *sg;
215 int amount;
Andrew Victor65dbf342006-04-02 19:18:51 +0100216 unsigned int *sgbuffer;
217
218 sg = &data->sg[i];
219
Jens Axboe45711f12007-10-22 21:19:53 +0200220 sgbuffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
Andrew Victor65dbf342006-04-02 19:18:51 +0100221 amount = min(size, sg->length);
222 size -= amount;
Andrew Victor65dbf342006-04-02 19:18:51 +0100223
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100224 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
225 int index;
226
227 for (index = 0; index < (amount / 4); index++)
228 *dmabuf++ = swab32(sgbuffer[index]);
Ville Syrjala5385edc2008-06-14 20:27:20 +0300229 } else {
Wolfgang Muees0b3520f2010-03-05 13:43:38 -0800230 char *tmpv = (char *)dmabuf;
231 memcpy(tmpv, sgbuffer, amount);
232 tmpv += amount;
233 dmabuf = (unsigned *)tmpv;
Ville Syrjala5385edc2008-06-14 20:27:20 +0300234 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100235
Wolfgang Muees0b3520f2010-03-05 13:43:38 -0800236 kunmap_atomic(((void *)sgbuffer) - sg->offset, KM_BIO_SRC_IRQ);
Andrew Victor65dbf342006-04-02 19:18:51 +0100237
238 if (size == 0)
239 break;
240 }
241
242 /*
243 * Check that we didn't get a request to transfer
244 * more data than can fit into the SG list.
245 */
246 BUG_ON(size != 0);
247}
248
249/*
250 * Prepare a dma read
251 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200252static void at91_mci_pre_dma_read(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100253{
254 int i;
255 struct scatterlist *sg;
256 struct mmc_command *cmd;
257 struct mmc_data *data;
258
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100259 pr_debug("pre dma read\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100260
261 cmd = host->cmd;
262 if (!cmd) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100263 pr_debug("no command\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100264 return;
265 }
266
267 data = cmd->data;
268 if (!data) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100269 pr_debug("no data\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100270 return;
271 }
272
273 for (i = 0; i < 2; i++) {
274 /* nothing left to transfer */
275 if (host->transfer_index >= data->sg_len) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100276 pr_debug("Nothing left to transfer (index = %d)\n", host->transfer_index);
Andrew Victor65dbf342006-04-02 19:18:51 +0100277 break;
278 }
279
280 /* Check to see if this needs filling */
281 if (i == 0) {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100282 if (at91_mci_read(host, ATMEL_PDC_RCR) != 0) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100283 pr_debug("Transfer active in current\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100284 continue;
285 }
286 }
287 else {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100288 if (at91_mci_read(host, ATMEL_PDC_RNCR) != 0) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100289 pr_debug("Transfer active in next\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100290 continue;
291 }
292 }
293
294 /* Setup the next transfer */
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100295 pr_debug("Using transfer index %d\n", host->transfer_index);
Andrew Victor65dbf342006-04-02 19:18:51 +0100296
297 sg = &data->sg[host->transfer_index++];
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100298 pr_debug("sg = %p\n", sg);
Andrew Victor65dbf342006-04-02 19:18:51 +0100299
Jens Axboe45711f12007-10-22 21:19:53 +0200300 sg->dma_address = dma_map_page(NULL, sg_page(sg), sg->offset, sg->length, DMA_FROM_DEVICE);
Andrew Victor65dbf342006-04-02 19:18:51 +0100301
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100302 pr_debug("dma address = %08X, length = %d\n", sg->dma_address, sg->length);
Andrew Victor65dbf342006-04-02 19:18:51 +0100303
304 if (i == 0) {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100305 at91_mci_write(host, ATMEL_PDC_RPR, sg->dma_address);
Marc Pignat80f92542008-05-30 14:05:24 +0200306 at91_mci_write(host, ATMEL_PDC_RCR, (data->blksz & 0x3) ? sg->length : sg->length / 4);
Andrew Victor65dbf342006-04-02 19:18:51 +0100307 }
308 else {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100309 at91_mci_write(host, ATMEL_PDC_RNPR, sg->dma_address);
Marc Pignat80f92542008-05-30 14:05:24 +0200310 at91_mci_write(host, ATMEL_PDC_RNCR, (data->blksz & 0x3) ? sg->length : sg->length / 4);
Andrew Victor65dbf342006-04-02 19:18:51 +0100311 }
312 }
313
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100314 pr_debug("pre dma read done\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100315}
316
317/*
318 * Handle after a dma read
319 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200320static void at91_mci_post_dma_read(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100321{
322 struct mmc_command *cmd;
323 struct mmc_data *data;
324
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100325 pr_debug("post dma read\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100326
327 cmd = host->cmd;
328 if (!cmd) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100329 pr_debug("no command\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100330 return;
331 }
332
333 data = cmd->data;
334 if (!data) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100335 pr_debug("no data\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100336 return;
337 }
338
339 while (host->in_use_index < host->transfer_index) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100340 struct scatterlist *sg;
341
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100342 pr_debug("finishing index %d\n", host->in_use_index);
Andrew Victor65dbf342006-04-02 19:18:51 +0100343
344 sg = &data->sg[host->in_use_index++];
345
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100346 pr_debug("Unmapping page %08X\n", sg->dma_address);
Andrew Victor65dbf342006-04-02 19:18:51 +0100347
348 dma_unmap_page(NULL, sg->dma_address, sg->length, DMA_FROM_DEVICE);
349
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100350 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
Nicolas Ferreed99c542007-07-09 14:58:16 +0200351 unsigned int *buffer;
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100352 int index;
Andrew Victor65dbf342006-04-02 19:18:51 +0100353
Nicolas Ferreed99c542007-07-09 14:58:16 +0200354 /* Swap the contents of the buffer */
Jens Axboe45711f12007-10-22 21:19:53 +0200355 buffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
Nicolas Ferreed99c542007-07-09 14:58:16 +0200356 pr_debug("buffer = %p, length = %d\n", buffer, sg->length);
357
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100358 for (index = 0; index < (sg->length / 4); index++)
359 buffer[index] = swab32(buffer[index]);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200360
361 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
Andrew Victor65dbf342006-04-02 19:18:51 +0100362 }
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100363
Jens Axboe45711f12007-10-22 21:19:53 +0200364 flush_dcache_page(sg_page(sg));
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200365
366 data->bytes_xfered += sg->length;
Andrew Victor65dbf342006-04-02 19:18:51 +0100367 }
368
369 /* Is there another transfer to trigger? */
370 if (host->transfer_index < data->sg_len)
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200371 at91_mci_pre_dma_read(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100372 else {
Nicolas Ferreed99c542007-07-09 14:58:16 +0200373 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_ENDRX);
Andrew Victore0b19b82006-10-25 19:42:38 +0200374 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_RXBUFF);
Andrew Victor65dbf342006-04-02 19:18:51 +0100375 }
376
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100377 pr_debug("post dma read done\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100378}
379
380/*
381 * Handle transmitted data
382 */
383static void at91_mci_handle_transmitted(struct at91mci_host *host)
384{
385 struct mmc_command *cmd;
386 struct mmc_data *data;
387
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100388 pr_debug("Handling the transmit\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100389
390 /* Disable the transfer */
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100391 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100392
393 /* Now wait for cmd ready */
Andrew Victore0b19b82006-10-25 19:42:38 +0200394 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE);
Andrew Victor65dbf342006-04-02 19:18:51 +0100395
396 cmd = host->cmd;
397 if (!cmd) return;
398
399 data = cmd->data;
400 if (!data) return;
401
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200402 if (cmd->data->blocks > 1) {
Nicolas Ferreed99c542007-07-09 14:58:16 +0200403 pr_debug("multiple write : wait for BLKE...\n");
404 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
405 } else
406 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
Andrew Victor65dbf342006-04-02 19:18:51 +0100407}
408
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200409/*
410 * Update bytes tranfered count during a write operation
411 */
412static void at91_mci_update_bytes_xfered(struct at91mci_host *host)
413{
414 struct mmc_data *data;
415
416 /* always deal with the effective request (and not the current cmd) */
417
418 if (host->request->cmd && host->request->cmd->error != 0)
419 return;
420
421 if (host->request->data) {
422 data = host->request->data;
423 if (data->flags & MMC_DATA_WRITE) {
424 /* card is in IDLE mode now */
425 pr_debug("-> bytes_xfered %d, total_length = %d\n",
426 data->bytes_xfered, host->total_length);
Ville Syrjala5385edc2008-06-14 20:27:20 +0300427 data->bytes_xfered = data->blksz * data->blocks;
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200428 }
429 }
430}
431
432
Nicolas Ferreed99c542007-07-09 14:58:16 +0200433/*Handle after command sent ready*/
434static int at91_mci_handle_cmdrdy(struct at91mci_host *host)
435{
436 if (!host->cmd)
437 return 1;
438 else if (!host->cmd->data) {
439 if (host->flags & FL_SENT_STOP) {
440 /*After multi block write, we must wait for NOTBUSY*/
441 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
442 } else return 1;
443 } else if (host->cmd->data->flags & MMC_DATA_WRITE) {
444 /*After sendding multi-block-write command, start DMA transfer*/
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200445 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_TXBUFE | AT91_MCI_BLKE);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200446 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
447 }
448
449 /* command not completed, have to wait */
450 return 0;
451}
452
453
Andrew Victor65dbf342006-04-02 19:18:51 +0100454/*
455 * Enable the controller
456 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200457static void at91_mci_enable(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100458{
Nicolas Ferreed99c542007-07-09 14:58:16 +0200459 unsigned int mr;
460
Andrew Victore0b19b82006-10-25 19:42:38 +0200461 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200462 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
Andrew Victore0b19b82006-10-25 19:42:38 +0200463 at91_mci_write(host, AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200464 mr = AT91_MCI_PDCMODE | 0x34a;
465
466 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
467 mr |= AT91_MCI_RDPROOF | AT91_MCI_WRPROOF;
468
469 at91_mci_write(host, AT91_MCI_MR, mr);
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100470
471 /* use Slot A or B (only one at same time) */
472 at91_mci_write(host, AT91_MCI_SDCR, host->board->slot_b);
Andrew Victor65dbf342006-04-02 19:18:51 +0100473}
474
475/*
476 * Disable the controller
477 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200478static void at91_mci_disable(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100479{
Andrew Victore0b19b82006-10-25 19:42:38 +0200480 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
Andrew Victor65dbf342006-04-02 19:18:51 +0100481}
482
483/*
484 * Send a command
Andrew Victor65dbf342006-04-02 19:18:51 +0100485 */
Nicolas Ferreed99c542007-07-09 14:58:16 +0200486static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd)
Andrew Victor65dbf342006-04-02 19:18:51 +0100487{
488 unsigned int cmdr, mr;
489 unsigned int block_length;
490 struct mmc_data *data = cmd->data;
491
492 unsigned int blocks;
493 unsigned int ier = 0;
494
495 host->cmd = cmd;
496
Nicolas Ferreed99c542007-07-09 14:58:16 +0200497 /* Needed for leaving busy state before CMD1 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200498 if ((at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100499 pr_debug("Clearing timeout\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200500 at91_mci_write(host, AT91_MCI_ARGR, 0);
501 at91_mci_write(host, AT91_MCI_CMDR, AT91_MCI_OPDCMD);
502 while (!(at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_CMDRDY)) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100503 /* spin */
Andrew Victore0b19b82006-10-25 19:42:38 +0200504 pr_debug("Clearing: SR = %08X\n", at91_mci_read(host, AT91_MCI_SR));
Andrew Victor65dbf342006-04-02 19:18:51 +0100505 }
506 }
Nicolas Ferreed99c542007-07-09 14:58:16 +0200507
Andrew Victor65dbf342006-04-02 19:18:51 +0100508 cmdr = cmd->opcode;
509
510 if (mmc_resp_type(cmd) == MMC_RSP_NONE)
511 cmdr |= AT91_MCI_RSPTYP_NONE;
512 else {
513 /* if a response is expected then allow maximum response latancy */
514 cmdr |= AT91_MCI_MAXLAT;
515 /* set 136 bit response for R2, 48 bit response otherwise */
516 if (mmc_resp_type(cmd) == MMC_RSP_R2)
517 cmdr |= AT91_MCI_RSPTYP_136;
518 else
519 cmdr |= AT91_MCI_RSPTYP_48;
520 }
521
522 if (data) {
Marc Pignat1d4de9e2007-08-09 13:56:29 +0200523
Ville Syrjala9da3cba2008-06-09 22:06:44 +0300524 if (cpu_is_at91rm9200() || cpu_is_at91sam9261()) {
525 if (data->blksz & 0x3) {
526 pr_debug("Unsupported block size\n");
527 cmd->error = -EINVAL;
528 mmc_request_done(host->mmc, host->request);
529 return;
530 }
531 if (data->flags & MMC_DATA_STREAM) {
532 pr_debug("Stream commands not supported\n");
533 cmd->error = -EINVAL;
534 mmc_request_done(host->mmc, host->request);
535 return;
536 }
Marc Pignat1d4de9e2007-08-09 13:56:29 +0200537 }
538
Russell Kinga3fd4a12006-06-04 17:51:15 +0100539 block_length = data->blksz;
Andrew Victor65dbf342006-04-02 19:18:51 +0100540 blocks = data->blocks;
541
542 /* always set data start - also set direction flag for read */
543 if (data->flags & MMC_DATA_READ)
544 cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START);
545 else if (data->flags & MMC_DATA_WRITE)
546 cmdr |= AT91_MCI_TRCMD_START;
547
548 if (data->flags & MMC_DATA_STREAM)
549 cmdr |= AT91_MCI_TRTYP_STREAM;
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200550 if (data->blocks > 1)
Andrew Victor65dbf342006-04-02 19:18:51 +0100551 cmdr |= AT91_MCI_TRTYP_MULTIPLE;
552 }
553 else {
554 block_length = 0;
555 blocks = 0;
556 }
557
Marc Pignatb6cedb32007-06-06 20:27:59 +0200558 if (host->flags & FL_SENT_STOP)
Andrew Victor65dbf342006-04-02 19:18:51 +0100559 cmdr |= AT91_MCI_TRCMD_STOP;
560
561 if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
562 cmdr |= AT91_MCI_OPDCMD;
563
564 /*
565 * Set the arguments and send the command
566 */
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200567 pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",
Andrew Victore0b19b82006-10-25 19:42:38 +0200568 cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(host, AT91_MCI_MR));
Andrew Victor65dbf342006-04-02 19:18:51 +0100569
570 if (!data) {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100571 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS | ATMEL_PDC_RXTDIS);
572 at91_mci_write(host, ATMEL_PDC_RPR, 0);
573 at91_mci_write(host, ATMEL_PDC_RCR, 0);
574 at91_mci_write(host, ATMEL_PDC_RNPR, 0);
575 at91_mci_write(host, ATMEL_PDC_RNCR, 0);
576 at91_mci_write(host, ATMEL_PDC_TPR, 0);
577 at91_mci_write(host, ATMEL_PDC_TCR, 0);
578 at91_mci_write(host, ATMEL_PDC_TNPR, 0);
579 at91_mci_write(host, ATMEL_PDC_TNCR, 0);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200580 ier = AT91_MCI_CMDRDY;
581 } else {
582 /* zero block length and PDC mode */
Ville Syrjala12bd2572008-06-09 22:06:45 +0300583 mr = at91_mci_read(host, AT91_MCI_MR) & 0x5fff;
Marc Pignat80f92542008-05-30 14:05:24 +0200584 mr |= (data->blksz & 0x3) ? AT91_MCI_PDCFBYTE : 0;
585 mr |= (block_length << 16);
586 mr |= AT91_MCI_PDCMODE;
587 at91_mci_write(host, AT91_MCI_MR, mr);
Andrew Victor65dbf342006-04-02 19:18:51 +0100588
Ville Syrjala9da3cba2008-06-09 22:06:44 +0300589 if (!(cpu_is_at91rm9200() || cpu_is_at91sam9261()))
Marc Pignatc5a89c62008-05-30 14:07:47 +0200590 at91_mci_write(host, AT91_MCI_BLKR,
591 AT91_MCI_BLKR_BCNT(blocks) |
592 AT91_MCI_BLKR_BLKLEN(block_length));
593
Nicolas Ferreed99c542007-07-09 14:58:16 +0200594 /*
595 * Disable the PDC controller
596 */
597 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100598
Nicolas Ferreed99c542007-07-09 14:58:16 +0200599 if (cmdr & AT91_MCI_TRCMD_START) {
600 data->bytes_xfered = 0;
601 host->transfer_index = 0;
602 host->in_use_index = 0;
603 if (cmdr & AT91_MCI_TRDIR) {
604 /*
605 * Handle a read
606 */
607 host->buffer = NULL;
608 host->total_length = 0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100609
Nicolas Ferreed99c542007-07-09 14:58:16 +0200610 at91_mci_pre_dma_read(host);
611 ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
612 }
613 else {
614 /*
615 * Handle a write
616 */
617 host->total_length = block_length * blocks;
Ville Syrjala5385edc2008-06-14 20:27:20 +0300618 /*
619 * AT91SAM926[0/3] Data Write Operation and
620 * number of bytes erratum
621 */
622 if (cpu_is_at91sam9260 () || cpu_is_at91sam9263())
623 if (host->total_length < 12)
624 host->total_length = 12;
David Brownelle385ea62008-09-02 14:35:46 -0700625
626 host->buffer = kmalloc(host->total_length, GFP_KERNEL);
627 if (!host->buffer) {
628 pr_debug("Can't alloc tx buffer\n");
629 cmd->error = -ENOMEM;
630 mmc_request_done(host->mmc, host->request);
631 return;
632 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100633
Nicolas Ferreed99c542007-07-09 14:58:16 +0200634 at91_mci_sg_to_dma(host, data);
Andrew Victor65dbf342006-04-02 19:18:51 +0100635
David Brownelle385ea62008-09-02 14:35:46 -0700636 host->physical_address = dma_map_single(NULL,
637 host->buffer, host->total_length,
638 DMA_TO_DEVICE);
639
Nicolas Ferreed99c542007-07-09 14:58:16 +0200640 pr_debug("Transmitting %d bytes\n", host->total_length);
Andrew Victor65dbf342006-04-02 19:18:51 +0100641
Nicolas Ferreed99c542007-07-09 14:58:16 +0200642 at91_mci_write(host, ATMEL_PDC_TPR, host->physical_address);
Marc Pignat80f92542008-05-30 14:05:24 +0200643 at91_mci_write(host, ATMEL_PDC_TCR, (data->blksz & 0x3) ?
644 host->total_length : host->total_length / 4);
645
Nicolas Ferreed99c542007-07-09 14:58:16 +0200646 ier = AT91_MCI_CMDRDY;
647 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100648 }
649 }
650
651 /*
652 * Send the command and then enable the PDC - not the other way round as
653 * the data sheet says
654 */
655
Andrew Victore0b19b82006-10-25 19:42:38 +0200656 at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
657 at91_mci_write(host, AT91_MCI_CMDR, cmdr);
Andrew Victor65dbf342006-04-02 19:18:51 +0100658
659 if (cmdr & AT91_MCI_TRCMD_START) {
660 if (cmdr & AT91_MCI_TRDIR)
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100661 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Andrew Victor65dbf342006-04-02 19:18:51 +0100662 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100663
Nicolas Ferreed99c542007-07-09 14:58:16 +0200664 /* Enable selected interrupts */
Andrew Victordf05a302006-10-23 14:50:09 +0200665 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_ERRORS | ier);
Andrew Victor65dbf342006-04-02 19:18:51 +0100666}
667
668/*
669 * Process the next step in the request
670 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200671static void at91_mci_process_next(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100672{
673 if (!(host->flags & FL_SENT_COMMAND)) {
674 host->flags |= FL_SENT_COMMAND;
Nicolas Ferreed99c542007-07-09 14:58:16 +0200675 at91_mci_send_command(host, host->request->cmd);
Andrew Victor65dbf342006-04-02 19:18:51 +0100676 }
677 else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) {
678 host->flags |= FL_SENT_STOP;
Nicolas Ferreed99c542007-07-09 14:58:16 +0200679 at91_mci_send_command(host, host->request->stop);
Marc Pignate181dce2008-05-30 14:06:32 +0200680 } else {
681 del_timer(&host->timer);
Marc Pignatc5a89c62008-05-30 14:07:47 +0200682 /* the at91rm9200 mci controller hangs after some transfers,
683 * and the workaround is to reset it after each transfer.
684 */
685 if (cpu_is_at91rm9200())
686 at91_reset_host(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100687 mmc_request_done(host->mmc, host->request);
Marc Pignate181dce2008-05-30 14:06:32 +0200688 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100689}
690
691/*
692 * Handle a command that has been completed
693 */
Nicolas Ferreba7deee2008-05-30 14:28:45 +0200694static void at91_mci_completed_command(struct at91mci_host *host, unsigned int status)
Andrew Victor65dbf342006-04-02 19:18:51 +0100695{
696 struct mmc_command *cmd = host->cmd;
Nicolas Ferrefa1fe012008-06-10 11:27:29 +0200697 struct mmc_data *data = cmd->data;
Andrew Victor65dbf342006-04-02 19:18:51 +0100698
Eric Benard7a6588b2008-05-30 14:26:05 +0200699 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
Andrew Victor65dbf342006-04-02 19:18:51 +0100700
Andrew Victore0b19b82006-10-25 19:42:38 +0200701 cmd->resp[0] = at91_mci_read(host, AT91_MCI_RSPR(0));
702 cmd->resp[1] = at91_mci_read(host, AT91_MCI_RSPR(1));
703 cmd->resp[2] = at91_mci_read(host, AT91_MCI_RSPR(2));
704 cmd->resp[3] = at91_mci_read(host, AT91_MCI_RSPR(3));
Andrew Victor65dbf342006-04-02 19:18:51 +0100705
706 if (host->buffer) {
David Brownelle385ea62008-09-02 14:35:46 -0700707 dma_unmap_single(NULL,
708 host->physical_address, host->total_length,
709 DMA_TO_DEVICE);
710 kfree(host->buffer);
Andrew Victor65dbf342006-04-02 19:18:51 +0100711 host->buffer = NULL;
712 }
713
Nicolas Ferreba7deee2008-05-30 14:28:45 +0200714 pr_debug("Status = %08X/%08x [%08X %08X %08X %08X]\n",
715 status, at91_mci_read(host, AT91_MCI_SR),
716 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
Andrew Victor65dbf342006-04-02 19:18:51 +0100717
Andrew Victor9e3866b2007-10-17 11:53:40 +0200718 if (status & AT91_MCI_ERRORS) {
Marc Pignatb6cedb32007-06-06 20:27:59 +0200719 if ((status & AT91_MCI_RCRCE) && !(mmc_resp_type(cmd) & MMC_RSP_CRC)) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200720 cmd->error = 0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100721 }
722 else {
Nicolas Ferrefa1fe012008-06-10 11:27:29 +0200723 if (status & (AT91_MCI_DTOE | AT91_MCI_DCRCE)) {
724 if (data) {
725 if (status & AT91_MCI_DTOE)
726 data->error = -ETIMEDOUT;
727 else if (status & AT91_MCI_DCRCE)
728 data->error = -EILSEQ;
729 }
730 } else {
731 if (status & AT91_MCI_RTOE)
732 cmd->error = -ETIMEDOUT;
733 else if (status & AT91_MCI_RCRCE)
734 cmd->error = -EILSEQ;
735 else
736 cmd->error = -EIO;
737 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100738
Nicolas Ferrefa1fe012008-06-10 11:27:29 +0200739 pr_debug("Error detected and set to %d/%d (cmd = %d, retries = %d)\n",
740 cmd->error, data ? data->error : 0,
741 cmd->opcode, cmd->retries);
Andrew Victor65dbf342006-04-02 19:18:51 +0100742 }
743 }
744 else
Pierre Ossman17b04292007-07-22 22:18:46 +0200745 cmd->error = 0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100746
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200747 at91_mci_process_next(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100748}
749
750/*
751 * Handle an MMC request
752 */
753static void at91_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
754{
755 struct at91mci_host *host = mmc_priv(mmc);
756 host->request = mrq;
757 host->flags = 0;
758
Wolfgang Mueesa04ac5b2010-03-05 13:43:39 -0800759 /* more than 1s timeout needed with slow SD cards */
760 mod_timer(&host->timer, jiffies + msecs_to_jiffies(2000));
Marc Pignate181dce2008-05-30 14:06:32 +0200761
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200762 at91_mci_process_next(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100763}
764
765/*
766 * Set the IOS
767 */
768static void at91_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
769{
770 int clkdiv;
771 struct at91mci_host *host = mmc_priv(mmc);
Andrew Victor3dd3b032006-10-23 14:46:54 +0200772 unsigned long at91_master_clock = clk_get_rate(host->mci_clk);
Andrew Victor65dbf342006-04-02 19:18:51 +0100773
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100774 host->bus_mode = ios->bus_mode;
Andrew Victor65dbf342006-04-02 19:18:51 +0100775
776 if (ios->clock == 0) {
777 /* Disable the MCI controller */
Andrew Victore0b19b82006-10-25 19:42:38 +0200778 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100779 clkdiv = 0;
780 }
781 else {
782 /* Enable the MCI controller */
Andrew Victore0b19b82006-10-25 19:42:38 +0200783 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
Andrew Victor65dbf342006-04-02 19:18:51 +0100784
785 if ((at91_master_clock % (ios->clock * 2)) == 0)
786 clkdiv = ((at91_master_clock / ios->clock) / 2) - 1;
787 else
788 clkdiv = (at91_master_clock / ios->clock) / 2;
789
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100790 pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv,
Andrew Victor65dbf342006-04-02 19:18:51 +0100791 at91_master_clock / (2 * (clkdiv + 1)));
792 }
793 if (ios->bus_width == MMC_BUS_WIDTH_4 && host->board->wire4) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100794 pr_debug("MMC: Setting controller bus width to 4\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200795 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) | AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100796 }
797 else {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100798 pr_debug("MMC: Setting controller bus width to 1\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200799 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100800 }
801
802 /* Set the clock divider */
Andrew Victore0b19b82006-10-25 19:42:38 +0200803 at91_mci_write(host, AT91_MCI_MR, (at91_mci_read(host, AT91_MCI_MR) & ~AT91_MCI_CLKDIV) | clkdiv);
Andrew Victor65dbf342006-04-02 19:18:51 +0100804
805 /* maybe switch power to the card */
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100806 if (host->board->vcc_pin) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100807 switch (ios->power_mode) {
808 case MMC_POWER_OFF:
David Brownell6e996ee2008-02-04 18:12:48 +0100809 gpio_set_value(host->board->vcc_pin, 0);
Andrew Victor65dbf342006-04-02 19:18:51 +0100810 break;
811 case MMC_POWER_UP:
David Brownell6e996ee2008-02-04 18:12:48 +0100812 gpio_set_value(host->board->vcc_pin, 1);
Andrew Victor65dbf342006-04-02 19:18:51 +0100813 break;
Marc Pignate5c0ef92008-05-09 11:07:07 +0200814 case MMC_POWER_ON:
815 break;
816 default:
817 WARN_ON(1);
Andrew Victor65dbf342006-04-02 19:18:51 +0100818 }
819 }
820}
821
822/*
823 * Handle an interrupt
824 */
David Howells7d12e782006-10-05 14:55:46 +0100825static irqreturn_t at91_mci_irq(int irq, void *devid)
Andrew Victor65dbf342006-04-02 19:18:51 +0100826{
827 struct at91mci_host *host = devid;
828 int completed = 0;
Andrew Victordf05a302006-10-23 14:50:09 +0200829 unsigned int int_status, int_mask;
Andrew Victor65dbf342006-04-02 19:18:51 +0100830
Andrew Victore0b19b82006-10-25 19:42:38 +0200831 int_status = at91_mci_read(host, AT91_MCI_SR);
Andrew Victordf05a302006-10-23 14:50:09 +0200832 int_mask = at91_mci_read(host, AT91_MCI_IMR);
Nicolas Ferre37b758e82007-08-08 12:01:44 +0200833
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200834 pr_debug("MCI irq: status = %08X, %08X, %08X\n", int_status, int_mask,
Andrew Victordf05a302006-10-23 14:50:09 +0200835 int_status & int_mask);
Nicolas Ferre37b758e82007-08-08 12:01:44 +0200836
Andrew Victordf05a302006-10-23 14:50:09 +0200837 int_status = int_status & int_mask;
Andrew Victor65dbf342006-04-02 19:18:51 +0100838
Andrew Victordf05a302006-10-23 14:50:09 +0200839 if (int_status & AT91_MCI_ERRORS) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100840 completed = 1;
Nicolas Ferre37b758e82007-08-08 12:01:44 +0200841
Andrew Victordf05a302006-10-23 14:50:09 +0200842 if (int_status & AT91_MCI_UNRE)
843 pr_debug("MMC: Underrun error\n");
844 if (int_status & AT91_MCI_OVRE)
845 pr_debug("MMC: Overrun error\n");
846 if (int_status & AT91_MCI_DTOE)
847 pr_debug("MMC: Data timeout\n");
848 if (int_status & AT91_MCI_DCRCE)
849 pr_debug("MMC: CRC error in data\n");
850 if (int_status & AT91_MCI_RTOE)
851 pr_debug("MMC: Response timeout\n");
852 if (int_status & AT91_MCI_RENDE)
853 pr_debug("MMC: Response end bit error\n");
854 if (int_status & AT91_MCI_RCRCE)
855 pr_debug("MMC: Response CRC error\n");
856 if (int_status & AT91_MCI_RDIRE)
857 pr_debug("MMC: Response direction error\n");
858 if (int_status & AT91_MCI_RINDE)
859 pr_debug("MMC: Response index error\n");
860 } else {
861 /* Only continue processing if no errors */
Andrew Victor65dbf342006-04-02 19:18:51 +0100862
Andrew Victor65dbf342006-04-02 19:18:51 +0100863 if (int_status & AT91_MCI_TXBUFE) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100864 pr_debug("TX buffer empty\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100865 at91_mci_handle_transmitted(host);
866 }
867
Nicolas Ferreed99c542007-07-09 14:58:16 +0200868 if (int_status & AT91_MCI_ENDRX) {
869 pr_debug("ENDRX\n");
870 at91_mci_post_dma_read(host);
871 }
872
Andrew Victor65dbf342006-04-02 19:18:51 +0100873 if (int_status & AT91_MCI_RXBUFF) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100874 pr_debug("RX buffer full\n");
Nicolas Ferreed99c542007-07-09 14:58:16 +0200875 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
876 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_RXBUFF | AT91_MCI_ENDRX);
877 completed = 1;
Andrew Victor65dbf342006-04-02 19:18:51 +0100878 }
879
Andrew Victordf05a302006-10-23 14:50:09 +0200880 if (int_status & AT91_MCI_ENDTX)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100881 pr_debug("Transmit has ended\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100882
Andrew Victor65dbf342006-04-02 19:18:51 +0100883 if (int_status & AT91_MCI_NOTBUSY) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100884 pr_debug("Card is ready\n");
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200885 at91_mci_update_bytes_xfered(host);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200886 completed = 1;
Andrew Victor65dbf342006-04-02 19:18:51 +0100887 }
888
Andrew Victordf05a302006-10-23 14:50:09 +0200889 if (int_status & AT91_MCI_DTIP)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100890 pr_debug("Data transfer in progress\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100891
Nicolas Ferreed99c542007-07-09 14:58:16 +0200892 if (int_status & AT91_MCI_BLKE) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100893 pr_debug("Block transfer has ended\n");
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200894 if (host->request->data && host->request->data->blocks > 1) {
895 /* multi block write : complete multi write
896 * command and send stop */
897 completed = 1;
898 } else {
899 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
900 }
Nicolas Ferreed99c542007-07-09 14:58:16 +0200901 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100902
Eric Benard7a6588b2008-05-30 14:26:05 +0200903 if (int_status & AT91_MCI_SDIOIRQA)
904 mmc_signal_sdio_irq(host->mmc);
905
906 if (int_status & AT91_MCI_SDIOIRQB)
907 mmc_signal_sdio_irq(host->mmc);
908
Andrew Victordf05a302006-10-23 14:50:09 +0200909 if (int_status & AT91_MCI_TXRDY)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100910 pr_debug("Ready to transmit\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100911
Andrew Victordf05a302006-10-23 14:50:09 +0200912 if (int_status & AT91_MCI_RXRDY)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100913 pr_debug("Ready to receive\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100914
915 if (int_status & AT91_MCI_CMDRDY) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100916 pr_debug("Command ready\n");
Nicolas Ferreed99c542007-07-09 14:58:16 +0200917 completed = at91_mci_handle_cmdrdy(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100918 }
919 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100920
921 if (completed) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100922 pr_debug("Completed command\n");
Eric Benard7a6588b2008-05-30 14:26:05 +0200923 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
Nicolas Ferreba7deee2008-05-30 14:28:45 +0200924 at91_mci_completed_command(host, int_status);
Andrew Victordf05a302006-10-23 14:50:09 +0200925 } else
Eric Benard7a6588b2008-05-30 14:26:05 +0200926 at91_mci_write(host, AT91_MCI_IDR, int_status & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
Andrew Victor65dbf342006-04-02 19:18:51 +0100927
928 return IRQ_HANDLED;
929}
930
David Howells7d12e782006-10-05 14:55:46 +0100931static irqreturn_t at91_mmc_det_irq(int irq, void *_host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100932{
933 struct at91mci_host *host = _host;
David Brownell6e996ee2008-02-04 18:12:48 +0100934 int present = !gpio_get_value(irq_to_gpio(irq));
Andrew Victor65dbf342006-04-02 19:18:51 +0100935
936 /*
937 * we expect this irq on both insert and remove,
938 * and use a short delay to debounce.
939 */
940 if (present != host->present) {
941 host->present = present;
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100942 pr_debug("%s: card %s\n", mmc_hostname(host->mmc),
Andrew Victor65dbf342006-04-02 19:18:51 +0100943 present ? "insert" : "remove");
944 if (!present) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100945 pr_debug("****** Resetting SD-card bus width ******\n");
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100946 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100947 }
Wolfgang Mueesa04ac5b2010-03-05 13:43:39 -0800948 /* 0.5s needed because of early card detect switch firing */
949 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
Andrew Victor65dbf342006-04-02 19:18:51 +0100950 }
951 return IRQ_HANDLED;
952}
953
David Brownella26b4982006-12-26 14:45:26 -0800954static int at91_mci_get_ro(struct mmc_host *mmc)
Andrew Victor65dbf342006-04-02 19:18:51 +0100955{
Andrew Victor65dbf342006-04-02 19:18:51 +0100956 struct at91mci_host *host = mmc_priv(mmc);
957
Anton Vorontsov08f80bb2008-06-17 18:17:39 +0400958 if (host->board->wp_pin)
959 return !!gpio_get_value(host->board->wp_pin);
960 /*
961 * Board doesn't support read only detection; let the mmc core
962 * decide what to do.
963 */
964 return -ENOSYS;
Andrew Victor65dbf342006-04-02 19:18:51 +0100965}
966
Eric Benard7a6588b2008-05-30 14:26:05 +0200967static void at91_mci_enable_sdio_irq(struct mmc_host *mmc, int enable)
968{
969 struct at91mci_host *host = mmc_priv(mmc);
970
971 pr_debug("%s: sdio_irq %c : %s\n", mmc_hostname(host->mmc),
972 host->board->slot_b ? 'B':'A', enable ? "enable" : "disable");
973 at91_mci_write(host, enable ? AT91_MCI_IER : AT91_MCI_IDR,
974 host->board->slot_b ? AT91_MCI_SDIOIRQB : AT91_MCI_SDIOIRQA);
975
976}
977
David Brownellab7aefd2006-11-12 17:55:30 -0800978static const struct mmc_host_ops at91_mci_ops = {
Andrew Victor65dbf342006-04-02 19:18:51 +0100979 .request = at91_mci_request,
980 .set_ios = at91_mci_set_ios,
981 .get_ro = at91_mci_get_ro,
Eric Benard7a6588b2008-05-30 14:26:05 +0200982 .enable_sdio_irq = at91_mci_enable_sdio_irq,
Andrew Victor65dbf342006-04-02 19:18:51 +0100983};
984
985/*
986 * Probe for the device
987 */
David Brownella26b4982006-12-26 14:45:26 -0800988static int __init at91_mci_probe(struct platform_device *pdev)
Andrew Victor65dbf342006-04-02 19:18:51 +0100989{
990 struct mmc_host *mmc;
991 struct at91mci_host *host;
Andrew Victor17ea0592006-10-23 14:44:40 +0200992 struct resource *res;
Andrew Victor65dbf342006-04-02 19:18:51 +0100993 int ret;
994
Andrew Victor17ea0592006-10-23 14:44:40 +0200995 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
996 if (!res)
997 return -ENXIO;
998
999 if (!request_mem_region(res->start, res->end - res->start + 1, DRIVER_NAME))
1000 return -EBUSY;
1001
Andrew Victor65dbf342006-04-02 19:18:51 +01001002 mmc = mmc_alloc_host(sizeof(struct at91mci_host), &pdev->dev);
1003 if (!mmc) {
David Brownell6e996ee2008-02-04 18:12:48 +01001004 ret = -ENOMEM;
1005 dev_dbg(&pdev->dev, "couldn't allocate mmc host\n");
1006 goto fail6;
Andrew Victor65dbf342006-04-02 19:18:51 +01001007 }
1008
1009 mmc->ops = &at91_mci_ops;
1010 mmc->f_min = 375000;
1011 mmc->f_max = 25000000;
1012 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Pierre Ossman23af6032008-07-06 01:10:27 +02001013 mmc->caps = MMC_CAP_SDIO_IRQ;
Andrew Victor65dbf342006-04-02 19:18:51 +01001014
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001015 mmc->max_blk_size = 4095;
Pierre Ossman55db8902006-11-21 17:55:45 +01001016 mmc->max_blk_count = mmc->max_req_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001017
Andrew Victor65dbf342006-04-02 19:18:51 +01001018 host = mmc_priv(mmc);
1019 host->mmc = mmc;
1020 host->buffer = NULL;
1021 host->bus_mode = 0;
1022 host->board = pdev->dev.platform_data;
1023 if (host->board->wire4) {
Nicolas Ferreed99c542007-07-09 14:58:16 +02001024 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
1025 mmc->caps |= MMC_CAP_4_BIT_DATA;
1026 else
David Brownell6e996ee2008-02-04 18:12:48 +01001027 dev_warn(&pdev->dev, "4 wire bus mode not supported"
Nicolas Ferreed99c542007-07-09 14:58:16 +02001028 " - using 1 wire\n");
Andrew Victor65dbf342006-04-02 19:18:51 +01001029 }
1030
1031 /*
David Brownell6e996ee2008-02-04 18:12:48 +01001032 * Reserve GPIOs ... board init code makes sure these pins are set
1033 * up as GPIOs with the right direction (input, except for vcc)
1034 */
1035 if (host->board->det_pin) {
1036 ret = gpio_request(host->board->det_pin, "mmc_detect");
1037 if (ret < 0) {
1038 dev_dbg(&pdev->dev, "couldn't claim card detect pin\n");
1039 goto fail5;
1040 }
1041 }
1042 if (host->board->wp_pin) {
1043 ret = gpio_request(host->board->wp_pin, "mmc_wp");
1044 if (ret < 0) {
1045 dev_dbg(&pdev->dev, "couldn't claim wp sense pin\n");
1046 goto fail4;
1047 }
1048 }
1049 if (host->board->vcc_pin) {
1050 ret = gpio_request(host->board->vcc_pin, "mmc_vcc");
1051 if (ret < 0) {
1052 dev_dbg(&pdev->dev, "couldn't claim vcc switch pin\n");
1053 goto fail3;
1054 }
1055 }
1056
1057 /*
Andrew Victor65dbf342006-04-02 19:18:51 +01001058 * Get Clock
1059 */
Andrew Victor3dd3b032006-10-23 14:46:54 +02001060 host->mci_clk = clk_get(&pdev->dev, "mci_clk");
1061 if (IS_ERR(host->mci_clk)) {
David Brownell6e996ee2008-02-04 18:12:48 +01001062 ret = -ENODEV;
1063 dev_dbg(&pdev->dev, "no mci_clk?\n");
1064 goto fail2;
Andrew Victor65dbf342006-04-02 19:18:51 +01001065 }
Andrew Victor65dbf342006-04-02 19:18:51 +01001066
Andrew Victor17ea0592006-10-23 14:44:40 +02001067 /*
1068 * Map I/O region
1069 */
1070 host->baseaddr = ioremap(res->start, res->end - res->start + 1);
1071 if (!host->baseaddr) {
David Brownell6e996ee2008-02-04 18:12:48 +01001072 ret = -ENOMEM;
1073 goto fail1;
Andrew Victor17ea0592006-10-23 14:44:40 +02001074 }
Andrew Victore0b19b82006-10-25 19:42:38 +02001075
1076 /*
1077 * Reset hardware
1078 */
Andrew Victor3dd3b032006-10-23 14:46:54 +02001079 clk_enable(host->mci_clk); /* Enable the peripheral clock */
Andrew Victore0b19b82006-10-25 19:42:38 +02001080 at91_mci_disable(host);
1081 at91_mci_enable(host);
1082
Andrew Victor65dbf342006-04-02 19:18:51 +01001083 /*
1084 * Allocate the MCI interrupt
1085 */
Andrew Victor17ea0592006-10-23 14:44:40 +02001086 host->irq = platform_get_irq(pdev, 0);
David Brownell6e996ee2008-02-04 18:12:48 +01001087 ret = request_irq(host->irq, at91_mci_irq, IRQF_SHARED,
1088 mmc_hostname(mmc), host);
Andrew Victor65dbf342006-04-02 19:18:51 +01001089 if (ret) {
David Brownell6e996ee2008-02-04 18:12:48 +01001090 dev_dbg(&pdev->dev, "request MCI interrupt failed\n");
1091 goto fail0;
Andrew Victor65dbf342006-04-02 19:18:51 +01001092 }
1093
Nicolas Ferre99ba0402008-11-27 17:23:49 +01001094 setup_timer(&host->timer, at91_timeout_timer, (unsigned long)host);
1095
Andrew Victor65dbf342006-04-02 19:18:51 +01001096 platform_set_drvdata(pdev, mmc);
1097
1098 /*
1099 * Add host to MMC layer
1100 */
Marc Pignat63b66432007-07-16 11:07:02 +02001101 if (host->board->det_pin) {
David Brownell6e996ee2008-02-04 18:12:48 +01001102 host->present = !gpio_get_value(host->board->det_pin);
Marc Pignat63b66432007-07-16 11:07:02 +02001103 }
Andrew Victor65dbf342006-04-02 19:18:51 +01001104 else
1105 host->present = -1;
1106
1107 mmc_add_host(mmc);
1108
1109 /*
1110 * monitor card insertion/removal if we can
1111 */
1112 if (host->board->det_pin) {
David Brownell6e996ee2008-02-04 18:12:48 +01001113 ret = request_irq(gpio_to_irq(host->board->det_pin),
1114 at91_mmc_det_irq, 0, mmc_hostname(mmc), host);
Andrew Victor65dbf342006-04-02 19:18:51 +01001115 if (ret)
David Brownell6e996ee2008-02-04 18:12:48 +01001116 dev_warn(&pdev->dev, "request MMC detect irq failed\n");
1117 else
1118 device_init_wakeup(&pdev->dev, 1);
Andrew Victor65dbf342006-04-02 19:18:51 +01001119 }
1120
Andrew Victorf3a8efa2006-10-23 14:53:20 +02001121 pr_debug("Added MCI driver\n");
Andrew Victor65dbf342006-04-02 19:18:51 +01001122
1123 return 0;
David Brownell6e996ee2008-02-04 18:12:48 +01001124
1125fail0:
1126 clk_disable(host->mci_clk);
1127 iounmap(host->baseaddr);
1128fail1:
1129 clk_put(host->mci_clk);
1130fail2:
1131 if (host->board->vcc_pin)
1132 gpio_free(host->board->vcc_pin);
1133fail3:
1134 if (host->board->wp_pin)
1135 gpio_free(host->board->wp_pin);
1136fail4:
1137 if (host->board->det_pin)
1138 gpio_free(host->board->det_pin);
1139fail5:
1140 mmc_free_host(mmc);
1141fail6:
1142 release_mem_region(res->start, res->end - res->start + 1);
1143 dev_err(&pdev->dev, "probe failed, err %d\n", ret);
1144 return ret;
Andrew Victor65dbf342006-04-02 19:18:51 +01001145}
1146
1147/*
1148 * Remove a device
1149 */
David Brownella26b4982006-12-26 14:45:26 -08001150static int __exit at91_mci_remove(struct platform_device *pdev)
Andrew Victor65dbf342006-04-02 19:18:51 +01001151{
1152 struct mmc_host *mmc = platform_get_drvdata(pdev);
1153 struct at91mci_host *host;
Andrew Victor17ea0592006-10-23 14:44:40 +02001154 struct resource *res;
Andrew Victor65dbf342006-04-02 19:18:51 +01001155
1156 if (!mmc)
1157 return -1;
1158
1159 host = mmc_priv(mmc);
1160
Anti Sulline0cda542007-08-30 16:15:16 +02001161 if (host->board->det_pin) {
David Brownell6e996ee2008-02-04 18:12:48 +01001162 if (device_can_wakeup(&pdev->dev))
1163 free_irq(gpio_to_irq(host->board->det_pin), host);
Marc Pignat63b66432007-07-16 11:07:02 +02001164 device_init_wakeup(&pdev->dev, 0);
David Brownell6e996ee2008-02-04 18:12:48 +01001165 gpio_free(host->board->det_pin);
Andrew Victor65dbf342006-04-02 19:18:51 +01001166 }
1167
Andrew Victore0b19b82006-10-25 19:42:38 +02001168 at91_mci_disable(host);
Marc Pignate181dce2008-05-30 14:06:32 +02001169 del_timer_sync(&host->timer);
Andrew Victor17ea0592006-10-23 14:44:40 +02001170 mmc_remove_host(mmc);
1171 free_irq(host->irq, host);
Andrew Victor65dbf342006-04-02 19:18:51 +01001172
Andrew Victor3dd3b032006-10-23 14:46:54 +02001173 clk_disable(host->mci_clk); /* Disable the peripheral clock */
1174 clk_put(host->mci_clk);
Andrew Victor65dbf342006-04-02 19:18:51 +01001175
David Brownell6e996ee2008-02-04 18:12:48 +01001176 if (host->board->vcc_pin)
1177 gpio_free(host->board->vcc_pin);
1178 if (host->board->wp_pin)
1179 gpio_free(host->board->wp_pin);
1180
Andrew Victor17ea0592006-10-23 14:44:40 +02001181 iounmap(host->baseaddr);
1182 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1183 release_mem_region(res->start, res->end - res->start + 1);
Andrew Victor65dbf342006-04-02 19:18:51 +01001184
Andrew Victor17ea0592006-10-23 14:44:40 +02001185 mmc_free_host(mmc);
1186 platform_set_drvdata(pdev, NULL);
Andrew Victorb44fb7a2006-06-19 13:06:05 +01001187 pr_debug("MCI Removed\n");
Andrew Victor65dbf342006-04-02 19:18:51 +01001188
1189 return 0;
1190}
1191
1192#ifdef CONFIG_PM
1193static int at91_mci_suspend(struct platform_device *pdev, pm_message_t state)
1194{
1195 struct mmc_host *mmc = platform_get_drvdata(pdev);
Marc Pignat63b66432007-07-16 11:07:02 +02001196 struct at91mci_host *host = mmc_priv(mmc);
Andrew Victor65dbf342006-04-02 19:18:51 +01001197 int ret = 0;
1198
Anti Sulline0cda542007-08-30 16:15:16 +02001199 if (host->board->det_pin && device_may_wakeup(&pdev->dev))
Marc Pignat63b66432007-07-16 11:07:02 +02001200 enable_irq_wake(host->board->det_pin);
1201
Andrew Victor65dbf342006-04-02 19:18:51 +01001202 if (mmc)
1203 ret = mmc_suspend_host(mmc, state);
1204
1205 return ret;
1206}
1207
1208static int at91_mci_resume(struct platform_device *pdev)
1209{
1210 struct mmc_host *mmc = platform_get_drvdata(pdev);
Marc Pignat63b66432007-07-16 11:07:02 +02001211 struct at91mci_host *host = mmc_priv(mmc);
Andrew Victor65dbf342006-04-02 19:18:51 +01001212 int ret = 0;
1213
Anti Sulline0cda542007-08-30 16:15:16 +02001214 if (host->board->det_pin && device_may_wakeup(&pdev->dev))
Marc Pignat63b66432007-07-16 11:07:02 +02001215 disable_irq_wake(host->board->det_pin);
1216
Andrew Victor65dbf342006-04-02 19:18:51 +01001217 if (mmc)
1218 ret = mmc_resume_host(mmc);
1219
1220 return ret;
1221}
1222#else
1223#define at91_mci_suspend NULL
1224#define at91_mci_resume NULL
1225#endif
1226
1227static struct platform_driver at91_mci_driver = {
David Brownella26b4982006-12-26 14:45:26 -08001228 .remove = __exit_p(at91_mci_remove),
Andrew Victor65dbf342006-04-02 19:18:51 +01001229 .suspend = at91_mci_suspend,
1230 .resume = at91_mci_resume,
1231 .driver = {
1232 .name = DRIVER_NAME,
1233 .owner = THIS_MODULE,
1234 },
1235};
1236
1237static int __init at91_mci_init(void)
1238{
David Brownella26b4982006-12-26 14:45:26 -08001239 return platform_driver_probe(&at91_mci_driver, at91_mci_probe);
Andrew Victor65dbf342006-04-02 19:18:51 +01001240}
1241
1242static void __exit at91_mci_exit(void)
1243{
1244 platform_driver_unregister(&at91_mci_driver);
1245}
1246
1247module_init(at91_mci_init);
1248module_exit(at91_mci_exit);
1249
1250MODULE_DESCRIPTION("AT91 Multimedia Card Interface driver");
1251MODULE_AUTHOR("Nick Randell");
1252MODULE_LICENSE("GPL");
Kay Sieversbc65c722008-04-15 14:34:28 -07001253MODULE_ALIAS("platform:at91_mci");