Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * tifm_sd.c - TI FlashMedia driver |
| 3 | * |
| 4 | * Copyright (C) 2006 Alex Dubov <oakad@yahoo.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | |
| 13 | #include <linux/tifm.h> |
| 14 | #include <linux/mmc/protocol.h> |
| 15 | #include <linux/mmc/host.h> |
| 16 | #include <linux/highmem.h> |
Al Viro | 2099c99 | 2006-10-24 11:17:06 +0100 | [diff] [blame] | 17 | #include <asm/io.h> |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 18 | |
| 19 | #define DRIVER_NAME "tifm_sd" |
Alex Dubov | 1289335 | 2006-12-08 16:50:47 +1100 | [diff] [blame] | 20 | #define DRIVER_VERSION "0.7" |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 21 | |
| 22 | static int no_dma = 0; |
| 23 | static int fixed_timeout = 0; |
| 24 | module_param(no_dma, bool, 0644); |
| 25 | module_param(fixed_timeout, bool, 0644); |
| 26 | |
| 27 | /* Constants here are mostly from OMAP5912 datasheet */ |
| 28 | #define TIFM_MMCSD_RESET 0x0002 |
| 29 | #define TIFM_MMCSD_CLKMASK 0x03ff |
| 30 | #define TIFM_MMCSD_POWER 0x0800 |
| 31 | #define TIFM_MMCSD_4BBUS 0x8000 |
| 32 | #define TIFM_MMCSD_RXDE 0x8000 /* rx dma enable */ |
| 33 | #define TIFM_MMCSD_TXDE 0x0080 /* tx dma enable */ |
| 34 | #define TIFM_MMCSD_BUFINT 0x0c00 /* set bits: AE, AF */ |
| 35 | #define TIFM_MMCSD_DPE 0x0020 /* data timeout counted in kilocycles */ |
| 36 | #define TIFM_MMCSD_INAB 0x0080 /* abort / initialize command */ |
| 37 | #define TIFM_MMCSD_READ 0x8000 |
| 38 | |
| 39 | #define TIFM_MMCSD_DATAMASK 0x001d /* set bits: EOFB, BRS, CB, EOC */ |
| 40 | #define TIFM_MMCSD_ERRMASK 0x41e0 /* set bits: CERR, CCRC, CTO, DCRC, DTO */ |
| 41 | #define TIFM_MMCSD_EOC 0x0001 /* end of command phase */ |
| 42 | #define TIFM_MMCSD_CB 0x0004 /* card enter busy state */ |
| 43 | #define TIFM_MMCSD_BRS 0x0008 /* block received/sent */ |
| 44 | #define TIFM_MMCSD_EOFB 0x0010 /* card exit busy state */ |
| 45 | #define TIFM_MMCSD_DTO 0x0020 /* data time-out */ |
| 46 | #define TIFM_MMCSD_DCRC 0x0040 /* data crc error */ |
| 47 | #define TIFM_MMCSD_CTO 0x0080 /* command time-out */ |
| 48 | #define TIFM_MMCSD_CCRC 0x0100 /* command crc error */ |
| 49 | #define TIFM_MMCSD_AF 0x0400 /* fifo almost full */ |
| 50 | #define TIFM_MMCSD_AE 0x0800 /* fifo almost empty */ |
| 51 | #define TIFM_MMCSD_CERR 0x4000 /* card status error */ |
| 52 | |
| 53 | #define TIFM_MMCSD_FIFO_SIZE 0x0020 |
| 54 | |
| 55 | #define TIFM_MMCSD_RSP_R0 0x0000 |
| 56 | #define TIFM_MMCSD_RSP_R1 0x0100 |
| 57 | #define TIFM_MMCSD_RSP_R2 0x0200 |
| 58 | #define TIFM_MMCSD_RSP_R3 0x0300 |
| 59 | #define TIFM_MMCSD_RSP_R4 0x0400 |
| 60 | #define TIFM_MMCSD_RSP_R5 0x0500 |
| 61 | #define TIFM_MMCSD_RSP_R6 0x0600 |
| 62 | |
| 63 | #define TIFM_MMCSD_RSP_BUSY 0x0800 |
| 64 | |
| 65 | #define TIFM_MMCSD_CMD_BC 0x0000 |
| 66 | #define TIFM_MMCSD_CMD_BCR 0x1000 |
| 67 | #define TIFM_MMCSD_CMD_AC 0x2000 |
| 68 | #define TIFM_MMCSD_CMD_ADTC 0x3000 |
| 69 | |
| 70 | typedef enum { |
| 71 | IDLE = 0, |
| 72 | CMD, /* main command ended */ |
| 73 | BRS, /* block transfer finished */ |
| 74 | SCMD, /* stop command ended */ |
| 75 | CARD, /* card left busy state */ |
| 76 | FIFO, /* FIFO operation completed (uncertain) */ |
| 77 | READY |
| 78 | } card_state_t; |
| 79 | |
| 80 | enum { |
| 81 | FIFO_RDY = 0x0001, /* hardware dependent value */ |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 82 | EJECT = 0x0004, |
| 83 | EJECT_DONE = 0x0008, |
| 84 | CARD_BUSY = 0x0010, |
| 85 | OPENDRAIN = 0x0040, /* hardware dependent value */ |
| 86 | CARD_EVENT = 0x0100, /* hardware dependent value */ |
| 87 | CARD_RO = 0x0200, /* hardware dependent value */ |
| 88 | FIFO_EVENT = 0x10000 }; /* hardware dependent value */ |
| 89 | |
| 90 | struct tifm_sd { |
| 91 | struct tifm_dev *dev; |
| 92 | |
| 93 | unsigned int flags; |
| 94 | card_state_t state; |
| 95 | unsigned int clk_freq; |
| 96 | unsigned int clk_div; |
Alex Dubov | 0803dd0 | 2006-12-08 16:50:49 +1100 | [diff] [blame] | 97 | unsigned long timeout_jiffies; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 98 | |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 99 | struct tasklet_struct finish_tasklet; |
Alex Dubov | 0803dd0 | 2006-12-08 16:50:49 +1100 | [diff] [blame] | 100 | struct timer_list timer; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 101 | struct mmc_request *req; |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 102 | wait_queue_head_t notify; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 103 | |
| 104 | size_t written_blocks; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 105 | size_t buffer_size; |
| 106 | size_t buffer_pos; |
| 107 | |
| 108 | }; |
| 109 | |
Pierre Ossman | f9d429a | 2007-02-03 13:36:41 +0100 | [diff] [blame] | 110 | static char* tifm_sd_data_buffer(struct mmc_data *data) |
Alex Dubov | 255ef22 | 2006-12-08 16:50:48 +1100 | [diff] [blame] | 111 | { |
Pierre Ossman | f9d429a | 2007-02-03 13:36:41 +0100 | [diff] [blame] | 112 | return page_address(data->sg->page) + data->sg->offset; |
Alex Dubov | 255ef22 | 2006-12-08 16:50:48 +1100 | [diff] [blame] | 113 | } |
| 114 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 115 | static int tifm_sd_transfer_data(struct tifm_dev *sock, struct tifm_sd *host, |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 116 | unsigned int host_status) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 117 | { |
| 118 | struct mmc_command *cmd = host->req->cmd; |
| 119 | unsigned int t_val = 0, cnt = 0; |
Alex Dubov | 255ef22 | 2006-12-08 16:50:48 +1100 | [diff] [blame] | 120 | char *buffer; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 121 | |
| 122 | if (host_status & TIFM_MMCSD_BRS) { |
| 123 | /* in non-dma rx mode BRS fires when fifo is still not empty */ |
Alex Dubov | 255ef22 | 2006-12-08 16:50:48 +1100 | [diff] [blame] | 124 | if (no_dma && (cmd->data->flags & MMC_DATA_READ)) { |
Pierre Ossman | f9d429a | 2007-02-03 13:36:41 +0100 | [diff] [blame] | 125 | buffer = tifm_sd_data_buffer(host->req->data); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 126 | while (host->buffer_size > host->buffer_pos) { |
| 127 | t_val = readl(sock->addr + SOCK_MMCSD_DATA); |
Alex Dubov | 255ef22 | 2006-12-08 16:50:48 +1100 | [diff] [blame] | 128 | buffer[host->buffer_pos++] = t_val & 0xff; |
| 129 | buffer[host->buffer_pos++] = |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 130 | (t_val >> 8) & 0xff; |
| 131 | } |
| 132 | } |
| 133 | return 1; |
Alex Dubov | 255ef22 | 2006-12-08 16:50:48 +1100 | [diff] [blame] | 134 | } else if (no_dma) { |
Pierre Ossman | f9d429a | 2007-02-03 13:36:41 +0100 | [diff] [blame] | 135 | buffer = tifm_sd_data_buffer(host->req->data); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 136 | if ((cmd->data->flags & MMC_DATA_READ) && |
| 137 | (host_status & TIFM_MMCSD_AF)) { |
| 138 | for (cnt = 0; cnt < TIFM_MMCSD_FIFO_SIZE; cnt++) { |
| 139 | t_val = readl(sock->addr + SOCK_MMCSD_DATA); |
| 140 | if (host->buffer_size > host->buffer_pos) { |
Alex Dubov | 255ef22 | 2006-12-08 16:50:48 +1100 | [diff] [blame] | 141 | buffer[host->buffer_pos++] = |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 142 | t_val & 0xff; |
Alex Dubov | 255ef22 | 2006-12-08 16:50:48 +1100 | [diff] [blame] | 143 | buffer[host->buffer_pos++] = |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 144 | (t_val >> 8) & 0xff; |
| 145 | } |
| 146 | } |
| 147 | } else if ((cmd->data->flags & MMC_DATA_WRITE) |
| 148 | && (host_status & TIFM_MMCSD_AE)) { |
| 149 | for (cnt = 0; cnt < TIFM_MMCSD_FIFO_SIZE; cnt++) { |
| 150 | if (host->buffer_size > host->buffer_pos) { |
Alex Dubov | 255ef22 | 2006-12-08 16:50:48 +1100 | [diff] [blame] | 151 | t_val = buffer[host->buffer_pos++] |
| 152 | & 0x00ff; |
| 153 | t_val |= ((buffer[host->buffer_pos++]) |
| 154 | << 8) & 0xff00; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 155 | writel(t_val, |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 156 | sock->addr + SOCK_MMCSD_DATA); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static unsigned int tifm_sd_op_flags(struct mmc_command *cmd) |
| 165 | { |
| 166 | unsigned int rc = 0; |
| 167 | |
| 168 | switch (mmc_resp_type(cmd)) { |
| 169 | case MMC_RSP_NONE: |
| 170 | rc |= TIFM_MMCSD_RSP_R0; |
| 171 | break; |
| 172 | case MMC_RSP_R1B: |
| 173 | rc |= TIFM_MMCSD_RSP_BUSY; // deliberate fall-through |
| 174 | case MMC_RSP_R1: |
| 175 | rc |= TIFM_MMCSD_RSP_R1; |
| 176 | break; |
| 177 | case MMC_RSP_R2: |
| 178 | rc |= TIFM_MMCSD_RSP_R2; |
| 179 | break; |
| 180 | case MMC_RSP_R3: |
| 181 | rc |= TIFM_MMCSD_RSP_R3; |
| 182 | break; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 183 | default: |
| 184 | BUG(); |
| 185 | } |
| 186 | |
| 187 | switch (mmc_cmd_type(cmd)) { |
| 188 | case MMC_CMD_BC: |
| 189 | rc |= TIFM_MMCSD_CMD_BC; |
| 190 | break; |
| 191 | case MMC_CMD_BCR: |
| 192 | rc |= TIFM_MMCSD_CMD_BCR; |
| 193 | break; |
| 194 | case MMC_CMD_AC: |
| 195 | rc |= TIFM_MMCSD_CMD_AC; |
| 196 | break; |
| 197 | case MMC_CMD_ADTC: |
| 198 | rc |= TIFM_MMCSD_CMD_ADTC; |
| 199 | break; |
| 200 | default: |
| 201 | BUG(); |
| 202 | } |
| 203 | return rc; |
| 204 | } |
| 205 | |
| 206 | static void tifm_sd_exec(struct tifm_sd *host, struct mmc_command *cmd) |
| 207 | { |
| 208 | struct tifm_dev *sock = host->dev; |
| 209 | unsigned int cmd_mask = tifm_sd_op_flags(cmd) | |
| 210 | (host->flags & OPENDRAIN); |
| 211 | |
| 212 | if (cmd->data && (cmd->data->flags & MMC_DATA_READ)) |
| 213 | cmd_mask |= TIFM_MMCSD_READ; |
| 214 | |
| 215 | dev_dbg(&sock->dev, "executing opcode 0x%x, arg: 0x%x, mask: 0x%x\n", |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 216 | cmd->opcode, cmd->arg, cmd_mask); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 217 | |
| 218 | writel((cmd->arg >> 16) & 0xffff, sock->addr + SOCK_MMCSD_ARG_HIGH); |
| 219 | writel(cmd->arg & 0xffff, sock->addr + SOCK_MMCSD_ARG_LOW); |
| 220 | writel(cmd->opcode | cmd_mask, sock->addr + SOCK_MMCSD_COMMAND); |
| 221 | } |
| 222 | |
| 223 | static void tifm_sd_fetch_resp(struct mmc_command *cmd, struct tifm_dev *sock) |
| 224 | { |
| 225 | cmd->resp[0] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x1c) << 16) |
| 226 | | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x18); |
| 227 | cmd->resp[1] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x14) << 16) |
| 228 | | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x10); |
| 229 | cmd->resp[2] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x0c) << 16) |
| 230 | | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x08); |
| 231 | cmd->resp[3] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x04) << 16) |
| 232 | | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x00); |
| 233 | } |
| 234 | |
| 235 | static void tifm_sd_process_cmd(struct tifm_dev *sock, struct tifm_sd *host, |
| 236 | unsigned int host_status) |
| 237 | { |
| 238 | struct mmc_command *cmd = host->req->cmd; |
| 239 | |
| 240 | change_state: |
| 241 | switch (host->state) { |
| 242 | case IDLE: |
| 243 | return; |
| 244 | case CMD: |
| 245 | if (host_status & TIFM_MMCSD_EOC) { |
| 246 | tifm_sd_fetch_resp(cmd, sock); |
| 247 | if (cmd->data) { |
| 248 | host->state = BRS; |
Alex Dubov | 1289335 | 2006-12-08 16:50:47 +1100 | [diff] [blame] | 249 | } else { |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 250 | host->state = READY; |
Alex Dubov | 1289335 | 2006-12-08 16:50:47 +1100 | [diff] [blame] | 251 | } |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 252 | goto change_state; |
| 253 | } |
| 254 | break; |
| 255 | case BRS: |
| 256 | if (tifm_sd_transfer_data(sock, host, host_status)) { |
Alex Dubov | 1289335 | 2006-12-08 16:50:47 +1100 | [diff] [blame] | 257 | if (cmd->data->flags & MMC_DATA_WRITE) { |
| 258 | host->state = CARD; |
| 259 | } else { |
| 260 | if (no_dma) { |
| 261 | if (host->req->stop) { |
| 262 | tifm_sd_exec(host, host->req->stop); |
| 263 | host->state = SCMD; |
| 264 | } else { |
| 265 | host->state = READY; |
| 266 | } |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 267 | } else { |
Alex Dubov | 1289335 | 2006-12-08 16:50:47 +1100 | [diff] [blame] | 268 | host->state = FIFO; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 269 | } |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 270 | } |
Alex Dubov | 1289335 | 2006-12-08 16:50:47 +1100 | [diff] [blame] | 271 | goto change_state; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 272 | } |
| 273 | break; |
| 274 | case SCMD: |
| 275 | if (host_status & TIFM_MMCSD_EOC) { |
| 276 | tifm_sd_fetch_resp(host->req->stop, sock); |
Alex Dubov | 1289335 | 2006-12-08 16:50:47 +1100 | [diff] [blame] | 277 | host->state = READY; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 278 | goto change_state; |
| 279 | } |
| 280 | break; |
| 281 | case CARD: |
Alex Dubov | 1289335 | 2006-12-08 16:50:47 +1100 | [diff] [blame] | 282 | dev_dbg(&sock->dev, "waiting for CARD, have %zd blocks\n", |
| 283 | host->written_blocks); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 284 | if (!(host->flags & CARD_BUSY) |
| 285 | && (host->written_blocks == cmd->data->blocks)) { |
Alex Dubov | 1289335 | 2006-12-08 16:50:47 +1100 | [diff] [blame] | 286 | if (no_dma) { |
| 287 | if (host->req->stop) { |
| 288 | tifm_sd_exec(host, host->req->stop); |
| 289 | host->state = SCMD; |
| 290 | } else { |
| 291 | host->state = READY; |
| 292 | } |
| 293 | } else { |
| 294 | host->state = FIFO; |
| 295 | } |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 296 | goto change_state; |
| 297 | } |
| 298 | break; |
| 299 | case FIFO: |
| 300 | if (host->flags & FIFO_RDY) { |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 301 | host->flags &= ~FIFO_RDY; |
Alex Dubov | 1289335 | 2006-12-08 16:50:47 +1100 | [diff] [blame] | 302 | if (host->req->stop) { |
| 303 | tifm_sd_exec(host, host->req->stop); |
| 304 | host->state = SCMD; |
| 305 | } else { |
| 306 | host->state = READY; |
| 307 | } |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 308 | goto change_state; |
| 309 | } |
| 310 | break; |
| 311 | case READY: |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 312 | tasklet_schedule(&host->finish_tasklet); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 313 | return; |
| 314 | } |
| 315 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /* Called from interrupt handler */ |
Alex Dubov | 50743f4 | 2006-12-11 01:55:30 +1100 | [diff] [blame] | 319 | static void tifm_sd_signal_irq(struct tifm_dev *sock, |
| 320 | unsigned int sock_irq_status) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 321 | { |
| 322 | struct tifm_sd *host; |
| 323 | unsigned int host_status = 0, fifo_status = 0; |
| 324 | int error_code = 0; |
| 325 | |
| 326 | spin_lock(&sock->lock); |
| 327 | host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock)); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 328 | |
| 329 | if (sock_irq_status & FIFO_EVENT) { |
| 330 | fifo_status = readl(sock->addr + SOCK_DMA_FIFO_STATUS); |
| 331 | writel(fifo_status, sock->addr + SOCK_DMA_FIFO_STATUS); |
| 332 | |
| 333 | host->flags |= fifo_status & FIFO_RDY; |
| 334 | } |
| 335 | |
| 336 | if (sock_irq_status & CARD_EVENT) { |
| 337 | host_status = readl(sock->addr + SOCK_MMCSD_STATUS); |
| 338 | writel(host_status, sock->addr + SOCK_MMCSD_STATUS); |
| 339 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 340 | if (!host->req) |
| 341 | goto done; |
| 342 | |
| 343 | if (host_status & TIFM_MMCSD_ERRMASK) { |
| 344 | if (host_status & TIFM_MMCSD_CERR) |
| 345 | error_code = MMC_ERR_FAILED; |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 346 | else if (host_status |
| 347 | & (TIFM_MMCSD_CTO | TIFM_MMCSD_DTO)) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 348 | error_code = MMC_ERR_TIMEOUT; |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 349 | else if (host_status |
| 350 | & (TIFM_MMCSD_CCRC | TIFM_MMCSD_DCRC)) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 351 | error_code = MMC_ERR_BADCRC; |
| 352 | |
| 353 | writel(TIFM_FIFO_INT_SETALL, |
| 354 | sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR); |
| 355 | writel(TIFM_DMA_RESET, sock->addr + SOCK_DMA_CONTROL); |
| 356 | |
| 357 | if (host->req->stop) { |
| 358 | if (host->state == SCMD) { |
| 359 | host->req->stop->error = error_code; |
Alex Dubov | 1289335 | 2006-12-08 16:50:47 +1100 | [diff] [blame] | 360 | } else if (host->state == BRS |
| 361 | || host->state == CARD |
| 362 | || host->state == FIFO) { |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 363 | host->req->cmd->error = error_code; |
| 364 | tifm_sd_exec(host, host->req->stop); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 365 | host->state = SCMD; |
| 366 | goto done; |
| 367 | } else { |
| 368 | host->req->cmd->error = error_code; |
| 369 | } |
| 370 | } else { |
| 371 | host->req->cmd->error = error_code; |
| 372 | } |
| 373 | host->state = READY; |
| 374 | } |
| 375 | |
| 376 | if (host_status & TIFM_MMCSD_CB) |
| 377 | host->flags |= CARD_BUSY; |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 378 | if ((host_status & TIFM_MMCSD_EOFB) |
| 379 | && (host->flags & CARD_BUSY)) { |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 380 | host->written_blocks++; |
| 381 | host->flags &= ~CARD_BUSY; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | if (host->req) |
| 386 | tifm_sd_process_cmd(sock, host, host_status); |
| 387 | done: |
| 388 | dev_dbg(&sock->dev, "host_status %x, fifo_status %x\n", |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 389 | host_status, fifo_status); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 390 | spin_unlock(&sock->lock); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 393 | static void tifm_sd_prepare_data(struct tifm_sd *host, struct mmc_command *cmd) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 394 | { |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 395 | struct tifm_dev *sock = host->dev; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 396 | unsigned int dest_cnt; |
| 397 | |
| 398 | /* DMA style IO */ |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 399 | dev_dbg(&sock->dev, "setting dma for %d blocks\n", |
| 400 | cmd->data->blocks); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 401 | writel(TIFM_FIFO_INT_SETALL, |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 402 | sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR); |
David Howells | f0d1b0b | 2006-12-08 02:37:49 -0800 | [diff] [blame] | 403 | writel(ilog2(cmd->data->blksz) - 2, |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 404 | sock->addr + SOCK_FIFO_PAGE_SIZE); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 405 | writel(TIFM_FIFO_ENABLE, sock->addr + SOCK_FIFO_CONTROL); |
| 406 | writel(TIFM_FIFO_INTMASK, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET); |
| 407 | |
| 408 | dest_cnt = (cmd->data->blocks) << 8; |
| 409 | |
| 410 | writel(sg_dma_address(cmd->data->sg), sock->addr + SOCK_DMA_ADDRESS); |
| 411 | |
| 412 | writel(cmd->data->blocks - 1, sock->addr + SOCK_MMCSD_NUM_BLOCKS); |
| 413 | writel(cmd->data->blksz - 1, sock->addr + SOCK_MMCSD_BLOCK_LEN); |
| 414 | |
| 415 | if (cmd->data->flags & MMC_DATA_WRITE) { |
| 416 | writel(TIFM_MMCSD_TXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG); |
| 417 | writel(dest_cnt | TIFM_DMA_TX | TIFM_DMA_EN, |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 418 | sock->addr + SOCK_DMA_CONTROL); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 419 | } else { |
| 420 | writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG); |
| 421 | writel(dest_cnt | TIFM_DMA_EN, sock->addr + SOCK_DMA_CONTROL); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | static void tifm_sd_set_data_timeout(struct tifm_sd *host, |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 426 | struct mmc_data *data) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 427 | { |
| 428 | struct tifm_dev *sock = host->dev; |
| 429 | unsigned int data_timeout = data->timeout_clks; |
| 430 | |
| 431 | if (fixed_timeout) |
| 432 | return; |
| 433 | |
| 434 | data_timeout += data->timeout_ns / |
Alex Dubov | 83d420b | 2006-12-08 16:50:50 +1100 | [diff] [blame] | 435 | ((1000000000UL / host->clk_freq) * host->clk_div); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 436 | |
| 437 | if (data_timeout < 0xffff) { |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 438 | writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO); |
Alex Dubov | 83d420b | 2006-12-08 16:50:50 +1100 | [diff] [blame] | 439 | writel((~TIFM_MMCSD_DPE) |
| 440 | & readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG), |
| 441 | sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 442 | } else { |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 443 | data_timeout = (data_timeout >> 10) + 1; |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 444 | if (data_timeout > 0xffff) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 445 | data_timeout = 0; /* set to unlimited */ |
| 446 | writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO); |
Alex Dubov | 83d420b | 2006-12-08 16:50:50 +1100 | [diff] [blame] | 447 | writel(TIFM_MMCSD_DPE |
| 448 | | readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG), |
| 449 | sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
| 453 | static void tifm_sd_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 454 | { |
| 455 | struct tifm_sd *host = mmc_priv(mmc); |
| 456 | struct tifm_dev *sock = host->dev; |
| 457 | unsigned long flags; |
| 458 | int sg_count = 0; |
| 459 | struct mmc_data *r_data = mrq->cmd->data; |
| 460 | |
| 461 | spin_lock_irqsave(&sock->lock, flags); |
| 462 | if (host->flags & EJECT) { |
| 463 | spin_unlock_irqrestore(&sock->lock, flags); |
| 464 | goto err_out; |
| 465 | } |
| 466 | |
| 467 | if (host->req) { |
| 468 | printk(KERN_ERR DRIVER_NAME ": unfinished request detected\n"); |
| 469 | spin_unlock_irqrestore(&sock->lock, flags); |
| 470 | goto err_out; |
| 471 | } |
| 472 | |
| 473 | if (r_data) { |
| 474 | tifm_sd_set_data_timeout(host, r_data); |
| 475 | |
| 476 | sg_count = tifm_map_sg(sock, r_data->sg, r_data->sg_len, |
| 477 | mrq->cmd->flags & MMC_DATA_WRITE |
| 478 | ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE); |
| 479 | if (sg_count != 1) { |
| 480 | printk(KERN_ERR DRIVER_NAME |
| 481 | ": scatterlist map failed\n"); |
| 482 | spin_unlock_irqrestore(&sock->lock, flags); |
| 483 | goto err_out; |
| 484 | } |
| 485 | |
| 486 | host->written_blocks = 0; |
| 487 | host->flags &= ~CARD_BUSY; |
| 488 | tifm_sd_prepare_data(host, mrq->cmd); |
| 489 | } |
| 490 | |
| 491 | host->req = mrq; |
Alex Dubov | 0803dd0 | 2006-12-08 16:50:49 +1100 | [diff] [blame] | 492 | mod_timer(&host->timer, jiffies + host->timeout_jiffies); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 493 | host->state = CMD; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 494 | writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL), |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 495 | sock->addr + SOCK_CONTROL); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 496 | tifm_sd_exec(host, mrq->cmd); |
| 497 | spin_unlock_irqrestore(&sock->lock, flags); |
| 498 | return; |
| 499 | |
| 500 | err_out: |
| 501 | if (sg_count > 0) |
| 502 | tifm_unmap_sg(sock, r_data->sg, r_data->sg_len, |
| 503 | (r_data->flags & MMC_DATA_WRITE) |
| 504 | ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE); |
| 505 | |
| 506 | mrq->cmd->error = MMC_ERR_TIMEOUT; |
| 507 | mmc_request_done(mmc, mrq); |
| 508 | } |
| 509 | |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 510 | static void tifm_sd_end_cmd(unsigned long data) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 511 | { |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 512 | struct tifm_sd *host = (struct tifm_sd*)data; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 513 | struct tifm_dev *sock = host->dev; |
| 514 | struct mmc_host *mmc = tifm_get_drvdata(sock); |
| 515 | struct mmc_request *mrq; |
Al Viro | e069d79 | 2006-10-09 20:29:43 +0100 | [diff] [blame] | 516 | struct mmc_data *r_data = NULL; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 517 | unsigned long flags; |
| 518 | |
| 519 | spin_lock_irqsave(&sock->lock, flags); |
| 520 | |
Alex Dubov | 0803dd0 | 2006-12-08 16:50:49 +1100 | [diff] [blame] | 521 | del_timer(&host->timer); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 522 | mrq = host->req; |
Al Viro | e069d79 | 2006-10-09 20:29:43 +0100 | [diff] [blame] | 523 | host->req = NULL; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 524 | host->state = IDLE; |
| 525 | |
| 526 | if (!mrq) { |
| 527 | printk(KERN_ERR DRIVER_NAME ": no request to complete?\n"); |
| 528 | spin_unlock_irqrestore(&sock->lock, flags); |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | r_data = mrq->cmd->data; |
| 533 | if (r_data) { |
| 534 | if (r_data->flags & MMC_DATA_WRITE) { |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 535 | r_data->bytes_xfered = host->written_blocks |
| 536 | * r_data->blksz; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 537 | } else { |
| 538 | r_data->bytes_xfered = r_data->blocks - |
| 539 | readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS) - 1; |
| 540 | r_data->bytes_xfered *= r_data->blksz; |
| 541 | r_data->bytes_xfered += r_data->blksz - |
| 542 | readl(sock->addr + SOCK_MMCSD_BLOCK_LEN) + 1; |
| 543 | } |
| 544 | tifm_unmap_sg(sock, r_data->sg, r_data->sg_len, |
| 545 | (r_data->flags & MMC_DATA_WRITE) |
| 546 | ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE); |
| 547 | } |
| 548 | |
| 549 | writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL), |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 550 | sock->addr + SOCK_CONTROL); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 551 | |
| 552 | spin_unlock_irqrestore(&sock->lock, flags); |
| 553 | mmc_request_done(mmc, mrq); |
| 554 | } |
| 555 | |
| 556 | static void tifm_sd_request_nodma(struct mmc_host *mmc, struct mmc_request *mrq) |
| 557 | { |
| 558 | struct tifm_sd *host = mmc_priv(mmc); |
| 559 | struct tifm_dev *sock = host->dev; |
| 560 | unsigned long flags; |
| 561 | struct mmc_data *r_data = mrq->cmd->data; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 562 | |
| 563 | spin_lock_irqsave(&sock->lock, flags); |
| 564 | if (host->flags & EJECT) { |
| 565 | spin_unlock_irqrestore(&sock->lock, flags); |
| 566 | goto err_out; |
| 567 | } |
| 568 | |
| 569 | if (host->req) { |
| 570 | printk(KERN_ERR DRIVER_NAME ": unfinished request detected\n"); |
| 571 | spin_unlock_irqrestore(&sock->lock, flags); |
| 572 | goto err_out; |
| 573 | } |
| 574 | |
| 575 | if (r_data) { |
| 576 | tifm_sd_set_data_timeout(host, r_data); |
| 577 | |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 578 | host->buffer_size = mrq->cmd->data->blocks |
| 579 | * mrq->cmd->data->blksz; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 580 | |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 581 | writel(TIFM_MMCSD_BUFINT |
| 582 | | readl(sock->addr + SOCK_MMCSD_INT_ENABLE), |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 583 | sock->addr + SOCK_MMCSD_INT_ENABLE); |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 584 | writel(((TIFM_MMCSD_FIFO_SIZE - 1) << 8) |
| 585 | | (TIFM_MMCSD_FIFO_SIZE - 1), |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 586 | sock->addr + SOCK_MMCSD_BUFFER_CONFIG); |
| 587 | |
| 588 | host->written_blocks = 0; |
| 589 | host->flags &= ~CARD_BUSY; |
| 590 | host->buffer_pos = 0; |
| 591 | writel(r_data->blocks - 1, sock->addr + SOCK_MMCSD_NUM_BLOCKS); |
| 592 | writel(r_data->blksz - 1, sock->addr + SOCK_MMCSD_BLOCK_LEN); |
| 593 | } |
| 594 | |
| 595 | host->req = mrq; |
Alex Dubov | 0803dd0 | 2006-12-08 16:50:49 +1100 | [diff] [blame] | 596 | mod_timer(&host->timer, jiffies + host->timeout_jiffies); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 597 | host->state = CMD; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 598 | writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL), |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 599 | sock->addr + SOCK_CONTROL); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 600 | tifm_sd_exec(host, mrq->cmd); |
| 601 | spin_unlock_irqrestore(&sock->lock, flags); |
| 602 | return; |
| 603 | |
| 604 | err_out: |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 605 | mrq->cmd->error = MMC_ERR_TIMEOUT; |
| 606 | mmc_request_done(mmc, mrq); |
| 607 | } |
| 608 | |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 609 | static void tifm_sd_end_cmd_nodma(unsigned long data) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 610 | { |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 611 | struct tifm_sd *host = (struct tifm_sd*)data; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 612 | struct tifm_dev *sock = host->dev; |
| 613 | struct mmc_host *mmc = tifm_get_drvdata(sock); |
| 614 | struct mmc_request *mrq; |
Al Viro | e069d79 | 2006-10-09 20:29:43 +0100 | [diff] [blame] | 615 | struct mmc_data *r_data = NULL; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 616 | unsigned long flags; |
| 617 | |
| 618 | spin_lock_irqsave(&sock->lock, flags); |
| 619 | |
Alex Dubov | 0803dd0 | 2006-12-08 16:50:49 +1100 | [diff] [blame] | 620 | del_timer(&host->timer); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 621 | mrq = host->req; |
Al Viro | e069d79 | 2006-10-09 20:29:43 +0100 | [diff] [blame] | 622 | host->req = NULL; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 623 | host->state = IDLE; |
| 624 | |
| 625 | if (!mrq) { |
| 626 | printk(KERN_ERR DRIVER_NAME ": no request to complete?\n"); |
| 627 | spin_unlock_irqrestore(&sock->lock, flags); |
| 628 | return; |
| 629 | } |
| 630 | |
| 631 | r_data = mrq->cmd->data; |
| 632 | if (r_data) { |
| 633 | writel((~TIFM_MMCSD_BUFINT) & |
| 634 | readl(sock->addr + SOCK_MMCSD_INT_ENABLE), |
| 635 | sock->addr + SOCK_MMCSD_INT_ENABLE); |
| 636 | |
| 637 | if (r_data->flags & MMC_DATA_WRITE) { |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 638 | r_data->bytes_xfered = host->written_blocks |
| 639 | * r_data->blksz; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 640 | } else { |
| 641 | r_data->bytes_xfered = r_data->blocks - |
| 642 | readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS) - 1; |
| 643 | r_data->bytes_xfered *= r_data->blksz; |
| 644 | r_data->bytes_xfered += r_data->blksz - |
| 645 | readl(sock->addr + SOCK_MMCSD_BLOCK_LEN) + 1; |
| 646 | } |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 647 | host->buffer_pos = 0; |
| 648 | host->buffer_size = 0; |
| 649 | } |
| 650 | |
| 651 | writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL), |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 652 | sock->addr + SOCK_CONTROL); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 653 | |
| 654 | spin_unlock_irqrestore(&sock->lock, flags); |
| 655 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 656 | mmc_request_done(mmc, mrq); |
| 657 | } |
| 658 | |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 659 | static void tifm_sd_terminate(struct tifm_sd *host) |
| 660 | { |
| 661 | struct tifm_dev *sock = host->dev; |
| 662 | unsigned long flags; |
| 663 | |
| 664 | writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE); |
| 665 | mmiowb(); |
| 666 | spin_lock_irqsave(&sock->lock, flags); |
| 667 | host->flags |= EJECT; |
| 668 | if (host->req) { |
| 669 | writel(TIFM_FIFO_INT_SETALL, |
| 670 | sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR); |
| 671 | writel(0, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET); |
| 672 | tasklet_schedule(&host->finish_tasklet); |
| 673 | } |
| 674 | spin_unlock_irqrestore(&sock->lock, flags); |
| 675 | } |
| 676 | |
Alex Dubov | 0803dd0 | 2006-12-08 16:50:49 +1100 | [diff] [blame] | 677 | static void tifm_sd_abort(unsigned long data) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 678 | { |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 679 | struct tifm_sd *host = (struct tifm_sd*)data; |
| 680 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 681 | printk(KERN_ERR DRIVER_NAME |
Alex Dubov | 0803dd0 | 2006-12-08 16:50:49 +1100 | [diff] [blame] | 682 | ": card failed to respond for a long period of time"); |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 683 | |
| 684 | tifm_sd_terminate(host); |
| 685 | tifm_eject(host->dev); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | static void tifm_sd_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 689 | { |
| 690 | struct tifm_sd *host = mmc_priv(mmc); |
| 691 | struct tifm_dev *sock = host->dev; |
| 692 | unsigned int clk_div1, clk_div2; |
| 693 | unsigned long flags; |
| 694 | |
| 695 | spin_lock_irqsave(&sock->lock, flags); |
| 696 | |
| 697 | dev_dbg(&sock->dev, "Setting bus width %d, power %d\n", ios->bus_width, |
| 698 | ios->power_mode); |
| 699 | if (ios->bus_width == MMC_BUS_WIDTH_4) { |
| 700 | writel(TIFM_MMCSD_4BBUS | readl(sock->addr + SOCK_MMCSD_CONFIG), |
| 701 | sock->addr + SOCK_MMCSD_CONFIG); |
| 702 | } else { |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 703 | writel((~TIFM_MMCSD_4BBUS) |
| 704 | & readl(sock->addr + SOCK_MMCSD_CONFIG), |
| 705 | sock->addr + SOCK_MMCSD_CONFIG); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | if (ios->clock) { |
| 709 | clk_div1 = 20000000 / ios->clock; |
| 710 | if (!clk_div1) |
| 711 | clk_div1 = 1; |
| 712 | |
| 713 | clk_div2 = 24000000 / ios->clock; |
| 714 | if (!clk_div2) |
| 715 | clk_div2 = 1; |
| 716 | |
| 717 | if ((20000000 / clk_div1) > ios->clock) |
| 718 | clk_div1++; |
| 719 | if ((24000000 / clk_div2) > ios->clock) |
| 720 | clk_div2++; |
| 721 | if ((20000000 / clk_div1) > (24000000 / clk_div2)) { |
| 722 | host->clk_freq = 20000000; |
| 723 | host->clk_div = clk_div1; |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 724 | writel((~TIFM_CTRL_FAST_CLK) |
| 725 | & readl(sock->addr + SOCK_CONTROL), |
| 726 | sock->addr + SOCK_CONTROL); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 727 | } else { |
| 728 | host->clk_freq = 24000000; |
| 729 | host->clk_div = clk_div2; |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 730 | writel(TIFM_CTRL_FAST_CLK |
| 731 | | readl(sock->addr + SOCK_CONTROL), |
| 732 | sock->addr + SOCK_CONTROL); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 733 | } |
| 734 | } else { |
| 735 | host->clk_div = 0; |
| 736 | } |
| 737 | host->clk_div &= TIFM_MMCSD_CLKMASK; |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 738 | writel(host->clk_div |
| 739 | | ((~TIFM_MMCSD_CLKMASK) |
| 740 | & readl(sock->addr + SOCK_MMCSD_CONFIG)), |
| 741 | sock->addr + SOCK_MMCSD_CONFIG); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 742 | |
| 743 | if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) |
| 744 | host->flags |= OPENDRAIN; |
| 745 | else |
| 746 | host->flags &= ~OPENDRAIN; |
| 747 | |
| 748 | /* chip_select : maybe later */ |
| 749 | //vdd |
| 750 | //power is set before probe / after remove |
| 751 | //I believe, power_off when already marked for eject is sufficient to |
| 752 | // allow removal. |
| 753 | if ((host->flags & EJECT) && ios->power_mode == MMC_POWER_OFF) { |
| 754 | host->flags |= EJECT_DONE; |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 755 | wake_up_all(&host->notify); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | spin_unlock_irqrestore(&sock->lock, flags); |
| 759 | } |
| 760 | |
| 761 | static int tifm_sd_ro(struct mmc_host *mmc) |
| 762 | { |
| 763 | int rc; |
| 764 | struct tifm_sd *host = mmc_priv(mmc); |
| 765 | struct tifm_dev *sock = host->dev; |
| 766 | unsigned long flags; |
| 767 | |
| 768 | spin_lock_irqsave(&sock->lock, flags); |
| 769 | |
| 770 | host->flags |= (CARD_RO & readl(sock->addr + SOCK_PRESENT_STATE)); |
| 771 | rc = (host->flags & CARD_RO) ? 1 : 0; |
| 772 | |
| 773 | spin_unlock_irqrestore(&sock->lock, flags); |
| 774 | return rc; |
| 775 | } |
| 776 | |
| 777 | static struct mmc_host_ops tifm_sd_ops = { |
| 778 | .request = tifm_sd_request, |
| 779 | .set_ios = tifm_sd_ios, |
| 780 | .get_ro = tifm_sd_ro |
| 781 | }; |
| 782 | |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 783 | static int tifm_sd_initialize_host(struct tifm_sd *host) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 784 | { |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 785 | int rc; |
| 786 | unsigned int host_status = 0; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 787 | struct tifm_dev *sock = host->dev; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 788 | |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 789 | writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE); |
| 790 | mmiowb(); |
| 791 | host->clk_div = 61; |
| 792 | host->clk_freq = 20000000; |
| 793 | writel(TIFM_MMCSD_RESET, sock->addr + SOCK_MMCSD_SYSTEM_CONTROL); |
| 794 | writel(host->clk_div | TIFM_MMCSD_POWER, |
| 795 | sock->addr + SOCK_MMCSD_CONFIG); |
| 796 | |
| 797 | /* wait up to 0.51 sec for reset */ |
| 798 | for (rc = 2; rc <= 256; rc <<= 1) { |
| 799 | if (1 & readl(sock->addr + SOCK_MMCSD_SYSTEM_STATUS)) { |
| 800 | rc = 0; |
| 801 | break; |
| 802 | } |
| 803 | msleep(rc); |
| 804 | } |
| 805 | |
| 806 | if (rc) { |
| 807 | printk(KERN_ERR DRIVER_NAME |
| 808 | ": controller failed to reset\n"); |
| 809 | return -ENODEV; |
| 810 | } |
| 811 | |
| 812 | writel(0, sock->addr + SOCK_MMCSD_NUM_BLOCKS); |
| 813 | writel(host->clk_div | TIFM_MMCSD_POWER, |
| 814 | sock->addr + SOCK_MMCSD_CONFIG); |
| 815 | writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG); |
| 816 | |
| 817 | // command timeout fixed to 64 clocks for now |
| 818 | writel(64, sock->addr + SOCK_MMCSD_COMMAND_TO); |
| 819 | writel(TIFM_MMCSD_INAB, sock->addr + SOCK_MMCSD_COMMAND); |
| 820 | |
| 821 | /* INAB should take much less than reset */ |
| 822 | for (rc = 1; rc <= 16; rc <<= 1) { |
| 823 | host_status = readl(sock->addr + SOCK_MMCSD_STATUS); |
| 824 | writel(host_status, sock->addr + SOCK_MMCSD_STATUS); |
| 825 | if (!(host_status & TIFM_MMCSD_ERRMASK) |
| 826 | && (host_status & TIFM_MMCSD_EOC)) { |
| 827 | rc = 0; |
| 828 | break; |
| 829 | } |
| 830 | msleep(rc); |
| 831 | } |
| 832 | |
| 833 | if (rc) { |
| 834 | printk(KERN_ERR DRIVER_NAME |
| 835 | ": card not ready - probe failed on initialization\n"); |
| 836 | return -ENODEV; |
| 837 | } |
| 838 | |
| 839 | writel(TIFM_MMCSD_DATAMASK | TIFM_MMCSD_ERRMASK, |
| 840 | sock->addr + SOCK_MMCSD_INT_ENABLE); |
| 841 | mmiowb(); |
| 842 | |
| 843 | return 0; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | static int tifm_sd_probe(struct tifm_dev *sock) |
| 847 | { |
| 848 | struct mmc_host *mmc; |
| 849 | struct tifm_sd *host; |
| 850 | int rc = -EIO; |
| 851 | |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 852 | if (!(TIFM_SOCK_STATE_OCCUPIED |
| 853 | & readl(sock->addr + SOCK_PRESENT_STATE))) { |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 854 | printk(KERN_WARNING DRIVER_NAME ": card gone, unexpectedly\n"); |
| 855 | return rc; |
| 856 | } |
| 857 | |
| 858 | mmc = mmc_alloc_host(sizeof(struct tifm_sd), &sock->dev); |
| 859 | if (!mmc) |
| 860 | return -ENOMEM; |
| 861 | |
| 862 | host = mmc_priv(mmc); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 863 | tifm_set_drvdata(sock, mmc); |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 864 | host->dev = sock; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 865 | host->timeout_jiffies = msecs_to_jiffies(1000); |
| 866 | |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 867 | init_waitqueue_head(&host->notify); |
| 868 | tasklet_init(&host->finish_tasklet, |
| 869 | no_dma ? tifm_sd_end_cmd_nodma : tifm_sd_end_cmd, |
| 870 | (unsigned long)host); |
| 871 | setup_timer(&host->timer, tifm_sd_abort, (unsigned long)host); |
| 872 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 873 | tifm_sd_ops.request = no_dma ? tifm_sd_request_nodma : tifm_sd_request; |
| 874 | mmc->ops = &tifm_sd_ops; |
| 875 | mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 876 | mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 877 | mmc->f_min = 20000000 / 60; |
| 878 | mmc->f_max = 24000000; |
| 879 | mmc->max_hw_segs = 1; |
| 880 | mmc->max_phys_segs = 1; |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 881 | // limited by DMA counter - it's safer to stick with |
| 882 | // block counter has 11 bits though |
| 883 | mmc->max_blk_count = 256; |
| 884 | // 2k maximum hw block length |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 885 | mmc->max_blk_size = 2048; |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 886 | mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count; |
| 887 | mmc->max_seg_size = mmc->max_req_size; |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 888 | sock->signal_irq = tifm_sd_signal_irq; |
| 889 | rc = tifm_sd_initialize_host(host); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 890 | |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 891 | if (!rc) |
| 892 | rc = mmc_add_host(mmc); |
| 893 | if (rc) |
| 894 | goto out_free_mmc; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 895 | |
| 896 | return 0; |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 897 | out_free_mmc: |
| 898 | mmc_free_host(mmc); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 899 | return rc; |
| 900 | } |
| 901 | |
| 902 | static void tifm_sd_remove(struct tifm_dev *sock) |
| 903 | { |
| 904 | struct mmc_host *mmc = tifm_get_drvdata(sock); |
| 905 | struct tifm_sd *host = mmc_priv(mmc); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 906 | |
Alex Dubov | 0803dd0 | 2006-12-08 16:50:49 +1100 | [diff] [blame] | 907 | del_timer_sync(&host->timer); |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 908 | tifm_sd_terminate(host); |
| 909 | wait_event_timeout(host->notify, host->flags & EJECT_DONE, |
| 910 | host->timeout_jiffies); |
| 911 | tasklet_kill(&host->finish_tasklet); |
| 912 | mmc_remove_host(mmc); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 913 | |
| 914 | /* The meaning of the bit majority in this constant is unknown. */ |
| 915 | writel(0xfff8 & readl(sock->addr + SOCK_CONTROL), |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 916 | sock->addr + SOCK_CONTROL); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 917 | |
Al Viro | e069d79 | 2006-10-09 20:29:43 +0100 | [diff] [blame] | 918 | tifm_set_drvdata(sock, NULL); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 919 | mmc_free_host(mmc); |
| 920 | } |
| 921 | |
Alex Dubov | dba4acc | 2006-12-11 01:55:38 +1100 | [diff] [blame] | 922 | #ifdef CONFIG_PM |
| 923 | |
| 924 | static int tifm_sd_suspend(struct tifm_dev *sock, pm_message_t state) |
| 925 | { |
| 926 | struct mmc_host *mmc = tifm_get_drvdata(sock); |
| 927 | int rc; |
| 928 | |
| 929 | rc = mmc_suspend_host(mmc, state); |
| 930 | /* The meaning of the bit majority in this constant is unknown. */ |
| 931 | writel(0xfff8 & readl(sock->addr + SOCK_CONTROL), |
| 932 | sock->addr + SOCK_CONTROL); |
| 933 | return rc; |
| 934 | } |
| 935 | |
| 936 | static int tifm_sd_resume(struct tifm_dev *sock) |
| 937 | { |
| 938 | struct mmc_host *mmc = tifm_get_drvdata(sock); |
| 939 | struct tifm_sd *host = mmc_priv(mmc); |
| 940 | |
| 941 | if (sock->media_id != FM_SD |
| 942 | || tifm_sd_initialize_host(host)) { |
| 943 | tifm_eject(sock); |
| 944 | return 0; |
| 945 | } else { |
| 946 | return mmc_resume_host(mmc); |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | #else |
| 951 | |
| 952 | #define tifm_sd_suspend NULL |
| 953 | #define tifm_sd_resume NULL |
| 954 | |
| 955 | #endif /* CONFIG_PM */ |
| 956 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 957 | static tifm_media_id tifm_sd_id_tbl[] = { |
| 958 | FM_SD, 0 |
| 959 | }; |
| 960 | |
| 961 | static struct tifm_driver tifm_sd_driver = { |
| 962 | .driver = { |
| 963 | .name = DRIVER_NAME, |
| 964 | .owner = THIS_MODULE |
| 965 | }, |
| 966 | .id_table = tifm_sd_id_tbl, |
| 967 | .probe = tifm_sd_probe, |
Alex Dubov | dba4acc | 2006-12-11 01:55:38 +1100 | [diff] [blame] | 968 | .remove = tifm_sd_remove, |
| 969 | .suspend = tifm_sd_suspend, |
| 970 | .resume = tifm_sd_resume |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 971 | }; |
| 972 | |
| 973 | static int __init tifm_sd_init(void) |
| 974 | { |
| 975 | return tifm_register_driver(&tifm_sd_driver); |
| 976 | } |
| 977 | |
| 978 | static void __exit tifm_sd_exit(void) |
| 979 | { |
| 980 | tifm_unregister_driver(&tifm_sd_driver); |
| 981 | } |
| 982 | |
| 983 | MODULE_AUTHOR("Alex Dubov"); |
| 984 | MODULE_DESCRIPTION("TI FlashMedia SD driver"); |
| 985 | MODULE_LICENSE("GPL"); |
| 986 | MODULE_DEVICE_TABLE(tifm, tifm_sd_id_tbl); |
| 987 | MODULE_VERSION(DRIVER_VERSION); |
| 988 | |
| 989 | module_init(tifm_sd_init); |
| 990 | module_exit(tifm_sd_exit); |