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