]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/mmc/host/at91_mci.c
s3cmci: s3cmci_card_present: Use no_detect to decide whether there is a card detect pin
[net-next-2.6.git] / drivers / mmc / host / at91_mci.c
CommitLineData
65dbf343 1/*
70f10482 2 * linux/drivers/mmc/host/at91_mci.c - ATMEL AT91 MCI Driver
65dbf343
AV
3 *
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/*
99eeb8df 14 This is the AT91 MCI driver that has been tested with both MMC cards
65dbf343
AV
15 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.
99eeb8df
AV
41 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.
65dbf343
AV
43
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
65dbf343
AV
56#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>
93a3ddc2 67#include <linux/atmel_pdc.h>
65dbf343
AV
68
69#include <linux/mmc/host.h>
65dbf343
AV
70
71#include <asm/io.h>
72#include <asm/irq.h>
6e996ee8
DB
73#include <asm/gpio.h>
74
a09e64fb
RK
75#include <mach/board.h>
76#include <mach/cpu.h>
77#include <mach/at91_mci.h>
65dbf343
AV
78
79#define DRIVER_NAME "at91_mci"
80
df05a303
AV
81#define FL_SENT_COMMAND (1 << 0)
82#define FL_SENT_STOP (1 << 1)
65dbf343 83
df05a303
AV
84#define AT91_MCI_ERRORS (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE \
85 | AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE \
37b758e8 86 | AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)
65dbf343 87
e0b19b83
AV
88#define at91_mci_read(host, reg) __raw_readl((host)->baseaddr + (reg))
89#define at91_mci_write(host, reg, val) __raw_writel((val), (host)->baseaddr + (reg))
65dbf343 90
65dbf343
AV
91
92/*
93 * Low level type for this driver
94 */
95struct at91mci_host
96{
97 struct mmc_host *mmc;
98 struct mmc_command *cmd;
99 struct mmc_request *request;
100
e0b19b83 101 void __iomem *baseaddr;
17ea0595 102 int irq;
e0b19b83 103
65dbf343
AV
104 struct at91_mmc_data *board;
105 int present;
106
3dd3b039
AV
107 struct clk *mci_clk;
108
65dbf343
AV
109 /*
110 * Flag indicating when the command has been sent. This is used to
111 * work out whether or not to send the stop
112 */
113 unsigned int flags;
114 /* flag for current bus settings */
115 u32 bus_mode;
116
117 /* DMA buffer used for transmitting */
118 unsigned int* buffer;
119 dma_addr_t physical_address;
120 unsigned int total_length;
121
122 /* Latest in the scatterlist that has been enabled for transfer, but not freed */
123 int in_use_index;
124
125 /* Latest in the scatterlist that has been enabled for transfer */
126 int transfer_index;
e181dce8
MP
127
128 /* Timer for timeouts */
129 struct timer_list timer;
65dbf343
AV
130};
131
c5a89c6c
MP
132/*
133 * Reset the controller and restore most of the state
134 */
135static void at91_reset_host(struct at91mci_host *host)
136{
137 unsigned long flags;
138 u32 mr;
139 u32 sdcr;
140 u32 dtor;
141 u32 imr;
142
143 local_irq_save(flags);
144 imr = at91_mci_read(host, AT91_MCI_IMR);
145
146 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
147
148 /* save current state */
149 mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff;
150 sdcr = at91_mci_read(host, AT91_MCI_SDCR);
151 dtor = at91_mci_read(host, AT91_MCI_DTOR);
152
153 /* reset the controller */
154 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
155
156 /* restore state */
157 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
158 at91_mci_write(host, AT91_MCI_MR, mr);
159 at91_mci_write(host, AT91_MCI_SDCR, sdcr);
160 at91_mci_write(host, AT91_MCI_DTOR, dtor);
161 at91_mci_write(host, AT91_MCI_IER, imr);
162
163 /* make sure sdio interrupts will fire */
164 at91_mci_read(host, AT91_MCI_SR);
165
166 local_irq_restore(flags);
167}
168
e181dce8
MP
169static void at91_timeout_timer(unsigned long data)
170{
171 struct at91mci_host *host;
172
173 host = (struct at91mci_host *)data;
174
175 if (host->request) {
176 dev_err(host->mmc->parent, "Timeout waiting end of packet\n");
177
178 if (host->cmd && host->cmd->data) {
179 host->cmd->data->error = -ETIMEDOUT;
180 } else {
181 if (host->cmd)
182 host->cmd->error = -ETIMEDOUT;
183 else
184 host->request->cmd->error = -ETIMEDOUT;
185 }
186
c5a89c6c 187 at91_reset_host(host);
e181dce8
MP
188 mmc_request_done(host->mmc, host->request);
189 }
190}
191
65dbf343
AV
192/*
193 * Copy from sg to a dma block - used for transfers
194 */
e8d04d3d 195static inline void at91_mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data)
65dbf343
AV
196{
197 unsigned int len, i, size;
198 unsigned *dmabuf = host->buffer;
199
5385edc5 200 size = data->blksz * data->blocks;
65dbf343
AV
201 len = data->sg_len;
202
5385edc5
VS
203 /* AT91SAM926[0/3] Data Write Operation and number of bytes erratum */
204 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
205 if (host->total_length == 12)
206 memset(dmabuf, 0, 12);
207
65dbf343
AV
208 /*
209 * Just loop through all entries. Size might not
210 * be the entire list though so make sure that
211 * we do not transfer too much.
212 */
213 for (i = 0; i < len; i++) {
214 struct scatterlist *sg;
215 int amount;
65dbf343
AV
216 unsigned int *sgbuffer;
217
218 sg = &data->sg[i];
219
45711f1a 220 sgbuffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
65dbf343
AV
221 amount = min(size, sg->length);
222 size -= amount;
65dbf343 223
99eeb8df
AV
224 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
225 int index;
226
227 for (index = 0; index < (amount / 4); index++)
228 *dmabuf++ = swab32(sgbuffer[index]);
5385edc5 229 } else {
99eeb8df 230 memcpy(dmabuf, sgbuffer, amount);
5385edc5
VS
231 dmabuf += amount;
232 }
65dbf343
AV
233
234 kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
235
236 if (size == 0)
237 break;
238 }
239
240 /*
241 * Check that we didn't get a request to transfer
242 * more data than can fit into the SG list.
243 */
244 BUG_ON(size != 0);
245}
246
247/*
248 * Prepare a dma read
249 */
e8d04d3d 250static void at91_mci_pre_dma_read(struct at91mci_host *host)
65dbf343
AV
251{
252 int i;
253 struct scatterlist *sg;
254 struct mmc_command *cmd;
255 struct mmc_data *data;
256
b44fb7a0 257 pr_debug("pre dma read\n");
65dbf343
AV
258
259 cmd = host->cmd;
260 if (!cmd) {
b44fb7a0 261 pr_debug("no command\n");
65dbf343
AV
262 return;
263 }
264
265 data = cmd->data;
266 if (!data) {
b44fb7a0 267 pr_debug("no data\n");
65dbf343
AV
268 return;
269 }
270
271 for (i = 0; i < 2; i++) {
272 /* nothing left to transfer */
273 if (host->transfer_index >= data->sg_len) {
b44fb7a0 274 pr_debug("Nothing left to transfer (index = %d)\n", host->transfer_index);
65dbf343
AV
275 break;
276 }
277
278 /* Check to see if this needs filling */
279 if (i == 0) {
93a3ddc2 280 if (at91_mci_read(host, ATMEL_PDC_RCR) != 0) {
b44fb7a0 281 pr_debug("Transfer active in current\n");
65dbf343
AV
282 continue;
283 }
284 }
285 else {
93a3ddc2 286 if (at91_mci_read(host, ATMEL_PDC_RNCR) != 0) {
b44fb7a0 287 pr_debug("Transfer active in next\n");
65dbf343
AV
288 continue;
289 }
290 }
291
292 /* Setup the next transfer */
b44fb7a0 293 pr_debug("Using transfer index %d\n", host->transfer_index);
65dbf343
AV
294
295 sg = &data->sg[host->transfer_index++];
b44fb7a0 296 pr_debug("sg = %p\n", sg);
65dbf343 297
45711f1a 298 sg->dma_address = dma_map_page(NULL, sg_page(sg), sg->offset, sg->length, DMA_FROM_DEVICE);
65dbf343 299
b44fb7a0 300 pr_debug("dma address = %08X, length = %d\n", sg->dma_address, sg->length);
65dbf343
AV
301
302 if (i == 0) {
93a3ddc2 303 at91_mci_write(host, ATMEL_PDC_RPR, sg->dma_address);
80f92546 304 at91_mci_write(host, ATMEL_PDC_RCR, (data->blksz & 0x3) ? sg->length : sg->length / 4);
65dbf343
AV
305 }
306 else {
93a3ddc2 307 at91_mci_write(host, ATMEL_PDC_RNPR, sg->dma_address);
80f92546 308 at91_mci_write(host, ATMEL_PDC_RNCR, (data->blksz & 0x3) ? sg->length : sg->length / 4);
65dbf343
AV
309 }
310 }
311
b44fb7a0 312 pr_debug("pre dma read done\n");
65dbf343
AV
313}
314
315/*
316 * Handle after a dma read
317 */
e8d04d3d 318static void at91_mci_post_dma_read(struct at91mci_host *host)
65dbf343
AV
319{
320 struct mmc_command *cmd;
321 struct mmc_data *data;
322
b44fb7a0 323 pr_debug("post dma read\n");
65dbf343
AV
324
325 cmd = host->cmd;
326 if (!cmd) {
b44fb7a0 327 pr_debug("no command\n");
65dbf343
AV
328 return;
329 }
330
331 data = cmd->data;
332 if (!data) {
b44fb7a0 333 pr_debug("no data\n");
65dbf343
AV
334 return;
335 }
336
337 while (host->in_use_index < host->transfer_index) {
65dbf343
AV
338 struct scatterlist *sg;
339
b44fb7a0 340 pr_debug("finishing index %d\n", host->in_use_index);
65dbf343
AV
341
342 sg = &data->sg[host->in_use_index++];
343
b44fb7a0 344 pr_debug("Unmapping page %08X\n", sg->dma_address);
65dbf343
AV
345
346 dma_unmap_page(NULL, sg->dma_address, sg->length, DMA_FROM_DEVICE);
347
99eeb8df 348 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
ed99c541 349 unsigned int *buffer;
99eeb8df 350 int index;
65dbf343 351
ed99c541 352 /* Swap the contents of the buffer */
45711f1a 353 buffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
ed99c541
NF
354 pr_debug("buffer = %p, length = %d\n", buffer, sg->length);
355
99eeb8df
AV
356 for (index = 0; index < (sg->length / 4); index++)
357 buffer[index] = swab32(buffer[index]);
ed99c541
NF
358
359 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
65dbf343 360 }
99eeb8df 361
45711f1a 362 flush_dcache_page(sg_page(sg));
4ac24a87
NF
363
364 data->bytes_xfered += sg->length;
65dbf343
AV
365 }
366
367 /* Is there another transfer to trigger? */
368 if (host->transfer_index < data->sg_len)
e8d04d3d 369 at91_mci_pre_dma_read(host);
65dbf343 370 else {
ed99c541 371 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_ENDRX);
e0b19b83 372 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_RXBUFF);
65dbf343
AV
373 }
374
b44fb7a0 375 pr_debug("post dma read done\n");
65dbf343
AV
376}
377
378/*
379 * Handle transmitted data
380 */
381static void at91_mci_handle_transmitted(struct at91mci_host *host)
382{
383 struct mmc_command *cmd;
384 struct mmc_data *data;
385
b44fb7a0 386 pr_debug("Handling the transmit\n");
65dbf343
AV
387
388 /* Disable the transfer */
93a3ddc2 389 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
65dbf343
AV
390
391 /* Now wait for cmd ready */
e0b19b83 392 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE);
65dbf343
AV
393
394 cmd = host->cmd;
395 if (!cmd) return;
396
397 data = cmd->data;
398 if (!data) return;
399
be0192aa 400 if (cmd->data->blocks > 1) {
ed99c541
NF
401 pr_debug("multiple write : wait for BLKE...\n");
402 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
403 } else
404 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
4ac24a87
NF
405}
406
407/*
408 * Update bytes tranfered count during a write operation
409 */
410static void at91_mci_update_bytes_xfered(struct at91mci_host *host)
411{
412 struct mmc_data *data;
ed99c541 413
4ac24a87
NF
414 /* always deal with the effective request (and not the current cmd) */
415
416 if (host->request->cmd && host->request->cmd->error != 0)
417 return;
418
419 if (host->request->data) {
420 data = host->request->data;
421 if (data->flags & MMC_DATA_WRITE) {
422 /* card is in IDLE mode now */
423 pr_debug("-> bytes_xfered %d, total_length = %d\n",
424 data->bytes_xfered, host->total_length);
5385edc5 425 data->bytes_xfered = data->blksz * data->blocks;
4ac24a87
NF
426 }
427 }
65dbf343
AV
428}
429
4ac24a87 430
ed99c541
NF
431/*Handle after command sent ready*/
432static int at91_mci_handle_cmdrdy(struct at91mci_host *host)
433{
434 if (!host->cmd)
435 return 1;
436 else if (!host->cmd->data) {
437 if (host->flags & FL_SENT_STOP) {
438 /*After multi block write, we must wait for NOTBUSY*/
439 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
440 } else return 1;
441 } else if (host->cmd->data->flags & MMC_DATA_WRITE) {
442 /*After sendding multi-block-write command, start DMA transfer*/
4ac24a87 443 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_TXBUFE | AT91_MCI_BLKE);
ed99c541
NF
444 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
445 }
446
447 /* command not completed, have to wait */
448 return 0;
449}
450
451
65dbf343
AV
452/*
453 * Enable the controller
454 */
e0b19b83 455static void at91_mci_enable(struct at91mci_host *host)
65dbf343 456{
ed99c541
NF
457 unsigned int mr;
458
e0b19b83 459 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
f3a8efa9 460 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
e0b19b83 461 at91_mci_write(host, AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
ed99c541
NF
462 mr = AT91_MCI_PDCMODE | 0x34a;
463
464 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
465 mr |= AT91_MCI_RDPROOF | AT91_MCI_WRPROOF;
466
467 at91_mci_write(host, AT91_MCI_MR, mr);
99eeb8df
AV
468
469 /* use Slot A or B (only one at same time) */
470 at91_mci_write(host, AT91_MCI_SDCR, host->board->slot_b);
65dbf343
AV
471}
472
473/*
474 * Disable the controller
475 */
e0b19b83 476static void at91_mci_disable(struct at91mci_host *host)
65dbf343 477{
e0b19b83 478 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
65dbf343
AV
479}
480
481/*
482 * Send a command
65dbf343 483 */
ed99c541 484static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd)
65dbf343
AV
485{
486 unsigned int cmdr, mr;
487 unsigned int block_length;
488 struct mmc_data *data = cmd->data;
489
490 unsigned int blocks;
491 unsigned int ier = 0;
492
493 host->cmd = cmd;
494
ed99c541 495 /* Needed for leaving busy state before CMD1 */
e0b19b83 496 if ((at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {
b44fb7a0 497 pr_debug("Clearing timeout\n");
e0b19b83
AV
498 at91_mci_write(host, AT91_MCI_ARGR, 0);
499 at91_mci_write(host, AT91_MCI_CMDR, AT91_MCI_OPDCMD);
500 while (!(at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_CMDRDY)) {
65dbf343 501 /* spin */
e0b19b83 502 pr_debug("Clearing: SR = %08X\n", at91_mci_read(host, AT91_MCI_SR));
65dbf343
AV
503 }
504 }
ed99c541 505
65dbf343
AV
506 cmdr = cmd->opcode;
507
508 if (mmc_resp_type(cmd) == MMC_RSP_NONE)
509 cmdr |= AT91_MCI_RSPTYP_NONE;
510 else {
511 /* if a response is expected then allow maximum response latancy */
512 cmdr |= AT91_MCI_MAXLAT;
513 /* set 136 bit response for R2, 48 bit response otherwise */
514 if (mmc_resp_type(cmd) == MMC_RSP_R2)
515 cmdr |= AT91_MCI_RSPTYP_136;
516 else
517 cmdr |= AT91_MCI_RSPTYP_48;
518 }
519
520 if (data) {
1d4de9ed 521
9da3cbaf
VS
522 if (cpu_is_at91rm9200() || cpu_is_at91sam9261()) {
523 if (data->blksz & 0x3) {
524 pr_debug("Unsupported block size\n");
525 cmd->error = -EINVAL;
526 mmc_request_done(host->mmc, host->request);
527 return;
528 }
529 if (data->flags & MMC_DATA_STREAM) {
530 pr_debug("Stream commands not supported\n");
531 cmd->error = -EINVAL;
532 mmc_request_done(host->mmc, host->request);
533 return;
534 }
1d4de9ed
MP
535 }
536
a3fd4a1b 537 block_length = data->blksz;
65dbf343
AV
538 blocks = data->blocks;
539
540 /* always set data start - also set direction flag for read */
541 if (data->flags & MMC_DATA_READ)
542 cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START);
543 else if (data->flags & MMC_DATA_WRITE)
544 cmdr |= AT91_MCI_TRCMD_START;
545
546 if (data->flags & MMC_DATA_STREAM)
547 cmdr |= AT91_MCI_TRTYP_STREAM;
be0192aa 548 if (data->blocks > 1)
65dbf343
AV
549 cmdr |= AT91_MCI_TRTYP_MULTIPLE;
550 }
551 else {
552 block_length = 0;
553 blocks = 0;
554 }
555
b6cedb38 556 if (host->flags & FL_SENT_STOP)
65dbf343
AV
557 cmdr |= AT91_MCI_TRCMD_STOP;
558
559 if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
560 cmdr |= AT91_MCI_OPDCMD;
561
562 /*
563 * Set the arguments and send the command
564 */
f3a8efa9 565 pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",
e0b19b83 566 cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(host, AT91_MCI_MR));
65dbf343
AV
567
568 if (!data) {
93a3ddc2
AV
569 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS | ATMEL_PDC_RXTDIS);
570 at91_mci_write(host, ATMEL_PDC_RPR, 0);
571 at91_mci_write(host, ATMEL_PDC_RCR, 0);
572 at91_mci_write(host, ATMEL_PDC_RNPR, 0);
573 at91_mci_write(host, ATMEL_PDC_RNCR, 0);
574 at91_mci_write(host, ATMEL_PDC_TPR, 0);
575 at91_mci_write(host, ATMEL_PDC_TCR, 0);
576 at91_mci_write(host, ATMEL_PDC_TNPR, 0);
577 at91_mci_write(host, ATMEL_PDC_TNCR, 0);
ed99c541
NF
578 ier = AT91_MCI_CMDRDY;
579 } else {
580 /* zero block length and PDC mode */
12bd2575 581 mr = at91_mci_read(host, AT91_MCI_MR) & 0x5fff;
80f92546
MP
582 mr |= (data->blksz & 0x3) ? AT91_MCI_PDCFBYTE : 0;
583 mr |= (block_length << 16);
584 mr |= AT91_MCI_PDCMODE;
585 at91_mci_write(host, AT91_MCI_MR, mr);
e0b19b83 586
9da3cbaf 587 if (!(cpu_is_at91rm9200() || cpu_is_at91sam9261()))
c5a89c6c
MP
588 at91_mci_write(host, AT91_MCI_BLKR,
589 AT91_MCI_BLKR_BCNT(blocks) |
590 AT91_MCI_BLKR_BLKLEN(block_length));
591
ed99c541
NF
592 /*
593 * Disable the PDC controller
594 */
595 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
65dbf343 596
ed99c541
NF
597 if (cmdr & AT91_MCI_TRCMD_START) {
598 data->bytes_xfered = 0;
599 host->transfer_index = 0;
600 host->in_use_index = 0;
601 if (cmdr & AT91_MCI_TRDIR) {
602 /*
603 * Handle a read
604 */
605 host->buffer = NULL;
606 host->total_length = 0;
607
608 at91_mci_pre_dma_read(host);
609 ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
610 }
611 else {
612 /*
613 * Handle a write
614 */
615 host->total_length = block_length * blocks;
5385edc5
VS
616 /*
617 * AT91SAM926[0/3] Data Write Operation and
618 * number of bytes erratum
619 */
620 if (cpu_is_at91sam9260 () || cpu_is_at91sam9263())
621 if (host->total_length < 12)
622 host->total_length = 12;
e385ea63
DB
623
624 host->buffer = kmalloc(host->total_length, GFP_KERNEL);
625 if (!host->buffer) {
626 pr_debug("Can't alloc tx buffer\n");
627 cmd->error = -ENOMEM;
628 mmc_request_done(host->mmc, host->request);
629 return;
630 }
ed99c541
NF
631
632 at91_mci_sg_to_dma(host, data);
633
e385ea63
DB
634 host->physical_address = dma_map_single(NULL,
635 host->buffer, host->total_length,
636 DMA_TO_DEVICE);
637
ed99c541
NF
638 pr_debug("Transmitting %d bytes\n", host->total_length);
639
640 at91_mci_write(host, ATMEL_PDC_TPR, host->physical_address);
80f92546
MP
641 at91_mci_write(host, ATMEL_PDC_TCR, (data->blksz & 0x3) ?
642 host->total_length : host->total_length / 4);
643
ed99c541
NF
644 ier = AT91_MCI_CMDRDY;
645 }
65dbf343
AV
646 }
647 }
648
649 /*
650 * Send the command and then enable the PDC - not the other way round as
651 * the data sheet says
652 */
653
e0b19b83
AV
654 at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
655 at91_mci_write(host, AT91_MCI_CMDR, cmdr);
65dbf343
AV
656
657 if (cmdr & AT91_MCI_TRCMD_START) {
658 if (cmdr & AT91_MCI_TRDIR)
93a3ddc2 659 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
65dbf343 660 }
65dbf343 661
ed99c541 662 /* Enable selected interrupts */
df05a303 663 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_ERRORS | ier);
65dbf343
AV
664}
665
666/*
667 * Process the next step in the request
668 */
e8d04d3d 669static void at91_mci_process_next(struct at91mci_host *host)
65dbf343
AV
670{
671 if (!(host->flags & FL_SENT_COMMAND)) {
672 host->flags |= FL_SENT_COMMAND;
ed99c541 673 at91_mci_send_command(host, host->request->cmd);
65dbf343
AV
674 }
675 else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) {
676 host->flags |= FL_SENT_STOP;
ed99c541 677 at91_mci_send_command(host, host->request->stop);
e181dce8
MP
678 } else {
679 del_timer(&host->timer);
c5a89c6c
MP
680 /* the at91rm9200 mci controller hangs after some transfers,
681 * and the workaround is to reset it after each transfer.
682 */
683 if (cpu_is_at91rm9200())
684 at91_reset_host(host);
65dbf343 685 mmc_request_done(host->mmc, host->request);
e181dce8 686 }
65dbf343
AV
687}
688
689/*
690 * Handle a command that has been completed
691 */
ba7deeed 692static void at91_mci_completed_command(struct at91mci_host *host, unsigned int status)
65dbf343
AV
693{
694 struct mmc_command *cmd = host->cmd;
fa1fe010 695 struct mmc_data *data = cmd->data;
65dbf343 696
7a6588ba 697 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
65dbf343 698
e0b19b83
AV
699 cmd->resp[0] = at91_mci_read(host, AT91_MCI_RSPR(0));
700 cmd->resp[1] = at91_mci_read(host, AT91_MCI_RSPR(1));
701 cmd->resp[2] = at91_mci_read(host, AT91_MCI_RSPR(2));
702 cmd->resp[3] = at91_mci_read(host, AT91_MCI_RSPR(3));
65dbf343
AV
703
704 if (host->buffer) {
e385ea63
DB
705 dma_unmap_single(NULL,
706 host->physical_address, host->total_length,
707 DMA_TO_DEVICE);
708 kfree(host->buffer);
65dbf343
AV
709 host->buffer = NULL;
710 }
711
ba7deeed
NF
712 pr_debug("Status = %08X/%08x [%08X %08X %08X %08X]\n",
713 status, at91_mci_read(host, AT91_MCI_SR),
714 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
65dbf343 715
9e3866b5 716 if (status & AT91_MCI_ERRORS) {
b6cedb38 717 if ((status & AT91_MCI_RCRCE) && !(mmc_resp_type(cmd) & MMC_RSP_CRC)) {
17b0429d 718 cmd->error = 0;
65dbf343
AV
719 }
720 else {
fa1fe010
NF
721 if (status & (AT91_MCI_DTOE | AT91_MCI_DCRCE)) {
722 if (data) {
723 if (status & AT91_MCI_DTOE)
724 data->error = -ETIMEDOUT;
725 else if (status & AT91_MCI_DCRCE)
726 data->error = -EILSEQ;
727 }
728 } else {
729 if (status & AT91_MCI_RTOE)
730 cmd->error = -ETIMEDOUT;
731 else if (status & AT91_MCI_RCRCE)
732 cmd->error = -EILSEQ;
733 else
734 cmd->error = -EIO;
735 }
65dbf343 736
fa1fe010
NF
737 pr_debug("Error detected and set to %d/%d (cmd = %d, retries = %d)\n",
738 cmd->error, data ? data->error : 0,
739 cmd->opcode, cmd->retries);
65dbf343
AV
740 }
741 }
742 else
17b0429d 743 cmd->error = 0;
65dbf343 744
e8d04d3d 745 at91_mci_process_next(host);
65dbf343
AV
746}
747
748/*
749 * Handle an MMC request
750 */
751static void at91_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
752{
753 struct at91mci_host *host = mmc_priv(mmc);
754 host->request = mrq;
755 host->flags = 0;
756
e181dce8
MP
757 mod_timer(&host->timer, jiffies + HZ);
758
e8d04d3d 759 at91_mci_process_next(host);
65dbf343
AV
760}
761
762/*
763 * Set the IOS
764 */
765static void at91_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
766{
767 int clkdiv;
768 struct at91mci_host *host = mmc_priv(mmc);
3dd3b039 769 unsigned long at91_master_clock = clk_get_rate(host->mci_clk);
65dbf343 770
b44fb7a0 771 host->bus_mode = ios->bus_mode;
65dbf343
AV
772
773 if (ios->clock == 0) {
774 /* Disable the MCI controller */
e0b19b83 775 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS);
65dbf343
AV
776 clkdiv = 0;
777 }
778 else {
779 /* Enable the MCI controller */
e0b19b83 780 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
65dbf343
AV
781
782 if ((at91_master_clock % (ios->clock * 2)) == 0)
783 clkdiv = ((at91_master_clock / ios->clock) / 2) - 1;
784 else
785 clkdiv = (at91_master_clock / ios->clock) / 2;
786
b44fb7a0 787 pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv,
65dbf343
AV
788 at91_master_clock / (2 * (clkdiv + 1)));
789 }
790 if (ios->bus_width == MMC_BUS_WIDTH_4 && host->board->wire4) {
b44fb7a0 791 pr_debug("MMC: Setting controller bus width to 4\n");
e0b19b83 792 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) | AT91_MCI_SDCBUS);
65dbf343
AV
793 }
794 else {
b44fb7a0 795 pr_debug("MMC: Setting controller bus width to 1\n");
e0b19b83 796 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
65dbf343
AV
797 }
798
799 /* Set the clock divider */
e0b19b83 800 at91_mci_write(host, AT91_MCI_MR, (at91_mci_read(host, AT91_MCI_MR) & ~AT91_MCI_CLKDIV) | clkdiv);
65dbf343
AV
801
802 /* maybe switch power to the card */
b44fb7a0 803 if (host->board->vcc_pin) {
65dbf343
AV
804 switch (ios->power_mode) {
805 case MMC_POWER_OFF:
6e996ee8 806 gpio_set_value(host->board->vcc_pin, 0);
65dbf343
AV
807 break;
808 case MMC_POWER_UP:
6e996ee8 809 gpio_set_value(host->board->vcc_pin, 1);
65dbf343 810 break;
e5c0ef90
MP
811 case MMC_POWER_ON:
812 break;
813 default:
814 WARN_ON(1);
65dbf343
AV
815 }
816 }
817}
818
819/*
820 * Handle an interrupt
821 */
7d12e780 822static irqreturn_t at91_mci_irq(int irq, void *devid)
65dbf343
AV
823{
824 struct at91mci_host *host = devid;
825 int completed = 0;
df05a303 826 unsigned int int_status, int_mask;
65dbf343 827
e0b19b83 828 int_status = at91_mci_read(host, AT91_MCI_SR);
df05a303 829 int_mask = at91_mci_read(host, AT91_MCI_IMR);
37b758e8 830
f3a8efa9 831 pr_debug("MCI irq: status = %08X, %08X, %08X\n", int_status, int_mask,
df05a303 832 int_status & int_mask);
37b758e8 833
df05a303
AV
834 int_status = int_status & int_mask;
835
836 if (int_status & AT91_MCI_ERRORS) {
65dbf343 837 completed = 1;
37b758e8 838
df05a303
AV
839 if (int_status & AT91_MCI_UNRE)
840 pr_debug("MMC: Underrun error\n");
841 if (int_status & AT91_MCI_OVRE)
842 pr_debug("MMC: Overrun error\n");
843 if (int_status & AT91_MCI_DTOE)
844 pr_debug("MMC: Data timeout\n");
845 if (int_status & AT91_MCI_DCRCE)
846 pr_debug("MMC: CRC error in data\n");
847 if (int_status & AT91_MCI_RTOE)
848 pr_debug("MMC: Response timeout\n");
849 if (int_status & AT91_MCI_RENDE)
850 pr_debug("MMC: Response end bit error\n");
851 if (int_status & AT91_MCI_RCRCE)
852 pr_debug("MMC: Response CRC error\n");
853 if (int_status & AT91_MCI_RDIRE)
854 pr_debug("MMC: Response direction error\n");
855 if (int_status & AT91_MCI_RINDE)
856 pr_debug("MMC: Response index error\n");
857 } else {
858 /* Only continue processing if no errors */
65dbf343 859
65dbf343 860 if (int_status & AT91_MCI_TXBUFE) {
b44fb7a0 861 pr_debug("TX buffer empty\n");
65dbf343
AV
862 at91_mci_handle_transmitted(host);
863 }
864
ed99c541
NF
865 if (int_status & AT91_MCI_ENDRX) {
866 pr_debug("ENDRX\n");
867 at91_mci_post_dma_read(host);
868 }
869
65dbf343 870 if (int_status & AT91_MCI_RXBUFF) {
b44fb7a0 871 pr_debug("RX buffer full\n");
ed99c541
NF
872 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
873 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_RXBUFF | AT91_MCI_ENDRX);
874 completed = 1;
65dbf343
AV
875 }
876
df05a303 877 if (int_status & AT91_MCI_ENDTX)
b44fb7a0 878 pr_debug("Transmit has ended\n");
65dbf343 879
65dbf343 880 if (int_status & AT91_MCI_NOTBUSY) {
b44fb7a0 881 pr_debug("Card is ready\n");
4ac24a87 882 at91_mci_update_bytes_xfered(host);
ed99c541 883 completed = 1;
65dbf343
AV
884 }
885
df05a303 886 if (int_status & AT91_MCI_DTIP)
b44fb7a0 887 pr_debug("Data transfer in progress\n");
65dbf343 888
ed99c541 889 if (int_status & AT91_MCI_BLKE) {
b44fb7a0 890 pr_debug("Block transfer has ended\n");
4ac24a87
NF
891 if (host->request->data && host->request->data->blocks > 1) {
892 /* multi block write : complete multi write
893 * command and send stop */
894 completed = 1;
895 } else {
896 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
897 }
ed99c541 898 }
65dbf343 899
7a6588ba
EB
900 if (int_status & AT91_MCI_SDIOIRQA)
901 mmc_signal_sdio_irq(host->mmc);
902
903 if (int_status & AT91_MCI_SDIOIRQB)
904 mmc_signal_sdio_irq(host->mmc);
905
df05a303 906 if (int_status & AT91_MCI_TXRDY)
b44fb7a0 907 pr_debug("Ready to transmit\n");
65dbf343 908
df05a303 909 if (int_status & AT91_MCI_RXRDY)
b44fb7a0 910 pr_debug("Ready to receive\n");
65dbf343
AV
911
912 if (int_status & AT91_MCI_CMDRDY) {
b44fb7a0 913 pr_debug("Command ready\n");
ed99c541 914 completed = at91_mci_handle_cmdrdy(host);
65dbf343
AV
915 }
916 }
65dbf343
AV
917
918 if (completed) {
b44fb7a0 919 pr_debug("Completed command\n");
7a6588ba 920 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
ba7deeed 921 at91_mci_completed_command(host, int_status);
df05a303 922 } else
7a6588ba 923 at91_mci_write(host, AT91_MCI_IDR, int_status & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
65dbf343
AV
924
925 return IRQ_HANDLED;
926}
927
7d12e780 928static irqreturn_t at91_mmc_det_irq(int irq, void *_host)
65dbf343
AV
929{
930 struct at91mci_host *host = _host;
6e996ee8 931 int present = !gpio_get_value(irq_to_gpio(irq));
65dbf343
AV
932
933 /*
934 * we expect this irq on both insert and remove,
935 * and use a short delay to debounce.
936 */
937 if (present != host->present) {
938 host->present = present;
b44fb7a0 939 pr_debug("%s: card %s\n", mmc_hostname(host->mmc),
65dbf343
AV
940 present ? "insert" : "remove");
941 if (!present) {
b44fb7a0 942 pr_debug("****** Resetting SD-card bus width ******\n");
99eeb8df 943 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
65dbf343
AV
944 }
945 mmc_detect_change(host->mmc, msecs_to_jiffies(100));
946 }
947 return IRQ_HANDLED;
948}
949
a26b498c 950static int at91_mci_get_ro(struct mmc_host *mmc)
65dbf343 951{
65dbf343
AV
952 struct at91mci_host *host = mmc_priv(mmc);
953
08f80bb5
AV
954 if (host->board->wp_pin)
955 return !!gpio_get_value(host->board->wp_pin);
956 /*
957 * Board doesn't support read only detection; let the mmc core
958 * decide what to do.
959 */
960 return -ENOSYS;
65dbf343
AV
961}
962
7a6588ba
EB
963static void at91_mci_enable_sdio_irq(struct mmc_host *mmc, int enable)
964{
965 struct at91mci_host *host = mmc_priv(mmc);
966
967 pr_debug("%s: sdio_irq %c : %s\n", mmc_hostname(host->mmc),
968 host->board->slot_b ? 'B':'A', enable ? "enable" : "disable");
969 at91_mci_write(host, enable ? AT91_MCI_IER : AT91_MCI_IDR,
970 host->board->slot_b ? AT91_MCI_SDIOIRQB : AT91_MCI_SDIOIRQA);
971
972}
973
ab7aefd0 974static const struct mmc_host_ops at91_mci_ops = {
65dbf343
AV
975 .request = at91_mci_request,
976 .set_ios = at91_mci_set_ios,
977 .get_ro = at91_mci_get_ro,
7a6588ba 978 .enable_sdio_irq = at91_mci_enable_sdio_irq,
65dbf343
AV
979};
980
981/*
982 * Probe for the device
983 */
a26b498c 984static int __init at91_mci_probe(struct platform_device *pdev)
65dbf343
AV
985{
986 struct mmc_host *mmc;
987 struct at91mci_host *host;
17ea0595 988 struct resource *res;
65dbf343
AV
989 int ret;
990
17ea0595
AV
991 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
992 if (!res)
993 return -ENXIO;
994
995 if (!request_mem_region(res->start, res->end - res->start + 1, DRIVER_NAME))
996 return -EBUSY;
997
65dbf343
AV
998 mmc = mmc_alloc_host(sizeof(struct at91mci_host), &pdev->dev);
999 if (!mmc) {
6e996ee8
DB
1000 ret = -ENOMEM;
1001 dev_dbg(&pdev->dev, "couldn't allocate mmc host\n");
1002 goto fail6;
65dbf343
AV
1003 }
1004
1005 mmc->ops = &at91_mci_ops;
1006 mmc->f_min = 375000;
1007 mmc->f_max = 25000000;
1008 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
23af6039 1009 mmc->caps = MMC_CAP_SDIO_IRQ;
65dbf343 1010
fe4a3c7a 1011 mmc->max_blk_size = 4095;
55db890a 1012 mmc->max_blk_count = mmc->max_req_size;
fe4a3c7a 1013
65dbf343
AV
1014 host = mmc_priv(mmc);
1015 host->mmc = mmc;
1016 host->buffer = NULL;
1017 host->bus_mode = 0;
1018 host->board = pdev->dev.platform_data;
1019 if (host->board->wire4) {
ed99c541
NF
1020 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
1021 mmc->caps |= MMC_CAP_4_BIT_DATA;
1022 else
6e996ee8 1023 dev_warn(&pdev->dev, "4 wire bus mode not supported"
ed99c541 1024 " - using 1 wire\n");
65dbf343
AV
1025 }
1026
6e996ee8
DB
1027 /*
1028 * Reserve GPIOs ... board init code makes sure these pins are set
1029 * up as GPIOs with the right direction (input, except for vcc)
1030 */
1031 if (host->board->det_pin) {
1032 ret = gpio_request(host->board->det_pin, "mmc_detect");
1033 if (ret < 0) {
1034 dev_dbg(&pdev->dev, "couldn't claim card detect pin\n");
1035 goto fail5;
1036 }
1037 }
1038 if (host->board->wp_pin) {
1039 ret = gpio_request(host->board->wp_pin, "mmc_wp");
1040 if (ret < 0) {
1041 dev_dbg(&pdev->dev, "couldn't claim wp sense pin\n");
1042 goto fail4;
1043 }
1044 }
1045 if (host->board->vcc_pin) {
1046 ret = gpio_request(host->board->vcc_pin, "mmc_vcc");
1047 if (ret < 0) {
1048 dev_dbg(&pdev->dev, "couldn't claim vcc switch pin\n");
1049 goto fail3;
1050 }
1051 }
1052
65dbf343
AV
1053 /*
1054 * Get Clock
1055 */
3dd3b039
AV
1056 host->mci_clk = clk_get(&pdev->dev, "mci_clk");
1057 if (IS_ERR(host->mci_clk)) {
6e996ee8
DB
1058 ret = -ENODEV;
1059 dev_dbg(&pdev->dev, "no mci_clk?\n");
1060 goto fail2;
65dbf343 1061 }
65dbf343 1062
17ea0595
AV
1063 /*
1064 * Map I/O region
1065 */
1066 host->baseaddr = ioremap(res->start, res->end - res->start + 1);
1067 if (!host->baseaddr) {
6e996ee8
DB
1068 ret = -ENOMEM;
1069 goto fail1;
17ea0595 1070 }
e0b19b83
AV
1071
1072 /*
1073 * Reset hardware
1074 */
3dd3b039 1075 clk_enable(host->mci_clk); /* Enable the peripheral clock */
e0b19b83
AV
1076 at91_mci_disable(host);
1077 at91_mci_enable(host);
1078
65dbf343
AV
1079 /*
1080 * Allocate the MCI interrupt
1081 */
17ea0595 1082 host->irq = platform_get_irq(pdev, 0);
6e996ee8
DB
1083 ret = request_irq(host->irq, at91_mci_irq, IRQF_SHARED,
1084 mmc_hostname(mmc), host);
65dbf343 1085 if (ret) {
6e996ee8
DB
1086 dev_dbg(&pdev->dev, "request MCI interrupt failed\n");
1087 goto fail0;
65dbf343
AV
1088 }
1089
99ba0405
NF
1090 setup_timer(&host->timer, at91_timeout_timer, (unsigned long)host);
1091
65dbf343
AV
1092 platform_set_drvdata(pdev, mmc);
1093
1094 /*
1095 * Add host to MMC layer
1096 */
63b66438 1097 if (host->board->det_pin) {
6e996ee8 1098 host->present = !gpio_get_value(host->board->det_pin);
63b66438 1099 }
65dbf343
AV
1100 else
1101 host->present = -1;
1102
1103 mmc_add_host(mmc);
1104
1105 /*
1106 * monitor card insertion/removal if we can
1107 */
1108 if (host->board->det_pin) {
6e996ee8
DB
1109 ret = request_irq(gpio_to_irq(host->board->det_pin),
1110 at91_mmc_det_irq, 0, mmc_hostname(mmc), host);
65dbf343 1111 if (ret)
6e996ee8
DB
1112 dev_warn(&pdev->dev, "request MMC detect irq failed\n");
1113 else
1114 device_init_wakeup(&pdev->dev, 1);
65dbf343
AV
1115 }
1116
f3a8efa9 1117 pr_debug("Added MCI driver\n");
65dbf343
AV
1118
1119 return 0;
6e996ee8
DB
1120
1121fail0:
1122 clk_disable(host->mci_clk);
1123 iounmap(host->baseaddr);
1124fail1:
1125 clk_put(host->mci_clk);
1126fail2:
1127 if (host->board->vcc_pin)
1128 gpio_free(host->board->vcc_pin);
1129fail3:
1130 if (host->board->wp_pin)
1131 gpio_free(host->board->wp_pin);
1132fail4:
1133 if (host->board->det_pin)
1134 gpio_free(host->board->det_pin);
1135fail5:
1136 mmc_free_host(mmc);
1137fail6:
1138 release_mem_region(res->start, res->end - res->start + 1);
1139 dev_err(&pdev->dev, "probe failed, err %d\n", ret);
1140 return ret;
65dbf343
AV
1141}
1142
1143/*
1144 * Remove a device
1145 */
a26b498c 1146static int __exit at91_mci_remove(struct platform_device *pdev)
65dbf343
AV
1147{
1148 struct mmc_host *mmc = platform_get_drvdata(pdev);
1149 struct at91mci_host *host;
17ea0595 1150 struct resource *res;
65dbf343
AV
1151
1152 if (!mmc)
1153 return -1;
1154
1155 host = mmc_priv(mmc);
1156
e0cda54e 1157 if (host->board->det_pin) {
6e996ee8
DB
1158 if (device_can_wakeup(&pdev->dev))
1159 free_irq(gpio_to_irq(host->board->det_pin), host);
63b66438 1160 device_init_wakeup(&pdev->dev, 0);
6e996ee8 1161 gpio_free(host->board->det_pin);
65dbf343
AV
1162 }
1163
e0b19b83 1164 at91_mci_disable(host);
e181dce8 1165 del_timer_sync(&host->timer);
17ea0595
AV
1166 mmc_remove_host(mmc);
1167 free_irq(host->irq, host);
65dbf343 1168
3dd3b039
AV
1169 clk_disable(host->mci_clk); /* Disable the peripheral clock */
1170 clk_put(host->mci_clk);
65dbf343 1171
6e996ee8
DB
1172 if (host->board->vcc_pin)
1173 gpio_free(host->board->vcc_pin);
1174 if (host->board->wp_pin)
1175 gpio_free(host->board->wp_pin);
1176
17ea0595
AV
1177 iounmap(host->baseaddr);
1178 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1179 release_mem_region(res->start, res->end - res->start + 1);
65dbf343 1180
17ea0595
AV
1181 mmc_free_host(mmc);
1182 platform_set_drvdata(pdev, NULL);
b44fb7a0 1183 pr_debug("MCI Removed\n");
65dbf343
AV
1184
1185 return 0;
1186}
1187
1188#ifdef CONFIG_PM
1189static int at91_mci_suspend(struct platform_device *pdev, pm_message_t state)
1190{
1191 struct mmc_host *mmc = platform_get_drvdata(pdev);
63b66438 1192 struct at91mci_host *host = mmc_priv(mmc);
65dbf343
AV
1193 int ret = 0;
1194
e0cda54e 1195 if (host->board->det_pin && device_may_wakeup(&pdev->dev))
63b66438
MP
1196 enable_irq_wake(host->board->det_pin);
1197
65dbf343
AV
1198 if (mmc)
1199 ret = mmc_suspend_host(mmc, state);
1200
1201 return ret;
1202}
1203
1204static int at91_mci_resume(struct platform_device *pdev)
1205{
1206 struct mmc_host *mmc = platform_get_drvdata(pdev);
63b66438 1207 struct at91mci_host *host = mmc_priv(mmc);
65dbf343
AV
1208 int ret = 0;
1209
e0cda54e 1210 if (host->board->det_pin && device_may_wakeup(&pdev->dev))
63b66438
MP
1211 disable_irq_wake(host->board->det_pin);
1212
65dbf343
AV
1213 if (mmc)
1214 ret = mmc_resume_host(mmc);
1215
1216 return ret;
1217}
1218#else
1219#define at91_mci_suspend NULL
1220#define at91_mci_resume NULL
1221#endif
1222
1223static struct platform_driver at91_mci_driver = {
a26b498c 1224 .remove = __exit_p(at91_mci_remove),
65dbf343
AV
1225 .suspend = at91_mci_suspend,
1226 .resume = at91_mci_resume,
1227 .driver = {
1228 .name = DRIVER_NAME,
1229 .owner = THIS_MODULE,
1230 },
1231};
1232
1233static int __init at91_mci_init(void)
1234{
a26b498c 1235 return platform_driver_probe(&at91_mci_driver, at91_mci_probe);
65dbf343
AV
1236}
1237
1238static void __exit at91_mci_exit(void)
1239{
1240 platform_driver_unregister(&at91_mci_driver);
1241}
1242
1243module_init(at91_mci_init);
1244module_exit(at91_mci_exit);
1245
1246MODULE_DESCRIPTION("AT91 Multimedia Card Interface driver");
1247MODULE_AUTHOR("Nick Randell");
1248MODULE_LICENSE("GPL");
bc65c724 1249MODULE_ALIAS("platform:at91_mci");