blob: 9b4be67330dd2fa5cefb0cf139b77f989c2f0383 [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
Manuel Laussb6507592014-07-23 16:36:56 +020035#include <linux/clk.h>
Pete Popovba264b32005-09-21 06:18:27 +000036#include <linux/module.h>
37#include <linux/init.h>
Martin Michlmayrb256f9d2006-03-04 23:01:13 +000038#include <linux/platform_device.h>
Pete Popovba264b32005-09-21 06:18:27 +000039#include <linux/mm.h>
40#include <linux/interrupt.h>
41#include <linux/dma-mapping.h>
Al Viro0ada7a02007-10-27 19:40:46 +010042#include <linux/scatterlist.h>
Christoph Hellwiga6720c02018-05-28 08:14:21 +020043#include <linux/highmem.h>
Manuel Laussc4223c22008-06-09 08:36:13 +020044#include <linux/leds.h>
Pete Popovba264b32005-09-21 06:18:27 +000045#include <linux/mmc/host.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Manuel Laussc4223c22008-06-09 08:36:13 +020047
Pete Popovba264b32005-09-21 06:18:27 +000048#include <asm/io.h>
49#include <asm/mach-au1x00/au1000.h>
50#include <asm/mach-au1x00/au1xxx_dbdma.h>
51#include <asm/mach-au1x00/au1100_mmc.h>
Pete Popovba264b32005-09-21 06:18:27 +000052
Pete Popovba264b32005-09-21 06:18:27 +000053#define DRIVER_NAME "au1xxx-mmc"
54
55/* Set this to enable special debugging macros */
Manuel Laussc4223c22008-06-09 08:36:13 +020056/* #define DEBUG */
Pete Popovba264b32005-09-21 06:18:27 +000057
Russell Kingc6563172006-03-29 09:30:20 +010058#ifdef DEBUG
Manuel Lauss5c0a8892008-06-09 08:38:35 +020059#define DBG(fmt, idx, args...) \
Girish K Sa3c76eb2011-10-11 11:44:09 +053060 pr_debug("au1xmmc(%d): DEBUG: " fmt, idx, ##args)
Pete Popovba264b32005-09-21 06:18:27 +000061#else
Manuel Lauss5c0a8892008-06-09 08:38:35 +020062#define DBG(fmt, idx, args...) do {} while (0)
Pete Popovba264b32005-09-21 06:18:27 +000063#endif
64
Manuel Lauss5c0a8892008-06-09 08:38:35 +020065/* Hardware definitions */
66#define AU1XMMC_DESCRIPTOR_COUNT 1
Manuel Lausse491d232008-07-29 10:10:49 +020067
68/* max DMA seg size: 64KB on Au1100, 4MB on Au1200 */
Manuel Lauss1177d992011-08-02 19:51:07 +020069#define AU1100_MMC_DESCRIPTOR_SIZE 0x0000ffff
70#define AU1200_MMC_DESCRIPTOR_SIZE 0x003fffff
Manuel Lauss5c0a8892008-06-09 08:38:35 +020071
72#define AU1XMMC_OCR (MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 | \
73 MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 | \
74 MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36)
75
76/* This gives us a hard value for the stop command that we can write directly
77 * to the command register.
78 */
79#define STOP_CMD \
80 (SD_CMD_RT_1B | SD_CMD_CT_7 | (0xC << SD_CMD_CI_SHIFT) | SD_CMD_GO)
81
82/* This is the set of interrupts that we configure by default. */
83#define AU1XMMC_INTERRUPTS \
84 (SD_CONFIG_SC | SD_CONFIG_DT | SD_CONFIG_RAT | \
85 SD_CONFIG_CR | SD_CONFIG_I)
86
87/* The poll event (looking for insert/remove events runs twice a second. */
88#define AU1XMMC_DETECT_TIMEOUT (HZ/2)
89
90struct au1xmmc_host {
91 struct mmc_host *mmc;
92 struct mmc_request *mrq;
93
94 u32 flags;
Manuel Lauss2f73bfb2014-07-23 16:36:26 +020095 void __iomem *iobase;
Manuel Lauss5c0a8892008-06-09 08:38:35 +020096 u32 clock;
97 u32 bus_width;
98 u32 power_mode;
99
100 int status;
101
102 struct {
103 int len;
104 int dir;
105 } dma;
106
107 struct {
108 int index;
109 int offset;
110 int len;
111 } pio;
112
113 u32 tx_chan;
114 u32 rx_chan;
115
116 int irq;
117
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200118 struct tasklet_struct finish_task;
119 struct tasklet_struct data_task;
120 struct au1xmmc_platform_data *platdata;
121 struct platform_device *pdev;
122 struct resource *ioarea;
Manuel Laussb6507592014-07-23 16:36:56 +0200123 struct clk *clk;
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200124};
125
126/* Status flags used by the host structure */
127#define HOST_F_XMIT 0x0001
128#define HOST_F_RECV 0x0002
129#define HOST_F_DMA 0x0010
Manuel Lauss1177d992011-08-02 19:51:07 +0200130#define HOST_F_DBDMA 0x0020
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200131#define HOST_F_ACTIVE 0x0100
132#define HOST_F_STOP 0x1000
133
134#define HOST_S_IDLE 0x0001
135#define HOST_S_CMD 0x0002
136#define HOST_S_DATA 0x0003
137#define HOST_S_STOP 0x0004
138
139/* Easy access macros */
140#define HOST_STATUS(h) ((h)->iobase + SD_STATUS)
141#define HOST_CONFIG(h) ((h)->iobase + SD_CONFIG)
142#define HOST_ENABLE(h) ((h)->iobase + SD_ENABLE)
143#define HOST_TXPORT(h) ((h)->iobase + SD_TXPORT)
144#define HOST_RXPORT(h) ((h)->iobase + SD_RXPORT)
145#define HOST_CMDARG(h) ((h)->iobase + SD_CMDARG)
146#define HOST_BLKSIZE(h) ((h)->iobase + SD_BLKSIZE)
147#define HOST_CMD(h) ((h)->iobase + SD_CMD)
148#define HOST_CONFIG2(h) ((h)->iobase + SD_CONFIG2)
149#define HOST_TIMEOUT(h) ((h)->iobase + SD_TIMEOUT)
150#define HOST_DEBUG(h) ((h)->iobase + SD_DEBUG)
151
152#define DMA_CHANNEL(h) \
153 (((h)->flags & HOST_F_XMIT) ? (h)->tx_chan : (h)->rx_chan)
154
Manuel Lauss1177d992011-08-02 19:51:07 +0200155static inline int has_dbdma(void)
156{
157 switch (alchemy_get_cputype()) {
158 case ALCHEMY_CPU_AU1200:
Manuel Lauss809f36c2011-11-01 20:03:30 +0100159 case ALCHEMY_CPU_AU1300:
Manuel Lauss1177d992011-08-02 19:51:07 +0200160 return 1;
161 default:
162 return 0;
163 }
164}
165
Pete Popovba264b32005-09-21 06:18:27 +0000166static inline void IRQ_ON(struct au1xmmc_host *host, u32 mask)
167{
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200168 u32 val = __raw_readl(HOST_CONFIG(host));
Pete Popovba264b32005-09-21 06:18:27 +0000169 val |= mask;
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200170 __raw_writel(val, HOST_CONFIG(host));
171 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000172}
173
174static inline void FLUSH_FIFO(struct au1xmmc_host *host)
175{
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200176 u32 val = __raw_readl(HOST_CONFIG2(host));
Pete Popovba264b32005-09-21 06:18:27 +0000177
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200178 __raw_writel(val | SD_CONFIG2_FF, HOST_CONFIG2(host));
179 wmb(); /* drain writebuffer */
180 mdelay(1);
Pete Popovba264b32005-09-21 06:18:27 +0000181
182 /* SEND_STOP will turn off clock control - this re-enables it */
183 val &= ~SD_CONFIG2_DF;
184
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200185 __raw_writel(val, HOST_CONFIG2(host));
186 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000187}
188
189static inline void IRQ_OFF(struct au1xmmc_host *host, u32 mask)
190{
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200191 u32 val = __raw_readl(HOST_CONFIG(host));
Pete Popovba264b32005-09-21 06:18:27 +0000192 val &= ~mask;
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200193 __raw_writel(val, HOST_CONFIG(host));
194 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000195}
196
197static inline void SEND_STOP(struct au1xmmc_host *host)
198{
Manuel Lauss281dd232008-06-09 08:37:33 +0200199 u32 config2;
Pete Popovba264b32005-09-21 06:18:27 +0000200
201 WARN_ON(host->status != HOST_S_DATA);
202 host->status = HOST_S_STOP;
203
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200204 config2 = __raw_readl(HOST_CONFIG2(host));
205 __raw_writel(config2 | SD_CONFIG2_DF, HOST_CONFIG2(host));
206 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000207
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400208 /* Send the stop command */
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200209 __raw_writel(STOP_CMD, HOST_CMD(host));
210 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000211}
212
213static void au1xmmc_set_power(struct au1xmmc_host *host, int state)
214{
Manuel Laussc4223c22008-06-09 08:36:13 +0200215 if (host->platdata && host->platdata->set_power)
216 host->platdata->set_power(host->mmc, state);
Pete Popovba264b32005-09-21 06:18:27 +0000217}
218
Manuel Lausse2d26472008-06-27 18:25:18 +0200219static int au1xmmc_card_inserted(struct mmc_host *mmc)
Pete Popovba264b32005-09-21 06:18:27 +0000220{
Manuel Lausse2d26472008-06-27 18:25:18 +0200221 struct au1xmmc_host *host = mmc_priv(mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200222
223 if (host->platdata && host->platdata->card_inserted)
Manuel Lausse2d26472008-06-27 18:25:18 +0200224 return !!host->platdata->card_inserted(host->mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200225
Manuel Lausse2d26472008-06-27 18:25:18 +0200226 return -ENOSYS;
Pete Popovba264b32005-09-21 06:18:27 +0000227}
228
Manuel Lauss82999772007-01-25 10:29:24 +0100229static int au1xmmc_card_readonly(struct mmc_host *mmc)
Pete Popovba264b32005-09-21 06:18:27 +0000230{
Manuel Lauss82999772007-01-25 10:29:24 +0100231 struct au1xmmc_host *host = mmc_priv(mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200232
233 if (host->platdata && host->platdata->card_readonly)
Manuel Lausse2d26472008-06-27 18:25:18 +0200234 return !!host->platdata->card_readonly(mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200235
Manuel Lausse2d26472008-06-27 18:25:18 +0200236 return -ENOSYS;
Pete Popovba264b32005-09-21 06:18:27 +0000237}
238
239static void au1xmmc_finish_request(struct au1xmmc_host *host)
240{
Pete Popovba264b32005-09-21 06:18:27 +0000241 struct mmc_request *mrq = host->mrq;
242
243 host->mrq = NULL;
Manuel Laussc4223c22008-06-09 08:36:13 +0200244 host->flags &= HOST_F_ACTIVE | HOST_F_DMA;
Pete Popovba264b32005-09-21 06:18:27 +0000245
246 host->dma.len = 0;
247 host->dma.dir = 0;
248
249 host->pio.index = 0;
250 host->pio.offset = 0;
251 host->pio.len = 0;
252
253 host->status = HOST_S_IDLE;
254
Pete Popovba264b32005-09-21 06:18:27 +0000255 mmc_request_done(host->mmc, mrq);
256}
257
258static void au1xmmc_tasklet_finish(unsigned long param)
259{
260 struct au1xmmc_host *host = (struct au1xmmc_host *) param;
261 au1xmmc_finish_request(host);
262}
263
264static int au1xmmc_send_command(struct au1xmmc_host *host, int wait,
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200265 struct mmc_command *cmd, struct mmc_data *data)
Pete Popovba264b32005-09-21 06:18:27 +0000266{
Pete Popovba264b32005-09-21 06:18:27 +0000267 u32 mmccmd = (cmd->opcode << SD_CMD_CI_SHIFT);
268
Martin Michlmayre142c242006-03-04 23:01:39 +0000269 switch (mmc_resp_type(cmd)) {
Manuel Lauss279bc442007-01-25 10:27:41 +0100270 case MMC_RSP_NONE:
271 break;
Pete Popovba264b32005-09-21 06:18:27 +0000272 case MMC_RSP_R1:
273 mmccmd |= SD_CMD_RT_1;
274 break;
275 case MMC_RSP_R1B:
276 mmccmd |= SD_CMD_RT_1B;
277 break;
278 case MMC_RSP_R2:
279 mmccmd |= SD_CMD_RT_2;
280 break;
281 case MMC_RSP_R3:
282 mmccmd |= SD_CMD_RT_3;
283 break;
Manuel Lauss279bc442007-01-25 10:27:41 +0100284 default:
Girish K Sa3c76eb2011-10-11 11:44:09 +0530285 pr_info("au1xmmc: unhandled response type %02x\n",
Manuel Lauss279bc442007-01-25 10:27:41 +0100286 mmc_resp_type(cmd));
Pierre Ossman17b04292007-07-22 22:18:46 +0200287 return -EINVAL;
Pete Popovba264b32005-09-21 06:18:27 +0000288 }
289
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200290 if (data) {
Pierre Ossman6356a9d2007-10-22 18:16:16 +0200291 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200292 if (data->blocks > 1)
293 mmccmd |= SD_CMD_CT_4;
294 else
295 mmccmd |= SD_CMD_CT_2;
Pierre Ossman6356a9d2007-10-22 18:16:16 +0200296 } else if (data->flags & MMC_DATA_WRITE) {
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200297 if (data->blocks > 1)
298 mmccmd |= SD_CMD_CT_3;
299 else
300 mmccmd |= SD_CMD_CT_1;
301 }
Pete Popovba264b32005-09-21 06:18:27 +0000302 }
303
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200304 __raw_writel(cmd->arg, HOST_CMDARG(host));
305 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000306
307 if (wait)
308 IRQ_OFF(host, SD_CONFIG_CR);
309
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200310 __raw_writel((mmccmd | SD_CMD_GO), HOST_CMD(host));
311 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000312
313 /* Wait for the command to go on the line */
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200314 while (__raw_readl(HOST_CMD(host)) & SD_CMD_GO)
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200315 /* nop */;
Pete Popovba264b32005-09-21 06:18:27 +0000316
317 /* Wait for the command to come back */
Pete Popovba264b32005-09-21 06:18:27 +0000318 if (wait) {
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200319 u32 status = __raw_readl(HOST_STATUS(host));
Pete Popovba264b32005-09-21 06:18:27 +0000320
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200321 while (!(status & SD_STATUS_CR))
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200322 status = __raw_readl(HOST_STATUS(host));
Pete Popovba264b32005-09-21 06:18:27 +0000323
324 /* Clear the CR status */
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200325 __raw_writel(SD_STATUS_CR, HOST_STATUS(host));
Pete Popovba264b32005-09-21 06:18:27 +0000326
327 IRQ_ON(host, SD_CONFIG_CR);
328 }
329
Pierre Ossman17b04292007-07-22 22:18:46 +0200330 return 0;
Pete Popovba264b32005-09-21 06:18:27 +0000331}
332
333static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
334{
Pete Popovba264b32005-09-21 06:18:27 +0000335 struct mmc_request *mrq = host->mrq;
336 struct mmc_data *data;
337 u32 crc;
338
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200339 WARN_ON((host->status != HOST_S_DATA) && (host->status != HOST_S_STOP));
Pete Popovba264b32005-09-21 06:18:27 +0000340
341 if (host->mrq == NULL)
342 return;
343
344 data = mrq->cmd->data;
345
346 if (status == 0)
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200347 status = __raw_readl(HOST_STATUS(host));
Pete Popovba264b32005-09-21 06:18:27 +0000348
349 /* The transaction is really over when the SD_STATUS_DB bit is clear */
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200350 while ((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB))
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200351 status = __raw_readl(HOST_STATUS(host));
Pete Popovba264b32005-09-21 06:18:27 +0000352
Pierre Ossman17b04292007-07-22 22:18:46 +0200353 data->error = 0;
Pete Popovba264b32005-09-21 06:18:27 +0000354 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir);
355
356 /* Process any errors */
Pete Popovba264b32005-09-21 06:18:27 +0000357 crc = (status & (SD_STATUS_WC | SD_STATUS_RC));
358 if (host->flags & HOST_F_XMIT)
359 crc |= ((status & 0x07) == 0x02) ? 0 : 1;
360
361 if (crc)
Pierre Ossman17b04292007-07-22 22:18:46 +0200362 data->error = -EILSEQ;
Pete Popovba264b32005-09-21 06:18:27 +0000363
364 /* Clear the CRC bits */
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200365 __raw_writel(SD_STATUS_WC | SD_STATUS_RC, HOST_STATUS(host));
Pete Popovba264b32005-09-21 06:18:27 +0000366
367 data->bytes_xfered = 0;
368
Pierre Ossman17b04292007-07-22 22:18:46 +0200369 if (!data->error) {
Manuel Lauss1177d992011-08-02 19:51:07 +0200370 if (host->flags & (HOST_F_DMA | HOST_F_DBDMA)) {
Pete Popovba264b32005-09-21 06:18:27 +0000371 u32 chan = DMA_CHANNEL(host);
372
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200373 chan_tab_t *c = *((chan_tab_t **)chan);
Pete Popovba264b32005-09-21 06:18:27 +0000374 au1x_dma_chan_t *cp = c->chan_ptr;
375 data->bytes_xfered = cp->ddma_bytecnt;
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200376 } else
Pete Popovba264b32005-09-21 06:18:27 +0000377 data->bytes_xfered =
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200378 (data->blocks * data->blksz) - host->pio.len;
Pete Popovba264b32005-09-21 06:18:27 +0000379 }
380
381 au1xmmc_finish_request(host);
382}
383
384static void au1xmmc_tasklet_data(unsigned long param)
385{
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200386 struct au1xmmc_host *host = (struct au1xmmc_host *)param;
Pete Popovba264b32005-09-21 06:18:27 +0000387
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200388 u32 status = __raw_readl(HOST_STATUS(host));
Pete Popovba264b32005-09-21 06:18:27 +0000389 au1xmmc_data_complete(host, status);
390}
391
392#define AU1XMMC_MAX_TRANSFER 8
393
394static void au1xmmc_send_pio(struct au1xmmc_host *host)
395{
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200396 struct mmc_data *data;
397 int sg_len, max, count;
398 unsigned char *sg_ptr, val;
399 u32 status;
Pete Popovba264b32005-09-21 06:18:27 +0000400 struct scatterlist *sg;
401
402 data = host->mrq->data;
403
404 if (!(host->flags & HOST_F_XMIT))
405 return;
406
407 /* This is the pointer to the data buffer */
408 sg = &data->sg[host->pio.index];
Christoph Hellwiga6720c02018-05-28 08:14:21 +0200409 sg_ptr = kmap_atomic(sg_page(sg)) + sg->offset + host->pio.offset;
Pete Popovba264b32005-09-21 06:18:27 +0000410
411 /* This is the space left inside the buffer */
412 sg_len = data->sg[host->pio.index].length - host->pio.offset;
413
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200414 /* Check if we need less than the size of the sg_buffer */
Pete Popovba264b32005-09-21 06:18:27 +0000415 max = (sg_len > host->pio.len) ? host->pio.len : sg_len;
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200416 if (max > AU1XMMC_MAX_TRANSFER)
417 max = AU1XMMC_MAX_TRANSFER;
Pete Popovba264b32005-09-21 06:18:27 +0000418
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200419 for (count = 0; count < max; count++) {
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200420 status = __raw_readl(HOST_STATUS(host));
Pete Popovba264b32005-09-21 06:18:27 +0000421
422 if (!(status & SD_STATUS_TH))
423 break;
424
Christoph Hellwiga6720c02018-05-28 08:14:21 +0200425 val = sg_ptr[count];
Pete Popovba264b32005-09-21 06:18:27 +0000426
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200427 __raw_writel((unsigned long)val, HOST_TXPORT(host));
428 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000429 }
Christoph Hellwiga6720c02018-05-28 08:14:21 +0200430 kunmap_atomic(sg_ptr);
Pete Popovba264b32005-09-21 06:18:27 +0000431
432 host->pio.len -= count;
433 host->pio.offset += count;
434
435 if (count == sg_len) {
436 host->pio.index++;
437 host->pio.offset = 0;
438 }
439
440 if (host->pio.len == 0) {
441 IRQ_OFF(host, SD_CONFIG_TH);
442
443 if (host->flags & HOST_F_STOP)
444 SEND_STOP(host);
445
446 tasklet_schedule(&host->data_task);
447 }
448}
449
450static void au1xmmc_receive_pio(struct au1xmmc_host *host)
451{
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200452 struct mmc_data *data;
453 int max, count, sg_len = 0;
454 unsigned char *sg_ptr = NULL;
455 u32 status, val;
Pete Popovba264b32005-09-21 06:18:27 +0000456 struct scatterlist *sg;
457
458 data = host->mrq->data;
459
460 if (!(host->flags & HOST_F_RECV))
461 return;
462
463 max = host->pio.len;
464
465 if (host->pio.index < host->dma.len) {
466 sg = &data->sg[host->pio.index];
Christoph Hellwiga6720c02018-05-28 08:14:21 +0200467 sg_ptr = kmap_atomic(sg_page(sg)) + sg->offset + host->pio.offset;
Pete Popovba264b32005-09-21 06:18:27 +0000468
469 /* This is the space left inside the buffer */
470 sg_len = sg_dma_len(&data->sg[host->pio.index]) - host->pio.offset;
471
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200472 /* Check if we need less than the size of the sg_buffer */
473 if (sg_len < max)
474 max = sg_len;
Pete Popovba264b32005-09-21 06:18:27 +0000475 }
476
477 if (max > AU1XMMC_MAX_TRANSFER)
478 max = AU1XMMC_MAX_TRANSFER;
479
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200480 for (count = 0; count < max; count++) {
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200481 status = __raw_readl(HOST_STATUS(host));
Pete Popovba264b32005-09-21 06:18:27 +0000482
483 if (!(status & SD_STATUS_NE))
484 break;
485
486 if (status & SD_STATUS_RC) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200487 DBG("RX CRC Error [%d + %d].\n", host->pdev->id,
Pete Popovba264b32005-09-21 06:18:27 +0000488 host->pio.len, count);
489 break;
490 }
491
492 if (status & SD_STATUS_RO) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200493 DBG("RX Overrun [%d + %d]\n", host->pdev->id,
Pete Popovba264b32005-09-21 06:18:27 +0000494 host->pio.len, count);
495 break;
496 }
497 else if (status & SD_STATUS_RU) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200498 DBG("RX Underrun [%d + %d]\n", host->pdev->id,
Pete Popovba264b32005-09-21 06:18:27 +0000499 host->pio.len, count);
500 break;
501 }
502
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200503 val = __raw_readl(HOST_RXPORT(host));
Pete Popovba264b32005-09-21 06:18:27 +0000504
505 if (sg_ptr)
Christoph Hellwiga6720c02018-05-28 08:14:21 +0200506 sg_ptr[count] = (unsigned char)(val & 0xFF);
Pete Popovba264b32005-09-21 06:18:27 +0000507 }
Christoph Hellwiga6720c02018-05-28 08:14:21 +0200508 if (sg_ptr)
509 kunmap_atomic(sg_ptr);
Pete Popovba264b32005-09-21 06:18:27 +0000510
511 host->pio.len -= count;
512 host->pio.offset += count;
513
514 if (sg_len && count == sg_len) {
515 host->pio.index++;
516 host->pio.offset = 0;
517 }
518
519 if (host->pio.len == 0) {
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200520 /* IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF); */
Pete Popovba264b32005-09-21 06:18:27 +0000521 IRQ_OFF(host, SD_CONFIG_NE);
522
523 if (host->flags & HOST_F_STOP)
524 SEND_STOP(host);
525
526 tasklet_schedule(&host->data_task);
527 }
528}
529
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200530/* This is called when a command has been completed - grab the response
531 * and check for errors. Then start the data transfer if it is indicated.
532 */
Pete Popovba264b32005-09-21 06:18:27 +0000533static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
534{
Pete Popovba264b32005-09-21 06:18:27 +0000535 struct mmc_request *mrq = host->mrq;
536 struct mmc_command *cmd;
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200537 u32 r[4];
538 int i, trans;
Pete Popovba264b32005-09-21 06:18:27 +0000539
540 if (!host->mrq)
541 return;
542
543 cmd = mrq->cmd;
Pierre Ossman17b04292007-07-22 22:18:46 +0200544 cmd->error = 0;
Pete Popovba264b32005-09-21 06:18:27 +0000545
Russell Kinge9225172006-02-02 12:23:12 +0000546 if (cmd->flags & MMC_RSP_PRESENT) {
547 if (cmd->flags & MMC_RSP_136) {
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200548 r[0] = __raw_readl(host->iobase + SD_RESP3);
549 r[1] = __raw_readl(host->iobase + SD_RESP2);
550 r[2] = __raw_readl(host->iobase + SD_RESP1);
551 r[3] = __raw_readl(host->iobase + SD_RESP0);
Pete Popovba264b32005-09-21 06:18:27 +0000552
Russell Kinge9225172006-02-02 12:23:12 +0000553 /* The CRC is omitted from the response, so really
554 * we only got 120 bytes, but the engine expects
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200555 * 128 bits, so we have to shift things up.
Russell Kinge9225172006-02-02 12:23:12 +0000556 */
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200557 for (i = 0; i < 4; i++) {
Russell Kinge9225172006-02-02 12:23:12 +0000558 cmd->resp[i] = (r[i] & 0x00FFFFFF) << 8;
559 if (i != 3)
560 cmd->resp[i] |= (r[i + 1] & 0xFF000000) >> 24;
561 }
562 } else {
563 /* Techincally, we should be getting all 48 bits of
564 * the response (SD_RESP1 + SD_RESP2), but because
565 * our response omits the CRC, our data ends up
566 * being shifted 8 bits to the right. In this case,
567 * that means that the OSR data starts at bit 31,
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200568 * so we can just read RESP0 and return that.
Russell Kinge9225172006-02-02 12:23:12 +0000569 */
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200570 cmd->resp[0] = __raw_readl(host->iobase + SD_RESP0);
Pete Popovba264b32005-09-21 06:18:27 +0000571 }
572 }
573
574 /* Figure out errors */
Pete Popovba264b32005-09-21 06:18:27 +0000575 if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC))
Pierre Ossman17b04292007-07-22 22:18:46 +0200576 cmd->error = -EILSEQ;
Pete Popovba264b32005-09-21 06:18:27 +0000577
578 trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);
579
Pierre Ossman17b04292007-07-22 22:18:46 +0200580 if (!trans || cmd->error) {
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200581 IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF);
Pete Popovba264b32005-09-21 06:18:27 +0000582 tasklet_schedule(&host->finish_task);
583 return;
584 }
585
586 host->status = HOST_S_DATA;
587
Manuel Lauss1177d992011-08-02 19:51:07 +0200588 if ((host->flags & (HOST_F_DMA | HOST_F_DBDMA))) {
Pete Popovba264b32005-09-21 06:18:27 +0000589 u32 channel = DMA_CHANNEL(host);
590
Manuel Lauss1177d992011-08-02 19:51:07 +0200591 /* Start the DBDMA as soon as the buffer gets something in it */
Pete Popovba264b32005-09-21 06:18:27 +0000592
593 if (host->flags & HOST_F_RECV) {
594 u32 mask = SD_STATUS_DB | SD_STATUS_NE;
595
596 while((status & mask) != mask)
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200597 status = __raw_readl(HOST_STATUS(host));
Pete Popovba264b32005-09-21 06:18:27 +0000598 }
599
600 au1xxx_dbdma_start(channel);
601 }
602}
603
604static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate)
605{
Manuel Laussb6507592014-07-23 16:36:56 +0200606 unsigned int pbus = clk_get_rate(host->clk);
607 unsigned int divisor = ((pbus / rate) / 2) - 1;
Pete Popovba264b32005-09-21 06:18:27 +0000608 u32 config;
609
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200610 config = __raw_readl(HOST_CONFIG(host));
Pete Popovba264b32005-09-21 06:18:27 +0000611
612 config &= ~(SD_CONFIG_DIV);
613 config |= (divisor & SD_CONFIG_DIV) | SD_CONFIG_DE;
614
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200615 __raw_writel(config, HOST_CONFIG(host));
616 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000617}
618
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200619static int au1xmmc_prepare_data(struct au1xmmc_host *host,
620 struct mmc_data *data)
Pete Popovba264b32005-09-21 06:18:27 +0000621{
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100622 int datalen = data->blocks * data->blksz;
Pete Popovba264b32005-09-21 06:18:27 +0000623
Pete Popovba264b32005-09-21 06:18:27 +0000624 if (data->flags & MMC_DATA_READ)
625 host->flags |= HOST_F_RECV;
626 else
627 host->flags |= HOST_F_XMIT;
628
629 if (host->mrq->stop)
630 host->flags |= HOST_F_STOP;
631
632 host->dma.dir = DMA_BIDIRECTIONAL;
633
634 host->dma.len = dma_map_sg(mmc_dev(host->mmc), data->sg,
635 data->sg_len, host->dma.dir);
636
637 if (host->dma.len == 0)
Pierre Ossman17b04292007-07-22 22:18:46 +0200638 return -ETIMEDOUT;
Pete Popovba264b32005-09-21 06:18:27 +0000639
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200640 __raw_writel(data->blksz - 1, HOST_BLKSIZE(host));
Pete Popovba264b32005-09-21 06:18:27 +0000641
Manuel Lauss1177d992011-08-02 19:51:07 +0200642 if (host->flags & (HOST_F_DMA | HOST_F_DBDMA)) {
Pete Popovba264b32005-09-21 06:18:27 +0000643 int i;
644 u32 channel = DMA_CHANNEL(host);
645
646 au1xxx_dbdma_stop(channel);
647
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200648 for (i = 0; i < host->dma.len; i++) {
Pete Popovba264b32005-09-21 06:18:27 +0000649 u32 ret = 0, flags = DDMA_FLAGS_NOIE;
650 struct scatterlist *sg = &data->sg[i];
651 int sg_len = sg->length;
652
653 int len = (datalen > sg_len) ? sg_len : datalen;
654
655 if (i == host->dma.len - 1)
656 flags = DDMA_FLAGS_IE;
657
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200658 if (host->flags & HOST_F_XMIT) {
Manuel Laussea071cc2009-10-13 20:22:34 +0200659 ret = au1xxx_dbdma_put_source(channel,
Manuel Lauss963accb2009-10-13 20:22:35 +0200660 sg_phys(sg), len, flags);
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200661 } else {
Manuel Laussea071cc2009-10-13 20:22:34 +0200662 ret = au1xxx_dbdma_put_dest(channel,
Manuel Lauss963accb2009-10-13 20:22:35 +0200663 sg_phys(sg), len, flags);
Pete Popovba264b32005-09-21 06:18:27 +0000664 }
665
Manuel Laussc4223c22008-06-09 08:36:13 +0200666 if (!ret)
Pete Popovba264b32005-09-21 06:18:27 +0000667 goto dataerr;
668
669 datalen -= len;
670 }
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200671 } else {
Pete Popovba264b32005-09-21 06:18:27 +0000672 host->pio.index = 0;
673 host->pio.offset = 0;
674 host->pio.len = datalen;
675
676 if (host->flags & HOST_F_XMIT)
677 IRQ_ON(host, SD_CONFIG_TH);
678 else
679 IRQ_ON(host, SD_CONFIG_NE);
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200680 /* IRQ_ON(host, SD_CONFIG_RA | SD_CONFIG_RF); */
Pete Popovba264b32005-09-21 06:18:27 +0000681 }
682
Pierre Ossman17b04292007-07-22 22:18:46 +0200683 return 0;
Pete Popovba264b32005-09-21 06:18:27 +0000684
Manuel Laussc4223c22008-06-09 08:36:13 +0200685dataerr:
686 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
687 host->dma.dir);
Pierre Ossman17b04292007-07-22 22:18:46 +0200688 return -ETIMEDOUT;
Pete Popovba264b32005-09-21 06:18:27 +0000689}
690
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200691/* This actually starts a command or data transaction */
Pete Popovba264b32005-09-21 06:18:27 +0000692static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
693{
Pete Popovba264b32005-09-21 06:18:27 +0000694 struct au1xmmc_host *host = mmc_priv(mmc);
Pierre Ossman17b04292007-07-22 22:18:46 +0200695 int ret = 0;
Pete Popovba264b32005-09-21 06:18:27 +0000696
697 WARN_ON(irqs_disabled());
698 WARN_ON(host->status != HOST_S_IDLE);
699
700 host->mrq = mrq;
701 host->status = HOST_S_CMD;
702
Manuel Lauss88b8d9a2008-06-09 08:39:11 +0200703 /* fail request immediately if no card is present */
Manuel Lausse2d26472008-06-27 18:25:18 +0200704 if (0 == au1xmmc_card_inserted(mmc)) {
Manuel Lauss88b8d9a2008-06-09 08:39:11 +0200705 mrq->cmd->error = -ENOMEDIUM;
706 au1xmmc_finish_request(host);
707 return;
708 }
709
Pete Popovba264b32005-09-21 06:18:27 +0000710 if (mrq->data) {
711 FLUSH_FIFO(host);
712 ret = au1xmmc_prepare_data(host, mrq->data);
713 }
714
Pierre Ossman17b04292007-07-22 22:18:46 +0200715 if (!ret)
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200716 ret = au1xmmc_send_command(host, 0, mrq->cmd, mrq->data);
Pete Popovba264b32005-09-21 06:18:27 +0000717
Pierre Ossman17b04292007-07-22 22:18:46 +0200718 if (ret) {
Pete Popovba264b32005-09-21 06:18:27 +0000719 mrq->cmd->error = ret;
720 au1xmmc_finish_request(host);
721 }
722}
723
724static void au1xmmc_reset_controller(struct au1xmmc_host *host)
725{
Pete Popovba264b32005-09-21 06:18:27 +0000726 /* Apply the clock */
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200727 __raw_writel(SD_ENABLE_CE, HOST_ENABLE(host));
728 wmb(); /* drain writebuffer */
729 mdelay(1);
Pete Popovba264b32005-09-21 06:18:27 +0000730
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200731 __raw_writel(SD_ENABLE_R | SD_ENABLE_CE, HOST_ENABLE(host));
732 wmb(); /* drain writebuffer */
733 mdelay(5);
Pete Popovba264b32005-09-21 06:18:27 +0000734
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200735 __raw_writel(~0, HOST_STATUS(host));
736 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000737
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200738 __raw_writel(0, HOST_BLKSIZE(host));
739 __raw_writel(0x001fffff, HOST_TIMEOUT(host));
740 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000741
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200742 __raw_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
743 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000744
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200745 __raw_writel(SD_CONFIG2_EN | SD_CONFIG2_FF, HOST_CONFIG2(host));
746 wmb(); /* drain writebuffer */
747 mdelay(1);
Pete Popovba264b32005-09-21 06:18:27 +0000748
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200749 __raw_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
750 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000751
752 /* Configure interrupts */
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200753 __raw_writel(AU1XMMC_INTERRUPTS, HOST_CONFIG(host));
754 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000755}
756
757
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200758static void au1xmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pete Popovba264b32005-09-21 06:18:27 +0000759{
760 struct au1xmmc_host *host = mmc_priv(mmc);
Manuel Lauss281dd232008-06-09 08:37:33 +0200761 u32 config2;
Pete Popovba264b32005-09-21 06:18:27 +0000762
Pete Popovba264b32005-09-21 06:18:27 +0000763 if (ios->power_mode == MMC_POWER_OFF)
764 au1xmmc_set_power(host, 0);
765 else if (ios->power_mode == MMC_POWER_ON) {
766 au1xmmc_set_power(host, 1);
767 }
768
769 if (ios->clock && ios->clock != host->clock) {
770 au1xmmc_set_clock(host, ios->clock);
771 host->clock = ios->clock;
772 }
Manuel Lauss281dd232008-06-09 08:37:33 +0200773
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200774 config2 = __raw_readl(HOST_CONFIG2(host));
Manuel Lauss281dd232008-06-09 08:37:33 +0200775 switch (ios->bus_width) {
Manuel Lauss809f36c2011-11-01 20:03:30 +0100776 case MMC_BUS_WIDTH_8:
777 config2 |= SD_CONFIG2_BB;
778 break;
Manuel Lauss281dd232008-06-09 08:37:33 +0200779 case MMC_BUS_WIDTH_4:
Manuel Lauss809f36c2011-11-01 20:03:30 +0100780 config2 &= ~SD_CONFIG2_BB;
Manuel Lauss281dd232008-06-09 08:37:33 +0200781 config2 |= SD_CONFIG2_WB;
782 break;
783 case MMC_BUS_WIDTH_1:
Manuel Lauss809f36c2011-11-01 20:03:30 +0100784 config2 &= ~(SD_CONFIG2_WB | SD_CONFIG2_BB);
Manuel Lauss281dd232008-06-09 08:37:33 +0200785 break;
786 }
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200787 __raw_writel(config2, HOST_CONFIG2(host));
788 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000789}
790
Manuel Laussc4223c22008-06-09 08:36:13 +0200791#define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
792#define STATUS_DATA_IN (SD_STATUS_NE)
793#define STATUS_DATA_OUT (SD_STATUS_TH)
794
795static irqreturn_t au1xmmc_irq(int irq, void *dev_id)
Pete Popovba264b32005-09-21 06:18:27 +0000796{
Manuel Laussc4223c22008-06-09 08:36:13 +0200797 struct au1xmmc_host *host = dev_id;
798 u32 status;
799
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200800 status = __raw_readl(HOST_STATUS(host));
Manuel Laussc4223c22008-06-09 08:36:13 +0200801
802 if (!(status & SD_STATUS_I))
803 return IRQ_NONE; /* not ours */
804
Manuel Lauss20f522f2008-06-09 08:38:03 +0200805 if (status & SD_STATUS_SI) /* SDIO */
806 mmc_signal_sdio_irq(host->mmc);
807
Manuel Laussc4223c22008-06-09 08:36:13 +0200808 if (host->mrq && (status & STATUS_TIMEOUT)) {
809 if (status & SD_STATUS_RAT)
810 host->mrq->cmd->error = -ETIMEDOUT;
811 else if (status & SD_STATUS_DT)
812 host->mrq->data->error = -ETIMEDOUT;
813
814 /* In PIO mode, interrupts might still be enabled */
815 IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
816
817 /* IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF); */
818 tasklet_schedule(&host->finish_task);
819 }
820#if 0
821 else if (status & SD_STATUS_DD) {
822 /* Sometimes we get a DD before a NE in PIO mode */
823 if (!(host->flags & HOST_F_DMA) && (status & SD_STATUS_NE))
824 au1xmmc_receive_pio(host);
825 else {
826 au1xmmc_data_complete(host, status);
827 /* tasklet_schedule(&host->data_task); */
828 }
829 }
830#endif
831 else if (status & SD_STATUS_CR) {
832 if (host->status == HOST_S_CMD)
833 au1xmmc_cmd_complete(host, status);
834
835 } else if (!(host->flags & HOST_F_DMA)) {
836 if ((host->flags & HOST_F_XMIT) && (status & STATUS_DATA_OUT))
837 au1xmmc_send_pio(host);
838 else if ((host->flags & HOST_F_RECV) && (status & STATUS_DATA_IN))
839 au1xmmc_receive_pio(host);
840
841 } else if (status & 0x203F3C70) {
842 DBG("Unhandled status %8.8x\n", host->pdev->id,
843 status);
844 }
845
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200846 __raw_writel(status, HOST_STATUS(host));
847 wmb(); /* drain writebuffer */
Manuel Laussc4223c22008-06-09 08:36:13 +0200848
849 return IRQ_HANDLED;
850}
851
Manuel Laussc4223c22008-06-09 08:36:13 +0200852/* 8bit memory DMA device */
853static dbdev_tab_t au1xmmc_mem_dbdev = {
854 .dev_id = DSCR_CMD0_ALWAYS,
855 .dev_flags = DEV_FLAGS_ANYUSE,
856 .dev_tsize = 0,
857 .dev_devwidth = 8,
858 .dev_physaddr = 0x00000000,
859 .dev_intlevel = 0,
860 .dev_intpolarity = 0,
861};
862static int memid;
863
864static void au1xmmc_dbdma_callback(int irq, void *dev_id)
865{
866 struct au1xmmc_host *host = (struct au1xmmc_host *)dev_id;
Pete Popovba264b32005-09-21 06:18:27 +0000867
868 /* Avoid spurious interrupts */
Pete Popovba264b32005-09-21 06:18:27 +0000869 if (!host->mrq)
870 return;
871
872 if (host->flags & HOST_F_STOP)
873 SEND_STOP(host);
874
875 tasklet_schedule(&host->data_task);
876}
877
Manuel Laussc4223c22008-06-09 08:36:13 +0200878static int au1xmmc_dbdma_init(struct au1xmmc_host *host)
Pete Popovba264b32005-09-21 06:18:27 +0000879{
Manuel Laussc4223c22008-06-09 08:36:13 +0200880 struct resource *res;
881 int txid, rxid;
Pete Popovba264b32005-09-21 06:18:27 +0000882
Manuel Laussc4223c22008-06-09 08:36:13 +0200883 res = platform_get_resource(host->pdev, IORESOURCE_DMA, 0);
884 if (!res)
885 return -ENODEV;
886 txid = res->start;
Pete Popovba264b32005-09-21 06:18:27 +0000887
Manuel Laussc4223c22008-06-09 08:36:13 +0200888 res = platform_get_resource(host->pdev, IORESOURCE_DMA, 1);
889 if (!res)
890 return -ENODEV;
891 rxid = res->start;
Pete Popovba264b32005-09-21 06:18:27 +0000892
Manuel Laussc4223c22008-06-09 08:36:13 +0200893 if (!memid)
894 return -ENODEV;
Pete Popovba264b32005-09-21 06:18:27 +0000895
Manuel Laussc4223c22008-06-09 08:36:13 +0200896 host->tx_chan = au1xxx_dbdma_chan_alloc(memid, txid,
897 au1xmmc_dbdma_callback, (void *)host);
898 if (!host->tx_chan) {
899 dev_err(&host->pdev->dev, "cannot allocate TX DMA\n");
900 return -ENODEV;
901 }
Pete Popovba264b32005-09-21 06:18:27 +0000902
Manuel Laussc4223c22008-06-09 08:36:13 +0200903 host->rx_chan = au1xxx_dbdma_chan_alloc(rxid, memid,
904 au1xmmc_dbdma_callback, (void *)host);
905 if (!host->rx_chan) {
906 dev_err(&host->pdev->dev, "cannot allocate RX DMA\n");
907 au1xxx_dbdma_chan_free(host->tx_chan);
908 return -ENODEV;
909 }
Pete Popovba264b32005-09-21 06:18:27 +0000910
Manuel Laussc4223c22008-06-09 08:36:13 +0200911 au1xxx_dbdma_set_devwidth(host->tx_chan, 8);
912 au1xxx_dbdma_set_devwidth(host->rx_chan, 8);
Pete Popovba264b32005-09-21 06:18:27 +0000913
Manuel Laussc4223c22008-06-09 08:36:13 +0200914 au1xxx_dbdma_ring_alloc(host->tx_chan, AU1XMMC_DESCRIPTOR_COUNT);
915 au1xxx_dbdma_ring_alloc(host->rx_chan, AU1XMMC_DESCRIPTOR_COUNT);
Pete Popovba264b32005-09-21 06:18:27 +0000916
Manuel Laussc4223c22008-06-09 08:36:13 +0200917 /* DBDMA is good to go */
Manuel Lauss1177d992011-08-02 19:51:07 +0200918 host->flags |= HOST_F_DMA | HOST_F_DBDMA;
Pete Popovba264b32005-09-21 06:18:27 +0000919
Manuel Laussc4223c22008-06-09 08:36:13 +0200920 return 0;
921}
Pete Popovba264b32005-09-21 06:18:27 +0000922
Manuel Laussc4223c22008-06-09 08:36:13 +0200923static void au1xmmc_dbdma_shutdown(struct au1xmmc_host *host)
924{
925 if (host->flags & HOST_F_DMA) {
926 host->flags &= ~HOST_F_DMA;
927 au1xxx_dbdma_chan_free(host->tx_chan);
928 au1xxx_dbdma_chan_free(host->rx_chan);
929 }
930}
Pete Popovba264b32005-09-21 06:18:27 +0000931
Manuel Lauss20f522f2008-06-09 08:38:03 +0200932static void au1xmmc_enable_sdio_irq(struct mmc_host *mmc, int en)
933{
934 struct au1xmmc_host *host = mmc_priv(mmc);
935
936 if (en)
937 IRQ_ON(host, SD_CONFIG_SI);
938 else
939 IRQ_OFF(host, SD_CONFIG_SI);
940}
941
Yoichi Yuasabf8c80a2006-12-05 07:43:38 +0100942static const struct mmc_host_ops au1xmmc_ops = {
Pete Popovba264b32005-09-21 06:18:27 +0000943 .request = au1xmmc_request,
944 .set_ios = au1xmmc_set_ios,
Manuel Lauss82999772007-01-25 10:29:24 +0100945 .get_ro = au1xmmc_card_readonly,
Manuel Lausse2d26472008-06-27 18:25:18 +0200946 .get_cd = au1xmmc_card_inserted,
Manuel Lauss20f522f2008-06-09 08:38:03 +0200947 .enable_sdio_irq = au1xmmc_enable_sdio_irq,
Pete Popovba264b32005-09-21 06:18:27 +0000948};
949
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500950static int au1xmmc_probe(struct platform_device *pdev)
Pete Popovba264b32005-09-21 06:18:27 +0000951{
Manuel Laussc4223c22008-06-09 08:36:13 +0200952 struct mmc_host *mmc;
953 struct au1xmmc_host *host;
954 struct resource *r;
Manuel Lauss809f36c2011-11-01 20:03:30 +0100955 int ret, iflag;
Pete Popovba264b32005-09-21 06:18:27 +0000956
Manuel Laussc4223c22008-06-09 08:36:13 +0200957 mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), &pdev->dev);
958 if (!mmc) {
959 dev_err(&pdev->dev, "no memory for mmc_host\n");
960 ret = -ENOMEM;
961 goto out0;
Pete Popovba264b32005-09-21 06:18:27 +0000962 }
963
Manuel Laussc4223c22008-06-09 08:36:13 +0200964 host = mmc_priv(mmc);
965 host->mmc = mmc;
966 host->platdata = pdev->dev.platform_data;
967 host->pdev = pdev;
Pete Popovba264b32005-09-21 06:18:27 +0000968
Manuel Laussc4223c22008-06-09 08:36:13 +0200969 ret = -ENODEV;
970 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
971 if (!r) {
972 dev_err(&pdev->dev, "no mmio defined\n");
973 goto out1;
974 }
Pete Popovba264b32005-09-21 06:18:27 +0000975
H Hartley Sweeten7a5ea56a2009-12-14 14:28:06 -0500976 host->ioarea = request_mem_region(r->start, resource_size(r),
Manuel Laussc4223c22008-06-09 08:36:13 +0200977 pdev->name);
978 if (!host->ioarea) {
979 dev_err(&pdev->dev, "mmio already in use\n");
980 goto out1;
981 }
982
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200983 host->iobase = ioremap(r->start, 0x3c);
Manuel Laussc4223c22008-06-09 08:36:13 +0200984 if (!host->iobase) {
985 dev_err(&pdev->dev, "cannot remap mmio\n");
986 goto out2;
987 }
988
989 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
990 if (!r) {
991 dev_err(&pdev->dev, "no IRQ defined\n");
992 goto out3;
993 }
Manuel Laussc4223c22008-06-09 08:36:13 +0200994 host->irq = r->start;
Manuel Laussc4223c22008-06-09 08:36:13 +0200995
996 mmc->ops = &au1xmmc_ops;
997
998 mmc->f_min = 450000;
999 mmc->f_max = 24000000;
1000
Manuel Laussc4223c22008-06-09 08:36:13 +02001001 mmc->max_blk_size = 2048;
1002 mmc->max_blk_count = 512;
1003
1004 mmc->ocr_avail = AU1XMMC_OCR;
Manuel Lauss20f522f2008-06-09 08:38:03 +02001005 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
Manuel Lauss809f36c2011-11-01 20:03:30 +01001006 mmc->max_segs = AU1XMMC_DESCRIPTOR_COUNT;
1007
1008 iflag = IRQF_SHARED; /* Au1100/Au1200: one int for both ctrls */
1009
1010 switch (alchemy_get_cputype()) {
1011 case ALCHEMY_CPU_AU1100:
1012 mmc->max_seg_size = AU1100_MMC_DESCRIPTOR_SIZE;
1013 break;
1014 case ALCHEMY_CPU_AU1200:
1015 mmc->max_seg_size = AU1200_MMC_DESCRIPTOR_SIZE;
1016 break;
1017 case ALCHEMY_CPU_AU1300:
1018 iflag = 0; /* nothing is shared */
1019 mmc->max_seg_size = AU1200_MMC_DESCRIPTOR_SIZE;
1020 mmc->f_max = 52000000;
1021 if (host->ioarea->start == AU1100_SD0_PHYS_ADDR)
1022 mmc->caps |= MMC_CAP_8_BIT_DATA;
1023 break;
1024 }
1025
1026 ret = request_irq(host->irq, au1xmmc_irq, iflag, DRIVER_NAME, host);
1027 if (ret) {
1028 dev_err(&pdev->dev, "cannot grab IRQ\n");
1029 goto out3;
1030 }
Manuel Laussc4223c22008-06-09 08:36:13 +02001031
Manuel Laussb6507592014-07-23 16:36:56 +02001032 host->clk = clk_get(&pdev->dev, ALCHEMY_PERIPH_CLK);
1033 if (IS_ERR(host->clk)) {
1034 dev_err(&pdev->dev, "cannot find clock\n");
Wei Yongjunee9d19d2014-08-14 09:00:19 +08001035 ret = PTR_ERR(host->clk);
Manuel Laussb6507592014-07-23 16:36:56 +02001036 goto out_irq;
1037 }
Wei Yongjunee9d19d2014-08-14 09:00:19 +08001038
1039 ret = clk_prepare_enable(host->clk);
1040 if (ret) {
Manuel Laussb6507592014-07-23 16:36:56 +02001041 dev_err(&pdev->dev, "cannot enable clock\n");
1042 goto out_clk;
1043 }
1044
Manuel Laussc4223c22008-06-09 08:36:13 +02001045 host->status = HOST_S_IDLE;
1046
1047 /* board-specific carddetect setup, if any */
1048 if (host->platdata && host->platdata->cd_setup) {
1049 ret = host->platdata->cd_setup(mmc, 1);
1050 if (ret) {
Manuel Lausse2d26472008-06-27 18:25:18 +02001051 dev_warn(&pdev->dev, "board CD setup failed\n");
1052 mmc->caps |= MMC_CAP_NEEDS_POLL;
Pete Popovba264b32005-09-21 06:18:27 +00001053 }
Manuel Lausse2d26472008-06-27 18:25:18 +02001054 } else
1055 mmc->caps |= MMC_CAP_NEEDS_POLL;
Pete Popovba264b32005-09-21 06:18:27 +00001056
Manuel Lauss3b839072009-10-14 09:38:06 +02001057 /* platform may not be able to use all advertised caps */
1058 if (host->platdata)
1059 mmc->caps &= ~(host->platdata->mask_host_caps);
1060
Manuel Laussc4223c22008-06-09 08:36:13 +02001061 tasklet_init(&host->data_task, au1xmmc_tasklet_data,
1062 (unsigned long)host);
Pete Popovba264b32005-09-21 06:18:27 +00001063
Manuel Laussc4223c22008-06-09 08:36:13 +02001064 tasklet_init(&host->finish_task, au1xmmc_tasklet_finish,
1065 (unsigned long)host);
Pete Popovba264b32005-09-21 06:18:27 +00001066
Manuel Lauss1177d992011-08-02 19:51:07 +02001067 if (has_dbdma()) {
1068 ret = au1xmmc_dbdma_init(host);
1069 if (ret)
Linus Torvaldsd6748062011-11-03 13:28:14 -07001070 pr_info(DRIVER_NAME ": DBDMA init failed; using PIO\n");
Manuel Lauss1177d992011-08-02 19:51:07 +02001071 }
Pete Popovba264b32005-09-21 06:18:27 +00001072
Manuel Laussc4223c22008-06-09 08:36:13 +02001073#ifdef CONFIG_LEDS_CLASS
1074 if (host->platdata && host->platdata->led) {
1075 struct led_classdev *led = host->platdata->led;
1076 led->name = mmc_hostname(mmc);
1077 led->brightness = LED_OFF;
1078 led->default_trigger = mmc_hostname(mmc);
1079 ret = led_classdev_register(mmc_dev(mmc), led);
1080 if (ret)
1081 goto out5;
1082 }
1083#endif
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001084
Manuel Laussc4223c22008-06-09 08:36:13 +02001085 au1xmmc_reset_controller(host);
Pete Popovba264b32005-09-21 06:18:27 +00001086
Manuel Laussc4223c22008-06-09 08:36:13 +02001087 ret = mmc_add_host(mmc);
1088 if (ret) {
1089 dev_err(&pdev->dev, "cannot add mmc host\n");
1090 goto out6;
1091 }
Pete Popovba264b32005-09-21 06:18:27 +00001092
Manuel Laussdd8572a2008-07-17 13:07:28 +02001093 platform_set_drvdata(pdev, host);
Pete Popovba264b32005-09-21 06:18:27 +00001094
Manuel Lauss2f73bfb2014-07-23 16:36:26 +02001095 pr_info(DRIVER_NAME ": MMC Controller %d set up at %p"
Manuel Laussc4223c22008-06-09 08:36:13 +02001096 " (mode=%s)\n", pdev->id, host->iobase,
1097 host->flags & HOST_F_DMA ? "dma" : "pio");
Pete Popovba264b32005-09-21 06:18:27 +00001098
Manuel Laussc4223c22008-06-09 08:36:13 +02001099 return 0; /* all ok */
Pete Popovba264b32005-09-21 06:18:27 +00001100
Manuel Laussc4223c22008-06-09 08:36:13 +02001101out6:
1102#ifdef CONFIG_LEDS_CLASS
1103 if (host->platdata && host->platdata->led)
1104 led_classdev_unregister(host->platdata->led);
1105out5:
1106#endif
Manuel Lauss2f73bfb2014-07-23 16:36:26 +02001107 __raw_writel(0, HOST_ENABLE(host));
1108 __raw_writel(0, HOST_CONFIG(host));
1109 __raw_writel(0, HOST_CONFIG2(host));
1110 wmb(); /* drain writebuffer */
Manuel Laussc4223c22008-06-09 08:36:13 +02001111
Manuel Lauss1177d992011-08-02 19:51:07 +02001112 if (host->flags & HOST_F_DBDMA)
1113 au1xmmc_dbdma_shutdown(host);
Manuel Laussc4223c22008-06-09 08:36:13 +02001114
1115 tasklet_kill(&host->data_task);
1116 tasklet_kill(&host->finish_task);
1117
Manuel Lausse2d26472008-06-27 18:25:18 +02001118 if (host->platdata && host->platdata->cd_setup &&
1119 !(mmc->caps & MMC_CAP_NEEDS_POLL))
Manuel Laussc4223c22008-06-09 08:36:13 +02001120 host->platdata->cd_setup(mmc, 0);
Manuel Laussb6507592014-07-23 16:36:56 +02001121out_clk:
1122 clk_disable_unprepare(host->clk);
1123 clk_put(host->clk);
1124out_irq:
Manuel Laussc4223c22008-06-09 08:36:13 +02001125 free_irq(host->irq, host);
1126out3:
1127 iounmap((void *)host->iobase);
1128out2:
1129 release_resource(host->ioarea);
1130 kfree(host->ioarea);
1131out1:
1132 mmc_free_host(mmc);
1133out0:
1134 return ret;
Pete Popovba264b32005-09-21 06:18:27 +00001135}
1136
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001137static int au1xmmc_remove(struct platform_device *pdev)
Pete Popovba264b32005-09-21 06:18:27 +00001138{
Manuel Laussdd8572a2008-07-17 13:07:28 +02001139 struct au1xmmc_host *host = platform_get_drvdata(pdev);
Pete Popovba264b32005-09-21 06:18:27 +00001140
Manuel Laussdd8572a2008-07-17 13:07:28 +02001141 if (host) {
1142 mmc_remove_host(host->mmc);
Pete Popovba264b32005-09-21 06:18:27 +00001143
Manuel Laussc4223c22008-06-09 08:36:13 +02001144#ifdef CONFIG_LEDS_CLASS
1145 if (host->platdata && host->platdata->led)
1146 led_classdev_unregister(host->platdata->led);
1147#endif
1148
Manuel Lausse2d26472008-06-27 18:25:18 +02001149 if (host->platdata && host->platdata->cd_setup &&
Manuel Laussdd8572a2008-07-17 13:07:28 +02001150 !(host->mmc->caps & MMC_CAP_NEEDS_POLL))
1151 host->platdata->cd_setup(host->mmc, 0);
Manuel Laussc4223c22008-06-09 08:36:13 +02001152
Manuel Lauss2f73bfb2014-07-23 16:36:26 +02001153 __raw_writel(0, HOST_ENABLE(host));
1154 __raw_writel(0, HOST_CONFIG(host));
1155 __raw_writel(0, HOST_CONFIG2(host));
1156 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +00001157
1158 tasklet_kill(&host->data_task);
1159 tasklet_kill(&host->finish_task);
1160
Manuel Lauss1177d992011-08-02 19:51:07 +02001161 if (host->flags & HOST_F_DBDMA)
1162 au1xmmc_dbdma_shutdown(host);
1163
Pete Popovba264b32005-09-21 06:18:27 +00001164 au1xmmc_set_power(host, 0);
1165
Manuel Laussb6507592014-07-23 16:36:56 +02001166 clk_disable_unprepare(host->clk);
1167 clk_put(host->clk);
1168
Manuel Laussc4223c22008-06-09 08:36:13 +02001169 free_irq(host->irq, host);
1170 iounmap((void *)host->iobase);
1171 release_resource(host->ioarea);
1172 kfree(host->ioarea);
Pete Popovba264b32005-09-21 06:18:27 +00001173
Manuel Laussdd8572a2008-07-17 13:07:28 +02001174 mmc_free_host(host->mmc);
Pete Popovba264b32005-09-21 06:18:27 +00001175 }
Pete Popovba264b32005-09-21 06:18:27 +00001176 return 0;
1177}
1178
Manuel Laussdd8572a2008-07-17 13:07:28 +02001179#ifdef CONFIG_PM
1180static int au1xmmc_suspend(struct platform_device *pdev, pm_message_t state)
1181{
1182 struct au1xmmc_host *host = platform_get_drvdata(pdev);
Manuel Laussdd8572a2008-07-17 13:07:28 +02001183
Manuel Lauss2f73bfb2014-07-23 16:36:26 +02001184 __raw_writel(0, HOST_CONFIG2(host));
1185 __raw_writel(0, HOST_CONFIG(host));
1186 __raw_writel(0xffffffff, HOST_STATUS(host));
1187 __raw_writel(0, HOST_ENABLE(host));
1188 wmb(); /* drain writebuffer */
Manuel Laussdd8572a2008-07-17 13:07:28 +02001189
1190 return 0;
1191}
1192
1193static int au1xmmc_resume(struct platform_device *pdev)
1194{
1195 struct au1xmmc_host *host = platform_get_drvdata(pdev);
1196
1197 au1xmmc_reset_controller(host);
1198
Ulf Hansson1e63d482013-09-25 10:55:23 +02001199 return 0;
Manuel Laussdd8572a2008-07-17 13:07:28 +02001200}
1201#else
1202#define au1xmmc_suspend NULL
1203#define au1xmmc_resume NULL
1204#endif
1205
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001206static struct platform_driver au1xmmc_driver = {
Pete Popovba264b32005-09-21 06:18:27 +00001207 .probe = au1xmmc_probe,
1208 .remove = au1xmmc_remove,
Manuel Laussdd8572a2008-07-17 13:07:28 +02001209 .suspend = au1xmmc_suspend,
1210 .resume = au1xmmc_resume,
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001211 .driver = {
1212 .name = DRIVER_NAME,
1213 },
Pete Popovba264b32005-09-21 06:18:27 +00001214};
1215
1216static int __init au1xmmc_init(void)
1217{
Manuel Lauss1177d992011-08-02 19:51:07 +02001218 if (has_dbdma()) {
1219 /* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride
1220 * of 8 bits. And since devices are shared, we need to create
1221 * our own to avoid freaking out other devices.
1222 */
1223 memid = au1xxx_ddma_add_device(&au1xmmc_mem_dbdev);
1224 if (!memid)
Linus Torvaldsd6748062011-11-03 13:28:14 -07001225 pr_err("au1xmmc: cannot add memory dbdma\n");
Manuel Lauss1177d992011-08-02 19:51:07 +02001226 }
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001227 return platform_driver_register(&au1xmmc_driver);
Pete Popovba264b32005-09-21 06:18:27 +00001228}
1229
1230static void __exit au1xmmc_exit(void)
1231{
Manuel Lauss1177d992011-08-02 19:51:07 +02001232 if (has_dbdma() && memid)
Manuel Laussc4223c22008-06-09 08:36:13 +02001233 au1xxx_ddma_del_device(memid);
Manuel Lauss1177d992011-08-02 19:51:07 +02001234
Martin Michlmayrb256f9d2006-03-04 23:01:13 +00001235 platform_driver_unregister(&au1xmmc_driver);
Pete Popovba264b32005-09-21 06:18:27 +00001236}
1237
1238module_init(au1xmmc_init);
1239module_exit(au1xmmc_exit);
1240
Pete Popovba264b32005-09-21 06:18:27 +00001241MODULE_AUTHOR("Advanced Micro Devices, Inc");
1242MODULE_DESCRIPTION("MMC/SD driver for the Alchemy Au1XXX");
1243MODULE_LICENSE("GPL");
Kay Sieversbc65c722008-04-15 14:34:28 -07001244MODULE_ALIAS("platform:au1xxx-mmc");