blob: b00d6731905826864abf43a762910899087a3887 [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>
25#include <linux/dma-mapping.h>
Russell Kingebebd9b2007-08-20 10:20:03 +010026#include <linux/clk.h>
27#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/mmc/host.h>
Russell King05678a92008-11-28 16:04:54 +000029#include <linux/io.h>
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -030030#include <linux/regulator/consumer.h>
Robert Jarzmikb405db62009-06-23 23:21:03 +020031#include <linux/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/sizes.h>
34
Russell King05678a92008-11-28 16:04:54 +000035#include <mach/hardware.h>
Eric Miao7ebc8d52009-01-02 19:38:42 +080036#include <mach/dma.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010037#include <mach/mmc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include "pxamci.h"
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define DRIVER_NAME "pxa2xx-mci"
42
43#define NR_SG 1
Russell Kingd8cb70d2007-10-26 17:56:40 +010044#define CLKRT_OFF (~0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46struct pxamci_host {
47 struct mmc_host *mmc;
48 spinlock_t lock;
49 struct resource *res;
50 void __iomem *base;
Russell Kingebebd9b2007-08-20 10:20:03 +010051 struct clk *clk;
52 unsigned long clkrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 int irq;
54 int dma;
55 unsigned int clkrt;
56 unsigned int cmdat;
57 unsigned int imask;
58 unsigned int power_mode;
59 struct pxamci_platform_data *pdata;
60
61 struct mmc_request *mrq;
62 struct mmc_command *cmd;
63 struct mmc_data *data;
64
65 dma_addr_t sg_dma;
66 struct pxa_dma_desc *sg_cpu;
67 unsigned int dma_len;
68
69 unsigned int dma_dir;
Bridge Wu9a788c62007-12-14 10:40:25 +010070 unsigned int dma_drcmrrx;
71 unsigned int dma_drcmrtx;
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -030072
73 struct regulator *vcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -030076static inline void pxamci_init_ocr(struct pxamci_host *host)
77{
78#ifdef CONFIG_REGULATOR
79 host->vcc = regulator_get(mmc_dev(host->mmc), "vmmc");
80
81 if (IS_ERR(host->vcc))
82 host->vcc = NULL;
83 else {
84 host->mmc->ocr_avail = mmc_regulator_get_ocrmask(host->vcc);
85 if (host->pdata && host->pdata->ocr_mask)
86 dev_warn(mmc_dev(host->mmc),
87 "ocr_mask/setpower will not be used\n");
88 }
89#endif
90 if (host->vcc == NULL) {
91 /* fall-back to platform data */
92 host->mmc->ocr_avail = host->pdata ?
93 host->pdata->ocr_mask :
94 MMC_VDD_32_33 | MMC_VDD_33_34;
95 }
96}
97
98static inline void pxamci_set_power(struct pxamci_host *host, unsigned int vdd)
99{
Robert Jarzmikb405db62009-06-23 23:21:03 +0200100 int on;
101
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -0300102#ifdef CONFIG_REGULATOR
103 if (host->vcc)
104 mmc_regulator_set_ocr(host->vcc, vdd);
105#endif
Robert Jarzmikb405db62009-06-23 23:21:03 +0200106 if (!host->vcc && host->pdata &&
107 gpio_is_valid(host->pdata->gpio_power)) {
108 on = ((1 << vdd) & host->pdata->ocr_mask);
109 gpio_set_value(host->pdata->gpio_power,
110 !!on ^ host->pdata->gpio_power_invert);
111 }
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -0300112 if (!host->vcc && host->pdata && host->pdata->setpower)
113 host->pdata->setpower(mmc_dev(host->mmc), vdd);
114}
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static void pxamci_stop_clock(struct pxamci_host *host)
117{
118 if (readl(host->base + MMC_STAT) & STAT_CLK_EN) {
119 unsigned long timeout = 10000;
120 unsigned int v;
121
122 writel(STOP_CLOCK, host->base + MMC_STRPCL);
123
124 do {
125 v = readl(host->base + MMC_STAT);
126 if (!(v & STAT_CLK_EN))
127 break;
128 udelay(1);
129 } while (timeout--);
130
131 if (v & STAT_CLK_EN)
132 dev_err(mmc_dev(host->mmc), "unable to stop clock\n");
133 }
134}
135
136static void pxamci_enable_irq(struct pxamci_host *host, unsigned int mask)
137{
138 unsigned long flags;
139
140 spin_lock_irqsave(&host->lock, flags);
141 host->imask &= ~mask;
142 writel(host->imask, host->base + MMC_I_MASK);
143 spin_unlock_irqrestore(&host->lock, flags);
144}
145
146static void pxamci_disable_irq(struct pxamci_host *host, unsigned int mask)
147{
148 unsigned long flags;
149
150 spin_lock_irqsave(&host->lock, flags);
151 host->imask |= mask;
152 writel(host->imask, host->base + MMC_I_MASK);
153 spin_unlock_irqrestore(&host->lock, flags);
154}
155
156static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data)
157{
158 unsigned int nob = data->blocks;
Russell King3d63abe2006-04-24 11:27:02 +0100159 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 unsigned int timeout;
Philipp Zabel97f85712008-07-06 01:15:34 +0200161 bool dalgn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 u32 dcmd;
163 int i;
164
165 host->data = data;
166
167 if (data->flags & MMC_DATA_STREAM)
168 nob = 0xffff;
169
170 writel(nob, host->base + MMC_NOB);
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100171 writel(data->blksz, host->base + MMC_BLKLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Russell Kingebebd9b2007-08-20 10:20:03 +0100173 clks = (unsigned long long)data->timeout_ns * host->clkrate;
Russell King3d63abe2006-04-24 11:27:02 +0100174 do_div(clks, 1000000000UL);
175 timeout = (unsigned int)clks + (data->timeout_clks << host->clkrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 writel((timeout + 255) / 256, host->base + MMC_RDTO);
177
178 if (data->flags & MMC_DATA_READ) {
179 host->dma_dir = DMA_FROM_DEVICE;
Matt Reimer7eeff482009-06-15 13:21:25 -0700180 dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC;
Bridge Wu9a788c62007-12-14 10:40:25 +0100181 DRCMR(host->dma_drcmrtx) = 0;
182 DRCMR(host->dma_drcmrrx) = host->dma | DRCMR_MAPVLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 } else {
184 host->dma_dir = DMA_TO_DEVICE;
Matt Reimer7eeff482009-06-15 13:21:25 -0700185 dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG;
Bridge Wu9a788c62007-12-14 10:40:25 +0100186 DRCMR(host->dma_drcmrrx) = 0;
187 DRCMR(host->dma_drcmrtx) = host->dma | DRCMR_MAPVLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189
190 dcmd |= DCMD_BURST32 | DCMD_WIDTH1;
191
192 host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
193 host->dma_dir);
194
195 for (i = 0; i < host->dma_len; i++) {
Nicolas Pitrec7838372007-10-09 17:07:58 -0400196 unsigned int length = sg_dma_len(&data->sg[i]);
197 host->sg_cpu[i].dcmd = dcmd | length;
198 if (length & 31 && !(data->flags & MMC_DATA_READ))
199 host->sg_cpu[i].dcmd |= DCMD_ENDIRQEN;
Philipp Zabel97f85712008-07-06 01:15:34 +0200200 /* Not aligned to 8-byte boundary? */
201 if (sg_dma_address(&data->sg[i]) & 0x7)
202 dalgn = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (data->flags & MMC_DATA_READ) {
204 host->sg_cpu[i].dsadr = host->res->start + MMC_RXFIFO;
205 host->sg_cpu[i].dtadr = sg_dma_address(&data->sg[i]);
206 } else {
207 host->sg_cpu[i].dsadr = sg_dma_address(&data->sg[i]);
208 host->sg_cpu[i].dtadr = host->res->start + MMC_TXFIFO;
209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 host->sg_cpu[i].ddadr = host->sg_dma + (i + 1) *
211 sizeof(struct pxa_dma_desc);
212 }
213 host->sg_cpu[host->dma_len - 1].ddadr = DDADR_STOP;
214 wmb();
215
Philipp Zabel97f85712008-07-06 01:15:34 +0200216 /*
217 * The PXA27x DMA controller encounters overhead when working with
218 * unaligned (to 8-byte boundaries) data, so switch on byte alignment
219 * mode only if we have unaligned data.
220 */
221 if (dalgn)
222 DALGN |= (1 << host->dma);
223 else
Karl Beldan4fe16892008-07-16 18:29:11 +0200224 DALGN &= ~(1 << host->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 DDADR(host->dma) = host->sg_dma;
Cliff Brakeb6018952009-01-22 17:07:03 -0500226
227 /*
228 * workaround for erratum #91:
229 * only start DMA now if we are doing a read,
230 * otherwise we wait until CMD/RESP has finished
231 * before starting DMA.
232 */
233 if (!cpu_is_pxa27x() || data->flags & MMC_DATA_READ)
234 DCSR(host->dma) = DCSR_RUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237static void pxamci_start_cmd(struct pxamci_host *host, struct mmc_command *cmd, unsigned int cmdat)
238{
239 WARN_ON(host->cmd != NULL);
240 host->cmd = cmd;
241
242 if (cmd->flags & MMC_RSP_BUSY)
243 cmdat |= CMDAT_BUSY;
244
Russell Kinge9225172006-02-02 12:23:12 +0000245#define RSP_TYPE(x) ((x) & ~(MMC_RSP_BUSY|MMC_RSP_OPCODE))
246 switch (RSP_TYPE(mmc_resp_type(cmd))) {
Philip Langdale6f949902007-01-04 07:04:47 -0800247 case RSP_TYPE(MMC_RSP_R1): /* r1, r1b, r6, r7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 cmdat |= CMDAT_RESP_SHORT;
249 break;
Russell Kinge9225172006-02-02 12:23:12 +0000250 case RSP_TYPE(MMC_RSP_R3):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 cmdat |= CMDAT_RESP_R3;
252 break;
Russell Kinge9225172006-02-02 12:23:12 +0000253 case RSP_TYPE(MMC_RSP_R2):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 cmdat |= CMDAT_RESP_R2;
255 break;
256 default:
257 break;
258 }
259
260 writel(cmd->opcode, host->base + MMC_CMD);
261 writel(cmd->arg >> 16, host->base + MMC_ARGH);
262 writel(cmd->arg & 0xffff, host->base + MMC_ARGL);
263 writel(cmdat, host->base + MMC_CMDAT);
264 writel(host->clkrt, host->base + MMC_CLKRT);
265
266 writel(START_CLOCK, host->base + MMC_STRPCL);
267
268 pxamci_enable_irq(host, END_CMD_RES);
269}
270
271static void pxamci_finish_request(struct pxamci_host *host, struct mmc_request *mrq)
272{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 host->mrq = NULL;
274 host->cmd = NULL;
275 host->data = NULL;
276 mmc_request_done(host->mmc, mrq);
277}
278
279static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat)
280{
281 struct mmc_command *cmd = host->cmd;
282 int i;
283 u32 v;
284
285 if (!cmd)
286 return 0;
287
288 host->cmd = NULL;
289
290 /*
291 * Did I mention this is Sick. We always need to
292 * discard the upper 8 bits of the first 16-bit word.
293 */
294 v = readl(host->base + MMC_RES) & 0xffff;
295 for (i = 0; i < 4; i++) {
296 u32 w1 = readl(host->base + MMC_RES) & 0xffff;
297 u32 w2 = readl(host->base + MMC_RES) & 0xffff;
298 cmd->resp[i] = v << 24 | w1 << 8 | w2 >> 8;
299 v = w2;
300 }
301
302 if (stat & STAT_TIME_OUT_RESPONSE) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200303 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 } else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /*
306 * workaround for erratum #42:
307 * Intel PXA27x Family Processor Specification Update Rev 001
Nicolas Pitre90e07d92007-05-13 18:03:08 +0200308 * A bogus CRC error can appear if the msb of a 136 bit
309 * response is a one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 */
Cliff Brakee10a8542009-01-22 16:58:58 -0500311 if (cpu_is_pxa27x() &&
312 (cmd->flags & MMC_RSP_136 && cmd->resp[0] & 0x80000000))
Nicolas Pitre90e07d92007-05-13 18:03:08 +0200313 pr_debug("ignoring CRC from command %d - *risky*\n", cmd->opcode);
Cliff Brakee10a8542009-01-22 16:58:58 -0500314 else
315 cmd->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317
318 pxamci_disable_irq(host, END_CMD_RES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200319 if (host->data && !cmd->error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 pxamci_enable_irq(host, DATA_TRAN_DONE);
Cliff Brakeb6018952009-01-22 17:07:03 -0500321 /*
322 * workaround for erratum #91, if doing write
323 * enable DMA late
324 */
325 if (cpu_is_pxa27x() && host->data->flags & MMC_DATA_WRITE)
326 DCSR(host->dma) = DCSR_RUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 } else {
328 pxamci_finish_request(host, host->mrq);
329 }
330
331 return 1;
332}
333
334static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
335{
336 struct mmc_data *data = host->data;
337
338 if (!data)
339 return 0;
340
341 DCSR(host->dma) = 0;
Vernon Sauderc00a46a2008-12-29 19:21:28 -0500342 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 host->dma_dir);
344
345 if (stat & STAT_READ_TIME_OUT)
Pierre Ossman17b04292007-07-22 22:18:46 +0200346 data->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR))
Pierre Ossman17b04292007-07-22 22:18:46 +0200348 data->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 /*
351 * There appears to be a hardware design bug here. There seems to
352 * be no way to find out how much data was transferred to the card.
353 * This means that if there was an error on any block, we mark all
354 * data blocks as being in error.
355 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200356 if (!data->error)
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100357 data->bytes_xfered = data->blocks * data->blksz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 else
359 data->bytes_xfered = 0;
360
361 pxamci_disable_irq(host, DATA_TRAN_DONE);
362
363 host->data = NULL;
Russell King58741e82006-05-02 20:02:39 +0100364 if (host->mrq->stop) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 pxamci_stop_clock(host);
Bridge Wudf456f42007-09-25 19:09:19 +0200366 pxamci_start_cmd(host, host->mrq->stop, host->cmdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 } else {
368 pxamci_finish_request(host, host->mrq);
369 }
370
371 return 1;
372}
373
David Howells7d12e782006-10-05 14:55:46 +0100374static irqreturn_t pxamci_irq(int irq, void *devid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 struct pxamci_host *host = devid;
377 unsigned int ireg;
378 int handled = 0;
379
Bridge Wu81ab570f2007-09-25 18:59:07 +0200380 ireg = readl(host->base + MMC_I_REG) & ~readl(host->base + MMC_I_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (ireg) {
383 unsigned stat = readl(host->base + MMC_STAT);
384
Russell Kingd78e9072006-05-02 20:18:53 +0100385 pr_debug("PXAMCI: irq %08x stat %08x\n", ireg, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 if (ireg & END_CMD_RES)
388 handled |= pxamci_cmd_done(host, stat);
389 if (ireg & DATA_TRAN_DONE)
390 handled |= pxamci_data_done(host, stat);
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200391 if (ireg & SDIO_INT) {
392 mmc_signal_sdio_irq(host->mmc);
393 handled = 1;
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396
397 return IRQ_RETVAL(handled);
398}
399
400static void pxamci_request(struct mmc_host *mmc, struct mmc_request *mrq)
401{
402 struct pxamci_host *host = mmc_priv(mmc);
403 unsigned int cmdat;
404
405 WARN_ON(host->mrq != NULL);
406
407 host->mrq = mrq;
408
409 pxamci_stop_clock(host);
410
411 cmdat = host->cmdat;
412 host->cmdat &= ~CMDAT_INIT;
413
414 if (mrq->data) {
415 pxamci_setup_data(host, mrq->data);
416
417 cmdat &= ~CMDAT_BUSY;
418 cmdat |= CMDAT_DATAEN | CMDAT_DMAEN;
419 if (mrq->data->flags & MMC_DATA_WRITE)
420 cmdat |= CMDAT_WRITE;
421
422 if (mrq->data->flags & MMC_DATA_STREAM)
423 cmdat |= CMDAT_STREAM;
424 }
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
Robert Jarzmikb405db62009-06-23 23:21:03 +0200433 if (host->pdata && gpio_is_valid(host->pdata->gpio_card_ro)) {
434 if (host->pdata->gpio_card_ro_invert)
435 return !gpio_get_value(host->pdata->gpio_card_ro);
436 else
437 return gpio_get_value(host->pdata->gpio_card_ro);
438 }
Richard Purdiee6195242005-09-06 15:18:56 -0700439 if (host->pdata && host->pdata->get_ro)
Anton Vorontsov08f80bb2008-06-17 18:17:39 +0400440 return !!host->pdata->get_ro(mmc_dev(mmc));
441 /*
442 * Board doesn't support read only detection; let the mmc core
443 * decide what to do.
444 */
445 return -ENOSYS;
Richard Purdiee6195242005-09-06 15:18:56 -0700446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
449{
450 struct pxamci_host *host = mmc_priv(mmc);
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (ios->clock) {
Russell Kingebebd9b2007-08-20 10:20:03 +0100453 unsigned long rate = host->clkrate;
454 unsigned int clk = rate / ios->clock;
455
Russell Kingd8cb70d2007-10-26 17:56:40 +0100456 if (host->clkrt == CLKRT_OFF)
457 clk_enable(host->clk);
458
Bridge Wu64eb0362007-12-13 07:24:30 +0100459 if (ios->clock == 26000000) {
460 /* to support 26MHz on pxa300/pxa310 */
461 host->clkrt = 7;
462 } else {
463 /* to handle (19.5MHz, 26MHz) */
464 if (!clk)
465 clk = 1;
466
467 /*
468 * clk might result in a lower divisor than we
469 * desire. check for that condition and adjust
470 * as appropriate.
471 */
472 if (rate / clk > ios->clock)
473 clk <<= 1;
474 host->clkrt = fls(clk) - 1;
475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 /*
478 * we write clkrt on the next command
479 */
480 } else {
481 pxamci_stop_clock(host);
Russell Kingd8cb70d2007-10-26 17:56:40 +0100482 if (host->clkrt != CLKRT_OFF) {
483 host->clkrt = CLKRT_OFF;
484 clk_disable(host->clk);
485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487
488 if (host->power_mode != ios->power_mode) {
489 host->power_mode = ios->power_mode;
490
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -0300491 pxamci_set_power(host, ios->vdd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 if (ios->power_mode == MMC_POWER_ON)
494 host->cmdat |= CMDAT_INIT;
495 }
496
Bridge Wudf456f42007-09-25 19:09:19 +0200497 if (ios->bus_width == MMC_BUS_WIDTH_4)
498 host->cmdat |= CMDAT_SD_4DAT;
499 else
500 host->cmdat &= ~CMDAT_SD_4DAT;
501
Russell Kingd78e9072006-05-02 20:18:53 +0100502 pr_debug("PXAMCI: clkrt = %x cmdat = %x\n",
Russell Kingc6563172006-03-29 09:30:20 +0100503 host->clkrt, host->cmdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200506static void pxamci_enable_sdio_irq(struct mmc_host *host, int enable)
507{
508 struct pxamci_host *pxa_host = mmc_priv(host);
509
510 if (enable)
511 pxamci_enable_irq(pxa_host, SDIO_INT);
512 else
513 pxamci_disable_irq(pxa_host, SDIO_INT);
514}
515
David Brownellab7aefd2006-11-12 17:55:30 -0800516static const struct mmc_host_ops pxamci_ops = {
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200517 .request = pxamci_request,
518 .get_ro = pxamci_get_ro,
519 .set_ios = pxamci_set_ios,
520 .enable_sdio_irq = pxamci_enable_sdio_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521};
522
David Howells7d12e782006-10-05 14:55:46 +0100523static void pxamci_dma_irq(int dma, void *devid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Nicolas Pitrec7838372007-10-09 17:07:58 -0400525 struct pxamci_host *host = devid;
526 int dcsr = DCSR(dma);
527 DCSR(dma) = dcsr & ~DCSR_STOPIRQEN;
528
529 if (dcsr & DCSR_ENDINTR) {
530 writel(BUF_PART_FULL, host->base + MMC_PRTBUF);
531 } else {
532 printk(KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n",
533 mmc_hostname(host->mmc), dma, dcsr);
534 host->data->error = -EIO;
535 pxamci_data_done(host, 0);
536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
David Howells7d12e782006-10-05 14:55:46 +0100539static irqreturn_t pxamci_detect_irq(int irq, void *devid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Richard Purdiec26971c2005-09-08 22:48:16 +0100541 struct pxamci_host *host = mmc_priv(devid);
542
543 mmc_detect_change(devid, host->pdata->detect_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return IRQ_HANDLED;
545}
546
Russell King3ae5eae2005-11-09 22:32:44 +0000547static int pxamci_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct mmc_host *mmc;
550 struct pxamci_host *host = NULL;
Bridge Wu9a788c62007-12-14 10:40:25 +0100551 struct resource *r, *dmarx, *dmatx;
Robert Jarzmikb405db62009-06-23 23:21:03 +0200552 int ret, irq, gpio_cd = -1, gpio_ro = -1, gpio_power = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
555 irq = platform_get_irq(pdev, 0);
David Vrabel48944732006-01-19 17:56:29 +0000556 if (!r || irq < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return -ENXIO;
558
559 r = request_mem_region(r->start, SZ_4K, DRIVER_NAME);
560 if (!r)
561 return -EBUSY;
562
Russell King3ae5eae2005-11-09 22:32:44 +0000563 mmc = mmc_alloc_host(sizeof(struct pxamci_host), &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (!mmc) {
565 ret = -ENOMEM;
566 goto out;
567 }
568
569 mmc->ops = &pxamci_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 /*
572 * We can do SG-DMA, but we don't because we never know how much
573 * data we successfully wrote to the card.
574 */
575 mmc->max_phys_segs = NR_SG;
576
577 /*
578 * Our hardware DMA can handle a maximum of one page per SG entry.
579 */
580 mmc->max_seg_size = PAGE_SIZE;
581
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100582 /*
Nicolas Pitrefe2dc442007-09-24 15:47:18 -0400583 * Block length register is only 10 bits before PXA27x.
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100584 */
Eric Miao0ffcbfd2008-09-11 10:27:30 +0800585 mmc->max_blk_size = cpu_is_pxa25x() ? 1023 : 2048;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100586
Pierre Ossman55db8902006-11-21 17:55:45 +0100587 /*
588 * Block count register is 16 bits.
589 */
590 mmc->max_blk_count = 65535;
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 host = mmc_priv(mmc);
593 host->mmc = mmc;
594 host->dma = -1;
595 host->pdata = pdev->dev.platform_data;
Russell Kingd8cb70d2007-10-26 17:56:40 +0100596 host->clkrt = CLKRT_OFF;
Russell Kingebebd9b2007-08-20 10:20:03 +0100597
Russell Kinge0d8b132008-11-11 17:52:32 +0000598 host->clk = clk_get(&pdev->dev, NULL);
Russell Kingebebd9b2007-08-20 10:20:03 +0100599 if (IS_ERR(host->clk)) {
600 ret = PTR_ERR(host->clk);
601 host->clk = NULL;
602 goto out;
603 }
604
605 host->clkrate = clk_get_rate(host->clk);
606
607 /*
608 * Calculate minimum clock rate, rounding up.
609 */
610 mmc->f_min = (host->clkrate + 63) / 64;
Bridge Wu64eb0362007-12-13 07:24:30 +0100611 mmc->f_max = (cpu_is_pxa300() || cpu_is_pxa310()) ? 26000000
612 : host->clkrate;
Russell Kingebebd9b2007-08-20 10:20:03 +0100613
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -0300614 pxamci_init_ocr(host);
615
Bridge Wudf456f42007-09-25 19:09:19 +0200616 mmc->caps = 0;
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200617 host->cmdat = 0;
Eric Miao0ffcbfd2008-09-11 10:27:30 +0800618 if (!cpu_is_pxa25x()) {
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200619 mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
620 host->cmdat |= CMDAT_SDIO_INT_EN;
Bridge Wu64eb0362007-12-13 07:24:30 +0100621 if (cpu_is_pxa300() || cpu_is_pxa310())
622 mmc->caps |= MMC_CAP_MMC_HIGHSPEED |
623 MMC_CAP_SD_HIGHSPEED;
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Russell King3ae5eae2005-11-09 22:32:44 +0000626 host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &host->sg_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (!host->sg_cpu) {
628 ret = -ENOMEM;
629 goto out;
630 }
631
632 spin_lock_init(&host->lock);
633 host->res = r;
634 host->irq = irq;
635 host->imask = MMC_I_MASK_ALL;
636
637 host->base = ioremap(r->start, SZ_4K);
638 if (!host->base) {
639 ret = -ENOMEM;
640 goto out;
641 }
642
643 /*
644 * Ensure that the host controller is shut down, and setup
645 * with our defaults.
646 */
647 pxamci_stop_clock(host);
648 writel(0, host->base + MMC_SPI);
649 writel(64, host->base + MMC_RESTO);
650 writel(host->imask, host->base + MMC_I_MASK);
651
652 host->dma = pxa_request_dma(DRIVER_NAME, DMA_PRIO_LOW,
653 pxamci_dma_irq, host);
654 if (host->dma < 0) {
655 ret = -EBUSY;
656 goto out;
657 }
658
659 ret = request_irq(host->irq, pxamci_irq, 0, DRIVER_NAME, host);
660 if (ret)
661 goto out;
662
Russell King3ae5eae2005-11-09 22:32:44 +0000663 platform_set_drvdata(pdev, mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Bridge Wu9a788c62007-12-14 10:40:25 +0100665 dmarx = platform_get_resource(pdev, IORESOURCE_DMA, 0);
666 if (!dmarx) {
667 ret = -ENXIO;
668 goto out;
669 }
670 host->dma_drcmrrx = dmarx->start;
671
672 dmatx = platform_get_resource(pdev, IORESOURCE_DMA, 1);
673 if (!dmatx) {
674 ret = -ENXIO;
675 goto out;
676 }
677 host->dma_drcmrtx = dmatx->start;
678
Robert Jarzmikb405db62009-06-23 23:21:03 +0200679 if (host->pdata) {
680 gpio_cd = host->pdata->gpio_card_detect;
681 gpio_ro = host->pdata->gpio_card_ro;
682 gpio_power = host->pdata->gpio_power;
683 }
684 if (gpio_is_valid(gpio_power)) {
685 ret = gpio_request(gpio_power, "mmc card power");
686 if (ret) {
687 dev_err(&pdev->dev, "Failed requesting gpio_power %d\n", gpio_power);
688 goto out;
689 }
690 gpio_direction_output(gpio_power,
691 host->pdata->gpio_power_invert);
692 }
693 if (gpio_is_valid(gpio_ro)) {
694 ret = gpio_request(gpio_ro, "mmc card read only");
695 if (ret) {
Antonio Ospite48f02952009-10-02 16:24:02 +0200696 dev_err(&pdev->dev, "Failed requesting gpio_ro %d\n", gpio_ro);
Robert Jarzmikb405db62009-06-23 23:21:03 +0200697 goto err_gpio_ro;
698 }
699 gpio_direction_input(gpio_ro);
700 }
701 if (gpio_is_valid(gpio_cd)) {
702 ret = gpio_request(gpio_cd, "mmc card detect");
703 if (ret) {
Antonio Ospite48f02952009-10-02 16:24:02 +0200704 dev_err(&pdev->dev, "Failed requesting gpio_cd %d\n", gpio_cd);
Robert Jarzmikb405db62009-06-23 23:21:03 +0200705 goto err_gpio_cd;
706 }
707 gpio_direction_input(gpio_cd);
708
709 ret = request_irq(gpio_to_irq(gpio_cd), pxamci_detect_irq,
710 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
711 "mmc card detect", mmc);
712 if (ret) {
713 dev_err(&pdev->dev, "failed to request card detect IRQ\n");
714 goto err_request_irq;
715 }
716 }
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (host->pdata && host->pdata->init)
Russell King3ae5eae2005-11-09 22:32:44 +0000719 host->pdata->init(&pdev->dev, pxamci_detect_irq, mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Robert Jarzmikb405db62009-06-23 23:21:03 +0200721 if (gpio_is_valid(gpio_power) && host->pdata->setpower)
722 dev_warn(&pdev->dev, "gpio_power and setpower() both defined\n");
723 if (gpio_is_valid(gpio_ro) && host->pdata->get_ro)
724 dev_warn(&pdev->dev, "gpio_ro and get_ro() both defined\n");
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 mmc_add_host(mmc);
727
728 return 0;
729
Robert Jarzmikb405db62009-06-23 23:21:03 +0200730err_request_irq:
731 gpio_free(gpio_cd);
732err_gpio_cd:
733 gpio_free(gpio_ro);
734err_gpio_ro:
735 gpio_free(gpio_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 out:
737 if (host) {
738 if (host->dma >= 0)
739 pxa_free_dma(host->dma);
740 if (host->base)
741 iounmap(host->base);
742 if (host->sg_cpu)
Russell King3ae5eae2005-11-09 22:32:44 +0000743 dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
Russell Kingebebd9b2007-08-20 10:20:03 +0100744 if (host->clk)
745 clk_put(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747 if (mmc)
748 mmc_free_host(mmc);
749 release_resource(r);
750 return ret;
751}
752
Russell King3ae5eae2005-11-09 22:32:44 +0000753static int pxamci_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Russell King3ae5eae2005-11-09 22:32:44 +0000755 struct mmc_host *mmc = platform_get_drvdata(pdev);
Robert Jarzmikb405db62009-06-23 23:21:03 +0200756 int gpio_cd = -1, gpio_ro = -1, gpio_power = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Russell King3ae5eae2005-11-09 22:32:44 +0000758 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 if (mmc) {
761 struct pxamci_host *host = mmc_priv(mmc);
762
Robert Jarzmikb405db62009-06-23 23:21:03 +0200763 if (host->pdata) {
764 gpio_cd = host->pdata->gpio_card_detect;
765 gpio_ro = host->pdata->gpio_card_ro;
766 gpio_power = host->pdata->gpio_power;
767 }
768 if (gpio_is_valid(gpio_cd)) {
769 free_irq(gpio_to_irq(gpio_cd), mmc);
770 gpio_free(gpio_cd);
771 }
772 if (gpio_is_valid(gpio_ro))
773 gpio_free(gpio_ro);
774 if (gpio_is_valid(gpio_power))
775 gpio_free(gpio_power);
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -0300776 if (host->vcc)
777 regulator_put(host->vcc);
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (host->pdata && host->pdata->exit)
Russell King3ae5eae2005-11-09 22:32:44 +0000780 host->pdata->exit(&pdev->dev, mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 mmc_remove_host(mmc);
783
784 pxamci_stop_clock(host);
785 writel(TXFIFO_WR_REQ|RXFIFO_RD_REQ|CLK_IS_OFF|STOP_CMD|
786 END_CMD_RES|PRG_DONE|DATA_TRAN_DONE,
787 host->base + MMC_I_MASK);
788
Bridge Wu9a788c62007-12-14 10:40:25 +0100789 DRCMR(host->dma_drcmrrx) = 0;
790 DRCMR(host->dma_drcmrtx) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 free_irq(host->irq, host);
793 pxa_free_dma(host->dma);
794 iounmap(host->base);
Russell King3ae5eae2005-11-09 22:32:44 +0000795 dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Russell Kingebebd9b2007-08-20 10:20:03 +0100797 clk_put(host->clk);
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 release_resource(host->res);
800
801 mmc_free_host(mmc);
802 }
803 return 0;
804}
805
806#ifdef CONFIG_PM
Mike Rapoport33264f92009-07-29 11:59:24 +0300807static int pxamci_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Mike Rapoport33264f92009-07-29 11:59:24 +0300809 struct mmc_host *mmc = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 int ret = 0;
811
Russell King9480e302005-10-28 09:52:56 -0700812 if (mmc)
Mike Rapoport33264f92009-07-29 11:59:24 +0300813 ret = mmc_suspend_host(mmc, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 return ret;
816}
817
Mike Rapoport33264f92009-07-29 11:59:24 +0300818static int pxamci_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Mike Rapoport33264f92009-07-29 11:59:24 +0300820 struct mmc_host *mmc = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 int ret = 0;
822
Russell King9480e302005-10-28 09:52:56 -0700823 if (mmc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 ret = mmc_resume_host(mmc);
825
826 return ret;
827}
Mike Rapoport33264f92009-07-29 11:59:24 +0300828
829static struct dev_pm_ops pxamci_pm_ops = {
830 .suspend = pxamci_suspend,
831 .resume = pxamci_resume,
832};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833#endif
834
Russell King3ae5eae2005-11-09 22:32:44 +0000835static struct platform_driver pxamci_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 .probe = pxamci_probe,
837 .remove = pxamci_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000838 .driver = {
839 .name = DRIVER_NAME,
Kay Sieversbc65c722008-04-15 14:34:28 -0700840 .owner = THIS_MODULE,
Mike Rapoport33264f92009-07-29 11:59:24 +0300841#ifdef CONFIG_PM
842 .pm = &pxamci_pm_ops,
843#endif
Russell King3ae5eae2005-11-09 22:32:44 +0000844 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845};
846
847static int __init pxamci_init(void)
848{
Russell King3ae5eae2005-11-09 22:32:44 +0000849 return platform_driver_register(&pxamci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
852static void __exit pxamci_exit(void)
853{
Russell King3ae5eae2005-11-09 22:32:44 +0000854 platform_driver_unregister(&pxamci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
857module_init(pxamci_init);
858module_exit(pxamci_exit);
859
860MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver");
861MODULE_LICENSE("GPL");
Kay Sieversbc65c722008-04-15 14:34:28 -0700862MODULE_ALIAS("platform:pxa2xx-mci");