blob: b9d4ed6b29b1655f6e67569645295bb771f5c4a9 [file] [log] [blame]
Andrew Victor65dbf342006-04-02 19:18:51 +01001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/at91_mci.c - ATMEL AT91 MCI Driver
Andrew Victor65dbf342006-04-02 19:18:51 +01003 *
4 * Copyright (C) 2005 Cougar Creek Computing Devices Ltd, All Rights Reserved
5 *
6 * Copyright (C) 2006 Malcolm Noyes
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13/*
Andrew Victor99eeb8d2006-12-11 12:40:23 +010014 This is the AT91 MCI driver that has been tested with both MMC cards
Andrew Victor65dbf342006-04-02 19:18:51 +010015 and SD-cards. Boards that support write protect are now supported.
16 The CCAT91SBC001 board does not support SD cards.
17
18 The three entry points are at91_mci_request, at91_mci_set_ios
19 and at91_mci_get_ro.
20
21 SET IOS
22 This configures the device to put it into the correct mode and clock speed
23 required.
24
25 MCI REQUEST
26 MCI request processes the commands sent in the mmc_request structure. This
27 can consist of a processing command and a stop command in the case of
28 multiple block transfers.
29
30 There are three main types of request, commands, reads and writes.
31
32 Commands are straight forward. The command is submitted to the controller and
33 the request function returns. When the controller generates an interrupt to indicate
34 the command is finished, the response to the command are read and the mmc_request_done
35 function called to end the request.
36
37 Reads and writes work in a similar manner to normal commands but involve the PDC (DMA)
38 controller to manage the transfers.
39
40 A read is done from the controller directly to the scatterlist passed in from the request.
Andrew Victor99eeb8d2006-12-11 12:40:23 +010041 Due to a bug in the AT91RM9200 controller, when a read is completed, all the words are byte
42 swapped in the scatterlist buffers. AT91SAM926x are not affected by this bug.
Andrew Victor65dbf342006-04-02 19:18:51 +010043
44 The sequence of read interrupts is: ENDRX, RXBUFF, CMDRDY
45
46 A write is slightly different in that the bytes to write are read from the scatterlist
47 into a dma memory buffer (this is in case the source buffer should be read only). The
48 entire write buffer is then done from this single dma memory buffer.
49
50 The sequence of write interrupts is: ENDTX, TXBUFE, NOTBUSY, CMDRDY
51
52 GET RO
53 Gets the status of the write protect pin, if available.
54*/
55
Andrew Victor65dbf342006-04-02 19:18:51 +010056#include <linux/module.h>
57#include <linux/moduleparam.h>
58#include <linux/init.h>
59#include <linux/ioport.h>
60#include <linux/platform_device.h>
61#include <linux/interrupt.h>
62#include <linux/blkdev.h>
63#include <linux/delay.h>
64#include <linux/err.h>
65#include <linux/dma-mapping.h>
66#include <linux/clk.h>
Andrew Victor93a3ddc2007-02-08 11:31:22 +010067#include <linux/atmel_pdc.h>
Andrew Victor65dbf342006-04-02 19:18:51 +010068
69#include <linux/mmc/host.h>
Andrew Victor65dbf342006-04-02 19:18:51 +010070
71#include <asm/io.h>
72#include <asm/irq.h>
David Brownell6e996ee2008-02-04 18:12:48 +010073#include <asm/gpio.h>
74
Andrew Victor65dbf342006-04-02 19:18:51 +010075#include <asm/mach/mmc.h>
76#include <asm/arch/board.h>
Andrew Victor99eeb8d2006-12-11 12:40:23 +010077#include <asm/arch/cpu.h>
Andrew Victor55d8bae2006-11-30 17:16:43 +010078#include <asm/arch/at91_mci.h>
Andrew Victor65dbf342006-04-02 19:18:51 +010079
80#define DRIVER_NAME "at91_mci"
81
Andrew Victordf05a302006-10-23 14:50:09 +020082#define FL_SENT_COMMAND (1 << 0)
83#define FL_SENT_STOP (1 << 1)
Andrew Victor65dbf342006-04-02 19:18:51 +010084
Andrew Victordf05a302006-10-23 14:50:09 +020085#define AT91_MCI_ERRORS (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE \
86 | AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE \
Nicolas Ferre37b758e2007-08-08 12:01:44 +020087 | AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)
Andrew Victor65dbf342006-04-02 19:18:51 +010088
Andrew Victore0b19b82006-10-25 19:42:38 +020089#define at91_mci_read(host, reg) __raw_readl((host)->baseaddr + (reg))
90#define at91_mci_write(host, reg, val) __raw_writel((val), (host)->baseaddr + (reg))
Andrew Victor65dbf342006-04-02 19:18:51 +010091
Andrew Victor65dbf342006-04-02 19:18:51 +010092
93/*
94 * Low level type for this driver
95 */
96struct at91mci_host
97{
98 struct mmc_host *mmc;
99 struct mmc_command *cmd;
100 struct mmc_request *request;
101
Andrew Victore0b19b82006-10-25 19:42:38 +0200102 void __iomem *baseaddr;
Andrew Victor17ea0592006-10-23 14:44:40 +0200103 int irq;
Andrew Victore0b19b82006-10-25 19:42:38 +0200104
Andrew Victor65dbf342006-04-02 19:18:51 +0100105 struct at91_mmc_data *board;
106 int present;
107
Andrew Victor3dd3b032006-10-23 14:46:54 +0200108 struct clk *mci_clk;
109
Andrew Victor65dbf342006-04-02 19:18:51 +0100110 /*
111 * Flag indicating when the command has been sent. This is used to
112 * work out whether or not to send the stop
113 */
114 unsigned int flags;
115 /* flag for current bus settings */
116 u32 bus_mode;
117
118 /* DMA buffer used for transmitting */
119 unsigned int* buffer;
120 dma_addr_t physical_address;
121 unsigned int total_length;
122
123 /* Latest in the scatterlist that has been enabled for transfer, but not freed */
124 int in_use_index;
125
126 /* Latest in the scatterlist that has been enabled for transfer */
127 int transfer_index;
128};
129
130/*
131 * Copy from sg to a dma block - used for transfers
132 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200133static inline void at91_mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data)
Andrew Victor65dbf342006-04-02 19:18:51 +0100134{
135 unsigned int len, i, size;
136 unsigned *dmabuf = host->buffer;
137
138 size = host->total_length;
139 len = data->sg_len;
140
141 /*
142 * Just loop through all entries. Size might not
143 * be the entire list though so make sure that
144 * we do not transfer too much.
145 */
146 for (i = 0; i < len; i++) {
147 struct scatterlist *sg;
148 int amount;
Andrew Victor65dbf342006-04-02 19:18:51 +0100149 unsigned int *sgbuffer;
150
151 sg = &data->sg[i];
152
Jens Axboe45711f12007-10-22 21:19:53 +0200153 sgbuffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
Andrew Victor65dbf342006-04-02 19:18:51 +0100154 amount = min(size, sg->length);
155 size -= amount;
Andrew Victor65dbf342006-04-02 19:18:51 +0100156
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100157 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
158 int index;
159
160 for (index = 0; index < (amount / 4); index++)
161 *dmabuf++ = swab32(sgbuffer[index]);
162 }
163 else
164 memcpy(dmabuf, sgbuffer, amount);
Andrew Victor65dbf342006-04-02 19:18:51 +0100165
166 kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
167
168 if (size == 0)
169 break;
170 }
171
172 /*
173 * Check that we didn't get a request to transfer
174 * more data than can fit into the SG list.
175 */
176 BUG_ON(size != 0);
177}
178
179/*
180 * Prepare a dma read
181 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200182static void at91_mci_pre_dma_read(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100183{
184 int i;
185 struct scatterlist *sg;
186 struct mmc_command *cmd;
187 struct mmc_data *data;
188
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100189 pr_debug("pre dma read\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100190
191 cmd = host->cmd;
192 if (!cmd) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100193 pr_debug("no command\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100194 return;
195 }
196
197 data = cmd->data;
198 if (!data) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100199 pr_debug("no data\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100200 return;
201 }
202
203 for (i = 0; i < 2; i++) {
204 /* nothing left to transfer */
205 if (host->transfer_index >= data->sg_len) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100206 pr_debug("Nothing left to transfer (index = %d)\n", host->transfer_index);
Andrew Victor65dbf342006-04-02 19:18:51 +0100207 break;
208 }
209
210 /* Check to see if this needs filling */
211 if (i == 0) {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100212 if (at91_mci_read(host, ATMEL_PDC_RCR) != 0) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100213 pr_debug("Transfer active in current\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100214 continue;
215 }
216 }
217 else {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100218 if (at91_mci_read(host, ATMEL_PDC_RNCR) != 0) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100219 pr_debug("Transfer active in next\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100220 continue;
221 }
222 }
223
224 /* Setup the next transfer */
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100225 pr_debug("Using transfer index %d\n", host->transfer_index);
Andrew Victor65dbf342006-04-02 19:18:51 +0100226
227 sg = &data->sg[host->transfer_index++];
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100228 pr_debug("sg = %p\n", sg);
Andrew Victor65dbf342006-04-02 19:18:51 +0100229
Jens Axboe45711f12007-10-22 21:19:53 +0200230 sg->dma_address = dma_map_page(NULL, sg_page(sg), sg->offset, sg->length, DMA_FROM_DEVICE);
Andrew Victor65dbf342006-04-02 19:18:51 +0100231
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100232 pr_debug("dma address = %08X, length = %d\n", sg->dma_address, sg->length);
Andrew Victor65dbf342006-04-02 19:18:51 +0100233
234 if (i == 0) {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100235 at91_mci_write(host, ATMEL_PDC_RPR, sg->dma_address);
236 at91_mci_write(host, ATMEL_PDC_RCR, sg->length / 4);
Andrew Victor65dbf342006-04-02 19:18:51 +0100237 }
238 else {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100239 at91_mci_write(host, ATMEL_PDC_RNPR, sg->dma_address);
240 at91_mci_write(host, ATMEL_PDC_RNCR, sg->length / 4);
Andrew Victor65dbf342006-04-02 19:18:51 +0100241 }
242 }
243
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100244 pr_debug("pre dma read done\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100245}
246
247/*
248 * Handle after a dma read
249 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200250static void at91_mci_post_dma_read(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100251{
252 struct mmc_command *cmd;
253 struct mmc_data *data;
254
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100255 pr_debug("post dma read\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100256
257 cmd = host->cmd;
258 if (!cmd) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100259 pr_debug("no command\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100260 return;
261 }
262
263 data = cmd->data;
264 if (!data) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100265 pr_debug("no data\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100266 return;
267 }
268
269 while (host->in_use_index < host->transfer_index) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100270 struct scatterlist *sg;
271
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100272 pr_debug("finishing index %d\n", host->in_use_index);
Andrew Victor65dbf342006-04-02 19:18:51 +0100273
274 sg = &data->sg[host->in_use_index++];
275
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100276 pr_debug("Unmapping page %08X\n", sg->dma_address);
Andrew Victor65dbf342006-04-02 19:18:51 +0100277
278 dma_unmap_page(NULL, sg->dma_address, sg->length, DMA_FROM_DEVICE);
279
Andrew Victor65dbf342006-04-02 19:18:51 +0100280 data->bytes_xfered += sg->length;
281
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100282 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
Nicolas Ferreed99c542007-07-09 14:58:16 +0200283 unsigned int *buffer;
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100284 int index;
Andrew Victor65dbf342006-04-02 19:18:51 +0100285
Nicolas Ferreed99c542007-07-09 14:58:16 +0200286 /* Swap the contents of the buffer */
Jens Axboe45711f12007-10-22 21:19:53 +0200287 buffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
Nicolas Ferreed99c542007-07-09 14:58:16 +0200288 pr_debug("buffer = %p, length = %d\n", buffer, sg->length);
289
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100290 for (index = 0; index < (sg->length / 4); index++)
291 buffer[index] = swab32(buffer[index]);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200292
293 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
Andrew Victor65dbf342006-04-02 19:18:51 +0100294 }
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100295
Jens Axboe45711f12007-10-22 21:19:53 +0200296 flush_dcache_page(sg_page(sg));
Andrew Victor65dbf342006-04-02 19:18:51 +0100297 }
298
299 /* Is there another transfer to trigger? */
300 if (host->transfer_index < data->sg_len)
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200301 at91_mci_pre_dma_read(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100302 else {
Nicolas Ferreed99c542007-07-09 14:58:16 +0200303 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_ENDRX);
Andrew Victore0b19b82006-10-25 19:42:38 +0200304 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_RXBUFF);
Andrew Victor65dbf342006-04-02 19:18:51 +0100305 }
306
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100307 pr_debug("post dma read done\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100308}
309
310/*
311 * Handle transmitted data
312 */
313static void at91_mci_handle_transmitted(struct at91mci_host *host)
314{
315 struct mmc_command *cmd;
316 struct mmc_data *data;
317
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100318 pr_debug("Handling the transmit\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100319
320 /* Disable the transfer */
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100321 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100322
323 /* Now wait for cmd ready */
Andrew Victore0b19b82006-10-25 19:42:38 +0200324 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE);
Andrew Victor65dbf342006-04-02 19:18:51 +0100325
326 cmd = host->cmd;
327 if (!cmd) return;
328
329 data = cmd->data;
330 if (!data) return;
331
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200332 if (cmd->data->blocks > 1) {
Nicolas Ferreed99c542007-07-09 14:58:16 +0200333 pr_debug("multiple write : wait for BLKE...\n");
334 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
335 } else
336 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
337
Andrew Victor65dbf342006-04-02 19:18:51 +0100338 data->bytes_xfered = host->total_length;
339}
340
Nicolas Ferreed99c542007-07-09 14:58:16 +0200341/*Handle after command sent ready*/
342static int at91_mci_handle_cmdrdy(struct at91mci_host *host)
343{
344 if (!host->cmd)
345 return 1;
346 else if (!host->cmd->data) {
347 if (host->flags & FL_SENT_STOP) {
348 /*After multi block write, we must wait for NOTBUSY*/
349 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
350 } else return 1;
351 } else if (host->cmd->data->flags & MMC_DATA_WRITE) {
352 /*After sendding multi-block-write command, start DMA transfer*/
353 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_TXBUFE);
354 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
355 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
356 }
357
358 /* command not completed, have to wait */
359 return 0;
360}
361
362
Andrew Victor65dbf342006-04-02 19:18:51 +0100363/*
364 * Enable the controller
365 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200366static void at91_mci_enable(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100367{
Nicolas Ferreed99c542007-07-09 14:58:16 +0200368 unsigned int mr;
369
Andrew Victore0b19b82006-10-25 19:42:38 +0200370 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200371 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
Andrew Victore0b19b82006-10-25 19:42:38 +0200372 at91_mci_write(host, AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200373 mr = AT91_MCI_PDCMODE | 0x34a;
374
375 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
376 mr |= AT91_MCI_RDPROOF | AT91_MCI_WRPROOF;
377
378 at91_mci_write(host, AT91_MCI_MR, mr);
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100379
380 /* use Slot A or B (only one at same time) */
381 at91_mci_write(host, AT91_MCI_SDCR, host->board->slot_b);
Andrew Victor65dbf342006-04-02 19:18:51 +0100382}
383
384/*
385 * Disable the controller
386 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200387static void at91_mci_disable(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100388{
Andrew Victore0b19b82006-10-25 19:42:38 +0200389 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
Andrew Victor65dbf342006-04-02 19:18:51 +0100390}
391
392/*
393 * Send a command
Andrew Victor65dbf342006-04-02 19:18:51 +0100394 */
Nicolas Ferreed99c542007-07-09 14:58:16 +0200395static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd)
Andrew Victor65dbf342006-04-02 19:18:51 +0100396{
397 unsigned int cmdr, mr;
398 unsigned int block_length;
399 struct mmc_data *data = cmd->data;
400
401 unsigned int blocks;
402 unsigned int ier = 0;
403
404 host->cmd = cmd;
405
Nicolas Ferreed99c542007-07-09 14:58:16 +0200406 /* Needed for leaving busy state before CMD1 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200407 if ((at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100408 pr_debug("Clearing timeout\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200409 at91_mci_write(host, AT91_MCI_ARGR, 0);
410 at91_mci_write(host, AT91_MCI_CMDR, AT91_MCI_OPDCMD);
411 while (!(at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_CMDRDY)) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100412 /* spin */
Andrew Victore0b19b82006-10-25 19:42:38 +0200413 pr_debug("Clearing: SR = %08X\n", at91_mci_read(host, AT91_MCI_SR));
Andrew Victor65dbf342006-04-02 19:18:51 +0100414 }
415 }
Nicolas Ferreed99c542007-07-09 14:58:16 +0200416
Andrew Victor65dbf342006-04-02 19:18:51 +0100417 cmdr = cmd->opcode;
418
419 if (mmc_resp_type(cmd) == MMC_RSP_NONE)
420 cmdr |= AT91_MCI_RSPTYP_NONE;
421 else {
422 /* if a response is expected then allow maximum response latancy */
423 cmdr |= AT91_MCI_MAXLAT;
424 /* set 136 bit response for R2, 48 bit response otherwise */
425 if (mmc_resp_type(cmd) == MMC_RSP_R2)
426 cmdr |= AT91_MCI_RSPTYP_136;
427 else
428 cmdr |= AT91_MCI_RSPTYP_48;
429 }
430
431 if (data) {
Marc Pignat1d4de9e2007-08-09 13:56:29 +0200432
433 if ( data->blksz & 0x3 ) {
434 pr_debug("Unsupported block size\n");
435 cmd->error = -EINVAL;
436 mmc_request_done(host->mmc, host->request);
437 return;
438 }
439
Russell Kinga3fd4a12006-06-04 17:51:15 +0100440 block_length = data->blksz;
Andrew Victor65dbf342006-04-02 19:18:51 +0100441 blocks = data->blocks;
442
443 /* always set data start - also set direction flag for read */
444 if (data->flags & MMC_DATA_READ)
445 cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START);
446 else if (data->flags & MMC_DATA_WRITE)
447 cmdr |= AT91_MCI_TRCMD_START;
448
449 if (data->flags & MMC_DATA_STREAM)
450 cmdr |= AT91_MCI_TRTYP_STREAM;
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200451 if (data->blocks > 1)
Andrew Victor65dbf342006-04-02 19:18:51 +0100452 cmdr |= AT91_MCI_TRTYP_MULTIPLE;
453 }
454 else {
455 block_length = 0;
456 blocks = 0;
457 }
458
Marc Pignatb6cedb32007-06-06 20:27:59 +0200459 if (host->flags & FL_SENT_STOP)
Andrew Victor65dbf342006-04-02 19:18:51 +0100460 cmdr |= AT91_MCI_TRCMD_STOP;
461
462 if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
463 cmdr |= AT91_MCI_OPDCMD;
464
465 /*
466 * Set the arguments and send the command
467 */
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200468 pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",
Andrew Victore0b19b82006-10-25 19:42:38 +0200469 cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(host, AT91_MCI_MR));
Andrew Victor65dbf342006-04-02 19:18:51 +0100470
471 if (!data) {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100472 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS | ATMEL_PDC_RXTDIS);
473 at91_mci_write(host, ATMEL_PDC_RPR, 0);
474 at91_mci_write(host, ATMEL_PDC_RCR, 0);
475 at91_mci_write(host, ATMEL_PDC_RNPR, 0);
476 at91_mci_write(host, ATMEL_PDC_RNCR, 0);
477 at91_mci_write(host, ATMEL_PDC_TPR, 0);
478 at91_mci_write(host, ATMEL_PDC_TCR, 0);
479 at91_mci_write(host, ATMEL_PDC_TNPR, 0);
480 at91_mci_write(host, ATMEL_PDC_TNCR, 0);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200481 ier = AT91_MCI_CMDRDY;
482 } else {
483 /* zero block length and PDC mode */
484 mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff;
485 at91_mci_write(host, AT91_MCI_MR, mr | (block_length << 16) | AT91_MCI_PDCMODE);
Andrew Victor65dbf342006-04-02 19:18:51 +0100486
Nicolas Ferreed99c542007-07-09 14:58:16 +0200487 /*
488 * Disable the PDC controller
489 */
490 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100491
Nicolas Ferreed99c542007-07-09 14:58:16 +0200492 if (cmdr & AT91_MCI_TRCMD_START) {
493 data->bytes_xfered = 0;
494 host->transfer_index = 0;
495 host->in_use_index = 0;
496 if (cmdr & AT91_MCI_TRDIR) {
497 /*
498 * Handle a read
499 */
500 host->buffer = NULL;
501 host->total_length = 0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100502
Nicolas Ferreed99c542007-07-09 14:58:16 +0200503 at91_mci_pre_dma_read(host);
504 ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
505 }
506 else {
507 /*
508 * Handle a write
509 */
510 host->total_length = block_length * blocks;
511 host->buffer = dma_alloc_coherent(NULL,
512 host->total_length,
513 &host->physical_address, GFP_KERNEL);
Andrew Victor65dbf342006-04-02 19:18:51 +0100514
Nicolas Ferreed99c542007-07-09 14:58:16 +0200515 at91_mci_sg_to_dma(host, data);
Andrew Victor65dbf342006-04-02 19:18:51 +0100516
Nicolas Ferreed99c542007-07-09 14:58:16 +0200517 pr_debug("Transmitting %d bytes\n", host->total_length);
Andrew Victor65dbf342006-04-02 19:18:51 +0100518
Nicolas Ferreed99c542007-07-09 14:58:16 +0200519 at91_mci_write(host, ATMEL_PDC_TPR, host->physical_address);
520 at91_mci_write(host, ATMEL_PDC_TCR, host->total_length / 4);
521 ier = AT91_MCI_CMDRDY;
522 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100523 }
524 }
525
526 /*
527 * Send the command and then enable the PDC - not the other way round as
528 * the data sheet says
529 */
530
Andrew Victore0b19b82006-10-25 19:42:38 +0200531 at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
532 at91_mci_write(host, AT91_MCI_CMDR, cmdr);
Andrew Victor65dbf342006-04-02 19:18:51 +0100533
534 if (cmdr & AT91_MCI_TRCMD_START) {
535 if (cmdr & AT91_MCI_TRDIR)
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100536 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Andrew Victor65dbf342006-04-02 19:18:51 +0100537 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100538
Nicolas Ferreed99c542007-07-09 14:58:16 +0200539 /* Enable selected interrupts */
Andrew Victordf05a302006-10-23 14:50:09 +0200540 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_ERRORS | ier);
Andrew Victor65dbf342006-04-02 19:18:51 +0100541}
542
543/*
544 * Process the next step in the request
545 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200546static void at91_mci_process_next(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100547{
548 if (!(host->flags & FL_SENT_COMMAND)) {
549 host->flags |= FL_SENT_COMMAND;
Nicolas Ferreed99c542007-07-09 14:58:16 +0200550 at91_mci_send_command(host, host->request->cmd);
Andrew Victor65dbf342006-04-02 19:18:51 +0100551 }
552 else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) {
553 host->flags |= FL_SENT_STOP;
Nicolas Ferreed99c542007-07-09 14:58:16 +0200554 at91_mci_send_command(host, host->request->stop);
Andrew Victor65dbf342006-04-02 19:18:51 +0100555 }
556 else
557 mmc_request_done(host->mmc, host->request);
558}
559
560/*
561 * Handle a command that has been completed
562 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200563static void at91_mci_completed_command(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100564{
565 struct mmc_command *cmd = host->cmd;
566 unsigned int status;
567
Andrew Victore0b19b82006-10-25 19:42:38 +0200568 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
Andrew Victor65dbf342006-04-02 19:18:51 +0100569
Andrew Victore0b19b82006-10-25 19:42:38 +0200570 cmd->resp[0] = at91_mci_read(host, AT91_MCI_RSPR(0));
571 cmd->resp[1] = at91_mci_read(host, AT91_MCI_RSPR(1));
572 cmd->resp[2] = at91_mci_read(host, AT91_MCI_RSPR(2));
573 cmd->resp[3] = at91_mci_read(host, AT91_MCI_RSPR(3));
Andrew Victor65dbf342006-04-02 19:18:51 +0100574
575 if (host->buffer) {
576 dma_free_coherent(NULL, host->total_length, host->buffer, host->physical_address);
577 host->buffer = NULL;
578 }
579
Andrew Victore0b19b82006-10-25 19:42:38 +0200580 status = at91_mci_read(host, AT91_MCI_SR);
Andrew Victor65dbf342006-04-02 19:18:51 +0100581
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100582 pr_debug("Status = %08X [%08X %08X %08X %08X]\n",
Andrew Victor65dbf342006-04-02 19:18:51 +0100583 status, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
584
Andrew Victor9e3866b2007-10-17 11:53:40 +0200585 if (status & AT91_MCI_ERRORS) {
Marc Pignatb6cedb32007-06-06 20:27:59 +0200586 if ((status & AT91_MCI_RCRCE) && !(mmc_resp_type(cmd) & MMC_RSP_CRC)) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200587 cmd->error = 0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100588 }
589 else {
590 if (status & (AT91_MCI_RTOE | AT91_MCI_DTOE))
Pierre Ossman17b04292007-07-22 22:18:46 +0200591 cmd->error = -ETIMEDOUT;
Andrew Victor65dbf342006-04-02 19:18:51 +0100592 else if (status & (AT91_MCI_RCRCE | AT91_MCI_DCRCE))
Pierre Ossman17b04292007-07-22 22:18:46 +0200593 cmd->error = -EILSEQ;
Andrew Victor65dbf342006-04-02 19:18:51 +0100594 else
Pierre Ossman17b04292007-07-22 22:18:46 +0200595 cmd->error = -EIO;
Andrew Victor65dbf342006-04-02 19:18:51 +0100596
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100597 pr_debug("Error detected and set to %d (cmd = %d, retries = %d)\n",
Andrew Victor65dbf342006-04-02 19:18:51 +0100598 cmd->error, cmd->opcode, cmd->retries);
599 }
600 }
601 else
Pierre Ossman17b04292007-07-22 22:18:46 +0200602 cmd->error = 0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100603
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200604 at91_mci_process_next(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100605}
606
607/*
608 * Handle an MMC request
609 */
610static void at91_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
611{
612 struct at91mci_host *host = mmc_priv(mmc);
613 host->request = mrq;
614 host->flags = 0;
615
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200616 at91_mci_process_next(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100617}
618
619/*
620 * Set the IOS
621 */
622static void at91_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
623{
624 int clkdiv;
625 struct at91mci_host *host = mmc_priv(mmc);
Andrew Victor3dd3b032006-10-23 14:46:54 +0200626 unsigned long at91_master_clock = clk_get_rate(host->mci_clk);
Andrew Victor65dbf342006-04-02 19:18:51 +0100627
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100628 host->bus_mode = ios->bus_mode;
Andrew Victor65dbf342006-04-02 19:18:51 +0100629
630 if (ios->clock == 0) {
631 /* Disable the MCI controller */
Andrew Victore0b19b82006-10-25 19:42:38 +0200632 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100633 clkdiv = 0;
634 }
635 else {
636 /* Enable the MCI controller */
Andrew Victore0b19b82006-10-25 19:42:38 +0200637 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
Andrew Victor65dbf342006-04-02 19:18:51 +0100638
639 if ((at91_master_clock % (ios->clock * 2)) == 0)
640 clkdiv = ((at91_master_clock / ios->clock) / 2) - 1;
641 else
642 clkdiv = (at91_master_clock / ios->clock) / 2;
643
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100644 pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv,
Andrew Victor65dbf342006-04-02 19:18:51 +0100645 at91_master_clock / (2 * (clkdiv + 1)));
646 }
647 if (ios->bus_width == MMC_BUS_WIDTH_4 && host->board->wire4) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100648 pr_debug("MMC: Setting controller bus width to 4\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200649 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) | AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100650 }
651 else {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100652 pr_debug("MMC: Setting controller bus width to 1\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200653 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100654 }
655
656 /* Set the clock divider */
Andrew Victore0b19b82006-10-25 19:42:38 +0200657 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 +0100658
659 /* maybe switch power to the card */
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100660 if (host->board->vcc_pin) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100661 switch (ios->power_mode) {
662 case MMC_POWER_OFF:
David Brownell6e996ee2008-02-04 18:12:48 +0100663 gpio_set_value(host->board->vcc_pin, 0);
Andrew Victor65dbf342006-04-02 19:18:51 +0100664 break;
665 case MMC_POWER_UP:
David Brownell6e996ee2008-02-04 18:12:48 +0100666 gpio_set_value(host->board->vcc_pin, 1);
Andrew Victor65dbf342006-04-02 19:18:51 +0100667 break;
Marc Pignate5c0ef92008-05-09 11:07:07 +0200668 case MMC_POWER_ON:
669 break;
670 default:
671 WARN_ON(1);
Andrew Victor65dbf342006-04-02 19:18:51 +0100672 }
673 }
674}
675
676/*
677 * Handle an interrupt
678 */
David Howells7d12e782006-10-05 14:55:46 +0100679static irqreturn_t at91_mci_irq(int irq, void *devid)
Andrew Victor65dbf342006-04-02 19:18:51 +0100680{
681 struct at91mci_host *host = devid;
682 int completed = 0;
Andrew Victordf05a302006-10-23 14:50:09 +0200683 unsigned int int_status, int_mask;
Andrew Victor65dbf342006-04-02 19:18:51 +0100684
Andrew Victore0b19b82006-10-25 19:42:38 +0200685 int_status = at91_mci_read(host, AT91_MCI_SR);
Andrew Victordf05a302006-10-23 14:50:09 +0200686 int_mask = at91_mci_read(host, AT91_MCI_IMR);
Nicolas Ferre37b758e2007-08-08 12:01:44 +0200687
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200688 pr_debug("MCI irq: status = %08X, %08X, %08X\n", int_status, int_mask,
Andrew Victordf05a302006-10-23 14:50:09 +0200689 int_status & int_mask);
Nicolas Ferre37b758e2007-08-08 12:01:44 +0200690
Andrew Victordf05a302006-10-23 14:50:09 +0200691 int_status = int_status & int_mask;
Andrew Victor65dbf342006-04-02 19:18:51 +0100692
Andrew Victordf05a302006-10-23 14:50:09 +0200693 if (int_status & AT91_MCI_ERRORS) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100694 completed = 1;
Nicolas Ferre37b758e2007-08-08 12:01:44 +0200695
Andrew Victordf05a302006-10-23 14:50:09 +0200696 if (int_status & AT91_MCI_UNRE)
697 pr_debug("MMC: Underrun error\n");
698 if (int_status & AT91_MCI_OVRE)
699 pr_debug("MMC: Overrun error\n");
700 if (int_status & AT91_MCI_DTOE)
701 pr_debug("MMC: Data timeout\n");
702 if (int_status & AT91_MCI_DCRCE)
703 pr_debug("MMC: CRC error in data\n");
704 if (int_status & AT91_MCI_RTOE)
705 pr_debug("MMC: Response timeout\n");
706 if (int_status & AT91_MCI_RENDE)
707 pr_debug("MMC: Response end bit error\n");
708 if (int_status & AT91_MCI_RCRCE)
709 pr_debug("MMC: Response CRC error\n");
710 if (int_status & AT91_MCI_RDIRE)
711 pr_debug("MMC: Response direction error\n");
712 if (int_status & AT91_MCI_RINDE)
713 pr_debug("MMC: Response index error\n");
714 } else {
715 /* Only continue processing if no errors */
Andrew Victor65dbf342006-04-02 19:18:51 +0100716
Andrew Victor65dbf342006-04-02 19:18:51 +0100717 if (int_status & AT91_MCI_TXBUFE) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100718 pr_debug("TX buffer empty\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100719 at91_mci_handle_transmitted(host);
720 }
721
Nicolas Ferreed99c542007-07-09 14:58:16 +0200722 if (int_status & AT91_MCI_ENDRX) {
723 pr_debug("ENDRX\n");
724 at91_mci_post_dma_read(host);
725 }
726
Andrew Victor65dbf342006-04-02 19:18:51 +0100727 if (int_status & AT91_MCI_RXBUFF) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100728 pr_debug("RX buffer full\n");
Nicolas Ferreed99c542007-07-09 14:58:16 +0200729 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
730 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_RXBUFF | AT91_MCI_ENDRX);
731 completed = 1;
Andrew Victor65dbf342006-04-02 19:18:51 +0100732 }
733
Andrew Victordf05a302006-10-23 14:50:09 +0200734 if (int_status & AT91_MCI_ENDTX)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100735 pr_debug("Transmit has ended\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100736
Andrew Victor65dbf342006-04-02 19:18:51 +0100737 if (int_status & AT91_MCI_NOTBUSY) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100738 pr_debug("Card is ready\n");
Nicolas Ferreed99c542007-07-09 14:58:16 +0200739 completed = 1;
Andrew Victor65dbf342006-04-02 19:18:51 +0100740 }
741
Andrew Victordf05a302006-10-23 14:50:09 +0200742 if (int_status & AT91_MCI_DTIP)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100743 pr_debug("Data transfer in progress\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100744
Nicolas Ferreed99c542007-07-09 14:58:16 +0200745 if (int_status & AT91_MCI_BLKE) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100746 pr_debug("Block transfer has ended\n");
Nicolas Ferreed99c542007-07-09 14:58:16 +0200747 completed = 1;
748 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100749
Andrew Victordf05a302006-10-23 14:50:09 +0200750 if (int_status & AT91_MCI_TXRDY)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100751 pr_debug("Ready to transmit\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100752
Andrew Victordf05a302006-10-23 14:50:09 +0200753 if (int_status & AT91_MCI_RXRDY)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100754 pr_debug("Ready to receive\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100755
756 if (int_status & AT91_MCI_CMDRDY) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100757 pr_debug("Command ready\n");
Nicolas Ferreed99c542007-07-09 14:58:16 +0200758 completed = at91_mci_handle_cmdrdy(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100759 }
760 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100761
762 if (completed) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100763 pr_debug("Completed command\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200764 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200765 at91_mci_completed_command(host);
Andrew Victordf05a302006-10-23 14:50:09 +0200766 } else
767 at91_mci_write(host, AT91_MCI_IDR, int_status);
Andrew Victor65dbf342006-04-02 19:18:51 +0100768
769 return IRQ_HANDLED;
770}
771
David Howells7d12e782006-10-05 14:55:46 +0100772static irqreturn_t at91_mmc_det_irq(int irq, void *_host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100773{
774 struct at91mci_host *host = _host;
David Brownell6e996ee2008-02-04 18:12:48 +0100775 int present = !gpio_get_value(irq_to_gpio(irq));
Andrew Victor65dbf342006-04-02 19:18:51 +0100776
777 /*
778 * we expect this irq on both insert and remove,
779 * and use a short delay to debounce.
780 */
781 if (present != host->present) {
782 host->present = present;
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100783 pr_debug("%s: card %s\n", mmc_hostname(host->mmc),
Andrew Victor65dbf342006-04-02 19:18:51 +0100784 present ? "insert" : "remove");
785 if (!present) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100786 pr_debug("****** Resetting SD-card bus width ******\n");
Andrew Victor99eeb8d2006-12-11 12:40:23 +0100787 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100788 }
789 mmc_detect_change(host->mmc, msecs_to_jiffies(100));
790 }
791 return IRQ_HANDLED;
792}
793
David Brownella26b4982006-12-26 14:45:26 -0800794static int at91_mci_get_ro(struct mmc_host *mmc)
Andrew Victor65dbf342006-04-02 19:18:51 +0100795{
Andrew Victor65dbf342006-04-02 19:18:51 +0100796 struct at91mci_host *host = mmc_priv(mmc);
797
Anton Vorontsov08f80bb2008-06-17 18:17:39 +0400798 if (host->board->wp_pin)
799 return !!gpio_get_value(host->board->wp_pin);
800 /*
801 * Board doesn't support read only detection; let the mmc core
802 * decide what to do.
803 */
804 return -ENOSYS;
Andrew Victor65dbf342006-04-02 19:18:51 +0100805}
806
David Brownellab7aefd2006-11-12 17:55:30 -0800807static const struct mmc_host_ops at91_mci_ops = {
Andrew Victor65dbf342006-04-02 19:18:51 +0100808 .request = at91_mci_request,
809 .set_ios = at91_mci_set_ios,
810 .get_ro = at91_mci_get_ro,
811};
812
813/*
814 * Probe for the device
815 */
David Brownella26b4982006-12-26 14:45:26 -0800816static int __init at91_mci_probe(struct platform_device *pdev)
Andrew Victor65dbf342006-04-02 19:18:51 +0100817{
818 struct mmc_host *mmc;
819 struct at91mci_host *host;
Andrew Victor17ea0592006-10-23 14:44:40 +0200820 struct resource *res;
Andrew Victor65dbf342006-04-02 19:18:51 +0100821 int ret;
822
Andrew Victor17ea0592006-10-23 14:44:40 +0200823 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
824 if (!res)
825 return -ENXIO;
826
827 if (!request_mem_region(res->start, res->end - res->start + 1, DRIVER_NAME))
828 return -EBUSY;
829
Andrew Victor65dbf342006-04-02 19:18:51 +0100830 mmc = mmc_alloc_host(sizeof(struct at91mci_host), &pdev->dev);
831 if (!mmc) {
David Brownell6e996ee2008-02-04 18:12:48 +0100832 ret = -ENOMEM;
833 dev_dbg(&pdev->dev, "couldn't allocate mmc host\n");
834 goto fail6;
Andrew Victor65dbf342006-04-02 19:18:51 +0100835 }
836
837 mmc->ops = &at91_mci_ops;
838 mmc->f_min = 375000;
839 mmc->f_max = 25000000;
840 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
841
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100842 mmc->max_blk_size = 4095;
Pierre Ossman55db8902006-11-21 17:55:45 +0100843 mmc->max_blk_count = mmc->max_req_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100844
Andrew Victor65dbf342006-04-02 19:18:51 +0100845 host = mmc_priv(mmc);
846 host->mmc = mmc;
847 host->buffer = NULL;
848 host->bus_mode = 0;
849 host->board = pdev->dev.platform_data;
850 if (host->board->wire4) {
Nicolas Ferreed99c542007-07-09 14:58:16 +0200851 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
852 mmc->caps |= MMC_CAP_4_BIT_DATA;
853 else
David Brownell6e996ee2008-02-04 18:12:48 +0100854 dev_warn(&pdev->dev, "4 wire bus mode not supported"
Nicolas Ferreed99c542007-07-09 14:58:16 +0200855 " - using 1 wire\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100856 }
857
858 /*
David Brownell6e996ee2008-02-04 18:12:48 +0100859 * Reserve GPIOs ... board init code makes sure these pins are set
860 * up as GPIOs with the right direction (input, except for vcc)
861 */
862 if (host->board->det_pin) {
863 ret = gpio_request(host->board->det_pin, "mmc_detect");
864 if (ret < 0) {
865 dev_dbg(&pdev->dev, "couldn't claim card detect pin\n");
866 goto fail5;
867 }
868 }
869 if (host->board->wp_pin) {
870 ret = gpio_request(host->board->wp_pin, "mmc_wp");
871 if (ret < 0) {
872 dev_dbg(&pdev->dev, "couldn't claim wp sense pin\n");
873 goto fail4;
874 }
875 }
876 if (host->board->vcc_pin) {
877 ret = gpio_request(host->board->vcc_pin, "mmc_vcc");
878 if (ret < 0) {
879 dev_dbg(&pdev->dev, "couldn't claim vcc switch pin\n");
880 goto fail3;
881 }
882 }
883
884 /*
Andrew Victor65dbf342006-04-02 19:18:51 +0100885 * Get Clock
886 */
Andrew Victor3dd3b032006-10-23 14:46:54 +0200887 host->mci_clk = clk_get(&pdev->dev, "mci_clk");
888 if (IS_ERR(host->mci_clk)) {
David Brownell6e996ee2008-02-04 18:12:48 +0100889 ret = -ENODEV;
890 dev_dbg(&pdev->dev, "no mci_clk?\n");
891 goto fail2;
Andrew Victor65dbf342006-04-02 19:18:51 +0100892 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100893
Andrew Victor17ea0592006-10-23 14:44:40 +0200894 /*
895 * Map I/O region
896 */
897 host->baseaddr = ioremap(res->start, res->end - res->start + 1);
898 if (!host->baseaddr) {
David Brownell6e996ee2008-02-04 18:12:48 +0100899 ret = -ENOMEM;
900 goto fail1;
Andrew Victor17ea0592006-10-23 14:44:40 +0200901 }
Andrew Victore0b19b82006-10-25 19:42:38 +0200902
903 /*
904 * Reset hardware
905 */
Andrew Victor3dd3b032006-10-23 14:46:54 +0200906 clk_enable(host->mci_clk); /* Enable the peripheral clock */
Andrew Victore0b19b82006-10-25 19:42:38 +0200907 at91_mci_disable(host);
908 at91_mci_enable(host);
909
Andrew Victor65dbf342006-04-02 19:18:51 +0100910 /*
911 * Allocate the MCI interrupt
912 */
Andrew Victor17ea0592006-10-23 14:44:40 +0200913 host->irq = platform_get_irq(pdev, 0);
David Brownell6e996ee2008-02-04 18:12:48 +0100914 ret = request_irq(host->irq, at91_mci_irq, IRQF_SHARED,
915 mmc_hostname(mmc), host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100916 if (ret) {
David Brownell6e996ee2008-02-04 18:12:48 +0100917 dev_dbg(&pdev->dev, "request MCI interrupt failed\n");
918 goto fail0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100919 }
920
921 platform_set_drvdata(pdev, mmc);
922
923 /*
924 * Add host to MMC layer
925 */
Marc Pignat63b66432007-07-16 11:07:02 +0200926 if (host->board->det_pin) {
David Brownell6e996ee2008-02-04 18:12:48 +0100927 host->present = !gpio_get_value(host->board->det_pin);
Marc Pignat63b66432007-07-16 11:07:02 +0200928 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100929 else
930 host->present = -1;
931
932 mmc_add_host(mmc);
933
934 /*
935 * monitor card insertion/removal if we can
936 */
937 if (host->board->det_pin) {
David Brownell6e996ee2008-02-04 18:12:48 +0100938 ret = request_irq(gpio_to_irq(host->board->det_pin),
939 at91_mmc_det_irq, 0, mmc_hostname(mmc), host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100940 if (ret)
David Brownell6e996ee2008-02-04 18:12:48 +0100941 dev_warn(&pdev->dev, "request MMC detect irq failed\n");
942 else
943 device_init_wakeup(&pdev->dev, 1);
Andrew Victor65dbf342006-04-02 19:18:51 +0100944 }
945
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200946 pr_debug("Added MCI driver\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100947
948 return 0;
David Brownell6e996ee2008-02-04 18:12:48 +0100949
950fail0:
951 clk_disable(host->mci_clk);
952 iounmap(host->baseaddr);
953fail1:
954 clk_put(host->mci_clk);
955fail2:
956 if (host->board->vcc_pin)
957 gpio_free(host->board->vcc_pin);
958fail3:
959 if (host->board->wp_pin)
960 gpio_free(host->board->wp_pin);
961fail4:
962 if (host->board->det_pin)
963 gpio_free(host->board->det_pin);
964fail5:
965 mmc_free_host(mmc);
966fail6:
967 release_mem_region(res->start, res->end - res->start + 1);
968 dev_err(&pdev->dev, "probe failed, err %d\n", ret);
969 return ret;
Andrew Victor65dbf342006-04-02 19:18:51 +0100970}
971
972/*
973 * Remove a device
974 */
David Brownella26b4982006-12-26 14:45:26 -0800975static int __exit at91_mci_remove(struct platform_device *pdev)
Andrew Victor65dbf342006-04-02 19:18:51 +0100976{
977 struct mmc_host *mmc = platform_get_drvdata(pdev);
978 struct at91mci_host *host;
Andrew Victor17ea0592006-10-23 14:44:40 +0200979 struct resource *res;
Andrew Victor65dbf342006-04-02 19:18:51 +0100980
981 if (!mmc)
982 return -1;
983
984 host = mmc_priv(mmc);
985
Anti Sulline0cda542007-08-30 16:15:16 +0200986 if (host->board->det_pin) {
David Brownell6e996ee2008-02-04 18:12:48 +0100987 if (device_can_wakeup(&pdev->dev))
988 free_irq(gpio_to_irq(host->board->det_pin), host);
Marc Pignat63b66432007-07-16 11:07:02 +0200989 device_init_wakeup(&pdev->dev, 0);
David Brownell6e996ee2008-02-04 18:12:48 +0100990 gpio_free(host->board->det_pin);
Andrew Victor65dbf342006-04-02 19:18:51 +0100991 }
992
Andrew Victore0b19b82006-10-25 19:42:38 +0200993 at91_mci_disable(host);
Andrew Victor17ea0592006-10-23 14:44:40 +0200994 mmc_remove_host(mmc);
995 free_irq(host->irq, host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100996
Andrew Victor3dd3b032006-10-23 14:46:54 +0200997 clk_disable(host->mci_clk); /* Disable the peripheral clock */
998 clk_put(host->mci_clk);
Andrew Victor65dbf342006-04-02 19:18:51 +0100999
David Brownell6e996ee2008-02-04 18:12:48 +01001000 if (host->board->vcc_pin)
1001 gpio_free(host->board->vcc_pin);
1002 if (host->board->wp_pin)
1003 gpio_free(host->board->wp_pin);
1004
Andrew Victor17ea0592006-10-23 14:44:40 +02001005 iounmap(host->baseaddr);
1006 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1007 release_mem_region(res->start, res->end - res->start + 1);
Andrew Victor65dbf342006-04-02 19:18:51 +01001008
Andrew Victor17ea0592006-10-23 14:44:40 +02001009 mmc_free_host(mmc);
1010 platform_set_drvdata(pdev, NULL);
Andrew Victorb44fb7a2006-06-19 13:06:05 +01001011 pr_debug("MCI Removed\n");
Andrew Victor65dbf342006-04-02 19:18:51 +01001012
1013 return 0;
1014}
1015
1016#ifdef CONFIG_PM
1017static int at91_mci_suspend(struct platform_device *pdev, pm_message_t state)
1018{
1019 struct mmc_host *mmc = platform_get_drvdata(pdev);
Marc Pignat63b66432007-07-16 11:07:02 +02001020 struct at91mci_host *host = mmc_priv(mmc);
Andrew Victor65dbf342006-04-02 19:18:51 +01001021 int ret = 0;
1022
Anti Sulline0cda542007-08-30 16:15:16 +02001023 if (host->board->det_pin && device_may_wakeup(&pdev->dev))
Marc Pignat63b66432007-07-16 11:07:02 +02001024 enable_irq_wake(host->board->det_pin);
1025
Andrew Victor65dbf342006-04-02 19:18:51 +01001026 if (mmc)
1027 ret = mmc_suspend_host(mmc, state);
1028
1029 return ret;
1030}
1031
1032static int at91_mci_resume(struct platform_device *pdev)
1033{
1034 struct mmc_host *mmc = platform_get_drvdata(pdev);
Marc Pignat63b66432007-07-16 11:07:02 +02001035 struct at91mci_host *host = mmc_priv(mmc);
Andrew Victor65dbf342006-04-02 19:18:51 +01001036 int ret = 0;
1037
Anti Sulline0cda542007-08-30 16:15:16 +02001038 if (host->board->det_pin && device_may_wakeup(&pdev->dev))
Marc Pignat63b66432007-07-16 11:07:02 +02001039 disable_irq_wake(host->board->det_pin);
1040
Andrew Victor65dbf342006-04-02 19:18:51 +01001041 if (mmc)
1042 ret = mmc_resume_host(mmc);
1043
1044 return ret;
1045}
1046#else
1047#define at91_mci_suspend NULL
1048#define at91_mci_resume NULL
1049#endif
1050
1051static struct platform_driver at91_mci_driver = {
David Brownella26b4982006-12-26 14:45:26 -08001052 .remove = __exit_p(at91_mci_remove),
Andrew Victor65dbf342006-04-02 19:18:51 +01001053 .suspend = at91_mci_suspend,
1054 .resume = at91_mci_resume,
1055 .driver = {
1056 .name = DRIVER_NAME,
1057 .owner = THIS_MODULE,
1058 },
1059};
1060
1061static int __init at91_mci_init(void)
1062{
David Brownella26b4982006-12-26 14:45:26 -08001063 return platform_driver_probe(&at91_mci_driver, at91_mci_probe);
Andrew Victor65dbf342006-04-02 19:18:51 +01001064}
1065
1066static void __exit at91_mci_exit(void)
1067{
1068 platform_driver_unregister(&at91_mci_driver);
1069}
1070
1071module_init(at91_mci_init);
1072module_exit(at91_mci_exit);
1073
1074MODULE_DESCRIPTION("AT91 Multimedia Card Interface driver");
1075MODULE_AUTHOR("Nick Randell");
1076MODULE_LICENSE("GPL");
Kay Sieversbc65c722008-04-15 14:34:28 -07001077MODULE_ALIAS("platform:at91_mci");