blob: b1edcefdd4f9e7a615e2e38238f4d43e8456a9b4 [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 Victor99eeb8df2006-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 Victor99eeb8df2006-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>
73#include <asm/mach/mmc.h>
74#include <asm/arch/board.h>
Andrew Victor99eeb8df2006-12-11 12:40:23 +010075#include <asm/arch/cpu.h>
Andrew Victor65dbf342006-04-02 19:18:51 +010076#include <asm/arch/gpio.h>
Andrew Victor55d8bae2006-11-30 17:16:43 +010077#include <asm/arch/at91_mci.h>
Andrew Victor65dbf342006-04-02 19:18:51 +010078
79#define DRIVER_NAME "at91_mci"
80
Andrew Victordf05a302006-10-23 14:50:09 +020081#define FL_SENT_COMMAND (1 << 0)
82#define FL_SENT_STOP (1 << 1)
Andrew Victor65dbf342006-04-02 19:18:51 +010083
Andrew Victordf05a302006-10-23 14:50:09 +020084#define AT91_MCI_ERRORS (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE \
85 | AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE \
Nicolas Ferre37b758e2007-08-08 12:01:44 +020086 | AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)
Andrew Victor65dbf342006-04-02 19:18:51 +010087
Andrew Victore0b19b82006-10-25 19:42:38 +020088#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))
Andrew Victor65dbf342006-04-02 19:18:51 +010090
Andrew Victor65dbf342006-04-02 19:18:51 +010091
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
Andrew Victore0b19b82006-10-25 19:42:38 +0200101 void __iomem *baseaddr;
Andrew Victor17ea0592006-10-23 14:44:40 +0200102 int irq;
Andrew Victore0b19b82006-10-25 19:42:38 +0200103
Andrew Victor65dbf342006-04-02 19:18:51 +0100104 struct at91_mmc_data *board;
105 int present;
106
Andrew Victor3dd3b032006-10-23 14:46:54 +0200107 struct clk *mci_clk;
108
Andrew Victor65dbf342006-04-02 19:18:51 +0100109 /*
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;
127};
128
129/*
130 * Copy from sg to a dma block - used for transfers
131 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200132static inline void at91_mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data)
Andrew Victor65dbf342006-04-02 19:18:51 +0100133{
134 unsigned int len, i, size;
135 unsigned *dmabuf = host->buffer;
136
137 size = host->total_length;
138 len = data->sg_len;
139
140 /*
141 * Just loop through all entries. Size might not
142 * be the entire list though so make sure that
143 * we do not transfer too much.
144 */
145 for (i = 0; i < len; i++) {
146 struct scatterlist *sg;
147 int amount;
Andrew Victor65dbf342006-04-02 19:18:51 +0100148 unsigned int *sgbuffer;
149
150 sg = &data->sg[i];
151
Jens Axboe45711f12007-10-22 21:19:53 +0200152 sgbuffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
Andrew Victor65dbf342006-04-02 19:18:51 +0100153 amount = min(size, sg->length);
154 size -= amount;
Andrew Victor65dbf342006-04-02 19:18:51 +0100155
Andrew Victor99eeb8df2006-12-11 12:40:23 +0100156 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
157 int index;
158
159 for (index = 0; index < (amount / 4); index++)
160 *dmabuf++ = swab32(sgbuffer[index]);
161 }
162 else
163 memcpy(dmabuf, sgbuffer, amount);
Andrew Victor65dbf342006-04-02 19:18:51 +0100164
165 kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
166
167 if (size == 0)
168 break;
169 }
170
171 /*
172 * Check that we didn't get a request to transfer
173 * more data than can fit into the SG list.
174 */
175 BUG_ON(size != 0);
176}
177
178/*
179 * Prepare a dma read
180 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200181static void at91_mci_pre_dma_read(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100182{
183 int i;
184 struct scatterlist *sg;
185 struct mmc_command *cmd;
186 struct mmc_data *data;
187
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100188 pr_debug("pre dma read\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100189
190 cmd = host->cmd;
191 if (!cmd) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100192 pr_debug("no command\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100193 return;
194 }
195
196 data = cmd->data;
197 if (!data) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100198 pr_debug("no data\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100199 return;
200 }
201
202 for (i = 0; i < 2; i++) {
203 /* nothing left to transfer */
204 if (host->transfer_index >= data->sg_len) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100205 pr_debug("Nothing left to transfer (index = %d)\n", host->transfer_index);
Andrew Victor65dbf342006-04-02 19:18:51 +0100206 break;
207 }
208
209 /* Check to see if this needs filling */
210 if (i == 0) {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100211 if (at91_mci_read(host, ATMEL_PDC_RCR) != 0) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100212 pr_debug("Transfer active in current\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100213 continue;
214 }
215 }
216 else {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100217 if (at91_mci_read(host, ATMEL_PDC_RNCR) != 0) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100218 pr_debug("Transfer active in next\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100219 continue;
220 }
221 }
222
223 /* Setup the next transfer */
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100224 pr_debug("Using transfer index %d\n", host->transfer_index);
Andrew Victor65dbf342006-04-02 19:18:51 +0100225
226 sg = &data->sg[host->transfer_index++];
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100227 pr_debug("sg = %p\n", sg);
Andrew Victor65dbf342006-04-02 19:18:51 +0100228
Jens Axboe45711f12007-10-22 21:19:53 +0200229 sg->dma_address = dma_map_page(NULL, sg_page(sg), sg->offset, sg->length, DMA_FROM_DEVICE);
Andrew Victor65dbf342006-04-02 19:18:51 +0100230
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100231 pr_debug("dma address = %08X, length = %d\n", sg->dma_address, sg->length);
Andrew Victor65dbf342006-04-02 19:18:51 +0100232
233 if (i == 0) {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100234 at91_mci_write(host, ATMEL_PDC_RPR, sg->dma_address);
235 at91_mci_write(host, ATMEL_PDC_RCR, sg->length / 4);
Andrew Victor65dbf342006-04-02 19:18:51 +0100236 }
237 else {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100238 at91_mci_write(host, ATMEL_PDC_RNPR, sg->dma_address);
239 at91_mci_write(host, ATMEL_PDC_RNCR, sg->length / 4);
Andrew Victor65dbf342006-04-02 19:18:51 +0100240 }
241 }
242
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100243 pr_debug("pre dma read done\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100244}
245
246/*
247 * Handle after a dma read
248 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200249static void at91_mci_post_dma_read(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100250{
251 struct mmc_command *cmd;
252 struct mmc_data *data;
253
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100254 pr_debug("post dma read\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100255
256 cmd = host->cmd;
257 if (!cmd) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100258 pr_debug("no command\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100259 return;
260 }
261
262 data = cmd->data;
263 if (!data) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100264 pr_debug("no data\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100265 return;
266 }
267
268 while (host->in_use_index < host->transfer_index) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100269 struct scatterlist *sg;
270
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100271 pr_debug("finishing index %d\n", host->in_use_index);
Andrew Victor65dbf342006-04-02 19:18:51 +0100272
273 sg = &data->sg[host->in_use_index++];
274
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100275 pr_debug("Unmapping page %08X\n", sg->dma_address);
Andrew Victor65dbf342006-04-02 19:18:51 +0100276
277 dma_unmap_page(NULL, sg->dma_address, sg->length, DMA_FROM_DEVICE);
278
Andrew Victor65dbf342006-04-02 19:18:51 +0100279 data->bytes_xfered += sg->length;
280
Andrew Victor99eeb8df2006-12-11 12:40:23 +0100281 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
Nicolas Ferreed99c542007-07-09 14:58:16 +0200282 unsigned int *buffer;
Andrew Victor99eeb8df2006-12-11 12:40:23 +0100283 int index;
Andrew Victor65dbf342006-04-02 19:18:51 +0100284
Nicolas Ferreed99c542007-07-09 14:58:16 +0200285 /* Swap the contents of the buffer */
Jens Axboe45711f12007-10-22 21:19:53 +0200286 buffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
Nicolas Ferreed99c542007-07-09 14:58:16 +0200287 pr_debug("buffer = %p, length = %d\n", buffer, sg->length);
288
Andrew Victor99eeb8df2006-12-11 12:40:23 +0100289 for (index = 0; index < (sg->length / 4); index++)
290 buffer[index] = swab32(buffer[index]);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200291
292 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
Andrew Victor65dbf342006-04-02 19:18:51 +0100293 }
Andrew Victor99eeb8df2006-12-11 12:40:23 +0100294
Jens Axboe45711f12007-10-22 21:19:53 +0200295 flush_dcache_page(sg_page(sg));
Andrew Victor65dbf342006-04-02 19:18:51 +0100296 }
297
298 /* Is there another transfer to trigger? */
299 if (host->transfer_index < data->sg_len)
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200300 at91_mci_pre_dma_read(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100301 else {
Nicolas Ferreed99c542007-07-09 14:58:16 +0200302 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_ENDRX);
Andrew Victore0b19b82006-10-25 19:42:38 +0200303 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_RXBUFF);
Andrew Victor65dbf342006-04-02 19:18:51 +0100304 }
305
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100306 pr_debug("post dma read done\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100307}
308
309/*
310 * Handle transmitted data
311 */
312static void at91_mci_handle_transmitted(struct at91mci_host *host)
313{
314 struct mmc_command *cmd;
315 struct mmc_data *data;
316
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100317 pr_debug("Handling the transmit\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100318
319 /* Disable the transfer */
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100320 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100321
322 /* Now wait for cmd ready */
Andrew Victore0b19b82006-10-25 19:42:38 +0200323 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE);
Andrew Victor65dbf342006-04-02 19:18:51 +0100324
325 cmd = host->cmd;
326 if (!cmd) return;
327
328 data = cmd->data;
329 if (!data) return;
330
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200331 if (cmd->data->blocks > 1) {
Nicolas Ferreed99c542007-07-09 14:58:16 +0200332 pr_debug("multiple write : wait for BLKE...\n");
333 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
334 } else
335 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
336
Andrew Victor65dbf342006-04-02 19:18:51 +0100337 data->bytes_xfered = host->total_length;
338}
339
Nicolas Ferreed99c542007-07-09 14:58:16 +0200340/*Handle after command sent ready*/
341static int at91_mci_handle_cmdrdy(struct at91mci_host *host)
342{
343 if (!host->cmd)
344 return 1;
345 else if (!host->cmd->data) {
346 if (host->flags & FL_SENT_STOP) {
347 /*After multi block write, we must wait for NOTBUSY*/
348 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
349 } else return 1;
350 } else if (host->cmd->data->flags & MMC_DATA_WRITE) {
351 /*After sendding multi-block-write command, start DMA transfer*/
352 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_TXBUFE);
353 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
354 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
355 }
356
357 /* command not completed, have to wait */
358 return 0;
359}
360
361
Andrew Victor65dbf342006-04-02 19:18:51 +0100362/*
363 * Enable the controller
364 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200365static void at91_mci_enable(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100366{
Nicolas Ferreed99c542007-07-09 14:58:16 +0200367 unsigned int mr;
368
Andrew Victore0b19b82006-10-25 19:42:38 +0200369 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200370 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
Andrew Victore0b19b82006-10-25 19:42:38 +0200371 at91_mci_write(host, AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200372 mr = AT91_MCI_PDCMODE | 0x34a;
373
374 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
375 mr |= AT91_MCI_RDPROOF | AT91_MCI_WRPROOF;
376
377 at91_mci_write(host, AT91_MCI_MR, mr);
Andrew Victor99eeb8df2006-12-11 12:40:23 +0100378
379 /* use Slot A or B (only one at same time) */
380 at91_mci_write(host, AT91_MCI_SDCR, host->board->slot_b);
Andrew Victor65dbf342006-04-02 19:18:51 +0100381}
382
383/*
384 * Disable the controller
385 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200386static void at91_mci_disable(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100387{
Andrew Victore0b19b82006-10-25 19:42:38 +0200388 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
Andrew Victor65dbf342006-04-02 19:18:51 +0100389}
390
391/*
392 * Send a command
Andrew Victor65dbf342006-04-02 19:18:51 +0100393 */
Nicolas Ferreed99c542007-07-09 14:58:16 +0200394static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd)
Andrew Victor65dbf342006-04-02 19:18:51 +0100395{
396 unsigned int cmdr, mr;
397 unsigned int block_length;
398 struct mmc_data *data = cmd->data;
399
400 unsigned int blocks;
401 unsigned int ier = 0;
402
403 host->cmd = cmd;
404
Nicolas Ferreed99c542007-07-09 14:58:16 +0200405 /* Needed for leaving busy state before CMD1 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200406 if ((at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100407 pr_debug("Clearing timeout\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200408 at91_mci_write(host, AT91_MCI_ARGR, 0);
409 at91_mci_write(host, AT91_MCI_CMDR, AT91_MCI_OPDCMD);
410 while (!(at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_CMDRDY)) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100411 /* spin */
Andrew Victore0b19b82006-10-25 19:42:38 +0200412 pr_debug("Clearing: SR = %08X\n", at91_mci_read(host, AT91_MCI_SR));
Andrew Victor65dbf342006-04-02 19:18:51 +0100413 }
414 }
Nicolas Ferreed99c542007-07-09 14:58:16 +0200415
Andrew Victor65dbf342006-04-02 19:18:51 +0100416 cmdr = cmd->opcode;
417
418 if (mmc_resp_type(cmd) == MMC_RSP_NONE)
419 cmdr |= AT91_MCI_RSPTYP_NONE;
420 else {
421 /* if a response is expected then allow maximum response latancy */
422 cmdr |= AT91_MCI_MAXLAT;
423 /* set 136 bit response for R2, 48 bit response otherwise */
424 if (mmc_resp_type(cmd) == MMC_RSP_R2)
425 cmdr |= AT91_MCI_RSPTYP_136;
426 else
427 cmdr |= AT91_MCI_RSPTYP_48;
428 }
429
430 if (data) {
Marc Pignat1d4de9e2007-08-09 13:56:29 +0200431
432 if ( data->blksz & 0x3 ) {
433 pr_debug("Unsupported block size\n");
434 cmd->error = -EINVAL;
435 mmc_request_done(host->mmc, host->request);
436 return;
437 }
438
Russell Kinga3fd4a12006-06-04 17:51:15 +0100439 block_length = data->blksz;
Andrew Victor65dbf342006-04-02 19:18:51 +0100440 blocks = data->blocks;
441
442 /* always set data start - also set direction flag for read */
443 if (data->flags & MMC_DATA_READ)
444 cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START);
445 else if (data->flags & MMC_DATA_WRITE)
446 cmdr |= AT91_MCI_TRCMD_START;
447
448 if (data->flags & MMC_DATA_STREAM)
449 cmdr |= AT91_MCI_TRTYP_STREAM;
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200450 if (data->blocks > 1)
Andrew Victor65dbf342006-04-02 19:18:51 +0100451 cmdr |= AT91_MCI_TRTYP_MULTIPLE;
452 }
453 else {
454 block_length = 0;
455 blocks = 0;
456 }
457
Marc Pignatb6cedb32007-06-06 20:27:59 +0200458 if (host->flags & FL_SENT_STOP)
Andrew Victor65dbf342006-04-02 19:18:51 +0100459 cmdr |= AT91_MCI_TRCMD_STOP;
460
461 if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
462 cmdr |= AT91_MCI_OPDCMD;
463
464 /*
465 * Set the arguments and send the command
466 */
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200467 pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",
Andrew Victore0b19b82006-10-25 19:42:38 +0200468 cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(host, AT91_MCI_MR));
Andrew Victor65dbf342006-04-02 19:18:51 +0100469
470 if (!data) {
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100471 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS | ATMEL_PDC_RXTDIS);
472 at91_mci_write(host, ATMEL_PDC_RPR, 0);
473 at91_mci_write(host, ATMEL_PDC_RCR, 0);
474 at91_mci_write(host, ATMEL_PDC_RNPR, 0);
475 at91_mci_write(host, ATMEL_PDC_RNCR, 0);
476 at91_mci_write(host, ATMEL_PDC_TPR, 0);
477 at91_mci_write(host, ATMEL_PDC_TCR, 0);
478 at91_mci_write(host, ATMEL_PDC_TNPR, 0);
479 at91_mci_write(host, ATMEL_PDC_TNCR, 0);
Nicolas Ferreed99c542007-07-09 14:58:16 +0200480 ier = AT91_MCI_CMDRDY;
481 } else {
482 /* zero block length and PDC mode */
483 mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff;
484 at91_mci_write(host, AT91_MCI_MR, mr | (block_length << 16) | AT91_MCI_PDCMODE);
Andrew Victor65dbf342006-04-02 19:18:51 +0100485
Nicolas Ferreed99c542007-07-09 14:58:16 +0200486 /*
487 * Disable the PDC controller
488 */
489 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100490
Nicolas Ferreed99c542007-07-09 14:58:16 +0200491 if (cmdr & AT91_MCI_TRCMD_START) {
492 data->bytes_xfered = 0;
493 host->transfer_index = 0;
494 host->in_use_index = 0;
495 if (cmdr & AT91_MCI_TRDIR) {
496 /*
497 * Handle a read
498 */
499 host->buffer = NULL;
500 host->total_length = 0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100501
Nicolas Ferreed99c542007-07-09 14:58:16 +0200502 at91_mci_pre_dma_read(host);
503 ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
504 }
505 else {
506 /*
507 * Handle a write
508 */
509 host->total_length = block_length * blocks;
510 host->buffer = dma_alloc_coherent(NULL,
511 host->total_length,
512 &host->physical_address, GFP_KERNEL);
Andrew Victor65dbf342006-04-02 19:18:51 +0100513
Nicolas Ferreed99c542007-07-09 14:58:16 +0200514 at91_mci_sg_to_dma(host, data);
Andrew Victor65dbf342006-04-02 19:18:51 +0100515
Nicolas Ferreed99c542007-07-09 14:58:16 +0200516 pr_debug("Transmitting %d bytes\n", host->total_length);
Andrew Victor65dbf342006-04-02 19:18:51 +0100517
Nicolas Ferreed99c542007-07-09 14:58:16 +0200518 at91_mci_write(host, ATMEL_PDC_TPR, host->physical_address);
519 at91_mci_write(host, ATMEL_PDC_TCR, host->total_length / 4);
520 ier = AT91_MCI_CMDRDY;
521 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100522 }
523 }
524
525 /*
526 * Send the command and then enable the PDC - not the other way round as
527 * the data sheet says
528 */
529
Andrew Victore0b19b82006-10-25 19:42:38 +0200530 at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
531 at91_mci_write(host, AT91_MCI_CMDR, cmdr);
Andrew Victor65dbf342006-04-02 19:18:51 +0100532
533 if (cmdr & AT91_MCI_TRCMD_START) {
534 if (cmdr & AT91_MCI_TRDIR)
Andrew Victor93a3ddc2007-02-08 11:31:22 +0100535 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Andrew Victor65dbf342006-04-02 19:18:51 +0100536 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100537
Nicolas Ferreed99c542007-07-09 14:58:16 +0200538 /* Enable selected interrupts */
Andrew Victordf05a302006-10-23 14:50:09 +0200539 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_ERRORS | ier);
Andrew Victor65dbf342006-04-02 19:18:51 +0100540}
541
542/*
543 * Process the next step in the request
544 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200545static void at91_mci_process_next(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100546{
547 if (!(host->flags & FL_SENT_COMMAND)) {
548 host->flags |= FL_SENT_COMMAND;
Nicolas Ferreed99c542007-07-09 14:58:16 +0200549 at91_mci_send_command(host, host->request->cmd);
Andrew Victor65dbf342006-04-02 19:18:51 +0100550 }
551 else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) {
552 host->flags |= FL_SENT_STOP;
Nicolas Ferreed99c542007-07-09 14:58:16 +0200553 at91_mci_send_command(host, host->request->stop);
Andrew Victor65dbf342006-04-02 19:18:51 +0100554 }
555 else
556 mmc_request_done(host->mmc, host->request);
557}
558
559/*
560 * Handle a command that has been completed
561 */
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200562static void at91_mci_completed_command(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100563{
564 struct mmc_command *cmd = host->cmd;
565 unsigned int status;
566
Andrew Victore0b19b82006-10-25 19:42:38 +0200567 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
Andrew Victor65dbf342006-04-02 19:18:51 +0100568
Andrew Victore0b19b82006-10-25 19:42:38 +0200569 cmd->resp[0] = at91_mci_read(host, AT91_MCI_RSPR(0));
570 cmd->resp[1] = at91_mci_read(host, AT91_MCI_RSPR(1));
571 cmd->resp[2] = at91_mci_read(host, AT91_MCI_RSPR(2));
572 cmd->resp[3] = at91_mci_read(host, AT91_MCI_RSPR(3));
Andrew Victor65dbf342006-04-02 19:18:51 +0100573
574 if (host->buffer) {
575 dma_free_coherent(NULL, host->total_length, host->buffer, host->physical_address);
576 host->buffer = NULL;
577 }
578
Andrew Victore0b19b82006-10-25 19:42:38 +0200579 status = at91_mci_read(host, AT91_MCI_SR);
Andrew Victor65dbf342006-04-02 19:18:51 +0100580
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100581 pr_debug("Status = %08X [%08X %08X %08X %08X]\n",
Andrew Victor65dbf342006-04-02 19:18:51 +0100582 status, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
583
Andrew Victor9e3866b2007-10-17 11:53:40 +0200584 if (status & AT91_MCI_ERRORS) {
Marc Pignatb6cedb32007-06-06 20:27:59 +0200585 if ((status & AT91_MCI_RCRCE) && !(mmc_resp_type(cmd) & MMC_RSP_CRC)) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200586 cmd->error = 0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100587 }
588 else {
589 if (status & (AT91_MCI_RTOE | AT91_MCI_DTOE))
Pierre Ossman17b04292007-07-22 22:18:46 +0200590 cmd->error = -ETIMEDOUT;
Andrew Victor65dbf342006-04-02 19:18:51 +0100591 else if (status & (AT91_MCI_RCRCE | AT91_MCI_DCRCE))
Pierre Ossman17b04292007-07-22 22:18:46 +0200592 cmd->error = -EILSEQ;
Andrew Victor65dbf342006-04-02 19:18:51 +0100593 else
Pierre Ossman17b04292007-07-22 22:18:46 +0200594 cmd->error = -EIO;
Andrew Victor65dbf342006-04-02 19:18:51 +0100595
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100596 pr_debug("Error detected and set to %d (cmd = %d, retries = %d)\n",
Andrew Victor65dbf342006-04-02 19:18:51 +0100597 cmd->error, cmd->opcode, cmd->retries);
598 }
599 }
600 else
Pierre Ossman17b04292007-07-22 22:18:46 +0200601 cmd->error = 0;
Andrew Victor65dbf342006-04-02 19:18:51 +0100602
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200603 at91_mci_process_next(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100604}
605
606/*
607 * Handle an MMC request
608 */
609static void at91_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
610{
611 struct at91mci_host *host = mmc_priv(mmc);
612 host->request = mrq;
613 host->flags = 0;
614
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200615 at91_mci_process_next(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100616}
617
618/*
619 * Set the IOS
620 */
621static void at91_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
622{
623 int clkdiv;
624 struct at91mci_host *host = mmc_priv(mmc);
Andrew Victor3dd3b032006-10-23 14:46:54 +0200625 unsigned long at91_master_clock = clk_get_rate(host->mci_clk);
Andrew Victor65dbf342006-04-02 19:18:51 +0100626
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100627 host->bus_mode = ios->bus_mode;
Andrew Victor65dbf342006-04-02 19:18:51 +0100628
629 if (ios->clock == 0) {
630 /* Disable the MCI controller */
Andrew Victore0b19b82006-10-25 19:42:38 +0200631 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100632 clkdiv = 0;
633 }
634 else {
635 /* Enable the MCI controller */
Andrew Victore0b19b82006-10-25 19:42:38 +0200636 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
Andrew Victor65dbf342006-04-02 19:18:51 +0100637
638 if ((at91_master_clock % (ios->clock * 2)) == 0)
639 clkdiv = ((at91_master_clock / ios->clock) / 2) - 1;
640 else
641 clkdiv = (at91_master_clock / ios->clock) / 2;
642
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100643 pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv,
Andrew Victor65dbf342006-04-02 19:18:51 +0100644 at91_master_clock / (2 * (clkdiv + 1)));
645 }
646 if (ios->bus_width == MMC_BUS_WIDTH_4 && host->board->wire4) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100647 pr_debug("MMC: Setting controller bus width to 4\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200648 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) | AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100649 }
650 else {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100651 pr_debug("MMC: Setting controller bus width to 1\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200652 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100653 }
654
655 /* Set the clock divider */
Andrew Victore0b19b82006-10-25 19:42:38 +0200656 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 +0100657
658 /* maybe switch power to the card */
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100659 if (host->board->vcc_pin) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100660 switch (ios->power_mode) {
661 case MMC_POWER_OFF:
Andrew Victor99eeb8df2006-12-11 12:40:23 +0100662 at91_set_gpio_value(host->board->vcc_pin, 0);
Andrew Victor65dbf342006-04-02 19:18:51 +0100663 break;
664 case MMC_POWER_UP:
665 case MMC_POWER_ON:
Andrew Victor99eeb8df2006-12-11 12:40:23 +0100666 at91_set_gpio_value(host->board->vcc_pin, 1);
Andrew Victor65dbf342006-04-02 19:18:51 +0100667 break;
668 }
669 }
670}
671
672/*
673 * Handle an interrupt
674 */
David Howells7d12e782006-10-05 14:55:46 +0100675static irqreturn_t at91_mci_irq(int irq, void *devid)
Andrew Victor65dbf342006-04-02 19:18:51 +0100676{
677 struct at91mci_host *host = devid;
678 int completed = 0;
Andrew Victordf05a302006-10-23 14:50:09 +0200679 unsigned int int_status, int_mask;
Andrew Victor65dbf342006-04-02 19:18:51 +0100680
Andrew Victore0b19b82006-10-25 19:42:38 +0200681 int_status = at91_mci_read(host, AT91_MCI_SR);
Andrew Victordf05a302006-10-23 14:50:09 +0200682 int_mask = at91_mci_read(host, AT91_MCI_IMR);
Nicolas Ferre37b758e2007-08-08 12:01:44 +0200683
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200684 pr_debug("MCI irq: status = %08X, %08X, %08X\n", int_status, int_mask,
Andrew Victordf05a302006-10-23 14:50:09 +0200685 int_status & int_mask);
Nicolas Ferre37b758e2007-08-08 12:01:44 +0200686
Andrew Victordf05a302006-10-23 14:50:09 +0200687 int_status = int_status & int_mask;
Andrew Victor65dbf342006-04-02 19:18:51 +0100688
Andrew Victordf05a302006-10-23 14:50:09 +0200689 if (int_status & AT91_MCI_ERRORS) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100690 completed = 1;
Nicolas Ferre37b758e2007-08-08 12:01:44 +0200691
Andrew Victordf05a302006-10-23 14:50:09 +0200692 if (int_status & AT91_MCI_UNRE)
693 pr_debug("MMC: Underrun error\n");
694 if (int_status & AT91_MCI_OVRE)
695 pr_debug("MMC: Overrun error\n");
696 if (int_status & AT91_MCI_DTOE)
697 pr_debug("MMC: Data timeout\n");
698 if (int_status & AT91_MCI_DCRCE)
699 pr_debug("MMC: CRC error in data\n");
700 if (int_status & AT91_MCI_RTOE)
701 pr_debug("MMC: Response timeout\n");
702 if (int_status & AT91_MCI_RENDE)
703 pr_debug("MMC: Response end bit error\n");
704 if (int_status & AT91_MCI_RCRCE)
705 pr_debug("MMC: Response CRC error\n");
706 if (int_status & AT91_MCI_RDIRE)
707 pr_debug("MMC: Response direction error\n");
708 if (int_status & AT91_MCI_RINDE)
709 pr_debug("MMC: Response index error\n");
710 } else {
711 /* Only continue processing if no errors */
Andrew Victor65dbf342006-04-02 19:18:51 +0100712
Andrew Victor65dbf342006-04-02 19:18:51 +0100713 if (int_status & AT91_MCI_TXBUFE) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100714 pr_debug("TX buffer empty\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100715 at91_mci_handle_transmitted(host);
716 }
717
Nicolas Ferreed99c542007-07-09 14:58:16 +0200718 if (int_status & AT91_MCI_ENDRX) {
719 pr_debug("ENDRX\n");
720 at91_mci_post_dma_read(host);
721 }
722
Andrew Victor65dbf342006-04-02 19:18:51 +0100723 if (int_status & AT91_MCI_RXBUFF) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100724 pr_debug("RX buffer full\n");
Nicolas Ferreed99c542007-07-09 14:58:16 +0200725 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
726 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_RXBUFF | AT91_MCI_ENDRX);
727 completed = 1;
Andrew Victor65dbf342006-04-02 19:18:51 +0100728 }
729
Andrew Victordf05a302006-10-23 14:50:09 +0200730 if (int_status & AT91_MCI_ENDTX)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100731 pr_debug("Transmit has ended\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100732
Andrew Victor65dbf342006-04-02 19:18:51 +0100733 if (int_status & AT91_MCI_NOTBUSY) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100734 pr_debug("Card is ready\n");
Nicolas Ferreed99c542007-07-09 14:58:16 +0200735 completed = 1;
Andrew Victor65dbf342006-04-02 19:18:51 +0100736 }
737
Andrew Victordf05a302006-10-23 14:50:09 +0200738 if (int_status & AT91_MCI_DTIP)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100739 pr_debug("Data transfer in progress\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100740
Nicolas Ferreed99c542007-07-09 14:58:16 +0200741 if (int_status & AT91_MCI_BLKE) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100742 pr_debug("Block transfer has ended\n");
Nicolas Ferreed99c542007-07-09 14:58:16 +0200743 completed = 1;
744 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100745
Andrew Victordf05a302006-10-23 14:50:09 +0200746 if (int_status & AT91_MCI_TXRDY)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100747 pr_debug("Ready to transmit\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100748
Andrew Victordf05a302006-10-23 14:50:09 +0200749 if (int_status & AT91_MCI_RXRDY)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100750 pr_debug("Ready to receive\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100751
752 if (int_status & AT91_MCI_CMDRDY) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100753 pr_debug("Command ready\n");
Nicolas Ferreed99c542007-07-09 14:58:16 +0200754 completed = at91_mci_handle_cmdrdy(host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100755 }
756 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100757
758 if (completed) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100759 pr_debug("Completed command\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200760 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
Nicolas Ferree8d04d32007-06-19 18:32:34 +0200761 at91_mci_completed_command(host);
Andrew Victordf05a302006-10-23 14:50:09 +0200762 } else
763 at91_mci_write(host, AT91_MCI_IDR, int_status);
Andrew Victor65dbf342006-04-02 19:18:51 +0100764
765 return IRQ_HANDLED;
766}
767
David Howells7d12e782006-10-05 14:55:46 +0100768static irqreturn_t at91_mmc_det_irq(int irq, void *_host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100769{
770 struct at91mci_host *host = _host;
771 int present = !at91_get_gpio_value(irq);
772
773 /*
774 * we expect this irq on both insert and remove,
775 * and use a short delay to debounce.
776 */
777 if (present != host->present) {
778 host->present = present;
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100779 pr_debug("%s: card %s\n", mmc_hostname(host->mmc),
Andrew Victor65dbf342006-04-02 19:18:51 +0100780 present ? "insert" : "remove");
781 if (!present) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100782 pr_debug("****** Resetting SD-card bus width ******\n");
Andrew Victor99eeb8df2006-12-11 12:40:23 +0100783 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100784 }
785 mmc_detect_change(host->mmc, msecs_to_jiffies(100));
786 }
787 return IRQ_HANDLED;
788}
789
David Brownella26b4982006-12-26 14:45:26 -0800790static int at91_mci_get_ro(struct mmc_host *mmc)
Andrew Victor65dbf342006-04-02 19:18:51 +0100791{
792 int read_only = 0;
793 struct at91mci_host *host = mmc_priv(mmc);
794
795 if (host->board->wp_pin) {
796 read_only = at91_get_gpio_value(host->board->wp_pin);
797 printk(KERN_WARNING "%s: card is %s\n", mmc_hostname(mmc),
798 (read_only ? "read-only" : "read-write") );
799 }
800 else {
801 printk(KERN_WARNING "%s: host does not support reading read-only "
802 "switch. Assuming write-enable.\n", mmc_hostname(mmc));
803 }
804 return read_only;
805}
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 Victorb44fb7a2006-06-19 13:06:05 +0100823 pr_debug("Probe MCI devices\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100824
Andrew Victor17ea0592006-10-23 14:44:40 +0200825 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
826 if (!res)
827 return -ENXIO;
828
829 if (!request_mem_region(res->start, res->end - res->start + 1, DRIVER_NAME))
830 return -EBUSY;
831
Andrew Victor65dbf342006-04-02 19:18:51 +0100832 mmc = mmc_alloc_host(sizeof(struct at91mci_host), &pdev->dev);
833 if (!mmc) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100834 pr_debug("Failed to allocate mmc host\n");
Andrew Victor17ea0592006-10-23 14:44:40 +0200835 release_mem_region(res->start, res->end - res->start + 1);
Andrew Victor65dbf342006-04-02 19:18:51 +0100836 return -ENOMEM;
837 }
838
839 mmc->ops = &at91_mci_ops;
840 mmc->f_min = 375000;
841 mmc->f_max = 25000000;
842 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
843
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100844 mmc->max_blk_size = 4095;
Pierre Ossman55db8902006-11-21 17:55:45 +0100845 mmc->max_blk_count = mmc->max_req_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100846
Andrew Victor65dbf342006-04-02 19:18:51 +0100847 host = mmc_priv(mmc);
848 host->mmc = mmc;
849 host->buffer = NULL;
850 host->bus_mode = 0;
851 host->board = pdev->dev.platform_data;
852 if (host->board->wire4) {
Nicolas Ferreed99c542007-07-09 14:58:16 +0200853 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
854 mmc->caps |= MMC_CAP_4_BIT_DATA;
855 else
856 printk("AT91 MMC: 4 wire bus mode not supported"
857 " - using 1 wire\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100858 }
859
860 /*
861 * Get Clock
862 */
Andrew Victor3dd3b032006-10-23 14:46:54 +0200863 host->mci_clk = clk_get(&pdev->dev, "mci_clk");
864 if (IS_ERR(host->mci_clk)) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100865 printk(KERN_ERR "AT91 MMC: no clock defined.\n");
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100866 mmc_free_host(mmc);
Andrew Victor17ea0592006-10-23 14:44:40 +0200867 release_mem_region(res->start, res->end - res->start + 1);
Andrew Victor65dbf342006-04-02 19:18:51 +0100868 return -ENODEV;
869 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100870
Andrew Victor17ea0592006-10-23 14:44:40 +0200871 /*
872 * Map I/O region
873 */
874 host->baseaddr = ioremap(res->start, res->end - res->start + 1);
875 if (!host->baseaddr) {
Andrew Victor3dd3b032006-10-23 14:46:54 +0200876 clk_put(host->mci_clk);
Andrew Victor17ea0592006-10-23 14:44:40 +0200877 mmc_free_host(mmc);
878 release_mem_region(res->start, res->end - res->start + 1);
879 return -ENOMEM;
880 }
Andrew Victore0b19b82006-10-25 19:42:38 +0200881
882 /*
883 * Reset hardware
884 */
Andrew Victor3dd3b032006-10-23 14:46:54 +0200885 clk_enable(host->mci_clk); /* Enable the peripheral clock */
Andrew Victore0b19b82006-10-25 19:42:38 +0200886 at91_mci_disable(host);
887 at91_mci_enable(host);
888
Andrew Victor65dbf342006-04-02 19:18:51 +0100889 /*
890 * Allocate the MCI interrupt
891 */
Andrew Victor17ea0592006-10-23 14:44:40 +0200892 host->irq = platform_get_irq(pdev, 0);
893 ret = request_irq(host->irq, at91_mci_irq, IRQF_SHARED, DRIVER_NAME, host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100894 if (ret) {
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200895 printk(KERN_ERR "AT91 MMC: Failed to request MCI interrupt\n");
Andrew Victor3dd3b032006-10-23 14:46:54 +0200896 clk_disable(host->mci_clk);
897 clk_put(host->mci_clk);
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100898 mmc_free_host(mmc);
Andrew Victor17ea0592006-10-23 14:44:40 +0200899 iounmap(host->baseaddr);
900 release_mem_region(res->start, res->end - res->start + 1);
Andrew Victor65dbf342006-04-02 19:18:51 +0100901 return ret;
902 }
903
904 platform_set_drvdata(pdev, mmc);
905
906 /*
907 * Add host to MMC layer
908 */
Marc Pignat63b66432007-07-16 11:07:02 +0200909 if (host->board->det_pin) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100910 host->present = !at91_get_gpio_value(host->board->det_pin);
Marc Pignat63b66432007-07-16 11:07:02 +0200911 device_init_wakeup(&pdev->dev, 1);
912 }
Andrew Victor65dbf342006-04-02 19:18:51 +0100913 else
914 host->present = -1;
915
916 mmc_add_host(mmc);
917
918 /*
919 * monitor card insertion/removal if we can
920 */
921 if (host->board->det_pin) {
922 ret = request_irq(host->board->det_pin, at91_mmc_det_irq,
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100923 0, DRIVER_NAME, host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100924 if (ret)
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200925 printk(KERN_ERR "AT91 MMC: Couldn't allocate MMC detect irq\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100926 }
927
Andrew Victorf3a8efa2006-10-23 14:53:20 +0200928 pr_debug("Added MCI driver\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100929
930 return 0;
931}
932
933/*
934 * Remove a device
935 */
David Brownella26b4982006-12-26 14:45:26 -0800936static int __exit at91_mci_remove(struct platform_device *pdev)
Andrew Victor65dbf342006-04-02 19:18:51 +0100937{
938 struct mmc_host *mmc = platform_get_drvdata(pdev);
939 struct at91mci_host *host;
Andrew Victor17ea0592006-10-23 14:44:40 +0200940 struct resource *res;
Andrew Victor65dbf342006-04-02 19:18:51 +0100941
942 if (!mmc)
943 return -1;
944
945 host = mmc_priv(mmc);
946
Anti Sulline0cda542007-08-30 16:15:16 +0200947 if (host->board->det_pin) {
Marc Pignat63b66432007-07-16 11:07:02 +0200948 device_init_wakeup(&pdev->dev, 0);
Andrew Victor65dbf342006-04-02 19:18:51 +0100949 free_irq(host->board->det_pin, host);
950 cancel_delayed_work(&host->mmc->detect);
951 }
952
Andrew Victore0b19b82006-10-25 19:42:38 +0200953 at91_mci_disable(host);
Andrew Victor17ea0592006-10-23 14:44:40 +0200954 mmc_remove_host(mmc);
955 free_irq(host->irq, host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100956
Andrew Victor3dd3b032006-10-23 14:46:54 +0200957 clk_disable(host->mci_clk); /* Disable the peripheral clock */
958 clk_put(host->mci_clk);
Andrew Victor65dbf342006-04-02 19:18:51 +0100959
Andrew Victor17ea0592006-10-23 14:44:40 +0200960 iounmap(host->baseaddr);
961 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
962 release_mem_region(res->start, res->end - res->start + 1);
Andrew Victor65dbf342006-04-02 19:18:51 +0100963
Andrew Victor17ea0592006-10-23 14:44:40 +0200964 mmc_free_host(mmc);
965 platform_set_drvdata(pdev, NULL);
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100966 pr_debug("MCI Removed\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100967
968 return 0;
969}
970
971#ifdef CONFIG_PM
972static int at91_mci_suspend(struct platform_device *pdev, pm_message_t state)
973{
974 struct mmc_host *mmc = platform_get_drvdata(pdev);
Marc Pignat63b66432007-07-16 11:07:02 +0200975 struct at91mci_host *host = mmc_priv(mmc);
Andrew Victor65dbf342006-04-02 19:18:51 +0100976 int ret = 0;
977
Anti Sulline0cda542007-08-30 16:15:16 +0200978 if (host->board->det_pin && device_may_wakeup(&pdev->dev))
Marc Pignat63b66432007-07-16 11:07:02 +0200979 enable_irq_wake(host->board->det_pin);
980
Andrew Victor65dbf342006-04-02 19:18:51 +0100981 if (mmc)
982 ret = mmc_suspend_host(mmc, state);
983
984 return ret;
985}
986
987static int at91_mci_resume(struct platform_device *pdev)
988{
989 struct mmc_host *mmc = platform_get_drvdata(pdev);
Marc Pignat63b66432007-07-16 11:07:02 +0200990 struct at91mci_host *host = mmc_priv(mmc);
Andrew Victor65dbf342006-04-02 19:18:51 +0100991 int ret = 0;
992
Anti Sulline0cda542007-08-30 16:15:16 +0200993 if (host->board->det_pin && device_may_wakeup(&pdev->dev))
Marc Pignat63b66432007-07-16 11:07:02 +0200994 disable_irq_wake(host->board->det_pin);
995
Andrew Victor65dbf342006-04-02 19:18:51 +0100996 if (mmc)
997 ret = mmc_resume_host(mmc);
998
999 return ret;
1000}
1001#else
1002#define at91_mci_suspend NULL
1003#define at91_mci_resume NULL
1004#endif
1005
1006static struct platform_driver at91_mci_driver = {
David Brownella26b4982006-12-26 14:45:26 -08001007 .remove = __exit_p(at91_mci_remove),
Andrew Victor65dbf342006-04-02 19:18:51 +01001008 .suspend = at91_mci_suspend,
1009 .resume = at91_mci_resume,
1010 .driver = {
1011 .name = DRIVER_NAME,
1012 .owner = THIS_MODULE,
1013 },
1014};
1015
1016static int __init at91_mci_init(void)
1017{
David Brownella26b4982006-12-26 14:45:26 -08001018 return platform_driver_probe(&at91_mci_driver, at91_mci_probe);
Andrew Victor65dbf342006-04-02 19:18:51 +01001019}
1020
1021static void __exit at91_mci_exit(void)
1022{
1023 platform_driver_unregister(&at91_mci_driver);
1024}
1025
1026module_init(at91_mci_init);
1027module_exit(at91_mci_exit);
1028
1029MODULE_DESCRIPTION("AT91 Multimedia Card Interface driver");
1030MODULE_AUTHOR("Nick Randell");
1031MODULE_LICENSE("GPL");