blob: e7d80c83da2c837f6b85a49b2eef4f1377425048 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/pxa.c - PXA MMCI driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2003 Russell King, All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This hardware is really sick:
11 * - No way to clear interrupts.
12 * - Have to turn off the clock whenever we touch the device.
13 * - Doesn't tell you how many data blocks were transferred.
14 * Yuck!
15 *
16 * 1 and 3 byte data transfers not supported
17 * max block length up to 1023
18 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/ioport.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010022#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/delay.h>
24#include <linux/interrupt.h>
Daniel Mack6464b712015-06-06 23:15:22 +020025#include <linux/dmaengine.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/dma-mapping.h>
Russell Kingebebd9b2007-08-20 10:20:03 +010027#include <linux/clk.h>
28#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/mmc/host.h>
Robert Jarzmikfd546ee2015-09-26 21:41:01 +020030#include <linux/mmc/slot-gpio.h>
Russell King05678a92008-11-28 16:04:54 +000031#include <linux/io.h>
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -030032#include <linux/regulator/consumer.h>
Linus Walleijf54005b2018-12-02 09:43:27 +010033#include <linux/gpio/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/gfp.h>
Daniel Macke6027b42012-07-28 12:07:34 +020035#include <linux/of.h>
Daniel Macke6027b42012-07-28 12:07:34 +020036#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Masahiro Yamada87dfb312019-05-14 15:46:51 -070038#include <linux/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Russell King05678a92008-11-28 16:04:54 +000040#include <mach/hardware.h>
Arnd Bergmann293b2da2012-08-24 15:16:48 +020041#include <linux/platform_data/mmc-pxamci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "pxamci.h"
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define DRIVER_NAME "pxa2xx-mci"
46
47#define NR_SG 1
Russell Kingd8cb70d2007-10-26 17:56:40 +010048#define CLKRT_OFF (~0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Haojian Zhuangfa3f9932009-08-31 21:52:53 +080050#define mmc_has_26MHz() (cpu_is_pxa300() || cpu_is_pxa310() \
51 || cpu_is_pxa935())
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053struct pxamci_host {
54 struct mmc_host *mmc;
55 spinlock_t lock;
56 struct resource *res;
57 void __iomem *base;
Russell Kingebebd9b2007-08-20 10:20:03 +010058 struct clk *clk;
59 unsigned long clkrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 unsigned int clkrt;
61 unsigned int cmdat;
62 unsigned int imask;
63 unsigned int power_mode;
Daniel Mack38a8dda2018-07-03 22:10:26 +020064 unsigned long detect_delay_ms;
Linus Walleijc914a272018-12-02 09:43:24 +010065 bool use_ro_gpio;
Linus Walleijf54005b2018-12-02 09:43:27 +010066 struct gpio_desc *power;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 struct pxamci_platform_data *pdata;
68
69 struct mmc_request *mrq;
70 struct mmc_command *cmd;
71 struct mmc_data *data;
72
Daniel Mack6464b712015-06-06 23:15:22 +020073 struct dma_chan *dma_chan_rx;
74 struct dma_chan *dma_chan_tx;
75 dma_cookie_t dma_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 unsigned int dma_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 unsigned int dma_dir;
78};
79
Daniel Mack61951fd2018-06-30 20:14:05 +020080static int pxamci_init_ocr(struct pxamci_host *host)
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -030081{
Daniel Mack61951fd2018-06-30 20:14:05 +020082 struct mmc_host *mmc = host->mmc;
83 int ret;
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -030084
Daniel Mack61951fd2018-06-30 20:14:05 +020085 ret = mmc_regulator_get_supply(mmc);
86 if (ret < 0)
87 return ret;
88
89 if (IS_ERR(mmc->supply.vmmc)) {
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -030090 /* fall-back to platform data */
Daniel Mack61951fd2018-06-30 20:14:05 +020091 mmc->ocr_avail = host->pdata ?
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -030092 host->pdata->ocr_mask :
93 MMC_VDD_32_33 | MMC_VDD_33_34;
94 }
Daniel Mack61951fd2018-06-30 20:14:05 +020095
96 return 0;
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -030097}
98
Linus Walleij99fc5132010-09-29 01:08:27 -040099static inline int pxamci_set_power(struct pxamci_host *host,
100 unsigned char power_mode,
101 unsigned int vdd)
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -0300102{
Daniel Mack61951fd2018-06-30 20:14:05 +0200103 struct mmc_host *mmc = host->mmc;
104 struct regulator *supply = mmc->supply.vmmc;
Robert Jarzmikb405db62009-06-23 23:21:03 +0200105
Daniel Mack61951fd2018-06-30 20:14:05 +0200106 if (!IS_ERR(supply))
107 return mmc_regulator_set_ocr(mmc, supply, vdd);
Linus Walleij99fc5132010-09-29 01:08:27 -0400108
Linus Walleijf54005b2018-12-02 09:43:27 +0100109 if (host->power) {
110 bool on = !!((1 << vdd) & host->pdata->ocr_mask);
111 gpiod_set_value(host->power, on);
Robert Jarzmikb405db62009-06-23 23:21:03 +0200112 }
Daniel Mack61951fd2018-06-30 20:14:05 +0200113
114 if (host->pdata && host->pdata->setpower)
Arnd Bergmanna829abf2013-07-05 17:51:20 +0200115 return host->pdata->setpower(mmc_dev(host->mmc), vdd);
Linus Walleij99fc5132010-09-29 01:08:27 -0400116
117 return 0;
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -0300118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static void pxamci_stop_clock(struct pxamci_host *host)
121{
122 if (readl(host->base + MMC_STAT) & STAT_CLK_EN) {
123 unsigned long timeout = 10000;
124 unsigned int v;
125
126 writel(STOP_CLOCK, host->base + MMC_STRPCL);
127
128 do {
129 v = readl(host->base + MMC_STAT);
130 if (!(v & STAT_CLK_EN))
131 break;
132 udelay(1);
133 } while (timeout--);
134
135 if (v & STAT_CLK_EN)
136 dev_err(mmc_dev(host->mmc), "unable to stop clock\n");
137 }
138}
139
140static void pxamci_enable_irq(struct pxamci_host *host, unsigned int mask)
141{
142 unsigned long flags;
143
144 spin_lock_irqsave(&host->lock, flags);
145 host->imask &= ~mask;
146 writel(host->imask, host->base + MMC_I_MASK);
147 spin_unlock_irqrestore(&host->lock, flags);
148}
149
150static void pxamci_disable_irq(struct pxamci_host *host, unsigned int mask)
151{
152 unsigned long flags;
153
154 spin_lock_irqsave(&host->lock, flags);
155 host->imask |= mask;
156 writel(host->imask, host->base + MMC_I_MASK);
157 spin_unlock_irqrestore(&host->lock, flags);
158}
159
Daniel Mack6464b712015-06-06 23:15:22 +0200160static void pxamci_dma_irq(void *param);
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data)
163{
Daniel Mack6464b712015-06-06 23:15:22 +0200164 struct dma_async_tx_descriptor *tx;
Arnd Bergmanne60a5822019-03-07 11:09:19 +0100165 enum dma_transfer_direction direction;
Daniel Mack6464b712015-06-06 23:15:22 +0200166 struct dma_slave_config config;
167 struct dma_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned int nob = data->blocks;
Russell King3d63abe2006-04-24 11:27:02 +0100169 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 unsigned int timeout;
Daniel Mack6464b712015-06-06 23:15:22 +0200171 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 host->data = data;
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 writel(nob, host->base + MMC_NOB);
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100176 writel(data->blksz, host->base + MMC_BLKLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Russell Kingebebd9b2007-08-20 10:20:03 +0100178 clks = (unsigned long long)data->timeout_ns * host->clkrate;
Russell King3d63abe2006-04-24 11:27:02 +0100179 do_div(clks, 1000000000UL);
180 timeout = (unsigned int)clks + (data->timeout_clks << host->clkrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 writel((timeout + 255) / 256, host->base + MMC_RDTO);
182
Daniel Mack6464b712015-06-06 23:15:22 +0200183 memset(&config, 0, sizeof(config));
184 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
185 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
186 config.src_addr = host->res->start + MMC_RXFIFO;
187 config.dst_addr = host->res->start + MMC_TXFIFO;
188 config.src_maxburst = 32;
189 config.dst_maxburst = 32;
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (data->flags & MMC_DATA_READ) {
192 host->dma_dir = DMA_FROM_DEVICE;
Daniel Mack6464b712015-06-06 23:15:22 +0200193 direction = DMA_DEV_TO_MEM;
194 chan = host->dma_chan_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 } else {
196 host->dma_dir = DMA_TO_DEVICE;
Daniel Mack6464b712015-06-06 23:15:22 +0200197 direction = DMA_MEM_TO_DEV;
198 chan = host->dma_chan_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200
Daniel Mack6464b712015-06-06 23:15:22 +0200201 config.direction = direction;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Daniel Mack6464b712015-06-06 23:15:22 +0200203 ret = dmaengine_slave_config(chan, &config);
204 if (ret < 0) {
205 dev_err(mmc_dev(host->mmc), "dma slave config failed\n");
206 return;
207 }
208
209 host->dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 host->dma_dir);
211
Daniel Mack6464b712015-06-06 23:15:22 +0200212 tx = dmaengine_prep_slave_sg(chan, data->sg, host->dma_len, direction,
213 DMA_PREP_INTERRUPT);
214 if (!tx) {
215 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
216 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Daniel Mack6464b712015-06-06 23:15:22 +0200219 if (!(data->flags & MMC_DATA_READ)) {
220 tx->callback = pxamci_dma_irq;
221 tx->callback_param = host;
222 }
223
224 host->dma_cookie = dmaengine_submit(tx);
Cliff Brakeb6018952009-01-22 17:07:03 -0500225
226 /*
227 * workaround for erratum #91:
228 * only start DMA now if we are doing a read,
229 * otherwise we wait until CMD/RESP has finished
230 * before starting DMA.
231 */
232 if (!cpu_is_pxa27x() || data->flags & MMC_DATA_READ)
Daniel Mack6464b712015-06-06 23:15:22 +0200233 dma_async_issue_pending(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236static void pxamci_start_cmd(struct pxamci_host *host, struct mmc_command *cmd, unsigned int cmdat)
237{
238 WARN_ON(host->cmd != NULL);
239 host->cmd = cmd;
240
241 if (cmd->flags & MMC_RSP_BUSY)
242 cmdat |= CMDAT_BUSY;
243
Russell Kinge9225172006-02-02 12:23:12 +0000244#define RSP_TYPE(x) ((x) & ~(MMC_RSP_BUSY|MMC_RSP_OPCODE))
245 switch (RSP_TYPE(mmc_resp_type(cmd))) {
Philip Langdale6f949902007-01-04 07:04:47 -0800246 case RSP_TYPE(MMC_RSP_R1): /* r1, r1b, r6, r7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 cmdat |= CMDAT_RESP_SHORT;
248 break;
Russell Kinge9225172006-02-02 12:23:12 +0000249 case RSP_TYPE(MMC_RSP_R3):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 cmdat |= CMDAT_RESP_R3;
251 break;
Russell Kinge9225172006-02-02 12:23:12 +0000252 case RSP_TYPE(MMC_RSP_R2):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 cmdat |= CMDAT_RESP_R2;
254 break;
255 default:
256 break;
257 }
258
259 writel(cmd->opcode, host->base + MMC_CMD);
260 writel(cmd->arg >> 16, host->base + MMC_ARGH);
261 writel(cmd->arg & 0xffff, host->base + MMC_ARGL);
262 writel(cmdat, host->base + MMC_CMDAT);
263 writel(host->clkrt, host->base + MMC_CLKRT);
264
265 writel(START_CLOCK, host->base + MMC_STRPCL);
266
267 pxamci_enable_irq(host, END_CMD_RES);
268}
269
270static void pxamci_finish_request(struct pxamci_host *host, struct mmc_request *mrq)
271{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 host->mrq = NULL;
273 host->cmd = NULL;
274 host->data = NULL;
275 mmc_request_done(host->mmc, mrq);
276}
277
278static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat)
279{
280 struct mmc_command *cmd = host->cmd;
281 int i;
282 u32 v;
283
284 if (!cmd)
285 return 0;
286
287 host->cmd = NULL;
288
289 /*
290 * Did I mention this is Sick. We always need to
291 * discard the upper 8 bits of the first 16-bit word.
292 */
293 v = readl(host->base + MMC_RES) & 0xffff;
294 for (i = 0; i < 4; i++) {
295 u32 w1 = readl(host->base + MMC_RES) & 0xffff;
296 u32 w2 = readl(host->base + MMC_RES) & 0xffff;
297 cmd->resp[i] = v << 24 | w1 << 8 | w2 >> 8;
298 v = w2;
299 }
300
301 if (stat & STAT_TIME_OUT_RESPONSE) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200302 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 } else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /*
305 * workaround for erratum #42:
306 * Intel PXA27x Family Processor Specification Update Rev 001
Nicolas Pitre90e07d92007-05-13 18:03:08 +0200307 * A bogus CRC error can appear if the msb of a 136 bit
308 * response is a one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 */
Cliff Brakee10a8542009-01-22 16:58:58 -0500310 if (cpu_is_pxa27x() &&
311 (cmd->flags & MMC_RSP_136 && cmd->resp[0] & 0x80000000))
Nicolas Pitre90e07d92007-05-13 18:03:08 +0200312 pr_debug("ignoring CRC from command %d - *risky*\n", cmd->opcode);
Cliff Brakee10a8542009-01-22 16:58:58 -0500313 else
314 cmd->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316
317 pxamci_disable_irq(host, END_CMD_RES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200318 if (host->data && !cmd->error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 pxamci_enable_irq(host, DATA_TRAN_DONE);
Cliff Brakeb6018952009-01-22 17:07:03 -0500320 /*
321 * workaround for erratum #91, if doing write
322 * enable DMA late
323 */
324 if (cpu_is_pxa27x() && host->data->flags & MMC_DATA_WRITE)
Daniel Mack6464b712015-06-06 23:15:22 +0200325 dma_async_issue_pending(host->dma_chan_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 } else {
327 pxamci_finish_request(host, host->mrq);
328 }
329
330 return 1;
331}
332
333static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
334{
335 struct mmc_data *data = host->data;
Daniel Mack6464b712015-06-06 23:15:22 +0200336 struct dma_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 if (!data)
339 return 0;
340
Daniel Mack6464b712015-06-06 23:15:22 +0200341 if (data->flags & MMC_DATA_READ)
342 chan = host->dma_chan_rx;
343 else
344 chan = host->dma_chan_tx;
345 dma_unmap_sg(chan->device->dev,
346 data->sg, data->sg_len, host->dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 if (stat & STAT_READ_TIME_OUT)
Pierre Ossman17b04292007-07-22 22:18:46 +0200349 data->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR))
Pierre Ossman17b04292007-07-22 22:18:46 +0200351 data->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /*
354 * There appears to be a hardware design bug here. There seems to
355 * be no way to find out how much data was transferred to the card.
356 * This means that if there was an error on any block, we mark all
357 * data blocks as being in error.
358 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200359 if (!data->error)
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100360 data->bytes_xfered = data->blocks * data->blksz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 else
362 data->bytes_xfered = 0;
363
364 pxamci_disable_irq(host, DATA_TRAN_DONE);
365
366 host->data = NULL;
Russell King58741e82006-05-02 20:02:39 +0100367 if (host->mrq->stop) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 pxamci_stop_clock(host);
Bridge Wudf456f42007-09-25 19:09:19 +0200369 pxamci_start_cmd(host, host->mrq->stop, host->cmdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 } else {
371 pxamci_finish_request(host, host->mrq);
372 }
373
374 return 1;
375}
376
David Howells7d12e782006-10-05 14:55:46 +0100377static irqreturn_t pxamci_irq(int irq, void *devid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 struct pxamci_host *host = devid;
380 unsigned int ireg;
381 int handled = 0;
382
Bridge Wu81ab570f2007-09-25 18:59:07 +0200383 ireg = readl(host->base + MMC_I_REG) & ~readl(host->base + MMC_I_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (ireg) {
386 unsigned stat = readl(host->base + MMC_STAT);
387
Russell Kingd78e9072006-05-02 20:18:53 +0100388 pr_debug("PXAMCI: irq %08x stat %08x\n", ireg, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 if (ireg & END_CMD_RES)
391 handled |= pxamci_cmd_done(host, stat);
392 if (ireg & DATA_TRAN_DONE)
393 handled |= pxamci_data_done(host, stat);
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200394 if (ireg & SDIO_INT) {
395 mmc_signal_sdio_irq(host->mmc);
396 handled = 1;
397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
400 return IRQ_RETVAL(handled);
401}
402
403static void pxamci_request(struct mmc_host *mmc, struct mmc_request *mrq)
404{
405 struct pxamci_host *host = mmc_priv(mmc);
406 unsigned int cmdat;
407
408 WARN_ON(host->mrq != NULL);
409
410 host->mrq = mrq;
411
412 pxamci_stop_clock(host);
413
414 cmdat = host->cmdat;
415 host->cmdat &= ~CMDAT_INIT;
416
417 if (mrq->data) {
418 pxamci_setup_data(host, mrq->data);
419
420 cmdat &= ~CMDAT_BUSY;
421 cmdat |= CMDAT_DATAEN | CMDAT_DMAEN;
422 if (mrq->data->flags & MMC_DATA_WRITE)
423 cmdat |= CMDAT_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
426 pxamci_start_cmd(host, mrq->cmd, cmdat);
427}
428
Richard Purdiee6195242005-09-06 15:18:56 -0700429static int pxamci_get_ro(struct mmc_host *mmc)
430{
431 struct pxamci_host *host = mmc_priv(mmc);
432
Linus Walleijc914a272018-12-02 09:43:24 +0100433 if (host->use_ro_gpio)
Robert Jarzmikfd546ee2015-09-26 21:41:01 +0200434 return mmc_gpio_get_ro(mmc);
Richard Purdiee6195242005-09-06 15:18:56 -0700435 if (host->pdata && host->pdata->get_ro)
Anton Vorontsov08f80bb2008-06-17 18:17:39 +0400436 return !!host->pdata->get_ro(mmc_dev(mmc));
437 /*
438 * Board doesn't support read only detection; let the mmc core
439 * decide what to do.
440 */
441 return -ENOSYS;
Richard Purdiee6195242005-09-06 15:18:56 -0700442}
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
445{
446 struct pxamci_host *host = mmc_priv(mmc);
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (ios->clock) {
Russell Kingebebd9b2007-08-20 10:20:03 +0100449 unsigned long rate = host->clkrate;
450 unsigned int clk = rate / ios->clock;
451
Russell Kingd8cb70d2007-10-26 17:56:40 +0100452 if (host->clkrt == CLKRT_OFF)
Robert Jarzmike7370812014-09-02 11:23:55 +0200453 clk_prepare_enable(host->clk);
Russell Kingd8cb70d2007-10-26 17:56:40 +0100454
Bridge Wu64eb0362007-12-13 07:24:30 +0100455 if (ios->clock == 26000000) {
Haojian Zhuangfa3f9932009-08-31 21:52:53 +0800456 /* to support 26MHz */
Bridge Wu64eb0362007-12-13 07:24:30 +0100457 host->clkrt = 7;
458 } else {
459 /* to handle (19.5MHz, 26MHz) */
460 if (!clk)
461 clk = 1;
462
463 /*
464 * clk might result in a lower divisor than we
465 * desire. check for that condition and adjust
466 * as appropriate.
467 */
468 if (rate / clk > ios->clock)
469 clk <<= 1;
470 host->clkrt = fls(clk) - 1;
471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /*
474 * we write clkrt on the next command
475 */
476 } else {
477 pxamci_stop_clock(host);
Russell Kingd8cb70d2007-10-26 17:56:40 +0100478 if (host->clkrt != CLKRT_OFF) {
479 host->clkrt = CLKRT_OFF;
Robert Jarzmike7370812014-09-02 11:23:55 +0200480 clk_disable_unprepare(host->clk);
Russell Kingd8cb70d2007-10-26 17:56:40 +0100481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483
484 if (host->power_mode != ios->power_mode) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400485 int ret;
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 host->power_mode = ios->power_mode;
488
Linus Walleij99fc5132010-09-29 01:08:27 -0400489 ret = pxamci_set_power(host, ios->power_mode, ios->vdd);
490 if (ret) {
491 dev_err(mmc_dev(mmc), "unable to set power\n");
492 /*
493 * The .set_ios() function in the mmc_host_ops
494 * struct return void, and failing to set the
495 * power should be rare so we print an error and
496 * return here.
497 */
498 return;
499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if (ios->power_mode == MMC_POWER_ON)
502 host->cmdat |= CMDAT_INIT;
503 }
504
Bridge Wudf456f42007-09-25 19:09:19 +0200505 if (ios->bus_width == MMC_BUS_WIDTH_4)
506 host->cmdat |= CMDAT_SD_4DAT;
507 else
508 host->cmdat &= ~CMDAT_SD_4DAT;
509
Linus Walleij99fc5132010-09-29 01:08:27 -0400510 dev_dbg(mmc_dev(mmc), "PXAMCI: clkrt = %x cmdat = %x\n",
511 host->clkrt, host->cmdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200514static void pxamci_enable_sdio_irq(struct mmc_host *host, int enable)
515{
516 struct pxamci_host *pxa_host = mmc_priv(host);
517
518 if (enable)
519 pxamci_enable_irq(pxa_host, SDIO_INT);
520 else
521 pxamci_disable_irq(pxa_host, SDIO_INT);
522}
523
David Brownellab7aefd2006-11-12 17:55:30 -0800524static const struct mmc_host_ops pxamci_ops = {
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200525 .request = pxamci_request,
Robert Jarzmikfd546ee2015-09-26 21:41:01 +0200526 .get_cd = mmc_gpio_get_cd,
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200527 .get_ro = pxamci_get_ro,
528 .set_ios = pxamci_set_ios,
529 .enable_sdio_irq = pxamci_enable_sdio_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530};
531
Daniel Mack6464b712015-06-06 23:15:22 +0200532static void pxamci_dma_irq(void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Daniel Mack6464b712015-06-06 23:15:22 +0200534 struct pxamci_host *host = param;
535 struct dma_tx_state state;
536 enum dma_status status;
537 struct dma_chan *chan;
538 unsigned long flags;
Nicolas Pitrec7838372007-10-09 17:07:58 -0400539
Daniel Mack6464b712015-06-06 23:15:22 +0200540 spin_lock_irqsave(&host->lock, flags);
541
542 if (!host->data)
543 goto out_unlock;
544
545 if (host->data->flags & MMC_DATA_READ)
546 chan = host->dma_chan_rx;
547 else
548 chan = host->dma_chan_tx;
549
550 status = dmaengine_tx_status(chan, host->dma_cookie, &state);
551
552 if (likely(status == DMA_COMPLETE)) {
Nicolas Pitrec7838372007-10-09 17:07:58 -0400553 writel(BUF_PART_FULL, host->base + MMC_PRTBUF);
554 } else {
Daniel Mack6464b712015-06-06 23:15:22 +0200555 pr_err("%s: DMA error on %s channel\n", mmc_hostname(host->mmc),
556 host->data->flags & MMC_DATA_READ ? "rx" : "tx");
Nicolas Pitrec7838372007-10-09 17:07:58 -0400557 host->data->error = -EIO;
558 pxamci_data_done(host, 0);
559 }
Daniel Mack6464b712015-06-06 23:15:22 +0200560
561out_unlock:
562 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
David Howells7d12e782006-10-05 14:55:46 +0100565static irqreturn_t pxamci_detect_irq(int irq, void *devid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Richard Purdiec26971c2005-09-08 22:48:16 +0100567 struct pxamci_host *host = mmc_priv(devid);
568
Daniel Mack38a8dda2018-07-03 22:10:26 +0200569 mmc_detect_change(devid, msecs_to_jiffies(host->detect_delay_ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return IRQ_HANDLED;
571}
572
Daniel Macke6027b42012-07-28 12:07:34 +0200573#ifdef CONFIG_OF
574static const struct of_device_id pxa_mmc_dt_ids[] = {
575 { .compatible = "marvell,pxa-mmc" },
576 { }
577};
578
579MODULE_DEVICE_TABLE(of, pxa_mmc_dt_ids);
580
Daniel Mackfa3a5112018-06-30 20:14:03 +0200581static int pxamci_of_init(struct platform_device *pdev,
582 struct mmc_host *mmc)
Daniel Macke6027b42012-07-28 12:07:34 +0200583{
Daniel Mack0da53582018-06-30 20:14:02 +0200584 struct device_node *np = pdev->dev.of_node;
Daniel Mack38a8dda2018-07-03 22:10:26 +0200585 struct pxamci_host *host = mmc_priv(mmc);
Daniel Mack0da53582018-06-30 20:14:02 +0200586 u32 tmp;
Daniel Mackfa3a5112018-06-30 20:14:03 +0200587 int ret;
Daniel Macke6027b42012-07-28 12:07:34 +0200588
Daniel Mack0da53582018-06-30 20:14:02 +0200589 if (!np)
590 return 0;
Daniel Macke6027b42012-07-28 12:07:34 +0200591
Daniel Macke6027b42012-07-28 12:07:34 +0200592 /* pxa-mmc specific */
Daniel Macke6027b42012-07-28 12:07:34 +0200593 if (of_property_read_u32(np, "pxa-mmc,detect-delay-ms", &tmp) == 0)
Daniel Mack38a8dda2018-07-03 22:10:26 +0200594 host->detect_delay_ms = tmp;
Daniel Macke6027b42012-07-28 12:07:34 +0200595
Daniel Mackfa3a5112018-06-30 20:14:03 +0200596 ret = mmc_of_parse(mmc);
597 if (ret < 0)
598 return ret;
599
Daniel Mack0da53582018-06-30 20:14:02 +0200600 return 0;
Daniel Macke6027b42012-07-28 12:07:34 +0200601}
602#else
Daniel Mackfa3a5112018-06-30 20:14:03 +0200603static int pxamci_of_init(struct platform_device *pdev,
604 struct mmc_host *mmc)
Daniel Macke6027b42012-07-28 12:07:34 +0200605{
606 return 0;
607}
608#endif
609
Russell King3ae5eae2005-11-09 22:32:44 +0000610static int pxamci_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 struct mmc_host *mmc;
613 struct pxamci_host *host = NULL;
Daniel Mack23f3ff72018-07-03 22:10:27 +0200614 struct device *dev = &pdev->dev;
Robert Jarzmik6b3348f9e2018-06-17 19:02:07 +0200615 struct resource *r;
Daniel Mack38a8dda2018-07-03 22:10:26 +0200616 int ret, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
619 irq = platform_get_irq(pdev, 0);
Robert Jarzmik07e77162016-02-08 15:17:57 +0100620 if (irq < 0)
621 return irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Daniel Mack23f3ff72018-07-03 22:10:27 +0200623 mmc = mmc_alloc_host(sizeof(struct pxamci_host), dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (!mmc) {
625 ret = -ENOMEM;
626 goto out;
627 }
628
629 mmc->ops = &pxamci_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 /*
632 * We can do SG-DMA, but we don't because we never know how much
633 * data we successfully wrote to the card.
634 */
Martin K. Petersena36274e2010-09-10 01:33:59 -0400635 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 /*
638 * Our hardware DMA can handle a maximum of one page per SG entry.
639 */
640 mmc->max_seg_size = PAGE_SIZE;
641
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100642 /*
Nicolas Pitrefe2dc442007-09-24 15:47:18 -0400643 * Block length register is only 10 bits before PXA27x.
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100644 */
Eric Miao0ffcbfd2008-09-11 10:27:30 +0800645 mmc->max_blk_size = cpu_is_pxa25x() ? 1023 : 2048;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100646
Pierre Ossman55db8902006-11-21 17:55:45 +0100647 /*
648 * Block count register is 16 bits.
649 */
650 mmc->max_blk_count = 65535;
651
Daniel Mackfa3a5112018-06-30 20:14:03 +0200652 ret = pxamci_of_init(pdev, mmc);
653 if (ret)
654 return ret;
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 host = mmc_priv(mmc);
657 host->mmc = mmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 host->pdata = pdev->dev.platform_data;
Russell Kingd8cb70d2007-10-26 17:56:40 +0100659 host->clkrt = CLKRT_OFF;
Russell Kingebebd9b2007-08-20 10:20:03 +0100660
Daniel Mack23f3ff72018-07-03 22:10:27 +0200661 host->clk = devm_clk_get(dev, NULL);
Russell Kingebebd9b2007-08-20 10:20:03 +0100662 if (IS_ERR(host->clk)) {
663 ret = PTR_ERR(host->clk);
664 host->clk = NULL;
665 goto out;
666 }
667
668 host->clkrate = clk_get_rate(host->clk);
669
670 /*
671 * Calculate minimum clock rate, rounding up.
672 */
673 mmc->f_min = (host->clkrate + 63) / 64;
Haojian Zhuangfa3f9932009-08-31 21:52:53 +0800674 mmc->f_max = (mmc_has_26MHz()) ? 26000000 : host->clkrate;
Russell Kingebebd9b2007-08-20 10:20:03 +0100675
Daniel Mack61951fd2018-06-30 20:14:05 +0200676 ret = pxamci_init_ocr(host);
677 if (ret < 0)
678 return ret;
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -0300679
Linus Walleijde3ee992017-09-20 10:56:14 +0200680 mmc->caps = 0;
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200681 host->cmdat = 0;
Eric Miao0ffcbfd2008-09-11 10:27:30 +0800682 if (!cpu_is_pxa25x()) {
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200683 mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
684 host->cmdat |= CMDAT_SDIO_INT_EN;
Haojian Zhuangfa3f9932009-08-31 21:52:53 +0800685 if (mmc_has_26MHz())
Bridge Wu64eb0362007-12-13 07:24:30 +0100686 mmc->caps |= MMC_CAP_MMC_HIGHSPEED |
687 MMC_CAP_SD_HIGHSPEED;
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 spin_lock_init(&host->lock);
691 host->res = r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 host->imask = MMC_I_MASK_ALL;
693
Daniel Mack23f3ff72018-07-03 22:10:27 +0200694 host->base = devm_ioremap_resource(dev, r);
Robert Jarzmik07e77162016-02-08 15:17:57 +0100695 if (IS_ERR(host->base)) {
696 ret = PTR_ERR(host->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 goto out;
698 }
699
700 /*
701 * Ensure that the host controller is shut down, and setup
702 * with our defaults.
703 */
704 pxamci_stop_clock(host);
705 writel(0, host->base + MMC_SPI);
706 writel(64, host->base + MMC_RESTO);
707 writel(host->imask, host->base + MMC_I_MASK);
708
Daniel Mack23f3ff72018-07-03 22:10:27 +0200709 ret = devm_request_irq(dev, irq, pxamci_irq, 0,
Robert Jarzmik07e77162016-02-08 15:17:57 +0100710 DRIVER_NAME, host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (ret)
712 goto out;
713
Russell King3ae5eae2005-11-09 22:32:44 +0000714 platform_set_drvdata(pdev, mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Daniel Mack23f3ff72018-07-03 22:10:27 +0200716 host->dma_chan_rx = dma_request_slave_channel(dev, "rx");
Daniel Mack6464b712015-06-06 23:15:22 +0200717 if (host->dma_chan_rx == NULL) {
Daniel Mack23f3ff72018-07-03 22:10:27 +0200718 dev_err(dev, "unable to request rx dma channel\n");
Daniel Mack6464b712015-06-06 23:15:22 +0200719 ret = -ENODEV;
Bridge Wu9a788c62007-12-14 10:40:25 +0100720 goto out;
721 }
Daniel Mack6464b712015-06-06 23:15:22 +0200722
Daniel Mack23f3ff72018-07-03 22:10:27 +0200723 host->dma_chan_tx = dma_request_slave_channel(dev, "tx");
Daniel Mack6464b712015-06-06 23:15:22 +0200724 if (host->dma_chan_tx == NULL) {
Daniel Mack23f3ff72018-07-03 22:10:27 +0200725 dev_err(dev, "unable to request tx dma channel\n");
Daniel Mack6464b712015-06-06 23:15:22 +0200726 ret = -ENODEV;
727 goto out;
728 }
Bridge Wu9a788c62007-12-14 10:40:25 +0100729
Robert Jarzmikb405db62009-06-23 23:21:03 +0200730 if (host->pdata) {
Daniel Mack38a8dda2018-07-03 22:10:26 +0200731 host->detect_delay_ms = host->pdata->detect_delay_ms;
732
Linus Walleijf54005b2018-12-02 09:43:27 +0100733 host->power = devm_gpiod_get_optional(dev, "power", GPIOD_OUT_LOW);
734 if (IS_ERR(host->power)) {
735 dev_err(dev, "Failed requesting gpio_power\n");
736 goto out;
Daniel Mack38a8dda2018-07-03 22:10:26 +0200737 }
738
Linus Walleijc914a272018-12-02 09:43:24 +0100739 /* FIXME: should we pass detection delay to debounce? */
740 ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0, NULL);
741 if (ret && ret != -ENOENT) {
742 dev_err(dev, "Failed requesting gpio_cd\n");
743 goto out;
744 }
Linus Walleijc914a272018-12-02 09:43:24 +0100745
Linus Walleija2b760a2019-02-05 10:30:22 +0100746 ret = mmc_gpiod_request_ro(mmc, "wp", 0, 0, NULL);
Linus Walleijc914a272018-12-02 09:43:24 +0100747 if (ret && ret != -ENOENT) {
748 dev_err(dev, "Failed requesting gpio_ro\n");
749 goto out;
750 }
Linus Walleijc914a272018-12-02 09:43:24 +0100751 if (!ret) {
752 host->use_ro_gpio = true;
753 mmc->caps2 |= host->pdata->gpio_card_ro_invert ?
754 0 : MMC_CAP2_RO_ACTIVE_HIGH;
Robert Jarzmikb405db62009-06-23 23:21:03 +0200755 }
Robert Jarzmikb405db62009-06-23 23:21:03 +0200756
Daniel Mack38a8dda2018-07-03 22:10:26 +0200757 if (host->pdata->init)
Daniel Mack23f3ff72018-07-03 22:10:27 +0200758 host->pdata->init(dev, pxamci_detect_irq, mmc);
Daniel Mack38a8dda2018-07-03 22:10:26 +0200759
Linus Walleijf54005b2018-12-02 09:43:27 +0100760 if (host->power && host->pdata->setpower)
Daniel Mack23f3ff72018-07-03 22:10:27 +0200761 dev_warn(dev, "gpio_power and setpower() both defined\n");
Linus Walleijc914a272018-12-02 09:43:24 +0100762 if (host->use_ro_gpio && host->pdata->get_ro)
Daniel Mack23f3ff72018-07-03 22:10:27 +0200763 dev_warn(dev, "gpio_ro and get_ro() both defined\n");
Robert Jarzmikb405db62009-06-23 23:21:03 +0200764 }
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 mmc_add_host(mmc);
767
768 return 0;
769
Robert Jarzmikfd546ee2015-09-26 21:41:01 +0200770out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (host) {
Daniel Mack6464b712015-06-06 23:15:22 +0200772 if (host->dma_chan_rx)
773 dma_release_channel(host->dma_chan_rx);
774 if (host->dma_chan_tx)
775 dma_release_channel(host->dma_chan_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777 if (mmc)
778 mmc_free_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return ret;
780}
781
Russell King3ae5eae2005-11-09 22:32:44 +0000782static int pxamci_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Russell King3ae5eae2005-11-09 22:32:44 +0000784 struct mmc_host *mmc = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (mmc) {
787 struct pxamci_host *host = mmc_priv(mmc);
788
Daniel Mack5d6b1edf2009-12-01 18:17:18 +0100789 mmc_remove_host(mmc);
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (host->pdata && host->pdata->exit)
Russell King3ae5eae2005-11-09 22:32:44 +0000792 host->pdata->exit(&pdev->dev, mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 pxamci_stop_clock(host);
795 writel(TXFIFO_WR_REQ|RXFIFO_RD_REQ|CLK_IS_OFF|STOP_CMD|
796 END_CMD_RES|PRG_DONE|DATA_TRAN_DONE,
797 host->base + MMC_I_MASK);
798
Daniel Mack6464b712015-06-06 23:15:22 +0200799 dmaengine_terminate_all(host->dma_chan_rx);
800 dmaengine_terminate_all(host->dma_chan_tx);
801 dma_release_channel(host->dma_chan_rx);
802 dma_release_channel(host->dma_chan_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 mmc_free_host(mmc);
805 }
Daniel Mack52c09182018-06-30 20:14:01 +0200806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return 0;
808}
809
Russell King3ae5eae2005-11-09 22:32:44 +0000810static struct platform_driver pxamci_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 .probe = pxamci_probe,
812 .remove = pxamci_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000813 .driver = {
814 .name = DRIVER_NAME,
Daniel Macke6027b42012-07-28 12:07:34 +0200815 .of_match_table = of_match_ptr(pxa_mmc_dt_ids),
Russell King3ae5eae2005-11-09 22:32:44 +0000816 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817};
818
Axel Lind1f81a62011-11-26 12:55:43 +0800819module_platform_driver(pxamci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver");
822MODULE_LICENSE("GPL");
Kay Sieversbc65c722008-04-15 14:34:28 -0700823MODULE_ALIAS("platform:pxa2xx-mci");