blob: bf00e8cf670c068e2ebb5a778ef486eda0b0b4ac [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 Dubov4552f0c2007-04-12 16:59:12 +100020#define DRIVER_VERSION "0.8"
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
Alex Dubov12c83452007-02-10 21:16:40 -080039#define TIFM_MMCSD_DATAMASK 0x401d /* set bits: CERR, EOFB, BRS, CB, EOC */
40#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
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,
Alex Dubov4020f2d2006-10-04 02:15:37 -070083 CARD_BUSY = 0x0010,
84 OPENDRAIN = 0x0040, /* hardware dependent value */
85 CARD_EVENT = 0x0100, /* hardware dependent value */
86 CARD_RO = 0x0200, /* hardware dependent value */
87 FIFO_EVENT = 0x10000 }; /* hardware dependent value */
88
89struct tifm_sd {
90 struct tifm_dev *dev;
91
92 unsigned int flags;
93 card_state_t state;
94 unsigned int clk_freq;
95 unsigned int clk_div;
Alex Dubov0803dd02006-12-08 16:50:49 +110096 unsigned long timeout_jiffies;
Alex Dubov4020f2d2006-10-04 02:15:37 -070097
Alex Dubov8e02f852006-12-08 16:50:51 +110098 struct tasklet_struct finish_tasklet;
Alex Dubov0803dd02006-12-08 16:50:49 +110099 struct timer_list timer;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700100 struct mmc_request *req;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700101
102 size_t written_blocks;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700103 size_t buffer_size;
104 size_t buffer_pos;
105
106};
107
Pierre Ossmanf9d429a2007-02-03 13:36:41 +0100108static char* tifm_sd_data_buffer(struct mmc_data *data)
Alex Dubov255ef222006-12-08 16:50:48 +1100109{
Pierre Ossmanf9d429a2007-02-03 13:36:41 +0100110 return page_address(data->sg->page) + data->sg->offset;
Alex Dubov255ef222006-12-08 16:50:48 +1100111}
112
Alex Dubov4020f2d2006-10-04 02:15:37 -0700113static int tifm_sd_transfer_data(struct tifm_dev *sock, struct tifm_sd *host,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100114 unsigned int host_status)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700115{
116 struct mmc_command *cmd = host->req->cmd;
117 unsigned int t_val = 0, cnt = 0;
Alex Dubov255ef222006-12-08 16:50:48 +1100118 char *buffer;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700119
120 if (host_status & TIFM_MMCSD_BRS) {
121 /* in non-dma rx mode BRS fires when fifo is still not empty */
Alex Dubov255ef222006-12-08 16:50:48 +1100122 if (no_dma && (cmd->data->flags & MMC_DATA_READ)) {
Pierre Ossmanf9d429a2007-02-03 13:36:41 +0100123 buffer = tifm_sd_data_buffer(host->req->data);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700124 while (host->buffer_size > host->buffer_pos) {
125 t_val = readl(sock->addr + SOCK_MMCSD_DATA);
Alex Dubov255ef222006-12-08 16:50:48 +1100126 buffer[host->buffer_pos++] = t_val & 0xff;
127 buffer[host->buffer_pos++] =
Alex Dubov4020f2d2006-10-04 02:15:37 -0700128 (t_val >> 8) & 0xff;
129 }
130 }
131 return 1;
Alex Dubov255ef222006-12-08 16:50:48 +1100132 } else if (no_dma) {
Pierre Ossmanf9d429a2007-02-03 13:36:41 +0100133 buffer = tifm_sd_data_buffer(host->req->data);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700134 if ((cmd->data->flags & MMC_DATA_READ) &&
135 (host_status & TIFM_MMCSD_AF)) {
136 for (cnt = 0; cnt < TIFM_MMCSD_FIFO_SIZE; cnt++) {
137 t_val = readl(sock->addr + SOCK_MMCSD_DATA);
138 if (host->buffer_size > host->buffer_pos) {
Alex Dubov255ef222006-12-08 16:50:48 +1100139 buffer[host->buffer_pos++] =
Alex Dubov4020f2d2006-10-04 02:15:37 -0700140 t_val & 0xff;
Alex Dubov255ef222006-12-08 16:50:48 +1100141 buffer[host->buffer_pos++] =
Alex Dubov4020f2d2006-10-04 02:15:37 -0700142 (t_val >> 8) & 0xff;
143 }
144 }
145 } else if ((cmd->data->flags & MMC_DATA_WRITE)
146 && (host_status & TIFM_MMCSD_AE)) {
147 for (cnt = 0; cnt < TIFM_MMCSD_FIFO_SIZE; cnt++) {
148 if (host->buffer_size > host->buffer_pos) {
Alex Dubov255ef222006-12-08 16:50:48 +1100149 t_val = buffer[host->buffer_pos++]
150 & 0x00ff;
151 t_val |= ((buffer[host->buffer_pos++])
152 << 8) & 0xff00;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700153 writel(t_val,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100154 sock->addr + SOCK_MMCSD_DATA);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700155 }
156 }
157 }
158 }
159 return 0;
160}
161
162static unsigned int tifm_sd_op_flags(struct mmc_command *cmd)
163{
164 unsigned int rc = 0;
165
166 switch (mmc_resp_type(cmd)) {
167 case MMC_RSP_NONE:
168 rc |= TIFM_MMCSD_RSP_R0;
169 break;
170 case MMC_RSP_R1B:
171 rc |= TIFM_MMCSD_RSP_BUSY; // deliberate fall-through
172 case MMC_RSP_R1:
173 rc |= TIFM_MMCSD_RSP_R1;
174 break;
175 case MMC_RSP_R2:
176 rc |= TIFM_MMCSD_RSP_R2;
177 break;
178 case MMC_RSP_R3:
179 rc |= TIFM_MMCSD_RSP_R3;
180 break;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700181 default:
182 BUG();
183 }
184
185 switch (mmc_cmd_type(cmd)) {
186 case MMC_CMD_BC:
187 rc |= TIFM_MMCSD_CMD_BC;
188 break;
189 case MMC_CMD_BCR:
190 rc |= TIFM_MMCSD_CMD_BCR;
191 break;
192 case MMC_CMD_AC:
193 rc |= TIFM_MMCSD_CMD_AC;
194 break;
195 case MMC_CMD_ADTC:
196 rc |= TIFM_MMCSD_CMD_ADTC;
197 break;
198 default:
199 BUG();
200 }
201 return rc;
202}
203
204static void tifm_sd_exec(struct tifm_sd *host, struct mmc_command *cmd)
205{
206 struct tifm_dev *sock = host->dev;
207 unsigned int cmd_mask = tifm_sd_op_flags(cmd) |
208 (host->flags & OPENDRAIN);
209
210 if (cmd->data && (cmd->data->flags & MMC_DATA_READ))
211 cmd_mask |= TIFM_MMCSD_READ;
212
213 dev_dbg(&sock->dev, "executing opcode 0x%x, arg: 0x%x, mask: 0x%x\n",
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100214 cmd->opcode, cmd->arg, cmd_mask);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700215
216 writel((cmd->arg >> 16) & 0xffff, sock->addr + SOCK_MMCSD_ARG_HIGH);
217 writel(cmd->arg & 0xffff, sock->addr + SOCK_MMCSD_ARG_LOW);
218 writel(cmd->opcode | cmd_mask, sock->addr + SOCK_MMCSD_COMMAND);
219}
220
221static void tifm_sd_fetch_resp(struct mmc_command *cmd, struct tifm_dev *sock)
222{
223 cmd->resp[0] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x1c) << 16)
224 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x18);
225 cmd->resp[1] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x14) << 16)
226 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x10);
227 cmd->resp[2] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x0c) << 16)
228 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x08);
229 cmd->resp[3] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x04) << 16)
230 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x00);
231}
232
233static void tifm_sd_process_cmd(struct tifm_dev *sock, struct tifm_sd *host,
234 unsigned int host_status)
235{
236 struct mmc_command *cmd = host->req->cmd;
237
238change_state:
239 switch (host->state) {
240 case IDLE:
241 return;
242 case CMD:
Alex Dubov12c83452007-02-10 21:16:40 -0800243 if (host_status & (TIFM_MMCSD_EOC | TIFM_MMCSD_CERR)) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700244 tifm_sd_fetch_resp(cmd, sock);
245 if (cmd->data) {
246 host->state = BRS;
Alex Dubov12893352006-12-08 16:50:47 +1100247 } else {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700248 host->state = READY;
Alex Dubov12893352006-12-08 16:50:47 +1100249 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700250 goto change_state;
251 }
252 break;
253 case BRS:
254 if (tifm_sd_transfer_data(sock, host, host_status)) {
Alex Dubov12893352006-12-08 16:50:47 +1100255 if (cmd->data->flags & MMC_DATA_WRITE) {
256 host->state = CARD;
257 } else {
258 if (no_dma) {
259 if (host->req->stop) {
260 tifm_sd_exec(host, host->req->stop);
261 host->state = SCMD;
262 } else {
263 host->state = READY;
264 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700265 } else {
Alex Dubov12893352006-12-08 16:50:47 +1100266 host->state = FIFO;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700267 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700268 }
Alex Dubov12893352006-12-08 16:50:47 +1100269 goto change_state;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700270 }
271 break;
272 case SCMD:
273 if (host_status & TIFM_MMCSD_EOC) {
274 tifm_sd_fetch_resp(host->req->stop, sock);
Alex Dubov12893352006-12-08 16:50:47 +1100275 host->state = READY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700276 goto change_state;
277 }
278 break;
279 case CARD:
Alex Dubov12893352006-12-08 16:50:47 +1100280 dev_dbg(&sock->dev, "waiting for CARD, have %zd blocks\n",
281 host->written_blocks);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700282 if (!(host->flags & CARD_BUSY)
283 && (host->written_blocks == cmd->data->blocks)) {
Alex Dubov12893352006-12-08 16:50:47 +1100284 if (no_dma) {
285 if (host->req->stop) {
286 tifm_sd_exec(host, host->req->stop);
287 host->state = SCMD;
288 } else {
289 host->state = READY;
290 }
291 } else {
292 host->state = FIFO;
293 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700294 goto change_state;
295 }
296 break;
297 case FIFO:
298 if (host->flags & FIFO_RDY) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700299 host->flags &= ~FIFO_RDY;
Alex Dubov12893352006-12-08 16:50:47 +1100300 if (host->req->stop) {
301 tifm_sd_exec(host, host->req->stop);
302 host->state = SCMD;
303 } else {
304 host->state = READY;
305 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700306 goto change_state;
307 }
308 break;
309 case READY:
Alex Dubov8e02f852006-12-08 16:50:51 +1100310 tasklet_schedule(&host->finish_tasklet);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700311 return;
312 }
313
Alex Dubov4020f2d2006-10-04 02:15:37 -0700314}
315
316/* Called from interrupt handler */
Alex Dubov4552f0c2007-04-12 16:59:12 +1000317static void tifm_sd_data_event(struct tifm_dev *sock)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700318{
319 struct tifm_sd *host;
Alex Dubov4552f0c2007-04-12 16:59:12 +1000320 unsigned int fifo_status = 0;
321
322 spin_lock(&sock->lock);
323 host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock));
324
325 fifo_status = readl(sock->addr + SOCK_DMA_FIFO_STATUS);
326 writel(fifo_status, sock->addr + SOCK_DMA_FIFO_STATUS);
327
328 host->flags |= fifo_status & FIFO_RDY;
329
330 if (host->req)
331 tifm_sd_process_cmd(sock, host, 0);
332
333 dev_dbg(&sock->dev, "fifo_status %x\n", fifo_status);
334 spin_unlock(&sock->lock);
335
336}
337
338/* Called from interrupt handler */
339static void tifm_sd_card_event(struct tifm_dev *sock)
340{
341 struct tifm_sd *host;
342 unsigned int host_status = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700343 int error_code = 0;
344
345 spin_lock(&sock->lock);
346 host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock));
Alex Dubov4020f2d2006-10-04 02:15:37 -0700347
Alex Dubov4020f2d2006-10-04 02:15:37 -0700348
Alex Dubov4020f2d2006-10-04 02:15:37 -0700349 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
350 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
351
Alex Dubov4020f2d2006-10-04 02:15:37 -0700352 if (!host->req)
353 goto done;
354
355 if (host_status & TIFM_MMCSD_ERRMASK) {
Alex Dubov12c83452007-02-10 21:16:40 -0800356 if (host_status & (TIFM_MMCSD_CTO | TIFM_MMCSD_DTO))
Alex Dubov4020f2d2006-10-04 02:15:37 -0700357 error_code = MMC_ERR_TIMEOUT;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100358 else if (host_status
359 & (TIFM_MMCSD_CCRC | TIFM_MMCSD_DCRC))
Alex Dubov4020f2d2006-10-04 02:15:37 -0700360 error_code = MMC_ERR_BADCRC;
361
362 writel(TIFM_FIFO_INT_SETALL,
363 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
364 writel(TIFM_DMA_RESET, sock->addr + SOCK_DMA_CONTROL);
365
366 if (host->req->stop) {
367 if (host->state == SCMD) {
368 host->req->stop->error = error_code;
Alex Dubov12893352006-12-08 16:50:47 +1100369 } else if (host->state == BRS
370 || host->state == CARD
371 || host->state == FIFO) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700372 host->req->cmd->error = error_code;
373 tifm_sd_exec(host, host->req->stop);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700374 host->state = SCMD;
375 goto done;
376 } else {
377 host->req->cmd->error = error_code;
378 }
379 } else {
380 host->req->cmd->error = error_code;
381 }
382 host->state = READY;
383 }
384
385 if (host_status & TIFM_MMCSD_CB)
386 host->flags |= CARD_BUSY;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100387 if ((host_status & TIFM_MMCSD_EOFB)
388 && (host->flags & CARD_BUSY)) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700389 host->written_blocks++;
390 host->flags &= ~CARD_BUSY;
391 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700392
393 if (host->req)
394 tifm_sd_process_cmd(sock, host, host_status);
395done:
Alex Dubov4552f0c2007-04-12 16:59:12 +1000396 dev_dbg(&sock->dev, "host_status %x\n", host_status);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700397 spin_unlock(&sock->lock);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700398}
399
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100400static void tifm_sd_prepare_data(struct tifm_sd *host, struct mmc_command *cmd)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700401{
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100402 struct tifm_dev *sock = host->dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700403 unsigned int dest_cnt;
404
405 /* DMA style IO */
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100406 dev_dbg(&sock->dev, "setting dma for %d blocks\n",
407 cmd->data->blocks);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700408 writel(TIFM_FIFO_INT_SETALL,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100409 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
David Howellsf0d1b0b2006-12-08 02:37:49 -0800410 writel(ilog2(cmd->data->blksz) - 2,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100411 sock->addr + SOCK_FIFO_PAGE_SIZE);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700412 writel(TIFM_FIFO_ENABLE, sock->addr + SOCK_FIFO_CONTROL);
413 writel(TIFM_FIFO_INTMASK, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
414
415 dest_cnt = (cmd->data->blocks) << 8;
416
417 writel(sg_dma_address(cmd->data->sg), sock->addr + SOCK_DMA_ADDRESS);
418
419 writel(cmd->data->blocks - 1, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
420 writel(cmd->data->blksz - 1, sock->addr + SOCK_MMCSD_BLOCK_LEN);
421
422 if (cmd->data->flags & MMC_DATA_WRITE) {
423 writel(TIFM_MMCSD_TXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
424 writel(dest_cnt | TIFM_DMA_TX | TIFM_DMA_EN,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100425 sock->addr + SOCK_DMA_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700426 } else {
427 writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
428 writel(dest_cnt | TIFM_DMA_EN, sock->addr + SOCK_DMA_CONTROL);
429 }
430}
431
432static void tifm_sd_set_data_timeout(struct tifm_sd *host,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100433 struct mmc_data *data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700434{
435 struct tifm_dev *sock = host->dev;
436 unsigned int data_timeout = data->timeout_clks;
437
438 if (fixed_timeout)
439 return;
440
441 data_timeout += data->timeout_ns /
Alex Dubov83d420b2006-12-08 16:50:50 +1100442 ((1000000000UL / host->clk_freq) * host->clk_div);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700443
444 if (data_timeout < 0xffff) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700445 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
Alex Dubov83d420b2006-12-08 16:50:50 +1100446 writel((~TIFM_MMCSD_DPE)
447 & readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
448 sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700449 } else {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700450 data_timeout = (data_timeout >> 10) + 1;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100451 if (data_timeout > 0xffff)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700452 data_timeout = 0; /* set to unlimited */
453 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
Alex Dubov83d420b2006-12-08 16:50:50 +1100454 writel(TIFM_MMCSD_DPE
455 | readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
456 sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700457 }
458}
459
460static void tifm_sd_request(struct mmc_host *mmc, struct mmc_request *mrq)
461{
462 struct tifm_sd *host = mmc_priv(mmc);
463 struct tifm_dev *sock = host->dev;
464 unsigned long flags;
465 int sg_count = 0;
466 struct mmc_data *r_data = mrq->cmd->data;
467
468 spin_lock_irqsave(&sock->lock, flags);
469 if (host->flags & EJECT) {
470 spin_unlock_irqrestore(&sock->lock, flags);
471 goto err_out;
472 }
473
474 if (host->req) {
475 printk(KERN_ERR DRIVER_NAME ": unfinished request detected\n");
476 spin_unlock_irqrestore(&sock->lock, flags);
477 goto err_out;
478 }
479
480 if (r_data) {
481 tifm_sd_set_data_timeout(host, r_data);
482
483 sg_count = tifm_map_sg(sock, r_data->sg, r_data->sg_len,
484 mrq->cmd->flags & MMC_DATA_WRITE
485 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
486 if (sg_count != 1) {
487 printk(KERN_ERR DRIVER_NAME
488 ": scatterlist map failed\n");
489 spin_unlock_irqrestore(&sock->lock, flags);
490 goto err_out;
491 }
492
493 host->written_blocks = 0;
494 host->flags &= ~CARD_BUSY;
495 tifm_sd_prepare_data(host, mrq->cmd);
496 }
497
498 host->req = mrq;
Alex Dubov0803dd02006-12-08 16:50:49 +1100499 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700500 host->state = CMD;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700501 writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100502 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700503 tifm_sd_exec(host, mrq->cmd);
504 spin_unlock_irqrestore(&sock->lock, flags);
505 return;
506
507err_out:
508 if (sg_count > 0)
509 tifm_unmap_sg(sock, r_data->sg, r_data->sg_len,
510 (r_data->flags & MMC_DATA_WRITE)
511 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
512
513 mrq->cmd->error = MMC_ERR_TIMEOUT;
514 mmc_request_done(mmc, mrq);
515}
516
Alex Dubov8e02f852006-12-08 16:50:51 +1100517static void tifm_sd_end_cmd(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700518{
Alex Dubov8e02f852006-12-08 16:50:51 +1100519 struct tifm_sd *host = (struct tifm_sd*)data;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700520 struct tifm_dev *sock = host->dev;
521 struct mmc_host *mmc = tifm_get_drvdata(sock);
522 struct mmc_request *mrq;
Al Viroe069d792006-10-09 20:29:43 +0100523 struct mmc_data *r_data = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700524 unsigned long flags;
525
526 spin_lock_irqsave(&sock->lock, flags);
527
Alex Dubov0803dd02006-12-08 16:50:49 +1100528 del_timer(&host->timer);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700529 mrq = host->req;
Al Viroe069d792006-10-09 20:29:43 +0100530 host->req = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700531 host->state = IDLE;
532
533 if (!mrq) {
534 printk(KERN_ERR DRIVER_NAME ": no request to complete?\n");
535 spin_unlock_irqrestore(&sock->lock, flags);
536 return;
537 }
538
539 r_data = mrq->cmd->data;
540 if (r_data) {
541 if (r_data->flags & MMC_DATA_WRITE) {
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100542 r_data->bytes_xfered = host->written_blocks
543 * r_data->blksz;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700544 } else {
545 r_data->bytes_xfered = r_data->blocks -
546 readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS) - 1;
547 r_data->bytes_xfered *= r_data->blksz;
548 r_data->bytes_xfered += r_data->blksz -
549 readl(sock->addr + SOCK_MMCSD_BLOCK_LEN) + 1;
550 }
551 tifm_unmap_sg(sock, r_data->sg, r_data->sg_len,
552 (r_data->flags & MMC_DATA_WRITE)
553 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
554 }
555
556 writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100557 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700558
559 spin_unlock_irqrestore(&sock->lock, flags);
560 mmc_request_done(mmc, mrq);
561}
562
563static void tifm_sd_request_nodma(struct mmc_host *mmc, struct mmc_request *mrq)
564{
565 struct tifm_sd *host = mmc_priv(mmc);
566 struct tifm_dev *sock = host->dev;
567 unsigned long flags;
568 struct mmc_data *r_data = mrq->cmd->data;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700569
570 spin_lock_irqsave(&sock->lock, flags);
571 if (host->flags & EJECT) {
572 spin_unlock_irqrestore(&sock->lock, flags);
573 goto err_out;
574 }
575
576 if (host->req) {
577 printk(KERN_ERR DRIVER_NAME ": unfinished request detected\n");
578 spin_unlock_irqrestore(&sock->lock, flags);
579 goto err_out;
580 }
581
582 if (r_data) {
583 tifm_sd_set_data_timeout(host, r_data);
584
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100585 host->buffer_size = mrq->cmd->data->blocks
586 * mrq->cmd->data->blksz;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700587
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100588 writel(TIFM_MMCSD_BUFINT
589 | readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
Alex Dubov4020f2d2006-10-04 02:15:37 -0700590 sock->addr + SOCK_MMCSD_INT_ENABLE);
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100591 writel(((TIFM_MMCSD_FIFO_SIZE - 1) << 8)
592 | (TIFM_MMCSD_FIFO_SIZE - 1),
Alex Dubov4020f2d2006-10-04 02:15:37 -0700593 sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
594
595 host->written_blocks = 0;
596 host->flags &= ~CARD_BUSY;
597 host->buffer_pos = 0;
598 writel(r_data->blocks - 1, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
599 writel(r_data->blksz - 1, sock->addr + SOCK_MMCSD_BLOCK_LEN);
600 }
601
602 host->req = mrq;
Alex Dubov0803dd02006-12-08 16:50:49 +1100603 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700604 host->state = CMD;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700605 writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100606 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700607 tifm_sd_exec(host, mrq->cmd);
608 spin_unlock_irqrestore(&sock->lock, flags);
609 return;
610
611err_out:
Alex Dubov4020f2d2006-10-04 02:15:37 -0700612 mrq->cmd->error = MMC_ERR_TIMEOUT;
613 mmc_request_done(mmc, mrq);
614}
615
Alex Dubov8e02f852006-12-08 16:50:51 +1100616static void tifm_sd_end_cmd_nodma(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700617{
Alex Dubov8e02f852006-12-08 16:50:51 +1100618 struct tifm_sd *host = (struct tifm_sd*)data;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700619 struct tifm_dev *sock = host->dev;
620 struct mmc_host *mmc = tifm_get_drvdata(sock);
621 struct mmc_request *mrq;
Al Viroe069d792006-10-09 20:29:43 +0100622 struct mmc_data *r_data = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700623 unsigned long flags;
624
625 spin_lock_irqsave(&sock->lock, flags);
626
Alex Dubov0803dd02006-12-08 16:50:49 +1100627 del_timer(&host->timer);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700628 mrq = host->req;
Al Viroe069d792006-10-09 20:29:43 +0100629 host->req = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700630 host->state = IDLE;
631
632 if (!mrq) {
633 printk(KERN_ERR DRIVER_NAME ": no request to complete?\n");
634 spin_unlock_irqrestore(&sock->lock, flags);
635 return;
636 }
637
638 r_data = mrq->cmd->data;
639 if (r_data) {
640 writel((~TIFM_MMCSD_BUFINT) &
641 readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
642 sock->addr + SOCK_MMCSD_INT_ENABLE);
643
644 if (r_data->flags & MMC_DATA_WRITE) {
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100645 r_data->bytes_xfered = host->written_blocks
646 * r_data->blksz;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700647 } else {
648 r_data->bytes_xfered = r_data->blocks -
649 readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS) - 1;
650 r_data->bytes_xfered *= r_data->blksz;
651 r_data->bytes_xfered += r_data->blksz -
652 readl(sock->addr + SOCK_MMCSD_BLOCK_LEN) + 1;
653 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700654 host->buffer_pos = 0;
655 host->buffer_size = 0;
656 }
657
658 writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100659 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700660
661 spin_unlock_irqrestore(&sock->lock, flags);
662
Alex Dubov4020f2d2006-10-04 02:15:37 -0700663 mmc_request_done(mmc, mrq);
664}
665
Alex Dubov0803dd02006-12-08 16:50:49 +1100666static void tifm_sd_abort(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700667{
Alex Dubov8e02f852006-12-08 16:50:51 +1100668 struct tifm_sd *host = (struct tifm_sd*)data;
669
Alex Dubov4020f2d2006-10-04 02:15:37 -0700670 printk(KERN_ERR DRIVER_NAME
Alex Dubovb039d4a2007-04-12 16:59:21 +1000671 ": card failed to respond for a long period of time\n");
Alex Dubov8e02f852006-12-08 16:50:51 +1100672
Alex Dubov8e02f852006-12-08 16:50:51 +1100673 tifm_eject(host->dev);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700674}
675
676static void tifm_sd_ios(struct mmc_host *mmc, struct mmc_ios *ios)
677{
678 struct tifm_sd *host = mmc_priv(mmc);
679 struct tifm_dev *sock = host->dev;
680 unsigned int clk_div1, clk_div2;
681 unsigned long flags;
682
683 spin_lock_irqsave(&sock->lock, flags);
684
685 dev_dbg(&sock->dev, "Setting bus width %d, power %d\n", ios->bus_width,
686 ios->power_mode);
687 if (ios->bus_width == MMC_BUS_WIDTH_4) {
688 writel(TIFM_MMCSD_4BBUS | readl(sock->addr + SOCK_MMCSD_CONFIG),
689 sock->addr + SOCK_MMCSD_CONFIG);
690 } else {
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100691 writel((~TIFM_MMCSD_4BBUS)
692 & readl(sock->addr + SOCK_MMCSD_CONFIG),
693 sock->addr + SOCK_MMCSD_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700694 }
695
696 if (ios->clock) {
697 clk_div1 = 20000000 / ios->clock;
698 if (!clk_div1)
699 clk_div1 = 1;
700
701 clk_div2 = 24000000 / ios->clock;
702 if (!clk_div2)
703 clk_div2 = 1;
704
705 if ((20000000 / clk_div1) > ios->clock)
706 clk_div1++;
707 if ((24000000 / clk_div2) > ios->clock)
708 clk_div2++;
709 if ((20000000 / clk_div1) > (24000000 / clk_div2)) {
710 host->clk_freq = 20000000;
711 host->clk_div = clk_div1;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100712 writel((~TIFM_CTRL_FAST_CLK)
713 & readl(sock->addr + SOCK_CONTROL),
714 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700715 } else {
716 host->clk_freq = 24000000;
717 host->clk_div = clk_div2;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100718 writel(TIFM_CTRL_FAST_CLK
719 | readl(sock->addr + SOCK_CONTROL),
720 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700721 }
722 } else {
723 host->clk_div = 0;
724 }
725 host->clk_div &= TIFM_MMCSD_CLKMASK;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100726 writel(host->clk_div
727 | ((~TIFM_MMCSD_CLKMASK)
728 & readl(sock->addr + SOCK_MMCSD_CONFIG)),
729 sock->addr + SOCK_MMCSD_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700730
731 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
732 host->flags |= OPENDRAIN;
733 else
734 host->flags &= ~OPENDRAIN;
735
736 /* chip_select : maybe later */
737 //vdd
738 //power is set before probe / after remove
Alex Dubov4020f2d2006-10-04 02:15:37 -0700739
740 spin_unlock_irqrestore(&sock->lock, flags);
741}
742
743static int tifm_sd_ro(struct mmc_host *mmc)
744{
745 int rc;
746 struct tifm_sd *host = mmc_priv(mmc);
747 struct tifm_dev *sock = host->dev;
748 unsigned long flags;
749
750 spin_lock_irqsave(&sock->lock, flags);
751
752 host->flags |= (CARD_RO & readl(sock->addr + SOCK_PRESENT_STATE));
753 rc = (host->flags & CARD_RO) ? 1 : 0;
754
755 spin_unlock_irqrestore(&sock->lock, flags);
756 return rc;
757}
758
759static struct mmc_host_ops tifm_sd_ops = {
760 .request = tifm_sd_request,
761 .set_ios = tifm_sd_ios,
762 .get_ro = tifm_sd_ro
763};
764
Alex Dubov8e02f852006-12-08 16:50:51 +1100765static int tifm_sd_initialize_host(struct tifm_sd *host)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700766{
Alex Dubov8e02f852006-12-08 16:50:51 +1100767 int rc;
768 unsigned int host_status = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700769 struct tifm_dev *sock = host->dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700770
Alex Dubov8e02f852006-12-08 16:50:51 +1100771 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
772 mmiowb();
773 host->clk_div = 61;
774 host->clk_freq = 20000000;
775 writel(TIFM_MMCSD_RESET, sock->addr + SOCK_MMCSD_SYSTEM_CONTROL);
776 writel(host->clk_div | TIFM_MMCSD_POWER,
777 sock->addr + SOCK_MMCSD_CONFIG);
778
779 /* wait up to 0.51 sec for reset */
780 for (rc = 2; rc <= 256; rc <<= 1) {
781 if (1 & readl(sock->addr + SOCK_MMCSD_SYSTEM_STATUS)) {
782 rc = 0;
783 break;
784 }
785 msleep(rc);
786 }
787
788 if (rc) {
789 printk(KERN_ERR DRIVER_NAME
790 ": controller failed to reset\n");
791 return -ENODEV;
792 }
793
794 writel(0, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
795 writel(host->clk_div | TIFM_MMCSD_POWER,
796 sock->addr + SOCK_MMCSD_CONFIG);
797 writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
798
799 // command timeout fixed to 64 clocks for now
800 writel(64, sock->addr + SOCK_MMCSD_COMMAND_TO);
801 writel(TIFM_MMCSD_INAB, sock->addr + SOCK_MMCSD_COMMAND);
802
803 /* INAB should take much less than reset */
804 for (rc = 1; rc <= 16; rc <<= 1) {
805 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
806 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
807 if (!(host_status & TIFM_MMCSD_ERRMASK)
808 && (host_status & TIFM_MMCSD_EOC)) {
809 rc = 0;
810 break;
811 }
812 msleep(rc);
813 }
814
815 if (rc) {
816 printk(KERN_ERR DRIVER_NAME
817 ": card not ready - probe failed on initialization\n");
818 return -ENODEV;
819 }
820
821 writel(TIFM_MMCSD_DATAMASK | TIFM_MMCSD_ERRMASK,
822 sock->addr + SOCK_MMCSD_INT_ENABLE);
823 mmiowb();
824
825 return 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700826}
827
828static int tifm_sd_probe(struct tifm_dev *sock)
829{
830 struct mmc_host *mmc;
831 struct tifm_sd *host;
832 int rc = -EIO;
833
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100834 if (!(TIFM_SOCK_STATE_OCCUPIED
835 & readl(sock->addr + SOCK_PRESENT_STATE))) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700836 printk(KERN_WARNING DRIVER_NAME ": card gone, unexpectedly\n");
837 return rc;
838 }
839
840 mmc = mmc_alloc_host(sizeof(struct tifm_sd), &sock->dev);
841 if (!mmc)
842 return -ENOMEM;
843
844 host = mmc_priv(mmc);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700845 tifm_set_drvdata(sock, mmc);
Alex Dubov8e02f852006-12-08 16:50:51 +1100846 host->dev = sock;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700847 host->timeout_jiffies = msecs_to_jiffies(1000);
848
Alex Dubov8e02f852006-12-08 16:50:51 +1100849 tasklet_init(&host->finish_tasklet,
850 no_dma ? tifm_sd_end_cmd_nodma : tifm_sd_end_cmd,
851 (unsigned long)host);
852 setup_timer(&host->timer, tifm_sd_abort, (unsigned long)host);
853
Alex Dubov4020f2d2006-10-04 02:15:37 -0700854 tifm_sd_ops.request = no_dma ? tifm_sd_request_nodma : tifm_sd_request;
855 mmc->ops = &tifm_sd_ops;
856 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Alex Dubov8e02f852006-12-08 16:50:51 +1100857 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700858 mmc->f_min = 20000000 / 60;
859 mmc->f_max = 24000000;
860 mmc->max_hw_segs = 1;
861 mmc->max_phys_segs = 1;
Pierre Ossman55db8902006-11-21 17:55:45 +0100862 // limited by DMA counter - it's safer to stick with
863 // block counter has 11 bits though
864 mmc->max_blk_count = 256;
865 // 2k maximum hw block length
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100866 mmc->max_blk_size = 2048;
Pierre Ossman55db8902006-11-21 17:55:45 +0100867 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
868 mmc->max_seg_size = mmc->max_req_size;
Alex Dubov4552f0c2007-04-12 16:59:12 +1000869 sock->card_event = tifm_sd_card_event;
870 sock->data_event = tifm_sd_data_event;
Alex Dubov8e02f852006-12-08 16:50:51 +1100871 rc = tifm_sd_initialize_host(host);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700872
Alex Dubov8e02f852006-12-08 16:50:51 +1100873 if (!rc)
874 rc = mmc_add_host(mmc);
875 if (rc)
876 goto out_free_mmc;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700877
878 return 0;
Alex Dubov8e02f852006-12-08 16:50:51 +1100879out_free_mmc:
880 mmc_free_host(mmc);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700881 return rc;
882}
883
884static void tifm_sd_remove(struct tifm_dev *sock)
885{
886 struct mmc_host *mmc = tifm_get_drvdata(sock);
887 struct tifm_sd *host = mmc_priv(mmc);
Alex Dubovb039d4a2007-04-12 16:59:21 +1000888 unsigned long flags;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700889
Alex Dubovb039d4a2007-04-12 16:59:21 +1000890 spin_lock_irqsave(&sock->lock, flags);
891 host->flags |= EJECT;
Alex Dubov592d3722007-04-12 16:59:22 +1000892 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
893 mmiowb();
894 spin_unlock_irqrestore(&sock->lock, flags);
895
896 tasklet_kill(&host->finish_tasklet);
897
898 spin_lock_irqsave(&sock->lock, flags);
Alex Dubovb039d4a2007-04-12 16:59:21 +1000899 if (host->req) {
900 writel(TIFM_FIFO_INT_SETALL,
901 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
902 writel(0, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
Alex Dubov592d3722007-04-12 16:59:22 +1000903 host->req->cmd->error = MMC_ERR_TIMEOUT;
904 if (host->req->stop)
905 host->req->stop->error = MMC_ERR_TIMEOUT;
Alex Dubovb039d4a2007-04-12 16:59:21 +1000906 tasklet_schedule(&host->finish_tasklet);
907 }
908 spin_unlock_irqrestore(&sock->lock, flags);
Alex Dubov8e02f852006-12-08 16:50:51 +1100909 mmc_remove_host(mmc);
Alex Dubov592d3722007-04-12 16:59:22 +1000910 dev_dbg(&sock->dev, "after remove\n");
Alex Dubov4020f2d2006-10-04 02:15:37 -0700911
912 /* The meaning of the bit majority in this constant is unknown. */
913 writel(0xfff8 & readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100914 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700915
Alex Dubov4020f2d2006-10-04 02:15:37 -0700916 mmc_free_host(mmc);
917}
918
Alex Dubovdba4acc2006-12-11 01:55:38 +1100919#ifdef CONFIG_PM
920
921static int tifm_sd_suspend(struct tifm_dev *sock, pm_message_t state)
922{
923 struct mmc_host *mmc = tifm_get_drvdata(sock);
924 int rc;
925
926 rc = mmc_suspend_host(mmc, state);
927 /* The meaning of the bit majority in this constant is unknown. */
928 writel(0xfff8 & readl(sock->addr + SOCK_CONTROL),
929 sock->addr + SOCK_CONTROL);
930 return rc;
931}
932
933static int tifm_sd_resume(struct tifm_dev *sock)
934{
935 struct mmc_host *mmc = tifm_get_drvdata(sock);
936 struct tifm_sd *host = mmc_priv(mmc);
937
Alex Dubove23f2b82007-04-12 16:59:14 +1000938 if (sock->type != TIFM_TYPE_SD
Alex Dubovdba4acc2006-12-11 01:55:38 +1100939 || tifm_sd_initialize_host(host)) {
940 tifm_eject(sock);
941 return 0;
942 } else {
943 return mmc_resume_host(mmc);
944 }
945}
946
947#else
948
949#define tifm_sd_suspend NULL
950#define tifm_sd_resume NULL
951
952#endif /* CONFIG_PM */
953
Alex Dubove23f2b82007-04-12 16:59:14 +1000954static struct tifm_device_id tifm_sd_id_tbl[] = {
955 { TIFM_TYPE_SD }, { }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700956};
957
958static struct tifm_driver tifm_sd_driver = {
959 .driver = {
960 .name = DRIVER_NAME,
961 .owner = THIS_MODULE
962 },
963 .id_table = tifm_sd_id_tbl,
964 .probe = tifm_sd_probe,
Alex Dubovdba4acc2006-12-11 01:55:38 +1100965 .remove = tifm_sd_remove,
966 .suspend = tifm_sd_suspend,
967 .resume = tifm_sd_resume
Alex Dubov4020f2d2006-10-04 02:15:37 -0700968};
969
970static int __init tifm_sd_init(void)
971{
972 return tifm_register_driver(&tifm_sd_driver);
973}
974
975static void __exit tifm_sd_exit(void)
976{
977 tifm_unregister_driver(&tifm_sd_driver);
978}
979
980MODULE_AUTHOR("Alex Dubov");
981MODULE_DESCRIPTION("TI FlashMedia SD driver");
982MODULE_LICENSE("GPL");
983MODULE_DEVICE_TABLE(tifm, tifm_sd_id_tbl);
984MODULE_VERSION(DRIVER_VERSION);
985
986module_init(tifm_sd_init);
987module_exit(tifm_sd_exit);