blob: d3e96ff4142877238311761d8489a2f71ad3998f [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
Andrew Victor65dbf342006-04-02 19:18:51 +010075#include <asm/mach/mmc.h>
76#include <asm/arch/board.h>
Andrew Victor99eeb8d2006-12-11 12:40:23 +010077#include <asm/arch/cpu.h>
Andrew Victor55d8bae2006-11-30 17:16:43 +010078#include <asm/arch/at91_mci.h>
Andrew Victor65dbf342006-04-02 19:18:51 +010079
80#define DRIVER_NAME "at91_mci"
81
Andrew Victordf05a302006-10-23 14:50:09 +020082#define FL_SENT_COMMAND (1 << 0)
83#define FL_SENT_STOP (1 << 1)
Andrew Victor65dbf342006-04-02 19:18:51 +010084
Andrew Victordf05a302006-10-23 14:50:09 +020085#define AT91_MCI_ERRORS (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE \
86 | AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE \
Nicolas Ferre37b758e82007-08-08 12:01:44 +020087 | AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)
Andrew Victor65dbf342006-04-02 19:18:51 +010088
Andrew Victore0b19b82006-10-25 19:42:38 +020089#define at91_mci_read(host, reg) __raw_readl((host)->baseaddr + (reg))
90#define at91_mci_write(host, reg, val) __raw_writel((val), (host)->baseaddr + (reg))
Andrew Victor65dbf342006-04-02 19:18:51 +010091
Andrew Victor65dbf342006-04-02 19:18:51 +010092
93/*
94 * Low level type for this driver
95 */
96struct at91mci_host
97{
98 struct mmc_host *mmc;
99 struct mmc_command *cmd;
100 struct mmc_request *request;
101
Andrew Victore0b19b82006-10-25 19:42:38 +0200102 void __iomem *baseaddr;
Andrew Victor17ea0592006-10-23 14:44:40 +0200103 int irq;
Andrew Victore0b19b82006-10-25 19:42:38 +0200104
Andrew Victor65dbf342006-04-02 19:18:51 +0100105 struct at91_mmc_data *board;
106 int present;
107
Andrew Victor3dd3b032006-10-23 14:46:54 +0200108 struct clk *mci_clk;
109
Andrew Victor65dbf342006-04-02 19:18:51 +0100110 /*
111 * Flag indicating when the command has been sent. This is used to
112 * work out whether or not to send the stop
113 */
114 unsigned int flags;
115 /* flag for current bus settings */
116 u32 bus_mode;
117
118 /* DMA buffer used for transmitting */
119 unsigned int* buffer;
120 dma_addr_t physical_address;
121 unsigned int total_length;
122
123 /* Latest in the scatterlist that has been enabled for transfer, but not freed */
124 int in_use_index;
125
126 /* Latest in the scatterlist that has been enabled for transfer */
127 int transfer_index;
Marc Pignate181dce2008-05-30 14:06:32 +0200128
129 /* Timer for timeouts */
130 struct timer_list timer;
Andrew Victor65dbf342006-04-02 19:18:51 +0100131};
132
Marc Pignatc5a89c62008-05-30 14:07:47 +0200133/*
134 * Reset the controller and restore most of the state
135 */
136static void at91_reset_host(struct at91mci_host *host)
137{
138 unsigned long flags;
139 u32 mr;
140 u32 sdcr;
141 u32 dtor;
142 u32 imr;
143
144 local_irq_save(flags);
145 imr = at91_mci_read(host, AT91_MCI_IMR);
146
147 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
148
149 /* save current state */
150 mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff;
151 sdcr = at91_mci_read(host, AT91_MCI_SDCR);
152 dtor = at91_mci_read(host, AT91_MCI_DTOR);
153
154 /* reset the controller */
155 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
156
157 /* restore state */
158 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
159 at91_mci_write(host, AT91_MCI_MR, mr);
160 at91_mci_write(host, AT91_MCI_SDCR, sdcr);
161 at91_mci_write(host, AT91_MCI_DTOR, dtor);
162 at91_mci_write(host, AT91_MCI_IER, imr);
163
164 /* make sure sdio interrupts will fire */
165 at91_mci_read(host, AT91_MCI_SR);
166
167 local_irq_restore(flags);
168}
169
Marc Pignate181dce2008-05-30 14:06:32 +0200170static void at91_timeout_timer(unsigned long data)
171{
172 struct at91mci_host *host;
173
174 host = (struct at91mci_host *)data;
175
176 if (host->request) {
177 dev_err(host->mmc->parent, "Timeout waiting end of packet\n");
178
179 if (host->cmd && host->cmd->data) {
180 host->cmd->data->error = -ETIMEDOUT;
181 } else {
182 if (host->cmd)
183 host->cmd->error = -ETIMEDOUT;
184 else
185 host->request->cmd->error = -ETIMEDOUT;
186 }
187
Marc Pignatc5a89c62008-05-30 14:07:47 +0200188 at91_reset_host(host);
Marc Pignate181dce2008-05-30 14:06:32 +0200189 mmc_request_done(host->mmc, host->request);
190 }
191}
192
Andrew Victor65dbf342006-04-02 19:18:51 +0100193/*
194 * Copy from sg to a dma block - used for transfers
195 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200196static inline void at91_mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data)
Andrew Victor65dbf342006-04-02 19:18:51 +0100197{
198 unsigned int len, i, size;
199 unsigned *dmabuf = host->buffer;
200
Ville Syrjala5385edc2008-06-14 20:27:20 +0300201 size = data->blksz * data->blocks;
Andrew Victor65dbf342006-04-02 19:18:51 +0100202 len = data->sg_len;
203
Ville Syrjala5385edc2008-06-14 20:27:20 +0300204 /* AT91SAM926[0/3] Data Write Operation and number of bytes erratum */
205 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
206 if (host->total_length == 12)
207 memset(dmabuf, 0, 12);
208
Andrew Victor65dbf342006-04-02 19:18:51 +0100209 /*
210 * Just loop through all entries. Size might not
211 * be the entire list though so make sure that
212 * we do not transfer too much.
213 */
214 for (i = 0; i < len; i++) {
215 struct scatterlist *sg;
216 int amount;
Andrew Victor65dbf342006-04-02 19:18:51 +0100217 unsigned int *sgbuffer;
218
219 sg = &data->sg[i];
220
Jens Axboe45711f12007-10-22 21:19:53 +0200221 sgbuffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
Andrew Victor65dbf342006-04-02 19:18:51 +0100222 amount = min(size, sg->length);
223 size -= amount;
Andrew Victor65dbf342006-04-02 19:18:51 +0100224
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100225 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
226 int index;
227
228 for (index = 0; index < (amount / 4); index++)
229 *dmabuf++ = swab32(sgbuffer[index]);
Ville Syrjala5385edc2008-06-14 20:27:20 +0300230 } else {
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100231 memcpy(dmabuf, sgbuffer, amount);
Ville Syrjala5385edc2008-06-14 20:27:20 +0300232 dmabuf += amount;
233 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100234
235 kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
236
237 if (size == 0)
238 break;
239 }
240
241 /*
242 * Check that we didn't get a request to transfer
243 * more data than can fit into the SG list.
244 */
245 BUG_ON(size != 0);
246}
247
248/*
249 * Prepare a dma read
250 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200251static void at91_mci_pre_dma_read(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100252{
253 int i;
254 struct scatterlist *sg;
255 struct mmc_command *cmd;
256 struct mmc_data *data;
257
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100258 pr_debug("pre dma read\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100259
260 cmd = host->cmd;
261 if (!cmd) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100262 pr_debug("no command\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100263 return;
264 }
265
266 data = cmd->data;
267 if (!data) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100268 pr_debug("no data\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100269 return;
270 }
271
272 for (i = 0; i < 2; i++) {
273 /* nothing left to transfer */
274 if (host->transfer_index >= data->sg_len) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100275 pr_debug("Nothing left to transfer (index = %d)\n", host->transfer_index);
Andrew Victor65dbf342006-04-02 19:18:51 +0100276 break;
277 }
278
279 /* Check to see if this needs filling */
280 if (i == 0) {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100281 if (at91_mci_read(host, ATMEL_PDC_RCR) != 0) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100282 pr_debug("Transfer active in current\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100283 continue;
284 }
285 }
286 else {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100287 if (at91_mci_read(host, ATMEL_PDC_RNCR) != 0) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100288 pr_debug("Transfer active in next\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100289 continue;
290 }
291 }
292
293 /* Setup the next transfer */
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100294 pr_debug("Using transfer index %d\n", host->transfer_index);
Andrew Victor65dbf342006-04-02 19:18:51 +0100295
296 sg = &data->sg[host->transfer_index++];
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100297 pr_debug("sg = %p\n", sg);
Andrew Victor65dbf342006-04-02 19:18:51 +0100298
Jens Axboe45711f12007-10-22 21:19:53 +0200299 sg->dma_address = dma_map_page(NULL, sg_page(sg), sg->offset, sg->length, DMA_FROM_DEVICE);
Andrew Victor65dbf342006-04-02 19:18:51 +0100300
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100301 pr_debug("dma address = %08X, length = %d\n", sg->dma_address, sg->length);
Andrew Victor65dbf342006-04-02 19:18:51 +0100302
303 if (i == 0) {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100304 at91_mci_write(host, ATMEL_PDC_RPR, sg->dma_address);
Marc Pignat80f92542008-05-30 14:05:24 +0200305 at91_mci_write(host, ATMEL_PDC_RCR, (data->blksz & 0x3) ? sg->length : sg->length / 4);
Andrew Victor65dbf342006-04-02 19:18:51 +0100306 }
307 else {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100308 at91_mci_write(host, ATMEL_PDC_RNPR, sg->dma_address);
Marc Pignat80f92542008-05-30 14:05:24 +0200309 at91_mci_write(host, ATMEL_PDC_RNCR, (data->blksz & 0x3) ? sg->length : sg->length / 4);
Andrew Victor65dbf342006-04-02 19:18:51 +0100310 }
311 }
312
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100313 pr_debug("pre dma read done\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100314}
315
316/*
317 * Handle after a dma read
318 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200319static void at91_mci_post_dma_read(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100320{
321 struct mmc_command *cmd;
322 struct mmc_data *data;
323
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100324 pr_debug("post dma read\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100325
326 cmd = host->cmd;
327 if (!cmd) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100328 pr_debug("no command\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100329 return;
330 }
331
332 data = cmd->data;
333 if (!data) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100334 pr_debug("no data\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100335 return;
336 }
337
338 while (host->in_use_index < host->transfer_index) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100339 struct scatterlist *sg;
340
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100341 pr_debug("finishing index %d\n", host->in_use_index);
Andrew Victor65dbf342006-04-02 19:18:51 +0100342
343 sg = &data->sg[host->in_use_index++];
344
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100345 pr_debug("Unmapping page %08X\n", sg->dma_address);
Andrew Victor65dbf342006-04-02 19:18:51 +0100346
347 dma_unmap_page(NULL, sg->dma_address, sg->length, DMA_FROM_DEVICE);
348
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100349 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
Nicolas Ferreed99c542007-07-09 14:58:16 +0200350 unsigned int *buffer;
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100351 int index;
Andrew Victor65dbf342006-04-02 19:18:51 +0100352
Nicolas Ferreed99c542007-07-09 14:58:16 +0200353 /* Swap the contents of the buffer */
Jens Axboe45711f12007-10-22 21:19:53 +0200354 buffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
Nicolas Ferreed99c542007-07-09 14:58:16 +0200355 pr_debug("buffer = %p, length = %d\n", buffer, sg->length);
356
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100357 for (index = 0; index < (sg->length / 4); index++)
358 buffer[index] = swab32(buffer[index]);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200359
360 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
Andrew Victor65dbf342006-04-02 19:18:51 +0100361 }
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100362
Jens Axboe45711f12007-10-22 21:19:53 +0200363 flush_dcache_page(sg_page(sg));
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200364
365 data->bytes_xfered += sg->length;
Andrew Victor65dbf342006-04-02 19:18:51 +0100366 }
367
368 /* Is there another transfer to trigger? */
369 if (host->transfer_index < data->sg_len)
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200370 at91_mci_pre_dma_read(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100371 else {
Nicolas Ferreed99c542007-07-09 14:58:16 +0200372 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_ENDRX);
Andrew Victore0b19b82006-10-25 19:42:38 +0200373 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_RXBUFF);
Andrew Victor65dbf342006-04-02 19:18:51 +0100374 }
375
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100376 pr_debug("post dma read done\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100377}
378
379/*
380 * Handle transmitted data
381 */
382static void at91_mci_handle_transmitted(struct at91mci_host *host)
383{
384 struct mmc_command *cmd;
385 struct mmc_data *data;
386
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100387 pr_debug("Handling the transmit\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100388
389 /* Disable the transfer */
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100390 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100391
392 /* Now wait for cmd ready */
Andrew Victore0b19b82006-10-25 19:42:38 +0200393 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE);
Andrew Victor65dbf342006-04-02 19:18:51 +0100394
395 cmd = host->cmd;
396 if (!cmd) return;
397
398 data = cmd->data;
399 if (!data) return;
400
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200401 if (cmd->data->blocks > 1) {
Nicolas Ferreed99c542007-07-09 14:58:16 +0200402 pr_debug("multiple write : wait for BLKE...\n");
403 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
404 } else
405 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
Andrew Victor65dbf342006-04-02 19:18:51 +0100406}
407
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200408/*
409 * Update bytes tranfered count during a write operation
410 */
411static void at91_mci_update_bytes_xfered(struct at91mci_host *host)
412{
413 struct mmc_data *data;
414
415 /* always deal with the effective request (and not the current cmd) */
416
417 if (host->request->cmd && host->request->cmd->error != 0)
418 return;
419
420 if (host->request->data) {
421 data = host->request->data;
422 if (data->flags & MMC_DATA_WRITE) {
423 /* card is in IDLE mode now */
424 pr_debug("-> bytes_xfered %d, total_length = %d\n",
425 data->bytes_xfered, host->total_length);
Ville Syrjala5385edc2008-06-14 20:27:20 +0300426 data->bytes_xfered = data->blksz * data->blocks;
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200427 }
428 }
429}
430
431
Nicolas Ferreed99c542007-07-09 14:58:16 +0200432/*Handle after command sent ready*/
433static int at91_mci_handle_cmdrdy(struct at91mci_host *host)
434{
435 if (!host->cmd)
436 return 1;
437 else if (!host->cmd->data) {
438 if (host->flags & FL_SENT_STOP) {
439 /*After multi block write, we must wait for NOTBUSY*/
440 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
441 } else return 1;
442 } else if (host->cmd->data->flags & MMC_DATA_WRITE) {
443 /*After sendding multi-block-write command, start DMA transfer*/
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200444 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_TXBUFE | AT91_MCI_BLKE);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200445 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
446 }
447
448 /* command not completed, have to wait */
449 return 0;
450}
451
452
Andrew Victor65dbf342006-04-02 19:18:51 +0100453/*
454 * Enable the controller
455 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200456static void at91_mci_enable(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100457{
Nicolas Ferreed99c542007-07-09 14:58:16 +0200458 unsigned int mr;
459
Andrew Victore0b19b82006-10-25 19:42:38 +0200460 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200461 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
Andrew Victore0b19b82006-10-25 19:42:38 +0200462 at91_mci_write(host, AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200463 mr = AT91_MCI_PDCMODE | 0x34a;
464
465 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
466 mr |= AT91_MCI_RDPROOF | AT91_MCI_WRPROOF;
467
468 at91_mci_write(host, AT91_MCI_MR, mr);
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100469
470 /* use Slot A or B (only one at same time) */
471 at91_mci_write(host, AT91_MCI_SDCR, host->board->slot_b);
Andrew Victor65dbf342006-04-02 19:18:51 +0100472}
473
474/*
475 * Disable the controller
476 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200477static void at91_mci_disable(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100478{
Andrew Victore0b19b82006-10-25 19:42:38 +0200479 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
Andrew Victor65dbf342006-04-02 19:18:51 +0100480}
481
482/*
483 * Send a command
Andrew Victor65dbf342006-04-02 19:18:51 +0100484 */
Nicolas Ferreed99c542007-07-09 14:58:16 +0200485static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd)
Andrew Victor65dbf342006-04-02 19:18:51 +0100486{
487 unsigned int cmdr, mr;
488 unsigned int block_length;
489 struct mmc_data *data = cmd->data;
490
491 unsigned int blocks;
492 unsigned int ier = 0;
493
494 host->cmd = cmd;
495
Nicolas Ferreed99c542007-07-09 14:58:16 +0200496 /* Needed for leaving busy state before CMD1 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200497 if ((at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100498 pr_debug("Clearing timeout\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200499 at91_mci_write(host, AT91_MCI_ARGR, 0);
500 at91_mci_write(host, AT91_MCI_CMDR, AT91_MCI_OPDCMD);
501 while (!(at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_CMDRDY)) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100502 /* spin */
Andrew Victore0b19b82006-10-25 19:42:38 +0200503 pr_debug("Clearing: SR = %08X\n", at91_mci_read(host, AT91_MCI_SR));
Andrew Victor65dbf342006-04-02 19:18:51 +0100504 }
505 }
Nicolas Ferreed99c542007-07-09 14:58:16 +0200506
Andrew Victor65dbf342006-04-02 19:18:51 +0100507 cmdr = cmd->opcode;
508
509 if (mmc_resp_type(cmd) == MMC_RSP_NONE)
510 cmdr |= AT91_MCI_RSPTYP_NONE;
511 else {
512 /* if a response is expected then allow maximum response latancy */
513 cmdr |= AT91_MCI_MAXLAT;
514 /* set 136 bit response for R2, 48 bit response otherwise */
515 if (mmc_resp_type(cmd) == MMC_RSP_R2)
516 cmdr |= AT91_MCI_RSPTYP_136;
517 else
518 cmdr |= AT91_MCI_RSPTYP_48;
519 }
520
521 if (data) {
Marc Pignat1d4de9e2007-08-09 13:56:29 +0200522
Marc Pignat80f92542008-05-30 14:05:24 +0200523 if ( cpu_is_at91rm9200() && (data->blksz & 0x3) ) {
Marc Pignat1d4de9e2007-08-09 13:56:29 +0200524 pr_debug("Unsupported block size\n");
525 cmd->error = -EINVAL;
526 mmc_request_done(host->mmc, host->request);
527 return;
528 }
529
Russell Kinga3fd4a12006-06-04 17:51:15 +0100530 block_length = data->blksz;
Andrew Victor65dbf342006-04-02 19:18:51 +0100531 blocks = data->blocks;
532
533 /* always set data start - also set direction flag for read */
534 if (data->flags & MMC_DATA_READ)
535 cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START);
536 else if (data->flags & MMC_DATA_WRITE)
537 cmdr |= AT91_MCI_TRCMD_START;
538
539 if (data->flags & MMC_DATA_STREAM)
540 cmdr |= AT91_MCI_TRTYP_STREAM;
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200541 if (data->blocks > 1)
Andrew Victor65dbf342006-04-02 19:18:51 +0100542 cmdr |= AT91_MCI_TRTYP_MULTIPLE;
543 }
544 else {
545 block_length = 0;
546 blocks = 0;
547 }
548
Marc Pignatb6cedb32007-06-06 20:27:59 +0200549 if (host->flags & FL_SENT_STOP)
Andrew Victor65dbf342006-04-02 19:18:51 +0100550 cmdr |= AT91_MCI_TRCMD_STOP;
551
552 if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
553 cmdr |= AT91_MCI_OPDCMD;
554
555 /*
556 * Set the arguments and send the command
557 */
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200558 pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",
Andrew Victore0b19b82006-10-25 19:42:38 +0200559 cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(host, AT91_MCI_MR));
Andrew Victor65dbf342006-04-02 19:18:51 +0100560
561 if (!data) {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100562 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS | ATMEL_PDC_RXTDIS);
563 at91_mci_write(host, ATMEL_PDC_RPR, 0);
564 at91_mci_write(host, ATMEL_PDC_RCR, 0);
565 at91_mci_write(host, ATMEL_PDC_RNPR, 0);
566 at91_mci_write(host, ATMEL_PDC_RNCR, 0);
567 at91_mci_write(host, ATMEL_PDC_TPR, 0);
568 at91_mci_write(host, ATMEL_PDC_TCR, 0);
569 at91_mci_write(host, ATMEL_PDC_TNPR, 0);
570 at91_mci_write(host, ATMEL_PDC_TNCR, 0);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200571 ier = AT91_MCI_CMDRDY;
572 } else {
573 /* zero block length and PDC mode */
574 mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff;
Marc Pignat80f92542008-05-30 14:05:24 +0200575 mr |= (data->blksz & 0x3) ? AT91_MCI_PDCFBYTE : 0;
576 mr |= (block_length << 16);
577 mr |= AT91_MCI_PDCMODE;
578 at91_mci_write(host, AT91_MCI_MR, mr);
Andrew Victor65dbf342006-04-02 19:18:51 +0100579
Marc Pignatc5a89c62008-05-30 14:07:47 +0200580 if (!cpu_is_at91rm9200())
581 at91_mci_write(host, AT91_MCI_BLKR,
582 AT91_MCI_BLKR_BCNT(blocks) |
583 AT91_MCI_BLKR_BLKLEN(block_length));
584
Nicolas Ferreed99c542007-07-09 14:58:16 +0200585 /*
586 * Disable the PDC controller
587 */
588 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100589
Nicolas Ferreed99c542007-07-09 14:58:16 +0200590 if (cmdr & AT91_MCI_TRCMD_START) {
591 data->bytes_xfered = 0;
592 host->transfer_index = 0;
593 host->in_use_index = 0;
594 if (cmdr & AT91_MCI_TRDIR) {
595 /*
596 * Handle a read
597 */
598 host->buffer = NULL;
599 host->total_length = 0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100600
Nicolas Ferreed99c542007-07-09 14:58:16 +0200601 at91_mci_pre_dma_read(host);
602 ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
603 }
604 else {
605 /*
606 * Handle a write
607 */
608 host->total_length = block_length * blocks;
Ville Syrjala5385edc2008-06-14 20:27:20 +0300609 /*
610 * AT91SAM926[0/3] Data Write Operation and
611 * number of bytes erratum
612 */
613 if (cpu_is_at91sam9260 () || cpu_is_at91sam9263())
614 if (host->total_length < 12)
615 host->total_length = 12;
Nicolas Ferreed99c542007-07-09 14:58:16 +0200616 host->buffer = dma_alloc_coherent(NULL,
617 host->total_length,
618 &host->physical_address, GFP_KERNEL);
Andrew Victor65dbf342006-04-02 19:18:51 +0100619
Nicolas Ferreed99c542007-07-09 14:58:16 +0200620 at91_mci_sg_to_dma(host, data);
Andrew Victor65dbf342006-04-02 19:18:51 +0100621
Nicolas Ferreed99c542007-07-09 14:58:16 +0200622 pr_debug("Transmitting %d bytes\n", host->total_length);
Andrew Victor65dbf342006-04-02 19:18:51 +0100623
Nicolas Ferreed99c542007-07-09 14:58:16 +0200624 at91_mci_write(host, ATMEL_PDC_TPR, host->physical_address);
Marc Pignat80f92542008-05-30 14:05:24 +0200625 at91_mci_write(host, ATMEL_PDC_TCR, (data->blksz & 0x3) ?
626 host->total_length : host->total_length / 4);
627
Nicolas Ferreed99c542007-07-09 14:58:16 +0200628 ier = AT91_MCI_CMDRDY;
629 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100630 }
631 }
632
633 /*
634 * Send the command and then enable the PDC - not the other way round as
635 * the data sheet says
636 */
637
Andrew Victore0b19b82006-10-25 19:42:38 +0200638 at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
639 at91_mci_write(host, AT91_MCI_CMDR, cmdr);
Andrew Victor65dbf342006-04-02 19:18:51 +0100640
641 if (cmdr & AT91_MCI_TRCMD_START) {
642 if (cmdr & AT91_MCI_TRDIR)
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100643 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Andrew Victor65dbf342006-04-02 19:18:51 +0100644 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100645
Nicolas Ferreed99c542007-07-09 14:58:16 +0200646 /* Enable selected interrupts */
Andrew Victordf05a302006-10-23 14:50:09 +0200647 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_ERRORS | ier);
Andrew Victor65dbf342006-04-02 19:18:51 +0100648}
649
650/*
651 * Process the next step in the request
652 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200653static void at91_mci_process_next(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100654{
655 if (!(host->flags & FL_SENT_COMMAND)) {
656 host->flags |= FL_SENT_COMMAND;
Nicolas Ferreed99c542007-07-09 14:58:16 +0200657 at91_mci_send_command(host, host->request->cmd);
Andrew Victor65dbf342006-04-02 19:18:51 +0100658 }
659 else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) {
660 host->flags |= FL_SENT_STOP;
Nicolas Ferreed99c542007-07-09 14:58:16 +0200661 at91_mci_send_command(host, host->request->stop);
Marc Pignate181dce2008-05-30 14:06:32 +0200662 } else {
663 del_timer(&host->timer);
Marc Pignatc5a89c62008-05-30 14:07:47 +0200664 /* the at91rm9200 mci controller hangs after some transfers,
665 * and the workaround is to reset it after each transfer.
666 */
667 if (cpu_is_at91rm9200())
668 at91_reset_host(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100669 mmc_request_done(host->mmc, host->request);
Marc Pignate181dce2008-05-30 14:06:32 +0200670 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100671}
672
673/*
674 * Handle a command that has been completed
675 */
Nicolas Ferreba7deee2008-05-30 14:28:45 +0200676static void at91_mci_completed_command(struct at91mci_host *host, unsigned int status)
Andrew Victor65dbf342006-04-02 19:18:51 +0100677{
678 struct mmc_command *cmd = host->cmd;
Nicolas Ferrefa1fe012008-06-10 11:27:29 +0200679 struct mmc_data *data = cmd->data;
Andrew Victor65dbf342006-04-02 19:18:51 +0100680
Eric Benard7a6588b2008-05-30 14:26:05 +0200681 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
Andrew Victor65dbf342006-04-02 19:18:51 +0100682
Andrew Victore0b19b82006-10-25 19:42:38 +0200683 cmd->resp[0] = at91_mci_read(host, AT91_MCI_RSPR(0));
684 cmd->resp[1] = at91_mci_read(host, AT91_MCI_RSPR(1));
685 cmd->resp[2] = at91_mci_read(host, AT91_MCI_RSPR(2));
686 cmd->resp[3] = at91_mci_read(host, AT91_MCI_RSPR(3));
Andrew Victor65dbf342006-04-02 19:18:51 +0100687
688 if (host->buffer) {
689 dma_free_coherent(NULL, host->total_length, host->buffer, host->physical_address);
690 host->buffer = NULL;
691 }
692
Nicolas Ferreba7deee2008-05-30 14:28:45 +0200693 pr_debug("Status = %08X/%08x [%08X %08X %08X %08X]\n",
694 status, at91_mci_read(host, AT91_MCI_SR),
695 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
Andrew Victor65dbf342006-04-02 19:18:51 +0100696
Andrew Victor9e3866b2007-10-17 11:53:40 +0200697 if (status & AT91_MCI_ERRORS) {
Marc Pignatb6cedb32007-06-06 20:27:59 +0200698 if ((status & AT91_MCI_RCRCE) && !(mmc_resp_type(cmd) & MMC_RSP_CRC)) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200699 cmd->error = 0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100700 }
701 else {
Nicolas Ferrefa1fe012008-06-10 11:27:29 +0200702 if (status & (AT91_MCI_DTOE | AT91_MCI_DCRCE)) {
703 if (data) {
704 if (status & AT91_MCI_DTOE)
705 data->error = -ETIMEDOUT;
706 else if (status & AT91_MCI_DCRCE)
707 data->error = -EILSEQ;
708 }
709 } else {
710 if (status & AT91_MCI_RTOE)
711 cmd->error = -ETIMEDOUT;
712 else if (status & AT91_MCI_RCRCE)
713 cmd->error = -EILSEQ;
714 else
715 cmd->error = -EIO;
716 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100717
Nicolas Ferrefa1fe012008-06-10 11:27:29 +0200718 pr_debug("Error detected and set to %d/%d (cmd = %d, retries = %d)\n",
719 cmd->error, data ? data->error : 0,
720 cmd->opcode, cmd->retries);
Andrew Victor65dbf342006-04-02 19:18:51 +0100721 }
722 }
723 else
Pierre Ossman17b04292007-07-22 22:18:46 +0200724 cmd->error = 0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100725
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200726 at91_mci_process_next(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100727}
728
729/*
730 * Handle an MMC request
731 */
732static void at91_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
733{
734 struct at91mci_host *host = mmc_priv(mmc);
735 host->request = mrq;
736 host->flags = 0;
737
Marc Pignate181dce2008-05-30 14:06:32 +0200738 mod_timer(&host->timer, jiffies + HZ);
739
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200740 at91_mci_process_next(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100741}
742
743/*
744 * Set the IOS
745 */
746static void at91_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
747{
748 int clkdiv;
749 struct at91mci_host *host = mmc_priv(mmc);
Andrew Victor3dd3b032006-10-23 14:46:54 +0200750 unsigned long at91_master_clock = clk_get_rate(host->mci_clk);
Andrew Victor65dbf342006-04-02 19:18:51 +0100751
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100752 host->bus_mode = ios->bus_mode;
Andrew Victor65dbf342006-04-02 19:18:51 +0100753
754 if (ios->clock == 0) {
755 /* Disable the MCI controller */
Andrew Victore0b19b82006-10-25 19:42:38 +0200756 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100757 clkdiv = 0;
758 }
759 else {
760 /* Enable the MCI controller */
Andrew Victore0b19b82006-10-25 19:42:38 +0200761 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
Andrew Victor65dbf342006-04-02 19:18:51 +0100762
763 if ((at91_master_clock % (ios->clock * 2)) == 0)
764 clkdiv = ((at91_master_clock / ios->clock) / 2) - 1;
765 else
766 clkdiv = (at91_master_clock / ios->clock) / 2;
767
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100768 pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv,
Andrew Victor65dbf342006-04-02 19:18:51 +0100769 at91_master_clock / (2 * (clkdiv + 1)));
770 }
771 if (ios->bus_width == MMC_BUS_WIDTH_4 && host->board->wire4) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100772 pr_debug("MMC: Setting controller bus width to 4\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200773 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) | AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100774 }
775 else {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100776 pr_debug("MMC: Setting controller bus width to 1\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200777 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100778 }
779
780 /* Set the clock divider */
Andrew Victore0b19b82006-10-25 19:42:38 +0200781 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 +0100782
783 /* maybe switch power to the card */
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100784 if (host->board->vcc_pin) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100785 switch (ios->power_mode) {
786 case MMC_POWER_OFF:
David Brownell6e996ee2008-02-04 18:12:48 +0100787 gpio_set_value(host->board->vcc_pin, 0);
Andrew Victor65dbf342006-04-02 19:18:51 +0100788 break;
789 case MMC_POWER_UP:
David Brownell6e996ee2008-02-04 18:12:48 +0100790 gpio_set_value(host->board->vcc_pin, 1);
Andrew Victor65dbf342006-04-02 19:18:51 +0100791 break;
Marc Pignate5c0ef92008-05-09 11:07:07 +0200792 case MMC_POWER_ON:
793 break;
794 default:
795 WARN_ON(1);
Andrew Victor65dbf342006-04-02 19:18:51 +0100796 }
797 }
798}
799
800/*
801 * Handle an interrupt
802 */
David Howells7d12e782006-10-05 14:55:46 +0100803static irqreturn_t at91_mci_irq(int irq, void *devid)
Andrew Victor65dbf342006-04-02 19:18:51 +0100804{
805 struct at91mci_host *host = devid;
806 int completed = 0;
Andrew Victordf05a302006-10-23 14:50:09 +0200807 unsigned int int_status, int_mask;
Andrew Victor65dbf342006-04-02 19:18:51 +0100808
Andrew Victore0b19b82006-10-25 19:42:38 +0200809 int_status = at91_mci_read(host, AT91_MCI_SR);
Andrew Victordf05a302006-10-23 14:50:09 +0200810 int_mask = at91_mci_read(host, AT91_MCI_IMR);
Nicolas Ferre37b758e82007-08-08 12:01:44 +0200811
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200812 pr_debug("MCI irq: status = %08X, %08X, %08X\n", int_status, int_mask,
Andrew Victordf05a302006-10-23 14:50:09 +0200813 int_status & int_mask);
Nicolas Ferre37b758e82007-08-08 12:01:44 +0200814
Andrew Victordf05a302006-10-23 14:50:09 +0200815 int_status = int_status & int_mask;
Andrew Victor65dbf342006-04-02 19:18:51 +0100816
Andrew Victordf05a302006-10-23 14:50:09 +0200817 if (int_status & AT91_MCI_ERRORS) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100818 completed = 1;
Nicolas Ferre37b758e82007-08-08 12:01:44 +0200819
Andrew Victordf05a302006-10-23 14:50:09 +0200820 if (int_status & AT91_MCI_UNRE)
821 pr_debug("MMC: Underrun error\n");
822 if (int_status & AT91_MCI_OVRE)
823 pr_debug("MMC: Overrun error\n");
824 if (int_status & AT91_MCI_DTOE)
825 pr_debug("MMC: Data timeout\n");
826 if (int_status & AT91_MCI_DCRCE)
827 pr_debug("MMC: CRC error in data\n");
828 if (int_status & AT91_MCI_RTOE)
829 pr_debug("MMC: Response timeout\n");
830 if (int_status & AT91_MCI_RENDE)
831 pr_debug("MMC: Response end bit error\n");
832 if (int_status & AT91_MCI_RCRCE)
833 pr_debug("MMC: Response CRC error\n");
834 if (int_status & AT91_MCI_RDIRE)
835 pr_debug("MMC: Response direction error\n");
836 if (int_status & AT91_MCI_RINDE)
837 pr_debug("MMC: Response index error\n");
838 } else {
839 /* Only continue processing if no errors */
Andrew Victor65dbf342006-04-02 19:18:51 +0100840
Andrew Victor65dbf342006-04-02 19:18:51 +0100841 if (int_status & AT91_MCI_TXBUFE) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100842 pr_debug("TX buffer empty\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100843 at91_mci_handle_transmitted(host);
844 }
845
Nicolas Ferreed99c542007-07-09 14:58:16 +0200846 if (int_status & AT91_MCI_ENDRX) {
847 pr_debug("ENDRX\n");
848 at91_mci_post_dma_read(host);
849 }
850
Andrew Victor65dbf342006-04-02 19:18:51 +0100851 if (int_status & AT91_MCI_RXBUFF) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100852 pr_debug("RX buffer full\n");
Nicolas Ferreed99c542007-07-09 14:58:16 +0200853 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
854 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_RXBUFF | AT91_MCI_ENDRX);
855 completed = 1;
Andrew Victor65dbf342006-04-02 19:18:51 +0100856 }
857
Andrew Victordf05a302006-10-23 14:50:09 +0200858 if (int_status & AT91_MCI_ENDTX)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100859 pr_debug("Transmit has ended\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100860
Andrew Victor65dbf342006-04-02 19:18:51 +0100861 if (int_status & AT91_MCI_NOTBUSY) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100862 pr_debug("Card is ready\n");
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200863 at91_mci_update_bytes_xfered(host);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200864 completed = 1;
Andrew Victor65dbf342006-04-02 19:18:51 +0100865 }
866
Andrew Victordf05a302006-10-23 14:50:09 +0200867 if (int_status & AT91_MCI_DTIP)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100868 pr_debug("Data transfer in progress\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100869
Nicolas Ferreed99c542007-07-09 14:58:16 +0200870 if (int_status & AT91_MCI_BLKE) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100871 pr_debug("Block transfer has ended\n");
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200872 if (host->request->data && host->request->data->blocks > 1) {
873 /* multi block write : complete multi write
874 * command and send stop */
875 completed = 1;
876 } else {
877 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
878 }
Nicolas Ferreed99c542007-07-09 14:58:16 +0200879 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100880
Eric Benard7a6588b2008-05-30 14:26:05 +0200881 if (int_status & AT91_MCI_SDIOIRQA)
882 mmc_signal_sdio_irq(host->mmc);
883
884 if (int_status & AT91_MCI_SDIOIRQB)
885 mmc_signal_sdio_irq(host->mmc);
886
Andrew Victordf05a302006-10-23 14:50:09 +0200887 if (int_status & AT91_MCI_TXRDY)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100888 pr_debug("Ready to transmit\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100889
Andrew Victordf05a302006-10-23 14:50:09 +0200890 if (int_status & AT91_MCI_RXRDY)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100891 pr_debug("Ready to receive\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100892
893 if (int_status & AT91_MCI_CMDRDY) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100894 pr_debug("Command ready\n");
Nicolas Ferreed99c542007-07-09 14:58:16 +0200895 completed = at91_mci_handle_cmdrdy(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100896 }
897 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100898
899 if (completed) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100900 pr_debug("Completed command\n");
Eric Benard7a6588b2008-05-30 14:26:05 +0200901 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
Nicolas Ferreba7deee2008-05-30 14:28:45 +0200902 at91_mci_completed_command(host, int_status);
Andrew Victordf05a302006-10-23 14:50:09 +0200903 } else
Eric Benard7a6588b2008-05-30 14:26:05 +0200904 at91_mci_write(host, AT91_MCI_IDR, int_status & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
Andrew Victor65dbf342006-04-02 19:18:51 +0100905
906 return IRQ_HANDLED;
907}
908
David Howells7d12e782006-10-05 14:55:46 +0100909static irqreturn_t at91_mmc_det_irq(int irq, void *_host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100910{
911 struct at91mci_host *host = _host;
David Brownell6e996ee2008-02-04 18:12:48 +0100912 int present = !gpio_get_value(irq_to_gpio(irq));
Andrew Victor65dbf342006-04-02 19:18:51 +0100913
914 /*
915 * we expect this irq on both insert and remove,
916 * and use a short delay to debounce.
917 */
918 if (present != host->present) {
919 host->present = present;
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100920 pr_debug("%s: card %s\n", mmc_hostname(host->mmc),
Andrew Victor65dbf342006-04-02 19:18:51 +0100921 present ? "insert" : "remove");
922 if (!present) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100923 pr_debug("****** Resetting SD-card bus width ******\n");
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100924 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100925 }
926 mmc_detect_change(host->mmc, msecs_to_jiffies(100));
927 }
928 return IRQ_HANDLED;
929}
930
David Brownella26b4982006-12-26 14:45:26 -0800931static int at91_mci_get_ro(struct mmc_host *mmc)
Andrew Victor65dbf342006-04-02 19:18:51 +0100932{
Andrew Victor65dbf342006-04-02 19:18:51 +0100933 struct at91mci_host *host = mmc_priv(mmc);
934
Anton Vorontsov08f80bb2008-06-17 18:17:39 +0400935 if (host->board->wp_pin)
936 return !!gpio_get_value(host->board->wp_pin);
937 /*
938 * Board doesn't support read only detection; let the mmc core
939 * decide what to do.
940 */
941 return -ENOSYS;
Andrew Victor65dbf342006-04-02 19:18:51 +0100942}
943
Eric Benard7a6588b2008-05-30 14:26:05 +0200944static void at91_mci_enable_sdio_irq(struct mmc_host *mmc, int enable)
945{
946 struct at91mci_host *host = mmc_priv(mmc);
947
948 pr_debug("%s: sdio_irq %c : %s\n", mmc_hostname(host->mmc),
949 host->board->slot_b ? 'B':'A', enable ? "enable" : "disable");
950 at91_mci_write(host, enable ? AT91_MCI_IER : AT91_MCI_IDR,
951 host->board->slot_b ? AT91_MCI_SDIOIRQB : AT91_MCI_SDIOIRQA);
952
953}
954
David Brownellab7aefd2006-11-12 17:55:30 -0800955static const struct mmc_host_ops at91_mci_ops = {
Andrew Victor65dbf342006-04-02 19:18:51 +0100956 .request = at91_mci_request,
957 .set_ios = at91_mci_set_ios,
958 .get_ro = at91_mci_get_ro,
Eric Benard7a6588b2008-05-30 14:26:05 +0200959 .enable_sdio_irq = at91_mci_enable_sdio_irq,
Andrew Victor65dbf342006-04-02 19:18:51 +0100960};
961
962/*
963 * Probe for the device
964 */
David Brownella26b4982006-12-26 14:45:26 -0800965static int __init at91_mci_probe(struct platform_device *pdev)
Andrew Victor65dbf342006-04-02 19:18:51 +0100966{
967 struct mmc_host *mmc;
968 struct at91mci_host *host;
Andrew Victor17ea0592006-10-23 14:44:40 +0200969 struct resource *res;
Andrew Victor65dbf342006-04-02 19:18:51 +0100970 int ret;
971
Andrew Victor17ea0592006-10-23 14:44:40 +0200972 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
973 if (!res)
974 return -ENXIO;
975
976 if (!request_mem_region(res->start, res->end - res->start + 1, DRIVER_NAME))
977 return -EBUSY;
978
Andrew Victor65dbf342006-04-02 19:18:51 +0100979 mmc = mmc_alloc_host(sizeof(struct at91mci_host), &pdev->dev);
980 if (!mmc) {
David Brownell6e996ee2008-02-04 18:12:48 +0100981 ret = -ENOMEM;
982 dev_dbg(&pdev->dev, "couldn't allocate mmc host\n");
983 goto fail6;
Andrew Victor65dbf342006-04-02 19:18:51 +0100984 }
985
986 mmc->ops = &at91_mci_ops;
987 mmc->f_min = 375000;
988 mmc->f_max = 25000000;
989 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Eric Benard7a6588b2008-05-30 14:26:05 +0200990 mmc->caps = MMC_CAP_MULTIWRITE | MMC_CAP_SDIO_IRQ;
Andrew Victor65dbf342006-04-02 19:18:51 +0100991
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100992 mmc->max_blk_size = 4095;
Pierre Ossman55db8902006-11-21 17:55:45 +0100993 mmc->max_blk_count = mmc->max_req_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100994
Andrew Victor65dbf342006-04-02 19:18:51 +0100995 host = mmc_priv(mmc);
996 host->mmc = mmc;
997 host->buffer = NULL;
998 host->bus_mode = 0;
999 host->board = pdev->dev.platform_data;
1000 if (host->board->wire4) {
Nicolas Ferreed99c542007-07-09 14:58:16 +02001001 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
1002 mmc->caps |= MMC_CAP_4_BIT_DATA;
1003 else
David Brownell6e996ee2008-02-04 18:12:48 +01001004 dev_warn(&pdev->dev, "4 wire bus mode not supported"
Nicolas Ferreed99c542007-07-09 14:58:16 +02001005 " - using 1 wire\n");
Andrew Victor65dbf342006-04-02 19:18:51 +01001006 }
1007
1008 /*
David Brownell6e996ee2008-02-04 18:12:48 +01001009 * Reserve GPIOs ... board init code makes sure these pins are set
1010 * up as GPIOs with the right direction (input, except for vcc)
1011 */
1012 if (host->board->det_pin) {
1013 ret = gpio_request(host->board->det_pin, "mmc_detect");
1014 if (ret < 0) {
1015 dev_dbg(&pdev->dev, "couldn't claim card detect pin\n");
1016 goto fail5;
1017 }
1018 }
1019 if (host->board->wp_pin) {
1020 ret = gpio_request(host->board->wp_pin, "mmc_wp");
1021 if (ret < 0) {
1022 dev_dbg(&pdev->dev, "couldn't claim wp sense pin\n");
1023 goto fail4;
1024 }
1025 }
1026 if (host->board->vcc_pin) {
1027 ret = gpio_request(host->board->vcc_pin, "mmc_vcc");
1028 if (ret < 0) {
1029 dev_dbg(&pdev->dev, "couldn't claim vcc switch pin\n");
1030 goto fail3;
1031 }
1032 }
1033
1034 /*
Andrew Victor65dbf342006-04-02 19:18:51 +01001035 * Get Clock
1036 */
Andrew Victor3dd3b032006-10-23 14:46:54 +02001037 host->mci_clk = clk_get(&pdev->dev, "mci_clk");
1038 if (IS_ERR(host->mci_clk)) {
David Brownell6e996ee2008-02-04 18:12:48 +01001039 ret = -ENODEV;
1040 dev_dbg(&pdev->dev, "no mci_clk?\n");
1041 goto fail2;
Andrew Victor65dbf342006-04-02 19:18:51 +01001042 }
Andrew Victor65dbf342006-04-02 19:18:51 +01001043
Andrew Victor17ea0592006-10-23 14:44:40 +02001044 /*
1045 * Map I/O region
1046 */
1047 host->baseaddr = ioremap(res->start, res->end - res->start + 1);
1048 if (!host->baseaddr) {
David Brownell6e996ee2008-02-04 18:12:48 +01001049 ret = -ENOMEM;
1050 goto fail1;
Andrew Victor17ea0592006-10-23 14:44:40 +02001051 }
Andrew Victore0b19b82006-10-25 19:42:38 +02001052
1053 /*
1054 * Reset hardware
1055 */
Andrew Victor3dd3b032006-10-23 14:46:54 +02001056 clk_enable(host->mci_clk); /* Enable the peripheral clock */
Andrew Victore0b19b82006-10-25 19:42:38 +02001057 at91_mci_disable(host);
1058 at91_mci_enable(host);
1059
Andrew Victor65dbf342006-04-02 19:18:51 +01001060 /*
1061 * Allocate the MCI interrupt
1062 */
Andrew Victor17ea0592006-10-23 14:44:40 +02001063 host->irq = platform_get_irq(pdev, 0);
David Brownell6e996ee2008-02-04 18:12:48 +01001064 ret = request_irq(host->irq, at91_mci_irq, IRQF_SHARED,
1065 mmc_hostname(mmc), host);
Andrew Victor65dbf342006-04-02 19:18:51 +01001066 if (ret) {
David Brownell6e996ee2008-02-04 18:12:48 +01001067 dev_dbg(&pdev->dev, "request MCI interrupt failed\n");
1068 goto fail0;
Andrew Victor65dbf342006-04-02 19:18:51 +01001069 }
1070
1071 platform_set_drvdata(pdev, mmc);
1072
1073 /*
1074 * Add host to MMC layer
1075 */
Marc Pignat63b66432007-07-16 11:07:02 +02001076 if (host->board->det_pin) {
David Brownell6e996ee2008-02-04 18:12:48 +01001077 host->present = !gpio_get_value(host->board->det_pin);
Marc Pignat63b66432007-07-16 11:07:02 +02001078 }
Andrew Victor65dbf342006-04-02 19:18:51 +01001079 else
1080 host->present = -1;
1081
1082 mmc_add_host(mmc);
1083
Marc Pignate181dce2008-05-30 14:06:32 +02001084 setup_timer(&host->timer, at91_timeout_timer, (unsigned long)host);
1085
Andrew Victor65dbf342006-04-02 19:18:51 +01001086 /*
1087 * monitor card insertion/removal if we can
1088 */
1089 if (host->board->det_pin) {
David Brownell6e996ee2008-02-04 18:12:48 +01001090 ret = request_irq(gpio_to_irq(host->board->det_pin),
1091 at91_mmc_det_irq, 0, mmc_hostname(mmc), host);
Andrew Victor65dbf342006-04-02 19:18:51 +01001092 if (ret)
David Brownell6e996ee2008-02-04 18:12:48 +01001093 dev_warn(&pdev->dev, "request MMC detect irq failed\n");
1094 else
1095 device_init_wakeup(&pdev->dev, 1);
Andrew Victor65dbf342006-04-02 19:18:51 +01001096 }
1097
Andrew Victorf3a8efa2006-10-23 14:53:20 +02001098 pr_debug("Added MCI driver\n");
Andrew Victor65dbf342006-04-02 19:18:51 +01001099
1100 return 0;
David Brownell6e996ee2008-02-04 18:12:48 +01001101
1102fail0:
1103 clk_disable(host->mci_clk);
1104 iounmap(host->baseaddr);
1105fail1:
1106 clk_put(host->mci_clk);
1107fail2:
1108 if (host->board->vcc_pin)
1109 gpio_free(host->board->vcc_pin);
1110fail3:
1111 if (host->board->wp_pin)
1112 gpio_free(host->board->wp_pin);
1113fail4:
1114 if (host->board->det_pin)
1115 gpio_free(host->board->det_pin);
1116fail5:
1117 mmc_free_host(mmc);
1118fail6:
1119 release_mem_region(res->start, res->end - res->start + 1);
1120 dev_err(&pdev->dev, "probe failed, err %d\n", ret);
1121 return ret;
Andrew Victor65dbf342006-04-02 19:18:51 +01001122}
1123
1124/*
1125 * Remove a device
1126 */
David Brownella26b4982006-12-26 14:45:26 -08001127static int __exit at91_mci_remove(struct platform_device *pdev)
Andrew Victor65dbf342006-04-02 19:18:51 +01001128{
1129 struct mmc_host *mmc = platform_get_drvdata(pdev);
1130 struct at91mci_host *host;
Andrew Victor17ea0592006-10-23 14:44:40 +02001131 struct resource *res;
Andrew Victor65dbf342006-04-02 19:18:51 +01001132
1133 if (!mmc)
1134 return -1;
1135
1136 host = mmc_priv(mmc);
1137
Anti Sulline0cda542007-08-30 16:15:16 +02001138 if (host->board->det_pin) {
David Brownell6e996ee2008-02-04 18:12:48 +01001139 if (device_can_wakeup(&pdev->dev))
1140 free_irq(gpio_to_irq(host->board->det_pin), host);
Marc Pignat63b66432007-07-16 11:07:02 +02001141 device_init_wakeup(&pdev->dev, 0);
David Brownell6e996ee2008-02-04 18:12:48 +01001142 gpio_free(host->board->det_pin);
Andrew Victor65dbf342006-04-02 19:18:51 +01001143 }
1144
Andrew Victore0b19b82006-10-25 19:42:38 +02001145 at91_mci_disable(host);
Marc Pignate181dce2008-05-30 14:06:32 +02001146 del_timer_sync(&host->timer);
Andrew Victor17ea0592006-10-23 14:44:40 +02001147 mmc_remove_host(mmc);
1148 free_irq(host->irq, host);
Andrew Victor65dbf342006-04-02 19:18:51 +01001149
Andrew Victor3dd3b032006-10-23 14:46:54 +02001150 clk_disable(host->mci_clk); /* Disable the peripheral clock */
1151 clk_put(host->mci_clk);
Andrew Victor65dbf342006-04-02 19:18:51 +01001152
David Brownell6e996ee2008-02-04 18:12:48 +01001153 if (host->board->vcc_pin)
1154 gpio_free(host->board->vcc_pin);
1155 if (host->board->wp_pin)
1156 gpio_free(host->board->wp_pin);
1157
Andrew Victor17ea0592006-10-23 14:44:40 +02001158 iounmap(host->baseaddr);
1159 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1160 release_mem_region(res->start, res->end - res->start + 1);
Andrew Victor65dbf342006-04-02 19:18:51 +01001161
Andrew Victor17ea0592006-10-23 14:44:40 +02001162 mmc_free_host(mmc);
1163 platform_set_drvdata(pdev, NULL);
Andrew Victorb44fb7a2006-06-19 13:06:05 +01001164 pr_debug("MCI Removed\n");
Andrew Victor65dbf342006-04-02 19:18:51 +01001165
1166 return 0;
1167}
1168
1169#ifdef CONFIG_PM
1170static int at91_mci_suspend(struct platform_device *pdev, pm_message_t state)
1171{
1172 struct mmc_host *mmc = platform_get_drvdata(pdev);
Marc Pignat63b66432007-07-16 11:07:02 +02001173 struct at91mci_host *host = mmc_priv(mmc);
Andrew Victor65dbf342006-04-02 19:18:51 +01001174 int ret = 0;
1175
Anti Sulline0cda542007-08-30 16:15:16 +02001176 if (host->board->det_pin && device_may_wakeup(&pdev->dev))
Marc Pignat63b66432007-07-16 11:07:02 +02001177 enable_irq_wake(host->board->det_pin);
1178
Andrew Victor65dbf342006-04-02 19:18:51 +01001179 if (mmc)
1180 ret = mmc_suspend_host(mmc, state);
1181
1182 return ret;
1183}
1184
1185static int at91_mci_resume(struct platform_device *pdev)
1186{
1187 struct mmc_host *mmc = platform_get_drvdata(pdev);
Marc Pignat63b66432007-07-16 11:07:02 +02001188 struct at91mci_host *host = mmc_priv(mmc);
Andrew Victor65dbf342006-04-02 19:18:51 +01001189 int ret = 0;
1190
Anti Sulline0cda542007-08-30 16:15:16 +02001191 if (host->board->det_pin && device_may_wakeup(&pdev->dev))
Marc Pignat63b66432007-07-16 11:07:02 +02001192 disable_irq_wake(host->board->det_pin);
1193
Andrew Victor65dbf342006-04-02 19:18:51 +01001194 if (mmc)
1195 ret = mmc_resume_host(mmc);
1196
1197 return ret;
1198}
1199#else
1200#define at91_mci_suspend NULL
1201#define at91_mci_resume NULL
1202#endif
1203
1204static struct platform_driver at91_mci_driver = {
David Brownella26b4982006-12-26 14:45:26 -08001205 .remove = __exit_p(at91_mci_remove),
Andrew Victor65dbf342006-04-02 19:18:51 +01001206 .suspend = at91_mci_suspend,
1207 .resume = at91_mci_resume,
1208 .driver = {
1209 .name = DRIVER_NAME,
1210 .owner = THIS_MODULE,
1211 },
1212};
1213
1214static int __init at91_mci_init(void)
1215{
David Brownella26b4982006-12-26 14:45:26 -08001216 return platform_driver_probe(&at91_mci_driver, at91_mci_probe);
Andrew Victor65dbf342006-04-02 19:18:51 +01001217}
1218
1219static void __exit at91_mci_exit(void)
1220{
1221 platform_driver_unregister(&at91_mci_driver);
1222}
1223
1224module_init(at91_mci_init);
1225module_exit(at91_mci_exit);
1226
1227MODULE_DESCRIPTION("AT91 Multimedia Card Interface driver");
1228MODULE_AUTHOR("Nick Randell");
1229MODULE_LICENSE("GPL");
Kay Sieversbc65c722008-04-15 14:34:28 -07001230MODULE_ALIAS("platform:at91_mci");