blob: b7dfcac31e8a8ad29cb16be886656c8ca60ae16c [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/sizes.h>
35
Russell King05678a92008-11-28 16:04:54 +000036#include <mach/hardware.h>
Eric Miao7ebc8d52009-01-02 19:38:42 +080037#include <mach/dma.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010038#include <mach/mmc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include "pxamci.h"
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define DRIVER_NAME "pxa2xx-mci"
43
44#define NR_SG 1
Russell Kingd8cb70d2007-10-26 17:56:40 +010045#define CLKRT_OFF (~0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Haojian Zhuangfa3f9932009-08-31 21:52:53 +080047#define mmc_has_26MHz() (cpu_is_pxa300() || cpu_is_pxa310() \
48 || cpu_is_pxa935())
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050struct pxamci_host {
51 struct mmc_host *mmc;
52 spinlock_t lock;
53 struct resource *res;
54 void __iomem *base;
Russell Kingebebd9b2007-08-20 10:20:03 +010055 struct clk *clk;
56 unsigned long clkrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 int irq;
58 int dma;
59 unsigned int clkrt;
60 unsigned int cmdat;
61 unsigned int imask;
62 unsigned int power_mode;
63 struct pxamci_platform_data *pdata;
64
65 struct mmc_request *mrq;
66 struct mmc_command *cmd;
67 struct mmc_data *data;
68
69 dma_addr_t sg_dma;
70 struct pxa_dma_desc *sg_cpu;
71 unsigned int dma_len;
72
73 unsigned int dma_dir;
Bridge Wu9a788c62007-12-14 10:40:25 +010074 unsigned int dma_drcmrrx;
75 unsigned int dma_drcmrtx;
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -030076
77 struct regulator *vcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -030080static inline void pxamci_init_ocr(struct pxamci_host *host)
81{
82#ifdef CONFIG_REGULATOR
83 host->vcc = regulator_get(mmc_dev(host->mmc), "vmmc");
84
85 if (IS_ERR(host->vcc))
86 host->vcc = NULL;
87 else {
88 host->mmc->ocr_avail = mmc_regulator_get_ocrmask(host->vcc);
89 if (host->pdata && host->pdata->ocr_mask)
90 dev_warn(mmc_dev(host->mmc),
91 "ocr_mask/setpower will not be used\n");
92 }
93#endif
94 if (host->vcc == NULL) {
95 /* fall-back to platform data */
96 host->mmc->ocr_avail = host->pdata ?
97 host->pdata->ocr_mask :
98 MMC_VDD_32_33 | MMC_VDD_33_34;
99 }
100}
101
102static inline void pxamci_set_power(struct pxamci_host *host, unsigned int vdd)
103{
Robert Jarzmikb405db62009-06-23 23:21:03 +0200104 int on;
105
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -0300106#ifdef CONFIG_REGULATOR
107 if (host->vcc)
108 mmc_regulator_set_ocr(host->vcc, vdd);
109#endif
Robert Jarzmikb405db62009-06-23 23:21:03 +0200110 if (!host->vcc && host->pdata &&
111 gpio_is_valid(host->pdata->gpio_power)) {
112 on = ((1 << vdd) & host->pdata->ocr_mask);
113 gpio_set_value(host->pdata->gpio_power,
114 !!on ^ host->pdata->gpio_power_invert);
115 }
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -0300116 if (!host->vcc && host->pdata && host->pdata->setpower)
117 host->pdata->setpower(mmc_dev(host->mmc), vdd);
118}
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
160static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data)
161{
162 unsigned int nob = data->blocks;
Russell King3d63abe2006-04-24 11:27:02 +0100163 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 unsigned int timeout;
Philipp Zabel97f85712008-07-06 01:15:34 +0200165 bool dalgn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 u32 dcmd;
167 int i;
168
169 host->data = data;
170
171 if (data->flags & MMC_DATA_STREAM)
172 nob = 0xffff;
173
174 writel(nob, host->base + MMC_NOB);
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100175 writel(data->blksz, host->base + MMC_BLKLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Russell Kingebebd9b2007-08-20 10:20:03 +0100177 clks = (unsigned long long)data->timeout_ns * host->clkrate;
Russell King3d63abe2006-04-24 11:27:02 +0100178 do_div(clks, 1000000000UL);
179 timeout = (unsigned int)clks + (data->timeout_clks << host->clkrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 writel((timeout + 255) / 256, host->base + MMC_RDTO);
181
182 if (data->flags & MMC_DATA_READ) {
183 host->dma_dir = DMA_FROM_DEVICE;
Matt Reimer7eeff482009-06-15 13:21:25 -0700184 dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC;
Bridge Wu9a788c62007-12-14 10:40:25 +0100185 DRCMR(host->dma_drcmrtx) = 0;
186 DRCMR(host->dma_drcmrrx) = host->dma | DRCMR_MAPVLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 } else {
188 host->dma_dir = DMA_TO_DEVICE;
Matt Reimer7eeff482009-06-15 13:21:25 -0700189 dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG;
Bridge Wu9a788c62007-12-14 10:40:25 +0100190 DRCMR(host->dma_drcmrrx) = 0;
191 DRCMR(host->dma_drcmrtx) = host->dma | DRCMR_MAPVLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193
194 dcmd |= DCMD_BURST32 | DCMD_WIDTH1;
195
196 host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
197 host->dma_dir);
198
199 for (i = 0; i < host->dma_len; i++) {
Nicolas Pitrec7838372007-10-09 17:07:58 -0400200 unsigned int length = sg_dma_len(&data->sg[i]);
201 host->sg_cpu[i].dcmd = dcmd | length;
202 if (length & 31 && !(data->flags & MMC_DATA_READ))
203 host->sg_cpu[i].dcmd |= DCMD_ENDIRQEN;
Philipp Zabel97f85712008-07-06 01:15:34 +0200204 /* Not aligned to 8-byte boundary? */
205 if (sg_dma_address(&data->sg[i]) & 0x7)
206 dalgn = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (data->flags & MMC_DATA_READ) {
208 host->sg_cpu[i].dsadr = host->res->start + MMC_RXFIFO;
209 host->sg_cpu[i].dtadr = sg_dma_address(&data->sg[i]);
210 } else {
211 host->sg_cpu[i].dsadr = sg_dma_address(&data->sg[i]);
212 host->sg_cpu[i].dtadr = host->res->start + MMC_TXFIFO;
213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 host->sg_cpu[i].ddadr = host->sg_dma + (i + 1) *
215 sizeof(struct pxa_dma_desc);
216 }
217 host->sg_cpu[host->dma_len - 1].ddadr = DDADR_STOP;
218 wmb();
219
Philipp Zabel97f85712008-07-06 01:15:34 +0200220 /*
221 * The PXA27x DMA controller encounters overhead when working with
222 * unaligned (to 8-byte boundaries) data, so switch on byte alignment
223 * mode only if we have unaligned data.
224 */
225 if (dalgn)
226 DALGN |= (1 << host->dma);
227 else
Karl Beldan4fe16892008-07-16 18:29:11 +0200228 DALGN &= ~(1 << host->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 DDADR(host->dma) = host->sg_dma;
Cliff Brakeb6018952009-01-22 17:07:03 -0500230
231 /*
232 * workaround for erratum #91:
233 * only start DMA now if we are doing a read,
234 * otherwise we wait until CMD/RESP has finished
235 * before starting DMA.
236 */
237 if (!cpu_is_pxa27x() || data->flags & MMC_DATA_READ)
238 DCSR(host->dma) = DCSR_RUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241static void pxamci_start_cmd(struct pxamci_host *host, struct mmc_command *cmd, unsigned int cmdat)
242{
243 WARN_ON(host->cmd != NULL);
244 host->cmd = cmd;
245
246 if (cmd->flags & MMC_RSP_BUSY)
247 cmdat |= CMDAT_BUSY;
248
Russell Kinge9225172006-02-02 12:23:12 +0000249#define RSP_TYPE(x) ((x) & ~(MMC_RSP_BUSY|MMC_RSP_OPCODE))
250 switch (RSP_TYPE(mmc_resp_type(cmd))) {
Philip Langdale6f949902007-01-04 07:04:47 -0800251 case RSP_TYPE(MMC_RSP_R1): /* r1, r1b, r6, r7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 cmdat |= CMDAT_RESP_SHORT;
253 break;
Russell Kinge9225172006-02-02 12:23:12 +0000254 case RSP_TYPE(MMC_RSP_R3):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 cmdat |= CMDAT_RESP_R3;
256 break;
Russell Kinge9225172006-02-02 12:23:12 +0000257 case RSP_TYPE(MMC_RSP_R2):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 cmdat |= CMDAT_RESP_R2;
259 break;
260 default:
261 break;
262 }
263
264 writel(cmd->opcode, host->base + MMC_CMD);
265 writel(cmd->arg >> 16, host->base + MMC_ARGH);
266 writel(cmd->arg & 0xffff, host->base + MMC_ARGL);
267 writel(cmdat, host->base + MMC_CMDAT);
268 writel(host->clkrt, host->base + MMC_CLKRT);
269
270 writel(START_CLOCK, host->base + MMC_STRPCL);
271
272 pxamci_enable_irq(host, END_CMD_RES);
273}
274
275static void pxamci_finish_request(struct pxamci_host *host, struct mmc_request *mrq)
276{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 host->mrq = NULL;
278 host->cmd = NULL;
279 host->data = NULL;
280 mmc_request_done(host->mmc, mrq);
281}
282
283static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat)
284{
285 struct mmc_command *cmd = host->cmd;
286 int i;
287 u32 v;
288
289 if (!cmd)
290 return 0;
291
292 host->cmd = NULL;
293
294 /*
295 * Did I mention this is Sick. We always need to
296 * discard the upper 8 bits of the first 16-bit word.
297 */
298 v = readl(host->base + MMC_RES) & 0xffff;
299 for (i = 0; i < 4; i++) {
300 u32 w1 = readl(host->base + MMC_RES) & 0xffff;
301 u32 w2 = readl(host->base + MMC_RES) & 0xffff;
302 cmd->resp[i] = v << 24 | w1 << 8 | w2 >> 8;
303 v = w2;
304 }
305
306 if (stat & STAT_TIME_OUT_RESPONSE) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200307 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 } else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /*
310 * workaround for erratum #42:
311 * Intel PXA27x Family Processor Specification Update Rev 001
Nicolas Pitre90e07d92007-05-13 18:03:08 +0200312 * A bogus CRC error can appear if the msb of a 136 bit
313 * response is a one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 */
Cliff Brakee10a8542009-01-22 16:58:58 -0500315 if (cpu_is_pxa27x() &&
316 (cmd->flags & MMC_RSP_136 && cmd->resp[0] & 0x80000000))
Nicolas Pitre90e07d92007-05-13 18:03:08 +0200317 pr_debug("ignoring CRC from command %d - *risky*\n", cmd->opcode);
Cliff Brakee10a8542009-01-22 16:58:58 -0500318 else
319 cmd->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321
322 pxamci_disable_irq(host, END_CMD_RES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200323 if (host->data && !cmd->error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 pxamci_enable_irq(host, DATA_TRAN_DONE);
Cliff Brakeb6018952009-01-22 17:07:03 -0500325 /*
326 * workaround for erratum #91, if doing write
327 * enable DMA late
328 */
329 if (cpu_is_pxa27x() && host->data->flags & MMC_DATA_WRITE)
330 DCSR(host->dma) = DCSR_RUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 } else {
332 pxamci_finish_request(host, host->mrq);
333 }
334
335 return 1;
336}
337
338static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
339{
340 struct mmc_data *data = host->data;
341
342 if (!data)
343 return 0;
344
345 DCSR(host->dma) = 0;
Vernon Sauderc00a46a2008-12-29 19:21:28 -0500346 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 host->dma_dir);
348
349 if (stat & STAT_READ_TIME_OUT)
Pierre Ossman17b04292007-07-22 22:18:46 +0200350 data->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR))
Pierre Ossman17b04292007-07-22 22:18:46 +0200352 data->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /*
355 * There appears to be a hardware design bug here. There seems to
356 * be no way to find out how much data was transferred to the card.
357 * This means that if there was an error on any block, we mark all
358 * data blocks as being in error.
359 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200360 if (!data->error)
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100361 data->bytes_xfered = data->blocks * data->blksz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 else
363 data->bytes_xfered = 0;
364
365 pxamci_disable_irq(host, DATA_TRAN_DONE);
366
367 host->data = NULL;
Russell King58741e82006-05-02 20:02:39 +0100368 if (host->mrq->stop) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 pxamci_stop_clock(host);
Bridge Wudf456f42007-09-25 19:09:19 +0200370 pxamci_start_cmd(host, host->mrq->stop, host->cmdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 } else {
372 pxamci_finish_request(host, host->mrq);
373 }
374
375 return 1;
376}
377
David Howells7d12e782006-10-05 14:55:46 +0100378static irqreturn_t pxamci_irq(int irq, void *devid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 struct pxamci_host *host = devid;
381 unsigned int ireg;
382 int handled = 0;
383
Bridge Wu81ab570f2007-09-25 18:59:07 +0200384 ireg = readl(host->base + MMC_I_REG) & ~readl(host->base + MMC_I_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (ireg) {
387 unsigned stat = readl(host->base + MMC_STAT);
388
Russell Kingd78e9072006-05-02 20:18:53 +0100389 pr_debug("PXAMCI: irq %08x stat %08x\n", ireg, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (ireg & END_CMD_RES)
392 handled |= pxamci_cmd_done(host, stat);
393 if (ireg & DATA_TRAN_DONE)
394 handled |= pxamci_data_done(host, stat);
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200395 if (ireg & SDIO_INT) {
396 mmc_signal_sdio_irq(host->mmc);
397 handled = 1;
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
401 return IRQ_RETVAL(handled);
402}
403
404static void pxamci_request(struct mmc_host *mmc, struct mmc_request *mrq)
405{
406 struct pxamci_host *host = mmc_priv(mmc);
407 unsigned int cmdat;
408
409 WARN_ON(host->mrq != NULL);
410
411 host->mrq = mrq;
412
413 pxamci_stop_clock(host);
414
415 cmdat = host->cmdat;
416 host->cmdat &= ~CMDAT_INIT;
417
418 if (mrq->data) {
419 pxamci_setup_data(host, mrq->data);
420
421 cmdat &= ~CMDAT_BUSY;
422 cmdat |= CMDAT_DATAEN | CMDAT_DMAEN;
423 if (mrq->data->flags & MMC_DATA_WRITE)
424 cmdat |= CMDAT_WRITE;
425
426 if (mrq->data->flags & MMC_DATA_STREAM)
427 cmdat |= CMDAT_STREAM;
428 }
429
430 pxamci_start_cmd(host, mrq->cmd, cmdat);
431}
432
Richard Purdiee6195242005-09-06 15:18:56 -0700433static int pxamci_get_ro(struct mmc_host *mmc)
434{
435 struct pxamci_host *host = mmc_priv(mmc);
436
Robert Jarzmikb405db62009-06-23 23:21:03 +0200437 if (host->pdata && gpio_is_valid(host->pdata->gpio_card_ro)) {
438 if (host->pdata->gpio_card_ro_invert)
439 return !gpio_get_value(host->pdata->gpio_card_ro);
440 else
441 return gpio_get_value(host->pdata->gpio_card_ro);
442 }
Richard Purdiee6195242005-09-06 15:18:56 -0700443 if (host->pdata && host->pdata->get_ro)
Anton Vorontsov08f80bb2008-06-17 18:17:39 +0400444 return !!host->pdata->get_ro(mmc_dev(mmc));
445 /*
446 * Board doesn't support read only detection; let the mmc core
447 * decide what to do.
448 */
449 return -ENOSYS;
Richard Purdiee6195242005-09-06 15:18:56 -0700450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
453{
454 struct pxamci_host *host = mmc_priv(mmc);
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (ios->clock) {
Russell Kingebebd9b2007-08-20 10:20:03 +0100457 unsigned long rate = host->clkrate;
458 unsigned int clk = rate / ios->clock;
459
Russell Kingd8cb70d2007-10-26 17:56:40 +0100460 if (host->clkrt == CLKRT_OFF)
461 clk_enable(host->clk);
462
Bridge Wu64eb0362007-12-13 07:24:30 +0100463 if (ios->clock == 26000000) {
Haojian Zhuangfa3f9932009-08-31 21:52:53 +0800464 /* to support 26MHz */
Bridge Wu64eb0362007-12-13 07:24:30 +0100465 host->clkrt = 7;
466 } else {
467 /* to handle (19.5MHz, 26MHz) */
468 if (!clk)
469 clk = 1;
470
471 /*
472 * clk might result in a lower divisor than we
473 * desire. check for that condition and adjust
474 * as appropriate.
475 */
476 if (rate / clk > ios->clock)
477 clk <<= 1;
478 host->clkrt = fls(clk) - 1;
479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 /*
482 * we write clkrt on the next command
483 */
484 } else {
485 pxamci_stop_clock(host);
Russell Kingd8cb70d2007-10-26 17:56:40 +0100486 if (host->clkrt != CLKRT_OFF) {
487 host->clkrt = CLKRT_OFF;
488 clk_disable(host->clk);
489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491
492 if (host->power_mode != ios->power_mode) {
493 host->power_mode = ios->power_mode;
494
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -0300495 pxamci_set_power(host, ios->vdd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 if (ios->power_mode == MMC_POWER_ON)
498 host->cmdat |= CMDAT_INIT;
499 }
500
Bridge Wudf456f42007-09-25 19:09:19 +0200501 if (ios->bus_width == MMC_BUS_WIDTH_4)
502 host->cmdat |= CMDAT_SD_4DAT;
503 else
504 host->cmdat &= ~CMDAT_SD_4DAT;
505
Russell Kingd78e9072006-05-02 20:18:53 +0100506 pr_debug("PXAMCI: clkrt = %x cmdat = %x\n",
Russell Kingc6563172006-03-29 09:30:20 +0100507 host->clkrt, host->cmdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200510static void pxamci_enable_sdio_irq(struct mmc_host *host, int enable)
511{
512 struct pxamci_host *pxa_host = mmc_priv(host);
513
514 if (enable)
515 pxamci_enable_irq(pxa_host, SDIO_INT);
516 else
517 pxamci_disable_irq(pxa_host, SDIO_INT);
518}
519
David Brownellab7aefd2006-11-12 17:55:30 -0800520static const struct mmc_host_ops pxamci_ops = {
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200521 .request = pxamci_request,
522 .get_ro = pxamci_get_ro,
523 .set_ios = pxamci_set_ios,
524 .enable_sdio_irq = pxamci_enable_sdio_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525};
526
David Howells7d12e782006-10-05 14:55:46 +0100527static void pxamci_dma_irq(int dma, void *devid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Nicolas Pitrec7838372007-10-09 17:07:58 -0400529 struct pxamci_host *host = devid;
530 int dcsr = DCSR(dma);
531 DCSR(dma) = dcsr & ~DCSR_STOPIRQEN;
532
533 if (dcsr & DCSR_ENDINTR) {
534 writel(BUF_PART_FULL, host->base + MMC_PRTBUF);
535 } else {
536 printk(KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n",
537 mmc_hostname(host->mmc), dma, dcsr);
538 host->data->error = -EIO;
539 pxamci_data_done(host, 0);
540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
David Howells7d12e782006-10-05 14:55:46 +0100543static irqreturn_t pxamci_detect_irq(int irq, void *devid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Richard Purdiec26971c2005-09-08 22:48:16 +0100545 struct pxamci_host *host = mmc_priv(devid);
546
Eric Miaof97cab22010-04-14 07:00:42 +0800547 mmc_detect_change(devid, msecs_to_jiffies(host->pdata->detect_delay_ms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return IRQ_HANDLED;
549}
550
Russell King3ae5eae2005-11-09 22:32:44 +0000551static int pxamci_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 struct mmc_host *mmc;
554 struct pxamci_host *host = NULL;
Bridge Wu9a788c62007-12-14 10:40:25 +0100555 struct resource *r, *dmarx, *dmatx;
Robert Jarzmikb405db62009-06-23 23:21:03 +0200556 int ret, irq, gpio_cd = -1, gpio_ro = -1, gpio_power = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
559 irq = platform_get_irq(pdev, 0);
David Vrabel48944732006-01-19 17:56:29 +0000560 if (!r || irq < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return -ENXIO;
562
563 r = request_mem_region(r->start, SZ_4K, DRIVER_NAME);
564 if (!r)
565 return -EBUSY;
566
Russell King3ae5eae2005-11-09 22:32:44 +0000567 mmc = mmc_alloc_host(sizeof(struct pxamci_host), &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!mmc) {
569 ret = -ENOMEM;
570 goto out;
571 }
572
573 mmc->ops = &pxamci_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 /*
576 * We can do SG-DMA, but we don't because we never know how much
577 * data we successfully wrote to the card.
578 */
Martin K. Petersena36274e2010-09-10 01:33:59 -0400579 mmc->max_segs = NR_SG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 /*
582 * Our hardware DMA can handle a maximum of one page per SG entry.
583 */
584 mmc->max_seg_size = PAGE_SIZE;
585
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100586 /*
Nicolas Pitrefe2dc442007-09-24 15:47:18 -0400587 * Block length register is only 10 bits before PXA27x.
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100588 */
Eric Miao0ffcbfd2008-09-11 10:27:30 +0800589 mmc->max_blk_size = cpu_is_pxa25x() ? 1023 : 2048;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100590
Pierre Ossman55db8902006-11-21 17:55:45 +0100591 /*
592 * Block count register is 16 bits.
593 */
594 mmc->max_blk_count = 65535;
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 host = mmc_priv(mmc);
597 host->mmc = mmc;
598 host->dma = -1;
599 host->pdata = pdev->dev.platform_data;
Russell Kingd8cb70d2007-10-26 17:56:40 +0100600 host->clkrt = CLKRT_OFF;
Russell Kingebebd9b2007-08-20 10:20:03 +0100601
Russell Kinge0d8b132008-11-11 17:52:32 +0000602 host->clk = clk_get(&pdev->dev, NULL);
Russell Kingebebd9b2007-08-20 10:20:03 +0100603 if (IS_ERR(host->clk)) {
604 ret = PTR_ERR(host->clk);
605 host->clk = NULL;
606 goto out;
607 }
608
609 host->clkrate = clk_get_rate(host->clk);
610
611 /*
612 * Calculate minimum clock rate, rounding up.
613 */
614 mmc->f_min = (host->clkrate + 63) / 64;
Haojian Zhuangfa3f9932009-08-31 21:52:53 +0800615 mmc->f_max = (mmc_has_26MHz()) ? 26000000 : host->clkrate;
Russell Kingebebd9b2007-08-20 10:20:03 +0100616
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -0300617 pxamci_init_ocr(host);
618
Bridge Wudf456f42007-09-25 19:09:19 +0200619 mmc->caps = 0;
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200620 host->cmdat = 0;
Eric Miao0ffcbfd2008-09-11 10:27:30 +0800621 if (!cpu_is_pxa25x()) {
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200622 mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
623 host->cmdat |= CMDAT_SDIO_INT_EN;
Haojian Zhuangfa3f9932009-08-31 21:52:53 +0800624 if (mmc_has_26MHz())
Bridge Wu64eb0362007-12-13 07:24:30 +0100625 mmc->caps |= MMC_CAP_MMC_HIGHSPEED |
626 MMC_CAP_SD_HIGHSPEED;
Bridge Wu5d3ad4e2007-09-25 19:11:00 +0200627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Russell King3ae5eae2005-11-09 22:32:44 +0000629 host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &host->sg_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (!host->sg_cpu) {
631 ret = -ENOMEM;
632 goto out;
633 }
634
635 spin_lock_init(&host->lock);
636 host->res = r;
637 host->irq = irq;
638 host->imask = MMC_I_MASK_ALL;
639
640 host->base = ioremap(r->start, SZ_4K);
641 if (!host->base) {
642 ret = -ENOMEM;
643 goto out;
644 }
645
646 /*
647 * Ensure that the host controller is shut down, and setup
648 * with our defaults.
649 */
650 pxamci_stop_clock(host);
651 writel(0, host->base + MMC_SPI);
652 writel(64, host->base + MMC_RESTO);
653 writel(host->imask, host->base + MMC_I_MASK);
654
655 host->dma = pxa_request_dma(DRIVER_NAME, DMA_PRIO_LOW,
656 pxamci_dma_irq, host);
657 if (host->dma < 0) {
658 ret = -EBUSY;
659 goto out;
660 }
661
662 ret = request_irq(host->irq, pxamci_irq, 0, DRIVER_NAME, host);
663 if (ret)
664 goto out;
665
Russell King3ae5eae2005-11-09 22:32:44 +0000666 platform_set_drvdata(pdev, mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Bridge Wu9a788c62007-12-14 10:40:25 +0100668 dmarx = platform_get_resource(pdev, IORESOURCE_DMA, 0);
669 if (!dmarx) {
670 ret = -ENXIO;
671 goto out;
672 }
673 host->dma_drcmrrx = dmarx->start;
674
675 dmatx = platform_get_resource(pdev, IORESOURCE_DMA, 1);
676 if (!dmatx) {
677 ret = -ENXIO;
678 goto out;
679 }
680 host->dma_drcmrtx = dmatx->start;
681
Robert Jarzmikb405db62009-06-23 23:21:03 +0200682 if (host->pdata) {
683 gpio_cd = host->pdata->gpio_card_detect;
684 gpio_ro = host->pdata->gpio_card_ro;
685 gpio_power = host->pdata->gpio_power;
686 }
687 if (gpio_is_valid(gpio_power)) {
688 ret = gpio_request(gpio_power, "mmc card power");
689 if (ret) {
690 dev_err(&pdev->dev, "Failed requesting gpio_power %d\n", gpio_power);
691 goto out;
692 }
693 gpio_direction_output(gpio_power,
694 host->pdata->gpio_power_invert);
695 }
696 if (gpio_is_valid(gpio_ro)) {
697 ret = gpio_request(gpio_ro, "mmc card read only");
698 if (ret) {
Antonio Ospite48f02952009-10-02 16:24:02 +0200699 dev_err(&pdev->dev, "Failed requesting gpio_ro %d\n", gpio_ro);
Robert Jarzmikb405db62009-06-23 23:21:03 +0200700 goto err_gpio_ro;
701 }
702 gpio_direction_input(gpio_ro);
703 }
704 if (gpio_is_valid(gpio_cd)) {
705 ret = gpio_request(gpio_cd, "mmc card detect");
706 if (ret) {
Antonio Ospite48f02952009-10-02 16:24:02 +0200707 dev_err(&pdev->dev, "Failed requesting gpio_cd %d\n", gpio_cd);
Robert Jarzmikb405db62009-06-23 23:21:03 +0200708 goto err_gpio_cd;
709 }
710 gpio_direction_input(gpio_cd);
711
712 ret = request_irq(gpio_to_irq(gpio_cd), pxamci_detect_irq,
713 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
714 "mmc card detect", mmc);
715 if (ret) {
716 dev_err(&pdev->dev, "failed to request card detect IRQ\n");
717 goto err_request_irq;
718 }
719 }
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (host->pdata && host->pdata->init)
Russell King3ae5eae2005-11-09 22:32:44 +0000722 host->pdata->init(&pdev->dev, pxamci_detect_irq, mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Robert Jarzmikb405db62009-06-23 23:21:03 +0200724 if (gpio_is_valid(gpio_power) && host->pdata->setpower)
725 dev_warn(&pdev->dev, "gpio_power and setpower() both defined\n");
726 if (gpio_is_valid(gpio_ro) && host->pdata->get_ro)
727 dev_warn(&pdev->dev, "gpio_ro and get_ro() both defined\n");
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 mmc_add_host(mmc);
730
731 return 0;
732
Robert Jarzmikb405db62009-06-23 23:21:03 +0200733err_request_irq:
734 gpio_free(gpio_cd);
735err_gpio_cd:
736 gpio_free(gpio_ro);
737err_gpio_ro:
738 gpio_free(gpio_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 out:
740 if (host) {
741 if (host->dma >= 0)
742 pxa_free_dma(host->dma);
743 if (host->base)
744 iounmap(host->base);
745 if (host->sg_cpu)
Russell King3ae5eae2005-11-09 22:32:44 +0000746 dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
Russell Kingebebd9b2007-08-20 10:20:03 +0100747 if (host->clk)
748 clk_put(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750 if (mmc)
751 mmc_free_host(mmc);
752 release_resource(r);
753 return ret;
754}
755
Russell King3ae5eae2005-11-09 22:32:44 +0000756static int pxamci_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Russell King3ae5eae2005-11-09 22:32:44 +0000758 struct mmc_host *mmc = platform_get_drvdata(pdev);
Robert Jarzmikb405db62009-06-23 23:21:03 +0200759 int gpio_cd = -1, gpio_ro = -1, gpio_power = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Russell King3ae5eae2005-11-09 22:32:44 +0000761 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 if (mmc) {
764 struct pxamci_host *host = mmc_priv(mmc);
765
Daniel Mack5d6b1ed2009-12-01 18:17:18 +0100766 mmc_remove_host(mmc);
767
Robert Jarzmikb405db62009-06-23 23:21:03 +0200768 if (host->pdata) {
769 gpio_cd = host->pdata->gpio_card_detect;
770 gpio_ro = host->pdata->gpio_card_ro;
771 gpio_power = host->pdata->gpio_power;
772 }
773 if (gpio_is_valid(gpio_cd)) {
774 free_irq(gpio_to_irq(gpio_cd), mmc);
775 gpio_free(gpio_cd);
776 }
777 if (gpio_is_valid(gpio_ro))
778 gpio_free(gpio_ro);
779 if (gpio_is_valid(gpio_power))
780 gpio_free(gpio_power);
Daniel Ribeiro8385f9c2009-05-21 08:54:18 -0300781 if (host->vcc)
782 regulator_put(host->vcc);
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (host->pdata && host->pdata->exit)
Russell King3ae5eae2005-11-09 22:32:44 +0000785 host->pdata->exit(&pdev->dev, mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 pxamci_stop_clock(host);
788 writel(TXFIFO_WR_REQ|RXFIFO_RD_REQ|CLK_IS_OFF|STOP_CMD|
789 END_CMD_RES|PRG_DONE|DATA_TRAN_DONE,
790 host->base + MMC_I_MASK);
791
Bridge Wu9a788c62007-12-14 10:40:25 +0100792 DRCMR(host->dma_drcmrrx) = 0;
793 DRCMR(host->dma_drcmrtx) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 free_irq(host->irq, host);
796 pxa_free_dma(host->dma);
797 iounmap(host->base);
Russell King3ae5eae2005-11-09 22:32:44 +0000798 dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Russell Kingebebd9b2007-08-20 10:20:03 +0100800 clk_put(host->clk);
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 release_resource(host->res);
803
804 mmc_free_host(mmc);
805 }
806 return 0;
807}
808
809#ifdef CONFIG_PM
Mike Rapoport33264f92009-07-29 11:59:24 +0300810static int pxamci_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Mike Rapoport33264f92009-07-29 11:59:24 +0300812 struct mmc_host *mmc = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 int ret = 0;
814
Russell King9480e302005-10-28 09:52:56 -0700815 if (mmc)
Matt Fleming1a13f8f2010-05-26 14:42:08 -0700816 ret = mmc_suspend_host(mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 return ret;
819}
820
Mike Rapoport33264f92009-07-29 11:59:24 +0300821static int pxamci_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Mike Rapoport33264f92009-07-29 11:59:24 +0300823 struct mmc_host *mmc = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 int ret = 0;
825
Russell King9480e302005-10-28 09:52:56 -0700826 if (mmc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 ret = mmc_resume_host(mmc);
828
829 return ret;
830}
Mike Rapoport33264f92009-07-29 11:59:24 +0300831
Alexey Dobriyan47145212009-12-14 18:00:08 -0800832static const struct dev_pm_ops pxamci_pm_ops = {
Mike Rapoport33264f92009-07-29 11:59:24 +0300833 .suspend = pxamci_suspend,
834 .resume = pxamci_resume,
835};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836#endif
837
Russell King3ae5eae2005-11-09 22:32:44 +0000838static struct platform_driver pxamci_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 .probe = pxamci_probe,
840 .remove = pxamci_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000841 .driver = {
842 .name = DRIVER_NAME,
Kay Sieversbc65c722008-04-15 14:34:28 -0700843 .owner = THIS_MODULE,
Mike Rapoport33264f92009-07-29 11:59:24 +0300844#ifdef CONFIG_PM
845 .pm = &pxamci_pm_ops,
846#endif
Russell King3ae5eae2005-11-09 22:32:44 +0000847 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848};
849
850static int __init pxamci_init(void)
851{
Russell King3ae5eae2005-11-09 22:32:44 +0000852 return platform_driver_register(&pxamci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
855static void __exit pxamci_exit(void)
856{
Russell King3ae5eae2005-11-09 22:32:44 +0000857 platform_driver_unregister(&pxamci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
860module_init(pxamci_init);
861module_exit(pxamci_exit);
862
863MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver");
864MODULE_LICENSE("GPL");
Kay Sieversbc65c722008-04-15 14:34:28 -0700865MODULE_ALIAS("platform:pxa2xx-mci");