11 #include <linux/module.h>
15 #include <linux/device.h>
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
35 #include <linux/types.h>
37 #include <asm/div64.h>
39 #include <asm/sizes.h>
43 #define DRIVER_NAME "mmci-pl18x"
45 static unsigned int fmax = 515633;
77 .fifohalfsize = 8 * 4,
78 .datalength_bits = 16,
84 .fifohalfsize = 64 * 4,
85 .datalength_bits = 16,
91 .fifohalfsize = 8 * 4,
93 .datalength_bits = 16,
96 .signal_direction =
true,
101 .fifohalfsize = 8 * 4,
103 .datalength_bits = 24,
107 .signal_direction =
true,
112 .fifohalfsize = 8 * 4,
115 .datalength_bits = 24,
119 .signal_direction =
true,
124 .fifohalfsize = 8 * 4,
127 .datalength_bits = 24,
130 .blksz_datactrl16 =
true,
132 .signal_direction =
true,
149 static void mmci_write_pwrreg(
struct mmci_host *host,
u32 pwr)
160 static void mmci_set_clkreg(
struct mmci_host *host,
unsigned int desired)
166 if (desired >= host->
mclk) {
181 host->
cclk = host->
mclk / (clk + 2);
187 clk = host->
mclk / (2 * desired) - 1;
190 host->
cclk = host->
mclk / (2 * (clk + 1));
204 mmci_write_clkreg(host, clk);
219 pm_runtime_mark_last_busy(
mmc_dev(host->
mmc));
220 pm_runtime_put_autosuspend(
mmc_dev(host->
mmc));
223 static void mmci_set_mask1(
struct mmci_host *host,
unsigned int mask)
239 static void mmci_stop_data(
struct mmci_host *host)
242 mmci_set_mask1(host, 0);
263 #ifdef CONFIG_DMA_ENGINE
267 const char *rxname, *txname;
276 host->next_data.cookie = 1;
292 if (!host->dma_rx_channel)
300 if (!host->dma_tx_channel)
303 host->dma_tx_channel = host->dma_rx_channel;
306 if (host->dma_rx_channel)
307 rxname = dma_chan_name(host->dma_rx_channel);
311 if (host->dma_tx_channel)
312 txname = dma_chan_name(host->dma_tx_channel);
323 if (host->dma_tx_channel) {
324 struct device *
dev = host->dma_tx_channel->device->dev;
325 unsigned int max_seg_size = dma_get_max_seg_size(dev);
327 if (max_seg_size < host->mmc->max_seg_size)
328 host->
mmc->max_seg_size = max_seg_size;
330 if (host->dma_rx_channel) {
331 struct device *dev = host->dma_rx_channel->device->dev;
332 unsigned int max_seg_size = dma_get_max_seg_size(dev);
334 if (max_seg_size < host->mmc->max_seg_size)
335 host->
mmc->max_seg_size = max_seg_size;
343 static inline void mmci_dma_release(
struct mmci_host *host)
347 if (host->dma_rx_channel)
351 host->dma_rx_channel = host->dma_tx_channel =
NULL;
376 dmaengine_terminate_all(chan);
394 if (status & MCI_RXDATAAVLBLMASK) {
396 mmci_dma_release(host);
400 static void mmci_dma_data_error(
struct mmci_host *host)
403 dmaengine_terminate_all(host->dma_current);
427 host->dma_current && host->dma_desc_current)
431 host->dma_current =
NULL;
432 host->dma_desc_current =
NULL;
438 chan = host->dma_rx_channel;
442 chan = host->dma_tx_channel;
458 dmaengine_slave_config(chan, &conf);
459 desc = dmaengine_prep_slave_sg(chan, data->
sg, nr_sg,
468 host->dma_current =
chan;
469 host->dma_desc_current = desc;
476 dmaengine_terminate_all(chan);
481 static int mmci_dma_start_data(
struct mmci_host *host,
unsigned int datactrl)
486 ret = mmci_dma_prep_data(host, host->
data,
NULL);
492 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
494 dmaengine_submit(host->dma_desc_current);
495 dma_async_issue_pending(host->dma_current);
517 pr_warning(
"[%s] invalid cookie: data->host_cookie %d"
518 " host->next_data.cookie %d\n",
519 __func__, data->
host_cookie, host->next_data.cookie);
526 host->dma_desc_current = next->
dma_desc;
551 if (mmci_dma_prep_data(host, data, nd))
571 chan = host->dma_rx_channel;
574 chan = host->dma_tx_channel;
581 dmaengine_terminate_all(chan);
585 mrq->
data->host_cookie = 0;
594 static inline void mmci_dma_setup(
struct mmci_host *host)
598 static inline void mmci_dma_release(
struct mmci_host *host)
606 static inline void mmci_dma_data_error(
struct mmci_host *host)
610 static inline int mmci_dma_start_data(
struct mmci_host *host,
unsigned int datactrl)
615 #define mmci_pre_request NULL
616 #define mmci_post_request NULL
623 unsigned int datactrl,
timeout, irqmask;
624 unsigned long long clks;
656 if (variant->
sdio && host->
mmc->card)
664 if (!mmci_dma_start_data(host, datactrl))
668 mmci_init_sg(host, data);
690 mmci_set_mask1(host, irqmask);
732 mmci_dma_data_error(host);
757 if (success > host->
variant->fifosize)
758 success -= host->
variant->fifosize;
771 mmci_dma_unmap(host, data);
772 mmci_stop_data(host);
779 mmci_request_end(host, data->
mrq);
781 mmci_start_command(host, data->
stop, 0);
809 mmci_dma_data_error(host);
810 mmci_stop_data(host);
812 mmci_request_end(host, cmd->
mrq);
814 mmci_start_data(host, cmd->
data);
818 static int mmci_pio_read(
struct mmci_host *host,
char *
buffer,
unsigned int remain)
823 int host_remain = host->
size;
842 unsigned char buf[4];
855 host_remain -=
count;
866 static int mmci_pio_write(
struct mmci_host *host,
char *buffer,
unsigned int remain,
u32 status)
873 unsigned int count, maxcnt;
877 count =
min(remain, maxcnt);
892 mmci_write_clkreg(host, clk);
936 unsigned int remain,
len;
952 buffer = sg_miter->
addr;
953 remain = sg_miter->
length;
957 len = mmci_pio_read(host, buffer, remain);
959 len = mmci_pio_write(host, buffer, remain, status);
981 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
989 if (host->
size == 0) {
990 mmci_set_mask1(host, 0);
1000 static irqreturn_t mmci_irq(
int irq,
void *dev_id)
1006 spin_lock(&host->
lock);
1016 mmci_pio_irq(irq, dev_id);
1027 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_STARTBITERR|
1029 MCI_DATABLOCKEND) && data)
1030 mmci_data_irq(host, data, status);
1034 mmci_cmd_irq(host, cmd, status);
1039 spin_unlock(&host->
lock);
1047 unsigned long flags;
1059 pm_runtime_get_sync(
mmc_dev(mmc));
1066 mmci_get_next_data(host, mrq->
data);
1069 mmci_start_data(host, mrq->
data);
1071 mmci_start_command(host, mrq->
cmd, 0);
1073 spin_unlock_irqrestore(&host->
lock, flags);
1081 unsigned long flags;
1084 pm_runtime_get_sync(
mmc_dev(mmc));
1086 if (host->
plat->ios_handler &&
1093 ret = mmc_regulator_set_ocr(mmc, host->
vcc, 0);
1097 ret = mmc_regulator_set_ocr(mmc, host->
vcc, ios->
vdd);
1128 pwr |= host->
plat->sigdir;
1152 mmci_set_clkreg(host, ios->
clock);
1153 mmci_write_pwrreg(host, pwr);
1155 spin_unlock_irqrestore(&host->
lock, flags);
1158 pm_runtime_mark_last_busy(
mmc_dev(mmc));
1159 pm_runtime_put_autosuspend(
mmc_dev(mmc));
1162 static int mmci_get_ro(
struct mmc_host *mmc)
1172 static int mmci_get_cd(
struct mmc_host *mmc)
1194 static irqreturn_t mmci_cd_irq(
int irq,
void *dev_id)
1204 .request = mmci_request,
1207 .set_ios = mmci_set_ios,
1208 .get_ro = mmci_get_ro,
1209 .get_cd = mmci_get_cd,
1213 static void mmci_dt_populate_generic_pdata(
struct device_node *np,
1218 pdata->
gpio_wp = of_get_named_gpio(np,
"wp-gpios", 0);
1219 pdata->
gpio_cd = of_get_named_gpio(np,
"cd-gpios", 0);
1226 of_property_read_u32(np,
"max-frequency", &pdata->
f_max);
1235 of_property_read_u32(np,
"bus-width", &bus_width);
1236 switch (bus_width) {
1251 static void mmci_dt_populate_generic_pdata(
struct device_node *np,
1270 dev_err(&dev->
dev,
"No plat data or DT found\n");
1281 mmci_dt_populate_generic_pdata(np, plat);
1293 host = mmc_priv(mmc);
1306 if (IS_ERR(host->
clk)) {
1307 ret = PTR_ERR(host->
clk);
1312 ret = clk_prepare_enable(host->
clk);
1324 if (host->
mclk > 100000000) {
1339 mmc->
ops = &mmci_ops;
1363 #ifdef CONFIG_REGULATOR
1366 if (IS_ERR(host->
vcc))
1369 int mask = mmc_regulator_get_ocrmask(host->
vcc);
1372 dev_err(&dev->
dev,
"error getting OCR mask (%d)\n",
1375 host->
mmc->ocr_avail = (
u32) mask;
1378 "Provided ocr_mask/setpower will not be used "
1379 "(using regulator instead)\n");
1428 if (gpio_is_valid(plat->
gpio_cd)) {
1455 if (gpio_is_valid(plat->
gpio_wp)) {
1486 dev_info(&dev->
dev,
"%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
1488 amba_rev(dev), (
unsigned long long)dev->
res.start,
1489 dev->
irq[0], dev->
irq[1]);
1491 mmci_dma_setup(host);
1494 pm_runtime_use_autosuspend(&dev->
dev);
1495 pm_runtime_put(&dev->
dev);
1514 clk_disable_unprepare(host->
clk);
1538 pm_runtime_get_sync(&dev->
dev);
1548 mmci_dma_release(host);
1561 clk_disable_unprepare(host->
clk);
1565 mmc_regulator_set_ocr(mmc, host->
vcc, 0);
1576 #ifdef CONFIG_SUSPEND
1577 static int mmci_suspend(
struct device *dev)
1588 pm_runtime_get_sync(dev);
1596 static int mmci_resume(
struct device *dev)
1606 pm_runtime_put(dev);
1615 static const struct dev_pm_ops mmci_dev_pm_ops = {
1619 static struct amba_id mmci_ids[] = {
1623 .data = &variant_arm,
1628 .data = &variant_arm_extended_fifo,
1633 .data = &variant_arm,
1639 .data = &variant_u300,
1644 .data = &variant_nomadik,
1649 .data = &variant_u300,
1654 .data = &variant_ux500,
1659 .data = &variant_ux500v2,
1669 .pm = &mmci_dev_pm_ops,
1671 .probe = mmci_probe,
1673 .id_table = mmci_ids,