blob: e65f8a0a9349c4145711938a9a6a3de5e158413e [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
Pierre Ossmanf9d429a2007-02-03 13:36:41 +0100110static char* tifm_sd_data_buffer(struct mmc_data *data)
Alex Dubov255ef222006-12-08 16:50:48 +1100111{
Pierre Ossmanf9d429a2007-02-03 13:36:41 +0100112 return page_address(data->sg->page) + data->sg->offset;
Alex Dubov255ef222006-12-08 16:50:48 +1100113}
114
Alex Dubov4020f2d2006-10-04 02:15:37 -0700115static int tifm_sd_transfer_data(struct tifm_dev *sock, struct tifm_sd *host,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100116 unsigned int host_status)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700117{
118 struct mmc_command *cmd = host->req->cmd;
119 unsigned int t_val = 0, cnt = 0;
Alex Dubov255ef222006-12-08 16:50:48 +1100120 char *buffer;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700121
122 if (host_status & TIFM_MMCSD_BRS) {
123 /* in non-dma rx mode BRS fires when fifo is still not empty */
Alex Dubov255ef222006-12-08 16:50:48 +1100124 if (no_dma && (cmd->data->flags & MMC_DATA_READ)) {
Pierre Ossmanf9d429a2007-02-03 13:36:41 +0100125 buffer = tifm_sd_data_buffer(host->req->data);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700126 while (host->buffer_size > host->buffer_pos) {
127 t_val = readl(sock->addr + SOCK_MMCSD_DATA);
Alex Dubov255ef222006-12-08 16:50:48 +1100128 buffer[host->buffer_pos++] = t_val & 0xff;
129 buffer[host->buffer_pos++] =
Alex Dubov4020f2d2006-10-04 02:15:37 -0700130 (t_val >> 8) & 0xff;
131 }
132 }
133 return 1;
Alex Dubov255ef222006-12-08 16:50:48 +1100134 } else if (no_dma) {
Pierre Ossmanf9d429a2007-02-03 13:36:41 +0100135 buffer = tifm_sd_data_buffer(host->req->data);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700136 if ((cmd->data->flags & MMC_DATA_READ) &&
137 (host_status & TIFM_MMCSD_AF)) {
138 for (cnt = 0; cnt < TIFM_MMCSD_FIFO_SIZE; cnt++) {
139 t_val = readl(sock->addr + SOCK_MMCSD_DATA);
140 if (host->buffer_size > host->buffer_pos) {
Alex Dubov255ef222006-12-08 16:50:48 +1100141 buffer[host->buffer_pos++] =
Alex Dubov4020f2d2006-10-04 02:15:37 -0700142 t_val & 0xff;
Alex Dubov255ef222006-12-08 16:50:48 +1100143 buffer[host->buffer_pos++] =
Alex Dubov4020f2d2006-10-04 02:15:37 -0700144 (t_val >> 8) & 0xff;
145 }
146 }
147 } else if ((cmd->data->flags & MMC_DATA_WRITE)
148 && (host_status & TIFM_MMCSD_AE)) {
149 for (cnt = 0; cnt < TIFM_MMCSD_FIFO_SIZE; cnt++) {
150 if (host->buffer_size > host->buffer_pos) {
Alex Dubov255ef222006-12-08 16:50:48 +1100151 t_val = buffer[host->buffer_pos++]
152 & 0x00ff;
153 t_val |= ((buffer[host->buffer_pos++])
154 << 8) & 0xff00;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700155 writel(t_val,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100156 sock->addr + SOCK_MMCSD_DATA);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700157 }
158 }
159 }
160 }
161 return 0;
162}
163
164static unsigned int tifm_sd_op_flags(struct mmc_command *cmd)
165{
166 unsigned int rc = 0;
167
168 switch (mmc_resp_type(cmd)) {
169 case MMC_RSP_NONE:
170 rc |= TIFM_MMCSD_RSP_R0;
171 break;
172 case MMC_RSP_R1B:
173 rc |= TIFM_MMCSD_RSP_BUSY; // deliberate fall-through
174 case MMC_RSP_R1:
175 rc |= TIFM_MMCSD_RSP_R1;
176 break;
177 case MMC_RSP_R2:
178 rc |= TIFM_MMCSD_RSP_R2;
179 break;
180 case MMC_RSP_R3:
181 rc |= TIFM_MMCSD_RSP_R3;
182 break;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700183 default:
184 BUG();
185 }
186
187 switch (mmc_cmd_type(cmd)) {
188 case MMC_CMD_BC:
189 rc |= TIFM_MMCSD_CMD_BC;
190 break;
191 case MMC_CMD_BCR:
192 rc |= TIFM_MMCSD_CMD_BCR;
193 break;
194 case MMC_CMD_AC:
195 rc |= TIFM_MMCSD_CMD_AC;
196 break;
197 case MMC_CMD_ADTC:
198 rc |= TIFM_MMCSD_CMD_ADTC;
199 break;
200 default:
201 BUG();
202 }
203 return rc;
204}
205
206static void tifm_sd_exec(struct tifm_sd *host, struct mmc_command *cmd)
207{
208 struct tifm_dev *sock = host->dev;
209 unsigned int cmd_mask = tifm_sd_op_flags(cmd) |
210 (host->flags & OPENDRAIN);
211
212 if (cmd->data && (cmd->data->flags & MMC_DATA_READ))
213 cmd_mask |= TIFM_MMCSD_READ;
214
215 dev_dbg(&sock->dev, "executing opcode 0x%x, arg: 0x%x, mask: 0x%x\n",
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100216 cmd->opcode, cmd->arg, cmd_mask);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700217
218 writel((cmd->arg >> 16) & 0xffff, sock->addr + SOCK_MMCSD_ARG_HIGH);
219 writel(cmd->arg & 0xffff, sock->addr + SOCK_MMCSD_ARG_LOW);
220 writel(cmd->opcode | cmd_mask, sock->addr + SOCK_MMCSD_COMMAND);
221}
222
223static void tifm_sd_fetch_resp(struct mmc_command *cmd, struct tifm_dev *sock)
224{
225 cmd->resp[0] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x1c) << 16)
226 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x18);
227 cmd->resp[1] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x14) << 16)
228 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x10);
229 cmd->resp[2] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x0c) << 16)
230 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x08);
231 cmd->resp[3] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x04) << 16)
232 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x00);
233}
234
235static void tifm_sd_process_cmd(struct tifm_dev *sock, struct tifm_sd *host,
236 unsigned int host_status)
237{
238 struct mmc_command *cmd = host->req->cmd;
239
240change_state:
241 switch (host->state) {
242 case IDLE:
243 return;
244 case CMD:
245 if (host_status & TIFM_MMCSD_EOC) {
246 tifm_sd_fetch_resp(cmd, sock);
247 if (cmd->data) {
248 host->state = BRS;
Alex Dubov12893352006-12-08 16:50:47 +1100249 } else {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700250 host->state = READY;
Alex Dubov12893352006-12-08 16:50:47 +1100251 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700252 goto change_state;
253 }
254 break;
255 case BRS:
256 if (tifm_sd_transfer_data(sock, host, host_status)) {
Alex Dubov12893352006-12-08 16:50:47 +1100257 if (cmd->data->flags & MMC_DATA_WRITE) {
258 host->state = CARD;
259 } else {
260 if (no_dma) {
261 if (host->req->stop) {
262 tifm_sd_exec(host, host->req->stop);
263 host->state = SCMD;
264 } else {
265 host->state = READY;
266 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700267 } else {
Alex Dubov12893352006-12-08 16:50:47 +1100268 host->state = FIFO;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700269 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700270 }
Alex Dubov12893352006-12-08 16:50:47 +1100271 goto change_state;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700272 }
273 break;
274 case SCMD:
275 if (host_status & TIFM_MMCSD_EOC) {
276 tifm_sd_fetch_resp(host->req->stop, sock);
Alex Dubov12893352006-12-08 16:50:47 +1100277 host->state = READY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700278 goto change_state;
279 }
280 break;
281 case CARD:
Alex Dubov12893352006-12-08 16:50:47 +1100282 dev_dbg(&sock->dev, "waiting for CARD, have %zd blocks\n",
283 host->written_blocks);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700284 if (!(host->flags & CARD_BUSY)
285 && (host->written_blocks == cmd->data->blocks)) {
Alex Dubov12893352006-12-08 16:50:47 +1100286 if (no_dma) {
287 if (host->req->stop) {
288 tifm_sd_exec(host, host->req->stop);
289 host->state = SCMD;
290 } else {
291 host->state = READY;
292 }
293 } else {
294 host->state = FIFO;
295 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700296 goto change_state;
297 }
298 break;
299 case FIFO:
300 if (host->flags & FIFO_RDY) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700301 host->flags &= ~FIFO_RDY;
Alex Dubov12893352006-12-08 16:50:47 +1100302 if (host->req->stop) {
303 tifm_sd_exec(host, host->req->stop);
304 host->state = SCMD;
305 } else {
306 host->state = READY;
307 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700308 goto change_state;
309 }
310 break;
311 case READY:
Alex Dubov8e02f852006-12-08 16:50:51 +1100312 tasklet_schedule(&host->finish_tasklet);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700313 return;
314 }
315
Alex Dubov4020f2d2006-10-04 02:15:37 -0700316}
317
318/* Called from interrupt handler */
Alex Dubov50743f42006-12-11 01:55:30 +1100319static void tifm_sd_signal_irq(struct tifm_dev *sock,
320 unsigned int sock_irq_status)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700321{
322 struct tifm_sd *host;
323 unsigned int host_status = 0, fifo_status = 0;
324 int error_code = 0;
325
326 spin_lock(&sock->lock);
327 host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock));
Alex Dubov4020f2d2006-10-04 02:15:37 -0700328
329 if (sock_irq_status & FIFO_EVENT) {
330 fifo_status = readl(sock->addr + SOCK_DMA_FIFO_STATUS);
331 writel(fifo_status, sock->addr + SOCK_DMA_FIFO_STATUS);
332
333 host->flags |= fifo_status & FIFO_RDY;
334 }
335
336 if (sock_irq_status & CARD_EVENT) {
337 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
338 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
339
Alex Dubov4020f2d2006-10-04 02:15:37 -0700340 if (!host->req)
341 goto done;
342
343 if (host_status & TIFM_MMCSD_ERRMASK) {
344 if (host_status & TIFM_MMCSD_CERR)
345 error_code = MMC_ERR_FAILED;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100346 else if (host_status
347 & (TIFM_MMCSD_CTO | TIFM_MMCSD_DTO))
Alex Dubov4020f2d2006-10-04 02:15:37 -0700348 error_code = MMC_ERR_TIMEOUT;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100349 else if (host_status
350 & (TIFM_MMCSD_CCRC | TIFM_MMCSD_DCRC))
Alex Dubov4020f2d2006-10-04 02:15:37 -0700351 error_code = MMC_ERR_BADCRC;
352
353 writel(TIFM_FIFO_INT_SETALL,
354 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
355 writel(TIFM_DMA_RESET, sock->addr + SOCK_DMA_CONTROL);
356
357 if (host->req->stop) {
358 if (host->state == SCMD) {
359 host->req->stop->error = error_code;
Alex Dubov12893352006-12-08 16:50:47 +1100360 } else if (host->state == BRS
361 || host->state == CARD
362 || host->state == FIFO) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700363 host->req->cmd->error = error_code;
364 tifm_sd_exec(host, host->req->stop);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700365 host->state = SCMD;
366 goto done;
367 } else {
368 host->req->cmd->error = error_code;
369 }
370 } else {
371 host->req->cmd->error = error_code;
372 }
373 host->state = READY;
374 }
375
376 if (host_status & TIFM_MMCSD_CB)
377 host->flags |= CARD_BUSY;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100378 if ((host_status & TIFM_MMCSD_EOFB)
379 && (host->flags & CARD_BUSY)) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700380 host->written_blocks++;
381 host->flags &= ~CARD_BUSY;
382 }
383 }
384
385 if (host->req)
386 tifm_sd_process_cmd(sock, host, host_status);
387done:
388 dev_dbg(&sock->dev, "host_status %x, fifo_status %x\n",
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100389 host_status, fifo_status);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700390 spin_unlock(&sock->lock);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700391}
392
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100393static void tifm_sd_prepare_data(struct tifm_sd *host, struct mmc_command *cmd)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700394{
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100395 struct tifm_dev *sock = host->dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700396 unsigned int dest_cnt;
397
398 /* DMA style IO */
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100399 dev_dbg(&sock->dev, "setting dma for %d blocks\n",
400 cmd->data->blocks);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700401 writel(TIFM_FIFO_INT_SETALL,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100402 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
David Howellsf0d1b0b2006-12-08 02:37:49 -0800403 writel(ilog2(cmd->data->blksz) - 2,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100404 sock->addr + SOCK_FIFO_PAGE_SIZE);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700405 writel(TIFM_FIFO_ENABLE, sock->addr + SOCK_FIFO_CONTROL);
406 writel(TIFM_FIFO_INTMASK, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
407
408 dest_cnt = (cmd->data->blocks) << 8;
409
410 writel(sg_dma_address(cmd->data->sg), sock->addr + SOCK_DMA_ADDRESS);
411
412 writel(cmd->data->blocks - 1, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
413 writel(cmd->data->blksz - 1, sock->addr + SOCK_MMCSD_BLOCK_LEN);
414
415 if (cmd->data->flags & MMC_DATA_WRITE) {
416 writel(TIFM_MMCSD_TXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
417 writel(dest_cnt | TIFM_DMA_TX | TIFM_DMA_EN,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100418 sock->addr + SOCK_DMA_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700419 } else {
420 writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
421 writel(dest_cnt | TIFM_DMA_EN, sock->addr + SOCK_DMA_CONTROL);
422 }
423}
424
425static void tifm_sd_set_data_timeout(struct tifm_sd *host,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100426 struct mmc_data *data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700427{
428 struct tifm_dev *sock = host->dev;
429 unsigned int data_timeout = data->timeout_clks;
430
431 if (fixed_timeout)
432 return;
433
434 data_timeout += data->timeout_ns /
Alex Dubov83d420b2006-12-08 16:50:50 +1100435 ((1000000000UL / host->clk_freq) * host->clk_div);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700436
437 if (data_timeout < 0xffff) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700438 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
Alex Dubov83d420b2006-12-08 16:50:50 +1100439 writel((~TIFM_MMCSD_DPE)
440 & readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
441 sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700442 } else {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700443 data_timeout = (data_timeout >> 10) + 1;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100444 if (data_timeout > 0xffff)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700445 data_timeout = 0; /* set to unlimited */
446 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 }
451}
452
453static void tifm_sd_request(struct mmc_host *mmc, struct mmc_request *mrq)
454{
455 struct tifm_sd *host = mmc_priv(mmc);
456 struct tifm_dev *sock = host->dev;
457 unsigned long flags;
458 int sg_count = 0;
459 struct mmc_data *r_data = mrq->cmd->data;
460
461 spin_lock_irqsave(&sock->lock, flags);
462 if (host->flags & EJECT) {
463 spin_unlock_irqrestore(&sock->lock, flags);
464 goto err_out;
465 }
466
467 if (host->req) {
468 printk(KERN_ERR DRIVER_NAME ": unfinished request detected\n");
469 spin_unlock_irqrestore(&sock->lock, flags);
470 goto err_out;
471 }
472
473 if (r_data) {
474 tifm_sd_set_data_timeout(host, r_data);
475
476 sg_count = tifm_map_sg(sock, r_data->sg, r_data->sg_len,
477 mrq->cmd->flags & MMC_DATA_WRITE
478 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
479 if (sg_count != 1) {
480 printk(KERN_ERR DRIVER_NAME
481 ": scatterlist map failed\n");
482 spin_unlock_irqrestore(&sock->lock, flags);
483 goto err_out;
484 }
485
486 host->written_blocks = 0;
487 host->flags &= ~CARD_BUSY;
488 tifm_sd_prepare_data(host, mrq->cmd);
489 }
490
491 host->req = mrq;
Alex Dubov0803dd02006-12-08 16:50:49 +1100492 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700493 host->state = CMD;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700494 writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100495 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700496 tifm_sd_exec(host, mrq->cmd);
497 spin_unlock_irqrestore(&sock->lock, flags);
498 return;
499
500err_out:
501 if (sg_count > 0)
502 tifm_unmap_sg(sock, r_data->sg, r_data->sg_len,
503 (r_data->flags & MMC_DATA_WRITE)
504 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
505
506 mrq->cmd->error = MMC_ERR_TIMEOUT;
507 mmc_request_done(mmc, mrq);
508}
509
Alex Dubov8e02f852006-12-08 16:50:51 +1100510static void tifm_sd_end_cmd(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700511{
Alex Dubov8e02f852006-12-08 16:50:51 +1100512 struct tifm_sd *host = (struct tifm_sd*)data;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700513 struct tifm_dev *sock = host->dev;
514 struct mmc_host *mmc = tifm_get_drvdata(sock);
515 struct mmc_request *mrq;
Al Viroe069d792006-10-09 20:29:43 +0100516 struct mmc_data *r_data = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700517 unsigned long flags;
518
519 spin_lock_irqsave(&sock->lock, flags);
520
Alex Dubov0803dd02006-12-08 16:50:49 +1100521 del_timer(&host->timer);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700522 mrq = host->req;
Al Viroe069d792006-10-09 20:29:43 +0100523 host->req = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700524 host->state = IDLE;
525
526 if (!mrq) {
527 printk(KERN_ERR DRIVER_NAME ": no request to complete?\n");
528 spin_unlock_irqrestore(&sock->lock, flags);
529 return;
530 }
531
532 r_data = mrq->cmd->data;
533 if (r_data) {
534 if (r_data->flags & MMC_DATA_WRITE) {
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100535 r_data->bytes_xfered = host->written_blocks
536 * r_data->blksz;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700537 } else {
538 r_data->bytes_xfered = r_data->blocks -
539 readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS) - 1;
540 r_data->bytes_xfered *= r_data->blksz;
541 r_data->bytes_xfered += r_data->blksz -
542 readl(sock->addr + SOCK_MMCSD_BLOCK_LEN) + 1;
543 }
544 tifm_unmap_sg(sock, r_data->sg, r_data->sg_len,
545 (r_data->flags & MMC_DATA_WRITE)
546 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
547 }
548
549 writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100550 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700551
552 spin_unlock_irqrestore(&sock->lock, flags);
553 mmc_request_done(mmc, mrq);
554}
555
556static void tifm_sd_request_nodma(struct mmc_host *mmc, struct mmc_request *mrq)
557{
558 struct tifm_sd *host = mmc_priv(mmc);
559 struct tifm_dev *sock = host->dev;
560 unsigned long flags;
561 struct mmc_data *r_data = mrq->cmd->data;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700562
563 spin_lock_irqsave(&sock->lock, flags);
564 if (host->flags & EJECT) {
565 spin_unlock_irqrestore(&sock->lock, flags);
566 goto err_out;
567 }
568
569 if (host->req) {
570 printk(KERN_ERR DRIVER_NAME ": unfinished request detected\n");
571 spin_unlock_irqrestore(&sock->lock, flags);
572 goto err_out;
573 }
574
575 if (r_data) {
576 tifm_sd_set_data_timeout(host, r_data);
577
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100578 host->buffer_size = mrq->cmd->data->blocks
579 * mrq->cmd->data->blksz;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700580
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100581 writel(TIFM_MMCSD_BUFINT
582 | readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
Alex Dubov4020f2d2006-10-04 02:15:37 -0700583 sock->addr + SOCK_MMCSD_INT_ENABLE);
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100584 writel(((TIFM_MMCSD_FIFO_SIZE - 1) << 8)
585 | (TIFM_MMCSD_FIFO_SIZE - 1),
Alex Dubov4020f2d2006-10-04 02:15:37 -0700586 sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
587
588 host->written_blocks = 0;
589 host->flags &= ~CARD_BUSY;
590 host->buffer_pos = 0;
591 writel(r_data->blocks - 1, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
592 writel(r_data->blksz - 1, sock->addr + SOCK_MMCSD_BLOCK_LEN);
593 }
594
595 host->req = mrq;
Alex Dubov0803dd02006-12-08 16:50:49 +1100596 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700597 host->state = CMD;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700598 writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100599 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700600 tifm_sd_exec(host, mrq->cmd);
601 spin_unlock_irqrestore(&sock->lock, flags);
602 return;
603
604err_out:
Alex Dubov4020f2d2006-10-04 02:15:37 -0700605 mrq->cmd->error = MMC_ERR_TIMEOUT;
606 mmc_request_done(mmc, mrq);
607}
608
Alex Dubov8e02f852006-12-08 16:50:51 +1100609static void tifm_sd_end_cmd_nodma(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700610{
Alex Dubov8e02f852006-12-08 16:50:51 +1100611 struct tifm_sd *host = (struct tifm_sd*)data;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700612 struct tifm_dev *sock = host->dev;
613 struct mmc_host *mmc = tifm_get_drvdata(sock);
614 struct mmc_request *mrq;
Al Viroe069d792006-10-09 20:29:43 +0100615 struct mmc_data *r_data = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700616 unsigned long flags;
617
618 spin_lock_irqsave(&sock->lock, flags);
619
Alex Dubov0803dd02006-12-08 16:50:49 +1100620 del_timer(&host->timer);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700621 mrq = host->req;
Al Viroe069d792006-10-09 20:29:43 +0100622 host->req = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700623 host->state = IDLE;
624
625 if (!mrq) {
626 printk(KERN_ERR DRIVER_NAME ": no request to complete?\n");
627 spin_unlock_irqrestore(&sock->lock, flags);
628 return;
629 }
630
631 r_data = mrq->cmd->data;
632 if (r_data) {
633 writel((~TIFM_MMCSD_BUFINT) &
634 readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
635 sock->addr + SOCK_MMCSD_INT_ENABLE);
636
637 if (r_data->flags & MMC_DATA_WRITE) {
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100638 r_data->bytes_xfered = host->written_blocks
639 * r_data->blksz;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700640 } else {
641 r_data->bytes_xfered = r_data->blocks -
642 readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS) - 1;
643 r_data->bytes_xfered *= r_data->blksz;
644 r_data->bytes_xfered += r_data->blksz -
645 readl(sock->addr + SOCK_MMCSD_BLOCK_LEN) + 1;
646 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700647 host->buffer_pos = 0;
648 host->buffer_size = 0;
649 }
650
651 writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100652 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700653
654 spin_unlock_irqrestore(&sock->lock, flags);
655
Alex Dubov4020f2d2006-10-04 02:15:37 -0700656 mmc_request_done(mmc, mrq);
657}
658
Alex Dubov8e02f852006-12-08 16:50:51 +1100659static void tifm_sd_terminate(struct tifm_sd *host)
660{
661 struct tifm_dev *sock = host->dev;
662 unsigned long flags;
663
664 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
665 mmiowb();
666 spin_lock_irqsave(&sock->lock, flags);
667 host->flags |= EJECT;
668 if (host->req) {
669 writel(TIFM_FIFO_INT_SETALL,
670 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
671 writel(0, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
672 tasklet_schedule(&host->finish_tasklet);
673 }
674 spin_unlock_irqrestore(&sock->lock, flags);
675}
676
Alex Dubov0803dd02006-12-08 16:50:49 +1100677static void tifm_sd_abort(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700678{
Alex Dubov8e02f852006-12-08 16:50:51 +1100679 struct tifm_sd *host = (struct tifm_sd*)data;
680
Alex Dubov4020f2d2006-10-04 02:15:37 -0700681 printk(KERN_ERR DRIVER_NAME
Alex Dubov0803dd02006-12-08 16:50:49 +1100682 ": card failed to respond for a long period of time");
Alex Dubov8e02f852006-12-08 16:50:51 +1100683
684 tifm_sd_terminate(host);
685 tifm_eject(host->dev);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700686}
687
688static void tifm_sd_ios(struct mmc_host *mmc, struct mmc_ios *ios)
689{
690 struct tifm_sd *host = mmc_priv(mmc);
691 struct tifm_dev *sock = host->dev;
692 unsigned int clk_div1, clk_div2;
693 unsigned long flags;
694
695 spin_lock_irqsave(&sock->lock, flags);
696
697 dev_dbg(&sock->dev, "Setting bus width %d, power %d\n", ios->bus_width,
698 ios->power_mode);
699 if (ios->bus_width == MMC_BUS_WIDTH_4) {
700 writel(TIFM_MMCSD_4BBUS | readl(sock->addr + SOCK_MMCSD_CONFIG),
701 sock->addr + SOCK_MMCSD_CONFIG);
702 } else {
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100703 writel((~TIFM_MMCSD_4BBUS)
704 & readl(sock->addr + SOCK_MMCSD_CONFIG),
705 sock->addr + SOCK_MMCSD_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700706 }
707
708 if (ios->clock) {
709 clk_div1 = 20000000 / ios->clock;
710 if (!clk_div1)
711 clk_div1 = 1;
712
713 clk_div2 = 24000000 / ios->clock;
714 if (!clk_div2)
715 clk_div2 = 1;
716
717 if ((20000000 / clk_div1) > ios->clock)
718 clk_div1++;
719 if ((24000000 / clk_div2) > ios->clock)
720 clk_div2++;
721 if ((20000000 / clk_div1) > (24000000 / clk_div2)) {
722 host->clk_freq = 20000000;
723 host->clk_div = clk_div1;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100724 writel((~TIFM_CTRL_FAST_CLK)
725 & readl(sock->addr + SOCK_CONTROL),
726 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700727 } else {
728 host->clk_freq = 24000000;
729 host->clk_div = clk_div2;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100730 writel(TIFM_CTRL_FAST_CLK
731 | readl(sock->addr + SOCK_CONTROL),
732 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700733 }
734 } else {
735 host->clk_div = 0;
736 }
737 host->clk_div &= TIFM_MMCSD_CLKMASK;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100738 writel(host->clk_div
739 | ((~TIFM_MMCSD_CLKMASK)
740 & readl(sock->addr + SOCK_MMCSD_CONFIG)),
741 sock->addr + SOCK_MMCSD_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700742
743 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
744 host->flags |= OPENDRAIN;
745 else
746 host->flags &= ~OPENDRAIN;
747
748 /* chip_select : maybe later */
749 //vdd
750 //power is set before probe / after remove
751 //I believe, power_off when already marked for eject is sufficient to
752 // allow removal.
753 if ((host->flags & EJECT) && ios->power_mode == MMC_POWER_OFF) {
754 host->flags |= EJECT_DONE;
Alex Dubov8e02f852006-12-08 16:50:51 +1100755 wake_up_all(&host->notify);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700756 }
757
758 spin_unlock_irqrestore(&sock->lock, flags);
759}
760
761static int tifm_sd_ro(struct mmc_host *mmc)
762{
763 int rc;
764 struct tifm_sd *host = mmc_priv(mmc);
765 struct tifm_dev *sock = host->dev;
766 unsigned long flags;
767
768 spin_lock_irqsave(&sock->lock, flags);
769
770 host->flags |= (CARD_RO & readl(sock->addr + SOCK_PRESENT_STATE));
771 rc = (host->flags & CARD_RO) ? 1 : 0;
772
773 spin_unlock_irqrestore(&sock->lock, flags);
774 return rc;
775}
776
777static struct mmc_host_ops tifm_sd_ops = {
778 .request = tifm_sd_request,
779 .set_ios = tifm_sd_ios,
780 .get_ro = tifm_sd_ro
781};
782
Alex Dubov8e02f852006-12-08 16:50:51 +1100783static int tifm_sd_initialize_host(struct tifm_sd *host)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700784{
Alex Dubov8e02f852006-12-08 16:50:51 +1100785 int rc;
786 unsigned int host_status = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700787 struct tifm_dev *sock = host->dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700788
Alex Dubov8e02f852006-12-08 16:50:51 +1100789 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
790 mmiowb();
791 host->clk_div = 61;
792 host->clk_freq = 20000000;
793 writel(TIFM_MMCSD_RESET, sock->addr + SOCK_MMCSD_SYSTEM_CONTROL);
794 writel(host->clk_div | TIFM_MMCSD_POWER,
795 sock->addr + SOCK_MMCSD_CONFIG);
796
797 /* wait up to 0.51 sec for reset */
798 for (rc = 2; rc <= 256; rc <<= 1) {
799 if (1 & readl(sock->addr + SOCK_MMCSD_SYSTEM_STATUS)) {
800 rc = 0;
801 break;
802 }
803 msleep(rc);
804 }
805
806 if (rc) {
807 printk(KERN_ERR DRIVER_NAME
808 ": controller failed to reset\n");
809 return -ENODEV;
810 }
811
812 writel(0, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
813 writel(host->clk_div | TIFM_MMCSD_POWER,
814 sock->addr + SOCK_MMCSD_CONFIG);
815 writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
816
817 // command timeout fixed to 64 clocks for now
818 writel(64, sock->addr + SOCK_MMCSD_COMMAND_TO);
819 writel(TIFM_MMCSD_INAB, sock->addr + SOCK_MMCSD_COMMAND);
820
821 /* INAB should take much less than reset */
822 for (rc = 1; rc <= 16; rc <<= 1) {
823 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
824 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
825 if (!(host_status & TIFM_MMCSD_ERRMASK)
826 && (host_status & TIFM_MMCSD_EOC)) {
827 rc = 0;
828 break;
829 }
830 msleep(rc);
831 }
832
833 if (rc) {
834 printk(KERN_ERR DRIVER_NAME
835 ": card not ready - probe failed on initialization\n");
836 return -ENODEV;
837 }
838
839 writel(TIFM_MMCSD_DATAMASK | TIFM_MMCSD_ERRMASK,
840 sock->addr + SOCK_MMCSD_INT_ENABLE);
841 mmiowb();
842
843 return 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700844}
845
846static int tifm_sd_probe(struct tifm_dev *sock)
847{
848 struct mmc_host *mmc;
849 struct tifm_sd *host;
850 int rc = -EIO;
851
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100852 if (!(TIFM_SOCK_STATE_OCCUPIED
853 & readl(sock->addr + SOCK_PRESENT_STATE))) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700854 printk(KERN_WARNING DRIVER_NAME ": card gone, unexpectedly\n");
855 return rc;
856 }
857
858 mmc = mmc_alloc_host(sizeof(struct tifm_sd), &sock->dev);
859 if (!mmc)
860 return -ENOMEM;
861
862 host = mmc_priv(mmc);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700863 tifm_set_drvdata(sock, mmc);
Alex Dubov8e02f852006-12-08 16:50:51 +1100864 host->dev = sock;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700865 host->timeout_jiffies = msecs_to_jiffies(1000);
866
Alex Dubov8e02f852006-12-08 16:50:51 +1100867 init_waitqueue_head(&host->notify);
868 tasklet_init(&host->finish_tasklet,
869 no_dma ? tifm_sd_end_cmd_nodma : tifm_sd_end_cmd,
870 (unsigned long)host);
871 setup_timer(&host->timer, tifm_sd_abort, (unsigned long)host);
872
Alex Dubov4020f2d2006-10-04 02:15:37 -0700873 tifm_sd_ops.request = no_dma ? tifm_sd_request_nodma : tifm_sd_request;
874 mmc->ops = &tifm_sd_ops;
875 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Alex Dubov8e02f852006-12-08 16:50:51 +1100876 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700877 mmc->f_min = 20000000 / 60;
878 mmc->f_max = 24000000;
879 mmc->max_hw_segs = 1;
880 mmc->max_phys_segs = 1;
Pierre Ossman55db8902006-11-21 17:55:45 +0100881 // limited by DMA counter - it's safer to stick with
882 // block counter has 11 bits though
883 mmc->max_blk_count = 256;
884 // 2k maximum hw block length
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100885 mmc->max_blk_size = 2048;
Pierre Ossman55db8902006-11-21 17:55:45 +0100886 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
887 mmc->max_seg_size = mmc->max_req_size;
Alex Dubov8e02f852006-12-08 16:50:51 +1100888 sock->signal_irq = tifm_sd_signal_irq;
889 rc = tifm_sd_initialize_host(host);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700890
Alex Dubov8e02f852006-12-08 16:50:51 +1100891 if (!rc)
892 rc = mmc_add_host(mmc);
893 if (rc)
894 goto out_free_mmc;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700895
896 return 0;
Alex Dubov8e02f852006-12-08 16:50:51 +1100897out_free_mmc:
898 mmc_free_host(mmc);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700899 return rc;
900}
901
902static void tifm_sd_remove(struct tifm_dev *sock)
903{
904 struct mmc_host *mmc = tifm_get_drvdata(sock);
905 struct tifm_sd *host = mmc_priv(mmc);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700906
Alex Dubov0803dd02006-12-08 16:50:49 +1100907 del_timer_sync(&host->timer);
Alex Dubov8e02f852006-12-08 16:50:51 +1100908 tifm_sd_terminate(host);
909 wait_event_timeout(host->notify, host->flags & EJECT_DONE,
910 host->timeout_jiffies);
911 tasklet_kill(&host->finish_tasklet);
912 mmc_remove_host(mmc);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700913
914 /* The meaning of the bit majority in this constant is unknown. */
915 writel(0xfff8 & readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100916 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700917
Al Viroe069d792006-10-09 20:29:43 +0100918 tifm_set_drvdata(sock, NULL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700919 mmc_free_host(mmc);
920}
921
Alex Dubovdba4acc2006-12-11 01:55:38 +1100922#ifdef CONFIG_PM
923
924static int tifm_sd_suspend(struct tifm_dev *sock, pm_message_t state)
925{
926 struct mmc_host *mmc = tifm_get_drvdata(sock);
927 int rc;
928
929 rc = mmc_suspend_host(mmc, state);
930 /* The meaning of the bit majority in this constant is unknown. */
931 writel(0xfff8 & readl(sock->addr + SOCK_CONTROL),
932 sock->addr + SOCK_CONTROL);
933 return rc;
934}
935
936static int tifm_sd_resume(struct tifm_dev *sock)
937{
938 struct mmc_host *mmc = tifm_get_drvdata(sock);
939 struct tifm_sd *host = mmc_priv(mmc);
940
941 if (sock->media_id != FM_SD
942 || tifm_sd_initialize_host(host)) {
943 tifm_eject(sock);
944 return 0;
945 } else {
946 return mmc_resume_host(mmc);
947 }
948}
949
950#else
951
952#define tifm_sd_suspend NULL
953#define tifm_sd_resume NULL
954
955#endif /* CONFIG_PM */
956
Alex Dubov4020f2d2006-10-04 02:15:37 -0700957static tifm_media_id tifm_sd_id_tbl[] = {
958 FM_SD, 0
959};
960
961static struct tifm_driver tifm_sd_driver = {
962 .driver = {
963 .name = DRIVER_NAME,
964 .owner = THIS_MODULE
965 },
966 .id_table = tifm_sd_id_tbl,
967 .probe = tifm_sd_probe,
Alex Dubovdba4acc2006-12-11 01:55:38 +1100968 .remove = tifm_sd_remove,
969 .suspend = tifm_sd_suspend,
970 .resume = tifm_sd_resume
Alex Dubov4020f2d2006-10-04 02:15:37 -0700971};
972
973static int __init tifm_sd_init(void)
974{
975 return tifm_register_driver(&tifm_sd_driver);
976}
977
978static void __exit tifm_sd_exit(void)
979{
980 tifm_unregister_driver(&tifm_sd_driver);
981}
982
983MODULE_AUTHOR("Alex Dubov");
984MODULE_DESCRIPTION("TI FlashMedia SD driver");
985MODULE_LICENSE("GPL");
986MODULE_DEVICE_TABLE(tifm, tifm_sd_id_tbl);
987MODULE_VERSION(DRIVER_VERSION);
988
989module_init(tifm_sd_init);
990module_exit(tifm_sd_exit);