blob: 8cadd74e8407bb08d7e277a82ac5d80d496f77e4 [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>
Daniel Mack6464b712015-06-06 23:15:22 +020027#include <linux/dma/pxa-dma.h>
Russell Kingebebd9b2007-08-20 10:20:03 +010028#include <linux/clk.h>
29#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/mmc/host.h>
Robert Jarzmikfd546ee2015-09-26 21:41:01 +020031#include <linux/mmc/slot-gpio.h>
Russell King05678a92008-11-28 16:04:54 +000032#include <linux/io.h>
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -030033#include <linux/regulator/consumer.h>
Robert Jarzmikb405db62009-06-23 23:21:03 +020034#include <linux/gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/gfp.h>
Daniel Macke6027b42012-07-28 12:07:34 +020036#include <linux/of.h>
37#include <linux/of_gpio.h>
38#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/sizes.h>
41
Russell King05678a92008-11-28 16:04:54 +000042#include <mach/hardware.h>
Arnd Bergmann293b2da2012-08-24 15:16:48 +020043#include <linux/platform_data/mmc-pxamci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include "pxamci.h"
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define DRIVER_NAME "pxa2xx-mci"
48
49#define NR_SG 1
Russell Kingd8cb70d2007-10-26 17:56:40 +010050#define CLKRT_OFF (~0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Haojian Zhuangfa3f9932009-08-31 21:52:53 +080052#define mmc_has_26MHz() (cpu_is_pxa300() || cpu_is_pxa310() \
53 || cpu_is_pxa935())
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055struct pxamci_host {
56 struct mmc_host *mmc;
57 spinlock_t lock;
58 struct resource *res;
59 void __iomem *base;
Russell Kingebebd9b2007-08-20 10:20:03 +010060 struct clk *clk;
61 unsigned long clkrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 unsigned int clkrt;
64 unsigned int cmdat;
65 unsigned int imask;
66 unsigned int power_mode;
67 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 dma_addr_t sg_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 unsigned int dma_len;
78
79 unsigned int dma_dir;
Bridge Wu9a788c62007-12-14 10:40:25 +010080 unsigned int dma_drcmrrx;
81 unsigned int dma_drcmrtx;
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -030082
83 struct regulator *vcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -030086static inline void pxamci_init_ocr(struct pxamci_host *host)
87{
88#ifdef CONFIG_REGULATOR
Mark Brown6ae87172013-07-29 21:55:40 +010089 host->vcc = regulator_get_optional(mmc_dev(host->mmc), "vmmc");
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -030090
91 if (IS_ERR(host->vcc))
92 host->vcc = NULL;
93 else {
94 host->mmc->ocr_avail = mmc_regulator_get_ocrmask(host->vcc);
95 if (host->pdata && host->pdata->ocr_mask)
96 dev_warn(mmc_dev(host->mmc),
97 "ocr_mask/setpower will not be used\n");
98 }
99#endif
100 if (host->vcc == NULL) {
101 /* fall-back to platform data */
102 host->mmc->ocr_avail = host->pdata ?
103 host->pdata->ocr_mask :
104 MMC_VDD_32_33 | MMC_VDD_33_34;
105 }
106}
107
Linus Walleij99fc5132010-09-29 01:08:27 -0400108static inline int pxamci_set_power(struct pxamci_host *host,
109 unsigned char power_mode,
110 unsigned int vdd)
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -0300111{
Robert Jarzmikb405db62009-06-23 23:21:03 +0200112 int on;
113
Linus Walleij99fc5132010-09-29 01:08:27 -0400114 if (host->vcc) {
115 int ret;
116
117 if (power_mode == MMC_POWER_UP) {
118 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
119 if (ret)
120 return ret;
121 } else if (power_mode == MMC_POWER_OFF) {
122 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
123 if (ret)
124 return ret;
125 }
126 }
Robert Jarzmikb405db62009-06-23 23:21:03 +0200127 if (!host->vcc && host->pdata &&
128 gpio_is_valid(host->pdata->gpio_power)) {
129 on = ((1 << vdd) & host->pdata->ocr_mask);
130 gpio_set_value(host->pdata->gpio_power,
131 !!on ^ host->pdata->gpio_power_invert);
132 }
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -0300133 if (!host->vcc && host->pdata && host->pdata->setpower)
Arnd Bergmanna829abf2013-07-05 17:51:20 +0200134 return host->pdata->setpower(mmc_dev(host->mmc), vdd);
Linus Walleij99fc5132010-09-29 01:08:27 -0400135
136 return 0;
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -0300137}
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static void pxamci_stop_clock(struct pxamci_host *host)
140{
141 if (readl(host->base + MMC_STAT) & STAT_CLK_EN) {
142 unsigned long timeout = 10000;
143 unsigned int v;
144
145 writel(STOP_CLOCK, host->base + MMC_STRPCL);
146
147 do {
148 v = readl(host->base + MMC_STAT);
149 if (!(v & STAT_CLK_EN))
150 break;
151 udelay(1);
152 } while (timeout--);
153
154 if (v & STAT_CLK_EN)
155 dev_err(mmc_dev(host->mmc), "unable to stop clock\n");
156 }
157}
158
159static void pxamci_enable_irq(struct pxamci_host *host, unsigned int mask)
160{
161 unsigned long flags;
162
163 spin_lock_irqsave(&host->lock, flags);
164 host->imask &= ~mask;
165 writel(host->imask, host->base + MMC_I_MASK);
166 spin_unlock_irqrestore(&host->lock, flags);
167}
168
169static void pxamci_disable_irq(struct pxamci_host *host, unsigned int mask)
170{
171 unsigned long flags;
172
173 spin_lock_irqsave(&host->lock, flags);
174 host->imask |= mask;
175 writel(host->imask, host->base + MMC_I_MASK);
176 spin_unlock_irqrestore(&host->lock, flags);
177}
178
Daniel Mack6464b712015-06-06 23:15:22 +0200179static void pxamci_dma_irq(void *param);
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data)
182{
Daniel Mack6464b712015-06-06 23:15:22 +0200183 struct dma_async_tx_descriptor *tx;
184 enum dma_data_direction direction;
185 struct dma_slave_config config;
186 struct dma_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 unsigned int nob = data->blocks;
Russell King3d63abe2006-04-24 11:27:02 +0100188 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 unsigned int timeout;
Daniel Mack6464b712015-06-06 23:15:22 +0200190 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 host->data = data;
193
194 if (data->flags & MMC_DATA_STREAM)
195 nob = 0xffff;
196
197 writel(nob, host->base + MMC_NOB);
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100198 writel(data->blksz, host->base + MMC_BLKLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Russell Kingebebd9b2007-08-20 10:20:03 +0100200 clks = (unsigned long long)data->timeout_ns * host->clkrate;
Russell King3d63abe2006-04-24 11:27:02 +0100201 do_div(clks, 1000000000UL);
202 timeout = (unsigned int)clks + (data->timeout_clks << host->clkrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 writel((timeout + 255) / 256, host->base + MMC_RDTO);
204
Daniel Mack6464b712015-06-06 23:15:22 +0200205 memset(&config, 0, sizeof(config));
206 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
207 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
208 config.src_addr = host->res->start + MMC_RXFIFO;
209 config.dst_addr = host->res->start + MMC_TXFIFO;
210 config.src_maxburst = 32;
211 config.dst_maxburst = 32;
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (data->flags & MMC_DATA_READ) {
214 host->dma_dir = DMA_FROM_DEVICE;
Daniel Mack6464b712015-06-06 23:15:22 +0200215 direction = DMA_DEV_TO_MEM;
216 chan = host->dma_chan_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 } else {
218 host->dma_dir = DMA_TO_DEVICE;
Daniel Mack6464b712015-06-06 23:15:22 +0200219 direction = DMA_MEM_TO_DEV;
220 chan = host->dma_chan_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222
Daniel Mack6464b712015-06-06 23:15:22 +0200223 config.direction = direction;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Daniel Mack6464b712015-06-06 23:15:22 +0200225 ret = dmaengine_slave_config(chan, &config);
226 if (ret < 0) {
227 dev_err(mmc_dev(host->mmc), "dma slave config failed\n");
228 return;
229 }
230
231 host->dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 host->dma_dir);
233
Daniel Mack6464b712015-06-06 23:15:22 +0200234 tx = dmaengine_prep_slave_sg(chan, data->sg, host->dma_len, direction,
235 DMA_PREP_INTERRUPT);
236 if (!tx) {
237 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
238 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Daniel Mack6464b712015-06-06 23:15:22 +0200241 if (!(data->flags & MMC_DATA_READ)) {
242 tx->callback = pxamci_dma_irq;
243 tx->callback_param = host;
244 }
245
246 host->dma_cookie = dmaengine_submit(tx);
Cliff Brakeb6018952009-01-22 17:07:03 -0500247
248 /*
249 * workaround for erratum #91:
250 * only start DMA now if we are doing a read,
251 * otherwise we wait until CMD/RESP has finished
252 * before starting DMA.
253 */
254 if (!cpu_is_pxa27x() || data->flags & MMC_DATA_READ)
Daniel Mack6464b712015-06-06 23:15:22 +0200255 dma_async_issue_pending(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258static void pxamci_start_cmd(struct pxamci_host *host, struct mmc_command *cmd, unsigned int cmdat)
259{
260 WARN_ON(host->cmd != NULL);
261 host->cmd = cmd;
262
263 if (cmd->flags & MMC_RSP_BUSY)
264 cmdat |= CMDAT_BUSY;
265
Russell Kinge9225172006-02-02 12:23:12 +0000266#define RSP_TYPE(x) ((x) & ~(MMC_RSP_BUSY|MMC_RSP_OPCODE))
267 switch (RSP_TYPE(mmc_resp_type(cmd))) {
Philip Langdale6f949902007-01-04 07:04:47 -0800268 case RSP_TYPE(MMC_RSP_R1): /* r1, r1b, r6, r7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 cmdat |= CMDAT_RESP_SHORT;
270 break;
Russell Kinge9225172006-02-02 12:23:12 +0000271 case RSP_TYPE(MMC_RSP_R3):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 cmdat |= CMDAT_RESP_R3;
273 break;
Russell Kinge9225172006-02-02 12:23:12 +0000274 case RSP_TYPE(MMC_RSP_R2):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 cmdat |= CMDAT_RESP_R2;
276 break;
277 default:
278 break;
279 }
280
281 writel(cmd->opcode, host->base + MMC_CMD);
282 writel(cmd->arg >> 16, host->base + MMC_ARGH);
283 writel(cmd->arg & 0xffff, host->base + MMC_ARGL);
284 writel(cmdat, host->base + MMC_CMDAT);
285 writel(host->clkrt, host->base + MMC_CLKRT);
286
287 writel(START_CLOCK, host->base + MMC_STRPCL);
288
289 pxamci_enable_irq(host, END_CMD_RES);
290}
291
292static void pxamci_finish_request(struct pxamci_host *host, struct mmc_request *mrq)
293{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 host->mrq = NULL;
295 host->cmd = NULL;
296 host->data = NULL;
297 mmc_request_done(host->mmc, mrq);
298}
299
300static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat)
301{
302 struct mmc_command *cmd = host->cmd;
303 int i;
304 u32 v;
305
306 if (!cmd)
307 return 0;
308
309 host->cmd = NULL;
310
311 /*
312 * Did I mention this is Sick. We always need to
313 * discard the upper 8 bits of the first 16-bit word.
314 */
315 v = readl(host->base + MMC_RES) & 0xffff;
316 for (i = 0; i < 4; i++) {
317 u32 w1 = readl(host->base + MMC_RES) & 0xffff;
318 u32 w2 = readl(host->base + MMC_RES) & 0xffff;
319 cmd->resp[i] = v << 24 | w1 << 8 | w2 >> 8;
320 v = w2;
321 }
322
323 if (stat & STAT_TIME_OUT_RESPONSE) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200324 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 } else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /*
327 * workaround for erratum #42:
328 * Intel PXA27x Family Processor Specification Update Rev 001
Nicolas Pitre90e07d92007-05-13 18:03:08 +0200329 * A bogus CRC error can appear if the msb of a 136 bit
330 * response is a one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 */
Cliff Brakee10a8542009-01-22 16:58:58 -0500332 if (cpu_is_pxa27x() &&
333 (cmd->flags & MMC_RSP_136 && cmd->resp[0] & 0x80000000))
Nicolas Pitre90e07d92007-05-13 18:03:08 +0200334 pr_debug("ignoring CRC from command %d - *risky*\n", cmd->opcode);
Cliff Brakee10a8542009-01-22 16:58:58 -0500335 else
336 cmd->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338
339 pxamci_disable_irq(host, END_CMD_RES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200340 if (host->data && !cmd->error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 pxamci_enable_irq(host, DATA_TRAN_DONE);
Cliff Brakeb6018952009-01-22 17:07:03 -0500342 /*
343 * workaround for erratum #91, if doing write
344 * enable DMA late
345 */
346 if (cpu_is_pxa27x() && host->data->flags & MMC_DATA_WRITE)
Daniel Mack6464b712015-06-06 23:15:22 +0200347 dma_async_issue_pending(host->dma_chan_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 } else {
349 pxamci_finish_request(host, host->mrq);
350 }
351
352 return 1;
353}
354
355static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
356{
357 struct mmc_data *data = host->data;
Daniel Mack6464b712015-06-06 23:15:22 +0200358 struct dma_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (!data)
361 return 0;
362
Daniel Mack6464b712015-06-06 23:15:22 +0200363 if (data->flags & MMC_DATA_READ)
364 chan = host->dma_chan_rx;
365 else
366 chan = host->dma_chan_tx;
367 dma_unmap_sg(chan->device->dev,
368 data->sg, data->sg_len, host->dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 if (stat & STAT_READ_TIME_OUT)
Pierre Ossman17b04292007-07-22 22:18:46 +0200371 data->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR))
Pierre Ossman17b04292007-07-22 22:18:46 +0200373 data->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /*
376 * There appears to be a hardware design bug here. There seems to
377 * be no way to find out how much data was transferred to the card.
378 * This means that if there was an error on any block, we mark all
379 * data blocks as being in error.
380 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200381 if (!data->error)
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100382 data->bytes_xfered = data->blocks * data->blksz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 else
384 data->bytes_xfered = 0;
385
386 pxamci_disable_irq(host, DATA_TRAN_DONE);
387
388 host->data = NULL;
Russell King58741e82006-05-02 20:02:39 +0100389 if (host->mrq->stop) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 pxamci_stop_clock(host);
Bridge Wudf456f42007-09-25 19:09:19 +0200391 pxamci_start_cmd(host, host->mrq->stop, host->cmdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 } else {
393 pxamci_finish_request(host, host->mrq);
394 }
395
396 return 1;
397}
398
David Howells7d12e782006-10-05 14:55:46 +0100399static irqreturn_t pxamci_irq(int irq, void *devid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 struct pxamci_host *host = devid;
402 unsigned int ireg;
403 int handled = 0;
404
Bridge Wu81ab570f2007-09-25 18:59:07 +0200405 ireg = readl(host->base + MMC_I_REG) & ~readl(host->base + MMC_I_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (ireg) {
408 unsigned stat = readl(host->base + MMC_STAT);
409
Russell Kingd78e9072006-05-02 20:18:53 +0100410 pr_debug("PXAMCI: irq %08x stat %08x\n", ireg, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 if (ireg & END_CMD_RES)
413 handled |= pxamci_cmd_done(host, stat);
414 if (ireg & DATA_TRAN_DONE)
415 handled |= pxamci_data_done(host, stat);
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200416 if (ireg & SDIO_INT) {
417 mmc_signal_sdio_irq(host->mmc);
418 handled = 1;
419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
422 return IRQ_RETVAL(handled);
423}
424
425static void pxamci_request(struct mmc_host *mmc, struct mmc_request *mrq)
426{
427 struct pxamci_host *host = mmc_priv(mmc);
428 unsigned int cmdat;
429
430 WARN_ON(host->mrq != NULL);
431
432 host->mrq = mrq;
433
434 pxamci_stop_clock(host);
435
436 cmdat = host->cmdat;
437 host->cmdat &= ~CMDAT_INIT;
438
439 if (mrq->data) {
440 pxamci_setup_data(host, mrq->data);
441
442 cmdat &= ~CMDAT_BUSY;
443 cmdat |= CMDAT_DATAEN | CMDAT_DMAEN;
444 if (mrq->data->flags & MMC_DATA_WRITE)
445 cmdat |= CMDAT_WRITE;
446
447 if (mrq->data->flags & MMC_DATA_STREAM)
448 cmdat |= CMDAT_STREAM;
449 }
450
451 pxamci_start_cmd(host, mrq->cmd, cmdat);
452}
453
Richard Purdiee6195242005-09-06 15:18:56 -0700454static int pxamci_get_ro(struct mmc_host *mmc)
455{
456 struct pxamci_host *host = mmc_priv(mmc);
457
Robert Jarzmikfd546ee2015-09-26 21:41:01 +0200458 if (host->pdata && gpio_is_valid(host->pdata->gpio_card_ro))
459 return mmc_gpio_get_ro(mmc);
Richard Purdiee6195242005-09-06 15:18:56 -0700460 if (host->pdata && host->pdata->get_ro)
Anton Vorontsov08f80bb2008-06-17 18:17:39 +0400461 return !!host->pdata->get_ro(mmc_dev(mmc));
462 /*
463 * Board doesn't support read only detection; let the mmc core
464 * decide what to do.
465 */
466 return -ENOSYS;
Richard Purdiee6195242005-09-06 15:18:56 -0700467}
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
470{
471 struct pxamci_host *host = mmc_priv(mmc);
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (ios->clock) {
Russell Kingebebd9b2007-08-20 10:20:03 +0100474 unsigned long rate = host->clkrate;
475 unsigned int clk = rate / ios->clock;
476
Russell Kingd8cb70d2007-10-26 17:56:40 +0100477 if (host->clkrt == CLKRT_OFF)
Robert Jarzmike7370812014-09-02 11:23:55 +0200478 clk_prepare_enable(host->clk);
Russell Kingd8cb70d2007-10-26 17:56:40 +0100479
Bridge Wu64eb0362007-12-13 07:24:30 +0100480 if (ios->clock == 26000000) {
Haojian Zhuangfa3f9932009-08-31 21:52:53 +0800481 /* to support 26MHz */
Bridge Wu64eb0362007-12-13 07:24:30 +0100482 host->clkrt = 7;
483 } else {
484 /* to handle (19.5MHz, 26MHz) */
485 if (!clk)
486 clk = 1;
487
488 /*
489 * clk might result in a lower divisor than we
490 * desire. check for that condition and adjust
491 * as appropriate.
492 */
493 if (rate / clk > ios->clock)
494 clk <<= 1;
495 host->clkrt = fls(clk) - 1;
496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /*
499 * we write clkrt on the next command
500 */
501 } else {
502 pxamci_stop_clock(host);
Russell Kingd8cb70d2007-10-26 17:56:40 +0100503 if (host->clkrt != CLKRT_OFF) {
504 host->clkrt = CLKRT_OFF;
Robert Jarzmike7370812014-09-02 11:23:55 +0200505 clk_disable_unprepare(host->clk);
Russell Kingd8cb70d2007-10-26 17:56:40 +0100506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
509 if (host->power_mode != ios->power_mode) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400510 int ret;
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 host->power_mode = ios->power_mode;
513
Linus Walleij99fc5132010-09-29 01:08:27 -0400514 ret = pxamci_set_power(host, ios->power_mode, ios->vdd);
515 if (ret) {
516 dev_err(mmc_dev(mmc), "unable to set power\n");
517 /*
518 * The .set_ios() function in the mmc_host_ops
519 * struct return void, and failing to set the
520 * power should be rare so we print an error and
521 * return here.
522 */
523 return;
524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 if (ios->power_mode == MMC_POWER_ON)
527 host->cmdat |= CMDAT_INIT;
528 }
529
Bridge Wudf456f42007-09-25 19:09:19 +0200530 if (ios->bus_width == MMC_BUS_WIDTH_4)
531 host->cmdat |= CMDAT_SD_4DAT;
532 else
533 host->cmdat &= ~CMDAT_SD_4DAT;
534
Linus Walleij99fc5132010-09-29 01:08:27 -0400535 dev_dbg(mmc_dev(mmc), "PXAMCI: clkrt = %x cmdat = %x\n",
536 host->clkrt, host->cmdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200539static void pxamci_enable_sdio_irq(struct mmc_host *host, int enable)
540{
541 struct pxamci_host *pxa_host = mmc_priv(host);
542
543 if (enable)
544 pxamci_enable_irq(pxa_host, SDIO_INT);
545 else
546 pxamci_disable_irq(pxa_host, SDIO_INT);
547}
548
David Brownellab7aefd2006-11-12 17:55:30 -0800549static const struct mmc_host_ops pxamci_ops = {
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200550 .request = pxamci_request,
Robert Jarzmikfd546ee2015-09-26 21:41:01 +0200551 .get_cd = mmc_gpio_get_cd,
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200552 .get_ro = pxamci_get_ro,
553 .set_ios = pxamci_set_ios,
554 .enable_sdio_irq = pxamci_enable_sdio_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555};
556
Daniel Mack6464b712015-06-06 23:15:22 +0200557static void pxamci_dma_irq(void *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Daniel Mack6464b712015-06-06 23:15:22 +0200559 struct pxamci_host *host = param;
560 struct dma_tx_state state;
561 enum dma_status status;
562 struct dma_chan *chan;
563 unsigned long flags;
Nicolas Pitrec7838372007-10-09 17:07:58 -0400564
Daniel Mack6464b712015-06-06 23:15:22 +0200565 spin_lock_irqsave(&host->lock, flags);
566
567 if (!host->data)
568 goto out_unlock;
569
570 if (host->data->flags & MMC_DATA_READ)
571 chan = host->dma_chan_rx;
572 else
573 chan = host->dma_chan_tx;
574
575 status = dmaengine_tx_status(chan, host->dma_cookie, &state);
576
577 if (likely(status == DMA_COMPLETE)) {
Nicolas Pitrec7838372007-10-09 17:07:58 -0400578 writel(BUF_PART_FULL, host->base + MMC_PRTBUF);
579 } else {
Daniel Mack6464b712015-06-06 23:15:22 +0200580 pr_err("%s: DMA error on %s channel\n", mmc_hostname(host->mmc),
581 host->data->flags & MMC_DATA_READ ? "rx" : "tx");
Nicolas Pitrec7838372007-10-09 17:07:58 -0400582 host->data->error = -EIO;
583 pxamci_data_done(host, 0);
584 }
Daniel Mack6464b712015-06-06 23:15:22 +0200585
586out_unlock:
587 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
David Howells7d12e782006-10-05 14:55:46 +0100590static irqreturn_t pxamci_detect_irq(int irq, void *devid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Richard Purdiec26971c2005-09-08 22:48:16 +0100592 struct pxamci_host *host = mmc_priv(devid);
593
Eric Miaof97cab22010-04-14 07:00:42 +0800594 mmc_detect_change(devid, msecs_to_jiffies(host->pdata->detect_delay_ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return IRQ_HANDLED;
596}
597
Daniel Macke6027b42012-07-28 12:07:34 +0200598#ifdef CONFIG_OF
599static const struct of_device_id pxa_mmc_dt_ids[] = {
600 { .compatible = "marvell,pxa-mmc" },
601 { }
602};
603
604MODULE_DEVICE_TABLE(of, pxa_mmc_dt_ids);
605
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500606static int pxamci_of_init(struct platform_device *pdev)
Daniel Macke6027b42012-07-28 12:07:34 +0200607{
608 struct device_node *np = pdev->dev.of_node;
609 struct pxamci_platform_data *pdata;
610 u32 tmp;
611
612 if (!np)
613 return 0;
614
615 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
616 if (!pdata)
617 return -ENOMEM;
618
619 pdata->gpio_card_detect =
620 of_get_named_gpio(np, "cd-gpios", 0);
621 pdata->gpio_card_ro =
622 of_get_named_gpio(np, "wp-gpios", 0);
623
624 /* pxa-mmc specific */
625 pdata->gpio_power =
626 of_get_named_gpio(np, "pxa-mmc,gpio-power", 0);
627
628 if (of_property_read_u32(np, "pxa-mmc,detect-delay-ms", &tmp) == 0)
629 pdata->detect_delay_ms = tmp;
630
631 pdev->dev.platform_data = pdata;
632
633 return 0;
634}
635#else
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500636static int pxamci_of_init(struct platform_device *pdev)
Daniel Macke6027b42012-07-28 12:07:34 +0200637{
638 return 0;
639}
640#endif
641
Russell King3ae5eae2005-11-09 22:32:44 +0000642static int pxamci_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct mmc_host *mmc;
645 struct pxamci_host *host = NULL;
Bridge Wu9a788c62007-12-14 10:40:25 +0100646 struct resource *r, *dmarx, *dmatx;
Daniel Mack6464b712015-06-06 23:15:22 +0200647 struct pxad_param param_rx, param_tx;
Robert Jarzmikb405db62009-06-23 23:21:03 +0200648 int ret, irq, gpio_cd = -1, gpio_ro = -1, gpio_power = -1;
Daniel Mack6464b712015-06-06 23:15:22 +0200649 dma_cap_mask_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Daniel Macke6027b42012-07-28 12:07:34 +0200651 ret = pxamci_of_init(pdev);
652 if (ret)
653 return ret;
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
656 irq = platform_get_irq(pdev, 0);
David Vrabel48944732006-01-19 17:56:29 +0000657 if (!r || irq < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return -ENXIO;
659
660 r = request_mem_region(r->start, SZ_4K, DRIVER_NAME);
661 if (!r)
662 return -EBUSY;
663
Russell King3ae5eae2005-11-09 22:32:44 +0000664 mmc = mmc_alloc_host(sizeof(struct pxamci_host), &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (!mmc) {
666 ret = -ENOMEM;
667 goto out;
668 }
669
670 mmc->ops = &pxamci_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 /*
673 * We can do SG-DMA, but we don't because we never know how much
674 * data we successfully wrote to the card.
675 */
Martin K. Petersena36274e2010-09-10 01:33:59 -0400676 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 /*
679 * Our hardware DMA can handle a maximum of one page per SG entry.
680 */
681 mmc->max_seg_size = PAGE_SIZE;
682
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100683 /*
Nicolas Pitrefe2dc442007-09-24 15:47:18 -0400684 * Block length register is only 10 bits before PXA27x.
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100685 */
Eric Miao0ffcbfd2008-09-11 10:27:30 +0800686 mmc->max_blk_size = cpu_is_pxa25x() ? 1023 : 2048;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100687
Pierre Ossman55db8902006-11-21 17:55:45 +0100688 /*
689 * Block count register is 16 bits.
690 */
691 mmc->max_blk_count = 65535;
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 host = mmc_priv(mmc);
694 host->mmc = mmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 host->pdata = pdev->dev.platform_data;
Russell Kingd8cb70d2007-10-26 17:56:40 +0100696 host->clkrt = CLKRT_OFF;
Russell Kingebebd9b2007-08-20 10:20:03 +0100697
Russell Kinge0d8b132008-11-11 17:52:32 +0000698 host->clk = clk_get(&pdev->dev, NULL);
Russell Kingebebd9b2007-08-20 10:20:03 +0100699 if (IS_ERR(host->clk)) {
700 ret = PTR_ERR(host->clk);
701 host->clk = NULL;
702 goto out;
703 }
704
705 host->clkrate = clk_get_rate(host->clk);
706
707 /*
708 * Calculate minimum clock rate, rounding up.
709 */
710 mmc->f_min = (host->clkrate + 63) / 64;
Haojian Zhuangfa3f9932009-08-31 21:52:53 +0800711 mmc->f_max = (mmc_has_26MHz()) ? 26000000 : host->clkrate;
Russell Kingebebd9b2007-08-20 10:20:03 +0100712
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -0300713 pxamci_init_ocr(host);
714
Bridge Wudf456f42007-09-25 19:09:19 +0200715 mmc->caps = 0;
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200716 host->cmdat = 0;
Eric Miao0ffcbfd2008-09-11 10:27:30 +0800717 if (!cpu_is_pxa25x()) {
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200718 mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
719 host->cmdat |= CMDAT_SDIO_INT_EN;
Haojian Zhuangfa3f9932009-08-31 21:52:53 +0800720 if (mmc_has_26MHz())
Bridge Wu64eb0362007-12-13 07:24:30 +0100721 mmc->caps |= MMC_CAP_MMC_HIGHSPEED |
722 MMC_CAP_SD_HIGHSPEED;
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 spin_lock_init(&host->lock);
726 host->res = r;
727 host->irq = irq;
728 host->imask = MMC_I_MASK_ALL;
729
730 host->base = ioremap(r->start, SZ_4K);
731 if (!host->base) {
732 ret = -ENOMEM;
733 goto out;
734 }
735
736 /*
737 * Ensure that the host controller is shut down, and setup
738 * with our defaults.
739 */
740 pxamci_stop_clock(host);
741 writel(0, host->base + MMC_SPI);
742 writel(64, host->base + MMC_RESTO);
743 writel(host->imask, host->base + MMC_I_MASK);
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 ret = request_irq(host->irq, pxamci_irq, 0, DRIVER_NAME, host);
746 if (ret)
747 goto out;
748
Russell King3ae5eae2005-11-09 22:32:44 +0000749 platform_set_drvdata(pdev, mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Daniel Mack6464b712015-06-06 23:15:22 +0200751 if (!pdev->dev.of_node) {
752 dmarx = platform_get_resource(pdev, IORESOURCE_DMA, 0);
753 dmatx = platform_get_resource(pdev, IORESOURCE_DMA, 1);
754 if (!dmarx || !dmatx) {
755 ret = -ENXIO;
756 goto out;
757 }
758 param_rx.prio = PXAD_PRIO_LOWEST;
759 param_rx.drcmr = dmarx->start;
760 param_tx.prio = PXAD_PRIO_LOWEST;
761 param_tx.drcmr = dmatx->start;
Bridge Wu9a788c62007-12-14 10:40:25 +0100762 }
Bridge Wu9a788c62007-12-14 10:40:25 +0100763
Daniel Mack6464b712015-06-06 23:15:22 +0200764 dma_cap_zero(mask);
765 dma_cap_set(DMA_SLAVE, mask);
766
767 host->dma_chan_rx =
768 dma_request_slave_channel_compat(mask, pxad_filter_fn,
769 &param_rx, &pdev->dev, "rx");
770 if (host->dma_chan_rx == NULL) {
771 dev_err(&pdev->dev, "unable to request rx dma channel\n");
772 ret = -ENODEV;
Bridge Wu9a788c62007-12-14 10:40:25 +0100773 goto out;
774 }
Daniel Mack6464b712015-06-06 23:15:22 +0200775
776 host->dma_chan_tx =
777 dma_request_slave_channel_compat(mask, pxad_filter_fn,
778 &param_tx, &pdev->dev, "tx");
779 if (host->dma_chan_tx == NULL) {
780 dev_err(&pdev->dev, "unable to request tx dma channel\n");
781 ret = -ENODEV;
782 goto out;
783 }
Bridge Wu9a788c62007-12-14 10:40:25 +0100784
Robert Jarzmikb405db62009-06-23 23:21:03 +0200785 if (host->pdata) {
786 gpio_cd = host->pdata->gpio_card_detect;
787 gpio_ro = host->pdata->gpio_card_ro;
788 gpio_power = host->pdata->gpio_power;
789 }
790 if (gpio_is_valid(gpio_power)) {
Robert Jarzmikfd546ee2015-09-26 21:41:01 +0200791 ret = devm_gpio_request(&pdev->dev, gpio_power,
792 "mmc card power");
Robert Jarzmikb405db62009-06-23 23:21:03 +0200793 if (ret) {
Robert Jarzmikfd546ee2015-09-26 21:41:01 +0200794 dev_err(&pdev->dev, "Failed requesting gpio_power %d\n",
795 gpio_power);
Robert Jarzmikb405db62009-06-23 23:21:03 +0200796 goto out;
797 }
798 gpio_direction_output(gpio_power,
799 host->pdata->gpio_power_invert);
800 }
Robert Jarzmikfd546ee2015-09-26 21:41:01 +0200801 if (gpio_is_valid(gpio_ro))
802 ret = mmc_gpio_request_ro(mmc, gpio_ro);
803 if (ret) {
804 dev_err(&pdev->dev, "Failed requesting gpio_ro %d\n", gpio_ro);
805 goto out;
806 } else {
807 mmc->caps |= host->pdata->gpio_card_ro_invert ?
808 MMC_CAP2_RO_ACTIVE_HIGH : 0;
Robert Jarzmikb405db62009-06-23 23:21:03 +0200809 }
Robert Jarzmikb405db62009-06-23 23:21:03 +0200810
Robert Jarzmikfd546ee2015-09-26 21:41:01 +0200811 if (gpio_is_valid(gpio_cd))
812 ret = mmc_gpio_request_cd(mmc, gpio_cd, 0);
813 if (ret) {
814 dev_err(&pdev->dev, "Failed requesting gpio_cd %d\n", gpio_cd);
815 goto out;
Robert Jarzmikb405db62009-06-23 23:21:03 +0200816 }
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (host->pdata && host->pdata->init)
Russell King3ae5eae2005-11-09 22:32:44 +0000819 host->pdata->init(&pdev->dev, pxamci_detect_irq, mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Robert Jarzmikb405db62009-06-23 23:21:03 +0200821 if (gpio_is_valid(gpio_power) && host->pdata->setpower)
822 dev_warn(&pdev->dev, "gpio_power and setpower() both defined\n");
823 if (gpio_is_valid(gpio_ro) && host->pdata->get_ro)
824 dev_warn(&pdev->dev, "gpio_ro and get_ro() both defined\n");
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 mmc_add_host(mmc);
827
828 return 0;
829
Robert Jarzmikfd546ee2015-09-26 21:41:01 +0200830out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (host) {
Daniel Mack6464b712015-06-06 23:15:22 +0200832 if (host->dma_chan_rx)
833 dma_release_channel(host->dma_chan_rx);
834 if (host->dma_chan_tx)
835 dma_release_channel(host->dma_chan_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (host->base)
837 iounmap(host->base);
Russell Kingebebd9b2007-08-20 10:20:03 +0100838 if (host->clk)
839 clk_put(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841 if (mmc)
842 mmc_free_host(mmc);
843 release_resource(r);
844 return ret;
845}
846
Russell King3ae5eae2005-11-09 22:32:44 +0000847static int pxamci_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Russell King3ae5eae2005-11-09 22:32:44 +0000849 struct mmc_host *mmc = platform_get_drvdata(pdev);
Robert Jarzmikb405db62009-06-23 23:21:03 +0200850 int gpio_cd = -1, gpio_ro = -1, gpio_power = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (mmc) {
853 struct pxamci_host *host = mmc_priv(mmc);
854
Daniel Mack5d6b1edf2009-12-01 18:17:18 +0100855 mmc_remove_host(mmc);
856
Robert Jarzmikb405db62009-06-23 23:21:03 +0200857 if (host->pdata) {
858 gpio_cd = host->pdata->gpio_card_detect;
859 gpio_ro = host->pdata->gpio_card_ro;
860 gpio_power = host->pdata->gpio_power;
861 }
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -0300862 if (host->vcc)
863 regulator_put(host->vcc);
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (host->pdata && host->pdata->exit)
Russell King3ae5eae2005-11-09 22:32:44 +0000866 host->pdata->exit(&pdev->dev, mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 pxamci_stop_clock(host);
869 writel(TXFIFO_WR_REQ|RXFIFO_RD_REQ|CLK_IS_OFF|STOP_CMD|
870 END_CMD_RES|PRG_DONE|DATA_TRAN_DONE,
871 host->base + MMC_I_MASK);
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 free_irq(host->irq, host);
Daniel Mack6464b712015-06-06 23:15:22 +0200874 dmaengine_terminate_all(host->dma_chan_rx);
875 dmaengine_terminate_all(host->dma_chan_tx);
876 dma_release_channel(host->dma_chan_rx);
877 dma_release_channel(host->dma_chan_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 iounmap(host->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Russell Kingebebd9b2007-08-20 10:20:03 +0100880 clk_put(host->clk);
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 release_resource(host->res);
883
884 mmc_free_host(mmc);
885 }
886 return 0;
887}
888
Russell King3ae5eae2005-11-09 22:32:44 +0000889static struct platform_driver pxamci_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 .probe = pxamci_probe,
891 .remove = pxamci_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000892 .driver = {
893 .name = DRIVER_NAME,
Daniel Macke6027b42012-07-28 12:07:34 +0200894 .of_match_table = of_match_ptr(pxa_mmc_dt_ids),
Russell King3ae5eae2005-11-09 22:32:44 +0000895 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896};
897
Axel Lind1f81a62011-11-26 12:55:43 +0800898module_platform_driver(pxamci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver");
901MODULE_LICENSE("GPL");
Kay Sieversbc65c722008-04-15 14:34:28 -0700902MODULE_ALIAS("platform:pxa2xx-mci");