blob: 718eb879587f5b8a7c675aab4e097edfd0d8cbb3 [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
Manuel Lauss88b8d9a2008-06-09 08:39:11 +0200700 /* fail request immediately if no card is present */
701 if (0 == au1xmmc_card_inserted(host)) {
702 mrq->cmd->error = -ENOMEDIUM;
703 au1xmmc_finish_request(host);
704 return;
705 }
706
Pete Popovba264b32005-09-21 06:18:27 +0000707 if (mrq->data) {
708 FLUSH_FIFO(host);
709 ret = au1xmmc_prepare_data(host, mrq->data);
710 }
711
Pierre Ossman17b04292007-07-22 22:18:46 +0200712 if (!ret)
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200713 ret = au1xmmc_send_command(host, 0, mrq->cmd, mrq->data);
Pete Popovba264b32005-09-21 06:18:27 +0000714
Pierre Ossman17b04292007-07-22 22:18:46 +0200715 if (ret) {
Pete Popovba264b32005-09-21 06:18:27 +0000716 mrq->cmd->error = ret;
717 au1xmmc_finish_request(host);
718 }
719}
720
721static void au1xmmc_reset_controller(struct au1xmmc_host *host)
722{
Pete Popovba264b32005-09-21 06:18:27 +0000723 /* Apply the clock */
724 au_writel(SD_ENABLE_CE, HOST_ENABLE(host));
725 au_sync_delay(1);
726
727 au_writel(SD_ENABLE_R | SD_ENABLE_CE, HOST_ENABLE(host));
728 au_sync_delay(5);
729
730 au_writel(~0, HOST_STATUS(host));
731 au_sync();
732
733 au_writel(0, HOST_BLKSIZE(host));
734 au_writel(0x001fffff, HOST_TIMEOUT(host));
735 au_sync();
736
737 au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
738 au_sync();
739
740 au_writel(SD_CONFIG2_EN | SD_CONFIG2_FF, HOST_CONFIG2(host));
741 au_sync_delay(1);
742
743 au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
744 au_sync();
745
746 /* Configure interrupts */
747 au_writel(AU1XMMC_INTERRUPTS, HOST_CONFIG(host));
748 au_sync();
749}
750
751
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200752static void au1xmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pete Popovba264b32005-09-21 06:18:27 +0000753{
754 struct au1xmmc_host *host = mmc_priv(mmc);
Manuel Lauss281dd232008-06-09 08:37:33 +0200755 u32 config2;
Pete Popovba264b32005-09-21 06:18:27 +0000756
Pete Popovba264b32005-09-21 06:18:27 +0000757 if (ios->power_mode == MMC_POWER_OFF)
758 au1xmmc_set_power(host, 0);
759 else if (ios->power_mode == MMC_POWER_ON) {
760 au1xmmc_set_power(host, 1);
761 }
762
763 if (ios->clock && ios->clock != host->clock) {
764 au1xmmc_set_clock(host, ios->clock);
765 host->clock = ios->clock;
766 }
Manuel Lauss281dd232008-06-09 08:37:33 +0200767
768 config2 = au_readl(HOST_CONFIG2(host));
769 switch (ios->bus_width) {
770 case MMC_BUS_WIDTH_4:
771 config2 |= SD_CONFIG2_WB;
772 break;
773 case MMC_BUS_WIDTH_1:
774 config2 &= ~SD_CONFIG2_WB;
775 break;
776 }
777 au_writel(config2, HOST_CONFIG2(host));
778 au_sync();
Pete Popovba264b32005-09-21 06:18:27 +0000779}
780
Manuel Laussc4223c22008-06-09 08:36:13 +0200781#define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
782#define STATUS_DATA_IN (SD_STATUS_NE)
783#define STATUS_DATA_OUT (SD_STATUS_TH)
784
785static irqreturn_t au1xmmc_irq(int irq, void *dev_id)
Pete Popovba264b32005-09-21 06:18:27 +0000786{
Manuel Laussc4223c22008-06-09 08:36:13 +0200787 struct au1xmmc_host *host = dev_id;
788 u32 status;
789
790 status = au_readl(HOST_STATUS(host));
791
792 if (!(status & SD_STATUS_I))
793 return IRQ_NONE; /* not ours */
794
Manuel Lauss20f522f2008-06-09 08:38:03 +0200795 if (status & SD_STATUS_SI) /* SDIO */
796 mmc_signal_sdio_irq(host->mmc);
797
Manuel Laussc4223c22008-06-09 08:36:13 +0200798 if (host->mrq && (status & STATUS_TIMEOUT)) {
799 if (status & SD_STATUS_RAT)
800 host->mrq->cmd->error = -ETIMEDOUT;
801 else if (status & SD_STATUS_DT)
802 host->mrq->data->error = -ETIMEDOUT;
803
804 /* In PIO mode, interrupts might still be enabled */
805 IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
806
807 /* IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF); */
808 tasklet_schedule(&host->finish_task);
809 }
810#if 0
811 else if (status & SD_STATUS_DD) {
812 /* Sometimes we get a DD before a NE in PIO mode */
813 if (!(host->flags & HOST_F_DMA) && (status & SD_STATUS_NE))
814 au1xmmc_receive_pio(host);
815 else {
816 au1xmmc_data_complete(host, status);
817 /* tasklet_schedule(&host->data_task); */
818 }
819 }
820#endif
821 else if (status & SD_STATUS_CR) {
822 if (host->status == HOST_S_CMD)
823 au1xmmc_cmd_complete(host, status);
824
825 } else if (!(host->flags & HOST_F_DMA)) {
826 if ((host->flags & HOST_F_XMIT) && (status & STATUS_DATA_OUT))
827 au1xmmc_send_pio(host);
828 else if ((host->flags & HOST_F_RECV) && (status & STATUS_DATA_IN))
829 au1xmmc_receive_pio(host);
830
831 } else if (status & 0x203F3C70) {
832 DBG("Unhandled status %8.8x\n", host->pdev->id,
833 status);
834 }
835
836 au_writel(status, HOST_STATUS(host));
837 au_sync();
838
839 return IRQ_HANDLED;
840}
841
842#ifdef CONFIG_SOC_AU1200
843/* 8bit memory DMA device */
844static dbdev_tab_t au1xmmc_mem_dbdev = {
845 .dev_id = DSCR_CMD0_ALWAYS,
846 .dev_flags = DEV_FLAGS_ANYUSE,
847 .dev_tsize = 0,
848 .dev_devwidth = 8,
849 .dev_physaddr = 0x00000000,
850 .dev_intlevel = 0,
851 .dev_intpolarity = 0,
852};
853static int memid;
854
855static void au1xmmc_dbdma_callback(int irq, void *dev_id)
856{
857 struct au1xmmc_host *host = (struct au1xmmc_host *)dev_id;
Pete Popovba264b32005-09-21 06:18:27 +0000858
859 /* Avoid spurious interrupts */
Pete Popovba264b32005-09-21 06:18:27 +0000860 if (!host->mrq)
861 return;
862
863 if (host->flags & HOST_F_STOP)
864 SEND_STOP(host);
865
866 tasklet_schedule(&host->data_task);
867}
868
Manuel Laussc4223c22008-06-09 08:36:13 +0200869static int au1xmmc_dbdma_init(struct au1xmmc_host *host)
Pete Popovba264b32005-09-21 06:18:27 +0000870{
Manuel Laussc4223c22008-06-09 08:36:13 +0200871 struct resource *res;
872 int txid, rxid;
Pete Popovba264b32005-09-21 06:18:27 +0000873
Manuel Laussc4223c22008-06-09 08:36:13 +0200874 res = platform_get_resource(host->pdev, IORESOURCE_DMA, 0);
875 if (!res)
876 return -ENODEV;
877 txid = res->start;
Pete Popovba264b32005-09-21 06:18:27 +0000878
Manuel Laussc4223c22008-06-09 08:36:13 +0200879 res = platform_get_resource(host->pdev, IORESOURCE_DMA, 1);
880 if (!res)
881 return -ENODEV;
882 rxid = res->start;
Pete Popovba264b32005-09-21 06:18:27 +0000883
Manuel Laussc4223c22008-06-09 08:36:13 +0200884 if (!memid)
885 return -ENODEV;
Pete Popovba264b32005-09-21 06:18:27 +0000886
Manuel Laussc4223c22008-06-09 08:36:13 +0200887 host->tx_chan = au1xxx_dbdma_chan_alloc(memid, txid,
888 au1xmmc_dbdma_callback, (void *)host);
889 if (!host->tx_chan) {
890 dev_err(&host->pdev->dev, "cannot allocate TX DMA\n");
891 return -ENODEV;
892 }
Pete Popovba264b32005-09-21 06:18:27 +0000893
Manuel Laussc4223c22008-06-09 08:36:13 +0200894 host->rx_chan = au1xxx_dbdma_chan_alloc(rxid, memid,
895 au1xmmc_dbdma_callback, (void *)host);
896 if (!host->rx_chan) {
897 dev_err(&host->pdev->dev, "cannot allocate RX DMA\n");
898 au1xxx_dbdma_chan_free(host->tx_chan);
899 return -ENODEV;
900 }
Pete Popovba264b32005-09-21 06:18:27 +0000901
Manuel Laussc4223c22008-06-09 08:36:13 +0200902 au1xxx_dbdma_set_devwidth(host->tx_chan, 8);
903 au1xxx_dbdma_set_devwidth(host->rx_chan, 8);
Pete Popovba264b32005-09-21 06:18:27 +0000904
Manuel Laussc4223c22008-06-09 08:36:13 +0200905 au1xxx_dbdma_ring_alloc(host->tx_chan, AU1XMMC_DESCRIPTOR_COUNT);
906 au1xxx_dbdma_ring_alloc(host->rx_chan, AU1XMMC_DESCRIPTOR_COUNT);
Pete Popovba264b32005-09-21 06:18:27 +0000907
Manuel Laussc4223c22008-06-09 08:36:13 +0200908 /* DBDMA is good to go */
909 host->flags |= HOST_F_DMA;
Pete Popovba264b32005-09-21 06:18:27 +0000910
Manuel Laussc4223c22008-06-09 08:36:13 +0200911 return 0;
912}
Pete Popovba264b32005-09-21 06:18:27 +0000913
Manuel Laussc4223c22008-06-09 08:36:13 +0200914static void au1xmmc_dbdma_shutdown(struct au1xmmc_host *host)
915{
916 if (host->flags & HOST_F_DMA) {
917 host->flags &= ~HOST_F_DMA;
918 au1xxx_dbdma_chan_free(host->tx_chan);
919 au1xxx_dbdma_chan_free(host->rx_chan);
920 }
921}
Pete Popovba264b32005-09-21 06:18:27 +0000922#endif
Pete Popovba264b32005-09-21 06:18:27 +0000923
Manuel Lauss20f522f2008-06-09 08:38:03 +0200924static void au1xmmc_enable_sdio_irq(struct mmc_host *mmc, int en)
925{
926 struct au1xmmc_host *host = mmc_priv(mmc);
927
928 if (en)
929 IRQ_ON(host, SD_CONFIG_SI);
930 else
931 IRQ_OFF(host, SD_CONFIG_SI);
932}
933
Yoichi Yuasabf8c80a2006-12-05 07:43:38 +0100934static const struct mmc_host_ops au1xmmc_ops = {
Pete Popovba264b32005-09-21 06:18:27 +0000935 .request = au1xmmc_request,
936 .set_ios = au1xmmc_set_ios,
Manuel Lauss82999772007-01-25 10:29:24 +0100937 .get_ro = au1xmmc_card_readonly,
Manuel Lauss20f522f2008-06-09 08:38:03 +0200938 .enable_sdio_irq = au1xmmc_enable_sdio_irq,
Pete Popovba264b32005-09-21 06:18:27 +0000939};
940
Manuel Laussc4223c22008-06-09 08:36:13 +0200941static void au1xmmc_poll_event(unsigned long arg)
942{
943 struct au1xmmc_host *host = (struct au1xmmc_host *)arg;
944 int card = au1xmmc_card_inserted(host);
945 int controller = (host->flags & HOST_F_ACTIVE) ? 1 : 0;
946
947 if (card != controller) {
948 host->flags &= ~HOST_F_ACTIVE;
949 if (card)
950 host->flags |= HOST_F_ACTIVE;
951 mmc_detect_change(host->mmc, 0);
952 }
953
954#ifdef DEBUG
955 if (host->mrq != NULL) {
956 u32 status = au_readl(HOST_STATUS(host));
957 DBG("PENDING - %8.8x\n", host->pdev->id, status);
958 }
959#endif
960 mod_timer(&host->timer, jiffies + AU1XMMC_DETECT_TIMEOUT);
961}
962
963static void au1xmmc_init_cd_poll_timer(struct au1xmmc_host *host)
964{
965 init_timer(&host->timer);
966 host->timer.function = au1xmmc_poll_event;
967 host->timer.data = (unsigned long)host;
968 host->timer.expires = jiffies + AU1XMMC_DETECT_TIMEOUT;
969}
970
Martin Michlmayrb256f9d2006-03-04 23:01:13 +0000971static int __devinit au1xmmc_probe(struct platform_device *pdev)
Pete Popovba264b32005-09-21 06:18:27 +0000972{
Manuel Laussc4223c22008-06-09 08:36:13 +0200973 struct mmc_host *mmc;
974 struct au1xmmc_host *host;
975 struct resource *r;
976 int ret;
Pete Popovba264b32005-09-21 06:18:27 +0000977
Manuel Laussc4223c22008-06-09 08:36:13 +0200978 mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), &pdev->dev);
979 if (!mmc) {
980 dev_err(&pdev->dev, "no memory for mmc_host\n");
981 ret = -ENOMEM;
982 goto out0;
Pete Popovba264b32005-09-21 06:18:27 +0000983 }
984
Manuel Laussc4223c22008-06-09 08:36:13 +0200985 host = mmc_priv(mmc);
986 host->mmc = mmc;
987 host->platdata = pdev->dev.platform_data;
988 host->pdev = pdev;
Pete Popovba264b32005-09-21 06:18:27 +0000989
Manuel Laussc4223c22008-06-09 08:36:13 +0200990 ret = -ENODEV;
991 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
992 if (!r) {
993 dev_err(&pdev->dev, "no mmio defined\n");
994 goto out1;
995 }
Pete Popovba264b32005-09-21 06:18:27 +0000996
Manuel Laussc4223c22008-06-09 08:36:13 +0200997 host->ioarea = request_mem_region(r->start, r->end - r->start + 1,
998 pdev->name);
999 if (!host->ioarea) {
1000 dev_err(&pdev->dev, "mmio already in use\n");
1001 goto out1;
1002 }
1003
1004 host->iobase = (unsigned long)ioremap(r->start, 0x3c);
1005 if (!host->iobase) {
1006 dev_err(&pdev->dev, "cannot remap mmio\n");
1007 goto out2;
1008 }
1009
1010 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1011 if (!r) {
1012 dev_err(&pdev->dev, "no IRQ defined\n");
1013 goto out3;
1014 }
1015
1016 host->irq = r->start;
1017 /* IRQ is shared among both SD controllers */
1018 ret = request_irq(host->irq, au1xmmc_irq, IRQF_SHARED,
1019 DRIVER_NAME, host);
1020 if (ret) {
1021 dev_err(&pdev->dev, "cannot grab IRQ\n");
1022 goto out3;
1023 }
1024
1025 mmc->ops = &au1xmmc_ops;
1026
1027 mmc->f_min = 450000;
1028 mmc->f_max = 24000000;
1029
1030 mmc->max_seg_size = AU1XMMC_DESCRIPTOR_SIZE;
1031 mmc->max_phys_segs = AU1XMMC_DESCRIPTOR_COUNT;
1032
1033 mmc->max_blk_size = 2048;
1034 mmc->max_blk_count = 512;
1035
1036 mmc->ocr_avail = AU1XMMC_OCR;
Manuel Lauss20f522f2008-06-09 08:38:03 +02001037 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
Manuel Laussc4223c22008-06-09 08:36:13 +02001038
1039 host->status = HOST_S_IDLE;
1040
1041 /* board-specific carddetect setup, if any */
1042 if (host->platdata && host->platdata->cd_setup) {
1043 ret = host->platdata->cd_setup(mmc, 1);
1044 if (ret) {
1045 dev_err(&pdev->dev, "board CD setup failed\n");
1046 goto out4;
Pete Popovba264b32005-09-21 06:18:27 +00001047 }
Manuel Laussc4223c22008-06-09 08:36:13 +02001048 } else {
1049 /* poll the board-specific is-card-in-socket-? method */
1050 au1xmmc_init_cd_poll_timer(host);
1051 }
Pete Popovba264b32005-09-21 06:18:27 +00001052
Manuel Laussc4223c22008-06-09 08:36:13 +02001053 tasklet_init(&host->data_task, au1xmmc_tasklet_data,
1054 (unsigned long)host);
Pete Popovba264b32005-09-21 06:18:27 +00001055
Manuel Laussc4223c22008-06-09 08:36:13 +02001056 tasklet_init(&host->finish_task, au1xmmc_tasklet_finish,
1057 (unsigned long)host);
Pete Popovba264b32005-09-21 06:18:27 +00001058
Manuel Laussc4223c22008-06-09 08:36:13 +02001059#ifdef CONFIG_SOC_AU1200
1060 ret = au1xmmc_dbdma_init(host);
1061 if (ret)
1062 printk(KERN_INFO DRIVER_NAME ": DBDMA init failed; using PIO\n");
1063#endif
Pete Popovba264b32005-09-21 06:18:27 +00001064
Manuel Laussc4223c22008-06-09 08:36:13 +02001065#ifdef CONFIG_LEDS_CLASS
1066 if (host->platdata && host->platdata->led) {
1067 struct led_classdev *led = host->platdata->led;
1068 led->name = mmc_hostname(mmc);
1069 led->brightness = LED_OFF;
1070 led->default_trigger = mmc_hostname(mmc);
1071 ret = led_classdev_register(mmc_dev(mmc), led);
1072 if (ret)
1073 goto out5;
1074 }
1075#endif
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001076
Manuel Laussc4223c22008-06-09 08:36:13 +02001077 au1xmmc_reset_controller(host);
Pete Popovba264b32005-09-21 06:18:27 +00001078
Manuel Laussc4223c22008-06-09 08:36:13 +02001079 ret = mmc_add_host(mmc);
1080 if (ret) {
1081 dev_err(&pdev->dev, "cannot add mmc host\n");
1082 goto out6;
1083 }
Pete Popovba264b32005-09-21 06:18:27 +00001084
Manuel Laussc4223c22008-06-09 08:36:13 +02001085 platform_set_drvdata(pdev, mmc);
Pete Popovba264b32005-09-21 06:18:27 +00001086
Manuel Laussc4223c22008-06-09 08:36:13 +02001087 /* start the carddetect poll timer if necessary */
1088 if (!(host->platdata && host->platdata->cd_setup))
Pete Popovba264b32005-09-21 06:18:27 +00001089 add_timer(&host->timer);
1090
Manuel Laussc4223c22008-06-09 08:36:13 +02001091 printk(KERN_INFO DRIVER_NAME ": MMC Controller %d set up at %8.8X"
1092 " (mode=%s)\n", pdev->id, host->iobase,
1093 host->flags & HOST_F_DMA ? "dma" : "pio");
Pete Popovba264b32005-09-21 06:18:27 +00001094
Manuel Laussc4223c22008-06-09 08:36:13 +02001095 return 0; /* all ok */
Pete Popovba264b32005-09-21 06:18:27 +00001096
Manuel Laussc4223c22008-06-09 08:36:13 +02001097out6:
1098#ifdef CONFIG_LEDS_CLASS
1099 if (host->platdata && host->platdata->led)
1100 led_classdev_unregister(host->platdata->led);
1101out5:
1102#endif
1103 au_writel(0, HOST_ENABLE(host));
1104 au_writel(0, HOST_CONFIG(host));
1105 au_writel(0, HOST_CONFIG2(host));
1106 au_sync();
1107
1108#ifdef CONFIG_SOC_AU1200
1109 au1xmmc_dbdma_shutdown(host);
1110#endif
1111
1112 tasklet_kill(&host->data_task);
1113 tasklet_kill(&host->finish_task);
1114
1115 if (host->platdata && host->platdata->cd_setup)
1116 host->platdata->cd_setup(mmc, 0);
1117out4:
1118 free_irq(host->irq, host);
1119out3:
1120 iounmap((void *)host->iobase);
1121out2:
1122 release_resource(host->ioarea);
1123 kfree(host->ioarea);
1124out1:
1125 mmc_free_host(mmc);
1126out0:
1127 return ret;
Pete Popovba264b32005-09-21 06:18:27 +00001128}
1129
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001130static int __devexit au1xmmc_remove(struct platform_device *pdev)
Pete Popovba264b32005-09-21 06:18:27 +00001131{
Manuel Laussc4223c22008-06-09 08:36:13 +02001132 struct mmc_host *mmc = platform_get_drvdata(pdev);
1133 struct au1xmmc_host *host;
Pete Popovba264b32005-09-21 06:18:27 +00001134
Manuel Laussc4223c22008-06-09 08:36:13 +02001135 if (mmc) {
1136 host = mmc_priv(mmc);
Pete Popovba264b32005-09-21 06:18:27 +00001137
Manuel Laussc4223c22008-06-09 08:36:13 +02001138 mmc_remove_host(mmc);
Pete Popovba264b32005-09-21 06:18:27 +00001139
Manuel Laussc4223c22008-06-09 08:36:13 +02001140#ifdef CONFIG_LEDS_CLASS
1141 if (host->platdata && host->platdata->led)
1142 led_classdev_unregister(host->platdata->led);
1143#endif
1144
1145 if (host->platdata && host->platdata->cd_setup)
1146 host->platdata->cd_setup(mmc, 0);
1147 else
1148 del_timer_sync(&host->timer);
1149
1150 au_writel(0, HOST_ENABLE(host));
1151 au_writel(0, HOST_CONFIG(host));
1152 au_writel(0, HOST_CONFIG2(host));
1153 au_sync();
Pete Popovba264b32005-09-21 06:18:27 +00001154
1155 tasklet_kill(&host->data_task);
1156 tasklet_kill(&host->finish_task);
1157
Manuel Laussc4223c22008-06-09 08:36:13 +02001158#ifdef CONFIG_SOC_AU1200
1159 au1xmmc_dbdma_shutdown(host);
1160#endif
Pete Popovba264b32005-09-21 06:18:27 +00001161 au1xmmc_set_power(host, 0);
1162
Manuel Laussc4223c22008-06-09 08:36:13 +02001163 free_irq(host->irq, host);
1164 iounmap((void *)host->iobase);
1165 release_resource(host->ioarea);
1166 kfree(host->ioarea);
Pete Popovba264b32005-09-21 06:18:27 +00001167
Manuel Laussc4223c22008-06-09 08:36:13 +02001168 mmc_free_host(mmc);
Pete Popovba264b32005-09-21 06:18:27 +00001169 }
Pete Popovba264b32005-09-21 06:18:27 +00001170 return 0;
1171}
1172
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001173static struct platform_driver au1xmmc_driver = {
Pete Popovba264b32005-09-21 06:18:27 +00001174 .probe = au1xmmc_probe,
1175 .remove = au1xmmc_remove,
1176 .suspend = NULL,
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001177 .resume = NULL,
1178 .driver = {
1179 .name = DRIVER_NAME,
Kay Sieversbc65c722008-04-15 14:34:28 -07001180 .owner = THIS_MODULE,
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001181 },
Pete Popovba264b32005-09-21 06:18:27 +00001182};
1183
1184static int __init au1xmmc_init(void)
1185{
Manuel Laussc4223c22008-06-09 08:36:13 +02001186#ifdef CONFIG_SOC_AU1200
1187 /* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride
1188 * of 8 bits. And since devices are shared, we need to create
1189 * our own to avoid freaking out other devices.
1190 */
1191 memid = au1xxx_ddma_add_device(&au1xmmc_mem_dbdev);
1192 if (!memid)
1193 printk(KERN_ERR "au1xmmc: cannot add memory dbdma dev\n");
1194#endif
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001195 return platform_driver_register(&au1xmmc_driver);
Pete Popovba264b32005-09-21 06:18:27 +00001196}
1197
1198static void __exit au1xmmc_exit(void)
1199{
Manuel Laussc4223c22008-06-09 08:36:13 +02001200#ifdef CONFIG_SOC_AU1200
1201 if (memid)
1202 au1xxx_ddma_del_device(memid);
1203#endif
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001204 platform_driver_unregister(&au1xmmc_driver);
Pete Popovba264b32005-09-21 06:18:27 +00001205}
1206
1207module_init(au1xmmc_init);
1208module_exit(au1xmmc_exit);
1209
Pete Popovba264b32005-09-21 06:18:27 +00001210MODULE_AUTHOR("Advanced Micro Devices, Inc");
1211MODULE_DESCRIPTION("MMC/SD driver for the Alchemy Au1XXX");
1212MODULE_LICENSE("GPL");
Kay Sieversbc65c722008-04-15 14:34:28 -07001213MODULE_ALIAS("platform:au1xxx-mmc");