18 #include <linux/module.h>
20 #include <linux/kernel.h>
41 #include <mach/hardware.h>
46 #define OMAP_HSMMC_SYSSTATUS 0x0014
47 #define OMAP_HSMMC_CON 0x002C
48 #define OMAP_HSMMC_BLK 0x0104
49 #define OMAP_HSMMC_ARG 0x0108
50 #define OMAP_HSMMC_CMD 0x010C
51 #define OMAP_HSMMC_RSP10 0x0110
52 #define OMAP_HSMMC_RSP32 0x0114
53 #define OMAP_HSMMC_RSP54 0x0118
54 #define OMAP_HSMMC_RSP76 0x011C
55 #define OMAP_HSMMC_DATA 0x0120
56 #define OMAP_HSMMC_HCTL 0x0128
57 #define OMAP_HSMMC_SYSCTL 0x012C
58 #define OMAP_HSMMC_STAT 0x0130
59 #define OMAP_HSMMC_IE 0x0134
60 #define OMAP_HSMMC_ISE 0x0138
61 #define OMAP_HSMMC_CAPA 0x0140
63 #define VS18 (1 << 26)
64 #define VS30 (1 << 25)
65 #define SDVS18 (0x5 << 9)
66 #define SDVS30 (0x6 << 9)
67 #define SDVS33 (0x7 << 9)
68 #define SDVS_MASK 0x00000E00
69 #define SDVSCLR 0xFFFFF1FF
70 #define SDVSDET 0x00000400
77 #define CLKD_MASK 0x0000FFC0
79 #define DTO_MASK 0x000F0000
81 #define INT_EN_MASK 0x307F0033
82 #define BWR_ENABLE (1 << 4)
83 #define BRR_ENABLE (1 << 5)
84 #define DTO_ENABLE (1 << 20)
85 #define INIT_STREAM (1 << 1)
86 #define DP_SELECT (1 << 21)
91 #define FOUR_BIT (1 << 1)
98 #define CMD_TIMEOUT (1 << 16)
99 #define DATA_TIMEOUT (1 << 20)
100 #define CMD_CRC (1 << 17)
101 #define DATA_CRC (1 << 21)
102 #define CARD_ERR (1 << 28)
103 #define STAT_CLEAR 0xFFFFFFFF
104 #define INIT_STREAM_CMD 0x00000000
105 #define DUAL_VOLT_OCR_BIT 7
106 #define SRC (1 << 25)
107 #define SRD (1 << 26)
108 #define SOFTRESET (1 << 1)
109 #define RESETDONE (1 << 0)
111 #define MMC_AUTOSUSPEND_DELAY 100
112 #define MMC_TIMEOUT_MS 20
113 #define OMAP_MMC_MIN_CLOCK 400000
114 #define OMAP_MMC_MAX_CLOCK 52000000
115 #define DRIVER_NAME "omap_hsmmc"
122 #define mmc_slot(host) (host->pdata->slots[host->slot_id])
127 #define OMAP_HSMMC_READ(base, reg) \
128 __raw_readl((base) + OMAP_HSMMC_##reg)
130 #define OMAP_HSMMC_WRITE(base, reg, val) \
131 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
179 static int omap_hsmmc_card_detect(
struct device *
dev,
int slot)
197 static int omap_hsmmc_get_cover_state(
struct device *
dev,
int slot)
228 #define omap_hsmmc_suspend_cdirq NULL
229 #define omap_hsmmc_resume_cdirq NULL
233 #ifdef CONFIG_REGULATOR
235 static int omap_hsmmc_set_power(
struct device *
dev,
int slot,
int power_on,
257 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
273 ret = mmc_regulator_set_ocr(host->
mmc, host->
vcc, vdd);
275 if (ret == 0 && host->
vcc_aux) {
278 ret = mmc_regulator_set_ocr(host->
mmc,
287 ret = mmc_regulator_set_ocr(host->
mmc,
293 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
305 dev_dbg(host->
dev,
"vmmc regulator missing\n");
308 mmc_slot(host).set_power = omap_hsmmc_set_power;
310 ocr_value = mmc_regulator_get_ocrmask(reg);
312 mmc_slot(host).ocr_mask = ocr_value;
314 if (!(
mmc_slot(host).ocr_mask & ocr_value)) {
315 dev_err(host->
dev,
"ocrmask %x is not supported\n",
327 if (
mmc_slot(host).no_regulator_off_init)
358 static inline int omap_hsmmc_have_reg(
void)
374 static inline int omap_hsmmc_have_reg(
void)
385 if (gpio_is_valid(pdata->
slots[0].switch_pin)) {
386 if (pdata->
slots[0].cover)
387 pdata->
slots[0].get_cover_state =
388 omap_hsmmc_get_cover_state;
390 pdata->
slots[0].card_detect = omap_hsmmc_card_detect;
391 pdata->
slots[0].card_detect_irq =
402 if (gpio_is_valid(pdata->
slots[0].gpio_wp)) {
403 pdata->
slots[0].get_ro = omap_hsmmc_get_wp;
418 if (gpio_is_valid(pdata->
slots[0].switch_pin))
426 if (gpio_is_valid(pdata->
slots[0].gpio_wp))
428 if (gpio_is_valid(pdata->
slots[0].switch_pin))
455 unsigned int irq_mask;
500 omap_hsmmc_stop_clock(host);
504 regval = regval | (calc_divisor(host, ios) << 6) | (
DTO << 16);
515 omap_hsmmc_start_clock(host);
567 int context_loss = 0;
569 unsigned long timeout;
573 if (context_loss < 0)
611 omap_hsmmc_disable_irq(host);
617 omap_hsmmc_set_bus_width(host);
619 omap_hsmmc_set_clock(host);
621 omap_hsmmc_set_bus_mode(host);
640 if (context_loss < 0)
666 unsigned long timeout;
709 omap_hsmmc_cover_is_closed(host) ?
"closed" :
"open");
733 int cmdreg = 0, resptype = 0, cmdtype = 0;
739 omap_hsmmc_enable_irq(host, cmd);
757 if (cmd == host->
mrq->stop)
760 cmdreg = (cmd->
opcode << 24) | (resptype << 16) | (cmdtype << 22);
802 spin_unlock_irqrestore(&host->
irq_lock, flags);
804 omap_hsmmc_disable_irq(host);
822 if (host->
cmd && host->
cmd->opcode == 6 &&
828 omap_hsmmc_request_done(host, mrq);
840 omap_hsmmc_request_done(host, data->
mrq);
843 omap_hsmmc_start_command(host, data->
stop,
NULL);
867 omap_hsmmc_request_done(host, cmd->
mrq);
873 static void omap_hsmmc_dma_cleanup(
struct omap_hsmmc_host *host,
int errno)
878 host->
data->error = errno;
883 spin_unlock_irqrestore(&host->
irq_lock, flags);
885 if (host->
use_dma && dma_ch != -1) {
888 dmaengine_terminate_all(chan);
891 omap_hsmmc_get_dma_dir(host, host->
data));
893 host->
data->host_cookie = 0;
901 #ifdef CONFIG_MMC_DEBUG
905 static const char *omap_hsmmc_status_bits[] = {
906 "CC" ,
"TC" ,
"BGE",
"---",
"BWR" ,
"BRR" ,
"---" ,
"---" ,
907 "CIRQ",
"OBI" ,
"---",
"---",
"---" ,
"---" ,
"---" ,
"ERRI",
908 "CTO" ,
"CCRC",
"CEB",
"CIE",
"DTO" ,
"DCRC",
"DEB" ,
"---" ,
909 "ACE" ,
"---" ,
"---",
"---",
"CERR",
"BADA",
"---" ,
"---"
915 len =
sprintf(buf,
"MMC IRQ 0x%x :", status);
918 for (i = 0; i <
ARRAY_SIZE(omap_hsmmc_status_bits); i++)
919 if (status & (1 << i)) {
920 len =
sprintf(buf,
" %s", omap_hsmmc_status_bits[i]);
927 static inline void omap_hsmmc_dbg_report_irq(
struct omap_hsmmc_host *host,
940 static inline void omap_hsmmc_reset_controller_fsm(
struct omap_hsmmc_host *host,
967 "Timeout waiting on controller reset in %s\n",
973 omap_hsmmc_reset_controller_fsm(host,
SRC);
977 omap_hsmmc_reset_controller_fsm(host,
SRD);
978 omap_hsmmc_dma_cleanup(host, err);
983 static void omap_hsmmc_do_irq(
struct omap_hsmmc_host *host,
int status)
986 int end_cmd = 0, end_trans = 0;
992 omap_hsmmc_dbg_report_irq(host, status);
994 hsmmc_command_incomplete(host, -
ETIMEDOUT);
996 hsmmc_command_incomplete(host, -
EILSEQ);
1005 if (end_cmd || ((status &
CC) && host->
cmd))
1006 omap_hsmmc_cmd_done(host, host->
cmd);
1007 if ((end_trans || (status &
TC)) && host->
mrq)
1008 omap_hsmmc_xfer_done(host, data);
1021 omap_hsmmc_do_irq(host, status);
1051 static int omap_hsmmc_switch_opcond(
struct omap_hsmmc_host *host,
int vdd)
1057 pm_runtime_put_sync(host->
dev);
1059 clk_disable_unprepare(host->
dbclk);
1068 pm_runtime_get_sync(host->
dev);
1070 clk_prepare_enable(host->
dbclk);
1100 set_sd_bus_power(host);
1111 if (!
mmc_slot(host).get_cover_state)
1118 "card is now accessible\n",
1125 "card is now inaccessible\n",
1135 static irqreturn_t omap_hsmmc_detect(
int irq,
void *dev_id)
1146 if (slot->card_detect)
1147 carddetect = slot->card_detect(host->
dev, host->
slot_id);
1149 omap_hsmmc_protect_card(host);
1160 static void omap_hsmmc_dma_callback(
void *
param)
1165 int req_in_progress;
1173 data = host->
mrq->data;
1174 chan = omap_hsmmc_get_dma_chan(host, data);
1178 omap_hsmmc_get_dma_dir(host, data));
1185 if (!req_in_progress) {
1202 dev_warn(host->
dev,
"[%s] invalid cookie: data->host_cookie %d"
1203 " host->next_data.cookie %d\n",
1212 omap_hsmmc_get_dma_dir(host, data));
1235 static int omap_hsmmc_start_dma_transfer(
struct omap_hsmmc_host *host,
1245 for (i = 0; i < data->
sg_len; i++) {
1252 if ((data->
blksz % 4) != 0)
1260 chan = omap_hsmmc_get_dma_chan(host, data);
1266 cfg.src_maxburst = data->
blksz / 4;
1267 cfg.dst_maxburst = data->
blksz / 4;
1269 ret = dmaengine_slave_config(chan, &
cfg);
1273 ret = omap_hsmmc_pre_dma_transfer(host, data,
NULL, chan);
1277 tx = dmaengine_prep_slave_sg(chan, data->
sg, data->
sg_len,
1286 tx->
callback = omap_hsmmc_dma_callback;
1290 dmaengine_submit(tx);
1294 dma_async_issue_pending(chan);
1300 unsigned int timeout_ns,
1301 unsigned int timeout_clks)
1303 unsigned int timeout, cycle_ns;
1312 timeout = timeout_ns / cycle_ns;
1313 timeout += timeout_clks;
1315 while ((timeout & 0x80000000) == 0) {
1352 set_data_timeout(host, 100000000
U, 0);
1357 | (req->
data->blocks << 16));
1358 set_data_timeout(host, req->
data->timeout_ns, req->
data->timeout_clks);
1361 ret = omap_hsmmc_start_dma_transfer(host, req);
1377 struct dma_chan *
c = omap_hsmmc_get_dma_chan(host, data);
1380 omap_hsmmc_get_dma_dir(host, data));
1390 if (mrq->
data->host_cookie) {
1391 mrq->
data->host_cookie = 0;
1396 struct dma_chan *
c = omap_hsmmc_get_dma_chan(host, mrq->
data);
1398 if (omap_hsmmc_pre_dma_transfer(host, mrq->
data,
1400 mrq->
data->host_cookie = 0;
1421 omap_hsmmc_reset_controller_fsm(host,
SRD);
1422 omap_hsmmc_reset_controller_fsm(host,
SRC);
1428 req->
cmd->retries = 0;
1435 err = omap_hsmmc_prepare_data(host, req);
1445 omap_hsmmc_start_command(host, req->
cmd, req->
data);
1449 static void omap_hsmmc_set_ios(
struct mmc_host *mmc,
struct mmc_ios *ios)
1452 int do_send_init_stream = 0;
1454 pm_runtime_get_sync(host->
dev);
1467 do_send_init_stream = 1;
1475 omap_hsmmc_set_bus_width(host);
1488 !host->
dev->of_node) {
1495 if (omap_hsmmc_switch_opcond(host, ios->
vdd) != 0)
1497 "Switch operation failed\n");
1501 omap_hsmmc_set_clock(host);
1503 if (do_send_init_stream)
1504 send_init_stream(host);
1506 omap_hsmmc_set_bus_mode(host);
1508 pm_runtime_put_autosuspend(host->
dev);
1511 static int omap_hsmmc_get_cd(
struct mmc_host *mmc)
1520 static int omap_hsmmc_get_ro(
struct mmc_host *mmc)
1557 set_sd_bus_power(host);
1560 static int omap_hsmmc_enable_fclk(
struct mmc_host *mmc)
1564 pm_runtime_get_sync(host->
dev);
1569 static int omap_hsmmc_disable_fclk(
struct mmc_host *mmc)
1573 pm_runtime_mark_last_busy(host->
dev);
1574 pm_runtime_put_autosuspend(host->
dev);
1580 .enable = omap_hsmmc_enable_fclk,
1581 .disable = omap_hsmmc_disable_fclk,
1582 .post_req = omap_hsmmc_post_req,
1583 .pre_req = omap_hsmmc_pre_req,
1584 .request = omap_hsmmc_request,
1585 .set_ios = omap_hsmmc_set_ios,
1586 .get_cd = omap_hsmmc_get_cd,
1587 .get_ro = omap_hsmmc_get_ro,
1588 .init_card = omap_hsmmc_init_card,
1592 #ifdef CONFIG_DEBUG_FS
1594 static int omap_hsmmc_regs_show(
struct seq_file *
s,
void *data)
1598 int context_loss = 0;
1600 if (host->
pdata->get_context_loss_count)
1601 context_loss = host->
pdata->get_context_loss_count(host->
dev);
1603 seq_printf(s,
"mmc%d:\n ctx_loss:\t%d:%d\n\nregs:\n",
1607 seq_printf(s,
"host suspended, can't read registers\n");
1611 pm_runtime_get_sync(host->
dev);
1626 pm_runtime_mark_last_busy(host->
dev);
1627 pm_runtime_put_autosuspend(host->
dev);
1638 .
open = omap_hsmmc_regs_open,
1644 static void omap_hsmmc_debugfs(
struct mmc_host *mmc)
1648 mmc, &mmc_regs_fops);
1653 static void omap_hsmmc_debugfs(
struct mmc_host *mmc)
1660 static u16 omap4_reg_offset = 0x100;
1662 static const struct of_device_id omap_mmc_of_match[] = {
1667 .compatible =
"ti,omap3-hsmmc",
1670 .compatible =
"ti,omap4-hsmmc",
1671 .data = &omap4_reg_offset,
1692 pdata->
slots[0].switch_pin = of_get_named_gpio(np,
"cd-gpios", 0);
1693 pdata->
slots[0].gpio_wp = of_get_named_gpio(np,
"wp-gpios", 0);
1696 pdata->
slots[0].nonremovable =
true;
1697 pdata->
slots[0].no_regulator_off_init =
true;
1699 of_property_read_u32(np,
"bus-width", &bus_width);
1702 else if (bus_width == 8)
1712 *of_get_hsmmc_pdata(
struct device *dev)
1727 unsigned tx_req, rx_req;
1731 pdata = of_get_hsmmc_pdata(&pdev->
dev);
1733 const u16 *offsetp = match->
data;
1738 if (pdata ==
NULL) {
1739 dev_err(&pdev->
dev,
"Platform Data is missing\n");
1750 if (res ==
NULL || irq < 0)
1757 ret = omap_hsmmc_gpio_init(pdata);
1767 host = mmc_priv(mmc);
1780 platform_set_drvdata(pdev, host);
1782 mmc->
ops = &omap_hsmmc_ops;
1788 if (
mmc_slot(host).vcc_aux_disable_is_sleep)
1801 if (IS_ERR(host->
fclk)) {
1802 ret = PTR_ERR(host->
fclk);
1808 dev_info(&pdev->
dev,
"multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1813 pm_runtime_get_sync(host->
dev);
1815 pm_runtime_use_autosuspend(host->
dev);
1817 omap_hsmmc_context_save(host);
1823 if (IS_ERR(host->
dbclk)) {
1826 }
else if (clk_prepare_enable(host->
dbclk) != 0) {
1853 omap_hsmmc_conf_bus_power(host);
1861 tx_req = res->
start;
1869 rx_req = res->
start;
1876 dev_err(
mmc_dev(host->
mmc),
"unable to obtain RX DMA engine channel %u\n", rx_req);
1883 dev_err(
mmc_dev(host->
mmc),
"unable to obtain TX DMA engine channel %u\n", tx_req);
1897 if (pdata->
init(&pdev->
dev) != 0) {
1899 "Unable to configure MMC IRQs\n");
1900 goto err_irq_cd_init;
1904 if (omap_hsmmc_have_reg() && !
mmc_slot(host).set_power) {
1905 ret = omap_hsmmc_reg_get(host);
1914 if ((
mmc_slot(host).card_detect_irq)) {
1922 "Unable to grab MMC CD IRQ\n");
1929 omap_hsmmc_disable_irq(host);
1931 omap_hsmmc_protect_card(host);
1942 &dev_attr_cover_switch);
1947 omap_hsmmc_debugfs(mmc);
1948 pm_runtime_mark_last_busy(host->
dev);
1949 pm_runtime_put_autosuspend(host->
dev);
1958 omap_hsmmc_reg_put(host);
1960 if (host->
pdata->cleanup)
1969 pm_runtime_put_sync(host->
dev);
1970 pm_runtime_disable(host->
dev);
1973 clk_disable_unprepare(host->
dbclk);
1978 platform_set_drvdata(pdev,
NULL);
1981 omap_hsmmc_gpio_free(pdata);
1994 pm_runtime_get_sync(host->
dev);
1997 omap_hsmmc_reg_put(host);
1998 if (host->
pdata->cleanup)
2001 if (
mmc_slot(host).card_detect_irq)
2009 pm_runtime_put_sync(host->
dev);
2010 pm_runtime_disable(host->
dev);
2013 clk_disable_unprepare(host->
dbclk);
2017 omap_hsmmc_gpio_free(host->
pdata);
2024 platform_set_drvdata(pdev,
NULL);
2041 pm_runtime_get_sync(host->
dev);
2043 if (host->
pdata->suspend) {
2046 dev_dbg(dev,
"Unable to handle MMC board"
2047 " level suspend\n");
2056 if (host->
pdata->resume) {
2058 dev_dbg(dev,
"Unmask interrupt failed\n");
2064 omap_hsmmc_disable_irq(host);
2070 clk_disable_unprepare(host->
dbclk);
2072 pm_runtime_put_sync(host->
dev);
2088 pm_runtime_get_sync(host->
dev);
2091 clk_prepare_enable(host->
dbclk);
2094 omap_hsmmc_conf_bus_power(host);
2096 if (host->
pdata->resume) {
2099 dev_dbg(dev,
"Unmask interrupt failed\n");
2102 omap_hsmmc_protect_card(host);
2109 pm_runtime_mark_last_busy(host->
dev);
2110 pm_runtime_put_autosuspend(host->
dev);
2117 #define omap_hsmmc_suspend NULL
2118 #define omap_hsmmc_resume NULL
2121 static int omap_hsmmc_runtime_suspend(
struct device *dev)
2126 omap_hsmmc_context_save(host);
2132 static int omap_hsmmc_runtime_resume(
struct device *dev)
2137 omap_hsmmc_context_restore(host);
2143 static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
2146 .runtime_suspend = omap_hsmmc_runtime_suspend,
2147 .runtime_resume = omap_hsmmc_runtime_resume,
2151 .probe = omap_hsmmc_probe,
2156 .pm = &omap_hsmmc_dev_pm_ops,