blob: 97c9b3638d57ea81aef3917c042b0067234fa31b [file] [log] [blame]
San Mehat9d2bd732009-09-22 16:44:22 -07001/*
2 * linux/drivers/mmc/host/msm_sdcc.c - Qualcomm MSM 7X00A SDCC Driver
3 *
4 * Copyright (C) 2007 Google Inc,
5 * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
San Mehat56a8b5b2009-11-21 12:29:46 -08006 * Copyright (C) 2009, Code Aurora Forum. All Rights Reserved.
San Mehat9d2bd732009-09-22 16:44:22 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * Based on mmci.c
13 *
14 * Author: San Mehat (san@android.com)
15 *
16 */
17
18#include <linux/module.h>
19#include <linux/moduleparam.h>
20#include <linux/init.h>
21#include <linux/ioport.h>
22#include <linux/device.h>
23#include <linux/interrupt.h>
24#include <linux/delay.h>
25#include <linux/err.h>
26#include <linux/highmem.h>
27#include <linux/log2.h>
28#include <linux/mmc/host.h>
29#include <linux/mmc/card.h>
San Mehatb3fa5792009-11-02 18:46:09 -080030#include <linux/mmc/sdio.h>
San Mehat9d2bd732009-09-22 16:44:22 -070031#include <linux/clk.h>
32#include <linux/scatterlist.h>
33#include <linux/platform_device.h>
34#include <linux/dma-mapping.h>
35#include <linux/debugfs.h>
36#include <linux/io.h>
37#include <linux/memory.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/gfp.h>
Sahitya Tummala7a892482011-01-18 11:22:49 +053039#include <linux/gpio.h>
San Mehat9d2bd732009-09-22 16:44:22 -070040
41#include <asm/cacheflush.h>
42#include <asm/div64.h>
43#include <asm/sizes.h>
44
Pavel Machek3989d172009-12-08 11:11:36 -080045#include <mach/mmc.h>
San Mehat9d2bd732009-09-22 16:44:22 -070046#include <mach/msm_iomap.h>
47#include <mach/dma.h>
Sahitya Tummalab08bb352010-12-08 15:03:05 +053048#include <mach/clk.h>
San Mehat9d2bd732009-09-22 16:44:22 -070049
San Mehat9d2bd732009-09-22 16:44:22 -070050#include "msm_sdcc.h"
51
52#define DRIVER_NAME "msm-sdcc"
53
San Mehat24bbd7d2009-12-01 10:10:47 -080054#define BUSCLK_PWRSAVE 1
San Mehatc7fc9372009-11-22 17:19:07 -080055#define BUSCLK_TIMEOUT (HZ)
San Mehat9d2bd732009-09-22 16:44:22 -070056static unsigned int msmsdcc_fmin = 144000;
57static unsigned int msmsdcc_fmax = 50000000;
58static unsigned int msmsdcc_4bit = 1;
59static unsigned int msmsdcc_pwrsave = 1;
60static unsigned int msmsdcc_piopoll = 1;
61static unsigned int msmsdcc_sdioirq;
62
63#define PIO_SPINMAX 30
64#define CMD_SPINMAX 20
65
San Mehat865c8062009-11-13 13:42:06 -080066
San Mehatd0719e52009-12-03 10:58:54 -080067static inline void
San Mehatc7fc9372009-11-22 17:19:07 -080068msmsdcc_disable_clocks(struct msmsdcc_host *host, int deferr)
San Mehat865c8062009-11-13 13:42:06 -080069{
San Mehatc7fc9372009-11-22 17:19:07 -080070 WARN_ON(!host->clks_on);
San Mehat8b1c2ba2009-11-16 10:17:30 -080071
San Mehatf4748492009-11-23 15:36:31 -080072 BUG_ON(host->curr.mrq);
73
San Mehatc7fc9372009-11-22 17:19:07 -080074 if (deferr) {
75 mod_timer(&host->busclk_timer, jiffies + BUSCLK_TIMEOUT);
San Mehat865c8062009-11-13 13:42:06 -080076 } else {
San Mehatc7fc9372009-11-22 17:19:07 -080077 del_timer_sync(&host->busclk_timer);
San Mehatd0719e52009-12-03 10:58:54 -080078 /* Need to check clks_on again in case the busclk
79 * timer fired
80 */
81 if (host->clks_on) {
82 clk_disable(host->clk);
83 clk_disable(host->pclk);
84 host->clks_on = 0;
85 }
San Mehat865c8062009-11-13 13:42:06 -080086 }
San Mehatc7fc9372009-11-22 17:19:07 -080087}
88
89static inline int
90msmsdcc_enable_clocks(struct msmsdcc_host *host)
91{
92 int rc;
93
San Mehatc7fc9372009-11-22 17:19:07 -080094 del_timer_sync(&host->busclk_timer);
95
San Mehatd0719e52009-12-03 10:58:54 -080096 if (!host->clks_on) {
97 rc = clk_enable(host->pclk);
98 if (rc)
99 return rc;
100 rc = clk_enable(host->clk);
101 if (rc) {
102 clk_disable(host->pclk);
103 return rc;
104 }
105 udelay(1 + ((3 * USEC_PER_SEC) /
106 (host->clk_rate ? host->clk_rate : msmsdcc_fmin)));
107 host->clks_on = 1;
San Mehatc7fc9372009-11-22 17:19:07 -0800108 }
San Mehat865c8062009-11-13 13:42:06 -0800109 return 0;
110}
111
San Mehat8b1c2ba2009-11-16 10:17:30 -0800112static inline unsigned int
113msmsdcc_readl(struct msmsdcc_host *host, unsigned int reg)
114{
115 return readl(host->base + reg);
116}
117
118static inline void
119msmsdcc_writel(struct msmsdcc_host *host, u32 data, unsigned int reg)
120{
121 writel(data, host->base + reg);
122 /* 3 clk delay required! */
123 udelay(1 + ((3 * USEC_PER_SEC) /
124 (host->clk_rate ? host->clk_rate : msmsdcc_fmin)));
125}
San Mehat865c8062009-11-13 13:42:06 -0800126
San Mehat9d2bd732009-09-22 16:44:22 -0700127static void
128msmsdcc_start_command(struct msmsdcc_host *host, struct mmc_command *cmd,
129 u32 c);
130
Sahitya Tummalab08bb352010-12-08 15:03:05 +0530131static void msmsdcc_reset_and_restore(struct msmsdcc_host *host)
132{
133 u32 mci_clk = 0;
134 u32 mci_mask0 = 0;
135 int ret = 0;
136
137 /* Save the controller state */
138 mci_clk = readl(host->base + MMCICLOCK);
139 mci_mask0 = readl(host->base + MMCIMASK0);
140
141 /* Reset the controller */
142 ret = clk_reset(host->clk, CLK_RESET_ASSERT);
143 if (ret)
144 pr_err("%s: Clock assert failed at %u Hz with err %d\n",
145 mmc_hostname(host->mmc), host->clk_rate, ret);
146
147 ret = clk_reset(host->clk, CLK_RESET_DEASSERT);
148 if (ret)
149 pr_err("%s: Clock deassert failed at %u Hz with err %d\n",
150 mmc_hostname(host->mmc), host->clk_rate, ret);
151
152 pr_info("%s: Controller has been re-initialiazed\n",
153 mmc_hostname(host->mmc));
154
155 /* Restore the contoller state */
156 writel(host->pwr, host->base + MMCIPOWER);
157 writel(mci_clk, host->base + MMCICLOCK);
158 writel(mci_mask0, host->base + MMCIMASK0);
159 ret = clk_set_rate(host->clk, host->clk_rate);
160 if (ret)
161 pr_err("%s: Failed to set clk rate %u Hz (%d)\n",
162 mmc_hostname(host->mmc), host->clk_rate, ret);
163}
164
San Mehat9d2bd732009-09-22 16:44:22 -0700165static void
166msmsdcc_request_end(struct msmsdcc_host *host, struct mmc_request *mrq)
167{
San Mehat9d2bd732009-09-22 16:44:22 -0700168 BUG_ON(host->curr.data);
169
170 host->curr.mrq = NULL;
171 host->curr.cmd = NULL;
172
173 if (mrq->data)
174 mrq->data->bytes_xfered = host->curr.data_xfered;
175 if (mrq->cmd->error == -ETIMEDOUT)
176 mdelay(5);
177
San Mehatf4748492009-11-23 15:36:31 -0800178#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -0800179 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -0800180#endif
San Mehat9d2bd732009-09-22 16:44:22 -0700181 /*
182 * Need to drop the host lock here; mmc_request_done may call
183 * back into the driver...
184 */
185 spin_unlock(&host->lock);
186 mmc_request_done(host->mmc, mrq);
187 spin_lock(&host->lock);
188}
189
190static void
191msmsdcc_stop_data(struct msmsdcc_host *host)
192{
San Mehat9d2bd732009-09-22 16:44:22 -0700193 host->curr.data = NULL;
Sahitya Tummala0c521cc2010-12-08 15:03:07 +0530194 host->curr.got_dataend = 0;
San Mehat9d2bd732009-09-22 16:44:22 -0700195}
196
197uint32_t msmsdcc_fifo_addr(struct msmsdcc_host *host)
198{
Sahitya Tummalaedd4dd02010-07-29 16:57:41 +0530199 return host->memres->start + MMCIFIFO;
San Mehat9d2bd732009-09-22 16:44:22 -0700200}
201
San Mehat56a8b5b2009-11-21 12:29:46 -0800202static inline void
203msmsdcc_start_command_exec(struct msmsdcc_host *host, u32 arg, u32 c) {
204 msmsdcc_writel(host, arg, MMCIARGUMENT);
205 msmsdcc_writel(host, c, MMCICOMMAND);
206}
207
208static void
209msmsdcc_dma_exec_func(struct msm_dmov_cmd *cmd)
210{
San Mehat6ac9ea62009-12-02 17:24:58 -0800211 struct msmsdcc_host *host = (struct msmsdcc_host *)cmd->data;
San Mehat56a8b5b2009-11-21 12:29:46 -0800212
San Mehat6ac9ea62009-12-02 17:24:58 -0800213 msmsdcc_writel(host, host->cmd_timeout, MMCIDATATIMER);
San Mehatd0719e52009-12-03 10:58:54 -0800214 msmsdcc_writel(host, (unsigned int)host->curr.xfer_size,
215 MMCIDATALENGTH);
San Mehat6ac9ea62009-12-02 17:24:58 -0800216 msmsdcc_writel(host, host->cmd_pio_irqmask, MMCIMASK1);
217 msmsdcc_writel(host, host->cmd_datactrl, MMCIDATACTRL);
San Mehat56a8b5b2009-11-21 12:29:46 -0800218
San Mehat6ac9ea62009-12-02 17:24:58 -0800219 if (host->cmd_cmd) {
220 msmsdcc_start_command_exec(host,
221 (u32) host->cmd_cmd->arg,
222 (u32) host->cmd_c);
223 }
San Mehat56a8b5b2009-11-21 12:29:46 -0800224 host->dma.active = 1;
225}
226
San Mehat9d2bd732009-09-22 16:44:22 -0700227static void
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530228msmsdcc_dma_complete_tlet(unsigned long data)
San Mehat9d2bd732009-09-22 16:44:22 -0700229{
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530230 struct msmsdcc_host *host = (struct msmsdcc_host *)data;
San Mehat9d2bd732009-09-22 16:44:22 -0700231 unsigned long flags;
232 struct mmc_request *mrq;
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530233 struct msm_dmov_errdata err;
San Mehat9d2bd732009-09-22 16:44:22 -0700234
235 spin_lock_irqsave(&host->lock, flags);
San Mehat56a8b5b2009-11-21 12:29:46 -0800236 host->dma.active = 0;
237
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530238 err = host->dma.err;
San Mehat9d2bd732009-09-22 16:44:22 -0700239 mrq = host->curr.mrq;
240 BUG_ON(!mrq);
San Mehatb3b0ca82009-11-24 12:24:55 -0800241 WARN_ON(!mrq->data);
San Mehat9d2bd732009-09-22 16:44:22 -0700242
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530243 if (!(host->dma.result & DMOV_RSLT_VALID)) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700244 pr_err("msmsdcc: Invalid DataMover result\n");
San Mehat9d2bd732009-09-22 16:44:22 -0700245 goto out;
246 }
247
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530248 if (host->dma.result & DMOV_RSLT_DONE) {
San Mehat9d2bd732009-09-22 16:44:22 -0700249 host->curr.data_xfered = host->curr.xfer_size;
250 } else {
251 /* Error or flush */
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530252 if (host->dma.result & DMOV_RSLT_ERROR)
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700253 pr_err("%s: DMA error (0x%.8x)\n",
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530254 mmc_hostname(host->mmc), host->dma.result);
255 if (host->dma.result & DMOV_RSLT_FLUSH)
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700256 pr_err("%s: DMA channel flushed (0x%.8x)\n",
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530257 mmc_hostname(host->mmc), host->dma.result);
258
259 pr_err("Flush data: %.8x %.8x %.8x %.8x %.8x %.8x\n",
260 err.flush[0], err.flush[1], err.flush[2],
261 err.flush[3], err.flush[4], err.flush[5]);
Sahitya Tummalab08bb352010-12-08 15:03:05 +0530262
263 msmsdcc_reset_and_restore(host);
San Mehat9d2bd732009-09-22 16:44:22 -0700264 if (!mrq->data->error)
265 mrq->data->error = -EIO;
266 }
San Mehat9d2bd732009-09-22 16:44:22 -0700267 dma_unmap_sg(mmc_dev(host->mmc), host->dma.sg, host->dma.num_ents,
268 host->dma.dir);
269
270 if (host->curr.user_pages) {
271 struct scatterlist *sg = host->dma.sg;
272 int i;
273
Joe Perches75d14522009-09-22 16:44:24 -0700274 for (i = 0; i < host->dma.num_ents; i++)
275 flush_dcache_page(sg_page(sg++));
San Mehat9d2bd732009-09-22 16:44:22 -0700276 }
277
278 host->dma.sg = NULL;
San Mehat56a8b5b2009-11-21 12:29:46 -0800279 host->dma.busy = 0;
San Mehat9d2bd732009-09-22 16:44:22 -0700280
Sahitya Tummala0c521cc2010-12-08 15:03:07 +0530281 if (host->curr.got_dataend || mrq->data->error) {
San Mehat9d2bd732009-09-22 16:44:22 -0700282
283 /*
284 * If we've already gotten our DATAEND / DATABLKEND
285 * for this request, then complete it through here.
286 */
287 msmsdcc_stop_data(host);
288
289 if (!mrq->data->error)
290 host->curr.data_xfered = host->curr.xfer_size;
291 if (!mrq->data->stop || mrq->cmd->error) {
San Mehat9d2bd732009-09-22 16:44:22 -0700292 host->curr.mrq = NULL;
293 host->curr.cmd = NULL;
294 mrq->data->bytes_xfered = host->curr.data_xfered;
295
296 spin_unlock_irqrestore(&host->lock, flags);
San Mehatf4748492009-11-23 15:36:31 -0800297#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -0800298 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -0800299#endif
San Mehat9d2bd732009-09-22 16:44:22 -0700300 mmc_request_done(host->mmc, mrq);
301 return;
302 } else
303 msmsdcc_start_command(host, mrq->data->stop, 0);
304 }
305
306out:
307 spin_unlock_irqrestore(&host->lock, flags);
308 return;
309}
310
Sahitya Tummala62612cf2010-12-08 15:03:03 +0530311static void
312msmsdcc_dma_complete_func(struct msm_dmov_cmd *cmd,
313 unsigned int result,
314 struct msm_dmov_errdata *err)
315{
316 struct msmsdcc_dma_data *dma_data =
317 container_of(cmd, struct msmsdcc_dma_data, hdr);
318 struct msmsdcc_host *host = dma_data->host;
319
320 dma_data->result = result;
321 if (err)
322 memcpy(&dma_data->err, err, sizeof(struct msm_dmov_errdata));
323
324 tasklet_schedule(&host->dma_tlet);
325}
326
San Mehat9d2bd732009-09-22 16:44:22 -0700327static int validate_dma(struct msmsdcc_host *host, struct mmc_data *data)
328{
329 if (host->dma.channel == -1)
330 return -ENOENT;
331
332 if ((data->blksz * data->blocks) < MCI_FIFOSIZE)
333 return -EINVAL;
334 if ((data->blksz * data->blocks) % MCI_FIFOSIZE)
335 return -EINVAL;
336 return 0;
337}
338
339static int msmsdcc_config_dma(struct msmsdcc_host *host, struct mmc_data *data)
340{
341 struct msmsdcc_nc_dmadata *nc;
342 dmov_box *box;
343 uint32_t rows;
344 uint32_t crci;
345 unsigned int n;
346 int i, rc;
347 struct scatterlist *sg = data->sg;
348
349 rc = validate_dma(host, data);
350 if (rc)
351 return rc;
352
353 host->dma.sg = data->sg;
354 host->dma.num_ents = data->sg_len;
355
San Mehat56a8b5b2009-11-21 12:29:46 -0800356 BUG_ON(host->dma.num_ents > NR_SG); /* Prevent memory corruption */
357
San Mehat9d2bd732009-09-22 16:44:22 -0700358 nc = host->dma.nc;
359
Joe Perches75d14522009-09-22 16:44:24 -0700360 switch (host->pdev_id) {
361 case 1:
San Mehat9d2bd732009-09-22 16:44:22 -0700362 crci = MSMSDCC_CRCI_SDC1;
Joe Perches75d14522009-09-22 16:44:24 -0700363 break;
364 case 2:
San Mehat9d2bd732009-09-22 16:44:22 -0700365 crci = MSMSDCC_CRCI_SDC2;
Joe Perches75d14522009-09-22 16:44:24 -0700366 break;
367 case 3:
San Mehat9d2bd732009-09-22 16:44:22 -0700368 crci = MSMSDCC_CRCI_SDC3;
Joe Perches75d14522009-09-22 16:44:24 -0700369 break;
370 case 4:
San Mehat9d2bd732009-09-22 16:44:22 -0700371 crci = MSMSDCC_CRCI_SDC4;
Joe Perches75d14522009-09-22 16:44:24 -0700372 break;
373 default:
San Mehat9d2bd732009-09-22 16:44:22 -0700374 host->dma.sg = NULL;
375 host->dma.num_ents = 0;
376 return -ENOENT;
377 }
378
379 if (data->flags & MMC_DATA_READ)
380 host->dma.dir = DMA_FROM_DEVICE;
381 else
382 host->dma.dir = DMA_TO_DEVICE;
383
384 host->curr.user_pages = 0;
385
San Mehat9d2bd732009-09-22 16:44:22 -0700386 box = &nc->cmd[0];
Daniel Walker208028d2011-01-18 15:03:25 -0800387
388 /* location of command block must be 64 bit aligned */
389 BUG_ON(host->dma.cmd_busaddr & 0x07);
390
391 nc->cmdptr = (host->dma.cmd_busaddr >> 3) | CMD_PTR_LP;
392 host->dma.hdr.cmdptr = DMOV_CMD_PTR_LIST |
393 DMOV_CMD_ADDR(host->dma.cmdptr_busaddr);
394 host->dma.hdr.complete_func = msmsdcc_dma_complete_func;
395
396 n = dma_map_sg(mmc_dev(host->mmc), host->dma.sg,
397 host->dma.num_ents, host->dma.dir);
398 if (n == 0) {
399 printk(KERN_ERR "%s: Unable to map in all sg elements\n",
400 mmc_hostname(host->mmc));
401 host->dma.sg = NULL;
402 host->dma.num_ents = 0;
403 return -ENOMEM;
404 }
405
406 for_each_sg(host->dma.sg, sg, n, i) {
407
San Mehat9d2bd732009-09-22 16:44:22 -0700408 box->cmd = CMD_MODE_BOX;
409
Daniel Walker208028d2011-01-18 15:03:25 -0800410 if (i == n - 1)
San Mehat9d2bd732009-09-22 16:44:22 -0700411 box->cmd |= CMD_LC;
412 rows = (sg_dma_len(sg) % MCI_FIFOSIZE) ?
413 (sg_dma_len(sg) / MCI_FIFOSIZE) + 1 :
414 (sg_dma_len(sg) / MCI_FIFOSIZE) ;
415
416 if (data->flags & MMC_DATA_READ) {
417 box->src_row_addr = msmsdcc_fifo_addr(host);
418 box->dst_row_addr = sg_dma_address(sg);
419
420 box->src_dst_len = (MCI_FIFOSIZE << 16) |
421 (MCI_FIFOSIZE);
422 box->row_offset = MCI_FIFOSIZE;
423
424 box->num_rows = rows * ((1 << 16) + 1);
425 box->cmd |= CMD_SRC_CRCI(crci);
426 } else {
427 box->src_row_addr = sg_dma_address(sg);
428 box->dst_row_addr = msmsdcc_fifo_addr(host);
429
430 box->src_dst_len = (MCI_FIFOSIZE << 16) |
431 (MCI_FIFOSIZE);
432 box->row_offset = (MCI_FIFOSIZE << 16);
433
434 box->num_rows = rows * ((1 << 16) + 1);
435 box->cmd |= CMD_DST_CRCI(crci);
436 }
437 box++;
San Mehat56a8b5b2009-11-21 12:29:46 -0800438 }
San Mehat9d2bd732009-09-22 16:44:22 -0700439
440 return 0;
441}
442
San Mehat56a8b5b2009-11-21 12:29:46 -0800443static int
444snoop_cccr_abort(struct mmc_command *cmd)
445{
446 if ((cmd->opcode == 52) &&
447 (cmd->arg & 0x80000000) &&
448 (((cmd->arg >> 9) & 0x1ffff) == SDIO_CCCR_ABORT))
449 return 1;
San Mehat9d2bd732009-09-22 16:44:22 -0700450 return 0;
451}
452
453static void
San Mehat56a8b5b2009-11-21 12:29:46 -0800454msmsdcc_start_command_deferred(struct msmsdcc_host *host,
455 struct mmc_command *cmd, u32 *c)
456{
457 *c |= (cmd->opcode | MCI_CPSM_ENABLE);
458
459 if (cmd->flags & MMC_RSP_PRESENT) {
460 if (cmd->flags & MMC_RSP_136)
461 *c |= MCI_CPSM_LONGRSP;
462 *c |= MCI_CPSM_RESPONSE;
463 }
464
465 if (/*interrupt*/0)
466 *c |= MCI_CPSM_INTERRUPT;
467
468 if ((((cmd->opcode == 17) || (cmd->opcode == 18)) ||
469 ((cmd->opcode == 24) || (cmd->opcode == 25))) ||
470 (cmd->opcode == 53))
471 *c |= MCI_CSPM_DATCMD;
472
Sahitya Tummalad5137bd2010-12-08 15:03:04 +0530473 if (host->prog_scan && (cmd->opcode == 12)) {
474 *c |= MCI_CPSM_PROGENA;
475 host->prog_enable = true;
476 }
477
San Mehat56a8b5b2009-11-21 12:29:46 -0800478 if (cmd == cmd->mrq->stop)
479 *c |= MCI_CSPM_MCIABORT;
480
481 if (snoop_cccr_abort(cmd))
482 *c |= MCI_CSPM_MCIABORT;
483
484 if (host->curr.cmd != NULL) {
485 printk(KERN_ERR "%s: Overlapping command requests\n",
486 mmc_hostname(host->mmc));
487 }
488 host->curr.cmd = cmd;
489}
490
491static void
492msmsdcc_start_data(struct msmsdcc_host *host, struct mmc_data *data,
493 struct mmc_command *cmd, u32 c)
San Mehat9d2bd732009-09-22 16:44:22 -0700494{
495 unsigned int datactrl, timeout;
496 unsigned long long clks;
San Mehat9d2bd732009-09-22 16:44:22 -0700497 unsigned int pio_irqmask = 0;
498
499 host->curr.data = data;
500 host->curr.xfer_size = data->blksz * data->blocks;
501 host->curr.xfer_remain = host->curr.xfer_size;
502 host->curr.data_xfered = 0;
503 host->curr.got_dataend = 0;
San Mehat9d2bd732009-09-22 16:44:22 -0700504
505 memset(&host->pio, 0, sizeof(host->pio));
506
San Mehat9d2bd732009-09-22 16:44:22 -0700507 datactrl = MCI_DPSM_ENABLE | (data->blksz << 4);
508
509 if (!msmsdcc_config_dma(host, data))
510 datactrl |= MCI_DPSM_DMAENABLE;
511 else {
512 host->pio.sg = data->sg;
513 host->pio.sg_len = data->sg_len;
514 host->pio.sg_off = 0;
515
516 if (data->flags & MMC_DATA_READ) {
517 pio_irqmask = MCI_RXFIFOHALFFULLMASK;
518 if (host->curr.xfer_remain < MCI_FIFOSIZE)
519 pio_irqmask |= MCI_RXDATAAVLBLMASK;
520 } else
521 pio_irqmask = MCI_TXFIFOHALFEMPTYMASK;
522 }
523
524 if (data->flags & MMC_DATA_READ)
525 datactrl |= MCI_DPSM_DIRECTION;
526
San Mehat56a8b5b2009-11-21 12:29:46 -0800527 clks = (unsigned long long)data->timeout_ns * host->clk_rate;
528 do_div(clks, NSEC_PER_SEC);
529 timeout = data->timeout_clks + (unsigned int)clks*2 ;
San Mehat9d2bd732009-09-22 16:44:22 -0700530
531 if (datactrl & MCI_DPSM_DMAENABLE) {
San Mehat56a8b5b2009-11-21 12:29:46 -0800532 /* Save parameters for the exec function */
533 host->cmd_timeout = timeout;
534 host->cmd_pio_irqmask = pio_irqmask;
535 host->cmd_datactrl = datactrl;
536 host->cmd_cmd = cmd;
San Mehat9d2bd732009-09-22 16:44:22 -0700537
San Mehat56a8b5b2009-11-21 12:29:46 -0800538 host->dma.hdr.execute_func = msmsdcc_dma_exec_func;
539 host->dma.hdr.data = (void *)host;
San Mehat9d2bd732009-09-22 16:44:22 -0700540 host->dma.busy = 1;
San Mehat56a8b5b2009-11-21 12:29:46 -0800541
542 if (cmd) {
543 msmsdcc_start_command_deferred(host, cmd, &c);
544 host->cmd_c = c;
545 }
San Mehat9d2bd732009-09-22 16:44:22 -0700546 msm_dmov_enqueue_cmd(host->dma.channel, &host->dma.hdr);
Sahitya Tummalad5137bd2010-12-08 15:03:04 +0530547 if (data->flags & MMC_DATA_WRITE)
548 host->prog_scan = true;
San Mehat56a8b5b2009-11-21 12:29:46 -0800549 } else {
550 msmsdcc_writel(host, timeout, MMCIDATATIMER);
551
552 msmsdcc_writel(host, host->curr.xfer_size, MMCIDATALENGTH);
553
554 msmsdcc_writel(host, pio_irqmask, MMCIMASK1);
555 msmsdcc_writel(host, datactrl, MMCIDATACTRL);
556
557 if (cmd) {
558 /* Daisy-chain the command if requested */
559 msmsdcc_start_command(host, cmd, c);
560 }
San Mehat9d2bd732009-09-22 16:44:22 -0700561 }
562}
563
564static void
565msmsdcc_start_command(struct msmsdcc_host *host, struct mmc_command *cmd, u32 c)
566{
San Mehat9d2bd732009-09-22 16:44:22 -0700567 if (cmd == cmd->mrq->stop)
568 c |= MCI_CSPM_MCIABORT;
569
San Mehat9d2bd732009-09-22 16:44:22 -0700570 host->stats.cmds++;
571
San Mehat56a8b5b2009-11-21 12:29:46 -0800572 msmsdcc_start_command_deferred(host, cmd, &c);
573 msmsdcc_start_command_exec(host, cmd->arg, c);
San Mehat9d2bd732009-09-22 16:44:22 -0700574}
575
576static void
577msmsdcc_data_err(struct msmsdcc_host *host, struct mmc_data *data,
578 unsigned int status)
579{
580 if (status & MCI_DATACRCFAIL) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700581 pr_err("%s: Data CRC error\n", mmc_hostname(host->mmc));
582 pr_err("%s: opcode 0x%.8x\n", __func__,
San Mehat9d2bd732009-09-22 16:44:22 -0700583 data->mrq->cmd->opcode);
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700584 pr_err("%s: blksz %d, blocks %d\n", __func__,
San Mehat9d2bd732009-09-22 16:44:22 -0700585 data->blksz, data->blocks);
586 data->error = -EILSEQ;
587 } else if (status & MCI_DATATIMEOUT) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700588 pr_err("%s: Data timeout\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700589 data->error = -ETIMEDOUT;
590 } else if (status & MCI_RXOVERRUN) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700591 pr_err("%s: RX overrun\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700592 data->error = -EIO;
593 } else if (status & MCI_TXUNDERRUN) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700594 pr_err("%s: TX underrun\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700595 data->error = -EIO;
596 } else {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700597 pr_err("%s: Unknown error (0x%.8x)\n",
598 mmc_hostname(host->mmc), status);
San Mehat9d2bd732009-09-22 16:44:22 -0700599 data->error = -EIO;
600 }
601}
602
603
604static int
605msmsdcc_pio_read(struct msmsdcc_host *host, char *buffer, unsigned int remain)
606{
San Mehat9d2bd732009-09-22 16:44:22 -0700607 uint32_t *ptr = (uint32_t *) buffer;
608 int count = 0;
609
Sahitya Tummala71dd9102010-12-08 15:03:06 +0530610 if (remain % 4)
611 remain = ((remain >> 2) + 1) << 2;
612
San Mehat8b1c2ba2009-11-16 10:17:30 -0800613 while (msmsdcc_readl(host, MMCISTATUS) & MCI_RXDATAAVLBL) {
614 *ptr = msmsdcc_readl(host, MMCIFIFO + (count % MCI_FIFOSIZE));
San Mehat9d2bd732009-09-22 16:44:22 -0700615 ptr++;
616 count += sizeof(uint32_t);
617
618 remain -= sizeof(uint32_t);
619 if (remain == 0)
620 break;
621 }
622 return count;
623}
624
625static int
626msmsdcc_pio_write(struct msmsdcc_host *host, char *buffer,
627 unsigned int remain, u32 status)
628{
629 void __iomem *base = host->base;
630 char *ptr = buffer;
631
632 do {
Sahitya Tummala71dd9102010-12-08 15:03:06 +0530633 unsigned int count, maxcnt, sz;
San Mehat9d2bd732009-09-22 16:44:22 -0700634
635 maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE :
636 MCI_FIFOHALFSIZE;
637 count = min(remain, maxcnt);
638
Sahitya Tummala71dd9102010-12-08 15:03:06 +0530639 sz = count % 4 ? (count >> 2) + 1 : (count >> 2);
640 writesl(base + MMCIFIFO, ptr, sz);
San Mehat9d2bd732009-09-22 16:44:22 -0700641 ptr += count;
642 remain -= count;
643
644 if (remain == 0)
645 break;
646
San Mehat8b1c2ba2009-11-16 10:17:30 -0800647 status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700648 } while (status & MCI_TXFIFOHALFEMPTY);
649
650 return ptr - buffer;
651}
652
653static int
654msmsdcc_spin_on_status(struct msmsdcc_host *host, uint32_t mask, int maxspin)
655{
656 while (maxspin) {
San Mehat8b1c2ba2009-11-16 10:17:30 -0800657 if ((msmsdcc_readl(host, MMCISTATUS) & mask))
San Mehat9d2bd732009-09-22 16:44:22 -0700658 return 0;
659 udelay(1);
660 --maxspin;
661 }
662 return -ETIMEDOUT;
663}
664
San Mehat1cd22962010-02-03 12:59:29 -0800665static irqreturn_t
San Mehat9d2bd732009-09-22 16:44:22 -0700666msmsdcc_pio_irq(int irq, void *dev_id)
667{
668 struct msmsdcc_host *host = dev_id;
San Mehat9d2bd732009-09-22 16:44:22 -0700669 uint32_t status;
670
San Mehat8b1c2ba2009-11-16 10:17:30 -0800671 status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700672
673 do {
674 unsigned long flags;
675 unsigned int remain, len;
676 char *buffer;
677
678 if (!(status & (MCI_TXFIFOHALFEMPTY | MCI_RXDATAAVLBL))) {
679 if (host->curr.xfer_remain == 0 || !msmsdcc_piopoll)
680 break;
681
682 if (msmsdcc_spin_on_status(host,
683 (MCI_TXFIFOHALFEMPTY |
684 MCI_RXDATAAVLBL),
685 PIO_SPINMAX)) {
686 break;
687 }
688 }
689
690 /* Map the current scatter buffer */
691 local_irq_save(flags);
692 buffer = kmap_atomic(sg_page(host->pio.sg),
693 KM_BIO_SRC_IRQ) + host->pio.sg->offset;
694 buffer += host->pio.sg_off;
695 remain = host->pio.sg->length - host->pio.sg_off;
696 len = 0;
697 if (status & MCI_RXACTIVE)
698 len = msmsdcc_pio_read(host, buffer, remain);
699 if (status & MCI_TXACTIVE)
700 len = msmsdcc_pio_write(host, buffer, remain, status);
701
702 /* Unmap the buffer */
703 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
704 local_irq_restore(flags);
705
706 host->pio.sg_off += len;
707 host->curr.xfer_remain -= len;
708 host->curr.data_xfered += len;
709 remain -= len;
710
711 if (remain == 0) {
712 /* This sg page is full - do some housekeeping */
713 if (status & MCI_RXACTIVE && host->curr.user_pages)
714 flush_dcache_page(sg_page(host->pio.sg));
715
716 if (!--host->pio.sg_len) {
717 memset(&host->pio, 0, sizeof(host->pio));
718 break;
719 }
720
721 /* Advance to next sg */
722 host->pio.sg++;
723 host->pio.sg_off = 0;
724 }
725
San Mehat8b1c2ba2009-11-16 10:17:30 -0800726 status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700727 } while (1);
728
729 if (status & MCI_RXACTIVE && host->curr.xfer_remain < MCI_FIFOSIZE)
San Mehat8b1c2ba2009-11-16 10:17:30 -0800730 msmsdcc_writel(host, MCI_RXDATAAVLBLMASK, MMCIMASK1);
San Mehat9d2bd732009-09-22 16:44:22 -0700731
732 if (!host->curr.xfer_remain)
San Mehat8b1c2ba2009-11-16 10:17:30 -0800733 msmsdcc_writel(host, 0, MMCIMASK1);
San Mehat9d2bd732009-09-22 16:44:22 -0700734
735 return IRQ_HANDLED;
736}
737
738static void msmsdcc_do_cmdirq(struct msmsdcc_host *host, uint32_t status)
739{
740 struct mmc_command *cmd = host->curr.cmd;
San Mehat9d2bd732009-09-22 16:44:22 -0700741
742 host->curr.cmd = NULL;
San Mehat8b1c2ba2009-11-16 10:17:30 -0800743 cmd->resp[0] = msmsdcc_readl(host, MMCIRESPONSE0);
744 cmd->resp[1] = msmsdcc_readl(host, MMCIRESPONSE1);
745 cmd->resp[2] = msmsdcc_readl(host, MMCIRESPONSE2);
746 cmd->resp[3] = msmsdcc_readl(host, MMCIRESPONSE3);
San Mehat9d2bd732009-09-22 16:44:22 -0700747
San Mehat9d2bd732009-09-22 16:44:22 -0700748 if (status & MCI_CMDTIMEOUT) {
749 cmd->error = -ETIMEDOUT;
750 } else if (status & MCI_CMDCRCFAIL &&
751 cmd->flags & MMC_RSP_CRC) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700752 pr_err("%s: Command CRC error\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700753 cmd->error = -EILSEQ;
754 }
755
756 if (!cmd->data || cmd->error) {
757 if (host->curr.data && host->dma.sg)
758 msm_dmov_stop_cmd(host->dma.channel,
759 &host->dma.hdr, 0);
760 else if (host->curr.data) { /* Non DMA */
Sahitya Tummalab08bb352010-12-08 15:03:05 +0530761 msmsdcc_reset_and_restore(host);
San Mehat9d2bd732009-09-22 16:44:22 -0700762 msmsdcc_stop_data(host);
763 msmsdcc_request_end(host, cmd->mrq);
Sahitya Tummalad5137bd2010-12-08 15:03:04 +0530764 } else { /* host->data == NULL */
765 if (!cmd->error && host->prog_enable) {
766 if (status & MCI_PROGDONE) {
767 host->prog_scan = false;
768 host->prog_enable = false;
769 msmsdcc_request_end(host, cmd->mrq);
770 } else {
771 host->curr.cmd = cmd;
772 }
773 } else {
774 if (host->prog_enable) {
775 host->prog_scan = false;
776 host->prog_enable = false;
777 }
778 msmsdcc_request_end(host, cmd->mrq);
779 }
780 }
San Mehat56a8b5b2009-11-21 12:29:46 -0800781 } else if (cmd->data)
782 if (!(cmd->data->flags & MMC_DATA_READ))
783 msmsdcc_start_data(host, cmd->data,
784 NULL, 0);
San Mehat9d2bd732009-09-22 16:44:22 -0700785}
786
Joe Perchesb5a74d62009-09-22 16:44:25 -0700787static void
788msmsdcc_handle_irq_data(struct msmsdcc_host *host, u32 status,
789 void __iomem *base)
790{
791 struct mmc_data *data = host->curr.data;
792
San Mehat56a8b5b2009-11-21 12:29:46 -0800793 if (status & (MCI_CMDSENT | MCI_CMDRESPEND | MCI_CMDCRCFAIL |
Sahitya Tummalad5137bd2010-12-08 15:03:04 +0530794 MCI_CMDTIMEOUT | MCI_PROGDONE) && host->curr.cmd) {
San Mehat56a8b5b2009-11-21 12:29:46 -0800795 msmsdcc_do_cmdirq(host, status);
796 }
797
Joe Perchesb5a74d62009-09-22 16:44:25 -0700798 if (!data)
799 return;
800
801 /* Check for data errors */
802 if (status & (MCI_DATACRCFAIL | MCI_DATATIMEOUT |
803 MCI_TXUNDERRUN | MCI_RXOVERRUN)) {
804 msmsdcc_data_err(host, data, status);
805 host->curr.data_xfered = 0;
806 if (host->dma.sg)
807 msm_dmov_stop_cmd(host->dma.channel,
808 &host->dma.hdr, 0);
809 else {
Sahitya Tummalab08bb352010-12-08 15:03:05 +0530810 msmsdcc_reset_and_restore(host);
San Mehatb3b0ca82009-11-24 12:24:55 -0800811 if (host->curr.data)
812 msmsdcc_stop_data(host);
Joe Perchesb5a74d62009-09-22 16:44:25 -0700813 if (!data->stop)
814 msmsdcc_request_end(host, data->mrq);
815 else
816 msmsdcc_start_command(host, data->stop, 0);
817 }
818 }
819
820 /* Check for data done */
821 if (!host->curr.got_dataend && (status & MCI_DATAEND))
822 host->curr.got_dataend = 1;
823
Joe Perchesb5a74d62009-09-22 16:44:25 -0700824 /*
825 * If DMA is still in progress, we complete via the completion handler
826 */
Sahitya Tummala0c521cc2010-12-08 15:03:07 +0530827 if (host->curr.got_dataend && !host->dma.busy) {
Joe Perchesb5a74d62009-09-22 16:44:25 -0700828 /*
829 * There appears to be an issue in the controller where
830 * if you request a small block transfer (< fifo size),
831 * you may get your DATAEND/DATABLKEND irq without the
832 * PIO data irq.
833 *
834 * Check to see if there is still data to be read,
835 * and simulate a PIO irq.
836 */
837 if (readl(base + MMCISTATUS) & MCI_RXDATAAVLBL)
838 msmsdcc_pio_irq(1, host);
839
840 msmsdcc_stop_data(host);
841 if (!data->error)
842 host->curr.data_xfered = host->curr.xfer_size;
843
844 if (!data->stop)
845 msmsdcc_request_end(host, data->mrq);
846 else
847 msmsdcc_start_command(host, data->stop, 0);
848 }
849}
850
San Mehat9d2bd732009-09-22 16:44:22 -0700851static irqreturn_t
852msmsdcc_irq(int irq, void *dev_id)
853{
854 struct msmsdcc_host *host = dev_id;
855 void __iomem *base = host->base;
856 u32 status;
857 int ret = 0;
858 int cardint = 0;
859
860 spin_lock(&host->lock);
861
862 do {
San Mehat8b1c2ba2009-11-16 10:17:30 -0800863 status = msmsdcc_readl(host, MMCISTATUS);
Sahitya Tummala0c521cc2010-12-08 15:03:07 +0530864 status &= msmsdcc_readl(host, MMCIMASK0);
San Mehat8b1c2ba2009-11-16 10:17:30 -0800865 msmsdcc_writel(host, status, MMCICLEAR);
San Mehat9d2bd732009-09-22 16:44:22 -0700866
San Mehat865c8062009-11-13 13:42:06 -0800867 if (status & MCI_SDIOINTR)
868 status &= ~MCI_SDIOINTR;
869
870 if (!status)
871 break;
San Mehat9d2bd732009-09-22 16:44:22 -0700872
Joe Perchesb5a74d62009-09-22 16:44:25 -0700873 msmsdcc_handle_irq_data(host, status, base);
San Mehat9d2bd732009-09-22 16:44:22 -0700874
San Mehat9d2bd732009-09-22 16:44:22 -0700875 if (status & MCI_SDIOINTOPER) {
876 cardint = 1;
877 status &= ~MCI_SDIOINTOPER;
878 }
879 ret = 1;
880 } while (status);
881
882 spin_unlock(&host->lock);
883
884 /*
885 * We have to delay handling the card interrupt as it calls
886 * back into the driver.
887 */
888 if (cardint)
889 mmc_signal_sdio_irq(host->mmc);
890
891 return IRQ_RETVAL(ret);
892}
893
894static void
895msmsdcc_request(struct mmc_host *mmc, struct mmc_request *mrq)
896{
897 struct msmsdcc_host *host = mmc_priv(mmc);
898 unsigned long flags;
899
900 WARN_ON(host->curr.mrq != NULL);
901 WARN_ON(host->pwr == 0);
902
903 spin_lock_irqsave(&host->lock, flags);
904
905 host->stats.reqs++;
906
907 if (host->eject) {
908 if (mrq->data && !(mrq->data->flags & MMC_DATA_READ)) {
909 mrq->cmd->error = 0;
910 mrq->data->bytes_xfered = mrq->data->blksz *
911 mrq->data->blocks;
912 } else
913 mrq->cmd->error = -ENOMEDIUM;
914
915 spin_unlock_irqrestore(&host->lock, flags);
916 mmc_request_done(mmc, mrq);
917 return;
918 }
919
San Mehatd0719e52009-12-03 10:58:54 -0800920 msmsdcc_enable_clocks(host);
San Mehat9d2bd732009-09-22 16:44:22 -0700921
San Mehat9d2bd732009-09-22 16:44:22 -0700922 host->curr.mrq = mrq;
923
924 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
San Mehat56a8b5b2009-11-21 12:29:46 -0800925 /* Queue/read data, daisy-chain command when data starts */
926 msmsdcc_start_data(host, mrq->data, mrq->cmd, 0);
927 else
928 msmsdcc_start_command(host, mrq->cmd, 0);
San Mehat9d2bd732009-09-22 16:44:22 -0700929
930 if (host->cmdpoll && !msmsdcc_spin_on_status(host,
931 MCI_CMDRESPEND|MCI_CMDCRCFAIL|MCI_CMDTIMEOUT,
932 CMD_SPINMAX)) {
San Mehat8b1c2ba2009-11-16 10:17:30 -0800933 uint32_t status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700934 msmsdcc_do_cmdirq(host, status);
San Mehat8b1c2ba2009-11-16 10:17:30 -0800935 msmsdcc_writel(host,
936 MCI_CMDRESPEND | MCI_CMDCRCFAIL | MCI_CMDTIMEOUT,
937 MMCICLEAR);
San Mehat9d2bd732009-09-22 16:44:22 -0700938 host->stats.cmdpoll_hits++;
939 } else {
940 host->stats.cmdpoll_misses++;
San Mehat9d2bd732009-09-22 16:44:22 -0700941 }
942 spin_unlock_irqrestore(&host->lock, flags);
943}
944
Sahitya Tummala7a892482011-01-18 11:22:49 +0530945static void msmsdcc_setup_gpio(struct msmsdcc_host *host, bool enable)
946{
947 struct msm_mmc_gpio_data *curr;
948 int i, rc = 0;
949
950 if (!host->plat->gpio_data && host->gpio_config_status == enable)
951 return;
952
953 curr = host->plat->gpio_data;
954 for (i = 0; i < curr->size; i++) {
955 if (enable) {
956 rc = gpio_request(curr->gpio[i].no,
957 curr->gpio[i].name);
958 if (rc) {
959 pr_err("%s: gpio_request(%d, %s) failed %d\n",
960 mmc_hostname(host->mmc),
961 curr->gpio[i].no,
962 curr->gpio[i].name, rc);
963 goto free_gpios;
964 }
965 } else {
966 gpio_free(curr->gpio[i].no);
967 }
968 }
969 host->gpio_config_status = enable;
970 return;
971
972free_gpios:
973 for (; i >= 0; i--)
974 gpio_free(curr->gpio[i].no);
975}
976
San Mehat9d2bd732009-09-22 16:44:22 -0700977static void
978msmsdcc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
979{
980 struct msmsdcc_host *host = mmc_priv(mmc);
981 u32 clk = 0, pwr = 0;
982 int rc;
San Mehat4adbbcc2009-11-08 13:00:37 -0800983 unsigned long flags;
San Mehat9d2bd732009-09-22 16:44:22 -0700984
San Mehatc7fc9372009-11-22 17:19:07 -0800985 spin_lock_irqsave(&host->lock, flags);
San Mehat9d2bd732009-09-22 16:44:22 -0700986
San Mehatd0719e52009-12-03 10:58:54 -0800987 msmsdcc_enable_clocks(host);
San Mehat9d2bd732009-09-22 16:44:22 -0700988
Sahitya Tummala7a892482011-01-18 11:22:49 +0530989 spin_unlock_irqrestore(&host->lock, flags);
990
San Mehat9d2bd732009-09-22 16:44:22 -0700991 if (ios->clock) {
San Mehat9d2bd732009-09-22 16:44:22 -0700992 if (ios->clock != host->clk_rate) {
993 rc = clk_set_rate(host->clk, ios->clock);
994 if (rc < 0)
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700995 pr_err("%s: Error setting clock rate (%d)\n",
996 mmc_hostname(host->mmc), rc);
San Mehat9d2bd732009-09-22 16:44:22 -0700997 else
998 host->clk_rate = ios->clock;
999 }
1000 clk |= MCI_CLK_ENABLE;
1001 }
1002
1003 if (ios->bus_width == MMC_BUS_WIDTH_4)
1004 clk |= (2 << 10); /* Set WIDEBUS */
1005
1006 if (ios->clock > 400000 && msmsdcc_pwrsave)
1007 clk |= (1 << 9); /* PWRSAVE */
1008
1009 clk |= (1 << 12); /* FLOW_ENA */
1010 clk |= (1 << 15); /* feedback clock */
1011
1012 if (host->plat->translate_vdd)
1013 pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
1014
1015 switch (ios->power_mode) {
1016 case MMC_POWER_OFF:
Sahitya Tummala7a892482011-01-18 11:22:49 +05301017 msmsdcc_setup_gpio(host, false);
San Mehat9d2bd732009-09-22 16:44:22 -07001018 break;
1019 case MMC_POWER_UP:
1020 pwr |= MCI_PWR_UP;
Sahitya Tummala7a892482011-01-18 11:22:49 +05301021 msmsdcc_setup_gpio(host, true);
San Mehat9d2bd732009-09-22 16:44:22 -07001022 break;
1023 case MMC_POWER_ON:
San Mehat9d2bd732009-09-22 16:44:22 -07001024 pwr |= MCI_PWR_ON;
1025 break;
1026 }
1027
1028 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1029 pwr |= MCI_OD;
1030
San Mehat8b1c2ba2009-11-16 10:17:30 -08001031 msmsdcc_writel(host, clk, MMCICLOCK);
San Mehat9d2bd732009-09-22 16:44:22 -07001032
1033 if (host->pwr != pwr) {
1034 host->pwr = pwr;
San Mehat8b1c2ba2009-11-16 10:17:30 -08001035 msmsdcc_writel(host, pwr, MMCIPOWER);
San Mehat9d2bd732009-09-22 16:44:22 -07001036 }
San Mehatf4748492009-11-23 15:36:31 -08001037#if BUSCLK_PWRSAVE
Sahitya Tummala7a892482011-01-18 11:22:49 +05301038 spin_lock_irqsave(&host->lock, flags);
San Mehatc7fc9372009-11-22 17:19:07 -08001039 msmsdcc_disable_clocks(host, 1);
San Mehat4adbbcc2009-11-08 13:00:37 -08001040 spin_unlock_irqrestore(&host->lock, flags);
Sahitya Tummala7a892482011-01-18 11:22:49 +05301041#endif
San Mehat9d2bd732009-09-22 16:44:22 -07001042}
1043
1044static void msmsdcc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1045{
1046 struct msmsdcc_host *host = mmc_priv(mmc);
1047 unsigned long flags;
1048 u32 status;
1049
1050 spin_lock_irqsave(&host->lock, flags);
1051 if (msmsdcc_sdioirq == 1) {
San Mehat8b1c2ba2009-11-16 10:17:30 -08001052 status = msmsdcc_readl(host, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -07001053 if (enable)
1054 status |= MCI_SDIOINTOPERMASK;
1055 else
1056 status &= ~MCI_SDIOINTOPERMASK;
1057 host->saved_irq0mask = status;
San Mehat8b1c2ba2009-11-16 10:17:30 -08001058 msmsdcc_writel(host, status, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -07001059 }
1060 spin_unlock_irqrestore(&host->lock, flags);
1061}
1062
1063static const struct mmc_host_ops msmsdcc_ops = {
1064 .request = msmsdcc_request,
1065 .set_ios = msmsdcc_set_ios,
1066 .enable_sdio_irq = msmsdcc_enable_sdio_irq,
1067};
1068
1069static void
1070msmsdcc_check_status(unsigned long data)
1071{
1072 struct msmsdcc_host *host = (struct msmsdcc_host *)data;
1073 unsigned int status;
1074
1075 if (!host->plat->status) {
1076 mmc_detect_change(host->mmc, 0);
1077 goto out;
1078 }
1079
1080 status = host->plat->status(mmc_dev(host->mmc));
1081 host->eject = !status;
1082 if (status ^ host->oldstat) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001083 pr_info("%s: Slot status change detected (%d -> %d)\n",
1084 mmc_hostname(host->mmc), host->oldstat, status);
San Mehat9d2bd732009-09-22 16:44:22 -07001085 if (status)
1086 mmc_detect_change(host->mmc, (5 * HZ) / 2);
1087 else
1088 mmc_detect_change(host->mmc, 0);
1089 }
1090
1091 host->oldstat = status;
1092
1093out:
1094 if (host->timer.function)
1095 mod_timer(&host->timer, jiffies + HZ);
1096}
1097
1098static irqreturn_t
1099msmsdcc_platform_status_irq(int irq, void *dev_id)
1100{
1101 struct msmsdcc_host *host = dev_id;
1102
1103 printk(KERN_DEBUG "%s: %d\n", __func__, irq);
1104 msmsdcc_check_status((unsigned long) host);
1105 return IRQ_HANDLED;
1106}
1107
1108static void
1109msmsdcc_status_notify_cb(int card_present, void *dev_id)
1110{
1111 struct msmsdcc_host *host = dev_id;
1112
1113 printk(KERN_DEBUG "%s: card_present %d\n", mmc_hostname(host->mmc),
1114 card_present);
1115 msmsdcc_check_status((unsigned long) host);
1116}
1117
San Mehat9d2bd732009-09-22 16:44:22 -07001118static void
San Mehat865c8062009-11-13 13:42:06 -08001119msmsdcc_busclk_expired(unsigned long _data)
San Mehat9d2bd732009-09-22 16:44:22 -07001120{
1121 struct msmsdcc_host *host = (struct msmsdcc_host *) _data;
San Mehat9d2bd732009-09-22 16:44:22 -07001122
San Mehat865c8062009-11-13 13:42:06 -08001123 if (host->clks_on)
San Mehatc7fc9372009-11-22 17:19:07 -08001124 msmsdcc_disable_clocks(host, 0);
San Mehat9d2bd732009-09-22 16:44:22 -07001125}
1126
1127static int
1128msmsdcc_init_dma(struct msmsdcc_host *host)
1129{
1130 memset(&host->dma, 0, sizeof(struct msmsdcc_dma_data));
1131 host->dma.host = host;
1132 host->dma.channel = -1;
1133
1134 if (!host->dmares)
1135 return -ENODEV;
1136
1137 host->dma.nc = dma_alloc_coherent(NULL,
1138 sizeof(struct msmsdcc_nc_dmadata),
1139 &host->dma.nc_busaddr,
1140 GFP_KERNEL);
1141 if (host->dma.nc == NULL) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001142 pr_err("Unable to allocate DMA buffer\n");
San Mehat9d2bd732009-09-22 16:44:22 -07001143 return -ENOMEM;
1144 }
1145 memset(host->dma.nc, 0x00, sizeof(struct msmsdcc_nc_dmadata));
1146 host->dma.cmd_busaddr = host->dma.nc_busaddr;
1147 host->dma.cmdptr_busaddr = host->dma.nc_busaddr +
1148 offsetof(struct msmsdcc_nc_dmadata, cmdptr);
1149 host->dma.channel = host->dmares->start;
1150
1151 return 0;
1152}
1153
San Mehat9d2bd732009-09-22 16:44:22 -07001154static int
1155msmsdcc_probe(struct platform_device *pdev)
1156{
Sahitya Tummalab5d643d2010-07-29 16:55:34 +05301157 struct msm_mmc_platform_data *plat = pdev->dev.platform_data;
San Mehat9d2bd732009-09-22 16:44:22 -07001158 struct msmsdcc_host *host;
1159 struct mmc_host *mmc;
1160 struct resource *cmd_irqres = NULL;
1161 struct resource *pio_irqres = NULL;
1162 struct resource *stat_irqres = NULL;
1163 struct resource *memres = NULL;
1164 struct resource *dmares = NULL;
1165 int ret;
1166
1167 /* must have platform data */
1168 if (!plat) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001169 pr_err("%s: Platform data not available\n", __func__);
San Mehat9d2bd732009-09-22 16:44:22 -07001170 ret = -EINVAL;
1171 goto out;
1172 }
1173
1174 if (pdev->id < 1 || pdev->id > 4)
1175 return -EINVAL;
1176
1177 if (pdev->resource == NULL || pdev->num_resources < 2) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001178 pr_err("%s: Invalid resource\n", __func__);
San Mehat9d2bd732009-09-22 16:44:22 -07001179 return -ENXIO;
1180 }
1181
1182 memres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1183 dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1184 cmd_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1185 "cmd_irq");
1186 pio_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1187 "pio_irq");
1188 stat_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1189 "status_irq");
1190
1191 if (!cmd_irqres || !pio_irqres || !memres) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001192 pr_err("%s: Invalid resource\n", __func__);
San Mehat9d2bd732009-09-22 16:44:22 -07001193 return -ENXIO;
1194 }
1195
1196 /*
1197 * Setup our host structure
1198 */
1199
1200 mmc = mmc_alloc_host(sizeof(struct msmsdcc_host), &pdev->dev);
1201 if (!mmc) {
1202 ret = -ENOMEM;
1203 goto out;
1204 }
1205
1206 host = mmc_priv(mmc);
1207 host->pdev_id = pdev->id;
1208 host->plat = plat;
1209 host->mmc = mmc;
San Mehat56a8b5b2009-11-21 12:29:46 -08001210 host->curr.cmd = NULL;
San Mehat9d2bd732009-09-22 16:44:22 -07001211
1212 host->cmdpoll = 1;
1213
1214 host->base = ioremap(memres->start, PAGE_SIZE);
1215 if (!host->base) {
1216 ret = -ENOMEM;
1217 goto out;
1218 }
1219
1220 host->cmd_irqres = cmd_irqres;
1221 host->pio_irqres = pio_irqres;
1222 host->memres = memres;
1223 host->dmares = dmares;
1224 spin_lock_init(&host->lock);
1225
Sahitya Tummala62612cf2010-12-08 15:03:03 +05301226 tasklet_init(&host->dma_tlet, msmsdcc_dma_complete_tlet,
1227 (unsigned long)host);
1228
San Mehat9d2bd732009-09-22 16:44:22 -07001229 /*
1230 * Setup DMA
1231 */
1232 msmsdcc_init_dma(host);
1233
San Mehat4adbbcc2009-11-08 13:00:37 -08001234 /* Get our clocks */
San Mehat9d2bd732009-09-22 16:44:22 -07001235 host->pclk = clk_get(&pdev->dev, "sdc_pclk");
1236 if (IS_ERR(host->pclk)) {
1237 ret = PTR_ERR(host->pclk);
1238 goto host_free;
1239 }
1240
San Mehat9d2bd732009-09-22 16:44:22 -07001241 host->clk = clk_get(&pdev->dev, "sdc_clk");
1242 if (IS_ERR(host->clk)) {
1243 ret = PTR_ERR(host->clk);
San Mehat4adbbcc2009-11-08 13:00:37 -08001244 goto pclk_put;
San Mehat9d2bd732009-09-22 16:44:22 -07001245 }
1246
San Mehat4adbbcc2009-11-08 13:00:37 -08001247 /* Enable clocks */
San Mehatc7fc9372009-11-22 17:19:07 -08001248 ret = msmsdcc_enable_clocks(host);
San Mehat9d2bd732009-09-22 16:44:22 -07001249 if (ret)
1250 goto clk_put;
1251
1252 ret = clk_set_rate(host->clk, msmsdcc_fmin);
1253 if (ret) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001254 pr_err("%s: Clock rate set failed (%d)\n", __func__, ret);
San Mehat9d2bd732009-09-22 16:44:22 -07001255 goto clk_disable;
1256 }
1257
San Mehat4adbbcc2009-11-08 13:00:37 -08001258 host->pclk_rate = clk_get_rate(host->pclk);
San Mehat9d2bd732009-09-22 16:44:22 -07001259 host->clk_rate = clk_get_rate(host->clk);
1260
San Mehat9d2bd732009-09-22 16:44:22 -07001261 /*
1262 * Setup MMC host structure
1263 */
1264 mmc->ops = &msmsdcc_ops;
1265 mmc->f_min = msmsdcc_fmin;
1266 mmc->f_max = msmsdcc_fmax;
1267 mmc->ocr_avail = plat->ocr_mask;
1268
1269 if (msmsdcc_4bit)
1270 mmc->caps |= MMC_CAP_4_BIT_DATA;
1271 if (msmsdcc_sdioirq)
1272 mmc->caps |= MMC_CAP_SDIO_IRQ;
1273 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
1274
Martin K. Petersena36274e2010-09-10 01:33:59 -04001275 mmc->max_segs = NR_SG;
San Mehat9d2bd732009-09-22 16:44:22 -07001276 mmc->max_blk_size = 4096; /* MCI_DATA_CTL BLOCKSIZE up to 4096 */
1277 mmc->max_blk_count = 65536;
1278
1279 mmc->max_req_size = 33554432; /* MCI_DATA_LENGTH is 25 bits */
1280 mmc->max_seg_size = mmc->max_req_size;
1281
San Mehat8b1c2ba2009-11-16 10:17:30 -08001282 msmsdcc_writel(host, 0, MMCIMASK0);
1283 msmsdcc_writel(host, 0x5e007ff, MMCICLEAR);
San Mehat9d2bd732009-09-22 16:44:22 -07001284
San Mehat8b1c2ba2009-11-16 10:17:30 -08001285 msmsdcc_writel(host, MCI_IRQENABLE, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -07001286 host->saved_irq0mask = MCI_IRQENABLE;
1287
1288 /*
1289 * Setup card detect change
1290 */
1291
1292 memset(&host->timer, 0, sizeof(host->timer));
1293
1294 if (stat_irqres && !(stat_irqres->flags & IORESOURCE_DISABLED)) {
1295 unsigned long irqflags = IRQF_SHARED |
1296 (stat_irqres->flags & IRQF_TRIGGER_MASK);
1297
1298 host->stat_irq = stat_irqres->start;
1299 ret = request_irq(host->stat_irq,
1300 msmsdcc_platform_status_irq,
1301 irqflags,
1302 DRIVER_NAME " (slot)",
1303 host);
1304 if (ret) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001305 pr_err("%s: Unable to get slot IRQ %d (%d)\n",
1306 mmc_hostname(mmc), host->stat_irq, ret);
San Mehat9d2bd732009-09-22 16:44:22 -07001307 goto clk_disable;
1308 }
1309 } else if (plat->register_status_notify) {
1310 plat->register_status_notify(msmsdcc_status_notify_cb, host);
1311 } else if (!plat->status)
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001312 pr_err("%s: No card detect facilities available\n",
San Mehat9d2bd732009-09-22 16:44:22 -07001313 mmc_hostname(mmc));
1314 else {
1315 init_timer(&host->timer);
1316 host->timer.data = (unsigned long)host;
1317 host->timer.function = msmsdcc_check_status;
1318 host->timer.expires = jiffies + HZ;
1319 add_timer(&host->timer);
1320 }
1321
1322 if (plat->status) {
1323 host->oldstat = host->plat->status(mmc_dev(host->mmc));
1324 host->eject = !host->oldstat;
1325 }
1326
San Mehat865c8062009-11-13 13:42:06 -08001327 init_timer(&host->busclk_timer);
1328 host->busclk_timer.data = (unsigned long) host;
1329 host->busclk_timer.function = msmsdcc_busclk_expired;
San Mehat9d2bd732009-09-22 16:44:22 -07001330
1331 ret = request_irq(cmd_irqres->start, msmsdcc_irq, IRQF_SHARED,
1332 DRIVER_NAME " (cmd)", host);
1333 if (ret)
1334 goto stat_irq_free;
1335
1336 ret = request_irq(pio_irqres->start, msmsdcc_pio_irq, IRQF_SHARED,
1337 DRIVER_NAME " (pio)", host);
1338 if (ret)
1339 goto cmd_irq_free;
1340
1341 mmc_set_drvdata(pdev, mmc);
1342 mmc_add_host(mmc);
1343
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001344 pr_info("%s: Qualcomm MSM SDCC at 0x%016llx irq %d,%d dma %d\n",
1345 mmc_hostname(mmc), (unsigned long long)memres->start,
1346 (unsigned int) cmd_irqres->start,
1347 (unsigned int) host->stat_irq, host->dma.channel);
1348 pr_info("%s: 4 bit data mode %s\n", mmc_hostname(mmc),
1349 (mmc->caps & MMC_CAP_4_BIT_DATA ? "enabled" : "disabled"));
1350 pr_info("%s: MMC clock %u -> %u Hz, PCLK %u Hz\n",
1351 mmc_hostname(mmc), msmsdcc_fmin, msmsdcc_fmax, host->pclk_rate);
1352 pr_info("%s: Slot eject status = %d\n", mmc_hostname(mmc), host->eject);
1353 pr_info("%s: Power save feature enable = %d\n",
1354 mmc_hostname(mmc), msmsdcc_pwrsave);
San Mehat9d2bd732009-09-22 16:44:22 -07001355
1356 if (host->dma.channel != -1) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001357 pr_info("%s: DM non-cached buffer at %p, dma_addr 0x%.8x\n",
1358 mmc_hostname(mmc), host->dma.nc, host->dma.nc_busaddr);
1359 pr_info("%s: DM cmd busaddr 0x%.8x, cmdptr busaddr 0x%.8x\n",
1360 mmc_hostname(mmc), host->dma.cmd_busaddr,
1361 host->dma.cmdptr_busaddr);
San Mehat9d2bd732009-09-22 16:44:22 -07001362 } else
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001363 pr_info("%s: PIO transfer enabled\n", mmc_hostname(mmc));
San Mehat9d2bd732009-09-22 16:44:22 -07001364 if (host->timer.function)
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001365 pr_info("%s: Polling status mode enabled\n", mmc_hostname(mmc));
San Mehat9d2bd732009-09-22 16:44:22 -07001366
1367 return 0;
1368 cmd_irq_free:
1369 free_irq(cmd_irqres->start, host);
1370 stat_irq_free:
1371 if (host->stat_irq)
1372 free_irq(host->stat_irq, host);
1373 clk_disable:
San Mehatc7fc9372009-11-22 17:19:07 -08001374 msmsdcc_disable_clocks(host, 0);
San Mehat9d2bd732009-09-22 16:44:22 -07001375 clk_put:
1376 clk_put(host->clk);
San Mehat9d2bd732009-09-22 16:44:22 -07001377 pclk_put:
1378 clk_put(host->pclk);
1379 host_free:
1380 mmc_free_host(mmc);
1381 out:
1382 return ret;
1383}
1384
Daniel Walker08ecfde2010-06-23 12:32:20 -07001385#ifdef CONFIG_PM
1386#ifdef CONFIG_MMC_MSM7X00A_RESUME_IN_WQ
1387static void
1388do_resume_work(struct work_struct *work)
1389{
1390 struct msmsdcc_host *host =
1391 container_of(work, struct msmsdcc_host, resume_task);
1392 struct mmc_host *mmc = host->mmc;
1393
1394 if (mmc) {
1395 mmc_resume_host(mmc);
1396 if (host->stat_irq)
1397 enable_irq(host->stat_irq);
1398 }
1399}
1400#endif
1401
1402
San Mehat9d2bd732009-09-22 16:44:22 -07001403static int
1404msmsdcc_suspend(struct platform_device *dev, pm_message_t state)
1405{
1406 struct mmc_host *mmc = mmc_get_drvdata(dev);
1407 int rc = 0;
1408
1409 if (mmc) {
1410 struct msmsdcc_host *host = mmc_priv(mmc);
1411
1412 if (host->stat_irq)
1413 disable_irq(host->stat_irq);
1414
1415 if (mmc->card && mmc->card->type != MMC_TYPE_SDIO)
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001416 rc = mmc_suspend_host(mmc);
San Mehatd0719e52009-12-03 10:58:54 -08001417 if (!rc)
San Mehat8b1c2ba2009-11-16 10:17:30 -08001418 msmsdcc_writel(host, 0, MMCIMASK0);
San Mehatc7fc9372009-11-22 17:19:07 -08001419 if (host->clks_on)
1420 msmsdcc_disable_clocks(host, 0);
San Mehat9d2bd732009-09-22 16:44:22 -07001421 }
1422 return rc;
1423}
1424
1425static int
1426msmsdcc_resume(struct platform_device *dev)
1427{
1428 struct mmc_host *mmc = mmc_get_drvdata(dev);
San Mehat9d2bd732009-09-22 16:44:22 -07001429
1430 if (mmc) {
1431 struct msmsdcc_host *host = mmc_priv(mmc);
1432
San Mehatc7fc9372009-11-22 17:19:07 -08001433 msmsdcc_enable_clocks(host);
San Mehat9d2bd732009-09-22 16:44:22 -07001434
San Mehat8b1c2ba2009-11-16 10:17:30 -08001435 msmsdcc_writel(host, host->saved_irq0mask, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -07001436
1437 if (mmc->card && mmc->card->type != MMC_TYPE_SDIO)
1438 mmc_resume_host(mmc);
Roel Kluin5b8a2fb2010-01-17 20:25:36 +01001439 if (host->stat_irq)
San Mehat9d2bd732009-09-22 16:44:22 -07001440 enable_irq(host->stat_irq);
San Mehatf4748492009-11-23 15:36:31 -08001441#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -08001442 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -08001443#endif
San Mehat9d2bd732009-09-22 16:44:22 -07001444 }
1445 return 0;
1446}
Daniel Walker08ecfde2010-06-23 12:32:20 -07001447#else
1448#define msmsdcc_suspend 0
1449#define msmsdcc_resume 0
1450#endif
San Mehat9d2bd732009-09-22 16:44:22 -07001451
1452static struct platform_driver msmsdcc_driver = {
1453 .probe = msmsdcc_probe,
1454 .suspend = msmsdcc_suspend,
1455 .resume = msmsdcc_resume,
1456 .driver = {
1457 .name = "msm_sdcc",
1458 },
1459};
1460
1461static int __init msmsdcc_init(void)
1462{
1463 return platform_driver_register(&msmsdcc_driver);
1464}
1465
1466static void __exit msmsdcc_exit(void)
1467{
1468 platform_driver_unregister(&msmsdcc_driver);
1469}
1470
1471module_init(msmsdcc_init);
1472module_exit(msmsdcc_exit);
1473
1474MODULE_DESCRIPTION("Qualcomm MSM 7X00A Multimedia Card Interface driver");
1475MODULE_LICENSE("GPL");