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 | * |
| 13 | * TC6393XB TC6391XB TC6387XB T7L66XB |
| 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> |
| 32 | #include <linux/mmc/host.h> |
| 33 | #include <linux/mfd/core.h> |
| 34 | #include <linux/mfd/tmio.h> |
| 35 | |
| 36 | #include "tmio_mmc.h" |
| 37 | |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 38 | static void tmio_mmc_set_clock(struct tmio_mmc_host *host, int new_clock) |
| 39 | { |
Philipp Zabel | f0e46cc | 2009-06-04 20:12:31 +0200 | [diff] [blame] | 40 | u32 clk = 0, clock, f_min = host->mmc->f_min; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 41 | |
| 42 | if (new_clock) { |
Philipp Zabel | f0e46cc | 2009-06-04 20:12:31 +0200 | [diff] [blame] | 43 | for (clock = f_min, clk = 0x100; new_clock >= (clock<<1); ) { |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 44 | clock <<= 1; |
| 45 | clk >>= 1; |
| 46 | } |
| 47 | if (clk & 0x1) |
| 48 | clk = 0x20000; |
| 49 | |
| 50 | clk >>= 2; |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 51 | sd_config_write8(host, CNF_SD_CLK_MODE, (clk & 0x8000) ? 0 : 1); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 52 | clk |= 0x100; |
| 53 | } |
| 54 | |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 55 | sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static void tmio_mmc_clk_stop(struct tmio_mmc_host *host) |
| 59 | { |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 60 | sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 61 | msleep(10); |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 62 | sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 & |
| 63 | sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL)); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 64 | msleep(10); |
| 65 | } |
| 66 | |
| 67 | static void tmio_mmc_clk_start(struct tmio_mmc_host *host) |
| 68 | { |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 69 | sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 | |
| 70 | sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL)); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 71 | msleep(10); |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 72 | sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 73 | msleep(10); |
| 74 | } |
| 75 | |
| 76 | static void reset(struct tmio_mmc_host *host) |
| 77 | { |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 78 | /* FIXME - should we set stop clock reg here */ |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 79 | sd_ctrl_write16(host, CTL_RESET_SD, 0x0000); |
| 80 | sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 81 | msleep(10); |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 82 | sd_ctrl_write16(host, CTL_RESET_SD, 0x0001); |
| 83 | sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 84 | msleep(10); |
| 85 | } |
| 86 | |
| 87 | static void |
| 88 | tmio_mmc_finish_request(struct tmio_mmc_host *host) |
| 89 | { |
| 90 | struct mmc_request *mrq = host->mrq; |
| 91 | |
| 92 | host->mrq = NULL; |
| 93 | host->cmd = NULL; |
| 94 | host->data = NULL; |
| 95 | |
| 96 | mmc_request_done(host->mmc, mrq); |
| 97 | } |
| 98 | |
| 99 | /* These are the bitmasks the tmio chip requires to implement the MMC response |
| 100 | * types. Note that R1 and R6 are the same in this scheme. */ |
| 101 | #define APP_CMD 0x0040 |
| 102 | #define RESP_NONE 0x0300 |
| 103 | #define RESP_R1 0x0400 |
| 104 | #define RESP_R1B 0x0500 |
| 105 | #define RESP_R2 0x0600 |
| 106 | #define RESP_R3 0x0700 |
| 107 | #define DATA_PRESENT 0x0800 |
| 108 | #define TRANSFER_READ 0x1000 |
| 109 | #define TRANSFER_MULTI 0x2000 |
| 110 | #define SECURITY_CMD 0x4000 |
| 111 | |
| 112 | static int |
| 113 | tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd) |
| 114 | { |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 115 | struct mmc_data *data = host->data; |
| 116 | int c = cmd->opcode; |
| 117 | |
| 118 | /* Command 12 is handled by hardware */ |
| 119 | if (cmd->opcode == 12 && !cmd->arg) { |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 120 | sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | switch (mmc_resp_type(cmd)) { |
| 125 | case MMC_RSP_NONE: c |= RESP_NONE; break; |
| 126 | case MMC_RSP_R1: c |= RESP_R1; break; |
| 127 | case MMC_RSP_R1B: c |= RESP_R1B; break; |
| 128 | case MMC_RSP_R2: c |= RESP_R2; break; |
| 129 | case MMC_RSP_R3: c |= RESP_R3; break; |
| 130 | default: |
| 131 | pr_debug("Unknown response type %d\n", mmc_resp_type(cmd)); |
| 132 | return -EINVAL; |
| 133 | } |
| 134 | |
| 135 | host->cmd = cmd; |
| 136 | |
| 137 | /* FIXME - this seems to be ok comented out but the spec suggest this bit should |
| 138 | * be set when issuing app commands. |
| 139 | * if(cmd->flags & MMC_FLAG_ACMD) |
| 140 | * c |= APP_CMD; |
| 141 | */ |
| 142 | if (data) { |
| 143 | c |= DATA_PRESENT; |
| 144 | if (data->blocks > 1) { |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 145 | sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 146 | c |= TRANSFER_MULTI; |
| 147 | } |
| 148 | if (data->flags & MMC_DATA_READ) |
| 149 | c |= TRANSFER_READ; |
| 150 | } |
| 151 | |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 152 | enable_mmc_irqs(host, TMIO_MASK_CMD); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 153 | |
| 154 | /* Fire off the command */ |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 155 | sd_ctrl_write32(host, CTL_ARG_REG, cmd->arg); |
| 156 | sd_ctrl_write16(host, CTL_SD_CMD, c); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /* This chip always returns (at least?) as much data as you ask for. |
| 162 | * I'm unsure what happens if you ask for less than a block. This should be |
| 163 | * looked into to ensure that a funny length read doesnt hose the controller. |
| 164 | * |
| 165 | */ |
| 166 | static inline void tmio_mmc_pio_irq(struct tmio_mmc_host *host) |
| 167 | { |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 168 | struct mmc_data *data = host->data; |
| 169 | unsigned short *buf; |
| 170 | unsigned int count; |
| 171 | unsigned long flags; |
| 172 | |
| 173 | if (!data) { |
| 174 | pr_debug("Spurious PIO IRQ\n"); |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | buf = (unsigned short *)(tmio_mmc_kmap_atomic(host, &flags) + |
| 179 | host->sg_off); |
| 180 | |
| 181 | count = host->sg_ptr->length - host->sg_off; |
| 182 | if (count > data->blksz) |
| 183 | count = data->blksz; |
| 184 | |
| 185 | pr_debug("count: %08x offset: %08x flags %08x\n", |
| 186 | count, host->sg_off, data->flags); |
| 187 | |
| 188 | /* Transfer the data */ |
| 189 | if (data->flags & MMC_DATA_READ) |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 190 | sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 191 | else |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 192 | sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 193 | |
| 194 | host->sg_off += count; |
| 195 | |
| 196 | tmio_mmc_kunmap_atomic(host, &flags); |
| 197 | |
| 198 | if (host->sg_off == host->sg_ptr->length) |
| 199 | tmio_mmc_next_sg(host); |
| 200 | |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | static inline void tmio_mmc_data_irq(struct tmio_mmc_host *host) |
| 205 | { |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 206 | struct mmc_data *data = host->data; |
Julia Lawall | a0d045ca | 2008-12-16 16:13:09 +0100 | [diff] [blame] | 207 | struct mmc_command *stop; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 208 | |
| 209 | host->data = NULL; |
| 210 | |
| 211 | if (!data) { |
| 212 | pr_debug("Spurious data end IRQ\n"); |
| 213 | return; |
| 214 | } |
Julia Lawall | a0d045ca | 2008-12-16 16:13:09 +0100 | [diff] [blame] | 215 | stop = data->stop; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 216 | |
| 217 | /* FIXME - return correct transfer count on errors */ |
| 218 | if (!data->error) |
| 219 | data->bytes_xfered = data->blocks * data->blksz; |
| 220 | else |
| 221 | data->bytes_xfered = 0; |
| 222 | |
| 223 | pr_debug("Completed data request\n"); |
| 224 | |
| 225 | /*FIXME - other drivers allow an optional stop command of any given type |
| 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 | |
| 233 | if (data->flags & MMC_DATA_READ) |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 234 | disable_mmc_irqs(host, TMIO_MASK_READOP); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 235 | else |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 236 | disable_mmc_irqs(host, TMIO_MASK_WRITEOP); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 237 | |
| 238 | if (stop) { |
| 239 | if (stop->opcode == 12 && !stop->arg) |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 240 | sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 241 | else |
| 242 | BUG(); |
| 243 | } |
| 244 | |
| 245 | tmio_mmc_finish_request(host); |
| 246 | } |
| 247 | |
| 248 | static inline void tmio_mmc_cmd_irq(struct tmio_mmc_host *host, |
| 249 | unsigned int stat) |
| 250 | { |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 251 | struct mmc_command *cmd = host->cmd; |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 252 | int i, addr; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 253 | |
| 254 | if (!host->cmd) { |
| 255 | pr_debug("Spurious CMD irq\n"); |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | host->cmd = NULL; |
| 260 | |
| 261 | /* This controller is sicker than the PXA one. Not only do we need to |
| 262 | * drop the top 8 bits of the first response word, we also need to |
| 263 | * modify the order of the response for short response command types. |
| 264 | */ |
| 265 | |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 266 | for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4) |
| 267 | cmd->resp[i] = sd_ctrl_read32(host, addr); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 268 | |
| 269 | if (cmd->flags & MMC_RSP_136) { |
| 270 | cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24); |
| 271 | cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24); |
| 272 | cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24); |
| 273 | cmd->resp[3] <<= 8; |
| 274 | } else if (cmd->flags & MMC_RSP_R3) { |
| 275 | cmd->resp[0] = cmd->resp[3]; |
| 276 | } |
| 277 | |
| 278 | if (stat & TMIO_STAT_CMDTIMEOUT) |
| 279 | cmd->error = -ETIMEDOUT; |
| 280 | else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC) |
| 281 | cmd->error = -EILSEQ; |
| 282 | |
| 283 | /* If there is data to handle we enable data IRQs here, and |
| 284 | * we will ultimatley finish the request in the data_end handler. |
| 285 | * If theres no data or we encountered an error, finish now. |
| 286 | */ |
| 287 | if (host->data && !cmd->error) { |
| 288 | if (host->data->flags & MMC_DATA_READ) |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 289 | enable_mmc_irqs(host, TMIO_MASK_READOP); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 290 | else |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 291 | enable_mmc_irqs(host, TMIO_MASK_WRITEOP); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 292 | } else { |
| 293 | tmio_mmc_finish_request(host); |
| 294 | } |
| 295 | |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | static irqreturn_t tmio_mmc_irq(int irq, void *devid) |
| 301 | { |
| 302 | struct tmio_mmc_host *host = devid; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 303 | unsigned int ireg, irq_mask, status; |
| 304 | |
| 305 | pr_debug("MMC IRQ begin\n"); |
| 306 | |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 307 | status = sd_ctrl_read32(host, CTL_STATUS); |
| 308 | irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 309 | ireg = status & TMIO_MASK_IRQ & ~irq_mask; |
| 310 | |
| 311 | pr_debug_status(status); |
| 312 | pr_debug_status(ireg); |
| 313 | |
| 314 | if (!ireg) { |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 315 | disable_mmc_irqs(host, status & ~irq_mask); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 316 | |
| 317 | pr_debug("tmio_mmc: Spurious irq, disabling! " |
| 318 | "0x%08x 0x%08x 0x%08x\n", status, irq_mask, ireg); |
| 319 | pr_debug_status(status); |
| 320 | |
| 321 | goto out; |
| 322 | } |
| 323 | |
| 324 | while (ireg) { |
| 325 | /* Card insert / remove attempts */ |
| 326 | if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) { |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 327 | ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT | |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 328 | TMIO_STAT_CARD_REMOVE); |
| 329 | mmc_detect_change(host->mmc, 0); |
| 330 | } |
| 331 | |
| 332 | /* CRC and other errors */ |
| 333 | /* if (ireg & TMIO_STAT_ERR_IRQ) |
| 334 | * handled |= tmio_error_irq(host, irq, stat); |
| 335 | */ |
| 336 | |
| 337 | /* Command completion */ |
| 338 | if (ireg & TMIO_MASK_CMD) { |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 339 | ack_mmc_irqs(host, TMIO_MASK_CMD); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 340 | tmio_mmc_cmd_irq(host, status); |
| 341 | } |
| 342 | |
| 343 | /* Data transfer */ |
| 344 | if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) { |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 345 | ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 346 | tmio_mmc_pio_irq(host); |
| 347 | } |
| 348 | |
| 349 | /* Data transfer completion */ |
| 350 | if (ireg & TMIO_STAT_DATAEND) { |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 351 | ack_mmc_irqs(host, TMIO_STAT_DATAEND); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 352 | tmio_mmc_data_irq(host); |
| 353 | } |
| 354 | |
| 355 | /* Check status - keep going until we've handled it all */ |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 356 | status = sd_ctrl_read32(host, CTL_STATUS); |
| 357 | irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 358 | ireg = status & TMIO_MASK_IRQ & ~irq_mask; |
| 359 | |
| 360 | pr_debug("Status at end of loop: %08x\n", status); |
| 361 | pr_debug_status(status); |
| 362 | } |
| 363 | pr_debug("MMC IRQ end\n"); |
| 364 | |
| 365 | out: |
| 366 | return IRQ_HANDLED; |
| 367 | } |
| 368 | |
| 369 | static int tmio_mmc_start_data(struct tmio_mmc_host *host, |
| 370 | struct mmc_data *data) |
| 371 | { |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 372 | pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n", |
| 373 | data->blksz, data->blocks); |
| 374 | |
| 375 | /* Hardware cannot perform 1 and 2 byte requests in 4 bit mode */ |
| 376 | if (data->blksz < 4 && host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) { |
| 377 | printk(KERN_ERR "%s: %d byte block unsupported in 4 bit mode\n", |
| 378 | mmc_hostname(host->mmc), data->blksz); |
| 379 | return -EINVAL; |
| 380 | } |
| 381 | |
| 382 | tmio_mmc_init_sg(host, data); |
| 383 | host->data = data; |
| 384 | |
| 385 | /* Set transfer length / blocksize */ |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 386 | sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz); |
| 387 | sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | /* Process requests from the MMC layer */ |
| 393 | static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 394 | { |
| 395 | struct tmio_mmc_host *host = mmc_priv(mmc); |
| 396 | int ret; |
| 397 | |
| 398 | if (host->mrq) |
| 399 | pr_debug("request not null\n"); |
| 400 | |
| 401 | host->mrq = mrq; |
| 402 | |
| 403 | if (mrq->data) { |
| 404 | ret = tmio_mmc_start_data(host, mrq->data); |
| 405 | if (ret) |
| 406 | goto fail; |
| 407 | } |
| 408 | |
| 409 | ret = tmio_mmc_start_command(host, mrq->cmd); |
| 410 | |
| 411 | if (!ret) |
| 412 | return; |
| 413 | |
| 414 | fail: |
| 415 | mrq->cmd->error = ret; |
| 416 | mmc_request_done(mmc, mrq); |
| 417 | } |
| 418 | |
| 419 | /* Set MMC clock / power. |
| 420 | * Note: This controller uses a simple divider scheme therefore it cannot |
| 421 | * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as |
| 422 | * MMC wont run that fast, it has to be clocked at 12MHz which is the next |
| 423 | * slowest setting. |
| 424 | */ |
| 425 | static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 426 | { |
| 427 | struct tmio_mmc_host *host = mmc_priv(mmc); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 428 | |
| 429 | if (ios->clock) |
| 430 | tmio_mmc_set_clock(host, ios->clock); |
| 431 | |
| 432 | /* Power sequence - OFF -> ON -> UP */ |
| 433 | switch (ios->power_mode) { |
| 434 | case MMC_POWER_OFF: /* power down SD bus */ |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 435 | sd_config_write8(host, CNF_PWR_CTL_2, 0x00); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 436 | tmio_mmc_clk_stop(host); |
| 437 | break; |
| 438 | case MMC_POWER_ON: /* power up SD bus */ |
| 439 | |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 440 | sd_config_write8(host, CNF_PWR_CTL_2, 0x02); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 441 | break; |
| 442 | case MMC_POWER_UP: /* start bus clock */ |
| 443 | tmio_mmc_clk_start(host); |
| 444 | break; |
| 445 | } |
| 446 | |
| 447 | switch (ios->bus_width) { |
| 448 | case MMC_BUS_WIDTH_1: |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 449 | sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 450 | break; |
| 451 | case MMC_BUS_WIDTH_4: |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 452 | sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 453 | break; |
| 454 | } |
| 455 | |
| 456 | /* Let things settle. delay taken from winCE driver */ |
| 457 | udelay(140); |
| 458 | } |
| 459 | |
| 460 | static int tmio_mmc_get_ro(struct mmc_host *mmc) |
| 461 | { |
| 462 | struct tmio_mmc_host *host = mmc_priv(mmc); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 463 | |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 464 | return (sd_ctrl_read16(host, CTL_STATUS) & TMIO_STAT_WRPROTECT) ? 0 : 1; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | static struct mmc_host_ops tmio_mmc_ops = { |
| 468 | .request = tmio_mmc_request, |
| 469 | .set_ios = tmio_mmc_set_ios, |
| 470 | .get_ro = tmio_mmc_get_ro, |
| 471 | }; |
| 472 | |
| 473 | #ifdef CONFIG_PM |
| 474 | static int tmio_mmc_suspend(struct platform_device *dev, pm_message_t state) |
| 475 | { |
| 476 | struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data; |
| 477 | struct mmc_host *mmc = platform_get_drvdata(dev); |
| 478 | int ret; |
| 479 | |
| 480 | ret = mmc_suspend_host(mmc, state); |
| 481 | |
| 482 | /* Tell MFD core it can disable us now.*/ |
| 483 | if (!ret && cell->disable) |
| 484 | cell->disable(dev); |
| 485 | |
| 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | static int tmio_mmc_resume(struct platform_device *dev) |
| 490 | { |
| 491 | struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data; |
| 492 | struct mmc_host *mmc = platform_get_drvdata(dev); |
| 493 | struct tmio_mmc_host *host = mmc_priv(mmc); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 494 | int ret = 0; |
| 495 | |
| 496 | /* Enable the MMC/SD Control registers */ |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 497 | sd_config_write16(host, CNF_CMD, SDCREN); |
| 498 | sd_config_write32(host, CNF_CTL_BASE, |
| 499 | (dev->resource[0].start >> host->bus_shift) & 0xfffe); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 500 | |
| 501 | /* Tell the MFD core we are ready to be enabled */ |
| 502 | if (cell->enable) { |
| 503 | ret = cell->enable(dev); |
| 504 | if (ret) |
| 505 | goto out; |
| 506 | } |
| 507 | |
| 508 | mmc_resume_host(mmc); |
| 509 | |
| 510 | out: |
| 511 | return ret; |
| 512 | } |
| 513 | #else |
| 514 | #define tmio_mmc_suspend NULL |
| 515 | #define tmio_mmc_resume NULL |
| 516 | #endif |
| 517 | |
| 518 | static int __devinit tmio_mmc_probe(struct platform_device *dev) |
| 519 | { |
| 520 | struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data; |
Philipp Zabel | f0e46cc | 2009-06-04 20:12:31 +0200 | [diff] [blame] | 521 | struct tmio_mmc_data *pdata; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 522 | struct resource *res_ctl, *res_cnf; |
| 523 | struct tmio_mmc_host *host; |
| 524 | struct mmc_host *mmc; |
| 525 | int ret = -ENOMEM; |
| 526 | |
| 527 | if (dev->num_resources != 3) |
| 528 | goto out; |
| 529 | |
| 530 | res_ctl = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 531 | res_cnf = platform_get_resource(dev, IORESOURCE_MEM, 1); |
| 532 | if (!res_ctl || !res_cnf) { |
| 533 | ret = -EINVAL; |
| 534 | goto out; |
| 535 | } |
| 536 | |
Philipp Zabel | f0e46cc | 2009-06-04 20:12:31 +0200 | [diff] [blame] | 537 | pdata = cell->driver_data; |
| 538 | if (!pdata || !pdata->hclk) { |
| 539 | ret = -EINVAL; |
| 540 | goto out; |
| 541 | } |
| 542 | |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 543 | mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &dev->dev); |
| 544 | if (!mmc) |
| 545 | goto out; |
| 546 | |
| 547 | host = mmc_priv(mmc); |
| 548 | host->mmc = mmc; |
| 549 | platform_set_drvdata(dev, mmc); |
| 550 | |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 551 | /* SD control register space size is 0x200, 0x400 for bus_shift=1 */ |
| 552 | host->bus_shift = resource_size(res_ctl) >> 10; |
| 553 | |
Magnus Damm | bc6772a | 2009-03-11 21:58:54 +0900 | [diff] [blame] | 554 | host->ctl = ioremap(res_ctl->start, resource_size(res_ctl)); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 555 | if (!host->ctl) |
| 556 | goto host_free; |
| 557 | |
Magnus Damm | bc6772a | 2009-03-11 21:58:54 +0900 | [diff] [blame] | 558 | host->cnf = ioremap(res_cnf->start, resource_size(res_cnf)); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 559 | if (!host->cnf) |
| 560 | goto unmap_ctl; |
| 561 | |
| 562 | mmc->ops = &tmio_mmc_ops; |
| 563 | mmc->caps = MMC_CAP_4_BIT_DATA; |
Philipp Zabel | f0e46cc | 2009-06-04 20:12:31 +0200 | [diff] [blame] | 564 | mmc->f_max = pdata->hclk; |
| 565 | mmc->f_min = mmc->f_max / 512; |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 566 | mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 567 | |
| 568 | /* Enable the MMC/SD Control registers */ |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 569 | sd_config_write16(host, CNF_CMD, SDCREN); |
| 570 | sd_config_write32(host, CNF_CTL_BASE, |
| 571 | (dev->resource[0].start >> host->bus_shift) & 0xfffe); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 572 | |
| 573 | /* Tell the MFD core we are ready to be enabled */ |
| 574 | if (cell->enable) { |
| 575 | ret = cell->enable(dev); |
| 576 | if (ret) |
| 577 | goto unmap_cnf; |
| 578 | } |
| 579 | |
| 580 | /* Disable SD power during suspend */ |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 581 | sd_config_write8(host, CNF_PWR_CTL_3, 0x01); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 582 | |
| 583 | /* The below is required but why? FIXME */ |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 584 | sd_config_write8(host, CNF_STOP_CLK_CTL, 0x1f); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 585 | |
| 586 | /* Power down SD bus*/ |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 587 | sd_config_write8(host, CNF_PWR_CTL_2, 0x00); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 588 | |
| 589 | tmio_mmc_clk_stop(host); |
| 590 | reset(host); |
| 591 | |
| 592 | ret = platform_get_irq(dev, 0); |
| 593 | if (ret >= 0) |
| 594 | host->irq = ret; |
| 595 | else |
| 596 | goto unmap_cnf; |
| 597 | |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 598 | disable_mmc_irqs(host, TMIO_MASK_ALL); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 599 | |
Philipp Zabel | 6c413cc | 2009-06-04 20:12:33 +0200 | [diff] [blame^] | 600 | ret = request_irq(host->irq, tmio_mmc_irq, IRQF_DISABLED | |
| 601 | IRQF_TRIGGER_FALLING, "tmio-mmc", host); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 602 | if (ret) |
| 603 | goto unmap_cnf; |
| 604 | |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 605 | mmc_add_host(mmc); |
| 606 | |
| 607 | printk(KERN_INFO "%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc), |
| 608 | (unsigned long)host->ctl, host->irq); |
| 609 | |
| 610 | /* Unmask the IRQs we want to know about */ |
Philipp Zabel | 5e74672 | 2009-06-04 20:12:32 +0200 | [diff] [blame] | 611 | enable_mmc_irqs(host, TMIO_MASK_IRQ); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 612 | |
| 613 | return 0; |
| 614 | |
| 615 | unmap_cnf: |
| 616 | iounmap(host->cnf); |
| 617 | unmap_ctl: |
| 618 | iounmap(host->ctl); |
| 619 | host_free: |
| 620 | mmc_free_host(mmc); |
| 621 | out: |
| 622 | return ret; |
| 623 | } |
| 624 | |
| 625 | static int __devexit tmio_mmc_remove(struct platform_device *dev) |
| 626 | { |
| 627 | struct mmc_host *mmc = platform_get_drvdata(dev); |
| 628 | |
| 629 | platform_set_drvdata(dev, NULL); |
| 630 | |
| 631 | if (mmc) { |
| 632 | struct tmio_mmc_host *host = mmc_priv(mmc); |
| 633 | mmc_remove_host(mmc); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 634 | free_irq(host->irq, host); |
| 635 | iounmap(host->ctl); |
| 636 | iounmap(host->cnf); |
Magnus Damm | bedcc45 | 2009-03-11 21:59:03 +0900 | [diff] [blame] | 637 | mmc_free_host(mmc); |
Ian Molton | 4a48998 | 2008-07-15 16:02:21 +0100 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | /* ------------------- device registration ----------------------- */ |
| 644 | |
| 645 | static struct platform_driver tmio_mmc_driver = { |
| 646 | .driver = { |
| 647 | .name = "tmio-mmc", |
| 648 | .owner = THIS_MODULE, |
| 649 | }, |
| 650 | .probe = tmio_mmc_probe, |
| 651 | .remove = __devexit_p(tmio_mmc_remove), |
| 652 | .suspend = tmio_mmc_suspend, |
| 653 | .resume = tmio_mmc_resume, |
| 654 | }; |
| 655 | |
| 656 | |
| 657 | static int __init tmio_mmc_init(void) |
| 658 | { |
| 659 | return platform_driver_register(&tmio_mmc_driver); |
| 660 | } |
| 661 | |
| 662 | static void __exit tmio_mmc_exit(void) |
| 663 | { |
| 664 | platform_driver_unregister(&tmio_mmc_driver); |
| 665 | } |
| 666 | |
| 667 | module_init(tmio_mmc_init); |
| 668 | module_exit(tmio_mmc_exit); |
| 669 | |
| 670 | MODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver"); |
| 671 | MODULE_AUTHOR("Ian Molton <spyro@f2s.com>"); |
| 672 | MODULE_LICENSE("GPL v2"); |
| 673 | MODULE_ALIAS("platform:tmio-mmc"); |