blob: 8e69514e415d40e6899d6a2f2cbd168012bc8b13 [file] [log] [blame]
Alex Dubov4020f2d2006-10-04 02:15:37 -07001/*
2 * tifm_sd.c - TI FlashMedia driver
3 *
4 * Copyright (C) 2006 Alex Dubov <oakad@yahoo.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 */
11
12
13#include <linux/tifm.h>
14#include <linux/mmc/protocol.h>
15#include <linux/mmc/host.h>
16#include <linux/highmem.h>
Alex Dubov13cdf482007-04-12 17:05:25 +100017#include <linux/scatterlist.h>
Al Viro2099c992006-10-24 11:17:06 +010018#include <asm/io.h>
Alex Dubov4020f2d2006-10-04 02:15:37 -070019
20#define DRIVER_NAME "tifm_sd"
Alex Dubov4552f0c2007-04-12 16:59:12 +100021#define DRIVER_VERSION "0.8"
Alex Dubov4020f2d2006-10-04 02:15:37 -070022
23static int no_dma = 0;
24static int fixed_timeout = 0;
25module_param(no_dma, bool, 0644);
26module_param(fixed_timeout, bool, 0644);
27
28/* Constants here are mostly from OMAP5912 datasheet */
29#define TIFM_MMCSD_RESET 0x0002
30#define TIFM_MMCSD_CLKMASK 0x03ff
31#define TIFM_MMCSD_POWER 0x0800
32#define TIFM_MMCSD_4BBUS 0x8000
33#define TIFM_MMCSD_RXDE 0x8000 /* rx dma enable */
34#define TIFM_MMCSD_TXDE 0x0080 /* tx dma enable */
35#define TIFM_MMCSD_BUFINT 0x0c00 /* set bits: AE, AF */
36#define TIFM_MMCSD_DPE 0x0020 /* data timeout counted in kilocycles */
37#define TIFM_MMCSD_INAB 0x0080 /* abort / initialize command */
38#define TIFM_MMCSD_READ 0x8000
39
Alex Dubov12c83452007-02-10 21:16:40 -080040#define TIFM_MMCSD_ERRMASK 0x01e0 /* set bits: CCRC, CTO, DCRC, DTO */
Alex Dubov4020f2d2006-10-04 02:15:37 -070041#define TIFM_MMCSD_EOC 0x0001 /* end of command phase */
42#define TIFM_MMCSD_CB 0x0004 /* card enter busy state */
43#define TIFM_MMCSD_BRS 0x0008 /* block received/sent */
44#define TIFM_MMCSD_EOFB 0x0010 /* card exit busy state */
45#define TIFM_MMCSD_DTO 0x0020 /* data time-out */
46#define TIFM_MMCSD_DCRC 0x0040 /* data crc error */
47#define TIFM_MMCSD_CTO 0x0080 /* command time-out */
48#define TIFM_MMCSD_CCRC 0x0100 /* command crc error */
49#define TIFM_MMCSD_AF 0x0400 /* fifo almost full */
50#define TIFM_MMCSD_AE 0x0800 /* fifo almost empty */
51#define TIFM_MMCSD_CERR 0x4000 /* card status error */
52
Alex Dubov0007d482007-04-12 16:59:23 +100053#define TIFM_MMCSD_ODTO 0x0040 /* open drain / extended timeout */
54#define TIFM_MMCSD_CARD_RO 0x0200 /* card is read-only */
55
Alex Dubov4020f2d2006-10-04 02:15:37 -070056#define TIFM_MMCSD_FIFO_SIZE 0x0020
57
58#define TIFM_MMCSD_RSP_R0 0x0000
59#define TIFM_MMCSD_RSP_R1 0x0100
60#define TIFM_MMCSD_RSP_R2 0x0200
61#define TIFM_MMCSD_RSP_R3 0x0300
62#define TIFM_MMCSD_RSP_R4 0x0400
63#define TIFM_MMCSD_RSP_R5 0x0500
64#define TIFM_MMCSD_RSP_R6 0x0600
65
66#define TIFM_MMCSD_RSP_BUSY 0x0800
67
68#define TIFM_MMCSD_CMD_BC 0x0000
69#define TIFM_MMCSD_CMD_BCR 0x1000
70#define TIFM_MMCSD_CMD_AC 0x2000
71#define TIFM_MMCSD_CMD_ADTC 0x3000
72
Alex Dubov13cdf482007-04-12 17:05:25 +100073#define TIFM_MMCSD_MAX_BLOCK_SIZE 0x0800UL
74
Alex Dubov4020f2d2006-10-04 02:15:37 -070075enum {
Alex Dubov72dc9d92007-04-12 17:05:23 +100076 CMD_READY = 0x0001,
77 FIFO_READY = 0x0002,
78 BRS_READY = 0x0004,
79 SCMD_ACTIVE = 0x0008,
80 SCMD_READY = 0x0010,
81 CARD_BUSY = 0x0020,
82 DATA_CARRY = 0x0040
Alex Dubov0007d482007-04-12 16:59:23 +100083};
Alex Dubov4020f2d2006-10-04 02:15:37 -070084
85struct tifm_sd {
86 struct tifm_dev *dev;
87
Alex Dubov0007d482007-04-12 16:59:23 +100088 unsigned short eject:1,
89 open_drain:1,
90 no_dma:1;
91 unsigned short cmd_flags;
Alex Dubov72dc9d92007-04-12 17:05:23 +100092
Alex Dubov4020f2d2006-10-04 02:15:37 -070093 unsigned int clk_freq;
94 unsigned int clk_div;
Alex Dubov0803dd02006-12-08 16:50:49 +110095 unsigned long timeout_jiffies;
Alex Dubov4020f2d2006-10-04 02:15:37 -070096
Alex Dubov8e02f852006-12-08 16:50:51 +110097 struct tasklet_struct finish_tasklet;
Alex Dubov0803dd02006-12-08 16:50:49 +110098 struct timer_list timer;
Alex Dubov4020f2d2006-10-04 02:15:37 -070099 struct mmc_request *req;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700100
Alex Dubov13cdf482007-04-12 17:05:25 +1000101 int sg_len;
102 int sg_pos;
103 unsigned int block_pos;
104 struct scatterlist bounce_buf;
105 unsigned char bounce_buf_data[TIFM_MMCSD_MAX_BLOCK_SIZE];
Alex Dubov4020f2d2006-10-04 02:15:37 -0700106};
107
Alex Dubov13cdf482007-04-12 17:05:25 +1000108/* for some reason, host won't respond correctly to readw/writew */
109static void tifm_sd_read_fifo(struct tifm_sd *host, struct page *pg,
110 unsigned int off, unsigned int cnt)
Alex Dubov255ef222006-12-08 16:50:48 +1100111{
Alex Dubov13cdf482007-04-12 17:05:25 +1000112 struct tifm_dev *sock = host->dev;
113 unsigned char *buf;
114 unsigned int pos = 0, val;
115
116 buf = kmap_atomic(pg, KM_BIO_DST_IRQ) + off;
117 if (host->cmd_flags & DATA_CARRY) {
118 buf[pos++] = host->bounce_buf_data[0];
119 host->cmd_flags &= ~DATA_CARRY;
120 }
121
122 while (pos < cnt) {
123 val = readl(sock->addr + SOCK_MMCSD_DATA);
124 buf[pos++] = val & 0xff;
125 if (pos == cnt) {
126 host->bounce_buf_data[0] = (val >> 8) & 0xff;
127 host->cmd_flags |= DATA_CARRY;
128 break;
129 }
130 buf[pos++] = (val >> 8) & 0xff;
131 }
132 kunmap_atomic(buf - off, KM_BIO_DST_IRQ);
Alex Dubov255ef222006-12-08 16:50:48 +1100133}
134
Alex Dubov13cdf482007-04-12 17:05:25 +1000135static void tifm_sd_write_fifo(struct tifm_sd *host, struct page *pg,
136 unsigned int off, unsigned int cnt)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700137{
Alex Dubov13cdf482007-04-12 17:05:25 +1000138 struct tifm_dev *sock = host->dev;
139 unsigned char *buf;
140 unsigned int pos = 0, val;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700141
Alex Dubov13cdf482007-04-12 17:05:25 +1000142 buf = kmap_atomic(pg, KM_BIO_SRC_IRQ) + off;
143 if (host->cmd_flags & DATA_CARRY) {
144 val = host->bounce_buf_data[0] | ((buf[pos++] << 8) & 0xff00);
145 writel(val, sock->addr + SOCK_MMCSD_DATA);
146 host->cmd_flags &= ~DATA_CARRY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700147 }
Alex Dubov13cdf482007-04-12 17:05:25 +1000148
149 while (pos < cnt) {
150 val = buf[pos++];
151 if (pos == cnt) {
152 host->bounce_buf_data[0] = val & 0xff;
153 host->cmd_flags |= DATA_CARRY;
154 break;
155 }
156 val |= (buf[pos++] << 8) & 0xff00;
157 writel(val, sock->addr + SOCK_MMCSD_DATA);
158 }
159 kunmap_atomic(buf - off, KM_BIO_SRC_IRQ);
160}
161
162static void tifm_sd_transfer_data(struct tifm_sd *host)
163{
164 struct mmc_data *r_data = host->req->cmd->data;
165 struct scatterlist *sg = r_data->sg;
166 unsigned int off, cnt, t_size = TIFM_MMCSD_FIFO_SIZE * 2;
167 unsigned int p_off, p_cnt;
168 struct page *pg;
169
170 if (host->sg_pos == host->sg_len)
171 return;
172 while (t_size) {
173 cnt = sg[host->sg_pos].length - host->block_pos;
174 if (!cnt) {
175 host->block_pos = 0;
176 host->sg_pos++;
177 if (host->sg_pos == host->sg_len) {
178 if ((r_data->flags & MMC_DATA_WRITE)
179 && DATA_CARRY)
180 writel(host->bounce_buf_data[0],
181 host->dev->addr
182 + SOCK_MMCSD_DATA);
183
184 return;
185 }
186 cnt = sg[host->sg_pos].length;
187 }
188 off = sg[host->sg_pos].offset + host->block_pos;
189
190 pg = nth_page(sg[host->sg_pos].page, off >> PAGE_SHIFT);
191 p_off = offset_in_page(off);
192 p_cnt = PAGE_SIZE - p_off;
193 p_cnt = min(p_cnt, cnt);
194 p_cnt = min(p_cnt, t_size);
195
196 if (r_data->flags & MMC_DATA_READ)
197 tifm_sd_read_fifo(host, pg, p_off, p_cnt);
198 else if (r_data->flags & MMC_DATA_WRITE)
199 tifm_sd_write_fifo(host, pg, p_off, p_cnt);
200
201 t_size -= p_cnt;
202 host->block_pos += p_cnt;
203 }
204}
205
206static void tifm_sd_copy_page(struct page *dst, unsigned int dst_off,
207 struct page *src, unsigned int src_off,
208 unsigned int count)
209{
210 unsigned char *src_buf = kmap_atomic(src, KM_BIO_SRC_IRQ) + src_off;
211 unsigned char *dst_buf = kmap_atomic(dst, KM_BIO_DST_IRQ) + dst_off;
212
213 memcpy(dst_buf, src_buf, count);
214
215 kunmap_atomic(dst_buf - dst_off, KM_BIO_DST_IRQ);
216 kunmap_atomic(src_buf - src_off, KM_BIO_SRC_IRQ);
217}
218
219static void tifm_sd_bounce_block(struct tifm_sd *host, struct mmc_data *r_data)
220{
221 struct scatterlist *sg = r_data->sg;
222 unsigned int t_size = r_data->blksz;
223 unsigned int off, cnt;
224 unsigned int p_off, p_cnt;
225 struct page *pg;
226
227 dev_dbg(&host->dev->dev, "bouncing block\n");
228 while (t_size) {
229 cnt = sg[host->sg_pos].length - host->block_pos;
230 if (!cnt) {
231 host->block_pos = 0;
232 host->sg_pos++;
233 if (host->sg_pos == host->sg_len)
234 return;
235 cnt = sg[host->sg_pos].length;
236 }
237 off = sg[host->sg_pos].offset + host->block_pos;
238
239 pg = nth_page(sg[host->sg_pos].page, off >> PAGE_SHIFT);
240 p_off = offset_in_page(off);
241 p_cnt = PAGE_SIZE - p_off;
242 p_cnt = min(p_cnt, cnt);
243 p_cnt = min(p_cnt, t_size);
244
245 if (r_data->flags & MMC_DATA_WRITE)
246 tifm_sd_copy_page(host->bounce_buf.page,
247 r_data->blksz - t_size,
248 pg, p_off, p_cnt);
249 else if (r_data->flags & MMC_DATA_READ)
250 tifm_sd_copy_page(pg, p_off, host->bounce_buf.page,
251 r_data->blksz - t_size, p_cnt);
252
253 t_size -= p_cnt;
254 host->block_pos += p_cnt;
255 }
256}
257
258int tifm_sd_set_dma_data(struct tifm_sd *host, struct mmc_data *r_data)
259{
260 struct tifm_dev *sock = host->dev;
261 unsigned int t_size = TIFM_DMA_TSIZE * r_data->blksz;
262 unsigned int dma_len, dma_blk_cnt, dma_off;
263 struct scatterlist *sg = NULL;
264 unsigned long flags;
265
266 if (host->sg_pos == host->sg_len)
267 return 1;
268
269 if (host->cmd_flags & DATA_CARRY) {
270 host->cmd_flags &= ~DATA_CARRY;
271 local_irq_save(flags);
272 tifm_sd_bounce_block(host, r_data);
273 local_irq_restore(flags);
274 if (host->sg_pos == host->sg_len)
275 return 1;
276 }
277
278 dma_len = sg_dma_len(&r_data->sg[host->sg_pos]) - host->block_pos;
279 if (!dma_len) {
280 host->block_pos = 0;
281 host->sg_pos++;
282 if (host->sg_pos == host->sg_len)
283 return 1;
284 dma_len = sg_dma_len(&r_data->sg[host->sg_pos]);
285 }
286
287 if (dma_len < t_size) {
288 dma_blk_cnt = dma_len / r_data->blksz;
289 dma_off = host->block_pos;
290 host->block_pos += dma_blk_cnt * r_data->blksz;
291 } else {
292 dma_blk_cnt = TIFM_DMA_TSIZE;
293 dma_off = host->block_pos;
294 host->block_pos += t_size;
295 }
296
297 if (dma_blk_cnt)
298 sg = &r_data->sg[host->sg_pos];
299 else if (dma_len) {
300 if (r_data->flags & MMC_DATA_WRITE) {
301 local_irq_save(flags);
302 tifm_sd_bounce_block(host, r_data);
303 local_irq_restore(flags);
304 } else
305 host->cmd_flags |= DATA_CARRY;
306
307 sg = &host->bounce_buf;
308 dma_off = 0;
309 dma_blk_cnt = 1;
310 } else
311 return 1;
312
313 dev_dbg(&sock->dev, "setting dma for %d blocks\n", dma_blk_cnt);
314 writel(sg_dma_address(sg) + dma_off, sock->addr + SOCK_DMA_ADDRESS);
315 if (r_data->flags & MMC_DATA_WRITE)
316 writel((dma_blk_cnt << 8) | TIFM_DMA_TX | TIFM_DMA_EN,
317 sock->addr + SOCK_DMA_CONTROL);
318 else
319 writel((dma_blk_cnt << 8) | TIFM_DMA_EN,
320 sock->addr + SOCK_DMA_CONTROL);
321
Alex Dubov4020f2d2006-10-04 02:15:37 -0700322 return 0;
323}
324
325static unsigned int tifm_sd_op_flags(struct mmc_command *cmd)
326{
327 unsigned int rc = 0;
328
329 switch (mmc_resp_type(cmd)) {
330 case MMC_RSP_NONE:
331 rc |= TIFM_MMCSD_RSP_R0;
332 break;
333 case MMC_RSP_R1B:
334 rc |= TIFM_MMCSD_RSP_BUSY; // deliberate fall-through
335 case MMC_RSP_R1:
336 rc |= TIFM_MMCSD_RSP_R1;
337 break;
338 case MMC_RSP_R2:
339 rc |= TIFM_MMCSD_RSP_R2;
340 break;
341 case MMC_RSP_R3:
342 rc |= TIFM_MMCSD_RSP_R3;
343 break;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700344 default:
345 BUG();
346 }
347
348 switch (mmc_cmd_type(cmd)) {
349 case MMC_CMD_BC:
350 rc |= TIFM_MMCSD_CMD_BC;
351 break;
352 case MMC_CMD_BCR:
353 rc |= TIFM_MMCSD_CMD_BCR;
354 break;
355 case MMC_CMD_AC:
356 rc |= TIFM_MMCSD_CMD_AC;
357 break;
358 case MMC_CMD_ADTC:
359 rc |= TIFM_MMCSD_CMD_ADTC;
360 break;
361 default:
362 BUG();
363 }
364 return rc;
365}
366
367static void tifm_sd_exec(struct tifm_sd *host, struct mmc_command *cmd)
368{
369 struct tifm_dev *sock = host->dev;
Alex Dubov0007d482007-04-12 16:59:23 +1000370 unsigned int cmd_mask = tifm_sd_op_flags(cmd);
371
372 if (host->open_drain)
373 cmd_mask |= TIFM_MMCSD_ODTO;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700374
375 if (cmd->data && (cmd->data->flags & MMC_DATA_READ))
376 cmd_mask |= TIFM_MMCSD_READ;
377
378 dev_dbg(&sock->dev, "executing opcode 0x%x, arg: 0x%x, mask: 0x%x\n",
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100379 cmd->opcode, cmd->arg, cmd_mask);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700380
381 writel((cmd->arg >> 16) & 0xffff, sock->addr + SOCK_MMCSD_ARG_HIGH);
382 writel(cmd->arg & 0xffff, sock->addr + SOCK_MMCSD_ARG_LOW);
383 writel(cmd->opcode | cmd_mask, sock->addr + SOCK_MMCSD_COMMAND);
384}
385
386static void tifm_sd_fetch_resp(struct mmc_command *cmd, struct tifm_dev *sock)
387{
388 cmd->resp[0] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x1c) << 16)
389 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x18);
390 cmd->resp[1] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x14) << 16)
391 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x10);
392 cmd->resp[2] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x0c) << 16)
393 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x08);
394 cmd->resp[3] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x04) << 16)
395 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x00);
396}
397
Alex Dubov72dc9d92007-04-12 17:05:23 +1000398static void tifm_sd_check_status(struct tifm_sd *host)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700399{
Alex Dubov72dc9d92007-04-12 17:05:23 +1000400 struct tifm_dev *sock = host->dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700401 struct mmc_command *cmd = host->req->cmd;
402
Alex Dubov72dc9d92007-04-12 17:05:23 +1000403 if (cmd->error != MMC_ERR_NONE)
404 goto finish_request;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700405
Alex Dubov72dc9d92007-04-12 17:05:23 +1000406 if (!(host->cmd_flags & CMD_READY))
407 return;
408
409 if (cmd->data) {
410 if (cmd->data->error != MMC_ERR_NONE) {
411 if ((host->cmd_flags & SCMD_ACTIVE)
412 && !(host->cmd_flags & SCMD_READY))
413 return;
414
415 goto finish_request;
416 }
417
418 if (!(host->cmd_flags & BRS_READY))
419 return;
420
421 if (!(host->no_dma || (host->cmd_flags & FIFO_READY)))
422 return;
423
424 if (cmd->data->flags & MMC_DATA_WRITE) {
425 if (host->req->stop) {
426 if (!(host->cmd_flags & SCMD_ACTIVE)) {
427 host->cmd_flags |= SCMD_ACTIVE;
428 writel(TIFM_MMCSD_EOFB
429 | readl(sock->addr
430 + SOCK_MMCSD_INT_ENABLE),
431 sock->addr
432 + SOCK_MMCSD_INT_ENABLE);
433 tifm_sd_exec(host, host->req->stop);
434 return;
435 } else {
436 if (!(host->cmd_flags & SCMD_READY)
437 || (host->cmd_flags & CARD_BUSY))
438 return;
439 writel((~TIFM_MMCSD_EOFB)
440 & readl(sock->addr
441 + SOCK_MMCSD_INT_ENABLE),
442 sock->addr
443 + SOCK_MMCSD_INT_ENABLE);
444 }
445 } else {
446 if (host->cmd_flags & CARD_BUSY)
447 return;
448 writel((~TIFM_MMCSD_EOFB)
449 & readl(sock->addr
450 + SOCK_MMCSD_INT_ENABLE),
451 sock->addr + SOCK_MMCSD_INT_ENABLE);
452 }
453 } else {
454 if (host->req->stop) {
455 if (!(host->cmd_flags & SCMD_ACTIVE)) {
456 host->cmd_flags |= SCMD_ACTIVE;
457 tifm_sd_exec(host, host->req->stop);
458 return;
459 } else {
460 if (!(host->cmd_flags & SCMD_READY))
461 return;
462 }
463 }
464 }
465 }
466finish_request:
467 tasklet_schedule(&host->finish_tasklet);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700468}
469
470/* Called from interrupt handler */
Alex Dubov4552f0c2007-04-12 16:59:12 +1000471static void tifm_sd_data_event(struct tifm_dev *sock)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700472{
473 struct tifm_sd *host;
Alex Dubov4552f0c2007-04-12 16:59:12 +1000474 unsigned int fifo_status = 0;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000475 struct mmc_data *r_data = NULL;
Alex Dubov4552f0c2007-04-12 16:59:12 +1000476
477 spin_lock(&sock->lock);
478 host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock));
Alex Dubov4552f0c2007-04-12 16:59:12 +1000479 fifo_status = readl(sock->addr + SOCK_DMA_FIFO_STATUS);
Alex Dubov72dc9d92007-04-12 17:05:23 +1000480 dev_dbg(&sock->dev, "data event: fifo_status %x, flags %x\n",
481 fifo_status, host->cmd_flags);
482
483 if (host->req) {
484 r_data = host->req->cmd->data;
485
486 if (r_data && (fifo_status & TIFM_FIFO_READY)) {
Alex Dubov13cdf482007-04-12 17:05:25 +1000487 if (tifm_sd_set_dma_data(host, r_data)) {
488 host->cmd_flags |= FIFO_READY;
489 tifm_sd_check_status(host);
490 }
Alex Dubov72dc9d92007-04-12 17:05:23 +1000491 }
492 }
493
Alex Dubov4552f0c2007-04-12 16:59:12 +1000494 writel(fifo_status, sock->addr + SOCK_DMA_FIFO_STATUS);
Alex Dubov4552f0c2007-04-12 16:59:12 +1000495 spin_unlock(&sock->lock);
Alex Dubov4552f0c2007-04-12 16:59:12 +1000496}
497
498/* Called from interrupt handler */
499static void tifm_sd_card_event(struct tifm_dev *sock)
500{
501 struct tifm_sd *host;
502 unsigned int host_status = 0;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000503 int cmd_error = MMC_ERR_NONE;
504 struct mmc_command *cmd = NULL;
505 unsigned long flags;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700506
507 spin_lock(&sock->lock);
508 host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock));
Alex Dubov72dc9d92007-04-12 17:05:23 +1000509 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
510 dev_dbg(&sock->dev, "host event: host_status %x, flags %x\n",
511 host_status, host->cmd_flags);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700512
Alex Dubov72dc9d92007-04-12 17:05:23 +1000513 if (host->req) {
514 cmd = host->req->cmd;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700515
516 if (host_status & TIFM_MMCSD_ERRMASK) {
Alex Dubov72dc9d92007-04-12 17:05:23 +1000517 writel(host_status & TIFM_MMCSD_ERRMASK,
518 sock->addr + SOCK_MMCSD_STATUS);
519 if (host_status & TIFM_MMCSD_CTO)
520 cmd_error = MMC_ERR_TIMEOUT;
521 else if (host_status & TIFM_MMCSD_CCRC)
522 cmd_error = MMC_ERR_BADCRC;
523
524 if (cmd->data) {
525 if (host_status & TIFM_MMCSD_DTO)
526 cmd->data->error = MMC_ERR_TIMEOUT;
527 else if (host_status & TIFM_MMCSD_DCRC)
528 cmd->data->error = MMC_ERR_BADCRC;
529 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700530
531 writel(TIFM_FIFO_INT_SETALL,
532 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
533 writel(TIFM_DMA_RESET, sock->addr + SOCK_DMA_CONTROL);
534
535 if (host->req->stop) {
Alex Dubov72dc9d92007-04-12 17:05:23 +1000536 if (host->cmd_flags & SCMD_ACTIVE) {
537 host->req->stop->error = cmd_error;
538 host->cmd_flags |= SCMD_READY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700539 } else {
Alex Dubov72dc9d92007-04-12 17:05:23 +1000540 cmd->error = cmd_error;
541 host->cmd_flags |= SCMD_ACTIVE;
542 tifm_sd_exec(host, host->req->stop);
543 goto done;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700544 }
Alex Dubov72dc9d92007-04-12 17:05:23 +1000545 } else
546 cmd->error = cmd_error;
547 } else {
548 if (host_status & (TIFM_MMCSD_EOC | TIFM_MMCSD_CERR)) {
549 if (!(host->cmd_flags & CMD_READY)) {
550 host->cmd_flags |= CMD_READY;
551 tifm_sd_fetch_resp(cmd, sock);
552 } else if (host->cmd_flags & SCMD_ACTIVE) {
553 host->cmd_flags |= SCMD_READY;
554 tifm_sd_fetch_resp(host->req->stop,
555 sock);
556 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700557 }
Alex Dubov72dc9d92007-04-12 17:05:23 +1000558 if (host_status & TIFM_MMCSD_BRS)
559 host->cmd_flags |= BRS_READY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700560 }
561
Alex Dubov72dc9d92007-04-12 17:05:23 +1000562 if (host->no_dma && cmd->data) {
563 if (host_status & TIFM_MMCSD_AE)
564 writel(host_status & TIFM_MMCSD_AE,
565 sock->addr + SOCK_MMCSD_STATUS);
566
567 if (host_status & (TIFM_MMCSD_AE | TIFM_MMCSD_AF
568 | TIFM_MMCSD_BRS)) {
569 local_irq_save(flags);
Alex Dubov13cdf482007-04-12 17:05:25 +1000570 tifm_sd_transfer_data(host);
Alex Dubov72dc9d92007-04-12 17:05:23 +1000571 local_irq_restore(flags);
572 host_status &= ~TIFM_MMCSD_AE;
573 }
574 }
575
576 if (host_status & TIFM_MMCSD_EOFB)
Alex Dubov0007d482007-04-12 16:59:23 +1000577 host->cmd_flags &= ~CARD_BUSY;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000578 else if (host_status & TIFM_MMCSD_CB)
579 host->cmd_flags |= CARD_BUSY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700580
Alex Dubov72dc9d92007-04-12 17:05:23 +1000581 tifm_sd_check_status(host);
582 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700583done:
Alex Dubov72dc9d92007-04-12 17:05:23 +1000584 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700585 spin_unlock(&sock->lock);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700586}
587
Alex Dubov4020f2d2006-10-04 02:15:37 -0700588static void tifm_sd_set_data_timeout(struct tifm_sd *host,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100589 struct mmc_data *data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700590{
591 struct tifm_dev *sock = host->dev;
592 unsigned int data_timeout = data->timeout_clks;
593
594 if (fixed_timeout)
595 return;
596
597 data_timeout += data->timeout_ns /
Alex Dubov83d420b2006-12-08 16:50:50 +1100598 ((1000000000UL / host->clk_freq) * host->clk_div);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700599
600 if (data_timeout < 0xffff) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700601 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
Alex Dubov83d420b2006-12-08 16:50:50 +1100602 writel((~TIFM_MMCSD_DPE)
603 & readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
604 sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700605 } else {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700606 data_timeout = (data_timeout >> 10) + 1;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100607 if (data_timeout > 0xffff)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700608 data_timeout = 0; /* set to unlimited */
609 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
Alex Dubov83d420b2006-12-08 16:50:50 +1100610 writel(TIFM_MMCSD_DPE
611 | readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
612 sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700613 }
614}
615
616static void tifm_sd_request(struct mmc_host *mmc, struct mmc_request *mrq)
617{
618 struct tifm_sd *host = mmc_priv(mmc);
619 struct tifm_dev *sock = host->dev;
620 unsigned long flags;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700621 struct mmc_data *r_data = mrq->cmd->data;
622
623 spin_lock_irqsave(&sock->lock, flags);
Alex Dubov0007d482007-04-12 16:59:23 +1000624 if (host->eject) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700625 spin_unlock_irqrestore(&sock->lock, flags);
626 goto err_out;
627 }
628
629 if (host->req) {
630 printk(KERN_ERR DRIVER_NAME ": unfinished request detected\n");
631 spin_unlock_irqrestore(&sock->lock, flags);
632 goto err_out;
633 }
634
Alex Dubov13cdf482007-04-12 17:05:25 +1000635 host->cmd_flags = 0;
636 host->block_pos = 0;
637 host->sg_pos = 0;
638
Alex Dubov4020f2d2006-10-04 02:15:37 -0700639 if (r_data) {
640 tifm_sd_set_data_timeout(host, r_data);
641
Alex Dubov13cdf482007-04-12 17:05:25 +1000642 if ((r_data->flags & MMC_DATA_WRITE) && !mrq->stop)
643 writel(TIFM_MMCSD_EOFB
644 | readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
645 sock->addr + SOCK_MMCSD_INT_ENABLE);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700646
Alex Dubov13cdf482007-04-12 17:05:25 +1000647 if (host->no_dma) {
Alex Dubovdfef26d2007-04-12 16:59:24 +1000648 writel(TIFM_MMCSD_BUFINT
649 | readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
650 sock->addr + SOCK_MMCSD_INT_ENABLE);
651 writel(((TIFM_MMCSD_FIFO_SIZE - 1) << 8)
652 | (TIFM_MMCSD_FIFO_SIZE - 1),
653 sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
654
Alex Dubov13cdf482007-04-12 17:05:25 +1000655 host->sg_len = r_data->sg_len;
Alex Dubovdfef26d2007-04-12 16:59:24 +1000656 } else {
Alex Dubov13cdf482007-04-12 17:05:25 +1000657 sg_init_one(&host->bounce_buf, host->bounce_buf_data,
658 r_data->blksz);
659
660 if(1 != tifm_map_sg(sock, &host->bounce_buf, 1,
661 r_data->flags & MMC_DATA_WRITE
662 ? PCI_DMA_TODEVICE
663 : PCI_DMA_FROMDEVICE)) {
664 printk(KERN_ERR "%s : scatterlist map failed\n",
665 sock->dev.bus_id);
666 spin_unlock_irqrestore(&sock->lock, flags);
667 goto err_out;
668 }
669 host->sg_len = tifm_map_sg(sock, r_data->sg,
670 r_data->sg_len,
671 r_data->flags
672 & MMC_DATA_WRITE
673 ? PCI_DMA_TODEVICE
674 : PCI_DMA_FROMDEVICE);
675 if (host->sg_len < 1) {
676 printk(KERN_ERR "%s : scatterlist map failed\n",
677 sock->dev.bus_id);
678 tifm_unmap_sg(sock, &host->bounce_buf, 1,
679 r_data->flags & MMC_DATA_WRITE
680 ? PCI_DMA_TODEVICE
681 : PCI_DMA_FROMDEVICE);
Alex Dubovdfef26d2007-04-12 16:59:24 +1000682 spin_unlock_irqrestore(&sock->lock, flags);
683 goto err_out;
684 }
685
Alex Dubov13cdf482007-04-12 17:05:25 +1000686 writel(TIFM_FIFO_INT_SETALL,
687 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
688 writel(ilog2(r_data->blksz) - 2,
689 sock->addr + SOCK_FIFO_PAGE_SIZE);
690 writel(TIFM_FIFO_ENABLE,
691 sock->addr + SOCK_FIFO_CONTROL);
692 writel(TIFM_FIFO_INTMASK,
693 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
694
695 if (r_data->flags & MMC_DATA_WRITE)
696 writel(TIFM_MMCSD_TXDE,
697 sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
698 else
699 writel(TIFM_MMCSD_RXDE,
700 sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
701
702 tifm_sd_set_dma_data(host, r_data);
Alex Dubovdfef26d2007-04-12 16:59:24 +1000703 }
Alex Dubov13cdf482007-04-12 17:05:25 +1000704
705 writel(r_data->blocks - 1,
706 sock->addr + SOCK_MMCSD_NUM_BLOCKS);
707 writel(r_data->blksz - 1,
708 sock->addr + SOCK_MMCSD_BLOCK_LEN);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700709 }
710
711 host->req = mrq;
Alex Dubov0803dd02006-12-08 16:50:49 +1100712 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700713 writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100714 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700715 tifm_sd_exec(host, mrq->cmd);
716 spin_unlock_irqrestore(&sock->lock, flags);
717 return;
718
719err_out:
Alex Dubov4020f2d2006-10-04 02:15:37 -0700720 mrq->cmd->error = MMC_ERR_TIMEOUT;
721 mmc_request_done(mmc, mrq);
722}
723
Alex Dubov8e02f852006-12-08 16:50:51 +1100724static void tifm_sd_end_cmd(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700725{
Alex Dubov8e02f852006-12-08 16:50:51 +1100726 struct tifm_sd *host = (struct tifm_sd*)data;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700727 struct tifm_dev *sock = host->dev;
728 struct mmc_host *mmc = tifm_get_drvdata(sock);
729 struct mmc_request *mrq;
Al Viroe069d792006-10-09 20:29:43 +0100730 struct mmc_data *r_data = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700731 unsigned long flags;
732
733 spin_lock_irqsave(&sock->lock, flags);
734
Alex Dubov0803dd02006-12-08 16:50:49 +1100735 del_timer(&host->timer);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700736 mrq = host->req;
Al Viroe069d792006-10-09 20:29:43 +0100737 host->req = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700738
739 if (!mrq) {
740 printk(KERN_ERR DRIVER_NAME ": no request to complete?\n");
741 spin_unlock_irqrestore(&sock->lock, flags);
742 return;
743 }
744
745 r_data = mrq->cmd->data;
746 if (r_data) {
Alex Dubovdfef26d2007-04-12 16:59:24 +1000747 if (host->no_dma) {
Alex Dubov13cdf482007-04-12 17:05:25 +1000748 writel((~TIFM_MMCSD_BUFINT)
749 & readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
750 sock->addr + SOCK_MMCSD_INT_ENABLE);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700751 } else {
Alex Dubov13cdf482007-04-12 17:05:25 +1000752 tifm_unmap_sg(sock, &host->bounce_buf, 1,
753 (r_data->flags & MMC_DATA_WRITE)
754 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
Alex Dubovdfef26d2007-04-12 16:59:24 +1000755 tifm_unmap_sg(sock, r_data->sg, r_data->sg_len,
756 (r_data->flags & MMC_DATA_WRITE)
757 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700758 }
Alex Dubov13cdf482007-04-12 17:05:25 +1000759
760 r_data->bytes_xfered = r_data->blocks
761 - readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS) - 1;
762 r_data->bytes_xfered *= r_data->blksz;
763 r_data->bytes_xfered += r_data->blksz
764 - readl(sock->addr + SOCK_MMCSD_BLOCK_LEN) + 1;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700765 }
766
767 writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100768 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700769
770 spin_unlock_irqrestore(&sock->lock, flags);
771 mmc_request_done(mmc, mrq);
772}
773
Alex Dubov0803dd02006-12-08 16:50:49 +1100774static void tifm_sd_abort(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700775{
Alex Dubov8e02f852006-12-08 16:50:51 +1100776 struct tifm_sd *host = (struct tifm_sd*)data;
777
Alex Dubov4020f2d2006-10-04 02:15:37 -0700778 printk(KERN_ERR DRIVER_NAME
Alex Dubovb039d4a2007-04-12 16:59:21 +1000779 ": card failed to respond for a long period of time\n");
Alex Dubov8e02f852006-12-08 16:50:51 +1100780
Alex Dubov8e02f852006-12-08 16:50:51 +1100781 tifm_eject(host->dev);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700782}
783
784static void tifm_sd_ios(struct mmc_host *mmc, struct mmc_ios *ios)
785{
786 struct tifm_sd *host = mmc_priv(mmc);
787 struct tifm_dev *sock = host->dev;
788 unsigned int clk_div1, clk_div2;
789 unsigned long flags;
790
791 spin_lock_irqsave(&sock->lock, flags);
792
793 dev_dbg(&sock->dev, "Setting bus width %d, power %d\n", ios->bus_width,
794 ios->power_mode);
795 if (ios->bus_width == MMC_BUS_WIDTH_4) {
796 writel(TIFM_MMCSD_4BBUS | readl(sock->addr + SOCK_MMCSD_CONFIG),
797 sock->addr + SOCK_MMCSD_CONFIG);
798 } else {
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100799 writel((~TIFM_MMCSD_4BBUS)
800 & readl(sock->addr + SOCK_MMCSD_CONFIG),
801 sock->addr + SOCK_MMCSD_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700802 }
803
804 if (ios->clock) {
805 clk_div1 = 20000000 / ios->clock;
806 if (!clk_div1)
807 clk_div1 = 1;
808
809 clk_div2 = 24000000 / ios->clock;
810 if (!clk_div2)
811 clk_div2 = 1;
812
813 if ((20000000 / clk_div1) > ios->clock)
814 clk_div1++;
815 if ((24000000 / clk_div2) > ios->clock)
816 clk_div2++;
817 if ((20000000 / clk_div1) > (24000000 / clk_div2)) {
818 host->clk_freq = 20000000;
819 host->clk_div = clk_div1;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100820 writel((~TIFM_CTRL_FAST_CLK)
821 & readl(sock->addr + SOCK_CONTROL),
822 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700823 } else {
824 host->clk_freq = 24000000;
825 host->clk_div = clk_div2;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100826 writel(TIFM_CTRL_FAST_CLK
827 | readl(sock->addr + SOCK_CONTROL),
828 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700829 }
830 } else {
831 host->clk_div = 0;
832 }
833 host->clk_div &= TIFM_MMCSD_CLKMASK;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100834 writel(host->clk_div
835 | ((~TIFM_MMCSD_CLKMASK)
836 & readl(sock->addr + SOCK_MMCSD_CONFIG)),
837 sock->addr + SOCK_MMCSD_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700838
Alex Dubov0007d482007-04-12 16:59:23 +1000839 host->open_drain = (ios->bus_mode == MMC_BUSMODE_OPENDRAIN);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700840
841 /* chip_select : maybe later */
842 //vdd
843 //power is set before probe / after remove
Alex Dubov4020f2d2006-10-04 02:15:37 -0700844
845 spin_unlock_irqrestore(&sock->lock, flags);
846}
847
848static int tifm_sd_ro(struct mmc_host *mmc)
849{
Alex Dubov0007d482007-04-12 16:59:23 +1000850 int rc = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700851 struct tifm_sd *host = mmc_priv(mmc);
852 struct tifm_dev *sock = host->dev;
853 unsigned long flags;
854
855 spin_lock_irqsave(&sock->lock, flags);
Alex Dubov0007d482007-04-12 16:59:23 +1000856 if (TIFM_MMCSD_CARD_RO & readl(sock->addr + SOCK_PRESENT_STATE))
857 rc = 1;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700858 spin_unlock_irqrestore(&sock->lock, flags);
859 return rc;
860}
861
Alex Dubovdfef26d2007-04-12 16:59:24 +1000862static const struct mmc_host_ops tifm_sd_ops = {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700863 .request = tifm_sd_request,
864 .set_ios = tifm_sd_ios,
865 .get_ro = tifm_sd_ro
866};
867
Alex Dubov8e02f852006-12-08 16:50:51 +1100868static int tifm_sd_initialize_host(struct tifm_sd *host)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700869{
Alex Dubov8e02f852006-12-08 16:50:51 +1100870 int rc;
871 unsigned int host_status = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700872 struct tifm_dev *sock = host->dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700873
Alex Dubov8e02f852006-12-08 16:50:51 +1100874 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
875 mmiowb();
876 host->clk_div = 61;
877 host->clk_freq = 20000000;
878 writel(TIFM_MMCSD_RESET, sock->addr + SOCK_MMCSD_SYSTEM_CONTROL);
879 writel(host->clk_div | TIFM_MMCSD_POWER,
880 sock->addr + SOCK_MMCSD_CONFIG);
881
882 /* wait up to 0.51 sec for reset */
Alex Dubov5897d652007-04-12 17:05:24 +1000883 for (rc = 32; rc <= 256; rc <<= 1) {
Alex Dubov8e02f852006-12-08 16:50:51 +1100884 if (1 & readl(sock->addr + SOCK_MMCSD_SYSTEM_STATUS)) {
885 rc = 0;
886 break;
887 }
888 msleep(rc);
889 }
890
891 if (rc) {
Alex Dubov5897d652007-04-12 17:05:24 +1000892 printk(KERN_ERR "%s : controller failed to reset\n",
893 sock->dev.bus_id);
Alex Dubov8e02f852006-12-08 16:50:51 +1100894 return -ENODEV;
895 }
896
897 writel(0, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
898 writel(host->clk_div | TIFM_MMCSD_POWER,
899 sock->addr + SOCK_MMCSD_CONFIG);
900 writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
901
902 // command timeout fixed to 64 clocks for now
903 writel(64, sock->addr + SOCK_MMCSD_COMMAND_TO);
904 writel(TIFM_MMCSD_INAB, sock->addr + SOCK_MMCSD_COMMAND);
905
Alex Dubov5897d652007-04-12 17:05:24 +1000906 for (rc = 16; rc <= 64; rc <<= 1) {
Alex Dubov8e02f852006-12-08 16:50:51 +1100907 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
908 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
909 if (!(host_status & TIFM_MMCSD_ERRMASK)
910 && (host_status & TIFM_MMCSD_EOC)) {
911 rc = 0;
912 break;
913 }
914 msleep(rc);
915 }
916
917 if (rc) {
Alex Dubov5897d652007-04-12 17:05:24 +1000918 printk(KERN_ERR
919 "%s : card not ready - probe failed on initialization\n",
920 sock->dev.bus_id);
Alex Dubov8e02f852006-12-08 16:50:51 +1100921 return -ENODEV;
922 }
923
Alex Dubov5897d652007-04-12 17:05:24 +1000924 writel(TIFM_MMCSD_CERR | TIFM_MMCSD_BRS | TIFM_MMCSD_EOC
925 | TIFM_MMCSD_ERRMASK,
Alex Dubov8e02f852006-12-08 16:50:51 +1100926 sock->addr + SOCK_MMCSD_INT_ENABLE);
927 mmiowb();
928
929 return 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700930}
931
932static int tifm_sd_probe(struct tifm_dev *sock)
933{
934 struct mmc_host *mmc;
935 struct tifm_sd *host;
936 int rc = -EIO;
937
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100938 if (!(TIFM_SOCK_STATE_OCCUPIED
939 & readl(sock->addr + SOCK_PRESENT_STATE))) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700940 printk(KERN_WARNING DRIVER_NAME ": card gone, unexpectedly\n");
941 return rc;
942 }
943
944 mmc = mmc_alloc_host(sizeof(struct tifm_sd), &sock->dev);
945 if (!mmc)
946 return -ENOMEM;
947
948 host = mmc_priv(mmc);
Alex Dubov0007d482007-04-12 16:59:23 +1000949 host->no_dma = no_dma;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700950 tifm_set_drvdata(sock, mmc);
Alex Dubov8e02f852006-12-08 16:50:51 +1100951 host->dev = sock;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700952 host->timeout_jiffies = msecs_to_jiffies(1000);
953
Alex Dubovdfef26d2007-04-12 16:59:24 +1000954 tasklet_init(&host->finish_tasklet, tifm_sd_end_cmd,
Alex Dubov8e02f852006-12-08 16:50:51 +1100955 (unsigned long)host);
956 setup_timer(&host->timer, tifm_sd_abort, (unsigned long)host);
957
Alex Dubov4020f2d2006-10-04 02:15:37 -0700958 mmc->ops = &tifm_sd_ops;
959 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Alex Dubov8e02f852006-12-08 16:50:51 +1100960 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700961 mmc->f_min = 20000000 / 60;
962 mmc->f_max = 24000000;
Alex Dubov13cdf482007-04-12 17:05:25 +1000963
964 mmc->max_blk_count = 2048;
965 mmc->max_hw_segs = mmc->max_blk_count;
966 mmc->max_blk_size = min(TIFM_MMCSD_MAX_BLOCK_SIZE, PAGE_SIZE);
967 mmc->max_seg_size = mmc->max_blk_count * mmc->max_blk_size;
968 mmc->max_req_size = mmc->max_seg_size;
969 mmc->max_phys_segs = mmc->max_hw_segs;
970
Alex Dubov4552f0c2007-04-12 16:59:12 +1000971 sock->card_event = tifm_sd_card_event;
972 sock->data_event = tifm_sd_data_event;
Alex Dubov8e02f852006-12-08 16:50:51 +1100973 rc = tifm_sd_initialize_host(host);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700974
Alex Dubov8e02f852006-12-08 16:50:51 +1100975 if (!rc)
976 rc = mmc_add_host(mmc);
977 if (rc)
978 goto out_free_mmc;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700979
980 return 0;
Alex Dubov8e02f852006-12-08 16:50:51 +1100981out_free_mmc:
982 mmc_free_host(mmc);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700983 return rc;
984}
985
986static void tifm_sd_remove(struct tifm_dev *sock)
987{
988 struct mmc_host *mmc = tifm_get_drvdata(sock);
989 struct tifm_sd *host = mmc_priv(mmc);
Alex Dubovb039d4a2007-04-12 16:59:21 +1000990 unsigned long flags;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700991
Alex Dubovb039d4a2007-04-12 16:59:21 +1000992 spin_lock_irqsave(&sock->lock, flags);
Alex Dubov0007d482007-04-12 16:59:23 +1000993 host->eject = 1;
Alex Dubov592d3722007-04-12 16:59:22 +1000994 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
995 mmiowb();
996 spin_unlock_irqrestore(&sock->lock, flags);
997
998 tasklet_kill(&host->finish_tasklet);
999
1000 spin_lock_irqsave(&sock->lock, flags);
Alex Dubovb039d4a2007-04-12 16:59:21 +10001001 if (host->req) {
1002 writel(TIFM_FIFO_INT_SETALL,
1003 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
1004 writel(0, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
Alex Dubov592d3722007-04-12 16:59:22 +10001005 host->req->cmd->error = MMC_ERR_TIMEOUT;
1006 if (host->req->stop)
1007 host->req->stop->error = MMC_ERR_TIMEOUT;
Alex Dubovb039d4a2007-04-12 16:59:21 +10001008 tasklet_schedule(&host->finish_tasklet);
1009 }
1010 spin_unlock_irqrestore(&sock->lock, flags);
Alex Dubov8e02f852006-12-08 16:50:51 +11001011 mmc_remove_host(mmc);
Alex Dubov592d3722007-04-12 16:59:22 +10001012 dev_dbg(&sock->dev, "after remove\n");
Alex Dubov4020f2d2006-10-04 02:15:37 -07001013
1014 /* The meaning of the bit majority in this constant is unknown. */
1015 writel(0xfff8 & readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +11001016 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -07001017
Alex Dubov4020f2d2006-10-04 02:15:37 -07001018 mmc_free_host(mmc);
1019}
1020
Alex Dubovdba4acc2006-12-11 01:55:38 +11001021#ifdef CONFIG_PM
1022
1023static int tifm_sd_suspend(struct tifm_dev *sock, pm_message_t state)
1024{
1025 struct mmc_host *mmc = tifm_get_drvdata(sock);
1026 int rc;
1027
1028 rc = mmc_suspend_host(mmc, state);
1029 /* The meaning of the bit majority in this constant is unknown. */
1030 writel(0xfff8 & readl(sock->addr + SOCK_CONTROL),
1031 sock->addr + SOCK_CONTROL);
1032 return rc;
1033}
1034
1035static int tifm_sd_resume(struct tifm_dev *sock)
1036{
1037 struct mmc_host *mmc = tifm_get_drvdata(sock);
1038 struct tifm_sd *host = mmc_priv(mmc);
Alex Dubov5897d652007-04-12 17:05:24 +10001039 int rc;
Alex Dubovdba4acc2006-12-11 01:55:38 +11001040
Alex Dubov5897d652007-04-12 17:05:24 +10001041 rc = tifm_sd_initialize_host(host);
1042 dev_dbg(&sock->dev, "resume initialize %d\n", rc);
1043
1044 if (rc)
1045 host->eject = 1;
1046 else
1047 rc = mmc_resume_host(mmc);
1048
1049 return rc;
Alex Dubovdba4acc2006-12-11 01:55:38 +11001050}
1051
1052#else
1053
1054#define tifm_sd_suspend NULL
1055#define tifm_sd_resume NULL
1056
1057#endif /* CONFIG_PM */
1058
Alex Dubove23f2b82007-04-12 16:59:14 +10001059static struct tifm_device_id tifm_sd_id_tbl[] = {
1060 { TIFM_TYPE_SD }, { }
Alex Dubov4020f2d2006-10-04 02:15:37 -07001061};
1062
1063static struct tifm_driver tifm_sd_driver = {
1064 .driver = {
1065 .name = DRIVER_NAME,
1066 .owner = THIS_MODULE
1067 },
1068 .id_table = tifm_sd_id_tbl,
1069 .probe = tifm_sd_probe,
Alex Dubovdba4acc2006-12-11 01:55:38 +11001070 .remove = tifm_sd_remove,
1071 .suspend = tifm_sd_suspend,
1072 .resume = tifm_sd_resume
Alex Dubov4020f2d2006-10-04 02:15:37 -07001073};
1074
1075static int __init tifm_sd_init(void)
1076{
1077 return tifm_register_driver(&tifm_sd_driver);
1078}
1079
1080static void __exit tifm_sd_exit(void)
1081{
1082 tifm_unregister_driver(&tifm_sd_driver);
1083}
1084
1085MODULE_AUTHOR("Alex Dubov");
1086MODULE_DESCRIPTION("TI FlashMedia SD driver");
1087MODULE_LICENSE("GPL");
1088MODULE_DEVICE_TABLE(tifm, tifm_sd_id_tbl);
1089MODULE_VERSION(DRIVER_VERSION);
1090
1091module_init(tifm_sd_init);
1092module_exit(tifm_sd_exit);