blob: cc05469983671cab6bea70061d32712c68e133ca [file] [log] [blame]
Andrew Victor65dbf342006-04-02 19:18:51 +01001/*
2 * linux/drivers/mmc/at91_mci.c - ATMEL AT91RM9200 MCI Driver
3 *
4 * Copyright (C) 2005 Cougar Creek Computing Devices Ltd, All Rights Reserved
5 *
6 * Copyright (C) 2006 Malcolm Noyes
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13/*
14 This is the AT91RM9200 MCI driver that has been tested with both MMC cards
15 and SD-cards. Boards that support write protect are now supported.
16 The CCAT91SBC001 board does not support SD cards.
17
18 The three entry points are at91_mci_request, at91_mci_set_ios
19 and at91_mci_get_ro.
20
21 SET IOS
22 This configures the device to put it into the correct mode and clock speed
23 required.
24
25 MCI REQUEST
26 MCI request processes the commands sent in the mmc_request structure. This
27 can consist of a processing command and a stop command in the case of
28 multiple block transfers.
29
30 There are three main types of request, commands, reads and writes.
31
32 Commands are straight forward. The command is submitted to the controller and
33 the request function returns. When the controller generates an interrupt to indicate
34 the command is finished, the response to the command are read and the mmc_request_done
35 function called to end the request.
36
37 Reads and writes work in a similar manner to normal commands but involve the PDC (DMA)
38 controller to manage the transfers.
39
40 A read is done from the controller directly to the scatterlist passed in from the request.
41 Due to a bug in the controller, when a read is completed, all the words are byte
42 swapped in the scatterlist buffers.
43
44 The sequence of read interrupts is: ENDRX, RXBUFF, CMDRDY
45
46 A write is slightly different in that the bytes to write are read from the scatterlist
47 into a dma memory buffer (this is in case the source buffer should be read only). The
48 entire write buffer is then done from this single dma memory buffer.
49
50 The sequence of write interrupts is: ENDTX, TXBUFE, NOTBUSY, CMDRDY
51
52 GET RO
53 Gets the status of the write protect pin, if available.
54*/
55
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>
67
68#include <linux/mmc/host.h>
69#include <linux/mmc/protocol.h>
70
71#include <asm/io.h>
72#include <asm/irq.h>
73#include <asm/mach/mmc.h>
74#include <asm/arch/board.h>
75#include <asm/arch/gpio.h>
Andrew Victor55d8bae2006-11-30 17:16:43 +010076#include <asm/arch/at91_mci.h>
77#include <asm/arch/at91_pdc.h>
Andrew Victor65dbf342006-04-02 19:18:51 +010078
79#define DRIVER_NAME "at91_mci"
80
81#undef SUPPORT_4WIRE
82
Andrew Victor65dbf342006-04-02 19:18:51 +010083static struct clk *mci_clk;
84
85#define FL_SENT_COMMAND (1 << 0)
86#define FL_SENT_STOP (1 << 1)
87
88
89
Andrew Victore0b19b82006-10-25 19:42:38 +020090#define at91_mci_read(host, reg) __raw_readl((host)->baseaddr + (reg))
91#define at91_mci_write(host, reg, val) __raw_writel((val), (host)->baseaddr + (reg))
Andrew Victor65dbf342006-04-02 19:18:51 +010092
Andrew Victor65dbf342006-04-02 19:18:51 +010093
94/*
95 * Low level type for this driver
96 */
97struct at91mci_host
98{
99 struct mmc_host *mmc;
100 struct mmc_command *cmd;
101 struct mmc_request *request;
102
Andrew Victore0b19b82006-10-25 19:42:38 +0200103 void __iomem *baseaddr;
104
Andrew Victor65dbf342006-04-02 19:18:51 +0100105 struct at91_mmc_data *board;
106 int present;
107
108 /*
109 * Flag indicating when the command has been sent. This is used to
110 * work out whether or not to send the stop
111 */
112 unsigned int flags;
113 /* flag for current bus settings */
114 u32 bus_mode;
115
116 /* DMA buffer used for transmitting */
117 unsigned int* buffer;
118 dma_addr_t physical_address;
119 unsigned int total_length;
120
121 /* Latest in the scatterlist that has been enabled for transfer, but not freed */
122 int in_use_index;
123
124 /* Latest in the scatterlist that has been enabled for transfer */
125 int transfer_index;
126};
127
128/*
129 * Copy from sg to a dma block - used for transfers
130 */
131static inline void at91mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data)
132{
133 unsigned int len, i, size;
134 unsigned *dmabuf = host->buffer;
135
136 size = host->total_length;
137 len = data->sg_len;
138
139 /*
140 * Just loop through all entries. Size might not
141 * be the entire list though so make sure that
142 * we do not transfer too much.
143 */
144 for (i = 0; i < len; i++) {
145 struct scatterlist *sg;
146 int amount;
147 int index;
148 unsigned int *sgbuffer;
149
150 sg = &data->sg[i];
151
152 sgbuffer = kmap_atomic(sg->page, KM_BIO_SRC_IRQ) + sg->offset;
153 amount = min(size, sg->length);
154 size -= amount;
155 amount /= 4;
156
157 for (index = 0; index < amount; index++)
158 *dmabuf++ = swab32(sgbuffer[index]);
159
160 kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
161
162 if (size == 0)
163 break;
164 }
165
166 /*
167 * Check that we didn't get a request to transfer
168 * more data than can fit into the SG list.
169 */
170 BUG_ON(size != 0);
171}
172
173/*
174 * Prepare a dma read
175 */
176static void at91mci_pre_dma_read(struct at91mci_host *host)
177{
178 int i;
179 struct scatterlist *sg;
180 struct mmc_command *cmd;
181 struct mmc_data *data;
182
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100183 pr_debug("pre dma read\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100184
185 cmd = host->cmd;
186 if (!cmd) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100187 pr_debug("no command\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100188 return;
189 }
190
191 data = cmd->data;
192 if (!data) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100193 pr_debug("no data\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100194 return;
195 }
196
197 for (i = 0; i < 2; i++) {
198 /* nothing left to transfer */
199 if (host->transfer_index >= data->sg_len) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100200 pr_debug("Nothing left to transfer (index = %d)\n", host->transfer_index);
Andrew Victor65dbf342006-04-02 19:18:51 +0100201 break;
202 }
203
204 /* Check to see if this needs filling */
205 if (i == 0) {
Andrew Victore0b19b82006-10-25 19:42:38 +0200206 if (at91_mci_read(host, AT91_PDC_RCR) != 0) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100207 pr_debug("Transfer active in current\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100208 continue;
209 }
210 }
211 else {
Andrew Victore0b19b82006-10-25 19:42:38 +0200212 if (at91_mci_read(host, AT91_PDC_RNCR) != 0) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100213 pr_debug("Transfer active in next\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100214 continue;
215 }
216 }
217
218 /* Setup the next transfer */
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100219 pr_debug("Using transfer index %d\n", host->transfer_index);
Andrew Victor65dbf342006-04-02 19:18:51 +0100220
221 sg = &data->sg[host->transfer_index++];
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100222 pr_debug("sg = %p\n", sg);
Andrew Victor65dbf342006-04-02 19:18:51 +0100223
224 sg->dma_address = dma_map_page(NULL, sg->page, sg->offset, sg->length, DMA_FROM_DEVICE);
225
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100226 pr_debug("dma address = %08X, length = %d\n", sg->dma_address, sg->length);
Andrew Victor65dbf342006-04-02 19:18:51 +0100227
228 if (i == 0) {
Andrew Victore0b19b82006-10-25 19:42:38 +0200229 at91_mci_write(host, AT91_PDC_RPR, sg->dma_address);
230 at91_mci_write(host, AT91_PDC_RCR, sg->length / 4);
Andrew Victor65dbf342006-04-02 19:18:51 +0100231 }
232 else {
Andrew Victore0b19b82006-10-25 19:42:38 +0200233 at91_mci_write(host, AT91_PDC_RNPR, sg->dma_address);
234 at91_mci_write(host, AT91_PDC_RNCR, sg->length / 4);
Andrew Victor65dbf342006-04-02 19:18:51 +0100235 }
236 }
237
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100238 pr_debug("pre dma read done\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100239}
240
241/*
242 * Handle after a dma read
243 */
244static void at91mci_post_dma_read(struct at91mci_host *host)
245{
246 struct mmc_command *cmd;
247 struct mmc_data *data;
248
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100249 pr_debug("post dma read\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100250
251 cmd = host->cmd;
252 if (!cmd) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100253 pr_debug("no command\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100254 return;
255 }
256
257 data = cmd->data;
258 if (!data) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100259 pr_debug("no data\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100260 return;
261 }
262
263 while (host->in_use_index < host->transfer_index) {
264 unsigned int *buffer;
265 int index;
266 int len;
267
268 struct scatterlist *sg;
269
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100270 pr_debug("finishing index %d\n", host->in_use_index);
Andrew Victor65dbf342006-04-02 19:18:51 +0100271
272 sg = &data->sg[host->in_use_index++];
273
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100274 pr_debug("Unmapping page %08X\n", sg->dma_address);
Andrew Victor65dbf342006-04-02 19:18:51 +0100275
276 dma_unmap_page(NULL, sg->dma_address, sg->length, DMA_FROM_DEVICE);
277
278 /* Swap the contents of the buffer */
279 buffer = kmap_atomic(sg->page, KM_BIO_SRC_IRQ) + sg->offset;
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100280 pr_debug("buffer = %p, length = %d\n", buffer, sg->length);
Andrew Victor65dbf342006-04-02 19:18:51 +0100281
282 data->bytes_xfered += sg->length;
283
284 len = sg->length / 4;
285
286 for (index = 0; index < len; index++) {
287 buffer[index] = swab32(buffer[index]);
288 }
289 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
290 flush_dcache_page(sg->page);
291 }
292
293 /* Is there another transfer to trigger? */
294 if (host->transfer_index < data->sg_len)
295 at91mci_pre_dma_read(host);
296 else {
Andrew Victore0b19b82006-10-25 19:42:38 +0200297 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_RXBUFF);
298 at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100299 }
300
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100301 pr_debug("post dma read done\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100302}
303
304/*
305 * Handle transmitted data
306 */
307static void at91_mci_handle_transmitted(struct at91mci_host *host)
308{
309 struct mmc_command *cmd;
310 struct mmc_data *data;
311
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100312 pr_debug("Handling the transmit\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100313
314 /* Disable the transfer */
Andrew Victore0b19b82006-10-25 19:42:38 +0200315 at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100316
317 /* Now wait for cmd ready */
Andrew Victore0b19b82006-10-25 19:42:38 +0200318 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE);
319 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
Andrew Victor65dbf342006-04-02 19:18:51 +0100320
321 cmd = host->cmd;
322 if (!cmd) return;
323
324 data = cmd->data;
325 if (!data) return;
326
327 data->bytes_xfered = host->total_length;
328}
329
330/*
331 * Enable the controller
332 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200333static void at91_mci_enable(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100334{
Andrew Victore0b19b82006-10-25 19:42:38 +0200335 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
336 at91_mci_write(host, AT91_MCI_IDR, 0xFFFFFFFF);
337 at91_mci_write(host, AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
338 at91_mci_write(host, AT91_MCI_MR, 0x834A);
339 at91_mci_write(host, AT91_MCI_SDCR, 0x0);
Andrew Victor65dbf342006-04-02 19:18:51 +0100340}
341
342/*
343 * Disable the controller
344 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200345static void at91_mci_disable(struct at91mci_host *host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100346{
Andrew Victore0b19b82006-10-25 19:42:38 +0200347 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
Andrew Victor65dbf342006-04-02 19:18:51 +0100348}
349
350/*
351 * Send a command
352 * return the interrupts to enable
353 */
354static unsigned int at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd)
355{
356 unsigned int cmdr, mr;
357 unsigned int block_length;
358 struct mmc_data *data = cmd->data;
359
360 unsigned int blocks;
361 unsigned int ier = 0;
362
363 host->cmd = cmd;
364
365 /* Not sure if this is needed */
366#if 0
Andrew Victore0b19b82006-10-25 19:42:38 +0200367 if ((at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100368 pr_debug("Clearing timeout\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200369 at91_mci_write(host, AT91_MCI_ARGR, 0);
370 at91_mci_write(host, AT91_MCI_CMDR, AT91_MCI_OPDCMD);
371 while (!(at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_CMDRDY)) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100372 /* spin */
Andrew Victore0b19b82006-10-25 19:42:38 +0200373 pr_debug("Clearing: SR = %08X\n", at91_mci_read(host, AT91_MCI_SR));
Andrew Victor65dbf342006-04-02 19:18:51 +0100374 }
375 }
376#endif
377 cmdr = cmd->opcode;
378
379 if (mmc_resp_type(cmd) == MMC_RSP_NONE)
380 cmdr |= AT91_MCI_RSPTYP_NONE;
381 else {
382 /* if a response is expected then allow maximum response latancy */
383 cmdr |= AT91_MCI_MAXLAT;
384 /* set 136 bit response for R2, 48 bit response otherwise */
385 if (mmc_resp_type(cmd) == MMC_RSP_R2)
386 cmdr |= AT91_MCI_RSPTYP_136;
387 else
388 cmdr |= AT91_MCI_RSPTYP_48;
389 }
390
391 if (data) {
Russell Kinga3fd4a12006-06-04 17:51:15 +0100392 block_length = data->blksz;
Andrew Victor65dbf342006-04-02 19:18:51 +0100393 blocks = data->blocks;
394
395 /* always set data start - also set direction flag for read */
396 if (data->flags & MMC_DATA_READ)
397 cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START);
398 else if (data->flags & MMC_DATA_WRITE)
399 cmdr |= AT91_MCI_TRCMD_START;
400
401 if (data->flags & MMC_DATA_STREAM)
402 cmdr |= AT91_MCI_TRTYP_STREAM;
403 if (data->flags & MMC_DATA_MULTI)
404 cmdr |= AT91_MCI_TRTYP_MULTIPLE;
405 }
406 else {
407 block_length = 0;
408 blocks = 0;
409 }
410
411 if (cmd->opcode == MMC_STOP_TRANSMISSION)
412 cmdr |= AT91_MCI_TRCMD_STOP;
413
414 if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
415 cmdr |= AT91_MCI_OPDCMD;
416
417 /*
418 * Set the arguments and send the command
419 */
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100420 pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08lX)\n",
Andrew Victore0b19b82006-10-25 19:42:38 +0200421 cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(host, AT91_MCI_MR));
Andrew Victor65dbf342006-04-02 19:18:51 +0100422
423 if (!data) {
Andrew Victore0b19b82006-10-25 19:42:38 +0200424 at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_TXTDIS | AT91_PDC_RXTDIS);
425 at91_mci_write(host, AT91_PDC_RPR, 0);
426 at91_mci_write(host, AT91_PDC_RCR, 0);
427 at91_mci_write(host, AT91_PDC_RNPR, 0);
428 at91_mci_write(host, AT91_PDC_RNCR, 0);
429 at91_mci_write(host, AT91_PDC_TPR, 0);
430 at91_mci_write(host, AT91_PDC_TCR, 0);
431 at91_mci_write(host, AT91_PDC_TNPR, 0);
432 at91_mci_write(host, AT91_PDC_TNCR, 0);
Andrew Victor65dbf342006-04-02 19:18:51 +0100433
Andrew Victore0b19b82006-10-25 19:42:38 +0200434 at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
435 at91_mci_write(host, AT91_MCI_CMDR, cmdr);
Andrew Victor65dbf342006-04-02 19:18:51 +0100436 return AT91_MCI_CMDRDY;
437 }
438
Andrew Victore0b19b82006-10-25 19:42:38 +0200439 mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff; /* zero block length and PDC mode */
440 at91_mci_write(host, AT91_MCI_MR, mr | (block_length << 16) | AT91_MCI_PDCMODE);
Andrew Victor65dbf342006-04-02 19:18:51 +0100441
442 /*
443 * Disable the PDC controller
444 */
Andrew Victore0b19b82006-10-25 19:42:38 +0200445 at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100446
447 if (cmdr & AT91_MCI_TRCMD_START) {
448 data->bytes_xfered = 0;
449 host->transfer_index = 0;
450 host->in_use_index = 0;
451 if (cmdr & AT91_MCI_TRDIR) {
452 /*
453 * Handle a read
454 */
455 host->buffer = NULL;
456 host->total_length = 0;
457
458 at91mci_pre_dma_read(host);
459 ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
460 }
461 else {
462 /*
463 * Handle a write
464 */
465 host->total_length = block_length * blocks;
466 host->buffer = dma_alloc_coherent(NULL,
467 host->total_length,
468 &host->physical_address, GFP_KERNEL);
469
470 at91mci_sg_to_dma(host, data);
471
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100472 pr_debug("Transmitting %d bytes\n", host->total_length);
Andrew Victor65dbf342006-04-02 19:18:51 +0100473
Andrew Victore0b19b82006-10-25 19:42:38 +0200474 at91_mci_write(host, AT91_PDC_TPR, host->physical_address);
475 at91_mci_write(host, AT91_PDC_TCR, host->total_length / 4);
Andrew Victor65dbf342006-04-02 19:18:51 +0100476 ier = AT91_MCI_TXBUFE;
477 }
478 }
479
480 /*
481 * Send the command and then enable the PDC - not the other way round as
482 * the data sheet says
483 */
484
Andrew Victore0b19b82006-10-25 19:42:38 +0200485 at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
486 at91_mci_write(host, AT91_MCI_CMDR, cmdr);
Andrew Victor65dbf342006-04-02 19:18:51 +0100487
488 if (cmdr & AT91_MCI_TRCMD_START) {
489 if (cmdr & AT91_MCI_TRDIR)
Andrew Victore0b19b82006-10-25 19:42:38 +0200490 at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_RXTEN);
Andrew Victor65dbf342006-04-02 19:18:51 +0100491 else
Andrew Victore0b19b82006-10-25 19:42:38 +0200492 at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_TXTEN);
Andrew Victor65dbf342006-04-02 19:18:51 +0100493 }
494 return ier;
495}
496
497/*
498 * Wait for a command to complete
499 */
500static void at91mci_process_command(struct at91mci_host *host, struct mmc_command *cmd)
501{
502 unsigned int ier;
503
504 ier = at91_mci_send_command(host, cmd);
505
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100506 pr_debug("setting ier to %08X\n", ier);
Andrew Victor65dbf342006-04-02 19:18:51 +0100507
508 /* Stop on errors or the required value */
Andrew Victore0b19b82006-10-25 19:42:38 +0200509 at91_mci_write(host, AT91_MCI_IER, 0xffff0000 | ier);
Andrew Victor65dbf342006-04-02 19:18:51 +0100510}
511
512/*
513 * Process the next step in the request
514 */
515static void at91mci_process_next(struct at91mci_host *host)
516{
517 if (!(host->flags & FL_SENT_COMMAND)) {
518 host->flags |= FL_SENT_COMMAND;
519 at91mci_process_command(host, host->request->cmd);
520 }
521 else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) {
522 host->flags |= FL_SENT_STOP;
523 at91mci_process_command(host, host->request->stop);
524 }
525 else
526 mmc_request_done(host->mmc, host->request);
527}
528
529/*
530 * Handle a command that has been completed
531 */
532static void at91mci_completed_command(struct at91mci_host *host)
533{
534 struct mmc_command *cmd = host->cmd;
535 unsigned int status;
536
Andrew Victore0b19b82006-10-25 19:42:38 +0200537 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
Andrew Victor65dbf342006-04-02 19:18:51 +0100538
Andrew Victore0b19b82006-10-25 19:42:38 +0200539 cmd->resp[0] = at91_mci_read(host, AT91_MCI_RSPR(0));
540 cmd->resp[1] = at91_mci_read(host, AT91_MCI_RSPR(1));
541 cmd->resp[2] = at91_mci_read(host, AT91_MCI_RSPR(2));
542 cmd->resp[3] = at91_mci_read(host, AT91_MCI_RSPR(3));
Andrew Victor65dbf342006-04-02 19:18:51 +0100543
544 if (host->buffer) {
545 dma_free_coherent(NULL, host->total_length, host->buffer, host->physical_address);
546 host->buffer = NULL;
547 }
548
Andrew Victore0b19b82006-10-25 19:42:38 +0200549 status = at91_mci_read(host, AT91_MCI_SR);
Andrew Victor65dbf342006-04-02 19:18:51 +0100550
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100551 pr_debug("Status = %08X [%08X %08X %08X %08X]\n",
Andrew Victor65dbf342006-04-02 19:18:51 +0100552 status, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
553
554 if (status & (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE |
555 AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE |
556 AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)) {
557 if ((status & AT91_MCI_RCRCE) &&
558 ((cmd->opcode == MMC_SEND_OP_COND) || (cmd->opcode == SD_APP_OP_COND))) {
559 cmd->error = MMC_ERR_NONE;
560 }
561 else {
562 if (status & (AT91_MCI_RTOE | AT91_MCI_DTOE))
563 cmd->error = MMC_ERR_TIMEOUT;
564 else if (status & (AT91_MCI_RCRCE | AT91_MCI_DCRCE))
565 cmd->error = MMC_ERR_BADCRC;
566 else if (status & (AT91_MCI_OVRE | AT91_MCI_UNRE))
567 cmd->error = MMC_ERR_FIFO;
568 else
569 cmd->error = MMC_ERR_FAILED;
570
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100571 pr_debug("Error detected and set to %d (cmd = %d, retries = %d)\n",
Andrew Victor65dbf342006-04-02 19:18:51 +0100572 cmd->error, cmd->opcode, cmd->retries);
573 }
574 }
575 else
576 cmd->error = MMC_ERR_NONE;
577
578 at91mci_process_next(host);
579}
580
581/*
582 * Handle an MMC request
583 */
584static void at91_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
585{
586 struct at91mci_host *host = mmc_priv(mmc);
587 host->request = mrq;
588 host->flags = 0;
589
590 at91mci_process_next(host);
591}
592
593/*
594 * Set the IOS
595 */
596static void at91_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
597{
598 int clkdiv;
599 struct at91mci_host *host = mmc_priv(mmc);
600 unsigned long at91_master_clock = clk_get_rate(mci_clk);
601
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100602 host->bus_mode = ios->bus_mode;
Andrew Victor65dbf342006-04-02 19:18:51 +0100603
604 if (ios->clock == 0) {
605 /* Disable the MCI controller */
Andrew Victore0b19b82006-10-25 19:42:38 +0200606 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100607 clkdiv = 0;
608 }
609 else {
610 /* Enable the MCI controller */
Andrew Victore0b19b82006-10-25 19:42:38 +0200611 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
Andrew Victor65dbf342006-04-02 19:18:51 +0100612
613 if ((at91_master_clock % (ios->clock * 2)) == 0)
614 clkdiv = ((at91_master_clock / ios->clock) / 2) - 1;
615 else
616 clkdiv = (at91_master_clock / ios->clock) / 2;
617
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100618 pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv,
Andrew Victor65dbf342006-04-02 19:18:51 +0100619 at91_master_clock / (2 * (clkdiv + 1)));
620 }
621 if (ios->bus_width == MMC_BUS_WIDTH_4 && host->board->wire4) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100622 pr_debug("MMC: Setting controller bus width to 4\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200623 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) | AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100624 }
625 else {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100626 pr_debug("MMC: Setting controller bus width to 1\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200627 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
Andrew Victor65dbf342006-04-02 19:18:51 +0100628 }
629
630 /* Set the clock divider */
Andrew Victore0b19b82006-10-25 19:42:38 +0200631 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 +0100632
633 /* maybe switch power to the card */
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100634 if (host->board->vcc_pin) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100635 switch (ios->power_mode) {
636 case MMC_POWER_OFF:
637 at91_set_gpio_output(host->board->vcc_pin, 0);
638 break;
639 case MMC_POWER_UP:
640 case MMC_POWER_ON:
641 at91_set_gpio_output(host->board->vcc_pin, 1);
642 break;
643 }
644 }
645}
646
647/*
648 * Handle an interrupt
649 */
David Howells7d12e782006-10-05 14:55:46 +0100650static irqreturn_t at91_mci_irq(int irq, void *devid)
Andrew Victor65dbf342006-04-02 19:18:51 +0100651{
652 struct at91mci_host *host = devid;
653 int completed = 0;
654
655 unsigned int int_status;
656
Andrew Victore0b19b82006-10-25 19:42:38 +0200657 int_status = at91_mci_read(host, AT91_MCI_SR);
658 pr_debug("MCI irq: status = %08X, %08lX, %08lX\n", int_status, at91_mci_read(host, AT91_MCI_IMR),
659 int_status & at91_mci_read(host, AT91_MCI_IMR));
Andrew Victor65dbf342006-04-02 19:18:51 +0100660
Andrew Victore0b19b82006-10-25 19:42:38 +0200661 if ((int_status & at91_mci_read(host, AT91_MCI_IMR)) & 0xffff0000)
Andrew Victor65dbf342006-04-02 19:18:51 +0100662 completed = 1;
663
Andrew Victore0b19b82006-10-25 19:42:38 +0200664 int_status &= at91_mci_read(host, AT91_MCI_IMR);
Andrew Victor65dbf342006-04-02 19:18:51 +0100665
666 if (int_status & AT91_MCI_UNRE)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100667 pr_debug("MMC: Underrun error\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100668 if (int_status & AT91_MCI_OVRE)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100669 pr_debug("MMC: Overrun error\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100670 if (int_status & AT91_MCI_DTOE)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100671 pr_debug("MMC: Data timeout\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100672 if (int_status & AT91_MCI_DCRCE)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100673 pr_debug("MMC: CRC error in data\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100674 if (int_status & AT91_MCI_RTOE)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100675 pr_debug("MMC: Response timeout\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100676 if (int_status & AT91_MCI_RENDE)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100677 pr_debug("MMC: Response end bit error\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100678 if (int_status & AT91_MCI_RCRCE)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100679 pr_debug("MMC: Response CRC error\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100680 if (int_status & AT91_MCI_RDIRE)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100681 pr_debug("MMC: Response direction error\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100682 if (int_status & AT91_MCI_RINDE)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100683 pr_debug("MMC: Response index error\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100684
685 /* Only continue processing if no errors */
686 if (!completed) {
687 if (int_status & AT91_MCI_TXBUFE) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100688 pr_debug("TX buffer empty\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100689 at91_mci_handle_transmitted(host);
690 }
691
692 if (int_status & AT91_MCI_RXBUFF) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100693 pr_debug("RX buffer full\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200694 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_CMDRDY);
Andrew Victor65dbf342006-04-02 19:18:51 +0100695 }
696
697 if (int_status & AT91_MCI_ENDTX) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100698 pr_debug("Transmit has ended\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100699 }
700
701 if (int_status & AT91_MCI_ENDRX) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100702 pr_debug("Receive has ended\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100703 at91mci_post_dma_read(host);
704 }
705
706 if (int_status & AT91_MCI_NOTBUSY) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100707 pr_debug("Card is ready\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200708 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_CMDRDY);
Andrew Victor65dbf342006-04-02 19:18:51 +0100709 }
710
711 if (int_status & AT91_MCI_DTIP) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100712 pr_debug("Data transfer in progress\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100713 }
714
715 if (int_status & AT91_MCI_BLKE) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100716 pr_debug("Block transfer has ended\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100717 }
718
719 if (int_status & AT91_MCI_TXRDY) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100720 pr_debug("Ready to transmit\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100721 }
722
723 if (int_status & AT91_MCI_RXRDY) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100724 pr_debug("Ready to receive\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100725 }
726
727 if (int_status & AT91_MCI_CMDRDY) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100728 pr_debug("Command ready\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100729 completed = 1;
730 }
731 }
Andrew Victore0b19b82006-10-25 19:42:38 +0200732 at91_mci_write(host, AT91_MCI_IDR, int_status);
Andrew Victor65dbf342006-04-02 19:18:51 +0100733
734 if (completed) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100735 pr_debug("Completed command\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200736 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
Andrew Victor65dbf342006-04-02 19:18:51 +0100737 at91mci_completed_command(host);
738 }
739
740 return IRQ_HANDLED;
741}
742
David Howells7d12e782006-10-05 14:55:46 +0100743static irqreturn_t at91_mmc_det_irq(int irq, void *_host)
Andrew Victor65dbf342006-04-02 19:18:51 +0100744{
745 struct at91mci_host *host = _host;
746 int present = !at91_get_gpio_value(irq);
747
748 /*
749 * we expect this irq on both insert and remove,
750 * and use a short delay to debounce.
751 */
752 if (present != host->present) {
753 host->present = present;
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100754 pr_debug("%s: card %s\n", mmc_hostname(host->mmc),
Andrew Victor65dbf342006-04-02 19:18:51 +0100755 present ? "insert" : "remove");
756 if (!present) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100757 pr_debug("****** Resetting SD-card bus width ******\n");
Andrew Victore0b19b82006-10-25 19:42:38 +0200758 at91_mci_write(host, AT91_MCI_SDCR, 0);
Andrew Victor65dbf342006-04-02 19:18:51 +0100759 }
760 mmc_detect_change(host->mmc, msecs_to_jiffies(100));
761 }
762 return IRQ_HANDLED;
763}
764
765int at91_mci_get_ro(struct mmc_host *mmc)
766{
767 int read_only = 0;
768 struct at91mci_host *host = mmc_priv(mmc);
769
770 if (host->board->wp_pin) {
771 read_only = at91_get_gpio_value(host->board->wp_pin);
772 printk(KERN_WARNING "%s: card is %s\n", mmc_hostname(mmc),
773 (read_only ? "read-only" : "read-write") );
774 }
775 else {
776 printk(KERN_WARNING "%s: host does not support reading read-only "
777 "switch. Assuming write-enable.\n", mmc_hostname(mmc));
778 }
779 return read_only;
780}
781
David Brownellab7aefd2006-11-12 17:55:30 -0800782static const struct mmc_host_ops at91_mci_ops = {
Andrew Victor65dbf342006-04-02 19:18:51 +0100783 .request = at91_mci_request,
784 .set_ios = at91_mci_set_ios,
785 .get_ro = at91_mci_get_ro,
786};
787
788/*
789 * Probe for the device
790 */
791static int at91_mci_probe(struct platform_device *pdev)
792{
793 struct mmc_host *mmc;
794 struct at91mci_host *host;
795 int ret;
796
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100797 pr_debug("Probe MCI devices\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100798
799 mmc = mmc_alloc_host(sizeof(struct at91mci_host), &pdev->dev);
800 if (!mmc) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100801 pr_debug("Failed to allocate mmc host\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100802 return -ENOMEM;
803 }
804
805 mmc->ops = &at91_mci_ops;
806 mmc->f_min = 375000;
807 mmc->f_max = 25000000;
808 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Russell King42431ac2006-09-24 10:44:09 +0100809 mmc->caps = MMC_CAP_BYTEBLOCK;
Andrew Victor65dbf342006-04-02 19:18:51 +0100810
811 host = mmc_priv(mmc);
812 host->mmc = mmc;
813 host->buffer = NULL;
814 host->bus_mode = 0;
815 host->board = pdev->dev.platform_data;
816 if (host->board->wire4) {
817#ifdef SUPPORT_4WIRE
818 mmc->caps |= MMC_CAP_4_BIT_DATA;
819#else
820 printk("MMC: 4 wire bus mode not supported by this driver - using 1 wire\n");
821#endif
822 }
823
824 /*
825 * Get Clock
826 */
827 mci_clk = clk_get(&pdev->dev, "mci_clk");
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100828 if (IS_ERR(mci_clk)) {
Andrew Victor65dbf342006-04-02 19:18:51 +0100829 printk(KERN_ERR "AT91 MMC: no clock defined.\n");
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100830 mmc_free_host(mmc);
Andrew Victor65dbf342006-04-02 19:18:51 +0100831 return -ENODEV;
832 }
833 clk_enable(mci_clk); /* Enable the peripheral clock */
834
Andrew Victore0b19b82006-10-25 19:42:38 +0200835 host->baseaddr = (void __iomem *)AT91_VA_BASE_MCI;
836
837 /*
838 * Reset hardware
839 */
840 at91_mci_disable(host);
841 at91_mci_enable(host);
842
Andrew Victor65dbf342006-04-02 19:18:51 +0100843 /*
844 * Allocate the MCI interrupt
845 */
Andrew Victor72729912006-09-27 09:44:11 +0100846 ret = request_irq(AT91RM9200_ID_MCI, at91_mci_irq, IRQF_SHARED, DRIVER_NAME, host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100847 if (ret) {
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100848 printk(KERN_ERR "Failed to request MCI interrupt\n");
849 clk_disable(mci_clk);
850 clk_put(mci_clk);
851 mmc_free_host(mmc);
Andrew Victor65dbf342006-04-02 19:18:51 +0100852 return ret;
853 }
854
855 platform_set_drvdata(pdev, mmc);
856
857 /*
858 * Add host to MMC layer
859 */
860 if (host->board->det_pin)
861 host->present = !at91_get_gpio_value(host->board->det_pin);
862 else
863 host->present = -1;
864
865 mmc_add_host(mmc);
866
867 /*
868 * monitor card insertion/removal if we can
869 */
870 if (host->board->det_pin) {
871 ret = request_irq(host->board->det_pin, at91_mmc_det_irq,
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100872 0, DRIVER_NAME, host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100873 if (ret)
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100874 printk(KERN_ERR "couldn't allocate MMC detect irq\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100875 }
876
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100877 pr_debug(KERN_INFO "Added MCI driver\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100878
879 return 0;
880}
881
882/*
883 * Remove a device
884 */
885static int at91_mci_remove(struct platform_device *pdev)
886{
887 struct mmc_host *mmc = platform_get_drvdata(pdev);
888 struct at91mci_host *host;
889
890 if (!mmc)
891 return -1;
892
893 host = mmc_priv(mmc);
894
895 if (host->present != -1) {
896 free_irq(host->board->det_pin, host);
897 cancel_delayed_work(&host->mmc->detect);
898 }
899
900 mmc_remove_host(mmc);
Andrew Victore0b19b82006-10-25 19:42:38 +0200901 at91_mci_disable(host);
Andrew Victor72729912006-09-27 09:44:11 +0100902 free_irq(AT91RM9200_ID_MCI, host);
Andrew Victor65dbf342006-04-02 19:18:51 +0100903 mmc_free_host(mmc);
904
905 clk_disable(mci_clk); /* Disable the peripheral clock */
906 clk_put(mci_clk);
907
908 platform_set_drvdata(pdev, NULL);
909
Andrew Victorb44fb7a2006-06-19 13:06:05 +0100910 pr_debug("MCI Removed\n");
Andrew Victor65dbf342006-04-02 19:18:51 +0100911
912 return 0;
913}
914
915#ifdef CONFIG_PM
916static int at91_mci_suspend(struct platform_device *pdev, pm_message_t state)
917{
918 struct mmc_host *mmc = platform_get_drvdata(pdev);
919 int ret = 0;
920
921 if (mmc)
922 ret = mmc_suspend_host(mmc, state);
923
924 return ret;
925}
926
927static int at91_mci_resume(struct platform_device *pdev)
928{
929 struct mmc_host *mmc = platform_get_drvdata(pdev);
930 int ret = 0;
931
932 if (mmc)
933 ret = mmc_resume_host(mmc);
934
935 return ret;
936}
937#else
938#define at91_mci_suspend NULL
939#define at91_mci_resume NULL
940#endif
941
942static struct platform_driver at91_mci_driver = {
943 .probe = at91_mci_probe,
944 .remove = at91_mci_remove,
945 .suspend = at91_mci_suspend,
946 .resume = at91_mci_resume,
947 .driver = {
948 .name = DRIVER_NAME,
949 .owner = THIS_MODULE,
950 },
951};
952
953static int __init at91_mci_init(void)
954{
955 return platform_driver_register(&at91_mci_driver);
956}
957
958static void __exit at91_mci_exit(void)
959{
960 platform_driver_unregister(&at91_mci_driver);
961}
962
963module_init(at91_mci_init);
964module_exit(at91_mci_exit);
965
966MODULE_DESCRIPTION("AT91 Multimedia Card Interface driver");
967MODULE_AUTHOR("Nick Randell");
968MODULE_LICENSE("GPL");