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> |
| 41 | #include <linux/gpio.h> |
| 42 | #include <linux/regulator/consumer.h> |
Paul Gortmaker | 88b4767 | 2011-07-03 15:15:51 -0400 | [diff] [blame] | 43 | #include <linux/module.h> |
Shawn Guo | 9c92cf2 | 2012-05-06 22:56:16 +0800 | [diff] [blame] | 44 | #include <linux/pinctrl/consumer.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; |
Shawn Guo | 31b0ff5 | 2012-05-06 13:33:40 +0800 | [diff] [blame] | 73 | int wp_gpio; |
Marek Vasut | b6e76f1 | 2012-07-19 11:11:39 -0400 | [diff] [blame] | 74 | bool wp_inverted; |
Marc Kleine-Budde | 4c0c9be | 2013-04-10 11:13:43 +0200 | [diff] [blame] | 75 | bool cd_inverted; |
Marc Kleine-Budde | 5086e5f | 2013-04-10 11:13:44 +0200 | [diff] [blame^] | 76 | bool non_removable; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | static int mxs_mmc_get_ro(struct mmc_host *mmc) |
| 80 | { |
| 81 | struct mxs_mmc_host *host = mmc_priv(mmc); |
Marek Vasut | b6e76f1 | 2012-07-19 11:11:39 -0400 | [diff] [blame] | 82 | int ret; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 83 | |
Shawn Guo | 31b0ff5 | 2012-05-06 13:33:40 +0800 | [diff] [blame] | 84 | if (!gpio_is_valid(host->wp_gpio)) |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 85 | return -EINVAL; |
| 86 | |
Marek Vasut | b6e76f1 | 2012-07-19 11:11:39 -0400 | [diff] [blame] | 87 | ret = gpio_get_value(host->wp_gpio); |
| 88 | |
| 89 | if (host->wp_inverted) |
| 90 | ret = !ret; |
| 91 | |
| 92 | return ret; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static int mxs_mmc_get_cd(struct mmc_host *mmc) |
| 96 | { |
| 97 | struct mxs_mmc_host *host = mmc_priv(mmc); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 98 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 99 | |
Marc Kleine-Budde | 5086e5f | 2013-04-10 11:13:44 +0200 | [diff] [blame^] | 100 | return host->non_removable || |
| 101 | !(readl(ssp->base + HW_SSP_STATUS(ssp)) & |
| 102 | BM_SSP_STATUS_CARD_DETECT) ^ host->cd_inverted; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static void mxs_mmc_reset(struct mxs_mmc_host *host) |
| 106 | { |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 107 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 108 | u32 ctrl0, ctrl1; |
| 109 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 110 | stmp_reset_block(ssp->base); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 111 | |
| 112 | ctrl0 = BM_SSP_CTRL0_IGNORE_CRC; |
| 113 | ctrl1 = BF_SSP(0x3, CTRL1_SSP_MODE) | |
| 114 | BF_SSP(0x7, CTRL1_WORD_LENGTH) | |
| 115 | BM_SSP_CTRL1_DMA_ENABLE | |
| 116 | BM_SSP_CTRL1_POLARITY | |
| 117 | BM_SSP_CTRL1_RECV_TIMEOUT_IRQ_EN | |
| 118 | BM_SSP_CTRL1_DATA_CRC_IRQ_EN | |
| 119 | BM_SSP_CTRL1_DATA_TIMEOUT_IRQ_EN | |
| 120 | BM_SSP_CTRL1_RESP_TIMEOUT_IRQ_EN | |
| 121 | BM_SSP_CTRL1_RESP_ERR_IRQ_EN; |
| 122 | |
| 123 | writel(BF_SSP(0xffff, TIMING_TIMEOUT) | |
| 124 | BF_SSP(2, TIMING_CLOCK_DIVIDE) | |
| 125 | BF_SSP(0, TIMING_CLOCK_RATE), |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 126 | ssp->base + HW_SSP_TIMING(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 127 | |
| 128 | if (host->sdio_irq_en) { |
| 129 | ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK; |
| 130 | ctrl1 |= BM_SSP_CTRL1_SDIO_IRQ_EN; |
| 131 | } |
| 132 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 133 | writel(ctrl0, ssp->base + HW_SSP_CTRL0); |
| 134 | writel(ctrl1, ssp->base + HW_SSP_CTRL1(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static void mxs_mmc_start_cmd(struct mxs_mmc_host *host, |
| 138 | struct mmc_command *cmd); |
| 139 | |
| 140 | static void mxs_mmc_request_done(struct mxs_mmc_host *host) |
| 141 | { |
| 142 | struct mmc_command *cmd = host->cmd; |
| 143 | struct mmc_data *data = host->data; |
| 144 | struct mmc_request *mrq = host->mrq; |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 145 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 146 | |
| 147 | if (mmc_resp_type(cmd) & MMC_RSP_PRESENT) { |
| 148 | if (mmc_resp_type(cmd) & MMC_RSP_136) { |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 149 | cmd->resp[3] = readl(ssp->base + HW_SSP_SDRESP0(ssp)); |
| 150 | cmd->resp[2] = readl(ssp->base + HW_SSP_SDRESP1(ssp)); |
| 151 | cmd->resp[1] = readl(ssp->base + HW_SSP_SDRESP2(ssp)); |
| 152 | cmd->resp[0] = readl(ssp->base + HW_SSP_SDRESP3(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 153 | } else { |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 154 | cmd->resp[0] = readl(ssp->base + HW_SSP_SDRESP0(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | if (data) { |
| 159 | dma_unmap_sg(mmc_dev(host->mmc), data->sg, |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 160 | data->sg_len, ssp->dma_dir); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 161 | /* |
| 162 | * If there was an error on any block, we mark all |
| 163 | * data blocks as being in error. |
| 164 | */ |
| 165 | if (!data->error) |
| 166 | data->bytes_xfered = data->blocks * data->blksz; |
| 167 | else |
| 168 | data->bytes_xfered = 0; |
| 169 | |
| 170 | host->data = NULL; |
| 171 | if (mrq->stop) { |
| 172 | mxs_mmc_start_cmd(host, mrq->stop); |
| 173 | return; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | host->mrq = NULL; |
| 178 | mmc_request_done(host->mmc, mrq); |
| 179 | } |
| 180 | |
| 181 | static void mxs_mmc_dma_irq_callback(void *param) |
| 182 | { |
| 183 | struct mxs_mmc_host *host = param; |
| 184 | |
| 185 | mxs_mmc_request_done(host); |
| 186 | } |
| 187 | |
| 188 | static irqreturn_t mxs_mmc_irq_handler(int irq, void *dev_id) |
| 189 | { |
| 190 | struct mxs_mmc_host *host = dev_id; |
| 191 | struct mmc_command *cmd = host->cmd; |
| 192 | struct mmc_data *data = host->data; |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 193 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 194 | u32 stat; |
| 195 | |
| 196 | spin_lock(&host->lock); |
| 197 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 198 | stat = readl(ssp->base + HW_SSP_CTRL1(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 199 | writel(stat & MXS_MMC_IRQ_BITS, |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 200 | ssp->base + HW_SSP_CTRL1(ssp) + STMP_OFFSET_REG_CLR); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 201 | |
Lauri Hintsala | 1af36b2 | 2012-07-17 17:16:09 +0300 | [diff] [blame] | 202 | spin_unlock(&host->lock); |
| 203 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 204 | if ((stat & BM_SSP_CTRL1_SDIO_IRQ) && (stat & BM_SSP_CTRL1_SDIO_IRQ_EN)) |
| 205 | mmc_signal_sdio_irq(host->mmc); |
| 206 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 207 | if (stat & BM_SSP_CTRL1_RESP_TIMEOUT_IRQ) |
| 208 | cmd->error = -ETIMEDOUT; |
| 209 | else if (stat & BM_SSP_CTRL1_RESP_ERR_IRQ) |
| 210 | cmd->error = -EIO; |
| 211 | |
| 212 | if (data) { |
| 213 | if (stat & (BM_SSP_CTRL1_DATA_TIMEOUT_IRQ | |
| 214 | BM_SSP_CTRL1_RECV_TIMEOUT_IRQ)) |
| 215 | data->error = -ETIMEDOUT; |
| 216 | else if (stat & BM_SSP_CTRL1_DATA_CRC_IRQ) |
| 217 | data->error = -EILSEQ; |
| 218 | else if (stat & (BM_SSP_CTRL1_FIFO_UNDERRUN_IRQ | |
| 219 | BM_SSP_CTRL1_FIFO_OVERRUN_IRQ)) |
| 220 | data->error = -EIO; |
| 221 | } |
| 222 | |
| 223 | return IRQ_HANDLED; |
| 224 | } |
| 225 | |
| 226 | static struct dma_async_tx_descriptor *mxs_mmc_prep_dma( |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 227 | struct mxs_mmc_host *host, unsigned long flags) |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 228 | { |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 229 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 230 | struct dma_async_tx_descriptor *desc; |
| 231 | struct mmc_data *data = host->data; |
| 232 | struct scatterlist * sgl; |
| 233 | unsigned int sg_len; |
| 234 | |
| 235 | if (data) { |
| 236 | /* data */ |
| 237 | dma_map_sg(mmc_dev(host->mmc), data->sg, |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 238 | data->sg_len, ssp->dma_dir); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 239 | sgl = data->sg; |
| 240 | sg_len = data->sg_len; |
| 241 | } else { |
| 242 | /* pio */ |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 243 | sgl = (struct scatterlist *) ssp->ssp_pio_words; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 244 | sg_len = SSP_PIO_NUM; |
| 245 | } |
| 246 | |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 247 | desc = dmaengine_prep_slave_sg(ssp->dmach, |
| 248 | sgl, sg_len, ssp->slave_dirn, flags); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 249 | if (desc) { |
| 250 | desc->callback = mxs_mmc_dma_irq_callback; |
| 251 | desc->callback_param = host; |
| 252 | } else { |
| 253 | if (data) |
| 254 | dma_unmap_sg(mmc_dev(host->mmc), data->sg, |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 255 | data->sg_len, ssp->dma_dir); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | return desc; |
| 259 | } |
| 260 | |
| 261 | static void mxs_mmc_bc(struct mxs_mmc_host *host) |
| 262 | { |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 263 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 264 | struct mmc_command *cmd = host->cmd; |
| 265 | struct dma_async_tx_descriptor *desc; |
| 266 | u32 ctrl0, cmd0, cmd1; |
| 267 | |
| 268 | ctrl0 = BM_SSP_CTRL0_ENABLE | BM_SSP_CTRL0_IGNORE_CRC; |
| 269 | cmd0 = BF_SSP(cmd->opcode, CMD0_CMD) | BM_SSP_CMD0_APPEND_8CYC; |
| 270 | cmd1 = cmd->arg; |
| 271 | |
| 272 | if (host->sdio_irq_en) { |
| 273 | ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK; |
| 274 | cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN; |
| 275 | } |
| 276 | |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 277 | ssp->ssp_pio_words[0] = ctrl0; |
| 278 | ssp->ssp_pio_words[1] = cmd0; |
| 279 | ssp->ssp_pio_words[2] = cmd1; |
| 280 | ssp->dma_dir = DMA_NONE; |
| 281 | ssp->slave_dirn = DMA_TRANS_NONE; |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 282 | desc = mxs_mmc_prep_dma(host, DMA_CTRL_ACK); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 283 | if (!desc) |
| 284 | goto out; |
| 285 | |
| 286 | dmaengine_submit(desc); |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 287 | dma_async_issue_pending(ssp->dmach); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 288 | return; |
| 289 | |
| 290 | out: |
| 291 | dev_warn(mmc_dev(host->mmc), |
| 292 | "%s: failed to prep dma\n", __func__); |
| 293 | } |
| 294 | |
| 295 | static void mxs_mmc_ac(struct mxs_mmc_host *host) |
| 296 | { |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 297 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 298 | struct mmc_command *cmd = host->cmd; |
| 299 | struct dma_async_tx_descriptor *desc; |
| 300 | u32 ignore_crc, get_resp, long_resp; |
| 301 | u32 ctrl0, cmd0, cmd1; |
| 302 | |
| 303 | ignore_crc = (mmc_resp_type(cmd) & MMC_RSP_CRC) ? |
| 304 | 0 : BM_SSP_CTRL0_IGNORE_CRC; |
| 305 | get_resp = (mmc_resp_type(cmd) & MMC_RSP_PRESENT) ? |
| 306 | BM_SSP_CTRL0_GET_RESP : 0; |
| 307 | long_resp = (mmc_resp_type(cmd) & MMC_RSP_136) ? |
| 308 | BM_SSP_CTRL0_LONG_RESP : 0; |
| 309 | |
| 310 | ctrl0 = BM_SSP_CTRL0_ENABLE | ignore_crc | get_resp | long_resp; |
| 311 | cmd0 = BF_SSP(cmd->opcode, CMD0_CMD); |
| 312 | cmd1 = cmd->arg; |
| 313 | |
| 314 | if (host->sdio_irq_en) { |
| 315 | ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK; |
| 316 | cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN; |
| 317 | } |
| 318 | |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 319 | ssp->ssp_pio_words[0] = ctrl0; |
| 320 | ssp->ssp_pio_words[1] = cmd0; |
| 321 | ssp->ssp_pio_words[2] = cmd1; |
| 322 | ssp->dma_dir = DMA_NONE; |
| 323 | ssp->slave_dirn = DMA_TRANS_NONE; |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 324 | desc = mxs_mmc_prep_dma(host, DMA_CTRL_ACK); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 325 | if (!desc) |
| 326 | goto out; |
| 327 | |
| 328 | dmaengine_submit(desc); |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 329 | dma_async_issue_pending(ssp->dmach); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 330 | return; |
| 331 | |
| 332 | out: |
| 333 | dev_warn(mmc_dev(host->mmc), |
| 334 | "%s: failed to prep dma\n", __func__); |
| 335 | } |
| 336 | |
| 337 | static unsigned short mxs_ns_to_ssp_ticks(unsigned clock_rate, unsigned ns) |
| 338 | { |
| 339 | const unsigned int ssp_timeout_mul = 4096; |
| 340 | /* |
| 341 | * Calculate ticks in ms since ns are large numbers |
| 342 | * and might overflow |
| 343 | */ |
| 344 | const unsigned int clock_per_ms = clock_rate / 1000; |
| 345 | const unsigned int ms = ns / 1000; |
| 346 | const unsigned int ticks = ms * clock_per_ms; |
| 347 | const unsigned int ssp_ticks = ticks / ssp_timeout_mul; |
| 348 | |
| 349 | WARN_ON(ssp_ticks == 0); |
| 350 | return ssp_ticks; |
| 351 | } |
| 352 | |
| 353 | static void mxs_mmc_adtc(struct mxs_mmc_host *host) |
| 354 | { |
| 355 | struct mmc_command *cmd = host->cmd; |
| 356 | struct mmc_data *data = cmd->data; |
| 357 | struct dma_async_tx_descriptor *desc; |
| 358 | struct scatterlist *sgl = data->sg, *sg; |
| 359 | unsigned int sg_len = data->sg_len; |
Fabio Estevam | fd63ac7 | 2013-01-07 22:42:35 -0200 | [diff] [blame] | 360 | unsigned int i; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 361 | |
| 362 | unsigned short dma_data_dir, timeout; |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 363 | enum dma_transfer_direction slave_dirn; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 364 | unsigned int data_size = 0, log2_blksz; |
| 365 | unsigned int blocks = data->blocks; |
| 366 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 367 | struct mxs_ssp *ssp = &host->ssp; |
| 368 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 369 | u32 ignore_crc, get_resp, long_resp, read; |
| 370 | u32 ctrl0, cmd0, cmd1, val; |
| 371 | |
| 372 | ignore_crc = (mmc_resp_type(cmd) & MMC_RSP_CRC) ? |
| 373 | 0 : BM_SSP_CTRL0_IGNORE_CRC; |
| 374 | get_resp = (mmc_resp_type(cmd) & MMC_RSP_PRESENT) ? |
| 375 | BM_SSP_CTRL0_GET_RESP : 0; |
| 376 | long_resp = (mmc_resp_type(cmd) & MMC_RSP_136) ? |
| 377 | BM_SSP_CTRL0_LONG_RESP : 0; |
| 378 | |
| 379 | if (data->flags & MMC_DATA_WRITE) { |
| 380 | dma_data_dir = DMA_TO_DEVICE; |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 381 | slave_dirn = DMA_MEM_TO_DEV; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 382 | read = 0; |
| 383 | } else { |
| 384 | dma_data_dir = DMA_FROM_DEVICE; |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 385 | slave_dirn = DMA_DEV_TO_MEM; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 386 | read = BM_SSP_CTRL0_READ; |
| 387 | } |
| 388 | |
| 389 | ctrl0 = BF_SSP(host->bus_width, CTRL0_BUS_WIDTH) | |
| 390 | ignore_crc | get_resp | long_resp | |
| 391 | BM_SSP_CTRL0_DATA_XFER | read | |
| 392 | BM_SSP_CTRL0_WAIT_FOR_IRQ | |
| 393 | BM_SSP_CTRL0_ENABLE; |
| 394 | |
| 395 | cmd0 = BF_SSP(cmd->opcode, CMD0_CMD); |
| 396 | |
| 397 | /* get logarithm to base 2 of block size for setting register */ |
| 398 | log2_blksz = ilog2(data->blksz); |
| 399 | |
| 400 | /* |
| 401 | * take special care of the case that data size from data->sg |
| 402 | * is not equal to blocks x blksz |
| 403 | */ |
| 404 | for_each_sg(sgl, sg, sg_len, i) |
| 405 | data_size += sg->length; |
| 406 | |
| 407 | if (data_size != data->blocks * data->blksz) |
| 408 | blocks = 1; |
| 409 | |
| 410 | /* xfer count, block size and count need to be set differently */ |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 411 | if (ssp_is_old(ssp)) { |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 412 | ctrl0 |= BF_SSP(data_size, CTRL0_XFER_COUNT); |
| 413 | cmd0 |= BF_SSP(log2_blksz, CMD0_BLOCK_SIZE) | |
| 414 | BF_SSP(blocks - 1, CMD0_BLOCK_COUNT); |
| 415 | } else { |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 416 | writel(data_size, ssp->base + HW_SSP_XFER_SIZE); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 417 | writel(BF_SSP(log2_blksz, BLOCK_SIZE_BLOCK_SIZE) | |
| 418 | BF_SSP(blocks - 1, BLOCK_SIZE_BLOCK_COUNT), |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 419 | ssp->base + HW_SSP_BLOCK_SIZE); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | if ((cmd->opcode == MMC_STOP_TRANSMISSION) || |
| 423 | (cmd->opcode == SD_IO_RW_EXTENDED)) |
| 424 | cmd0 |= BM_SSP_CMD0_APPEND_8CYC; |
| 425 | |
| 426 | cmd1 = cmd->arg; |
| 427 | |
| 428 | if (host->sdio_irq_en) { |
| 429 | ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK; |
| 430 | cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN; |
| 431 | } |
| 432 | |
| 433 | /* set the timeout count */ |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 434 | timeout = mxs_ns_to_ssp_ticks(ssp->clk_rate, data->timeout_ns); |
| 435 | val = readl(ssp->base + HW_SSP_TIMING(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 436 | val &= ~(BM_SSP_TIMING_TIMEOUT); |
| 437 | val |= BF_SSP(timeout, TIMING_TIMEOUT); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 438 | writel(val, ssp->base + HW_SSP_TIMING(ssp)); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 439 | |
| 440 | /* pio */ |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 441 | ssp->ssp_pio_words[0] = ctrl0; |
| 442 | ssp->ssp_pio_words[1] = cmd0; |
| 443 | ssp->ssp_pio_words[2] = cmd1; |
| 444 | ssp->dma_dir = DMA_NONE; |
| 445 | ssp->slave_dirn = DMA_TRANS_NONE; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 446 | desc = mxs_mmc_prep_dma(host, 0); |
| 447 | if (!desc) |
| 448 | goto out; |
| 449 | |
| 450 | /* append data sg */ |
| 451 | WARN_ON(host->data != NULL); |
| 452 | host->data = data; |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 453 | ssp->dma_dir = dma_data_dir; |
| 454 | ssp->slave_dirn = slave_dirn; |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 455 | desc = mxs_mmc_prep_dma(host, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 456 | if (!desc) |
| 457 | goto out; |
| 458 | |
| 459 | dmaengine_submit(desc); |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 460 | dma_async_issue_pending(ssp->dmach); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 461 | return; |
| 462 | out: |
| 463 | dev_warn(mmc_dev(host->mmc), |
| 464 | "%s: failed to prep dma\n", __func__); |
| 465 | } |
| 466 | |
| 467 | static void mxs_mmc_start_cmd(struct mxs_mmc_host *host, |
| 468 | struct mmc_command *cmd) |
| 469 | { |
| 470 | host->cmd = cmd; |
| 471 | |
| 472 | switch (mmc_cmd_type(cmd)) { |
| 473 | case MMC_CMD_BC: |
| 474 | mxs_mmc_bc(host); |
| 475 | break; |
| 476 | case MMC_CMD_BCR: |
| 477 | mxs_mmc_ac(host); |
| 478 | break; |
| 479 | case MMC_CMD_AC: |
| 480 | mxs_mmc_ac(host); |
| 481 | break; |
| 482 | case MMC_CMD_ADTC: |
| 483 | mxs_mmc_adtc(host); |
| 484 | break; |
| 485 | default: |
| 486 | dev_warn(mmc_dev(host->mmc), |
| 487 | "%s: unknown MMC command\n", __func__); |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | static void mxs_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 493 | { |
| 494 | struct mxs_mmc_host *host = mmc_priv(mmc); |
| 495 | |
| 496 | WARN_ON(host->mrq != NULL); |
| 497 | host->mrq = mrq; |
| 498 | mxs_mmc_start_cmd(host, mrq->cmd); |
| 499 | } |
| 500 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 501 | static void mxs_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 502 | { |
| 503 | struct mxs_mmc_host *host = mmc_priv(mmc); |
| 504 | |
| 505 | if (ios->bus_width == MMC_BUS_WIDTH_8) |
| 506 | host->bus_width = 2; |
| 507 | else if (ios->bus_width == MMC_BUS_WIDTH_4) |
| 508 | host->bus_width = 1; |
| 509 | else |
| 510 | host->bus_width = 0; |
| 511 | |
| 512 | if (ios->clock) |
Marek Vasut | 1308239 | 2012-08-03 17:26:10 +0200 | [diff] [blame] | 513 | mxs_ssp_set_clk_rate(&host->ssp, ios->clock); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | static void mxs_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable) |
| 517 | { |
| 518 | struct mxs_mmc_host *host = mmc_priv(mmc); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 519 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 520 | unsigned long flags; |
| 521 | |
| 522 | spin_lock_irqsave(&host->lock, flags); |
| 523 | |
| 524 | host->sdio_irq_en = enable; |
| 525 | |
| 526 | if (enable) { |
| 527 | writel(BM_SSP_CTRL0_SDIO_IRQ_CHECK, |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 528 | ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 529 | writel(BM_SSP_CTRL1_SDIO_IRQ_EN, |
Marek Vasut | 4c5bb2e | 2012-10-01 20:52:43 +0000 | [diff] [blame] | 530 | ssp->base + HW_SSP_CTRL1(ssp) + STMP_OFFSET_REG_SET); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 531 | } else { |
| 532 | writel(BM_SSP_CTRL0_SDIO_IRQ_CHECK, |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 533 | ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 534 | writel(BM_SSP_CTRL1_SDIO_IRQ_EN, |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 535 | ssp->base + HW_SSP_CTRL1(ssp) + STMP_OFFSET_REG_CLR); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | spin_unlock_irqrestore(&host->lock, flags); |
Lauri Hintsala | fc108d2 | 2012-07-17 17:16:10 +0300 | [diff] [blame] | 539 | |
Mark Brown | ac48f6c | 2012-09-25 13:41:02 +0100 | [diff] [blame] | 540 | if (enable && readl(ssp->base + HW_SSP_STATUS(ssp)) & |
Lauri Hintsala | fc108d2 | 2012-07-17 17:16:10 +0300 | [diff] [blame] | 541 | BM_SSP_STATUS_SDIO_IRQ) |
| 542 | mmc_signal_sdio_irq(host->mmc); |
| 543 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | static const struct mmc_host_ops mxs_mmc_ops = { |
| 547 | .request = mxs_mmc_request, |
| 548 | .get_ro = mxs_mmc_get_ro, |
| 549 | .get_cd = mxs_mmc_get_cd, |
| 550 | .set_ios = mxs_mmc_set_ios, |
| 551 | .enable_sdio_irq = mxs_mmc_enable_sdio_irq, |
| 552 | }; |
| 553 | |
| 554 | static bool mxs_mmc_dma_filter(struct dma_chan *chan, void *param) |
| 555 | { |
| 556 | struct mxs_mmc_host *host = param; |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 557 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 558 | |
| 559 | if (!mxs_dma_is_apbh(chan)) |
| 560 | return false; |
| 561 | |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 562 | if (chan->chan_id != ssp->dma_channel) |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 563 | return false; |
| 564 | |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 565 | chan->private = &ssp->dma_data; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 566 | |
| 567 | return true; |
| 568 | } |
| 569 | |
Marek Vasut | 600a991 | 2012-08-03 17:26:07 +0200 | [diff] [blame] | 570 | static struct platform_device_id mxs_ssp_ids[] = { |
Shawn Guo | ef9b4d3 | 2012-05-05 20:24:01 +0800 | [diff] [blame] | 571 | { |
| 572 | .name = "imx23-mmc", |
Marek Vasut | 600a991 | 2012-08-03 17:26:07 +0200 | [diff] [blame] | 573 | .driver_data = IMX23_SSP, |
Shawn Guo | ef9b4d3 | 2012-05-05 20:24:01 +0800 | [diff] [blame] | 574 | }, { |
| 575 | .name = "imx28-mmc", |
Marek Vasut | 600a991 | 2012-08-03 17:26:07 +0200 | [diff] [blame] | 576 | .driver_data = IMX28_SSP, |
Shawn Guo | ef9b4d3 | 2012-05-05 20:24:01 +0800 | [diff] [blame] | 577 | }, { |
| 578 | /* sentinel */ |
| 579 | } |
| 580 | }; |
Marek Vasut | 600a991 | 2012-08-03 17:26:07 +0200 | [diff] [blame] | 581 | MODULE_DEVICE_TABLE(platform, mxs_ssp_ids); |
Shawn Guo | ef9b4d3 | 2012-05-05 20:24:01 +0800 | [diff] [blame] | 582 | |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 583 | static const struct of_device_id mxs_mmc_dt_ids[] = { |
Marek Vasut | 600a991 | 2012-08-03 17:26:07 +0200 | [diff] [blame] | 584 | { .compatible = "fsl,imx23-mmc", .data = (void *) IMX23_SSP, }, |
| 585 | { .compatible = "fsl,imx28-mmc", .data = (void *) IMX28_SSP, }, |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 586 | { /* sentinel */ } |
| 587 | }; |
| 588 | MODULE_DEVICE_TABLE(of, mxs_mmc_dt_ids); |
| 589 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 590 | static int mxs_mmc_probe(struct platform_device *pdev) |
| 591 | { |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 592 | const struct of_device_id *of_id = |
| 593 | of_match_device(mxs_mmc_dt_ids, &pdev->dev); |
| 594 | struct device_node *np = pdev->dev.of_node; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 595 | struct mxs_mmc_host *host; |
| 596 | struct mmc_host *mmc; |
Shawn Guo | df06bfc | 2012-05-06 11:20:40 +0800 | [diff] [blame] | 597 | struct resource *iores, *dmares; |
Shawn Guo | 9c92cf2 | 2012-05-06 22:56:16 +0800 | [diff] [blame] | 598 | struct pinctrl *pinctrl; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 599 | int ret = 0, irq_err, irq_dma; |
| 600 | dma_cap_mask_t mask; |
Shawn Guo | 4dc5a79 | 2012-06-26 16:38:57 +0800 | [diff] [blame] | 601 | struct regulator *reg_vmmc; |
Marek Vasut | b6e76f1 | 2012-07-19 11:11:39 -0400 | [diff] [blame] | 602 | enum of_gpio_flags flags; |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 603 | struct mxs_ssp *ssp; |
Fabio Estevam | d6ed91a | 2012-10-31 09:22:58 -0200 | [diff] [blame] | 604 | u32 bus_width = 0; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 605 | |
| 606 | iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 607 | dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 608 | irq_err = platform_get_irq(pdev, 0); |
| 609 | irq_dma = platform_get_irq(pdev, 1); |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 610 | if (!iores || irq_err < 0 || irq_dma < 0) |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 611 | return -EINVAL; |
| 612 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 613 | mmc = mmc_alloc_host(sizeof(struct mxs_mmc_host), &pdev->dev); |
Shawn Guo | df06bfc | 2012-05-06 11:20:40 +0800 | [diff] [blame] | 614 | if (!mmc) |
| 615 | return -ENOMEM; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 616 | |
| 617 | host = mmc_priv(mmc); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 618 | ssp = &host->ssp; |
| 619 | ssp->dev = &pdev->dev; |
Thierry Reding | a3e2cd7 | 2013-01-21 11:09:11 +0100 | [diff] [blame] | 620 | ssp->base = devm_ioremap_resource(&pdev->dev, iores); |
| 621 | if (IS_ERR(ssp->base)) { |
| 622 | ret = PTR_ERR(ssp->base); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 623 | goto out_mmc_free; |
| 624 | } |
| 625 | |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 626 | if (np) { |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 627 | ssp->devid = (enum mxs_ssp_id) of_id->data; |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 628 | /* |
| 629 | * TODO: This is a temporary solution and should be changed |
| 630 | * to use generic DMA binding later when the helpers get in. |
| 631 | */ |
| 632 | ret = of_property_read_u32(np, "fsl,ssp-dma-channel", |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 633 | &ssp->dma_channel); |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 634 | if (ret) { |
| 635 | dev_err(mmc_dev(host->mmc), |
| 636 | "failed to get dma channel\n"); |
| 637 | goto out_mmc_free; |
| 638 | } |
| 639 | } else { |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 640 | ssp->devid = pdev->id_entry->driver_data; |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 641 | ssp->dma_channel = dmares->start; |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 642 | } |
| 643 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 644 | host->mmc = mmc; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 645 | host->sdio_irq_en = 0; |
| 646 | |
Shawn Guo | 4dc5a79 | 2012-06-26 16:38:57 +0800 | [diff] [blame] | 647 | reg_vmmc = devm_regulator_get(&pdev->dev, "vmmc"); |
| 648 | if (!IS_ERR(reg_vmmc)) { |
| 649 | ret = regulator_enable(reg_vmmc); |
| 650 | if (ret) { |
| 651 | dev_err(&pdev->dev, |
| 652 | "Failed to enable vmmc regulator: %d\n", ret); |
| 653 | goto out_mmc_free; |
| 654 | } |
| 655 | } |
| 656 | |
Shawn Guo | 9c92cf2 | 2012-05-06 22:56:16 +0800 | [diff] [blame] | 657 | pinctrl = devm_pinctrl_get_select_default(&pdev->dev); |
| 658 | if (IS_ERR(pinctrl)) { |
| 659 | ret = PTR_ERR(pinctrl); |
Shawn Guo | 6de4d81 | 2012-05-06 13:30:44 +0800 | [diff] [blame] | 660 | goto out_mmc_free; |
Shawn Guo | 9c92cf2 | 2012-05-06 22:56:16 +0800 | [diff] [blame] | 661 | } |
| 662 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 663 | ssp->clk = clk_get(&pdev->dev, NULL); |
| 664 | if (IS_ERR(ssp->clk)) { |
| 665 | ret = PTR_ERR(ssp->clk); |
Shawn Guo | df06bfc | 2012-05-06 11:20:40 +0800 | [diff] [blame] | 666 | goto out_mmc_free; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 667 | } |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 668 | clk_prepare_enable(ssp->clk); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 669 | |
| 670 | mxs_mmc_reset(host); |
| 671 | |
| 672 | dma_cap_zero(mask); |
| 673 | dma_cap_set(DMA_SLAVE, mask); |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 674 | ssp->dma_data.chan_irq = irq_dma; |
| 675 | ssp->dmach = dma_request_channel(mask, mxs_mmc_dma_filter, host); |
| 676 | if (!ssp->dmach) { |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 677 | dev_err(mmc_dev(host->mmc), |
| 678 | "%s: failed to request dma\n", __func__); |
| 679 | goto out_clk_put; |
| 680 | } |
| 681 | |
| 682 | /* set mmc core parameters */ |
| 683 | mmc->ops = &mxs_mmc_ops; |
| 684 | mmc->caps = MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED | |
| 685 | MMC_CAP_SDIO_IRQ | MMC_CAP_NEEDS_POLL; |
| 686 | |
Fabio Estevam | d6ed91a | 2012-10-31 09:22:58 -0200 | [diff] [blame] | 687 | of_property_read_u32(np, "bus-width", &bus_width); |
| 688 | if (bus_width == 4) |
| 689 | mmc->caps |= MMC_CAP_4_BIT_DATA; |
| 690 | else if (bus_width == 8) |
| 691 | mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA; |
Marc Kleine-Budde | 5086e5f | 2013-04-10 11:13:44 +0200 | [diff] [blame^] | 692 | host->non_removable = of_property_read_bool(np, "non-removable"); |
| 693 | if (host->non_removable) |
| 694 | mmc->caps |= MMC_CAP_NONREMOVABLE; |
Fabio Estevam | d6ed91a | 2012-10-31 09:22:58 -0200 | [diff] [blame] | 695 | host->wp_gpio = of_get_named_gpio_flags(np, "wp-gpios", 0, &flags); |
Fabio Estevam | d6ed91a | 2012-10-31 09:22:58 -0200 | [diff] [blame] | 696 | if (flags & OF_GPIO_ACTIVE_LOW) |
| 697 | host->wp_inverted = 1; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 698 | |
Marc Kleine-Budde | 4c0c9be | 2013-04-10 11:13:43 +0200 | [diff] [blame] | 699 | host->cd_inverted = of_property_read_bool(np, "cd-inverted"); |
| 700 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 701 | mmc->f_min = 400000; |
| 702 | mmc->f_max = 288000000; |
| 703 | mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 704 | |
| 705 | mmc->max_segs = 52; |
| 706 | mmc->max_blk_size = 1 << 0xf; |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 707 | mmc->max_blk_count = (ssp_is_old(ssp)) ? 0xff : 0xffffff; |
| 708 | mmc->max_req_size = (ssp_is_old(ssp)) ? 0xffff : 0xffffffff; |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 709 | 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] | 710 | |
| 711 | platform_set_drvdata(pdev, mmc); |
| 712 | |
Shawn Guo | df06bfc | 2012-05-06 11:20:40 +0800 | [diff] [blame] | 713 | ret = devm_request_irq(&pdev->dev, irq_err, mxs_mmc_irq_handler, 0, |
| 714 | DRIVER_NAME, host); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 715 | if (ret) |
| 716 | goto out_free_dma; |
| 717 | |
| 718 | spin_lock_init(&host->lock); |
| 719 | |
| 720 | ret = mmc_add_host(mmc); |
| 721 | if (ret) |
Shawn Guo | df06bfc | 2012-05-06 11:20:40 +0800 | [diff] [blame] | 722 | goto out_free_dma; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 723 | |
| 724 | dev_info(mmc_dev(host->mmc), "initialized\n"); |
| 725 | |
| 726 | return 0; |
| 727 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 728 | out_free_dma: |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 729 | if (ssp->dmach) |
| 730 | dma_release_channel(ssp->dmach); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 731 | out_clk_put: |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 732 | clk_disable_unprepare(ssp->clk); |
| 733 | clk_put(ssp->clk); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 734 | out_mmc_free: |
| 735 | mmc_free_host(mmc); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 736 | return ret; |
| 737 | } |
| 738 | |
| 739 | static int mxs_mmc_remove(struct platform_device *pdev) |
| 740 | { |
| 741 | struct mmc_host *mmc = platform_get_drvdata(pdev); |
| 742 | struct mxs_mmc_host *host = mmc_priv(mmc); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 743 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 744 | |
| 745 | mmc_remove_host(mmc); |
| 746 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 747 | platform_set_drvdata(pdev, NULL); |
| 748 | |
Marek Vasut | 65defb9 | 2012-08-03 17:26:12 +0200 | [diff] [blame] | 749 | if (ssp->dmach) |
| 750 | dma_release_channel(ssp->dmach); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 751 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 752 | clk_disable_unprepare(ssp->clk); |
| 753 | clk_put(ssp->clk); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 754 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 755 | mmc_free_host(mmc); |
| 756 | |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | #ifdef CONFIG_PM |
| 761 | static int mxs_mmc_suspend(struct device *dev) |
| 762 | { |
| 763 | struct mmc_host *mmc = dev_get_drvdata(dev); |
| 764 | struct mxs_mmc_host *host = mmc_priv(mmc); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 765 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 766 | int ret = 0; |
| 767 | |
| 768 | ret = mmc_suspend_host(mmc); |
| 769 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 770 | clk_disable_unprepare(ssp->clk); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 771 | |
| 772 | return ret; |
| 773 | } |
| 774 | |
| 775 | static int mxs_mmc_resume(struct device *dev) |
| 776 | { |
| 777 | struct mmc_host *mmc = dev_get_drvdata(dev); |
| 778 | struct mxs_mmc_host *host = mmc_priv(mmc); |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 779 | struct mxs_ssp *ssp = &host->ssp; |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 780 | int ret = 0; |
| 781 | |
Marek Vasut | 829c1bf | 2012-08-03 17:26:09 +0200 | [diff] [blame] | 782 | clk_prepare_enable(ssp->clk); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 783 | |
| 784 | ret = mmc_resume_host(mmc); |
| 785 | |
| 786 | return ret; |
| 787 | } |
| 788 | |
| 789 | static const struct dev_pm_ops mxs_mmc_pm_ops = { |
| 790 | .suspend = mxs_mmc_suspend, |
| 791 | .resume = mxs_mmc_resume, |
| 792 | }; |
| 793 | #endif |
| 794 | |
| 795 | static struct platform_driver mxs_mmc_driver = { |
| 796 | .probe = mxs_mmc_probe, |
| 797 | .remove = mxs_mmc_remove, |
Marek Vasut | 600a991 | 2012-08-03 17:26:07 +0200 | [diff] [blame] | 798 | .id_table = mxs_ssp_ids, |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 799 | .driver = { |
| 800 | .name = DRIVER_NAME, |
| 801 | .owner = THIS_MODULE, |
| 802 | #ifdef CONFIG_PM |
| 803 | .pm = &mxs_mmc_pm_ops, |
| 804 | #endif |
Marek Vasut | a3e545e | 2012-05-21 06:33:27 +0200 | [diff] [blame] | 805 | .of_match_table = mxs_mmc_dt_ids, |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 806 | }, |
| 807 | }; |
| 808 | |
Axel Lin | d1f81a6 | 2011-11-26 12:55:43 +0800 | [diff] [blame] | 809 | module_platform_driver(mxs_mmc_driver); |
Shawn Guo | e4243f1 | 2011-02-21 18:35:28 +0800 | [diff] [blame] | 810 | |
| 811 | MODULE_DESCRIPTION("FREESCALE MXS MMC peripheral"); |
| 812 | MODULE_AUTHOR("Freescale Semiconductor"); |
| 813 | MODULE_LICENSE("GPL"); |
Fabio Estevam | e7be434 | 2013-01-07 20:41:52 -0200 | [diff] [blame] | 814 | MODULE_ALIAS("platform:" DRIVER_NAME); |