Guennadi Liakhovetski | b614749 | 2011-03-23 12:42:44 +0100 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/mmc/host/tmio_mmc_pio.c |
| 3 | * |
| 4 | * Copyright (C) 2011 Guennadi Liakhovetski |
| 5 | * Copyright (C) 2007 Ian Molton |
| 6 | * Copyright (C) 2004 Ian Molton |
| 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 version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | * Driver for the MMC / SD / SDIO IP found in: |
| 13 | * |
| 14 | * TC6393XB, TC6391XB, TC6387XB, T7L66XB, ASIC3, SH-Mobile SoCs |
| 15 | * |
| 16 | * This driver draws mainly on scattered spec sheets, Reverse engineering |
| 17 | * of the toshiba e800 SD driver and some parts of the 2.4 ASIC3 driver (4 bit |
| 18 | * support). (Further 4 bit support from a later datasheet). |
| 19 | * |
| 20 | * TODO: |
| 21 | * Investigate using a workqueue for PIO transfers |
| 22 | * Eliminate FIXMEs |
| 23 | * SDIO support |
| 24 | * Better Power management |
| 25 | * Handle MMC errors better |
| 26 | * double buffer support |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include <linux/delay.h> |
| 31 | #include <linux/device.h> |
| 32 | #include <linux/highmem.h> |
| 33 | #include <linux/interrupt.h> |
| 34 | #include <linux/io.h> |
| 35 | #include <linux/irq.h> |
| 36 | #include <linux/mfd/tmio.h> |
| 37 | #include <linux/mmc/host.h> |
| 38 | #include <linux/module.h> |
| 39 | #include <linux/pagemap.h> |
| 40 | #include <linux/platform_device.h> |
| 41 | #include <linux/scatterlist.h> |
| 42 | #include <linux/workqueue.h> |
| 43 | #include <linux/spinlock.h> |
| 44 | |
| 45 | #include "tmio_mmc.h" |
| 46 | |
| 47 | #define CTL_SD_CMD 0x00 |
| 48 | #define CTL_ARG_REG 0x04 |
| 49 | #define CTL_STOP_INTERNAL_ACTION 0x08 |
| 50 | #define CTL_XFER_BLK_COUNT 0xa |
| 51 | #define CTL_RESPONSE 0x0c |
| 52 | #define CTL_STATUS 0x1c |
| 53 | #define CTL_IRQ_MASK 0x20 |
| 54 | #define CTL_SD_CARD_CLK_CTL 0x24 |
| 55 | #define CTL_SD_XFER_LEN 0x26 |
| 56 | #define CTL_SD_MEM_CARD_OPT 0x28 |
| 57 | #define CTL_SD_ERROR_DETAIL_STATUS 0x2c |
| 58 | #define CTL_SD_DATA_PORT 0x30 |
| 59 | #define CTL_TRANSACTION_CTL 0x34 |
| 60 | #define CTL_SDIO_STATUS 0x36 |
| 61 | #define CTL_SDIO_IRQ_MASK 0x38 |
| 62 | #define CTL_RESET_SD 0xe0 |
| 63 | #define CTL_SDIO_REGS 0x100 |
| 64 | #define CTL_CLK_AND_WAIT_CTL 0x138 |
| 65 | #define CTL_RESET_SDIO 0x1e0 |
| 66 | |
| 67 | static u16 sd_ctrl_read16(struct tmio_mmc_host *host, int addr) |
| 68 | { |
| 69 | return readw(host->ctl + (addr << host->bus_shift)); |
| 70 | } |
| 71 | |
| 72 | static void sd_ctrl_read16_rep(struct tmio_mmc_host *host, int addr, |
| 73 | u16 *buf, int count) |
| 74 | { |
| 75 | readsw(host->ctl + (addr << host->bus_shift), buf, count); |
| 76 | } |
| 77 | |
| 78 | static u32 sd_ctrl_read32(struct tmio_mmc_host *host, int addr) |
| 79 | { |
| 80 | return readw(host->ctl + (addr << host->bus_shift)) | |
| 81 | readw(host->ctl + ((addr + 2) << host->bus_shift)) << 16; |
| 82 | } |
| 83 | |
| 84 | static void sd_ctrl_write16(struct tmio_mmc_host *host, int addr, u16 val) |
| 85 | { |
| 86 | writew(val, host->ctl + (addr << host->bus_shift)); |
| 87 | } |
| 88 | |
| 89 | static void sd_ctrl_write16_rep(struct tmio_mmc_host *host, int addr, |
| 90 | u16 *buf, int count) |
| 91 | { |
| 92 | writesw(host->ctl + (addr << host->bus_shift), buf, count); |
| 93 | } |
| 94 | |
| 95 | static void sd_ctrl_write32(struct tmio_mmc_host *host, int addr, u32 val) |
| 96 | { |
| 97 | writew(val, host->ctl + (addr << host->bus_shift)); |
| 98 | writew(val >> 16, host->ctl + ((addr + 2) << host->bus_shift)); |
| 99 | } |
| 100 | |
| 101 | void tmio_mmc_enable_mmc_irqs(struct tmio_mmc_host *host, u32 i) |
| 102 | { |
| 103 | u32 mask = sd_ctrl_read32(host, CTL_IRQ_MASK) & ~(i & TMIO_MASK_IRQ); |
| 104 | sd_ctrl_write32(host, CTL_IRQ_MASK, mask); |
| 105 | } |
| 106 | |
| 107 | void tmio_mmc_disable_mmc_irqs(struct tmio_mmc_host *host, u32 i) |
| 108 | { |
| 109 | u32 mask = sd_ctrl_read32(host, CTL_IRQ_MASK) | (i & TMIO_MASK_IRQ); |
| 110 | sd_ctrl_write32(host, CTL_IRQ_MASK, mask); |
| 111 | } |
| 112 | |
| 113 | static void tmio_mmc_ack_mmc_irqs(struct tmio_mmc_host *host, u32 i) |
| 114 | { |
| 115 | sd_ctrl_write32(host, CTL_STATUS, ~i); |
| 116 | } |
| 117 | |
| 118 | static void tmio_mmc_init_sg(struct tmio_mmc_host *host, struct mmc_data *data) |
| 119 | { |
| 120 | host->sg_len = data->sg_len; |
| 121 | host->sg_ptr = data->sg; |
| 122 | host->sg_orig = data->sg; |
| 123 | host->sg_off = 0; |
| 124 | } |
| 125 | |
| 126 | static int tmio_mmc_next_sg(struct tmio_mmc_host *host) |
| 127 | { |
| 128 | host->sg_ptr = sg_next(host->sg_ptr); |
| 129 | host->sg_off = 0; |
| 130 | return --host->sg_len; |
| 131 | } |
| 132 | |
| 133 | #ifdef CONFIG_MMC_DEBUG |
| 134 | |
| 135 | #define STATUS_TO_TEXT(a, status, i) \ |
| 136 | do { \ |
| 137 | if (status & TMIO_STAT_##a) { \ |
| 138 | if (i++) \ |
| 139 | printk(" | "); \ |
| 140 | printk(#a); \ |
| 141 | } \ |
| 142 | } while (0) |
| 143 | |
| 144 | static void pr_debug_status(u32 status) |
| 145 | { |
| 146 | int i = 0; |
| 147 | printk(KERN_DEBUG "status: %08x = ", status); |
| 148 | STATUS_TO_TEXT(CARD_REMOVE, status, i); |
| 149 | STATUS_TO_TEXT(CARD_INSERT, status, i); |
| 150 | STATUS_TO_TEXT(SIGSTATE, status, i); |
| 151 | STATUS_TO_TEXT(WRPROTECT, status, i); |
| 152 | STATUS_TO_TEXT(CARD_REMOVE_A, status, i); |
| 153 | STATUS_TO_TEXT(CARD_INSERT_A, status, i); |
| 154 | STATUS_TO_TEXT(SIGSTATE_A, status, i); |
| 155 | STATUS_TO_TEXT(CMD_IDX_ERR, status, i); |
| 156 | STATUS_TO_TEXT(STOPBIT_ERR, status, i); |
| 157 | STATUS_TO_TEXT(ILL_FUNC, status, i); |
| 158 | STATUS_TO_TEXT(CMD_BUSY, status, i); |
| 159 | STATUS_TO_TEXT(CMDRESPEND, status, i); |
| 160 | STATUS_TO_TEXT(DATAEND, status, i); |
| 161 | STATUS_TO_TEXT(CRCFAIL, status, i); |
| 162 | STATUS_TO_TEXT(DATATIMEOUT, status, i); |
| 163 | STATUS_TO_TEXT(CMDTIMEOUT, status, i); |
| 164 | STATUS_TO_TEXT(RXOVERFLOW, status, i); |
| 165 | STATUS_TO_TEXT(TXUNDERRUN, status, i); |
| 166 | STATUS_TO_TEXT(RXRDY, status, i); |
| 167 | STATUS_TO_TEXT(TXRQ, status, i); |
| 168 | STATUS_TO_TEXT(ILL_ACCESS, status, i); |
| 169 | printk("\n"); |
| 170 | } |
| 171 | |
| 172 | #else |
| 173 | #define pr_debug_status(s) do { } while (0) |
| 174 | #endif |
| 175 | |
| 176 | static void tmio_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable) |
| 177 | { |
| 178 | struct tmio_mmc_host *host = mmc_priv(mmc); |
| 179 | |
| 180 | if (enable) { |
| 181 | host->sdio_irq_enabled = 1; |
| 182 | sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0001); |
| 183 | sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, |
| 184 | (TMIO_SDIO_MASK_ALL & ~TMIO_SDIO_STAT_IOIRQ)); |
| 185 | } else { |
| 186 | sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, TMIO_SDIO_MASK_ALL); |
| 187 | sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0000); |
| 188 | host->sdio_irq_enabled = 0; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static void tmio_mmc_set_clock(struct tmio_mmc_host *host, int new_clock) |
| 193 | { |
| 194 | u32 clk = 0, clock; |
| 195 | |
| 196 | if (new_clock) { |
| 197 | for (clock = host->mmc->f_min, clk = 0x80000080; |
| 198 | new_clock >= (clock<<1); clk >>= 1) |
| 199 | clock <<= 1; |
| 200 | clk |= 0x100; |
| 201 | } |
| 202 | |
| 203 | if (host->set_clk_div) |
| 204 | host->set_clk_div(host->pdev, (clk>>22) & 1); |
| 205 | |
| 206 | sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & 0x1ff); |
| 207 | } |
| 208 | |
| 209 | static void tmio_mmc_clk_stop(struct tmio_mmc_host *host) |
| 210 | { |
Guennadi Liakhovetski | 69d1fe1 | 2011-03-09 17:28:55 +0100 | [diff] [blame] | 211 | struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0); |
Guennadi Liakhovetski | b614749 | 2011-03-23 12:42:44 +0100 | [diff] [blame] | 212 | |
Guennadi Liakhovetski | 69d1fe1 | 2011-03-09 17:28:55 +0100 | [diff] [blame] | 213 | /* implicit BUG_ON(!res) */ |
| 214 | if (resource_size(res) > 0x100) { |
| 215 | sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000); |
| 216 | msleep(10); |
| 217 | } |
Guennadi Liakhovetski | d9b0342 | 2011-03-10 18:43:07 +0100 | [diff] [blame] | 218 | |
Guennadi Liakhovetski | b614749 | 2011-03-23 12:42:44 +0100 | [diff] [blame] | 219 | sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 & |
| 220 | sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL)); |
| 221 | msleep(10); |
| 222 | } |
| 223 | |
| 224 | static void tmio_mmc_clk_start(struct tmio_mmc_host *host) |
| 225 | { |
Guennadi Liakhovetski | 69d1fe1 | 2011-03-09 17:28:55 +0100 | [diff] [blame] | 226 | struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0); |
Guennadi Liakhovetski | b614749 | 2011-03-23 12:42:44 +0100 | [diff] [blame] | 227 | |
| 228 | sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 | |
| 229 | sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL)); |
| 230 | msleep(10); |
Guennadi Liakhovetski | d9b0342 | 2011-03-10 18:43:07 +0100 | [diff] [blame] | 231 | |
Guennadi Liakhovetski | 69d1fe1 | 2011-03-09 17:28:55 +0100 | [diff] [blame] | 232 | /* implicit BUG_ON(!res) */ |
| 233 | if (resource_size(res) > 0x100) { |
| 234 | sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100); |
| 235 | msleep(10); |
| 236 | } |
Guennadi Liakhovetski | b614749 | 2011-03-23 12:42:44 +0100 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static void tmio_mmc_reset(struct tmio_mmc_host *host) |
| 240 | { |
Guennadi Liakhovetski | 69d1fe1 | 2011-03-09 17:28:55 +0100 | [diff] [blame] | 241 | struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0); |
| 242 | |
Guennadi Liakhovetski | b614749 | 2011-03-23 12:42:44 +0100 | [diff] [blame] | 243 | /* FIXME - should we set stop clock reg here */ |
| 244 | sd_ctrl_write16(host, CTL_RESET_SD, 0x0000); |
Guennadi Liakhovetski | 69d1fe1 | 2011-03-09 17:28:55 +0100 | [diff] [blame] | 245 | /* implicit BUG_ON(!res) */ |
| 246 | if (resource_size(res) > 0x100) |
| 247 | sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000); |
Guennadi Liakhovetski | b614749 | 2011-03-23 12:42:44 +0100 | [diff] [blame] | 248 | msleep(10); |
| 249 | sd_ctrl_write16(host, CTL_RESET_SD, 0x0001); |
Guennadi Liakhovetski | 69d1fe1 | 2011-03-09 17:28:55 +0100 | [diff] [blame] | 250 | if (resource_size(res) > 0x100) |
| 251 | sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001); |
Guennadi Liakhovetski | b614749 | 2011-03-23 12:42:44 +0100 | [diff] [blame] | 252 | msleep(10); |
| 253 | } |
| 254 | |
| 255 | static void tmio_mmc_reset_work(struct work_struct *work) |
| 256 | { |
| 257 | struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host, |
| 258 | delayed_reset_work.work); |
| 259 | struct mmc_request *mrq; |
| 260 | unsigned long flags; |
| 261 | |
| 262 | spin_lock_irqsave(&host->lock, flags); |
| 263 | mrq = host->mrq; |
| 264 | |
| 265 | /* request already finished */ |
| 266 | if (!mrq |
| 267 | || time_is_after_jiffies(host->last_req_ts + |
| 268 | msecs_to_jiffies(2000))) { |
| 269 | spin_unlock_irqrestore(&host->lock, flags); |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | dev_warn(&host->pdev->dev, |
| 274 | "timeout waiting for hardware interrupt (CMD%u)\n", |
| 275 | mrq->cmd->opcode); |
| 276 | |
| 277 | if (host->data) |
| 278 | host->data->error = -ETIMEDOUT; |
| 279 | else if (host->cmd) |
| 280 | host->cmd->error = -ETIMEDOUT; |
| 281 | else |
| 282 | mrq->cmd->error = -ETIMEDOUT; |
| 283 | |
| 284 | host->cmd = NULL; |
| 285 | host->data = NULL; |
| 286 | host->mrq = NULL; |
| 287 | host->force_pio = false; |
| 288 | |
| 289 | spin_unlock_irqrestore(&host->lock, flags); |
| 290 | |
| 291 | tmio_mmc_reset(host); |
| 292 | |
| 293 | mmc_request_done(host->mmc, mrq); |
| 294 | } |
| 295 | |
| 296 | static void tmio_mmc_finish_request(struct tmio_mmc_host *host) |
| 297 | { |
| 298 | struct mmc_request *mrq = host->mrq; |
| 299 | |
| 300 | if (!mrq) |
| 301 | return; |
| 302 | |
| 303 | host->mrq = NULL; |
| 304 | host->cmd = NULL; |
| 305 | host->data = NULL; |
| 306 | host->force_pio = false; |
| 307 | |
| 308 | cancel_delayed_work(&host->delayed_reset_work); |
| 309 | |
| 310 | mmc_request_done(host->mmc, mrq); |
| 311 | } |
| 312 | |
| 313 | /* These are the bitmasks the tmio chip requires to implement the MMC response |
| 314 | * types. Note that R1 and R6 are the same in this scheme. */ |
| 315 | #define APP_CMD 0x0040 |
| 316 | #define RESP_NONE 0x0300 |
| 317 | #define RESP_R1 0x0400 |
| 318 | #define RESP_R1B 0x0500 |
| 319 | #define RESP_R2 0x0600 |
| 320 | #define RESP_R3 0x0700 |
| 321 | #define DATA_PRESENT 0x0800 |
| 322 | #define TRANSFER_READ 0x1000 |
| 323 | #define TRANSFER_MULTI 0x2000 |
| 324 | #define SECURITY_CMD 0x4000 |
| 325 | |
| 326 | static int tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd) |
| 327 | { |
| 328 | struct mmc_data *data = host->data; |
| 329 | int c = cmd->opcode; |
| 330 | |
| 331 | /* Command 12 is handled by hardware */ |
| 332 | if (cmd->opcode == 12 && !cmd->arg) { |
| 333 | sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001); |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | switch (mmc_resp_type(cmd)) { |
| 338 | case MMC_RSP_NONE: c |= RESP_NONE; break; |
| 339 | case MMC_RSP_R1: c |= RESP_R1; break; |
| 340 | case MMC_RSP_R1B: c |= RESP_R1B; break; |
| 341 | case MMC_RSP_R2: c |= RESP_R2; break; |
| 342 | case MMC_RSP_R3: c |= RESP_R3; break; |
| 343 | default: |
| 344 | pr_debug("Unknown response type %d\n", mmc_resp_type(cmd)); |
| 345 | return -EINVAL; |
| 346 | } |
| 347 | |
| 348 | host->cmd = cmd; |
| 349 | |
| 350 | /* FIXME - this seems to be ok commented out but the spec suggest this bit |
| 351 | * should be set when issuing app commands. |
| 352 | * if(cmd->flags & MMC_FLAG_ACMD) |
| 353 | * c |= APP_CMD; |
| 354 | */ |
| 355 | if (data) { |
| 356 | c |= DATA_PRESENT; |
| 357 | if (data->blocks > 1) { |
| 358 | sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100); |
| 359 | c |= TRANSFER_MULTI; |
| 360 | } |
| 361 | if (data->flags & MMC_DATA_READ) |
| 362 | c |= TRANSFER_READ; |
| 363 | } |
| 364 | |
| 365 | tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_CMD); |
| 366 | |
| 367 | /* Fire off the command */ |
| 368 | sd_ctrl_write32(host, CTL_ARG_REG, cmd->arg); |
| 369 | sd_ctrl_write16(host, CTL_SD_CMD, c); |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | * This chip always returns (at least?) as much data as you ask for. |
| 376 | * I'm unsure what happens if you ask for less than a block. This should be |
| 377 | * looked into to ensure that a funny length read doesnt hose the controller. |
| 378 | */ |
| 379 | static void tmio_mmc_pio_irq(struct tmio_mmc_host *host) |
| 380 | { |
| 381 | struct mmc_data *data = host->data; |
| 382 | void *sg_virt; |
| 383 | unsigned short *buf; |
| 384 | unsigned int count; |
| 385 | unsigned long flags; |
| 386 | |
| 387 | if ((host->chan_tx || host->chan_rx) && !host->force_pio) { |
| 388 | pr_err("PIO IRQ in DMA mode!\n"); |
| 389 | return; |
| 390 | } else if (!data) { |
| 391 | pr_debug("Spurious PIO IRQ\n"); |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | sg_virt = tmio_mmc_kmap_atomic(host->sg_ptr, &flags); |
| 396 | buf = (unsigned short *)(sg_virt + host->sg_off); |
| 397 | |
| 398 | count = host->sg_ptr->length - host->sg_off; |
| 399 | if (count > data->blksz) |
| 400 | count = data->blksz; |
| 401 | |
| 402 | pr_debug("count: %08x offset: %08x flags %08x\n", |
| 403 | count, host->sg_off, data->flags); |
| 404 | |
| 405 | /* Transfer the data */ |
| 406 | if (data->flags & MMC_DATA_READ) |
| 407 | sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1); |
| 408 | else |
| 409 | sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1); |
| 410 | |
| 411 | host->sg_off += count; |
| 412 | |
| 413 | tmio_mmc_kunmap_atomic(host->sg_ptr, &flags, sg_virt); |
| 414 | |
| 415 | if (host->sg_off == host->sg_ptr->length) |
| 416 | tmio_mmc_next_sg(host); |
| 417 | |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | static void tmio_mmc_check_bounce_buffer(struct tmio_mmc_host *host) |
| 422 | { |
| 423 | if (host->sg_ptr == &host->bounce_sg) { |
| 424 | unsigned long flags; |
| 425 | void *sg_vaddr = tmio_mmc_kmap_atomic(host->sg_orig, &flags); |
| 426 | memcpy(sg_vaddr, host->bounce_buf, host->bounce_sg.length); |
| 427 | tmio_mmc_kunmap_atomic(host->sg_orig, &flags, sg_vaddr); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /* needs to be called with host->lock held */ |
| 432 | void tmio_mmc_do_data_irq(struct tmio_mmc_host *host) |
| 433 | { |
| 434 | struct mmc_data *data = host->data; |
| 435 | struct mmc_command *stop; |
| 436 | |
| 437 | host->data = NULL; |
| 438 | |
| 439 | if (!data) { |
| 440 | dev_warn(&host->pdev->dev, "Spurious data end IRQ\n"); |
| 441 | return; |
| 442 | } |
| 443 | stop = data->stop; |
| 444 | |
| 445 | /* FIXME - return correct transfer count on errors */ |
| 446 | if (!data->error) |
| 447 | data->bytes_xfered = data->blocks * data->blksz; |
| 448 | else |
| 449 | data->bytes_xfered = 0; |
| 450 | |
| 451 | pr_debug("Completed data request\n"); |
| 452 | |
| 453 | /* |
| 454 | * FIXME: other drivers allow an optional stop command of any given type |
| 455 | * which we dont do, as the chip can auto generate them. |
| 456 | * Perhaps we can be smarter about when to use auto CMD12 and |
| 457 | * only issue the auto request when we know this is the desired |
| 458 | * stop command, allowing fallback to the stop command the |
| 459 | * upper layers expect. For now, we do what works. |
| 460 | */ |
| 461 | |
| 462 | if (data->flags & MMC_DATA_READ) { |
| 463 | if (host->chan_rx && !host->force_pio) |
| 464 | tmio_mmc_check_bounce_buffer(host); |
| 465 | dev_dbg(&host->pdev->dev, "Complete Rx request %p\n", |
| 466 | host->mrq); |
| 467 | } else { |
| 468 | dev_dbg(&host->pdev->dev, "Complete Tx request %p\n", |
| 469 | host->mrq); |
| 470 | } |
| 471 | |
| 472 | if (stop) { |
| 473 | if (stop->opcode == 12 && !stop->arg) |
| 474 | sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000); |
| 475 | else |
| 476 | BUG(); |
| 477 | } |
| 478 | |
| 479 | tmio_mmc_finish_request(host); |
| 480 | } |
| 481 | |
| 482 | static void tmio_mmc_data_irq(struct tmio_mmc_host *host) |
| 483 | { |
| 484 | struct mmc_data *data; |
| 485 | spin_lock(&host->lock); |
| 486 | data = host->data; |
| 487 | |
| 488 | if (!data) |
| 489 | goto out; |
| 490 | |
| 491 | if (host->chan_tx && (data->flags & MMC_DATA_WRITE) && !host->force_pio) { |
| 492 | /* |
| 493 | * Has all data been written out yet? Testing on SuperH showed, |
| 494 | * that in most cases the first interrupt comes already with the |
| 495 | * BUSY status bit clear, but on some operations, like mount or |
| 496 | * in the beginning of a write / sync / umount, there is one |
| 497 | * DATAEND interrupt with the BUSY bit set, in this cases |
| 498 | * waiting for one more interrupt fixes the problem. |
| 499 | */ |
| 500 | if (!(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_CMD_BUSY)) { |
| 501 | tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND); |
| 502 | tasklet_schedule(&host->dma_complete); |
| 503 | } |
| 504 | } else if (host->chan_rx && (data->flags & MMC_DATA_READ) && !host->force_pio) { |
| 505 | tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND); |
| 506 | tasklet_schedule(&host->dma_complete); |
| 507 | } else { |
| 508 | tmio_mmc_do_data_irq(host); |
| 509 | tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_READOP | TMIO_MASK_WRITEOP); |
| 510 | } |
| 511 | out: |
| 512 | spin_unlock(&host->lock); |
| 513 | } |
| 514 | |
| 515 | static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host, |
| 516 | unsigned int stat) |
| 517 | { |
| 518 | struct mmc_command *cmd = host->cmd; |
| 519 | int i, addr; |
| 520 | |
| 521 | spin_lock(&host->lock); |
| 522 | |
| 523 | if (!host->cmd) { |
| 524 | pr_debug("Spurious CMD irq\n"); |
| 525 | goto out; |
| 526 | } |
| 527 | |
| 528 | host->cmd = NULL; |
| 529 | |
| 530 | /* This controller is sicker than the PXA one. Not only do we need to |
| 531 | * drop the top 8 bits of the first response word, we also need to |
| 532 | * modify the order of the response for short response command types. |
| 533 | */ |
| 534 | |
| 535 | for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4) |
| 536 | cmd->resp[i] = sd_ctrl_read32(host, addr); |
| 537 | |
| 538 | if (cmd->flags & MMC_RSP_136) { |
| 539 | cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24); |
| 540 | cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24); |
| 541 | cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24); |
| 542 | cmd->resp[3] <<= 8; |
| 543 | } else if (cmd->flags & MMC_RSP_R3) { |
| 544 | cmd->resp[0] = cmd->resp[3]; |
| 545 | } |
| 546 | |
| 547 | if (stat & TMIO_STAT_CMDTIMEOUT) |
| 548 | cmd->error = -ETIMEDOUT; |
| 549 | else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC) |
| 550 | cmd->error = -EILSEQ; |
| 551 | |
| 552 | /* If there is data to handle we enable data IRQs here, and |
| 553 | * we will ultimatley finish the request in the data_end handler. |
| 554 | * If theres no data or we encountered an error, finish now. |
| 555 | */ |
| 556 | if (host->data && !cmd->error) { |
| 557 | if (host->data->flags & MMC_DATA_READ) { |
| 558 | if (host->force_pio || !host->chan_rx) |
| 559 | tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_READOP); |
| 560 | else |
| 561 | tasklet_schedule(&host->dma_issue); |
| 562 | } else { |
| 563 | if (host->force_pio || !host->chan_tx) |
| 564 | tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_WRITEOP); |
| 565 | else |
| 566 | tasklet_schedule(&host->dma_issue); |
| 567 | } |
| 568 | } else { |
| 569 | tmio_mmc_finish_request(host); |
| 570 | } |
| 571 | |
| 572 | out: |
| 573 | spin_unlock(&host->lock); |
| 574 | } |
| 575 | |
| 576 | static irqreturn_t tmio_mmc_irq(int irq, void *devid) |
| 577 | { |
| 578 | struct tmio_mmc_host *host = devid; |
| 579 | struct tmio_mmc_data *pdata = host->pdata; |
| 580 | unsigned int ireg, irq_mask, status; |
| 581 | unsigned int sdio_ireg, sdio_irq_mask, sdio_status; |
| 582 | |
| 583 | pr_debug("MMC IRQ begin\n"); |
| 584 | |
| 585 | status = sd_ctrl_read32(host, CTL_STATUS); |
| 586 | irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK); |
| 587 | ireg = status & TMIO_MASK_IRQ & ~irq_mask; |
| 588 | |
| 589 | sdio_ireg = 0; |
| 590 | if (!ireg && pdata->flags & TMIO_MMC_SDIO_IRQ) { |
| 591 | sdio_status = sd_ctrl_read16(host, CTL_SDIO_STATUS); |
| 592 | sdio_irq_mask = sd_ctrl_read16(host, CTL_SDIO_IRQ_MASK); |
| 593 | sdio_ireg = sdio_status & TMIO_SDIO_MASK_ALL & ~sdio_irq_mask; |
| 594 | |
| 595 | sd_ctrl_write16(host, CTL_SDIO_STATUS, sdio_status & ~TMIO_SDIO_MASK_ALL); |
| 596 | |
| 597 | if (sdio_ireg && !host->sdio_irq_enabled) { |
| 598 | pr_warning("tmio_mmc: Spurious SDIO IRQ, disabling! 0x%04x 0x%04x 0x%04x\n", |
| 599 | sdio_status, sdio_irq_mask, sdio_ireg); |
| 600 | tmio_mmc_enable_sdio_irq(host->mmc, 0); |
| 601 | goto out; |
| 602 | } |
| 603 | |
| 604 | if (host->mmc->caps & MMC_CAP_SDIO_IRQ && |
| 605 | sdio_ireg & TMIO_SDIO_STAT_IOIRQ) |
| 606 | mmc_signal_sdio_irq(host->mmc); |
| 607 | |
| 608 | if (sdio_ireg) |
| 609 | goto out; |
| 610 | } |
| 611 | |
| 612 | pr_debug_status(status); |
| 613 | pr_debug_status(ireg); |
| 614 | |
| 615 | if (!ireg) { |
| 616 | tmio_mmc_disable_mmc_irqs(host, status & ~irq_mask); |
| 617 | |
| 618 | pr_warning("tmio_mmc: Spurious irq, disabling! " |
| 619 | "0x%08x 0x%08x 0x%08x\n", status, irq_mask, ireg); |
| 620 | pr_debug_status(status); |
| 621 | |
| 622 | goto out; |
| 623 | } |
| 624 | |
| 625 | while (ireg) { |
| 626 | /* Card insert / remove attempts */ |
| 627 | if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) { |
| 628 | tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT | |
| 629 | TMIO_STAT_CARD_REMOVE); |
| 630 | mmc_detect_change(host->mmc, msecs_to_jiffies(100)); |
| 631 | } |
| 632 | |
| 633 | /* CRC and other errors */ |
| 634 | /* if (ireg & TMIO_STAT_ERR_IRQ) |
| 635 | * handled |= tmio_error_irq(host, irq, stat); |
| 636 | */ |
| 637 | |
| 638 | /* Command completion */ |
| 639 | if (ireg & (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT)) { |
| 640 | tmio_mmc_ack_mmc_irqs(host, |
| 641 | TMIO_STAT_CMDRESPEND | |
| 642 | TMIO_STAT_CMDTIMEOUT); |
| 643 | tmio_mmc_cmd_irq(host, status); |
| 644 | } |
| 645 | |
| 646 | /* Data transfer */ |
| 647 | if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) { |
| 648 | tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ); |
| 649 | tmio_mmc_pio_irq(host); |
| 650 | } |
| 651 | |
| 652 | /* Data transfer completion */ |
| 653 | if (ireg & TMIO_STAT_DATAEND) { |
| 654 | tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_DATAEND); |
| 655 | tmio_mmc_data_irq(host); |
| 656 | } |
| 657 | |
| 658 | /* Check status - keep going until we've handled it all */ |
| 659 | status = sd_ctrl_read32(host, CTL_STATUS); |
| 660 | irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK); |
| 661 | ireg = status & TMIO_MASK_IRQ & ~irq_mask; |
| 662 | |
| 663 | pr_debug("Status at end of loop: %08x\n", status); |
| 664 | pr_debug_status(status); |
| 665 | } |
| 666 | pr_debug("MMC IRQ end\n"); |
| 667 | |
| 668 | out: |
| 669 | return IRQ_HANDLED; |
| 670 | } |
| 671 | |
| 672 | static int tmio_mmc_start_data(struct tmio_mmc_host *host, |
| 673 | struct mmc_data *data) |
| 674 | { |
| 675 | struct tmio_mmc_data *pdata = host->pdata; |
| 676 | |
| 677 | pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n", |
| 678 | data->blksz, data->blocks); |
| 679 | |
| 680 | /* Some hardware cannot perform 2 byte requests in 4 bit mode */ |
| 681 | if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) { |
| 682 | int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES; |
| 683 | |
| 684 | if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) { |
| 685 | pr_err("%s: %d byte block unsupported in 4 bit mode\n", |
| 686 | mmc_hostname(host->mmc), data->blksz); |
| 687 | return -EINVAL; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | tmio_mmc_init_sg(host, data); |
| 692 | host->data = data; |
| 693 | |
| 694 | /* Set transfer length / blocksize */ |
| 695 | sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz); |
| 696 | sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks); |
| 697 | |
| 698 | tmio_mmc_start_dma(host, data); |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | /* Process requests from the MMC layer */ |
| 704 | static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 705 | { |
| 706 | struct tmio_mmc_host *host = mmc_priv(mmc); |
| 707 | int ret; |
| 708 | |
| 709 | if (host->mrq) |
| 710 | pr_debug("request not null\n"); |
| 711 | |
| 712 | host->last_req_ts = jiffies; |
| 713 | wmb(); |
| 714 | host->mrq = mrq; |
| 715 | |
| 716 | if (mrq->data) { |
| 717 | ret = tmio_mmc_start_data(host, mrq->data); |
| 718 | if (ret) |
| 719 | goto fail; |
| 720 | } |
| 721 | |
| 722 | ret = tmio_mmc_start_command(host, mrq->cmd); |
| 723 | if (!ret) { |
| 724 | schedule_delayed_work(&host->delayed_reset_work, |
| 725 | msecs_to_jiffies(2000)); |
| 726 | return; |
| 727 | } |
| 728 | |
| 729 | fail: |
| 730 | host->mrq = NULL; |
| 731 | host->force_pio = false; |
| 732 | mrq->cmd->error = ret; |
| 733 | mmc_request_done(mmc, mrq); |
| 734 | } |
| 735 | |
| 736 | /* Set MMC clock / power. |
| 737 | * Note: This controller uses a simple divider scheme therefore it cannot |
| 738 | * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as |
| 739 | * MMC wont run that fast, it has to be clocked at 12MHz which is the next |
| 740 | * slowest setting. |
| 741 | */ |
| 742 | static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 743 | { |
| 744 | struct tmio_mmc_host *host = mmc_priv(mmc); |
| 745 | |
| 746 | if (ios->clock) |
| 747 | tmio_mmc_set_clock(host, ios->clock); |
| 748 | |
Guennadi Liakhovetski | a7edbe3 | 2011-03-09 14:38:58 +0100 | [diff] [blame] | 749 | /* Power sequence - OFF -> UP -> ON */ |
Guennadi Liakhovetski | 5fd0157 | 2011-03-09 14:45:44 +0100 | [diff] [blame^] | 750 | if (ios->power_mode == MMC_POWER_OFF || !ios->clock) { |
| 751 | /* power down SD bus */ |
| 752 | if (ios->power_mode == MMC_POWER_OFF && host->set_pwr) |
Guennadi Liakhovetski | b614749 | 2011-03-23 12:42:44 +0100 | [diff] [blame] | 753 | host->set_pwr(host->pdev, 0); |
| 754 | tmio_mmc_clk_stop(host); |
Guennadi Liakhovetski | 5fd0157 | 2011-03-09 14:45:44 +0100 | [diff] [blame^] | 755 | } else if (ios->power_mode == MMC_POWER_UP) { |
| 756 | /* power up SD bus */ |
Guennadi Liakhovetski | b614749 | 2011-03-23 12:42:44 +0100 | [diff] [blame] | 757 | if (host->set_pwr) |
| 758 | host->set_pwr(host->pdev, 1); |
Guennadi Liakhovetski | 5fd0157 | 2011-03-09 14:45:44 +0100 | [diff] [blame^] | 759 | } else { |
| 760 | /* start bus clock */ |
| 761 | tmio_mmc_clk_start(host); |
Guennadi Liakhovetski | b614749 | 2011-03-23 12:42:44 +0100 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | switch (ios->bus_width) { |
| 765 | case MMC_BUS_WIDTH_1: |
| 766 | sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0); |
| 767 | break; |
| 768 | case MMC_BUS_WIDTH_4: |
| 769 | sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0); |
| 770 | break; |
| 771 | } |
| 772 | |
| 773 | /* Let things settle. delay taken from winCE driver */ |
| 774 | udelay(140); |
| 775 | } |
| 776 | |
| 777 | static int tmio_mmc_get_ro(struct mmc_host *mmc) |
| 778 | { |
| 779 | struct tmio_mmc_host *host = mmc_priv(mmc); |
| 780 | struct tmio_mmc_data *pdata = host->pdata; |
| 781 | |
| 782 | return ((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) || |
| 783 | !(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT)); |
| 784 | } |
| 785 | |
| 786 | static int tmio_mmc_get_cd(struct mmc_host *mmc) |
| 787 | { |
| 788 | struct tmio_mmc_host *host = mmc_priv(mmc); |
| 789 | struct tmio_mmc_data *pdata = host->pdata; |
| 790 | |
| 791 | if (!pdata->get_cd) |
| 792 | return -ENOSYS; |
| 793 | else |
| 794 | return pdata->get_cd(host->pdev); |
| 795 | } |
| 796 | |
| 797 | static const struct mmc_host_ops tmio_mmc_ops = { |
| 798 | .request = tmio_mmc_request, |
| 799 | .set_ios = tmio_mmc_set_ios, |
| 800 | .get_ro = tmio_mmc_get_ro, |
| 801 | .get_cd = tmio_mmc_get_cd, |
| 802 | .enable_sdio_irq = tmio_mmc_enable_sdio_irq, |
| 803 | }; |
| 804 | |
| 805 | int __devinit tmio_mmc_host_probe(struct tmio_mmc_host **host, |
| 806 | struct platform_device *pdev, |
| 807 | struct tmio_mmc_data *pdata) |
| 808 | { |
| 809 | struct tmio_mmc_host *_host; |
| 810 | struct mmc_host *mmc; |
| 811 | struct resource *res_ctl; |
| 812 | int ret; |
| 813 | u32 irq_mask = TMIO_MASK_CMD; |
| 814 | |
| 815 | res_ctl = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 816 | if (!res_ctl) |
| 817 | return -EINVAL; |
| 818 | |
| 819 | mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &pdev->dev); |
| 820 | if (!mmc) |
| 821 | return -ENOMEM; |
| 822 | |
| 823 | _host = mmc_priv(mmc); |
| 824 | _host->pdata = pdata; |
| 825 | _host->mmc = mmc; |
| 826 | _host->pdev = pdev; |
| 827 | platform_set_drvdata(pdev, mmc); |
| 828 | |
| 829 | _host->set_pwr = pdata->set_pwr; |
| 830 | _host->set_clk_div = pdata->set_clk_div; |
| 831 | |
| 832 | /* SD control register space size is 0x200, 0x400 for bus_shift=1 */ |
| 833 | _host->bus_shift = resource_size(res_ctl) >> 10; |
| 834 | |
| 835 | _host->ctl = ioremap(res_ctl->start, resource_size(res_ctl)); |
| 836 | if (!_host->ctl) { |
| 837 | ret = -ENOMEM; |
| 838 | goto host_free; |
| 839 | } |
| 840 | |
| 841 | mmc->ops = &tmio_mmc_ops; |
| 842 | mmc->caps = MMC_CAP_4_BIT_DATA | pdata->capabilities; |
| 843 | mmc->f_max = pdata->hclk; |
| 844 | mmc->f_min = mmc->f_max / 512; |
| 845 | mmc->max_segs = 32; |
| 846 | mmc->max_blk_size = 512; |
| 847 | mmc->max_blk_count = (PAGE_CACHE_SIZE / mmc->max_blk_size) * |
| 848 | mmc->max_segs; |
| 849 | mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count; |
| 850 | mmc->max_seg_size = mmc->max_req_size; |
| 851 | if (pdata->ocr_mask) |
| 852 | mmc->ocr_avail = pdata->ocr_mask; |
| 853 | else |
| 854 | mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 855 | |
| 856 | tmio_mmc_clk_stop(_host); |
| 857 | tmio_mmc_reset(_host); |
| 858 | |
| 859 | ret = platform_get_irq(pdev, 0); |
| 860 | if (ret < 0) |
| 861 | goto unmap_ctl; |
| 862 | |
| 863 | _host->irq = ret; |
| 864 | |
| 865 | tmio_mmc_disable_mmc_irqs(_host, TMIO_MASK_ALL); |
| 866 | if (pdata->flags & TMIO_MMC_SDIO_IRQ) |
| 867 | tmio_mmc_enable_sdio_irq(mmc, 0); |
| 868 | |
| 869 | ret = request_irq(_host->irq, tmio_mmc_irq, IRQF_DISABLED | |
| 870 | IRQF_TRIGGER_FALLING, dev_name(&pdev->dev), _host); |
| 871 | if (ret) |
| 872 | goto unmap_ctl; |
| 873 | |
| 874 | spin_lock_init(&_host->lock); |
| 875 | |
| 876 | /* Init delayed work for request timeouts */ |
| 877 | INIT_DELAYED_WORK(&_host->delayed_reset_work, tmio_mmc_reset_work); |
| 878 | |
| 879 | /* See if we also get DMA */ |
| 880 | tmio_mmc_request_dma(_host, pdata); |
| 881 | |
| 882 | mmc_add_host(mmc); |
| 883 | |
| 884 | /* Unmask the IRQs we want to know about */ |
| 885 | if (!_host->chan_rx) |
| 886 | irq_mask |= TMIO_MASK_READOP; |
| 887 | if (!_host->chan_tx) |
| 888 | irq_mask |= TMIO_MASK_WRITEOP; |
| 889 | |
| 890 | tmio_mmc_enable_mmc_irqs(_host, irq_mask); |
| 891 | |
| 892 | *host = _host; |
| 893 | |
| 894 | return 0; |
| 895 | |
| 896 | unmap_ctl: |
| 897 | iounmap(_host->ctl); |
| 898 | host_free: |
| 899 | mmc_free_host(mmc); |
| 900 | |
| 901 | return ret; |
| 902 | } |
| 903 | EXPORT_SYMBOL(tmio_mmc_host_probe); |
| 904 | |
| 905 | void tmio_mmc_host_remove(struct tmio_mmc_host *host) |
| 906 | { |
| 907 | mmc_remove_host(host->mmc); |
| 908 | cancel_delayed_work_sync(&host->delayed_reset_work); |
| 909 | tmio_mmc_release_dma(host); |
| 910 | free_irq(host->irq, host); |
| 911 | iounmap(host->ctl); |
| 912 | mmc_free_host(host->mmc); |
| 913 | } |
| 914 | EXPORT_SYMBOL(tmio_mmc_host_remove); |
| 915 | |
| 916 | MODULE_LICENSE("GPL v2"); |