blob: 54271b92ee5991ff5b11c1b10d7e5e285844391e [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Alex Dubov4020f2d2006-10-04 02:15:37 -07002/*
3 * tifm_sd.c - TI FlashMedia driver
4 *
5 * Copyright (C) 2006 Alex Dubov <oakad@yahoo.com>
6 *
Alex Dubov91f8d012007-04-12 17:05:26 +10007 * Special thanks to Brad Campbell for extensive testing of this driver.
Alex Dubov4020f2d2006-10-04 02:15:37 -07008 */
9
10
11#include <linux/tifm.h>
Alex Dubov4020f2d2006-10-04 02:15:37 -070012#include <linux/mmc/host.h>
13#include <linux/highmem.h>
Alex Dubov13cdf482007-04-12 17:05:25 +100014#include <linux/scatterlist.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040015#include <linux/module.h>
Al Viro2099c992006-10-24 11:17:06 +010016#include <asm/io.h>
Alex Dubov4020f2d2006-10-04 02:15:37 -070017
18#define DRIVER_NAME "tifm_sd"
Alex Dubov4552f0c2007-04-12 16:59:12 +100019#define DRIVER_VERSION "0.8"
Alex Dubov4020f2d2006-10-04 02:15:37 -070020
Rusty Russell90ab5ee2012-01-13 09:32:20 +103021static bool no_dma = 0;
22static bool fixed_timeout = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -070023module_param(no_dma, bool, 0644);
24module_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 Dubov12c83452007-02-10 21:16:40 -080038#define TIFM_MMCSD_ERRMASK 0x01e0 /* set bits: CCRC, CTO, DCRC, DTO */
Alex Dubov4020f2d2006-10-04 02:15:37 -070039#define TIFM_MMCSD_EOC 0x0001 /* end of command phase */
Alex Dubov91f8d012007-04-12 17:05:26 +100040#define TIFM_MMCSD_CD 0x0002 /* card detect */
Alex Dubov4020f2d2006-10-04 02:15:37 -070041#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 Dubov91f8d012007-04-12 17:05:26 +100050#define TIFM_MMCSD_OCRB 0x1000 /* OCR busy */
51#define TIFM_MMCSD_CIRQ 0x2000 /* card irq (cmd40/sdio) */
Alex Dubov4020f2d2006-10-04 02:15:37 -070052#define TIFM_MMCSD_CERR 0x4000 /* card status error */
53
Alex Dubov0007d482007-04-12 16:59:23 +100054#define TIFM_MMCSD_ODTO 0x0040 /* open drain / extended timeout */
55#define TIFM_MMCSD_CARD_RO 0x0200 /* card is read-only */
56
Alex Dubov4020f2d2006-10-04 02:15:37 -070057#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 Dubov13cdf482007-04-12 17:05:25 +100074#define TIFM_MMCSD_MAX_BLOCK_SIZE 0x0800UL
75
Alex Dubov4020f2d2006-10-04 02:15:37 -070076enum {
Alex Dubov72dc9d92007-04-12 17:05:23 +100077 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 Dubov0007d482007-04-12 16:59:23 +100084};
Alex Dubov4020f2d2006-10-04 02:15:37 -070085
86struct tifm_sd {
Alex Dubov91f8d012007-04-12 17:05:26 +100087 struct tifm_dev *dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -070088
Alex Dubov91f8d012007-04-12 17:05:26 +100089 unsigned short eject:1,
90 open_drain:1,
91 no_dma:1;
92 unsigned short cmd_flags;
Alex Dubov72dc9d92007-04-12 17:05:23 +100093
Alex Dubov91f8d012007-04-12 17:05:26 +100094 unsigned int clk_freq;
95 unsigned int clk_div;
96 unsigned long timeout_jiffies;
Alex Dubov4020f2d2006-10-04 02:15:37 -070097
Alex Dubov8e02f852006-12-08 16:50:51 +110098 struct tasklet_struct finish_tasklet;
Alex Dubov0803dd02006-12-08 16:50:49 +110099 struct timer_list timer;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700100 struct mmc_request *req;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700101
Alex Dubov13cdf482007-04-12 17:05:25 +1000102 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 Dubov4020f2d2006-10-04 02:15:37 -0700107};
108
Alex Dubov13cdf482007-04-12 17:05:25 +1000109/* for some reason, host won't respond correctly to readw/writew */
110static void tifm_sd_read_fifo(struct tifm_sd *host, struct page *pg,
111 unsigned int off, unsigned int cnt)
Alex Dubov255ef222006-12-08 16:50:48 +1100112{
Alex Dubov13cdf482007-04-12 17:05:25 +1000113 struct tifm_dev *sock = host->dev;
114 unsigned char *buf;
115 unsigned int pos = 0, val;
116
Cong Wang482fce92011-11-27 13:27:00 +0800117 buf = kmap_atomic(pg) + off;
Alex Dubov13cdf482007-04-12 17:05:25 +1000118 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 Wang482fce92011-11-27 13:27:00 +0800133 kunmap_atomic(buf - off);
Alex Dubov255ef222006-12-08 16:50:48 +1100134}
135
Alex Dubov13cdf482007-04-12 17:05:25 +1000136static void tifm_sd_write_fifo(struct tifm_sd *host, struct page *pg,
137 unsigned int off, unsigned int cnt)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700138{
Alex Dubov13cdf482007-04-12 17:05:25 +1000139 struct tifm_dev *sock = host->dev;
140 unsigned char *buf;
141 unsigned int pos = 0, val;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700142
Cong Wang482fce92011-11-27 13:27:00 +0800143 buf = kmap_atomic(pg) + off;
Alex Dubov13cdf482007-04-12 17:05:25 +1000144 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 Dubov4020f2d2006-10-04 02:15:37 -0700148 }
Alex Dubov13cdf482007-04-12 17:05:25 +1000149
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 Wang482fce92011-11-27 13:27:00 +0800160 kunmap_atomic(buf - off);
Alex Dubov13cdf482007-04-12 17:05:25 +1000161}
162
163static 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 Kluince636452008-03-15 16:00:38 +0100180 && (host->cmd_flags & DATA_CARRY))
Alex Dubov13cdf482007-04-12 17:05:25 +1000181 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 Axboe45711f12007-10-22 21:19:53 +0200191 pg = nth_page(sg_page(&sg[host->sg_pos]), off >> PAGE_SHIFT);
Alex Dubov13cdf482007-04-12 17:05:25 +1000192 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
207static 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 Wang482fce92011-11-27 13:27:00 +0800211 unsigned char *src_buf = kmap_atomic(src) + src_off;
212 unsigned char *dst_buf = kmap_atomic(dst) + dst_off;
Alex Dubov13cdf482007-04-12 17:05:25 +1000213
214 memcpy(dst_buf, src_buf, count);
215
Cong Wang482fce92011-11-27 13:27:00 +0800216 kunmap_atomic(dst_buf - dst_off);
217 kunmap_atomic(src_buf - src_off);
Alex Dubov13cdf482007-04-12 17:05:25 +1000218}
219
220static 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 Axboe45711f12007-10-22 21:19:53 +0200240 pg = nth_page(sg_page(&sg[host->sg_pos]), off >> PAGE_SHIFT);
Alex Dubov13cdf482007-04-12 17:05:25 +1000241 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 Axboe45711f12007-10-22 21:19:53 +0200247 tifm_sd_copy_page(sg_page(&host->bounce_buf),
Alex Dubov13cdf482007-04-12 17:05:25 +1000248 r_data->blksz - t_size,
249 pg, p_off, p_cnt);
250 else if (r_data->flags & MMC_DATA_READ)
Jens Axboe45711f12007-10-22 21:19:53 +0200251 tifm_sd_copy_page(pg, p_off, sg_page(&host->bounce_buf),
Alex Dubov13cdf482007-04-12 17:05:25 +1000252 r_data->blksz - t_size, p_cnt);
253
254 t_size -= p_cnt;
255 host->block_pos += p_cnt;
256 }
257}
258
Adrian Bunkd97956f2007-05-01 16:14:29 +0200259static int tifm_sd_set_dma_data(struct tifm_sd *host, struct mmc_data *r_data)
Alex Dubov13cdf482007-04-12 17:05:25 +1000260{
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 Dubov4020f2d2006-10-04 02:15:37 -0700323 return 0;
324}
325
326static 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. Silvaa5c83eb2018-10-05 11:54:57 +0200335 rc |= TIFM_MMCSD_RSP_BUSY;
336 /* fall-through */
Alex Dubov4020f2d2006-10-04 02:15:37 -0700337 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 Dubov4020f2d2006-10-04 02:15:37 -0700346 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
369static void tifm_sd_exec(struct tifm_sd *host, struct mmc_command *cmd)
370{
371 struct tifm_dev *sock = host->dev;
Alex Dubov0007d482007-04-12 16:59:23 +1000372 unsigned int cmd_mask = tifm_sd_op_flags(cmd);
373
374 if (host->open_drain)
375 cmd_mask |= TIFM_MMCSD_ODTO;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700376
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 Dubov2e8ce5e2006-12-08 16:50:52 +1100381 cmd->opcode, cmd->arg, cmd_mask);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700382
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
388static 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 Dubov72dc9d92007-04-12 17:05:23 +1000400static void tifm_sd_check_status(struct tifm_sd *host)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700401{
Alex Dubov72dc9d92007-04-12 17:05:23 +1000402 struct tifm_dev *sock = host->dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700403 struct mmc_command *cmd = host->req->cmd;
404
Pierre Ossman17b04292007-07-22 22:18:46 +0200405 if (cmd->error)
Alex Dubov72dc9d92007-04-12 17:05:23 +1000406 goto finish_request;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700407
Alex Dubov72dc9d92007-04-12 17:05:23 +1000408 if (!(host->cmd_flags & CMD_READY))
409 return;
410
411 if (cmd->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200412 if (cmd->data->error) {
Alex Dubov72dc9d92007-04-12 17:05:23 +1000413 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 }
468finish_request:
469 tasklet_schedule(&host->finish_tasklet);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700470}
471
472/* Called from interrupt handler */
Alex Dubov4552f0c2007-04-12 16:59:12 +1000473static void tifm_sd_data_event(struct tifm_dev *sock)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700474{
475 struct tifm_sd *host;
Alex Dubov4552f0c2007-04-12 16:59:12 +1000476 unsigned int fifo_status = 0;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000477 struct mmc_data *r_data = NULL;
Alex Dubov4552f0c2007-04-12 16:59:12 +1000478
479 spin_lock(&sock->lock);
480 host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock));
Alex Dubov4552f0c2007-04-12 16:59:12 +1000481 fifo_status = readl(sock->addr + SOCK_DMA_FIFO_STATUS);
Alex Dubov72dc9d92007-04-12 17:05:23 +1000482 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 Dubov13cdf482007-04-12 17:05:25 +1000489 if (tifm_sd_set_dma_data(host, r_data)) {
490 host->cmd_flags |= FIFO_READY;
491 tifm_sd_check_status(host);
492 }
Alex Dubov72dc9d92007-04-12 17:05:23 +1000493 }
494 }
495
Alex Dubov4552f0c2007-04-12 16:59:12 +1000496 writel(fifo_status, sock->addr + SOCK_DMA_FIFO_STATUS);
Alex Dubov4552f0c2007-04-12 16:59:12 +1000497 spin_unlock(&sock->lock);
Alex Dubov4552f0c2007-04-12 16:59:12 +1000498}
499
500/* Called from interrupt handler */
501static void tifm_sd_card_event(struct tifm_dev *sock)
502{
503 struct tifm_sd *host;
504 unsigned int host_status = 0;
Pierre Ossman17b04292007-07-22 22:18:46 +0200505 int cmd_error = 0;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000506 struct mmc_command *cmd = NULL;
507 unsigned long flags;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700508
509 spin_lock(&sock->lock);
510 host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock));
Alex Dubov72dc9d92007-04-12 17:05:23 +1000511 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 Dubov4020f2d2006-10-04 02:15:37 -0700514
Alex Dubov72dc9d92007-04-12 17:05:23 +1000515 if (host->req) {
516 cmd = host->req->cmd;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700517
518 if (host_status & TIFM_MMCSD_ERRMASK) {
Alex Dubov72dc9d92007-04-12 17:05:23 +1000519 writel(host_status & TIFM_MMCSD_ERRMASK,
520 sock->addr + SOCK_MMCSD_STATUS);
521 if (host_status & TIFM_MMCSD_CTO)
Pierre Ossman17b04292007-07-22 22:18:46 +0200522 cmd_error = -ETIMEDOUT;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000523 else if (host_status & TIFM_MMCSD_CCRC)
Pierre Ossman17b04292007-07-22 22:18:46 +0200524 cmd_error = -EILSEQ;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000525
526 if (cmd->data) {
527 if (host_status & TIFM_MMCSD_DTO)
Pierre Ossman17b04292007-07-22 22:18:46 +0200528 cmd->data->error = -ETIMEDOUT;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000529 else if (host_status & TIFM_MMCSD_DCRC)
Pierre Ossman17b04292007-07-22 22:18:46 +0200530 cmd->data->error = -EILSEQ;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000531 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700532
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 Dubov72dc9d92007-04-12 17:05:23 +1000538 if (host->cmd_flags & SCMD_ACTIVE) {
539 host->req->stop->error = cmd_error;
540 host->cmd_flags |= SCMD_READY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700541 } else {
Alex Dubov72dc9d92007-04-12 17:05:23 +1000542 cmd->error = cmd_error;
543 host->cmd_flags |= SCMD_ACTIVE;
544 tifm_sd_exec(host, host->req->stop);
545 goto done;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700546 }
Alex Dubov72dc9d92007-04-12 17:05:23 +1000547 } 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 Dubov4020f2d2006-10-04 02:15:37 -0700559 }
Alex Dubov72dc9d92007-04-12 17:05:23 +1000560 if (host_status & TIFM_MMCSD_BRS)
561 host->cmd_flags |= BRS_READY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700562 }
563
Alex Dubov72dc9d92007-04-12 17:05:23 +1000564 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 Dubov13cdf482007-04-12 17:05:25 +1000572 tifm_sd_transfer_data(host);
Alex Dubov72dc9d92007-04-12 17:05:23 +1000573 local_irq_restore(flags);
574 host_status &= ~TIFM_MMCSD_AE;
575 }
576 }
577
578 if (host_status & TIFM_MMCSD_EOFB)
Alex Dubov0007d482007-04-12 16:59:23 +1000579 host->cmd_flags &= ~CARD_BUSY;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000580 else if (host_status & TIFM_MMCSD_CB)
581 host->cmd_flags |= CARD_BUSY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700582
Alex Dubov72dc9d92007-04-12 17:05:23 +1000583 tifm_sd_check_status(host);
584 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700585done:
Alex Dubov72dc9d92007-04-12 17:05:23 +1000586 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700587 spin_unlock(&sock->lock);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700588}
589
Alex Dubov4020f2d2006-10-04 02:15:37 -0700590static void tifm_sd_set_data_timeout(struct tifm_sd *host,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100591 struct mmc_data *data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700592{
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 Dubov83d420b2006-12-08 16:50:50 +1100600 ((1000000000UL / host->clk_freq) * host->clk_div);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700601
602 if (data_timeout < 0xffff) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700603 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
Alex Dubov83d420b2006-12-08 16:50:50 +1100604 writel((~TIFM_MMCSD_DPE)
605 & readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
606 sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700607 } else {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700608 data_timeout = (data_timeout >> 10) + 1;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100609 if (data_timeout > 0xffff)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700610 data_timeout = 0; /* set to unlimited */
611 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
Alex Dubov83d420b2006-12-08 16:50:50 +1100612 writel(TIFM_MMCSD_DPE
613 | readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
614 sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700615 }
616}
617
618static 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 Dubov4020f2d2006-10-04 02:15:37 -0700623 struct mmc_data *r_data = mrq->cmd->data;
624
625 spin_lock_irqsave(&sock->lock, flags);
Alex Dubov0007d482007-04-12 16:59:23 +1000626 if (host->eject) {
Pierre Ossman255d01a2007-07-24 20:38:53 +0200627 mrq->cmd->error = -ENOMEDIUM;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700628 goto err_out;
629 }
630
631 if (host->req) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530632 pr_err("%s : unfinished request detected\n",
Kay Sieversd1b26862008-11-08 21:37:46 +0100633 dev_name(&sock->dev));
Pierre Ossman255d01a2007-07-24 20:38:53 +0200634 mrq->cmd->error = -ETIMEDOUT;
635 goto err_out;
636 }
637
Alex Dubov13cdf482007-04-12 17:05:25 +1000638 host->cmd_flags = 0;
639 host->block_pos = 0;
640 host->sg_pos = 0;
641
Alex Dubovb37a0502007-11-14 23:55:36 +1100642 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 Dubov4020f2d2006-10-04 02:15:37 -0700647 if (r_data) {
648 tifm_sd_set_data_timeout(host, r_data);
649
Alex Dubov13cdf482007-04-12 17:05:25 +1000650 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 Dubov4020f2d2006-10-04 02:15:37 -0700654
Alex Dubov13cdf482007-04-12 17:05:25 +1000655 if (host->no_dma) {
Alex Dubovdfef26d2007-04-12 16:59:24 +1000656 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 Dubov13cdf482007-04-12 17:05:25 +1000663 host->sg_len = r_data->sg_len;
Alex Dubovdfef26d2007-04-12 16:59:24 +1000664 } else {
Alex Dubov13cdf482007-04-12 17:05:25 +1000665 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 Sa3c76eb2011-10-11 11:44:09 +0530672 pr_err("%s : scatterlist map failed\n",
Kay Sieversd1b26862008-11-08 21:37:46 +0100673 dev_name(&sock->dev));
Alex Dubovb37a0502007-11-14 23:55:36 +1100674 mrq->cmd->error = -ENOMEM;
Alex Dubov13cdf482007-04-12 17:05:25 +1000675 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 Sa3c76eb2011-10-11 11:44:09 +0530684 pr_err("%s : scatterlist map failed\n",
Kay Sieversd1b26862008-11-08 21:37:46 +0100685 dev_name(&sock->dev));
Alex Dubov13cdf482007-04-12 17:05:25 +1000686 tifm_unmap_sg(sock, &host->bounce_buf, 1,
687 r_data->flags & MMC_DATA_WRITE
688 ? PCI_DMA_TODEVICE
689 : PCI_DMA_FROMDEVICE);
Alex Dubovb37a0502007-11-14 23:55:36 +1100690 mrq->cmd->error = -ENOMEM;
Alex Dubovdfef26d2007-04-12 16:59:24 +1000691 goto err_out;
692 }
693
Alex Dubov13cdf482007-04-12 17:05:25 +1000694 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 Dubovdfef26d2007-04-12 16:59:24 +1000711 }
Alex Dubov13cdf482007-04-12 17:05:25 +1000712
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 Dubov4020f2d2006-10-04 02:15:37 -0700717 }
718
719 host->req = mrq;
Alex Dubov0803dd02006-12-08 16:50:49 +1100720 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700721 writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100722 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700723 tifm_sd_exec(host, mrq->cmd);
724 spin_unlock_irqrestore(&sock->lock, flags);
725 return;
726
727err_out:
Pierre Ossman255d01a2007-07-24 20:38:53 +0200728 spin_unlock_irqrestore(&sock->lock, flags);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700729 mmc_request_done(mmc, mrq);
730}
731
Alex Dubov8e02f852006-12-08 16:50:51 +1100732static void tifm_sd_end_cmd(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700733{
Alex Dubov8e02f852006-12-08 16:50:51 +1100734 struct tifm_sd *host = (struct tifm_sd*)data;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700735 struct tifm_dev *sock = host->dev;
736 struct mmc_host *mmc = tifm_get_drvdata(sock);
737 struct mmc_request *mrq;
Al Viroe069d792006-10-09 20:29:43 +0100738 struct mmc_data *r_data = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700739 unsigned long flags;
740
741 spin_lock_irqsave(&sock->lock, flags);
742
Alex Dubov0803dd02006-12-08 16:50:49 +1100743 del_timer(&host->timer);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700744 mrq = host->req;
Al Viroe069d792006-10-09 20:29:43 +0100745 host->req = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700746
747 if (!mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530748 pr_err(" %s : no request to complete?\n",
Kay Sieversd1b26862008-11-08 21:37:46 +0100749 dev_name(&sock->dev));
Alex Dubov4020f2d2006-10-04 02:15:37 -0700750 spin_unlock_irqrestore(&sock->lock, flags);
751 return;
752 }
753
754 r_data = mrq->cmd->data;
755 if (r_data) {
Alex Dubovdfef26d2007-04-12 16:59:24 +1000756 if (host->no_dma) {
Alex Dubov13cdf482007-04-12 17:05:25 +1000757 writel((~TIFM_MMCSD_BUFINT)
758 & readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
759 sock->addr + SOCK_MMCSD_INT_ENABLE);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700760 } else {
Alex Dubov13cdf482007-04-12 17:05:25 +1000761 tifm_unmap_sg(sock, &host->bounce_buf, 1,
762 (r_data->flags & MMC_DATA_WRITE)
763 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
Alex Dubovdfef26d2007-04-12 16:59:24 +1000764 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 Dubov4020f2d2006-10-04 02:15:37 -0700767 }
Alex Dubov13cdf482007-04-12 17:05:25 +1000768
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 Dubov4020f2d2006-10-04 02:15:37 -0700774 }
775
776 writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100777 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700778
779 spin_unlock_irqrestore(&sock->lock, flags);
780 mmc_request_done(mmc, mrq);
781}
782
Kees Cook2ee4f622017-10-24 08:03:45 -0700783static void tifm_sd_abort(struct timer_list *t)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700784{
Kees Cook2ee4f622017-10-24 08:03:45 -0700785 struct tifm_sd *host = from_timer(host, t, timer);
Alex Dubov8e02f852006-12-08 16:50:51 +1100786
Girish K Sa3c76eb2011-10-11 11:44:09 +0530787 pr_err("%s : card failed to respond for a long period of time "
Alex Dubov91f8d012007-04-12 17:05:26 +1000788 "(%x, %x)\n",
Kay Sieversd1b26862008-11-08 21:37:46 +0100789 dev_name(&host->dev->dev), host->req->cmd->opcode, host->cmd_flags);
Alex Dubov8e02f852006-12-08 16:50:51 +1100790
Alex Dubov8e02f852006-12-08 16:50:51 +1100791 tifm_eject(host->dev);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700792}
793
794static 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 Dubov91f8d012007-04-12 17:05:26 +1000803 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 Dubov4020f2d2006-10-04 02:15:37 -0700808 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 Dubov2e8ce5e2006-12-08 16:50:52 +1100812 writel((~TIFM_MMCSD_4BBUS)
813 & readl(sock->addr + SOCK_MMCSD_CONFIG),
814 sock->addr + SOCK_MMCSD_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700815 }
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 Dubov2e8ce5e2006-12-08 16:50:52 +1100833 writel((~TIFM_CTRL_FAST_CLK)
834 & readl(sock->addr + SOCK_CONTROL),
835 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700836 } else {
837 host->clk_freq = 24000000;
838 host->clk_div = clk_div2;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100839 writel(TIFM_CTRL_FAST_CLK
840 | readl(sock->addr + SOCK_CONTROL),
841 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700842 }
843 } else {
844 host->clk_div = 0;
845 }
846 host->clk_div &= TIFM_MMCSD_CLKMASK;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100847 writel(host->clk_div
848 | ((~TIFM_MMCSD_CLKMASK)
849 & readl(sock->addr + SOCK_MMCSD_CONFIG)),
850 sock->addr + SOCK_MMCSD_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700851
Alex Dubov0007d482007-04-12 16:59:23 +1000852 host->open_drain = (ios->bus_mode == MMC_BUSMODE_OPENDRAIN);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700853
854 /* chip_select : maybe later */
855 //vdd
856 //power is set before probe / after remove
Alex Dubov4020f2d2006-10-04 02:15:37 -0700857
858 spin_unlock_irqrestore(&sock->lock, flags);
859}
860
861static int tifm_sd_ro(struct mmc_host *mmc)
862{
Alex Dubov0007d482007-04-12 16:59:23 +1000863 int rc = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700864 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 Dubov0007d482007-04-12 16:59:23 +1000869 if (TIFM_MMCSD_CARD_RO & readl(sock->addr + SOCK_PRESENT_STATE))
870 rc = 1;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700871 spin_unlock_irqrestore(&sock->lock, flags);
872 return rc;
873}
874
Alex Dubovdfef26d2007-04-12 16:59:24 +1000875static const struct mmc_host_ops tifm_sd_ops = {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700876 .request = tifm_sd_request,
877 .set_ios = tifm_sd_ios,
878 .get_ro = tifm_sd_ro
879};
880
Alex Dubov8e02f852006-12-08 16:50:51 +1100881static int tifm_sd_initialize_host(struct tifm_sd *host)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700882{
Alex Dubov8e02f852006-12-08 16:50:51 +1100883 int rc;
884 unsigned int host_status = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700885 struct tifm_dev *sock = host->dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700886
Alex Dubov8e02f852006-12-08 16:50:51 +1100887 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
Alex Dubov8e02f852006-12-08 16:50:51 +1100888 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 Dubov5897d652007-04-12 17:05:24 +1000895 for (rc = 32; rc <= 256; rc <<= 1) {
Alex Dubov8e02f852006-12-08 16:50:51 +1100896 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 Sa3c76eb2011-10-11 11:44:09 +0530904 pr_err("%s : controller failed to reset\n",
Kay Sieversd1b26862008-11-08 21:37:46 +0100905 dev_name(&sock->dev));
Alex Dubov8e02f852006-12-08 16:50:51 +1100906 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 Dubov5897d652007-04-12 17:05:24 +1000918 for (rc = 16; rc <= 64; rc <<= 1) {
Alex Dubov8e02f852006-12-08 16:50:51 +1100919 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 Sa3c76eb2011-10-11 11:44:09 +0530930 pr_err("%s : card not ready - probe failed on initialization\n",
Kay Sieversd1b26862008-11-08 21:37:46 +0100931 dev_name(&sock->dev));
Alex Dubov8e02f852006-12-08 16:50:51 +1100932 return -ENODEV;
933 }
934
Alex Dubov5897d652007-04-12 17:05:24 +1000935 writel(TIFM_MMCSD_CERR | TIFM_MMCSD_BRS | TIFM_MMCSD_EOC
936 | TIFM_MMCSD_ERRMASK,
Alex Dubov8e02f852006-12-08 16:50:51 +1100937 sock->addr + SOCK_MMCSD_INT_ENABLE);
Alex Dubov8e02f852006-12-08 16:50:51 +1100938
939 return 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700940}
941
942static 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 Dubov2e8ce5e2006-12-08 16:50:52 +1100948 if (!(TIFM_SOCK_STATE_OCCUPIED
949 & readl(sock->addr + SOCK_PRESENT_STATE))) {
Joe Perches66061102014-09-12 14:56:56 -0700950 pr_warn("%s : card gone, unexpectedly\n",
951 dev_name(&sock->dev));
Alex Dubov4020f2d2006-10-04 02:15:37 -0700952 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 Dubov4020f2d2006-10-04 02:15:37 -0700960 tifm_set_drvdata(sock, mmc);
Alex Dubov8e02f852006-12-08 16:50:51 +1100961 host->dev = sock;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700962 host->timeout_jiffies = msecs_to_jiffies(1000);
963
Alex Dubovdfef26d2007-04-12 16:59:24 +1000964 tasklet_init(&host->finish_tasklet, tifm_sd_end_cmd,
Alex Dubov8e02f852006-12-08 16:50:51 +1100965 (unsigned long)host);
Kees Cook2ee4f622017-10-24 08:03:45 -0700966 timer_setup(&host->timer, tifm_sd_abort, 0);
Alex Dubov8e02f852006-12-08 16:50:51 +1100967
Alex Dubov4020f2d2006-10-04 02:15:37 -0700968 mmc->ops = &tifm_sd_ops;
969 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Pierre Ossman23af6032008-07-06 01:10:27 +0200970 mmc->caps = MMC_CAP_4_BIT_DATA;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700971 mmc->f_min = 20000000 / 60;
972 mmc->f_max = 24000000;
Alex Dubov13cdf482007-04-12 17:05:25 +1000973
974 mmc->max_blk_count = 2048;
Martin K. Petersena36274e2010-09-10 01:33:59 -0400975 mmc->max_segs = mmc->max_blk_count;
Alex Dubov13cdf482007-04-12 17:05:25 +1000976 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 Dubov13cdf482007-04-12 17:05:25 +1000979
Alex Dubov4552f0c2007-04-12 16:59:12 +1000980 sock->card_event = tifm_sd_card_event;
981 sock->data_event = tifm_sd_data_event;
Alex Dubov8e02f852006-12-08 16:50:51 +1100982 rc = tifm_sd_initialize_host(host);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700983
Alex Dubov8e02f852006-12-08 16:50:51 +1100984 if (!rc)
985 rc = mmc_add_host(mmc);
Alex Dubov91f8d012007-04-12 17:05:26 +1000986 if (!rc)
987 return 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700988
Alex Dubov8e02f852006-12-08 16:50:51 +1100989 mmc_free_host(mmc);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700990 return rc;
991}
992
993static 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 Dubovb039d4a2007-04-12 16:59:21 +1000997 unsigned long flags;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700998
Alex Dubovb039d4a2007-04-12 16:59:21 +1000999 spin_lock_irqsave(&sock->lock, flags);
Alex Dubov0007d482007-04-12 16:59:23 +10001000 host->eject = 1;
Alex Dubov592d3722007-04-12 16:59:22 +10001001 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
Alex Dubov592d3722007-04-12 16:59:22 +10001002 spin_unlock_irqrestore(&sock->lock, flags);
1003
1004 tasklet_kill(&host->finish_tasklet);
1005
1006 spin_lock_irqsave(&sock->lock, flags);
Alex Dubovb039d4a2007-04-12 16:59:21 +10001007 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 Ossman17b04292007-07-22 22:18:46 +02001011 host->req->cmd->error = -ENOMEDIUM;
Alex Dubov592d3722007-04-12 16:59:22 +10001012 if (host->req->stop)
Pierre Ossman17b04292007-07-22 22:18:46 +02001013 host->req->stop->error = -ENOMEDIUM;
Alex Dubovb039d4a2007-04-12 16:59:21 +10001014 tasklet_schedule(&host->finish_tasklet);
1015 }
1016 spin_unlock_irqrestore(&sock->lock, flags);
Alex Dubov8e02f852006-12-08 16:50:51 +11001017 mmc_remove_host(mmc);
Alex Dubov592d3722007-04-12 16:59:22 +10001018 dev_dbg(&sock->dev, "after remove\n");
Alex Dubov4020f2d2006-10-04 02:15:37 -07001019
Alex Dubov4020f2d2006-10-04 02:15:37 -07001020 mmc_free_host(mmc);
1021}
1022
Alex Dubovdba4acc2006-12-11 01:55:38 +11001023#ifdef CONFIG_PM
1024
1025static int tifm_sd_suspend(struct tifm_dev *sock, pm_message_t state)
1026{
Ulf Hanssonccbd4832013-09-25 14:30:03 +02001027 return 0;
Alex Dubovdba4acc2006-12-11 01:55:38 +11001028}
1029
1030static 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 Dubov5897d652007-04-12 17:05:24 +10001034 int rc;
Alex Dubovdba4acc2006-12-11 01:55:38 +11001035
Alex Dubov5897d652007-04-12 17:05:24 +10001036 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 Dubov5897d652007-04-12 17:05:24 +10001041
1042 return rc;
Alex Dubovdba4acc2006-12-11 01:55:38 +11001043}
1044
1045#else
1046
1047#define tifm_sd_suspend NULL
1048#define tifm_sd_resume NULL
1049
1050#endif /* CONFIG_PM */
1051
Alex Dubove23f2b82007-04-12 16:59:14 +10001052static struct tifm_device_id tifm_sd_id_tbl[] = {
1053 { TIFM_TYPE_SD }, { }
Alex Dubov4020f2d2006-10-04 02:15:37 -07001054};
1055
1056static 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 Dubovdba4acc2006-12-11 01:55:38 +11001063 .remove = tifm_sd_remove,
1064 .suspend = tifm_sd_suspend,
1065 .resume = tifm_sd_resume
Alex Dubov4020f2d2006-10-04 02:15:37 -07001066};
1067
1068static int __init tifm_sd_init(void)
1069{
1070 return tifm_register_driver(&tifm_sd_driver);
1071}
1072
1073static void __exit tifm_sd_exit(void)
1074{
1075 tifm_unregister_driver(&tifm_sd_driver);
1076}
1077
1078MODULE_AUTHOR("Alex Dubov");
1079MODULE_DESCRIPTION("TI FlashMedia SD driver");
1080MODULE_LICENSE("GPL");
1081MODULE_DEVICE_TABLE(tifm, tifm_sd_id_tbl);
1082MODULE_VERSION(DRIVER_VERSION);
1083
1084module_init(tifm_sd_init);
1085module_exit(tifm_sd_exit);