blob: 3f15eb204895f25cfaa75af21e8ee59dca16b8f2 [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
64#define AU1XMMC_DESCRIPTOR_SIZE 2048
65
66#define AU1XMMC_OCR (MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 | \
67 MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 | \
68 MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36)
69
70/* This gives us a hard value for the stop command that we can write directly
71 * to the command register.
72 */
73#define STOP_CMD \
74 (SD_CMD_RT_1B | SD_CMD_CT_7 | (0xC << SD_CMD_CI_SHIFT) | SD_CMD_GO)
75
76/* This is the set of interrupts that we configure by default. */
77#define AU1XMMC_INTERRUPTS \
78 (SD_CONFIG_SC | SD_CONFIG_DT | SD_CONFIG_RAT | \
79 SD_CONFIG_CR | SD_CONFIG_I)
80
81/* The poll event (looking for insert/remove events runs twice a second. */
82#define AU1XMMC_DETECT_TIMEOUT (HZ/2)
83
84struct au1xmmc_host {
85 struct mmc_host *mmc;
86 struct mmc_request *mrq;
87
88 u32 flags;
89 u32 iobase;
90 u32 clock;
91 u32 bus_width;
92 u32 power_mode;
93
94 int status;
95
96 struct {
97 int len;
98 int dir;
99 } dma;
100
101 struct {
102 int index;
103 int offset;
104 int len;
105 } pio;
106
107 u32 tx_chan;
108 u32 rx_chan;
109
110 int irq;
111
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200112 struct tasklet_struct finish_task;
113 struct tasklet_struct data_task;
114 struct au1xmmc_platform_data *platdata;
115 struct platform_device *pdev;
116 struct resource *ioarea;
117};
118
119/* Status flags used by the host structure */
120#define HOST_F_XMIT 0x0001
121#define HOST_F_RECV 0x0002
122#define HOST_F_DMA 0x0010
123#define HOST_F_ACTIVE 0x0100
124#define HOST_F_STOP 0x1000
125
126#define HOST_S_IDLE 0x0001
127#define HOST_S_CMD 0x0002
128#define HOST_S_DATA 0x0003
129#define HOST_S_STOP 0x0004
130
131/* Easy access macros */
132#define HOST_STATUS(h) ((h)->iobase + SD_STATUS)
133#define HOST_CONFIG(h) ((h)->iobase + SD_CONFIG)
134#define HOST_ENABLE(h) ((h)->iobase + SD_ENABLE)
135#define HOST_TXPORT(h) ((h)->iobase + SD_TXPORT)
136#define HOST_RXPORT(h) ((h)->iobase + SD_RXPORT)
137#define HOST_CMDARG(h) ((h)->iobase + SD_CMDARG)
138#define HOST_BLKSIZE(h) ((h)->iobase + SD_BLKSIZE)
139#define HOST_CMD(h) ((h)->iobase + SD_CMD)
140#define HOST_CONFIG2(h) ((h)->iobase + SD_CONFIG2)
141#define HOST_TIMEOUT(h) ((h)->iobase + SD_TIMEOUT)
142#define HOST_DEBUG(h) ((h)->iobase + SD_DEBUG)
143
144#define DMA_CHANNEL(h) \
145 (((h)->flags & HOST_F_XMIT) ? (h)->tx_chan : (h)->rx_chan)
146
Pete Popovba264b32005-09-21 06:18:27 +0000147static inline void IRQ_ON(struct au1xmmc_host *host, u32 mask)
148{
149 u32 val = au_readl(HOST_CONFIG(host));
150 val |= mask;
151 au_writel(val, HOST_CONFIG(host));
152 au_sync();
153}
154
155static inline void FLUSH_FIFO(struct au1xmmc_host *host)
156{
157 u32 val = au_readl(HOST_CONFIG2(host));
158
159 au_writel(val | SD_CONFIG2_FF, HOST_CONFIG2(host));
160 au_sync_delay(1);
161
162 /* SEND_STOP will turn off clock control - this re-enables it */
163 val &= ~SD_CONFIG2_DF;
164
165 au_writel(val, HOST_CONFIG2(host));
166 au_sync();
167}
168
169static inline void IRQ_OFF(struct au1xmmc_host *host, u32 mask)
170{
171 u32 val = au_readl(HOST_CONFIG(host));
172 val &= ~mask;
173 au_writel(val, HOST_CONFIG(host));
174 au_sync();
175}
176
177static inline void SEND_STOP(struct au1xmmc_host *host)
178{
Manuel Lauss281dd232008-06-09 08:37:33 +0200179 u32 config2;
Pete Popovba264b32005-09-21 06:18:27 +0000180
181 WARN_ON(host->status != HOST_S_DATA);
182 host->status = HOST_S_STOP;
183
Manuel Lauss281dd232008-06-09 08:37:33 +0200184 config2 = au_readl(HOST_CONFIG2(host));
185 au_writel(config2 | SD_CONFIG2_DF, HOST_CONFIG2(host));
Pete Popovba264b32005-09-21 06:18:27 +0000186 au_sync();
187
188 /* Send the stop commmand */
189 au_writel(STOP_CMD, HOST_CMD(host));
190}
191
192static void au1xmmc_set_power(struct au1xmmc_host *host, int state)
193{
Manuel Laussc4223c22008-06-09 08:36:13 +0200194 if (host->platdata && host->platdata->set_power)
195 host->platdata->set_power(host->mmc, state);
Pete Popovba264b32005-09-21 06:18:27 +0000196}
197
Manuel Lausse2d26472008-06-27 18:25:18 +0200198static int au1xmmc_card_inserted(struct mmc_host *mmc)
Pete Popovba264b32005-09-21 06:18:27 +0000199{
Manuel Lausse2d26472008-06-27 18:25:18 +0200200 struct au1xmmc_host *host = mmc_priv(mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200201
202 if (host->platdata && host->platdata->card_inserted)
Manuel Lausse2d26472008-06-27 18:25:18 +0200203 return !!host->platdata->card_inserted(host->mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200204
Manuel Lausse2d26472008-06-27 18:25:18 +0200205 return -ENOSYS;
Pete Popovba264b32005-09-21 06:18:27 +0000206}
207
Manuel Lauss82999772007-01-25 10:29:24 +0100208static int au1xmmc_card_readonly(struct mmc_host *mmc)
Pete Popovba264b32005-09-21 06:18:27 +0000209{
Manuel Lauss82999772007-01-25 10:29:24 +0100210 struct au1xmmc_host *host = mmc_priv(mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200211
212 if (host->platdata && host->platdata->card_readonly)
Manuel Lausse2d26472008-06-27 18:25:18 +0200213 return !!host->platdata->card_readonly(mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200214
Manuel Lausse2d26472008-06-27 18:25:18 +0200215 return -ENOSYS;
Pete Popovba264b32005-09-21 06:18:27 +0000216}
217
218static void au1xmmc_finish_request(struct au1xmmc_host *host)
219{
Pete Popovba264b32005-09-21 06:18:27 +0000220 struct mmc_request *mrq = host->mrq;
221
222 host->mrq = NULL;
Manuel Laussc4223c22008-06-09 08:36:13 +0200223 host->flags &= HOST_F_ACTIVE | HOST_F_DMA;
Pete Popovba264b32005-09-21 06:18:27 +0000224
225 host->dma.len = 0;
226 host->dma.dir = 0;
227
228 host->pio.index = 0;
229 host->pio.offset = 0;
230 host->pio.len = 0;
231
232 host->status = HOST_S_IDLE;
233
Pete Popovba264b32005-09-21 06:18:27 +0000234 mmc_request_done(host->mmc, mrq);
235}
236
237static void au1xmmc_tasklet_finish(unsigned long param)
238{
239 struct au1xmmc_host *host = (struct au1xmmc_host *) param;
240 au1xmmc_finish_request(host);
241}
242
243static int au1xmmc_send_command(struct au1xmmc_host *host, int wait,
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200244 struct mmc_command *cmd, struct mmc_data *data)
Pete Popovba264b32005-09-21 06:18:27 +0000245{
Pete Popovba264b32005-09-21 06:18:27 +0000246 u32 mmccmd = (cmd->opcode << SD_CMD_CI_SHIFT);
247
Martin Michlmayre142c242006-03-04 23:01:39 +0000248 switch (mmc_resp_type(cmd)) {
Manuel Lauss279bc442007-01-25 10:27:41 +0100249 case MMC_RSP_NONE:
250 break;
Pete Popovba264b32005-09-21 06:18:27 +0000251 case MMC_RSP_R1:
252 mmccmd |= SD_CMD_RT_1;
253 break;
254 case MMC_RSP_R1B:
255 mmccmd |= SD_CMD_RT_1B;
256 break;
257 case MMC_RSP_R2:
258 mmccmd |= SD_CMD_RT_2;
259 break;
260 case MMC_RSP_R3:
261 mmccmd |= SD_CMD_RT_3;
262 break;
Manuel Lauss279bc442007-01-25 10:27:41 +0100263 default:
264 printk(KERN_INFO "au1xmmc: unhandled response type %02x\n",
265 mmc_resp_type(cmd));
Pierre Ossman17b04292007-07-22 22:18:46 +0200266 return -EINVAL;
Pete Popovba264b32005-09-21 06:18:27 +0000267 }
268
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200269 if (data) {
Pierre Ossman6356a9d2007-10-22 18:16:16 +0200270 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200271 if (data->blocks > 1)
272 mmccmd |= SD_CMD_CT_4;
273 else
274 mmccmd |= SD_CMD_CT_2;
Pierre Ossman6356a9d2007-10-22 18:16:16 +0200275 } else if (data->flags & MMC_DATA_WRITE) {
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200276 if (data->blocks > 1)
277 mmccmd |= SD_CMD_CT_3;
278 else
279 mmccmd |= SD_CMD_CT_1;
280 }
Pete Popovba264b32005-09-21 06:18:27 +0000281 }
282
283 au_writel(cmd->arg, HOST_CMDARG(host));
284 au_sync();
285
286 if (wait)
287 IRQ_OFF(host, SD_CONFIG_CR);
288
289 au_writel((mmccmd | SD_CMD_GO), HOST_CMD(host));
290 au_sync();
291
292 /* Wait for the command to go on the line */
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200293 while (au_readl(HOST_CMD(host)) & SD_CMD_GO)
294 /* nop */;
Pete Popovba264b32005-09-21 06:18:27 +0000295
296 /* Wait for the command to come back */
Pete Popovba264b32005-09-21 06:18:27 +0000297 if (wait) {
298 u32 status = au_readl(HOST_STATUS(host));
299
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200300 while (!(status & SD_STATUS_CR))
Pete Popovba264b32005-09-21 06:18:27 +0000301 status = au_readl(HOST_STATUS(host));
302
303 /* Clear the CR status */
304 au_writel(SD_STATUS_CR, HOST_STATUS(host));
305
306 IRQ_ON(host, SD_CONFIG_CR);
307 }
308
Pierre Ossman17b04292007-07-22 22:18:46 +0200309 return 0;
Pete Popovba264b32005-09-21 06:18:27 +0000310}
311
312static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
313{
Pete Popovba264b32005-09-21 06:18:27 +0000314 struct mmc_request *mrq = host->mrq;
315 struct mmc_data *data;
316 u32 crc;
317
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200318 WARN_ON((host->status != HOST_S_DATA) && (host->status != HOST_S_STOP));
Pete Popovba264b32005-09-21 06:18:27 +0000319
320 if (host->mrq == NULL)
321 return;
322
323 data = mrq->cmd->data;
324
325 if (status == 0)
326 status = au_readl(HOST_STATUS(host));
327
328 /* The transaction is really over when the SD_STATUS_DB bit is clear */
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200329 while ((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB))
Pete Popovba264b32005-09-21 06:18:27 +0000330 status = au_readl(HOST_STATUS(host));
331
Pierre Ossman17b04292007-07-22 22:18:46 +0200332 data->error = 0;
Pete Popovba264b32005-09-21 06:18:27 +0000333 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir);
334
335 /* Process any errors */
Pete Popovba264b32005-09-21 06:18:27 +0000336 crc = (status & (SD_STATUS_WC | SD_STATUS_RC));
337 if (host->flags & HOST_F_XMIT)
338 crc |= ((status & 0x07) == 0x02) ? 0 : 1;
339
340 if (crc)
Pierre Ossman17b04292007-07-22 22:18:46 +0200341 data->error = -EILSEQ;
Pete Popovba264b32005-09-21 06:18:27 +0000342
343 /* Clear the CRC bits */
344 au_writel(SD_STATUS_WC | SD_STATUS_RC, HOST_STATUS(host));
345
346 data->bytes_xfered = 0;
347
Pierre Ossman17b04292007-07-22 22:18:46 +0200348 if (!data->error) {
Pete Popovba264b32005-09-21 06:18:27 +0000349 if (host->flags & HOST_F_DMA) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200350#ifdef CONFIG_SOC_AU1200 /* DBDMA */
Pete Popovba264b32005-09-21 06:18:27 +0000351 u32 chan = DMA_CHANNEL(host);
352
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200353 chan_tab_t *c = *((chan_tab_t **)chan);
Pete Popovba264b32005-09-21 06:18:27 +0000354 au1x_dma_chan_t *cp = c->chan_ptr;
355 data->bytes_xfered = cp->ddma_bytecnt;
Manuel Laussc4223c22008-06-09 08:36:13 +0200356#endif
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200357 } else
Pete Popovba264b32005-09-21 06:18:27 +0000358 data->bytes_xfered =
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200359 (data->blocks * data->blksz) - host->pio.len;
Pete Popovba264b32005-09-21 06:18:27 +0000360 }
361
362 au1xmmc_finish_request(host);
363}
364
365static void au1xmmc_tasklet_data(unsigned long param)
366{
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200367 struct au1xmmc_host *host = (struct au1xmmc_host *)param;
Pete Popovba264b32005-09-21 06:18:27 +0000368
369 u32 status = au_readl(HOST_STATUS(host));
370 au1xmmc_data_complete(host, status);
371}
372
373#define AU1XMMC_MAX_TRANSFER 8
374
375static void au1xmmc_send_pio(struct au1xmmc_host *host)
376{
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200377 struct mmc_data *data;
378 int sg_len, max, count;
379 unsigned char *sg_ptr, val;
380 u32 status;
Pete Popovba264b32005-09-21 06:18:27 +0000381 struct scatterlist *sg;
382
383 data = host->mrq->data;
384
385 if (!(host->flags & HOST_F_XMIT))
386 return;
387
388 /* This is the pointer to the data buffer */
389 sg = &data->sg[host->pio.index];
Jens Axboe45711f12007-10-22 21:19:53 +0200390 sg_ptr = sg_virt(sg) + host->pio.offset;
Pete Popovba264b32005-09-21 06:18:27 +0000391
392 /* This is the space left inside the buffer */
393 sg_len = data->sg[host->pio.index].length - host->pio.offset;
394
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200395 /* Check if we need less than the size of the sg_buffer */
Pete Popovba264b32005-09-21 06:18:27 +0000396 max = (sg_len > host->pio.len) ? host->pio.len : sg_len;
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200397 if (max > AU1XMMC_MAX_TRANSFER)
398 max = AU1XMMC_MAX_TRANSFER;
Pete Popovba264b32005-09-21 06:18:27 +0000399
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200400 for (count = 0; count < max; count++) {
Pete Popovba264b32005-09-21 06:18:27 +0000401 status = au_readl(HOST_STATUS(host));
402
403 if (!(status & SD_STATUS_TH))
404 break;
405
406 val = *sg_ptr++;
407
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200408 au_writel((unsigned long)val, HOST_TXPORT(host));
Pete Popovba264b32005-09-21 06:18:27 +0000409 au_sync();
410 }
411
412 host->pio.len -= count;
413 host->pio.offset += count;
414
415 if (count == sg_len) {
416 host->pio.index++;
417 host->pio.offset = 0;
418 }
419
420 if (host->pio.len == 0) {
421 IRQ_OFF(host, SD_CONFIG_TH);
422
423 if (host->flags & HOST_F_STOP)
424 SEND_STOP(host);
425
426 tasklet_schedule(&host->data_task);
427 }
428}
429
430static void au1xmmc_receive_pio(struct au1xmmc_host *host)
431{
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200432 struct mmc_data *data;
433 int max, count, sg_len = 0;
434 unsigned char *sg_ptr = NULL;
435 u32 status, val;
Pete Popovba264b32005-09-21 06:18:27 +0000436 struct scatterlist *sg;
437
438 data = host->mrq->data;
439
440 if (!(host->flags & HOST_F_RECV))
441 return;
442
443 max = host->pio.len;
444
445 if (host->pio.index < host->dma.len) {
446 sg = &data->sg[host->pio.index];
Jens Axboe45711f12007-10-22 21:19:53 +0200447 sg_ptr = sg_virt(sg) + host->pio.offset;
Pete Popovba264b32005-09-21 06:18:27 +0000448
449 /* This is the space left inside the buffer */
450 sg_len = sg_dma_len(&data->sg[host->pio.index]) - host->pio.offset;
451
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200452 /* Check if we need less than the size of the sg_buffer */
453 if (sg_len < max)
454 max = sg_len;
Pete Popovba264b32005-09-21 06:18:27 +0000455 }
456
457 if (max > AU1XMMC_MAX_TRANSFER)
458 max = AU1XMMC_MAX_TRANSFER;
459
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200460 for (count = 0; count < max; count++) {
Pete Popovba264b32005-09-21 06:18:27 +0000461 status = au_readl(HOST_STATUS(host));
462
463 if (!(status & SD_STATUS_NE))
464 break;
465
466 if (status & SD_STATUS_RC) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200467 DBG("RX CRC Error [%d + %d].\n", host->pdev->id,
Pete Popovba264b32005-09-21 06:18:27 +0000468 host->pio.len, count);
469 break;
470 }
471
472 if (status & SD_STATUS_RO) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200473 DBG("RX Overrun [%d + %d]\n", host->pdev->id,
Pete Popovba264b32005-09-21 06:18:27 +0000474 host->pio.len, count);
475 break;
476 }
477 else if (status & SD_STATUS_RU) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200478 DBG("RX Underrun [%d + %d]\n", host->pdev->id,
Pete Popovba264b32005-09-21 06:18:27 +0000479 host->pio.len, count);
480 break;
481 }
482
483 val = au_readl(HOST_RXPORT(host));
484
485 if (sg_ptr)
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200486 *sg_ptr++ = (unsigned char)(val & 0xFF);
Pete Popovba264b32005-09-21 06:18:27 +0000487 }
488
489 host->pio.len -= count;
490 host->pio.offset += count;
491
492 if (sg_len && count == sg_len) {
493 host->pio.index++;
494 host->pio.offset = 0;
495 }
496
497 if (host->pio.len == 0) {
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200498 /* IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF); */
Pete Popovba264b32005-09-21 06:18:27 +0000499 IRQ_OFF(host, SD_CONFIG_NE);
500
501 if (host->flags & HOST_F_STOP)
502 SEND_STOP(host);
503
504 tasklet_schedule(&host->data_task);
505 }
506}
507
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200508/* This is called when a command has been completed - grab the response
509 * and check for errors. Then start the data transfer if it is indicated.
510 */
Pete Popovba264b32005-09-21 06:18:27 +0000511static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
512{
Pete Popovba264b32005-09-21 06:18:27 +0000513 struct mmc_request *mrq = host->mrq;
514 struct mmc_command *cmd;
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200515 u32 r[4];
516 int i, trans;
Pete Popovba264b32005-09-21 06:18:27 +0000517
518 if (!host->mrq)
519 return;
520
521 cmd = mrq->cmd;
Pierre Ossman17b04292007-07-22 22:18:46 +0200522 cmd->error = 0;
Pete Popovba264b32005-09-21 06:18:27 +0000523
Russell Kinge9225172006-02-02 12:23:12 +0000524 if (cmd->flags & MMC_RSP_PRESENT) {
525 if (cmd->flags & MMC_RSP_136) {
Russell Kinge9225172006-02-02 12:23:12 +0000526 r[0] = au_readl(host->iobase + SD_RESP3);
527 r[1] = au_readl(host->iobase + SD_RESP2);
528 r[2] = au_readl(host->iobase + SD_RESP1);
529 r[3] = au_readl(host->iobase + SD_RESP0);
Pete Popovba264b32005-09-21 06:18:27 +0000530
Russell Kinge9225172006-02-02 12:23:12 +0000531 /* The CRC is omitted from the response, so really
532 * we only got 120 bytes, but the engine expects
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200533 * 128 bits, so we have to shift things up.
Russell Kinge9225172006-02-02 12:23:12 +0000534 */
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200535 for (i = 0; i < 4; i++) {
Russell Kinge9225172006-02-02 12:23:12 +0000536 cmd->resp[i] = (r[i] & 0x00FFFFFF) << 8;
537 if (i != 3)
538 cmd->resp[i] |= (r[i + 1] & 0xFF000000) >> 24;
539 }
540 } else {
541 /* Techincally, we should be getting all 48 bits of
542 * the response (SD_RESP1 + SD_RESP2), but because
543 * our response omits the CRC, our data ends up
544 * being shifted 8 bits to the right. In this case,
545 * that means that the OSR data starts at bit 31,
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200546 * so we can just read RESP0 and return that.
Russell Kinge9225172006-02-02 12:23:12 +0000547 */
548 cmd->resp[0] = au_readl(host->iobase + SD_RESP0);
Pete Popovba264b32005-09-21 06:18:27 +0000549 }
550 }
551
552 /* Figure out errors */
Pete Popovba264b32005-09-21 06:18:27 +0000553 if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC))
Pierre Ossman17b04292007-07-22 22:18:46 +0200554 cmd->error = -EILSEQ;
Pete Popovba264b32005-09-21 06:18:27 +0000555
556 trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);
557
Pierre Ossman17b04292007-07-22 22:18:46 +0200558 if (!trans || cmd->error) {
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200559 IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF);
Pete Popovba264b32005-09-21 06:18:27 +0000560 tasklet_schedule(&host->finish_task);
561 return;
562 }
563
564 host->status = HOST_S_DATA;
565
566 if (host->flags & HOST_F_DMA) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200567#ifdef CONFIG_SOC_AU1200 /* DBDMA */
Pete Popovba264b32005-09-21 06:18:27 +0000568 u32 channel = DMA_CHANNEL(host);
569
570 /* Start the DMA as soon as the buffer gets something in it */
571
572 if (host->flags & HOST_F_RECV) {
573 u32 mask = SD_STATUS_DB | SD_STATUS_NE;
574
575 while((status & mask) != mask)
576 status = au_readl(HOST_STATUS(host));
577 }
578
579 au1xxx_dbdma_start(channel);
Manuel Laussc4223c22008-06-09 08:36:13 +0200580#endif
Pete Popovba264b32005-09-21 06:18:27 +0000581 }
582}
583
584static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate)
585{
Pete Popovba264b32005-09-21 06:18:27 +0000586 unsigned int pbus = get_au1x00_speed();
587 unsigned int divisor;
588 u32 config;
589
590 /* From databook:
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200591 * divisor = ((((cpuclock / sbus_divisor) / 2) / mmcclock) / 2) - 1
592 */
Pete Popovba264b32005-09-21 06:18:27 +0000593 pbus /= ((au_readl(SYS_POWERCTRL) & 0x3) + 2);
594 pbus /= 2;
Pete Popovba264b32005-09-21 06:18:27 +0000595 divisor = ((pbus / rate) / 2) - 1;
596
597 config = au_readl(HOST_CONFIG(host));
598
599 config &= ~(SD_CONFIG_DIV);
600 config |= (divisor & SD_CONFIG_DIV) | SD_CONFIG_DE;
601
602 au_writel(config, HOST_CONFIG(host));
603 au_sync();
604}
605
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200606static int au1xmmc_prepare_data(struct au1xmmc_host *host,
607 struct mmc_data *data)
Pete Popovba264b32005-09-21 06:18:27 +0000608{
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100609 int datalen = data->blocks * data->blksz;
Pete Popovba264b32005-09-21 06:18:27 +0000610
Pete Popovba264b32005-09-21 06:18:27 +0000611 if (data->flags & MMC_DATA_READ)
612 host->flags |= HOST_F_RECV;
613 else
614 host->flags |= HOST_F_XMIT;
615
616 if (host->mrq->stop)
617 host->flags |= HOST_F_STOP;
618
619 host->dma.dir = DMA_BIDIRECTIONAL;
620
621 host->dma.len = dma_map_sg(mmc_dev(host->mmc), data->sg,
622 data->sg_len, host->dma.dir);
623
624 if (host->dma.len == 0)
Pierre Ossman17b04292007-07-22 22:18:46 +0200625 return -ETIMEDOUT;
Pete Popovba264b32005-09-21 06:18:27 +0000626
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100627 au_writel(data->blksz - 1, HOST_BLKSIZE(host));
Pete Popovba264b32005-09-21 06:18:27 +0000628
629 if (host->flags & HOST_F_DMA) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200630#ifdef CONFIG_SOC_AU1200 /* DBDMA */
Pete Popovba264b32005-09-21 06:18:27 +0000631 int i;
632 u32 channel = DMA_CHANNEL(host);
633
634 au1xxx_dbdma_stop(channel);
635
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200636 for (i = 0; i < host->dma.len; i++) {
Pete Popovba264b32005-09-21 06:18:27 +0000637 u32 ret = 0, flags = DDMA_FLAGS_NOIE;
638 struct scatterlist *sg = &data->sg[i];
639 int sg_len = sg->length;
640
641 int len = (datalen > sg_len) ? sg_len : datalen;
642
643 if (i == host->dma.len - 1)
644 flags = DDMA_FLAGS_IE;
645
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200646 if (host->flags & HOST_F_XMIT) {
647 ret = au1xxx_dbdma_put_source_flags(channel,
648 (void *)sg_virt(sg), len, flags);
649 } else {
650 ret = au1xxx_dbdma_put_dest_flags(channel,
651 (void *)sg_virt(sg), len, flags);
Pete Popovba264b32005-09-21 06:18:27 +0000652 }
653
Manuel Laussc4223c22008-06-09 08:36:13 +0200654 if (!ret)
Pete Popovba264b32005-09-21 06:18:27 +0000655 goto dataerr;
656
657 datalen -= len;
658 }
Manuel Laussc4223c22008-06-09 08:36:13 +0200659#endif
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200660 } else {
Pete Popovba264b32005-09-21 06:18:27 +0000661 host->pio.index = 0;
662 host->pio.offset = 0;
663 host->pio.len = datalen;
664
665 if (host->flags & HOST_F_XMIT)
666 IRQ_ON(host, SD_CONFIG_TH);
667 else
668 IRQ_ON(host, SD_CONFIG_NE);
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200669 /* IRQ_ON(host, SD_CONFIG_RA | SD_CONFIG_RF); */
Pete Popovba264b32005-09-21 06:18:27 +0000670 }
671
Pierre Ossman17b04292007-07-22 22:18:46 +0200672 return 0;
Pete Popovba264b32005-09-21 06:18:27 +0000673
Manuel Laussc4223c22008-06-09 08:36:13 +0200674dataerr:
675 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
676 host->dma.dir);
Pierre Ossman17b04292007-07-22 22:18:46 +0200677 return -ETIMEDOUT;
Pete Popovba264b32005-09-21 06:18:27 +0000678}
679
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200680/* This actually starts a command or data transaction */
Pete Popovba264b32005-09-21 06:18:27 +0000681static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
682{
Pete Popovba264b32005-09-21 06:18:27 +0000683 struct au1xmmc_host *host = mmc_priv(mmc);
Pierre Ossman17b04292007-07-22 22:18:46 +0200684 int ret = 0;
Pete Popovba264b32005-09-21 06:18:27 +0000685
686 WARN_ON(irqs_disabled());
687 WARN_ON(host->status != HOST_S_IDLE);
688
689 host->mrq = mrq;
690 host->status = HOST_S_CMD;
691
Manuel Lauss88b8d9a2008-06-09 08:39:11 +0200692 /* fail request immediately if no card is present */
Manuel Lausse2d26472008-06-27 18:25:18 +0200693 if (0 == au1xmmc_card_inserted(mmc)) {
Manuel Lauss88b8d9a2008-06-09 08:39:11 +0200694 mrq->cmd->error = -ENOMEDIUM;
695 au1xmmc_finish_request(host);
696 return;
697 }
698
Pete Popovba264b32005-09-21 06:18:27 +0000699 if (mrq->data) {
700 FLUSH_FIFO(host);
701 ret = au1xmmc_prepare_data(host, mrq->data);
702 }
703
Pierre Ossman17b04292007-07-22 22:18:46 +0200704 if (!ret)
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200705 ret = au1xmmc_send_command(host, 0, mrq->cmd, mrq->data);
Pete Popovba264b32005-09-21 06:18:27 +0000706
Pierre Ossman17b04292007-07-22 22:18:46 +0200707 if (ret) {
Pete Popovba264b32005-09-21 06:18:27 +0000708 mrq->cmd->error = ret;
709 au1xmmc_finish_request(host);
710 }
711}
712
713static void au1xmmc_reset_controller(struct au1xmmc_host *host)
714{
Pete Popovba264b32005-09-21 06:18:27 +0000715 /* Apply the clock */
716 au_writel(SD_ENABLE_CE, HOST_ENABLE(host));
717 au_sync_delay(1);
718
719 au_writel(SD_ENABLE_R | SD_ENABLE_CE, HOST_ENABLE(host));
720 au_sync_delay(5);
721
722 au_writel(~0, HOST_STATUS(host));
723 au_sync();
724
725 au_writel(0, HOST_BLKSIZE(host));
726 au_writel(0x001fffff, HOST_TIMEOUT(host));
727 au_sync();
728
729 au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
730 au_sync();
731
732 au_writel(SD_CONFIG2_EN | SD_CONFIG2_FF, HOST_CONFIG2(host));
733 au_sync_delay(1);
734
735 au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
736 au_sync();
737
738 /* Configure interrupts */
739 au_writel(AU1XMMC_INTERRUPTS, HOST_CONFIG(host));
740 au_sync();
741}
742
743
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200744static void au1xmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pete Popovba264b32005-09-21 06:18:27 +0000745{
746 struct au1xmmc_host *host = mmc_priv(mmc);
Manuel Lauss281dd232008-06-09 08:37:33 +0200747 u32 config2;
Pete Popovba264b32005-09-21 06:18:27 +0000748
Pete Popovba264b32005-09-21 06:18:27 +0000749 if (ios->power_mode == MMC_POWER_OFF)
750 au1xmmc_set_power(host, 0);
751 else if (ios->power_mode == MMC_POWER_ON) {
752 au1xmmc_set_power(host, 1);
753 }
754
755 if (ios->clock && ios->clock != host->clock) {
756 au1xmmc_set_clock(host, ios->clock);
757 host->clock = ios->clock;
758 }
Manuel Lauss281dd232008-06-09 08:37:33 +0200759
760 config2 = au_readl(HOST_CONFIG2(host));
761 switch (ios->bus_width) {
762 case MMC_BUS_WIDTH_4:
763 config2 |= SD_CONFIG2_WB;
764 break;
765 case MMC_BUS_WIDTH_1:
766 config2 &= ~SD_CONFIG2_WB;
767 break;
768 }
769 au_writel(config2, HOST_CONFIG2(host));
770 au_sync();
Pete Popovba264b32005-09-21 06:18:27 +0000771}
772
Manuel Laussc4223c22008-06-09 08:36:13 +0200773#define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
774#define STATUS_DATA_IN (SD_STATUS_NE)
775#define STATUS_DATA_OUT (SD_STATUS_TH)
776
777static irqreturn_t au1xmmc_irq(int irq, void *dev_id)
Pete Popovba264b32005-09-21 06:18:27 +0000778{
Manuel Laussc4223c22008-06-09 08:36:13 +0200779 struct au1xmmc_host *host = dev_id;
780 u32 status;
781
782 status = au_readl(HOST_STATUS(host));
783
784 if (!(status & SD_STATUS_I))
785 return IRQ_NONE; /* not ours */
786
Manuel Lauss20f522f2008-06-09 08:38:03 +0200787 if (status & SD_STATUS_SI) /* SDIO */
788 mmc_signal_sdio_irq(host->mmc);
789
Manuel Laussc4223c22008-06-09 08:36:13 +0200790 if (host->mrq && (status & STATUS_TIMEOUT)) {
791 if (status & SD_STATUS_RAT)
792 host->mrq->cmd->error = -ETIMEDOUT;
793 else if (status & SD_STATUS_DT)
794 host->mrq->data->error = -ETIMEDOUT;
795
796 /* In PIO mode, interrupts might still be enabled */
797 IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
798
799 /* IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF); */
800 tasklet_schedule(&host->finish_task);
801 }
802#if 0
803 else if (status & SD_STATUS_DD) {
804 /* Sometimes we get a DD before a NE in PIO mode */
805 if (!(host->flags & HOST_F_DMA) && (status & SD_STATUS_NE))
806 au1xmmc_receive_pio(host);
807 else {
808 au1xmmc_data_complete(host, status);
809 /* tasklet_schedule(&host->data_task); */
810 }
811 }
812#endif
813 else if (status & SD_STATUS_CR) {
814 if (host->status == HOST_S_CMD)
815 au1xmmc_cmd_complete(host, status);
816
817 } else if (!(host->flags & HOST_F_DMA)) {
818 if ((host->flags & HOST_F_XMIT) && (status & STATUS_DATA_OUT))
819 au1xmmc_send_pio(host);
820 else if ((host->flags & HOST_F_RECV) && (status & STATUS_DATA_IN))
821 au1xmmc_receive_pio(host);
822
823 } else if (status & 0x203F3C70) {
824 DBG("Unhandled status %8.8x\n", host->pdev->id,
825 status);
826 }
827
828 au_writel(status, HOST_STATUS(host));
829 au_sync();
830
831 return IRQ_HANDLED;
832}
833
834#ifdef CONFIG_SOC_AU1200
835/* 8bit memory DMA device */
836static dbdev_tab_t au1xmmc_mem_dbdev = {
837 .dev_id = DSCR_CMD0_ALWAYS,
838 .dev_flags = DEV_FLAGS_ANYUSE,
839 .dev_tsize = 0,
840 .dev_devwidth = 8,
841 .dev_physaddr = 0x00000000,
842 .dev_intlevel = 0,
843 .dev_intpolarity = 0,
844};
845static int memid;
846
847static void au1xmmc_dbdma_callback(int irq, void *dev_id)
848{
849 struct au1xmmc_host *host = (struct au1xmmc_host *)dev_id;
Pete Popovba264b32005-09-21 06:18:27 +0000850
851 /* Avoid spurious interrupts */
Pete Popovba264b32005-09-21 06:18:27 +0000852 if (!host->mrq)
853 return;
854
855 if (host->flags & HOST_F_STOP)
856 SEND_STOP(host);
857
858 tasklet_schedule(&host->data_task);
859}
860
Manuel Laussc4223c22008-06-09 08:36:13 +0200861static int au1xmmc_dbdma_init(struct au1xmmc_host *host)
Pete Popovba264b32005-09-21 06:18:27 +0000862{
Manuel Laussc4223c22008-06-09 08:36:13 +0200863 struct resource *res;
864 int txid, rxid;
Pete Popovba264b32005-09-21 06:18:27 +0000865
Manuel Laussc4223c22008-06-09 08:36:13 +0200866 res = platform_get_resource(host->pdev, IORESOURCE_DMA, 0);
867 if (!res)
868 return -ENODEV;
869 txid = res->start;
Pete Popovba264b32005-09-21 06:18:27 +0000870
Manuel Laussc4223c22008-06-09 08:36:13 +0200871 res = platform_get_resource(host->pdev, IORESOURCE_DMA, 1);
872 if (!res)
873 return -ENODEV;
874 rxid = res->start;
Pete Popovba264b32005-09-21 06:18:27 +0000875
Manuel Laussc4223c22008-06-09 08:36:13 +0200876 if (!memid)
877 return -ENODEV;
Pete Popovba264b32005-09-21 06:18:27 +0000878
Manuel Laussc4223c22008-06-09 08:36:13 +0200879 host->tx_chan = au1xxx_dbdma_chan_alloc(memid, txid,
880 au1xmmc_dbdma_callback, (void *)host);
881 if (!host->tx_chan) {
882 dev_err(&host->pdev->dev, "cannot allocate TX DMA\n");
883 return -ENODEV;
884 }
Pete Popovba264b32005-09-21 06:18:27 +0000885
Manuel Laussc4223c22008-06-09 08:36:13 +0200886 host->rx_chan = au1xxx_dbdma_chan_alloc(rxid, memid,
887 au1xmmc_dbdma_callback, (void *)host);
888 if (!host->rx_chan) {
889 dev_err(&host->pdev->dev, "cannot allocate RX DMA\n");
890 au1xxx_dbdma_chan_free(host->tx_chan);
891 return -ENODEV;
892 }
Pete Popovba264b32005-09-21 06:18:27 +0000893
Manuel Laussc4223c22008-06-09 08:36:13 +0200894 au1xxx_dbdma_set_devwidth(host->tx_chan, 8);
895 au1xxx_dbdma_set_devwidth(host->rx_chan, 8);
Pete Popovba264b32005-09-21 06:18:27 +0000896
Manuel Laussc4223c22008-06-09 08:36:13 +0200897 au1xxx_dbdma_ring_alloc(host->tx_chan, AU1XMMC_DESCRIPTOR_COUNT);
898 au1xxx_dbdma_ring_alloc(host->rx_chan, AU1XMMC_DESCRIPTOR_COUNT);
Pete Popovba264b32005-09-21 06:18:27 +0000899
Manuel Laussc4223c22008-06-09 08:36:13 +0200900 /* DBDMA is good to go */
901 host->flags |= HOST_F_DMA;
Pete Popovba264b32005-09-21 06:18:27 +0000902
Manuel Laussc4223c22008-06-09 08:36:13 +0200903 return 0;
904}
Pete Popovba264b32005-09-21 06:18:27 +0000905
Manuel Laussc4223c22008-06-09 08:36:13 +0200906static void au1xmmc_dbdma_shutdown(struct au1xmmc_host *host)
907{
908 if (host->flags & HOST_F_DMA) {
909 host->flags &= ~HOST_F_DMA;
910 au1xxx_dbdma_chan_free(host->tx_chan);
911 au1xxx_dbdma_chan_free(host->rx_chan);
912 }
913}
Pete Popovba264b32005-09-21 06:18:27 +0000914#endif
Pete Popovba264b32005-09-21 06:18:27 +0000915
Manuel Lauss20f522f2008-06-09 08:38:03 +0200916static void au1xmmc_enable_sdio_irq(struct mmc_host *mmc, int en)
917{
918 struct au1xmmc_host *host = mmc_priv(mmc);
919
920 if (en)
921 IRQ_ON(host, SD_CONFIG_SI);
922 else
923 IRQ_OFF(host, SD_CONFIG_SI);
924}
925
Yoichi Yuasabf8c80a2006-12-05 07:43:38 +0100926static const struct mmc_host_ops au1xmmc_ops = {
Pete Popovba264b32005-09-21 06:18:27 +0000927 .request = au1xmmc_request,
928 .set_ios = au1xmmc_set_ios,
Manuel Lauss82999772007-01-25 10:29:24 +0100929 .get_ro = au1xmmc_card_readonly,
Manuel Lausse2d26472008-06-27 18:25:18 +0200930 .get_cd = au1xmmc_card_inserted,
Manuel Lauss20f522f2008-06-09 08:38:03 +0200931 .enable_sdio_irq = au1xmmc_enable_sdio_irq,
Pete Popovba264b32005-09-21 06:18:27 +0000932};
933
Martin Michlmayrb256f9d2006-03-04 23:01:13 +0000934static int __devinit au1xmmc_probe(struct platform_device *pdev)
Pete Popovba264b32005-09-21 06:18:27 +0000935{
Manuel Laussc4223c22008-06-09 08:36:13 +0200936 struct mmc_host *mmc;
937 struct au1xmmc_host *host;
938 struct resource *r;
939 int ret;
Pete Popovba264b32005-09-21 06:18:27 +0000940
Manuel Laussc4223c22008-06-09 08:36:13 +0200941 mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), &pdev->dev);
942 if (!mmc) {
943 dev_err(&pdev->dev, "no memory for mmc_host\n");
944 ret = -ENOMEM;
945 goto out0;
Pete Popovba264b32005-09-21 06:18:27 +0000946 }
947
Manuel Laussc4223c22008-06-09 08:36:13 +0200948 host = mmc_priv(mmc);
949 host->mmc = mmc;
950 host->platdata = pdev->dev.platform_data;
951 host->pdev = pdev;
Pete Popovba264b32005-09-21 06:18:27 +0000952
Manuel Laussc4223c22008-06-09 08:36:13 +0200953 ret = -ENODEV;
954 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
955 if (!r) {
956 dev_err(&pdev->dev, "no mmio defined\n");
957 goto out1;
958 }
Pete Popovba264b32005-09-21 06:18:27 +0000959
Manuel Laussc4223c22008-06-09 08:36:13 +0200960 host->ioarea = request_mem_region(r->start, r->end - r->start + 1,
961 pdev->name);
962 if (!host->ioarea) {
963 dev_err(&pdev->dev, "mmio already in use\n");
964 goto out1;
965 }
966
967 host->iobase = (unsigned long)ioremap(r->start, 0x3c);
968 if (!host->iobase) {
969 dev_err(&pdev->dev, "cannot remap mmio\n");
970 goto out2;
971 }
972
973 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
974 if (!r) {
975 dev_err(&pdev->dev, "no IRQ defined\n");
976 goto out3;
977 }
978
979 host->irq = r->start;
980 /* IRQ is shared among both SD controllers */
981 ret = request_irq(host->irq, au1xmmc_irq, IRQF_SHARED,
982 DRIVER_NAME, host);
983 if (ret) {
984 dev_err(&pdev->dev, "cannot grab IRQ\n");
985 goto out3;
986 }
987
988 mmc->ops = &au1xmmc_ops;
989
990 mmc->f_min = 450000;
991 mmc->f_max = 24000000;
992
993 mmc->max_seg_size = AU1XMMC_DESCRIPTOR_SIZE;
994 mmc->max_phys_segs = AU1XMMC_DESCRIPTOR_COUNT;
995
996 mmc->max_blk_size = 2048;
997 mmc->max_blk_count = 512;
998
999 mmc->ocr_avail = AU1XMMC_OCR;
Manuel Lauss20f522f2008-06-09 08:38:03 +02001000 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
Manuel Laussc4223c22008-06-09 08:36:13 +02001001
1002 host->status = HOST_S_IDLE;
1003
1004 /* board-specific carddetect setup, if any */
1005 if (host->platdata && host->platdata->cd_setup) {
1006 ret = host->platdata->cd_setup(mmc, 1);
1007 if (ret) {
Manuel Lausse2d26472008-06-27 18:25:18 +02001008 dev_warn(&pdev->dev, "board CD setup failed\n");
1009 mmc->caps |= MMC_CAP_NEEDS_POLL;
Pete Popovba264b32005-09-21 06:18:27 +00001010 }
Manuel Lausse2d26472008-06-27 18:25:18 +02001011 } else
1012 mmc->caps |= MMC_CAP_NEEDS_POLL;
Pete Popovba264b32005-09-21 06:18:27 +00001013
Manuel Laussc4223c22008-06-09 08:36:13 +02001014 tasklet_init(&host->data_task, au1xmmc_tasklet_data,
1015 (unsigned long)host);
Pete Popovba264b32005-09-21 06:18:27 +00001016
Manuel Laussc4223c22008-06-09 08:36:13 +02001017 tasklet_init(&host->finish_task, au1xmmc_tasklet_finish,
1018 (unsigned long)host);
Pete Popovba264b32005-09-21 06:18:27 +00001019
Manuel Laussc4223c22008-06-09 08:36:13 +02001020#ifdef CONFIG_SOC_AU1200
1021 ret = au1xmmc_dbdma_init(host);
1022 if (ret)
1023 printk(KERN_INFO DRIVER_NAME ": DBDMA init failed; using PIO\n");
1024#endif
Pete Popovba264b32005-09-21 06:18:27 +00001025
Manuel Laussc4223c22008-06-09 08:36:13 +02001026#ifdef CONFIG_LEDS_CLASS
1027 if (host->platdata && host->platdata->led) {
1028 struct led_classdev *led = host->platdata->led;
1029 led->name = mmc_hostname(mmc);
1030 led->brightness = LED_OFF;
1031 led->default_trigger = mmc_hostname(mmc);
1032 ret = led_classdev_register(mmc_dev(mmc), led);
1033 if (ret)
1034 goto out5;
1035 }
1036#endif
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001037
Manuel Laussc4223c22008-06-09 08:36:13 +02001038 au1xmmc_reset_controller(host);
Pete Popovba264b32005-09-21 06:18:27 +00001039
Manuel Laussc4223c22008-06-09 08:36:13 +02001040 ret = mmc_add_host(mmc);
1041 if (ret) {
1042 dev_err(&pdev->dev, "cannot add mmc host\n");
1043 goto out6;
1044 }
Pete Popovba264b32005-09-21 06:18:27 +00001045
Manuel Laussc4223c22008-06-09 08:36:13 +02001046 platform_set_drvdata(pdev, mmc);
Pete Popovba264b32005-09-21 06:18:27 +00001047
Manuel Laussc4223c22008-06-09 08:36:13 +02001048 printk(KERN_INFO DRIVER_NAME ": MMC Controller %d set up at %8.8X"
1049 " (mode=%s)\n", pdev->id, host->iobase,
1050 host->flags & HOST_F_DMA ? "dma" : "pio");
Pete Popovba264b32005-09-21 06:18:27 +00001051
Manuel Laussc4223c22008-06-09 08:36:13 +02001052 return 0; /* all ok */
Pete Popovba264b32005-09-21 06:18:27 +00001053
Manuel Laussc4223c22008-06-09 08:36:13 +02001054out6:
1055#ifdef CONFIG_LEDS_CLASS
1056 if (host->platdata && host->platdata->led)
1057 led_classdev_unregister(host->platdata->led);
1058out5:
1059#endif
1060 au_writel(0, HOST_ENABLE(host));
1061 au_writel(0, HOST_CONFIG(host));
1062 au_writel(0, HOST_CONFIG2(host));
1063 au_sync();
1064
1065#ifdef CONFIG_SOC_AU1200
1066 au1xmmc_dbdma_shutdown(host);
1067#endif
1068
1069 tasklet_kill(&host->data_task);
1070 tasklet_kill(&host->finish_task);
1071
Manuel Lausse2d26472008-06-27 18:25:18 +02001072 if (host->platdata && host->platdata->cd_setup &&
1073 !(mmc->caps & MMC_CAP_NEEDS_POLL))
Manuel Laussc4223c22008-06-09 08:36:13 +02001074 host->platdata->cd_setup(mmc, 0);
Manuel Lausse2d26472008-06-27 18:25:18 +02001075
Manuel Laussc4223c22008-06-09 08:36:13 +02001076 free_irq(host->irq, host);
1077out3:
1078 iounmap((void *)host->iobase);
1079out2:
1080 release_resource(host->ioarea);
1081 kfree(host->ioarea);
1082out1:
1083 mmc_free_host(mmc);
1084out0:
1085 return ret;
Pete Popovba264b32005-09-21 06:18:27 +00001086}
1087
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001088static int __devexit au1xmmc_remove(struct platform_device *pdev)
Pete Popovba264b32005-09-21 06:18:27 +00001089{
Manuel Laussc4223c22008-06-09 08:36:13 +02001090 struct mmc_host *mmc = platform_get_drvdata(pdev);
1091 struct au1xmmc_host *host;
Pete Popovba264b32005-09-21 06:18:27 +00001092
Manuel Laussc4223c22008-06-09 08:36:13 +02001093 if (mmc) {
1094 host = mmc_priv(mmc);
Pete Popovba264b32005-09-21 06:18:27 +00001095
Manuel Laussc4223c22008-06-09 08:36:13 +02001096 mmc_remove_host(mmc);
Pete Popovba264b32005-09-21 06:18:27 +00001097
Manuel Laussc4223c22008-06-09 08:36:13 +02001098#ifdef CONFIG_LEDS_CLASS
1099 if (host->platdata && host->platdata->led)
1100 led_classdev_unregister(host->platdata->led);
1101#endif
1102
Manuel Lausse2d26472008-06-27 18:25:18 +02001103 if (host->platdata && host->platdata->cd_setup &&
1104 !(mmc->caps & MMC_CAP_NEEDS_POLL))
Manuel Laussc4223c22008-06-09 08:36:13 +02001105 host->platdata->cd_setup(mmc, 0);
Manuel Laussc4223c22008-06-09 08:36:13 +02001106
1107 au_writel(0, HOST_ENABLE(host));
1108 au_writel(0, HOST_CONFIG(host));
1109 au_writel(0, HOST_CONFIG2(host));
1110 au_sync();
Pete Popovba264b32005-09-21 06:18:27 +00001111
1112 tasklet_kill(&host->data_task);
1113 tasklet_kill(&host->finish_task);
1114
Manuel Laussc4223c22008-06-09 08:36:13 +02001115#ifdef CONFIG_SOC_AU1200
1116 au1xmmc_dbdma_shutdown(host);
1117#endif
Pete Popovba264b32005-09-21 06:18:27 +00001118 au1xmmc_set_power(host, 0);
1119
Manuel Laussc4223c22008-06-09 08:36:13 +02001120 free_irq(host->irq, host);
1121 iounmap((void *)host->iobase);
1122 release_resource(host->ioarea);
1123 kfree(host->ioarea);
Pete Popovba264b32005-09-21 06:18:27 +00001124
Manuel Laussc4223c22008-06-09 08:36:13 +02001125 mmc_free_host(mmc);
Pete Popovba264b32005-09-21 06:18:27 +00001126 }
Pete Popovba264b32005-09-21 06:18:27 +00001127 return 0;
1128}
1129
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001130static struct platform_driver au1xmmc_driver = {
Pete Popovba264b32005-09-21 06:18:27 +00001131 .probe = au1xmmc_probe,
1132 .remove = au1xmmc_remove,
1133 .suspend = NULL,
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001134 .resume = NULL,
1135 .driver = {
1136 .name = DRIVER_NAME,
Kay Sieversbc65c722008-04-15 14:34:28 -07001137 .owner = THIS_MODULE,
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001138 },
Pete Popovba264b32005-09-21 06:18:27 +00001139};
1140
1141static int __init au1xmmc_init(void)
1142{
Manuel Laussc4223c22008-06-09 08:36:13 +02001143#ifdef CONFIG_SOC_AU1200
1144 /* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride
1145 * of 8 bits. And since devices are shared, we need to create
1146 * our own to avoid freaking out other devices.
1147 */
1148 memid = au1xxx_ddma_add_device(&au1xmmc_mem_dbdev);
1149 if (!memid)
1150 printk(KERN_ERR "au1xmmc: cannot add memory dbdma dev\n");
1151#endif
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001152 return platform_driver_register(&au1xmmc_driver);
Pete Popovba264b32005-09-21 06:18:27 +00001153}
1154
1155static void __exit au1xmmc_exit(void)
1156{
Manuel Laussc4223c22008-06-09 08:36:13 +02001157#ifdef CONFIG_SOC_AU1200
1158 if (memid)
1159 au1xxx_ddma_del_device(memid);
1160#endif
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001161 platform_driver_unregister(&au1xmmc_driver);
Pete Popovba264b32005-09-21 06:18:27 +00001162}
1163
1164module_init(au1xmmc_init);
1165module_exit(au1xmmc_exit);
1166
Pete Popovba264b32005-09-21 06:18:27 +00001167MODULE_AUTHOR("Advanced Micro Devices, Inc");
1168MODULE_DESCRIPTION("MMC/SD driver for the Alchemy Au1XXX");
1169MODULE_LICENSE("GPL");
Kay Sieversbc65c722008-04-15 14:34:28 -07001170MODULE_ALIAS("platform:au1xxx-mmc");