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