blob: fcbaf40e35535f41bf873f60a4e420c210df7fbb [file] [log] [blame]
Pete Popovba264b32005-09-21 06:18:27 +00001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/au1xmmc.c - AU1XX0 MMC driver
Pete Popovba264b32005-09-21 06:18:27 +00003 *
4 * Copyright (c) 2005, Advanced Micro Devices, Inc.
5 *
6 * Developed with help from the 2.4.30 MMC AU1XXX controller including
7 * the following copyright notices:
8 * Copyright (c) 2003-2004 Embedded Edge, LLC.
9 * Portions Copyright (C) 2002 Embedix, Inc
10 * Copyright 2002 Hewlett-Packard Company
11
12 * 2.6 version of this driver inspired by:
13 * (drivers/mmc/wbsd.c) Copyright (C) 2004-2005 Pierre Ossman,
14 * All Rights Reserved.
15 * (drivers/mmc/pxa.c) Copyright (C) 2003 Russell King,
16 * All Rights Reserved.
17 *
18
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 2 as
21 * published by the Free Software Foundation.
22 */
23
24/* Why is a timer used to detect insert events?
25 *
26 * From the AU1100 MMC application guide:
27 * If the Au1100-based design is intended to support both MultiMediaCards
28 * and 1- or 4-data bit SecureDigital cards, then the solution is to
29 * connect a weak (560KOhm) pull-up resistor to connector pin 1.
30 * In doing so, a MMC card never enters SPI-mode communications,
31 * but now the SecureDigital card-detect feature of CD/DAT3 is ineffective
32 * (the low to high transition will not occur).
33 *
34 * So we use the timer to check the status manually.
35 */
36
Pete Popovba264b32005-09-21 06:18:27 +000037#include <linux/module.h>
38#include <linux/init.h>
Martin Michlmayrb256f9d2006-03-04 23:01:13 +000039#include <linux/platform_device.h>
Pete Popovba264b32005-09-21 06:18:27 +000040#include <linux/mm.h>
41#include <linux/interrupt.h>
42#include <linux/dma-mapping.h>
Al Viro0ada7a02007-10-27 19:40:46 +010043#include <linux/scatterlist.h>
Manuel Laussc4223c22008-06-09 08:36:13 +020044#include <linux/leds.h>
Pete Popovba264b32005-09-21 06:18:27 +000045#include <linux/mmc/host.h>
Manuel Laussc4223c22008-06-09 08:36:13 +020046
Pete Popovba264b32005-09-21 06:18:27 +000047#include <asm/io.h>
48#include <asm/mach-au1x00/au1000.h>
49#include <asm/mach-au1x00/au1xxx_dbdma.h>
50#include <asm/mach-au1x00/au1100_mmc.h>
Pete Popovba264b32005-09-21 06:18:27 +000051
Pete Popovba264b32005-09-21 06:18:27 +000052#define DRIVER_NAME "au1xxx-mmc"
53
54/* Set this to enable special debugging macros */
Manuel Laussc4223c22008-06-09 08:36:13 +020055/* #define DEBUG */
Pete Popovba264b32005-09-21 06:18:27 +000056
Russell Kingc6563172006-03-29 09:30:20 +010057#ifdef DEBUG
Manuel Lauss5c0a8892008-06-09 08:38:35 +020058#define DBG(fmt, idx, args...) \
59 printk(KERN_DEBUG "au1xmmc(%d): DEBUG: " fmt, idx, ##args)
Pete Popovba264b32005-09-21 06:18:27 +000060#else
Manuel Lauss5c0a8892008-06-09 08:38:35 +020061#define DBG(fmt, idx, args...) do {} while (0)
Pete Popovba264b32005-09-21 06:18:27 +000062#endif
63
Manuel Lauss5c0a8892008-06-09 08:38:35 +020064/* Hardware definitions */
65#define AU1XMMC_DESCRIPTOR_COUNT 1
66#define AU1XMMC_DESCRIPTOR_SIZE 2048
67
68#define AU1XMMC_OCR (MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 | \
69 MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 | \
70 MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36)
71
72/* This gives us a hard value for the stop command that we can write directly
73 * to the command register.
74 */
75#define STOP_CMD \
76 (SD_CMD_RT_1B | SD_CMD_CT_7 | (0xC << SD_CMD_CI_SHIFT) | SD_CMD_GO)
77
78/* This is the set of interrupts that we configure by default. */
79#define AU1XMMC_INTERRUPTS \
80 (SD_CONFIG_SC | SD_CONFIG_DT | SD_CONFIG_RAT | \
81 SD_CONFIG_CR | SD_CONFIG_I)
82
83/* The poll event (looking for insert/remove events runs twice a second. */
84#define AU1XMMC_DETECT_TIMEOUT (HZ/2)
85
86struct au1xmmc_host {
87 struct mmc_host *mmc;
88 struct mmc_request *mrq;
89
90 u32 flags;
91 u32 iobase;
92 u32 clock;
93 u32 bus_width;
94 u32 power_mode;
95
96 int status;
97
98 struct {
99 int len;
100 int dir;
101 } dma;
102
103 struct {
104 int index;
105 int offset;
106 int len;
107 } pio;
108
109 u32 tx_chan;
110 u32 rx_chan;
111
112 int irq;
113
114 struct timer_list timer;
115 struct tasklet_struct finish_task;
116 struct tasklet_struct data_task;
117 struct au1xmmc_platform_data *platdata;
118 struct platform_device *pdev;
119 struct resource *ioarea;
120};
121
122/* Status flags used by the host structure */
123#define HOST_F_XMIT 0x0001
124#define HOST_F_RECV 0x0002
125#define HOST_F_DMA 0x0010
126#define HOST_F_ACTIVE 0x0100
127#define HOST_F_STOP 0x1000
128
129#define HOST_S_IDLE 0x0001
130#define HOST_S_CMD 0x0002
131#define HOST_S_DATA 0x0003
132#define HOST_S_STOP 0x0004
133
134/* Easy access macros */
135#define HOST_STATUS(h) ((h)->iobase + SD_STATUS)
136#define HOST_CONFIG(h) ((h)->iobase + SD_CONFIG)
137#define HOST_ENABLE(h) ((h)->iobase + SD_ENABLE)
138#define HOST_TXPORT(h) ((h)->iobase + SD_TXPORT)
139#define HOST_RXPORT(h) ((h)->iobase + SD_RXPORT)
140#define HOST_CMDARG(h) ((h)->iobase + SD_CMDARG)
141#define HOST_BLKSIZE(h) ((h)->iobase + SD_BLKSIZE)
142#define HOST_CMD(h) ((h)->iobase + SD_CMD)
143#define HOST_CONFIG2(h) ((h)->iobase + SD_CONFIG2)
144#define HOST_TIMEOUT(h) ((h)->iobase + SD_TIMEOUT)
145#define HOST_DEBUG(h) ((h)->iobase + SD_DEBUG)
146
147#define DMA_CHANNEL(h) \
148 (((h)->flags & HOST_F_XMIT) ? (h)->tx_chan : (h)->rx_chan)
149
Pete Popovba264b32005-09-21 06:18:27 +0000150static inline void IRQ_ON(struct au1xmmc_host *host, u32 mask)
151{
152 u32 val = au_readl(HOST_CONFIG(host));
153 val |= mask;
154 au_writel(val, HOST_CONFIG(host));
155 au_sync();
156}
157
158static inline void FLUSH_FIFO(struct au1xmmc_host *host)
159{
160 u32 val = au_readl(HOST_CONFIG2(host));
161
162 au_writel(val | SD_CONFIG2_FF, HOST_CONFIG2(host));
163 au_sync_delay(1);
164
165 /* SEND_STOP will turn off clock control - this re-enables it */
166 val &= ~SD_CONFIG2_DF;
167
168 au_writel(val, HOST_CONFIG2(host));
169 au_sync();
170}
171
172static inline void IRQ_OFF(struct au1xmmc_host *host, u32 mask)
173{
174 u32 val = au_readl(HOST_CONFIG(host));
175 val &= ~mask;
176 au_writel(val, HOST_CONFIG(host));
177 au_sync();
178}
179
180static inline void SEND_STOP(struct au1xmmc_host *host)
181{
Manuel Lauss281dd232008-06-09 08:37:33 +0200182 u32 config2;
Pete Popovba264b32005-09-21 06:18:27 +0000183
184 WARN_ON(host->status != HOST_S_DATA);
185 host->status = HOST_S_STOP;
186
Manuel Lauss281dd232008-06-09 08:37:33 +0200187 config2 = au_readl(HOST_CONFIG2(host));
188 au_writel(config2 | SD_CONFIG2_DF, HOST_CONFIG2(host));
Pete Popovba264b32005-09-21 06:18:27 +0000189 au_sync();
190
191 /* Send the stop commmand */
192 au_writel(STOP_CMD, HOST_CMD(host));
193}
194
195static void au1xmmc_set_power(struct au1xmmc_host *host, int state)
196{
Manuel Laussc4223c22008-06-09 08:36:13 +0200197 if (host->platdata && host->platdata->set_power)
198 host->platdata->set_power(host->mmc, state);
Pete Popovba264b32005-09-21 06:18:27 +0000199}
200
Manuel Laussc4223c22008-06-09 08:36:13 +0200201static int au1xmmc_card_inserted(struct au1xmmc_host *host)
Pete Popovba264b32005-09-21 06:18:27 +0000202{
Manuel Laussc4223c22008-06-09 08:36:13 +0200203 int ret;
204
205 if (host->platdata && host->platdata->card_inserted)
206 ret = host->platdata->card_inserted(host->mmc);
207 else
208 ret = 1; /* assume there is a card */
209
210 return ret;
Pete Popovba264b32005-09-21 06:18:27 +0000211}
212
Manuel Lauss82999772007-01-25 10:29:24 +0100213static int au1xmmc_card_readonly(struct mmc_host *mmc)
Pete Popovba264b32005-09-21 06:18:27 +0000214{
Manuel Lauss82999772007-01-25 10:29:24 +0100215 struct au1xmmc_host *host = mmc_priv(mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200216 int ret;
217
218 if (host->platdata && host->platdata->card_readonly)
219 ret = host->platdata->card_readonly(mmc);
220 else
221 ret = 0; /* assume card is read-write */
222
223 return ret;
Pete Popovba264b32005-09-21 06:18:27 +0000224}
225
226static void au1xmmc_finish_request(struct au1xmmc_host *host)
227{
Pete Popovba264b32005-09-21 06:18:27 +0000228 struct mmc_request *mrq = host->mrq;
229
230 host->mrq = NULL;
Manuel Laussc4223c22008-06-09 08:36:13 +0200231 host->flags &= HOST_F_ACTIVE | HOST_F_DMA;
Pete Popovba264b32005-09-21 06:18:27 +0000232
233 host->dma.len = 0;
234 host->dma.dir = 0;
235
236 host->pio.index = 0;
237 host->pio.offset = 0;
238 host->pio.len = 0;
239
240 host->status = HOST_S_IDLE;
241
Pete Popovba264b32005-09-21 06:18:27 +0000242 mmc_request_done(host->mmc, mrq);
243}
244
245static void au1xmmc_tasklet_finish(unsigned long param)
246{
247 struct au1xmmc_host *host = (struct au1xmmc_host *) param;
248 au1xmmc_finish_request(host);
249}
250
251static int au1xmmc_send_command(struct au1xmmc_host *host, int wait,
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200252 struct mmc_command *cmd, struct mmc_data *data)
Pete Popovba264b32005-09-21 06:18:27 +0000253{
Pete Popovba264b32005-09-21 06:18:27 +0000254 u32 mmccmd = (cmd->opcode << SD_CMD_CI_SHIFT);
255
Martin Michlmayre142c242006-03-04 23:01:39 +0000256 switch (mmc_resp_type(cmd)) {
Manuel Lauss279bc442007-01-25 10:27:41 +0100257 case MMC_RSP_NONE:
258 break;
Pete Popovba264b32005-09-21 06:18:27 +0000259 case MMC_RSP_R1:
260 mmccmd |= SD_CMD_RT_1;
261 break;
262 case MMC_RSP_R1B:
263 mmccmd |= SD_CMD_RT_1B;
264 break;
265 case MMC_RSP_R2:
266 mmccmd |= SD_CMD_RT_2;
267 break;
268 case MMC_RSP_R3:
269 mmccmd |= SD_CMD_RT_3;
270 break;
Manuel Lauss279bc442007-01-25 10:27:41 +0100271 default:
272 printk(KERN_INFO "au1xmmc: unhandled response type %02x\n",
273 mmc_resp_type(cmd));
Pierre Ossman17b04292007-07-22 22:18:46 +0200274 return -EINVAL;
Pete Popovba264b32005-09-21 06:18:27 +0000275 }
276
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200277 if (data) {
Pierre Ossman6356a9d2007-10-22 18:16:16 +0200278 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200279 if (data->blocks > 1)
280 mmccmd |= SD_CMD_CT_4;
281 else
282 mmccmd |= SD_CMD_CT_2;
Pierre Ossman6356a9d2007-10-22 18:16:16 +0200283 } else if (data->flags & MMC_DATA_WRITE) {
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200284 if (data->blocks > 1)
285 mmccmd |= SD_CMD_CT_3;
286 else
287 mmccmd |= SD_CMD_CT_1;
288 }
Pete Popovba264b32005-09-21 06:18:27 +0000289 }
290
291 au_writel(cmd->arg, HOST_CMDARG(host));
292 au_sync();
293
294 if (wait)
295 IRQ_OFF(host, SD_CONFIG_CR);
296
297 au_writel((mmccmd | SD_CMD_GO), HOST_CMD(host));
298 au_sync();
299
300 /* Wait for the command to go on the line */
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200301 while (au_readl(HOST_CMD(host)) & SD_CMD_GO)
302 /* nop */;
Pete Popovba264b32005-09-21 06:18:27 +0000303
304 /* Wait for the command to come back */
Pete Popovba264b32005-09-21 06:18:27 +0000305 if (wait) {
306 u32 status = au_readl(HOST_STATUS(host));
307
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200308 while (!(status & SD_STATUS_CR))
Pete Popovba264b32005-09-21 06:18:27 +0000309 status = au_readl(HOST_STATUS(host));
310
311 /* Clear the CR status */
312 au_writel(SD_STATUS_CR, HOST_STATUS(host));
313
314 IRQ_ON(host, SD_CONFIG_CR);
315 }
316
Pierre Ossman17b04292007-07-22 22:18:46 +0200317 return 0;
Pete Popovba264b32005-09-21 06:18:27 +0000318}
319
320static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
321{
Pete Popovba264b32005-09-21 06:18:27 +0000322 struct mmc_request *mrq = host->mrq;
323 struct mmc_data *data;
324 u32 crc;
325
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200326 WARN_ON((host->status != HOST_S_DATA) && (host->status != HOST_S_STOP));
Pete Popovba264b32005-09-21 06:18:27 +0000327
328 if (host->mrq == NULL)
329 return;
330
331 data = mrq->cmd->data;
332
333 if (status == 0)
334 status = au_readl(HOST_STATUS(host));
335
336 /* The transaction is really over when the SD_STATUS_DB bit is clear */
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200337 while ((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB))
Pete Popovba264b32005-09-21 06:18:27 +0000338 status = au_readl(HOST_STATUS(host));
339
Pierre Ossman17b04292007-07-22 22:18:46 +0200340 data->error = 0;
Pete Popovba264b32005-09-21 06:18:27 +0000341 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir);
342
343 /* Process any errors */
Pete Popovba264b32005-09-21 06:18:27 +0000344 crc = (status & (SD_STATUS_WC | SD_STATUS_RC));
345 if (host->flags & HOST_F_XMIT)
346 crc |= ((status & 0x07) == 0x02) ? 0 : 1;
347
348 if (crc)
Pierre Ossman17b04292007-07-22 22:18:46 +0200349 data->error = -EILSEQ;
Pete Popovba264b32005-09-21 06:18:27 +0000350
351 /* Clear the CRC bits */
352 au_writel(SD_STATUS_WC | SD_STATUS_RC, HOST_STATUS(host));
353
354 data->bytes_xfered = 0;
355
Pierre Ossman17b04292007-07-22 22:18:46 +0200356 if (!data->error) {
Pete Popovba264b32005-09-21 06:18:27 +0000357 if (host->flags & HOST_F_DMA) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200358#ifdef CONFIG_SOC_AU1200 /* DBDMA */
Pete Popovba264b32005-09-21 06:18:27 +0000359 u32 chan = DMA_CHANNEL(host);
360
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200361 chan_tab_t *c = *((chan_tab_t **)chan);
Pete Popovba264b32005-09-21 06:18:27 +0000362 au1x_dma_chan_t *cp = c->chan_ptr;
363 data->bytes_xfered = cp->ddma_bytecnt;
Manuel Laussc4223c22008-06-09 08:36:13 +0200364#endif
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200365 } else
Pete Popovba264b32005-09-21 06:18:27 +0000366 data->bytes_xfered =
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200367 (data->blocks * data->blksz) - host->pio.len;
Pete Popovba264b32005-09-21 06:18:27 +0000368 }
369
370 au1xmmc_finish_request(host);
371}
372
373static void au1xmmc_tasklet_data(unsigned long param)
374{
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200375 struct au1xmmc_host *host = (struct au1xmmc_host *)param;
Pete Popovba264b32005-09-21 06:18:27 +0000376
377 u32 status = au_readl(HOST_STATUS(host));
378 au1xmmc_data_complete(host, status);
379}
380
381#define AU1XMMC_MAX_TRANSFER 8
382
383static void au1xmmc_send_pio(struct au1xmmc_host *host)
384{
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200385 struct mmc_data *data;
386 int sg_len, max, count;
387 unsigned char *sg_ptr, val;
388 u32 status;
Pete Popovba264b32005-09-21 06:18:27 +0000389 struct scatterlist *sg;
390
391 data = host->mrq->data;
392
393 if (!(host->flags & HOST_F_XMIT))
394 return;
395
396 /* This is the pointer to the data buffer */
397 sg = &data->sg[host->pio.index];
Jens Axboe45711f12007-10-22 21:19:53 +0200398 sg_ptr = sg_virt(sg) + host->pio.offset;
Pete Popovba264b32005-09-21 06:18:27 +0000399
400 /* This is the space left inside the buffer */
401 sg_len = data->sg[host->pio.index].length - host->pio.offset;
402
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200403 /* Check if we need less than the size of the sg_buffer */
Pete Popovba264b32005-09-21 06:18:27 +0000404 max = (sg_len > host->pio.len) ? host->pio.len : sg_len;
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200405 if (max > AU1XMMC_MAX_TRANSFER)
406 max = AU1XMMC_MAX_TRANSFER;
Pete Popovba264b32005-09-21 06:18:27 +0000407
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200408 for (count = 0; count < max; count++) {
Pete Popovba264b32005-09-21 06:18:27 +0000409 status = au_readl(HOST_STATUS(host));
410
411 if (!(status & SD_STATUS_TH))
412 break;
413
414 val = *sg_ptr++;
415
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200416 au_writel((unsigned long)val, HOST_TXPORT(host));
Pete Popovba264b32005-09-21 06:18:27 +0000417 au_sync();
418 }
419
420 host->pio.len -= count;
421 host->pio.offset += count;
422
423 if (count == sg_len) {
424 host->pio.index++;
425 host->pio.offset = 0;
426 }
427
428 if (host->pio.len == 0) {
429 IRQ_OFF(host, SD_CONFIG_TH);
430
431 if (host->flags & HOST_F_STOP)
432 SEND_STOP(host);
433
434 tasklet_schedule(&host->data_task);
435 }
436}
437
438static void au1xmmc_receive_pio(struct au1xmmc_host *host)
439{
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200440 struct mmc_data *data;
441 int max, count, sg_len = 0;
442 unsigned char *sg_ptr = NULL;
443 u32 status, val;
Pete Popovba264b32005-09-21 06:18:27 +0000444 struct scatterlist *sg;
445
446 data = host->mrq->data;
447
448 if (!(host->flags & HOST_F_RECV))
449 return;
450
451 max = host->pio.len;
452
453 if (host->pio.index < host->dma.len) {
454 sg = &data->sg[host->pio.index];
Jens Axboe45711f12007-10-22 21:19:53 +0200455 sg_ptr = sg_virt(sg) + host->pio.offset;
Pete Popovba264b32005-09-21 06:18:27 +0000456
457 /* This is the space left inside the buffer */
458 sg_len = sg_dma_len(&data->sg[host->pio.index]) - host->pio.offset;
459
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200460 /* Check if we need less than the size of the sg_buffer */
461 if (sg_len < max)
462 max = sg_len;
Pete Popovba264b32005-09-21 06:18:27 +0000463 }
464
465 if (max > AU1XMMC_MAX_TRANSFER)
466 max = AU1XMMC_MAX_TRANSFER;
467
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200468 for (count = 0; count < max; count++) {
Pete Popovba264b32005-09-21 06:18:27 +0000469 status = au_readl(HOST_STATUS(host));
470
471 if (!(status & SD_STATUS_NE))
472 break;
473
474 if (status & SD_STATUS_RC) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200475 DBG("RX CRC Error [%d + %d].\n", host->pdev->id,
Pete Popovba264b32005-09-21 06:18:27 +0000476 host->pio.len, count);
477 break;
478 }
479
480 if (status & SD_STATUS_RO) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200481 DBG("RX Overrun [%d + %d]\n", host->pdev->id,
Pete Popovba264b32005-09-21 06:18:27 +0000482 host->pio.len, count);
483 break;
484 }
485 else if (status & SD_STATUS_RU) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200486 DBG("RX Underrun [%d + %d]\n", host->pdev->id,
Pete Popovba264b32005-09-21 06:18:27 +0000487 host->pio.len, count);
488 break;
489 }
490
491 val = au_readl(HOST_RXPORT(host));
492
493 if (sg_ptr)
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200494 *sg_ptr++ = (unsigned char)(val & 0xFF);
Pete Popovba264b32005-09-21 06:18:27 +0000495 }
496
497 host->pio.len -= count;
498 host->pio.offset += count;
499
500 if (sg_len && count == sg_len) {
501 host->pio.index++;
502 host->pio.offset = 0;
503 }
504
505 if (host->pio.len == 0) {
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200506 /* IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF); */
Pete Popovba264b32005-09-21 06:18:27 +0000507 IRQ_OFF(host, SD_CONFIG_NE);
508
509 if (host->flags & HOST_F_STOP)
510 SEND_STOP(host);
511
512 tasklet_schedule(&host->data_task);
513 }
514}
515
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200516/* This is called when a command has been completed - grab the response
517 * and check for errors. Then start the data transfer if it is indicated.
518 */
Pete Popovba264b32005-09-21 06:18:27 +0000519static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
520{
Pete Popovba264b32005-09-21 06:18:27 +0000521 struct mmc_request *mrq = host->mrq;
522 struct mmc_command *cmd;
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200523 u32 r[4];
524 int i, trans;
Pete Popovba264b32005-09-21 06:18:27 +0000525
526 if (!host->mrq)
527 return;
528
529 cmd = mrq->cmd;
Pierre Ossman17b04292007-07-22 22:18:46 +0200530 cmd->error = 0;
Pete Popovba264b32005-09-21 06:18:27 +0000531
Russell Kinge9225172006-02-02 12:23:12 +0000532 if (cmd->flags & MMC_RSP_PRESENT) {
533 if (cmd->flags & MMC_RSP_136) {
Russell Kinge9225172006-02-02 12:23:12 +0000534 r[0] = au_readl(host->iobase + SD_RESP3);
535 r[1] = au_readl(host->iobase + SD_RESP2);
536 r[2] = au_readl(host->iobase + SD_RESP1);
537 r[3] = au_readl(host->iobase + SD_RESP0);
Pete Popovba264b32005-09-21 06:18:27 +0000538
Russell Kinge9225172006-02-02 12:23:12 +0000539 /* The CRC is omitted from the response, so really
540 * we only got 120 bytes, but the engine expects
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200541 * 128 bits, so we have to shift things up.
Russell Kinge9225172006-02-02 12:23:12 +0000542 */
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200543 for (i = 0; i < 4; i++) {
Russell Kinge9225172006-02-02 12:23:12 +0000544 cmd->resp[i] = (r[i] & 0x00FFFFFF) << 8;
545 if (i != 3)
546 cmd->resp[i] |= (r[i + 1] & 0xFF000000) >> 24;
547 }
548 } else {
549 /* Techincally, we should be getting all 48 bits of
550 * the response (SD_RESP1 + SD_RESP2), but because
551 * our response omits the CRC, our data ends up
552 * being shifted 8 bits to the right. In this case,
553 * that means that the OSR data starts at bit 31,
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200554 * so we can just read RESP0 and return that.
Russell Kinge9225172006-02-02 12:23:12 +0000555 */
556 cmd->resp[0] = au_readl(host->iobase + SD_RESP0);
Pete Popovba264b32005-09-21 06:18:27 +0000557 }
558 }
559
560 /* Figure out errors */
Pete Popovba264b32005-09-21 06:18:27 +0000561 if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC))
Pierre Ossman17b04292007-07-22 22:18:46 +0200562 cmd->error = -EILSEQ;
Pete Popovba264b32005-09-21 06:18:27 +0000563
564 trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);
565
Pierre Ossman17b04292007-07-22 22:18:46 +0200566 if (!trans || cmd->error) {
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200567 IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF);
Pete Popovba264b32005-09-21 06:18:27 +0000568 tasklet_schedule(&host->finish_task);
569 return;
570 }
571
572 host->status = HOST_S_DATA;
573
574 if (host->flags & HOST_F_DMA) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200575#ifdef CONFIG_SOC_AU1200 /* DBDMA */
Pete Popovba264b32005-09-21 06:18:27 +0000576 u32 channel = DMA_CHANNEL(host);
577
578 /* Start the DMA as soon as the buffer gets something in it */
579
580 if (host->flags & HOST_F_RECV) {
581 u32 mask = SD_STATUS_DB | SD_STATUS_NE;
582
583 while((status & mask) != mask)
584 status = au_readl(HOST_STATUS(host));
585 }
586
587 au1xxx_dbdma_start(channel);
Manuel Laussc4223c22008-06-09 08:36:13 +0200588#endif
Pete Popovba264b32005-09-21 06:18:27 +0000589 }
590}
591
592static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate)
593{
Pete Popovba264b32005-09-21 06:18:27 +0000594 unsigned int pbus = get_au1x00_speed();
595 unsigned int divisor;
596 u32 config;
597
598 /* From databook:
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200599 * divisor = ((((cpuclock / sbus_divisor) / 2) / mmcclock) / 2) - 1
600 */
Pete Popovba264b32005-09-21 06:18:27 +0000601 pbus /= ((au_readl(SYS_POWERCTRL) & 0x3) + 2);
602 pbus /= 2;
Pete Popovba264b32005-09-21 06:18:27 +0000603 divisor = ((pbus / rate) / 2) - 1;
604
605 config = au_readl(HOST_CONFIG(host));
606
607 config &= ~(SD_CONFIG_DIV);
608 config |= (divisor & SD_CONFIG_DIV) | SD_CONFIG_DE;
609
610 au_writel(config, HOST_CONFIG(host));
611 au_sync();
612}
613
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200614static int au1xmmc_prepare_data(struct au1xmmc_host *host,
615 struct mmc_data *data)
Pete Popovba264b32005-09-21 06:18:27 +0000616{
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100617 int datalen = data->blocks * data->blksz;
Pete Popovba264b32005-09-21 06:18:27 +0000618
Pete Popovba264b32005-09-21 06:18:27 +0000619 if (data->flags & MMC_DATA_READ)
620 host->flags |= HOST_F_RECV;
621 else
622 host->flags |= HOST_F_XMIT;
623
624 if (host->mrq->stop)
625 host->flags |= HOST_F_STOP;
626
627 host->dma.dir = DMA_BIDIRECTIONAL;
628
629 host->dma.len = dma_map_sg(mmc_dev(host->mmc), data->sg,
630 data->sg_len, host->dma.dir);
631
632 if (host->dma.len == 0)
Pierre Ossman17b04292007-07-22 22:18:46 +0200633 return -ETIMEDOUT;
Pete Popovba264b32005-09-21 06:18:27 +0000634
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100635 au_writel(data->blksz - 1, HOST_BLKSIZE(host));
Pete Popovba264b32005-09-21 06:18:27 +0000636
637 if (host->flags & HOST_F_DMA) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200638#ifdef CONFIG_SOC_AU1200 /* DBDMA */
Pete Popovba264b32005-09-21 06:18:27 +0000639 int i;
640 u32 channel = DMA_CHANNEL(host);
641
642 au1xxx_dbdma_stop(channel);
643
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200644 for (i = 0; i < host->dma.len; i++) {
Pete Popovba264b32005-09-21 06:18:27 +0000645 u32 ret = 0, flags = DDMA_FLAGS_NOIE;
646 struct scatterlist *sg = &data->sg[i];
647 int sg_len = sg->length;
648
649 int len = (datalen > sg_len) ? sg_len : datalen;
650
651 if (i == host->dma.len - 1)
652 flags = DDMA_FLAGS_IE;
653
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200654 if (host->flags & HOST_F_XMIT) {
655 ret = au1xxx_dbdma_put_source_flags(channel,
656 (void *)sg_virt(sg), len, flags);
657 } else {
658 ret = au1xxx_dbdma_put_dest_flags(channel,
659 (void *)sg_virt(sg), len, flags);
Pete Popovba264b32005-09-21 06:18:27 +0000660 }
661
Manuel Laussc4223c22008-06-09 08:36:13 +0200662 if (!ret)
Pete Popovba264b32005-09-21 06:18:27 +0000663 goto dataerr;
664
665 datalen -= len;
666 }
Manuel Laussc4223c22008-06-09 08:36:13 +0200667#endif
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200668 } else {
Pete Popovba264b32005-09-21 06:18:27 +0000669 host->pio.index = 0;
670 host->pio.offset = 0;
671 host->pio.len = datalen;
672
673 if (host->flags & HOST_F_XMIT)
674 IRQ_ON(host, SD_CONFIG_TH);
675 else
676 IRQ_ON(host, SD_CONFIG_NE);
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200677 /* IRQ_ON(host, SD_CONFIG_RA | SD_CONFIG_RF); */
Pete Popovba264b32005-09-21 06:18:27 +0000678 }
679
Pierre Ossman17b04292007-07-22 22:18:46 +0200680 return 0;
Pete Popovba264b32005-09-21 06:18:27 +0000681
Manuel Laussc4223c22008-06-09 08:36:13 +0200682dataerr:
683 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
684 host->dma.dir);
Pierre Ossman17b04292007-07-22 22:18:46 +0200685 return -ETIMEDOUT;
Pete Popovba264b32005-09-21 06:18:27 +0000686}
687
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200688/* This actually starts a command or data transaction */
Pete Popovba264b32005-09-21 06:18:27 +0000689static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
690{
Pete Popovba264b32005-09-21 06:18:27 +0000691 struct au1xmmc_host *host = mmc_priv(mmc);
Pierre Ossman17b04292007-07-22 22:18:46 +0200692 int ret = 0;
Pete Popovba264b32005-09-21 06:18:27 +0000693
694 WARN_ON(irqs_disabled());
695 WARN_ON(host->status != HOST_S_IDLE);
696
697 host->mrq = mrq;
698 host->status = HOST_S_CMD;
699
Pete Popovba264b32005-09-21 06:18:27 +0000700 if (mrq->data) {
701 FLUSH_FIFO(host);
702 ret = au1xmmc_prepare_data(host, mrq->data);
703 }
704
Pierre Ossman17b04292007-07-22 22:18:46 +0200705 if (!ret)
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200706 ret = au1xmmc_send_command(host, 0, mrq->cmd, mrq->data);
Pete Popovba264b32005-09-21 06:18:27 +0000707
Pierre Ossman17b04292007-07-22 22:18:46 +0200708 if (ret) {
Pete Popovba264b32005-09-21 06:18:27 +0000709 mrq->cmd->error = ret;
710 au1xmmc_finish_request(host);
711 }
712}
713
714static void au1xmmc_reset_controller(struct au1xmmc_host *host)
715{
Pete Popovba264b32005-09-21 06:18:27 +0000716 /* Apply the clock */
717 au_writel(SD_ENABLE_CE, HOST_ENABLE(host));
718 au_sync_delay(1);
719
720 au_writel(SD_ENABLE_R | SD_ENABLE_CE, HOST_ENABLE(host));
721 au_sync_delay(5);
722
723 au_writel(~0, HOST_STATUS(host));
724 au_sync();
725
726 au_writel(0, HOST_BLKSIZE(host));
727 au_writel(0x001fffff, HOST_TIMEOUT(host));
728 au_sync();
729
730 au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
731 au_sync();
732
733 au_writel(SD_CONFIG2_EN | SD_CONFIG2_FF, HOST_CONFIG2(host));
734 au_sync_delay(1);
735
736 au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
737 au_sync();
738
739 /* Configure interrupts */
740 au_writel(AU1XMMC_INTERRUPTS, HOST_CONFIG(host));
741 au_sync();
742}
743
744
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200745static void au1xmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pete Popovba264b32005-09-21 06:18:27 +0000746{
747 struct au1xmmc_host *host = mmc_priv(mmc);
Manuel Lauss281dd232008-06-09 08:37:33 +0200748 u32 config2;
Pete Popovba264b32005-09-21 06:18:27 +0000749
Pete Popovba264b32005-09-21 06:18:27 +0000750 if (ios->power_mode == MMC_POWER_OFF)
751 au1xmmc_set_power(host, 0);
752 else if (ios->power_mode == MMC_POWER_ON) {
753 au1xmmc_set_power(host, 1);
754 }
755
756 if (ios->clock && ios->clock != host->clock) {
757 au1xmmc_set_clock(host, ios->clock);
758 host->clock = ios->clock;
759 }
Manuel Lauss281dd232008-06-09 08:37:33 +0200760
761 config2 = au_readl(HOST_CONFIG2(host));
762 switch (ios->bus_width) {
763 case MMC_BUS_WIDTH_4:
764 config2 |= SD_CONFIG2_WB;
765 break;
766 case MMC_BUS_WIDTH_1:
767 config2 &= ~SD_CONFIG2_WB;
768 break;
769 }
770 au_writel(config2, HOST_CONFIG2(host));
771 au_sync();
Pete Popovba264b32005-09-21 06:18:27 +0000772}
773
Manuel Laussc4223c22008-06-09 08:36:13 +0200774#define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
775#define STATUS_DATA_IN (SD_STATUS_NE)
776#define STATUS_DATA_OUT (SD_STATUS_TH)
777
778static irqreturn_t au1xmmc_irq(int irq, void *dev_id)
Pete Popovba264b32005-09-21 06:18:27 +0000779{
Manuel Laussc4223c22008-06-09 08:36:13 +0200780 struct au1xmmc_host *host = dev_id;
781 u32 status;
782
783 status = au_readl(HOST_STATUS(host));
784
785 if (!(status & SD_STATUS_I))
786 return IRQ_NONE; /* not ours */
787
Manuel Lauss20f522f2008-06-09 08:38:03 +0200788 if (status & SD_STATUS_SI) /* SDIO */
789 mmc_signal_sdio_irq(host->mmc);
790
Manuel Laussc4223c22008-06-09 08:36:13 +0200791 if (host->mrq && (status & STATUS_TIMEOUT)) {
792 if (status & SD_STATUS_RAT)
793 host->mrq->cmd->error = -ETIMEDOUT;
794 else if (status & SD_STATUS_DT)
795 host->mrq->data->error = -ETIMEDOUT;
796
797 /* In PIO mode, interrupts might still be enabled */
798 IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
799
800 /* IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF); */
801 tasklet_schedule(&host->finish_task);
802 }
803#if 0
804 else if (status & SD_STATUS_DD) {
805 /* Sometimes we get a DD before a NE in PIO mode */
806 if (!(host->flags & HOST_F_DMA) && (status & SD_STATUS_NE))
807 au1xmmc_receive_pio(host);
808 else {
809 au1xmmc_data_complete(host, status);
810 /* tasklet_schedule(&host->data_task); */
811 }
812 }
813#endif
814 else if (status & SD_STATUS_CR) {
815 if (host->status == HOST_S_CMD)
816 au1xmmc_cmd_complete(host, status);
817
818 } else if (!(host->flags & HOST_F_DMA)) {
819 if ((host->flags & HOST_F_XMIT) && (status & STATUS_DATA_OUT))
820 au1xmmc_send_pio(host);
821 else if ((host->flags & HOST_F_RECV) && (status & STATUS_DATA_IN))
822 au1xmmc_receive_pio(host);
823
824 } else if (status & 0x203F3C70) {
825 DBG("Unhandled status %8.8x\n", host->pdev->id,
826 status);
827 }
828
829 au_writel(status, HOST_STATUS(host));
830 au_sync();
831
832 return IRQ_HANDLED;
833}
834
835#ifdef CONFIG_SOC_AU1200
836/* 8bit memory DMA device */
837static dbdev_tab_t au1xmmc_mem_dbdev = {
838 .dev_id = DSCR_CMD0_ALWAYS,
839 .dev_flags = DEV_FLAGS_ANYUSE,
840 .dev_tsize = 0,
841 .dev_devwidth = 8,
842 .dev_physaddr = 0x00000000,
843 .dev_intlevel = 0,
844 .dev_intpolarity = 0,
845};
846static int memid;
847
848static void au1xmmc_dbdma_callback(int irq, void *dev_id)
849{
850 struct au1xmmc_host *host = (struct au1xmmc_host *)dev_id;
Pete Popovba264b32005-09-21 06:18:27 +0000851
852 /* Avoid spurious interrupts */
Pete Popovba264b32005-09-21 06:18:27 +0000853 if (!host->mrq)
854 return;
855
856 if (host->flags & HOST_F_STOP)
857 SEND_STOP(host);
858
859 tasklet_schedule(&host->data_task);
860}
861
Manuel Laussc4223c22008-06-09 08:36:13 +0200862static int au1xmmc_dbdma_init(struct au1xmmc_host *host)
Pete Popovba264b32005-09-21 06:18:27 +0000863{
Manuel Laussc4223c22008-06-09 08:36:13 +0200864 struct resource *res;
865 int txid, rxid;
Pete Popovba264b32005-09-21 06:18:27 +0000866
Manuel Laussc4223c22008-06-09 08:36:13 +0200867 res = platform_get_resource(host->pdev, IORESOURCE_DMA, 0);
868 if (!res)
869 return -ENODEV;
870 txid = res->start;
Pete Popovba264b32005-09-21 06:18:27 +0000871
Manuel Laussc4223c22008-06-09 08:36:13 +0200872 res = platform_get_resource(host->pdev, IORESOURCE_DMA, 1);
873 if (!res)
874 return -ENODEV;
875 rxid = res->start;
Pete Popovba264b32005-09-21 06:18:27 +0000876
Manuel Laussc4223c22008-06-09 08:36:13 +0200877 if (!memid)
878 return -ENODEV;
Pete Popovba264b32005-09-21 06:18:27 +0000879
Manuel Laussc4223c22008-06-09 08:36:13 +0200880 host->tx_chan = au1xxx_dbdma_chan_alloc(memid, txid,
881 au1xmmc_dbdma_callback, (void *)host);
882 if (!host->tx_chan) {
883 dev_err(&host->pdev->dev, "cannot allocate TX DMA\n");
884 return -ENODEV;
885 }
Pete Popovba264b32005-09-21 06:18:27 +0000886
Manuel Laussc4223c22008-06-09 08:36:13 +0200887 host->rx_chan = au1xxx_dbdma_chan_alloc(rxid, memid,
888 au1xmmc_dbdma_callback, (void *)host);
889 if (!host->rx_chan) {
890 dev_err(&host->pdev->dev, "cannot allocate RX DMA\n");
891 au1xxx_dbdma_chan_free(host->tx_chan);
892 return -ENODEV;
893 }
Pete Popovba264b32005-09-21 06:18:27 +0000894
Manuel Laussc4223c22008-06-09 08:36:13 +0200895 au1xxx_dbdma_set_devwidth(host->tx_chan, 8);
896 au1xxx_dbdma_set_devwidth(host->rx_chan, 8);
Pete Popovba264b32005-09-21 06:18:27 +0000897
Manuel Laussc4223c22008-06-09 08:36:13 +0200898 au1xxx_dbdma_ring_alloc(host->tx_chan, AU1XMMC_DESCRIPTOR_COUNT);
899 au1xxx_dbdma_ring_alloc(host->rx_chan, AU1XMMC_DESCRIPTOR_COUNT);
Pete Popovba264b32005-09-21 06:18:27 +0000900
Manuel Laussc4223c22008-06-09 08:36:13 +0200901 /* DBDMA is good to go */
902 host->flags |= HOST_F_DMA;
Pete Popovba264b32005-09-21 06:18:27 +0000903
Manuel Laussc4223c22008-06-09 08:36:13 +0200904 return 0;
905}
Pete Popovba264b32005-09-21 06:18:27 +0000906
Manuel Laussc4223c22008-06-09 08:36:13 +0200907static void au1xmmc_dbdma_shutdown(struct au1xmmc_host *host)
908{
909 if (host->flags & HOST_F_DMA) {
910 host->flags &= ~HOST_F_DMA;
911 au1xxx_dbdma_chan_free(host->tx_chan);
912 au1xxx_dbdma_chan_free(host->rx_chan);
913 }
914}
Pete Popovba264b32005-09-21 06:18:27 +0000915#endif
Pete Popovba264b32005-09-21 06:18:27 +0000916
Manuel Lauss20f522f2008-06-09 08:38:03 +0200917static void au1xmmc_enable_sdio_irq(struct mmc_host *mmc, int en)
918{
919 struct au1xmmc_host *host = mmc_priv(mmc);
920
921 if (en)
922 IRQ_ON(host, SD_CONFIG_SI);
923 else
924 IRQ_OFF(host, SD_CONFIG_SI);
925}
926
Yoichi Yuasabf8c80a2006-12-05 07:43:38 +0100927static const struct mmc_host_ops au1xmmc_ops = {
Pete Popovba264b32005-09-21 06:18:27 +0000928 .request = au1xmmc_request,
929 .set_ios = au1xmmc_set_ios,
Manuel Lauss82999772007-01-25 10:29:24 +0100930 .get_ro = au1xmmc_card_readonly,
Manuel Lauss20f522f2008-06-09 08:38:03 +0200931 .enable_sdio_irq = au1xmmc_enable_sdio_irq,
Pete Popovba264b32005-09-21 06:18:27 +0000932};
933
Manuel Laussc4223c22008-06-09 08:36:13 +0200934static void au1xmmc_poll_event(unsigned long arg)
935{
936 struct au1xmmc_host *host = (struct au1xmmc_host *)arg;
937 int card = au1xmmc_card_inserted(host);
938 int controller = (host->flags & HOST_F_ACTIVE) ? 1 : 0;
939
940 if (card != controller) {
941 host->flags &= ~HOST_F_ACTIVE;
942 if (card)
943 host->flags |= HOST_F_ACTIVE;
944 mmc_detect_change(host->mmc, 0);
945 }
946
947#ifdef DEBUG
948 if (host->mrq != NULL) {
949 u32 status = au_readl(HOST_STATUS(host));
950 DBG("PENDING - %8.8x\n", host->pdev->id, status);
951 }
952#endif
953 mod_timer(&host->timer, jiffies + AU1XMMC_DETECT_TIMEOUT);
954}
955
956static void au1xmmc_init_cd_poll_timer(struct au1xmmc_host *host)
957{
958 init_timer(&host->timer);
959 host->timer.function = au1xmmc_poll_event;
960 host->timer.data = (unsigned long)host;
961 host->timer.expires = jiffies + AU1XMMC_DETECT_TIMEOUT;
962}
963
Martin Michlmayrb256f9d2006-03-04 23:01:13 +0000964static int __devinit au1xmmc_probe(struct platform_device *pdev)
Pete Popovba264b32005-09-21 06:18:27 +0000965{
Manuel Laussc4223c22008-06-09 08:36:13 +0200966 struct mmc_host *mmc;
967 struct au1xmmc_host *host;
968 struct resource *r;
969 int ret;
Pete Popovba264b32005-09-21 06:18:27 +0000970
Manuel Laussc4223c22008-06-09 08:36:13 +0200971 mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), &pdev->dev);
972 if (!mmc) {
973 dev_err(&pdev->dev, "no memory for mmc_host\n");
974 ret = -ENOMEM;
975 goto out0;
Pete Popovba264b32005-09-21 06:18:27 +0000976 }
977
Manuel Laussc4223c22008-06-09 08:36:13 +0200978 host = mmc_priv(mmc);
979 host->mmc = mmc;
980 host->platdata = pdev->dev.platform_data;
981 host->pdev = pdev;
Pete Popovba264b32005-09-21 06:18:27 +0000982
Manuel Laussc4223c22008-06-09 08:36:13 +0200983 ret = -ENODEV;
984 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
985 if (!r) {
986 dev_err(&pdev->dev, "no mmio defined\n");
987 goto out1;
988 }
Pete Popovba264b32005-09-21 06:18:27 +0000989
Manuel Laussc4223c22008-06-09 08:36:13 +0200990 host->ioarea = request_mem_region(r->start, r->end - r->start + 1,
991 pdev->name);
992 if (!host->ioarea) {
993 dev_err(&pdev->dev, "mmio already in use\n");
994 goto out1;
995 }
996
997 host->iobase = (unsigned long)ioremap(r->start, 0x3c);
998 if (!host->iobase) {
999 dev_err(&pdev->dev, "cannot remap mmio\n");
1000 goto out2;
1001 }
1002
1003 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1004 if (!r) {
1005 dev_err(&pdev->dev, "no IRQ defined\n");
1006 goto out3;
1007 }
1008
1009 host->irq = r->start;
1010 /* IRQ is shared among both SD controllers */
1011 ret = request_irq(host->irq, au1xmmc_irq, IRQF_SHARED,
1012 DRIVER_NAME, host);
1013 if (ret) {
1014 dev_err(&pdev->dev, "cannot grab IRQ\n");
1015 goto out3;
1016 }
1017
1018 mmc->ops = &au1xmmc_ops;
1019
1020 mmc->f_min = 450000;
1021 mmc->f_max = 24000000;
1022
1023 mmc->max_seg_size = AU1XMMC_DESCRIPTOR_SIZE;
1024 mmc->max_phys_segs = AU1XMMC_DESCRIPTOR_COUNT;
1025
1026 mmc->max_blk_size = 2048;
1027 mmc->max_blk_count = 512;
1028
1029 mmc->ocr_avail = AU1XMMC_OCR;
Manuel Lauss20f522f2008-06-09 08:38:03 +02001030 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
Manuel Laussc4223c22008-06-09 08:36:13 +02001031
1032 host->status = HOST_S_IDLE;
1033
1034 /* board-specific carddetect setup, if any */
1035 if (host->platdata && host->platdata->cd_setup) {
1036 ret = host->platdata->cd_setup(mmc, 1);
1037 if (ret) {
1038 dev_err(&pdev->dev, "board CD setup failed\n");
1039 goto out4;
Pete Popovba264b32005-09-21 06:18:27 +00001040 }
Manuel Laussc4223c22008-06-09 08:36:13 +02001041 } else {
1042 /* poll the board-specific is-card-in-socket-? method */
1043 au1xmmc_init_cd_poll_timer(host);
1044 }
Pete Popovba264b32005-09-21 06:18:27 +00001045
Manuel Laussc4223c22008-06-09 08:36:13 +02001046 tasklet_init(&host->data_task, au1xmmc_tasklet_data,
1047 (unsigned long)host);
Pete Popovba264b32005-09-21 06:18:27 +00001048
Manuel Laussc4223c22008-06-09 08:36:13 +02001049 tasklet_init(&host->finish_task, au1xmmc_tasklet_finish,
1050 (unsigned long)host);
Pete Popovba264b32005-09-21 06:18:27 +00001051
Manuel Laussc4223c22008-06-09 08:36:13 +02001052#ifdef CONFIG_SOC_AU1200
1053 ret = au1xmmc_dbdma_init(host);
1054 if (ret)
1055 printk(KERN_INFO DRIVER_NAME ": DBDMA init failed; using PIO\n");
1056#endif
Pete Popovba264b32005-09-21 06:18:27 +00001057
Manuel Laussc4223c22008-06-09 08:36:13 +02001058#ifdef CONFIG_LEDS_CLASS
1059 if (host->platdata && host->platdata->led) {
1060 struct led_classdev *led = host->platdata->led;
1061 led->name = mmc_hostname(mmc);
1062 led->brightness = LED_OFF;
1063 led->default_trigger = mmc_hostname(mmc);
1064 ret = led_classdev_register(mmc_dev(mmc), led);
1065 if (ret)
1066 goto out5;
1067 }
1068#endif
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001069
Manuel Laussc4223c22008-06-09 08:36:13 +02001070 au1xmmc_reset_controller(host);
Pete Popovba264b32005-09-21 06:18:27 +00001071
Manuel Laussc4223c22008-06-09 08:36:13 +02001072 ret = mmc_add_host(mmc);
1073 if (ret) {
1074 dev_err(&pdev->dev, "cannot add mmc host\n");
1075 goto out6;
1076 }
Pete Popovba264b32005-09-21 06:18:27 +00001077
Manuel Laussc4223c22008-06-09 08:36:13 +02001078 platform_set_drvdata(pdev, mmc);
Pete Popovba264b32005-09-21 06:18:27 +00001079
Manuel Laussc4223c22008-06-09 08:36:13 +02001080 /* start the carddetect poll timer if necessary */
1081 if (!(host->platdata && host->platdata->cd_setup))
Pete Popovba264b32005-09-21 06:18:27 +00001082 add_timer(&host->timer);
1083
Manuel Laussc4223c22008-06-09 08:36:13 +02001084 printk(KERN_INFO DRIVER_NAME ": MMC Controller %d set up at %8.8X"
1085 " (mode=%s)\n", pdev->id, host->iobase,
1086 host->flags & HOST_F_DMA ? "dma" : "pio");
Pete Popovba264b32005-09-21 06:18:27 +00001087
Manuel Laussc4223c22008-06-09 08:36:13 +02001088 return 0; /* all ok */
Pete Popovba264b32005-09-21 06:18:27 +00001089
Manuel Laussc4223c22008-06-09 08:36:13 +02001090out6:
1091#ifdef CONFIG_LEDS_CLASS
1092 if (host->platdata && host->platdata->led)
1093 led_classdev_unregister(host->platdata->led);
1094out5:
1095#endif
1096 au_writel(0, HOST_ENABLE(host));
1097 au_writel(0, HOST_CONFIG(host));
1098 au_writel(0, HOST_CONFIG2(host));
1099 au_sync();
1100
1101#ifdef CONFIG_SOC_AU1200
1102 au1xmmc_dbdma_shutdown(host);
1103#endif
1104
1105 tasklet_kill(&host->data_task);
1106 tasklet_kill(&host->finish_task);
1107
1108 if (host->platdata && host->platdata->cd_setup)
1109 host->platdata->cd_setup(mmc, 0);
1110out4:
1111 free_irq(host->irq, host);
1112out3:
1113 iounmap((void *)host->iobase);
1114out2:
1115 release_resource(host->ioarea);
1116 kfree(host->ioarea);
1117out1:
1118 mmc_free_host(mmc);
1119out0:
1120 return ret;
Pete Popovba264b32005-09-21 06:18:27 +00001121}
1122
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001123static int __devexit au1xmmc_remove(struct platform_device *pdev)
Pete Popovba264b32005-09-21 06:18:27 +00001124{
Manuel Laussc4223c22008-06-09 08:36:13 +02001125 struct mmc_host *mmc = platform_get_drvdata(pdev);
1126 struct au1xmmc_host *host;
Pete Popovba264b32005-09-21 06:18:27 +00001127
Manuel Laussc4223c22008-06-09 08:36:13 +02001128 if (mmc) {
1129 host = mmc_priv(mmc);
Pete Popovba264b32005-09-21 06:18:27 +00001130
Manuel Laussc4223c22008-06-09 08:36:13 +02001131 mmc_remove_host(mmc);
Pete Popovba264b32005-09-21 06:18:27 +00001132
Manuel Laussc4223c22008-06-09 08:36:13 +02001133#ifdef CONFIG_LEDS_CLASS
1134 if (host->platdata && host->platdata->led)
1135 led_classdev_unregister(host->platdata->led);
1136#endif
1137
1138 if (host->platdata && host->platdata->cd_setup)
1139 host->platdata->cd_setup(mmc, 0);
1140 else
1141 del_timer_sync(&host->timer);
1142
1143 au_writel(0, HOST_ENABLE(host));
1144 au_writel(0, HOST_CONFIG(host));
1145 au_writel(0, HOST_CONFIG2(host));
1146 au_sync();
Pete Popovba264b32005-09-21 06:18:27 +00001147
1148 tasklet_kill(&host->data_task);
1149 tasklet_kill(&host->finish_task);
1150
Manuel Laussc4223c22008-06-09 08:36:13 +02001151#ifdef CONFIG_SOC_AU1200
1152 au1xmmc_dbdma_shutdown(host);
1153#endif
Pete Popovba264b32005-09-21 06:18:27 +00001154 au1xmmc_set_power(host, 0);
1155
Manuel Laussc4223c22008-06-09 08:36:13 +02001156 free_irq(host->irq, host);
1157 iounmap((void *)host->iobase);
1158 release_resource(host->ioarea);
1159 kfree(host->ioarea);
Pete Popovba264b32005-09-21 06:18:27 +00001160
Manuel Laussc4223c22008-06-09 08:36:13 +02001161 mmc_free_host(mmc);
Pete Popovba264b32005-09-21 06:18:27 +00001162 }
Pete Popovba264b32005-09-21 06:18:27 +00001163 return 0;
1164}
1165
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001166static struct platform_driver au1xmmc_driver = {
Pete Popovba264b32005-09-21 06:18:27 +00001167 .probe = au1xmmc_probe,
1168 .remove = au1xmmc_remove,
1169 .suspend = NULL,
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001170 .resume = NULL,
1171 .driver = {
1172 .name = DRIVER_NAME,
Kay Sieversbc65c722008-04-15 14:34:28 -07001173 .owner = THIS_MODULE,
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001174 },
Pete Popovba264b32005-09-21 06:18:27 +00001175};
1176
1177static int __init au1xmmc_init(void)
1178{
Manuel Laussc4223c22008-06-09 08:36:13 +02001179#ifdef CONFIG_SOC_AU1200
1180 /* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride
1181 * of 8 bits. And since devices are shared, we need to create
1182 * our own to avoid freaking out other devices.
1183 */
1184 memid = au1xxx_ddma_add_device(&au1xmmc_mem_dbdev);
1185 if (!memid)
1186 printk(KERN_ERR "au1xmmc: cannot add memory dbdma dev\n");
1187#endif
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001188 return platform_driver_register(&au1xmmc_driver);
Pete Popovba264b32005-09-21 06:18:27 +00001189}
1190
1191static void __exit au1xmmc_exit(void)
1192{
Manuel Laussc4223c22008-06-09 08:36:13 +02001193#ifdef CONFIG_SOC_AU1200
1194 if (memid)
1195 au1xxx_ddma_del_device(memid);
1196#endif
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001197 platform_driver_unregister(&au1xmmc_driver);
Pete Popovba264b32005-09-21 06:18:27 +00001198}
1199
1200module_init(au1xmmc_init);
1201module_exit(au1xmmc_exit);
1202
Pete Popovba264b32005-09-21 06:18:27 +00001203MODULE_AUTHOR("Advanced Micro Devices, Inc");
1204MODULE_DESCRIPTION("MMC/SD driver for the Alchemy Au1XXX");
1205MODULE_LICENSE("GPL");
Kay Sieversbc65c722008-04-15 14:34:28 -07001206MODULE_ALIAS("platform:au1xxx-mmc");