blob: d3e6a962f42343ac4f30fdd7dc7da8f13ff8a95a [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090068#include <linux/gfp.h>
Marc Kleine-Budde23ef3092010-09-09 16:37:48 -070069#include <linux/highmem.h>
Andrew Victor65dbf342006-04-02 19:18:51 +010070
71#include <linux/mmc/host.h>
Yauhen Kharuzhya2255ff2010-11-25 12:11:51 +020072#include <linux/mmc/sdio.h>
Andrew Victor65dbf342006-04-02 19:18:51 +010073
74#include <asm/io.h>
75#include <asm/irq.h>
David Brownell6e996ee2008-02-04 18:12:48 +010076#include <asm/gpio.h>
77
Russell Kinga09e64f2008-08-05 16:14:15 +010078#include <mach/board.h>
79#include <mach/cpu.h>
80#include <mach/at91_mci.h>
Andrew Victor65dbf342006-04-02 19:18:51 +010081
82#define DRIVER_NAME "at91_mci"
83
Nicolas Ferre5b27a1a2010-03-05 13:43:44 -080084static inline int at91mci_is_mci1rev2xx(void)
85{
86 return ( cpu_is_at91sam9260()
87 || cpu_is_at91sam9263()
88 || cpu_is_at91cap9()
89 || cpu_is_at91sam9rl()
90 || cpu_is_at91sam9g10()
91 || cpu_is_at91sam9g20()
92 );
93}
94
Andrew Victordf05a302006-10-23 14:50:09 +020095#define FL_SENT_COMMAND (1 << 0)
96#define FL_SENT_STOP (1 << 1)
Andrew Victor65dbf342006-04-02 19:18:51 +010097
Andrew Victordf05a302006-10-23 14:50:09 +020098#define AT91_MCI_ERRORS (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE \
99 | AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE \
Nicolas Ferre37b758e82007-08-08 12:01:44 +0200100 | AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)
Andrew Victor65dbf342006-04-02 19:18:51 +0100101
Andrew Victore0b19b82006-10-25 19:42:38 +0200102#define at91_mci_read(host, reg) __raw_readl((host)->baseaddr + (reg))
103#define at91_mci_write(host, reg, val) __raw_writel((val), (host)->baseaddr + (reg))
Andrew Victor65dbf342006-04-02 19:18:51 +0100104
Wolfgang Muees3780d902010-03-05 13:43:40 -0800105#define MCI_BLKSIZE 512
106#define MCI_MAXBLKSIZE 4095
107#define MCI_BLKATONCE 256
108#define MCI_BUFSIZE (MCI_BLKSIZE * MCI_BLKATONCE)
Andrew Victor65dbf342006-04-02 19:18:51 +0100109
110/*
111 * Low level type for this driver
112 */
113struct at91mci_host
114{
115 struct mmc_host *mmc;
116 struct mmc_command *cmd;
117 struct mmc_request *request;
118
Andrew Victore0b19b82006-10-25 19:42:38 +0200119 void __iomem *baseaddr;
Andrew Victor17ea0592006-10-23 14:44:40 +0200120 int irq;
Andrew Victore0b19b82006-10-25 19:42:38 +0200121
Andrew Victor65dbf342006-04-02 19:18:51 +0100122 struct at91_mmc_data *board;
123 int present;
124
Andrew Victor3dd3b032006-10-23 14:46:54 +0200125 struct clk *mci_clk;
126
Andrew Victor65dbf342006-04-02 19:18:51 +0100127 /*
128 * Flag indicating when the command has been sent. This is used to
129 * work out whether or not to send the stop
130 */
131 unsigned int flags;
132 /* flag for current bus settings */
133 u32 bus_mode;
134
135 /* DMA buffer used for transmitting */
136 unsigned int* buffer;
137 dma_addr_t physical_address;
138 unsigned int total_length;
139
140 /* Latest in the scatterlist that has been enabled for transfer, but not freed */
141 int in_use_index;
142
143 /* Latest in the scatterlist that has been enabled for transfer */
144 int transfer_index;
Marc Pignate181dce2008-05-30 14:06:32 +0200145
146 /* Timer for timeouts */
147 struct timer_list timer;
Andrew Victor65dbf342006-04-02 19:18:51 +0100148};
149
Marc Pignatc5a89c62008-05-30 14:07:47 +0200150/*
151 * Reset the controller and restore most of the state
152 */
153static void at91_reset_host(struct at91mci_host *host)
154{
155 unsigned long flags;
156 u32 mr;
157 u32 sdcr;
158 u32 dtor;
159 u32 imr;
160
161 local_irq_save(flags);
162 imr = at91_mci_read(host, AT91_MCI_IMR);
163
164 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
165
166 /* save current state */
167 mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff;
168 sdcr = at91_mci_read(host, AT91_MCI_SDCR);
169 dtor = at91_mci_read(host, AT91_MCI_DTOR);
170
171 /* reset the controller */
172 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
173
174 /* restore state */
175 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
176 at91_mci_write(host, AT91_MCI_MR, mr);
177 at91_mci_write(host, AT91_MCI_SDCR, sdcr);
178 at91_mci_write(host, AT91_MCI_DTOR, dtor);
179 at91_mci_write(host, AT91_MCI_IER, imr);
180
181 /* make sure sdio interrupts will fire */
182 at91_mci_read(host, AT91_MCI_SR);
183
184 local_irq_restore(flags);
185}
186
Marc Pignate181dce2008-05-30 14:06:32 +0200187static void at91_timeout_timer(unsigned long data)
188{
189 struct at91mci_host *host;
190
191 host = (struct at91mci_host *)data;
192
193 if (host->request) {
194 dev_err(host->mmc->parent, "Timeout waiting end of packet\n");
195
196 if (host->cmd && host->cmd->data) {
197 host->cmd->data->error = -ETIMEDOUT;
198 } else {
199 if (host->cmd)
200 host->cmd->error = -ETIMEDOUT;
201 else
202 host->request->cmd->error = -ETIMEDOUT;
203 }
204
Marc Pignatc5a89c62008-05-30 14:07:47 +0200205 at91_reset_host(host);
Marc Pignate181dce2008-05-30 14:06:32 +0200206 mmc_request_done(host->mmc, host->request);
207 }
208}
209
Andrew Victor65dbf342006-04-02 19:18:51 +0100210/*
211 * Copy from sg to a dma block - used for transfers
212 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200213static inline void at91_mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data)
Andrew Victor65dbf342006-04-02 19:18:51 +0100214{
215 unsigned int len, i, size;
216 unsigned *dmabuf = host->buffer;
217
Ville Syrjala5385edc2008-06-14 20:27:20 +0300218 size = data->blksz * data->blocks;
Andrew Victor65dbf342006-04-02 19:18:51 +0100219 len = data->sg_len;
220
Nicolas Ferre5b27a1a2010-03-05 13:43:44 -0800221 /* MCI1 rev2xx Data Write Operation and number of bytes erratum */
222 if (at91mci_is_mci1rev2xx())
Ville Syrjala5385edc2008-06-14 20:27:20 +0300223 if (host->total_length == 12)
224 memset(dmabuf, 0, 12);
225
Andrew Victor65dbf342006-04-02 19:18:51 +0100226 /*
227 * Just loop through all entries. Size might not
228 * be the entire list though so make sure that
229 * we do not transfer too much.
230 */
231 for (i = 0; i < len; i++) {
232 struct scatterlist *sg;
233 int amount;
Andrew Victor65dbf342006-04-02 19:18:51 +0100234 unsigned int *sgbuffer;
235
236 sg = &data->sg[i];
237
Jens Axboe45711f12007-10-22 21:19:53 +0200238 sgbuffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
Andrew Victor65dbf342006-04-02 19:18:51 +0100239 amount = min(size, sg->length);
240 size -= amount;
Andrew Victor65dbf342006-04-02 19:18:51 +0100241
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100242 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
243 int index;
244
245 for (index = 0; index < (amount / 4); index++)
246 *dmabuf++ = swab32(sgbuffer[index]);
Ville Syrjala5385edc2008-06-14 20:27:20 +0300247 } else {
Wolfgang Muees0b3520f2010-03-05 13:43:38 -0800248 char *tmpv = (char *)dmabuf;
249 memcpy(tmpv, sgbuffer, amount);
250 tmpv += amount;
251 dmabuf = (unsigned *)tmpv;
Ville Syrjala5385edc2008-06-14 20:27:20 +0300252 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100253
Nicolas Ferre752993e2010-03-05 13:43:45 -0800254 kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
Andrew Victor65dbf342006-04-02 19:18:51 +0100255
256 if (size == 0)
257 break;
258 }
259
260 /*
261 * Check that we didn't get a request to transfer
262 * more data than can fit into the SG list.
263 */
264 BUG_ON(size != 0);
265}
266
267/*
Andrew Victor65dbf342006-04-02 19:18:51 +0100268 * Handle after a dma read
269 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200270static void at91_mci_post_dma_read(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100271{
272 struct mmc_command *cmd;
273 struct mmc_data *data;
Wolfgang Muees86ee26f2010-03-05 13:43:41 -0800274 unsigned int len, i, size;
275 unsigned *dmabuf = host->buffer;
Andrew Victor65dbf342006-04-02 19:18:51 +0100276
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100277 pr_debug("post dma read\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100278
279 cmd = host->cmd;
280 if (!cmd) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100281 pr_debug("no command\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100282 return;
283 }
284
285 data = cmd->data;
286 if (!data) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100287 pr_debug("no data\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100288 return;
289 }
290
Wolfgang Muees86ee26f2010-03-05 13:43:41 -0800291 size = data->blksz * data->blocks;
292 len = data->sg_len;
293
294 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_ENDRX);
295 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_RXBUFF);
296
297 for (i = 0; i < len; i++) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100298 struct scatterlist *sg;
Wolfgang Muees86ee26f2010-03-05 13:43:41 -0800299 int amount;
300 unsigned int *sgbuffer;
Andrew Victor65dbf342006-04-02 19:18:51 +0100301
Wolfgang Muees86ee26f2010-03-05 13:43:41 -0800302 sg = &data->sg[i];
Andrew Victor65dbf342006-04-02 19:18:51 +0100303
Wolfgang Muees86ee26f2010-03-05 13:43:41 -0800304 sgbuffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
305 amount = min(size, sg->length);
306 size -= amount;
Andrew Victor65dbf342006-04-02 19:18:51 +0100307
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100308 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
309 int index;
Wolfgang Muees86ee26f2010-03-05 13:43:41 -0800310 for (index = 0; index < (amount / 4); index++)
311 sgbuffer[index] = swab32(*dmabuf++);
312 } else {
313 char *tmpv = (char *)dmabuf;
314 memcpy(sgbuffer, tmpv, amount);
315 tmpv += amount;
316 dmabuf = (unsigned *)tmpv;
Andrew Victor65dbf342006-04-02 19:18:51 +0100317 }
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100318
Nicolas Ferrebdef2fe2010-05-15 12:32:31 -0400319 flush_kernel_dcache_page(sg_page(sg));
Nicolas Ferre752993e2010-03-05 13:43:45 -0800320 kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
Wolfgang Muees86ee26f2010-03-05 13:43:41 -0800321 data->bytes_xfered += amount;
322 if (size == 0)
323 break;
Andrew Victor65dbf342006-04-02 19:18:51 +0100324 }
325
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100326 pr_debug("post dma read done\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100327}
328
329/*
330 * Handle transmitted data
331 */
332static void at91_mci_handle_transmitted(struct at91mci_host *host)
333{
334 struct mmc_command *cmd;
335 struct mmc_data *data;
336
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100337 pr_debug("Handling the transmit\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100338
339 /* Disable the transfer */
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100340 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100341
342 /* Now wait for cmd ready */
Andrew Victore0b19b82006-10-25 19:42:38 +0200343 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE);
Andrew Victor65dbf342006-04-02 19:18:51 +0100344
345 cmd = host->cmd;
346 if (!cmd) return;
347
348 data = cmd->data;
349 if (!data) return;
350
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200351 if (cmd->data->blocks > 1) {
Nicolas Ferreed99c542007-07-09 14:58:16 +0200352 pr_debug("multiple write : wait for BLKE...\n");
353 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
354 } else
355 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
Andrew Victor65dbf342006-04-02 19:18:51 +0100356}
357
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200358/*
359 * Update bytes tranfered count during a write operation
360 */
361static void at91_mci_update_bytes_xfered(struct at91mci_host *host)
362{
363 struct mmc_data *data;
364
365 /* always deal with the effective request (and not the current cmd) */
366
367 if (host->request->cmd && host->request->cmd->error != 0)
368 return;
369
370 if (host->request->data) {
371 data = host->request->data;
372 if (data->flags & MMC_DATA_WRITE) {
373 /* card is in IDLE mode now */
374 pr_debug("-> bytes_xfered %d, total_length = %d\n",
375 data->bytes_xfered, host->total_length);
Ville Syrjala5385edc2008-06-14 20:27:20 +0300376 data->bytes_xfered = data->blksz * data->blocks;
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200377 }
378 }
379}
380
381
Nicolas Ferreed99c542007-07-09 14:58:16 +0200382/*Handle after command sent ready*/
383static int at91_mci_handle_cmdrdy(struct at91mci_host *host)
384{
385 if (!host->cmd)
386 return 1;
387 else if (!host->cmd->data) {
388 if (host->flags & FL_SENT_STOP) {
389 /*After multi block write, we must wait for NOTBUSY*/
390 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
391 } else return 1;
392 } else if (host->cmd->data->flags & MMC_DATA_WRITE) {
393 /*After sendding multi-block-write command, start DMA transfer*/
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200394 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_TXBUFE | AT91_MCI_BLKE);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200395 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
396 }
397
398 /* command not completed, have to wait */
399 return 0;
400}
401
402
Andrew Victor65dbf342006-04-02 19:18:51 +0100403/*
404 * Enable the controller
405 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200406static void at91_mci_enable(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100407{
Nicolas Ferreed99c542007-07-09 14:58:16 +0200408 unsigned int mr;
409
Andrew Victore0b19b82006-10-25 19:42:38 +0200410 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200411 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
Andrew Victore0b19b82006-10-25 19:42:38 +0200412 at91_mci_write(host, AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200413 mr = AT91_MCI_PDCMODE | 0x34a;
414
Nicolas Ferre5b27a1a2010-03-05 13:43:44 -0800415 if (at91mci_is_mci1rev2xx())
Nicolas Ferreed99c542007-07-09 14:58:16 +0200416 mr |= AT91_MCI_RDPROOF | AT91_MCI_WRPROOF;
417
418 at91_mci_write(host, AT91_MCI_MR, mr);
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100419
420 /* use Slot A or B (only one at same time) */
421 at91_mci_write(host, AT91_MCI_SDCR, host->board->slot_b);
Andrew Victor65dbf342006-04-02 19:18:51 +0100422}
423
424/*
425 * Disable the controller
426 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200427static void at91_mci_disable(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100428{
Andrew Victore0b19b82006-10-25 19:42:38 +0200429 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
Andrew Victor65dbf342006-04-02 19:18:51 +0100430}
431
432/*
433 * Send a command
Andrew Victor65dbf342006-04-02 19:18:51 +0100434 */
Nicolas Ferreed99c542007-07-09 14:58:16 +0200435static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd)
Andrew Victor65dbf342006-04-02 19:18:51 +0100436{
437 unsigned int cmdr, mr;
438 unsigned int block_length;
439 struct mmc_data *data = cmd->data;
440
441 unsigned int blocks;
442 unsigned int ier = 0;
443
444 host->cmd = cmd;
445
Nicolas Ferreed99c542007-07-09 14:58:16 +0200446 /* Needed for leaving busy state before CMD1 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200447 if ((at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100448 pr_debug("Clearing timeout\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200449 at91_mci_write(host, AT91_MCI_ARGR, 0);
450 at91_mci_write(host, AT91_MCI_CMDR, AT91_MCI_OPDCMD);
451 while (!(at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_CMDRDY)) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100452 /* spin */
Andrew Victore0b19b82006-10-25 19:42:38 +0200453 pr_debug("Clearing: SR = %08X\n", at91_mci_read(host, AT91_MCI_SR));
Andrew Victor65dbf342006-04-02 19:18:51 +0100454 }
455 }
Nicolas Ferreed99c542007-07-09 14:58:16 +0200456
Andrew Victor65dbf342006-04-02 19:18:51 +0100457 cmdr = cmd->opcode;
458
459 if (mmc_resp_type(cmd) == MMC_RSP_NONE)
460 cmdr |= AT91_MCI_RSPTYP_NONE;
461 else {
462 /* if a response is expected then allow maximum response latancy */
463 cmdr |= AT91_MCI_MAXLAT;
464 /* set 136 bit response for R2, 48 bit response otherwise */
465 if (mmc_resp_type(cmd) == MMC_RSP_R2)
466 cmdr |= AT91_MCI_RSPTYP_136;
467 else
468 cmdr |= AT91_MCI_RSPTYP_48;
469 }
470
471 if (data) {
Marc Pignat1d4de9e2007-08-09 13:56:29 +0200472
Ville Syrjala9da3cba2008-06-09 22:06:44 +0300473 if (cpu_is_at91rm9200() || cpu_is_at91sam9261()) {
474 if (data->blksz & 0x3) {
475 pr_debug("Unsupported block size\n");
476 cmd->error = -EINVAL;
477 mmc_request_done(host->mmc, host->request);
478 return;
479 }
480 if (data->flags & MMC_DATA_STREAM) {
481 pr_debug("Stream commands not supported\n");
482 cmd->error = -EINVAL;
483 mmc_request_done(host->mmc, host->request);
484 return;
485 }
Marc Pignat1d4de9e2007-08-09 13:56:29 +0200486 }
487
Russell Kinga3fd4a12006-06-04 17:51:15 +0100488 block_length = data->blksz;
Andrew Victor65dbf342006-04-02 19:18:51 +0100489 blocks = data->blocks;
490
491 /* always set data start - also set direction flag for read */
492 if (data->flags & MMC_DATA_READ)
493 cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START);
494 else if (data->flags & MMC_DATA_WRITE)
495 cmdr |= AT91_MCI_TRCMD_START;
496
Yauhen Kharuzhya2255ff2010-11-25 12:11:51 +0200497 if (cmd->opcode == SD_IO_RW_EXTENDED) {
498 cmdr |= AT91_MCI_TRTYP_SDIO_BLOCK;
499 } else {
500 if (data->flags & MMC_DATA_STREAM)
501 cmdr |= AT91_MCI_TRTYP_STREAM;
502 if (data->blocks > 1)
503 cmdr |= AT91_MCI_TRTYP_MULTIPLE;
504 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100505 }
506 else {
507 block_length = 0;
508 blocks = 0;
509 }
510
Marc Pignatb6cedb32007-06-06 20:27:59 +0200511 if (host->flags & FL_SENT_STOP)
Andrew Victor65dbf342006-04-02 19:18:51 +0100512 cmdr |= AT91_MCI_TRCMD_STOP;
513
514 if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
515 cmdr |= AT91_MCI_OPDCMD;
516
517 /*
518 * Set the arguments and send the command
519 */
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200520 pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",
Andrew Victore0b19b82006-10-25 19:42:38 +0200521 cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(host, AT91_MCI_MR));
Andrew Victor65dbf342006-04-02 19:18:51 +0100522
523 if (!data) {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100524 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS | ATMEL_PDC_RXTDIS);
525 at91_mci_write(host, ATMEL_PDC_RPR, 0);
526 at91_mci_write(host, ATMEL_PDC_RCR, 0);
527 at91_mci_write(host, ATMEL_PDC_RNPR, 0);
528 at91_mci_write(host, ATMEL_PDC_RNCR, 0);
529 at91_mci_write(host, ATMEL_PDC_TPR, 0);
530 at91_mci_write(host, ATMEL_PDC_TCR, 0);
531 at91_mci_write(host, ATMEL_PDC_TNPR, 0);
532 at91_mci_write(host, ATMEL_PDC_TNCR, 0);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200533 ier = AT91_MCI_CMDRDY;
534 } else {
535 /* zero block length and PDC mode */
Ville Syrjala12bd2572008-06-09 22:06:45 +0300536 mr = at91_mci_read(host, AT91_MCI_MR) & 0x5fff;
Marc Pignat80f92542008-05-30 14:05:24 +0200537 mr |= (data->blksz & 0x3) ? AT91_MCI_PDCFBYTE : 0;
538 mr |= (block_length << 16);
539 mr |= AT91_MCI_PDCMODE;
540 at91_mci_write(host, AT91_MCI_MR, mr);
Andrew Victor65dbf342006-04-02 19:18:51 +0100541
Ville Syrjala9da3cba2008-06-09 22:06:44 +0300542 if (!(cpu_is_at91rm9200() || cpu_is_at91sam9261()))
Marc Pignatc5a89c62008-05-30 14:07:47 +0200543 at91_mci_write(host, AT91_MCI_BLKR,
544 AT91_MCI_BLKR_BCNT(blocks) |
545 AT91_MCI_BLKR_BLKLEN(block_length));
546
Nicolas Ferreed99c542007-07-09 14:58:16 +0200547 /*
548 * Disable the PDC controller
549 */
550 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100551
Nicolas Ferreed99c542007-07-09 14:58:16 +0200552 if (cmdr & AT91_MCI_TRCMD_START) {
553 data->bytes_xfered = 0;
554 host->transfer_index = 0;
555 host->in_use_index = 0;
556 if (cmdr & AT91_MCI_TRDIR) {
557 /*
558 * Handle a read
559 */
Nicolas Ferreed99c542007-07-09 14:58:16 +0200560 host->total_length = 0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100561
Wolfgang Muees86ee26f2010-03-05 13:43:41 -0800562 at91_mci_write(host, ATMEL_PDC_RPR, host->physical_address);
563 at91_mci_write(host, ATMEL_PDC_RCR, (data->blksz & 0x3) ?
564 (blocks * block_length) : (blocks * block_length) / 4);
565 at91_mci_write(host, ATMEL_PDC_RNPR, 0);
566 at91_mci_write(host, ATMEL_PDC_RNCR, 0);
567
Nicolas Ferreed99c542007-07-09 14:58:16 +0200568 ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
569 }
570 else {
571 /*
572 * Handle a write
573 */
574 host->total_length = block_length * blocks;
Ville Syrjala5385edc2008-06-14 20:27:20 +0300575 /*
Nicolas Ferre5b27a1a2010-03-05 13:43:44 -0800576 * MCI1 rev2xx Data Write Operation and
Ville Syrjala5385edc2008-06-14 20:27:20 +0300577 * number of bytes erratum
578 */
Nicolas Ferre5b27a1a2010-03-05 13:43:44 -0800579 if (at91mci_is_mci1rev2xx())
Ville Syrjala5385edc2008-06-14 20:27:20 +0300580 if (host->total_length < 12)
581 host->total_length = 12;
David Brownelle385ea62008-09-02 14:35:46 -0700582
Nicolas Ferreed99c542007-07-09 14:58:16 +0200583 at91_mci_sg_to_dma(host, data);
Andrew Victor65dbf342006-04-02 19:18:51 +0100584
Nicolas Ferreed99c542007-07-09 14:58:16 +0200585 pr_debug("Transmitting %d bytes\n", host->total_length);
Andrew Victor65dbf342006-04-02 19:18:51 +0100586
Nicolas Ferreed99c542007-07-09 14:58:16 +0200587 at91_mci_write(host, ATMEL_PDC_TPR, host->physical_address);
Marc Pignat80f92542008-05-30 14:05:24 +0200588 at91_mci_write(host, ATMEL_PDC_TCR, (data->blksz & 0x3) ?
589 host->total_length : host->total_length / 4);
590
Nicolas Ferreed99c542007-07-09 14:58:16 +0200591 ier = AT91_MCI_CMDRDY;
592 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100593 }
594 }
595
596 /*
597 * Send the command and then enable the PDC - not the other way round as
598 * the data sheet says
599 */
600
Andrew Victore0b19b82006-10-25 19:42:38 +0200601 at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
602 at91_mci_write(host, AT91_MCI_CMDR, cmdr);
Andrew Victor65dbf342006-04-02 19:18:51 +0100603
604 if (cmdr & AT91_MCI_TRCMD_START) {
605 if (cmdr & AT91_MCI_TRDIR)
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100606 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Andrew Victor65dbf342006-04-02 19:18:51 +0100607 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100608
Nicolas Ferreed99c542007-07-09 14:58:16 +0200609 /* Enable selected interrupts */
Andrew Victordf05a302006-10-23 14:50:09 +0200610 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_ERRORS | ier);
Andrew Victor65dbf342006-04-02 19:18:51 +0100611}
612
613/*
614 * Process the next step in the request
615 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200616static void at91_mci_process_next(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100617{
618 if (!(host->flags & FL_SENT_COMMAND)) {
619 host->flags |= FL_SENT_COMMAND;
Nicolas Ferreed99c542007-07-09 14:58:16 +0200620 at91_mci_send_command(host, host->request->cmd);
Andrew Victor65dbf342006-04-02 19:18:51 +0100621 }
622 else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) {
623 host->flags |= FL_SENT_STOP;
Nicolas Ferreed99c542007-07-09 14:58:16 +0200624 at91_mci_send_command(host, host->request->stop);
Marc Pignate181dce2008-05-30 14:06:32 +0200625 } else {
626 del_timer(&host->timer);
Marc Pignatc5a89c62008-05-30 14:07:47 +0200627 /* the at91rm9200 mci controller hangs after some transfers,
628 * and the workaround is to reset it after each transfer.
629 */
630 if (cpu_is_at91rm9200())
631 at91_reset_host(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100632 mmc_request_done(host->mmc, host->request);
Marc Pignate181dce2008-05-30 14:06:32 +0200633 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100634}
635
636/*
637 * Handle a command that has been completed
638 */
Nicolas Ferreba7deee2008-05-30 14:28:45 +0200639static void at91_mci_completed_command(struct at91mci_host *host, unsigned int status)
Andrew Victor65dbf342006-04-02 19:18:51 +0100640{
641 struct mmc_command *cmd = host->cmd;
Nicolas Ferrefa1fe012008-06-10 11:27:29 +0200642 struct mmc_data *data = cmd->data;
Andrew Victor65dbf342006-04-02 19:18:51 +0100643
Eric Benard7a6588b2008-05-30 14:26:05 +0200644 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
Andrew Victor65dbf342006-04-02 19:18:51 +0100645
Andrew Victore0b19b82006-10-25 19:42:38 +0200646 cmd->resp[0] = at91_mci_read(host, AT91_MCI_RSPR(0));
647 cmd->resp[1] = at91_mci_read(host, AT91_MCI_RSPR(1));
648 cmd->resp[2] = at91_mci_read(host, AT91_MCI_RSPR(2));
649 cmd->resp[3] = at91_mci_read(host, AT91_MCI_RSPR(3));
Andrew Victor65dbf342006-04-02 19:18:51 +0100650
Nicolas Ferreba7deee2008-05-30 14:28:45 +0200651 pr_debug("Status = %08X/%08x [%08X %08X %08X %08X]\n",
652 status, at91_mci_read(host, AT91_MCI_SR),
653 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
Andrew Victor65dbf342006-04-02 19:18:51 +0100654
Andrew Victor9e3866b2007-10-17 11:53:40 +0200655 if (status & AT91_MCI_ERRORS) {
Marc Pignatb6cedb32007-06-06 20:27:59 +0200656 if ((status & AT91_MCI_RCRCE) && !(mmc_resp_type(cmd) & MMC_RSP_CRC)) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200657 cmd->error = 0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100658 }
659 else {
Nicolas Ferrefa1fe012008-06-10 11:27:29 +0200660 if (status & (AT91_MCI_DTOE | AT91_MCI_DCRCE)) {
661 if (data) {
662 if (status & AT91_MCI_DTOE)
663 data->error = -ETIMEDOUT;
664 else if (status & AT91_MCI_DCRCE)
665 data->error = -EILSEQ;
666 }
667 } else {
668 if (status & AT91_MCI_RTOE)
669 cmd->error = -ETIMEDOUT;
670 else if (status & AT91_MCI_RCRCE)
671 cmd->error = -EILSEQ;
672 else
673 cmd->error = -EIO;
674 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100675
Nicolas Ferrefa1fe012008-06-10 11:27:29 +0200676 pr_debug("Error detected and set to %d/%d (cmd = %d, retries = %d)\n",
677 cmd->error, data ? data->error : 0,
678 cmd->opcode, cmd->retries);
Andrew Victor65dbf342006-04-02 19:18:51 +0100679 }
680 }
681 else
Pierre Ossman17b04292007-07-22 22:18:46 +0200682 cmd->error = 0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100683
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200684 at91_mci_process_next(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100685}
686
687/*
688 * Handle an MMC request
689 */
690static void at91_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
691{
692 struct at91mci_host *host = mmc_priv(mmc);
693 host->request = mrq;
694 host->flags = 0;
695
Wolfgang Mueesa04ac5b2010-03-05 13:43:39 -0800696 /* more than 1s timeout needed with slow SD cards */
697 mod_timer(&host->timer, jiffies + msecs_to_jiffies(2000));
Marc Pignate181dce2008-05-30 14:06:32 +0200698
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200699 at91_mci_process_next(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100700}
701
702/*
703 * Set the IOS
704 */
705static void at91_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
706{
707 int clkdiv;
708 struct at91mci_host *host = mmc_priv(mmc);
Andrew Victor3dd3b032006-10-23 14:46:54 +0200709 unsigned long at91_master_clock = clk_get_rate(host->mci_clk);
Andrew Victor65dbf342006-04-02 19:18:51 +0100710
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100711 host->bus_mode = ios->bus_mode;
Andrew Victor65dbf342006-04-02 19:18:51 +0100712
713 if (ios->clock == 0) {
714 /* Disable the MCI controller */
Andrew Victore0b19b82006-10-25 19:42:38 +0200715 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100716 clkdiv = 0;
717 }
718 else {
719 /* Enable the MCI controller */
Andrew Victore0b19b82006-10-25 19:42:38 +0200720 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
Andrew Victor65dbf342006-04-02 19:18:51 +0100721
722 if ((at91_master_clock % (ios->clock * 2)) == 0)
723 clkdiv = ((at91_master_clock / ios->clock) / 2) - 1;
724 else
725 clkdiv = (at91_master_clock / ios->clock) / 2;
726
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100727 pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv,
Andrew Victor65dbf342006-04-02 19:18:51 +0100728 at91_master_clock / (2 * (clkdiv + 1)));
729 }
730 if (ios->bus_width == MMC_BUS_WIDTH_4 && host->board->wire4) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100731 pr_debug("MMC: Setting controller bus width to 4\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200732 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) | AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100733 }
734 else {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100735 pr_debug("MMC: Setting controller bus width to 1\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200736 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100737 }
738
739 /* Set the clock divider */
Andrew Victore0b19b82006-10-25 19:42:38 +0200740 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 +0100741
742 /* maybe switch power to the card */
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100743 if (host->board->vcc_pin) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100744 switch (ios->power_mode) {
745 case MMC_POWER_OFF:
David Brownell6e996ee2008-02-04 18:12:48 +0100746 gpio_set_value(host->board->vcc_pin, 0);
Andrew Victor65dbf342006-04-02 19:18:51 +0100747 break;
748 case MMC_POWER_UP:
David Brownell6e996ee2008-02-04 18:12:48 +0100749 gpio_set_value(host->board->vcc_pin, 1);
Andrew Victor65dbf342006-04-02 19:18:51 +0100750 break;
Marc Pignate5c0ef92008-05-09 11:07:07 +0200751 case MMC_POWER_ON:
752 break;
753 default:
754 WARN_ON(1);
Andrew Victor65dbf342006-04-02 19:18:51 +0100755 }
756 }
757}
758
759/*
760 * Handle an interrupt
761 */
David Howells7d12e782006-10-05 14:55:46 +0100762static irqreturn_t at91_mci_irq(int irq, void *devid)
Andrew Victor65dbf342006-04-02 19:18:51 +0100763{
764 struct at91mci_host *host = devid;
765 int completed = 0;
Andrew Victordf05a302006-10-23 14:50:09 +0200766 unsigned int int_status, int_mask;
Andrew Victor65dbf342006-04-02 19:18:51 +0100767
Andrew Victore0b19b82006-10-25 19:42:38 +0200768 int_status = at91_mci_read(host, AT91_MCI_SR);
Andrew Victordf05a302006-10-23 14:50:09 +0200769 int_mask = at91_mci_read(host, AT91_MCI_IMR);
Nicolas Ferre37b758e82007-08-08 12:01:44 +0200770
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200771 pr_debug("MCI irq: status = %08X, %08X, %08X\n", int_status, int_mask,
Andrew Victordf05a302006-10-23 14:50:09 +0200772 int_status & int_mask);
Nicolas Ferre37b758e82007-08-08 12:01:44 +0200773
Andrew Victordf05a302006-10-23 14:50:09 +0200774 int_status = int_status & int_mask;
Andrew Victor65dbf342006-04-02 19:18:51 +0100775
Andrew Victordf05a302006-10-23 14:50:09 +0200776 if (int_status & AT91_MCI_ERRORS) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100777 completed = 1;
Nicolas Ferre37b758e82007-08-08 12:01:44 +0200778
Andrew Victordf05a302006-10-23 14:50:09 +0200779 if (int_status & AT91_MCI_UNRE)
780 pr_debug("MMC: Underrun error\n");
781 if (int_status & AT91_MCI_OVRE)
782 pr_debug("MMC: Overrun error\n");
783 if (int_status & AT91_MCI_DTOE)
784 pr_debug("MMC: Data timeout\n");
785 if (int_status & AT91_MCI_DCRCE)
786 pr_debug("MMC: CRC error in data\n");
787 if (int_status & AT91_MCI_RTOE)
788 pr_debug("MMC: Response timeout\n");
789 if (int_status & AT91_MCI_RENDE)
790 pr_debug("MMC: Response end bit error\n");
791 if (int_status & AT91_MCI_RCRCE)
792 pr_debug("MMC: Response CRC error\n");
793 if (int_status & AT91_MCI_RDIRE)
794 pr_debug("MMC: Response direction error\n");
795 if (int_status & AT91_MCI_RINDE)
796 pr_debug("MMC: Response index error\n");
797 } else {
798 /* Only continue processing if no errors */
Andrew Victor65dbf342006-04-02 19:18:51 +0100799
Andrew Victor65dbf342006-04-02 19:18:51 +0100800 if (int_status & AT91_MCI_TXBUFE) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100801 pr_debug("TX buffer empty\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100802 at91_mci_handle_transmitted(host);
803 }
804
Nicolas Ferreed99c542007-07-09 14:58:16 +0200805 if (int_status & AT91_MCI_ENDRX) {
806 pr_debug("ENDRX\n");
807 at91_mci_post_dma_read(host);
808 }
809
Andrew Victor65dbf342006-04-02 19:18:51 +0100810 if (int_status & AT91_MCI_RXBUFF) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100811 pr_debug("RX buffer full\n");
Nicolas Ferreed99c542007-07-09 14:58:16 +0200812 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
813 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_RXBUFF | AT91_MCI_ENDRX);
814 completed = 1;
Andrew Victor65dbf342006-04-02 19:18:51 +0100815 }
816
Andrew Victordf05a302006-10-23 14:50:09 +0200817 if (int_status & AT91_MCI_ENDTX)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100818 pr_debug("Transmit has ended\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100819
Andrew Victor65dbf342006-04-02 19:18:51 +0100820 if (int_status & AT91_MCI_NOTBUSY) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100821 pr_debug("Card is ready\n");
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200822 at91_mci_update_bytes_xfered(host);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200823 completed = 1;
Andrew Victor65dbf342006-04-02 19:18:51 +0100824 }
825
Andrew Victordf05a302006-10-23 14:50:09 +0200826 if (int_status & AT91_MCI_DTIP)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100827 pr_debug("Data transfer in progress\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100828
Nicolas Ferreed99c542007-07-09 14:58:16 +0200829 if (int_status & AT91_MCI_BLKE) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100830 pr_debug("Block transfer has ended\n");
Nicolas Ferre4ac24a82008-05-30 14:18:57 +0200831 if (host->request->data && host->request->data->blocks > 1) {
832 /* multi block write : complete multi write
833 * command and send stop */
834 completed = 1;
835 } else {
836 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
837 }
Nicolas Ferreed99c542007-07-09 14:58:16 +0200838 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100839
Eric Benard7a6588b2008-05-30 14:26:05 +0200840 if (int_status & AT91_MCI_SDIOIRQA)
841 mmc_signal_sdio_irq(host->mmc);
842
843 if (int_status & AT91_MCI_SDIOIRQB)
844 mmc_signal_sdio_irq(host->mmc);
845
Andrew Victordf05a302006-10-23 14:50:09 +0200846 if (int_status & AT91_MCI_TXRDY)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100847 pr_debug("Ready to transmit\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100848
Andrew Victordf05a302006-10-23 14:50:09 +0200849 if (int_status & AT91_MCI_RXRDY)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100850 pr_debug("Ready to receive\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100851
852 if (int_status & AT91_MCI_CMDRDY) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100853 pr_debug("Command ready\n");
Nicolas Ferreed99c542007-07-09 14:58:16 +0200854 completed = at91_mci_handle_cmdrdy(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100855 }
856 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100857
858 if (completed) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100859 pr_debug("Completed command\n");
Eric Benard7a6588b2008-05-30 14:26:05 +0200860 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
Nicolas Ferreba7deee2008-05-30 14:28:45 +0200861 at91_mci_completed_command(host, int_status);
Andrew Victordf05a302006-10-23 14:50:09 +0200862 } else
Eric Benard7a6588b2008-05-30 14:26:05 +0200863 at91_mci_write(host, AT91_MCI_IDR, int_status & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
Andrew Victor65dbf342006-04-02 19:18:51 +0100864
865 return IRQ_HANDLED;
866}
867
David Howells7d12e782006-10-05 14:55:46 +0100868static irqreturn_t at91_mmc_det_irq(int irq, void *_host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100869{
870 struct at91mci_host *host = _host;
David Brownell6e996ee2008-02-04 18:12:48 +0100871 int present = !gpio_get_value(irq_to_gpio(irq));
Andrew Victor65dbf342006-04-02 19:18:51 +0100872
873 /*
874 * we expect this irq on both insert and remove,
875 * and use a short delay to debounce.
876 */
877 if (present != host->present) {
878 host->present = present;
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100879 pr_debug("%s: card %s\n", mmc_hostname(host->mmc),
Andrew Victor65dbf342006-04-02 19:18:51 +0100880 present ? "insert" : "remove");
881 if (!present) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100882 pr_debug("****** Resetting SD-card bus width ******\n");
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100883 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100884 }
Wolfgang Mueesa04ac5b2010-03-05 13:43:39 -0800885 /* 0.5s needed because of early card detect switch firing */
886 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
Andrew Victor65dbf342006-04-02 19:18:51 +0100887 }
888 return IRQ_HANDLED;
889}
890
David Brownella26b4982006-12-26 14:45:26 -0800891static int at91_mci_get_ro(struct mmc_host *mmc)
Andrew Victor65dbf342006-04-02 19:18:51 +0100892{
Andrew Victor65dbf342006-04-02 19:18:51 +0100893 struct at91mci_host *host = mmc_priv(mmc);
894
Anton Vorontsov08f80bb2008-06-17 18:17:39 +0400895 if (host->board->wp_pin)
896 return !!gpio_get_value(host->board->wp_pin);
897 /*
898 * Board doesn't support read only detection; let the mmc core
899 * decide what to do.
900 */
901 return -ENOSYS;
Andrew Victor65dbf342006-04-02 19:18:51 +0100902}
903
Eric Benard7a6588b2008-05-30 14:26:05 +0200904static void at91_mci_enable_sdio_irq(struct mmc_host *mmc, int enable)
905{
906 struct at91mci_host *host = mmc_priv(mmc);
907
908 pr_debug("%s: sdio_irq %c : %s\n", mmc_hostname(host->mmc),
909 host->board->slot_b ? 'B':'A', enable ? "enable" : "disable");
910 at91_mci_write(host, enable ? AT91_MCI_IER : AT91_MCI_IDR,
911 host->board->slot_b ? AT91_MCI_SDIOIRQB : AT91_MCI_SDIOIRQA);
912
913}
914
David Brownellab7aefd2006-11-12 17:55:30 -0800915static const struct mmc_host_ops at91_mci_ops = {
Andrew Victor65dbf342006-04-02 19:18:51 +0100916 .request = at91_mci_request,
917 .set_ios = at91_mci_set_ios,
918 .get_ro = at91_mci_get_ro,
Eric Benard7a6588b2008-05-30 14:26:05 +0200919 .enable_sdio_irq = at91_mci_enable_sdio_irq,
Andrew Victor65dbf342006-04-02 19:18:51 +0100920};
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
H Hartley Sweetenaf2a85f2009-12-14 14:10:26 -0500936 if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME))
Andrew Victor17ea0592006-10-23 14:44:40 +0200937 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;
Nicolas Ferre541e7ef2010-03-05 13:43:43 -0800950 mmc->caps = 0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100951
Wolfgang Muees3780d902010-03-05 13:43:40 -0800952 mmc->max_blk_size = MCI_MAXBLKSIZE;
953 mmc->max_blk_count = MCI_BLKATONCE;
954 mmc->max_req_size = MCI_BUFSIZE;
Martin K. Petersena36274e2010-09-10 01:33:59 -0400955 mmc->max_segs = MCI_BLKATONCE;
Wolfgang Muees9af13be2010-03-05 13:43:42 -0800956 mmc->max_seg_size = MCI_BUFSIZE;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100957
Andrew Victor65dbf342006-04-02 19:18:51 +0100958 host = mmc_priv(mmc);
959 host->mmc = mmc;
Andrew Victor65dbf342006-04-02 19:18:51 +0100960 host->bus_mode = 0;
961 host->board = pdev->dev.platform_data;
962 if (host->board->wire4) {
Nicolas Ferre5b27a1a2010-03-05 13:43:44 -0800963 if (at91mci_is_mci1rev2xx())
Nicolas Ferreed99c542007-07-09 14:58:16 +0200964 mmc->caps |= MMC_CAP_4_BIT_DATA;
965 else
David Brownell6e996ee2008-02-04 18:12:48 +0100966 dev_warn(&pdev->dev, "4 wire bus mode not supported"
Nicolas Ferreed99c542007-07-09 14:58:16 +0200967 " - using 1 wire\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100968 }
969
Wolfgang Muees3780d902010-03-05 13:43:40 -0800970 host->buffer = dma_alloc_coherent(&pdev->dev, MCI_BUFSIZE,
971 &host->physical_address, GFP_KERNEL);
972 if (!host->buffer) {
973 ret = -ENOMEM;
974 dev_err(&pdev->dev, "Can't allocate transmit buffer\n");
975 goto fail5;
976 }
977
Nicolas Ferre541e7ef2010-03-05 13:43:43 -0800978 /* Add SDIO capability when available */
Nicolas Ferre5b27a1a2010-03-05 13:43:44 -0800979 if (at91mci_is_mci1rev2xx()) {
980 /* at91mci MCI1 rev2xx sdio interrupt erratum */
Nicolas Ferre541e7ef2010-03-05 13:43:43 -0800981 if (host->board->wire4 || !host->board->slot_b)
982 mmc->caps |= MMC_CAP_SDIO_IRQ;
983 }
984
Andrew Victor65dbf342006-04-02 19:18:51 +0100985 /*
David Brownell6e996ee2008-02-04 18:12:48 +0100986 * Reserve GPIOs ... board init code makes sure these pins are set
987 * up as GPIOs with the right direction (input, except for vcc)
988 */
989 if (host->board->det_pin) {
990 ret = gpio_request(host->board->det_pin, "mmc_detect");
991 if (ret < 0) {
992 dev_dbg(&pdev->dev, "couldn't claim card detect pin\n");
Wolfgang Muees3780d902010-03-05 13:43:40 -0800993 goto fail4b;
David Brownell6e996ee2008-02-04 18:12:48 +0100994 }
995 }
996 if (host->board->wp_pin) {
997 ret = gpio_request(host->board->wp_pin, "mmc_wp");
998 if (ret < 0) {
999 dev_dbg(&pdev->dev, "couldn't claim wp sense pin\n");
1000 goto fail4;
1001 }
1002 }
1003 if (host->board->vcc_pin) {
1004 ret = gpio_request(host->board->vcc_pin, "mmc_vcc");
1005 if (ret < 0) {
1006 dev_dbg(&pdev->dev, "couldn't claim vcc switch pin\n");
1007 goto fail3;
1008 }
1009 }
1010
1011 /*
Andrew Victor65dbf342006-04-02 19:18:51 +01001012 * Get Clock
1013 */
Andrew Victor3dd3b032006-10-23 14:46:54 +02001014 host->mci_clk = clk_get(&pdev->dev, "mci_clk");
1015 if (IS_ERR(host->mci_clk)) {
David Brownell6e996ee2008-02-04 18:12:48 +01001016 ret = -ENODEV;
1017 dev_dbg(&pdev->dev, "no mci_clk?\n");
1018 goto fail2;
Andrew Victor65dbf342006-04-02 19:18:51 +01001019 }
Andrew Victor65dbf342006-04-02 19:18:51 +01001020
Andrew Victor17ea0592006-10-23 14:44:40 +02001021 /*
1022 * Map I/O region
1023 */
H Hartley Sweetenaf2a85f2009-12-14 14:10:26 -05001024 host->baseaddr = ioremap(res->start, resource_size(res));
Andrew Victor17ea0592006-10-23 14:44:40 +02001025 if (!host->baseaddr) {
David Brownell6e996ee2008-02-04 18:12:48 +01001026 ret = -ENOMEM;
1027 goto fail1;
Andrew Victor17ea0592006-10-23 14:44:40 +02001028 }
Andrew Victore0b19b82006-10-25 19:42:38 +02001029
1030 /*
1031 * Reset hardware
1032 */
Andrew Victor3dd3b032006-10-23 14:46:54 +02001033 clk_enable(host->mci_clk); /* Enable the peripheral clock */
Andrew Victore0b19b82006-10-25 19:42:38 +02001034 at91_mci_disable(host);
1035 at91_mci_enable(host);
1036
Andrew Victor65dbf342006-04-02 19:18:51 +01001037 /*
1038 * Allocate the MCI interrupt
1039 */
Andrew Victor17ea0592006-10-23 14:44:40 +02001040 host->irq = platform_get_irq(pdev, 0);
David Brownell6e996ee2008-02-04 18:12:48 +01001041 ret = request_irq(host->irq, at91_mci_irq, IRQF_SHARED,
1042 mmc_hostname(mmc), host);
Andrew Victor65dbf342006-04-02 19:18:51 +01001043 if (ret) {
David Brownell6e996ee2008-02-04 18:12:48 +01001044 dev_dbg(&pdev->dev, "request MCI interrupt failed\n");
1045 goto fail0;
Andrew Victor65dbf342006-04-02 19:18:51 +01001046 }
1047
Nicolas Ferre99ba0402008-11-27 17:23:49 +01001048 setup_timer(&host->timer, at91_timeout_timer, (unsigned long)host);
1049
Andrew Victor65dbf342006-04-02 19:18:51 +01001050 platform_set_drvdata(pdev, mmc);
1051
1052 /*
1053 * Add host to MMC layer
1054 */
Marc Pignat63b66432007-07-16 11:07:02 +02001055 if (host->board->det_pin) {
David Brownell6e996ee2008-02-04 18:12:48 +01001056 host->present = !gpio_get_value(host->board->det_pin);
Marc Pignat63b66432007-07-16 11:07:02 +02001057 }
Andrew Victor65dbf342006-04-02 19:18:51 +01001058 else
1059 host->present = -1;
1060
1061 mmc_add_host(mmc);
1062
1063 /*
1064 * monitor card insertion/removal if we can
1065 */
1066 if (host->board->det_pin) {
David Brownell6e996ee2008-02-04 18:12:48 +01001067 ret = request_irq(gpio_to_irq(host->board->det_pin),
1068 at91_mmc_det_irq, 0, mmc_hostname(mmc), host);
Andrew Victor65dbf342006-04-02 19:18:51 +01001069 if (ret)
David Brownell6e996ee2008-02-04 18:12:48 +01001070 dev_warn(&pdev->dev, "request MMC detect irq failed\n");
1071 else
1072 device_init_wakeup(&pdev->dev, 1);
Andrew Victor65dbf342006-04-02 19:18:51 +01001073 }
1074
Andrew Victorf3a8efa2006-10-23 14:53:20 +02001075 pr_debug("Added MCI driver\n");
Andrew Victor65dbf342006-04-02 19:18:51 +01001076
1077 return 0;
David Brownell6e996ee2008-02-04 18:12:48 +01001078
1079fail0:
1080 clk_disable(host->mci_clk);
1081 iounmap(host->baseaddr);
1082fail1:
1083 clk_put(host->mci_clk);
1084fail2:
1085 if (host->board->vcc_pin)
1086 gpio_free(host->board->vcc_pin);
1087fail3:
1088 if (host->board->wp_pin)
1089 gpio_free(host->board->wp_pin);
1090fail4:
1091 if (host->board->det_pin)
1092 gpio_free(host->board->det_pin);
Wolfgang Muees3780d902010-03-05 13:43:40 -08001093fail4b:
1094 if (host->buffer)
1095 dma_free_coherent(&pdev->dev, MCI_BUFSIZE,
1096 host->buffer, host->physical_address);
David Brownell6e996ee2008-02-04 18:12:48 +01001097fail5:
1098 mmc_free_host(mmc);
1099fail6:
H Hartley Sweetenaf2a85f2009-12-14 14:10:26 -05001100 release_mem_region(res->start, resource_size(res));
David Brownell6e996ee2008-02-04 18:12:48 +01001101 dev_err(&pdev->dev, "probe failed, err %d\n", ret);
1102 return ret;
Andrew Victor65dbf342006-04-02 19:18:51 +01001103}
1104
1105/*
1106 * Remove a device
1107 */
David Brownella26b4982006-12-26 14:45:26 -08001108static int __exit at91_mci_remove(struct platform_device *pdev)
Andrew Victor65dbf342006-04-02 19:18:51 +01001109{
1110 struct mmc_host *mmc = platform_get_drvdata(pdev);
1111 struct at91mci_host *host;
Andrew Victor17ea0592006-10-23 14:44:40 +02001112 struct resource *res;
Andrew Victor65dbf342006-04-02 19:18:51 +01001113
1114 if (!mmc)
1115 return -1;
1116
1117 host = mmc_priv(mmc);
1118
Wolfgang Muees3780d902010-03-05 13:43:40 -08001119 if (host->buffer)
1120 dma_free_coherent(&pdev->dev, MCI_BUFSIZE,
1121 host->buffer, host->physical_address);
1122
Anti Sulline0cda542007-08-30 16:15:16 +02001123 if (host->board->det_pin) {
David Brownell6e996ee2008-02-04 18:12:48 +01001124 if (device_can_wakeup(&pdev->dev))
1125 free_irq(gpio_to_irq(host->board->det_pin), host);
Marc Pignat63b66432007-07-16 11:07:02 +02001126 device_init_wakeup(&pdev->dev, 0);
David Brownell6e996ee2008-02-04 18:12:48 +01001127 gpio_free(host->board->det_pin);
Andrew Victor65dbf342006-04-02 19:18:51 +01001128 }
1129
Andrew Victore0b19b82006-10-25 19:42:38 +02001130 at91_mci_disable(host);
Marc Pignate181dce2008-05-30 14:06:32 +02001131 del_timer_sync(&host->timer);
Andrew Victor17ea0592006-10-23 14:44:40 +02001132 mmc_remove_host(mmc);
1133 free_irq(host->irq, host);
Andrew Victor65dbf342006-04-02 19:18:51 +01001134
Andrew Victor3dd3b032006-10-23 14:46:54 +02001135 clk_disable(host->mci_clk); /* Disable the peripheral clock */
1136 clk_put(host->mci_clk);
Andrew Victor65dbf342006-04-02 19:18:51 +01001137
David Brownell6e996ee2008-02-04 18:12:48 +01001138 if (host->board->vcc_pin)
1139 gpio_free(host->board->vcc_pin);
1140 if (host->board->wp_pin)
1141 gpio_free(host->board->wp_pin);
1142
Andrew Victor17ea0592006-10-23 14:44:40 +02001143 iounmap(host->baseaddr);
1144 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
H Hartley Sweetenaf2a85f2009-12-14 14:10:26 -05001145 release_mem_region(res->start, resource_size(res));
Andrew Victor65dbf342006-04-02 19:18:51 +01001146
Andrew Victor17ea0592006-10-23 14:44:40 +02001147 mmc_free_host(mmc);
1148 platform_set_drvdata(pdev, NULL);
Andrew Victorb44fb7a2006-06-19 13:06:05 +01001149 pr_debug("MCI Removed\n");
Andrew Victor65dbf342006-04-02 19:18:51 +01001150
1151 return 0;
1152}
1153
1154#ifdef CONFIG_PM
1155static int at91_mci_suspend(struct platform_device *pdev, pm_message_t state)
1156{
1157 struct mmc_host *mmc = platform_get_drvdata(pdev);
Marc Pignat63b66432007-07-16 11:07:02 +02001158 struct at91mci_host *host = mmc_priv(mmc);
Andrew Victor65dbf342006-04-02 19:18:51 +01001159 int ret = 0;
1160
Anti Sulline0cda542007-08-30 16:15:16 +02001161 if (host->board->det_pin && device_may_wakeup(&pdev->dev))
Marc Pignat63b66432007-07-16 11:07:02 +02001162 enable_irq_wake(host->board->det_pin);
1163
Andrew Victor65dbf342006-04-02 19:18:51 +01001164 if (mmc)
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001165 ret = mmc_suspend_host(mmc);
Andrew Victor65dbf342006-04-02 19:18:51 +01001166
1167 return ret;
1168}
1169
1170static int at91_mci_resume(struct platform_device *pdev)
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 disable_irq_wake(host->board->det_pin);
1178
Andrew Victor65dbf342006-04-02 19:18:51 +01001179 if (mmc)
1180 ret = mmc_resume_host(mmc);
1181
1182 return ret;
1183}
1184#else
1185#define at91_mci_suspend NULL
1186#define at91_mci_resume NULL
1187#endif
1188
1189static struct platform_driver at91_mci_driver = {
David Brownella26b4982006-12-26 14:45:26 -08001190 .remove = __exit_p(at91_mci_remove),
Andrew Victor65dbf342006-04-02 19:18:51 +01001191 .suspend = at91_mci_suspend,
1192 .resume = at91_mci_resume,
1193 .driver = {
1194 .name = DRIVER_NAME,
1195 .owner = THIS_MODULE,
1196 },
1197};
1198
1199static int __init at91_mci_init(void)
1200{
David Brownella26b4982006-12-26 14:45:26 -08001201 return platform_driver_probe(&at91_mci_driver, at91_mci_probe);
Andrew Victor65dbf342006-04-02 19:18:51 +01001202}
1203
1204static void __exit at91_mci_exit(void)
1205{
1206 platform_driver_unregister(&at91_mci_driver);
1207}
1208
1209module_init(at91_mci_init);
1210module_exit(at91_mci_exit);
1211
1212MODULE_DESCRIPTION("AT91 Multimedia Card Interface driver");
1213MODULE_AUTHOR("Nick Randell");
1214MODULE_LICENSE("GPL");
Kay Sieversbc65c722008-04-15 14:34:28 -07001215MODULE_ALIAS("platform:at91_mci");