blob: 951392d2ce37140cabbac77b1d6cc146b56c3995 [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 *
Alex Dubov91f8d012007-04-12 17:05:26 +100010 * Special thanks to Brad Campbell for extensive testing of this driver.
11 *
Alex Dubov4020f2d2006-10-04 02:15:37 -070012 */
13
14
15#include <linux/tifm.h>
Alex Dubov4020f2d2006-10-04 02:15:37 -070016#include <linux/mmc/host.h>
17#include <linux/highmem.h>
Alex Dubov13cdf482007-04-12 17:05:25 +100018#include <linux/scatterlist.h>
Al Viro2099c992006-10-24 11:17:06 +010019#include <asm/io.h>
Alex Dubov4020f2d2006-10-04 02:15:37 -070020
21#define DRIVER_NAME "tifm_sd"
Alex Dubov4552f0c2007-04-12 16:59:12 +100022#define DRIVER_VERSION "0.8"
Alex Dubov4020f2d2006-10-04 02:15:37 -070023
24static int no_dma = 0;
25static int fixed_timeout = 0;
26module_param(no_dma, bool, 0644);
27module_param(fixed_timeout, bool, 0644);
28
29/* Constants here are mostly from OMAP5912 datasheet */
30#define TIFM_MMCSD_RESET 0x0002
31#define TIFM_MMCSD_CLKMASK 0x03ff
32#define TIFM_MMCSD_POWER 0x0800
33#define TIFM_MMCSD_4BBUS 0x8000
34#define TIFM_MMCSD_RXDE 0x8000 /* rx dma enable */
35#define TIFM_MMCSD_TXDE 0x0080 /* tx dma enable */
36#define TIFM_MMCSD_BUFINT 0x0c00 /* set bits: AE, AF */
37#define TIFM_MMCSD_DPE 0x0020 /* data timeout counted in kilocycles */
38#define TIFM_MMCSD_INAB 0x0080 /* abort / initialize command */
39#define TIFM_MMCSD_READ 0x8000
40
Alex Dubov12c83452007-02-10 21:16:40 -080041#define TIFM_MMCSD_ERRMASK 0x01e0 /* set bits: CCRC, CTO, DCRC, DTO */
Alex Dubov4020f2d2006-10-04 02:15:37 -070042#define TIFM_MMCSD_EOC 0x0001 /* end of command phase */
Alex Dubov91f8d012007-04-12 17:05:26 +100043#define TIFM_MMCSD_CD 0x0002 /* card detect */
Alex Dubov4020f2d2006-10-04 02:15:37 -070044#define TIFM_MMCSD_CB 0x0004 /* card enter busy state */
45#define TIFM_MMCSD_BRS 0x0008 /* block received/sent */
46#define TIFM_MMCSD_EOFB 0x0010 /* card exit busy state */
47#define TIFM_MMCSD_DTO 0x0020 /* data time-out */
48#define TIFM_MMCSD_DCRC 0x0040 /* data crc error */
49#define TIFM_MMCSD_CTO 0x0080 /* command time-out */
50#define TIFM_MMCSD_CCRC 0x0100 /* command crc error */
51#define TIFM_MMCSD_AF 0x0400 /* fifo almost full */
52#define TIFM_MMCSD_AE 0x0800 /* fifo almost empty */
Alex Dubov91f8d012007-04-12 17:05:26 +100053#define TIFM_MMCSD_OCRB 0x1000 /* OCR busy */
54#define TIFM_MMCSD_CIRQ 0x2000 /* card irq (cmd40/sdio) */
Alex Dubov4020f2d2006-10-04 02:15:37 -070055#define TIFM_MMCSD_CERR 0x4000 /* card status error */
56
Alex Dubov0007d482007-04-12 16:59:23 +100057#define TIFM_MMCSD_ODTO 0x0040 /* open drain / extended timeout */
58#define TIFM_MMCSD_CARD_RO 0x0200 /* card is read-only */
59
Alex Dubov4020f2d2006-10-04 02:15:37 -070060#define TIFM_MMCSD_FIFO_SIZE 0x0020
61
62#define TIFM_MMCSD_RSP_R0 0x0000
63#define TIFM_MMCSD_RSP_R1 0x0100
64#define TIFM_MMCSD_RSP_R2 0x0200
65#define TIFM_MMCSD_RSP_R3 0x0300
66#define TIFM_MMCSD_RSP_R4 0x0400
67#define TIFM_MMCSD_RSP_R5 0x0500
68#define TIFM_MMCSD_RSP_R6 0x0600
69
70#define TIFM_MMCSD_RSP_BUSY 0x0800
71
72#define TIFM_MMCSD_CMD_BC 0x0000
73#define TIFM_MMCSD_CMD_BCR 0x1000
74#define TIFM_MMCSD_CMD_AC 0x2000
75#define TIFM_MMCSD_CMD_ADTC 0x3000
76
Alex Dubov13cdf482007-04-12 17:05:25 +100077#define TIFM_MMCSD_MAX_BLOCK_SIZE 0x0800UL
78
Alex Dubov4020f2d2006-10-04 02:15:37 -070079enum {
Alex Dubov72dc9d92007-04-12 17:05:23 +100080 CMD_READY = 0x0001,
81 FIFO_READY = 0x0002,
82 BRS_READY = 0x0004,
83 SCMD_ACTIVE = 0x0008,
84 SCMD_READY = 0x0010,
85 CARD_BUSY = 0x0020,
86 DATA_CARRY = 0x0040
Alex Dubov0007d482007-04-12 16:59:23 +100087};
Alex Dubov4020f2d2006-10-04 02:15:37 -070088
89struct tifm_sd {
Alex Dubov91f8d012007-04-12 17:05:26 +100090 struct tifm_dev *dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -070091
Alex Dubov91f8d012007-04-12 17:05:26 +100092 unsigned short eject:1,
93 open_drain:1,
94 no_dma:1;
95 unsigned short cmd_flags;
Alex Dubov72dc9d92007-04-12 17:05:23 +100096
Alex Dubov91f8d012007-04-12 17:05:26 +100097 unsigned int clk_freq;
98 unsigned int clk_div;
99 unsigned long timeout_jiffies;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700100
Alex Dubov8e02f852006-12-08 16:50:51 +1100101 struct tasklet_struct finish_tasklet;
Alex Dubov0803dd02006-12-08 16:50:49 +1100102 struct timer_list timer;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700103 struct mmc_request *req;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700104
Alex Dubov13cdf482007-04-12 17:05:25 +1000105 int sg_len;
106 int sg_pos;
107 unsigned int block_pos;
108 struct scatterlist bounce_buf;
109 unsigned char bounce_buf_data[TIFM_MMCSD_MAX_BLOCK_SIZE];
Alex Dubov4020f2d2006-10-04 02:15:37 -0700110};
111
Alex Dubov13cdf482007-04-12 17:05:25 +1000112/* for some reason, host won't respond correctly to readw/writew */
113static void tifm_sd_read_fifo(struct tifm_sd *host, struct page *pg,
114 unsigned int off, unsigned int cnt)
Alex Dubov255ef222006-12-08 16:50:48 +1100115{
Alex Dubov13cdf482007-04-12 17:05:25 +1000116 struct tifm_dev *sock = host->dev;
117 unsigned char *buf;
118 unsigned int pos = 0, val;
119
120 buf = kmap_atomic(pg, KM_BIO_DST_IRQ) + off;
121 if (host->cmd_flags & DATA_CARRY) {
122 buf[pos++] = host->bounce_buf_data[0];
123 host->cmd_flags &= ~DATA_CARRY;
124 }
125
126 while (pos < cnt) {
127 val = readl(sock->addr + SOCK_MMCSD_DATA);
128 buf[pos++] = val & 0xff;
129 if (pos == cnt) {
130 host->bounce_buf_data[0] = (val >> 8) & 0xff;
131 host->cmd_flags |= DATA_CARRY;
132 break;
133 }
134 buf[pos++] = (val >> 8) & 0xff;
135 }
136 kunmap_atomic(buf - off, KM_BIO_DST_IRQ);
Alex Dubov255ef222006-12-08 16:50:48 +1100137}
138
Alex Dubov13cdf482007-04-12 17:05:25 +1000139static void tifm_sd_write_fifo(struct tifm_sd *host, struct page *pg,
140 unsigned int off, unsigned int cnt)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700141{
Alex Dubov13cdf482007-04-12 17:05:25 +1000142 struct tifm_dev *sock = host->dev;
143 unsigned char *buf;
144 unsigned int pos = 0, val;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700145
Alex Dubov13cdf482007-04-12 17:05:25 +1000146 buf = kmap_atomic(pg, KM_BIO_SRC_IRQ) + off;
147 if (host->cmd_flags & DATA_CARRY) {
148 val = host->bounce_buf_data[0] | ((buf[pos++] << 8) & 0xff00);
149 writel(val, sock->addr + SOCK_MMCSD_DATA);
150 host->cmd_flags &= ~DATA_CARRY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700151 }
Alex Dubov13cdf482007-04-12 17:05:25 +1000152
153 while (pos < cnt) {
154 val = buf[pos++];
155 if (pos == cnt) {
156 host->bounce_buf_data[0] = val & 0xff;
157 host->cmd_flags |= DATA_CARRY;
158 break;
159 }
160 val |= (buf[pos++] << 8) & 0xff00;
161 writel(val, sock->addr + SOCK_MMCSD_DATA);
162 }
163 kunmap_atomic(buf - off, KM_BIO_SRC_IRQ);
164}
165
166static void tifm_sd_transfer_data(struct tifm_sd *host)
167{
168 struct mmc_data *r_data = host->req->cmd->data;
169 struct scatterlist *sg = r_data->sg;
170 unsigned int off, cnt, t_size = TIFM_MMCSD_FIFO_SIZE * 2;
171 unsigned int p_off, p_cnt;
172 struct page *pg;
173
174 if (host->sg_pos == host->sg_len)
175 return;
176 while (t_size) {
177 cnt = sg[host->sg_pos].length - host->block_pos;
178 if (!cnt) {
179 host->block_pos = 0;
180 host->sg_pos++;
181 if (host->sg_pos == host->sg_len) {
182 if ((r_data->flags & MMC_DATA_WRITE)
183 && DATA_CARRY)
184 writel(host->bounce_buf_data[0],
185 host->dev->addr
186 + SOCK_MMCSD_DATA);
187
188 return;
189 }
190 cnt = sg[host->sg_pos].length;
191 }
192 off = sg[host->sg_pos].offset + host->block_pos;
193
194 pg = nth_page(sg[host->sg_pos].page, off >> PAGE_SHIFT);
195 p_off = offset_in_page(off);
196 p_cnt = PAGE_SIZE - p_off;
197 p_cnt = min(p_cnt, cnt);
198 p_cnt = min(p_cnt, t_size);
199
200 if (r_data->flags & MMC_DATA_READ)
201 tifm_sd_read_fifo(host, pg, p_off, p_cnt);
202 else if (r_data->flags & MMC_DATA_WRITE)
203 tifm_sd_write_fifo(host, pg, p_off, p_cnt);
204
205 t_size -= p_cnt;
206 host->block_pos += p_cnt;
207 }
208}
209
210static void tifm_sd_copy_page(struct page *dst, unsigned int dst_off,
211 struct page *src, unsigned int src_off,
212 unsigned int count)
213{
214 unsigned char *src_buf = kmap_atomic(src, KM_BIO_SRC_IRQ) + src_off;
215 unsigned char *dst_buf = kmap_atomic(dst, KM_BIO_DST_IRQ) + dst_off;
216
217 memcpy(dst_buf, src_buf, count);
218
219 kunmap_atomic(dst_buf - dst_off, KM_BIO_DST_IRQ);
220 kunmap_atomic(src_buf - src_off, KM_BIO_SRC_IRQ);
221}
222
223static void tifm_sd_bounce_block(struct tifm_sd *host, struct mmc_data *r_data)
224{
225 struct scatterlist *sg = r_data->sg;
226 unsigned int t_size = r_data->blksz;
227 unsigned int off, cnt;
228 unsigned int p_off, p_cnt;
229 struct page *pg;
230
231 dev_dbg(&host->dev->dev, "bouncing block\n");
232 while (t_size) {
233 cnt = sg[host->sg_pos].length - host->block_pos;
234 if (!cnt) {
235 host->block_pos = 0;
236 host->sg_pos++;
237 if (host->sg_pos == host->sg_len)
238 return;
239 cnt = sg[host->sg_pos].length;
240 }
241 off = sg[host->sg_pos].offset + host->block_pos;
242
243 pg = nth_page(sg[host->sg_pos].page, off >> PAGE_SHIFT);
244 p_off = offset_in_page(off);
245 p_cnt = PAGE_SIZE - p_off;
246 p_cnt = min(p_cnt, cnt);
247 p_cnt = min(p_cnt, t_size);
248
249 if (r_data->flags & MMC_DATA_WRITE)
250 tifm_sd_copy_page(host->bounce_buf.page,
251 r_data->blksz - t_size,
252 pg, p_off, p_cnt);
253 else if (r_data->flags & MMC_DATA_READ)
254 tifm_sd_copy_page(pg, p_off, host->bounce_buf.page,
255 r_data->blksz - t_size, p_cnt);
256
257 t_size -= p_cnt;
258 host->block_pos += p_cnt;
259 }
260}
261
Adrian Bunkd97956f2007-05-01 16:14:29 +0200262static int tifm_sd_set_dma_data(struct tifm_sd *host, struct mmc_data *r_data)
Alex Dubov13cdf482007-04-12 17:05:25 +1000263{
264 struct tifm_dev *sock = host->dev;
265 unsigned int t_size = TIFM_DMA_TSIZE * r_data->blksz;
266 unsigned int dma_len, dma_blk_cnt, dma_off;
267 struct scatterlist *sg = NULL;
268 unsigned long flags;
269
270 if (host->sg_pos == host->sg_len)
271 return 1;
272
273 if (host->cmd_flags & DATA_CARRY) {
274 host->cmd_flags &= ~DATA_CARRY;
275 local_irq_save(flags);
276 tifm_sd_bounce_block(host, r_data);
277 local_irq_restore(flags);
278 if (host->sg_pos == host->sg_len)
279 return 1;
280 }
281
282 dma_len = sg_dma_len(&r_data->sg[host->sg_pos]) - host->block_pos;
283 if (!dma_len) {
284 host->block_pos = 0;
285 host->sg_pos++;
286 if (host->sg_pos == host->sg_len)
287 return 1;
288 dma_len = sg_dma_len(&r_data->sg[host->sg_pos]);
289 }
290
291 if (dma_len < t_size) {
292 dma_blk_cnt = dma_len / r_data->blksz;
293 dma_off = host->block_pos;
294 host->block_pos += dma_blk_cnt * r_data->blksz;
295 } else {
296 dma_blk_cnt = TIFM_DMA_TSIZE;
297 dma_off = host->block_pos;
298 host->block_pos += t_size;
299 }
300
301 if (dma_blk_cnt)
302 sg = &r_data->sg[host->sg_pos];
303 else if (dma_len) {
304 if (r_data->flags & MMC_DATA_WRITE) {
305 local_irq_save(flags);
306 tifm_sd_bounce_block(host, r_data);
307 local_irq_restore(flags);
308 } else
309 host->cmd_flags |= DATA_CARRY;
310
311 sg = &host->bounce_buf;
312 dma_off = 0;
313 dma_blk_cnt = 1;
314 } else
315 return 1;
316
317 dev_dbg(&sock->dev, "setting dma for %d blocks\n", dma_blk_cnt);
318 writel(sg_dma_address(sg) + dma_off, sock->addr + SOCK_DMA_ADDRESS);
319 if (r_data->flags & MMC_DATA_WRITE)
320 writel((dma_blk_cnt << 8) | TIFM_DMA_TX | TIFM_DMA_EN,
321 sock->addr + SOCK_DMA_CONTROL);
322 else
323 writel((dma_blk_cnt << 8) | TIFM_DMA_EN,
324 sock->addr + SOCK_DMA_CONTROL);
325
Alex Dubov4020f2d2006-10-04 02:15:37 -0700326 return 0;
327}
328
329static unsigned int tifm_sd_op_flags(struct mmc_command *cmd)
330{
331 unsigned int rc = 0;
332
333 switch (mmc_resp_type(cmd)) {
334 case MMC_RSP_NONE:
335 rc |= TIFM_MMCSD_RSP_R0;
336 break;
337 case MMC_RSP_R1B:
338 rc |= TIFM_MMCSD_RSP_BUSY; // deliberate fall-through
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 Dubov4020f2d2006-10-04 02:15:37 -0700348 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
371static void tifm_sd_exec(struct tifm_sd *host, struct mmc_command *cmd)
372{
373 struct tifm_dev *sock = host->dev;
Alex Dubov0007d482007-04-12 16:59:23 +1000374 unsigned int cmd_mask = tifm_sd_op_flags(cmd);
375
376 if (host->open_drain)
377 cmd_mask |= TIFM_MMCSD_ODTO;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700378
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 Dubov2e8ce5e2006-12-08 16:50:52 +1100383 cmd->opcode, cmd->arg, cmd_mask);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700384
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
390static 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 Dubov72dc9d92007-04-12 17:05:23 +1000402static void tifm_sd_check_status(struct tifm_sd *host)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700403{
Alex Dubov72dc9d92007-04-12 17:05:23 +1000404 struct tifm_dev *sock = host->dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700405 struct mmc_command *cmd = host->req->cmd;
406
Pierre Ossman17b04292007-07-22 22:18:46 +0200407 if (cmd->error)
Alex Dubov72dc9d92007-04-12 17:05:23 +1000408 goto finish_request;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700409
Alex Dubov72dc9d92007-04-12 17:05:23 +1000410 if (!(host->cmd_flags & CMD_READY))
411 return;
412
413 if (cmd->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200414 if (cmd->data->error) {
Alex Dubov72dc9d92007-04-12 17:05:23 +1000415 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 }
470finish_request:
471 tasklet_schedule(&host->finish_tasklet);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700472}
473
474/* Called from interrupt handler */
Alex Dubov4552f0c2007-04-12 16:59:12 +1000475static void tifm_sd_data_event(struct tifm_dev *sock)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700476{
477 struct tifm_sd *host;
Alex Dubov4552f0c2007-04-12 16:59:12 +1000478 unsigned int fifo_status = 0;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000479 struct mmc_data *r_data = NULL;
Alex Dubov4552f0c2007-04-12 16:59:12 +1000480
481 spin_lock(&sock->lock);
482 host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock));
Alex Dubov4552f0c2007-04-12 16:59:12 +1000483 fifo_status = readl(sock->addr + SOCK_DMA_FIFO_STATUS);
Alex Dubov72dc9d92007-04-12 17:05:23 +1000484 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 Dubov13cdf482007-04-12 17:05:25 +1000491 if (tifm_sd_set_dma_data(host, r_data)) {
492 host->cmd_flags |= FIFO_READY;
493 tifm_sd_check_status(host);
494 }
Alex Dubov72dc9d92007-04-12 17:05:23 +1000495 }
496 }
497
Alex Dubov4552f0c2007-04-12 16:59:12 +1000498 writel(fifo_status, sock->addr + SOCK_DMA_FIFO_STATUS);
Alex Dubov4552f0c2007-04-12 16:59:12 +1000499 spin_unlock(&sock->lock);
Alex Dubov4552f0c2007-04-12 16:59:12 +1000500}
501
502/* Called from interrupt handler */
503static void tifm_sd_card_event(struct tifm_dev *sock)
504{
505 struct tifm_sd *host;
506 unsigned int host_status = 0;
Pierre Ossman17b04292007-07-22 22:18:46 +0200507 int cmd_error = 0;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000508 struct mmc_command *cmd = NULL;
509 unsigned long flags;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700510
511 spin_lock(&sock->lock);
512 host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock));
Alex Dubov72dc9d92007-04-12 17:05:23 +1000513 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 Dubov4020f2d2006-10-04 02:15:37 -0700516
Alex Dubov72dc9d92007-04-12 17:05:23 +1000517 if (host->req) {
518 cmd = host->req->cmd;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700519
520 if (host_status & TIFM_MMCSD_ERRMASK) {
Alex Dubov72dc9d92007-04-12 17:05:23 +1000521 writel(host_status & TIFM_MMCSD_ERRMASK,
522 sock->addr + SOCK_MMCSD_STATUS);
523 if (host_status & TIFM_MMCSD_CTO)
Pierre Ossman17b04292007-07-22 22:18:46 +0200524 cmd_error = -ETIMEDOUT;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000525 else if (host_status & TIFM_MMCSD_CCRC)
Pierre Ossman17b04292007-07-22 22:18:46 +0200526 cmd_error = -EILSEQ;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000527
528 if (cmd->data) {
529 if (host_status & TIFM_MMCSD_DTO)
Pierre Ossman17b04292007-07-22 22:18:46 +0200530 cmd->data->error = -ETIMEDOUT;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000531 else if (host_status & TIFM_MMCSD_DCRC)
Pierre Ossman17b04292007-07-22 22:18:46 +0200532 cmd->data->error = -EILSEQ;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000533 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700534
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 Dubov72dc9d92007-04-12 17:05:23 +1000540 if (host->cmd_flags & SCMD_ACTIVE) {
541 host->req->stop->error = cmd_error;
542 host->cmd_flags |= SCMD_READY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700543 } else {
Alex Dubov72dc9d92007-04-12 17:05:23 +1000544 cmd->error = cmd_error;
545 host->cmd_flags |= SCMD_ACTIVE;
546 tifm_sd_exec(host, host->req->stop);
547 goto done;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700548 }
Alex Dubov72dc9d92007-04-12 17:05:23 +1000549 } 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 Dubov4020f2d2006-10-04 02:15:37 -0700561 }
Alex Dubov72dc9d92007-04-12 17:05:23 +1000562 if (host_status & TIFM_MMCSD_BRS)
563 host->cmd_flags |= BRS_READY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700564 }
565
Alex Dubov72dc9d92007-04-12 17:05:23 +1000566 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 Dubov13cdf482007-04-12 17:05:25 +1000574 tifm_sd_transfer_data(host);
Alex Dubov72dc9d92007-04-12 17:05:23 +1000575 local_irq_restore(flags);
576 host_status &= ~TIFM_MMCSD_AE;
577 }
578 }
579
580 if (host_status & TIFM_MMCSD_EOFB)
Alex Dubov0007d482007-04-12 16:59:23 +1000581 host->cmd_flags &= ~CARD_BUSY;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000582 else if (host_status & TIFM_MMCSD_CB)
583 host->cmd_flags |= CARD_BUSY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700584
Alex Dubov72dc9d92007-04-12 17:05:23 +1000585 tifm_sd_check_status(host);
586 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700587done:
Alex Dubov72dc9d92007-04-12 17:05:23 +1000588 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700589 spin_unlock(&sock->lock);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700590}
591
Alex Dubov4020f2d2006-10-04 02:15:37 -0700592static void tifm_sd_set_data_timeout(struct tifm_sd *host,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100593 struct mmc_data *data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700594{
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 Dubov83d420b2006-12-08 16:50:50 +1100602 ((1000000000UL / host->clk_freq) * host->clk_div);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700603
604 if (data_timeout < 0xffff) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700605 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
Alex Dubov83d420b2006-12-08 16:50:50 +1100606 writel((~TIFM_MMCSD_DPE)
607 & readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
608 sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700609 } else {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700610 data_timeout = (data_timeout >> 10) + 1;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100611 if (data_timeout > 0xffff)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700612 data_timeout = 0; /* set to unlimited */
613 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
Alex Dubov83d420b2006-12-08 16:50:50 +1100614 writel(TIFM_MMCSD_DPE
615 | readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
616 sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700617 }
618}
619
620static 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 Dubov4020f2d2006-10-04 02:15:37 -0700625 struct mmc_data *r_data = mrq->cmd->data;
626
627 spin_lock_irqsave(&sock->lock, flags);
Alex Dubov0007d482007-04-12 16:59:23 +1000628 if (host->eject) {
Pierre Ossman255d01a2007-07-24 20:38:53 +0200629 mrq->cmd->error = -ENOMEDIUM;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700630 goto err_out;
631 }
632
633 if (host->req) {
Alex Dubov91f8d012007-04-12 17:05:26 +1000634 printk(KERN_ERR "%s : unfinished request detected\n",
635 sock->dev.bus_id);
Pierre Ossman255d01a2007-07-24 20:38:53 +0200636 mrq->cmd->error = -ETIMEDOUT;
637 goto err_out;
638 }
639
640 if (mrq->data && (hweight32(mrq->data->blksz) > 1)) {
641 printk(KERN_ERR "%s: Unsupported block size (%d bytes)\n",
642 sock->dev.bus_id, mrq->data->blksz);
643 mrq->cmd->error = -EINVAL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700644 goto err_out;
645 }
646
Alex Dubov13cdf482007-04-12 17:05:25 +1000647 host->cmd_flags = 0;
648 host->block_pos = 0;
649 host->sg_pos = 0;
650
Alex Dubov4020f2d2006-10-04 02:15:37 -0700651 if (r_data) {
652 tifm_sd_set_data_timeout(host, r_data);
653
Alex Dubov13cdf482007-04-12 17:05:25 +1000654 if ((r_data->flags & MMC_DATA_WRITE) && !mrq->stop)
655 writel(TIFM_MMCSD_EOFB
656 | readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
657 sock->addr + SOCK_MMCSD_INT_ENABLE);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700658
Alex Dubov13cdf482007-04-12 17:05:25 +1000659 if (host->no_dma) {
Alex Dubovdfef26d2007-04-12 16:59:24 +1000660 writel(TIFM_MMCSD_BUFINT
661 | readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
662 sock->addr + SOCK_MMCSD_INT_ENABLE);
663 writel(((TIFM_MMCSD_FIFO_SIZE - 1) << 8)
664 | (TIFM_MMCSD_FIFO_SIZE - 1),
665 sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
666
Alex Dubov13cdf482007-04-12 17:05:25 +1000667 host->sg_len = r_data->sg_len;
Alex Dubovdfef26d2007-04-12 16:59:24 +1000668 } else {
Alex Dubov13cdf482007-04-12 17:05:25 +1000669 sg_init_one(&host->bounce_buf, host->bounce_buf_data,
670 r_data->blksz);
671
672 if(1 != tifm_map_sg(sock, &host->bounce_buf, 1,
673 r_data->flags & MMC_DATA_WRITE
674 ? PCI_DMA_TODEVICE
675 : PCI_DMA_FROMDEVICE)) {
676 printk(KERN_ERR "%s : scatterlist map failed\n",
677 sock->dev.bus_id);
678 spin_unlock_irqrestore(&sock->lock, flags);
679 goto err_out;
680 }
681 host->sg_len = tifm_map_sg(sock, r_data->sg,
682 r_data->sg_len,
683 r_data->flags
684 & MMC_DATA_WRITE
685 ? PCI_DMA_TODEVICE
686 : PCI_DMA_FROMDEVICE);
687 if (host->sg_len < 1) {
688 printk(KERN_ERR "%s : scatterlist map failed\n",
689 sock->dev.bus_id);
690 tifm_unmap_sg(sock, &host->bounce_buf, 1,
691 r_data->flags & MMC_DATA_WRITE
692 ? PCI_DMA_TODEVICE
693 : PCI_DMA_FROMDEVICE);
Alex Dubovdfef26d2007-04-12 16:59:24 +1000694 spin_unlock_irqrestore(&sock->lock, flags);
695 goto err_out;
696 }
697
Alex Dubov13cdf482007-04-12 17:05:25 +1000698 writel(TIFM_FIFO_INT_SETALL,
699 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
700 writel(ilog2(r_data->blksz) - 2,
701 sock->addr + SOCK_FIFO_PAGE_SIZE);
702 writel(TIFM_FIFO_ENABLE,
703 sock->addr + SOCK_FIFO_CONTROL);
704 writel(TIFM_FIFO_INTMASK,
705 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
706
707 if (r_data->flags & MMC_DATA_WRITE)
708 writel(TIFM_MMCSD_TXDE,
709 sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
710 else
711 writel(TIFM_MMCSD_RXDE,
712 sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
713
714 tifm_sd_set_dma_data(host, r_data);
Alex Dubovdfef26d2007-04-12 16:59:24 +1000715 }
Alex Dubov13cdf482007-04-12 17:05:25 +1000716
717 writel(r_data->blocks - 1,
718 sock->addr + SOCK_MMCSD_NUM_BLOCKS);
719 writel(r_data->blksz - 1,
720 sock->addr + SOCK_MMCSD_BLOCK_LEN);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700721 }
722
723 host->req = mrq;
Alex Dubov0803dd02006-12-08 16:50:49 +1100724 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700725 writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100726 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700727 tifm_sd_exec(host, mrq->cmd);
728 spin_unlock_irqrestore(&sock->lock, flags);
729 return;
730
731err_out:
Pierre Ossman255d01a2007-07-24 20:38:53 +0200732 spin_unlock_irqrestore(&sock->lock, flags);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700733 mmc_request_done(mmc, mrq);
734}
735
Alex Dubov8e02f852006-12-08 16:50:51 +1100736static void tifm_sd_end_cmd(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700737{
Alex Dubov8e02f852006-12-08 16:50:51 +1100738 struct tifm_sd *host = (struct tifm_sd*)data;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700739 struct tifm_dev *sock = host->dev;
740 struct mmc_host *mmc = tifm_get_drvdata(sock);
741 struct mmc_request *mrq;
Al Viroe069d792006-10-09 20:29:43 +0100742 struct mmc_data *r_data = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700743 unsigned long flags;
744
745 spin_lock_irqsave(&sock->lock, flags);
746
Alex Dubov0803dd02006-12-08 16:50:49 +1100747 del_timer(&host->timer);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700748 mrq = host->req;
Al Viroe069d792006-10-09 20:29:43 +0100749 host->req = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700750
751 if (!mrq) {
Alex Dubov91f8d012007-04-12 17:05:26 +1000752 printk(KERN_ERR " %s : no request to complete?\n",
753 sock->dev.bus_id);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700754 spin_unlock_irqrestore(&sock->lock, flags);
755 return;
756 }
757
758 r_data = mrq->cmd->data;
759 if (r_data) {
Alex Dubovdfef26d2007-04-12 16:59:24 +1000760 if (host->no_dma) {
Alex Dubov13cdf482007-04-12 17:05:25 +1000761 writel((~TIFM_MMCSD_BUFINT)
762 & readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
763 sock->addr + SOCK_MMCSD_INT_ENABLE);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700764 } else {
Alex Dubov13cdf482007-04-12 17:05:25 +1000765 tifm_unmap_sg(sock, &host->bounce_buf, 1,
766 (r_data->flags & MMC_DATA_WRITE)
767 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
Alex Dubovdfef26d2007-04-12 16:59:24 +1000768 tifm_unmap_sg(sock, r_data->sg, r_data->sg_len,
769 (r_data->flags & MMC_DATA_WRITE)
770 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700771 }
Alex Dubov13cdf482007-04-12 17:05:25 +1000772
773 r_data->bytes_xfered = r_data->blocks
774 - readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS) - 1;
775 r_data->bytes_xfered *= r_data->blksz;
776 r_data->bytes_xfered += r_data->blksz
777 - readl(sock->addr + SOCK_MMCSD_BLOCK_LEN) + 1;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700778 }
779
780 writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100781 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700782
783 spin_unlock_irqrestore(&sock->lock, flags);
784 mmc_request_done(mmc, mrq);
785}
786
Alex Dubov0803dd02006-12-08 16:50:49 +1100787static void tifm_sd_abort(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700788{
Alex Dubov8e02f852006-12-08 16:50:51 +1100789 struct tifm_sd *host = (struct tifm_sd*)data;
790
Alex Dubov91f8d012007-04-12 17:05:26 +1000791 printk(KERN_ERR
792 "%s : card failed to respond for a long period of time "
793 "(%x, %x)\n",
794 host->dev->dev.bus_id, host->req->cmd->opcode, host->cmd_flags);
Alex Dubov8e02f852006-12-08 16:50:51 +1100795
Alex Dubov8e02f852006-12-08 16:50:51 +1100796 tifm_eject(host->dev);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700797}
798
799static void tifm_sd_ios(struct mmc_host *mmc, struct mmc_ios *ios)
800{
801 struct tifm_sd *host = mmc_priv(mmc);
802 struct tifm_dev *sock = host->dev;
803 unsigned int clk_div1, clk_div2;
804 unsigned long flags;
805
806 spin_lock_irqsave(&sock->lock, flags);
807
Alex Dubov91f8d012007-04-12 17:05:26 +1000808 dev_dbg(&sock->dev, "ios: clock = %u, vdd = %x, bus_mode = %x, "
809 "chip_select = %x, power_mode = %x, bus_width = %x\n",
810 ios->clock, ios->vdd, ios->bus_mode, ios->chip_select,
811 ios->power_mode, ios->bus_width);
812
Alex Dubov4020f2d2006-10-04 02:15:37 -0700813 if (ios->bus_width == MMC_BUS_WIDTH_4) {
814 writel(TIFM_MMCSD_4BBUS | readl(sock->addr + SOCK_MMCSD_CONFIG),
815 sock->addr + SOCK_MMCSD_CONFIG);
816 } else {
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100817 writel((~TIFM_MMCSD_4BBUS)
818 & readl(sock->addr + SOCK_MMCSD_CONFIG),
819 sock->addr + SOCK_MMCSD_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700820 }
821
822 if (ios->clock) {
823 clk_div1 = 20000000 / ios->clock;
824 if (!clk_div1)
825 clk_div1 = 1;
826
827 clk_div2 = 24000000 / ios->clock;
828 if (!clk_div2)
829 clk_div2 = 1;
830
831 if ((20000000 / clk_div1) > ios->clock)
832 clk_div1++;
833 if ((24000000 / clk_div2) > ios->clock)
834 clk_div2++;
835 if ((20000000 / clk_div1) > (24000000 / clk_div2)) {
836 host->clk_freq = 20000000;
837 host->clk_div = clk_div1;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100838 writel((~TIFM_CTRL_FAST_CLK)
839 & readl(sock->addr + SOCK_CONTROL),
840 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700841 } else {
842 host->clk_freq = 24000000;
843 host->clk_div = clk_div2;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100844 writel(TIFM_CTRL_FAST_CLK
845 | readl(sock->addr + SOCK_CONTROL),
846 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700847 }
848 } else {
849 host->clk_div = 0;
850 }
851 host->clk_div &= TIFM_MMCSD_CLKMASK;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100852 writel(host->clk_div
853 | ((~TIFM_MMCSD_CLKMASK)
854 & readl(sock->addr + SOCK_MMCSD_CONFIG)),
855 sock->addr + SOCK_MMCSD_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700856
Alex Dubov0007d482007-04-12 16:59:23 +1000857 host->open_drain = (ios->bus_mode == MMC_BUSMODE_OPENDRAIN);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700858
859 /* chip_select : maybe later */
860 //vdd
861 //power is set before probe / after remove
Alex Dubov4020f2d2006-10-04 02:15:37 -0700862
863 spin_unlock_irqrestore(&sock->lock, flags);
864}
865
866static int tifm_sd_ro(struct mmc_host *mmc)
867{
Alex Dubov0007d482007-04-12 16:59:23 +1000868 int rc = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700869 struct tifm_sd *host = mmc_priv(mmc);
870 struct tifm_dev *sock = host->dev;
871 unsigned long flags;
872
873 spin_lock_irqsave(&sock->lock, flags);
Alex Dubov0007d482007-04-12 16:59:23 +1000874 if (TIFM_MMCSD_CARD_RO & readl(sock->addr + SOCK_PRESENT_STATE))
875 rc = 1;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700876 spin_unlock_irqrestore(&sock->lock, flags);
877 return rc;
878}
879
Alex Dubovdfef26d2007-04-12 16:59:24 +1000880static const struct mmc_host_ops tifm_sd_ops = {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700881 .request = tifm_sd_request,
882 .set_ios = tifm_sd_ios,
883 .get_ro = tifm_sd_ro
884};
885
Alex Dubov8e02f852006-12-08 16:50:51 +1100886static int tifm_sd_initialize_host(struct tifm_sd *host)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700887{
Alex Dubov8e02f852006-12-08 16:50:51 +1100888 int rc;
889 unsigned int host_status = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700890 struct tifm_dev *sock = host->dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700891
Alex Dubov8e02f852006-12-08 16:50:51 +1100892 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
893 mmiowb();
894 host->clk_div = 61;
895 host->clk_freq = 20000000;
896 writel(TIFM_MMCSD_RESET, sock->addr + SOCK_MMCSD_SYSTEM_CONTROL);
897 writel(host->clk_div | TIFM_MMCSD_POWER,
898 sock->addr + SOCK_MMCSD_CONFIG);
899
900 /* wait up to 0.51 sec for reset */
Alex Dubov5897d652007-04-12 17:05:24 +1000901 for (rc = 32; rc <= 256; rc <<= 1) {
Alex Dubov8e02f852006-12-08 16:50:51 +1100902 if (1 & readl(sock->addr + SOCK_MMCSD_SYSTEM_STATUS)) {
903 rc = 0;
904 break;
905 }
906 msleep(rc);
907 }
908
909 if (rc) {
Alex Dubov5897d652007-04-12 17:05:24 +1000910 printk(KERN_ERR "%s : controller failed to reset\n",
911 sock->dev.bus_id);
Alex Dubov8e02f852006-12-08 16:50:51 +1100912 return -ENODEV;
913 }
914
915 writel(0, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
916 writel(host->clk_div | TIFM_MMCSD_POWER,
917 sock->addr + SOCK_MMCSD_CONFIG);
918 writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
919
920 // command timeout fixed to 64 clocks for now
921 writel(64, sock->addr + SOCK_MMCSD_COMMAND_TO);
922 writel(TIFM_MMCSD_INAB, sock->addr + SOCK_MMCSD_COMMAND);
923
Alex Dubov5897d652007-04-12 17:05:24 +1000924 for (rc = 16; rc <= 64; rc <<= 1) {
Alex Dubov8e02f852006-12-08 16:50:51 +1100925 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
926 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
927 if (!(host_status & TIFM_MMCSD_ERRMASK)
928 && (host_status & TIFM_MMCSD_EOC)) {
929 rc = 0;
930 break;
931 }
932 msleep(rc);
933 }
934
935 if (rc) {
Alex Dubov5897d652007-04-12 17:05:24 +1000936 printk(KERN_ERR
937 "%s : card not ready - probe failed on initialization\n",
938 sock->dev.bus_id);
Alex Dubov8e02f852006-12-08 16:50:51 +1100939 return -ENODEV;
940 }
941
Alex Dubov5897d652007-04-12 17:05:24 +1000942 writel(TIFM_MMCSD_CERR | TIFM_MMCSD_BRS | TIFM_MMCSD_EOC
943 | TIFM_MMCSD_ERRMASK,
Alex Dubov8e02f852006-12-08 16:50:51 +1100944 sock->addr + SOCK_MMCSD_INT_ENABLE);
945 mmiowb();
946
947 return 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700948}
949
950static int tifm_sd_probe(struct tifm_dev *sock)
951{
952 struct mmc_host *mmc;
953 struct tifm_sd *host;
954 int rc = -EIO;
955
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100956 if (!(TIFM_SOCK_STATE_OCCUPIED
957 & readl(sock->addr + SOCK_PRESENT_STATE))) {
Alex Dubov91f8d012007-04-12 17:05:26 +1000958 printk(KERN_WARNING "%s : card gone, unexpectedly\n",
959 sock->dev.bus_id);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700960 return rc;
961 }
962
963 mmc = mmc_alloc_host(sizeof(struct tifm_sd), &sock->dev);
964 if (!mmc)
965 return -ENOMEM;
966
967 host = mmc_priv(mmc);
Alex Dubov0007d482007-04-12 16:59:23 +1000968 host->no_dma = no_dma;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700969 tifm_set_drvdata(sock, mmc);
Alex Dubov8e02f852006-12-08 16:50:51 +1100970 host->dev = sock;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700971 host->timeout_jiffies = msecs_to_jiffies(1000);
972
Alex Dubovdfef26d2007-04-12 16:59:24 +1000973 tasklet_init(&host->finish_tasklet, tifm_sd_end_cmd,
Alex Dubov8e02f852006-12-08 16:50:51 +1100974 (unsigned long)host);
975 setup_timer(&host->timer, tifm_sd_abort, (unsigned long)host);
976
Alex Dubov4020f2d2006-10-04 02:15:37 -0700977 mmc->ops = &tifm_sd_ops;
978 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Alex Dubov8e02f852006-12-08 16:50:51 +1100979 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700980 mmc->f_min = 20000000 / 60;
981 mmc->f_max = 24000000;
Alex Dubov13cdf482007-04-12 17:05:25 +1000982
983 mmc->max_blk_count = 2048;
984 mmc->max_hw_segs = mmc->max_blk_count;
985 mmc->max_blk_size = min(TIFM_MMCSD_MAX_BLOCK_SIZE, PAGE_SIZE);
986 mmc->max_seg_size = mmc->max_blk_count * mmc->max_blk_size;
987 mmc->max_req_size = mmc->max_seg_size;
988 mmc->max_phys_segs = mmc->max_hw_segs;
989
Alex Dubov4552f0c2007-04-12 16:59:12 +1000990 sock->card_event = tifm_sd_card_event;
991 sock->data_event = tifm_sd_data_event;
Alex Dubov8e02f852006-12-08 16:50:51 +1100992 rc = tifm_sd_initialize_host(host);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700993
Alex Dubov8e02f852006-12-08 16:50:51 +1100994 if (!rc)
995 rc = mmc_add_host(mmc);
Alex Dubov91f8d012007-04-12 17:05:26 +1000996 if (!rc)
997 return 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700998
Alex Dubov8e02f852006-12-08 16:50:51 +1100999 mmc_free_host(mmc);
Alex Dubov4020f2d2006-10-04 02:15:37 -07001000 return rc;
1001}
1002
1003static void tifm_sd_remove(struct tifm_dev *sock)
1004{
1005 struct mmc_host *mmc = tifm_get_drvdata(sock);
1006 struct tifm_sd *host = mmc_priv(mmc);
Alex Dubovb039d4a2007-04-12 16:59:21 +10001007 unsigned long flags;
Alex Dubov4020f2d2006-10-04 02:15:37 -07001008
Alex Dubovb039d4a2007-04-12 16:59:21 +10001009 spin_lock_irqsave(&sock->lock, flags);
Alex Dubov0007d482007-04-12 16:59:23 +10001010 host->eject = 1;
Alex Dubov592d3722007-04-12 16:59:22 +10001011 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
1012 mmiowb();
1013 spin_unlock_irqrestore(&sock->lock, flags);
1014
1015 tasklet_kill(&host->finish_tasklet);
1016
1017 spin_lock_irqsave(&sock->lock, flags);
Alex Dubovb039d4a2007-04-12 16:59:21 +10001018 if (host->req) {
1019 writel(TIFM_FIFO_INT_SETALL,
1020 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
1021 writel(0, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
Pierre Ossman17b04292007-07-22 22:18:46 +02001022 host->req->cmd->error = -ENOMEDIUM;
Alex Dubov592d3722007-04-12 16:59:22 +10001023 if (host->req->stop)
Pierre Ossman17b04292007-07-22 22:18:46 +02001024 host->req->stop->error = -ENOMEDIUM;
Alex Dubovb039d4a2007-04-12 16:59:21 +10001025 tasklet_schedule(&host->finish_tasklet);
1026 }
1027 spin_unlock_irqrestore(&sock->lock, flags);
Alex Dubov8e02f852006-12-08 16:50:51 +11001028 mmc_remove_host(mmc);
Alex Dubov592d3722007-04-12 16:59:22 +10001029 dev_dbg(&sock->dev, "after remove\n");
Alex Dubov4020f2d2006-10-04 02:15:37 -07001030
Alex Dubov4020f2d2006-10-04 02:15:37 -07001031 mmc_free_host(mmc);
1032}
1033
Alex Dubovdba4acc2006-12-11 01:55:38 +11001034#ifdef CONFIG_PM
1035
1036static int tifm_sd_suspend(struct tifm_dev *sock, pm_message_t state)
1037{
Alex Dubov055b8222007-05-01 20:14:55 -07001038 return mmc_suspend_host(tifm_get_drvdata(sock), state);
Alex Dubovdba4acc2006-12-11 01:55:38 +11001039}
1040
1041static int tifm_sd_resume(struct tifm_dev *sock)
1042{
1043 struct mmc_host *mmc = tifm_get_drvdata(sock);
1044 struct tifm_sd *host = mmc_priv(mmc);
Alex Dubov5897d652007-04-12 17:05:24 +10001045 int rc;
Alex Dubovdba4acc2006-12-11 01:55:38 +11001046
Alex Dubov5897d652007-04-12 17:05:24 +10001047 rc = tifm_sd_initialize_host(host);
1048 dev_dbg(&sock->dev, "resume initialize %d\n", rc);
1049
1050 if (rc)
1051 host->eject = 1;
1052 else
1053 rc = mmc_resume_host(mmc);
1054
1055 return rc;
Alex Dubovdba4acc2006-12-11 01:55:38 +11001056}
1057
1058#else
1059
1060#define tifm_sd_suspend NULL
1061#define tifm_sd_resume NULL
1062
1063#endif /* CONFIG_PM */
1064
Alex Dubove23f2b82007-04-12 16:59:14 +10001065static struct tifm_device_id tifm_sd_id_tbl[] = {
1066 { TIFM_TYPE_SD }, { }
Alex Dubov4020f2d2006-10-04 02:15:37 -07001067};
1068
1069static struct tifm_driver tifm_sd_driver = {
1070 .driver = {
1071 .name = DRIVER_NAME,
1072 .owner = THIS_MODULE
1073 },
1074 .id_table = tifm_sd_id_tbl,
1075 .probe = tifm_sd_probe,
Alex Dubovdba4acc2006-12-11 01:55:38 +11001076 .remove = tifm_sd_remove,
1077 .suspend = tifm_sd_suspend,
1078 .resume = tifm_sd_resume
Alex Dubov4020f2d2006-10-04 02:15:37 -07001079};
1080
1081static int __init tifm_sd_init(void)
1082{
1083 return tifm_register_driver(&tifm_sd_driver);
1084}
1085
1086static void __exit tifm_sd_exit(void)
1087{
1088 tifm_unregister_driver(&tifm_sd_driver);
1089}
1090
1091MODULE_AUTHOR("Alex Dubov");
1092MODULE_DESCRIPTION("TI FlashMedia SD driver");
1093MODULE_LICENSE("GPL");
1094MODULE_DEVICE_TABLE(tifm, tifm_sd_id_tbl);
1095MODULE_VERSION(DRIVER_VERSION);
1096
1097module_init(tifm_sd_init);
1098module_exit(tifm_sd_exit);