blob: 0ea43c09803c96c41c822da949870b128dcea112 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Manuel Laussc4223c22008-06-09 08:36:13 +020045
Pete Popovba264b32005-09-21 06:18:27 +000046#include <asm/io.h>
47#include <asm/mach-au1x00/au1000.h>
48#include <asm/mach-au1x00/au1xxx_dbdma.h>
49#include <asm/mach-au1x00/au1100_mmc.h>
Pete Popovba264b32005-09-21 06:18:27 +000050
Pete Popovba264b32005-09-21 06:18:27 +000051#define DRIVER_NAME "au1xxx-mmc"
52
53/* Set this to enable special debugging macros */
Manuel Laussc4223c22008-06-09 08:36:13 +020054/* #define DEBUG */
Pete Popovba264b32005-09-21 06:18:27 +000055
Russell Kingc6563172006-03-29 09:30:20 +010056#ifdef DEBUG
Manuel Lauss5c0a8892008-06-09 08:38:35 +020057#define DBG(fmt, idx, args...) \
Girish K Sa3c76eb2011-10-11 11:44:09 +053058 pr_debug("au1xmmc(%d): DEBUG: " fmt, idx, ##args)
Pete Popovba264b32005-09-21 06:18:27 +000059#else
Manuel Lauss5c0a8892008-06-09 08:38:35 +020060#define DBG(fmt, idx, args...) do {} while (0)
Pete Popovba264b32005-09-21 06:18:27 +000061#endif
62
Manuel Lauss5c0a8892008-06-09 08:38:35 +020063/* Hardware definitions */
64#define AU1XMMC_DESCRIPTOR_COUNT 1
Manuel Lausse491d232008-07-29 10:10:49 +020065
66/* max DMA seg size: 64KB on Au1100, 4MB on Au1200 */
Manuel Lauss1177d992011-08-02 19:51:07 +020067#define AU1100_MMC_DESCRIPTOR_SIZE 0x0000ffff
68#define AU1200_MMC_DESCRIPTOR_SIZE 0x003fffff
Manuel Lauss5c0a8892008-06-09 08:38:35 +020069
70#define AU1XMMC_OCR (MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 | \
71 MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 | \
72 MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36)
73
74/* This gives us a hard value for the stop command that we can write directly
75 * to the command register.
76 */
77#define STOP_CMD \
78 (SD_CMD_RT_1B | SD_CMD_CT_7 | (0xC << SD_CMD_CI_SHIFT) | SD_CMD_GO)
79
80/* This is the set of interrupts that we configure by default. */
81#define AU1XMMC_INTERRUPTS \
82 (SD_CONFIG_SC | SD_CONFIG_DT | SD_CONFIG_RAT | \
83 SD_CONFIG_CR | SD_CONFIG_I)
84
85/* The poll event (looking for insert/remove events runs twice a second. */
86#define AU1XMMC_DETECT_TIMEOUT (HZ/2)
87
88struct au1xmmc_host {
89 struct mmc_host *mmc;
90 struct mmc_request *mrq;
91
92 u32 flags;
93 u32 iobase;
94 u32 clock;
95 u32 bus_width;
96 u32 power_mode;
97
98 int status;
99
100 struct {
101 int len;
102 int dir;
103 } dma;
104
105 struct {
106 int index;
107 int offset;
108 int len;
109 } pio;
110
111 u32 tx_chan;
112 u32 rx_chan;
113
114 int irq;
115
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200116 struct tasklet_struct finish_task;
117 struct tasklet_struct data_task;
118 struct au1xmmc_platform_data *platdata;
119 struct platform_device *pdev;
120 struct resource *ioarea;
121};
122
123/* Status flags used by the host structure */
124#define HOST_F_XMIT 0x0001
125#define HOST_F_RECV 0x0002
126#define HOST_F_DMA 0x0010
Manuel Lauss1177d992011-08-02 19:51:07 +0200127#define HOST_F_DBDMA 0x0020
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200128#define HOST_F_ACTIVE 0x0100
129#define HOST_F_STOP 0x1000
130
131#define HOST_S_IDLE 0x0001
132#define HOST_S_CMD 0x0002
133#define HOST_S_DATA 0x0003
134#define HOST_S_STOP 0x0004
135
136/* Easy access macros */
137#define HOST_STATUS(h) ((h)->iobase + SD_STATUS)
138#define HOST_CONFIG(h) ((h)->iobase + SD_CONFIG)
139#define HOST_ENABLE(h) ((h)->iobase + SD_ENABLE)
140#define HOST_TXPORT(h) ((h)->iobase + SD_TXPORT)
141#define HOST_RXPORT(h) ((h)->iobase + SD_RXPORT)
142#define HOST_CMDARG(h) ((h)->iobase + SD_CMDARG)
143#define HOST_BLKSIZE(h) ((h)->iobase + SD_BLKSIZE)
144#define HOST_CMD(h) ((h)->iobase + SD_CMD)
145#define HOST_CONFIG2(h) ((h)->iobase + SD_CONFIG2)
146#define HOST_TIMEOUT(h) ((h)->iobase + SD_TIMEOUT)
147#define HOST_DEBUG(h) ((h)->iobase + SD_DEBUG)
148
149#define DMA_CHANNEL(h) \
150 (((h)->flags & HOST_F_XMIT) ? (h)->tx_chan : (h)->rx_chan)
151
Manuel Lauss1177d992011-08-02 19:51:07 +0200152static inline int has_dbdma(void)
153{
154 switch (alchemy_get_cputype()) {
155 case ALCHEMY_CPU_AU1200:
Manuel Lauss809f36c2011-11-01 20:03:30 +0100156 case ALCHEMY_CPU_AU1300:
Manuel Lauss1177d992011-08-02 19:51:07 +0200157 return 1;
158 default:
159 return 0;
160 }
161}
162
Pete Popovba264b32005-09-21 06:18:27 +0000163static inline void IRQ_ON(struct au1xmmc_host *host, u32 mask)
164{
165 u32 val = au_readl(HOST_CONFIG(host));
166 val |= mask;
167 au_writel(val, HOST_CONFIG(host));
168 au_sync();
169}
170
171static inline void FLUSH_FIFO(struct au1xmmc_host *host)
172{
173 u32 val = au_readl(HOST_CONFIG2(host));
174
175 au_writel(val | SD_CONFIG2_FF, HOST_CONFIG2(host));
176 au_sync_delay(1);
177
178 /* SEND_STOP will turn off clock control - this re-enables it */
179 val &= ~SD_CONFIG2_DF;
180
181 au_writel(val, HOST_CONFIG2(host));
182 au_sync();
183}
184
185static inline void IRQ_OFF(struct au1xmmc_host *host, u32 mask)
186{
187 u32 val = au_readl(HOST_CONFIG(host));
188 val &= ~mask;
189 au_writel(val, HOST_CONFIG(host));
190 au_sync();
191}
192
193static inline void SEND_STOP(struct au1xmmc_host *host)
194{
Manuel Lauss281dd232008-06-09 08:37:33 +0200195 u32 config2;
Pete Popovba264b32005-09-21 06:18:27 +0000196
197 WARN_ON(host->status != HOST_S_DATA);
198 host->status = HOST_S_STOP;
199
Manuel Lauss281dd232008-06-09 08:37:33 +0200200 config2 = au_readl(HOST_CONFIG2(host));
201 au_writel(config2 | SD_CONFIG2_DF, HOST_CONFIG2(host));
Pete Popovba264b32005-09-21 06:18:27 +0000202 au_sync();
203
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400204 /* Send the stop command */
Pete Popovba264b32005-09-21 06:18:27 +0000205 au_writel(STOP_CMD, HOST_CMD(host));
206}
207
208static void au1xmmc_set_power(struct au1xmmc_host *host, int state)
209{
Manuel Laussc4223c22008-06-09 08:36:13 +0200210 if (host->platdata && host->platdata->set_power)
211 host->platdata->set_power(host->mmc, state);
Pete Popovba264b32005-09-21 06:18:27 +0000212}
213
Manuel Lausse2d26472008-06-27 18:25:18 +0200214static int au1xmmc_card_inserted(struct mmc_host *mmc)
Pete Popovba264b32005-09-21 06:18:27 +0000215{
Manuel Lausse2d26472008-06-27 18:25:18 +0200216 struct au1xmmc_host *host = mmc_priv(mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200217
218 if (host->platdata && host->platdata->card_inserted)
Manuel Lausse2d26472008-06-27 18:25:18 +0200219 return !!host->platdata->card_inserted(host->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
Manuel Lauss82999772007-01-25 10:29:24 +0100224static int au1xmmc_card_readonly(struct mmc_host *mmc)
Pete Popovba264b32005-09-21 06:18:27 +0000225{
Manuel Lauss82999772007-01-25 10:29:24 +0100226 struct au1xmmc_host *host = mmc_priv(mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200227
228 if (host->platdata && host->platdata->card_readonly)
Manuel Lausse2d26472008-06-27 18:25:18 +0200229 return !!host->platdata->card_readonly(mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200230
Manuel Lausse2d26472008-06-27 18:25:18 +0200231 return -ENOSYS;
Pete Popovba264b32005-09-21 06:18:27 +0000232}
233
234static void au1xmmc_finish_request(struct au1xmmc_host *host)
235{
Pete Popovba264b32005-09-21 06:18:27 +0000236 struct mmc_request *mrq = host->mrq;
237
238 host->mrq = NULL;
Manuel Laussc4223c22008-06-09 08:36:13 +0200239 host->flags &= HOST_F_ACTIVE | HOST_F_DMA;
Pete Popovba264b32005-09-21 06:18:27 +0000240
241 host->dma.len = 0;
242 host->dma.dir = 0;
243
244 host->pio.index = 0;
245 host->pio.offset = 0;
246 host->pio.len = 0;
247
248 host->status = HOST_S_IDLE;
249
Pete Popovba264b32005-09-21 06:18:27 +0000250 mmc_request_done(host->mmc, mrq);
251}
252
253static void au1xmmc_tasklet_finish(unsigned long param)
254{
255 struct au1xmmc_host *host = (struct au1xmmc_host *) param;
256 au1xmmc_finish_request(host);
257}
258
259static int au1xmmc_send_command(struct au1xmmc_host *host, int wait,
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200260 struct mmc_command *cmd, struct mmc_data *data)
Pete Popovba264b32005-09-21 06:18:27 +0000261{
Pete Popovba264b32005-09-21 06:18:27 +0000262 u32 mmccmd = (cmd->opcode << SD_CMD_CI_SHIFT);
263
Martin Michlmayre142c242006-03-04 23:01:39 +0000264 switch (mmc_resp_type(cmd)) {
Manuel Lauss279bc442007-01-25 10:27:41 +0100265 case MMC_RSP_NONE:
266 break;
Pete Popovba264b32005-09-21 06:18:27 +0000267 case MMC_RSP_R1:
268 mmccmd |= SD_CMD_RT_1;
269 break;
270 case MMC_RSP_R1B:
271 mmccmd |= SD_CMD_RT_1B;
272 break;
273 case MMC_RSP_R2:
274 mmccmd |= SD_CMD_RT_2;
275 break;
276 case MMC_RSP_R3:
277 mmccmd |= SD_CMD_RT_3;
278 break;
Manuel Lauss279bc442007-01-25 10:27:41 +0100279 default:
Girish K Sa3c76eb2011-10-11 11:44:09 +0530280 pr_info("au1xmmc: unhandled response type %02x\n",
Manuel Lauss279bc442007-01-25 10:27:41 +0100281 mmc_resp_type(cmd));
Pierre Ossman17b04292007-07-22 22:18:46 +0200282 return -EINVAL;
Pete Popovba264b32005-09-21 06:18:27 +0000283 }
284
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200285 if (data) {
Pierre Ossman6356a9d2007-10-22 18:16:16 +0200286 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200287 if (data->blocks > 1)
288 mmccmd |= SD_CMD_CT_4;
289 else
290 mmccmd |= SD_CMD_CT_2;
Pierre Ossman6356a9d2007-10-22 18:16:16 +0200291 } else if (data->flags & MMC_DATA_WRITE) {
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200292 if (data->blocks > 1)
293 mmccmd |= SD_CMD_CT_3;
294 else
295 mmccmd |= SD_CMD_CT_1;
296 }
Pete Popovba264b32005-09-21 06:18:27 +0000297 }
298
299 au_writel(cmd->arg, HOST_CMDARG(host));
300 au_sync();
301
302 if (wait)
303 IRQ_OFF(host, SD_CONFIG_CR);
304
305 au_writel((mmccmd | SD_CMD_GO), HOST_CMD(host));
306 au_sync();
307
308 /* Wait for the command to go on the line */
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200309 while (au_readl(HOST_CMD(host)) & SD_CMD_GO)
310 /* nop */;
Pete Popovba264b32005-09-21 06:18:27 +0000311
312 /* Wait for the command to come back */
Pete Popovba264b32005-09-21 06:18:27 +0000313 if (wait) {
314 u32 status = au_readl(HOST_STATUS(host));
315
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200316 while (!(status & SD_STATUS_CR))
Pete Popovba264b32005-09-21 06:18:27 +0000317 status = au_readl(HOST_STATUS(host));
318
319 /* Clear the CR status */
320 au_writel(SD_STATUS_CR, HOST_STATUS(host));
321
322 IRQ_ON(host, SD_CONFIG_CR);
323 }
324
Pierre Ossman17b04292007-07-22 22:18:46 +0200325 return 0;
Pete Popovba264b32005-09-21 06:18:27 +0000326}
327
328static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
329{
Pete Popovba264b32005-09-21 06:18:27 +0000330 struct mmc_request *mrq = host->mrq;
331 struct mmc_data *data;
332 u32 crc;
333
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200334 WARN_ON((host->status != HOST_S_DATA) && (host->status != HOST_S_STOP));
Pete Popovba264b32005-09-21 06:18:27 +0000335
336 if (host->mrq == NULL)
337 return;
338
339 data = mrq->cmd->data;
340
341 if (status == 0)
342 status = au_readl(HOST_STATUS(host));
343
344 /* The transaction is really over when the SD_STATUS_DB bit is clear */
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200345 while ((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB))
Pete Popovba264b32005-09-21 06:18:27 +0000346 status = au_readl(HOST_STATUS(host));
347
Pierre Ossman17b04292007-07-22 22:18:46 +0200348 data->error = 0;
Pete Popovba264b32005-09-21 06:18:27 +0000349 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir);
350
351 /* Process any errors */
Pete Popovba264b32005-09-21 06:18:27 +0000352 crc = (status & (SD_STATUS_WC | SD_STATUS_RC));
353 if (host->flags & HOST_F_XMIT)
354 crc |= ((status & 0x07) == 0x02) ? 0 : 1;
355
356 if (crc)
Pierre Ossman17b04292007-07-22 22:18:46 +0200357 data->error = -EILSEQ;
Pete Popovba264b32005-09-21 06:18:27 +0000358
359 /* Clear the CRC bits */
360 au_writel(SD_STATUS_WC | SD_STATUS_RC, HOST_STATUS(host));
361
362 data->bytes_xfered = 0;
363
Pierre Ossman17b04292007-07-22 22:18:46 +0200364 if (!data->error) {
Manuel Lauss1177d992011-08-02 19:51:07 +0200365 if (host->flags & (HOST_F_DMA | HOST_F_DBDMA)) {
Pete Popovba264b32005-09-21 06:18:27 +0000366 u32 chan = DMA_CHANNEL(host);
367
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200368 chan_tab_t *c = *((chan_tab_t **)chan);
Pete Popovba264b32005-09-21 06:18:27 +0000369 au1x_dma_chan_t *cp = c->chan_ptr;
370 data->bytes_xfered = cp->ddma_bytecnt;
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200371 } else
Pete Popovba264b32005-09-21 06:18:27 +0000372 data->bytes_xfered =
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200373 (data->blocks * data->blksz) - host->pio.len;
Pete Popovba264b32005-09-21 06:18:27 +0000374 }
375
376 au1xmmc_finish_request(host);
377}
378
379static void au1xmmc_tasklet_data(unsigned long param)
380{
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200381 struct au1xmmc_host *host = (struct au1xmmc_host *)param;
Pete Popovba264b32005-09-21 06:18:27 +0000382
383 u32 status = au_readl(HOST_STATUS(host));
384 au1xmmc_data_complete(host, status);
385}
386
387#define AU1XMMC_MAX_TRANSFER 8
388
389static void au1xmmc_send_pio(struct au1xmmc_host *host)
390{
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200391 struct mmc_data *data;
392 int sg_len, max, count;
393 unsigned char *sg_ptr, val;
394 u32 status;
Pete Popovba264b32005-09-21 06:18:27 +0000395 struct scatterlist *sg;
396
397 data = host->mrq->data;
398
399 if (!(host->flags & HOST_F_XMIT))
400 return;
401
402 /* This is the pointer to the data buffer */
403 sg = &data->sg[host->pio.index];
Jens Axboe45711f12007-10-22 21:19:53 +0200404 sg_ptr = sg_virt(sg) + host->pio.offset;
Pete Popovba264b32005-09-21 06:18:27 +0000405
406 /* This is the space left inside the buffer */
407 sg_len = data->sg[host->pio.index].length - host->pio.offset;
408
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200409 /* Check if we need less than the size of the sg_buffer */
Pete Popovba264b32005-09-21 06:18:27 +0000410 max = (sg_len > host->pio.len) ? host->pio.len : sg_len;
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200411 if (max > AU1XMMC_MAX_TRANSFER)
412 max = AU1XMMC_MAX_TRANSFER;
Pete Popovba264b32005-09-21 06:18:27 +0000413
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200414 for (count = 0; count < max; count++) {
Pete Popovba264b32005-09-21 06:18:27 +0000415 status = au_readl(HOST_STATUS(host));
416
417 if (!(status & SD_STATUS_TH))
418 break;
419
420 val = *sg_ptr++;
421
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200422 au_writel((unsigned long)val, HOST_TXPORT(host));
Pete Popovba264b32005-09-21 06:18:27 +0000423 au_sync();
424 }
425
426 host->pio.len -= count;
427 host->pio.offset += count;
428
429 if (count == sg_len) {
430 host->pio.index++;
431 host->pio.offset = 0;
432 }
433
434 if (host->pio.len == 0) {
435 IRQ_OFF(host, SD_CONFIG_TH);
436
437 if (host->flags & HOST_F_STOP)
438 SEND_STOP(host);
439
440 tasklet_schedule(&host->data_task);
441 }
442}
443
444static void au1xmmc_receive_pio(struct au1xmmc_host *host)
445{
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200446 struct mmc_data *data;
447 int max, count, sg_len = 0;
448 unsigned char *sg_ptr = NULL;
449 u32 status, val;
Pete Popovba264b32005-09-21 06:18:27 +0000450 struct scatterlist *sg;
451
452 data = host->mrq->data;
453
454 if (!(host->flags & HOST_F_RECV))
455 return;
456
457 max = host->pio.len;
458
459 if (host->pio.index < host->dma.len) {
460 sg = &data->sg[host->pio.index];
Jens Axboe45711f12007-10-22 21:19:53 +0200461 sg_ptr = sg_virt(sg) + host->pio.offset;
Pete Popovba264b32005-09-21 06:18:27 +0000462
463 /* This is the space left inside the buffer */
464 sg_len = sg_dma_len(&data->sg[host->pio.index]) - host->pio.offset;
465
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200466 /* Check if we need less than the size of the sg_buffer */
467 if (sg_len < max)
468 max = sg_len;
Pete Popovba264b32005-09-21 06:18:27 +0000469 }
470
471 if (max > AU1XMMC_MAX_TRANSFER)
472 max = AU1XMMC_MAX_TRANSFER;
473
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200474 for (count = 0; count < max; count++) {
Pete Popovba264b32005-09-21 06:18:27 +0000475 status = au_readl(HOST_STATUS(host));
476
477 if (!(status & SD_STATUS_NE))
478 break;
479
480 if (status & SD_STATUS_RC) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200481 DBG("RX CRC Error [%d + %d].\n", host->pdev->id,
Pete Popovba264b32005-09-21 06:18:27 +0000482 host->pio.len, count);
483 break;
484 }
485
486 if (status & SD_STATUS_RO) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200487 DBG("RX Overrun [%d + %d]\n", host->pdev->id,
Pete Popovba264b32005-09-21 06:18:27 +0000488 host->pio.len, count);
489 break;
490 }
491 else if (status & SD_STATUS_RU) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200492 DBG("RX Underrun [%d + %d]\n", host->pdev->id,
Pete Popovba264b32005-09-21 06:18:27 +0000493 host->pio.len, count);
494 break;
495 }
496
497 val = au_readl(HOST_RXPORT(host));
498
499 if (sg_ptr)
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200500 *sg_ptr++ = (unsigned char)(val & 0xFF);
Pete Popovba264b32005-09-21 06:18:27 +0000501 }
502
503 host->pio.len -= count;
504 host->pio.offset += count;
505
506 if (sg_len && count == sg_len) {
507 host->pio.index++;
508 host->pio.offset = 0;
509 }
510
511 if (host->pio.len == 0) {
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200512 /* IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF); */
Pete Popovba264b32005-09-21 06:18:27 +0000513 IRQ_OFF(host, SD_CONFIG_NE);
514
515 if (host->flags & HOST_F_STOP)
516 SEND_STOP(host);
517
518 tasklet_schedule(&host->data_task);
519 }
520}
521
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200522/* This is called when a command has been completed - grab the response
523 * and check for errors. Then start the data transfer if it is indicated.
524 */
Pete Popovba264b32005-09-21 06:18:27 +0000525static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
526{
Pete Popovba264b32005-09-21 06:18:27 +0000527 struct mmc_request *mrq = host->mrq;
528 struct mmc_command *cmd;
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200529 u32 r[4];
530 int i, trans;
Pete Popovba264b32005-09-21 06:18:27 +0000531
532 if (!host->mrq)
533 return;
534
535 cmd = mrq->cmd;
Pierre Ossman17b04292007-07-22 22:18:46 +0200536 cmd->error = 0;
Pete Popovba264b32005-09-21 06:18:27 +0000537
Russell Kinge9225172006-02-02 12:23:12 +0000538 if (cmd->flags & MMC_RSP_PRESENT) {
539 if (cmd->flags & MMC_RSP_136) {
Russell Kinge9225172006-02-02 12:23:12 +0000540 r[0] = au_readl(host->iobase + SD_RESP3);
541 r[1] = au_readl(host->iobase + SD_RESP2);
542 r[2] = au_readl(host->iobase + SD_RESP1);
543 r[3] = au_readl(host->iobase + SD_RESP0);
Pete Popovba264b32005-09-21 06:18:27 +0000544
Russell Kinge9225172006-02-02 12:23:12 +0000545 /* The CRC is omitted from the response, so really
546 * we only got 120 bytes, but the engine expects
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200547 * 128 bits, so we have to shift things up.
Russell Kinge9225172006-02-02 12:23:12 +0000548 */
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200549 for (i = 0; i < 4; i++) {
Russell Kinge9225172006-02-02 12:23:12 +0000550 cmd->resp[i] = (r[i] & 0x00FFFFFF) << 8;
551 if (i != 3)
552 cmd->resp[i] |= (r[i + 1] & 0xFF000000) >> 24;
553 }
554 } else {
555 /* Techincally, we should be getting all 48 bits of
556 * the response (SD_RESP1 + SD_RESP2), but because
557 * our response omits the CRC, our data ends up
558 * being shifted 8 bits to the right. In this case,
559 * that means that the OSR data starts at bit 31,
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200560 * so we can just read RESP0 and return that.
Russell Kinge9225172006-02-02 12:23:12 +0000561 */
562 cmd->resp[0] = au_readl(host->iobase + SD_RESP0);
Pete Popovba264b32005-09-21 06:18:27 +0000563 }
564 }
565
566 /* Figure out errors */
Pete Popovba264b32005-09-21 06:18:27 +0000567 if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC))
Pierre Ossman17b04292007-07-22 22:18:46 +0200568 cmd->error = -EILSEQ;
Pete Popovba264b32005-09-21 06:18:27 +0000569
570 trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);
571
Pierre Ossman17b04292007-07-22 22:18:46 +0200572 if (!trans || cmd->error) {
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200573 IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF);
Pete Popovba264b32005-09-21 06:18:27 +0000574 tasklet_schedule(&host->finish_task);
575 return;
576 }
577
578 host->status = HOST_S_DATA;
579
Manuel Lauss1177d992011-08-02 19:51:07 +0200580 if ((host->flags & (HOST_F_DMA | HOST_F_DBDMA))) {
Pete Popovba264b32005-09-21 06:18:27 +0000581 u32 channel = DMA_CHANNEL(host);
582
Manuel Lauss1177d992011-08-02 19:51:07 +0200583 /* Start the DBDMA as soon as the buffer gets something in it */
Pete Popovba264b32005-09-21 06:18:27 +0000584
585 if (host->flags & HOST_F_RECV) {
586 u32 mask = SD_STATUS_DB | SD_STATUS_NE;
587
588 while((status & mask) != mask)
589 status = au_readl(HOST_STATUS(host));
590 }
591
592 au1xxx_dbdma_start(channel);
593 }
594}
595
596static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate)
597{
Pete Popovba264b32005-09-21 06:18:27 +0000598 unsigned int pbus = get_au1x00_speed();
599 unsigned int divisor;
600 u32 config;
601
602 /* From databook:
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200603 * divisor = ((((cpuclock / sbus_divisor) / 2) / mmcclock) / 2) - 1
604 */
Manuel Lauss1d09de72014-07-23 16:36:24 +0200605 pbus /= ((alchemy_rdsys(AU1000_SYS_POWERCTRL) & 0x3) + 2);
Pete Popovba264b32005-09-21 06:18:27 +0000606 pbus /= 2;
Pete Popovba264b32005-09-21 06:18:27 +0000607 divisor = ((pbus / rate) / 2) - 1;
608
609 config = au_readl(HOST_CONFIG(host));
610
611 config &= ~(SD_CONFIG_DIV);
612 config |= (divisor & SD_CONFIG_DIV) | SD_CONFIG_DE;
613
614 au_writel(config, HOST_CONFIG(host));
615 au_sync();
616}
617
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200618static int au1xmmc_prepare_data(struct au1xmmc_host *host,
619 struct mmc_data *data)
Pete Popovba264b32005-09-21 06:18:27 +0000620{
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100621 int datalen = data->blocks * data->blksz;
Pete Popovba264b32005-09-21 06:18:27 +0000622
Pete Popovba264b32005-09-21 06:18:27 +0000623 if (data->flags & MMC_DATA_READ)
624 host->flags |= HOST_F_RECV;
625 else
626 host->flags |= HOST_F_XMIT;
627
628 if (host->mrq->stop)
629 host->flags |= HOST_F_STOP;
630
631 host->dma.dir = DMA_BIDIRECTIONAL;
632
633 host->dma.len = dma_map_sg(mmc_dev(host->mmc), data->sg,
634 data->sg_len, host->dma.dir);
635
636 if (host->dma.len == 0)
Pierre Ossman17b04292007-07-22 22:18:46 +0200637 return -ETIMEDOUT;
Pete Popovba264b32005-09-21 06:18:27 +0000638
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100639 au_writel(data->blksz - 1, HOST_BLKSIZE(host));
Pete Popovba264b32005-09-21 06:18:27 +0000640
Manuel Lauss1177d992011-08-02 19:51:07 +0200641 if (host->flags & (HOST_F_DMA | HOST_F_DBDMA)) {
Pete Popovba264b32005-09-21 06:18:27 +0000642 int i;
643 u32 channel = DMA_CHANNEL(host);
644
645 au1xxx_dbdma_stop(channel);
646
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200647 for (i = 0; i < host->dma.len; i++) {
Pete Popovba264b32005-09-21 06:18:27 +0000648 u32 ret = 0, flags = DDMA_FLAGS_NOIE;
649 struct scatterlist *sg = &data->sg[i];
650 int sg_len = sg->length;
651
652 int len = (datalen > sg_len) ? sg_len : datalen;
653
654 if (i == host->dma.len - 1)
655 flags = DDMA_FLAGS_IE;
656
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200657 if (host->flags & HOST_F_XMIT) {
Manuel Laussea071cc2009-10-13 20:22:34 +0200658 ret = au1xxx_dbdma_put_source(channel,
Manuel Lauss963accb2009-10-13 20:22:35 +0200659 sg_phys(sg), len, flags);
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200660 } else {
Manuel Laussea071cc2009-10-13 20:22:34 +0200661 ret = au1xxx_dbdma_put_dest(channel,
Manuel Lauss963accb2009-10-13 20:22:35 +0200662 sg_phys(sg), len, flags);
Pete Popovba264b32005-09-21 06:18:27 +0000663 }
664
Manuel Laussc4223c22008-06-09 08:36:13 +0200665 if (!ret)
Pete Popovba264b32005-09-21 06:18:27 +0000666 goto dataerr;
667
668 datalen -= len;
669 }
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200670 } else {
Pete Popovba264b32005-09-21 06:18:27 +0000671 host->pio.index = 0;
672 host->pio.offset = 0;
673 host->pio.len = datalen;
674
675 if (host->flags & HOST_F_XMIT)
676 IRQ_ON(host, SD_CONFIG_TH);
677 else
678 IRQ_ON(host, SD_CONFIG_NE);
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200679 /* IRQ_ON(host, SD_CONFIG_RA | SD_CONFIG_RF); */
Pete Popovba264b32005-09-21 06:18:27 +0000680 }
681
Pierre Ossman17b04292007-07-22 22:18:46 +0200682 return 0;
Pete Popovba264b32005-09-21 06:18:27 +0000683
Manuel Laussc4223c22008-06-09 08:36:13 +0200684dataerr:
685 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
686 host->dma.dir);
Pierre Ossman17b04292007-07-22 22:18:46 +0200687 return -ETIMEDOUT;
Pete Popovba264b32005-09-21 06:18:27 +0000688}
689
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200690/* This actually starts a command or data transaction */
Pete Popovba264b32005-09-21 06:18:27 +0000691static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
692{
Pete Popovba264b32005-09-21 06:18:27 +0000693 struct au1xmmc_host *host = mmc_priv(mmc);
Pierre Ossman17b04292007-07-22 22:18:46 +0200694 int ret = 0;
Pete Popovba264b32005-09-21 06:18:27 +0000695
696 WARN_ON(irqs_disabled());
697 WARN_ON(host->status != HOST_S_IDLE);
698
699 host->mrq = mrq;
700 host->status = HOST_S_CMD;
701
Manuel Lauss88b8d9a2008-06-09 08:39:11 +0200702 /* fail request immediately if no card is present */
Manuel Lausse2d26472008-06-27 18:25:18 +0200703 if (0 == au1xmmc_card_inserted(mmc)) {
Manuel Lauss88b8d9a2008-06-09 08:39:11 +0200704 mrq->cmd->error = -ENOMEDIUM;
705 au1xmmc_finish_request(host);
706 return;
707 }
708
Pete Popovba264b32005-09-21 06:18:27 +0000709 if (mrq->data) {
710 FLUSH_FIFO(host);
711 ret = au1xmmc_prepare_data(host, mrq->data);
712 }
713
Pierre Ossman17b04292007-07-22 22:18:46 +0200714 if (!ret)
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200715 ret = au1xmmc_send_command(host, 0, mrq->cmd, mrq->data);
Pete Popovba264b32005-09-21 06:18:27 +0000716
Pierre Ossman17b04292007-07-22 22:18:46 +0200717 if (ret) {
Pete Popovba264b32005-09-21 06:18:27 +0000718 mrq->cmd->error = ret;
719 au1xmmc_finish_request(host);
720 }
721}
722
723static void au1xmmc_reset_controller(struct au1xmmc_host *host)
724{
Pete Popovba264b32005-09-21 06:18:27 +0000725 /* Apply the clock */
726 au_writel(SD_ENABLE_CE, HOST_ENABLE(host));
727 au_sync_delay(1);
728
729 au_writel(SD_ENABLE_R | SD_ENABLE_CE, HOST_ENABLE(host));
730 au_sync_delay(5);
731
732 au_writel(~0, HOST_STATUS(host));
733 au_sync();
734
735 au_writel(0, HOST_BLKSIZE(host));
736 au_writel(0x001fffff, HOST_TIMEOUT(host));
737 au_sync();
738
739 au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
740 au_sync();
741
742 au_writel(SD_CONFIG2_EN | SD_CONFIG2_FF, HOST_CONFIG2(host));
743 au_sync_delay(1);
744
745 au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
746 au_sync();
747
748 /* Configure interrupts */
749 au_writel(AU1XMMC_INTERRUPTS, HOST_CONFIG(host));
750 au_sync();
751}
752
753
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200754static void au1xmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pete Popovba264b32005-09-21 06:18:27 +0000755{
756 struct au1xmmc_host *host = mmc_priv(mmc);
Manuel Lauss281dd232008-06-09 08:37:33 +0200757 u32 config2;
Pete Popovba264b32005-09-21 06:18:27 +0000758
Pete Popovba264b32005-09-21 06:18:27 +0000759 if (ios->power_mode == MMC_POWER_OFF)
760 au1xmmc_set_power(host, 0);
761 else if (ios->power_mode == MMC_POWER_ON) {
762 au1xmmc_set_power(host, 1);
763 }
764
765 if (ios->clock && ios->clock != host->clock) {
766 au1xmmc_set_clock(host, ios->clock);
767 host->clock = ios->clock;
768 }
Manuel Lauss281dd232008-06-09 08:37:33 +0200769
770 config2 = au_readl(HOST_CONFIG2(host));
771 switch (ios->bus_width) {
Manuel Lauss809f36c2011-11-01 20:03:30 +0100772 case MMC_BUS_WIDTH_8:
773 config2 |= SD_CONFIG2_BB;
774 break;
Manuel Lauss281dd232008-06-09 08:37:33 +0200775 case MMC_BUS_WIDTH_4:
Manuel Lauss809f36c2011-11-01 20:03:30 +0100776 config2 &= ~SD_CONFIG2_BB;
Manuel Lauss281dd232008-06-09 08:37:33 +0200777 config2 |= SD_CONFIG2_WB;
778 break;
779 case MMC_BUS_WIDTH_1:
Manuel Lauss809f36c2011-11-01 20:03:30 +0100780 config2 &= ~(SD_CONFIG2_WB | SD_CONFIG2_BB);
Manuel Lauss281dd232008-06-09 08:37:33 +0200781 break;
782 }
783 au_writel(config2, HOST_CONFIG2(host));
784 au_sync();
Pete Popovba264b32005-09-21 06:18:27 +0000785}
786
Manuel Laussc4223c22008-06-09 08:36:13 +0200787#define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
788#define STATUS_DATA_IN (SD_STATUS_NE)
789#define STATUS_DATA_OUT (SD_STATUS_TH)
790
791static irqreturn_t au1xmmc_irq(int irq, void *dev_id)
Pete Popovba264b32005-09-21 06:18:27 +0000792{
Manuel Laussc4223c22008-06-09 08:36:13 +0200793 struct au1xmmc_host *host = dev_id;
794 u32 status;
795
796 status = au_readl(HOST_STATUS(host));
797
798 if (!(status & SD_STATUS_I))
799 return IRQ_NONE; /* not ours */
800
Manuel Lauss20f522f2008-06-09 08:38:03 +0200801 if (status & SD_STATUS_SI) /* SDIO */
802 mmc_signal_sdio_irq(host->mmc);
803
Manuel Laussc4223c22008-06-09 08:36:13 +0200804 if (host->mrq && (status & STATUS_TIMEOUT)) {
805 if (status & SD_STATUS_RAT)
806 host->mrq->cmd->error = -ETIMEDOUT;
807 else if (status & SD_STATUS_DT)
808 host->mrq->data->error = -ETIMEDOUT;
809
810 /* In PIO mode, interrupts might still be enabled */
811 IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
812
813 /* IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF); */
814 tasklet_schedule(&host->finish_task);
815 }
816#if 0
817 else if (status & SD_STATUS_DD) {
818 /* Sometimes we get a DD before a NE in PIO mode */
819 if (!(host->flags & HOST_F_DMA) && (status & SD_STATUS_NE))
820 au1xmmc_receive_pio(host);
821 else {
822 au1xmmc_data_complete(host, status);
823 /* tasklet_schedule(&host->data_task); */
824 }
825 }
826#endif
827 else if (status & SD_STATUS_CR) {
828 if (host->status == HOST_S_CMD)
829 au1xmmc_cmd_complete(host, status);
830
831 } else if (!(host->flags & HOST_F_DMA)) {
832 if ((host->flags & HOST_F_XMIT) && (status & STATUS_DATA_OUT))
833 au1xmmc_send_pio(host);
834 else if ((host->flags & HOST_F_RECV) && (status & STATUS_DATA_IN))
835 au1xmmc_receive_pio(host);
836
837 } else if (status & 0x203F3C70) {
838 DBG("Unhandled status %8.8x\n", host->pdev->id,
839 status);
840 }
841
842 au_writel(status, HOST_STATUS(host));
843 au_sync();
844
845 return IRQ_HANDLED;
846}
847
Manuel Laussc4223c22008-06-09 08:36:13 +0200848/* 8bit memory DMA device */
849static dbdev_tab_t au1xmmc_mem_dbdev = {
850 .dev_id = DSCR_CMD0_ALWAYS,
851 .dev_flags = DEV_FLAGS_ANYUSE,
852 .dev_tsize = 0,
853 .dev_devwidth = 8,
854 .dev_physaddr = 0x00000000,
855 .dev_intlevel = 0,
856 .dev_intpolarity = 0,
857};
858static int memid;
859
860static void au1xmmc_dbdma_callback(int irq, void *dev_id)
861{
862 struct au1xmmc_host *host = (struct au1xmmc_host *)dev_id;
Pete Popovba264b32005-09-21 06:18:27 +0000863
864 /* Avoid spurious interrupts */
Pete Popovba264b32005-09-21 06:18:27 +0000865 if (!host->mrq)
866 return;
867
868 if (host->flags & HOST_F_STOP)
869 SEND_STOP(host);
870
871 tasklet_schedule(&host->data_task);
872}
873
Manuel Laussc4223c22008-06-09 08:36:13 +0200874static int au1xmmc_dbdma_init(struct au1xmmc_host *host)
Pete Popovba264b32005-09-21 06:18:27 +0000875{
Manuel Laussc4223c22008-06-09 08:36:13 +0200876 struct resource *res;
877 int txid, rxid;
Pete Popovba264b32005-09-21 06:18:27 +0000878
Manuel Laussc4223c22008-06-09 08:36:13 +0200879 res = platform_get_resource(host->pdev, IORESOURCE_DMA, 0);
880 if (!res)
881 return -ENODEV;
882 txid = res->start;
Pete Popovba264b32005-09-21 06:18:27 +0000883
Manuel Laussc4223c22008-06-09 08:36:13 +0200884 res = platform_get_resource(host->pdev, IORESOURCE_DMA, 1);
885 if (!res)
886 return -ENODEV;
887 rxid = res->start;
Pete Popovba264b32005-09-21 06:18:27 +0000888
Manuel Laussc4223c22008-06-09 08:36:13 +0200889 if (!memid)
890 return -ENODEV;
Pete Popovba264b32005-09-21 06:18:27 +0000891
Manuel Laussc4223c22008-06-09 08:36:13 +0200892 host->tx_chan = au1xxx_dbdma_chan_alloc(memid, txid,
893 au1xmmc_dbdma_callback, (void *)host);
894 if (!host->tx_chan) {
895 dev_err(&host->pdev->dev, "cannot allocate TX DMA\n");
896 return -ENODEV;
897 }
Pete Popovba264b32005-09-21 06:18:27 +0000898
Manuel Laussc4223c22008-06-09 08:36:13 +0200899 host->rx_chan = au1xxx_dbdma_chan_alloc(rxid, memid,
900 au1xmmc_dbdma_callback, (void *)host);
901 if (!host->rx_chan) {
902 dev_err(&host->pdev->dev, "cannot allocate RX DMA\n");
903 au1xxx_dbdma_chan_free(host->tx_chan);
904 return -ENODEV;
905 }
Pete Popovba264b32005-09-21 06:18:27 +0000906
Manuel Laussc4223c22008-06-09 08:36:13 +0200907 au1xxx_dbdma_set_devwidth(host->tx_chan, 8);
908 au1xxx_dbdma_set_devwidth(host->rx_chan, 8);
Pete Popovba264b32005-09-21 06:18:27 +0000909
Manuel Laussc4223c22008-06-09 08:36:13 +0200910 au1xxx_dbdma_ring_alloc(host->tx_chan, AU1XMMC_DESCRIPTOR_COUNT);
911 au1xxx_dbdma_ring_alloc(host->rx_chan, AU1XMMC_DESCRIPTOR_COUNT);
Pete Popovba264b32005-09-21 06:18:27 +0000912
Manuel Laussc4223c22008-06-09 08:36:13 +0200913 /* DBDMA is good to go */
Manuel Lauss1177d992011-08-02 19:51:07 +0200914 host->flags |= HOST_F_DMA | HOST_F_DBDMA;
Pete Popovba264b32005-09-21 06:18:27 +0000915
Manuel Laussc4223c22008-06-09 08:36:13 +0200916 return 0;
917}
Pete Popovba264b32005-09-21 06:18:27 +0000918
Manuel Laussc4223c22008-06-09 08:36:13 +0200919static void au1xmmc_dbdma_shutdown(struct au1xmmc_host *host)
920{
921 if (host->flags & HOST_F_DMA) {
922 host->flags &= ~HOST_F_DMA;
923 au1xxx_dbdma_chan_free(host->tx_chan);
924 au1xxx_dbdma_chan_free(host->rx_chan);
925 }
926}
Pete Popovba264b32005-09-21 06:18:27 +0000927
Manuel Lauss20f522f2008-06-09 08:38:03 +0200928static void au1xmmc_enable_sdio_irq(struct mmc_host *mmc, int en)
929{
930 struct au1xmmc_host *host = mmc_priv(mmc);
931
932 if (en)
933 IRQ_ON(host, SD_CONFIG_SI);
934 else
935 IRQ_OFF(host, SD_CONFIG_SI);
936}
937
Yoichi Yuasabf8c80a2006-12-05 07:43:38 +0100938static const struct mmc_host_ops au1xmmc_ops = {
Pete Popovba264b32005-09-21 06:18:27 +0000939 .request = au1xmmc_request,
940 .set_ios = au1xmmc_set_ios,
Manuel Lauss82999772007-01-25 10:29:24 +0100941 .get_ro = au1xmmc_card_readonly,
Manuel Lausse2d26472008-06-27 18:25:18 +0200942 .get_cd = au1xmmc_card_inserted,
Manuel Lauss20f522f2008-06-09 08:38:03 +0200943 .enable_sdio_irq = au1xmmc_enable_sdio_irq,
Pete Popovba264b32005-09-21 06:18:27 +0000944};
945
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500946static int au1xmmc_probe(struct platform_device *pdev)
Pete Popovba264b32005-09-21 06:18:27 +0000947{
Manuel Laussc4223c22008-06-09 08:36:13 +0200948 struct mmc_host *mmc;
949 struct au1xmmc_host *host;
950 struct resource *r;
Manuel Lauss809f36c2011-11-01 20:03:30 +0100951 int ret, iflag;
Pete Popovba264b32005-09-21 06:18:27 +0000952
Manuel Laussc4223c22008-06-09 08:36:13 +0200953 mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), &pdev->dev);
954 if (!mmc) {
955 dev_err(&pdev->dev, "no memory for mmc_host\n");
956 ret = -ENOMEM;
957 goto out0;
Pete Popovba264b32005-09-21 06:18:27 +0000958 }
959
Manuel Laussc4223c22008-06-09 08:36:13 +0200960 host = mmc_priv(mmc);
961 host->mmc = mmc;
962 host->platdata = pdev->dev.platform_data;
963 host->pdev = pdev;
Pete Popovba264b32005-09-21 06:18:27 +0000964
Manuel Laussc4223c22008-06-09 08:36:13 +0200965 ret = -ENODEV;
966 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
967 if (!r) {
968 dev_err(&pdev->dev, "no mmio defined\n");
969 goto out1;
970 }
Pete Popovba264b32005-09-21 06:18:27 +0000971
H Hartley Sweeten7a5ea56a2009-12-14 14:28:06 -0500972 host->ioarea = request_mem_region(r->start, resource_size(r),
Manuel Laussc4223c22008-06-09 08:36:13 +0200973 pdev->name);
974 if (!host->ioarea) {
975 dev_err(&pdev->dev, "mmio already in use\n");
976 goto out1;
977 }
978
979 host->iobase = (unsigned long)ioremap(r->start, 0x3c);
980 if (!host->iobase) {
981 dev_err(&pdev->dev, "cannot remap mmio\n");
982 goto out2;
983 }
984
985 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
986 if (!r) {
987 dev_err(&pdev->dev, "no IRQ defined\n");
988 goto out3;
989 }
Manuel Laussc4223c22008-06-09 08:36:13 +0200990 host->irq = r->start;
Manuel Laussc4223c22008-06-09 08:36:13 +0200991
992 mmc->ops = &au1xmmc_ops;
993
994 mmc->f_min = 450000;
995 mmc->f_max = 24000000;
996
Manuel Laussc4223c22008-06-09 08:36:13 +0200997 mmc->max_blk_size = 2048;
998 mmc->max_blk_count = 512;
999
1000 mmc->ocr_avail = AU1XMMC_OCR;
Manuel Lauss20f522f2008-06-09 08:38:03 +02001001 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
Manuel Lauss809f36c2011-11-01 20:03:30 +01001002 mmc->max_segs = AU1XMMC_DESCRIPTOR_COUNT;
1003
1004 iflag = IRQF_SHARED; /* Au1100/Au1200: one int for both ctrls */
1005
1006 switch (alchemy_get_cputype()) {
1007 case ALCHEMY_CPU_AU1100:
1008 mmc->max_seg_size = AU1100_MMC_DESCRIPTOR_SIZE;
1009 break;
1010 case ALCHEMY_CPU_AU1200:
1011 mmc->max_seg_size = AU1200_MMC_DESCRIPTOR_SIZE;
1012 break;
1013 case ALCHEMY_CPU_AU1300:
1014 iflag = 0; /* nothing is shared */
1015 mmc->max_seg_size = AU1200_MMC_DESCRIPTOR_SIZE;
1016 mmc->f_max = 52000000;
1017 if (host->ioarea->start == AU1100_SD0_PHYS_ADDR)
1018 mmc->caps |= MMC_CAP_8_BIT_DATA;
1019 break;
1020 }
1021
1022 ret = request_irq(host->irq, au1xmmc_irq, iflag, DRIVER_NAME, host);
1023 if (ret) {
1024 dev_err(&pdev->dev, "cannot grab IRQ\n");
1025 goto out3;
1026 }
Manuel Laussc4223c22008-06-09 08:36:13 +02001027
1028 host->status = HOST_S_IDLE;
1029
1030 /* board-specific carddetect setup, if any */
1031 if (host->platdata && host->platdata->cd_setup) {
1032 ret = host->platdata->cd_setup(mmc, 1);
1033 if (ret) {
Manuel Lausse2d26472008-06-27 18:25:18 +02001034 dev_warn(&pdev->dev, "board CD setup failed\n");
1035 mmc->caps |= MMC_CAP_NEEDS_POLL;
Pete Popovba264b32005-09-21 06:18:27 +00001036 }
Manuel Lausse2d26472008-06-27 18:25:18 +02001037 } else
1038 mmc->caps |= MMC_CAP_NEEDS_POLL;
Pete Popovba264b32005-09-21 06:18:27 +00001039
Manuel Lauss3b839072009-10-14 09:38:06 +02001040 /* platform may not be able to use all advertised caps */
1041 if (host->platdata)
1042 mmc->caps &= ~(host->platdata->mask_host_caps);
1043
Manuel Laussc4223c22008-06-09 08:36:13 +02001044 tasklet_init(&host->data_task, au1xmmc_tasklet_data,
1045 (unsigned long)host);
Pete Popovba264b32005-09-21 06:18:27 +00001046
Manuel Laussc4223c22008-06-09 08:36:13 +02001047 tasklet_init(&host->finish_task, au1xmmc_tasklet_finish,
1048 (unsigned long)host);
Pete Popovba264b32005-09-21 06:18:27 +00001049
Manuel Lauss1177d992011-08-02 19:51:07 +02001050 if (has_dbdma()) {
1051 ret = au1xmmc_dbdma_init(host);
1052 if (ret)
Linus Torvaldsd6748062011-11-03 13:28:14 -07001053 pr_info(DRIVER_NAME ": DBDMA init failed; using PIO\n");
Manuel Lauss1177d992011-08-02 19:51:07 +02001054 }
Pete Popovba264b32005-09-21 06:18:27 +00001055
Manuel Laussc4223c22008-06-09 08:36:13 +02001056#ifdef CONFIG_LEDS_CLASS
1057 if (host->platdata && host->platdata->led) {
1058 struct led_classdev *led = host->platdata->led;
1059 led->name = mmc_hostname(mmc);
1060 led->brightness = LED_OFF;
1061 led->default_trigger = mmc_hostname(mmc);
1062 ret = led_classdev_register(mmc_dev(mmc), led);
1063 if (ret)
1064 goto out5;
1065 }
1066#endif
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001067
Manuel Laussc4223c22008-06-09 08:36:13 +02001068 au1xmmc_reset_controller(host);
Pete Popovba264b32005-09-21 06:18:27 +00001069
Manuel Laussc4223c22008-06-09 08:36:13 +02001070 ret = mmc_add_host(mmc);
1071 if (ret) {
1072 dev_err(&pdev->dev, "cannot add mmc host\n");
1073 goto out6;
1074 }
Pete Popovba264b32005-09-21 06:18:27 +00001075
Manuel Laussdd8572a2008-07-17 13:07:28 +02001076 platform_set_drvdata(pdev, host);
Pete Popovba264b32005-09-21 06:18:27 +00001077
Girish K Sa3c76eb2011-10-11 11:44:09 +05301078 pr_info(DRIVER_NAME ": MMC Controller %d set up at %8.8X"
Manuel Laussc4223c22008-06-09 08:36:13 +02001079 " (mode=%s)\n", pdev->id, host->iobase,
1080 host->flags & HOST_F_DMA ? "dma" : "pio");
Pete Popovba264b32005-09-21 06:18:27 +00001081
Manuel Laussc4223c22008-06-09 08:36:13 +02001082 return 0; /* all ok */
Pete Popovba264b32005-09-21 06:18:27 +00001083
Manuel Laussc4223c22008-06-09 08:36:13 +02001084out6:
1085#ifdef CONFIG_LEDS_CLASS
1086 if (host->platdata && host->platdata->led)
1087 led_classdev_unregister(host->platdata->led);
1088out5:
1089#endif
1090 au_writel(0, HOST_ENABLE(host));
1091 au_writel(0, HOST_CONFIG(host));
1092 au_writel(0, HOST_CONFIG2(host));
1093 au_sync();
1094
Manuel Lauss1177d992011-08-02 19:51:07 +02001095 if (host->flags & HOST_F_DBDMA)
1096 au1xmmc_dbdma_shutdown(host);
Manuel Laussc4223c22008-06-09 08:36:13 +02001097
1098 tasklet_kill(&host->data_task);
1099 tasklet_kill(&host->finish_task);
1100
Manuel Lausse2d26472008-06-27 18:25:18 +02001101 if (host->platdata && host->platdata->cd_setup &&
1102 !(mmc->caps & MMC_CAP_NEEDS_POLL))
Manuel Laussc4223c22008-06-09 08:36:13 +02001103 host->platdata->cd_setup(mmc, 0);
Manuel Lausse2d26472008-06-27 18:25:18 +02001104
Manuel Laussc4223c22008-06-09 08:36:13 +02001105 free_irq(host->irq, host);
1106out3:
1107 iounmap((void *)host->iobase);
1108out2:
1109 release_resource(host->ioarea);
1110 kfree(host->ioarea);
1111out1:
1112 mmc_free_host(mmc);
1113out0:
1114 return ret;
Pete Popovba264b32005-09-21 06:18:27 +00001115}
1116
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001117static int au1xmmc_remove(struct platform_device *pdev)
Pete Popovba264b32005-09-21 06:18:27 +00001118{
Manuel Laussdd8572a2008-07-17 13:07:28 +02001119 struct au1xmmc_host *host = platform_get_drvdata(pdev);
Pete Popovba264b32005-09-21 06:18:27 +00001120
Manuel Laussdd8572a2008-07-17 13:07:28 +02001121 if (host) {
1122 mmc_remove_host(host->mmc);
Pete Popovba264b32005-09-21 06:18:27 +00001123
Manuel Laussc4223c22008-06-09 08:36:13 +02001124#ifdef CONFIG_LEDS_CLASS
1125 if (host->platdata && host->platdata->led)
1126 led_classdev_unregister(host->platdata->led);
1127#endif
1128
Manuel Lausse2d26472008-06-27 18:25:18 +02001129 if (host->platdata && host->platdata->cd_setup &&
Manuel Laussdd8572a2008-07-17 13:07:28 +02001130 !(host->mmc->caps & MMC_CAP_NEEDS_POLL))
1131 host->platdata->cd_setup(host->mmc, 0);
Manuel Laussc4223c22008-06-09 08:36:13 +02001132
1133 au_writel(0, HOST_ENABLE(host));
1134 au_writel(0, HOST_CONFIG(host));
1135 au_writel(0, HOST_CONFIG2(host));
1136 au_sync();
Pete Popovba264b32005-09-21 06:18:27 +00001137
1138 tasklet_kill(&host->data_task);
1139 tasklet_kill(&host->finish_task);
1140
Manuel Lauss1177d992011-08-02 19:51:07 +02001141 if (host->flags & HOST_F_DBDMA)
1142 au1xmmc_dbdma_shutdown(host);
1143
Pete Popovba264b32005-09-21 06:18:27 +00001144 au1xmmc_set_power(host, 0);
1145
Manuel Laussc4223c22008-06-09 08:36:13 +02001146 free_irq(host->irq, host);
1147 iounmap((void *)host->iobase);
1148 release_resource(host->ioarea);
1149 kfree(host->ioarea);
Pete Popovba264b32005-09-21 06:18:27 +00001150
Manuel Laussdd8572a2008-07-17 13:07:28 +02001151 mmc_free_host(host->mmc);
Pete Popovba264b32005-09-21 06:18:27 +00001152 }
Pete Popovba264b32005-09-21 06:18:27 +00001153 return 0;
1154}
1155
Manuel Laussdd8572a2008-07-17 13:07:28 +02001156#ifdef CONFIG_PM
1157static int au1xmmc_suspend(struct platform_device *pdev, pm_message_t state)
1158{
1159 struct au1xmmc_host *host = platform_get_drvdata(pdev);
Manuel Laussdd8572a2008-07-17 13:07:28 +02001160
1161 au_writel(0, HOST_CONFIG2(host));
1162 au_writel(0, HOST_CONFIG(host));
1163 au_writel(0xffffffff, HOST_STATUS(host));
1164 au_writel(0, HOST_ENABLE(host));
1165 au_sync();
1166
1167 return 0;
1168}
1169
1170static int au1xmmc_resume(struct platform_device *pdev)
1171{
1172 struct au1xmmc_host *host = platform_get_drvdata(pdev);
1173
1174 au1xmmc_reset_controller(host);
1175
Ulf Hansson1e63d482013-09-25 10:55:23 +02001176 return 0;
Manuel Laussdd8572a2008-07-17 13:07:28 +02001177}
1178#else
1179#define au1xmmc_suspend NULL
1180#define au1xmmc_resume NULL
1181#endif
1182
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001183static struct platform_driver au1xmmc_driver = {
Pete Popovba264b32005-09-21 06:18:27 +00001184 .probe = au1xmmc_probe,
1185 .remove = au1xmmc_remove,
Manuel Laussdd8572a2008-07-17 13:07:28 +02001186 .suspend = au1xmmc_suspend,
1187 .resume = au1xmmc_resume,
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001188 .driver = {
1189 .name = DRIVER_NAME,
Kay Sieversbc65c722008-04-15 14:34:28 -07001190 .owner = THIS_MODULE,
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001191 },
Pete Popovba264b32005-09-21 06:18:27 +00001192};
1193
1194static int __init au1xmmc_init(void)
1195{
Manuel Lauss1177d992011-08-02 19:51:07 +02001196 if (has_dbdma()) {
1197 /* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride
1198 * of 8 bits. And since devices are shared, we need to create
1199 * our own to avoid freaking out other devices.
1200 */
1201 memid = au1xxx_ddma_add_device(&au1xmmc_mem_dbdev);
1202 if (!memid)
Linus Torvaldsd6748062011-11-03 13:28:14 -07001203 pr_err("au1xmmc: cannot add memory dbdma\n");
Manuel Lauss1177d992011-08-02 19:51:07 +02001204 }
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001205 return platform_driver_register(&au1xmmc_driver);
Pete Popovba264b32005-09-21 06:18:27 +00001206}
1207
1208static void __exit au1xmmc_exit(void)
1209{
Manuel Lauss1177d992011-08-02 19:51:07 +02001210 if (has_dbdma() && memid)
Manuel Laussc4223c22008-06-09 08:36:13 +02001211 au1xxx_ddma_del_device(memid);
Manuel Lauss1177d992011-08-02 19:51:07 +02001212
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001213 platform_driver_unregister(&au1xmmc_driver);
Pete Popovba264b32005-09-21 06:18:27 +00001214}
1215
1216module_init(au1xmmc_init);
1217module_exit(au1xmmc_exit);
1218
Pete Popovba264b32005-09-21 06:18:27 +00001219MODULE_AUTHOR("Advanced Micro Devices, Inc");
1220MODULE_DESCRIPTION("MMC/SD driver for the Alchemy Au1XXX");
1221MODULE_LICENSE("GPL");
Kay Sieversbc65c722008-04-15 14:34:28 -07001222MODULE_ALIAS("platform:au1xxx-mmc");