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 | * |
Alex Dubov | 91f8d01 | 2007-04-12 17:05:26 +1000 | [diff] [blame] | 10 | * Special thanks to Brad Campbell for extensive testing of this driver. |
| 11 | * |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | |
| 15 | #include <linux/tifm.h> |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 16 | #include <linux/mmc/host.h> |
| 17 | #include <linux/highmem.h> |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 18 | #include <linux/scatterlist.h> |
Nicolas Pitre | 019a5f5 | 2007-10-11 01:06:03 -0400 | [diff] [blame] | 19 | #include <linux/log2.h> |
Al Viro | 2099c99 | 2006-10-24 11:17:06 +0100 | [diff] [blame] | 20 | #include <asm/io.h> |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 21 | |
| 22 | #define DRIVER_NAME "tifm_sd" |
Alex Dubov | 4552f0c | 2007-04-12 16:59:12 +1000 | [diff] [blame] | 23 | #define DRIVER_VERSION "0.8" |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 24 | |
| 25 | static int no_dma = 0; |
| 26 | static int fixed_timeout = 0; |
| 27 | module_param(no_dma, bool, 0644); |
| 28 | module_param(fixed_timeout, bool, 0644); |
| 29 | |
| 30 | /* Constants here are mostly from OMAP5912 datasheet */ |
| 31 | #define TIFM_MMCSD_RESET 0x0002 |
| 32 | #define TIFM_MMCSD_CLKMASK 0x03ff |
| 33 | #define TIFM_MMCSD_POWER 0x0800 |
| 34 | #define TIFM_MMCSD_4BBUS 0x8000 |
| 35 | #define TIFM_MMCSD_RXDE 0x8000 /* rx dma enable */ |
| 36 | #define TIFM_MMCSD_TXDE 0x0080 /* tx dma enable */ |
| 37 | #define TIFM_MMCSD_BUFINT 0x0c00 /* set bits: AE, AF */ |
| 38 | #define TIFM_MMCSD_DPE 0x0020 /* data timeout counted in kilocycles */ |
| 39 | #define TIFM_MMCSD_INAB 0x0080 /* abort / initialize command */ |
| 40 | #define TIFM_MMCSD_READ 0x8000 |
| 41 | |
Alex Dubov | 12c8345 | 2007-02-10 21:16:40 -0800 | [diff] [blame] | 42 | #define TIFM_MMCSD_ERRMASK 0x01e0 /* set bits: CCRC, CTO, DCRC, DTO */ |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 43 | #define TIFM_MMCSD_EOC 0x0001 /* end of command phase */ |
Alex Dubov | 91f8d01 | 2007-04-12 17:05:26 +1000 | [diff] [blame] | 44 | #define TIFM_MMCSD_CD 0x0002 /* card detect */ |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 45 | #define TIFM_MMCSD_CB 0x0004 /* card enter busy state */ |
| 46 | #define TIFM_MMCSD_BRS 0x0008 /* block received/sent */ |
| 47 | #define TIFM_MMCSD_EOFB 0x0010 /* card exit busy state */ |
| 48 | #define TIFM_MMCSD_DTO 0x0020 /* data time-out */ |
| 49 | #define TIFM_MMCSD_DCRC 0x0040 /* data crc error */ |
| 50 | #define TIFM_MMCSD_CTO 0x0080 /* command time-out */ |
| 51 | #define TIFM_MMCSD_CCRC 0x0100 /* command crc error */ |
| 52 | #define TIFM_MMCSD_AF 0x0400 /* fifo almost full */ |
| 53 | #define TIFM_MMCSD_AE 0x0800 /* fifo almost empty */ |
Alex Dubov | 91f8d01 | 2007-04-12 17:05:26 +1000 | [diff] [blame] | 54 | #define TIFM_MMCSD_OCRB 0x1000 /* OCR busy */ |
| 55 | #define TIFM_MMCSD_CIRQ 0x2000 /* card irq (cmd40/sdio) */ |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 56 | #define TIFM_MMCSD_CERR 0x4000 /* card status error */ |
| 57 | |
Alex Dubov | 0007d48 | 2007-04-12 16:59:23 +1000 | [diff] [blame] | 58 | #define TIFM_MMCSD_ODTO 0x0040 /* open drain / extended timeout */ |
| 59 | #define TIFM_MMCSD_CARD_RO 0x0200 /* card is read-only */ |
| 60 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 61 | #define TIFM_MMCSD_FIFO_SIZE 0x0020 |
| 62 | |
| 63 | #define TIFM_MMCSD_RSP_R0 0x0000 |
| 64 | #define TIFM_MMCSD_RSP_R1 0x0100 |
| 65 | #define TIFM_MMCSD_RSP_R2 0x0200 |
| 66 | #define TIFM_MMCSD_RSP_R3 0x0300 |
| 67 | #define TIFM_MMCSD_RSP_R4 0x0400 |
| 68 | #define TIFM_MMCSD_RSP_R5 0x0500 |
| 69 | #define TIFM_MMCSD_RSP_R6 0x0600 |
| 70 | |
| 71 | #define TIFM_MMCSD_RSP_BUSY 0x0800 |
| 72 | |
| 73 | #define TIFM_MMCSD_CMD_BC 0x0000 |
| 74 | #define TIFM_MMCSD_CMD_BCR 0x1000 |
| 75 | #define TIFM_MMCSD_CMD_AC 0x2000 |
| 76 | #define TIFM_MMCSD_CMD_ADTC 0x3000 |
| 77 | |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 78 | #define TIFM_MMCSD_MAX_BLOCK_SIZE 0x0800UL |
| 79 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 80 | enum { |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 81 | CMD_READY = 0x0001, |
| 82 | FIFO_READY = 0x0002, |
| 83 | BRS_READY = 0x0004, |
| 84 | SCMD_ACTIVE = 0x0008, |
| 85 | SCMD_READY = 0x0010, |
| 86 | CARD_BUSY = 0x0020, |
| 87 | DATA_CARRY = 0x0040 |
Alex Dubov | 0007d48 | 2007-04-12 16:59:23 +1000 | [diff] [blame] | 88 | }; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 89 | |
| 90 | struct tifm_sd { |
Alex Dubov | 91f8d01 | 2007-04-12 17:05:26 +1000 | [diff] [blame] | 91 | struct tifm_dev *dev; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 92 | |
Alex Dubov | 91f8d01 | 2007-04-12 17:05:26 +1000 | [diff] [blame] | 93 | unsigned short eject:1, |
| 94 | open_drain:1, |
| 95 | no_dma:1; |
| 96 | unsigned short cmd_flags; |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 97 | |
Alex Dubov | 91f8d01 | 2007-04-12 17:05:26 +1000 | [diff] [blame] | 98 | unsigned int clk_freq; |
| 99 | unsigned int clk_div; |
| 100 | unsigned long timeout_jiffies; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 101 | |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 102 | struct tasklet_struct finish_tasklet; |
Alex Dubov | 0803dd0 | 2006-12-08 16:50:49 +1100 | [diff] [blame] | 103 | struct timer_list timer; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 104 | struct mmc_request *req; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 105 | |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 106 | int sg_len; |
| 107 | int sg_pos; |
| 108 | unsigned int block_pos; |
| 109 | struct scatterlist bounce_buf; |
| 110 | unsigned char bounce_buf_data[TIFM_MMCSD_MAX_BLOCK_SIZE]; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 113 | /* for some reason, host won't respond correctly to readw/writew */ |
| 114 | static void tifm_sd_read_fifo(struct tifm_sd *host, struct page *pg, |
| 115 | unsigned int off, unsigned int cnt) |
Alex Dubov | 255ef22 | 2006-12-08 16:50:48 +1100 | [diff] [blame] | 116 | { |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 117 | struct tifm_dev *sock = host->dev; |
| 118 | unsigned char *buf; |
| 119 | unsigned int pos = 0, val; |
| 120 | |
| 121 | buf = kmap_atomic(pg, KM_BIO_DST_IRQ) + off; |
| 122 | if (host->cmd_flags & DATA_CARRY) { |
| 123 | buf[pos++] = host->bounce_buf_data[0]; |
| 124 | host->cmd_flags &= ~DATA_CARRY; |
| 125 | } |
| 126 | |
| 127 | while (pos < cnt) { |
| 128 | val = readl(sock->addr + SOCK_MMCSD_DATA); |
| 129 | buf[pos++] = val & 0xff; |
| 130 | if (pos == cnt) { |
| 131 | host->bounce_buf_data[0] = (val >> 8) & 0xff; |
| 132 | host->cmd_flags |= DATA_CARRY; |
| 133 | break; |
| 134 | } |
| 135 | buf[pos++] = (val >> 8) & 0xff; |
| 136 | } |
| 137 | kunmap_atomic(buf - off, KM_BIO_DST_IRQ); |
Alex Dubov | 255ef22 | 2006-12-08 16:50:48 +1100 | [diff] [blame] | 138 | } |
| 139 | |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 140 | static void tifm_sd_write_fifo(struct tifm_sd *host, struct page *pg, |
| 141 | unsigned int off, unsigned int cnt) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 142 | { |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 143 | struct tifm_dev *sock = host->dev; |
| 144 | unsigned char *buf; |
| 145 | unsigned int pos = 0, val; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 146 | |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 147 | buf = kmap_atomic(pg, KM_BIO_SRC_IRQ) + off; |
| 148 | if (host->cmd_flags & DATA_CARRY) { |
| 149 | val = host->bounce_buf_data[0] | ((buf[pos++] << 8) & 0xff00); |
| 150 | writel(val, sock->addr + SOCK_MMCSD_DATA); |
| 151 | host->cmd_flags &= ~DATA_CARRY; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 152 | } |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 153 | |
| 154 | while (pos < cnt) { |
| 155 | val = buf[pos++]; |
| 156 | if (pos == cnt) { |
| 157 | host->bounce_buf_data[0] = val & 0xff; |
| 158 | host->cmd_flags |= DATA_CARRY; |
| 159 | break; |
| 160 | } |
| 161 | val |= (buf[pos++] << 8) & 0xff00; |
| 162 | writel(val, sock->addr + SOCK_MMCSD_DATA); |
| 163 | } |
| 164 | kunmap_atomic(buf - off, KM_BIO_SRC_IRQ); |
| 165 | } |
| 166 | |
| 167 | static void tifm_sd_transfer_data(struct tifm_sd *host) |
| 168 | { |
| 169 | struct mmc_data *r_data = host->req->cmd->data; |
| 170 | struct scatterlist *sg = r_data->sg; |
| 171 | unsigned int off, cnt, t_size = TIFM_MMCSD_FIFO_SIZE * 2; |
| 172 | unsigned int p_off, p_cnt; |
| 173 | struct page *pg; |
| 174 | |
| 175 | if (host->sg_pos == host->sg_len) |
| 176 | return; |
| 177 | while (t_size) { |
| 178 | cnt = sg[host->sg_pos].length - host->block_pos; |
| 179 | if (!cnt) { |
| 180 | host->block_pos = 0; |
| 181 | host->sg_pos++; |
| 182 | if (host->sg_pos == host->sg_len) { |
| 183 | if ((r_data->flags & MMC_DATA_WRITE) |
| 184 | && DATA_CARRY) |
| 185 | writel(host->bounce_buf_data[0], |
| 186 | host->dev->addr |
| 187 | + SOCK_MMCSD_DATA); |
| 188 | |
| 189 | return; |
| 190 | } |
| 191 | cnt = sg[host->sg_pos].length; |
| 192 | } |
| 193 | off = sg[host->sg_pos].offset + host->block_pos; |
| 194 | |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame^] | 195 | pg = nth_page(sg_page(&sg[host->sg_pos]), off >> PAGE_SHIFT); |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 196 | p_off = offset_in_page(off); |
| 197 | p_cnt = PAGE_SIZE - p_off; |
| 198 | p_cnt = min(p_cnt, cnt); |
| 199 | p_cnt = min(p_cnt, t_size); |
| 200 | |
| 201 | if (r_data->flags & MMC_DATA_READ) |
| 202 | tifm_sd_read_fifo(host, pg, p_off, p_cnt); |
| 203 | else if (r_data->flags & MMC_DATA_WRITE) |
| 204 | tifm_sd_write_fifo(host, pg, p_off, p_cnt); |
| 205 | |
| 206 | t_size -= p_cnt; |
| 207 | host->block_pos += p_cnt; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | static void tifm_sd_copy_page(struct page *dst, unsigned int dst_off, |
| 212 | struct page *src, unsigned int src_off, |
| 213 | unsigned int count) |
| 214 | { |
| 215 | unsigned char *src_buf = kmap_atomic(src, KM_BIO_SRC_IRQ) + src_off; |
| 216 | unsigned char *dst_buf = kmap_atomic(dst, KM_BIO_DST_IRQ) + dst_off; |
| 217 | |
| 218 | memcpy(dst_buf, src_buf, count); |
| 219 | |
| 220 | kunmap_atomic(dst_buf - dst_off, KM_BIO_DST_IRQ); |
| 221 | kunmap_atomic(src_buf - src_off, KM_BIO_SRC_IRQ); |
| 222 | } |
| 223 | |
| 224 | static void tifm_sd_bounce_block(struct tifm_sd *host, struct mmc_data *r_data) |
| 225 | { |
| 226 | struct scatterlist *sg = r_data->sg; |
| 227 | unsigned int t_size = r_data->blksz; |
| 228 | unsigned int off, cnt; |
| 229 | unsigned int p_off, p_cnt; |
| 230 | struct page *pg; |
| 231 | |
| 232 | dev_dbg(&host->dev->dev, "bouncing block\n"); |
| 233 | while (t_size) { |
| 234 | cnt = sg[host->sg_pos].length - host->block_pos; |
| 235 | if (!cnt) { |
| 236 | host->block_pos = 0; |
| 237 | host->sg_pos++; |
| 238 | if (host->sg_pos == host->sg_len) |
| 239 | return; |
| 240 | cnt = sg[host->sg_pos].length; |
| 241 | } |
| 242 | off = sg[host->sg_pos].offset + host->block_pos; |
| 243 | |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame^] | 244 | pg = nth_page(sg_page(&sg[host->sg_pos]), off >> PAGE_SHIFT); |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 245 | p_off = offset_in_page(off); |
| 246 | p_cnt = PAGE_SIZE - p_off; |
| 247 | p_cnt = min(p_cnt, cnt); |
| 248 | p_cnt = min(p_cnt, t_size); |
| 249 | |
| 250 | if (r_data->flags & MMC_DATA_WRITE) |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame^] | 251 | tifm_sd_copy_page(sg_page(&host->bounce_buf), |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 252 | r_data->blksz - t_size, |
| 253 | pg, p_off, p_cnt); |
| 254 | else if (r_data->flags & MMC_DATA_READ) |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame^] | 255 | tifm_sd_copy_page(pg, p_off, sg_page(&host->bounce_buf), |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 256 | r_data->blksz - t_size, p_cnt); |
| 257 | |
| 258 | t_size -= p_cnt; |
| 259 | host->block_pos += p_cnt; |
| 260 | } |
| 261 | } |
| 262 | |
Adrian Bunk | d97956f | 2007-05-01 16:14:29 +0200 | [diff] [blame] | 263 | static int tifm_sd_set_dma_data(struct tifm_sd *host, struct mmc_data *r_data) |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 264 | { |
| 265 | struct tifm_dev *sock = host->dev; |
| 266 | unsigned int t_size = TIFM_DMA_TSIZE * r_data->blksz; |
| 267 | unsigned int dma_len, dma_blk_cnt, dma_off; |
| 268 | struct scatterlist *sg = NULL; |
| 269 | unsigned long flags; |
| 270 | |
| 271 | if (host->sg_pos == host->sg_len) |
| 272 | return 1; |
| 273 | |
| 274 | if (host->cmd_flags & DATA_CARRY) { |
| 275 | host->cmd_flags &= ~DATA_CARRY; |
| 276 | local_irq_save(flags); |
| 277 | tifm_sd_bounce_block(host, r_data); |
| 278 | local_irq_restore(flags); |
| 279 | if (host->sg_pos == host->sg_len) |
| 280 | return 1; |
| 281 | } |
| 282 | |
| 283 | dma_len = sg_dma_len(&r_data->sg[host->sg_pos]) - host->block_pos; |
| 284 | if (!dma_len) { |
| 285 | host->block_pos = 0; |
| 286 | host->sg_pos++; |
| 287 | if (host->sg_pos == host->sg_len) |
| 288 | return 1; |
| 289 | dma_len = sg_dma_len(&r_data->sg[host->sg_pos]); |
| 290 | } |
| 291 | |
| 292 | if (dma_len < t_size) { |
| 293 | dma_blk_cnt = dma_len / r_data->blksz; |
| 294 | dma_off = host->block_pos; |
| 295 | host->block_pos += dma_blk_cnt * r_data->blksz; |
| 296 | } else { |
| 297 | dma_blk_cnt = TIFM_DMA_TSIZE; |
| 298 | dma_off = host->block_pos; |
| 299 | host->block_pos += t_size; |
| 300 | } |
| 301 | |
| 302 | if (dma_blk_cnt) |
| 303 | sg = &r_data->sg[host->sg_pos]; |
| 304 | else if (dma_len) { |
| 305 | if (r_data->flags & MMC_DATA_WRITE) { |
| 306 | local_irq_save(flags); |
| 307 | tifm_sd_bounce_block(host, r_data); |
| 308 | local_irq_restore(flags); |
| 309 | } else |
| 310 | host->cmd_flags |= DATA_CARRY; |
| 311 | |
| 312 | sg = &host->bounce_buf; |
| 313 | dma_off = 0; |
| 314 | dma_blk_cnt = 1; |
| 315 | } else |
| 316 | return 1; |
| 317 | |
| 318 | dev_dbg(&sock->dev, "setting dma for %d blocks\n", dma_blk_cnt); |
| 319 | writel(sg_dma_address(sg) + dma_off, sock->addr + SOCK_DMA_ADDRESS); |
| 320 | if (r_data->flags & MMC_DATA_WRITE) |
| 321 | writel((dma_blk_cnt << 8) | TIFM_DMA_TX | TIFM_DMA_EN, |
| 322 | sock->addr + SOCK_DMA_CONTROL); |
| 323 | else |
| 324 | writel((dma_blk_cnt << 8) | TIFM_DMA_EN, |
| 325 | sock->addr + SOCK_DMA_CONTROL); |
| 326 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static unsigned int tifm_sd_op_flags(struct mmc_command *cmd) |
| 331 | { |
| 332 | unsigned int rc = 0; |
| 333 | |
| 334 | switch (mmc_resp_type(cmd)) { |
| 335 | case MMC_RSP_NONE: |
| 336 | rc |= TIFM_MMCSD_RSP_R0; |
| 337 | break; |
| 338 | case MMC_RSP_R1B: |
| 339 | rc |= TIFM_MMCSD_RSP_BUSY; // deliberate fall-through |
| 340 | case MMC_RSP_R1: |
| 341 | rc |= TIFM_MMCSD_RSP_R1; |
| 342 | break; |
| 343 | case MMC_RSP_R2: |
| 344 | rc |= TIFM_MMCSD_RSP_R2; |
| 345 | break; |
| 346 | case MMC_RSP_R3: |
| 347 | rc |= TIFM_MMCSD_RSP_R3; |
| 348 | break; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 349 | default: |
| 350 | BUG(); |
| 351 | } |
| 352 | |
| 353 | switch (mmc_cmd_type(cmd)) { |
| 354 | case MMC_CMD_BC: |
| 355 | rc |= TIFM_MMCSD_CMD_BC; |
| 356 | break; |
| 357 | case MMC_CMD_BCR: |
| 358 | rc |= TIFM_MMCSD_CMD_BCR; |
| 359 | break; |
| 360 | case MMC_CMD_AC: |
| 361 | rc |= TIFM_MMCSD_CMD_AC; |
| 362 | break; |
| 363 | case MMC_CMD_ADTC: |
| 364 | rc |= TIFM_MMCSD_CMD_ADTC; |
| 365 | break; |
| 366 | default: |
| 367 | BUG(); |
| 368 | } |
| 369 | return rc; |
| 370 | } |
| 371 | |
| 372 | static void tifm_sd_exec(struct tifm_sd *host, struct mmc_command *cmd) |
| 373 | { |
| 374 | struct tifm_dev *sock = host->dev; |
Alex Dubov | 0007d48 | 2007-04-12 16:59:23 +1000 | [diff] [blame] | 375 | unsigned int cmd_mask = tifm_sd_op_flags(cmd); |
| 376 | |
| 377 | if (host->open_drain) |
| 378 | cmd_mask |= TIFM_MMCSD_ODTO; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 379 | |
| 380 | if (cmd->data && (cmd->data->flags & MMC_DATA_READ)) |
| 381 | cmd_mask |= TIFM_MMCSD_READ; |
| 382 | |
| 383 | 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] | 384 | cmd->opcode, cmd->arg, cmd_mask); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 385 | |
| 386 | writel((cmd->arg >> 16) & 0xffff, sock->addr + SOCK_MMCSD_ARG_HIGH); |
| 387 | writel(cmd->arg & 0xffff, sock->addr + SOCK_MMCSD_ARG_LOW); |
| 388 | writel(cmd->opcode | cmd_mask, sock->addr + SOCK_MMCSD_COMMAND); |
| 389 | } |
| 390 | |
| 391 | static void tifm_sd_fetch_resp(struct mmc_command *cmd, struct tifm_dev *sock) |
| 392 | { |
| 393 | cmd->resp[0] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x1c) << 16) |
| 394 | | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x18); |
| 395 | cmd->resp[1] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x14) << 16) |
| 396 | | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x10); |
| 397 | cmd->resp[2] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x0c) << 16) |
| 398 | | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x08); |
| 399 | cmd->resp[3] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x04) << 16) |
| 400 | | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x00); |
| 401 | } |
| 402 | |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 403 | static void tifm_sd_check_status(struct tifm_sd *host) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 404 | { |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 405 | struct tifm_dev *sock = host->dev; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 406 | struct mmc_command *cmd = host->req->cmd; |
| 407 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 408 | if (cmd->error) |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 409 | goto finish_request; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 410 | |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 411 | if (!(host->cmd_flags & CMD_READY)) |
| 412 | return; |
| 413 | |
| 414 | if (cmd->data) { |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 415 | if (cmd->data->error) { |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 416 | if ((host->cmd_flags & SCMD_ACTIVE) |
| 417 | && !(host->cmd_flags & SCMD_READY)) |
| 418 | return; |
| 419 | |
| 420 | goto finish_request; |
| 421 | } |
| 422 | |
| 423 | if (!(host->cmd_flags & BRS_READY)) |
| 424 | return; |
| 425 | |
| 426 | if (!(host->no_dma || (host->cmd_flags & FIFO_READY))) |
| 427 | return; |
| 428 | |
| 429 | if (cmd->data->flags & MMC_DATA_WRITE) { |
| 430 | if (host->req->stop) { |
| 431 | if (!(host->cmd_flags & SCMD_ACTIVE)) { |
| 432 | host->cmd_flags |= SCMD_ACTIVE; |
| 433 | writel(TIFM_MMCSD_EOFB |
| 434 | | readl(sock->addr |
| 435 | + SOCK_MMCSD_INT_ENABLE), |
| 436 | sock->addr |
| 437 | + SOCK_MMCSD_INT_ENABLE); |
| 438 | tifm_sd_exec(host, host->req->stop); |
| 439 | return; |
| 440 | } else { |
| 441 | if (!(host->cmd_flags & SCMD_READY) |
| 442 | || (host->cmd_flags & CARD_BUSY)) |
| 443 | return; |
| 444 | writel((~TIFM_MMCSD_EOFB) |
| 445 | & readl(sock->addr |
| 446 | + SOCK_MMCSD_INT_ENABLE), |
| 447 | sock->addr |
| 448 | + SOCK_MMCSD_INT_ENABLE); |
| 449 | } |
| 450 | } else { |
| 451 | if (host->cmd_flags & CARD_BUSY) |
| 452 | return; |
| 453 | writel((~TIFM_MMCSD_EOFB) |
| 454 | & readl(sock->addr |
| 455 | + SOCK_MMCSD_INT_ENABLE), |
| 456 | sock->addr + SOCK_MMCSD_INT_ENABLE); |
| 457 | } |
| 458 | } else { |
| 459 | if (host->req->stop) { |
| 460 | if (!(host->cmd_flags & SCMD_ACTIVE)) { |
| 461 | host->cmd_flags |= SCMD_ACTIVE; |
| 462 | tifm_sd_exec(host, host->req->stop); |
| 463 | return; |
| 464 | } else { |
| 465 | if (!(host->cmd_flags & SCMD_READY)) |
| 466 | return; |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | finish_request: |
| 472 | tasklet_schedule(&host->finish_tasklet); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /* Called from interrupt handler */ |
Alex Dubov | 4552f0c | 2007-04-12 16:59:12 +1000 | [diff] [blame] | 476 | static void tifm_sd_data_event(struct tifm_dev *sock) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 477 | { |
| 478 | struct tifm_sd *host; |
Alex Dubov | 4552f0c | 2007-04-12 16:59:12 +1000 | [diff] [blame] | 479 | unsigned int fifo_status = 0; |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 480 | struct mmc_data *r_data = NULL; |
Alex Dubov | 4552f0c | 2007-04-12 16:59:12 +1000 | [diff] [blame] | 481 | |
| 482 | spin_lock(&sock->lock); |
| 483 | host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock)); |
Alex Dubov | 4552f0c | 2007-04-12 16:59:12 +1000 | [diff] [blame] | 484 | fifo_status = readl(sock->addr + SOCK_DMA_FIFO_STATUS); |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 485 | dev_dbg(&sock->dev, "data event: fifo_status %x, flags %x\n", |
| 486 | fifo_status, host->cmd_flags); |
| 487 | |
| 488 | if (host->req) { |
| 489 | r_data = host->req->cmd->data; |
| 490 | |
| 491 | if (r_data && (fifo_status & TIFM_FIFO_READY)) { |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 492 | if (tifm_sd_set_dma_data(host, r_data)) { |
| 493 | host->cmd_flags |= FIFO_READY; |
| 494 | tifm_sd_check_status(host); |
| 495 | } |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
Alex Dubov | 4552f0c | 2007-04-12 16:59:12 +1000 | [diff] [blame] | 499 | writel(fifo_status, sock->addr + SOCK_DMA_FIFO_STATUS); |
Alex Dubov | 4552f0c | 2007-04-12 16:59:12 +1000 | [diff] [blame] | 500 | spin_unlock(&sock->lock); |
Alex Dubov | 4552f0c | 2007-04-12 16:59:12 +1000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /* Called from interrupt handler */ |
| 504 | static void tifm_sd_card_event(struct tifm_dev *sock) |
| 505 | { |
| 506 | struct tifm_sd *host; |
| 507 | unsigned int host_status = 0; |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 508 | int cmd_error = 0; |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 509 | struct mmc_command *cmd = NULL; |
| 510 | unsigned long flags; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 511 | |
| 512 | spin_lock(&sock->lock); |
| 513 | host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock)); |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 514 | host_status = readl(sock->addr + SOCK_MMCSD_STATUS); |
| 515 | dev_dbg(&sock->dev, "host event: host_status %x, flags %x\n", |
| 516 | host_status, host->cmd_flags); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 517 | |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 518 | if (host->req) { |
| 519 | cmd = host->req->cmd; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 520 | |
| 521 | if (host_status & TIFM_MMCSD_ERRMASK) { |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 522 | writel(host_status & TIFM_MMCSD_ERRMASK, |
| 523 | sock->addr + SOCK_MMCSD_STATUS); |
| 524 | if (host_status & TIFM_MMCSD_CTO) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 525 | cmd_error = -ETIMEDOUT; |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 526 | else if (host_status & TIFM_MMCSD_CCRC) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 527 | cmd_error = -EILSEQ; |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 528 | |
| 529 | if (cmd->data) { |
| 530 | if (host_status & TIFM_MMCSD_DTO) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 531 | cmd->data->error = -ETIMEDOUT; |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 532 | else if (host_status & TIFM_MMCSD_DCRC) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 533 | cmd->data->error = -EILSEQ; |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 534 | } |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 535 | |
| 536 | writel(TIFM_FIFO_INT_SETALL, |
| 537 | sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR); |
| 538 | writel(TIFM_DMA_RESET, sock->addr + SOCK_DMA_CONTROL); |
| 539 | |
| 540 | if (host->req->stop) { |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 541 | if (host->cmd_flags & SCMD_ACTIVE) { |
| 542 | host->req->stop->error = cmd_error; |
| 543 | host->cmd_flags |= SCMD_READY; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 544 | } else { |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 545 | cmd->error = cmd_error; |
| 546 | host->cmd_flags |= SCMD_ACTIVE; |
| 547 | tifm_sd_exec(host, host->req->stop); |
| 548 | goto done; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 549 | } |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 550 | } else |
| 551 | cmd->error = cmd_error; |
| 552 | } else { |
| 553 | if (host_status & (TIFM_MMCSD_EOC | TIFM_MMCSD_CERR)) { |
| 554 | if (!(host->cmd_flags & CMD_READY)) { |
| 555 | host->cmd_flags |= CMD_READY; |
| 556 | tifm_sd_fetch_resp(cmd, sock); |
| 557 | } else if (host->cmd_flags & SCMD_ACTIVE) { |
| 558 | host->cmd_flags |= SCMD_READY; |
| 559 | tifm_sd_fetch_resp(host->req->stop, |
| 560 | sock); |
| 561 | } |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 562 | } |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 563 | if (host_status & TIFM_MMCSD_BRS) |
| 564 | host->cmd_flags |= BRS_READY; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 567 | if (host->no_dma && cmd->data) { |
| 568 | if (host_status & TIFM_MMCSD_AE) |
| 569 | writel(host_status & TIFM_MMCSD_AE, |
| 570 | sock->addr + SOCK_MMCSD_STATUS); |
| 571 | |
| 572 | if (host_status & (TIFM_MMCSD_AE | TIFM_MMCSD_AF |
| 573 | | TIFM_MMCSD_BRS)) { |
| 574 | local_irq_save(flags); |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 575 | tifm_sd_transfer_data(host); |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 576 | local_irq_restore(flags); |
| 577 | host_status &= ~TIFM_MMCSD_AE; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | if (host_status & TIFM_MMCSD_EOFB) |
Alex Dubov | 0007d48 | 2007-04-12 16:59:23 +1000 | [diff] [blame] | 582 | host->cmd_flags &= ~CARD_BUSY; |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 583 | else if (host_status & TIFM_MMCSD_CB) |
| 584 | host->cmd_flags |= CARD_BUSY; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 585 | |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 586 | tifm_sd_check_status(host); |
| 587 | } |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 588 | done: |
Alex Dubov | 72dc9d9 | 2007-04-12 17:05:23 +1000 | [diff] [blame] | 589 | writel(host_status, sock->addr + SOCK_MMCSD_STATUS); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 590 | spin_unlock(&sock->lock); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 593 | static void tifm_sd_set_data_timeout(struct tifm_sd *host, |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 594 | struct mmc_data *data) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 595 | { |
| 596 | struct tifm_dev *sock = host->dev; |
| 597 | unsigned int data_timeout = data->timeout_clks; |
| 598 | |
| 599 | if (fixed_timeout) |
| 600 | return; |
| 601 | |
| 602 | data_timeout += data->timeout_ns / |
Alex Dubov | 83d420b | 2006-12-08 16:50:50 +1100 | [diff] [blame] | 603 | ((1000000000UL / host->clk_freq) * host->clk_div); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 604 | |
| 605 | if (data_timeout < 0xffff) { |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 606 | writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO); |
Alex Dubov | 83d420b | 2006-12-08 16:50:50 +1100 | [diff] [blame] | 607 | writel((~TIFM_MMCSD_DPE) |
| 608 | & readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG), |
| 609 | sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 610 | } else { |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 611 | data_timeout = (data_timeout >> 10) + 1; |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 612 | if (data_timeout > 0xffff) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 613 | data_timeout = 0; /* set to unlimited */ |
| 614 | writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO); |
Alex Dubov | 83d420b | 2006-12-08 16:50:50 +1100 | [diff] [blame] | 615 | writel(TIFM_MMCSD_DPE |
| 616 | | readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG), |
| 617 | sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
| 621 | static void tifm_sd_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 622 | { |
| 623 | struct tifm_sd *host = mmc_priv(mmc); |
| 624 | struct tifm_dev *sock = host->dev; |
| 625 | unsigned long flags; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 626 | struct mmc_data *r_data = mrq->cmd->data; |
| 627 | |
| 628 | spin_lock_irqsave(&sock->lock, flags); |
Alex Dubov | 0007d48 | 2007-04-12 16:59:23 +1000 | [diff] [blame] | 629 | if (host->eject) { |
Pierre Ossman | 255d01a | 2007-07-24 20:38:53 +0200 | [diff] [blame] | 630 | mrq->cmd->error = -ENOMEDIUM; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 631 | goto err_out; |
| 632 | } |
| 633 | |
| 634 | if (host->req) { |
Alex Dubov | 91f8d01 | 2007-04-12 17:05:26 +1000 | [diff] [blame] | 635 | printk(KERN_ERR "%s : unfinished request detected\n", |
| 636 | sock->dev.bus_id); |
Pierre Ossman | 255d01a | 2007-07-24 20:38:53 +0200 | [diff] [blame] | 637 | mrq->cmd->error = -ETIMEDOUT; |
| 638 | goto err_out; |
| 639 | } |
| 640 | |
Nicolas Pitre | 019a5f5 | 2007-10-11 01:06:03 -0400 | [diff] [blame] | 641 | if (mrq->data && !is_power_of_2(mrq->data->blksz)) { |
Pierre Ossman | 255d01a | 2007-07-24 20:38:53 +0200 | [diff] [blame] | 642 | printk(KERN_ERR "%s: Unsupported block size (%d bytes)\n", |
| 643 | sock->dev.bus_id, mrq->data->blksz); |
| 644 | mrq->cmd->error = -EINVAL; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 645 | goto err_out; |
| 646 | } |
| 647 | |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 648 | host->cmd_flags = 0; |
| 649 | host->block_pos = 0; |
| 650 | host->sg_pos = 0; |
| 651 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 652 | if (r_data) { |
| 653 | tifm_sd_set_data_timeout(host, r_data); |
| 654 | |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 655 | if ((r_data->flags & MMC_DATA_WRITE) && !mrq->stop) |
| 656 | writel(TIFM_MMCSD_EOFB |
| 657 | | readl(sock->addr + SOCK_MMCSD_INT_ENABLE), |
| 658 | sock->addr + SOCK_MMCSD_INT_ENABLE); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 659 | |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 660 | if (host->no_dma) { |
Alex Dubov | dfef26d | 2007-04-12 16:59:24 +1000 | [diff] [blame] | 661 | writel(TIFM_MMCSD_BUFINT |
| 662 | | readl(sock->addr + SOCK_MMCSD_INT_ENABLE), |
| 663 | sock->addr + SOCK_MMCSD_INT_ENABLE); |
| 664 | writel(((TIFM_MMCSD_FIFO_SIZE - 1) << 8) |
| 665 | | (TIFM_MMCSD_FIFO_SIZE - 1), |
| 666 | sock->addr + SOCK_MMCSD_BUFFER_CONFIG); |
| 667 | |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 668 | host->sg_len = r_data->sg_len; |
Alex Dubov | dfef26d | 2007-04-12 16:59:24 +1000 | [diff] [blame] | 669 | } else { |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 670 | sg_init_one(&host->bounce_buf, host->bounce_buf_data, |
| 671 | r_data->blksz); |
| 672 | |
| 673 | if(1 != tifm_map_sg(sock, &host->bounce_buf, 1, |
| 674 | r_data->flags & MMC_DATA_WRITE |
| 675 | ? PCI_DMA_TODEVICE |
| 676 | : PCI_DMA_FROMDEVICE)) { |
| 677 | printk(KERN_ERR "%s : scatterlist map failed\n", |
| 678 | sock->dev.bus_id); |
| 679 | spin_unlock_irqrestore(&sock->lock, flags); |
| 680 | goto err_out; |
| 681 | } |
| 682 | host->sg_len = tifm_map_sg(sock, r_data->sg, |
| 683 | r_data->sg_len, |
| 684 | r_data->flags |
| 685 | & MMC_DATA_WRITE |
| 686 | ? PCI_DMA_TODEVICE |
| 687 | : PCI_DMA_FROMDEVICE); |
| 688 | if (host->sg_len < 1) { |
| 689 | printk(KERN_ERR "%s : scatterlist map failed\n", |
| 690 | sock->dev.bus_id); |
| 691 | tifm_unmap_sg(sock, &host->bounce_buf, 1, |
| 692 | r_data->flags & MMC_DATA_WRITE |
| 693 | ? PCI_DMA_TODEVICE |
| 694 | : PCI_DMA_FROMDEVICE); |
Alex Dubov | dfef26d | 2007-04-12 16:59:24 +1000 | [diff] [blame] | 695 | spin_unlock_irqrestore(&sock->lock, flags); |
| 696 | goto err_out; |
| 697 | } |
| 698 | |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 699 | writel(TIFM_FIFO_INT_SETALL, |
| 700 | sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR); |
| 701 | writel(ilog2(r_data->blksz) - 2, |
| 702 | sock->addr + SOCK_FIFO_PAGE_SIZE); |
| 703 | writel(TIFM_FIFO_ENABLE, |
| 704 | sock->addr + SOCK_FIFO_CONTROL); |
| 705 | writel(TIFM_FIFO_INTMASK, |
| 706 | sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET); |
| 707 | |
| 708 | if (r_data->flags & MMC_DATA_WRITE) |
| 709 | writel(TIFM_MMCSD_TXDE, |
| 710 | sock->addr + SOCK_MMCSD_BUFFER_CONFIG); |
| 711 | else |
| 712 | writel(TIFM_MMCSD_RXDE, |
| 713 | sock->addr + SOCK_MMCSD_BUFFER_CONFIG); |
| 714 | |
| 715 | tifm_sd_set_dma_data(host, r_data); |
Alex Dubov | dfef26d | 2007-04-12 16:59:24 +1000 | [diff] [blame] | 716 | } |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 717 | |
| 718 | writel(r_data->blocks - 1, |
| 719 | sock->addr + SOCK_MMCSD_NUM_BLOCKS); |
| 720 | writel(r_data->blksz - 1, |
| 721 | sock->addr + SOCK_MMCSD_BLOCK_LEN); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | host->req = mrq; |
Alex Dubov | 0803dd0 | 2006-12-08 16:50:49 +1100 | [diff] [blame] | 725 | mod_timer(&host->timer, jiffies + host->timeout_jiffies); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 726 | writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL), |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 727 | sock->addr + SOCK_CONTROL); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 728 | tifm_sd_exec(host, mrq->cmd); |
| 729 | spin_unlock_irqrestore(&sock->lock, flags); |
| 730 | return; |
| 731 | |
| 732 | err_out: |
Pierre Ossman | 255d01a | 2007-07-24 20:38:53 +0200 | [diff] [blame] | 733 | spin_unlock_irqrestore(&sock->lock, flags); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 734 | mmc_request_done(mmc, mrq); |
| 735 | } |
| 736 | |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 737 | static void tifm_sd_end_cmd(unsigned long data) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 738 | { |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 739 | struct tifm_sd *host = (struct tifm_sd*)data; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 740 | struct tifm_dev *sock = host->dev; |
| 741 | struct mmc_host *mmc = tifm_get_drvdata(sock); |
| 742 | struct mmc_request *mrq; |
Al Viro | e069d79 | 2006-10-09 20:29:43 +0100 | [diff] [blame] | 743 | struct mmc_data *r_data = NULL; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 744 | unsigned long flags; |
| 745 | |
| 746 | spin_lock_irqsave(&sock->lock, flags); |
| 747 | |
Alex Dubov | 0803dd0 | 2006-12-08 16:50:49 +1100 | [diff] [blame] | 748 | del_timer(&host->timer); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 749 | mrq = host->req; |
Al Viro | e069d79 | 2006-10-09 20:29:43 +0100 | [diff] [blame] | 750 | host->req = NULL; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 751 | |
| 752 | if (!mrq) { |
Alex Dubov | 91f8d01 | 2007-04-12 17:05:26 +1000 | [diff] [blame] | 753 | printk(KERN_ERR " %s : no request to complete?\n", |
| 754 | sock->dev.bus_id); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 755 | spin_unlock_irqrestore(&sock->lock, flags); |
| 756 | return; |
| 757 | } |
| 758 | |
| 759 | r_data = mrq->cmd->data; |
| 760 | if (r_data) { |
Alex Dubov | dfef26d | 2007-04-12 16:59:24 +1000 | [diff] [blame] | 761 | if (host->no_dma) { |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 762 | writel((~TIFM_MMCSD_BUFINT) |
| 763 | & readl(sock->addr + SOCK_MMCSD_INT_ENABLE), |
| 764 | sock->addr + SOCK_MMCSD_INT_ENABLE); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 765 | } else { |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 766 | tifm_unmap_sg(sock, &host->bounce_buf, 1, |
| 767 | (r_data->flags & MMC_DATA_WRITE) |
| 768 | ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE); |
Alex Dubov | dfef26d | 2007-04-12 16:59:24 +1000 | [diff] [blame] | 769 | tifm_unmap_sg(sock, r_data->sg, r_data->sg_len, |
| 770 | (r_data->flags & MMC_DATA_WRITE) |
| 771 | ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 772 | } |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 773 | |
| 774 | r_data->bytes_xfered = r_data->blocks |
| 775 | - readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS) - 1; |
| 776 | r_data->bytes_xfered *= r_data->blksz; |
| 777 | r_data->bytes_xfered += r_data->blksz |
| 778 | - readl(sock->addr + SOCK_MMCSD_BLOCK_LEN) + 1; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL), |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 782 | sock->addr + SOCK_CONTROL); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 783 | |
| 784 | spin_unlock_irqrestore(&sock->lock, flags); |
| 785 | mmc_request_done(mmc, mrq); |
| 786 | } |
| 787 | |
Alex Dubov | 0803dd0 | 2006-12-08 16:50:49 +1100 | [diff] [blame] | 788 | static void tifm_sd_abort(unsigned long data) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 789 | { |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 790 | struct tifm_sd *host = (struct tifm_sd*)data; |
| 791 | |
Alex Dubov | 91f8d01 | 2007-04-12 17:05:26 +1000 | [diff] [blame] | 792 | printk(KERN_ERR |
| 793 | "%s : card failed to respond for a long period of time " |
| 794 | "(%x, %x)\n", |
| 795 | host->dev->dev.bus_id, host->req->cmd->opcode, host->cmd_flags); |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 796 | |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 797 | tifm_eject(host->dev); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | static void tifm_sd_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 801 | { |
| 802 | struct tifm_sd *host = mmc_priv(mmc); |
| 803 | struct tifm_dev *sock = host->dev; |
| 804 | unsigned int clk_div1, clk_div2; |
| 805 | unsigned long flags; |
| 806 | |
| 807 | spin_lock_irqsave(&sock->lock, flags); |
| 808 | |
Alex Dubov | 91f8d01 | 2007-04-12 17:05:26 +1000 | [diff] [blame] | 809 | dev_dbg(&sock->dev, "ios: clock = %u, vdd = %x, bus_mode = %x, " |
| 810 | "chip_select = %x, power_mode = %x, bus_width = %x\n", |
| 811 | ios->clock, ios->vdd, ios->bus_mode, ios->chip_select, |
| 812 | ios->power_mode, ios->bus_width); |
| 813 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 814 | if (ios->bus_width == MMC_BUS_WIDTH_4) { |
| 815 | writel(TIFM_MMCSD_4BBUS | readl(sock->addr + SOCK_MMCSD_CONFIG), |
| 816 | sock->addr + SOCK_MMCSD_CONFIG); |
| 817 | } else { |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 818 | writel((~TIFM_MMCSD_4BBUS) |
| 819 | & readl(sock->addr + SOCK_MMCSD_CONFIG), |
| 820 | sock->addr + SOCK_MMCSD_CONFIG); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | if (ios->clock) { |
| 824 | clk_div1 = 20000000 / ios->clock; |
| 825 | if (!clk_div1) |
| 826 | clk_div1 = 1; |
| 827 | |
| 828 | clk_div2 = 24000000 / ios->clock; |
| 829 | if (!clk_div2) |
| 830 | clk_div2 = 1; |
| 831 | |
| 832 | if ((20000000 / clk_div1) > ios->clock) |
| 833 | clk_div1++; |
| 834 | if ((24000000 / clk_div2) > ios->clock) |
| 835 | clk_div2++; |
| 836 | if ((20000000 / clk_div1) > (24000000 / clk_div2)) { |
| 837 | host->clk_freq = 20000000; |
| 838 | host->clk_div = clk_div1; |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 839 | writel((~TIFM_CTRL_FAST_CLK) |
| 840 | & readl(sock->addr + SOCK_CONTROL), |
| 841 | sock->addr + SOCK_CONTROL); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 842 | } else { |
| 843 | host->clk_freq = 24000000; |
| 844 | host->clk_div = clk_div2; |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 845 | writel(TIFM_CTRL_FAST_CLK |
| 846 | | readl(sock->addr + SOCK_CONTROL), |
| 847 | sock->addr + SOCK_CONTROL); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 848 | } |
| 849 | } else { |
| 850 | host->clk_div = 0; |
| 851 | } |
| 852 | host->clk_div &= TIFM_MMCSD_CLKMASK; |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 853 | writel(host->clk_div |
| 854 | | ((~TIFM_MMCSD_CLKMASK) |
| 855 | & readl(sock->addr + SOCK_MMCSD_CONFIG)), |
| 856 | sock->addr + SOCK_MMCSD_CONFIG); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 857 | |
Alex Dubov | 0007d48 | 2007-04-12 16:59:23 +1000 | [diff] [blame] | 858 | host->open_drain = (ios->bus_mode == MMC_BUSMODE_OPENDRAIN); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 859 | |
| 860 | /* chip_select : maybe later */ |
| 861 | //vdd |
| 862 | //power is set before probe / after remove |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 863 | |
| 864 | spin_unlock_irqrestore(&sock->lock, flags); |
| 865 | } |
| 866 | |
| 867 | static int tifm_sd_ro(struct mmc_host *mmc) |
| 868 | { |
Alex Dubov | 0007d48 | 2007-04-12 16:59:23 +1000 | [diff] [blame] | 869 | int rc = 0; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 870 | struct tifm_sd *host = mmc_priv(mmc); |
| 871 | struct tifm_dev *sock = host->dev; |
| 872 | unsigned long flags; |
| 873 | |
| 874 | spin_lock_irqsave(&sock->lock, flags); |
Alex Dubov | 0007d48 | 2007-04-12 16:59:23 +1000 | [diff] [blame] | 875 | if (TIFM_MMCSD_CARD_RO & readl(sock->addr + SOCK_PRESENT_STATE)) |
| 876 | rc = 1; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 877 | spin_unlock_irqrestore(&sock->lock, flags); |
| 878 | return rc; |
| 879 | } |
| 880 | |
Alex Dubov | dfef26d | 2007-04-12 16:59:24 +1000 | [diff] [blame] | 881 | static const struct mmc_host_ops tifm_sd_ops = { |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 882 | .request = tifm_sd_request, |
| 883 | .set_ios = tifm_sd_ios, |
| 884 | .get_ro = tifm_sd_ro |
| 885 | }; |
| 886 | |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 887 | static int tifm_sd_initialize_host(struct tifm_sd *host) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 888 | { |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 889 | int rc; |
| 890 | unsigned int host_status = 0; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 891 | struct tifm_dev *sock = host->dev; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 892 | |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 893 | writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE); |
| 894 | mmiowb(); |
| 895 | host->clk_div = 61; |
| 896 | host->clk_freq = 20000000; |
| 897 | writel(TIFM_MMCSD_RESET, sock->addr + SOCK_MMCSD_SYSTEM_CONTROL); |
| 898 | writel(host->clk_div | TIFM_MMCSD_POWER, |
| 899 | sock->addr + SOCK_MMCSD_CONFIG); |
| 900 | |
| 901 | /* wait up to 0.51 sec for reset */ |
Alex Dubov | 5897d65 | 2007-04-12 17:05:24 +1000 | [diff] [blame] | 902 | for (rc = 32; rc <= 256; rc <<= 1) { |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 903 | if (1 & readl(sock->addr + SOCK_MMCSD_SYSTEM_STATUS)) { |
| 904 | rc = 0; |
| 905 | break; |
| 906 | } |
| 907 | msleep(rc); |
| 908 | } |
| 909 | |
| 910 | if (rc) { |
Alex Dubov | 5897d65 | 2007-04-12 17:05:24 +1000 | [diff] [blame] | 911 | printk(KERN_ERR "%s : controller failed to reset\n", |
| 912 | sock->dev.bus_id); |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 913 | return -ENODEV; |
| 914 | } |
| 915 | |
| 916 | writel(0, sock->addr + SOCK_MMCSD_NUM_BLOCKS); |
| 917 | writel(host->clk_div | TIFM_MMCSD_POWER, |
| 918 | sock->addr + SOCK_MMCSD_CONFIG); |
| 919 | writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG); |
| 920 | |
| 921 | // command timeout fixed to 64 clocks for now |
| 922 | writel(64, sock->addr + SOCK_MMCSD_COMMAND_TO); |
| 923 | writel(TIFM_MMCSD_INAB, sock->addr + SOCK_MMCSD_COMMAND); |
| 924 | |
Alex Dubov | 5897d65 | 2007-04-12 17:05:24 +1000 | [diff] [blame] | 925 | for (rc = 16; rc <= 64; rc <<= 1) { |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 926 | host_status = readl(sock->addr + SOCK_MMCSD_STATUS); |
| 927 | writel(host_status, sock->addr + SOCK_MMCSD_STATUS); |
| 928 | if (!(host_status & TIFM_MMCSD_ERRMASK) |
| 929 | && (host_status & TIFM_MMCSD_EOC)) { |
| 930 | rc = 0; |
| 931 | break; |
| 932 | } |
| 933 | msleep(rc); |
| 934 | } |
| 935 | |
| 936 | if (rc) { |
Alex Dubov | 5897d65 | 2007-04-12 17:05:24 +1000 | [diff] [blame] | 937 | printk(KERN_ERR |
| 938 | "%s : card not ready - probe failed on initialization\n", |
| 939 | sock->dev.bus_id); |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 940 | return -ENODEV; |
| 941 | } |
| 942 | |
Alex Dubov | 5897d65 | 2007-04-12 17:05:24 +1000 | [diff] [blame] | 943 | writel(TIFM_MMCSD_CERR | TIFM_MMCSD_BRS | TIFM_MMCSD_EOC |
| 944 | | TIFM_MMCSD_ERRMASK, |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 945 | sock->addr + SOCK_MMCSD_INT_ENABLE); |
| 946 | mmiowb(); |
| 947 | |
| 948 | return 0; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | static int tifm_sd_probe(struct tifm_dev *sock) |
| 952 | { |
| 953 | struct mmc_host *mmc; |
| 954 | struct tifm_sd *host; |
| 955 | int rc = -EIO; |
| 956 | |
Alex Dubov | 2e8ce5e | 2006-12-08 16:50:52 +1100 | [diff] [blame] | 957 | if (!(TIFM_SOCK_STATE_OCCUPIED |
| 958 | & readl(sock->addr + SOCK_PRESENT_STATE))) { |
Alex Dubov | 91f8d01 | 2007-04-12 17:05:26 +1000 | [diff] [blame] | 959 | printk(KERN_WARNING "%s : card gone, unexpectedly\n", |
| 960 | sock->dev.bus_id); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 961 | return rc; |
| 962 | } |
| 963 | |
| 964 | mmc = mmc_alloc_host(sizeof(struct tifm_sd), &sock->dev); |
| 965 | if (!mmc) |
| 966 | return -ENOMEM; |
| 967 | |
| 968 | host = mmc_priv(mmc); |
Alex Dubov | 0007d48 | 2007-04-12 16:59:23 +1000 | [diff] [blame] | 969 | host->no_dma = no_dma; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 970 | tifm_set_drvdata(sock, mmc); |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 971 | host->dev = sock; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 972 | host->timeout_jiffies = msecs_to_jiffies(1000); |
| 973 | |
Alex Dubov | dfef26d | 2007-04-12 16:59:24 +1000 | [diff] [blame] | 974 | tasklet_init(&host->finish_tasklet, tifm_sd_end_cmd, |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 975 | (unsigned long)host); |
| 976 | setup_timer(&host->timer, tifm_sd_abort, (unsigned long)host); |
| 977 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 978 | mmc->ops = &tifm_sd_ops; |
| 979 | mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 980 | mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 981 | mmc->f_min = 20000000 / 60; |
| 982 | mmc->f_max = 24000000; |
Alex Dubov | 13cdf48 | 2007-04-12 17:05:25 +1000 | [diff] [blame] | 983 | |
| 984 | mmc->max_blk_count = 2048; |
| 985 | mmc->max_hw_segs = mmc->max_blk_count; |
| 986 | mmc->max_blk_size = min(TIFM_MMCSD_MAX_BLOCK_SIZE, PAGE_SIZE); |
| 987 | mmc->max_seg_size = mmc->max_blk_count * mmc->max_blk_size; |
| 988 | mmc->max_req_size = mmc->max_seg_size; |
| 989 | mmc->max_phys_segs = mmc->max_hw_segs; |
| 990 | |
Alex Dubov | 4552f0c | 2007-04-12 16:59:12 +1000 | [diff] [blame] | 991 | sock->card_event = tifm_sd_card_event; |
| 992 | sock->data_event = tifm_sd_data_event; |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 993 | rc = tifm_sd_initialize_host(host); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 994 | |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 995 | if (!rc) |
| 996 | rc = mmc_add_host(mmc); |
Alex Dubov | 91f8d01 | 2007-04-12 17:05:26 +1000 | [diff] [blame] | 997 | if (!rc) |
| 998 | return 0; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 999 | |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 1000 | mmc_free_host(mmc); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 1001 | return rc; |
| 1002 | } |
| 1003 | |
| 1004 | static void tifm_sd_remove(struct tifm_dev *sock) |
| 1005 | { |
| 1006 | struct mmc_host *mmc = tifm_get_drvdata(sock); |
| 1007 | struct tifm_sd *host = mmc_priv(mmc); |
Alex Dubov | b039d4a | 2007-04-12 16:59:21 +1000 | [diff] [blame] | 1008 | unsigned long flags; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 1009 | |
Alex Dubov | b039d4a | 2007-04-12 16:59:21 +1000 | [diff] [blame] | 1010 | spin_lock_irqsave(&sock->lock, flags); |
Alex Dubov | 0007d48 | 2007-04-12 16:59:23 +1000 | [diff] [blame] | 1011 | host->eject = 1; |
Alex Dubov | 592d372 | 2007-04-12 16:59:22 +1000 | [diff] [blame] | 1012 | writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE); |
| 1013 | mmiowb(); |
| 1014 | spin_unlock_irqrestore(&sock->lock, flags); |
| 1015 | |
| 1016 | tasklet_kill(&host->finish_tasklet); |
| 1017 | |
| 1018 | spin_lock_irqsave(&sock->lock, flags); |
Alex Dubov | b039d4a | 2007-04-12 16:59:21 +1000 | [diff] [blame] | 1019 | if (host->req) { |
| 1020 | writel(TIFM_FIFO_INT_SETALL, |
| 1021 | sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR); |
| 1022 | writel(0, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1023 | host->req->cmd->error = -ENOMEDIUM; |
Alex Dubov | 592d372 | 2007-04-12 16:59:22 +1000 | [diff] [blame] | 1024 | if (host->req->stop) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1025 | host->req->stop->error = -ENOMEDIUM; |
Alex Dubov | b039d4a | 2007-04-12 16:59:21 +1000 | [diff] [blame] | 1026 | tasklet_schedule(&host->finish_tasklet); |
| 1027 | } |
| 1028 | spin_unlock_irqrestore(&sock->lock, flags); |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 1029 | mmc_remove_host(mmc); |
Alex Dubov | 592d372 | 2007-04-12 16:59:22 +1000 | [diff] [blame] | 1030 | dev_dbg(&sock->dev, "after remove\n"); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 1031 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 1032 | mmc_free_host(mmc); |
| 1033 | } |
| 1034 | |
Alex Dubov | dba4acc | 2006-12-11 01:55:38 +1100 | [diff] [blame] | 1035 | #ifdef CONFIG_PM |
| 1036 | |
| 1037 | static int tifm_sd_suspend(struct tifm_dev *sock, pm_message_t state) |
| 1038 | { |
Alex Dubov | 055b822 | 2007-05-01 20:14:55 -0700 | [diff] [blame] | 1039 | return mmc_suspend_host(tifm_get_drvdata(sock), state); |
Alex Dubov | dba4acc | 2006-12-11 01:55:38 +1100 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | static int tifm_sd_resume(struct tifm_dev *sock) |
| 1043 | { |
| 1044 | struct mmc_host *mmc = tifm_get_drvdata(sock); |
| 1045 | struct tifm_sd *host = mmc_priv(mmc); |
Alex Dubov | 5897d65 | 2007-04-12 17:05:24 +1000 | [diff] [blame] | 1046 | int rc; |
Alex Dubov | dba4acc | 2006-12-11 01:55:38 +1100 | [diff] [blame] | 1047 | |
Alex Dubov | 5897d65 | 2007-04-12 17:05:24 +1000 | [diff] [blame] | 1048 | rc = tifm_sd_initialize_host(host); |
| 1049 | dev_dbg(&sock->dev, "resume initialize %d\n", rc); |
| 1050 | |
| 1051 | if (rc) |
| 1052 | host->eject = 1; |
| 1053 | else |
| 1054 | rc = mmc_resume_host(mmc); |
| 1055 | |
| 1056 | return rc; |
Alex Dubov | dba4acc | 2006-12-11 01:55:38 +1100 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | #else |
| 1060 | |
| 1061 | #define tifm_sd_suspend NULL |
| 1062 | #define tifm_sd_resume NULL |
| 1063 | |
| 1064 | #endif /* CONFIG_PM */ |
| 1065 | |
Alex Dubov | e23f2b8 | 2007-04-12 16:59:14 +1000 | [diff] [blame] | 1066 | static struct tifm_device_id tifm_sd_id_tbl[] = { |
| 1067 | { TIFM_TYPE_SD }, { } |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 1068 | }; |
| 1069 | |
| 1070 | static struct tifm_driver tifm_sd_driver = { |
| 1071 | .driver = { |
| 1072 | .name = DRIVER_NAME, |
| 1073 | .owner = THIS_MODULE |
| 1074 | }, |
| 1075 | .id_table = tifm_sd_id_tbl, |
| 1076 | .probe = tifm_sd_probe, |
Alex Dubov | dba4acc | 2006-12-11 01:55:38 +1100 | [diff] [blame] | 1077 | .remove = tifm_sd_remove, |
| 1078 | .suspend = tifm_sd_suspend, |
| 1079 | .resume = tifm_sd_resume |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 1080 | }; |
| 1081 | |
| 1082 | static int __init tifm_sd_init(void) |
| 1083 | { |
| 1084 | return tifm_register_driver(&tifm_sd_driver); |
| 1085 | } |
| 1086 | |
| 1087 | static void __exit tifm_sd_exit(void) |
| 1088 | { |
| 1089 | tifm_unregister_driver(&tifm_sd_driver); |
| 1090 | } |
| 1091 | |
| 1092 | MODULE_AUTHOR("Alex Dubov"); |
| 1093 | MODULE_DESCRIPTION("TI FlashMedia SD driver"); |
| 1094 | MODULE_LICENSE("GPL"); |
| 1095 | MODULE_DEVICE_TABLE(tifm, tifm_sd_id_tbl); |
| 1096 | MODULE_VERSION(DRIVER_VERSION); |
| 1097 | |
| 1098 | module_init(tifm_sd_init); |
| 1099 | module_exit(tifm_sd_exit); |