18 #include <linux/module.h>
22 #include <linux/device.h>
41 #include <asm/cacheflush.h>
42 #include <asm/div64.h>
43 #include <asm/sizes.h>
52 #define DRIVER_NAME "msm-sdcc"
54 #define BUSCLK_PWRSAVE 1
55 #define BUSCLK_TIMEOUT (HZ)
56 static unsigned int msmsdcc_fmin = 144000;
57 static unsigned int msmsdcc_fmax = 50000000;
58 static unsigned int msmsdcc_4bit = 1;
59 static unsigned int msmsdcc_pwrsave = 1;
60 static unsigned int msmsdcc_piopoll = 1;
61 static unsigned int msmsdcc_sdioirq;
63 #define PIO_SPINMAX 30
64 #define CMD_SPINMAX 20
112 static inline unsigned int
131 static void msmsdcc_reset_and_restore(
struct msmsdcc_host *host)
144 pr_err(
"%s: Clock assert failed at %u Hz with err %d\n",
149 pr_err(
"%s: Clock deassert failed at %u Hz with err %d\n",
152 pr_info(
"%s: Controller has been re-initialiazed\n",
161 pr_err(
"%s: Failed to set clk rate %u Hz (%d)\n",
174 mrq->
data->bytes_xfered = host->
curr.data_xfered;
179 msmsdcc_disable_clocks(host, 1);
185 spin_unlock(&host->
lock);
187 spin_lock(&host->
lock);
194 host->
curr.got_dataend = 0;
214 msmsdcc_writel(host, (
unsigned int)host->
curr.xfer_size,
216 msmsdcc_writel(host, (msmsdcc_readl(host,
MMCIMASK0) &
221 msmsdcc_start_command_exec(host,
225 host->
dma.active = 1;
229 msmsdcc_dma_complete_tlet(
unsigned long data)
237 host->
dma.active = 0;
240 mrq = host->
curr.mrq;
245 pr_err(
"msmsdcc: Invalid DataMover result\n");
250 host->
curr.data_xfered = host->
curr.xfer_size;
254 pr_err(
"%s: DMA error (0x%.8x)\n",
257 pr_err(
"%s: DMA channel flushed (0x%.8x)\n",
260 pr_err(
"Flush data: %.8x %.8x %.8x %.8x %.8x %.8x\n",
261 err.flush[0],
err.flush[1],
err.flush[2],
262 err.flush[3],
err.flush[4],
err.flush[5]);
264 msmsdcc_reset_and_restore(host);
265 if (!mrq->
data->error)
274 if (host->
curr.got_dataend || mrq->
data->error) {
280 msmsdcc_stop_data(host);
282 if (!mrq->
data->error)
283 host->
curr.data_xfered = host->
curr.xfer_size;
284 if (!mrq->
data->stop || mrq->
cmd->error) {
287 mrq->
data->bytes_xfered = host->
curr.data_xfered;
289 spin_unlock_irqrestore(&host->
lock, flags);
291 msmsdcc_disable_clocks(host, 1);
296 msmsdcc_start_command(host, mrq->
data->stop, 0);
300 spin_unlock_irqrestore(&host->
lock, flags);
322 if (host->
dma.channel == -1)
342 rc = validate_dma(host, data);
346 host->
dma.sg = data->
sg;
368 host->
dma.num_ents = 0;
377 host->
curr.user_pages = 0;
387 host->
dma.hdr.complete_func = msmsdcc_dma_complete_func;
390 host->
dma.num_ents, host->
dma.dir);
392 pr_err(
"%s: Unable to map in all sg elements\n",
395 host->
dma.num_ents = 0;
417 box->
num_rows = rows * ((1 << 16) + 1);
427 box->
num_rows = rows * ((1 << 16) + 1);
439 if ((cmd->
opcode == 52) &&
440 (cmd->
arg & 0x80000000) &&
447 msmsdcc_start_command_deferred(
struct msmsdcc_host *host,
471 if (cmd == cmd->
mrq->stop)
474 if (snoop_cccr_abort(cmd))
478 pr_err(
"%s: Overlapping command requests\n",
488 unsigned int datactrl,
timeout;
489 unsigned long long clks;
490 unsigned int pio_irqmask = 0;
494 host->
curr.xfer_remain = host->
curr.xfer_size;
495 host->
curr.data_xfered = 0;
496 host->
curr.got_dataend = 0;
502 if (!msmsdcc_config_dma(host, data))
505 host->
pio.sg = data->
sg;
507 host->
pio.sg_off = 0;
531 host->
dma.hdr.execute_func = msmsdcc_dma_exec_func;
532 host->
dma.hdr.data = (
void *)host;
536 msmsdcc_start_command_deferred(host, cmd, &c);
547 msmsdcc_writel(host, (msmsdcc_readl(host,
MMCIMASK0) &
554 msmsdcc_start_command(host, cmd, c);
562 if (cmd == cmd->
mrq->stop)
567 msmsdcc_start_command_deferred(host, cmd, &c);
568 msmsdcc_start_command_exec(host, cmd->
arg, c);
577 pr_err(
"%s: opcode 0x%.8x\n", __func__,
578 data->
mrq->cmd->opcode);
579 pr_err(
"%s: blksz %d, blocks %d\n", __func__,
592 pr_err(
"%s: Unknown error (0x%.8x)\n",
606 remain = ((remain >> 2) + 1) << 2;
621 msmsdcc_pio_write(
struct msmsdcc_host *host,
char *buffer,
622 unsigned int remain,
u32 status)
628 unsigned int count, maxcnt, sz;
632 count =
min(remain, maxcnt);
634 sz = count % 4 ? (count >> 2) + 1 : (count >> 2);
661 msmsdcc_pio_irq(
int irq,
void *
dev_id)
668 mci_mask0 = msmsdcc_readl(host,
MMCIMASK0);
675 unsigned int remain,
len;
679 if (host->
curr.xfer_remain == 0 || !msmsdcc_piopoll)
682 if (msmsdcc_spin_on_status(host,
693 + host->
pio.sg->offset;
694 buffer += host->
pio.sg_off;
695 remain = host->
pio.sg->length - host->
pio.sg_off;
698 len = msmsdcc_pio_read(host, buffer, remain);
700 len = msmsdcc_pio_write(host, buffer, remain, status);
706 host->
pio.sg_off += len;
707 host->
curr.xfer_remain -= len;
708 host->
curr.data_xfered += len;
713 if (status & MCI_RXACTIVE && host->
curr.user_pages)
716 if (!--host->
pio.sg_len) {
723 host->
pio.sg_off = 0;
730 msmsdcc_writel(host, (mci_mask0 & (~MCI_IRQ_PIO)) |
733 if (!host->
curr.xfer_remain)
734 msmsdcc_writel(host, (mci_mask0 & (~MCI_IRQ_PIO)) | 0,
759 if (host->
curr.data && host->
dma.sg)
762 else if (host->
curr.data) {
763 msmsdcc_reset_and_restore(host);
764 msmsdcc_stop_data(host);
765 msmsdcc_request_end(host, cmd->
mrq);
771 msmsdcc_request_end(host, cmd->
mrq);
780 msmsdcc_request_end(host, cmd->
mrq);
783 }
else if (cmd->
data)
785 msmsdcc_start_data(host, cmd->
data,
797 msmsdcc_do_cmdirq(host, status);
804 if (status & (MCI_DATACRCFAIL | MCI_DATATIMEOUT |
805 MCI_TXUNDERRUN | MCI_RXOVERRUN)) {
806 msmsdcc_data_err(host, data, status);
807 host->
curr.data_xfered = 0;
812 msmsdcc_reset_and_restore(host);
814 msmsdcc_stop_data(host);
816 msmsdcc_request_end(host, data->
mrq);
818 msmsdcc_start_command(host, data->
stop, 0);
824 host->
curr.got_dataend = 1;
829 if (host->
curr.got_dataend && !host->
dma.busy) {
840 msmsdcc_pio_irq(1, host);
842 msmsdcc_stop_data(host);
844 host->
curr.data_xfered = host->
curr.xfer_size;
847 msmsdcc_request_end(host, data->
mrq);
849 msmsdcc_start_command(host, data->
stop, 0);
854 msmsdcc_irq(
int irq,
void *dev_id)
862 spin_lock(&host->
lock);
866 status &= msmsdcc_readl(host,
MMCIMASK0);
867 if ((status & (~MCI_IRQ_PIO)) == 0)
872 status &= ~MCI_SDIOINTR;
877 msmsdcc_handle_irq_data(host, status, base);
881 status &= ~MCI_SDIOINTOPER;
886 spin_unlock(&host->
lock);
893 mmc_signal_sdio_irq(host->
mmc);
914 mrq->
data->bytes_xfered = mrq->
data->blksz *
919 spin_unlock_irqrestore(&host->
lock, flags);
924 msmsdcc_enable_clocks(host);
926 host->
curr.mrq = mrq;
930 msmsdcc_start_data(host, mrq->
data, mrq->
cmd, 0);
932 msmsdcc_start_command(host, mrq->
cmd, 0);
934 if (host->
cmdpoll && !msmsdcc_spin_on_status(host,
938 msmsdcc_do_cmdirq(host, status);
942 host->
stats.cmdpoll_hits++;
944 host->
stats.cmdpoll_misses++;
946 spin_unlock_irqrestore(&host->
lock, flags);
957 curr = host->
plat->gpio_data;
958 for (i = 0; i < curr->
size; i++) {
963 pr_err(
"%s: gpio_request(%d, %s) failed %d\n",
966 curr->
gpio[i].name, rc);
991 msmsdcc_enable_clocks(host);
993 spin_unlock_irqrestore(&host->
lock, flags);
999 pr_err(
"%s: Error setting clock rate (%d)\n",
1010 if (ios->
clock > 400000 && msmsdcc_pwrsave)
1016 if (host->
plat->translate_vdd)
1021 msmsdcc_setup_gpio(host,
false);
1025 msmsdcc_setup_gpio(host,
true);
1043 msmsdcc_disable_clocks(host, 1);
1044 spin_unlock_irqrestore(&host->
lock, flags);
1048 static void msmsdcc_enable_sdio_irq(
struct mmc_host *mmc,
int enable)
1051 unsigned long flags;
1055 if (msmsdcc_sdioirq == 1) {
1056 status = msmsdcc_readl(host,
MMCIMASK0);
1062 msmsdcc_writel(host, status,
MMCIMASK0);
1064 spin_unlock_irqrestore(&host->
lock, flags);
1071 if (host->
plat->init_card)
1072 host->
plat->init_card(card);
1076 .request = msmsdcc_request,
1077 .set_ios = msmsdcc_set_ios,
1078 .enable_sdio_irq = msmsdcc_enable_sdio_irq,
1079 .init_card = msmsdcc_init_card,
1083 msmsdcc_check_status(
unsigned long data)
1088 if (!host->
plat->status) {
1096 pr_info(
"%s: Slot status change detected (%d -> %d)\n",
1107 if (host->
timer.function)
1112 msmsdcc_platform_status_irq(
int irq,
void *dev_id)
1116 pr_debug(
"%s: %d\n", __func__, irq);
1117 msmsdcc_check_status((
unsigned long) host);
1122 msmsdcc_status_notify_cb(
int card_present,
void *dev_id)
1128 msmsdcc_check_status((
unsigned long) host);
1132 msmsdcc_busclk_expired(
unsigned long _data)
1137 msmsdcc_disable_clocks(host, 0);
1144 host->
dma.host = host;
1145 host->
dma.channel = -1;
1152 &host->
dma.nc_busaddr,
1155 pr_err(
"Unable to allocate DMA buffer\n");
1159 host->
dma.cmd_busaddr = host->
dma.nc_busaddr;
1160 host->
dma.cmdptr_busaddr = host->
dma.nc_busaddr +
1162 host->
dma.channel = host->
dmares->start;
1181 pr_err(
"%s: Platform data not available\n", __func__);
1186 if (pdev->
id < 1 || pdev->
id > 4)
1190 pr_err(
"%s: Invalid resource\n", __func__);
1201 if (!cmd_irqres || !memres) {
1202 pr_err(
"%s: Invalid resource\n", __func__);
1216 host = mmc_priv(mmc);
1240 (
unsigned long)host);
1246 ret = msmsdcc_init_dma(host);
1250 host->
dma.channel = -1;
1255 if (IS_ERR(host->
pclk)) {
1256 ret = PTR_ERR(host->
pclk);
1261 if (IS_ERR(host->
clk)) {
1262 ret = PTR_ERR(host->
clk);
1268 pr_err(
"%s: Clock rate set failed (%d)\n", __func__, ret);
1273 ret = msmsdcc_enable_clocks(host);
1283 mmc->
ops = &msmsdcc_ops;
1284 mmc->
f_min = msmsdcc_fmin;
1285 mmc->
f_max = msmsdcc_fmax;
1290 if (msmsdcc_sdioirq)
1302 msmsdcc_writel(host, 0x5e007ff,
MMCICLEAR);
1319 msmsdcc_platform_status_irq,
1324 pr_err(
"%s: Unable to get slot IRQ %d (%d)\n",
1330 }
else if (!plat->
status)
1331 pr_err(
"%s: No card detect facilities available\n",
1336 host->
timer.function = msmsdcc_check_status;
1359 pr_info(
"%s: Qualcomm MSM SDCC at 0x%016llx irq %d,%d dma %d\n",
1361 (
unsigned int) cmd_irqres->
start,
1362 (
unsigned int) host->
stat_irq, host->
dma.channel);
1365 pr_info(
"%s: MMC clock %u -> %u Hz, PCLK %u Hz\n",
1368 pr_info(
"%s: Power save feature enable = %d\n",
1371 if (host->
dma.channel != -1) {
1372 pr_info(
"%s: DM non-cached buffer at %p, dma_addr 0x%.8x\n",
1374 pr_info(
"%s: DM cmd busaddr 0x%.8x, cmdptr busaddr 0x%.8x\n",
1376 host->
dma.cmdptr_busaddr);
1379 if (host->
timer.function)
1389 msmsdcc_disable_clocks(host, 0);
1397 host->
dma.nc, host->
dma.nc_busaddr);
1408 #ifdef CONFIG_MMC_MSM7X00A_RESUME_IN_WQ
1442 msmsdcc_disable_clocks(host, 0);
1455 msmsdcc_enable_clocks(host);
1464 msmsdcc_disable_clocks(host, 1);
1470 #define msmsdcc_suspend 0
1471 #define msmsdcc_resume 0
1475 .probe = msmsdcc_probe,