Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Portions copyright (C) 2003 Russell King, PXA MMCI Driver |
| 3 | * Portions copyright (C) 2004-2005 Pierre Ossman, W83L51xD SD/MMC driver |
| 4 | * |
| 5 | * Copyright 2008 Embedded Alley Solutions, Inc. |
| 6 | * Copyright 2009-2011 Freescale Semiconductor, Inc. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/ioport.h> |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 26 | #include <linux/of.h> |
| 27 | #include <linux/of_device.h> |
| 28 | #include <linux/of_gpio.h> |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 29 | #include <linux/platform_device.h> |
| 30 | #include <linux/delay.h> |
| 31 | #include <linux/interrupt.h> |
| 32 | #include <linux/dma-mapping.h> |
| 33 | #include <linux/dmaengine.h> |
| 34 | #include <linux/highmem.h> |
| 35 | #include <linux/clk.h> |
| 36 | #include <linux/err.h> |
| 37 | #include <linux/completion.h> |
| 38 | #include <linux/mmc/host.h> |
| 39 | #include <linux/mmc/mmc.h> |
| 40 | #include <linux/mmc/sdio.h> |
Sascha Hauer | abd37cc | 2013-12-05 14:34:50 +0100 | [diff] [blame] | 41 | #include <linux/mmc/slot-gpio.h> |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 42 | #include <linux/gpio.h> |
| 43 | #include <linux/regulator/consumer.h> |
Paul Gortmaker | 88b4767 | 2011-07-03 15:15:51 -0400 | [diff] [blame] | 44 | #include <linux/module.h> |
Shawn Guo | 70e6020 | 2012-05-05 19:40:09 +0800 | [diff] [blame] | 45 | #include <linux/stmp_device.h> |
Marek Vasut | 8be3d3b | 2012-08-03 17:26:06 +0200 | [diff] [blame] | 46 | #include <linux/spi/mxs-spi.h> |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 47 | |
| 48 | #define DRIVER_NAME "mxs-mmc" |
| 49 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 50 | #define MXS_MMC_IRQ_BITS (BM_SSP_CTRL1_SDIO_IRQ | \ |
| 51 | BM_SSP_CTRL1_RESP_ERR_IRQ | \ |
| 52 | BM_SSP_CTRL1_RESP_TIMEOUT_IRQ | \ |
| 53 | BM_SSP_CTRL1_DATA_TIMEOUT_IRQ | \ |
| 54 | BM_SSP_CTRL1_DATA_CRC_IRQ | \ |
| 55 | BM_SSP_CTRL1_FIFO_UNDERRUN_IRQ | \ |
| 56 | BM_SSP_CTRL1_RECV_TIMEOUT_IRQ | \ |
| 57 | BM_SSP_CTRL1_FIFO_OVERRUN_IRQ) |
| 58 | |
Marek Vasut | 8be3d3b | 2012-08-03 17:26:06 +0200 | [diff] [blame] | 59 | /* card detect polling timeout */ |
| 60 | #define MXS_MMC_DETECT_TIMEOUT (HZ/2) |
Shawn Guo | ef9b4d3 | 2012-05-05 20:24:01 +0800 | [diff] [blame] | 61 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 62 | struct mxs_mmc_host { |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 63 | struct mxs_ssp ssp; |
| 64 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 65 | struct mmc_host *mmc; |
| 66 | struct mmc_request *mrq; |
| 67 | struct mmc_command *cmd; |
| 68 | struct mmc_data *data; |
| 69 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 70 | unsigned char bus_width; |
| 71 | spinlock_t lock; |
| 72 | int sdio_irq_en; |
| 73 | }; |
| 74 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 75 | static int mxs_mmc_get_cd(struct mmc_host *mmc) |
| 76 | { |
| 77 | struct mxs_mmc_host *host = mmc_priv(mmc); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 78 | struct mxs_ssp *ssp = &host->ssp; |
Sascha Hauer | d1a1dfb | 2013-12-05 14:34:51 +0100 | [diff] [blame] | 79 | int present, ret; |
| 80 | |
| 81 | ret = mmc_gpio_get_cd(mmc); |
| 82 | if (ret >= 0) |
| 83 | return ret; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 84 | |
Sascha Hauer | 6c3331d | 2013-12-05 14:34:49 +0100 | [diff] [blame] | 85 | present = !(readl(ssp->base + HW_SSP_STATUS(ssp)) & |
| 86 | BM_SSP_STATUS_CARD_DETECT); |
| 87 | |
| 88 | if (mmc->caps2 & MMC_CAP2_CD_ACTIVE_HIGH) |
| 89 | present = !present; |
| 90 | |
| 91 | return present; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 92 | } |
| 93 | |
Fabio Estevam | 02c18c4 | 2013-07-03 17:02:38 -0300 | [diff] [blame] | 94 | static int mxs_mmc_reset(struct mxs_mmc_host *host) |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 95 | { |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 96 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 97 | u32 ctrl0, ctrl1; |
Fabio Estevam | 02c18c4 | 2013-07-03 17:02:38 -0300 | [diff] [blame] | 98 | int ret; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 99 | |
Fabio Estevam | 02c18c4 | 2013-07-03 17:02:38 -0300 | [diff] [blame] | 100 | ret = stmp_reset_block(ssp->base); |
| 101 | if (ret) |
| 102 | return ret; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 103 | |
| 104 | ctrl0 = BM_SSP_CTRL0_IGNORE_CRC; |
| 105 | ctrl1 = BF_SSP(0x3, CTRL1_SSP_MODE) | |
| 106 | BF_SSP(0x7, CTRL1_WORD_LENGTH) | |
| 107 | BM_SSP_CTRL1_DMA_ENABLE | |
| 108 | BM_SSP_CTRL1_POLARITY | |
| 109 | BM_SSP_CTRL1_RECV_TIMEOUT_IRQ_EN | |
| 110 | BM_SSP_CTRL1_DATA_CRC_IRQ_EN | |
| 111 | BM_SSP_CTRL1_DATA_TIMEOUT_IRQ_EN | |
| 112 | BM_SSP_CTRL1_RESP_TIMEOUT_IRQ_EN | |
| 113 | BM_SSP_CTRL1_RESP_ERR_IRQ_EN; |
| 114 | |
| 115 | writel(BF_SSP(0xffff, TIMING_TIMEOUT) | |
| 116 | BF_SSP(2, TIMING_CLOCK_DIVIDE) | |
| 117 | BF_SSP(0, TIMING_CLOCK_RATE), |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 118 | ssp->base + HW_SSP_TIMING(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 119 | |
| 120 | if (host->sdio_irq_en) { |
| 121 | ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK; |
| 122 | ctrl1 |= BM_SSP_CTRL1_SDIO_IRQ_EN; |
| 123 | } |
| 124 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 125 | writel(ctrl0, ssp->base + HW_SSP_CTRL0); |
| 126 | writel(ctrl1, ssp->base + HW_SSP_CTRL1(ssp)); |
Fabio Estevam | 02c18c4 | 2013-07-03 17:02:38 -0300 | [diff] [blame] | 127 | return 0; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static void mxs_mmc_start_cmd(struct mxs_mmc_host *host, |
| 131 | struct mmc_command *cmd); |
| 132 | |
| 133 | static void mxs_mmc_request_done(struct mxs_mmc_host *host) |
| 134 | { |
| 135 | struct mmc_command *cmd = host->cmd; |
| 136 | struct mmc_data *data = host->data; |
| 137 | struct mmc_request *mrq = host->mrq; |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 138 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 139 | |
| 140 | if (mmc_resp_type(cmd) & MMC_RSP_PRESENT) { |
| 141 | if (mmc_resp_type(cmd) & MMC_RSP_136) { |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 142 | cmd->resp[3] = readl(ssp->base + HW_SSP_SDRESP0(ssp)); |
| 143 | cmd->resp[2] = readl(ssp->base + HW_SSP_SDRESP1(ssp)); |
| 144 | cmd->resp[1] = readl(ssp->base + HW_SSP_SDRESP2(ssp)); |
| 145 | cmd->resp[0] = readl(ssp->base + HW_SSP_SDRESP3(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 146 | } else { |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 147 | cmd->resp[0] = readl(ssp->base + HW_SSP_SDRESP0(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | if (data) { |
| 152 | dma_unmap_sg(mmc_dev(host->mmc), data->sg, |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 153 | data->sg_len, ssp->dma_dir); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 154 | /* |
| 155 | * If there was an error on any block, we mark all |
| 156 | * data blocks as being in error. |
| 157 | */ |
| 158 | if (!data->error) |
| 159 | data->bytes_xfered = data->blocks * data->blksz; |
| 160 | else |
| 161 | data->bytes_xfered = 0; |
| 162 | |
| 163 | host->data = NULL; |
| 164 | if (mrq->stop) { |
| 165 | mxs_mmc_start_cmd(host, mrq->stop); |
| 166 | return; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | host->mrq = NULL; |
| 171 | mmc_request_done(host->mmc, mrq); |
| 172 | } |
| 173 | |
| 174 | static void mxs_mmc_dma_irq_callback(void *param) |
| 175 | { |
| 176 | struct mxs_mmc_host *host = param; |
| 177 | |
| 178 | mxs_mmc_request_done(host); |
| 179 | } |
| 180 | |
| 181 | static irqreturn_t mxs_mmc_irq_handler(int irq, void *dev_id) |
| 182 | { |
| 183 | struct mxs_mmc_host *host = dev_id; |
| 184 | struct mmc_command *cmd = host->cmd; |
| 185 | struct mmc_data *data = host->data; |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 186 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 187 | u32 stat; |
| 188 | |
| 189 | spin_lock(&host->lock); |
| 190 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 191 | stat = readl(ssp->base + HW_SSP_CTRL1(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 192 | writel(stat & MXS_MMC_IRQ_BITS, |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 193 | ssp->base + HW_SSP_CTRL1(ssp) + STMP_OFFSET_REG_CLR); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 194 | |
Lauri Hintsala | 1af36b2 | 2012-07-17 17:16:09 +0300 | [diff] [blame] | 195 | spin_unlock(&host->lock); |
| 196 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 197 | if ((stat & BM_SSP_CTRL1_SDIO_IRQ) && (stat & BM_SSP_CTRL1_SDIO_IRQ_EN)) |
| 198 | mmc_signal_sdio_irq(host->mmc); |
| 199 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 200 | if (stat & BM_SSP_CTRL1_RESP_TIMEOUT_IRQ) |
| 201 | cmd->error = -ETIMEDOUT; |
| 202 | else if (stat & BM_SSP_CTRL1_RESP_ERR_IRQ) |
| 203 | cmd->error = -EIO; |
| 204 | |
| 205 | if (data) { |
| 206 | if (stat & (BM_SSP_CTRL1_DATA_TIMEOUT_IRQ | |
| 207 | BM_SSP_CTRL1_RECV_TIMEOUT_IRQ)) |
| 208 | data->error = -ETIMEDOUT; |
| 209 | else if (stat & BM_SSP_CTRL1_DATA_CRC_IRQ) |
| 210 | data->error = -EILSEQ; |
| 211 | else if (stat & (BM_SSP_CTRL1_FIFO_UNDERRUN_IRQ | |
| 212 | BM_SSP_CTRL1_FIFO_OVERRUN_IRQ)) |
| 213 | data->error = -EIO; |
| 214 | } |
| 215 | |
| 216 | return IRQ_HANDLED; |
| 217 | } |
| 218 | |
| 219 | static struct dma_async_tx_descriptor *mxs_mmc_prep_dma( |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 220 | struct mxs_mmc_host *host, unsigned long flags) |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 221 | { |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 222 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 223 | struct dma_async_tx_descriptor *desc; |
| 224 | struct mmc_data *data = host->data; |
| 225 | struct scatterlist * sgl; |
| 226 | unsigned int sg_len; |
| 227 | |
| 228 | if (data) { |
| 229 | /* data */ |
| 230 | dma_map_sg(mmc_dev(host->mmc), data->sg, |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 231 | data->sg_len, ssp->dma_dir); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 232 | sgl = data->sg; |
| 233 | sg_len = data->sg_len; |
| 234 | } else { |
| 235 | /* pio */ |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 236 | sgl = (struct scatterlist *) ssp->ssp_pio_words; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 237 | sg_len = SSP_PIO_NUM; |
| 238 | } |
| 239 | |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 240 | desc = dmaengine_prep_slave_sg(ssp->dmach, |
| 241 | sgl, sg_len, ssp->slave_dirn, flags); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 242 | if (desc) { |
| 243 | desc->callback = mxs_mmc_dma_irq_callback; |
| 244 | desc->callback_param = host; |
| 245 | } else { |
| 246 | if (data) |
| 247 | dma_unmap_sg(mmc_dev(host->mmc), data->sg, |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 248 | data->sg_len, ssp->dma_dir); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | return desc; |
| 252 | } |
| 253 | |
| 254 | static void mxs_mmc_bc(struct mxs_mmc_host *host) |
| 255 | { |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 256 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 257 | struct mmc_command *cmd = host->cmd; |
| 258 | struct dma_async_tx_descriptor *desc; |
| 259 | u32 ctrl0, cmd0, cmd1; |
| 260 | |
| 261 | ctrl0 = BM_SSP_CTRL0_ENABLE | BM_SSP_CTRL0_IGNORE_CRC; |
| 262 | cmd0 = BF_SSP(cmd->opcode, CMD0_CMD) | BM_SSP_CMD0_APPEND_8CYC; |
| 263 | cmd1 = cmd->arg; |
| 264 | |
| 265 | if (host->sdio_irq_en) { |
| 266 | ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK; |
| 267 | cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN; |
| 268 | } |
| 269 | |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 270 | ssp->ssp_pio_words[0] = ctrl0; |
| 271 | ssp->ssp_pio_words[1] = cmd0; |
| 272 | ssp->ssp_pio_words[2] = cmd1; |
| 273 | ssp->dma_dir = DMA_NONE; |
| 274 | ssp->slave_dirn = DMA_TRANS_NONE; |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 275 | desc = mxs_mmc_prep_dma(host, DMA_CTRL_ACK); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 276 | if (!desc) |
| 277 | goto out; |
| 278 | |
| 279 | dmaengine_submit(desc); |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 280 | dma_async_issue_pending(ssp->dmach); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 281 | return; |
| 282 | |
| 283 | out: |
| 284 | dev_warn(mmc_dev(host->mmc), |
| 285 | "%s: failed to prep dma\n", __func__); |
| 286 | } |
| 287 | |
| 288 | static void mxs_mmc_ac(struct mxs_mmc_host *host) |
| 289 | { |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 290 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 291 | struct mmc_command *cmd = host->cmd; |
| 292 | struct dma_async_tx_descriptor *desc; |
| 293 | u32 ignore_crc, get_resp, long_resp; |
| 294 | u32 ctrl0, cmd0, cmd1; |
| 295 | |
| 296 | ignore_crc = (mmc_resp_type(cmd) & MMC_RSP_CRC) ? |
| 297 | 0 : BM_SSP_CTRL0_IGNORE_CRC; |
| 298 | get_resp = (mmc_resp_type(cmd) & MMC_RSP_PRESENT) ? |
| 299 | BM_SSP_CTRL0_GET_RESP : 0; |
| 300 | long_resp = (mmc_resp_type(cmd) & MMC_RSP_136) ? |
| 301 | BM_SSP_CTRL0_LONG_RESP : 0; |
| 302 | |
| 303 | ctrl0 = BM_SSP_CTRL0_ENABLE | ignore_crc | get_resp | long_resp; |
| 304 | cmd0 = BF_SSP(cmd->opcode, CMD0_CMD); |
| 305 | cmd1 = cmd->arg; |
| 306 | |
| 307 | if (host->sdio_irq_en) { |
| 308 | ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK; |
| 309 | cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN; |
| 310 | } |
| 311 | |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 312 | ssp->ssp_pio_words[0] = ctrl0; |
| 313 | ssp->ssp_pio_words[1] = cmd0; |
| 314 | ssp->ssp_pio_words[2] = cmd1; |
| 315 | ssp->dma_dir = DMA_NONE; |
| 316 | ssp->slave_dirn = DMA_TRANS_NONE; |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 317 | desc = mxs_mmc_prep_dma(host, DMA_CTRL_ACK); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 318 | if (!desc) |
| 319 | goto out; |
| 320 | |
| 321 | dmaengine_submit(desc); |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 322 | dma_async_issue_pending(ssp->dmach); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 323 | return; |
| 324 | |
| 325 | out: |
| 326 | dev_warn(mmc_dev(host->mmc), |
| 327 | "%s: failed to prep dma\n", __func__); |
| 328 | } |
| 329 | |
| 330 | static unsigned short mxs_ns_to_ssp_ticks(unsigned clock_rate, unsigned ns) |
| 331 | { |
| 332 | const unsigned int ssp_timeout_mul = 4096; |
| 333 | /* |
| 334 | * Calculate ticks in ms since ns are large numbers |
| 335 | * and might overflow |
| 336 | */ |
| 337 | const unsigned int clock_per_ms = clock_rate / 1000; |
| 338 | const unsigned int ms = ns / 1000; |
| 339 | const unsigned int ticks = ms * clock_per_ms; |
| 340 | const unsigned int ssp_ticks = ticks / ssp_timeout_mul; |
| 341 | |
| 342 | WARN_ON(ssp_ticks == 0); |
| 343 | return ssp_ticks; |
| 344 | } |
| 345 | |
| 346 | static void mxs_mmc_adtc(struct mxs_mmc_host *host) |
| 347 | { |
| 348 | struct mmc_command *cmd = host->cmd; |
| 349 | struct mmc_data *data = cmd->data; |
| 350 | struct dma_async_tx_descriptor *desc; |
| 351 | struct scatterlist *sgl = data->sg, *sg; |
| 352 | unsigned int sg_len = data->sg_len; |
Fabio Estevam | fd63ac7 | 2013-01-07 22:42:35 -0200 | [diff] [blame] | 353 | unsigned int i; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 354 | |
| 355 | unsigned short dma_data_dir, timeout; |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 356 | enum dma_transfer_direction slave_dirn; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 357 | unsigned int data_size = 0, log2_blksz; |
| 358 | unsigned int blocks = data->blocks; |
| 359 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 360 | struct mxs_ssp *ssp = &host->ssp; |
| 361 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 362 | u32 ignore_crc, get_resp, long_resp, read; |
| 363 | u32 ctrl0, cmd0, cmd1, val; |
| 364 | |
| 365 | ignore_crc = (mmc_resp_type(cmd) & MMC_RSP_CRC) ? |
| 366 | 0 : BM_SSP_CTRL0_IGNORE_CRC; |
| 367 | get_resp = (mmc_resp_type(cmd) & MMC_RSP_PRESENT) ? |
| 368 | BM_SSP_CTRL0_GET_RESP : 0; |
| 369 | long_resp = (mmc_resp_type(cmd) & MMC_RSP_136) ? |
| 370 | BM_SSP_CTRL0_LONG_RESP : 0; |
| 371 | |
| 372 | if (data->flags & MMC_DATA_WRITE) { |
| 373 | dma_data_dir = DMA_TO_DEVICE; |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 374 | slave_dirn = DMA_MEM_TO_DEV; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 375 | read = 0; |
| 376 | } else { |
| 377 | dma_data_dir = DMA_FROM_DEVICE; |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 378 | slave_dirn = DMA_DEV_TO_MEM; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 379 | read = BM_SSP_CTRL0_READ; |
| 380 | } |
| 381 | |
| 382 | ctrl0 = BF_SSP(host->bus_width, CTRL0_BUS_WIDTH) | |
| 383 | ignore_crc | get_resp | long_resp | |
| 384 | BM_SSP_CTRL0_DATA_XFER | read | |
| 385 | BM_SSP_CTRL0_WAIT_FOR_IRQ | |
| 386 | BM_SSP_CTRL0_ENABLE; |
| 387 | |
| 388 | cmd0 = BF_SSP(cmd->opcode, CMD0_CMD); |
| 389 | |
| 390 | /* get logarithm to base 2 of block size for setting register */ |
| 391 | log2_blksz = ilog2(data->blksz); |
| 392 | |
| 393 | /* |
| 394 | * take special care of the case that data size from data->sg |
| 395 | * is not equal to blocks x blksz |
| 396 | */ |
| 397 | for_each_sg(sgl, sg, sg_len, i) |
| 398 | data_size += sg->length; |
| 399 | |
| 400 | if (data_size != data->blocks * data->blksz) |
| 401 | blocks = 1; |
| 402 | |
| 403 | /* xfer count, block size and count need to be set differently */ |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 404 | if (ssp_is_old(ssp)) { |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 405 | ctrl0 |= BF_SSP(data_size, CTRL0_XFER_COUNT); |
| 406 | cmd0 |= BF_SSP(log2_blksz, CMD0_BLOCK_SIZE) | |
| 407 | BF_SSP(blocks - 1, CMD0_BLOCK_COUNT); |
| 408 | } else { |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 409 | writel(data_size, ssp->base + HW_SSP_XFER_SIZE); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 410 | writel(BF_SSP(log2_blksz, BLOCK_SIZE_BLOCK_SIZE) | |
| 411 | BF_SSP(blocks - 1, BLOCK_SIZE_BLOCK_COUNT), |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 412 | ssp->base + HW_SSP_BLOCK_SIZE); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | if ((cmd->opcode == MMC_STOP_TRANSMISSION) || |
| 416 | (cmd->opcode == SD_IO_RW_EXTENDED)) |
| 417 | cmd0 |= BM_SSP_CMD0_APPEND_8CYC; |
| 418 | |
| 419 | cmd1 = cmd->arg; |
| 420 | |
| 421 | if (host->sdio_irq_en) { |
| 422 | ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK; |
| 423 | cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN; |
| 424 | } |
| 425 | |
| 426 | /* set the timeout count */ |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 427 | timeout = mxs_ns_to_ssp_ticks(ssp->clk_rate, data->timeout_ns); |
| 428 | val = readl(ssp->base + HW_SSP_TIMING(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 429 | val &= ~(BM_SSP_TIMING_TIMEOUT); |
| 430 | val |= BF_SSP(timeout, TIMING_TIMEOUT); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 431 | writel(val, ssp->base + HW_SSP_TIMING(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 432 | |
| 433 | /* pio */ |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 434 | ssp->ssp_pio_words[0] = ctrl0; |
| 435 | ssp->ssp_pio_words[1] = cmd0; |
| 436 | ssp->ssp_pio_words[2] = cmd1; |
| 437 | ssp->dma_dir = DMA_NONE; |
| 438 | ssp->slave_dirn = DMA_TRANS_NONE; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 439 | desc = mxs_mmc_prep_dma(host, 0); |
| 440 | if (!desc) |
| 441 | goto out; |
| 442 | |
| 443 | /* append data sg */ |
| 444 | WARN_ON(host->data != NULL); |
| 445 | host->data = data; |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 446 | ssp->dma_dir = dma_data_dir; |
| 447 | ssp->slave_dirn = slave_dirn; |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 448 | desc = mxs_mmc_prep_dma(host, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 449 | if (!desc) |
| 450 | goto out; |
| 451 | |
| 452 | dmaengine_submit(desc); |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 453 | dma_async_issue_pending(ssp->dmach); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 454 | return; |
| 455 | out: |
| 456 | dev_warn(mmc_dev(host->mmc), |
| 457 | "%s: failed to prep dma\n", __func__); |
| 458 | } |
| 459 | |
| 460 | static void mxs_mmc_start_cmd(struct mxs_mmc_host *host, |
| 461 | struct mmc_command *cmd) |
| 462 | { |
| 463 | host->cmd = cmd; |
| 464 | |
| 465 | switch (mmc_cmd_type(cmd)) { |
| 466 | case MMC_CMD_BC: |
| 467 | mxs_mmc_bc(host); |
| 468 | break; |
| 469 | case MMC_CMD_BCR: |
| 470 | mxs_mmc_ac(host); |
| 471 | break; |
| 472 | case MMC_CMD_AC: |
| 473 | mxs_mmc_ac(host); |
| 474 | break; |
| 475 | case MMC_CMD_ADTC: |
| 476 | mxs_mmc_adtc(host); |
| 477 | break; |
| 478 | default: |
| 479 | dev_warn(mmc_dev(host->mmc), |
| 480 | "%s: unknown MMC command\n", __func__); |
| 481 | break; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | static void mxs_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 486 | { |
| 487 | struct mxs_mmc_host *host = mmc_priv(mmc); |
| 488 | |
| 489 | WARN_ON(host->mrq != NULL); |
| 490 | host->mrq = mrq; |
| 491 | mxs_mmc_start_cmd(host, mrq->cmd); |
| 492 | } |
| 493 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 494 | static void mxs_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 495 | { |
| 496 | struct mxs_mmc_host *host = mmc_priv(mmc); |
| 497 | |
| 498 | if (ios->bus_width == MMC_BUS_WIDTH_8) |
| 499 | host->bus_width = 2; |
| 500 | else if (ios->bus_width == MMC_BUS_WIDTH_4) |
| 501 | host->bus_width = 1; |
| 502 | else |
| 503 | host->bus_width = 0; |
| 504 | |
| 505 | if (ios->clock) |
Marek Vasut | 1308239 | 2012-08-03 17:26:10 +0200 | [diff] [blame] | 506 | mxs_ssp_set_clk_rate(&host->ssp, ios->clock); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | static void mxs_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable) |
| 510 | { |
| 511 | struct mxs_mmc_host *host = mmc_priv(mmc); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 512 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 513 | unsigned long flags; |
| 514 | |
| 515 | spin_lock_irqsave(&host->lock, flags); |
| 516 | |
| 517 | host->sdio_irq_en = enable; |
| 518 | |
| 519 | if (enable) { |
| 520 | writel(BM_SSP_CTRL0_SDIO_IRQ_CHECK, |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 521 | ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 522 | writel(BM_SSP_CTRL1_SDIO_IRQ_EN, |
Marek Vasut | 4c5bb2e | 2012-10-01 20:52:43 +0000 | [diff] [blame] | 523 | ssp->base + HW_SSP_CTRL1(ssp) + STMP_OFFSET_REG_SET); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 524 | } else { |
| 525 | writel(BM_SSP_CTRL0_SDIO_IRQ_CHECK, |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 526 | ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 527 | writel(BM_SSP_CTRL1_SDIO_IRQ_EN, |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 528 | ssp->base + HW_SSP_CTRL1(ssp) + STMP_OFFSET_REG_CLR); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | spin_unlock_irqrestore(&host->lock, flags); |
Lauri Hintsala | fc108d2 | 2012-07-17 17:16:10 +0300 | [diff] [blame] | 532 | |
Mark Brown | ac48f6c | 2012-09-25 13:41:02 +0100 | [diff] [blame] | 533 | if (enable && readl(ssp->base + HW_SSP_STATUS(ssp)) & |
Lauri Hintsala | fc108d2 | 2012-07-17 17:16:10 +0300 | [diff] [blame] | 534 | BM_SSP_STATUS_SDIO_IRQ) |
| 535 | mmc_signal_sdio_irq(host->mmc); |
| 536 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static const struct mmc_host_ops mxs_mmc_ops = { |
| 540 | .request = mxs_mmc_request, |
Sascha Hauer | abd37cc | 2013-12-05 14:34:50 +0100 | [diff] [blame] | 541 | .get_ro = mmc_gpio_get_ro, |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 542 | .get_cd = mxs_mmc_get_cd, |
| 543 | .set_ios = mxs_mmc_set_ios, |
| 544 | .enable_sdio_irq = mxs_mmc_enable_sdio_irq, |
| 545 | }; |
| 546 | |
Marek Vasut | 600a991 | 2012-08-03 17:26:07 +0200 | [diff] [blame] | 547 | static struct platform_device_id mxs_ssp_ids[] = { |
Shawn Guo | ef9b4d3 | 2012-05-05 20:24:01 +0800 | [diff] [blame] | 548 | { |
| 549 | .name = "imx23-mmc", |
Marek Vasut | 600a991 | 2012-08-03 17:26:07 +0200 | [diff] [blame] | 550 | .driver_data = IMX23_SSP, |
Shawn Guo | ef9b4d3 | 2012-05-05 20:24:01 +0800 | [diff] [blame] | 551 | }, { |
| 552 | .name = "imx28-mmc", |
Marek Vasut | 600a991 | 2012-08-03 17:26:07 +0200 | [diff] [blame] | 553 | .driver_data = IMX28_SSP, |
Shawn Guo | ef9b4d3 | 2012-05-05 20:24:01 +0800 | [diff] [blame] | 554 | }, { |
| 555 | /* sentinel */ |
| 556 | } |
| 557 | }; |
Marek Vasut | 600a991 | 2012-08-03 17:26:07 +0200 | [diff] [blame] | 558 | MODULE_DEVICE_TABLE(platform, mxs_ssp_ids); |
Shawn Guo | ef9b4d3 | 2012-05-05 20:24:01 +0800 | [diff] [blame] | 559 | |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 560 | static const struct of_device_id mxs_mmc_dt_ids[] = { |
Marek Vasut | 600a991 | 2012-08-03 17:26:07 +0200 | [diff] [blame] | 561 | { .compatible = "fsl,imx23-mmc", .data = (void *) IMX23_SSP, }, |
| 562 | { .compatible = "fsl,imx28-mmc", .data = (void *) IMX28_SSP, }, |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 563 | { /* sentinel */ } |
| 564 | }; |
| 565 | MODULE_DEVICE_TABLE(of, mxs_mmc_dt_ids); |
| 566 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 567 | static int mxs_mmc_probe(struct platform_device *pdev) |
| 568 | { |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 569 | const struct of_device_id *of_id = |
| 570 | of_match_device(mxs_mmc_dt_ids, &pdev->dev); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 571 | struct mxs_mmc_host *host; |
| 572 | struct mmc_host *mmc; |
Shawn Guo | 0e91e43 | 2013-02-26 10:10:38 +0800 | [diff] [blame] | 573 | struct resource *iores; |
Sascha Hauer | d1a1dfb | 2013-12-05 14:34:51 +0100 | [diff] [blame] | 574 | int ret = 0, irq_err; |
Shawn Guo | 4dc5a79 | 2012-06-26 16:38:57 +0800 | [diff] [blame] | 575 | struct regulator *reg_vmmc; |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 576 | struct mxs_ssp *ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 577 | |
| 578 | iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 579 | irq_err = platform_get_irq(pdev, 0); |
Shawn Guo | 0e91e43 | 2013-02-26 10:10:38 +0800 | [diff] [blame] | 580 | if (!iores || irq_err < 0) |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 581 | return -EINVAL; |
| 582 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 583 | mmc = mmc_alloc_host(sizeof(struct mxs_mmc_host), &pdev->dev); |
Shawn Guo | df06bfc | 2012-05-06 11:20:40 +0800 | [diff] [blame] | 584 | if (!mmc) |
| 585 | return -ENOMEM; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 586 | |
| 587 | host = mmc_priv(mmc); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 588 | ssp = &host->ssp; |
| 589 | ssp->dev = &pdev->dev; |
Thierry Reding | a3e2cd7 | 2013-01-21 11:09:11 +0100 | [diff] [blame] | 590 | ssp->base = devm_ioremap_resource(&pdev->dev, iores); |
| 591 | if (IS_ERR(ssp->base)) { |
| 592 | ret = PTR_ERR(ssp->base); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 593 | goto out_mmc_free; |
| 594 | } |
| 595 | |
Shawn Guo | 0e91e43 | 2013-02-26 10:10:38 +0800 | [diff] [blame] | 596 | ssp->devid = (enum mxs_ssp_id) of_id->data; |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 597 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 598 | host->mmc = mmc; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 599 | host->sdio_irq_en = 0; |
| 600 | |
Shawn Guo | 4dc5a79 | 2012-06-26 16:38:57 +0800 | [diff] [blame] | 601 | reg_vmmc = devm_regulator_get(&pdev->dev, "vmmc"); |
| 602 | if (!IS_ERR(reg_vmmc)) { |
| 603 | ret = regulator_enable(reg_vmmc); |
| 604 | if (ret) { |
| 605 | dev_err(&pdev->dev, |
| 606 | "Failed to enable vmmc regulator: %d\n", ret); |
| 607 | goto out_mmc_free; |
| 608 | } |
| 609 | } |
| 610 | |
Fabio Estevam | a5adbff | 2013-07-03 17:02:37 -0300 | [diff] [blame] | 611 | ssp->clk = devm_clk_get(&pdev->dev, NULL); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 612 | if (IS_ERR(ssp->clk)) { |
| 613 | ret = PTR_ERR(ssp->clk); |
Shawn Guo | df06bfc | 2012-05-06 11:20:40 +0800 | [diff] [blame] | 614 | goto out_mmc_free; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 615 | } |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 616 | clk_prepare_enable(ssp->clk); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 617 | |
Fabio Estevam | 02c18c4 | 2013-07-03 17:02:38 -0300 | [diff] [blame] | 618 | ret = mxs_mmc_reset(host); |
| 619 | if (ret) { |
| 620 | dev_err(&pdev->dev, "Failed to reset mmc: %d\n", ret); |
| 621 | goto out_clk_disable; |
| 622 | } |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 623 | |
Shawn Guo | 0e91e43 | 2013-02-26 10:10:38 +0800 | [diff] [blame] | 624 | ssp->dmach = dma_request_slave_channel(&pdev->dev, "rx-tx"); |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 625 | if (!ssp->dmach) { |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 626 | dev_err(mmc_dev(host->mmc), |
| 627 | "%s: failed to request dma\n", __func__); |
Wei Yongjun | 0c5ce16 | 2013-04-19 10:11:11 +0800 | [diff] [blame] | 628 | ret = -ENODEV; |
Fabio Estevam | a5adbff | 2013-07-03 17:02:37 -0300 | [diff] [blame] | 629 | goto out_clk_disable; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | /* set mmc core parameters */ |
| 633 | mmc->ops = &mxs_mmc_ops; |
| 634 | mmc->caps = MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED | |
| 635 | MMC_CAP_SDIO_IRQ | MMC_CAP_NEEDS_POLL; |
| 636 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 637 | mmc->f_min = 400000; |
| 638 | mmc->f_max = 288000000; |
Sascha Hauer | d1a1dfb | 2013-12-05 14:34:51 +0100 | [diff] [blame] | 639 | |
| 640 | ret = mmc_of_parse(mmc); |
| 641 | if (ret) |
| 642 | goto out_clk_disable; |
| 643 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 644 | mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 645 | |
| 646 | mmc->max_segs = 52; |
| 647 | mmc->max_blk_size = 1 << 0xf; |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 648 | mmc->max_blk_count = (ssp_is_old(ssp)) ? 0xff : 0xffffff; |
| 649 | mmc->max_req_size = (ssp_is_old(ssp)) ? 0xffff : 0xffffffff; |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 650 | mmc->max_seg_size = dma_get_max_seg_size(ssp->dmach->device->dev); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 651 | |
| 652 | platform_set_drvdata(pdev, mmc); |
| 653 | |
Shawn Guo | df06bfc | 2012-05-06 11:20:40 +0800 | [diff] [blame] | 654 | ret = devm_request_irq(&pdev->dev, irq_err, mxs_mmc_irq_handler, 0, |
| 655 | DRIVER_NAME, host); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 656 | if (ret) |
| 657 | goto out_free_dma; |
| 658 | |
| 659 | spin_lock_init(&host->lock); |
| 660 | |
| 661 | ret = mmc_add_host(mmc); |
| 662 | if (ret) |
Shawn Guo | df06bfc | 2012-05-06 11:20:40 +0800 | [diff] [blame] | 663 | goto out_free_dma; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 664 | |
| 665 | dev_info(mmc_dev(host->mmc), "initialized\n"); |
| 666 | |
| 667 | return 0; |
| 668 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 669 | out_free_dma: |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 670 | if (ssp->dmach) |
| 671 | dma_release_channel(ssp->dmach); |
Fabio Estevam | a5adbff | 2013-07-03 17:02:37 -0300 | [diff] [blame] | 672 | out_clk_disable: |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 673 | clk_disable_unprepare(ssp->clk); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 674 | out_mmc_free: |
| 675 | mmc_free_host(mmc); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 676 | return ret; |
| 677 | } |
| 678 | |
| 679 | static int mxs_mmc_remove(struct platform_device *pdev) |
| 680 | { |
| 681 | struct mmc_host *mmc = platform_get_drvdata(pdev); |
| 682 | struct mxs_mmc_host *host = mmc_priv(mmc); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 683 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 684 | |
| 685 | mmc_remove_host(mmc); |
| 686 | |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 687 | if (ssp->dmach) |
| 688 | dma_release_channel(ssp->dmach); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 689 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 690 | clk_disable_unprepare(ssp->clk); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 691 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 692 | mmc_free_host(mmc); |
| 693 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | #ifdef CONFIG_PM |
| 698 | static int mxs_mmc_suspend(struct device *dev) |
| 699 | { |
| 700 | struct mmc_host *mmc = dev_get_drvdata(dev); |
| 701 | struct mxs_mmc_host *host = mmc_priv(mmc); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 702 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 703 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 704 | clk_disable_unprepare(ssp->clk); |
Ulf Hansson | 6835907 | 2013-09-25 15:21:53 +0200 | [diff] [blame] | 705 | return 0; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | static int mxs_mmc_resume(struct device *dev) |
| 709 | { |
| 710 | struct mmc_host *mmc = dev_get_drvdata(dev); |
| 711 | struct mxs_mmc_host *host = mmc_priv(mmc); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 712 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 713 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 714 | clk_prepare_enable(ssp->clk); |
Ulf Hansson | 6835907 | 2013-09-25 15:21:53 +0200 | [diff] [blame] | 715 | return 0; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | static const struct dev_pm_ops mxs_mmc_pm_ops = { |
| 719 | .suspend = mxs_mmc_suspend, |
| 720 | .resume = mxs_mmc_resume, |
| 721 | }; |
| 722 | #endif |
| 723 | |
| 724 | static struct platform_driver mxs_mmc_driver = { |
| 725 | .probe = mxs_mmc_probe, |
| 726 | .remove = mxs_mmc_remove, |
Marek Vasut | 600a991 | 2012-08-03 17:26:07 +0200 | [diff] [blame] | 727 | .id_table = mxs_ssp_ids, |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 728 | .driver = { |
| 729 | .name = DRIVER_NAME, |
| 730 | .owner = THIS_MODULE, |
| 731 | #ifdef CONFIG_PM |
| 732 | .pm = &mxs_mmc_pm_ops, |
| 733 | #endif |
Marek Vasut | a3e545e | 2012-05-21 06:33:27 +0200 | [diff] [blame] | 734 | .of_match_table = mxs_mmc_dt_ids, |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 735 | }, |
| 736 | }; |
| 737 | |
Axel Lin | d1f81a6 | 2011-11-26 12:55:43 +0800 | [diff] [blame] | 738 | module_platform_driver(mxs_mmc_driver); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 739 | |
| 740 | MODULE_DESCRIPTION("FREESCALE MXS MMC peripheral"); |
| 741 | MODULE_AUTHOR("Freescale Semiconductor"); |
| 742 | MODULE_LICENSE("GPL"); |
Fabio Estevam | e7be434 | 2013-01-07 20:41:52 -0200 | [diff] [blame] | 743 | MODULE_ALIAS("platform:" DRIVER_NAME); |