Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Pierre Ossman | 70f1048 | 2007-07-11 20:04:50 +0200 | [diff] [blame] | 3 | * linux/drivers/mmc/host/pxa.c - PXA MMCI driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2003 Russell King, All Rights Reserved. |
| 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * This hardware is really sick: |
| 8 | * - No way to clear interrupts. |
| 9 | * - Have to turn off the clock whenever we touch the device. |
| 10 | * - Doesn't tell you how many data blocks were transferred. |
| 11 | * Yuck! |
| 12 | * |
| 13 | * 1 and 3 byte data transfers not supported |
| 14 | * max block length up to 1023 |
| 15 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/ioport.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/delay.h> |
| 21 | #include <linux/interrupt.h> |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 22 | #include <linux/dmaengine.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/dma-mapping.h> |
Russell King | ebebd9b | 2007-08-20 10:20:03 +0100 | [diff] [blame] | 24 | #include <linux/clk.h> |
| 25 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/mmc/host.h> |
Robert Jarzmik | fd546ee | 2015-09-26 21:41:01 +0200 | [diff] [blame] | 27 | #include <linux/mmc/slot-gpio.h> |
Russell King | 05678a9 | 2008-11-28 16:04:54 +0000 | [diff] [blame] | 28 | #include <linux/io.h> |
Daniel Ribeiro | 8385f9c | 2009-05-21 08:54:18 -0300 | [diff] [blame] | 29 | #include <linux/regulator/consumer.h> |
Linus Walleij | f54005b | 2018-12-02 09:43:27 +0100 | [diff] [blame] | 30 | #include <linux/gpio/consumer.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/gfp.h> |
Daniel Mack | e6027b4 | 2012-07-28 12:07:34 +0200 | [diff] [blame] | 32 | #include <linux/of.h> |
Daniel Mack | e6027b4 | 2012-07-28 12:07:34 +0200 | [diff] [blame] | 33 | #include <linux/of_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Masahiro Yamada | 87dfb31 | 2019-05-14 15:46:51 -0700 | [diff] [blame] | 35 | #include <linux/sizes.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Russell King | 05678a9 | 2008-11-28 16:04:54 +0000 | [diff] [blame] | 37 | #include <mach/hardware.h> |
Arnd Bergmann | 293b2da | 2012-08-24 15:16:48 +0200 | [diff] [blame] | 38 | #include <linux/platform_data/mmc-pxamci.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | #include "pxamci.h" |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #define DRIVER_NAME "pxa2xx-mci" |
| 43 | |
| 44 | #define NR_SG 1 |
Russell King | d8cb70d | 2007-10-26 17:56:40 +0100 | [diff] [blame] | 45 | #define CLKRT_OFF (~0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Haojian Zhuang | fa3f993 | 2009-08-31 21:52:53 +0800 | [diff] [blame] | 47 | #define mmc_has_26MHz() (cpu_is_pxa300() || cpu_is_pxa310() \ |
| 48 | || cpu_is_pxa935()) |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | struct pxamci_host { |
| 51 | struct mmc_host *mmc; |
| 52 | spinlock_t lock; |
| 53 | struct resource *res; |
| 54 | void __iomem *base; |
Russell King | ebebd9b | 2007-08-20 10:20:03 +0100 | [diff] [blame] | 55 | struct clk *clk; |
| 56 | unsigned long clkrate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | unsigned int clkrt; |
| 58 | unsigned int cmdat; |
| 59 | unsigned int imask; |
| 60 | unsigned int power_mode; |
Daniel Mack | 38a8dda | 2018-07-03 22:10:26 +0200 | [diff] [blame] | 61 | unsigned long detect_delay_ms; |
Linus Walleij | c914a27 | 2018-12-02 09:43:24 +0100 | [diff] [blame] | 62 | bool use_ro_gpio; |
Linus Walleij | f54005b | 2018-12-02 09:43:27 +0100 | [diff] [blame] | 63 | struct gpio_desc *power; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | struct pxamci_platform_data *pdata; |
| 65 | |
| 66 | struct mmc_request *mrq; |
| 67 | struct mmc_command *cmd; |
| 68 | struct mmc_data *data; |
| 69 | |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 70 | struct dma_chan *dma_chan_rx; |
| 71 | struct dma_chan *dma_chan_tx; |
| 72 | dma_cookie_t dma_cookie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | unsigned int dma_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | unsigned int dma_dir; |
| 75 | }; |
| 76 | |
Daniel Mack | 61951fd | 2018-06-30 20:14:05 +0200 | [diff] [blame] | 77 | static int pxamci_init_ocr(struct pxamci_host *host) |
Daniel Ribeiro | 8385f9c | 2009-05-21 08:54:18 -0300 | [diff] [blame] | 78 | { |
Daniel Mack | 61951fd | 2018-06-30 20:14:05 +0200 | [diff] [blame] | 79 | struct mmc_host *mmc = host->mmc; |
| 80 | int ret; |
Daniel Ribeiro | 8385f9c | 2009-05-21 08:54:18 -0300 | [diff] [blame] | 81 | |
Daniel Mack | 61951fd | 2018-06-30 20:14:05 +0200 | [diff] [blame] | 82 | ret = mmc_regulator_get_supply(mmc); |
| 83 | if (ret < 0) |
| 84 | return ret; |
| 85 | |
| 86 | if (IS_ERR(mmc->supply.vmmc)) { |
Daniel Ribeiro | 8385f9c | 2009-05-21 08:54:18 -0300 | [diff] [blame] | 87 | /* fall-back to platform data */ |
Daniel Mack | 61951fd | 2018-06-30 20:14:05 +0200 | [diff] [blame] | 88 | mmc->ocr_avail = host->pdata ? |
Daniel Ribeiro | 8385f9c | 2009-05-21 08:54:18 -0300 | [diff] [blame] | 89 | host->pdata->ocr_mask : |
| 90 | MMC_VDD_32_33 | MMC_VDD_33_34; |
| 91 | } |
Daniel Mack | 61951fd | 2018-06-30 20:14:05 +0200 | [diff] [blame] | 92 | |
| 93 | return 0; |
Daniel Ribeiro | 8385f9c | 2009-05-21 08:54:18 -0300 | [diff] [blame] | 94 | } |
| 95 | |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 96 | static inline int pxamci_set_power(struct pxamci_host *host, |
| 97 | unsigned char power_mode, |
| 98 | unsigned int vdd) |
Daniel Ribeiro | 8385f9c | 2009-05-21 08:54:18 -0300 | [diff] [blame] | 99 | { |
Daniel Mack | 61951fd | 2018-06-30 20:14:05 +0200 | [diff] [blame] | 100 | struct mmc_host *mmc = host->mmc; |
| 101 | struct regulator *supply = mmc->supply.vmmc; |
Robert Jarzmik | b405db6 | 2009-06-23 23:21:03 +0200 | [diff] [blame] | 102 | |
Daniel Mack | 61951fd | 2018-06-30 20:14:05 +0200 | [diff] [blame] | 103 | if (!IS_ERR(supply)) |
| 104 | return mmc_regulator_set_ocr(mmc, supply, vdd); |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 105 | |
Linus Walleij | f54005b | 2018-12-02 09:43:27 +0100 | [diff] [blame] | 106 | if (host->power) { |
| 107 | bool on = !!((1 << vdd) & host->pdata->ocr_mask); |
| 108 | gpiod_set_value(host->power, on); |
Robert Jarzmik | b405db6 | 2009-06-23 23:21:03 +0200 | [diff] [blame] | 109 | } |
Daniel Mack | 61951fd | 2018-06-30 20:14:05 +0200 | [diff] [blame] | 110 | |
| 111 | if (host->pdata && host->pdata->setpower) |
Arnd Bergmann | a829abf | 2013-07-05 17:51:20 +0200 | [diff] [blame] | 112 | return host->pdata->setpower(mmc_dev(host->mmc), vdd); |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 113 | |
| 114 | return 0; |
Daniel Ribeiro | 8385f9c | 2009-05-21 08:54:18 -0300 | [diff] [blame] | 115 | } |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | static void pxamci_stop_clock(struct pxamci_host *host) |
| 118 | { |
| 119 | if (readl(host->base + MMC_STAT) & STAT_CLK_EN) { |
| 120 | unsigned long timeout = 10000; |
| 121 | unsigned int v; |
| 122 | |
| 123 | writel(STOP_CLOCK, host->base + MMC_STRPCL); |
| 124 | |
| 125 | do { |
| 126 | v = readl(host->base + MMC_STAT); |
| 127 | if (!(v & STAT_CLK_EN)) |
| 128 | break; |
| 129 | udelay(1); |
| 130 | } while (timeout--); |
| 131 | |
| 132 | if (v & STAT_CLK_EN) |
| 133 | dev_err(mmc_dev(host->mmc), "unable to stop clock\n"); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static void pxamci_enable_irq(struct pxamci_host *host, unsigned int mask) |
| 138 | { |
| 139 | unsigned long flags; |
| 140 | |
| 141 | spin_lock_irqsave(&host->lock, flags); |
| 142 | host->imask &= ~mask; |
| 143 | writel(host->imask, host->base + MMC_I_MASK); |
| 144 | spin_unlock_irqrestore(&host->lock, flags); |
| 145 | } |
| 146 | |
| 147 | static void pxamci_disable_irq(struct pxamci_host *host, unsigned int mask) |
| 148 | { |
| 149 | unsigned long flags; |
| 150 | |
| 151 | spin_lock_irqsave(&host->lock, flags); |
| 152 | host->imask |= mask; |
| 153 | writel(host->imask, host->base + MMC_I_MASK); |
| 154 | spin_unlock_irqrestore(&host->lock, flags); |
| 155 | } |
| 156 | |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 157 | static void pxamci_dma_irq(void *param); |
| 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data) |
| 160 | { |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 161 | struct dma_async_tx_descriptor *tx; |
Arnd Bergmann | e60a582 | 2019-03-07 11:09:19 +0100 | [diff] [blame] | 162 | enum dma_transfer_direction direction; |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 163 | struct dma_slave_config config; |
| 164 | struct dma_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | unsigned int nob = data->blocks; |
Russell King | 3d63abe | 2006-04-24 11:27:02 +0100 | [diff] [blame] | 166 | unsigned long long clks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | unsigned int timeout; |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 168 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 170 | host->data = data; |
| 171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | writel(nob, host->base + MMC_NOB); |
Pavel Pisa | 2c171bf | 2006-05-19 21:48:03 +0100 | [diff] [blame] | 173 | writel(data->blksz, host->base + MMC_BLKLEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Russell King | ebebd9b | 2007-08-20 10:20:03 +0100 | [diff] [blame] | 175 | clks = (unsigned long long)data->timeout_ns * host->clkrate; |
Russell King | 3d63abe | 2006-04-24 11:27:02 +0100 | [diff] [blame] | 176 | do_div(clks, 1000000000UL); |
| 177 | timeout = (unsigned int)clks + (data->timeout_clks << host->clkrt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | writel((timeout + 255) / 256, host->base + MMC_RDTO); |
| 179 | |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 180 | memset(&config, 0, sizeof(config)); |
| 181 | config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 182 | config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 183 | config.src_addr = host->res->start + MMC_RXFIFO; |
| 184 | config.dst_addr = host->res->start + MMC_TXFIFO; |
| 185 | config.src_maxburst = 32; |
| 186 | config.dst_maxburst = 32; |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | if (data->flags & MMC_DATA_READ) { |
| 189 | host->dma_dir = DMA_FROM_DEVICE; |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 190 | direction = DMA_DEV_TO_MEM; |
| 191 | chan = host->dma_chan_rx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } else { |
| 193 | host->dma_dir = DMA_TO_DEVICE; |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 194 | direction = DMA_MEM_TO_DEV; |
| 195 | chan = host->dma_chan_tx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 198 | config.direction = direction; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 200 | ret = dmaengine_slave_config(chan, &config); |
| 201 | if (ret < 0) { |
| 202 | dev_err(mmc_dev(host->mmc), "dma slave config failed\n"); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | host->dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | host->dma_dir); |
| 208 | |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 209 | tx = dmaengine_prep_slave_sg(chan, data->sg, host->dma_len, direction, |
| 210 | DMA_PREP_INTERRUPT); |
| 211 | if (!tx) { |
| 212 | dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n"); |
| 213 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 216 | if (!(data->flags & MMC_DATA_READ)) { |
| 217 | tx->callback = pxamci_dma_irq; |
| 218 | tx->callback_param = host; |
| 219 | } |
| 220 | |
| 221 | host->dma_cookie = dmaengine_submit(tx); |
Cliff Brake | b601895 | 2009-01-22 17:07:03 -0500 | [diff] [blame] | 222 | |
| 223 | /* |
| 224 | * workaround for erratum #91: |
| 225 | * only start DMA now if we are doing a read, |
| 226 | * otherwise we wait until CMD/RESP has finished |
| 227 | * before starting DMA. |
| 228 | */ |
| 229 | if (!cpu_is_pxa27x() || data->flags & MMC_DATA_READ) |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 230 | dma_async_issue_pending(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static void pxamci_start_cmd(struct pxamci_host *host, struct mmc_command *cmd, unsigned int cmdat) |
| 234 | { |
| 235 | WARN_ON(host->cmd != NULL); |
| 236 | host->cmd = cmd; |
| 237 | |
| 238 | if (cmd->flags & MMC_RSP_BUSY) |
| 239 | cmdat |= CMDAT_BUSY; |
| 240 | |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 241 | #define RSP_TYPE(x) ((x) & ~(MMC_RSP_BUSY|MMC_RSP_OPCODE)) |
| 242 | switch (RSP_TYPE(mmc_resp_type(cmd))) { |
Philip Langdale | 6f94990 | 2007-01-04 07:04:47 -0800 | [diff] [blame] | 243 | case RSP_TYPE(MMC_RSP_R1): /* r1, r1b, r6, r7 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | cmdat |= CMDAT_RESP_SHORT; |
| 245 | break; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 246 | case RSP_TYPE(MMC_RSP_R3): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | cmdat |= CMDAT_RESP_R3; |
| 248 | break; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 249 | case RSP_TYPE(MMC_RSP_R2): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | cmdat |= CMDAT_RESP_R2; |
| 251 | break; |
| 252 | default: |
| 253 | break; |
| 254 | } |
| 255 | |
| 256 | writel(cmd->opcode, host->base + MMC_CMD); |
| 257 | writel(cmd->arg >> 16, host->base + MMC_ARGH); |
| 258 | writel(cmd->arg & 0xffff, host->base + MMC_ARGL); |
| 259 | writel(cmdat, host->base + MMC_CMDAT); |
| 260 | writel(host->clkrt, host->base + MMC_CLKRT); |
| 261 | |
| 262 | writel(START_CLOCK, host->base + MMC_STRPCL); |
| 263 | |
| 264 | pxamci_enable_irq(host, END_CMD_RES); |
| 265 | } |
| 266 | |
| 267 | static void pxamci_finish_request(struct pxamci_host *host, struct mmc_request *mrq) |
| 268 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | host->mrq = NULL; |
| 270 | host->cmd = NULL; |
| 271 | host->data = NULL; |
| 272 | mmc_request_done(host->mmc, mrq); |
| 273 | } |
| 274 | |
| 275 | static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat) |
| 276 | { |
| 277 | struct mmc_command *cmd = host->cmd; |
| 278 | int i; |
| 279 | u32 v; |
| 280 | |
| 281 | if (!cmd) |
| 282 | return 0; |
| 283 | |
| 284 | host->cmd = NULL; |
| 285 | |
| 286 | /* |
| 287 | * Did I mention this is Sick. We always need to |
| 288 | * discard the upper 8 bits of the first 16-bit word. |
| 289 | */ |
| 290 | v = readl(host->base + MMC_RES) & 0xffff; |
| 291 | for (i = 0; i < 4; i++) { |
| 292 | u32 w1 = readl(host->base + MMC_RES) & 0xffff; |
| 293 | u32 w2 = readl(host->base + MMC_RES) & 0xffff; |
| 294 | cmd->resp[i] = v << 24 | w1 << 8 | w2 >> 8; |
| 295 | v = w2; |
| 296 | } |
| 297 | |
| 298 | if (stat & STAT_TIME_OUT_RESPONSE) { |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 299 | cmd->error = -ETIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | /* |
| 302 | * workaround for erratum #42: |
| 303 | * Intel PXA27x Family Processor Specification Update Rev 001 |
Nicolas Pitre | 90e07d9 | 2007-05-13 18:03:08 +0200 | [diff] [blame] | 304 | * A bogus CRC error can appear if the msb of a 136 bit |
| 305 | * response is a one. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | */ |
Cliff Brake | e10a854 | 2009-01-22 16:58:58 -0500 | [diff] [blame] | 307 | if (cpu_is_pxa27x() && |
| 308 | (cmd->flags & MMC_RSP_136 && cmd->resp[0] & 0x80000000)) |
Nicolas Pitre | 90e07d9 | 2007-05-13 18:03:08 +0200 | [diff] [blame] | 309 | pr_debug("ignoring CRC from command %d - *risky*\n", cmd->opcode); |
Cliff Brake | e10a854 | 2009-01-22 16:58:58 -0500 | [diff] [blame] | 310 | else |
| 311 | cmd->error = -EILSEQ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | pxamci_disable_irq(host, END_CMD_RES); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 315 | if (host->data && !cmd->error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | pxamci_enable_irq(host, DATA_TRAN_DONE); |
Cliff Brake | b601895 | 2009-01-22 17:07:03 -0500 | [diff] [blame] | 317 | /* |
| 318 | * workaround for erratum #91, if doing write |
| 319 | * enable DMA late |
| 320 | */ |
| 321 | if (cpu_is_pxa27x() && host->data->flags & MMC_DATA_WRITE) |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 322 | dma_async_issue_pending(host->dma_chan_tx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } else { |
| 324 | pxamci_finish_request(host, host->mrq); |
| 325 | } |
| 326 | |
| 327 | return 1; |
| 328 | } |
| 329 | |
| 330 | static int pxamci_data_done(struct pxamci_host *host, unsigned int stat) |
| 331 | { |
| 332 | struct mmc_data *data = host->data; |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 333 | struct dma_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
| 335 | if (!data) |
| 336 | return 0; |
| 337 | |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 338 | if (data->flags & MMC_DATA_READ) |
| 339 | chan = host->dma_chan_rx; |
| 340 | else |
| 341 | chan = host->dma_chan_tx; |
| 342 | dma_unmap_sg(chan->device->dev, |
| 343 | data->sg, data->sg_len, host->dma_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
| 345 | if (stat & STAT_READ_TIME_OUT) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 346 | data->error = -ETIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR)) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 348 | data->error = -EILSEQ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
| 350 | /* |
| 351 | * There appears to be a hardware design bug here. There seems to |
| 352 | * be no way to find out how much data was transferred to the card. |
| 353 | * This means that if there was an error on any block, we mark all |
| 354 | * data blocks as being in error. |
| 355 | */ |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 356 | if (!data->error) |
Pavel Pisa | 2c171bf | 2006-05-19 21:48:03 +0100 | [diff] [blame] | 357 | data->bytes_xfered = data->blocks * data->blksz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | else |
| 359 | data->bytes_xfered = 0; |
| 360 | |
| 361 | pxamci_disable_irq(host, DATA_TRAN_DONE); |
| 362 | |
| 363 | host->data = NULL; |
Russell King | 58741e8 | 2006-05-02 20:02:39 +0100 | [diff] [blame] | 364 | if (host->mrq->stop) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | pxamci_stop_clock(host); |
Bridge Wu | df456f4 | 2007-09-25 19:09:19 +0200 | [diff] [blame] | 366 | pxamci_start_cmd(host, host->mrq->stop, host->cmdat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } else { |
| 368 | pxamci_finish_request(host, host->mrq); |
| 369 | } |
| 370 | |
| 371 | return 1; |
| 372 | } |
| 373 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 374 | static irqreturn_t pxamci_irq(int irq, void *devid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | { |
| 376 | struct pxamci_host *host = devid; |
| 377 | unsigned int ireg; |
| 378 | int handled = 0; |
| 379 | |
Bridge Wu | 81ab570f | 2007-09-25 18:59:07 +0200 | [diff] [blame] | 380 | ireg = readl(host->base + MMC_I_REG) & ~readl(host->base + MMC_I_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | if (ireg) { |
| 383 | unsigned stat = readl(host->base + MMC_STAT); |
| 384 | |
Russell King | d78e907 | 2006-05-02 20:18:53 +0100 | [diff] [blame] | 385 | pr_debug("PXAMCI: irq %08x stat %08x\n", ireg, stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
| 387 | if (ireg & END_CMD_RES) |
| 388 | handled |= pxamci_cmd_done(host, stat); |
| 389 | if (ireg & DATA_TRAN_DONE) |
| 390 | handled |= pxamci_data_done(host, stat); |
Bridge Wu | 5d3ad4e | 2007-09-25 19:11:00 +0200 | [diff] [blame] | 391 | if (ireg & SDIO_INT) { |
| 392 | mmc_signal_sdio_irq(host->mmc); |
| 393 | handled = 1; |
| 394 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | return IRQ_RETVAL(handled); |
| 398 | } |
| 399 | |
| 400 | static void pxamci_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 401 | { |
| 402 | struct pxamci_host *host = mmc_priv(mmc); |
| 403 | unsigned int cmdat; |
| 404 | |
| 405 | WARN_ON(host->mrq != NULL); |
| 406 | |
| 407 | host->mrq = mrq; |
| 408 | |
| 409 | pxamci_stop_clock(host); |
| 410 | |
| 411 | cmdat = host->cmdat; |
| 412 | host->cmdat &= ~CMDAT_INIT; |
| 413 | |
| 414 | if (mrq->data) { |
| 415 | pxamci_setup_data(host, mrq->data); |
| 416 | |
| 417 | cmdat &= ~CMDAT_BUSY; |
| 418 | cmdat |= CMDAT_DATAEN | CMDAT_DMAEN; |
| 419 | if (mrq->data->flags & MMC_DATA_WRITE) |
| 420 | cmdat |= CMDAT_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | pxamci_start_cmd(host, mrq->cmd, cmdat); |
| 424 | } |
| 425 | |
Richard Purdie | e619524 | 2005-09-06 15:18:56 -0700 | [diff] [blame] | 426 | static int pxamci_get_ro(struct mmc_host *mmc) |
| 427 | { |
| 428 | struct pxamci_host *host = mmc_priv(mmc); |
| 429 | |
Linus Walleij | c914a27 | 2018-12-02 09:43:24 +0100 | [diff] [blame] | 430 | if (host->use_ro_gpio) |
Robert Jarzmik | fd546ee | 2015-09-26 21:41:01 +0200 | [diff] [blame] | 431 | return mmc_gpio_get_ro(mmc); |
Richard Purdie | e619524 | 2005-09-06 15:18:56 -0700 | [diff] [blame] | 432 | if (host->pdata && host->pdata->get_ro) |
Anton Vorontsov | 08f80bb | 2008-06-17 18:17:39 +0400 | [diff] [blame] | 433 | return !!host->pdata->get_ro(mmc_dev(mmc)); |
| 434 | /* |
| 435 | * Board doesn't support read only detection; let the mmc core |
| 436 | * decide what to do. |
| 437 | */ |
| 438 | return -ENOSYS; |
Richard Purdie | e619524 | 2005-09-06 15:18:56 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 442 | { |
| 443 | struct pxamci_host *host = mmc_priv(mmc); |
| 444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | if (ios->clock) { |
Russell King | ebebd9b | 2007-08-20 10:20:03 +0100 | [diff] [blame] | 446 | unsigned long rate = host->clkrate; |
| 447 | unsigned int clk = rate / ios->clock; |
| 448 | |
Russell King | d8cb70d | 2007-10-26 17:56:40 +0100 | [diff] [blame] | 449 | if (host->clkrt == CLKRT_OFF) |
Robert Jarzmik | e737081 | 2014-09-02 11:23:55 +0200 | [diff] [blame] | 450 | clk_prepare_enable(host->clk); |
Russell King | d8cb70d | 2007-10-26 17:56:40 +0100 | [diff] [blame] | 451 | |
Bridge Wu | 64eb036 | 2007-12-13 07:24:30 +0100 | [diff] [blame] | 452 | if (ios->clock == 26000000) { |
Haojian Zhuang | fa3f993 | 2009-08-31 21:52:53 +0800 | [diff] [blame] | 453 | /* to support 26MHz */ |
Bridge Wu | 64eb036 | 2007-12-13 07:24:30 +0100 | [diff] [blame] | 454 | host->clkrt = 7; |
| 455 | } else { |
| 456 | /* to handle (19.5MHz, 26MHz) */ |
| 457 | if (!clk) |
| 458 | clk = 1; |
| 459 | |
| 460 | /* |
| 461 | * clk might result in a lower divisor than we |
| 462 | * desire. check for that condition and adjust |
| 463 | * as appropriate. |
| 464 | */ |
| 465 | if (rate / clk > ios->clock) |
| 466 | clk <<= 1; |
| 467 | host->clkrt = fls(clk) - 1; |
| 468 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
| 470 | /* |
| 471 | * we write clkrt on the next command |
| 472 | */ |
| 473 | } else { |
| 474 | pxamci_stop_clock(host); |
Russell King | d8cb70d | 2007-10-26 17:56:40 +0100 | [diff] [blame] | 475 | if (host->clkrt != CLKRT_OFF) { |
| 476 | host->clkrt = CLKRT_OFF; |
Robert Jarzmik | e737081 | 2014-09-02 11:23:55 +0200 | [diff] [blame] | 477 | clk_disable_unprepare(host->clk); |
Russell King | d8cb70d | 2007-10-26 17:56:40 +0100 | [diff] [blame] | 478 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | if (host->power_mode != ios->power_mode) { |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 482 | int ret; |
| 483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | host->power_mode = ios->power_mode; |
| 485 | |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 486 | ret = pxamci_set_power(host, ios->power_mode, ios->vdd); |
| 487 | if (ret) { |
| 488 | dev_err(mmc_dev(mmc), "unable to set power\n"); |
| 489 | /* |
| 490 | * The .set_ios() function in the mmc_host_ops |
| 491 | * struct return void, and failing to set the |
| 492 | * power should be rare so we print an error and |
| 493 | * return here. |
| 494 | */ |
| 495 | return; |
| 496 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
| 498 | if (ios->power_mode == MMC_POWER_ON) |
| 499 | host->cmdat |= CMDAT_INIT; |
| 500 | } |
| 501 | |
Bridge Wu | df456f4 | 2007-09-25 19:09:19 +0200 | [diff] [blame] | 502 | if (ios->bus_width == MMC_BUS_WIDTH_4) |
| 503 | host->cmdat |= CMDAT_SD_4DAT; |
| 504 | else |
| 505 | host->cmdat &= ~CMDAT_SD_4DAT; |
| 506 | |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 507 | dev_dbg(mmc_dev(mmc), "PXAMCI: clkrt = %x cmdat = %x\n", |
| 508 | host->clkrt, host->cmdat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Bridge Wu | 5d3ad4e | 2007-09-25 19:11:00 +0200 | [diff] [blame] | 511 | static void pxamci_enable_sdio_irq(struct mmc_host *host, int enable) |
| 512 | { |
| 513 | struct pxamci_host *pxa_host = mmc_priv(host); |
| 514 | |
| 515 | if (enable) |
| 516 | pxamci_enable_irq(pxa_host, SDIO_INT); |
| 517 | else |
| 518 | pxamci_disable_irq(pxa_host, SDIO_INT); |
| 519 | } |
| 520 | |
David Brownell | ab7aefd | 2006-11-12 17:55:30 -0800 | [diff] [blame] | 521 | static const struct mmc_host_ops pxamci_ops = { |
Bridge Wu | 5d3ad4e | 2007-09-25 19:11:00 +0200 | [diff] [blame] | 522 | .request = pxamci_request, |
Robert Jarzmik | fd546ee | 2015-09-26 21:41:01 +0200 | [diff] [blame] | 523 | .get_cd = mmc_gpio_get_cd, |
Bridge Wu | 5d3ad4e | 2007-09-25 19:11:00 +0200 | [diff] [blame] | 524 | .get_ro = pxamci_get_ro, |
| 525 | .set_ios = pxamci_set_ios, |
| 526 | .enable_sdio_irq = pxamci_enable_sdio_irq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | }; |
| 528 | |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 529 | static void pxamci_dma_irq(void *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | { |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 531 | struct pxamci_host *host = param; |
| 532 | struct dma_tx_state state; |
| 533 | enum dma_status status; |
| 534 | struct dma_chan *chan; |
| 535 | unsigned long flags; |
Nicolas Pitre | c783837 | 2007-10-09 17:07:58 -0400 | [diff] [blame] | 536 | |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 537 | spin_lock_irqsave(&host->lock, flags); |
| 538 | |
| 539 | if (!host->data) |
| 540 | goto out_unlock; |
| 541 | |
| 542 | if (host->data->flags & MMC_DATA_READ) |
| 543 | chan = host->dma_chan_rx; |
| 544 | else |
| 545 | chan = host->dma_chan_tx; |
| 546 | |
| 547 | status = dmaengine_tx_status(chan, host->dma_cookie, &state); |
| 548 | |
| 549 | if (likely(status == DMA_COMPLETE)) { |
Nicolas Pitre | c783837 | 2007-10-09 17:07:58 -0400 | [diff] [blame] | 550 | writel(BUF_PART_FULL, host->base + MMC_PRTBUF); |
| 551 | } else { |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 552 | pr_err("%s: DMA error on %s channel\n", mmc_hostname(host->mmc), |
| 553 | host->data->flags & MMC_DATA_READ ? "rx" : "tx"); |
Nicolas Pitre | c783837 | 2007-10-09 17:07:58 -0400 | [diff] [blame] | 554 | host->data->error = -EIO; |
| 555 | pxamci_data_done(host, 0); |
| 556 | } |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 557 | |
| 558 | out_unlock: |
| 559 | spin_unlock_irqrestore(&host->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | } |
| 561 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 562 | static irqreturn_t pxamci_detect_irq(int irq, void *devid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | { |
Richard Purdie | c26971c | 2005-09-08 22:48:16 +0100 | [diff] [blame] | 564 | struct pxamci_host *host = mmc_priv(devid); |
| 565 | |
Daniel Mack | 38a8dda | 2018-07-03 22:10:26 +0200 | [diff] [blame] | 566 | mmc_detect_change(devid, msecs_to_jiffies(host->detect_delay_ms)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | return IRQ_HANDLED; |
| 568 | } |
| 569 | |
Daniel Mack | e6027b4 | 2012-07-28 12:07:34 +0200 | [diff] [blame] | 570 | #ifdef CONFIG_OF |
| 571 | static const struct of_device_id pxa_mmc_dt_ids[] = { |
| 572 | { .compatible = "marvell,pxa-mmc" }, |
| 573 | { } |
| 574 | }; |
| 575 | |
| 576 | MODULE_DEVICE_TABLE(of, pxa_mmc_dt_ids); |
| 577 | |
Daniel Mack | fa3a511 | 2018-06-30 20:14:03 +0200 | [diff] [blame] | 578 | static int pxamci_of_init(struct platform_device *pdev, |
| 579 | struct mmc_host *mmc) |
Daniel Mack | e6027b4 | 2012-07-28 12:07:34 +0200 | [diff] [blame] | 580 | { |
Daniel Mack | 0da5358 | 2018-06-30 20:14:02 +0200 | [diff] [blame] | 581 | struct device_node *np = pdev->dev.of_node; |
Daniel Mack | 38a8dda | 2018-07-03 22:10:26 +0200 | [diff] [blame] | 582 | struct pxamci_host *host = mmc_priv(mmc); |
Daniel Mack | 0da5358 | 2018-06-30 20:14:02 +0200 | [diff] [blame] | 583 | u32 tmp; |
Daniel Mack | fa3a511 | 2018-06-30 20:14:03 +0200 | [diff] [blame] | 584 | int ret; |
Daniel Mack | e6027b4 | 2012-07-28 12:07:34 +0200 | [diff] [blame] | 585 | |
Daniel Mack | 0da5358 | 2018-06-30 20:14:02 +0200 | [diff] [blame] | 586 | if (!np) |
| 587 | return 0; |
Daniel Mack | e6027b4 | 2012-07-28 12:07:34 +0200 | [diff] [blame] | 588 | |
Daniel Mack | e6027b4 | 2012-07-28 12:07:34 +0200 | [diff] [blame] | 589 | /* pxa-mmc specific */ |
Daniel Mack | e6027b4 | 2012-07-28 12:07:34 +0200 | [diff] [blame] | 590 | if (of_property_read_u32(np, "pxa-mmc,detect-delay-ms", &tmp) == 0) |
Daniel Mack | 38a8dda | 2018-07-03 22:10:26 +0200 | [diff] [blame] | 591 | host->detect_delay_ms = tmp; |
Daniel Mack | e6027b4 | 2012-07-28 12:07:34 +0200 | [diff] [blame] | 592 | |
Daniel Mack | fa3a511 | 2018-06-30 20:14:03 +0200 | [diff] [blame] | 593 | ret = mmc_of_parse(mmc); |
| 594 | if (ret < 0) |
| 595 | return ret; |
| 596 | |
Daniel Mack | 0da5358 | 2018-06-30 20:14:02 +0200 | [diff] [blame] | 597 | return 0; |
Daniel Mack | e6027b4 | 2012-07-28 12:07:34 +0200 | [diff] [blame] | 598 | } |
| 599 | #else |
Daniel Mack | fa3a511 | 2018-06-30 20:14:03 +0200 | [diff] [blame] | 600 | static int pxamci_of_init(struct platform_device *pdev, |
| 601 | struct mmc_host *mmc) |
Daniel Mack | e6027b4 | 2012-07-28 12:07:34 +0200 | [diff] [blame] | 602 | { |
| 603 | return 0; |
| 604 | } |
| 605 | #endif |
| 606 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 607 | static int pxamci_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | struct mmc_host *mmc; |
| 610 | struct pxamci_host *host = NULL; |
Daniel Mack | 23f3ff7 | 2018-07-03 22:10:27 +0200 | [diff] [blame] | 611 | struct device *dev = &pdev->dev; |
Robert Jarzmik | 6b3348f9e | 2018-06-17 19:02:07 +0200 | [diff] [blame] | 612 | struct resource *r; |
Daniel Mack | 38a8dda | 2018-07-03 22:10:26 +0200 | [diff] [blame] | 613 | int ret, irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
| 615 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 616 | irq = platform_get_irq(pdev, 0); |
Robert Jarzmik | 07e7716 | 2016-02-08 15:17:57 +0100 | [diff] [blame] | 617 | if (irq < 0) |
| 618 | return irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
Daniel Mack | 23f3ff7 | 2018-07-03 22:10:27 +0200 | [diff] [blame] | 620 | mmc = mmc_alloc_host(sizeof(struct pxamci_host), dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | if (!mmc) { |
| 622 | ret = -ENOMEM; |
| 623 | goto out; |
| 624 | } |
| 625 | |
| 626 | mmc->ops = &pxamci_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
| 628 | /* |
| 629 | * We can do SG-DMA, but we don't because we never know how much |
| 630 | * data we successfully wrote to the card. |
| 631 | */ |
Martin K. Petersen | a36274e | 2010-09-10 01:33:59 -0400 | [diff] [blame] | 632 | mmc->max_segs = NR_SG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
| 634 | /* |
| 635 | * Our hardware DMA can handle a maximum of one page per SG entry. |
| 636 | */ |
| 637 | mmc->max_seg_size = PAGE_SIZE; |
| 638 | |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 639 | /* |
Nicolas Pitre | fe2dc44 | 2007-09-24 15:47:18 -0400 | [diff] [blame] | 640 | * Block length register is only 10 bits before PXA27x. |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 641 | */ |
Eric Miao | 0ffcbfd | 2008-09-11 10:27:30 +0800 | [diff] [blame] | 642 | mmc->max_blk_size = cpu_is_pxa25x() ? 1023 : 2048; |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 643 | |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 644 | /* |
| 645 | * Block count register is 16 bits. |
| 646 | */ |
| 647 | mmc->max_blk_count = 65535; |
| 648 | |
Daniel Mack | fa3a511 | 2018-06-30 20:14:03 +0200 | [diff] [blame] | 649 | ret = pxamci_of_init(pdev, mmc); |
| 650 | if (ret) |
| 651 | return ret; |
| 652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | host = mmc_priv(mmc); |
| 654 | host->mmc = mmc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | host->pdata = pdev->dev.platform_data; |
Russell King | d8cb70d | 2007-10-26 17:56:40 +0100 | [diff] [blame] | 656 | host->clkrt = CLKRT_OFF; |
Russell King | ebebd9b | 2007-08-20 10:20:03 +0100 | [diff] [blame] | 657 | |
Daniel Mack | 23f3ff7 | 2018-07-03 22:10:27 +0200 | [diff] [blame] | 658 | host->clk = devm_clk_get(dev, NULL); |
Russell King | ebebd9b | 2007-08-20 10:20:03 +0100 | [diff] [blame] | 659 | if (IS_ERR(host->clk)) { |
| 660 | ret = PTR_ERR(host->clk); |
| 661 | host->clk = NULL; |
| 662 | goto out; |
| 663 | } |
| 664 | |
| 665 | host->clkrate = clk_get_rate(host->clk); |
| 666 | |
| 667 | /* |
| 668 | * Calculate minimum clock rate, rounding up. |
| 669 | */ |
| 670 | mmc->f_min = (host->clkrate + 63) / 64; |
Haojian Zhuang | fa3f993 | 2009-08-31 21:52:53 +0800 | [diff] [blame] | 671 | mmc->f_max = (mmc_has_26MHz()) ? 26000000 : host->clkrate; |
Russell King | ebebd9b | 2007-08-20 10:20:03 +0100 | [diff] [blame] | 672 | |
Daniel Mack | 61951fd | 2018-06-30 20:14:05 +0200 | [diff] [blame] | 673 | ret = pxamci_init_ocr(host); |
| 674 | if (ret < 0) |
| 675 | return ret; |
Daniel Ribeiro | 8385f9c | 2009-05-21 08:54:18 -0300 | [diff] [blame] | 676 | |
Linus Walleij | de3ee99 | 2017-09-20 10:56:14 +0200 | [diff] [blame] | 677 | mmc->caps = 0; |
Bridge Wu | 5d3ad4e | 2007-09-25 19:11:00 +0200 | [diff] [blame] | 678 | host->cmdat = 0; |
Eric Miao | 0ffcbfd | 2008-09-11 10:27:30 +0800 | [diff] [blame] | 679 | if (!cpu_is_pxa25x()) { |
Bridge Wu | 5d3ad4e | 2007-09-25 19:11:00 +0200 | [diff] [blame] | 680 | mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ; |
| 681 | host->cmdat |= CMDAT_SDIO_INT_EN; |
Haojian Zhuang | fa3f993 | 2009-08-31 21:52:53 +0800 | [diff] [blame] | 682 | if (mmc_has_26MHz()) |
Bridge Wu | 64eb036 | 2007-12-13 07:24:30 +0100 | [diff] [blame] | 683 | mmc->caps |= MMC_CAP_MMC_HIGHSPEED | |
| 684 | MMC_CAP_SD_HIGHSPEED; |
Bridge Wu | 5d3ad4e | 2007-09-25 19:11:00 +0200 | [diff] [blame] | 685 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | spin_lock_init(&host->lock); |
| 688 | host->res = r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | host->imask = MMC_I_MASK_ALL; |
| 690 | |
Daniel Mack | 23f3ff7 | 2018-07-03 22:10:27 +0200 | [diff] [blame] | 691 | host->base = devm_ioremap_resource(dev, r); |
Robert Jarzmik | 07e7716 | 2016-02-08 15:17:57 +0100 | [diff] [blame] | 692 | if (IS_ERR(host->base)) { |
| 693 | ret = PTR_ERR(host->base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | goto out; |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * Ensure that the host controller is shut down, and setup |
| 699 | * with our defaults. |
| 700 | */ |
| 701 | pxamci_stop_clock(host); |
| 702 | writel(0, host->base + MMC_SPI); |
| 703 | writel(64, host->base + MMC_RESTO); |
| 704 | writel(host->imask, host->base + MMC_I_MASK); |
| 705 | |
Daniel Mack | 23f3ff7 | 2018-07-03 22:10:27 +0200 | [diff] [blame] | 706 | ret = devm_request_irq(dev, irq, pxamci_irq, 0, |
Robert Jarzmik | 07e7716 | 2016-02-08 15:17:57 +0100 | [diff] [blame] | 707 | DRIVER_NAME, host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | if (ret) |
| 709 | goto out; |
| 710 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 711 | platform_set_drvdata(pdev, mmc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | |
Peter Ujfalusi | e1ebb45 | 2019-12-17 13:30:04 +0200 | [diff] [blame] | 713 | host->dma_chan_rx = dma_request_chan(dev, "rx"); |
| 714 | if (IS_ERR(host->dma_chan_rx)) { |
Daniel Mack | 23f3ff7 | 2018-07-03 22:10:27 +0200 | [diff] [blame] | 715 | dev_err(dev, "unable to request rx dma channel\n"); |
Peter Ujfalusi | e1ebb45 | 2019-12-17 13:30:04 +0200 | [diff] [blame] | 716 | ret = PTR_ERR(host->dma_chan_rx); |
| 717 | host->dma_chan_rx = NULL; |
Bridge Wu | 9a788c6 | 2007-12-14 10:40:25 +0100 | [diff] [blame] | 718 | goto out; |
| 719 | } |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 720 | |
Peter Ujfalusi | e1ebb45 | 2019-12-17 13:30:04 +0200 | [diff] [blame] | 721 | host->dma_chan_tx = dma_request_chan(dev, "tx"); |
| 722 | if (IS_ERR(host->dma_chan_tx)) { |
Daniel Mack | 23f3ff7 | 2018-07-03 22:10:27 +0200 | [diff] [blame] | 723 | dev_err(dev, "unable to request tx dma channel\n"); |
Peter Ujfalusi | e1ebb45 | 2019-12-17 13:30:04 +0200 | [diff] [blame] | 724 | ret = PTR_ERR(host->dma_chan_tx); |
| 725 | host->dma_chan_tx = NULL; |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 726 | goto out; |
| 727 | } |
Bridge Wu | 9a788c6 | 2007-12-14 10:40:25 +0100 | [diff] [blame] | 728 | |
Robert Jarzmik | b405db6 | 2009-06-23 23:21:03 +0200 | [diff] [blame] | 729 | if (host->pdata) { |
Daniel Mack | 38a8dda | 2018-07-03 22:10:26 +0200 | [diff] [blame] | 730 | host->detect_delay_ms = host->pdata->detect_delay_ms; |
| 731 | |
Linus Walleij | f54005b | 2018-12-02 09:43:27 +0100 | [diff] [blame] | 732 | host->power = devm_gpiod_get_optional(dev, "power", GPIOD_OUT_LOW); |
| 733 | if (IS_ERR(host->power)) { |
| 734 | dev_err(dev, "Failed requesting gpio_power\n"); |
| 735 | goto out; |
Daniel Mack | 38a8dda | 2018-07-03 22:10:26 +0200 | [diff] [blame] | 736 | } |
| 737 | |
Linus Walleij | c914a27 | 2018-12-02 09:43:24 +0100 | [diff] [blame] | 738 | /* FIXME: should we pass detection delay to debounce? */ |
Michał Mirosław | d0052ad | 2019-12-11 03:40:56 +0100 | [diff] [blame] | 739 | ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0); |
Linus Walleij | c914a27 | 2018-12-02 09:43:24 +0100 | [diff] [blame] | 740 | if (ret && ret != -ENOENT) { |
| 741 | dev_err(dev, "Failed requesting gpio_cd\n"); |
| 742 | goto out; |
| 743 | } |
Linus Walleij | c914a27 | 2018-12-02 09:43:24 +0100 | [diff] [blame] | 744 | |
Michał Mirosław | 9073d10 | 2019-12-11 03:40:55 +0100 | [diff] [blame] | 745 | if (!host->pdata->gpio_card_ro_invert) |
| 746 | mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH; |
| 747 | |
Michał Mirosław | d0052ad | 2019-12-11 03:40:56 +0100 | [diff] [blame] | 748 | ret = mmc_gpiod_request_ro(mmc, "wp", 0, 0); |
Linus Walleij | c914a27 | 2018-12-02 09:43:24 +0100 | [diff] [blame] | 749 | if (ret && ret != -ENOENT) { |
| 750 | dev_err(dev, "Failed requesting gpio_ro\n"); |
| 751 | goto out; |
| 752 | } |
Michał Mirosław | 9073d10 | 2019-12-11 03:40:55 +0100 | [diff] [blame] | 753 | if (!ret) |
Linus Walleij | c914a27 | 2018-12-02 09:43:24 +0100 | [diff] [blame] | 754 | host->use_ro_gpio = true; |
Robert Jarzmik | b405db6 | 2009-06-23 23:21:03 +0200 | [diff] [blame] | 755 | |
Daniel Mack | 38a8dda | 2018-07-03 22:10:26 +0200 | [diff] [blame] | 756 | if (host->pdata->init) |
Daniel Mack | 23f3ff7 | 2018-07-03 22:10:27 +0200 | [diff] [blame] | 757 | host->pdata->init(dev, pxamci_detect_irq, mmc); |
Daniel Mack | 38a8dda | 2018-07-03 22:10:26 +0200 | [diff] [blame] | 758 | |
Linus Walleij | f54005b | 2018-12-02 09:43:27 +0100 | [diff] [blame] | 759 | if (host->power && host->pdata->setpower) |
Daniel Mack | 23f3ff7 | 2018-07-03 22:10:27 +0200 | [diff] [blame] | 760 | dev_warn(dev, "gpio_power and setpower() both defined\n"); |
Linus Walleij | c914a27 | 2018-12-02 09:43:24 +0100 | [diff] [blame] | 761 | if (host->use_ro_gpio && host->pdata->get_ro) |
Daniel Mack | 23f3ff7 | 2018-07-03 22:10:27 +0200 | [diff] [blame] | 762 | dev_warn(dev, "gpio_ro and get_ro() both defined\n"); |
Robert Jarzmik | b405db6 | 2009-06-23 23:21:03 +0200 | [diff] [blame] | 763 | } |
| 764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | mmc_add_host(mmc); |
| 766 | |
| 767 | return 0; |
| 768 | |
Robert Jarzmik | fd546ee | 2015-09-26 21:41:01 +0200 | [diff] [blame] | 769 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | if (host) { |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 771 | if (host->dma_chan_rx) |
| 772 | dma_release_channel(host->dma_chan_rx); |
| 773 | if (host->dma_chan_tx) |
| 774 | dma_release_channel(host->dma_chan_tx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | } |
| 776 | if (mmc) |
| 777 | mmc_free_host(mmc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | return ret; |
| 779 | } |
| 780 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 781 | static int pxamci_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 783 | struct mmc_host *mmc = platform_get_drvdata(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | if (mmc) { |
| 786 | struct pxamci_host *host = mmc_priv(mmc); |
| 787 | |
Daniel Mack | 5d6b1edf | 2009-12-01 18:17:18 +0100 | [diff] [blame] | 788 | mmc_remove_host(mmc); |
| 789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | if (host->pdata && host->pdata->exit) |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 791 | host->pdata->exit(&pdev->dev, mmc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | pxamci_stop_clock(host); |
| 794 | writel(TXFIFO_WR_REQ|RXFIFO_RD_REQ|CLK_IS_OFF|STOP_CMD| |
| 795 | END_CMD_RES|PRG_DONE|DATA_TRAN_DONE, |
| 796 | host->base + MMC_I_MASK); |
| 797 | |
Daniel Mack | 6464b71 | 2015-06-06 23:15:22 +0200 | [diff] [blame] | 798 | dmaengine_terminate_all(host->dma_chan_rx); |
| 799 | dmaengine_terminate_all(host->dma_chan_tx); |
| 800 | dma_release_channel(host->dma_chan_rx); |
| 801 | dma_release_channel(host->dma_chan_tx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | |
| 803 | mmc_free_host(mmc); |
| 804 | } |
Daniel Mack | 52c0918 | 2018-06-30 20:14:01 +0200 | [diff] [blame] | 805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | return 0; |
| 807 | } |
| 808 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 809 | static struct platform_driver pxamci_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | .probe = pxamci_probe, |
| 811 | .remove = pxamci_remove, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 812 | .driver = { |
| 813 | .name = DRIVER_NAME, |
Douglas Anderson | 21b2cec | 2020-09-03 16:24:36 -0700 | [diff] [blame] | 814 | .probe_type = PROBE_PREFER_ASYNCHRONOUS, |
Daniel Mack | e6027b4 | 2012-07-28 12:07:34 +0200 | [diff] [blame] | 815 | .of_match_table = of_match_ptr(pxa_mmc_dt_ids), |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 816 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | }; |
| 818 | |
Axel Lin | d1f81a6 | 2011-11-26 12:55:43 +0800 | [diff] [blame] | 819 | module_platform_driver(pxamci_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
| 821 | MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver"); |
| 822 | MODULE_LICENSE("GPL"); |
Kay Sievers | bc65c72 | 2008-04-15 14:34:28 -0700 | [diff] [blame] | 823 | MODULE_ALIAS("platform:pxa2xx-mci"); |