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