blob: b5a6e250fc1a946c83fdd3ad3901c73c8a04ab56 [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 Ferre37b758e2007-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
201 size = host->total_length;
202 len = data->sg_len;
203
204 /*
205 * Just loop through all entries. Size might not
206 * be the entire list though so make sure that
207 * we do not transfer too much.
208 */
209 for (i = 0; i < len; i++) {
210 struct scatterlist *sg;
211 int amount;
Andrew Victor65dbf342006-04-02 19:18:51 +0100212 unsigned int *sgbuffer;
213
214 sg = &data->sg[i];
215
Jens Axboe45711f12007-10-22 21:19:53 +0200216 sgbuffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
Andrew Victor65dbf342006-04-02 19:18:51 +0100217 amount = min(size, sg->length);
218 size -= amount;
Andrew Victor65dbf342006-04-02 19:18:51 +0100219
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100220 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
221 int index;
222
223 for (index = 0; index < (amount / 4); index++)
224 *dmabuf++ = swab32(sgbuffer[index]);
225 }
226 else
227 memcpy(dmabuf, sgbuffer, amount);
Andrew Victor65dbf342006-04-02 19:18:51 +0100228
229 kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
230
231 if (size == 0)
232 break;
233 }
234
235 /*
236 * Check that we didn't get a request to transfer
237 * more data than can fit into the SG list.
238 */
239 BUG_ON(size != 0);
240}
241
242/*
243 * Prepare a dma read
244 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200245static void at91_mci_pre_dma_read(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100246{
247 int i;
248 struct scatterlist *sg;
249 struct mmc_command *cmd;
250 struct mmc_data *data;
251
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100252 pr_debug("pre dma read\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100253
254 cmd = host->cmd;
255 if (!cmd) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100256 pr_debug("no command\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100257 return;
258 }
259
260 data = cmd->data;
261 if (!data) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100262 pr_debug("no data\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100263 return;
264 }
265
266 for (i = 0; i < 2; i++) {
267 /* nothing left to transfer */
268 if (host->transfer_index >= data->sg_len) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100269 pr_debug("Nothing left to transfer (index = %d)\n", host->transfer_index);
Andrew Victor65dbf342006-04-02 19:18:51 +0100270 break;
271 }
272
273 /* Check to see if this needs filling */
274 if (i == 0) {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100275 if (at91_mci_read(host, ATMEL_PDC_RCR) != 0) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100276 pr_debug("Transfer active in current\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100277 continue;
278 }
279 }
280 else {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100281 if (at91_mci_read(host, ATMEL_PDC_RNCR) != 0) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100282 pr_debug("Transfer active in next\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100283 continue;
284 }
285 }
286
287 /* Setup the next transfer */
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100288 pr_debug("Using transfer index %d\n", host->transfer_index);
Andrew Victor65dbf342006-04-02 19:18:51 +0100289
290 sg = &data->sg[host->transfer_index++];
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100291 pr_debug("sg = %p\n", sg);
Andrew Victor65dbf342006-04-02 19:18:51 +0100292
Jens Axboe45711f12007-10-22 21:19:53 +0200293 sg->dma_address = dma_map_page(NULL, sg_page(sg), sg->offset, sg->length, DMA_FROM_DEVICE);
Andrew Victor65dbf342006-04-02 19:18:51 +0100294
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100295 pr_debug("dma address = %08X, length = %d\n", sg->dma_address, sg->length);
Andrew Victor65dbf342006-04-02 19:18:51 +0100296
297 if (i == 0) {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100298 at91_mci_write(host, ATMEL_PDC_RPR, sg->dma_address);
Marc Pignat80f92542008-05-30 14:05:24 +0200299 at91_mci_write(host, ATMEL_PDC_RCR, (data->blksz & 0x3) ? sg->length : sg->length / 4);
Andrew Victor65dbf342006-04-02 19:18:51 +0100300 }
301 else {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100302 at91_mci_write(host, ATMEL_PDC_RNPR, sg->dma_address);
Marc Pignat80f92542008-05-30 14:05:24 +0200303 at91_mci_write(host, ATMEL_PDC_RNCR, (data->blksz & 0x3) ? sg->length : sg->length / 4);
Andrew Victor65dbf342006-04-02 19:18:51 +0100304 }
305 }
306
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100307 pr_debug("pre dma read done\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100308}
309
310/*
311 * Handle after a dma read
312 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200313static void at91_mci_post_dma_read(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100314{
315 struct mmc_command *cmd;
316 struct mmc_data *data;
317
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100318 pr_debug("post dma read\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100319
320 cmd = host->cmd;
321 if (!cmd) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100322 pr_debug("no command\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100323 return;
324 }
325
326 data = cmd->data;
327 if (!data) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100328 pr_debug("no data\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100329 return;
330 }
331
332 while (host->in_use_index < host->transfer_index) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100333 struct scatterlist *sg;
334
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100335 pr_debug("finishing index %d\n", host->in_use_index);
Andrew Victor65dbf342006-04-02 19:18:51 +0100336
337 sg = &data->sg[host->in_use_index++];
338
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100339 pr_debug("Unmapping page %08X\n", sg->dma_address);
Andrew Victor65dbf342006-04-02 19:18:51 +0100340
341 dma_unmap_page(NULL, sg->dma_address, sg->length, DMA_FROM_DEVICE);
342
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100343 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
Nicolas Ferreed99c542007-07-09 14:58:16 +0200344 unsigned int *buffer;
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100345 int index;
Andrew Victor65dbf342006-04-02 19:18:51 +0100346
Nicolas Ferreed99c542007-07-09 14:58:16 +0200347 /* Swap the contents of the buffer */
Jens Axboe45711f12007-10-22 21:19:53 +0200348 buffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
Nicolas Ferreed99c542007-07-09 14:58:16 +0200349 pr_debug("buffer = %p, length = %d\n", buffer, sg->length);
350
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100351 for (index = 0; index < (sg->length / 4); index++)
352 buffer[index] = swab32(buffer[index]);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200353
354 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
Andrew Victor65dbf342006-04-02 19:18:51 +0100355 }
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100356
Jens Axboe45711f12007-10-22 21:19:53 +0200357 flush_dcache_page(sg_page(sg));
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200358
359 data->bytes_xfered += sg->length;
Andrew Victor65dbf342006-04-02 19:18:51 +0100360 }
361
362 /* Is there another transfer to trigger? */
363 if (host->transfer_index < data->sg_len)
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200364 at91_mci_pre_dma_read(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100365 else {
Nicolas Ferreed99c542007-07-09 14:58:16 +0200366 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_ENDRX);
Andrew Victore0b19b82006-10-25 19:42:38 +0200367 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_RXBUFF);
Andrew Victor65dbf342006-04-02 19:18:51 +0100368 }
369
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100370 pr_debug("post dma read done\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100371}
372
373/*
374 * Handle transmitted data
375 */
376static void at91_mci_handle_transmitted(struct at91mci_host *host)
377{
378 struct mmc_command *cmd;
379 struct mmc_data *data;
380
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100381 pr_debug("Handling the transmit\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100382
383 /* Disable the transfer */
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100384 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100385
386 /* Now wait for cmd ready */
Andrew Victore0b19b82006-10-25 19:42:38 +0200387 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE);
Andrew Victor65dbf342006-04-02 19:18:51 +0100388
389 cmd = host->cmd;
390 if (!cmd) return;
391
392 data = cmd->data;
393 if (!data) return;
394
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200395 if (cmd->data->blocks > 1) {
Nicolas Ferreed99c542007-07-09 14:58:16 +0200396 pr_debug("multiple write : wait for BLKE...\n");
397 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
398 } else
399 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
Andrew Victor65dbf342006-04-02 19:18:51 +0100400}
401
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200402/*
403 * Update bytes tranfered count during a write operation
404 */
405static void at91_mci_update_bytes_xfered(struct at91mci_host *host)
406{
407 struct mmc_data *data;
408
409 /* always deal with the effective request (and not the current cmd) */
410
411 if (host->request->cmd && host->request->cmd->error != 0)
412 return;
413
414 if (host->request->data) {
415 data = host->request->data;
416 if (data->flags & MMC_DATA_WRITE) {
417 /* card is in IDLE mode now */
418 pr_debug("-> bytes_xfered %d, total_length = %d\n",
419 data->bytes_xfered, host->total_length);
420 data->bytes_xfered = host->total_length;
421 }
422 }
423}
424
425
Nicolas Ferreed99c542007-07-09 14:58:16 +0200426/*Handle after command sent ready*/
427static int at91_mci_handle_cmdrdy(struct at91mci_host *host)
428{
429 if (!host->cmd)
430 return 1;
431 else if (!host->cmd->data) {
432 if (host->flags & FL_SENT_STOP) {
433 /*After multi block write, we must wait for NOTBUSY*/
434 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
435 } else return 1;
436 } else if (host->cmd->data->flags & MMC_DATA_WRITE) {
437 /*After sendding multi-block-write command, start DMA transfer*/
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200438 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_TXBUFE | AT91_MCI_BLKE);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200439 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
440 }
441
442 /* command not completed, have to wait */
443 return 0;
444}
445
446
Andrew Victor65dbf342006-04-02 19:18:51 +0100447/*
448 * Enable the controller
449 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200450static void at91_mci_enable(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100451{
Nicolas Ferreed99c542007-07-09 14:58:16 +0200452 unsigned int mr;
453
Andrew Victore0b19b82006-10-25 19:42:38 +0200454 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200455 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
Andrew Victore0b19b82006-10-25 19:42:38 +0200456 at91_mci_write(host, AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200457 mr = AT91_MCI_PDCMODE | 0x34a;
458
459 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
460 mr |= AT91_MCI_RDPROOF | AT91_MCI_WRPROOF;
461
462 at91_mci_write(host, AT91_MCI_MR, mr);
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100463
464 /* use Slot A or B (only one at same time) */
465 at91_mci_write(host, AT91_MCI_SDCR, host->board->slot_b);
Andrew Victor65dbf342006-04-02 19:18:51 +0100466}
467
468/*
469 * Disable the controller
470 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200471static void at91_mci_disable(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100472{
Andrew Victore0b19b82006-10-25 19:42:38 +0200473 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
Andrew Victor65dbf342006-04-02 19:18:51 +0100474}
475
476/*
477 * Send a command
Andrew Victor65dbf342006-04-02 19:18:51 +0100478 */
Nicolas Ferreed99c542007-07-09 14:58:16 +0200479static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd)
Andrew Victor65dbf342006-04-02 19:18:51 +0100480{
481 unsigned int cmdr, mr;
482 unsigned int block_length;
483 struct mmc_data *data = cmd->data;
484
485 unsigned int blocks;
486 unsigned int ier = 0;
487
488 host->cmd = cmd;
489
Nicolas Ferreed99c542007-07-09 14:58:16 +0200490 /* Needed for leaving busy state before CMD1 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200491 if ((at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100492 pr_debug("Clearing timeout\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200493 at91_mci_write(host, AT91_MCI_ARGR, 0);
494 at91_mci_write(host, AT91_MCI_CMDR, AT91_MCI_OPDCMD);
495 while (!(at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_CMDRDY)) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100496 /* spin */
Andrew Victore0b19b82006-10-25 19:42:38 +0200497 pr_debug("Clearing: SR = %08X\n", at91_mci_read(host, AT91_MCI_SR));
Andrew Victor65dbf342006-04-02 19:18:51 +0100498 }
499 }
Nicolas Ferreed99c542007-07-09 14:58:16 +0200500
Andrew Victor65dbf342006-04-02 19:18:51 +0100501 cmdr = cmd->opcode;
502
503 if (mmc_resp_type(cmd) == MMC_RSP_NONE)
504 cmdr |= AT91_MCI_RSPTYP_NONE;
505 else {
506 /* if a response is expected then allow maximum response latancy */
507 cmdr |= AT91_MCI_MAXLAT;
508 /* set 136 bit response for R2, 48 bit response otherwise */
509 if (mmc_resp_type(cmd) == MMC_RSP_R2)
510 cmdr |= AT91_MCI_RSPTYP_136;
511 else
512 cmdr |= AT91_MCI_RSPTYP_48;
513 }
514
515 if (data) {
Marc Pignat1d4de9e2007-08-09 13:56:29 +0200516
Marc Pignat80f92542008-05-30 14:05:24 +0200517 if ( cpu_is_at91rm9200() && (data->blksz & 0x3) ) {
Marc Pignat1d4de9e2007-08-09 13:56:29 +0200518 pr_debug("Unsupported block size\n");
519 cmd->error = -EINVAL;
520 mmc_request_done(host->mmc, host->request);
521 return;
522 }
523
Russell Kinga3fd4a12006-06-04 17:51:15 +0100524 block_length = data->blksz;
Andrew Victor65dbf342006-04-02 19:18:51 +0100525 blocks = data->blocks;
526
527 /* always set data start - also set direction flag for read */
528 if (data->flags & MMC_DATA_READ)
529 cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START);
530 else if (data->flags & MMC_DATA_WRITE)
531 cmdr |= AT91_MCI_TRCMD_START;
532
533 if (data->flags & MMC_DATA_STREAM)
534 cmdr |= AT91_MCI_TRTYP_STREAM;
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200535 if (data->blocks > 1)
Andrew Victor65dbf342006-04-02 19:18:51 +0100536 cmdr |= AT91_MCI_TRTYP_MULTIPLE;
537 }
538 else {
539 block_length = 0;
540 blocks = 0;
541 }
542
Marc Pignatb6cedb32007-06-06 20:27:59 +0200543 if (host->flags & FL_SENT_STOP)
Andrew Victor65dbf342006-04-02 19:18:51 +0100544 cmdr |= AT91_MCI_TRCMD_STOP;
545
546 if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
547 cmdr |= AT91_MCI_OPDCMD;
548
549 /*
550 * Set the arguments and send the command
551 */
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200552 pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",
Andrew Victore0b19b82006-10-25 19:42:38 +0200553 cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(host, AT91_MCI_MR));
Andrew Victor65dbf342006-04-02 19:18:51 +0100554
555 if (!data) {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100556 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS | ATMEL_PDC_RXTDIS);
557 at91_mci_write(host, ATMEL_PDC_RPR, 0);
558 at91_mci_write(host, ATMEL_PDC_RCR, 0);
559 at91_mci_write(host, ATMEL_PDC_RNPR, 0);
560 at91_mci_write(host, ATMEL_PDC_RNCR, 0);
561 at91_mci_write(host, ATMEL_PDC_TPR, 0);
562 at91_mci_write(host, ATMEL_PDC_TCR, 0);
563 at91_mci_write(host, ATMEL_PDC_TNPR, 0);
564 at91_mci_write(host, ATMEL_PDC_TNCR, 0);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200565 ier = AT91_MCI_CMDRDY;
566 } else {
567 /* zero block length and PDC mode */
568 mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff;
Marc Pignat80f92542008-05-30 14:05:24 +0200569 mr |= (data->blksz & 0x3) ? AT91_MCI_PDCFBYTE : 0;
570 mr |= (block_length << 16);
571 mr |= AT91_MCI_PDCMODE;
572 at91_mci_write(host, AT91_MCI_MR, mr);
Andrew Victor65dbf342006-04-02 19:18:51 +0100573
Marc Pignatc5a89c62008-05-30 14:07:47 +0200574 if (!cpu_is_at91rm9200())
575 at91_mci_write(host, AT91_MCI_BLKR,
576 AT91_MCI_BLKR_BCNT(blocks) |
577 AT91_MCI_BLKR_BLKLEN(block_length));
578
Nicolas Ferreed99c542007-07-09 14:58:16 +0200579 /*
580 * Disable the PDC controller
581 */
582 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100583
Nicolas Ferreed99c542007-07-09 14:58:16 +0200584 if (cmdr & AT91_MCI_TRCMD_START) {
585 data->bytes_xfered = 0;
586 host->transfer_index = 0;
587 host->in_use_index = 0;
588 if (cmdr & AT91_MCI_TRDIR) {
589 /*
590 * Handle a read
591 */
592 host->buffer = NULL;
593 host->total_length = 0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100594
Nicolas Ferreed99c542007-07-09 14:58:16 +0200595 at91_mci_pre_dma_read(host);
596 ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
597 }
598 else {
599 /*
600 * Handle a write
601 */
602 host->total_length = block_length * blocks;
603 host->buffer = dma_alloc_coherent(NULL,
604 host->total_length,
605 &host->physical_address, GFP_KERNEL);
Andrew Victor65dbf342006-04-02 19:18:51 +0100606
Nicolas Ferreed99c542007-07-09 14:58:16 +0200607 at91_mci_sg_to_dma(host, data);
Andrew Victor65dbf342006-04-02 19:18:51 +0100608
Nicolas Ferreed99c542007-07-09 14:58:16 +0200609 pr_debug("Transmitting %d bytes\n", host->total_length);
Andrew Victor65dbf342006-04-02 19:18:51 +0100610
Nicolas Ferreed99c542007-07-09 14:58:16 +0200611 at91_mci_write(host, ATMEL_PDC_TPR, host->physical_address);
Marc Pignat80f92542008-05-30 14:05:24 +0200612 at91_mci_write(host, ATMEL_PDC_TCR, (data->blksz & 0x3) ?
613 host->total_length : host->total_length / 4);
614
Nicolas Ferreed99c542007-07-09 14:58:16 +0200615 ier = AT91_MCI_CMDRDY;
616 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100617 }
618 }
619
620 /*
621 * Send the command and then enable the PDC - not the other way round as
622 * the data sheet says
623 */
624
Andrew Victore0b19b82006-10-25 19:42:38 +0200625 at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
626 at91_mci_write(host, AT91_MCI_CMDR, cmdr);
Andrew Victor65dbf342006-04-02 19:18:51 +0100627
628 if (cmdr & AT91_MCI_TRCMD_START) {
629 if (cmdr & AT91_MCI_TRDIR)
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100630 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Andrew Victor65dbf342006-04-02 19:18:51 +0100631 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100632
Nicolas Ferreed99c542007-07-09 14:58:16 +0200633 /* Enable selected interrupts */
Andrew Victordf05a302006-10-23 14:50:09 +0200634 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_ERRORS | ier);
Andrew Victor65dbf342006-04-02 19:18:51 +0100635}
636
637/*
638 * Process the next step in the request
639 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200640static void at91_mci_process_next(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100641{
642 if (!(host->flags & FL_SENT_COMMAND)) {
643 host->flags |= FL_SENT_COMMAND;
Nicolas Ferreed99c542007-07-09 14:58:16 +0200644 at91_mci_send_command(host, host->request->cmd);
Andrew Victor65dbf342006-04-02 19:18:51 +0100645 }
646 else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) {
647 host->flags |= FL_SENT_STOP;
Nicolas Ferreed99c542007-07-09 14:58:16 +0200648 at91_mci_send_command(host, host->request->stop);
Marc Pignate181dce2008-05-30 14:06:32 +0200649 } else {
650 del_timer(&host->timer);
Marc Pignatc5a89c62008-05-30 14:07:47 +0200651 /* the at91rm9200 mci controller hangs after some transfers,
652 * and the workaround is to reset it after each transfer.
653 */
654 if (cpu_is_at91rm9200())
655 at91_reset_host(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100656 mmc_request_done(host->mmc, host->request);
Marc Pignate181dce2008-05-30 14:06:32 +0200657 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100658}
659
660/*
661 * Handle a command that has been completed
662 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200663static void at91_mci_completed_command(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100664{
665 struct mmc_command *cmd = host->cmd;
666 unsigned int status;
667
Andrew Victore0b19b82006-10-25 19:42:38 +0200668 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
Andrew Victor65dbf342006-04-02 19:18:51 +0100669
Andrew Victore0b19b82006-10-25 19:42:38 +0200670 cmd->resp[0] = at91_mci_read(host, AT91_MCI_RSPR(0));
671 cmd->resp[1] = at91_mci_read(host, AT91_MCI_RSPR(1));
672 cmd->resp[2] = at91_mci_read(host, AT91_MCI_RSPR(2));
673 cmd->resp[3] = at91_mci_read(host, AT91_MCI_RSPR(3));
Andrew Victor65dbf342006-04-02 19:18:51 +0100674
675 if (host->buffer) {
676 dma_free_coherent(NULL, host->total_length, host->buffer, host->physical_address);
677 host->buffer = NULL;
678 }
679
Andrew Victore0b19b82006-10-25 19:42:38 +0200680 status = at91_mci_read(host, AT91_MCI_SR);
Andrew Victor65dbf342006-04-02 19:18:51 +0100681
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100682 pr_debug("Status = %08X [%08X %08X %08X %08X]\n",
Andrew Victor65dbf342006-04-02 19:18:51 +0100683 status, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
684
Andrew Victor9e3866b2007-10-17 11:53:40 +0200685 if (status & AT91_MCI_ERRORS) {
Marc Pignatb6cedb32007-06-06 20:27:59 +0200686 if ((status & AT91_MCI_RCRCE) && !(mmc_resp_type(cmd) & MMC_RSP_CRC)) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200687 cmd->error = 0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100688 }
689 else {
690 if (status & (AT91_MCI_RTOE | AT91_MCI_DTOE))
Pierre Ossman17b04292007-07-22 22:18:46 +0200691 cmd->error = -ETIMEDOUT;
Andrew Victor65dbf342006-04-02 19:18:51 +0100692 else if (status & (AT91_MCI_RCRCE | AT91_MCI_DCRCE))
Pierre Ossman17b04292007-07-22 22:18:46 +0200693 cmd->error = -EILSEQ;
Andrew Victor65dbf342006-04-02 19:18:51 +0100694 else
Pierre Ossman17b04292007-07-22 22:18:46 +0200695 cmd->error = -EIO;
Andrew Victor65dbf342006-04-02 19:18:51 +0100696
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100697 pr_debug("Error detected and set to %d (cmd = %d, retries = %d)\n",
Andrew Victor65dbf342006-04-02 19:18:51 +0100698 cmd->error, cmd->opcode, cmd->retries);
699 }
700 }
701 else
Pierre Ossman17b04292007-07-22 22:18:46 +0200702 cmd->error = 0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100703
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200704 at91_mci_process_next(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100705}
706
707/*
708 * Handle an MMC request
709 */
710static void at91_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
711{
712 struct at91mci_host *host = mmc_priv(mmc);
713 host->request = mrq;
714 host->flags = 0;
715
Marc Pignate181dce2008-05-30 14:06:32 +0200716 mod_timer(&host->timer, jiffies + HZ);
717
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200718 at91_mci_process_next(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100719}
720
721/*
722 * Set the IOS
723 */
724static void at91_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
725{
726 int clkdiv;
727 struct at91mci_host *host = mmc_priv(mmc);
Andrew Victor3dd3b032006-10-23 14:46:54 +0200728 unsigned long at91_master_clock = clk_get_rate(host->mci_clk);
Andrew Victor65dbf342006-04-02 19:18:51 +0100729
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100730 host->bus_mode = ios->bus_mode;
Andrew Victor65dbf342006-04-02 19:18:51 +0100731
732 if (ios->clock == 0) {
733 /* Disable the MCI controller */
Andrew Victore0b19b82006-10-25 19:42:38 +0200734 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100735 clkdiv = 0;
736 }
737 else {
738 /* Enable the MCI controller */
Andrew Victore0b19b82006-10-25 19:42:38 +0200739 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
Andrew Victor65dbf342006-04-02 19:18:51 +0100740
741 if ((at91_master_clock % (ios->clock * 2)) == 0)
742 clkdiv = ((at91_master_clock / ios->clock) / 2) - 1;
743 else
744 clkdiv = (at91_master_clock / ios->clock) / 2;
745
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100746 pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv,
Andrew Victor65dbf342006-04-02 19:18:51 +0100747 at91_master_clock / (2 * (clkdiv + 1)));
748 }
749 if (ios->bus_width == MMC_BUS_WIDTH_4 && host->board->wire4) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100750 pr_debug("MMC: Setting controller bus width to 4\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200751 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) | AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100752 }
753 else {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100754 pr_debug("MMC: Setting controller bus width to 1\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200755 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100756 }
757
758 /* Set the clock divider */
Andrew Victore0b19b82006-10-25 19:42:38 +0200759 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 +0100760
761 /* maybe switch power to the card */
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100762 if (host->board->vcc_pin) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100763 switch (ios->power_mode) {
764 case MMC_POWER_OFF:
David Brownell6e996ee2008-02-04 18:12:48 +0100765 gpio_set_value(host->board->vcc_pin, 0);
Andrew Victor65dbf342006-04-02 19:18:51 +0100766 break;
767 case MMC_POWER_UP:
David Brownell6e996ee2008-02-04 18:12:48 +0100768 gpio_set_value(host->board->vcc_pin, 1);
Andrew Victor65dbf342006-04-02 19:18:51 +0100769 break;
Marc Pignate5c0ef92008-05-09 11:07:07 +0200770 case MMC_POWER_ON:
771 break;
772 default:
773 WARN_ON(1);
Andrew Victor65dbf342006-04-02 19:18:51 +0100774 }
775 }
776}
777
778/*
779 * Handle an interrupt
780 */
David Howells7d12e782006-10-05 14:55:46 +0100781static irqreturn_t at91_mci_irq(int irq, void *devid)
Andrew Victor65dbf342006-04-02 19:18:51 +0100782{
783 struct at91mci_host *host = devid;
784 int completed = 0;
Andrew Victordf05a302006-10-23 14:50:09 +0200785 unsigned int int_status, int_mask;
Andrew Victor65dbf342006-04-02 19:18:51 +0100786
Andrew Victore0b19b82006-10-25 19:42:38 +0200787 int_status = at91_mci_read(host, AT91_MCI_SR);
Andrew Victordf05a302006-10-23 14:50:09 +0200788 int_mask = at91_mci_read(host, AT91_MCI_IMR);
Nicolas Ferre37b758e2007-08-08 12:01:44 +0200789
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200790 pr_debug("MCI irq: status = %08X, %08X, %08X\n", int_status, int_mask,
Andrew Victordf05a302006-10-23 14:50:09 +0200791 int_status & int_mask);
Nicolas Ferre37b758e2007-08-08 12:01:44 +0200792
Andrew Victordf05a302006-10-23 14:50:09 +0200793 int_status = int_status & int_mask;
Andrew Victor65dbf342006-04-02 19:18:51 +0100794
Andrew Victordf05a302006-10-23 14:50:09 +0200795 if (int_status & AT91_MCI_ERRORS) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100796 completed = 1;
Nicolas Ferre37b758e2007-08-08 12:01:44 +0200797
Andrew Victordf05a302006-10-23 14:50:09 +0200798 if (int_status & AT91_MCI_UNRE)
799 pr_debug("MMC: Underrun error\n");
800 if (int_status & AT91_MCI_OVRE)
801 pr_debug("MMC: Overrun error\n");
802 if (int_status & AT91_MCI_DTOE)
803 pr_debug("MMC: Data timeout\n");
804 if (int_status & AT91_MCI_DCRCE)
805 pr_debug("MMC: CRC error in data\n");
806 if (int_status & AT91_MCI_RTOE)
807 pr_debug("MMC: Response timeout\n");
808 if (int_status & AT91_MCI_RENDE)
809 pr_debug("MMC: Response end bit error\n");
810 if (int_status & AT91_MCI_RCRCE)
811 pr_debug("MMC: Response CRC error\n");
812 if (int_status & AT91_MCI_RDIRE)
813 pr_debug("MMC: Response direction error\n");
814 if (int_status & AT91_MCI_RINDE)
815 pr_debug("MMC: Response index error\n");
816 } else {
817 /* Only continue processing if no errors */
Andrew Victor65dbf342006-04-02 19:18:51 +0100818
Andrew Victor65dbf342006-04-02 19:18:51 +0100819 if (int_status & AT91_MCI_TXBUFE) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100820 pr_debug("TX buffer empty\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100821 at91_mci_handle_transmitted(host);
822 }
823
Nicolas Ferreed99c542007-07-09 14:58:16 +0200824 if (int_status & AT91_MCI_ENDRX) {
825 pr_debug("ENDRX\n");
826 at91_mci_post_dma_read(host);
827 }
828
Andrew Victor65dbf342006-04-02 19:18:51 +0100829 if (int_status & AT91_MCI_RXBUFF) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100830 pr_debug("RX buffer full\n");
Nicolas Ferreed99c542007-07-09 14:58:16 +0200831 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
832 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_RXBUFF | AT91_MCI_ENDRX);
833 completed = 1;
Andrew Victor65dbf342006-04-02 19:18:51 +0100834 }
835
Andrew Victordf05a302006-10-23 14:50:09 +0200836 if (int_status & AT91_MCI_ENDTX)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100837 pr_debug("Transmit has ended\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100838
Andrew Victor65dbf342006-04-02 19:18:51 +0100839 if (int_status & AT91_MCI_NOTBUSY) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100840 pr_debug("Card is ready\n");
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200841 at91_mci_update_bytes_xfered(host);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200842 completed = 1;
Andrew Victor65dbf342006-04-02 19:18:51 +0100843 }
844
Andrew Victordf05a302006-10-23 14:50:09 +0200845 if (int_status & AT91_MCI_DTIP)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100846 pr_debug("Data transfer in progress\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100847
Nicolas Ferreed99c542007-07-09 14:58:16 +0200848 if (int_status & AT91_MCI_BLKE) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100849 pr_debug("Block transfer has ended\n");
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200850 if (host->request->data && host->request->data->blocks > 1) {
851 /* multi block write : complete multi write
852 * command and send stop */
853 completed = 1;
854 } else {
855 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
856 }
Nicolas Ferreed99c542007-07-09 14:58:16 +0200857 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100858
Andrew Victordf05a302006-10-23 14:50:09 +0200859 if (int_status & AT91_MCI_TXRDY)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100860 pr_debug("Ready to transmit\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100861
Andrew Victordf05a302006-10-23 14:50:09 +0200862 if (int_status & AT91_MCI_RXRDY)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100863 pr_debug("Ready to receive\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100864
865 if (int_status & AT91_MCI_CMDRDY) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100866 pr_debug("Command ready\n");
Nicolas Ferreed99c542007-07-09 14:58:16 +0200867 completed = at91_mci_handle_cmdrdy(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100868 }
869 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100870
871 if (completed) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100872 pr_debug("Completed command\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200873 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200874 at91_mci_completed_command(host);
Andrew Victordf05a302006-10-23 14:50:09 +0200875 } else
876 at91_mci_write(host, AT91_MCI_IDR, int_status);
Andrew Victor65dbf342006-04-02 19:18:51 +0100877
878 return IRQ_HANDLED;
879}
880
David Howells7d12e782006-10-05 14:55:46 +0100881static irqreturn_t at91_mmc_det_irq(int irq, void *_host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100882{
883 struct at91mci_host *host = _host;
David Brownell6e996ee2008-02-04 18:12:48 +0100884 int present = !gpio_get_value(irq_to_gpio(irq));
Andrew Victor65dbf342006-04-02 19:18:51 +0100885
886 /*
887 * we expect this irq on both insert and remove,
888 * and use a short delay to debounce.
889 */
890 if (present != host->present) {
891 host->present = present;
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100892 pr_debug("%s: card %s\n", mmc_hostname(host->mmc),
Andrew Victor65dbf342006-04-02 19:18:51 +0100893 present ? "insert" : "remove");
894 if (!present) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100895 pr_debug("****** Resetting SD-card bus width ******\n");
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100896 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100897 }
898 mmc_detect_change(host->mmc, msecs_to_jiffies(100));
899 }
900 return IRQ_HANDLED;
901}
902
David Brownella26b4982006-12-26 14:45:26 -0800903static int at91_mci_get_ro(struct mmc_host *mmc)
Andrew Victor65dbf342006-04-02 19:18:51 +0100904{
Andrew Victor65dbf342006-04-02 19:18:51 +0100905 struct at91mci_host *host = mmc_priv(mmc);
906
Anton Vorontsov08f80bb2008-06-17 18:17:39 +0400907 if (host->board->wp_pin)
908 return !!gpio_get_value(host->board->wp_pin);
909 /*
910 * Board doesn't support read only detection; let the mmc core
911 * decide what to do.
912 */
913 return -ENOSYS;
Andrew Victor65dbf342006-04-02 19:18:51 +0100914}
915
David Brownellab7aefd2006-11-12 17:55:30 -0800916static const struct mmc_host_ops at91_mci_ops = {
Andrew Victor65dbf342006-04-02 19:18:51 +0100917 .request = at91_mci_request,
918 .set_ios = at91_mci_set_ios,
919 .get_ro = at91_mci_get_ro,
920};
921
922/*
923 * Probe for the device
924 */
David Brownella26b4982006-12-26 14:45:26 -0800925static int __init at91_mci_probe(struct platform_device *pdev)
Andrew Victor65dbf342006-04-02 19:18:51 +0100926{
927 struct mmc_host *mmc;
928 struct at91mci_host *host;
Andrew Victor17ea0592006-10-23 14:44:40 +0200929 struct resource *res;
Andrew Victor65dbf342006-04-02 19:18:51 +0100930 int ret;
931
Andrew Victor17ea0592006-10-23 14:44:40 +0200932 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
933 if (!res)
934 return -ENXIO;
935
936 if (!request_mem_region(res->start, res->end - res->start + 1, DRIVER_NAME))
937 return -EBUSY;
938
Andrew Victor65dbf342006-04-02 19:18:51 +0100939 mmc = mmc_alloc_host(sizeof(struct at91mci_host), &pdev->dev);
940 if (!mmc) {
David Brownell6e996ee2008-02-04 18:12:48 +0100941 ret = -ENOMEM;
942 dev_dbg(&pdev->dev, "couldn't allocate mmc host\n");
943 goto fail6;
Andrew Victor65dbf342006-04-02 19:18:51 +0100944 }
945
946 mmc->ops = &at91_mci_ops;
947 mmc->f_min = 375000;
948 mmc->f_max = 25000000;
949 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
950
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100951 mmc->max_blk_size = 4095;
Pierre Ossman55db8902006-11-21 17:55:45 +0100952 mmc->max_blk_count = mmc->max_req_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100953
Andrew Victor65dbf342006-04-02 19:18:51 +0100954 host = mmc_priv(mmc);
955 host->mmc = mmc;
956 host->buffer = NULL;
957 host->bus_mode = 0;
958 host->board = pdev->dev.platform_data;
959 if (host->board->wire4) {
Nicolas Ferreed99c542007-07-09 14:58:16 +0200960 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
961 mmc->caps |= MMC_CAP_4_BIT_DATA;
962 else
David Brownell6e996ee2008-02-04 18:12:48 +0100963 dev_warn(&pdev->dev, "4 wire bus mode not supported"
Nicolas Ferreed99c542007-07-09 14:58:16 +0200964 " - using 1 wire\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100965 }
966
967 /*
David Brownell6e996ee2008-02-04 18:12:48 +0100968 * Reserve GPIOs ... board init code makes sure these pins are set
969 * up as GPIOs with the right direction (input, except for vcc)
970 */
971 if (host->board->det_pin) {
972 ret = gpio_request(host->board->det_pin, "mmc_detect");
973 if (ret < 0) {
974 dev_dbg(&pdev->dev, "couldn't claim card detect pin\n");
975 goto fail5;
976 }
977 }
978 if (host->board->wp_pin) {
979 ret = gpio_request(host->board->wp_pin, "mmc_wp");
980 if (ret < 0) {
981 dev_dbg(&pdev->dev, "couldn't claim wp sense pin\n");
982 goto fail4;
983 }
984 }
985 if (host->board->vcc_pin) {
986 ret = gpio_request(host->board->vcc_pin, "mmc_vcc");
987 if (ret < 0) {
988 dev_dbg(&pdev->dev, "couldn't claim vcc switch pin\n");
989 goto fail3;
990 }
991 }
992
993 /*
Andrew Victor65dbf342006-04-02 19:18:51 +0100994 * Get Clock
995 */
Andrew Victor3dd3b032006-10-23 14:46:54 +0200996 host->mci_clk = clk_get(&pdev->dev, "mci_clk");
997 if (IS_ERR(host->mci_clk)) {
David Brownell6e996ee2008-02-04 18:12:48 +0100998 ret = -ENODEV;
999 dev_dbg(&pdev->dev, "no mci_clk?\n");
1000 goto fail2;
Andrew Victor65dbf342006-04-02 19:18:51 +01001001 }
Andrew Victor65dbf342006-04-02 19:18:51 +01001002
Andrew Victor17ea0592006-10-23 14:44:40 +02001003 /*
1004 * Map I/O region
1005 */
1006 host->baseaddr = ioremap(res->start, res->end - res->start + 1);
1007 if (!host->baseaddr) {
David Brownell6e996ee2008-02-04 18:12:48 +01001008 ret = -ENOMEM;
1009 goto fail1;
Andrew Victor17ea0592006-10-23 14:44:40 +02001010 }
Andrew Victore0b19b82006-10-25 19:42:38 +02001011
1012 /*
1013 * Reset hardware
1014 */
Andrew Victor3dd3b032006-10-23 14:46:54 +02001015 clk_enable(host->mci_clk); /* Enable the peripheral clock */
Andrew Victore0b19b82006-10-25 19:42:38 +02001016 at91_mci_disable(host);
1017 at91_mci_enable(host);
1018
Andrew Victor65dbf342006-04-02 19:18:51 +01001019 /*
1020 * Allocate the MCI interrupt
1021 */
Andrew Victor17ea0592006-10-23 14:44:40 +02001022 host->irq = platform_get_irq(pdev, 0);
David Brownell6e996ee2008-02-04 18:12:48 +01001023 ret = request_irq(host->irq, at91_mci_irq, IRQF_SHARED,
1024 mmc_hostname(mmc), host);
Andrew Victor65dbf342006-04-02 19:18:51 +01001025 if (ret) {
David Brownell6e996ee2008-02-04 18:12:48 +01001026 dev_dbg(&pdev->dev, "request MCI interrupt failed\n");
1027 goto fail0;
Andrew Victor65dbf342006-04-02 19:18:51 +01001028 }
1029
1030 platform_set_drvdata(pdev, mmc);
1031
1032 /*
1033 * Add host to MMC layer
1034 */
Marc Pignat63b66432007-07-16 11:07:02 +02001035 if (host->board->det_pin) {
David Brownell6e996ee2008-02-04 18:12:48 +01001036 host->present = !gpio_get_value(host->board->det_pin);
Marc Pignat63b66432007-07-16 11:07:02 +02001037 }
Andrew Victor65dbf342006-04-02 19:18:51 +01001038 else
1039 host->present = -1;
1040
1041 mmc_add_host(mmc);
1042
Marc Pignate181dce2008-05-30 14:06:32 +02001043 setup_timer(&host->timer, at91_timeout_timer, (unsigned long)host);
1044
Andrew Victor65dbf342006-04-02 19:18:51 +01001045 /*
1046 * monitor card insertion/removal if we can
1047 */
1048 if (host->board->det_pin) {
David Brownell6e996ee2008-02-04 18:12:48 +01001049 ret = request_irq(gpio_to_irq(host->board->det_pin),
1050 at91_mmc_det_irq, 0, mmc_hostname(mmc), host);
Andrew Victor65dbf342006-04-02 19:18:51 +01001051 if (ret)
David Brownell6e996ee2008-02-04 18:12:48 +01001052 dev_warn(&pdev->dev, "request MMC detect irq failed\n");
1053 else
1054 device_init_wakeup(&pdev->dev, 1);
Andrew Victor65dbf342006-04-02 19:18:51 +01001055 }
1056
Andrew Victorf3a8efa2006-10-23 14:53:20 +02001057 pr_debug("Added MCI driver\n");
Andrew Victor65dbf342006-04-02 19:18:51 +01001058
1059 return 0;
David Brownell6e996ee2008-02-04 18:12:48 +01001060
1061fail0:
1062 clk_disable(host->mci_clk);
1063 iounmap(host->baseaddr);
1064fail1:
1065 clk_put(host->mci_clk);
1066fail2:
1067 if (host->board->vcc_pin)
1068 gpio_free(host->board->vcc_pin);
1069fail3:
1070 if (host->board->wp_pin)
1071 gpio_free(host->board->wp_pin);
1072fail4:
1073 if (host->board->det_pin)
1074 gpio_free(host->board->det_pin);
1075fail5:
1076 mmc_free_host(mmc);
1077fail6:
1078 release_mem_region(res->start, res->end - res->start + 1);
1079 dev_err(&pdev->dev, "probe failed, err %d\n", ret);
1080 return ret;
Andrew Victor65dbf342006-04-02 19:18:51 +01001081}
1082
1083/*
1084 * Remove a device
1085 */
David Brownella26b4982006-12-26 14:45:26 -08001086static int __exit at91_mci_remove(struct platform_device *pdev)
Andrew Victor65dbf342006-04-02 19:18:51 +01001087{
1088 struct mmc_host *mmc = platform_get_drvdata(pdev);
1089 struct at91mci_host *host;
Andrew Victor17ea0592006-10-23 14:44:40 +02001090 struct resource *res;
Andrew Victor65dbf342006-04-02 19:18:51 +01001091
1092 if (!mmc)
1093 return -1;
1094
1095 host = mmc_priv(mmc);
1096
Anti Sulline0cda542007-08-30 16:15:16 +02001097 if (host->board->det_pin) {
David Brownell6e996ee2008-02-04 18:12:48 +01001098 if (device_can_wakeup(&pdev->dev))
1099 free_irq(gpio_to_irq(host->board->det_pin), host);
Marc Pignat63b66432007-07-16 11:07:02 +02001100 device_init_wakeup(&pdev->dev, 0);
David Brownell6e996ee2008-02-04 18:12:48 +01001101 gpio_free(host->board->det_pin);
Andrew Victor65dbf342006-04-02 19:18:51 +01001102 }
1103
Andrew Victore0b19b82006-10-25 19:42:38 +02001104 at91_mci_disable(host);
Marc Pignate181dce2008-05-30 14:06:32 +02001105 del_timer_sync(&host->timer);
Andrew Victor17ea0592006-10-23 14:44:40 +02001106 mmc_remove_host(mmc);
1107 free_irq(host->irq, host);
Andrew Victor65dbf342006-04-02 19:18:51 +01001108
Andrew Victor3dd3b032006-10-23 14:46:54 +02001109 clk_disable(host->mci_clk); /* Disable the peripheral clock */
1110 clk_put(host->mci_clk);
Andrew Victor65dbf342006-04-02 19:18:51 +01001111
David Brownell6e996ee2008-02-04 18:12:48 +01001112 if (host->board->vcc_pin)
1113 gpio_free(host->board->vcc_pin);
1114 if (host->board->wp_pin)
1115 gpio_free(host->board->wp_pin);
1116
Andrew Victor17ea0592006-10-23 14:44:40 +02001117 iounmap(host->baseaddr);
1118 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1119 release_mem_region(res->start, res->end - res->start + 1);
Andrew Victor65dbf342006-04-02 19:18:51 +01001120
Andrew Victor17ea0592006-10-23 14:44:40 +02001121 mmc_free_host(mmc);
1122 platform_set_drvdata(pdev, NULL);
Andrew Victorb44fb7a2006-06-19 13:06:05 +01001123 pr_debug("MCI Removed\n");
Andrew Victor65dbf342006-04-02 19:18:51 +01001124
1125 return 0;
1126}
1127
1128#ifdef CONFIG_PM
1129static int at91_mci_suspend(struct platform_device *pdev, pm_message_t state)
1130{
1131 struct mmc_host *mmc = platform_get_drvdata(pdev);
Marc Pignat63b66432007-07-16 11:07:02 +02001132 struct at91mci_host *host = mmc_priv(mmc);
Andrew Victor65dbf342006-04-02 19:18:51 +01001133 int ret = 0;
1134
Anti Sulline0cda542007-08-30 16:15:16 +02001135 if (host->board->det_pin && device_may_wakeup(&pdev->dev))
Marc Pignat63b66432007-07-16 11:07:02 +02001136 enable_irq_wake(host->board->det_pin);
1137
Andrew Victor65dbf342006-04-02 19:18:51 +01001138 if (mmc)
1139 ret = mmc_suspend_host(mmc, state);
1140
1141 return ret;
1142}
1143
1144static int at91_mci_resume(struct platform_device *pdev)
1145{
1146 struct mmc_host *mmc = platform_get_drvdata(pdev);
Marc Pignat63b66432007-07-16 11:07:02 +02001147 struct at91mci_host *host = mmc_priv(mmc);
Andrew Victor65dbf342006-04-02 19:18:51 +01001148 int ret = 0;
1149
Anti Sulline0cda542007-08-30 16:15:16 +02001150 if (host->board->det_pin && device_may_wakeup(&pdev->dev))
Marc Pignat63b66432007-07-16 11:07:02 +02001151 disable_irq_wake(host->board->det_pin);
1152
Andrew Victor65dbf342006-04-02 19:18:51 +01001153 if (mmc)
1154 ret = mmc_resume_host(mmc);
1155
1156 return ret;
1157}
1158#else
1159#define at91_mci_suspend NULL
1160#define at91_mci_resume NULL
1161#endif
1162
1163static struct platform_driver at91_mci_driver = {
David Brownella26b4982006-12-26 14:45:26 -08001164 .remove = __exit_p(at91_mci_remove),
Andrew Victor65dbf342006-04-02 19:18:51 +01001165 .suspend = at91_mci_suspend,
1166 .resume = at91_mci_resume,
1167 .driver = {
1168 .name = DRIVER_NAME,
1169 .owner = THIS_MODULE,
1170 },
1171};
1172
1173static int __init at91_mci_init(void)
1174{
David Brownella26b4982006-12-26 14:45:26 -08001175 return platform_driver_probe(&at91_mci_driver, at91_mci_probe);
Andrew Victor65dbf342006-04-02 19:18:51 +01001176}
1177
1178static void __exit at91_mci_exit(void)
1179{
1180 platform_driver_unregister(&at91_mci_driver);
1181}
1182
1183module_init(at91_mci_init);
1184module_exit(at91_mci_exit);
1185
1186MODULE_DESCRIPTION("AT91 Multimedia Card Interface driver");
1187MODULE_AUTHOR("Nick Randell");
1188MODULE_LICENSE("GPL");
Kay Sieversbc65c722008-04-15 14:34:28 -07001189MODULE_ALIAS("platform:at91_mci");