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