blob: d3f55615c0990e46176a0afd49ed0c55977833ef [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
Manuel Lausse2d26472008-06-27 18:25:18 +020024/* Why don't we use the SD controllers' carddetect feature?
Pete Popovba264b32005-09-21 06:18:27 +000025 *
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).
Pete Popovba264b32005-09-21 06:18:27 +000033 */
34
Pete Popovba264b32005-09-21 06:18:27 +000035#include <linux/module.h>
36#include <linux/init.h>
Martin Michlmayrb256f9d2006-03-04 23:01:13 +000037#include <linux/platform_device.h>
Pete Popovba264b32005-09-21 06:18:27 +000038#include <linux/mm.h>
39#include <linux/interrupt.h>
40#include <linux/dma-mapping.h>
Al Viro0ada7a02007-10-27 19:40:46 +010041#include <linux/scatterlist.h>
Manuel Laussc4223c22008-06-09 08:36:13 +020042#include <linux/leds.h>
Pete Popovba264b32005-09-21 06:18:27 +000043#include <linux/mmc/host.h>
Manuel Laussc4223c22008-06-09 08:36:13 +020044
Pete Popovba264b32005-09-21 06:18:27 +000045#include <asm/io.h>
46#include <asm/mach-au1x00/au1000.h>
47#include <asm/mach-au1x00/au1xxx_dbdma.h>
48#include <asm/mach-au1x00/au1100_mmc.h>
Pete Popovba264b32005-09-21 06:18:27 +000049
Pete Popovba264b32005-09-21 06:18:27 +000050#define DRIVER_NAME "au1xxx-mmc"
51
52/* Set this to enable special debugging macros */
Manuel Laussc4223c22008-06-09 08:36:13 +020053/* #define DEBUG */
Pete Popovba264b32005-09-21 06:18:27 +000054
Russell Kingc6563172006-03-29 09:30:20 +010055#ifdef DEBUG
Manuel Lauss5c0a8892008-06-09 08:38:35 +020056#define DBG(fmt, idx, args...) \
57 printk(KERN_DEBUG "au1xmmc(%d): DEBUG: " fmt, idx, ##args)
Pete Popovba264b32005-09-21 06:18:27 +000058#else
Manuel Lauss5c0a8892008-06-09 08:38:35 +020059#define DBG(fmt, idx, args...) do {} while (0)
Pete Popovba264b32005-09-21 06:18:27 +000060#endif
61
Manuel Lauss5c0a8892008-06-09 08:38:35 +020062/* Hardware definitions */
63#define AU1XMMC_DESCRIPTOR_COUNT 1
Manuel Lausse491d232008-07-29 10:10:49 +020064
65/* max DMA seg size: 64KB on Au1100, 4MB on Au1200 */
66#ifdef CONFIG_SOC_AU1100
67#define AU1XMMC_DESCRIPTOR_SIZE 0x0000ffff
68#else /* Au1200 */
69#define AU1XMMC_DESCRIPTOR_SIZE 0x003fffff
70#endif
Manuel Lauss5c0a8892008-06-09 08:38:35 +020071
72#define AU1XMMC_OCR (MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 | \
73 MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 | \
74 MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36)
75
76/* This gives us a hard value for the stop command that we can write directly
77 * to the command register.
78 */
79#define STOP_CMD \
80 (SD_CMD_RT_1B | SD_CMD_CT_7 | (0xC << SD_CMD_CI_SHIFT) | SD_CMD_GO)
81
82/* This is the set of interrupts that we configure by default. */
83#define AU1XMMC_INTERRUPTS \
84 (SD_CONFIG_SC | SD_CONFIG_DT | SD_CONFIG_RAT | \
85 SD_CONFIG_CR | SD_CONFIG_I)
86
87/* The poll event (looking for insert/remove events runs twice a second. */
88#define AU1XMMC_DETECT_TIMEOUT (HZ/2)
89
90struct au1xmmc_host {
91 struct mmc_host *mmc;
92 struct mmc_request *mrq;
93
94 u32 flags;
95 u32 iobase;
96 u32 clock;
97 u32 bus_width;
98 u32 power_mode;
99
100 int status;
101
102 struct {
103 int len;
104 int dir;
105 } dma;
106
107 struct {
108 int index;
109 int offset;
110 int len;
111 } pio;
112
113 u32 tx_chan;
114 u32 rx_chan;
115
116 int irq;
117
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200118 struct tasklet_struct finish_task;
119 struct tasklet_struct data_task;
120 struct au1xmmc_platform_data *platdata;
121 struct platform_device *pdev;
122 struct resource *ioarea;
123};
124
125/* Status flags used by the host structure */
126#define HOST_F_XMIT 0x0001
127#define HOST_F_RECV 0x0002
128#define HOST_F_DMA 0x0010
129#define HOST_F_ACTIVE 0x0100
130#define HOST_F_STOP 0x1000
131
132#define HOST_S_IDLE 0x0001
133#define HOST_S_CMD 0x0002
134#define HOST_S_DATA 0x0003
135#define HOST_S_STOP 0x0004
136
137/* Easy access macros */
138#define HOST_STATUS(h) ((h)->iobase + SD_STATUS)
139#define HOST_CONFIG(h) ((h)->iobase + SD_CONFIG)
140#define HOST_ENABLE(h) ((h)->iobase + SD_ENABLE)
141#define HOST_TXPORT(h) ((h)->iobase + SD_TXPORT)
142#define HOST_RXPORT(h) ((h)->iobase + SD_RXPORT)
143#define HOST_CMDARG(h) ((h)->iobase + SD_CMDARG)
144#define HOST_BLKSIZE(h) ((h)->iobase + SD_BLKSIZE)
145#define HOST_CMD(h) ((h)->iobase + SD_CMD)
146#define HOST_CONFIG2(h) ((h)->iobase + SD_CONFIG2)
147#define HOST_TIMEOUT(h) ((h)->iobase + SD_TIMEOUT)
148#define HOST_DEBUG(h) ((h)->iobase + SD_DEBUG)
149
150#define DMA_CHANNEL(h) \
151 (((h)->flags & HOST_F_XMIT) ? (h)->tx_chan : (h)->rx_chan)
152
Pete Popovba264b32005-09-21 06:18:27 +0000153static inline void IRQ_ON(struct au1xmmc_host *host, u32 mask)
154{
155 u32 val = au_readl(HOST_CONFIG(host));
156 val |= mask;
157 au_writel(val, HOST_CONFIG(host));
158 au_sync();
159}
160
161static inline void FLUSH_FIFO(struct au1xmmc_host *host)
162{
163 u32 val = au_readl(HOST_CONFIG2(host));
164
165 au_writel(val | SD_CONFIG2_FF, HOST_CONFIG2(host));
166 au_sync_delay(1);
167
168 /* SEND_STOP will turn off clock control - this re-enables it */
169 val &= ~SD_CONFIG2_DF;
170
171 au_writel(val, HOST_CONFIG2(host));
172 au_sync();
173}
174
175static inline void IRQ_OFF(struct au1xmmc_host *host, u32 mask)
176{
177 u32 val = au_readl(HOST_CONFIG(host));
178 val &= ~mask;
179 au_writel(val, HOST_CONFIG(host));
180 au_sync();
181}
182
183static inline void SEND_STOP(struct au1xmmc_host *host)
184{
Manuel Lauss281dd232008-06-09 08:37:33 +0200185 u32 config2;
Pete Popovba264b32005-09-21 06:18:27 +0000186
187 WARN_ON(host->status != HOST_S_DATA);
188 host->status = HOST_S_STOP;
189
Manuel Lauss281dd232008-06-09 08:37:33 +0200190 config2 = au_readl(HOST_CONFIG2(host));
191 au_writel(config2 | SD_CONFIG2_DF, HOST_CONFIG2(host));
Pete Popovba264b32005-09-21 06:18:27 +0000192 au_sync();
193
194 /* Send the stop commmand */
195 au_writel(STOP_CMD, HOST_CMD(host));
196}
197
198static void au1xmmc_set_power(struct au1xmmc_host *host, int state)
199{
Manuel Laussc4223c22008-06-09 08:36:13 +0200200 if (host->platdata && host->platdata->set_power)
201 host->platdata->set_power(host->mmc, state);
Pete Popovba264b32005-09-21 06:18:27 +0000202}
203
Manuel Lausse2d26472008-06-27 18:25:18 +0200204static int au1xmmc_card_inserted(struct mmc_host *mmc)
Pete Popovba264b32005-09-21 06:18:27 +0000205{
Manuel Lausse2d26472008-06-27 18:25:18 +0200206 struct au1xmmc_host *host = mmc_priv(mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200207
208 if (host->platdata && host->platdata->card_inserted)
Manuel Lausse2d26472008-06-27 18:25:18 +0200209 return !!host->platdata->card_inserted(host->mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200210
Manuel Lausse2d26472008-06-27 18:25:18 +0200211 return -ENOSYS;
Pete Popovba264b32005-09-21 06:18:27 +0000212}
213
Manuel Lauss82999772007-01-25 10:29:24 +0100214static int au1xmmc_card_readonly(struct mmc_host *mmc)
Pete Popovba264b32005-09-21 06:18:27 +0000215{
Manuel Lauss82999772007-01-25 10:29:24 +0100216 struct au1xmmc_host *host = mmc_priv(mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200217
218 if (host->platdata && host->platdata->card_readonly)
Manuel Lausse2d26472008-06-27 18:25:18 +0200219 return !!host->platdata->card_readonly(mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200220
Manuel Lausse2d26472008-06-27 18:25:18 +0200221 return -ENOSYS;
Pete Popovba264b32005-09-21 06:18:27 +0000222}
223
224static void au1xmmc_finish_request(struct au1xmmc_host *host)
225{
Pete Popovba264b32005-09-21 06:18:27 +0000226 struct mmc_request *mrq = host->mrq;
227
228 host->mrq = NULL;
Manuel Laussc4223c22008-06-09 08:36:13 +0200229 host->flags &= HOST_F_ACTIVE | HOST_F_DMA;
Pete Popovba264b32005-09-21 06:18:27 +0000230
231 host->dma.len = 0;
232 host->dma.dir = 0;
233
234 host->pio.index = 0;
235 host->pio.offset = 0;
236 host->pio.len = 0;
237
238 host->status = HOST_S_IDLE;
239
Pete Popovba264b32005-09-21 06:18:27 +0000240 mmc_request_done(host->mmc, mrq);
241}
242
243static void au1xmmc_tasklet_finish(unsigned long param)
244{
245 struct au1xmmc_host *host = (struct au1xmmc_host *) param;
246 au1xmmc_finish_request(host);
247}
248
249static int au1xmmc_send_command(struct au1xmmc_host *host, int wait,
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200250 struct mmc_command *cmd, struct mmc_data *data)
Pete Popovba264b32005-09-21 06:18:27 +0000251{
Pete Popovba264b32005-09-21 06:18:27 +0000252 u32 mmccmd = (cmd->opcode << SD_CMD_CI_SHIFT);
253
Martin Michlmayre142c242006-03-04 23:01:39 +0000254 switch (mmc_resp_type(cmd)) {
Manuel Lauss279bc442007-01-25 10:27:41 +0100255 case MMC_RSP_NONE:
256 break;
Pete Popovba264b32005-09-21 06:18:27 +0000257 case MMC_RSP_R1:
258 mmccmd |= SD_CMD_RT_1;
259 break;
260 case MMC_RSP_R1B:
261 mmccmd |= SD_CMD_RT_1B;
262 break;
263 case MMC_RSP_R2:
264 mmccmd |= SD_CMD_RT_2;
265 break;
266 case MMC_RSP_R3:
267 mmccmd |= SD_CMD_RT_3;
268 break;
Manuel Lauss279bc442007-01-25 10:27:41 +0100269 default:
270 printk(KERN_INFO "au1xmmc: unhandled response type %02x\n",
271 mmc_resp_type(cmd));
Pierre Ossman17b04292007-07-22 22:18:46 +0200272 return -EINVAL;
Pete Popovba264b32005-09-21 06:18:27 +0000273 }
274
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200275 if (data) {
Pierre Ossman6356a9d2007-10-22 18:16:16 +0200276 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200277 if (data->blocks > 1)
278 mmccmd |= SD_CMD_CT_4;
279 else
280 mmccmd |= SD_CMD_CT_2;
Pierre Ossman6356a9d2007-10-22 18:16:16 +0200281 } else if (data->flags & MMC_DATA_WRITE) {
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200282 if (data->blocks > 1)
283 mmccmd |= SD_CMD_CT_3;
284 else
285 mmccmd |= SD_CMD_CT_1;
286 }
Pete Popovba264b32005-09-21 06:18:27 +0000287 }
288
289 au_writel(cmd->arg, HOST_CMDARG(host));
290 au_sync();
291
292 if (wait)
293 IRQ_OFF(host, SD_CONFIG_CR);
294
295 au_writel((mmccmd | SD_CMD_GO), HOST_CMD(host));
296 au_sync();
297
298 /* Wait for the command to go on the line */
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200299 while (au_readl(HOST_CMD(host)) & SD_CMD_GO)
300 /* nop */;
Pete Popovba264b32005-09-21 06:18:27 +0000301
302 /* Wait for the command to come back */
Pete Popovba264b32005-09-21 06:18:27 +0000303 if (wait) {
304 u32 status = au_readl(HOST_STATUS(host));
305
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200306 while (!(status & SD_STATUS_CR))
Pete Popovba264b32005-09-21 06:18:27 +0000307 status = au_readl(HOST_STATUS(host));
308
309 /* Clear the CR status */
310 au_writel(SD_STATUS_CR, HOST_STATUS(host));
311
312 IRQ_ON(host, SD_CONFIG_CR);
313 }
314
Pierre Ossman17b04292007-07-22 22:18:46 +0200315 return 0;
Pete Popovba264b32005-09-21 06:18:27 +0000316}
317
318static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
319{
Pete Popovba264b32005-09-21 06:18:27 +0000320 struct mmc_request *mrq = host->mrq;
321 struct mmc_data *data;
322 u32 crc;
323
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200324 WARN_ON((host->status != HOST_S_DATA) && (host->status != HOST_S_STOP));
Pete Popovba264b32005-09-21 06:18:27 +0000325
326 if (host->mrq == NULL)
327 return;
328
329 data = mrq->cmd->data;
330
331 if (status == 0)
332 status = au_readl(HOST_STATUS(host));
333
334 /* The transaction is really over when the SD_STATUS_DB bit is clear */
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200335 while ((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB))
Pete Popovba264b32005-09-21 06:18:27 +0000336 status = au_readl(HOST_STATUS(host));
337
Pierre Ossman17b04292007-07-22 22:18:46 +0200338 data->error = 0;
Pete Popovba264b32005-09-21 06:18:27 +0000339 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir);
340
341 /* Process any errors */
Pete Popovba264b32005-09-21 06:18:27 +0000342 crc = (status & (SD_STATUS_WC | SD_STATUS_RC));
343 if (host->flags & HOST_F_XMIT)
344 crc |= ((status & 0x07) == 0x02) ? 0 : 1;
345
346 if (crc)
Pierre Ossman17b04292007-07-22 22:18:46 +0200347 data->error = -EILSEQ;
Pete Popovba264b32005-09-21 06:18:27 +0000348
349 /* Clear the CRC bits */
350 au_writel(SD_STATUS_WC | SD_STATUS_RC, HOST_STATUS(host));
351
352 data->bytes_xfered = 0;
353
Pierre Ossman17b04292007-07-22 22:18:46 +0200354 if (!data->error) {
Pete Popovba264b32005-09-21 06:18:27 +0000355 if (host->flags & HOST_F_DMA) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200356#ifdef CONFIG_SOC_AU1200 /* DBDMA */
Pete Popovba264b32005-09-21 06:18:27 +0000357 u32 chan = DMA_CHANNEL(host);
358
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200359 chan_tab_t *c = *((chan_tab_t **)chan);
Pete Popovba264b32005-09-21 06:18:27 +0000360 au1x_dma_chan_t *cp = c->chan_ptr;
361 data->bytes_xfered = cp->ddma_bytecnt;
Manuel Laussc4223c22008-06-09 08:36:13 +0200362#endif
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200363 } else
Pete Popovba264b32005-09-21 06:18:27 +0000364 data->bytes_xfered =
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200365 (data->blocks * data->blksz) - host->pio.len;
Pete Popovba264b32005-09-21 06:18:27 +0000366 }
367
368 au1xmmc_finish_request(host);
369}
370
371static void au1xmmc_tasklet_data(unsigned long param)
372{
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200373 struct au1xmmc_host *host = (struct au1xmmc_host *)param;
Pete Popovba264b32005-09-21 06:18:27 +0000374
375 u32 status = au_readl(HOST_STATUS(host));
376 au1xmmc_data_complete(host, status);
377}
378
379#define AU1XMMC_MAX_TRANSFER 8
380
381static void au1xmmc_send_pio(struct au1xmmc_host *host)
382{
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200383 struct mmc_data *data;
384 int sg_len, max, count;
385 unsigned char *sg_ptr, val;
386 u32 status;
Pete Popovba264b32005-09-21 06:18:27 +0000387 struct scatterlist *sg;
388
389 data = host->mrq->data;
390
391 if (!(host->flags & HOST_F_XMIT))
392 return;
393
394 /* This is the pointer to the data buffer */
395 sg = &data->sg[host->pio.index];
Jens Axboe45711f12007-10-22 21:19:53 +0200396 sg_ptr = sg_virt(sg) + host->pio.offset;
Pete Popovba264b32005-09-21 06:18:27 +0000397
398 /* This is the space left inside the buffer */
399 sg_len = data->sg[host->pio.index].length - host->pio.offset;
400
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200401 /* Check if we need less than the size of the sg_buffer */
Pete Popovba264b32005-09-21 06:18:27 +0000402 max = (sg_len > host->pio.len) ? host->pio.len : sg_len;
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200403 if (max > AU1XMMC_MAX_TRANSFER)
404 max = AU1XMMC_MAX_TRANSFER;
Pete Popovba264b32005-09-21 06:18:27 +0000405
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200406 for (count = 0; count < max; count++) {
Pete Popovba264b32005-09-21 06:18:27 +0000407 status = au_readl(HOST_STATUS(host));
408
409 if (!(status & SD_STATUS_TH))
410 break;
411
412 val = *sg_ptr++;
413
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200414 au_writel((unsigned long)val, HOST_TXPORT(host));
Pete Popovba264b32005-09-21 06:18:27 +0000415 au_sync();
416 }
417
418 host->pio.len -= count;
419 host->pio.offset += count;
420
421 if (count == sg_len) {
422 host->pio.index++;
423 host->pio.offset = 0;
424 }
425
426 if (host->pio.len == 0) {
427 IRQ_OFF(host, SD_CONFIG_TH);
428
429 if (host->flags & HOST_F_STOP)
430 SEND_STOP(host);
431
432 tasklet_schedule(&host->data_task);
433 }
434}
435
436static void au1xmmc_receive_pio(struct au1xmmc_host *host)
437{
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200438 struct mmc_data *data;
439 int max, count, sg_len = 0;
440 unsigned char *sg_ptr = NULL;
441 u32 status, val;
Pete Popovba264b32005-09-21 06:18:27 +0000442 struct scatterlist *sg;
443
444 data = host->mrq->data;
445
446 if (!(host->flags & HOST_F_RECV))
447 return;
448
449 max = host->pio.len;
450
451 if (host->pio.index < host->dma.len) {
452 sg = &data->sg[host->pio.index];
Jens Axboe45711f12007-10-22 21:19:53 +0200453 sg_ptr = sg_virt(sg) + host->pio.offset;
Pete Popovba264b32005-09-21 06:18:27 +0000454
455 /* This is the space left inside the buffer */
456 sg_len = sg_dma_len(&data->sg[host->pio.index]) - host->pio.offset;
457
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200458 /* Check if we need less than the size of the sg_buffer */
459 if (sg_len < max)
460 max = sg_len;
Pete Popovba264b32005-09-21 06:18:27 +0000461 }
462
463 if (max > AU1XMMC_MAX_TRANSFER)
464 max = AU1XMMC_MAX_TRANSFER;
465
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200466 for (count = 0; count < max; count++) {
Pete Popovba264b32005-09-21 06:18:27 +0000467 status = au_readl(HOST_STATUS(host));
468
469 if (!(status & SD_STATUS_NE))
470 break;
471
472 if (status & SD_STATUS_RC) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200473 DBG("RX CRC Error [%d + %d].\n", host->pdev->id,
Pete Popovba264b32005-09-21 06:18:27 +0000474 host->pio.len, count);
475 break;
476 }
477
478 if (status & SD_STATUS_RO) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200479 DBG("RX Overrun [%d + %d]\n", host->pdev->id,
Pete Popovba264b32005-09-21 06:18:27 +0000480 host->pio.len, count);
481 break;
482 }
483 else if (status & SD_STATUS_RU) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200484 DBG("RX Underrun [%d + %d]\n", host->pdev->id,
Pete Popovba264b32005-09-21 06:18:27 +0000485 host->pio.len, count);
486 break;
487 }
488
489 val = au_readl(HOST_RXPORT(host));
490
491 if (sg_ptr)
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200492 *sg_ptr++ = (unsigned char)(val & 0xFF);
Pete Popovba264b32005-09-21 06:18:27 +0000493 }
494
495 host->pio.len -= count;
496 host->pio.offset += count;
497
498 if (sg_len && count == sg_len) {
499 host->pio.index++;
500 host->pio.offset = 0;
501 }
502
503 if (host->pio.len == 0) {
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200504 /* IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF); */
Pete Popovba264b32005-09-21 06:18:27 +0000505 IRQ_OFF(host, SD_CONFIG_NE);
506
507 if (host->flags & HOST_F_STOP)
508 SEND_STOP(host);
509
510 tasklet_schedule(&host->data_task);
511 }
512}
513
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200514/* This is called when a command has been completed - grab the response
515 * and check for errors. Then start the data transfer if it is indicated.
516 */
Pete Popovba264b32005-09-21 06:18:27 +0000517static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
518{
Pete Popovba264b32005-09-21 06:18:27 +0000519 struct mmc_request *mrq = host->mrq;
520 struct mmc_command *cmd;
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200521 u32 r[4];
522 int i, trans;
Pete Popovba264b32005-09-21 06:18:27 +0000523
524 if (!host->mrq)
525 return;
526
527 cmd = mrq->cmd;
Pierre Ossman17b04292007-07-22 22:18:46 +0200528 cmd->error = 0;
Pete Popovba264b32005-09-21 06:18:27 +0000529
Russell Kinge9225172006-02-02 12:23:12 +0000530 if (cmd->flags & MMC_RSP_PRESENT) {
531 if (cmd->flags & MMC_RSP_136) {
Russell Kinge9225172006-02-02 12:23:12 +0000532 r[0] = au_readl(host->iobase + SD_RESP3);
533 r[1] = au_readl(host->iobase + SD_RESP2);
534 r[2] = au_readl(host->iobase + SD_RESP1);
535 r[3] = au_readl(host->iobase + SD_RESP0);
Pete Popovba264b32005-09-21 06:18:27 +0000536
Russell Kinge9225172006-02-02 12:23:12 +0000537 /* The CRC is omitted from the response, so really
538 * we only got 120 bytes, but the engine expects
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200539 * 128 bits, so we have to shift things up.
Russell Kinge9225172006-02-02 12:23:12 +0000540 */
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200541 for (i = 0; i < 4; i++) {
Russell Kinge9225172006-02-02 12:23:12 +0000542 cmd->resp[i] = (r[i] & 0x00FFFFFF) << 8;
543 if (i != 3)
544 cmd->resp[i] |= (r[i + 1] & 0xFF000000) >> 24;
545 }
546 } else {
547 /* Techincally, we should be getting all 48 bits of
548 * the response (SD_RESP1 + SD_RESP2), but because
549 * our response omits the CRC, our data ends up
550 * being shifted 8 bits to the right. In this case,
551 * that means that the OSR data starts at bit 31,
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200552 * so we can just read RESP0 and return that.
Russell Kinge9225172006-02-02 12:23:12 +0000553 */
554 cmd->resp[0] = au_readl(host->iobase + SD_RESP0);
Pete Popovba264b32005-09-21 06:18:27 +0000555 }
556 }
557
558 /* Figure out errors */
Pete Popovba264b32005-09-21 06:18:27 +0000559 if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC))
Pierre Ossman17b04292007-07-22 22:18:46 +0200560 cmd->error = -EILSEQ;
Pete Popovba264b32005-09-21 06:18:27 +0000561
562 trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);
563
Pierre Ossman17b04292007-07-22 22:18:46 +0200564 if (!trans || cmd->error) {
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200565 IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF);
Pete Popovba264b32005-09-21 06:18:27 +0000566 tasklet_schedule(&host->finish_task);
567 return;
568 }
569
570 host->status = HOST_S_DATA;
571
572 if (host->flags & HOST_F_DMA) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200573#ifdef CONFIG_SOC_AU1200 /* DBDMA */
Pete Popovba264b32005-09-21 06:18:27 +0000574 u32 channel = DMA_CHANNEL(host);
575
576 /* Start the DMA as soon as the buffer gets something in it */
577
578 if (host->flags & HOST_F_RECV) {
579 u32 mask = SD_STATUS_DB | SD_STATUS_NE;
580
581 while((status & mask) != mask)
582 status = au_readl(HOST_STATUS(host));
583 }
584
585 au1xxx_dbdma_start(channel);
Manuel Laussc4223c22008-06-09 08:36:13 +0200586#endif
Pete Popovba264b32005-09-21 06:18:27 +0000587 }
588}
589
590static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate)
591{
Pete Popovba264b32005-09-21 06:18:27 +0000592 unsigned int pbus = get_au1x00_speed();
593 unsigned int divisor;
594 u32 config;
595
596 /* From databook:
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200597 * divisor = ((((cpuclock / sbus_divisor) / 2) / mmcclock) / 2) - 1
598 */
Pete Popovba264b32005-09-21 06:18:27 +0000599 pbus /= ((au_readl(SYS_POWERCTRL) & 0x3) + 2);
600 pbus /= 2;
Pete Popovba264b32005-09-21 06:18:27 +0000601 divisor = ((pbus / rate) / 2) - 1;
602
603 config = au_readl(HOST_CONFIG(host));
604
605 config &= ~(SD_CONFIG_DIV);
606 config |= (divisor & SD_CONFIG_DIV) | SD_CONFIG_DE;
607
608 au_writel(config, HOST_CONFIG(host));
609 au_sync();
610}
611
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200612static int au1xmmc_prepare_data(struct au1xmmc_host *host,
613 struct mmc_data *data)
Pete Popovba264b32005-09-21 06:18:27 +0000614{
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100615 int datalen = data->blocks * data->blksz;
Pete Popovba264b32005-09-21 06:18:27 +0000616
Pete Popovba264b32005-09-21 06:18:27 +0000617 if (data->flags & MMC_DATA_READ)
618 host->flags |= HOST_F_RECV;
619 else
620 host->flags |= HOST_F_XMIT;
621
622 if (host->mrq->stop)
623 host->flags |= HOST_F_STOP;
624
625 host->dma.dir = DMA_BIDIRECTIONAL;
626
627 host->dma.len = dma_map_sg(mmc_dev(host->mmc), data->sg,
628 data->sg_len, host->dma.dir);
629
630 if (host->dma.len == 0)
Pierre Ossman17b04292007-07-22 22:18:46 +0200631 return -ETIMEDOUT;
Pete Popovba264b32005-09-21 06:18:27 +0000632
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100633 au_writel(data->blksz - 1, HOST_BLKSIZE(host));
Pete Popovba264b32005-09-21 06:18:27 +0000634
635 if (host->flags & HOST_F_DMA) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200636#ifdef CONFIG_SOC_AU1200 /* DBDMA */
Pete Popovba264b32005-09-21 06:18:27 +0000637 int i;
638 u32 channel = DMA_CHANNEL(host);
639
640 au1xxx_dbdma_stop(channel);
641
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200642 for (i = 0; i < host->dma.len; i++) {
Pete Popovba264b32005-09-21 06:18:27 +0000643 u32 ret = 0, flags = DDMA_FLAGS_NOIE;
644 struct scatterlist *sg = &data->sg[i];
645 int sg_len = sg->length;
646
647 int len = (datalen > sg_len) ? sg_len : datalen;
648
649 if (i == host->dma.len - 1)
650 flags = DDMA_FLAGS_IE;
651
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200652 if (host->flags & HOST_F_XMIT) {
653 ret = au1xxx_dbdma_put_source_flags(channel,
654 (void *)sg_virt(sg), len, flags);
655 } else {
656 ret = au1xxx_dbdma_put_dest_flags(channel,
657 (void *)sg_virt(sg), len, flags);
Pete Popovba264b32005-09-21 06:18:27 +0000658 }
659
Manuel Laussc4223c22008-06-09 08:36:13 +0200660 if (!ret)
Pete Popovba264b32005-09-21 06:18:27 +0000661 goto dataerr;
662
663 datalen -= len;
664 }
Manuel Laussc4223c22008-06-09 08:36:13 +0200665#endif
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200666 } else {
Pete Popovba264b32005-09-21 06:18:27 +0000667 host->pio.index = 0;
668 host->pio.offset = 0;
669 host->pio.len = datalen;
670
671 if (host->flags & HOST_F_XMIT)
672 IRQ_ON(host, SD_CONFIG_TH);
673 else
674 IRQ_ON(host, SD_CONFIG_NE);
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200675 /* IRQ_ON(host, SD_CONFIG_RA | SD_CONFIG_RF); */
Pete Popovba264b32005-09-21 06:18:27 +0000676 }
677
Pierre Ossman17b04292007-07-22 22:18:46 +0200678 return 0;
Pete Popovba264b32005-09-21 06:18:27 +0000679
Manuel Laussc4223c22008-06-09 08:36:13 +0200680dataerr:
681 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
682 host->dma.dir);
Pierre Ossman17b04292007-07-22 22:18:46 +0200683 return -ETIMEDOUT;
Pete Popovba264b32005-09-21 06:18:27 +0000684}
685
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200686/* This actually starts a command or data transaction */
Pete Popovba264b32005-09-21 06:18:27 +0000687static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
688{
Pete Popovba264b32005-09-21 06:18:27 +0000689 struct au1xmmc_host *host = mmc_priv(mmc);
Pierre Ossman17b04292007-07-22 22:18:46 +0200690 int ret = 0;
Pete Popovba264b32005-09-21 06:18:27 +0000691
692 WARN_ON(irqs_disabled());
693 WARN_ON(host->status != HOST_S_IDLE);
694
695 host->mrq = mrq;
696 host->status = HOST_S_CMD;
697
Manuel Lauss88b8d9a2008-06-09 08:39:11 +0200698 /* fail request immediately if no card is present */
Manuel Lausse2d26472008-06-27 18:25:18 +0200699 if (0 == au1xmmc_card_inserted(mmc)) {
Manuel Lauss88b8d9a2008-06-09 08:39:11 +0200700 mrq->cmd->error = -ENOMEDIUM;
701 au1xmmc_finish_request(host);
702 return;
703 }
704
Pete Popovba264b32005-09-21 06:18:27 +0000705 if (mrq->data) {
706 FLUSH_FIFO(host);
707 ret = au1xmmc_prepare_data(host, mrq->data);
708 }
709
Pierre Ossman17b04292007-07-22 22:18:46 +0200710 if (!ret)
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200711 ret = au1xmmc_send_command(host, 0, mrq->cmd, mrq->data);
Pete Popovba264b32005-09-21 06:18:27 +0000712
Pierre Ossman17b04292007-07-22 22:18:46 +0200713 if (ret) {
Pete Popovba264b32005-09-21 06:18:27 +0000714 mrq->cmd->error = ret;
715 au1xmmc_finish_request(host);
716 }
717}
718
719static void au1xmmc_reset_controller(struct au1xmmc_host *host)
720{
Pete Popovba264b32005-09-21 06:18:27 +0000721 /* Apply the clock */
722 au_writel(SD_ENABLE_CE, HOST_ENABLE(host));
723 au_sync_delay(1);
724
725 au_writel(SD_ENABLE_R | SD_ENABLE_CE, HOST_ENABLE(host));
726 au_sync_delay(5);
727
728 au_writel(~0, HOST_STATUS(host));
729 au_sync();
730
731 au_writel(0, HOST_BLKSIZE(host));
732 au_writel(0x001fffff, HOST_TIMEOUT(host));
733 au_sync();
734
735 au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
736 au_sync();
737
738 au_writel(SD_CONFIG2_EN | SD_CONFIG2_FF, HOST_CONFIG2(host));
739 au_sync_delay(1);
740
741 au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
742 au_sync();
743
744 /* Configure interrupts */
745 au_writel(AU1XMMC_INTERRUPTS, HOST_CONFIG(host));
746 au_sync();
747}
748
749
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200750static void au1xmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pete Popovba264b32005-09-21 06:18:27 +0000751{
752 struct au1xmmc_host *host = mmc_priv(mmc);
Manuel Lauss281dd232008-06-09 08:37:33 +0200753 u32 config2;
Pete Popovba264b32005-09-21 06:18:27 +0000754
Pete Popovba264b32005-09-21 06:18:27 +0000755 if (ios->power_mode == MMC_POWER_OFF)
756 au1xmmc_set_power(host, 0);
757 else if (ios->power_mode == MMC_POWER_ON) {
758 au1xmmc_set_power(host, 1);
759 }
760
761 if (ios->clock && ios->clock != host->clock) {
762 au1xmmc_set_clock(host, ios->clock);
763 host->clock = ios->clock;
764 }
Manuel Lauss281dd232008-06-09 08:37:33 +0200765
766 config2 = au_readl(HOST_CONFIG2(host));
767 switch (ios->bus_width) {
768 case MMC_BUS_WIDTH_4:
769 config2 |= SD_CONFIG2_WB;
770 break;
771 case MMC_BUS_WIDTH_1:
772 config2 &= ~SD_CONFIG2_WB;
773 break;
774 }
775 au_writel(config2, HOST_CONFIG2(host));
776 au_sync();
Pete Popovba264b32005-09-21 06:18:27 +0000777}
778
Manuel Laussc4223c22008-06-09 08:36:13 +0200779#define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
780#define STATUS_DATA_IN (SD_STATUS_NE)
781#define STATUS_DATA_OUT (SD_STATUS_TH)
782
783static irqreturn_t au1xmmc_irq(int irq, void *dev_id)
Pete Popovba264b32005-09-21 06:18:27 +0000784{
Manuel Laussc4223c22008-06-09 08:36:13 +0200785 struct au1xmmc_host *host = dev_id;
786 u32 status;
787
788 status = au_readl(HOST_STATUS(host));
789
790 if (!(status & SD_STATUS_I))
791 return IRQ_NONE; /* not ours */
792
Manuel Lauss20f522f2008-06-09 08:38:03 +0200793 if (status & SD_STATUS_SI) /* SDIO */
794 mmc_signal_sdio_irq(host->mmc);
795
Manuel Laussc4223c22008-06-09 08:36:13 +0200796 if (host->mrq && (status & STATUS_TIMEOUT)) {
797 if (status & SD_STATUS_RAT)
798 host->mrq->cmd->error = -ETIMEDOUT;
799 else if (status & SD_STATUS_DT)
800 host->mrq->data->error = -ETIMEDOUT;
801
802 /* In PIO mode, interrupts might still be enabled */
803 IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
804
805 /* IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF); */
806 tasklet_schedule(&host->finish_task);
807 }
808#if 0
809 else if (status & SD_STATUS_DD) {
810 /* Sometimes we get a DD before a NE in PIO mode */
811 if (!(host->flags & HOST_F_DMA) && (status & SD_STATUS_NE))
812 au1xmmc_receive_pio(host);
813 else {
814 au1xmmc_data_complete(host, status);
815 /* tasklet_schedule(&host->data_task); */
816 }
817 }
818#endif
819 else if (status & SD_STATUS_CR) {
820 if (host->status == HOST_S_CMD)
821 au1xmmc_cmd_complete(host, status);
822
823 } else if (!(host->flags & HOST_F_DMA)) {
824 if ((host->flags & HOST_F_XMIT) && (status & STATUS_DATA_OUT))
825 au1xmmc_send_pio(host);
826 else if ((host->flags & HOST_F_RECV) && (status & STATUS_DATA_IN))
827 au1xmmc_receive_pio(host);
828
829 } else if (status & 0x203F3C70) {
830 DBG("Unhandled status %8.8x\n", host->pdev->id,
831 status);
832 }
833
834 au_writel(status, HOST_STATUS(host));
835 au_sync();
836
837 return IRQ_HANDLED;
838}
839
840#ifdef CONFIG_SOC_AU1200
841/* 8bit memory DMA device */
842static dbdev_tab_t au1xmmc_mem_dbdev = {
843 .dev_id = DSCR_CMD0_ALWAYS,
844 .dev_flags = DEV_FLAGS_ANYUSE,
845 .dev_tsize = 0,
846 .dev_devwidth = 8,
847 .dev_physaddr = 0x00000000,
848 .dev_intlevel = 0,
849 .dev_intpolarity = 0,
850};
851static int memid;
852
853static void au1xmmc_dbdma_callback(int irq, void *dev_id)
854{
855 struct au1xmmc_host *host = (struct au1xmmc_host *)dev_id;
Pete Popovba264b32005-09-21 06:18:27 +0000856
857 /* Avoid spurious interrupts */
Pete Popovba264b32005-09-21 06:18:27 +0000858 if (!host->mrq)
859 return;
860
861 if (host->flags & HOST_F_STOP)
862 SEND_STOP(host);
863
864 tasklet_schedule(&host->data_task);
865}
866
Manuel Laussc4223c22008-06-09 08:36:13 +0200867static int au1xmmc_dbdma_init(struct au1xmmc_host *host)
Pete Popovba264b32005-09-21 06:18:27 +0000868{
Manuel Laussc4223c22008-06-09 08:36:13 +0200869 struct resource *res;
870 int txid, rxid;
Pete Popovba264b32005-09-21 06:18:27 +0000871
Manuel Laussc4223c22008-06-09 08:36:13 +0200872 res = platform_get_resource(host->pdev, IORESOURCE_DMA, 0);
873 if (!res)
874 return -ENODEV;
875 txid = res->start;
Pete Popovba264b32005-09-21 06:18:27 +0000876
Manuel Laussc4223c22008-06-09 08:36:13 +0200877 res = platform_get_resource(host->pdev, IORESOURCE_DMA, 1);
878 if (!res)
879 return -ENODEV;
880 rxid = res->start;
Pete Popovba264b32005-09-21 06:18:27 +0000881
Manuel Laussc4223c22008-06-09 08:36:13 +0200882 if (!memid)
883 return -ENODEV;
Pete Popovba264b32005-09-21 06:18:27 +0000884
Manuel Laussc4223c22008-06-09 08:36:13 +0200885 host->tx_chan = au1xxx_dbdma_chan_alloc(memid, txid,
886 au1xmmc_dbdma_callback, (void *)host);
887 if (!host->tx_chan) {
888 dev_err(&host->pdev->dev, "cannot allocate TX DMA\n");
889 return -ENODEV;
890 }
Pete Popovba264b32005-09-21 06:18:27 +0000891
Manuel Laussc4223c22008-06-09 08:36:13 +0200892 host->rx_chan = au1xxx_dbdma_chan_alloc(rxid, memid,
893 au1xmmc_dbdma_callback, (void *)host);
894 if (!host->rx_chan) {
895 dev_err(&host->pdev->dev, "cannot allocate RX DMA\n");
896 au1xxx_dbdma_chan_free(host->tx_chan);
897 return -ENODEV;
898 }
Pete Popovba264b32005-09-21 06:18:27 +0000899
Manuel Laussc4223c22008-06-09 08:36:13 +0200900 au1xxx_dbdma_set_devwidth(host->tx_chan, 8);
901 au1xxx_dbdma_set_devwidth(host->rx_chan, 8);
Pete Popovba264b32005-09-21 06:18:27 +0000902
Manuel Laussc4223c22008-06-09 08:36:13 +0200903 au1xxx_dbdma_ring_alloc(host->tx_chan, AU1XMMC_DESCRIPTOR_COUNT);
904 au1xxx_dbdma_ring_alloc(host->rx_chan, AU1XMMC_DESCRIPTOR_COUNT);
Pete Popovba264b32005-09-21 06:18:27 +0000905
Manuel Laussc4223c22008-06-09 08:36:13 +0200906 /* DBDMA is good to go */
907 host->flags |= HOST_F_DMA;
Pete Popovba264b32005-09-21 06:18:27 +0000908
Manuel Laussc4223c22008-06-09 08:36:13 +0200909 return 0;
910}
Pete Popovba264b32005-09-21 06:18:27 +0000911
Manuel Laussc4223c22008-06-09 08:36:13 +0200912static void au1xmmc_dbdma_shutdown(struct au1xmmc_host *host)
913{
914 if (host->flags & HOST_F_DMA) {
915 host->flags &= ~HOST_F_DMA;
916 au1xxx_dbdma_chan_free(host->tx_chan);
917 au1xxx_dbdma_chan_free(host->rx_chan);
918 }
919}
Pete Popovba264b32005-09-21 06:18:27 +0000920#endif
Pete Popovba264b32005-09-21 06:18:27 +0000921
Manuel Lauss20f522f2008-06-09 08:38:03 +0200922static void au1xmmc_enable_sdio_irq(struct mmc_host *mmc, int en)
923{
924 struct au1xmmc_host *host = mmc_priv(mmc);
925
926 if (en)
927 IRQ_ON(host, SD_CONFIG_SI);
928 else
929 IRQ_OFF(host, SD_CONFIG_SI);
930}
931
Yoichi Yuasabf8c80a2006-12-05 07:43:38 +0100932static const struct mmc_host_ops au1xmmc_ops = {
Pete Popovba264b32005-09-21 06:18:27 +0000933 .request = au1xmmc_request,
934 .set_ios = au1xmmc_set_ios,
Manuel Lauss82999772007-01-25 10:29:24 +0100935 .get_ro = au1xmmc_card_readonly,
Manuel Lausse2d26472008-06-27 18:25:18 +0200936 .get_cd = au1xmmc_card_inserted,
Manuel Lauss20f522f2008-06-09 08:38:03 +0200937 .enable_sdio_irq = au1xmmc_enable_sdio_irq,
Pete Popovba264b32005-09-21 06:18:27 +0000938};
939
Martin Michlmayrb256f9d2006-03-04 23:01:13 +0000940static int __devinit au1xmmc_probe(struct platform_device *pdev)
Pete Popovba264b32005-09-21 06:18:27 +0000941{
Manuel Laussc4223c22008-06-09 08:36:13 +0200942 struct mmc_host *mmc;
943 struct au1xmmc_host *host;
944 struct resource *r;
945 int ret;
Pete Popovba264b32005-09-21 06:18:27 +0000946
Manuel Laussc4223c22008-06-09 08:36:13 +0200947 mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), &pdev->dev);
948 if (!mmc) {
949 dev_err(&pdev->dev, "no memory for mmc_host\n");
950 ret = -ENOMEM;
951 goto out0;
Pete Popovba264b32005-09-21 06:18:27 +0000952 }
953
Manuel Laussc4223c22008-06-09 08:36:13 +0200954 host = mmc_priv(mmc);
955 host->mmc = mmc;
956 host->platdata = pdev->dev.platform_data;
957 host->pdev = pdev;
Pete Popovba264b32005-09-21 06:18:27 +0000958
Manuel Laussc4223c22008-06-09 08:36:13 +0200959 ret = -ENODEV;
960 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
961 if (!r) {
962 dev_err(&pdev->dev, "no mmio defined\n");
963 goto out1;
964 }
Pete Popovba264b32005-09-21 06:18:27 +0000965
Manuel Laussc4223c22008-06-09 08:36:13 +0200966 host->ioarea = request_mem_region(r->start, r->end - r->start + 1,
967 pdev->name);
968 if (!host->ioarea) {
969 dev_err(&pdev->dev, "mmio already in use\n");
970 goto out1;
971 }
972
973 host->iobase = (unsigned long)ioremap(r->start, 0x3c);
974 if (!host->iobase) {
975 dev_err(&pdev->dev, "cannot remap mmio\n");
976 goto out2;
977 }
978
979 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
980 if (!r) {
981 dev_err(&pdev->dev, "no IRQ defined\n");
982 goto out3;
983 }
984
985 host->irq = r->start;
986 /* IRQ is shared among both SD controllers */
987 ret = request_irq(host->irq, au1xmmc_irq, IRQF_SHARED,
988 DRIVER_NAME, host);
989 if (ret) {
990 dev_err(&pdev->dev, "cannot grab IRQ\n");
991 goto out3;
992 }
993
994 mmc->ops = &au1xmmc_ops;
995
996 mmc->f_min = 450000;
997 mmc->f_max = 24000000;
998
999 mmc->max_seg_size = AU1XMMC_DESCRIPTOR_SIZE;
1000 mmc->max_phys_segs = AU1XMMC_DESCRIPTOR_COUNT;
1001
1002 mmc->max_blk_size = 2048;
1003 mmc->max_blk_count = 512;
1004
1005 mmc->ocr_avail = AU1XMMC_OCR;
Manuel Lauss20f522f2008-06-09 08:38:03 +02001006 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
Manuel Laussc4223c22008-06-09 08:36:13 +02001007
1008 host->status = HOST_S_IDLE;
1009
1010 /* board-specific carddetect setup, if any */
1011 if (host->platdata && host->platdata->cd_setup) {
1012 ret = host->platdata->cd_setup(mmc, 1);
1013 if (ret) {
Manuel Lausse2d26472008-06-27 18:25:18 +02001014 dev_warn(&pdev->dev, "board CD setup failed\n");
1015 mmc->caps |= MMC_CAP_NEEDS_POLL;
Pete Popovba264b32005-09-21 06:18:27 +00001016 }
Manuel Lausse2d26472008-06-27 18:25:18 +02001017 } else
1018 mmc->caps |= MMC_CAP_NEEDS_POLL;
Pete Popovba264b32005-09-21 06:18:27 +00001019
Manuel Laussc4223c22008-06-09 08:36:13 +02001020 tasklet_init(&host->data_task, au1xmmc_tasklet_data,
1021 (unsigned long)host);
Pete Popovba264b32005-09-21 06:18:27 +00001022
Manuel Laussc4223c22008-06-09 08:36:13 +02001023 tasklet_init(&host->finish_task, au1xmmc_tasklet_finish,
1024 (unsigned long)host);
Pete Popovba264b32005-09-21 06:18:27 +00001025
Manuel Laussc4223c22008-06-09 08:36:13 +02001026#ifdef CONFIG_SOC_AU1200
1027 ret = au1xmmc_dbdma_init(host);
1028 if (ret)
1029 printk(KERN_INFO DRIVER_NAME ": DBDMA init failed; using PIO\n");
1030#endif
Pete Popovba264b32005-09-21 06:18:27 +00001031
Manuel Laussc4223c22008-06-09 08:36:13 +02001032#ifdef CONFIG_LEDS_CLASS
1033 if (host->platdata && host->platdata->led) {
1034 struct led_classdev *led = host->platdata->led;
1035 led->name = mmc_hostname(mmc);
1036 led->brightness = LED_OFF;
1037 led->default_trigger = mmc_hostname(mmc);
1038 ret = led_classdev_register(mmc_dev(mmc), led);
1039 if (ret)
1040 goto out5;
1041 }
1042#endif
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001043
Manuel Laussc4223c22008-06-09 08:36:13 +02001044 au1xmmc_reset_controller(host);
Pete Popovba264b32005-09-21 06:18:27 +00001045
Manuel Laussc4223c22008-06-09 08:36:13 +02001046 ret = mmc_add_host(mmc);
1047 if (ret) {
1048 dev_err(&pdev->dev, "cannot add mmc host\n");
1049 goto out6;
1050 }
Pete Popovba264b32005-09-21 06:18:27 +00001051
Manuel Laussdd8572a2008-07-17 13:07:28 +02001052 platform_set_drvdata(pdev, host);
Pete Popovba264b32005-09-21 06:18:27 +00001053
Manuel Laussc4223c22008-06-09 08:36:13 +02001054 printk(KERN_INFO DRIVER_NAME ": MMC Controller %d set up at %8.8X"
1055 " (mode=%s)\n", pdev->id, host->iobase,
1056 host->flags & HOST_F_DMA ? "dma" : "pio");
Pete Popovba264b32005-09-21 06:18:27 +00001057
Manuel Laussc4223c22008-06-09 08:36:13 +02001058 return 0; /* all ok */
Pete Popovba264b32005-09-21 06:18:27 +00001059
Manuel Laussc4223c22008-06-09 08:36:13 +02001060out6:
1061#ifdef CONFIG_LEDS_CLASS
1062 if (host->platdata && host->platdata->led)
1063 led_classdev_unregister(host->platdata->led);
1064out5:
1065#endif
1066 au_writel(0, HOST_ENABLE(host));
1067 au_writel(0, HOST_CONFIG(host));
1068 au_writel(0, HOST_CONFIG2(host));
1069 au_sync();
1070
1071#ifdef CONFIG_SOC_AU1200
1072 au1xmmc_dbdma_shutdown(host);
1073#endif
1074
1075 tasklet_kill(&host->data_task);
1076 tasklet_kill(&host->finish_task);
1077
Manuel Lausse2d26472008-06-27 18:25:18 +02001078 if (host->platdata && host->platdata->cd_setup &&
1079 !(mmc->caps & MMC_CAP_NEEDS_POLL))
Manuel Laussc4223c22008-06-09 08:36:13 +02001080 host->platdata->cd_setup(mmc, 0);
Manuel Lausse2d26472008-06-27 18:25:18 +02001081
Manuel Laussc4223c22008-06-09 08:36:13 +02001082 free_irq(host->irq, host);
1083out3:
1084 iounmap((void *)host->iobase);
1085out2:
1086 release_resource(host->ioarea);
1087 kfree(host->ioarea);
1088out1:
1089 mmc_free_host(mmc);
1090out0:
1091 return ret;
Pete Popovba264b32005-09-21 06:18:27 +00001092}
1093
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001094static int __devexit au1xmmc_remove(struct platform_device *pdev)
Pete Popovba264b32005-09-21 06:18:27 +00001095{
Manuel Laussdd8572a2008-07-17 13:07:28 +02001096 struct au1xmmc_host *host = platform_get_drvdata(pdev);
Pete Popovba264b32005-09-21 06:18:27 +00001097
Manuel Laussdd8572a2008-07-17 13:07:28 +02001098 if (host) {
1099 mmc_remove_host(host->mmc);
Pete Popovba264b32005-09-21 06:18:27 +00001100
Manuel Laussc4223c22008-06-09 08:36:13 +02001101#ifdef CONFIG_LEDS_CLASS
1102 if (host->platdata && host->platdata->led)
1103 led_classdev_unregister(host->platdata->led);
1104#endif
1105
Manuel Lausse2d26472008-06-27 18:25:18 +02001106 if (host->platdata && host->platdata->cd_setup &&
Manuel Laussdd8572a2008-07-17 13:07:28 +02001107 !(host->mmc->caps & MMC_CAP_NEEDS_POLL))
1108 host->platdata->cd_setup(host->mmc, 0);
Manuel Laussc4223c22008-06-09 08:36:13 +02001109
1110 au_writel(0, HOST_ENABLE(host));
1111 au_writel(0, HOST_CONFIG(host));
1112 au_writel(0, HOST_CONFIG2(host));
1113 au_sync();
Pete Popovba264b32005-09-21 06:18:27 +00001114
1115 tasklet_kill(&host->data_task);
1116 tasklet_kill(&host->finish_task);
1117
Manuel Laussc4223c22008-06-09 08:36:13 +02001118#ifdef CONFIG_SOC_AU1200
1119 au1xmmc_dbdma_shutdown(host);
1120#endif
Pete Popovba264b32005-09-21 06:18:27 +00001121 au1xmmc_set_power(host, 0);
1122
Manuel Laussc4223c22008-06-09 08:36:13 +02001123 free_irq(host->irq, host);
1124 iounmap((void *)host->iobase);
1125 release_resource(host->ioarea);
1126 kfree(host->ioarea);
Pete Popovba264b32005-09-21 06:18:27 +00001127
Manuel Laussdd8572a2008-07-17 13:07:28 +02001128 mmc_free_host(host->mmc);
1129 platform_set_drvdata(pdev, NULL);
Pete Popovba264b32005-09-21 06:18:27 +00001130 }
Pete Popovba264b32005-09-21 06:18:27 +00001131 return 0;
1132}
1133
Manuel Laussdd8572a2008-07-17 13:07:28 +02001134#ifdef CONFIG_PM
1135static int au1xmmc_suspend(struct platform_device *pdev, pm_message_t state)
1136{
1137 struct au1xmmc_host *host = platform_get_drvdata(pdev);
1138 int ret;
1139
1140 ret = mmc_suspend_host(host->mmc, state);
1141 if (ret)
1142 return ret;
1143
1144 au_writel(0, HOST_CONFIG2(host));
1145 au_writel(0, HOST_CONFIG(host));
1146 au_writel(0xffffffff, HOST_STATUS(host));
1147 au_writel(0, HOST_ENABLE(host));
1148 au_sync();
1149
1150 return 0;
1151}
1152
1153static int au1xmmc_resume(struct platform_device *pdev)
1154{
1155 struct au1xmmc_host *host = platform_get_drvdata(pdev);
1156
1157 au1xmmc_reset_controller(host);
1158
1159 return mmc_resume_host(host->mmc);
1160}
1161#else
1162#define au1xmmc_suspend NULL
1163#define au1xmmc_resume NULL
1164#endif
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,
Manuel Laussdd8572a2008-07-17 13:07:28 +02001169 .suspend = au1xmmc_suspend,
1170 .resume = au1xmmc_resume,
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001171 .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");