Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/mmc/tmio_mmc.c |
| 3 | * |
| 4 | * Copyright (C) 2004 Ian Molton |
| 5 | * Copyright (C) 2007 Ian Molton |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * Driver for the MMC / SD / SDIO cell found in: |
| 12 | * |
Philipp Zabel | e6f2c7a | 2009-06-04 20:12:37 +0200 | [diff] [blame] | 13 | * TC6393XB TC6391XB TC6387XB T7L66XB ASIC3 |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 14 | * |
| 15 | * This driver draws mainly on scattered spec sheets, Reverse engineering |
| 16 | * of the toshiba e800 SD driver and some parts of the 2.4 ASIC3 driver (4 bit |
| 17 | * support). (Further 4 bit support from a later datasheet). |
| 18 | * |
| 19 | * TODO: |
| 20 | * Investigate using a workqueue for PIO transfers |
| 21 | * Eliminate FIXMEs |
| 22 | * SDIO support |
| 23 | * Better Power management |
| 24 | * Handle MMC errors better |
| 25 | * double buffer support |
| 26 | * |
| 27 | */ |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/irq.h> |
| 30 | #include <linux/device.h> |
| 31 | #include <linux/delay.h> |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 32 | #include <linux/dmaengine.h> |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 33 | #include <linux/mmc/host.h> |
| 34 | #include <linux/mfd/core.h> |
| 35 | #include <linux/mfd/tmio.h> |
| 36 | |
| 37 | #include "tmio_mmc.h" |
| 38 | |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 39 | static void tmio_mmc_set_clock(struct tmio_mmc_host *host, int new_clock) |
| 40 | { |
Ian Molton | da46a0b | 2009-06-12 21:53:05 +0100 | [diff] [blame] | 41 | u32 clk = 0, clock; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 42 | |
| 43 | if (new_clock) { |
Ian Molton | da46a0b | 2009-06-12 21:53:05 +0100 | [diff] [blame] | 44 | for (clock = host->mmc->f_min, clk = 0x80000080; |
| 45 | new_clock >= (clock<<1); clk >>= 1) |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 46 | clock <<= 1; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 47 | clk |= 0x100; |
| 48 | } |
| 49 | |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 50 | if (host->set_clk_div) |
| 51 | host->set_clk_div(host->pdev, (clk>>22) & 1); |
| 52 | |
Ian Molton | da46a0b | 2009-06-12 21:53:05 +0100 | [diff] [blame] | 53 | sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & 0x1ff); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static void tmio_mmc_clk_stop(struct tmio_mmc_host *host) |
| 57 | { |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 58 | sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 59 | msleep(10); |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 60 | sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 & |
| 61 | sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL)); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 62 | msleep(10); |
| 63 | } |
| 64 | |
| 65 | static void tmio_mmc_clk_start(struct tmio_mmc_host *host) |
| 66 | { |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 67 | sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 | |
| 68 | sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL)); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 69 | msleep(10); |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 70 | sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 71 | msleep(10); |
| 72 | } |
| 73 | |
| 74 | static void reset(struct tmio_mmc_host *host) |
| 75 | { |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 76 | /* FIXME - should we set stop clock reg here */ |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 77 | sd_ctrl_write16(host, CTL_RESET_SD, 0x0000); |
| 78 | sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 79 | msleep(10); |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 80 | sd_ctrl_write16(host, CTL_RESET_SD, 0x0001); |
| 81 | sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 82 | msleep(10); |
| 83 | } |
| 84 | |
| 85 | static void |
| 86 | tmio_mmc_finish_request(struct tmio_mmc_host *host) |
| 87 | { |
| 88 | struct mmc_request *mrq = host->mrq; |
| 89 | |
| 90 | host->mrq = NULL; |
| 91 | host->cmd = NULL; |
| 92 | host->data = NULL; |
| 93 | |
| 94 | mmc_request_done(host->mmc, mrq); |
| 95 | } |
| 96 | |
| 97 | /* These are the bitmasks the tmio chip requires to implement the MMC response |
| 98 | * types. Note that R1 and R6 are the same in this scheme. */ |
| 99 | #define APP_CMD 0x0040 |
| 100 | #define RESP_NONE 0x0300 |
| 101 | #define RESP_R1 0x0400 |
| 102 | #define RESP_R1B 0x0500 |
| 103 | #define RESP_R2 0x0600 |
| 104 | #define RESP_R3 0x0700 |
| 105 | #define DATA_PRESENT 0x0800 |
| 106 | #define TRANSFER_READ 0x1000 |
| 107 | #define TRANSFER_MULTI 0x2000 |
| 108 | #define SECURITY_CMD 0x4000 |
| 109 | |
| 110 | static int |
| 111 | tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd) |
| 112 | { |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 113 | struct mmc_data *data = host->data; |
| 114 | int c = cmd->opcode; |
| 115 | |
| 116 | /* Command 12 is handled by hardware */ |
| 117 | if (cmd->opcode == 12 && !cmd->arg) { |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 118 | sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | switch (mmc_resp_type(cmd)) { |
| 123 | case MMC_RSP_NONE: c |= RESP_NONE; break; |
| 124 | case MMC_RSP_R1: c |= RESP_R1; break; |
| 125 | case MMC_RSP_R1B: c |= RESP_R1B; break; |
| 126 | case MMC_RSP_R2: c |= RESP_R2; break; |
| 127 | case MMC_RSP_R3: c |= RESP_R3; break; |
| 128 | default: |
| 129 | pr_debug("Unknown response type %d\n", mmc_resp_type(cmd)); |
| 130 | return -EINVAL; |
| 131 | } |
| 132 | |
| 133 | host->cmd = cmd; |
| 134 | |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 135 | /* FIXME - this seems to be ok commented out but the spec suggest this bit |
| 136 | * should be set when issuing app commands. |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 137 | * if(cmd->flags & MMC_FLAG_ACMD) |
| 138 | * c |= APP_CMD; |
| 139 | */ |
| 140 | if (data) { |
| 141 | c |= DATA_PRESENT; |
| 142 | if (data->blocks > 1) { |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 143 | sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 144 | c |= TRANSFER_MULTI; |
| 145 | } |
| 146 | if (data->flags & MMC_DATA_READ) |
| 147 | c |= TRANSFER_READ; |
| 148 | } |
| 149 | |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 150 | enable_mmc_irqs(host, TMIO_MASK_CMD); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 151 | |
| 152 | /* Fire off the command */ |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 153 | sd_ctrl_write32(host, CTL_ARG_REG, cmd->arg); |
| 154 | sd_ctrl_write16(host, CTL_SD_CMD, c); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 159 | /* |
| 160 | * This chip always returns (at least?) as much data as you ask for. |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 161 | * I'm unsure what happens if you ask for less than a block. This should be |
| 162 | * looked into to ensure that a funny length read doesnt hose the controller. |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 163 | */ |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 164 | static void tmio_mmc_pio_irq(struct tmio_mmc_host *host) |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 165 | { |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 166 | struct mmc_data *data = host->data; |
Guennadi Liakhovetski | 5600efb | 2010-09-09 16:37:43 -0700 | [diff] [blame] | 167 | void *sg_virt; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 168 | unsigned short *buf; |
| 169 | unsigned int count; |
| 170 | unsigned long flags; |
| 171 | |
| 172 | if (!data) { |
| 173 | pr_debug("Spurious PIO IRQ\n"); |
| 174 | return; |
| 175 | } |
| 176 | |
Guennadi Liakhovetski | 5600efb | 2010-09-09 16:37:43 -0700 | [diff] [blame] | 177 | sg_virt = tmio_mmc_kmap_atomic(host->sg_ptr, &flags); |
| 178 | buf = (unsigned short *)(sg_virt + host->sg_off); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 179 | |
| 180 | count = host->sg_ptr->length - host->sg_off; |
| 181 | if (count > data->blksz) |
| 182 | count = data->blksz; |
| 183 | |
| 184 | pr_debug("count: %08x offset: %08x flags %08x\n", |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 185 | count, host->sg_off, data->flags); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 186 | |
| 187 | /* Transfer the data */ |
| 188 | if (data->flags & MMC_DATA_READ) |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 189 | sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 190 | else |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 191 | sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 192 | |
| 193 | host->sg_off += count; |
| 194 | |
Guennadi Liakhovetski | 5600efb | 2010-09-09 16:37:43 -0700 | [diff] [blame] | 195 | tmio_mmc_kunmap_atomic(sg_virt, &flags); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 196 | |
| 197 | if (host->sg_off == host->sg_ptr->length) |
| 198 | tmio_mmc_next_sg(host); |
| 199 | |
| 200 | return; |
| 201 | } |
| 202 | |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 203 | static void tmio_mmc_do_data_irq(struct tmio_mmc_host *host) |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 204 | { |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 205 | struct mmc_data *data = host->data; |
Julia Lawall | a0d045c | 2008-12-16 16:13:09 +0100 | [diff] [blame] | 206 | struct mmc_command *stop; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 207 | |
| 208 | host->data = NULL; |
| 209 | |
| 210 | if (!data) { |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 211 | dev_warn(&host->pdev->dev, "Spurious data end IRQ\n"); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 212 | return; |
| 213 | } |
Julia Lawall | a0d045c | 2008-12-16 16:13:09 +0100 | [diff] [blame] | 214 | stop = data->stop; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 215 | |
| 216 | /* FIXME - return correct transfer count on errors */ |
| 217 | if (!data->error) |
| 218 | data->bytes_xfered = data->blocks * data->blksz; |
| 219 | else |
| 220 | data->bytes_xfered = 0; |
| 221 | |
| 222 | pr_debug("Completed data request\n"); |
| 223 | |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 224 | /* |
| 225 | * FIXME: other drivers allow an optional stop command of any given type |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 226 | * which we dont do, as the chip can auto generate them. |
| 227 | * Perhaps we can be smarter about when to use auto CMD12 and |
| 228 | * only issue the auto request when we know this is the desired |
| 229 | * stop command, allowing fallback to the stop command the |
| 230 | * upper layers expect. For now, we do what works. |
| 231 | */ |
| 232 | |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 233 | if (data->flags & MMC_DATA_READ) { |
| 234 | if (!host->chan_rx) |
| 235 | disable_mmc_irqs(host, TMIO_MASK_READOP); |
| 236 | dev_dbg(&host->pdev->dev, "Complete Rx request %p\n", |
| 237 | host->mrq); |
| 238 | } else { |
| 239 | if (!host->chan_tx) |
| 240 | disable_mmc_irqs(host, TMIO_MASK_WRITEOP); |
| 241 | dev_dbg(&host->pdev->dev, "Complete Tx request %p\n", |
| 242 | host->mrq); |
| 243 | } |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 244 | |
| 245 | if (stop) { |
| 246 | if (stop->opcode == 12 && !stop->arg) |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 247 | sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 248 | else |
| 249 | BUG(); |
| 250 | } |
| 251 | |
| 252 | tmio_mmc_finish_request(host); |
| 253 | } |
| 254 | |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 255 | static void tmio_mmc_data_irq(struct tmio_mmc_host *host) |
| 256 | { |
| 257 | struct mmc_data *data = host->data; |
| 258 | |
| 259 | if (!data) |
| 260 | return; |
| 261 | |
| 262 | if (host->chan_tx && (data->flags & MMC_DATA_WRITE)) { |
| 263 | /* |
| 264 | * Has all data been written out yet? Testing on SuperH showed, |
| 265 | * that in most cases the first interrupt comes already with the |
| 266 | * BUSY status bit clear, but on some operations, like mount or |
| 267 | * in the beginning of a write / sync / umount, there is one |
| 268 | * DATAEND interrupt with the BUSY bit set, in this cases |
| 269 | * waiting for one more interrupt fixes the problem. |
| 270 | */ |
| 271 | if (!(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_CMD_BUSY)) { |
| 272 | disable_mmc_irqs(host, TMIO_STAT_DATAEND); |
| 273 | tasklet_schedule(&host->dma_complete); |
| 274 | } |
| 275 | } else if (host->chan_rx && (data->flags & MMC_DATA_READ)) { |
| 276 | disable_mmc_irqs(host, TMIO_STAT_DATAEND); |
| 277 | tasklet_schedule(&host->dma_complete); |
| 278 | } else { |
| 279 | tmio_mmc_do_data_irq(host); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host, |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 284 | unsigned int stat) |
| 285 | { |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 286 | struct mmc_command *cmd = host->cmd; |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 287 | int i, addr; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 288 | |
| 289 | if (!host->cmd) { |
| 290 | pr_debug("Spurious CMD irq\n"); |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | host->cmd = NULL; |
| 295 | |
| 296 | /* This controller is sicker than the PXA one. Not only do we need to |
| 297 | * drop the top 8 bits of the first response word, we also need to |
| 298 | * modify the order of the response for short response command types. |
| 299 | */ |
| 300 | |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 301 | for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4) |
| 302 | cmd->resp[i] = sd_ctrl_read32(host, addr); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 303 | |
| 304 | if (cmd->flags & MMC_RSP_136) { |
| 305 | cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24); |
| 306 | cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24); |
| 307 | cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24); |
| 308 | cmd->resp[3] <<= 8; |
| 309 | } else if (cmd->flags & MMC_RSP_R3) { |
| 310 | cmd->resp[0] = cmd->resp[3]; |
| 311 | } |
| 312 | |
| 313 | if (stat & TMIO_STAT_CMDTIMEOUT) |
| 314 | cmd->error = -ETIMEDOUT; |
| 315 | else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC) |
| 316 | cmd->error = -EILSEQ; |
| 317 | |
| 318 | /* If there is data to handle we enable data IRQs here, and |
| 319 | * we will ultimatley finish the request in the data_end handler. |
| 320 | * If theres no data or we encountered an error, finish now. |
| 321 | */ |
| 322 | if (host->data && !cmd->error) { |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 323 | if (host->data->flags & MMC_DATA_READ) { |
| 324 | if (!host->chan_rx) |
| 325 | enable_mmc_irqs(host, TMIO_MASK_READOP); |
| 326 | } else { |
| 327 | struct dma_chan *chan = host->chan_tx; |
| 328 | if (!chan) |
| 329 | enable_mmc_irqs(host, TMIO_MASK_WRITEOP); |
| 330 | else |
| 331 | tasklet_schedule(&host->dma_issue); |
| 332 | } |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 333 | } else { |
| 334 | tmio_mmc_finish_request(host); |
| 335 | } |
| 336 | |
| 337 | return; |
| 338 | } |
| 339 | |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 340 | static irqreturn_t tmio_mmc_irq(int irq, void *devid) |
| 341 | { |
| 342 | struct tmio_mmc_host *host = devid; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 343 | unsigned int ireg, irq_mask, status; |
| 344 | |
| 345 | pr_debug("MMC IRQ begin\n"); |
| 346 | |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 347 | status = sd_ctrl_read32(host, CTL_STATUS); |
| 348 | irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 349 | ireg = status & TMIO_MASK_IRQ & ~irq_mask; |
| 350 | |
| 351 | pr_debug_status(status); |
| 352 | pr_debug_status(ireg); |
| 353 | |
| 354 | if (!ireg) { |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 355 | disable_mmc_irqs(host, status & ~irq_mask); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 356 | |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 357 | pr_warning("tmio_mmc: Spurious irq, disabling! " |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 358 | "0x%08x 0x%08x 0x%08x\n", status, irq_mask, ireg); |
| 359 | pr_debug_status(status); |
| 360 | |
| 361 | goto out; |
| 362 | } |
| 363 | |
| 364 | while (ireg) { |
| 365 | /* Card insert / remove attempts */ |
| 366 | if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) { |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 367 | ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT | |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 368 | TMIO_STAT_CARD_REMOVE); |
Magnus Damm | 6d9af5af | 2010-02-17 16:38:04 +0900 | [diff] [blame] | 369 | mmc_detect_change(host->mmc, msecs_to_jiffies(100)); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* CRC and other errors */ |
| 373 | /* if (ireg & TMIO_STAT_ERR_IRQ) |
| 374 | * handled |= tmio_error_irq(host, irq, stat); |
| 375 | */ |
| 376 | |
| 377 | /* Command completion */ |
| 378 | if (ireg & TMIO_MASK_CMD) { |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 379 | ack_mmc_irqs(host, TMIO_MASK_CMD); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 380 | tmio_mmc_cmd_irq(host, status); |
| 381 | } |
| 382 | |
| 383 | /* Data transfer */ |
| 384 | if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) { |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 385 | ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 386 | tmio_mmc_pio_irq(host); |
| 387 | } |
| 388 | |
| 389 | /* Data transfer completion */ |
| 390 | if (ireg & TMIO_STAT_DATAEND) { |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 391 | ack_mmc_irqs(host, TMIO_STAT_DATAEND); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 392 | tmio_mmc_data_irq(host); |
| 393 | } |
| 394 | |
| 395 | /* Check status - keep going until we've handled it all */ |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 396 | status = sd_ctrl_read32(host, CTL_STATUS); |
| 397 | irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 398 | ireg = status & TMIO_MASK_IRQ & ~irq_mask; |
| 399 | |
| 400 | pr_debug("Status at end of loop: %08x\n", status); |
| 401 | pr_debug_status(status); |
| 402 | } |
| 403 | pr_debug("MMC IRQ end\n"); |
| 404 | |
| 405 | out: |
| 406 | return IRQ_HANDLED; |
| 407 | } |
| 408 | |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 409 | #ifdef CONFIG_TMIO_MMC_DMA |
| 410 | static void tmio_mmc_enable_dma(struct tmio_mmc_host *host, bool enable) |
| 411 | { |
| 412 | #if defined(CONFIG_SUPERH) || defined(CONFIG_ARCH_SHMOBILE) |
| 413 | /* Switch DMA mode on or off - SuperH specific? */ |
| 414 | sd_ctrl_write16(host, 0xd8, enable ? 2 : 0); |
| 415 | #endif |
| 416 | } |
| 417 | |
| 418 | static void tmio_dma_complete(void *arg) |
| 419 | { |
| 420 | struct tmio_mmc_host *host = arg; |
| 421 | |
| 422 | dev_dbg(&host->pdev->dev, "Command completed\n"); |
| 423 | |
| 424 | if (!host->data) |
| 425 | dev_warn(&host->pdev->dev, "NULL data in DMA completion!\n"); |
| 426 | else |
| 427 | enable_mmc_irqs(host, TMIO_STAT_DATAEND); |
| 428 | } |
| 429 | |
| 430 | static int tmio_mmc_start_dma_rx(struct tmio_mmc_host *host) |
| 431 | { |
| 432 | struct scatterlist *sg = host->sg_ptr; |
| 433 | struct dma_async_tx_descriptor *desc = NULL; |
| 434 | struct dma_chan *chan = host->chan_rx; |
| 435 | int ret; |
| 436 | |
| 437 | ret = dma_map_sg(&host->pdev->dev, sg, host->sg_len, DMA_FROM_DEVICE); |
| 438 | if (ret > 0) { |
| 439 | host->dma_sglen = ret; |
| 440 | desc = chan->device->device_prep_slave_sg(chan, sg, ret, |
| 441 | DMA_FROM_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 442 | } |
| 443 | |
| 444 | if (desc) { |
| 445 | host->desc = desc; |
| 446 | desc->callback = tmio_dma_complete; |
| 447 | desc->callback_param = host; |
| 448 | host->cookie = desc->tx_submit(desc); |
| 449 | if (host->cookie < 0) { |
| 450 | host->desc = NULL; |
| 451 | ret = host->cookie; |
| 452 | } else { |
| 453 | chan->device->device_issue_pending(chan); |
| 454 | } |
| 455 | } |
| 456 | dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n", |
| 457 | __func__, host->sg_len, ret, host->cookie, host->mrq); |
| 458 | |
| 459 | if (!host->desc) { |
| 460 | /* DMA failed, fall back to PIO */ |
| 461 | if (ret >= 0) |
| 462 | ret = -EIO; |
| 463 | host->chan_rx = NULL; |
| 464 | dma_release_channel(chan); |
| 465 | /* Free the Tx channel too */ |
| 466 | chan = host->chan_tx; |
| 467 | if (chan) { |
| 468 | host->chan_tx = NULL; |
| 469 | dma_release_channel(chan); |
| 470 | } |
| 471 | dev_warn(&host->pdev->dev, |
| 472 | "DMA failed: %d, falling back to PIO\n", ret); |
| 473 | tmio_mmc_enable_dma(host, false); |
| 474 | reset(host); |
| 475 | /* Fail this request, let above layers recover */ |
| 476 | host->mrq->cmd->error = ret; |
| 477 | tmio_mmc_finish_request(host); |
| 478 | } |
| 479 | |
| 480 | dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__, |
| 481 | desc, host->cookie, host->sg_len); |
| 482 | |
| 483 | return ret > 0 ? 0 : ret; |
| 484 | } |
| 485 | |
| 486 | static int tmio_mmc_start_dma_tx(struct tmio_mmc_host *host) |
| 487 | { |
| 488 | struct scatterlist *sg = host->sg_ptr; |
| 489 | struct dma_async_tx_descriptor *desc = NULL; |
| 490 | struct dma_chan *chan = host->chan_tx; |
| 491 | int ret; |
| 492 | |
| 493 | ret = dma_map_sg(&host->pdev->dev, sg, host->sg_len, DMA_TO_DEVICE); |
| 494 | if (ret > 0) { |
| 495 | host->dma_sglen = ret; |
| 496 | desc = chan->device->device_prep_slave_sg(chan, sg, ret, |
| 497 | DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 498 | } |
| 499 | |
| 500 | if (desc) { |
| 501 | host->desc = desc; |
| 502 | desc->callback = tmio_dma_complete; |
| 503 | desc->callback_param = host; |
| 504 | host->cookie = desc->tx_submit(desc); |
| 505 | if (host->cookie < 0) { |
| 506 | host->desc = NULL; |
| 507 | ret = host->cookie; |
| 508 | } |
| 509 | } |
| 510 | dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n", |
| 511 | __func__, host->sg_len, ret, host->cookie, host->mrq); |
| 512 | |
| 513 | if (!host->desc) { |
| 514 | /* DMA failed, fall back to PIO */ |
| 515 | if (ret >= 0) |
| 516 | ret = -EIO; |
| 517 | host->chan_tx = NULL; |
| 518 | dma_release_channel(chan); |
| 519 | /* Free the Rx channel too */ |
| 520 | chan = host->chan_rx; |
| 521 | if (chan) { |
| 522 | host->chan_rx = NULL; |
| 523 | dma_release_channel(chan); |
| 524 | } |
| 525 | dev_warn(&host->pdev->dev, |
| 526 | "DMA failed: %d, falling back to PIO\n", ret); |
| 527 | tmio_mmc_enable_dma(host, false); |
| 528 | reset(host); |
| 529 | /* Fail this request, let above layers recover */ |
| 530 | host->mrq->cmd->error = ret; |
| 531 | tmio_mmc_finish_request(host); |
| 532 | } |
| 533 | |
| 534 | dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d\n", __func__, |
| 535 | desc, host->cookie); |
| 536 | |
| 537 | return ret > 0 ? 0 : ret; |
| 538 | } |
| 539 | |
| 540 | static int tmio_mmc_start_dma(struct tmio_mmc_host *host, |
| 541 | struct mmc_data *data) |
| 542 | { |
| 543 | if (data->flags & MMC_DATA_READ) { |
| 544 | if (host->chan_rx) |
| 545 | return tmio_mmc_start_dma_rx(host); |
| 546 | } else { |
| 547 | if (host->chan_tx) |
| 548 | return tmio_mmc_start_dma_tx(host); |
| 549 | } |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | static void tmio_issue_tasklet_fn(unsigned long priv) |
| 555 | { |
| 556 | struct tmio_mmc_host *host = (struct tmio_mmc_host *)priv; |
| 557 | struct dma_chan *chan = host->chan_tx; |
| 558 | |
| 559 | chan->device->device_issue_pending(chan); |
| 560 | } |
| 561 | |
| 562 | static void tmio_tasklet_fn(unsigned long arg) |
| 563 | { |
| 564 | struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg; |
| 565 | |
| 566 | if (host->data->flags & MMC_DATA_READ) |
| 567 | dma_unmap_sg(&host->pdev->dev, host->sg_ptr, host->dma_sglen, |
| 568 | DMA_FROM_DEVICE); |
| 569 | else |
| 570 | dma_unmap_sg(&host->pdev->dev, host->sg_ptr, host->dma_sglen, |
| 571 | DMA_TO_DEVICE); |
| 572 | |
| 573 | tmio_mmc_do_data_irq(host); |
| 574 | } |
| 575 | |
| 576 | /* It might be necessary to make filter MFD specific */ |
| 577 | static bool tmio_mmc_filter(struct dma_chan *chan, void *arg) |
| 578 | { |
| 579 | dev_dbg(chan->device->dev, "%s: slave data %p\n", __func__, arg); |
| 580 | chan->private = arg; |
| 581 | return true; |
| 582 | } |
| 583 | |
| 584 | static void tmio_mmc_request_dma(struct tmio_mmc_host *host, |
| 585 | struct tmio_mmc_data *pdata) |
| 586 | { |
| 587 | host->cookie = -EINVAL; |
| 588 | host->desc = NULL; |
| 589 | |
| 590 | /* We can only either use DMA for both Tx and Rx or not use it at all */ |
| 591 | if (pdata->dma) { |
| 592 | dma_cap_mask_t mask; |
| 593 | |
| 594 | dma_cap_zero(mask); |
| 595 | dma_cap_set(DMA_SLAVE, mask); |
| 596 | |
| 597 | host->chan_tx = dma_request_channel(mask, tmio_mmc_filter, |
| 598 | pdata->dma->chan_priv_tx); |
| 599 | dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__, |
| 600 | host->chan_tx); |
| 601 | |
| 602 | if (!host->chan_tx) |
| 603 | return; |
| 604 | |
| 605 | host->chan_rx = dma_request_channel(mask, tmio_mmc_filter, |
| 606 | pdata->dma->chan_priv_rx); |
| 607 | dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__, |
| 608 | host->chan_rx); |
| 609 | |
| 610 | if (!host->chan_rx) { |
| 611 | dma_release_channel(host->chan_tx); |
| 612 | host->chan_tx = NULL; |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | tasklet_init(&host->dma_complete, tmio_tasklet_fn, (unsigned long)host); |
| 617 | tasklet_init(&host->dma_issue, tmio_issue_tasklet_fn, (unsigned long)host); |
| 618 | |
| 619 | tmio_mmc_enable_dma(host, true); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | static void tmio_mmc_release_dma(struct tmio_mmc_host *host) |
| 624 | { |
| 625 | if (host->chan_tx) { |
| 626 | struct dma_chan *chan = host->chan_tx; |
| 627 | host->chan_tx = NULL; |
| 628 | dma_release_channel(chan); |
| 629 | } |
| 630 | if (host->chan_rx) { |
| 631 | struct dma_chan *chan = host->chan_rx; |
| 632 | host->chan_rx = NULL; |
| 633 | dma_release_channel(chan); |
| 634 | } |
| 635 | |
| 636 | host->cookie = -EINVAL; |
| 637 | host->desc = NULL; |
| 638 | } |
| 639 | #else |
| 640 | static int tmio_mmc_start_dma(struct tmio_mmc_host *host, |
| 641 | struct mmc_data *data) |
| 642 | { |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | static void tmio_mmc_request_dma(struct tmio_mmc_host *host, |
| 647 | struct tmio_mmc_data *pdata) |
| 648 | { |
| 649 | host->chan_tx = NULL; |
| 650 | host->chan_rx = NULL; |
| 651 | } |
| 652 | |
| 653 | static void tmio_mmc_release_dma(struct tmio_mmc_host *host) |
| 654 | { |
| 655 | } |
| 656 | #endif |
| 657 | |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 658 | static int tmio_mmc_start_data(struct tmio_mmc_host *host, |
| 659 | struct mmc_data *data) |
| 660 | { |
Yusuke Goda | f1334fb | 2010-08-30 11:50:19 +0100 | [diff] [blame] | 661 | struct mfd_cell *cell = host->pdev->dev.platform_data; |
| 662 | struct tmio_mmc_data *pdata = cell->driver_data; |
| 663 | |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 664 | pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n", |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 665 | data->blksz, data->blocks); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 666 | |
Yusuke Goda | f1334fb | 2010-08-30 11:50:19 +0100 | [diff] [blame] | 667 | /* Some hardware cannot perform 2 byte requests in 4 bit mode */ |
| 668 | if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) { |
| 669 | int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES; |
| 670 | |
| 671 | if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) { |
| 672 | pr_err("%s: %d byte block unsupported in 4 bit mode\n", |
| 673 | mmc_hostname(host->mmc), data->blksz); |
| 674 | return -EINVAL; |
| 675 | } |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | tmio_mmc_init_sg(host, data); |
| 679 | host->data = data; |
| 680 | |
| 681 | /* Set transfer length / blocksize */ |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 682 | sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz); |
| 683 | sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 684 | |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 685 | return tmio_mmc_start_dma(host, data); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | /* Process requests from the MMC layer */ |
| 689 | static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 690 | { |
| 691 | struct tmio_mmc_host *host = mmc_priv(mmc); |
| 692 | int ret; |
| 693 | |
| 694 | if (host->mrq) |
| 695 | pr_debug("request not null\n"); |
| 696 | |
| 697 | host->mrq = mrq; |
| 698 | |
| 699 | if (mrq->data) { |
| 700 | ret = tmio_mmc_start_data(host, mrq->data); |
| 701 | if (ret) |
| 702 | goto fail; |
| 703 | } |
| 704 | |
| 705 | ret = tmio_mmc_start_command(host, mrq->cmd); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 706 | if (!ret) |
| 707 | return; |
| 708 | |
| 709 | fail: |
| 710 | mrq->cmd->error = ret; |
| 711 | mmc_request_done(mmc, mrq); |
| 712 | } |
| 713 | |
| 714 | /* Set MMC clock / power. |
| 715 | * Note: This controller uses a simple divider scheme therefore it cannot |
| 716 | * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as |
| 717 | * MMC wont run that fast, it has to be clocked at 12MHz which is the next |
| 718 | * slowest setting. |
| 719 | */ |
| 720 | static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 721 | { |
| 722 | struct tmio_mmc_host *host = mmc_priv(mmc); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 723 | |
| 724 | if (ios->clock) |
| 725 | tmio_mmc_set_clock(host, ios->clock); |
| 726 | |
| 727 | /* Power sequence - OFF -> ON -> UP */ |
| 728 | switch (ios->power_mode) { |
| 729 | case MMC_POWER_OFF: /* power down SD bus */ |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 730 | if (host->set_pwr) |
| 731 | host->set_pwr(host->pdev, 0); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 732 | tmio_mmc_clk_stop(host); |
| 733 | break; |
| 734 | case MMC_POWER_ON: /* power up SD bus */ |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 735 | if (host->set_pwr) |
| 736 | host->set_pwr(host->pdev, 1); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 737 | break; |
| 738 | case MMC_POWER_UP: /* start bus clock */ |
| 739 | tmio_mmc_clk_start(host); |
| 740 | break; |
| 741 | } |
| 742 | |
| 743 | switch (ios->bus_width) { |
| 744 | case MMC_BUS_WIDTH_1: |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 745 | sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 746 | break; |
| 747 | case MMC_BUS_WIDTH_4: |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 748 | sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 749 | break; |
| 750 | } |
| 751 | |
| 752 | /* Let things settle. delay taken from winCE driver */ |
| 753 | udelay(140); |
| 754 | } |
| 755 | |
| 756 | static int tmio_mmc_get_ro(struct mmc_host *mmc) |
| 757 | { |
| 758 | struct tmio_mmc_host *host = mmc_priv(mmc); |
Guennadi Liakhovetski | ac8fb3e | 2010-05-19 18:36:02 +0000 | [diff] [blame] | 759 | struct mfd_cell *cell = host->pdev->dev.platform_data; |
| 760 | struct tmio_mmc_data *pdata = cell->driver_data; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 761 | |
Guennadi Liakhovetski | ac8fb3e | 2010-05-19 18:36:02 +0000 | [diff] [blame] | 762 | return ((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) || |
| 763 | (sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT)) ? 0 : 1; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 764 | } |
| 765 | |
Arnd Hannemann | 19ca750 | 2010-08-24 17:26:59 +0200 | [diff] [blame] | 766 | static int tmio_mmc_get_cd(struct mmc_host *mmc) |
| 767 | { |
| 768 | struct tmio_mmc_host *host = mmc_priv(mmc); |
| 769 | struct mfd_cell *cell = host->pdev->dev.platform_data; |
| 770 | struct tmio_mmc_data *pdata = cell->driver_data; |
| 771 | |
| 772 | if (!pdata->get_cd) |
| 773 | return -ENOSYS; |
| 774 | else |
| 775 | return pdata->get_cd(host->pdev); |
| 776 | } |
| 777 | |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 778 | static const struct mmc_host_ops tmio_mmc_ops = { |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 779 | .request = tmio_mmc_request, |
| 780 | .set_ios = tmio_mmc_set_ios, |
| 781 | .get_ro = tmio_mmc_get_ro, |
Arnd Hannemann | 19ca750 | 2010-08-24 17:26:59 +0200 | [diff] [blame] | 782 | .get_cd = tmio_mmc_get_cd, |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 783 | }; |
| 784 | |
| 785 | #ifdef CONFIG_PM |
| 786 | static int tmio_mmc_suspend(struct platform_device *dev, pm_message_t state) |
| 787 | { |
| 788 | struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data; |
| 789 | struct mmc_host *mmc = platform_get_drvdata(dev); |
| 790 | int ret; |
| 791 | |
Matt Fleming | 1a13f8f | 2010-05-26 14:42:08 -0700 | [diff] [blame] | 792 | ret = mmc_suspend_host(mmc); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 793 | |
| 794 | /* Tell MFD core it can disable us now.*/ |
| 795 | if (!ret && cell->disable) |
| 796 | cell->disable(dev); |
| 797 | |
| 798 | return ret; |
| 799 | } |
| 800 | |
| 801 | static int tmio_mmc_resume(struct platform_device *dev) |
| 802 | { |
| 803 | struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data; |
| 804 | struct mmc_host *mmc = platform_get_drvdata(dev); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 805 | int ret = 0; |
| 806 | |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 807 | /* Tell the MFD core we are ready to be enabled */ |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 808 | if (cell->resume) { |
| 809 | ret = cell->resume(dev); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 810 | if (ret) |
| 811 | goto out; |
| 812 | } |
| 813 | |
| 814 | mmc_resume_host(mmc); |
| 815 | |
| 816 | out: |
| 817 | return ret; |
| 818 | } |
| 819 | #else |
| 820 | #define tmio_mmc_suspend NULL |
| 821 | #define tmio_mmc_resume NULL |
| 822 | #endif |
| 823 | |
| 824 | static int __devinit tmio_mmc_probe(struct platform_device *dev) |
| 825 | { |
| 826 | struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data; |
Philipp Zabel | f0e46cc | 2009-06-04 20:12:31 +0200 | [diff] [blame] | 827 | struct tmio_mmc_data *pdata; |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 828 | struct resource *res_ctl; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 829 | struct tmio_mmc_host *host; |
| 830 | struct mmc_host *mmc; |
Philipp Zabel | d6c9b5e | 2009-06-04 20:12:34 +0200 | [diff] [blame] | 831 | int ret = -EINVAL; |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 832 | u32 irq_mask = TMIO_MASK_CMD; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 833 | |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 834 | if (dev->num_resources != 2) |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 835 | goto out; |
| 836 | |
| 837 | res_ctl = platform_get_resource(dev, IORESOURCE_MEM, 0); |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 838 | if (!res_ctl) |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 839 | goto out; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 840 | |
Philipp Zabel | f0e46cc | 2009-06-04 20:12:31 +0200 | [diff] [blame] | 841 | pdata = cell->driver_data; |
Philipp Zabel | d6c9b5e | 2009-06-04 20:12:34 +0200 | [diff] [blame] | 842 | if (!pdata || !pdata->hclk) |
Philipp Zabel | f0e46cc | 2009-06-04 20:12:31 +0200 | [diff] [blame] | 843 | goto out; |
Philipp Zabel | d6c9b5e | 2009-06-04 20:12:34 +0200 | [diff] [blame] | 844 | |
| 845 | ret = -ENOMEM; |
Philipp Zabel | f0e46cc | 2009-06-04 20:12:31 +0200 | [diff] [blame] | 846 | |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 847 | mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &dev->dev); |
| 848 | if (!mmc) |
| 849 | goto out; |
| 850 | |
| 851 | host = mmc_priv(mmc); |
| 852 | host->mmc = mmc; |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 853 | host->pdev = dev; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 854 | platform_set_drvdata(dev, mmc); |
| 855 | |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 856 | host->set_pwr = pdata->set_pwr; |
| 857 | host->set_clk_div = pdata->set_clk_div; |
| 858 | |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 859 | /* SD control register space size is 0x200, 0x400 for bus_shift=1 */ |
| 860 | host->bus_shift = resource_size(res_ctl) >> 10; |
| 861 | |
Magnus Damm | bc6772a | 2009-03-11 21:58:54 +0900 | [diff] [blame] | 862 | host->ctl = ioremap(res_ctl->start, resource_size(res_ctl)); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 863 | if (!host->ctl) |
| 864 | goto host_free; |
| 865 | |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 866 | mmc->ops = &tmio_mmc_ops; |
| 867 | mmc->caps = MMC_CAP_4_BIT_DATA; |
Yusuke Goda | b741d44 | 2010-02-17 16:37:55 +0900 | [diff] [blame] | 868 | mmc->caps |= pdata->capabilities; |
Philipp Zabel | f0e46cc | 2009-06-04 20:12:31 +0200 | [diff] [blame] | 869 | mmc->f_max = pdata->hclk; |
| 870 | mmc->f_min = mmc->f_max / 512; |
Guennadi Liakhovetski | a2b14dc | 2010-05-19 18:37:25 +0000 | [diff] [blame] | 871 | if (pdata->ocr_mask) |
| 872 | mmc->ocr_avail = pdata->ocr_mask; |
| 873 | else |
| 874 | mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 875 | |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 876 | /* Tell the MFD core we are ready to be enabled */ |
| 877 | if (cell->enable) { |
| 878 | ret = cell->enable(dev); |
| 879 | if (ret) |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 880 | goto unmap_ctl; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 881 | } |
| 882 | |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 883 | tmio_mmc_clk_stop(host); |
| 884 | reset(host); |
| 885 | |
| 886 | ret = platform_get_irq(dev, 0); |
| 887 | if (ret >= 0) |
| 888 | host->irq = ret; |
| 889 | else |
Magnus Damm | 7ee422d | 2010-02-17 16:38:23 +0900 | [diff] [blame] | 890 | goto cell_disable; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 891 | |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 892 | disable_mmc_irqs(host, TMIO_MASK_ALL); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 893 | |
Philipp Zabel | 6c413cc | 2009-06-04 20:12:33 +0200 | [diff] [blame] | 894 | ret = request_irq(host->irq, tmio_mmc_irq, IRQF_DISABLED | |
Magnus Damm | 14f1b75 | 2009-12-14 18:01:33 -0800 | [diff] [blame] | 895 | IRQF_TRIGGER_FALLING, dev_name(&dev->dev), host); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 896 | if (ret) |
Magnus Damm | 7ee422d | 2010-02-17 16:38:23 +0900 | [diff] [blame] | 897 | goto cell_disable; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 898 | |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 899 | /* See if we also get DMA */ |
| 900 | tmio_mmc_request_dma(host, pdata); |
| 901 | |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 902 | mmc_add_host(mmc); |
| 903 | |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 904 | pr_info("%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc), |
| 905 | (unsigned long)host->ctl, host->irq); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 906 | |
| 907 | /* Unmask the IRQs we want to know about */ |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 908 | if (!host->chan_rx) |
| 909 | irq_mask |= TMIO_MASK_READOP; |
| 910 | if (!host->chan_tx) |
| 911 | irq_mask |= TMIO_MASK_WRITEOP; |
| 912 | enable_mmc_irqs(host, irq_mask); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 913 | |
| 914 | return 0; |
| 915 | |
Magnus Damm | 7ee422d | 2010-02-17 16:38:23 +0900 | [diff] [blame] | 916 | cell_disable: |
| 917 | if (cell->disable) |
| 918 | cell->disable(dev); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 919 | unmap_ctl: |
| 920 | iounmap(host->ctl); |
| 921 | host_free: |
| 922 | mmc_free_host(mmc); |
| 923 | out: |
| 924 | return ret; |
| 925 | } |
| 926 | |
| 927 | static int __devexit tmio_mmc_remove(struct platform_device *dev) |
| 928 | { |
Magnus Damm | 7ee422d | 2010-02-17 16:38:23 +0900 | [diff] [blame] | 929 | struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 930 | struct mmc_host *mmc = platform_get_drvdata(dev); |
| 931 | |
| 932 | platform_set_drvdata(dev, NULL); |
| 933 | |
| 934 | if (mmc) { |
| 935 | struct tmio_mmc_host *host = mmc_priv(mmc); |
| 936 | mmc_remove_host(mmc); |
Guennadi Liakhovetski | 311f3ac | 2010-05-19 18:34:22 +0000 | [diff] [blame] | 937 | tmio_mmc_release_dma(host); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 938 | free_irq(host->irq, host); |
Magnus Damm | 7ee422d | 2010-02-17 16:38:23 +0900 | [diff] [blame] | 939 | if (cell->disable) |
| 940 | cell->disable(dev); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 941 | iounmap(host->ctl); |
Magnus Damm | bedcc45 | 2009-03-11 21:59:03 +0900 | [diff] [blame] | 942 | mmc_free_host(mmc); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | return 0; |
| 946 | } |
| 947 | |
| 948 | /* ------------------- device registration ----------------------- */ |
| 949 | |
| 950 | static struct platform_driver tmio_mmc_driver = { |
| 951 | .driver = { |
| 952 | .name = "tmio-mmc", |
| 953 | .owner = THIS_MODULE, |
| 954 | }, |
| 955 | .probe = tmio_mmc_probe, |
| 956 | .remove = __devexit_p(tmio_mmc_remove), |
| 957 | .suspend = tmio_mmc_suspend, |
| 958 | .resume = tmio_mmc_resume, |
| 959 | }; |
| 960 | |
| 961 | |
| 962 | static int __init tmio_mmc_init(void) |
| 963 | { |
| 964 | return platform_driver_register(&tmio_mmc_driver); |
| 965 | } |
| 966 | |
| 967 | static void __exit tmio_mmc_exit(void) |
| 968 | { |
| 969 | platform_driver_unregister(&tmio_mmc_driver); |
| 970 | } |
| 971 | |
| 972 | module_init(tmio_mmc_init); |
| 973 | module_exit(tmio_mmc_exit); |
| 974 | |
| 975 | MODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver"); |
| 976 | MODULE_AUTHOR("Ian Molton <spyro@f2s.com>"); |
| 977 | MODULE_LICENSE("GPL v2"); |
| 978 | MODULE_ALIAS("platform:tmio-mmc"); |