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