blob: 0acc237843f7cc2d461ea7cf6512d8931435258b [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Pete Popovba264b32005-09-21 06:18:27 +00002/*
Pierre Ossman70f10482007-07-11 20:04:50 +02003 * linux/drivers/mmc/host/au1xmmc.c - AU1XX0 MMC driver
Pete Popovba264b32005-09-21 06:18:27 +00004 *
5 * Copyright (c) 2005, Advanced Micro Devices, Inc.
6 *
7 * Developed with help from the 2.4.30 MMC AU1XXX controller including
8 * the following copyright notices:
9 * Copyright (c) 2003-2004 Embedded Edge, LLC.
10 * Portions Copyright (C) 2002 Embedix, Inc
11 * Copyright 2002 Hewlett-Packard Company
12
13 * 2.6 version of this driver inspired by:
14 * (drivers/mmc/wbsd.c) Copyright (C) 2004-2005 Pierre Ossman,
15 * All Rights Reserved.
16 * (drivers/mmc/pxa.c) Copyright (C) 2003 Russell King,
17 * All Rights Reserved.
18 *
19
Pete Popovba264b32005-09-21 06:18:27 +000020 */
21
Manuel Lausse2d26472008-06-27 18:25:18 +020022/* Why don't we use the SD controllers' carddetect feature?
Pete Popovba264b32005-09-21 06:18:27 +000023 *
24 * From the AU1100 MMC application guide:
25 * If the Au1100-based design is intended to support both MultiMediaCards
26 * and 1- or 4-data bit SecureDigital cards, then the solution is to
27 * connect a weak (560KOhm) pull-up resistor to connector pin 1.
28 * In doing so, a MMC card never enters SPI-mode communications,
29 * but now the SecureDigital card-detect feature of CD/DAT3 is ineffective
30 * (the low to high transition will not occur).
Pete Popovba264b32005-09-21 06:18:27 +000031 */
32
Manuel Laussb6507592014-07-23 16:36:56 +020033#include <linux/clk.h>
Pete Popovba264b32005-09-21 06:18:27 +000034#include <linux/module.h>
35#include <linux/init.h>
Martin Michlmayrb256f9d2006-03-04 23:01:13 +000036#include <linux/platform_device.h>
Pete Popovba264b32005-09-21 06:18:27 +000037#include <linux/mm.h>
38#include <linux/interrupt.h>
39#include <linux/dma-mapping.h>
Al Viro0ada7a02007-10-27 19:40:46 +010040#include <linux/scatterlist.h>
Christoph Hellwiga6720c02018-05-28 08:14:21 +020041#include <linux/highmem.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;
Manuel Lauss2f73bfb2014-07-23 16:36:26 +020093 void __iomem *iobase;
Manuel Lauss5c0a8892008-06-09 08:38:35 +020094 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;
Manuel Laussb6507592014-07-23 16:36:56 +0200121 struct clk *clk;
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200122};
123
124/* Status flags used by the host structure */
125#define HOST_F_XMIT 0x0001
126#define HOST_F_RECV 0x0002
127#define HOST_F_DMA 0x0010
Manuel Lauss1177d992011-08-02 19:51:07 +0200128#define HOST_F_DBDMA 0x0020
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200129#define HOST_F_ACTIVE 0x0100
130#define HOST_F_STOP 0x1000
131
132#define HOST_S_IDLE 0x0001
133#define HOST_S_CMD 0x0002
134#define HOST_S_DATA 0x0003
135#define HOST_S_STOP 0x0004
136
137/* Easy access macros */
138#define HOST_STATUS(h) ((h)->iobase + SD_STATUS)
139#define HOST_CONFIG(h) ((h)->iobase + SD_CONFIG)
140#define HOST_ENABLE(h) ((h)->iobase + SD_ENABLE)
141#define HOST_TXPORT(h) ((h)->iobase + SD_TXPORT)
142#define HOST_RXPORT(h) ((h)->iobase + SD_RXPORT)
143#define HOST_CMDARG(h) ((h)->iobase + SD_CMDARG)
144#define HOST_BLKSIZE(h) ((h)->iobase + SD_BLKSIZE)
145#define HOST_CMD(h) ((h)->iobase + SD_CMD)
146#define HOST_CONFIG2(h) ((h)->iobase + SD_CONFIG2)
147#define HOST_TIMEOUT(h) ((h)->iobase + SD_TIMEOUT)
148#define HOST_DEBUG(h) ((h)->iobase + SD_DEBUG)
149
150#define DMA_CHANNEL(h) \
151 (((h)->flags & HOST_F_XMIT) ? (h)->tx_chan : (h)->rx_chan)
152
Manuel Lauss1177d992011-08-02 19:51:07 +0200153static inline int has_dbdma(void)
154{
155 switch (alchemy_get_cputype()) {
156 case ALCHEMY_CPU_AU1200:
Manuel Lauss809f36c2011-11-01 20:03:30 +0100157 case ALCHEMY_CPU_AU1300:
Manuel Lauss1177d992011-08-02 19:51:07 +0200158 return 1;
159 default:
160 return 0;
161 }
162}
163
Pete Popovba264b32005-09-21 06:18:27 +0000164static inline void IRQ_ON(struct au1xmmc_host *host, u32 mask)
165{
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200166 u32 val = __raw_readl(HOST_CONFIG(host));
Pete Popovba264b32005-09-21 06:18:27 +0000167 val |= mask;
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200168 __raw_writel(val, HOST_CONFIG(host));
169 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000170}
171
172static inline void FLUSH_FIFO(struct au1xmmc_host *host)
173{
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200174 u32 val = __raw_readl(HOST_CONFIG2(host));
Pete Popovba264b32005-09-21 06:18:27 +0000175
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200176 __raw_writel(val | SD_CONFIG2_FF, HOST_CONFIG2(host));
177 wmb(); /* drain writebuffer */
178 mdelay(1);
Pete Popovba264b32005-09-21 06:18:27 +0000179
180 /* SEND_STOP will turn off clock control - this re-enables it */
181 val &= ~SD_CONFIG2_DF;
182
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200183 __raw_writel(val, HOST_CONFIG2(host));
184 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000185}
186
187static inline void IRQ_OFF(struct au1xmmc_host *host, u32 mask)
188{
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200189 u32 val = __raw_readl(HOST_CONFIG(host));
Pete Popovba264b32005-09-21 06:18:27 +0000190 val &= ~mask;
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200191 __raw_writel(val, HOST_CONFIG(host));
192 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000193}
194
195static inline void SEND_STOP(struct au1xmmc_host *host)
196{
Manuel Lauss281dd232008-06-09 08:37:33 +0200197 u32 config2;
Pete Popovba264b32005-09-21 06:18:27 +0000198
199 WARN_ON(host->status != HOST_S_DATA);
200 host->status = HOST_S_STOP;
201
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200202 config2 = __raw_readl(HOST_CONFIG2(host));
203 __raw_writel(config2 | SD_CONFIG2_DF, HOST_CONFIG2(host));
204 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000205
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400206 /* Send the stop command */
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200207 __raw_writel(STOP_CMD, HOST_CMD(host));
208 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000209}
210
211static void au1xmmc_set_power(struct au1xmmc_host *host, int state)
212{
Manuel Laussc4223c22008-06-09 08:36:13 +0200213 if (host->platdata && host->platdata->set_power)
214 host->platdata->set_power(host->mmc, state);
Pete Popovba264b32005-09-21 06:18:27 +0000215}
216
Manuel Lausse2d26472008-06-27 18:25:18 +0200217static int au1xmmc_card_inserted(struct mmc_host *mmc)
Pete Popovba264b32005-09-21 06:18:27 +0000218{
Manuel Lausse2d26472008-06-27 18:25:18 +0200219 struct au1xmmc_host *host = mmc_priv(mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200220
221 if (host->platdata && host->platdata->card_inserted)
Manuel Lausse2d26472008-06-27 18:25:18 +0200222 return !!host->platdata->card_inserted(host->mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200223
Manuel Lausse2d26472008-06-27 18:25:18 +0200224 return -ENOSYS;
Pete Popovba264b32005-09-21 06:18:27 +0000225}
226
Manuel Lauss82999772007-01-25 10:29:24 +0100227static int au1xmmc_card_readonly(struct mmc_host *mmc)
Pete Popovba264b32005-09-21 06:18:27 +0000228{
Manuel Lauss82999772007-01-25 10:29:24 +0100229 struct au1xmmc_host *host = mmc_priv(mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200230
231 if (host->platdata && host->platdata->card_readonly)
Manuel Lausse2d26472008-06-27 18:25:18 +0200232 return !!host->platdata->card_readonly(mmc);
Manuel Laussc4223c22008-06-09 08:36:13 +0200233
Manuel Lausse2d26472008-06-27 18:25:18 +0200234 return -ENOSYS;
Pete Popovba264b32005-09-21 06:18:27 +0000235}
236
237static void au1xmmc_finish_request(struct au1xmmc_host *host)
238{
Pete Popovba264b32005-09-21 06:18:27 +0000239 struct mmc_request *mrq = host->mrq;
240
241 host->mrq = NULL;
Manuel Laussc4223c22008-06-09 08:36:13 +0200242 host->flags &= HOST_F_ACTIVE | HOST_F_DMA;
Pete Popovba264b32005-09-21 06:18:27 +0000243
244 host->dma.len = 0;
245 host->dma.dir = 0;
246
247 host->pio.index = 0;
248 host->pio.offset = 0;
249 host->pio.len = 0;
250
251 host->status = HOST_S_IDLE;
252
Pete Popovba264b32005-09-21 06:18:27 +0000253 mmc_request_done(host->mmc, mrq);
254}
255
Emil Renner Berthinga1149c32021-02-04 16:18:40 +0100256static void au1xmmc_tasklet_finish(struct tasklet_struct *t)
Pete Popovba264b32005-09-21 06:18:27 +0000257{
Emil Renner Berthinga1149c32021-02-04 16:18:40 +0100258 struct au1xmmc_host *host = from_tasklet(host, t, finish_task);
Pete Popovba264b32005-09-21 06:18:27 +0000259 au1xmmc_finish_request(host);
260}
261
Ulf Hansson4c3965a2020-04-06 13:43:37 +0200262static int au1xmmc_send_command(struct au1xmmc_host *host,
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200263 struct mmc_command *cmd, struct mmc_data *data)
Pete Popovba264b32005-09-21 06:18:27 +0000264{
Pete Popovba264b32005-09-21 06:18:27 +0000265 u32 mmccmd = (cmd->opcode << SD_CMD_CI_SHIFT);
266
Martin Michlmayre142c242006-03-04 23:01:39 +0000267 switch (mmc_resp_type(cmd)) {
Manuel Lauss279bc442007-01-25 10:27:41 +0100268 case MMC_RSP_NONE:
269 break;
Pete Popovba264b32005-09-21 06:18:27 +0000270 case MMC_RSP_R1:
271 mmccmd |= SD_CMD_RT_1;
272 break;
273 case MMC_RSP_R1B:
274 mmccmd |= SD_CMD_RT_1B;
275 break;
276 case MMC_RSP_R2:
277 mmccmd |= SD_CMD_RT_2;
278 break;
279 case MMC_RSP_R3:
280 mmccmd |= SD_CMD_RT_3;
281 break;
Manuel Lauss279bc442007-01-25 10:27:41 +0100282 default:
Girish K Sa3c76eb2011-10-11 11:44:09 +0530283 pr_info("au1xmmc: unhandled response type %02x\n",
Manuel Lauss279bc442007-01-25 10:27:41 +0100284 mmc_resp_type(cmd));
Pierre Ossman17b04292007-07-22 22:18:46 +0200285 return -EINVAL;
Pete Popovba264b32005-09-21 06:18:27 +0000286 }
287
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200288 if (data) {
Pierre Ossman6356a9d2007-10-22 18:16:16 +0200289 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200290 if (data->blocks > 1)
291 mmccmd |= SD_CMD_CT_4;
292 else
293 mmccmd |= SD_CMD_CT_2;
Pierre Ossman6356a9d2007-10-22 18:16:16 +0200294 } else if (data->flags & MMC_DATA_WRITE) {
Pierre Ossmanbe0192a2007-07-24 21:11:47 +0200295 if (data->blocks > 1)
296 mmccmd |= SD_CMD_CT_3;
297 else
298 mmccmd |= SD_CMD_CT_1;
299 }
Pete Popovba264b32005-09-21 06:18:27 +0000300 }
301
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200302 __raw_writel(cmd->arg, HOST_CMDARG(host));
303 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000304
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200305 __raw_writel((mmccmd | SD_CMD_GO), HOST_CMD(host));
306 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000307
308 /* Wait for the command to go on the line */
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200309 while (__raw_readl(HOST_CMD(host)) & SD_CMD_GO)
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200310 /* nop */;
Pete Popovba264b32005-09-21 06:18:27 +0000311
Pierre Ossman17b04292007-07-22 22:18:46 +0200312 return 0;
Pete Popovba264b32005-09-21 06:18:27 +0000313}
314
315static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
316{
Pete Popovba264b32005-09-21 06:18:27 +0000317 struct mmc_request *mrq = host->mrq;
318 struct mmc_data *data;
319 u32 crc;
320
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200321 WARN_ON((host->status != HOST_S_DATA) && (host->status != HOST_S_STOP));
Pete Popovba264b32005-09-21 06:18:27 +0000322
323 if (host->mrq == NULL)
324 return;
325
326 data = mrq->cmd->data;
327
328 if (status == 0)
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200329 status = __raw_readl(HOST_STATUS(host));
Pete Popovba264b32005-09-21 06:18:27 +0000330
331 /* The transaction is really over when the SD_STATUS_DB bit is clear */
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200332 while ((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB))
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200333 status = __raw_readl(HOST_STATUS(host));
Pete Popovba264b32005-09-21 06:18:27 +0000334
Pierre Ossman17b04292007-07-22 22:18:46 +0200335 data->error = 0;
Pete Popovba264b32005-09-21 06:18:27 +0000336 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir);
337
338 /* Process any errors */
Pete Popovba264b32005-09-21 06:18:27 +0000339 crc = (status & (SD_STATUS_WC | SD_STATUS_RC));
340 if (host->flags & HOST_F_XMIT)
341 crc |= ((status & 0x07) == 0x02) ? 0 : 1;
342
343 if (crc)
Pierre Ossman17b04292007-07-22 22:18:46 +0200344 data->error = -EILSEQ;
Pete Popovba264b32005-09-21 06:18:27 +0000345
346 /* Clear the CRC bits */
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200347 __raw_writel(SD_STATUS_WC | SD_STATUS_RC, HOST_STATUS(host));
Pete Popovba264b32005-09-21 06:18:27 +0000348
349 data->bytes_xfered = 0;
350
Pierre Ossman17b04292007-07-22 22:18:46 +0200351 if (!data->error) {
Manuel Lauss1177d992011-08-02 19:51:07 +0200352 if (host->flags & (HOST_F_DMA | HOST_F_DBDMA)) {
Pete Popovba264b32005-09-21 06:18:27 +0000353 u32 chan = DMA_CHANNEL(host);
354
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200355 chan_tab_t *c = *((chan_tab_t **)chan);
Pete Popovba264b32005-09-21 06:18:27 +0000356 au1x_dma_chan_t *cp = c->chan_ptr;
357 data->bytes_xfered = cp->ddma_bytecnt;
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200358 } else
Pete Popovba264b32005-09-21 06:18:27 +0000359 data->bytes_xfered =
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200360 (data->blocks * data->blksz) - host->pio.len;
Pete Popovba264b32005-09-21 06:18:27 +0000361 }
362
363 au1xmmc_finish_request(host);
364}
365
Emil Renner Berthinga1149c32021-02-04 16:18:40 +0100366static void au1xmmc_tasklet_data(struct tasklet_struct *t)
Pete Popovba264b32005-09-21 06:18:27 +0000367{
Emil Renner Berthinga1149c32021-02-04 16:18:40 +0100368 struct au1xmmc_host *host = from_tasklet(host, t, data_task);
Pete Popovba264b32005-09-21 06:18:27 +0000369
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200370 u32 status = __raw_readl(HOST_STATUS(host));
Pete Popovba264b32005-09-21 06:18:27 +0000371 au1xmmc_data_complete(host, status);
372}
373
374#define AU1XMMC_MAX_TRANSFER 8
375
376static void au1xmmc_send_pio(struct au1xmmc_host *host)
377{
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200378 struct mmc_data *data;
379 int sg_len, max, count;
380 unsigned char *sg_ptr, val;
381 u32 status;
Pete Popovba264b32005-09-21 06:18:27 +0000382 struct scatterlist *sg;
383
384 data = host->mrq->data;
385
386 if (!(host->flags & HOST_F_XMIT))
387 return;
388
389 /* This is the pointer to the data buffer */
390 sg = &data->sg[host->pio.index];
Christoph Hellwiga6720c02018-05-28 08:14:21 +0200391 sg_ptr = kmap_atomic(sg_page(sg)) + sg->offset + host->pio.offset;
Pete Popovba264b32005-09-21 06:18:27 +0000392
393 /* This is the space left inside the buffer */
394 sg_len = data->sg[host->pio.index].length - host->pio.offset;
395
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200396 /* Check if we need less than the size of the sg_buffer */
Pete Popovba264b32005-09-21 06:18:27 +0000397 max = (sg_len > host->pio.len) ? host->pio.len : sg_len;
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200398 if (max > AU1XMMC_MAX_TRANSFER)
399 max = AU1XMMC_MAX_TRANSFER;
Pete Popovba264b32005-09-21 06:18:27 +0000400
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200401 for (count = 0; count < max; count++) {
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200402 status = __raw_readl(HOST_STATUS(host));
Pete Popovba264b32005-09-21 06:18:27 +0000403
404 if (!(status & SD_STATUS_TH))
405 break;
406
Christoph Hellwiga6720c02018-05-28 08:14:21 +0200407 val = sg_ptr[count];
Pete Popovba264b32005-09-21 06:18:27 +0000408
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200409 __raw_writel((unsigned long)val, HOST_TXPORT(host));
410 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000411 }
Christoph Hellwiga6720c02018-05-28 08:14:21 +0200412 kunmap_atomic(sg_ptr);
Pete Popovba264b32005-09-21 06:18:27 +0000413
414 host->pio.len -= count;
415 host->pio.offset += count;
416
417 if (count == sg_len) {
418 host->pio.index++;
419 host->pio.offset = 0;
420 }
421
422 if (host->pio.len == 0) {
423 IRQ_OFF(host, SD_CONFIG_TH);
424
425 if (host->flags & HOST_F_STOP)
426 SEND_STOP(host);
427
428 tasklet_schedule(&host->data_task);
429 }
430}
431
432static void au1xmmc_receive_pio(struct au1xmmc_host *host)
433{
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200434 struct mmc_data *data;
435 int max, count, sg_len = 0;
436 unsigned char *sg_ptr = NULL;
437 u32 status, val;
Pete Popovba264b32005-09-21 06:18:27 +0000438 struct scatterlist *sg;
439
440 data = host->mrq->data;
441
442 if (!(host->flags & HOST_F_RECV))
443 return;
444
445 max = host->pio.len;
446
447 if (host->pio.index < host->dma.len) {
448 sg = &data->sg[host->pio.index];
Christoph Hellwiga6720c02018-05-28 08:14:21 +0200449 sg_ptr = kmap_atomic(sg_page(sg)) + sg->offset + host->pio.offset;
Pete Popovba264b32005-09-21 06:18:27 +0000450
451 /* This is the space left inside the buffer */
452 sg_len = sg_dma_len(&data->sg[host->pio.index]) - host->pio.offset;
453
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200454 /* Check if we need less than the size of the sg_buffer */
455 if (sg_len < max)
456 max = sg_len;
Pete Popovba264b32005-09-21 06:18:27 +0000457 }
458
459 if (max > AU1XMMC_MAX_TRANSFER)
460 max = AU1XMMC_MAX_TRANSFER;
461
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200462 for (count = 0; count < max; count++) {
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200463 status = __raw_readl(HOST_STATUS(host));
Pete Popovba264b32005-09-21 06:18:27 +0000464
465 if (!(status & SD_STATUS_NE))
466 break;
467
468 if (status & SD_STATUS_RC) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200469 DBG("RX CRC Error [%d + %d].\n", host->pdev->id,
Pete Popovba264b32005-09-21 06:18:27 +0000470 host->pio.len, count);
471 break;
472 }
473
474 if (status & SD_STATUS_RO) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200475 DBG("RX Overrun [%d + %d]\n", host->pdev->id,
Pete Popovba264b32005-09-21 06:18:27 +0000476 host->pio.len, count);
477 break;
478 }
479 else if (status & SD_STATUS_RU) {
Manuel Laussc4223c22008-06-09 08:36:13 +0200480 DBG("RX Underrun [%d + %d]\n", host->pdev->id,
Pete Popovba264b32005-09-21 06:18:27 +0000481 host->pio.len, count);
482 break;
483 }
484
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200485 val = __raw_readl(HOST_RXPORT(host));
Pete Popovba264b32005-09-21 06:18:27 +0000486
487 if (sg_ptr)
Christoph Hellwiga6720c02018-05-28 08:14:21 +0200488 sg_ptr[count] = (unsigned char)(val & 0xFF);
Pete Popovba264b32005-09-21 06:18:27 +0000489 }
Christoph Hellwiga6720c02018-05-28 08:14:21 +0200490 if (sg_ptr)
491 kunmap_atomic(sg_ptr);
Pete Popovba264b32005-09-21 06:18:27 +0000492
493 host->pio.len -= count;
494 host->pio.offset += count;
495
496 if (sg_len && count == sg_len) {
497 host->pio.index++;
498 host->pio.offset = 0;
499 }
500
501 if (host->pio.len == 0) {
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200502 /* IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF); */
Pete Popovba264b32005-09-21 06:18:27 +0000503 IRQ_OFF(host, SD_CONFIG_NE);
504
505 if (host->flags & HOST_F_STOP)
506 SEND_STOP(host);
507
508 tasklet_schedule(&host->data_task);
509 }
510}
511
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200512/* This is called when a command has been completed - grab the response
513 * and check for errors. Then start the data transfer if it is indicated.
514 */
Pete Popovba264b32005-09-21 06:18:27 +0000515static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
516{
Pete Popovba264b32005-09-21 06:18:27 +0000517 struct mmc_request *mrq = host->mrq;
518 struct mmc_command *cmd;
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200519 u32 r[4];
520 int i, trans;
Pete Popovba264b32005-09-21 06:18:27 +0000521
522 if (!host->mrq)
523 return;
524
525 cmd = mrq->cmd;
Pierre Ossman17b04292007-07-22 22:18:46 +0200526 cmd->error = 0;
Pete Popovba264b32005-09-21 06:18:27 +0000527
Russell Kinge9225172006-02-02 12:23:12 +0000528 if (cmd->flags & MMC_RSP_PRESENT) {
529 if (cmd->flags & MMC_RSP_136) {
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200530 r[0] = __raw_readl(host->iobase + SD_RESP3);
531 r[1] = __raw_readl(host->iobase + SD_RESP2);
532 r[2] = __raw_readl(host->iobase + SD_RESP1);
533 r[3] = __raw_readl(host->iobase + SD_RESP0);
Pete Popovba264b32005-09-21 06:18:27 +0000534
Russell Kinge9225172006-02-02 12:23:12 +0000535 /* The CRC is omitted from the response, so really
536 * we only got 120 bytes, but the engine expects
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200537 * 128 bits, so we have to shift things up.
Russell Kinge9225172006-02-02 12:23:12 +0000538 */
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200539 for (i = 0; i < 4; i++) {
Russell Kinge9225172006-02-02 12:23:12 +0000540 cmd->resp[i] = (r[i] & 0x00FFFFFF) << 8;
541 if (i != 3)
542 cmd->resp[i] |= (r[i + 1] & 0xFF000000) >> 24;
543 }
544 } else {
545 /* Techincally, we should be getting all 48 bits of
546 * the response (SD_RESP1 + SD_RESP2), but because
547 * our response omits the CRC, our data ends up
548 * being shifted 8 bits to the right. In this case,
549 * that means that the OSR data starts at bit 31,
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200550 * so we can just read RESP0 and return that.
Russell Kinge9225172006-02-02 12:23:12 +0000551 */
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200552 cmd->resp[0] = __raw_readl(host->iobase + SD_RESP0);
Pete Popovba264b32005-09-21 06:18:27 +0000553 }
554 }
555
556 /* Figure out errors */
Pete Popovba264b32005-09-21 06:18:27 +0000557 if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC))
Pierre Ossman17b04292007-07-22 22:18:46 +0200558 cmd->error = -EILSEQ;
Pete Popovba264b32005-09-21 06:18:27 +0000559
560 trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);
561
Pierre Ossman17b04292007-07-22 22:18:46 +0200562 if (!trans || cmd->error) {
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200563 IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF);
Pete Popovba264b32005-09-21 06:18:27 +0000564 tasklet_schedule(&host->finish_task);
565 return;
566 }
567
568 host->status = HOST_S_DATA;
569
Manuel Lauss1177d992011-08-02 19:51:07 +0200570 if ((host->flags & (HOST_F_DMA | HOST_F_DBDMA))) {
Pete Popovba264b32005-09-21 06:18:27 +0000571 u32 channel = DMA_CHANNEL(host);
572
Manuel Lauss1177d992011-08-02 19:51:07 +0200573 /* Start the DBDMA as soon as the buffer gets something in it */
Pete Popovba264b32005-09-21 06:18:27 +0000574
575 if (host->flags & HOST_F_RECV) {
576 u32 mask = SD_STATUS_DB | SD_STATUS_NE;
577
578 while((status & mask) != mask)
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200579 status = __raw_readl(HOST_STATUS(host));
Pete Popovba264b32005-09-21 06:18:27 +0000580 }
581
582 au1xxx_dbdma_start(channel);
583 }
584}
585
586static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate)
587{
Manuel Laussb6507592014-07-23 16:36:56 +0200588 unsigned int pbus = clk_get_rate(host->clk);
589 unsigned int divisor = ((pbus / rate) / 2) - 1;
Pete Popovba264b32005-09-21 06:18:27 +0000590 u32 config;
591
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200592 config = __raw_readl(HOST_CONFIG(host));
Pete Popovba264b32005-09-21 06:18:27 +0000593
594 config &= ~(SD_CONFIG_DIV);
595 config |= (divisor & SD_CONFIG_DIV) | SD_CONFIG_DE;
596
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200597 __raw_writel(config, HOST_CONFIG(host));
598 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000599}
600
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200601static int au1xmmc_prepare_data(struct au1xmmc_host *host,
602 struct mmc_data *data)
Pete Popovba264b32005-09-21 06:18:27 +0000603{
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100604 int datalen = data->blocks * data->blksz;
Pete Popovba264b32005-09-21 06:18:27 +0000605
Pete Popovba264b32005-09-21 06:18:27 +0000606 if (data->flags & MMC_DATA_READ)
607 host->flags |= HOST_F_RECV;
608 else
609 host->flags |= HOST_F_XMIT;
610
611 if (host->mrq->stop)
612 host->flags |= HOST_F_STOP;
613
614 host->dma.dir = DMA_BIDIRECTIONAL;
615
616 host->dma.len = dma_map_sg(mmc_dev(host->mmc), data->sg,
617 data->sg_len, host->dma.dir);
618
619 if (host->dma.len == 0)
Pierre Ossman17b04292007-07-22 22:18:46 +0200620 return -ETIMEDOUT;
Pete Popovba264b32005-09-21 06:18:27 +0000621
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200622 __raw_writel(data->blksz - 1, HOST_BLKSIZE(host));
Pete Popovba264b32005-09-21 06:18:27 +0000623
Manuel Lauss1177d992011-08-02 19:51:07 +0200624 if (host->flags & (HOST_F_DMA | HOST_F_DBDMA)) {
Pete Popovba264b32005-09-21 06:18:27 +0000625 int i;
626 u32 channel = DMA_CHANNEL(host);
627
628 au1xxx_dbdma_stop(channel);
629
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200630 for (i = 0; i < host->dma.len; i++) {
Pete Popovba264b32005-09-21 06:18:27 +0000631 u32 ret = 0, flags = DDMA_FLAGS_NOIE;
632 struct scatterlist *sg = &data->sg[i];
633 int sg_len = sg->length;
634
635 int len = (datalen > sg_len) ? sg_len : datalen;
636
637 if (i == host->dma.len - 1)
638 flags = DDMA_FLAGS_IE;
639
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200640 if (host->flags & HOST_F_XMIT) {
Manuel Laussea071cc2009-10-13 20:22:34 +0200641 ret = au1xxx_dbdma_put_source(channel,
Manuel Lauss963accb2009-10-13 20:22:35 +0200642 sg_phys(sg), len, flags);
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200643 } else {
Manuel Laussea071cc2009-10-13 20:22:34 +0200644 ret = au1xxx_dbdma_put_dest(channel,
Manuel Lauss963accb2009-10-13 20:22:35 +0200645 sg_phys(sg), len, flags);
Pete Popovba264b32005-09-21 06:18:27 +0000646 }
647
Manuel Laussc4223c22008-06-09 08:36:13 +0200648 if (!ret)
Pete Popovba264b32005-09-21 06:18:27 +0000649 goto dataerr;
650
651 datalen -= len;
652 }
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200653 } else {
Pete Popovba264b32005-09-21 06:18:27 +0000654 host->pio.index = 0;
655 host->pio.offset = 0;
656 host->pio.len = datalen;
657
658 if (host->flags & HOST_F_XMIT)
659 IRQ_ON(host, SD_CONFIG_TH);
660 else
661 IRQ_ON(host, SD_CONFIG_NE);
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200662 /* IRQ_ON(host, SD_CONFIG_RA | SD_CONFIG_RF); */
Pete Popovba264b32005-09-21 06:18:27 +0000663 }
664
Pierre Ossman17b04292007-07-22 22:18:46 +0200665 return 0;
Pete Popovba264b32005-09-21 06:18:27 +0000666
Manuel Laussc4223c22008-06-09 08:36:13 +0200667dataerr:
668 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
669 host->dma.dir);
Pierre Ossman17b04292007-07-22 22:18:46 +0200670 return -ETIMEDOUT;
Pete Popovba264b32005-09-21 06:18:27 +0000671}
672
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200673/* This actually starts a command or data transaction */
Pete Popovba264b32005-09-21 06:18:27 +0000674static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
675{
Pete Popovba264b32005-09-21 06:18:27 +0000676 struct au1xmmc_host *host = mmc_priv(mmc);
Pierre Ossman17b04292007-07-22 22:18:46 +0200677 int ret = 0;
Pete Popovba264b32005-09-21 06:18:27 +0000678
679 WARN_ON(irqs_disabled());
680 WARN_ON(host->status != HOST_S_IDLE);
681
682 host->mrq = mrq;
683 host->status = HOST_S_CMD;
684
Manuel Lauss88b8d9a2008-06-09 08:39:11 +0200685 /* fail request immediately if no card is present */
Manuel Lausse2d26472008-06-27 18:25:18 +0200686 if (0 == au1xmmc_card_inserted(mmc)) {
Manuel Lauss88b8d9a2008-06-09 08:39:11 +0200687 mrq->cmd->error = -ENOMEDIUM;
688 au1xmmc_finish_request(host);
689 return;
690 }
691
Pete Popovba264b32005-09-21 06:18:27 +0000692 if (mrq->data) {
693 FLUSH_FIFO(host);
694 ret = au1xmmc_prepare_data(host, mrq->data);
695 }
696
Pierre Ossman17b04292007-07-22 22:18:46 +0200697 if (!ret)
Ulf Hansson4c3965a2020-04-06 13:43:37 +0200698 ret = au1xmmc_send_command(host, mrq->cmd, mrq->data);
Pete Popovba264b32005-09-21 06:18:27 +0000699
Pierre Ossman17b04292007-07-22 22:18:46 +0200700 if (ret) {
Pete Popovba264b32005-09-21 06:18:27 +0000701 mrq->cmd->error = ret;
702 au1xmmc_finish_request(host);
703 }
704}
705
706static void au1xmmc_reset_controller(struct au1xmmc_host *host)
707{
Pete Popovba264b32005-09-21 06:18:27 +0000708 /* Apply the clock */
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200709 __raw_writel(SD_ENABLE_CE, HOST_ENABLE(host));
710 wmb(); /* drain writebuffer */
711 mdelay(1);
Pete Popovba264b32005-09-21 06:18:27 +0000712
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200713 __raw_writel(SD_ENABLE_R | SD_ENABLE_CE, HOST_ENABLE(host));
714 wmb(); /* drain writebuffer */
715 mdelay(5);
Pete Popovba264b32005-09-21 06:18:27 +0000716
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200717 __raw_writel(~0, HOST_STATUS(host));
718 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000719
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200720 __raw_writel(0, HOST_BLKSIZE(host));
721 __raw_writel(0x001fffff, HOST_TIMEOUT(host));
722 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000723
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200724 __raw_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
725 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000726
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200727 __raw_writel(SD_CONFIG2_EN | SD_CONFIG2_FF, HOST_CONFIG2(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_CONFIG2_EN, HOST_CONFIG2(host));
732 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000733
734 /* Configure interrupts */
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200735 __raw_writel(AU1XMMC_INTERRUPTS, HOST_CONFIG(host));
736 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000737}
738
739
Manuel Lauss5c0a8892008-06-09 08:38:35 +0200740static void au1xmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pete Popovba264b32005-09-21 06:18:27 +0000741{
742 struct au1xmmc_host *host = mmc_priv(mmc);
Manuel Lauss281dd232008-06-09 08:37:33 +0200743 u32 config2;
Pete Popovba264b32005-09-21 06:18:27 +0000744
Pete Popovba264b32005-09-21 06:18:27 +0000745 if (ios->power_mode == MMC_POWER_OFF)
746 au1xmmc_set_power(host, 0);
747 else if (ios->power_mode == MMC_POWER_ON) {
748 au1xmmc_set_power(host, 1);
749 }
750
751 if (ios->clock && ios->clock != host->clock) {
752 au1xmmc_set_clock(host, ios->clock);
753 host->clock = ios->clock;
754 }
Manuel Lauss281dd232008-06-09 08:37:33 +0200755
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200756 config2 = __raw_readl(HOST_CONFIG2(host));
Manuel Lauss281dd232008-06-09 08:37:33 +0200757 switch (ios->bus_width) {
Manuel Lauss809f36c2011-11-01 20:03:30 +0100758 case MMC_BUS_WIDTH_8:
759 config2 |= SD_CONFIG2_BB;
760 break;
Manuel Lauss281dd232008-06-09 08:37:33 +0200761 case MMC_BUS_WIDTH_4:
Manuel Lauss809f36c2011-11-01 20:03:30 +0100762 config2 &= ~SD_CONFIG2_BB;
Manuel Lauss281dd232008-06-09 08:37:33 +0200763 config2 |= SD_CONFIG2_WB;
764 break;
765 case MMC_BUS_WIDTH_1:
Manuel Lauss809f36c2011-11-01 20:03:30 +0100766 config2 &= ~(SD_CONFIG2_WB | SD_CONFIG2_BB);
Manuel Lauss281dd232008-06-09 08:37:33 +0200767 break;
768 }
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200769 __raw_writel(config2, HOST_CONFIG2(host));
770 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +0000771}
772
Manuel Laussc4223c22008-06-09 08:36:13 +0200773#define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
774#define STATUS_DATA_IN (SD_STATUS_NE)
775#define STATUS_DATA_OUT (SD_STATUS_TH)
776
777static irqreturn_t au1xmmc_irq(int irq, void *dev_id)
Pete Popovba264b32005-09-21 06:18:27 +0000778{
Manuel Laussc4223c22008-06-09 08:36:13 +0200779 struct au1xmmc_host *host = dev_id;
780 u32 status;
781
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200782 status = __raw_readl(HOST_STATUS(host));
Manuel Laussc4223c22008-06-09 08:36:13 +0200783
784 if (!(status & SD_STATUS_I))
785 return IRQ_NONE; /* not ours */
786
Manuel Lauss20f522f2008-06-09 08:38:03 +0200787 if (status & SD_STATUS_SI) /* SDIO */
788 mmc_signal_sdio_irq(host->mmc);
789
Manuel Laussc4223c22008-06-09 08:36:13 +0200790 if (host->mrq && (status & STATUS_TIMEOUT)) {
791 if (status & SD_STATUS_RAT)
792 host->mrq->cmd->error = -ETIMEDOUT;
793 else if (status & SD_STATUS_DT)
794 host->mrq->data->error = -ETIMEDOUT;
795
796 /* In PIO mode, interrupts might still be enabled */
797 IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
798
799 /* IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF); */
800 tasklet_schedule(&host->finish_task);
801 }
802#if 0
803 else if (status & SD_STATUS_DD) {
804 /* Sometimes we get a DD before a NE in PIO mode */
805 if (!(host->flags & HOST_F_DMA) && (status & SD_STATUS_NE))
806 au1xmmc_receive_pio(host);
807 else {
808 au1xmmc_data_complete(host, status);
809 /* tasklet_schedule(&host->data_task); */
810 }
811 }
812#endif
813 else if (status & SD_STATUS_CR) {
814 if (host->status == HOST_S_CMD)
815 au1xmmc_cmd_complete(host, status);
816
817 } else if (!(host->flags & HOST_F_DMA)) {
818 if ((host->flags & HOST_F_XMIT) && (status & STATUS_DATA_OUT))
819 au1xmmc_send_pio(host);
820 else if ((host->flags & HOST_F_RECV) && (status & STATUS_DATA_IN))
821 au1xmmc_receive_pio(host);
822
823 } else if (status & 0x203F3C70) {
824 DBG("Unhandled status %8.8x\n", host->pdev->id,
825 status);
826 }
827
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200828 __raw_writel(status, HOST_STATUS(host));
829 wmb(); /* drain writebuffer */
Manuel Laussc4223c22008-06-09 08:36:13 +0200830
831 return IRQ_HANDLED;
832}
833
Manuel Laussc4223c22008-06-09 08:36:13 +0200834/* 8bit memory DMA device */
835static dbdev_tab_t au1xmmc_mem_dbdev = {
836 .dev_id = DSCR_CMD0_ALWAYS,
837 .dev_flags = DEV_FLAGS_ANYUSE,
838 .dev_tsize = 0,
839 .dev_devwidth = 8,
840 .dev_physaddr = 0x00000000,
841 .dev_intlevel = 0,
842 .dev_intpolarity = 0,
843};
844static int memid;
845
846static void au1xmmc_dbdma_callback(int irq, void *dev_id)
847{
848 struct au1xmmc_host *host = (struct au1xmmc_host *)dev_id;
Pete Popovba264b32005-09-21 06:18:27 +0000849
850 /* Avoid spurious interrupts */
Pete Popovba264b32005-09-21 06:18:27 +0000851 if (!host->mrq)
852 return;
853
854 if (host->flags & HOST_F_STOP)
855 SEND_STOP(host);
856
857 tasklet_schedule(&host->data_task);
858}
859
Manuel Laussc4223c22008-06-09 08:36:13 +0200860static int au1xmmc_dbdma_init(struct au1xmmc_host *host)
Pete Popovba264b32005-09-21 06:18:27 +0000861{
Manuel Laussc4223c22008-06-09 08:36:13 +0200862 struct resource *res;
863 int txid, rxid;
Pete Popovba264b32005-09-21 06:18:27 +0000864
Manuel Laussc4223c22008-06-09 08:36:13 +0200865 res = platform_get_resource(host->pdev, IORESOURCE_DMA, 0);
866 if (!res)
867 return -ENODEV;
868 txid = res->start;
Pete Popovba264b32005-09-21 06:18:27 +0000869
Manuel Laussc4223c22008-06-09 08:36:13 +0200870 res = platform_get_resource(host->pdev, IORESOURCE_DMA, 1);
871 if (!res)
872 return -ENODEV;
873 rxid = res->start;
Pete Popovba264b32005-09-21 06:18:27 +0000874
Manuel Laussc4223c22008-06-09 08:36:13 +0200875 if (!memid)
876 return -ENODEV;
Pete Popovba264b32005-09-21 06:18:27 +0000877
Manuel Laussc4223c22008-06-09 08:36:13 +0200878 host->tx_chan = au1xxx_dbdma_chan_alloc(memid, txid,
879 au1xmmc_dbdma_callback, (void *)host);
880 if (!host->tx_chan) {
881 dev_err(&host->pdev->dev, "cannot allocate TX DMA\n");
882 return -ENODEV;
883 }
Pete Popovba264b32005-09-21 06:18:27 +0000884
Manuel Laussc4223c22008-06-09 08:36:13 +0200885 host->rx_chan = au1xxx_dbdma_chan_alloc(rxid, memid,
886 au1xmmc_dbdma_callback, (void *)host);
887 if (!host->rx_chan) {
888 dev_err(&host->pdev->dev, "cannot allocate RX DMA\n");
889 au1xxx_dbdma_chan_free(host->tx_chan);
890 return -ENODEV;
891 }
Pete Popovba264b32005-09-21 06:18:27 +0000892
Manuel Laussc4223c22008-06-09 08:36:13 +0200893 au1xxx_dbdma_set_devwidth(host->tx_chan, 8);
894 au1xxx_dbdma_set_devwidth(host->rx_chan, 8);
Pete Popovba264b32005-09-21 06:18:27 +0000895
Manuel Laussc4223c22008-06-09 08:36:13 +0200896 au1xxx_dbdma_ring_alloc(host->tx_chan, AU1XMMC_DESCRIPTOR_COUNT);
897 au1xxx_dbdma_ring_alloc(host->rx_chan, AU1XMMC_DESCRIPTOR_COUNT);
Pete Popovba264b32005-09-21 06:18:27 +0000898
Manuel Laussc4223c22008-06-09 08:36:13 +0200899 /* DBDMA is good to go */
Manuel Lauss1177d992011-08-02 19:51:07 +0200900 host->flags |= HOST_F_DMA | HOST_F_DBDMA;
Pete Popovba264b32005-09-21 06:18:27 +0000901
Manuel Laussc4223c22008-06-09 08:36:13 +0200902 return 0;
903}
Pete Popovba264b32005-09-21 06:18:27 +0000904
Manuel Laussc4223c22008-06-09 08:36:13 +0200905static void au1xmmc_dbdma_shutdown(struct au1xmmc_host *host)
906{
907 if (host->flags & HOST_F_DMA) {
908 host->flags &= ~HOST_F_DMA;
909 au1xxx_dbdma_chan_free(host->tx_chan);
910 au1xxx_dbdma_chan_free(host->rx_chan);
911 }
912}
Pete Popovba264b32005-09-21 06:18:27 +0000913
Manuel Lauss20f522f2008-06-09 08:38:03 +0200914static void au1xmmc_enable_sdio_irq(struct mmc_host *mmc, int en)
915{
916 struct au1xmmc_host *host = mmc_priv(mmc);
917
918 if (en)
919 IRQ_ON(host, SD_CONFIG_SI);
920 else
921 IRQ_OFF(host, SD_CONFIG_SI);
922}
923
Yoichi Yuasabf8c80a2006-12-05 07:43:38 +0100924static const struct mmc_host_ops au1xmmc_ops = {
Pete Popovba264b32005-09-21 06:18:27 +0000925 .request = au1xmmc_request,
926 .set_ios = au1xmmc_set_ios,
Manuel Lauss82999772007-01-25 10:29:24 +0100927 .get_ro = au1xmmc_card_readonly,
Manuel Lausse2d26472008-06-27 18:25:18 +0200928 .get_cd = au1xmmc_card_inserted,
Manuel Lauss20f522f2008-06-09 08:38:03 +0200929 .enable_sdio_irq = au1xmmc_enable_sdio_irq,
Pete Popovba264b32005-09-21 06:18:27 +0000930};
931
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500932static int au1xmmc_probe(struct platform_device *pdev)
Pete Popovba264b32005-09-21 06:18:27 +0000933{
Manuel Laussc4223c22008-06-09 08:36:13 +0200934 struct mmc_host *mmc;
935 struct au1xmmc_host *host;
936 struct resource *r;
Manuel Lauss809f36c2011-11-01 20:03:30 +0100937 int ret, iflag;
Pete Popovba264b32005-09-21 06:18:27 +0000938
Manuel Laussc4223c22008-06-09 08:36:13 +0200939 mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), &pdev->dev);
940 if (!mmc) {
941 dev_err(&pdev->dev, "no memory for mmc_host\n");
942 ret = -ENOMEM;
943 goto out0;
Pete Popovba264b32005-09-21 06:18:27 +0000944 }
945
Manuel Laussc4223c22008-06-09 08:36:13 +0200946 host = mmc_priv(mmc);
947 host->mmc = mmc;
948 host->platdata = pdev->dev.platform_data;
949 host->pdev = pdev;
Pete Popovba264b32005-09-21 06:18:27 +0000950
Manuel Laussc4223c22008-06-09 08:36:13 +0200951 ret = -ENODEV;
952 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
953 if (!r) {
954 dev_err(&pdev->dev, "no mmio defined\n");
955 goto out1;
956 }
Pete Popovba264b32005-09-21 06:18:27 +0000957
H Hartley Sweeten7a5ea56a2009-12-14 14:28:06 -0500958 host->ioarea = request_mem_region(r->start, resource_size(r),
Manuel Laussc4223c22008-06-09 08:36:13 +0200959 pdev->name);
960 if (!host->ioarea) {
961 dev_err(&pdev->dev, "mmio already in use\n");
962 goto out1;
963 }
964
Manuel Lauss2f73bfb2014-07-23 16:36:26 +0200965 host->iobase = ioremap(r->start, 0x3c);
Manuel Laussc4223c22008-06-09 08:36:13 +0200966 if (!host->iobase) {
967 dev_err(&pdev->dev, "cannot remap mmio\n");
968 goto out2;
969 }
970
Yangtao Li67d7d922019-12-15 17:51:20 +0000971 host->irq = platform_get_irq(pdev, 0);
972 if (host->irq < 0)
Manuel Laussc4223c22008-06-09 08:36:13 +0200973 goto out3;
Manuel Laussc4223c22008-06-09 08:36:13 +0200974
975 mmc->ops = &au1xmmc_ops;
976
977 mmc->f_min = 450000;
978 mmc->f_max = 24000000;
979
Manuel Laussc4223c22008-06-09 08:36:13 +0200980 mmc->max_blk_size = 2048;
981 mmc->max_blk_count = 512;
982
983 mmc->ocr_avail = AU1XMMC_OCR;
Manuel Lauss20f522f2008-06-09 08:38:03 +0200984 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
Manuel Lauss809f36c2011-11-01 20:03:30 +0100985 mmc->max_segs = AU1XMMC_DESCRIPTOR_COUNT;
986
987 iflag = IRQF_SHARED; /* Au1100/Au1200: one int for both ctrls */
988
989 switch (alchemy_get_cputype()) {
990 case ALCHEMY_CPU_AU1100:
991 mmc->max_seg_size = AU1100_MMC_DESCRIPTOR_SIZE;
992 break;
993 case ALCHEMY_CPU_AU1200:
994 mmc->max_seg_size = AU1200_MMC_DESCRIPTOR_SIZE;
995 break;
996 case ALCHEMY_CPU_AU1300:
997 iflag = 0; /* nothing is shared */
998 mmc->max_seg_size = AU1200_MMC_DESCRIPTOR_SIZE;
999 mmc->f_max = 52000000;
1000 if (host->ioarea->start == AU1100_SD0_PHYS_ADDR)
1001 mmc->caps |= MMC_CAP_8_BIT_DATA;
1002 break;
1003 }
1004
1005 ret = request_irq(host->irq, au1xmmc_irq, iflag, DRIVER_NAME, host);
1006 if (ret) {
1007 dev_err(&pdev->dev, "cannot grab IRQ\n");
1008 goto out3;
1009 }
Manuel Laussc4223c22008-06-09 08:36:13 +02001010
Manuel Laussb6507592014-07-23 16:36:56 +02001011 host->clk = clk_get(&pdev->dev, ALCHEMY_PERIPH_CLK);
1012 if (IS_ERR(host->clk)) {
1013 dev_err(&pdev->dev, "cannot find clock\n");
Wei Yongjunee9d19d2014-08-14 09:00:19 +08001014 ret = PTR_ERR(host->clk);
Manuel Laussb6507592014-07-23 16:36:56 +02001015 goto out_irq;
1016 }
Wei Yongjunee9d19d2014-08-14 09:00:19 +08001017
1018 ret = clk_prepare_enable(host->clk);
1019 if (ret) {
Manuel Laussb6507592014-07-23 16:36:56 +02001020 dev_err(&pdev->dev, "cannot enable clock\n");
1021 goto out_clk;
1022 }
1023
Manuel Laussc4223c22008-06-09 08:36:13 +02001024 host->status = HOST_S_IDLE;
1025
1026 /* board-specific carddetect setup, if any */
1027 if (host->platdata && host->platdata->cd_setup) {
1028 ret = host->platdata->cd_setup(mmc, 1);
1029 if (ret) {
Manuel Lausse2d26472008-06-27 18:25:18 +02001030 dev_warn(&pdev->dev, "board CD setup failed\n");
1031 mmc->caps |= MMC_CAP_NEEDS_POLL;
Pete Popovba264b32005-09-21 06:18:27 +00001032 }
Manuel Lausse2d26472008-06-27 18:25:18 +02001033 } else
1034 mmc->caps |= MMC_CAP_NEEDS_POLL;
Pete Popovba264b32005-09-21 06:18:27 +00001035
Manuel Lauss3b839072009-10-14 09:38:06 +02001036 /* platform may not be able to use all advertised caps */
1037 if (host->platdata)
1038 mmc->caps &= ~(host->platdata->mask_host_caps);
1039
Emil Renner Berthinga1149c32021-02-04 16:18:40 +01001040 tasklet_setup(&host->data_task, au1xmmc_tasklet_data);
Pete Popovba264b32005-09-21 06:18:27 +00001041
Emil Renner Berthinga1149c32021-02-04 16:18:40 +01001042 tasklet_setup(&host->finish_task, au1xmmc_tasklet_finish);
Pete Popovba264b32005-09-21 06:18:27 +00001043
Manuel Lauss1177d992011-08-02 19:51:07 +02001044 if (has_dbdma()) {
1045 ret = au1xmmc_dbdma_init(host);
1046 if (ret)
Linus Torvaldsd6748062011-11-03 13:28:14 -07001047 pr_info(DRIVER_NAME ": DBDMA init failed; using PIO\n");
Manuel Lauss1177d992011-08-02 19:51:07 +02001048 }
Pete Popovba264b32005-09-21 06:18:27 +00001049
Manuel Laussc4223c22008-06-09 08:36:13 +02001050#ifdef CONFIG_LEDS_CLASS
1051 if (host->platdata && host->platdata->led) {
1052 struct led_classdev *led = host->platdata->led;
1053 led->name = mmc_hostname(mmc);
1054 led->brightness = LED_OFF;
1055 led->default_trigger = mmc_hostname(mmc);
1056 ret = led_classdev_register(mmc_dev(mmc), led);
1057 if (ret)
1058 goto out5;
1059 }
1060#endif
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001061
Manuel Laussc4223c22008-06-09 08:36:13 +02001062 au1xmmc_reset_controller(host);
Pete Popovba264b32005-09-21 06:18:27 +00001063
Manuel Laussc4223c22008-06-09 08:36:13 +02001064 ret = mmc_add_host(mmc);
1065 if (ret) {
1066 dev_err(&pdev->dev, "cannot add mmc host\n");
1067 goto out6;
1068 }
Pete Popovba264b32005-09-21 06:18:27 +00001069
Manuel Laussdd8572a2008-07-17 13:07:28 +02001070 platform_set_drvdata(pdev, host);
Pete Popovba264b32005-09-21 06:18:27 +00001071
Manuel Lauss2f73bfb2014-07-23 16:36:26 +02001072 pr_info(DRIVER_NAME ": MMC Controller %d set up at %p"
Manuel Laussc4223c22008-06-09 08:36:13 +02001073 " (mode=%s)\n", pdev->id, host->iobase,
1074 host->flags & HOST_F_DMA ? "dma" : "pio");
Pete Popovba264b32005-09-21 06:18:27 +00001075
Manuel Laussc4223c22008-06-09 08:36:13 +02001076 return 0; /* all ok */
Pete Popovba264b32005-09-21 06:18:27 +00001077
Manuel Laussc4223c22008-06-09 08:36:13 +02001078out6:
1079#ifdef CONFIG_LEDS_CLASS
1080 if (host->platdata && host->platdata->led)
1081 led_classdev_unregister(host->platdata->led);
1082out5:
1083#endif
Manuel Lauss2f73bfb2014-07-23 16:36:26 +02001084 __raw_writel(0, HOST_ENABLE(host));
1085 __raw_writel(0, HOST_CONFIG(host));
1086 __raw_writel(0, HOST_CONFIG2(host));
1087 wmb(); /* drain writebuffer */
Manuel Laussc4223c22008-06-09 08:36:13 +02001088
Manuel Lauss1177d992011-08-02 19:51:07 +02001089 if (host->flags & HOST_F_DBDMA)
1090 au1xmmc_dbdma_shutdown(host);
Manuel Laussc4223c22008-06-09 08:36:13 +02001091
1092 tasklet_kill(&host->data_task);
1093 tasklet_kill(&host->finish_task);
1094
Manuel Lausse2d26472008-06-27 18:25:18 +02001095 if (host->platdata && host->platdata->cd_setup &&
1096 !(mmc->caps & MMC_CAP_NEEDS_POLL))
Manuel Laussc4223c22008-06-09 08:36:13 +02001097 host->platdata->cd_setup(mmc, 0);
Manuel Laussb6507592014-07-23 16:36:56 +02001098out_clk:
1099 clk_disable_unprepare(host->clk);
1100 clk_put(host->clk);
1101out_irq:
Manuel Laussc4223c22008-06-09 08:36:13 +02001102 free_irq(host->irq, host);
1103out3:
1104 iounmap((void *)host->iobase);
1105out2:
1106 release_resource(host->ioarea);
1107 kfree(host->ioarea);
1108out1:
1109 mmc_free_host(mmc);
1110out0:
1111 return ret;
Pete Popovba264b32005-09-21 06:18:27 +00001112}
1113
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001114static int au1xmmc_remove(struct platform_device *pdev)
Pete Popovba264b32005-09-21 06:18:27 +00001115{
Manuel Laussdd8572a2008-07-17 13:07:28 +02001116 struct au1xmmc_host *host = platform_get_drvdata(pdev);
Pete Popovba264b32005-09-21 06:18:27 +00001117
Manuel Laussdd8572a2008-07-17 13:07:28 +02001118 if (host) {
1119 mmc_remove_host(host->mmc);
Pete Popovba264b32005-09-21 06:18:27 +00001120
Manuel Laussc4223c22008-06-09 08:36:13 +02001121#ifdef CONFIG_LEDS_CLASS
1122 if (host->platdata && host->platdata->led)
1123 led_classdev_unregister(host->platdata->led);
1124#endif
1125
Manuel Lausse2d26472008-06-27 18:25:18 +02001126 if (host->platdata && host->platdata->cd_setup &&
Manuel Laussdd8572a2008-07-17 13:07:28 +02001127 !(host->mmc->caps & MMC_CAP_NEEDS_POLL))
1128 host->platdata->cd_setup(host->mmc, 0);
Manuel Laussc4223c22008-06-09 08:36:13 +02001129
Manuel Lauss2f73bfb2014-07-23 16:36:26 +02001130 __raw_writel(0, HOST_ENABLE(host));
1131 __raw_writel(0, HOST_CONFIG(host));
1132 __raw_writel(0, HOST_CONFIG2(host));
1133 wmb(); /* drain writebuffer */
Pete Popovba264b32005-09-21 06:18:27 +00001134
1135 tasklet_kill(&host->data_task);
1136 tasklet_kill(&host->finish_task);
1137
Manuel Lauss1177d992011-08-02 19:51:07 +02001138 if (host->flags & HOST_F_DBDMA)
1139 au1xmmc_dbdma_shutdown(host);
1140
Pete Popovba264b32005-09-21 06:18:27 +00001141 au1xmmc_set_power(host, 0);
1142
Manuel Laussb6507592014-07-23 16:36:56 +02001143 clk_disable_unprepare(host->clk);
1144 clk_put(host->clk);
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
Manuel Lauss2f73bfb2014-07-23 16:36:26 +02001161 __raw_writel(0, HOST_CONFIG2(host));
1162 __raw_writel(0, HOST_CONFIG(host));
1163 __raw_writel(0xffffffff, HOST_STATUS(host));
1164 __raw_writel(0, HOST_ENABLE(host));
1165 wmb(); /* drain writebuffer */
Manuel Laussdd8572a2008-07-17 13:07:28 +02001166
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,
Douglas Anderson21b2cec2020-09-03 16:24:36 -07001190 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
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");