blob: 37fe0c3ecb854c06f72fce7d03f8949ebcf97c12 [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>
Al Viro2099c992006-10-24 11:17:06 +010017#include <asm/io.h>
Alex Dubov4020f2d2006-10-04 02:15:37 -070018
19#define DRIVER_NAME "tifm_sd"
Alex Dubov12893352006-12-08 16:50:47 +110020#define DRIVER_VERSION "0.7"
Alex Dubov4020f2d2006-10-04 02:15:37 -070021
22static int no_dma = 0;
23static int fixed_timeout = 0;
24module_param(no_dma, bool, 0644);
25module_param(fixed_timeout, bool, 0644);
26
27/* Constants here are mostly from OMAP5912 datasheet */
28#define TIFM_MMCSD_RESET 0x0002
29#define TIFM_MMCSD_CLKMASK 0x03ff
30#define TIFM_MMCSD_POWER 0x0800
31#define TIFM_MMCSD_4BBUS 0x8000
32#define TIFM_MMCSD_RXDE 0x8000 /* rx dma enable */
33#define TIFM_MMCSD_TXDE 0x0080 /* tx dma enable */
34#define TIFM_MMCSD_BUFINT 0x0c00 /* set bits: AE, AF */
35#define TIFM_MMCSD_DPE 0x0020 /* data timeout counted in kilocycles */
36#define TIFM_MMCSD_INAB 0x0080 /* abort / initialize command */
37#define TIFM_MMCSD_READ 0x8000
38
39#define TIFM_MMCSD_DATAMASK 0x001d /* set bits: EOFB, BRS, CB, EOC */
40#define TIFM_MMCSD_ERRMASK 0x41e0 /* set bits: CERR, CCRC, CTO, DCRC, DTO */
41#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
53#define TIFM_MMCSD_FIFO_SIZE 0x0020
54
55#define TIFM_MMCSD_RSP_R0 0x0000
56#define TIFM_MMCSD_RSP_R1 0x0100
57#define TIFM_MMCSD_RSP_R2 0x0200
58#define TIFM_MMCSD_RSP_R3 0x0300
59#define TIFM_MMCSD_RSP_R4 0x0400
60#define TIFM_MMCSD_RSP_R5 0x0500
61#define TIFM_MMCSD_RSP_R6 0x0600
62
63#define TIFM_MMCSD_RSP_BUSY 0x0800
64
65#define TIFM_MMCSD_CMD_BC 0x0000
66#define TIFM_MMCSD_CMD_BCR 0x1000
67#define TIFM_MMCSD_CMD_AC 0x2000
68#define TIFM_MMCSD_CMD_ADTC 0x3000
69
70typedef enum {
71 IDLE = 0,
72 CMD, /* main command ended */
73 BRS, /* block transfer finished */
74 SCMD, /* stop command ended */
75 CARD, /* card left busy state */
76 FIFO, /* FIFO operation completed (uncertain) */
77 READY
78} card_state_t;
79
80enum {
81 FIFO_RDY = 0x0001, /* hardware dependent value */
Alex Dubov4020f2d2006-10-04 02:15:37 -070082 EJECT = 0x0004,
83 EJECT_DONE = 0x0008,
84 CARD_BUSY = 0x0010,
85 OPENDRAIN = 0x0040, /* hardware dependent value */
86 CARD_EVENT = 0x0100, /* hardware dependent value */
87 CARD_RO = 0x0200, /* hardware dependent value */
88 FIFO_EVENT = 0x10000 }; /* hardware dependent value */
89
90struct tifm_sd {
91 struct tifm_dev *dev;
92
93 unsigned int flags;
94 card_state_t state;
95 unsigned int clk_freq;
96 unsigned int clk_div;
Alex Dubov0803dd02006-12-08 16:50:49 +110097 unsigned long timeout_jiffies;
Alex Dubov4020f2d2006-10-04 02:15:37 -070098
Alex Dubov8e02f852006-12-08 16:50:51 +110099 struct tasklet_struct finish_tasklet;
Alex Dubov0803dd02006-12-08 16:50:49 +1100100 struct timer_list timer;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700101 struct mmc_request *req;
Alex Dubov8e02f852006-12-08 16:50:51 +1100102 wait_queue_head_t notify;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700103
104 size_t written_blocks;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700105 size_t buffer_size;
106 size_t buffer_pos;
107
108};
109
Alex Dubov255ef222006-12-08 16:50:48 +1100110static char* tifm_sd_kmap_atomic(struct mmc_data *data)
111{
112 return kmap_atomic(data->sg->page, KM_BIO_SRC_IRQ) + data->sg->offset;
113}
114
115static void tifm_sd_kunmap_atomic(char *buffer, struct mmc_data *data)
116{
117 kunmap_atomic(buffer - data->sg->offset, KM_BIO_SRC_IRQ);
118}
119
Alex Dubov4020f2d2006-10-04 02:15:37 -0700120static int tifm_sd_transfer_data(struct tifm_dev *sock, struct tifm_sd *host,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100121 unsigned int host_status)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700122{
123 struct mmc_command *cmd = host->req->cmd;
124 unsigned int t_val = 0, cnt = 0;
Alex Dubov255ef222006-12-08 16:50:48 +1100125 char *buffer;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700126
127 if (host_status & TIFM_MMCSD_BRS) {
128 /* in non-dma rx mode BRS fires when fifo is still not empty */
Alex Dubov255ef222006-12-08 16:50:48 +1100129 if (no_dma && (cmd->data->flags & MMC_DATA_READ)) {
130 buffer = tifm_sd_kmap_atomic(host->req->data);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700131 while (host->buffer_size > host->buffer_pos) {
132 t_val = readl(sock->addr + SOCK_MMCSD_DATA);
Alex Dubov255ef222006-12-08 16:50:48 +1100133 buffer[host->buffer_pos++] = t_val & 0xff;
134 buffer[host->buffer_pos++] =
Alex Dubov4020f2d2006-10-04 02:15:37 -0700135 (t_val >> 8) & 0xff;
136 }
Alex Dubov255ef222006-12-08 16:50:48 +1100137 tifm_sd_kunmap_atomic(buffer, host->req->data);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700138 }
139 return 1;
Alex Dubov255ef222006-12-08 16:50:48 +1100140 } else if (no_dma) {
141 buffer = tifm_sd_kmap_atomic(host->req->data);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700142 if ((cmd->data->flags & MMC_DATA_READ) &&
143 (host_status & TIFM_MMCSD_AF)) {
144 for (cnt = 0; cnt < TIFM_MMCSD_FIFO_SIZE; cnt++) {
145 t_val = readl(sock->addr + SOCK_MMCSD_DATA);
146 if (host->buffer_size > host->buffer_pos) {
Alex Dubov255ef222006-12-08 16:50:48 +1100147 buffer[host->buffer_pos++] =
Alex Dubov4020f2d2006-10-04 02:15:37 -0700148 t_val & 0xff;
Alex Dubov255ef222006-12-08 16:50:48 +1100149 buffer[host->buffer_pos++] =
Alex Dubov4020f2d2006-10-04 02:15:37 -0700150 (t_val >> 8) & 0xff;
151 }
152 }
153 } else if ((cmd->data->flags & MMC_DATA_WRITE)
154 && (host_status & TIFM_MMCSD_AE)) {
155 for (cnt = 0; cnt < TIFM_MMCSD_FIFO_SIZE; cnt++) {
156 if (host->buffer_size > host->buffer_pos) {
Alex Dubov255ef222006-12-08 16:50:48 +1100157 t_val = buffer[host->buffer_pos++]
158 & 0x00ff;
159 t_val |= ((buffer[host->buffer_pos++])
160 << 8) & 0xff00;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700161 writel(t_val,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100162 sock->addr + SOCK_MMCSD_DATA);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700163 }
164 }
165 }
Alex Dubov255ef222006-12-08 16:50:48 +1100166 tifm_sd_kunmap_atomic(buffer, host->req->data);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700167 }
168 return 0;
169}
170
171static unsigned int tifm_sd_op_flags(struct mmc_command *cmd)
172{
173 unsigned int rc = 0;
174
175 switch (mmc_resp_type(cmd)) {
176 case MMC_RSP_NONE:
177 rc |= TIFM_MMCSD_RSP_R0;
178 break;
179 case MMC_RSP_R1B:
180 rc |= TIFM_MMCSD_RSP_BUSY; // deliberate fall-through
181 case MMC_RSP_R1:
182 rc |= TIFM_MMCSD_RSP_R1;
183 break;
184 case MMC_RSP_R2:
185 rc |= TIFM_MMCSD_RSP_R2;
186 break;
187 case MMC_RSP_R3:
188 rc |= TIFM_MMCSD_RSP_R3;
189 break;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700190 default:
191 BUG();
192 }
193
194 switch (mmc_cmd_type(cmd)) {
195 case MMC_CMD_BC:
196 rc |= TIFM_MMCSD_CMD_BC;
197 break;
198 case MMC_CMD_BCR:
199 rc |= TIFM_MMCSD_CMD_BCR;
200 break;
201 case MMC_CMD_AC:
202 rc |= TIFM_MMCSD_CMD_AC;
203 break;
204 case MMC_CMD_ADTC:
205 rc |= TIFM_MMCSD_CMD_ADTC;
206 break;
207 default:
208 BUG();
209 }
210 return rc;
211}
212
213static void tifm_sd_exec(struct tifm_sd *host, struct mmc_command *cmd)
214{
215 struct tifm_dev *sock = host->dev;
216 unsigned int cmd_mask = tifm_sd_op_flags(cmd) |
217 (host->flags & OPENDRAIN);
218
219 if (cmd->data && (cmd->data->flags & MMC_DATA_READ))
220 cmd_mask |= TIFM_MMCSD_READ;
221
222 dev_dbg(&sock->dev, "executing opcode 0x%x, arg: 0x%x, mask: 0x%x\n",
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100223 cmd->opcode, cmd->arg, cmd_mask);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700224
225 writel((cmd->arg >> 16) & 0xffff, sock->addr + SOCK_MMCSD_ARG_HIGH);
226 writel(cmd->arg & 0xffff, sock->addr + SOCK_MMCSD_ARG_LOW);
227 writel(cmd->opcode | cmd_mask, sock->addr + SOCK_MMCSD_COMMAND);
228}
229
230static void tifm_sd_fetch_resp(struct mmc_command *cmd, struct tifm_dev *sock)
231{
232 cmd->resp[0] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x1c) << 16)
233 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x18);
234 cmd->resp[1] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x14) << 16)
235 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x10);
236 cmd->resp[2] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x0c) << 16)
237 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x08);
238 cmd->resp[3] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x04) << 16)
239 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x00);
240}
241
242static void tifm_sd_process_cmd(struct tifm_dev *sock, struct tifm_sd *host,
243 unsigned int host_status)
244{
245 struct mmc_command *cmd = host->req->cmd;
246
247change_state:
248 switch (host->state) {
249 case IDLE:
250 return;
251 case CMD:
252 if (host_status & TIFM_MMCSD_EOC) {
253 tifm_sd_fetch_resp(cmd, sock);
254 if (cmd->data) {
255 host->state = BRS;
Alex Dubov12893352006-12-08 16:50:47 +1100256 } else {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700257 host->state = READY;
Alex Dubov12893352006-12-08 16:50:47 +1100258 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700259 goto change_state;
260 }
261 break;
262 case BRS:
263 if (tifm_sd_transfer_data(sock, host, host_status)) {
Alex Dubov12893352006-12-08 16:50:47 +1100264 if (cmd->data->flags & MMC_DATA_WRITE) {
265 host->state = CARD;
266 } else {
267 if (no_dma) {
268 if (host->req->stop) {
269 tifm_sd_exec(host, host->req->stop);
270 host->state = SCMD;
271 } else {
272 host->state = READY;
273 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700274 } else {
Alex Dubov12893352006-12-08 16:50:47 +1100275 host->state = FIFO;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700276 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700277 }
Alex Dubov12893352006-12-08 16:50:47 +1100278 goto change_state;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700279 }
280 break;
281 case SCMD:
282 if (host_status & TIFM_MMCSD_EOC) {
283 tifm_sd_fetch_resp(host->req->stop, sock);
Alex Dubov12893352006-12-08 16:50:47 +1100284 host->state = READY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700285 goto change_state;
286 }
287 break;
288 case CARD:
Alex Dubov12893352006-12-08 16:50:47 +1100289 dev_dbg(&sock->dev, "waiting for CARD, have %zd blocks\n",
290 host->written_blocks);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700291 if (!(host->flags & CARD_BUSY)
292 && (host->written_blocks == cmd->data->blocks)) {
Alex Dubov12893352006-12-08 16:50:47 +1100293 if (no_dma) {
294 if (host->req->stop) {
295 tifm_sd_exec(host, host->req->stop);
296 host->state = SCMD;
297 } else {
298 host->state = READY;
299 }
300 } else {
301 host->state = FIFO;
302 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700303 goto change_state;
304 }
305 break;
306 case FIFO:
307 if (host->flags & FIFO_RDY) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700308 host->flags &= ~FIFO_RDY;
Alex Dubov12893352006-12-08 16:50:47 +1100309 if (host->req->stop) {
310 tifm_sd_exec(host, host->req->stop);
311 host->state = SCMD;
312 } else {
313 host->state = READY;
314 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700315 goto change_state;
316 }
317 break;
318 case READY:
Alex Dubov8e02f852006-12-08 16:50:51 +1100319 tasklet_schedule(&host->finish_tasklet);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700320 return;
321 }
322
Alex Dubov4020f2d2006-10-04 02:15:37 -0700323}
324
325/* Called from interrupt handler */
326static unsigned int tifm_sd_signal_irq(struct tifm_dev *sock,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100327 unsigned int sock_irq_status)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700328{
329 struct tifm_sd *host;
330 unsigned int host_status = 0, fifo_status = 0;
331 int error_code = 0;
332
333 spin_lock(&sock->lock);
334 host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock));
Alex Dubov4020f2d2006-10-04 02:15:37 -0700335
336 if (sock_irq_status & FIFO_EVENT) {
337 fifo_status = readl(sock->addr + SOCK_DMA_FIFO_STATUS);
338 writel(fifo_status, sock->addr + SOCK_DMA_FIFO_STATUS);
339
340 host->flags |= fifo_status & FIFO_RDY;
341 }
342
343 if (sock_irq_status & CARD_EVENT) {
344 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
345 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
346
Alex Dubov4020f2d2006-10-04 02:15:37 -0700347 if (!host->req)
348 goto done;
349
350 if (host_status & TIFM_MMCSD_ERRMASK) {
351 if (host_status & TIFM_MMCSD_CERR)
352 error_code = MMC_ERR_FAILED;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100353 else if (host_status
354 & (TIFM_MMCSD_CTO | TIFM_MMCSD_DTO))
Alex Dubov4020f2d2006-10-04 02:15:37 -0700355 error_code = MMC_ERR_TIMEOUT;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100356 else if (host_status
357 & (TIFM_MMCSD_CCRC | TIFM_MMCSD_DCRC))
Alex Dubov4020f2d2006-10-04 02:15:37 -0700358 error_code = MMC_ERR_BADCRC;
359
360 writel(TIFM_FIFO_INT_SETALL,
361 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
362 writel(TIFM_DMA_RESET, sock->addr + SOCK_DMA_CONTROL);
363
364 if (host->req->stop) {
365 if (host->state == SCMD) {
366 host->req->stop->error = error_code;
Alex Dubov12893352006-12-08 16:50:47 +1100367 } else if (host->state == BRS
368 || host->state == CARD
369 || host->state == FIFO) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700370 host->req->cmd->error = error_code;
371 tifm_sd_exec(host, host->req->stop);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700372 host->state = SCMD;
373 goto done;
374 } else {
375 host->req->cmd->error = error_code;
376 }
377 } else {
378 host->req->cmd->error = error_code;
379 }
380 host->state = READY;
381 }
382
383 if (host_status & TIFM_MMCSD_CB)
384 host->flags |= CARD_BUSY;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100385 if ((host_status & TIFM_MMCSD_EOFB)
386 && (host->flags & CARD_BUSY)) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700387 host->written_blocks++;
388 host->flags &= ~CARD_BUSY;
389 }
390 }
391
392 if (host->req)
393 tifm_sd_process_cmd(sock, host, host_status);
394done:
395 dev_dbg(&sock->dev, "host_status %x, fifo_status %x\n",
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100396 host_status, fifo_status);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700397 spin_unlock(&sock->lock);
398 return sock_irq_status;
399}
400
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100401static void tifm_sd_prepare_data(struct tifm_sd *host, struct mmc_command *cmd)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700402{
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100403 struct tifm_dev *sock = host->dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700404 unsigned int dest_cnt;
405
406 /* DMA style IO */
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100407 dev_dbg(&sock->dev, "setting dma for %d blocks\n",
408 cmd->data->blocks);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700409 writel(TIFM_FIFO_INT_SETALL,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100410 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
David Howellsf0d1b0b2006-12-08 02:37:49 -0800411 writel(ilog2(cmd->data->blksz) - 2,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100412 sock->addr + SOCK_FIFO_PAGE_SIZE);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700413 writel(TIFM_FIFO_ENABLE, sock->addr + SOCK_FIFO_CONTROL);
414 writel(TIFM_FIFO_INTMASK, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
415
416 dest_cnt = (cmd->data->blocks) << 8;
417
418 writel(sg_dma_address(cmd->data->sg), sock->addr + SOCK_DMA_ADDRESS);
419
420 writel(cmd->data->blocks - 1, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
421 writel(cmd->data->blksz - 1, sock->addr + SOCK_MMCSD_BLOCK_LEN);
422
423 if (cmd->data->flags & MMC_DATA_WRITE) {
424 writel(TIFM_MMCSD_TXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
425 writel(dest_cnt | TIFM_DMA_TX | TIFM_DMA_EN,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100426 sock->addr + SOCK_DMA_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700427 } else {
428 writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
429 writel(dest_cnt | TIFM_DMA_EN, sock->addr + SOCK_DMA_CONTROL);
430 }
431}
432
433static void tifm_sd_set_data_timeout(struct tifm_sd *host,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100434 struct mmc_data *data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700435{
436 struct tifm_dev *sock = host->dev;
437 unsigned int data_timeout = data->timeout_clks;
438
439 if (fixed_timeout)
440 return;
441
442 data_timeout += data->timeout_ns /
Alex Dubov83d420b2006-12-08 16:50:50 +1100443 ((1000000000UL / host->clk_freq) * host->clk_div);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700444
445 if (data_timeout < 0xffff) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700446 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
Alex Dubov83d420b2006-12-08 16:50:50 +1100447 writel((~TIFM_MMCSD_DPE)
448 & readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
449 sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700450 } else {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700451 data_timeout = (data_timeout >> 10) + 1;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100452 if (data_timeout > 0xffff)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700453 data_timeout = 0; /* set to unlimited */
454 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
Alex Dubov83d420b2006-12-08 16:50:50 +1100455 writel(TIFM_MMCSD_DPE
456 | readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
457 sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700458 }
459}
460
461static void tifm_sd_request(struct mmc_host *mmc, struct mmc_request *mrq)
462{
463 struct tifm_sd *host = mmc_priv(mmc);
464 struct tifm_dev *sock = host->dev;
465 unsigned long flags;
466 int sg_count = 0;
467 struct mmc_data *r_data = mrq->cmd->data;
468
469 spin_lock_irqsave(&sock->lock, flags);
470 if (host->flags & EJECT) {
471 spin_unlock_irqrestore(&sock->lock, flags);
472 goto err_out;
473 }
474
475 if (host->req) {
476 printk(KERN_ERR DRIVER_NAME ": unfinished request detected\n");
477 spin_unlock_irqrestore(&sock->lock, flags);
478 goto err_out;
479 }
480
481 if (r_data) {
482 tifm_sd_set_data_timeout(host, r_data);
483
484 sg_count = tifm_map_sg(sock, r_data->sg, r_data->sg_len,
485 mrq->cmd->flags & MMC_DATA_WRITE
486 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
487 if (sg_count != 1) {
488 printk(KERN_ERR DRIVER_NAME
489 ": scatterlist map failed\n");
490 spin_unlock_irqrestore(&sock->lock, flags);
491 goto err_out;
492 }
493
494 host->written_blocks = 0;
495 host->flags &= ~CARD_BUSY;
496 tifm_sd_prepare_data(host, mrq->cmd);
497 }
498
499 host->req = mrq;
Alex Dubov0803dd02006-12-08 16:50:49 +1100500 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700501 host->state = CMD;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700502 writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100503 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700504 tifm_sd_exec(host, mrq->cmd);
505 spin_unlock_irqrestore(&sock->lock, flags);
506 return;
507
508err_out:
509 if (sg_count > 0)
510 tifm_unmap_sg(sock, r_data->sg, r_data->sg_len,
511 (r_data->flags & MMC_DATA_WRITE)
512 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
513
514 mrq->cmd->error = MMC_ERR_TIMEOUT;
515 mmc_request_done(mmc, mrq);
516}
517
Alex Dubov8e02f852006-12-08 16:50:51 +1100518static void tifm_sd_end_cmd(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700519{
Alex Dubov8e02f852006-12-08 16:50:51 +1100520 struct tifm_sd *host = (struct tifm_sd*)data;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700521 struct tifm_dev *sock = host->dev;
522 struct mmc_host *mmc = tifm_get_drvdata(sock);
523 struct mmc_request *mrq;
Al Viroe069d792006-10-09 20:29:43 +0100524 struct mmc_data *r_data = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700525 unsigned long flags;
526
527 spin_lock_irqsave(&sock->lock, flags);
528
Alex Dubov0803dd02006-12-08 16:50:49 +1100529 del_timer(&host->timer);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700530 mrq = host->req;
Al Viroe069d792006-10-09 20:29:43 +0100531 host->req = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700532 host->state = IDLE;
533
534 if (!mrq) {
535 printk(KERN_ERR DRIVER_NAME ": no request to complete?\n");
536 spin_unlock_irqrestore(&sock->lock, flags);
537 return;
538 }
539
540 r_data = mrq->cmd->data;
541 if (r_data) {
542 if (r_data->flags & MMC_DATA_WRITE) {
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100543 r_data->bytes_xfered = host->written_blocks
544 * r_data->blksz;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700545 } else {
546 r_data->bytes_xfered = r_data->blocks -
547 readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS) - 1;
548 r_data->bytes_xfered *= r_data->blksz;
549 r_data->bytes_xfered += r_data->blksz -
550 readl(sock->addr + SOCK_MMCSD_BLOCK_LEN) + 1;
551 }
552 tifm_unmap_sg(sock, r_data->sg, r_data->sg_len,
553 (r_data->flags & MMC_DATA_WRITE)
554 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
555 }
556
557 writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100558 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700559
560 spin_unlock_irqrestore(&sock->lock, flags);
561 mmc_request_done(mmc, mrq);
562}
563
564static void tifm_sd_request_nodma(struct mmc_host *mmc, struct mmc_request *mrq)
565{
566 struct tifm_sd *host = mmc_priv(mmc);
567 struct tifm_dev *sock = host->dev;
568 unsigned long flags;
569 struct mmc_data *r_data = mrq->cmd->data;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700570
571 spin_lock_irqsave(&sock->lock, flags);
572 if (host->flags & EJECT) {
573 spin_unlock_irqrestore(&sock->lock, flags);
574 goto err_out;
575 }
576
577 if (host->req) {
578 printk(KERN_ERR DRIVER_NAME ": unfinished request detected\n");
579 spin_unlock_irqrestore(&sock->lock, flags);
580 goto err_out;
581 }
582
583 if (r_data) {
584 tifm_sd_set_data_timeout(host, r_data);
585
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100586 host->buffer_size = mrq->cmd->data->blocks
587 * mrq->cmd->data->blksz;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700588
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100589 writel(TIFM_MMCSD_BUFINT
590 | readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
Alex Dubov4020f2d2006-10-04 02:15:37 -0700591 sock->addr + SOCK_MMCSD_INT_ENABLE);
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100592 writel(((TIFM_MMCSD_FIFO_SIZE - 1) << 8)
593 | (TIFM_MMCSD_FIFO_SIZE - 1),
Alex Dubov4020f2d2006-10-04 02:15:37 -0700594 sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
595
596 host->written_blocks = 0;
597 host->flags &= ~CARD_BUSY;
598 host->buffer_pos = 0;
599 writel(r_data->blocks - 1, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
600 writel(r_data->blksz - 1, sock->addr + SOCK_MMCSD_BLOCK_LEN);
601 }
602
603 host->req = mrq;
Alex Dubov0803dd02006-12-08 16:50:49 +1100604 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700605 host->state = CMD;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700606 writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100607 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700608 tifm_sd_exec(host, mrq->cmd);
609 spin_unlock_irqrestore(&sock->lock, flags);
610 return;
611
612err_out:
Alex Dubov4020f2d2006-10-04 02:15:37 -0700613 mrq->cmd->error = MMC_ERR_TIMEOUT;
614 mmc_request_done(mmc, mrq);
615}
616
Alex Dubov8e02f852006-12-08 16:50:51 +1100617static void tifm_sd_end_cmd_nodma(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700618{
Alex Dubov8e02f852006-12-08 16:50:51 +1100619 struct tifm_sd *host = (struct tifm_sd*)data;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700620 struct tifm_dev *sock = host->dev;
621 struct mmc_host *mmc = tifm_get_drvdata(sock);
622 struct mmc_request *mrq;
Al Viroe069d792006-10-09 20:29:43 +0100623 struct mmc_data *r_data = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700624 unsigned long flags;
625
626 spin_lock_irqsave(&sock->lock, flags);
627
Alex Dubov0803dd02006-12-08 16:50:49 +1100628 del_timer(&host->timer);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700629 mrq = host->req;
Al Viroe069d792006-10-09 20:29:43 +0100630 host->req = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700631 host->state = IDLE;
632
633 if (!mrq) {
634 printk(KERN_ERR DRIVER_NAME ": no request to complete?\n");
635 spin_unlock_irqrestore(&sock->lock, flags);
636 return;
637 }
638
639 r_data = mrq->cmd->data;
640 if (r_data) {
641 writel((~TIFM_MMCSD_BUFINT) &
642 readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
643 sock->addr + SOCK_MMCSD_INT_ENABLE);
644
645 if (r_data->flags & MMC_DATA_WRITE) {
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100646 r_data->bytes_xfered = host->written_blocks
647 * r_data->blksz;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700648 } else {
649 r_data->bytes_xfered = r_data->blocks -
650 readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS) - 1;
651 r_data->bytes_xfered *= r_data->blksz;
652 r_data->bytes_xfered += r_data->blksz -
653 readl(sock->addr + SOCK_MMCSD_BLOCK_LEN) + 1;
654 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700655 host->buffer_pos = 0;
656 host->buffer_size = 0;
657 }
658
659 writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100660 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700661
662 spin_unlock_irqrestore(&sock->lock, flags);
663
Alex Dubov4020f2d2006-10-04 02:15:37 -0700664 mmc_request_done(mmc, mrq);
665}
666
Alex Dubov8e02f852006-12-08 16:50:51 +1100667static void tifm_sd_terminate(struct tifm_sd *host)
668{
669 struct tifm_dev *sock = host->dev;
670 unsigned long flags;
671
672 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
673 mmiowb();
674 spin_lock_irqsave(&sock->lock, flags);
675 host->flags |= EJECT;
676 if (host->req) {
677 writel(TIFM_FIFO_INT_SETALL,
678 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
679 writel(0, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
680 tasklet_schedule(&host->finish_tasklet);
681 }
682 spin_unlock_irqrestore(&sock->lock, flags);
683}
684
Alex Dubov0803dd02006-12-08 16:50:49 +1100685static void tifm_sd_abort(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700686{
Alex Dubov8e02f852006-12-08 16:50:51 +1100687 struct tifm_sd *host = (struct tifm_sd*)data;
688
Alex Dubov4020f2d2006-10-04 02:15:37 -0700689 printk(KERN_ERR DRIVER_NAME
Alex Dubov0803dd02006-12-08 16:50:49 +1100690 ": card failed to respond for a long period of time");
Alex Dubov8e02f852006-12-08 16:50:51 +1100691
692 tifm_sd_terminate(host);
693 tifm_eject(host->dev);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700694}
695
696static void tifm_sd_ios(struct mmc_host *mmc, struct mmc_ios *ios)
697{
698 struct tifm_sd *host = mmc_priv(mmc);
699 struct tifm_dev *sock = host->dev;
700 unsigned int clk_div1, clk_div2;
701 unsigned long flags;
702
703 spin_lock_irqsave(&sock->lock, flags);
704
705 dev_dbg(&sock->dev, "Setting bus width %d, power %d\n", ios->bus_width,
706 ios->power_mode);
707 if (ios->bus_width == MMC_BUS_WIDTH_4) {
708 writel(TIFM_MMCSD_4BBUS | readl(sock->addr + SOCK_MMCSD_CONFIG),
709 sock->addr + SOCK_MMCSD_CONFIG);
710 } else {
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100711 writel((~TIFM_MMCSD_4BBUS)
712 & readl(sock->addr + SOCK_MMCSD_CONFIG),
713 sock->addr + SOCK_MMCSD_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700714 }
715
716 if (ios->clock) {
717 clk_div1 = 20000000 / ios->clock;
718 if (!clk_div1)
719 clk_div1 = 1;
720
721 clk_div2 = 24000000 / ios->clock;
722 if (!clk_div2)
723 clk_div2 = 1;
724
725 if ((20000000 / clk_div1) > ios->clock)
726 clk_div1++;
727 if ((24000000 / clk_div2) > ios->clock)
728 clk_div2++;
729 if ((20000000 / clk_div1) > (24000000 / clk_div2)) {
730 host->clk_freq = 20000000;
731 host->clk_div = clk_div1;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100732 writel((~TIFM_CTRL_FAST_CLK)
733 & readl(sock->addr + SOCK_CONTROL),
734 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700735 } else {
736 host->clk_freq = 24000000;
737 host->clk_div = clk_div2;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100738 writel(TIFM_CTRL_FAST_CLK
739 | readl(sock->addr + SOCK_CONTROL),
740 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700741 }
742 } else {
743 host->clk_div = 0;
744 }
745 host->clk_div &= TIFM_MMCSD_CLKMASK;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100746 writel(host->clk_div
747 | ((~TIFM_MMCSD_CLKMASK)
748 & readl(sock->addr + SOCK_MMCSD_CONFIG)),
749 sock->addr + SOCK_MMCSD_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700750
751 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
752 host->flags |= OPENDRAIN;
753 else
754 host->flags &= ~OPENDRAIN;
755
756 /* chip_select : maybe later */
757 //vdd
758 //power is set before probe / after remove
759 //I believe, power_off when already marked for eject is sufficient to
760 // allow removal.
761 if ((host->flags & EJECT) && ios->power_mode == MMC_POWER_OFF) {
762 host->flags |= EJECT_DONE;
Alex Dubov8e02f852006-12-08 16:50:51 +1100763 wake_up_all(&host->notify);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700764 }
765
766 spin_unlock_irqrestore(&sock->lock, flags);
767}
768
769static int tifm_sd_ro(struct mmc_host *mmc)
770{
771 int rc;
772 struct tifm_sd *host = mmc_priv(mmc);
773 struct tifm_dev *sock = host->dev;
774 unsigned long flags;
775
776 spin_lock_irqsave(&sock->lock, flags);
777
778 host->flags |= (CARD_RO & readl(sock->addr + SOCK_PRESENT_STATE));
779 rc = (host->flags & CARD_RO) ? 1 : 0;
780
781 spin_unlock_irqrestore(&sock->lock, flags);
782 return rc;
783}
784
785static struct mmc_host_ops tifm_sd_ops = {
786 .request = tifm_sd_request,
787 .set_ios = tifm_sd_ios,
788 .get_ro = tifm_sd_ro
789};
790
Alex Dubov8e02f852006-12-08 16:50:51 +1100791static int tifm_sd_initialize_host(struct tifm_sd *host)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700792{
Alex Dubov8e02f852006-12-08 16:50:51 +1100793 int rc;
794 unsigned int host_status = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700795 struct tifm_dev *sock = host->dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700796
Alex Dubov8e02f852006-12-08 16:50:51 +1100797 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
798 mmiowb();
799 host->clk_div = 61;
800 host->clk_freq = 20000000;
801 writel(TIFM_MMCSD_RESET, sock->addr + SOCK_MMCSD_SYSTEM_CONTROL);
802 writel(host->clk_div | TIFM_MMCSD_POWER,
803 sock->addr + SOCK_MMCSD_CONFIG);
804
805 /* wait up to 0.51 sec for reset */
806 for (rc = 2; rc <= 256; rc <<= 1) {
807 if (1 & readl(sock->addr + SOCK_MMCSD_SYSTEM_STATUS)) {
808 rc = 0;
809 break;
810 }
811 msleep(rc);
812 }
813
814 if (rc) {
815 printk(KERN_ERR DRIVER_NAME
816 ": controller failed to reset\n");
817 return -ENODEV;
818 }
819
820 writel(0, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
821 writel(host->clk_div | TIFM_MMCSD_POWER,
822 sock->addr + SOCK_MMCSD_CONFIG);
823 writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
824
825 // command timeout fixed to 64 clocks for now
826 writel(64, sock->addr + SOCK_MMCSD_COMMAND_TO);
827 writel(TIFM_MMCSD_INAB, sock->addr + SOCK_MMCSD_COMMAND);
828
829 /* INAB should take much less than reset */
830 for (rc = 1; rc <= 16; rc <<= 1) {
831 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
832 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
833 if (!(host_status & TIFM_MMCSD_ERRMASK)
834 && (host_status & TIFM_MMCSD_EOC)) {
835 rc = 0;
836 break;
837 }
838 msleep(rc);
839 }
840
841 if (rc) {
842 printk(KERN_ERR DRIVER_NAME
843 ": card not ready - probe failed on initialization\n");
844 return -ENODEV;
845 }
846
847 writel(TIFM_MMCSD_DATAMASK | TIFM_MMCSD_ERRMASK,
848 sock->addr + SOCK_MMCSD_INT_ENABLE);
849 mmiowb();
850
851 return 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700852}
853
854static int tifm_sd_probe(struct tifm_dev *sock)
855{
856 struct mmc_host *mmc;
857 struct tifm_sd *host;
858 int rc = -EIO;
859
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100860 if (!(TIFM_SOCK_STATE_OCCUPIED
861 & readl(sock->addr + SOCK_PRESENT_STATE))) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700862 printk(KERN_WARNING DRIVER_NAME ": card gone, unexpectedly\n");
863 return rc;
864 }
865
866 mmc = mmc_alloc_host(sizeof(struct tifm_sd), &sock->dev);
867 if (!mmc)
868 return -ENOMEM;
869
870 host = mmc_priv(mmc);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700871 tifm_set_drvdata(sock, mmc);
Alex Dubov8e02f852006-12-08 16:50:51 +1100872 host->dev = sock;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700873 host->timeout_jiffies = msecs_to_jiffies(1000);
874
Alex Dubov8e02f852006-12-08 16:50:51 +1100875 init_waitqueue_head(&host->notify);
876 tasklet_init(&host->finish_tasklet,
877 no_dma ? tifm_sd_end_cmd_nodma : tifm_sd_end_cmd,
878 (unsigned long)host);
879 setup_timer(&host->timer, tifm_sd_abort, (unsigned long)host);
880
Alex Dubov4020f2d2006-10-04 02:15:37 -0700881 tifm_sd_ops.request = no_dma ? tifm_sd_request_nodma : tifm_sd_request;
882 mmc->ops = &tifm_sd_ops;
883 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Alex Dubov8e02f852006-12-08 16:50:51 +1100884 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700885 mmc->f_min = 20000000 / 60;
886 mmc->f_max = 24000000;
887 mmc->max_hw_segs = 1;
888 mmc->max_phys_segs = 1;
889 mmc->max_sectors = 127;
890 mmc->max_seg_size = mmc->max_sectors << 11; //2k maximum hw block length
Alex Dubov8e02f852006-12-08 16:50:51 +1100891 sock->signal_irq = tifm_sd_signal_irq;
892 rc = tifm_sd_initialize_host(host);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700893
Alex Dubov8e02f852006-12-08 16:50:51 +1100894 if (!rc)
895 rc = mmc_add_host(mmc);
896 if (rc)
897 goto out_free_mmc;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700898
899 return 0;
Alex Dubov8e02f852006-12-08 16:50:51 +1100900out_free_mmc:
901 mmc_free_host(mmc);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700902 return rc;
903}
904
905static void tifm_sd_remove(struct tifm_dev *sock)
906{
907 struct mmc_host *mmc = tifm_get_drvdata(sock);
908 struct tifm_sd *host = mmc_priv(mmc);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700909
Alex Dubov0803dd02006-12-08 16:50:49 +1100910 del_timer_sync(&host->timer);
Alex Dubov8e02f852006-12-08 16:50:51 +1100911 tifm_sd_terminate(host);
912 wait_event_timeout(host->notify, host->flags & EJECT_DONE,
913 host->timeout_jiffies);
914 tasklet_kill(&host->finish_tasklet);
915 mmc_remove_host(mmc);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700916
917 /* The meaning of the bit majority in this constant is unknown. */
918 writel(0xfff8 & readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100919 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700920
Al Viroe069d792006-10-09 20:29:43 +0100921 tifm_set_drvdata(sock, NULL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700922 mmc_free_host(mmc);
923}
924
925static tifm_media_id tifm_sd_id_tbl[] = {
926 FM_SD, 0
927};
928
929static struct tifm_driver tifm_sd_driver = {
930 .driver = {
931 .name = DRIVER_NAME,
932 .owner = THIS_MODULE
933 },
934 .id_table = tifm_sd_id_tbl,
935 .probe = tifm_sd_probe,
936 .remove = tifm_sd_remove
937};
938
939static int __init tifm_sd_init(void)
940{
941 return tifm_register_driver(&tifm_sd_driver);
942}
943
944static void __exit tifm_sd_exit(void)
945{
946 tifm_unregister_driver(&tifm_sd_driver);
947}
948
949MODULE_AUTHOR("Alex Dubov");
950MODULE_DESCRIPTION("TI FlashMedia SD driver");
951MODULE_LICENSE("GPL");
952MODULE_DEVICE_TABLE(tifm, tifm_sd_id_tbl);
953MODULE_VERSION(DRIVER_VERSION);
954
955module_init(tifm_sd_init);
956module_exit(tifm_sd_exit);