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