blob: b40558e18e8771e4cf45cab544432ed7954d3a34 [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>
38
39#include <asm/cacheflush.h>
40#include <asm/div64.h>
41#include <asm/sizes.h>
42
Pavel Machek3989d172009-12-08 11:11:36 -080043#include <mach/mmc.h>
San Mehat9d2bd732009-09-22 16:44:22 -070044#include <mach/msm_iomap.h>
45#include <mach/dma.h>
San Mehat9d2bd732009-09-22 16:44:22 -070046
San Mehat9d2bd732009-09-22 16:44:22 -070047#include "msm_sdcc.h"
48
49#define DRIVER_NAME "msm-sdcc"
50
San Mehat24bbd7d2009-12-01 10:10:47 -080051#define BUSCLK_PWRSAVE 1
San Mehatc7fc9372009-11-22 17:19:07 -080052#define BUSCLK_TIMEOUT (HZ)
San Mehat9d2bd732009-09-22 16:44:22 -070053static unsigned int msmsdcc_fmin = 144000;
54static unsigned int msmsdcc_fmax = 50000000;
55static unsigned int msmsdcc_4bit = 1;
56static unsigned int msmsdcc_pwrsave = 1;
57static unsigned int msmsdcc_piopoll = 1;
58static unsigned int msmsdcc_sdioirq;
59
60#define PIO_SPINMAX 30
61#define CMD_SPINMAX 20
62
San Mehat865c80642009-11-13 13:42:06 -080063
San Mehatd0719e52009-12-03 10:58:54 -080064static inline void
San Mehatc7fc9372009-11-22 17:19:07 -080065msmsdcc_disable_clocks(struct msmsdcc_host *host, int deferr)
San Mehat865c80642009-11-13 13:42:06 -080066{
San Mehatc7fc9372009-11-22 17:19:07 -080067 WARN_ON(!host->clks_on);
San Mehat8b1c2ba2009-11-16 10:17:30 -080068
San Mehatf4748492009-11-23 15:36:31 -080069 BUG_ON(host->curr.mrq);
70
San Mehatc7fc9372009-11-22 17:19:07 -080071 if (deferr) {
72 mod_timer(&host->busclk_timer, jiffies + BUSCLK_TIMEOUT);
San Mehat865c80642009-11-13 13:42:06 -080073 } else {
San Mehatc7fc9372009-11-22 17:19:07 -080074 del_timer_sync(&host->busclk_timer);
San Mehatd0719e52009-12-03 10:58:54 -080075 /* Need to check clks_on again in case the busclk
76 * timer fired
77 */
78 if (host->clks_on) {
79 clk_disable(host->clk);
80 clk_disable(host->pclk);
81 host->clks_on = 0;
82 }
San Mehat865c80642009-11-13 13:42:06 -080083 }
San Mehatc7fc9372009-11-22 17:19:07 -080084}
85
86static inline int
87msmsdcc_enable_clocks(struct msmsdcc_host *host)
88{
89 int rc;
90
San Mehatc7fc9372009-11-22 17:19:07 -080091 del_timer_sync(&host->busclk_timer);
92
San Mehatd0719e52009-12-03 10:58:54 -080093 if (!host->clks_on) {
94 rc = clk_enable(host->pclk);
95 if (rc)
96 return rc;
97 rc = clk_enable(host->clk);
98 if (rc) {
99 clk_disable(host->pclk);
100 return rc;
101 }
102 udelay(1 + ((3 * USEC_PER_SEC) /
103 (host->clk_rate ? host->clk_rate : msmsdcc_fmin)));
104 host->clks_on = 1;
San Mehatc7fc9372009-11-22 17:19:07 -0800105 }
San Mehat865c80642009-11-13 13:42:06 -0800106 return 0;
107}
108
San Mehat8b1c2ba2009-11-16 10:17:30 -0800109static inline unsigned int
110msmsdcc_readl(struct msmsdcc_host *host, unsigned int reg)
111{
112 return readl(host->base + reg);
113}
114
115static inline void
116msmsdcc_writel(struct msmsdcc_host *host, u32 data, unsigned int reg)
117{
118 writel(data, host->base + reg);
119 /* 3 clk delay required! */
120 udelay(1 + ((3 * USEC_PER_SEC) /
121 (host->clk_rate ? host->clk_rate : msmsdcc_fmin)));
122}
San Mehat865c80642009-11-13 13:42:06 -0800123
San Mehat9d2bd732009-09-22 16:44:22 -0700124static void
125msmsdcc_start_command(struct msmsdcc_host *host, struct mmc_command *cmd,
126 u32 c);
127
128static void
129msmsdcc_request_end(struct msmsdcc_host *host, struct mmc_request *mrq)
130{
San Mehat9d2bd732009-09-22 16:44:22 -0700131 BUG_ON(host->curr.data);
132
133 host->curr.mrq = NULL;
134 host->curr.cmd = NULL;
135
136 if (mrq->data)
137 mrq->data->bytes_xfered = host->curr.data_xfered;
138 if (mrq->cmd->error == -ETIMEDOUT)
139 mdelay(5);
140
San Mehatf4748492009-11-23 15:36:31 -0800141#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -0800142 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -0800143#endif
San Mehat9d2bd732009-09-22 16:44:22 -0700144 /*
145 * Need to drop the host lock here; mmc_request_done may call
146 * back into the driver...
147 */
148 spin_unlock(&host->lock);
149 mmc_request_done(host->mmc, mrq);
150 spin_lock(&host->lock);
151}
152
153static void
154msmsdcc_stop_data(struct msmsdcc_host *host)
155{
San Mehat9d2bd732009-09-22 16:44:22 -0700156 host->curr.data = NULL;
157 host->curr.got_dataend = host->curr.got_datablkend = 0;
158}
159
160uint32_t msmsdcc_fifo_addr(struct msmsdcc_host *host)
161{
Joe Perches75d14522009-09-22 16:44:24 -0700162 switch (host->pdev_id) {
163 case 1:
San Mehat9d2bd732009-09-22 16:44:22 -0700164 return MSM_SDC1_PHYS + MMCIFIFO;
Joe Perches75d14522009-09-22 16:44:24 -0700165 case 2:
San Mehat9d2bd732009-09-22 16:44:22 -0700166 return MSM_SDC2_PHYS + MMCIFIFO;
Joe Perches75d14522009-09-22 16:44:24 -0700167 case 3:
San Mehat9d2bd732009-09-22 16:44:22 -0700168 return MSM_SDC3_PHYS + MMCIFIFO;
Joe Perches75d14522009-09-22 16:44:24 -0700169 case 4:
San Mehat9d2bd732009-09-22 16:44:22 -0700170 return MSM_SDC4_PHYS + MMCIFIFO;
Joe Perches75d14522009-09-22 16:44:24 -0700171 }
172 BUG();
San Mehat9d2bd732009-09-22 16:44:22 -0700173 return 0;
174}
175
San Mehat56a8b5b2009-11-21 12:29:46 -0800176static inline void
177msmsdcc_start_command_exec(struct msmsdcc_host *host, u32 arg, u32 c) {
178 msmsdcc_writel(host, arg, MMCIARGUMENT);
179 msmsdcc_writel(host, c, MMCICOMMAND);
180}
181
182static void
183msmsdcc_dma_exec_func(struct msm_dmov_cmd *cmd)
184{
San Mehat6ac9ea62009-12-02 17:24:58 -0800185 struct msmsdcc_host *host = (struct msmsdcc_host *)cmd->data;
San Mehat56a8b5b2009-11-21 12:29:46 -0800186
San Mehat6ac9ea62009-12-02 17:24:58 -0800187 msmsdcc_writel(host, host->cmd_timeout, MMCIDATATIMER);
San Mehatd0719e52009-12-03 10:58:54 -0800188 msmsdcc_writel(host, (unsigned int)host->curr.xfer_size,
189 MMCIDATALENGTH);
San Mehat6ac9ea62009-12-02 17:24:58 -0800190 msmsdcc_writel(host, host->cmd_pio_irqmask, MMCIMASK1);
191 msmsdcc_writel(host, host->cmd_datactrl, MMCIDATACTRL);
San Mehat56a8b5b2009-11-21 12:29:46 -0800192
San Mehat6ac9ea62009-12-02 17:24:58 -0800193 if (host->cmd_cmd) {
194 msmsdcc_start_command_exec(host,
195 (u32) host->cmd_cmd->arg,
196 (u32) host->cmd_c);
197 }
San Mehat56a8b5b2009-11-21 12:29:46 -0800198 host->dma.active = 1;
199}
200
San Mehat9d2bd732009-09-22 16:44:22 -0700201static void
202msmsdcc_dma_complete_func(struct msm_dmov_cmd *cmd,
203 unsigned int result,
204 struct msm_dmov_errdata *err)
205{
206 struct msmsdcc_dma_data *dma_data =
207 container_of(cmd, struct msmsdcc_dma_data, hdr);
208 struct msmsdcc_host *host = dma_data->host;
209 unsigned long flags;
210 struct mmc_request *mrq;
211
212 spin_lock_irqsave(&host->lock, flags);
San Mehat56a8b5b2009-11-21 12:29:46 -0800213 host->dma.active = 0;
214
San Mehat9d2bd732009-09-22 16:44:22 -0700215 mrq = host->curr.mrq;
216 BUG_ON(!mrq);
San Mehatb3b0ca82009-11-24 12:24:55 -0800217 WARN_ON(!mrq->data);
San Mehat9d2bd732009-09-22 16:44:22 -0700218
219 if (!(result & DMOV_RSLT_VALID)) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700220 pr_err("msmsdcc: Invalid DataMover result\n");
San Mehat9d2bd732009-09-22 16:44:22 -0700221 goto out;
222 }
223
224 if (result & DMOV_RSLT_DONE) {
225 host->curr.data_xfered = host->curr.xfer_size;
226 } else {
227 /* Error or flush */
228 if (result & DMOV_RSLT_ERROR)
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700229 pr_err("%s: DMA error (0x%.8x)\n",
San Mehat9d2bd732009-09-22 16:44:22 -0700230 mmc_hostname(host->mmc), result);
231 if (result & DMOV_RSLT_FLUSH)
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700232 pr_err("%s: DMA channel flushed (0x%.8x)\n",
San Mehat9d2bd732009-09-22 16:44:22 -0700233 mmc_hostname(host->mmc), result);
234 if (err)
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700235 pr_err("Flush data: %.8x %.8x %.8x %.8x %.8x %.8x\n",
San Mehat9d2bd732009-09-22 16:44:22 -0700236 err->flush[0], err->flush[1], err->flush[2],
237 err->flush[3], err->flush[4], err->flush[5]);
238 if (!mrq->data->error)
239 mrq->data->error = -EIO;
240 }
San Mehat9d2bd732009-09-22 16:44:22 -0700241 dma_unmap_sg(mmc_dev(host->mmc), host->dma.sg, host->dma.num_ents,
242 host->dma.dir);
243
244 if (host->curr.user_pages) {
245 struct scatterlist *sg = host->dma.sg;
246 int i;
247
Joe Perches75d14522009-09-22 16:44:24 -0700248 for (i = 0; i < host->dma.num_ents; i++)
249 flush_dcache_page(sg_page(sg++));
San Mehat9d2bd732009-09-22 16:44:22 -0700250 }
251
252 host->dma.sg = NULL;
San Mehat56a8b5b2009-11-21 12:29:46 -0800253 host->dma.busy = 0;
San Mehat9d2bd732009-09-22 16:44:22 -0700254
255 if ((host->curr.got_dataend && host->curr.got_datablkend)
256 || mrq->data->error) {
257
258 /*
259 * If we've already gotten our DATAEND / DATABLKEND
260 * for this request, then complete it through here.
261 */
262 msmsdcc_stop_data(host);
263
264 if (!mrq->data->error)
265 host->curr.data_xfered = host->curr.xfer_size;
266 if (!mrq->data->stop || mrq->cmd->error) {
San Mehat9d2bd732009-09-22 16:44:22 -0700267 host->curr.mrq = NULL;
268 host->curr.cmd = NULL;
269 mrq->data->bytes_xfered = host->curr.data_xfered;
270
271 spin_unlock_irqrestore(&host->lock, flags);
San Mehatf4748492009-11-23 15:36:31 -0800272#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -0800273 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -0800274#endif
San Mehat9d2bd732009-09-22 16:44:22 -0700275 mmc_request_done(host->mmc, mrq);
276 return;
277 } else
278 msmsdcc_start_command(host, mrq->data->stop, 0);
279 }
280
281out:
282 spin_unlock_irqrestore(&host->lock, flags);
283 return;
284}
285
286static int validate_dma(struct msmsdcc_host *host, struct mmc_data *data)
287{
288 if (host->dma.channel == -1)
289 return -ENOENT;
290
291 if ((data->blksz * data->blocks) < MCI_FIFOSIZE)
292 return -EINVAL;
293 if ((data->blksz * data->blocks) % MCI_FIFOSIZE)
294 return -EINVAL;
295 return 0;
296}
297
298static int msmsdcc_config_dma(struct msmsdcc_host *host, struct mmc_data *data)
299{
300 struct msmsdcc_nc_dmadata *nc;
301 dmov_box *box;
302 uint32_t rows;
303 uint32_t crci;
304 unsigned int n;
305 int i, rc;
306 struct scatterlist *sg = data->sg;
307
308 rc = validate_dma(host, data);
309 if (rc)
310 return rc;
311
312 host->dma.sg = data->sg;
313 host->dma.num_ents = data->sg_len;
314
San Mehat56a8b5b2009-11-21 12:29:46 -0800315 BUG_ON(host->dma.num_ents > NR_SG); /* Prevent memory corruption */
316
San Mehat9d2bd732009-09-22 16:44:22 -0700317 nc = host->dma.nc;
318
Joe Perches75d14522009-09-22 16:44:24 -0700319 switch (host->pdev_id) {
320 case 1:
San Mehat9d2bd732009-09-22 16:44:22 -0700321 crci = MSMSDCC_CRCI_SDC1;
Joe Perches75d14522009-09-22 16:44:24 -0700322 break;
323 case 2:
San Mehat9d2bd732009-09-22 16:44:22 -0700324 crci = MSMSDCC_CRCI_SDC2;
Joe Perches75d14522009-09-22 16:44:24 -0700325 break;
326 case 3:
San Mehat9d2bd732009-09-22 16:44:22 -0700327 crci = MSMSDCC_CRCI_SDC3;
Joe Perches75d14522009-09-22 16:44:24 -0700328 break;
329 case 4:
San Mehat9d2bd732009-09-22 16:44:22 -0700330 crci = MSMSDCC_CRCI_SDC4;
Joe Perches75d14522009-09-22 16:44:24 -0700331 break;
332 default:
San Mehat9d2bd732009-09-22 16:44:22 -0700333 host->dma.sg = NULL;
334 host->dma.num_ents = 0;
335 return -ENOENT;
336 }
337
338 if (data->flags & MMC_DATA_READ)
339 host->dma.dir = DMA_FROM_DEVICE;
340 else
341 host->dma.dir = DMA_TO_DEVICE;
342
343 host->curr.user_pages = 0;
344
San Mehat9d2bd732009-09-22 16:44:22 -0700345 box = &nc->cmd[0];
346 for (i = 0; i < host->dma.num_ents; i++) {
347 box->cmd = CMD_MODE_BOX;
348
San Mehat56a8b5b2009-11-21 12:29:46 -0800349 /* Initialize sg dma address */
350 sg->dma_address = page_to_dma(mmc_dev(host->mmc), sg_page(sg))
351 + sg->offset;
352
353 if (i == (host->dma.num_ents - 1))
San Mehat9d2bd732009-09-22 16:44:22 -0700354 box->cmd |= CMD_LC;
355 rows = (sg_dma_len(sg) % MCI_FIFOSIZE) ?
356 (sg_dma_len(sg) / MCI_FIFOSIZE) + 1 :
357 (sg_dma_len(sg) / MCI_FIFOSIZE) ;
358
359 if (data->flags & MMC_DATA_READ) {
360 box->src_row_addr = msmsdcc_fifo_addr(host);
361 box->dst_row_addr = sg_dma_address(sg);
362
363 box->src_dst_len = (MCI_FIFOSIZE << 16) |
364 (MCI_FIFOSIZE);
365 box->row_offset = MCI_FIFOSIZE;
366
367 box->num_rows = rows * ((1 << 16) + 1);
368 box->cmd |= CMD_SRC_CRCI(crci);
369 } else {
370 box->src_row_addr = sg_dma_address(sg);
371 box->dst_row_addr = msmsdcc_fifo_addr(host);
372
373 box->src_dst_len = (MCI_FIFOSIZE << 16) |
374 (MCI_FIFOSIZE);
375 box->row_offset = (MCI_FIFOSIZE << 16);
376
377 box->num_rows = rows * ((1 << 16) + 1);
378 box->cmd |= CMD_DST_CRCI(crci);
379 }
380 box++;
381 sg++;
382 }
383
384 /* location of command block must be 64 bit aligned */
385 BUG_ON(host->dma.cmd_busaddr & 0x07);
386
387 nc->cmdptr = (host->dma.cmd_busaddr >> 3) | CMD_PTR_LP;
388 host->dma.hdr.cmdptr = DMOV_CMD_PTR_LIST |
389 DMOV_CMD_ADDR(host->dma.cmdptr_busaddr);
390 host->dma.hdr.complete_func = msmsdcc_dma_complete_func;
San Mehat56a8b5b2009-11-21 12:29:46 -0800391
392 n = dma_map_sg(mmc_dev(host->mmc), host->dma.sg,
393 host->dma.num_ents, host->dma.dir);
394/* dsb inside dma_map_sg will write nc out to mem as well */
395
396 if (n != host->dma.num_ents) {
397 printk(KERN_ERR "%s: Unable to map in all sg elements\n",
398 mmc_hostname(host->mmc));
399 host->dma.sg = NULL;
400 host->dma.num_ents = 0;
401 return -ENOMEM;
402 }
San Mehat9d2bd732009-09-22 16:44:22 -0700403
404 return 0;
405}
406
San Mehat56a8b5b2009-11-21 12:29:46 -0800407static int
408snoop_cccr_abort(struct mmc_command *cmd)
409{
410 if ((cmd->opcode == 52) &&
411 (cmd->arg & 0x80000000) &&
412 (((cmd->arg >> 9) & 0x1ffff) == SDIO_CCCR_ABORT))
413 return 1;
414 return 0;
415}
416
San Mehat9d2bd732009-09-22 16:44:22 -0700417static void
San Mehat56a8b5b2009-11-21 12:29:46 -0800418msmsdcc_start_command_deferred(struct msmsdcc_host *host,
419 struct mmc_command *cmd, u32 *c)
420{
421 *c |= (cmd->opcode | MCI_CPSM_ENABLE);
422
423 if (cmd->flags & MMC_RSP_PRESENT) {
424 if (cmd->flags & MMC_RSP_136)
425 *c |= MCI_CPSM_LONGRSP;
426 *c |= MCI_CPSM_RESPONSE;
427 }
428
429 if (/*interrupt*/0)
430 *c |= MCI_CPSM_INTERRUPT;
431
432 if ((((cmd->opcode == 17) || (cmd->opcode == 18)) ||
433 ((cmd->opcode == 24) || (cmd->opcode == 25))) ||
434 (cmd->opcode == 53))
435 *c |= MCI_CSPM_DATCMD;
436
437 if (cmd == cmd->mrq->stop)
438 *c |= MCI_CSPM_MCIABORT;
439
440 if (snoop_cccr_abort(cmd))
441 *c |= MCI_CSPM_MCIABORT;
442
443 if (host->curr.cmd != NULL) {
444 printk(KERN_ERR "%s: Overlapping command requests\n",
445 mmc_hostname(host->mmc));
446 }
447 host->curr.cmd = cmd;
448}
449
450static void
451msmsdcc_start_data(struct msmsdcc_host *host, struct mmc_data *data,
452 struct mmc_command *cmd, u32 c)
San Mehat9d2bd732009-09-22 16:44:22 -0700453{
454 unsigned int datactrl, timeout;
455 unsigned long long clks;
San Mehat9d2bd732009-09-22 16:44:22 -0700456 unsigned int pio_irqmask = 0;
457
458 host->curr.data = data;
459 host->curr.xfer_size = data->blksz * data->blocks;
460 host->curr.xfer_remain = host->curr.xfer_size;
461 host->curr.data_xfered = 0;
462 host->curr.got_dataend = 0;
463 host->curr.got_datablkend = 0;
464
465 memset(&host->pio, 0, sizeof(host->pio));
466
San Mehat9d2bd732009-09-22 16:44:22 -0700467 datactrl = MCI_DPSM_ENABLE | (data->blksz << 4);
468
469 if (!msmsdcc_config_dma(host, data))
470 datactrl |= MCI_DPSM_DMAENABLE;
471 else {
472 host->pio.sg = data->sg;
473 host->pio.sg_len = data->sg_len;
474 host->pio.sg_off = 0;
475
476 if (data->flags & MMC_DATA_READ) {
477 pio_irqmask = MCI_RXFIFOHALFFULLMASK;
478 if (host->curr.xfer_remain < MCI_FIFOSIZE)
479 pio_irqmask |= MCI_RXDATAAVLBLMASK;
480 } else
481 pio_irqmask = MCI_TXFIFOHALFEMPTYMASK;
482 }
483
484 if (data->flags & MMC_DATA_READ)
485 datactrl |= MCI_DPSM_DIRECTION;
486
San Mehat56a8b5b2009-11-21 12:29:46 -0800487 clks = (unsigned long long)data->timeout_ns * host->clk_rate;
488 do_div(clks, NSEC_PER_SEC);
489 timeout = data->timeout_clks + (unsigned int)clks*2 ;
San Mehat9d2bd732009-09-22 16:44:22 -0700490
491 if (datactrl & MCI_DPSM_DMAENABLE) {
San Mehat56a8b5b2009-11-21 12:29:46 -0800492 /* Save parameters for the exec function */
493 host->cmd_timeout = timeout;
494 host->cmd_pio_irqmask = pio_irqmask;
495 host->cmd_datactrl = datactrl;
496 host->cmd_cmd = cmd;
San Mehat9d2bd732009-09-22 16:44:22 -0700497
San Mehat56a8b5b2009-11-21 12:29:46 -0800498 host->dma.hdr.execute_func = msmsdcc_dma_exec_func;
499 host->dma.hdr.data = (void *)host;
500 host->dma.busy = 1;
501
502 if (cmd) {
503 msmsdcc_start_command_deferred(host, cmd, &c);
504 host->cmd_c = c;
505 }
506 msm_dmov_enqueue_cmd(host->dma.channel, &host->dma.hdr);
507 } else {
508 msmsdcc_writel(host, timeout, MMCIDATATIMER);
509
510 msmsdcc_writel(host, host->curr.xfer_size, MMCIDATALENGTH);
511
512 msmsdcc_writel(host, pio_irqmask, MMCIMASK1);
513 msmsdcc_writel(host, datactrl, MMCIDATACTRL);
514
515 if (cmd) {
516 /* Daisy-chain the command if requested */
517 msmsdcc_start_command(host, cmd, c);
518 }
519 }
San Mehatb3fa5792009-11-02 18:46:09 -0800520}
521
San Mehat9d2bd732009-09-22 16:44:22 -0700522static void
523msmsdcc_start_command(struct msmsdcc_host *host, struct mmc_command *cmd, u32 c)
524{
San Mehat9d2bd732009-09-22 16:44:22 -0700525 if (cmd == cmd->mrq->stop)
526 c |= MCI_CSPM_MCIABORT;
527
San Mehat9d2bd732009-09-22 16:44:22 -0700528 host->stats.cmds++;
529
San Mehat56a8b5b2009-11-21 12:29:46 -0800530 msmsdcc_start_command_deferred(host, cmd, &c);
531 msmsdcc_start_command_exec(host, cmd->arg, c);
San Mehat9d2bd732009-09-22 16:44:22 -0700532}
533
534static void
535msmsdcc_data_err(struct msmsdcc_host *host, struct mmc_data *data,
536 unsigned int status)
537{
538 if (status & MCI_DATACRCFAIL) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700539 pr_err("%s: Data CRC error\n", mmc_hostname(host->mmc));
540 pr_err("%s: opcode 0x%.8x\n", __func__,
San Mehat9d2bd732009-09-22 16:44:22 -0700541 data->mrq->cmd->opcode);
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700542 pr_err("%s: blksz %d, blocks %d\n", __func__,
San Mehat9d2bd732009-09-22 16:44:22 -0700543 data->blksz, data->blocks);
544 data->error = -EILSEQ;
545 } else if (status & MCI_DATATIMEOUT) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700546 pr_err("%s: Data timeout\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700547 data->error = -ETIMEDOUT;
548 } else if (status & MCI_RXOVERRUN) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700549 pr_err("%s: RX overrun\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700550 data->error = -EIO;
551 } else if (status & MCI_TXUNDERRUN) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700552 pr_err("%s: TX underrun\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700553 data->error = -EIO;
554 } else {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700555 pr_err("%s: Unknown error (0x%.8x)\n",
556 mmc_hostname(host->mmc), status);
San Mehat9d2bd732009-09-22 16:44:22 -0700557 data->error = -EIO;
558 }
559}
560
561
562static int
563msmsdcc_pio_read(struct msmsdcc_host *host, char *buffer, unsigned int remain)
564{
San Mehat9d2bd732009-09-22 16:44:22 -0700565 uint32_t *ptr = (uint32_t *) buffer;
566 int count = 0;
567
San Mehat8b1c2ba2009-11-16 10:17:30 -0800568 while (msmsdcc_readl(host, MMCISTATUS) & MCI_RXDATAAVLBL) {
569 *ptr = msmsdcc_readl(host, MMCIFIFO + (count % MCI_FIFOSIZE));
San Mehat9d2bd732009-09-22 16:44:22 -0700570 ptr++;
571 count += sizeof(uint32_t);
572
573 remain -= sizeof(uint32_t);
574 if (remain == 0)
575 break;
576 }
577 return count;
578}
579
580static int
581msmsdcc_pio_write(struct msmsdcc_host *host, char *buffer,
582 unsigned int remain, u32 status)
583{
584 void __iomem *base = host->base;
585 char *ptr = buffer;
586
587 do {
588 unsigned int count, maxcnt;
589
590 maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE :
591 MCI_FIFOHALFSIZE;
592 count = min(remain, maxcnt);
593
594 writesl(base + MMCIFIFO, ptr, count >> 2);
595 ptr += count;
596 remain -= count;
597
598 if (remain == 0)
599 break;
600
San Mehat8b1c2ba2009-11-16 10:17:30 -0800601 status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700602 } while (status & MCI_TXFIFOHALFEMPTY);
603
604 return ptr - buffer;
605}
606
607static int
608msmsdcc_spin_on_status(struct msmsdcc_host *host, uint32_t mask, int maxspin)
609{
610 while (maxspin) {
San Mehat8b1c2ba2009-11-16 10:17:30 -0800611 if ((msmsdcc_readl(host, MMCISTATUS) & mask))
San Mehat9d2bd732009-09-22 16:44:22 -0700612 return 0;
613 udelay(1);
614 --maxspin;
615 }
616 return -ETIMEDOUT;
617}
618
San Mehat1cd22962010-02-03 12:59:29 -0800619static irqreturn_t
San Mehat9d2bd732009-09-22 16:44:22 -0700620msmsdcc_pio_irq(int irq, void *dev_id)
621{
622 struct msmsdcc_host *host = dev_id;
San Mehat9d2bd732009-09-22 16:44:22 -0700623 uint32_t status;
624
San Mehat8b1c2ba2009-11-16 10:17:30 -0800625 status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700626
627 do {
628 unsigned long flags;
629 unsigned int remain, len;
630 char *buffer;
631
632 if (!(status & (MCI_TXFIFOHALFEMPTY | MCI_RXDATAAVLBL))) {
633 if (host->curr.xfer_remain == 0 || !msmsdcc_piopoll)
634 break;
635
636 if (msmsdcc_spin_on_status(host,
637 (MCI_TXFIFOHALFEMPTY |
638 MCI_RXDATAAVLBL),
639 PIO_SPINMAX)) {
640 break;
641 }
642 }
643
644 /* Map the current scatter buffer */
645 local_irq_save(flags);
646 buffer = kmap_atomic(sg_page(host->pio.sg),
647 KM_BIO_SRC_IRQ) + host->pio.sg->offset;
648 buffer += host->pio.sg_off;
649 remain = host->pio.sg->length - host->pio.sg_off;
650 len = 0;
651 if (status & MCI_RXACTIVE)
652 len = msmsdcc_pio_read(host, buffer, remain);
653 if (status & MCI_TXACTIVE)
654 len = msmsdcc_pio_write(host, buffer, remain, status);
655
656 /* Unmap the buffer */
657 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
658 local_irq_restore(flags);
659
660 host->pio.sg_off += len;
661 host->curr.xfer_remain -= len;
662 host->curr.data_xfered += len;
663 remain -= len;
664
665 if (remain == 0) {
666 /* This sg page is full - do some housekeeping */
667 if (status & MCI_RXACTIVE && host->curr.user_pages)
668 flush_dcache_page(sg_page(host->pio.sg));
669
670 if (!--host->pio.sg_len) {
671 memset(&host->pio, 0, sizeof(host->pio));
672 break;
673 }
674
675 /* Advance to next sg */
676 host->pio.sg++;
677 host->pio.sg_off = 0;
678 }
679
San Mehat8b1c2ba2009-11-16 10:17:30 -0800680 status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700681 } while (1);
682
683 if (status & MCI_RXACTIVE && host->curr.xfer_remain < MCI_FIFOSIZE)
San Mehat8b1c2ba2009-11-16 10:17:30 -0800684 msmsdcc_writel(host, MCI_RXDATAAVLBLMASK, MMCIMASK1);
San Mehat9d2bd732009-09-22 16:44:22 -0700685
686 if (!host->curr.xfer_remain)
San Mehat8b1c2ba2009-11-16 10:17:30 -0800687 msmsdcc_writel(host, 0, MMCIMASK1);
San Mehat9d2bd732009-09-22 16:44:22 -0700688
689 return IRQ_HANDLED;
690}
691
692static void msmsdcc_do_cmdirq(struct msmsdcc_host *host, uint32_t status)
693{
694 struct mmc_command *cmd = host->curr.cmd;
San Mehat9d2bd732009-09-22 16:44:22 -0700695
696 host->curr.cmd = NULL;
San Mehat8b1c2ba2009-11-16 10:17:30 -0800697 cmd->resp[0] = msmsdcc_readl(host, MMCIRESPONSE0);
698 cmd->resp[1] = msmsdcc_readl(host, MMCIRESPONSE1);
699 cmd->resp[2] = msmsdcc_readl(host, MMCIRESPONSE2);
700 cmd->resp[3] = msmsdcc_readl(host, MMCIRESPONSE3);
San Mehat9d2bd732009-09-22 16:44:22 -0700701
San Mehat9d2bd732009-09-22 16:44:22 -0700702 if (status & MCI_CMDTIMEOUT) {
703 cmd->error = -ETIMEDOUT;
704 } else if (status & MCI_CMDCRCFAIL &&
705 cmd->flags & MMC_RSP_CRC) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700706 pr_err("%s: Command CRC error\n", mmc_hostname(host->mmc));
San Mehat9d2bd732009-09-22 16:44:22 -0700707 cmd->error = -EILSEQ;
708 }
709
710 if (!cmd->data || cmd->error) {
711 if (host->curr.data && host->dma.sg)
712 msm_dmov_stop_cmd(host->dma.channel,
713 &host->dma.hdr, 0);
714 else if (host->curr.data) { /* Non DMA */
715 msmsdcc_stop_data(host);
716 msmsdcc_request_end(host, cmd->mrq);
717 } else /* host->data == NULL */
718 msmsdcc_request_end(host, cmd->mrq);
San Mehat56a8b5b2009-11-21 12:29:46 -0800719 } else if (cmd->data)
720 if (!(cmd->data->flags & MMC_DATA_READ))
721 msmsdcc_start_data(host, cmd->data,
722 NULL, 0);
San Mehat9d2bd732009-09-22 16:44:22 -0700723}
724
Joe Perchesb5a74d62009-09-22 16:44:25 -0700725static void
726msmsdcc_handle_irq_data(struct msmsdcc_host *host, u32 status,
727 void __iomem *base)
728{
San Mehatb3b0ca82009-11-24 12:24:55 -0800729 struct mmc_data *data = host->curr.data;
Joe Perchesb5a74d62009-09-22 16:44:25 -0700730
San Mehat56a8b5b2009-11-21 12:29:46 -0800731 if (status & (MCI_CMDSENT | MCI_CMDRESPEND | MCI_CMDCRCFAIL |
732 MCI_CMDTIMEOUT) && host->curr.cmd) {
733 msmsdcc_do_cmdirq(host, status);
734 }
735
Joe Perchesb5a74d62009-09-22 16:44:25 -0700736 if (!data)
737 return;
738
739 /* Check for data errors */
740 if (status & (MCI_DATACRCFAIL | MCI_DATATIMEOUT |
741 MCI_TXUNDERRUN | MCI_RXOVERRUN)) {
742 msmsdcc_data_err(host, data, status);
743 host->curr.data_xfered = 0;
744 if (host->dma.sg)
745 msm_dmov_stop_cmd(host->dma.channel,
746 &host->dma.hdr, 0);
747 else {
San Mehatb3b0ca82009-11-24 12:24:55 -0800748 if (host->curr.data)
749 msmsdcc_stop_data(host);
Joe Perchesb5a74d62009-09-22 16:44:25 -0700750 if (!data->stop)
751 msmsdcc_request_end(host, data->mrq);
752 else
753 msmsdcc_start_command(host, data->stop, 0);
754 }
755 }
756
757 /* Check for data done */
758 if (!host->curr.got_dataend && (status & MCI_DATAEND))
759 host->curr.got_dataend = 1;
760
761 if (!host->curr.got_datablkend && (status & MCI_DATABLOCKEND))
762 host->curr.got_datablkend = 1;
763
764 /*
765 * If DMA is still in progress, we complete via the completion handler
766 */
767 if (host->curr.got_dataend && host->curr.got_datablkend &&
768 !host->dma.busy) {
769 /*
770 * There appears to be an issue in the controller where
771 * if you request a small block transfer (< fifo size),
772 * you may get your DATAEND/DATABLKEND irq without the
773 * PIO data irq.
774 *
775 * Check to see if there is still data to be read,
776 * and simulate a PIO irq.
777 */
778 if (readl(base + MMCISTATUS) & MCI_RXDATAAVLBL)
779 msmsdcc_pio_irq(1, host);
780
781 msmsdcc_stop_data(host);
782 if (!data->error)
783 host->curr.data_xfered = host->curr.xfer_size;
784
785 if (!data->stop)
786 msmsdcc_request_end(host, data->mrq);
787 else
788 msmsdcc_start_command(host, data->stop, 0);
789 }
790}
791
San Mehat9d2bd732009-09-22 16:44:22 -0700792static irqreturn_t
793msmsdcc_irq(int irq, void *dev_id)
794{
795 struct msmsdcc_host *host = dev_id;
796 void __iomem *base = host->base;
797 u32 status;
798 int ret = 0;
799 int cardint = 0;
800
801 spin_lock(&host->lock);
802
803 do {
San Mehat8b1c2ba2009-11-16 10:17:30 -0800804 status = msmsdcc_readl(host, MMCISTATUS);
805 status &= (msmsdcc_readl(host, MMCIMASK0) |
806 MCI_DATABLOCKENDMASK);
807 msmsdcc_writel(host, status, MMCICLEAR);
San Mehat9d2bd732009-09-22 16:44:22 -0700808
San Mehat865c80642009-11-13 13:42:06 -0800809 if (status & MCI_SDIOINTR)
810 status &= ~MCI_SDIOINTR;
811
812 if (!status)
813 break;
814
Joe Perchesb5a74d62009-09-22 16:44:25 -0700815 msmsdcc_handle_irq_data(host, status, base);
San Mehat9d2bd732009-09-22 16:44:22 -0700816
San Mehat9d2bd732009-09-22 16:44:22 -0700817 if (status & MCI_SDIOINTOPER) {
818 cardint = 1;
819 status &= ~MCI_SDIOINTOPER;
820 }
821 ret = 1;
822 } while (status);
823
824 spin_unlock(&host->lock);
825
826 /*
827 * We have to delay handling the card interrupt as it calls
828 * back into the driver.
829 */
830 if (cardint)
831 mmc_signal_sdio_irq(host->mmc);
832
833 return IRQ_RETVAL(ret);
834}
835
836static void
837msmsdcc_request(struct mmc_host *mmc, struct mmc_request *mrq)
838{
839 struct msmsdcc_host *host = mmc_priv(mmc);
840 unsigned long flags;
841
842 WARN_ON(host->curr.mrq != NULL);
843 WARN_ON(host->pwr == 0);
844
845 spin_lock_irqsave(&host->lock, flags);
846
847 host->stats.reqs++;
848
849 if (host->eject) {
850 if (mrq->data && !(mrq->data->flags & MMC_DATA_READ)) {
851 mrq->cmd->error = 0;
852 mrq->data->bytes_xfered = mrq->data->blksz *
853 mrq->data->blocks;
854 } else
855 mrq->cmd->error = -ENOMEDIUM;
856
857 spin_unlock_irqrestore(&host->lock, flags);
858 mmc_request_done(mmc, mrq);
859 return;
860 }
861
San Mehatd0719e52009-12-03 10:58:54 -0800862 msmsdcc_enable_clocks(host);
San Mehat9d2bd732009-09-22 16:44:22 -0700863
Dmitry Shmidt91bb6492009-12-02 17:21:07 -0800864 host->curr.mrq = mrq;
865
San Mehat9d2bd732009-09-22 16:44:22 -0700866 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
San Mehat56a8b5b2009-11-21 12:29:46 -0800867 /* Queue/read data, daisy-chain command when data starts */
868 msmsdcc_start_data(host, mrq->data, mrq->cmd, 0);
869 else
870 msmsdcc_start_command(host, mrq->cmd, 0);
San Mehat9d2bd732009-09-22 16:44:22 -0700871
872 if (host->cmdpoll && !msmsdcc_spin_on_status(host,
873 MCI_CMDRESPEND|MCI_CMDCRCFAIL|MCI_CMDTIMEOUT,
874 CMD_SPINMAX)) {
San Mehat8b1c2ba2009-11-16 10:17:30 -0800875 uint32_t status = msmsdcc_readl(host, MMCISTATUS);
San Mehat9d2bd732009-09-22 16:44:22 -0700876 msmsdcc_do_cmdirq(host, status);
San Mehat8b1c2ba2009-11-16 10:17:30 -0800877 msmsdcc_writel(host,
878 MCI_CMDRESPEND | MCI_CMDCRCFAIL | MCI_CMDTIMEOUT,
879 MMCICLEAR);
San Mehat9d2bd732009-09-22 16:44:22 -0700880 host->stats.cmdpoll_hits++;
881 } else {
882 host->stats.cmdpoll_misses++;
San Mehat9d2bd732009-09-22 16:44:22 -0700883 }
884 spin_unlock_irqrestore(&host->lock, flags);
885}
886
887static void
888msmsdcc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
889{
890 struct msmsdcc_host *host = mmc_priv(mmc);
891 u32 clk = 0, pwr = 0;
892 int rc;
San Mehat4adbbcc2009-11-08 13:00:37 -0800893 unsigned long flags;
San Mehat9d2bd732009-09-22 16:44:22 -0700894
San Mehatc7fc9372009-11-22 17:19:07 -0800895 spin_lock_irqsave(&host->lock, flags);
San Mehat9d2bd732009-09-22 16:44:22 -0700896
San Mehatd0719e52009-12-03 10:58:54 -0800897 msmsdcc_enable_clocks(host);
898
San Mehat865c80642009-11-13 13:42:06 -0800899 if (ios->clock) {
San Mehat9d2bd732009-09-22 16:44:22 -0700900 if (ios->clock != host->clk_rate) {
901 rc = clk_set_rate(host->clk, ios->clock);
902 if (rc < 0)
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700903 pr_err("%s: Error setting clock rate (%d)\n",
904 mmc_hostname(host->mmc), rc);
San Mehat9d2bd732009-09-22 16:44:22 -0700905 else
906 host->clk_rate = ios->clock;
907 }
908 clk |= MCI_CLK_ENABLE;
909 }
910
911 if (ios->bus_width == MMC_BUS_WIDTH_4)
912 clk |= (2 << 10); /* Set WIDEBUS */
913
914 if (ios->clock > 400000 && msmsdcc_pwrsave)
915 clk |= (1 << 9); /* PWRSAVE */
916
917 clk |= (1 << 12); /* FLOW_ENA */
918 clk |= (1 << 15); /* feedback clock */
919
920 if (host->plat->translate_vdd)
921 pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
922
923 switch (ios->power_mode) {
924 case MMC_POWER_OFF:
San Mehat9d2bd732009-09-22 16:44:22 -0700925 break;
926 case MMC_POWER_UP:
927 pwr |= MCI_PWR_UP;
928 break;
929 case MMC_POWER_ON:
San Mehat9d2bd732009-09-22 16:44:22 -0700930 pwr |= MCI_PWR_ON;
931 break;
932 }
933
934 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
935 pwr |= MCI_OD;
936
San Mehat8b1c2ba2009-11-16 10:17:30 -0800937 msmsdcc_writel(host, clk, MMCICLOCK);
San Mehat9d2bd732009-09-22 16:44:22 -0700938
939 if (host->pwr != pwr) {
940 host->pwr = pwr;
San Mehat8b1c2ba2009-11-16 10:17:30 -0800941 msmsdcc_writel(host, pwr, MMCIPOWER);
San Mehat9d2bd732009-09-22 16:44:22 -0700942 }
San Mehatf4748492009-11-23 15:36:31 -0800943#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -0800944 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -0800945#endif
San Mehat4adbbcc2009-11-08 13:00:37 -0800946 spin_unlock_irqrestore(&host->lock, flags);
San Mehat9d2bd732009-09-22 16:44:22 -0700947}
948
949static void msmsdcc_enable_sdio_irq(struct mmc_host *mmc, int enable)
950{
951 struct msmsdcc_host *host = mmc_priv(mmc);
952 unsigned long flags;
953 u32 status;
954
955 spin_lock_irqsave(&host->lock, flags);
956 if (msmsdcc_sdioirq == 1) {
San Mehat8b1c2ba2009-11-16 10:17:30 -0800957 status = msmsdcc_readl(host, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -0700958 if (enable)
959 status |= MCI_SDIOINTOPERMASK;
960 else
961 status &= ~MCI_SDIOINTOPERMASK;
962 host->saved_irq0mask = status;
San Mehat8b1c2ba2009-11-16 10:17:30 -0800963 msmsdcc_writel(host, status, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -0700964 }
965 spin_unlock_irqrestore(&host->lock, flags);
966}
967
968static const struct mmc_host_ops msmsdcc_ops = {
969 .request = msmsdcc_request,
970 .set_ios = msmsdcc_set_ios,
971 .enable_sdio_irq = msmsdcc_enable_sdio_irq,
972};
973
974static void
975msmsdcc_check_status(unsigned long data)
976{
977 struct msmsdcc_host *host = (struct msmsdcc_host *)data;
978 unsigned int status;
979
980 if (!host->plat->status) {
981 mmc_detect_change(host->mmc, 0);
982 goto out;
983 }
984
985 status = host->plat->status(mmc_dev(host->mmc));
986 host->eject = !status;
987 if (status ^ host->oldstat) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -0700988 pr_info("%s: Slot status change detected (%d -> %d)\n",
989 mmc_hostname(host->mmc), host->oldstat, status);
San Mehat9d2bd732009-09-22 16:44:22 -0700990 if (status)
991 mmc_detect_change(host->mmc, (5 * HZ) / 2);
992 else
993 mmc_detect_change(host->mmc, 0);
994 }
995
996 host->oldstat = status;
997
998out:
999 if (host->timer.function)
1000 mod_timer(&host->timer, jiffies + HZ);
1001}
1002
1003static irqreturn_t
1004msmsdcc_platform_status_irq(int irq, void *dev_id)
1005{
1006 struct msmsdcc_host *host = dev_id;
1007
1008 printk(KERN_DEBUG "%s: %d\n", __func__, irq);
1009 msmsdcc_check_status((unsigned long) host);
1010 return IRQ_HANDLED;
1011}
1012
1013static void
1014msmsdcc_status_notify_cb(int card_present, void *dev_id)
1015{
1016 struct msmsdcc_host *host = dev_id;
1017
1018 printk(KERN_DEBUG "%s: card_present %d\n", mmc_hostname(host->mmc),
1019 card_present);
1020 msmsdcc_check_status((unsigned long) host);
1021}
1022
San Mehat865c80642009-11-13 13:42:06 -08001023static void
1024msmsdcc_busclk_expired(unsigned long _data)
1025{
1026 struct msmsdcc_host *host = (struct msmsdcc_host *) _data;
San Mehat865c80642009-11-13 13:42:06 -08001027
San Mehat865c80642009-11-13 13:42:06 -08001028 if (host->clks_on)
San Mehatc7fc9372009-11-22 17:19:07 -08001029 msmsdcc_disable_clocks(host, 0);
San Mehat865c80642009-11-13 13:42:06 -08001030}
1031
San Mehat9d2bd732009-09-22 16:44:22 -07001032static int
1033msmsdcc_init_dma(struct msmsdcc_host *host)
1034{
1035 memset(&host->dma, 0, sizeof(struct msmsdcc_dma_data));
1036 host->dma.host = host;
1037 host->dma.channel = -1;
1038
1039 if (!host->dmares)
1040 return -ENODEV;
1041
1042 host->dma.nc = dma_alloc_coherent(NULL,
1043 sizeof(struct msmsdcc_nc_dmadata),
1044 &host->dma.nc_busaddr,
1045 GFP_KERNEL);
1046 if (host->dma.nc == NULL) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001047 pr_err("Unable to allocate DMA buffer\n");
San Mehat9d2bd732009-09-22 16:44:22 -07001048 return -ENOMEM;
1049 }
1050 memset(host->dma.nc, 0x00, sizeof(struct msmsdcc_nc_dmadata));
1051 host->dma.cmd_busaddr = host->dma.nc_busaddr;
1052 host->dma.cmdptr_busaddr = host->dma.nc_busaddr +
1053 offsetof(struct msmsdcc_nc_dmadata, cmdptr);
1054 host->dma.channel = host->dmares->start;
1055
1056 return 0;
1057}
1058
1059#ifdef CONFIG_MMC_MSM7X00A_RESUME_IN_WQ
1060static void
1061do_resume_work(struct work_struct *work)
1062{
1063 struct msmsdcc_host *host =
1064 container_of(work, struct msmsdcc_host, resume_task);
1065 struct mmc_host *mmc = host->mmc;
1066
1067 if (mmc) {
1068 mmc_resume_host(mmc);
1069 if (host->stat_irq)
1070 enable_irq(host->stat_irq);
1071 }
1072}
1073#endif
1074
1075static int
1076msmsdcc_probe(struct platform_device *pdev)
1077{
1078 struct mmc_platform_data *plat = pdev->dev.platform_data;
1079 struct msmsdcc_host *host;
1080 struct mmc_host *mmc;
1081 struct resource *cmd_irqres = NULL;
1082 struct resource *pio_irqres = NULL;
1083 struct resource *stat_irqres = NULL;
1084 struct resource *memres = NULL;
1085 struct resource *dmares = NULL;
1086 int ret;
1087
1088 /* must have platform data */
1089 if (!plat) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001090 pr_err("%s: Platform data not available\n", __func__);
San Mehat9d2bd732009-09-22 16:44:22 -07001091 ret = -EINVAL;
1092 goto out;
1093 }
1094
1095 if (pdev->id < 1 || pdev->id > 4)
1096 return -EINVAL;
1097
1098 if (pdev->resource == NULL || pdev->num_resources < 2) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001099 pr_err("%s: Invalid resource\n", __func__);
San Mehat9d2bd732009-09-22 16:44:22 -07001100 return -ENXIO;
1101 }
1102
1103 memres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1104 dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1105 cmd_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1106 "cmd_irq");
1107 pio_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1108 "pio_irq");
1109 stat_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1110 "status_irq");
1111
1112 if (!cmd_irqres || !pio_irqres || !memres) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001113 pr_err("%s: Invalid resource\n", __func__);
San Mehat9d2bd732009-09-22 16:44:22 -07001114 return -ENXIO;
1115 }
1116
1117 /*
1118 * Setup our host structure
1119 */
1120
1121 mmc = mmc_alloc_host(sizeof(struct msmsdcc_host), &pdev->dev);
1122 if (!mmc) {
1123 ret = -ENOMEM;
1124 goto out;
1125 }
1126
1127 host = mmc_priv(mmc);
1128 host->pdev_id = pdev->id;
1129 host->plat = plat;
1130 host->mmc = mmc;
San Mehat56a8b5b2009-11-21 12:29:46 -08001131 host->curr.cmd = NULL;
San Mehat9d2bd732009-09-22 16:44:22 -07001132
1133 host->cmdpoll = 1;
1134
1135 host->base = ioremap(memres->start, PAGE_SIZE);
1136 if (!host->base) {
1137 ret = -ENOMEM;
1138 goto out;
1139 }
1140
1141 host->cmd_irqres = cmd_irqres;
1142 host->pio_irqres = pio_irqres;
1143 host->memres = memres;
1144 host->dmares = dmares;
1145 spin_lock_init(&host->lock);
1146
San Mehat1cd22962010-02-03 12:59:29 -08001147#ifdef CONFIG_MMC_EMBEDDED_SDIO
1148 if (plat->embedded_sdio)
1149 mmc_set_embedded_sdio_data(mmc,
1150 &plat->embedded_sdio->cis,
1151 &plat->embedded_sdio->cccr,
1152 plat->embedded_sdio->funcs,
1153 plat->embedded_sdio->num_funcs);
1154#endif
1155
San Mehat9d2bd732009-09-22 16:44:22 -07001156 /*
1157 * Setup DMA
1158 */
1159 msmsdcc_init_dma(host);
1160
San Mehat4adbbcc2009-11-08 13:00:37 -08001161 /* Get our clocks */
San Mehat9d2bd732009-09-22 16:44:22 -07001162 host->pclk = clk_get(&pdev->dev, "sdc_pclk");
1163 if (IS_ERR(host->pclk)) {
1164 ret = PTR_ERR(host->pclk);
1165 goto host_free;
1166 }
1167
San Mehat9d2bd732009-09-22 16:44:22 -07001168 host->clk = clk_get(&pdev->dev, "sdc_clk");
1169 if (IS_ERR(host->clk)) {
1170 ret = PTR_ERR(host->clk);
San Mehat4adbbcc2009-11-08 13:00:37 -08001171 goto pclk_put;
San Mehat9d2bd732009-09-22 16:44:22 -07001172 }
1173
San Mehat4adbbcc2009-11-08 13:00:37 -08001174 /* Enable clocks */
San Mehatc7fc9372009-11-22 17:19:07 -08001175 ret = msmsdcc_enable_clocks(host);
San Mehat9d2bd732009-09-22 16:44:22 -07001176 if (ret)
1177 goto clk_put;
1178
1179 ret = clk_set_rate(host->clk, msmsdcc_fmin);
1180 if (ret) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001181 pr_err("%s: Clock rate set failed (%d)\n", __func__, ret);
San Mehat9d2bd732009-09-22 16:44:22 -07001182 goto clk_disable;
1183 }
1184
San Mehat4adbbcc2009-11-08 13:00:37 -08001185 host->pclk_rate = clk_get_rate(host->pclk);
San Mehat9d2bd732009-09-22 16:44:22 -07001186 host->clk_rate = clk_get_rate(host->clk);
1187
San Mehat9d2bd732009-09-22 16:44:22 -07001188 /*
1189 * Setup MMC host structure
1190 */
1191 mmc->ops = &msmsdcc_ops;
1192 mmc->f_min = msmsdcc_fmin;
1193 mmc->f_max = msmsdcc_fmax;
1194 mmc->ocr_avail = plat->ocr_mask;
1195
1196 if (msmsdcc_4bit)
1197 mmc->caps |= MMC_CAP_4_BIT_DATA;
1198 if (msmsdcc_sdioirq)
1199 mmc->caps |= MMC_CAP_SDIO_IRQ;
1200 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
1201
1202 mmc->max_phys_segs = NR_SG;
1203 mmc->max_hw_segs = NR_SG;
1204 mmc->max_blk_size = 4096; /* MCI_DATA_CTL BLOCKSIZE up to 4096 */
1205 mmc->max_blk_count = 65536;
1206
1207 mmc->max_req_size = 33554432; /* MCI_DATA_LENGTH is 25 bits */
1208 mmc->max_seg_size = mmc->max_req_size;
1209
San Mehat8b1c2ba2009-11-16 10:17:30 -08001210 msmsdcc_writel(host, 0, MMCIMASK0);
1211 msmsdcc_writel(host, 0x5e007ff, MMCICLEAR);
San Mehat9d2bd732009-09-22 16:44:22 -07001212
San Mehat8b1c2ba2009-11-16 10:17:30 -08001213 msmsdcc_writel(host, MCI_IRQENABLE, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -07001214 host->saved_irq0mask = MCI_IRQENABLE;
1215
1216 /*
1217 * Setup card detect change
1218 */
1219
1220 memset(&host->timer, 0, sizeof(host->timer));
1221
1222 if (stat_irqres && !(stat_irqres->flags & IORESOURCE_DISABLED)) {
1223 unsigned long irqflags = IRQF_SHARED |
1224 (stat_irqres->flags & IRQF_TRIGGER_MASK);
1225
1226 host->stat_irq = stat_irqres->start;
1227 ret = request_irq(host->stat_irq,
1228 msmsdcc_platform_status_irq,
1229 irqflags,
1230 DRIVER_NAME " (slot)",
1231 host);
1232 if (ret) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001233 pr_err("%s: Unable to get slot IRQ %d (%d)\n",
1234 mmc_hostname(mmc), host->stat_irq, ret);
San Mehat9d2bd732009-09-22 16:44:22 -07001235 goto clk_disable;
1236 }
1237 } else if (plat->register_status_notify) {
1238 plat->register_status_notify(msmsdcc_status_notify_cb, host);
1239 } else if (!plat->status)
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001240 pr_err("%s: No card detect facilities available\n",
San Mehat9d2bd732009-09-22 16:44:22 -07001241 mmc_hostname(mmc));
1242 else {
1243 init_timer(&host->timer);
1244 host->timer.data = (unsigned long)host;
1245 host->timer.function = msmsdcc_check_status;
1246 host->timer.expires = jiffies + HZ;
1247 add_timer(&host->timer);
1248 }
1249
1250 if (plat->status) {
1251 host->oldstat = host->plat->status(mmc_dev(host->mmc));
1252 host->eject = !host->oldstat;
1253 }
1254
San Mehat865c80642009-11-13 13:42:06 -08001255 init_timer(&host->busclk_timer);
1256 host->busclk_timer.data = (unsigned long) host;
1257 host->busclk_timer.function = msmsdcc_busclk_expired;
1258
San Mehat9d2bd732009-09-22 16:44:22 -07001259 ret = request_irq(cmd_irqres->start, msmsdcc_irq, IRQF_SHARED,
1260 DRIVER_NAME " (cmd)", host);
1261 if (ret)
1262 goto stat_irq_free;
1263
1264 ret = request_irq(pio_irqres->start, msmsdcc_pio_irq, IRQF_SHARED,
1265 DRIVER_NAME " (pio)", host);
1266 if (ret)
1267 goto cmd_irq_free;
1268
1269 mmc_set_drvdata(pdev, mmc);
1270 mmc_add_host(mmc);
1271
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001272 pr_info("%s: Qualcomm MSM SDCC at 0x%016llx irq %d,%d dma %d\n",
1273 mmc_hostname(mmc), (unsigned long long)memres->start,
1274 (unsigned int) cmd_irqres->start,
1275 (unsigned int) host->stat_irq, host->dma.channel);
1276 pr_info("%s: 4 bit data mode %s\n", mmc_hostname(mmc),
1277 (mmc->caps & MMC_CAP_4_BIT_DATA ? "enabled" : "disabled"));
1278 pr_info("%s: MMC clock %u -> %u Hz, PCLK %u Hz\n",
1279 mmc_hostname(mmc), msmsdcc_fmin, msmsdcc_fmax, host->pclk_rate);
1280 pr_info("%s: Slot eject status = %d\n", mmc_hostname(mmc), host->eject);
1281 pr_info("%s: Power save feature enable = %d\n",
1282 mmc_hostname(mmc), msmsdcc_pwrsave);
San Mehat9d2bd732009-09-22 16:44:22 -07001283
1284 if (host->dma.channel != -1) {
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001285 pr_info("%s: DM non-cached buffer at %p, dma_addr 0x%.8x\n",
1286 mmc_hostname(mmc), host->dma.nc, host->dma.nc_busaddr);
1287 pr_info("%s: DM cmd busaddr 0x%.8x, cmdptr busaddr 0x%.8x\n",
1288 mmc_hostname(mmc), host->dma.cmd_busaddr,
1289 host->dma.cmdptr_busaddr);
San Mehat9d2bd732009-09-22 16:44:22 -07001290 } else
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001291 pr_info("%s: PIO transfer enabled\n", mmc_hostname(mmc));
San Mehat9d2bd732009-09-22 16:44:22 -07001292 if (host->timer.function)
Joe Perches0a7ff7c2009-09-22 16:44:23 -07001293 pr_info("%s: Polling status mode enabled\n", mmc_hostname(mmc));
San Mehat9d2bd732009-09-22 16:44:22 -07001294
San Mehatf4748492009-11-23 15:36:31 -08001295#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -08001296 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -08001297#endif
San Mehat9d2bd732009-09-22 16:44:22 -07001298 return 0;
1299 cmd_irq_free:
1300 free_irq(cmd_irqres->start, host);
1301 stat_irq_free:
1302 if (host->stat_irq)
1303 free_irq(host->stat_irq, host);
1304 clk_disable:
San Mehatc7fc9372009-11-22 17:19:07 -08001305 msmsdcc_disable_clocks(host, 0);
San Mehat9d2bd732009-09-22 16:44:22 -07001306 clk_put:
1307 clk_put(host->clk);
San Mehat9d2bd732009-09-22 16:44:22 -07001308 pclk_put:
1309 clk_put(host->pclk);
1310 host_free:
1311 mmc_free_host(mmc);
1312 out:
1313 return ret;
1314}
1315
1316static int
1317msmsdcc_suspend(struct platform_device *dev, pm_message_t state)
1318{
1319 struct mmc_host *mmc = mmc_get_drvdata(dev);
1320 int rc = 0;
1321
1322 if (mmc) {
1323 struct msmsdcc_host *host = mmc_priv(mmc);
1324
1325 if (host->stat_irq)
1326 disable_irq(host->stat_irq);
1327
1328 if (mmc->card && mmc->card->type != MMC_TYPE_SDIO)
1329 rc = mmc_suspend_host(mmc, state);
San Mehatd0719e52009-12-03 10:58:54 -08001330 if (!rc)
San Mehat8b1c2ba2009-11-16 10:17:30 -08001331 msmsdcc_writel(host, 0, MMCIMASK0);
San Mehatc7fc9372009-11-22 17:19:07 -08001332 if (host->clks_on)
1333 msmsdcc_disable_clocks(host, 0);
San Mehat9d2bd732009-09-22 16:44:22 -07001334 }
1335 return rc;
1336}
1337
1338static int
1339msmsdcc_resume(struct platform_device *dev)
1340{
1341 struct mmc_host *mmc = mmc_get_drvdata(dev);
San Mehat9d2bd732009-09-22 16:44:22 -07001342
1343 if (mmc) {
1344 struct msmsdcc_host *host = mmc_priv(mmc);
1345
San Mehatc7fc9372009-11-22 17:19:07 -08001346 msmsdcc_enable_clocks(host);
San Mehat56a8b5b2009-11-21 12:29:46 -08001347
San Mehat8b1c2ba2009-11-16 10:17:30 -08001348 msmsdcc_writel(host, host->saved_irq0mask, MMCIMASK0);
San Mehat9d2bd732009-09-22 16:44:22 -07001349
San Mehat9d2bd732009-09-22 16:44:22 -07001350 if (mmc->card && mmc->card->type != MMC_TYPE_SDIO)
1351 mmc_resume_host(mmc);
Roel Kluin5b8a2fb2010-01-17 20:25:36 +01001352 if (host->stat_irq)
San Mehat9d2bd732009-09-22 16:44:22 -07001353 enable_irq(host->stat_irq);
San Mehatf4748492009-11-23 15:36:31 -08001354#if BUSCLK_PWRSAVE
San Mehatc7fc9372009-11-22 17:19:07 -08001355 msmsdcc_disable_clocks(host, 1);
San Mehatf4748492009-11-23 15:36:31 -08001356#endif
San Mehat9d2bd732009-09-22 16:44:22 -07001357 }
1358 return 0;
1359}
1360
1361static struct platform_driver msmsdcc_driver = {
1362 .probe = msmsdcc_probe,
1363 .suspend = msmsdcc_suspend,
1364 .resume = msmsdcc_resume,
1365 .driver = {
1366 .name = "msm_sdcc",
1367 },
1368};
1369
1370static int __init msmsdcc_init(void)
1371{
1372 return platform_driver_register(&msmsdcc_driver);
1373}
1374
1375static void __exit msmsdcc_exit(void)
1376{
1377 platform_driver_unregister(&msmsdcc_driver);
1378}
1379
1380module_init(msmsdcc_init);
1381module_exit(msmsdcc_exit);
1382
1383MODULE_DESCRIPTION("Qualcomm MSM 7X00A Multimedia Card Interface driver");
1384MODULE_LICENSE("GPL");