blob: 103060f490a628976410f0dc4b3f1985eb626eb0 [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
Alex Dubov0007d482007-04-12 16:59:23 +100053#define TIFM_MMCSD_ODTO 0x0040 /* open drain / extended timeout */
54#define TIFM_MMCSD_CARD_RO 0x0200 /* card is read-only */
55
Alex Dubov4020f2d2006-10-04 02:15:37 -070056#define TIFM_MMCSD_FIFO_SIZE 0x0020
57
58#define TIFM_MMCSD_RSP_R0 0x0000
59#define TIFM_MMCSD_RSP_R1 0x0100
60#define TIFM_MMCSD_RSP_R2 0x0200
61#define TIFM_MMCSD_RSP_R3 0x0300
62#define TIFM_MMCSD_RSP_R4 0x0400
63#define TIFM_MMCSD_RSP_R5 0x0500
64#define TIFM_MMCSD_RSP_R6 0x0600
65
66#define TIFM_MMCSD_RSP_BUSY 0x0800
67
68#define TIFM_MMCSD_CMD_BC 0x0000
69#define TIFM_MMCSD_CMD_BCR 0x1000
70#define TIFM_MMCSD_CMD_AC 0x2000
71#define TIFM_MMCSD_CMD_ADTC 0x3000
72
Alex Dubov4020f2d2006-10-04 02:15:37 -070073enum {
Alex Dubov72dc9d92007-04-12 17:05:23 +100074 CMD_READY = 0x0001,
75 FIFO_READY = 0x0002,
76 BRS_READY = 0x0004,
77 SCMD_ACTIVE = 0x0008,
78 SCMD_READY = 0x0010,
79 CARD_BUSY = 0x0020,
80 DATA_CARRY = 0x0040
Alex Dubov0007d482007-04-12 16:59:23 +100081};
Alex Dubov4020f2d2006-10-04 02:15:37 -070082
83struct tifm_sd {
84 struct tifm_dev *dev;
85
Alex Dubov0007d482007-04-12 16:59:23 +100086 unsigned short eject:1,
87 open_drain:1,
88 no_dma:1;
89 unsigned short cmd_flags;
Alex Dubov72dc9d92007-04-12 17:05:23 +100090
Alex Dubov4020f2d2006-10-04 02:15:37 -070091 unsigned int clk_freq;
92 unsigned int clk_div;
Alex Dubov0803dd02006-12-08 16:50:49 +110093 unsigned long timeout_jiffies;
Alex Dubov4020f2d2006-10-04 02:15:37 -070094
Alex Dubov8e02f852006-12-08 16:50:51 +110095 struct tasklet_struct finish_tasklet;
Alex Dubov0803dd02006-12-08 16:50:49 +110096 struct timer_list timer;
Alex Dubov4020f2d2006-10-04 02:15:37 -070097 struct mmc_request *req;
Alex Dubov4020f2d2006-10-04 02:15:37 -070098
99 size_t written_blocks;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700100 size_t buffer_size;
101 size_t buffer_pos;
102
103};
104
Pierre Ossmanf9d429a2007-02-03 13:36:41 +0100105static char* tifm_sd_data_buffer(struct mmc_data *data)
Alex Dubov255ef222006-12-08 16:50:48 +1100106{
Pierre Ossmanf9d429a2007-02-03 13:36:41 +0100107 return page_address(data->sg->page) + data->sg->offset;
Alex Dubov255ef222006-12-08 16:50:48 +1100108}
109
Alex Dubov4020f2d2006-10-04 02:15:37 -0700110static int tifm_sd_transfer_data(struct tifm_dev *sock, struct tifm_sd *host,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100111 unsigned int host_status)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700112{
113 struct mmc_command *cmd = host->req->cmd;
114 unsigned int t_val = 0, cnt = 0;
Alex Dubov255ef222006-12-08 16:50:48 +1100115 char *buffer;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700116
117 if (host_status & TIFM_MMCSD_BRS) {
118 /* in non-dma rx mode BRS fires when fifo is still not empty */
Alex Dubov0007d482007-04-12 16:59:23 +1000119 if (host->no_dma && (cmd->data->flags & MMC_DATA_READ)) {
Pierre Ossmanf9d429a2007-02-03 13:36:41 +0100120 buffer = tifm_sd_data_buffer(host->req->data);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700121 while (host->buffer_size > host->buffer_pos) {
122 t_val = readl(sock->addr + SOCK_MMCSD_DATA);
Alex Dubov255ef222006-12-08 16:50:48 +1100123 buffer[host->buffer_pos++] = t_val & 0xff;
124 buffer[host->buffer_pos++] =
Alex Dubov4020f2d2006-10-04 02:15:37 -0700125 (t_val >> 8) & 0xff;
126 }
127 }
128 return 1;
Alex Dubov0007d482007-04-12 16:59:23 +1000129 } else if (host->no_dma) {
Pierre Ossmanf9d429a2007-02-03 13:36:41 +0100130 buffer = tifm_sd_data_buffer(host->req->data);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700131 if ((cmd->data->flags & MMC_DATA_READ) &&
132 (host_status & TIFM_MMCSD_AF)) {
133 for (cnt = 0; cnt < TIFM_MMCSD_FIFO_SIZE; cnt++) {
134 t_val = readl(sock->addr + SOCK_MMCSD_DATA);
135 if (host->buffer_size > host->buffer_pos) {
Alex Dubov255ef222006-12-08 16:50:48 +1100136 buffer[host->buffer_pos++] =
Alex Dubov4020f2d2006-10-04 02:15:37 -0700137 t_val & 0xff;
Alex Dubov255ef222006-12-08 16:50:48 +1100138 buffer[host->buffer_pos++] =
Alex Dubov4020f2d2006-10-04 02:15:37 -0700139 (t_val >> 8) & 0xff;
140 }
141 }
142 } else if ((cmd->data->flags & MMC_DATA_WRITE)
143 && (host_status & TIFM_MMCSD_AE)) {
144 for (cnt = 0; cnt < TIFM_MMCSD_FIFO_SIZE; cnt++) {
145 if (host->buffer_size > host->buffer_pos) {
Alex Dubov255ef222006-12-08 16:50:48 +1100146 t_val = buffer[host->buffer_pos++]
147 & 0x00ff;
148 t_val |= ((buffer[host->buffer_pos++])
149 << 8) & 0xff00;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700150 writel(t_val,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100151 sock->addr + SOCK_MMCSD_DATA);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700152 }
153 }
154 }
155 }
156 return 0;
157}
158
159static unsigned int tifm_sd_op_flags(struct mmc_command *cmd)
160{
161 unsigned int rc = 0;
162
163 switch (mmc_resp_type(cmd)) {
164 case MMC_RSP_NONE:
165 rc |= TIFM_MMCSD_RSP_R0;
166 break;
167 case MMC_RSP_R1B:
168 rc |= TIFM_MMCSD_RSP_BUSY; // deliberate fall-through
169 case MMC_RSP_R1:
170 rc |= TIFM_MMCSD_RSP_R1;
171 break;
172 case MMC_RSP_R2:
173 rc |= TIFM_MMCSD_RSP_R2;
174 break;
175 case MMC_RSP_R3:
176 rc |= TIFM_MMCSD_RSP_R3;
177 break;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700178 default:
179 BUG();
180 }
181
182 switch (mmc_cmd_type(cmd)) {
183 case MMC_CMD_BC:
184 rc |= TIFM_MMCSD_CMD_BC;
185 break;
186 case MMC_CMD_BCR:
187 rc |= TIFM_MMCSD_CMD_BCR;
188 break;
189 case MMC_CMD_AC:
190 rc |= TIFM_MMCSD_CMD_AC;
191 break;
192 case MMC_CMD_ADTC:
193 rc |= TIFM_MMCSD_CMD_ADTC;
194 break;
195 default:
196 BUG();
197 }
198 return rc;
199}
200
201static void tifm_sd_exec(struct tifm_sd *host, struct mmc_command *cmd)
202{
203 struct tifm_dev *sock = host->dev;
Alex Dubov0007d482007-04-12 16:59:23 +1000204 unsigned int cmd_mask = tifm_sd_op_flags(cmd);
205
206 if (host->open_drain)
207 cmd_mask |= TIFM_MMCSD_ODTO;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700208
209 if (cmd->data && (cmd->data->flags & MMC_DATA_READ))
210 cmd_mask |= TIFM_MMCSD_READ;
211
212 dev_dbg(&sock->dev, "executing opcode 0x%x, arg: 0x%x, mask: 0x%x\n",
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100213 cmd->opcode, cmd->arg, cmd_mask);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700214
215 writel((cmd->arg >> 16) & 0xffff, sock->addr + SOCK_MMCSD_ARG_HIGH);
216 writel(cmd->arg & 0xffff, sock->addr + SOCK_MMCSD_ARG_LOW);
217 writel(cmd->opcode | cmd_mask, sock->addr + SOCK_MMCSD_COMMAND);
218}
219
220static void tifm_sd_fetch_resp(struct mmc_command *cmd, struct tifm_dev *sock)
221{
222 cmd->resp[0] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x1c) << 16)
223 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x18);
224 cmd->resp[1] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x14) << 16)
225 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x10);
226 cmd->resp[2] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x0c) << 16)
227 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x08);
228 cmd->resp[3] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x04) << 16)
229 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x00);
230}
231
Alex Dubov72dc9d92007-04-12 17:05:23 +1000232static void tifm_sd_check_status(struct tifm_sd *host)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700233{
Alex Dubov72dc9d92007-04-12 17:05:23 +1000234 struct tifm_dev *sock = host->dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700235 struct mmc_command *cmd = host->req->cmd;
236
Alex Dubov72dc9d92007-04-12 17:05:23 +1000237 if (cmd->error != MMC_ERR_NONE)
238 goto finish_request;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700239
Alex Dubov72dc9d92007-04-12 17:05:23 +1000240 if (!(host->cmd_flags & CMD_READY))
241 return;
242
243 if (cmd->data) {
244 if (cmd->data->error != MMC_ERR_NONE) {
245 if ((host->cmd_flags & SCMD_ACTIVE)
246 && !(host->cmd_flags & SCMD_READY))
247 return;
248
249 goto finish_request;
250 }
251
252 if (!(host->cmd_flags & BRS_READY))
253 return;
254
255 if (!(host->no_dma || (host->cmd_flags & FIFO_READY)))
256 return;
257
258 if (cmd->data->flags & MMC_DATA_WRITE) {
259 if (host->req->stop) {
260 if (!(host->cmd_flags & SCMD_ACTIVE)) {
261 host->cmd_flags |= SCMD_ACTIVE;
262 writel(TIFM_MMCSD_EOFB
263 | readl(sock->addr
264 + SOCK_MMCSD_INT_ENABLE),
265 sock->addr
266 + SOCK_MMCSD_INT_ENABLE);
267 tifm_sd_exec(host, host->req->stop);
268 return;
269 } else {
270 if (!(host->cmd_flags & SCMD_READY)
271 || (host->cmd_flags & CARD_BUSY))
272 return;
273 writel((~TIFM_MMCSD_EOFB)
274 & readl(sock->addr
275 + SOCK_MMCSD_INT_ENABLE),
276 sock->addr
277 + SOCK_MMCSD_INT_ENABLE);
278 }
279 } else {
280 if (host->cmd_flags & CARD_BUSY)
281 return;
282 writel((~TIFM_MMCSD_EOFB)
283 & readl(sock->addr
284 + SOCK_MMCSD_INT_ENABLE),
285 sock->addr + SOCK_MMCSD_INT_ENABLE);
286 }
287 } else {
288 if (host->req->stop) {
289 if (!(host->cmd_flags & SCMD_ACTIVE)) {
290 host->cmd_flags |= SCMD_ACTIVE;
291 tifm_sd_exec(host, host->req->stop);
292 return;
293 } else {
294 if (!(host->cmd_flags & SCMD_READY))
295 return;
296 }
297 }
298 }
299 }
300finish_request:
301 tasklet_schedule(&host->finish_tasklet);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700302}
303
304/* Called from interrupt handler */
Alex Dubov4552f0c2007-04-12 16:59:12 +1000305static void tifm_sd_data_event(struct tifm_dev *sock)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700306{
307 struct tifm_sd *host;
Alex Dubov4552f0c2007-04-12 16:59:12 +1000308 unsigned int fifo_status = 0;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000309 struct mmc_data *r_data = NULL;
Alex Dubov4552f0c2007-04-12 16:59:12 +1000310
311 spin_lock(&sock->lock);
312 host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock));
Alex Dubov4552f0c2007-04-12 16:59:12 +1000313 fifo_status = readl(sock->addr + SOCK_DMA_FIFO_STATUS);
Alex Dubov72dc9d92007-04-12 17:05:23 +1000314 dev_dbg(&sock->dev, "data event: fifo_status %x, flags %x\n",
315 fifo_status, host->cmd_flags);
316
317 if (host->req) {
318 r_data = host->req->cmd->data;
319
320 if (r_data && (fifo_status & TIFM_FIFO_READY)) {
321 host->cmd_flags |= FIFO_READY;
322 tifm_sd_check_status(host);
323 }
324 }
325
Alex Dubov4552f0c2007-04-12 16:59:12 +1000326 writel(fifo_status, sock->addr + SOCK_DMA_FIFO_STATUS);
Alex Dubov4552f0c2007-04-12 16:59:12 +1000327 spin_unlock(&sock->lock);
Alex Dubov4552f0c2007-04-12 16:59:12 +1000328}
329
330/* Called from interrupt handler */
331static void tifm_sd_card_event(struct tifm_dev *sock)
332{
333 struct tifm_sd *host;
334 unsigned int host_status = 0;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000335 int cmd_error = MMC_ERR_NONE;
336 struct mmc_command *cmd = NULL;
337 unsigned long flags;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700338
339 spin_lock(&sock->lock);
340 host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock));
Alex Dubov72dc9d92007-04-12 17:05:23 +1000341 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
342 dev_dbg(&sock->dev, "host event: host_status %x, flags %x\n",
343 host_status, host->cmd_flags);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700344
Alex Dubov72dc9d92007-04-12 17:05:23 +1000345 if (host->req) {
346 cmd = host->req->cmd;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700347
348 if (host_status & TIFM_MMCSD_ERRMASK) {
Alex Dubov72dc9d92007-04-12 17:05:23 +1000349 writel(host_status & TIFM_MMCSD_ERRMASK,
350 sock->addr + SOCK_MMCSD_STATUS);
351 if (host_status & TIFM_MMCSD_CTO)
352 cmd_error = MMC_ERR_TIMEOUT;
353 else if (host_status & TIFM_MMCSD_CCRC)
354 cmd_error = MMC_ERR_BADCRC;
355
356 if (cmd->data) {
357 if (host_status & TIFM_MMCSD_DTO)
358 cmd->data->error = MMC_ERR_TIMEOUT;
359 else if (host_status & TIFM_MMCSD_DCRC)
360 cmd->data->error = MMC_ERR_BADCRC;
361 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700362
363 writel(TIFM_FIFO_INT_SETALL,
364 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
365 writel(TIFM_DMA_RESET, sock->addr + SOCK_DMA_CONTROL);
366
367 if (host->req->stop) {
Alex Dubov72dc9d92007-04-12 17:05:23 +1000368 if (host->cmd_flags & SCMD_ACTIVE) {
369 host->req->stop->error = cmd_error;
370 host->cmd_flags |= SCMD_READY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700371 } else {
Alex Dubov72dc9d92007-04-12 17:05:23 +1000372 cmd->error = cmd_error;
373 host->cmd_flags |= SCMD_ACTIVE;
374 tifm_sd_exec(host, host->req->stop);
375 goto done;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700376 }
Alex Dubov72dc9d92007-04-12 17:05:23 +1000377 } else
378 cmd->error = cmd_error;
379 } else {
380 if (host_status & (TIFM_MMCSD_EOC | TIFM_MMCSD_CERR)) {
381 if (!(host->cmd_flags & CMD_READY)) {
382 host->cmd_flags |= CMD_READY;
383 tifm_sd_fetch_resp(cmd, sock);
384 } else if (host->cmd_flags & SCMD_ACTIVE) {
385 host->cmd_flags |= SCMD_READY;
386 tifm_sd_fetch_resp(host->req->stop,
387 sock);
388 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700389 }
Alex Dubov72dc9d92007-04-12 17:05:23 +1000390 if (host_status & TIFM_MMCSD_BRS)
391 host->cmd_flags |= BRS_READY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700392 }
393
Alex Dubov72dc9d92007-04-12 17:05:23 +1000394 if (host->no_dma && cmd->data) {
395 if (host_status & TIFM_MMCSD_AE)
396 writel(host_status & TIFM_MMCSD_AE,
397 sock->addr + SOCK_MMCSD_STATUS);
398
399 if (host_status & (TIFM_MMCSD_AE | TIFM_MMCSD_AF
400 | TIFM_MMCSD_BRS)) {
401 local_irq_save(flags);
402 tifm_sd_transfer_data(sock, host, host_status);
403 local_irq_restore(flags);
404 host_status &= ~TIFM_MMCSD_AE;
405 }
406 }
407
408 if (host_status & TIFM_MMCSD_EOFB)
Alex Dubov0007d482007-04-12 16:59:23 +1000409 host->cmd_flags &= ~CARD_BUSY;
Alex Dubov72dc9d92007-04-12 17:05:23 +1000410 else if (host_status & TIFM_MMCSD_CB)
411 host->cmd_flags |= CARD_BUSY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700412
Alex Dubov72dc9d92007-04-12 17:05:23 +1000413 tifm_sd_check_status(host);
414 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700415done:
Alex Dubov72dc9d92007-04-12 17:05:23 +1000416 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700417 spin_unlock(&sock->lock);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700418}
419
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100420static void tifm_sd_prepare_data(struct tifm_sd *host, struct mmc_command *cmd)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700421{
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100422 struct tifm_dev *sock = host->dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700423 unsigned int dest_cnt;
424
425 /* DMA style IO */
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100426 dev_dbg(&sock->dev, "setting dma for %d blocks\n",
427 cmd->data->blocks);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700428 writel(TIFM_FIFO_INT_SETALL,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100429 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
David Howellsf0d1b0b2006-12-08 02:37:49 -0800430 writel(ilog2(cmd->data->blksz) - 2,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100431 sock->addr + SOCK_FIFO_PAGE_SIZE);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700432 writel(TIFM_FIFO_ENABLE, sock->addr + SOCK_FIFO_CONTROL);
433 writel(TIFM_FIFO_INTMASK, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
434
435 dest_cnt = (cmd->data->blocks) << 8;
436
437 writel(sg_dma_address(cmd->data->sg), sock->addr + SOCK_DMA_ADDRESS);
438
439 writel(cmd->data->blocks - 1, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
440 writel(cmd->data->blksz - 1, sock->addr + SOCK_MMCSD_BLOCK_LEN);
441
442 if (cmd->data->flags & MMC_DATA_WRITE) {
443 writel(TIFM_MMCSD_TXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
444 writel(dest_cnt | TIFM_DMA_TX | TIFM_DMA_EN,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100445 sock->addr + SOCK_DMA_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700446 } else {
447 writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
448 writel(dest_cnt | TIFM_DMA_EN, sock->addr + SOCK_DMA_CONTROL);
449 }
450}
451
452static void tifm_sd_set_data_timeout(struct tifm_sd *host,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100453 struct mmc_data *data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700454{
455 struct tifm_dev *sock = host->dev;
456 unsigned int data_timeout = data->timeout_clks;
457
458 if (fixed_timeout)
459 return;
460
461 data_timeout += data->timeout_ns /
Alex Dubov83d420b2006-12-08 16:50:50 +1100462 ((1000000000UL / host->clk_freq) * host->clk_div);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700463
464 if (data_timeout < 0xffff) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700465 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
Alex Dubov83d420b2006-12-08 16:50:50 +1100466 writel((~TIFM_MMCSD_DPE)
467 & readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
468 sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700469 } else {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700470 data_timeout = (data_timeout >> 10) + 1;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100471 if (data_timeout > 0xffff)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700472 data_timeout = 0; /* set to unlimited */
473 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
Alex Dubov83d420b2006-12-08 16:50:50 +1100474 writel(TIFM_MMCSD_DPE
475 | readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
476 sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700477 }
478}
479
480static void tifm_sd_request(struct mmc_host *mmc, struct mmc_request *mrq)
481{
482 struct tifm_sd *host = mmc_priv(mmc);
483 struct tifm_dev *sock = host->dev;
484 unsigned long flags;
485 int sg_count = 0;
486 struct mmc_data *r_data = mrq->cmd->data;
487
488 spin_lock_irqsave(&sock->lock, flags);
Alex Dubov0007d482007-04-12 16:59:23 +1000489 if (host->eject) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700490 spin_unlock_irqrestore(&sock->lock, flags);
491 goto err_out;
492 }
493
494 if (host->req) {
495 printk(KERN_ERR DRIVER_NAME ": unfinished request detected\n");
496 spin_unlock_irqrestore(&sock->lock, flags);
497 goto err_out;
498 }
499
500 if (r_data) {
501 tifm_sd_set_data_timeout(host, r_data);
502
Alex Dubovdfef26d2007-04-12 16:59:24 +1000503 if (host->no_dma) {
504 host->buffer_size = mrq->cmd->data->blocks
505 * mrq->cmd->data->blksz;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700506
Alex Dubovdfef26d2007-04-12 16:59:24 +1000507 writel(TIFM_MMCSD_BUFINT
508 | readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
509 sock->addr + SOCK_MMCSD_INT_ENABLE);
510 writel(((TIFM_MMCSD_FIFO_SIZE - 1) << 8)
511 | (TIFM_MMCSD_FIFO_SIZE - 1),
512 sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
513
514 host->written_blocks = 0;
515 host->cmd_flags &= ~CARD_BUSY;
516 host->buffer_pos = 0;
517 writel(r_data->blocks - 1,
518 sock->addr + SOCK_MMCSD_NUM_BLOCKS);
519 writel(r_data->blksz - 1,
520 sock->addr + SOCK_MMCSD_BLOCK_LEN);
521 } else {
522 sg_count = tifm_map_sg(sock, r_data->sg, r_data->sg_len,
523 mrq->cmd->flags & MMC_DATA_WRITE
524 ? PCI_DMA_TODEVICE
525 : PCI_DMA_FROMDEVICE);
526 if (sg_count != 1) {
527 printk(KERN_ERR DRIVER_NAME
528 ": scatterlist map failed\n");
529 spin_unlock_irqrestore(&sock->lock, flags);
530 goto err_out;
531 }
532
533 host->written_blocks = 0;
534 host->cmd_flags &= ~CARD_BUSY;
535 tifm_sd_prepare_data(host, mrq->cmd);
536 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700537 }
538
539 host->req = mrq;
Alex Dubov0803dd02006-12-08 16:50:49 +1100540 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
Alex Dubov72dc9d92007-04-12 17:05:23 +1000541 host->cmd_flags = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700542 writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100543 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700544 tifm_sd_exec(host, mrq->cmd);
545 spin_unlock_irqrestore(&sock->lock, flags);
546 return;
547
548err_out:
549 if (sg_count > 0)
550 tifm_unmap_sg(sock, r_data->sg, r_data->sg_len,
551 (r_data->flags & MMC_DATA_WRITE)
552 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
553
554 mrq->cmd->error = MMC_ERR_TIMEOUT;
555 mmc_request_done(mmc, mrq);
556}
557
Alex Dubov8e02f852006-12-08 16:50:51 +1100558static void tifm_sd_end_cmd(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700559{
Alex Dubov8e02f852006-12-08 16:50:51 +1100560 struct tifm_sd *host = (struct tifm_sd*)data;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700561 struct tifm_dev *sock = host->dev;
562 struct mmc_host *mmc = tifm_get_drvdata(sock);
563 struct mmc_request *mrq;
Al Viroe069d792006-10-09 20:29:43 +0100564 struct mmc_data *r_data = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700565 unsigned long flags;
566
567 spin_lock_irqsave(&sock->lock, flags);
568
Alex Dubov0803dd02006-12-08 16:50:49 +1100569 del_timer(&host->timer);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700570 mrq = host->req;
Al Viroe069d792006-10-09 20:29:43 +0100571 host->req = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700572
573 if (!mrq) {
574 printk(KERN_ERR DRIVER_NAME ": no request to complete?\n");
575 spin_unlock_irqrestore(&sock->lock, flags);
576 return;
577 }
578
579 r_data = mrq->cmd->data;
580 if (r_data) {
Alex Dubovdfef26d2007-04-12 16:59:24 +1000581 if (host->no_dma) {
582 writel((~TIFM_MMCSD_BUFINT) &
583 readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
584 sock->addr + SOCK_MMCSD_INT_ENABLE);
585
586 if (r_data->flags & MMC_DATA_WRITE) {
587 r_data->bytes_xfered = host->written_blocks
588 * r_data->blksz;
589 } else {
590 r_data->bytes_xfered = r_data->blocks -
591 readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS)
592 - 1;
593 r_data->bytes_xfered *= r_data->blksz;
594 r_data->bytes_xfered += r_data->blksz
595 - readl(sock->addr + SOCK_MMCSD_BLOCK_LEN)
596 + 1;
597 }
598 host->buffer_pos = 0;
599 host->buffer_size = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700600 } else {
Alex Dubovdfef26d2007-04-12 16:59:24 +1000601 if (r_data->flags & MMC_DATA_WRITE) {
602 r_data->bytes_xfered = host->written_blocks
603 * r_data->blksz;
604 } else {
605 r_data->bytes_xfered = r_data->blocks -
606 readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS) - 1;
607 r_data->bytes_xfered *= r_data->blksz;
608 r_data->bytes_xfered += r_data->blksz -
609 readl(sock->addr + SOCK_MMCSD_BLOCK_LEN)
610 + 1;
611 }
612 tifm_unmap_sg(sock, r_data->sg, r_data->sg_len,
613 (r_data->flags & MMC_DATA_WRITE)
614 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700615 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700616 }
617
618 writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100619 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700620
621 spin_unlock_irqrestore(&sock->lock, flags);
622 mmc_request_done(mmc, mrq);
623}
624
Alex Dubov0803dd02006-12-08 16:50:49 +1100625static void tifm_sd_abort(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700626{
Alex Dubov8e02f852006-12-08 16:50:51 +1100627 struct tifm_sd *host = (struct tifm_sd*)data;
628
Alex Dubov4020f2d2006-10-04 02:15:37 -0700629 printk(KERN_ERR DRIVER_NAME
Alex Dubovb039d4a2007-04-12 16:59:21 +1000630 ": card failed to respond for a long period of time\n");
Alex Dubov8e02f852006-12-08 16:50:51 +1100631
Alex Dubov8e02f852006-12-08 16:50:51 +1100632 tifm_eject(host->dev);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700633}
634
635static void tifm_sd_ios(struct mmc_host *mmc, struct mmc_ios *ios)
636{
637 struct tifm_sd *host = mmc_priv(mmc);
638 struct tifm_dev *sock = host->dev;
639 unsigned int clk_div1, clk_div2;
640 unsigned long flags;
641
642 spin_lock_irqsave(&sock->lock, flags);
643
644 dev_dbg(&sock->dev, "Setting bus width %d, power %d\n", ios->bus_width,
645 ios->power_mode);
646 if (ios->bus_width == MMC_BUS_WIDTH_4) {
647 writel(TIFM_MMCSD_4BBUS | readl(sock->addr + SOCK_MMCSD_CONFIG),
648 sock->addr + SOCK_MMCSD_CONFIG);
649 } else {
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100650 writel((~TIFM_MMCSD_4BBUS)
651 & readl(sock->addr + SOCK_MMCSD_CONFIG),
652 sock->addr + SOCK_MMCSD_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700653 }
654
655 if (ios->clock) {
656 clk_div1 = 20000000 / ios->clock;
657 if (!clk_div1)
658 clk_div1 = 1;
659
660 clk_div2 = 24000000 / ios->clock;
661 if (!clk_div2)
662 clk_div2 = 1;
663
664 if ((20000000 / clk_div1) > ios->clock)
665 clk_div1++;
666 if ((24000000 / clk_div2) > ios->clock)
667 clk_div2++;
668 if ((20000000 / clk_div1) > (24000000 / clk_div2)) {
669 host->clk_freq = 20000000;
670 host->clk_div = clk_div1;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100671 writel((~TIFM_CTRL_FAST_CLK)
672 & readl(sock->addr + SOCK_CONTROL),
673 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700674 } else {
675 host->clk_freq = 24000000;
676 host->clk_div = clk_div2;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100677 writel(TIFM_CTRL_FAST_CLK
678 | readl(sock->addr + SOCK_CONTROL),
679 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700680 }
681 } else {
682 host->clk_div = 0;
683 }
684 host->clk_div &= TIFM_MMCSD_CLKMASK;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100685 writel(host->clk_div
686 | ((~TIFM_MMCSD_CLKMASK)
687 & readl(sock->addr + SOCK_MMCSD_CONFIG)),
688 sock->addr + SOCK_MMCSD_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700689
Alex Dubov0007d482007-04-12 16:59:23 +1000690 host->open_drain = (ios->bus_mode == MMC_BUSMODE_OPENDRAIN);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700691
692 /* chip_select : maybe later */
693 //vdd
694 //power is set before probe / after remove
Alex Dubov4020f2d2006-10-04 02:15:37 -0700695
696 spin_unlock_irqrestore(&sock->lock, flags);
697}
698
699static int tifm_sd_ro(struct mmc_host *mmc)
700{
Alex Dubov0007d482007-04-12 16:59:23 +1000701 int rc = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700702 struct tifm_sd *host = mmc_priv(mmc);
703 struct tifm_dev *sock = host->dev;
704 unsigned long flags;
705
706 spin_lock_irqsave(&sock->lock, flags);
Alex Dubov0007d482007-04-12 16:59:23 +1000707 if (TIFM_MMCSD_CARD_RO & readl(sock->addr + SOCK_PRESENT_STATE))
708 rc = 1;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700709 spin_unlock_irqrestore(&sock->lock, flags);
710 return rc;
711}
712
Alex Dubovdfef26d2007-04-12 16:59:24 +1000713static const struct mmc_host_ops tifm_sd_ops = {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700714 .request = tifm_sd_request,
715 .set_ios = tifm_sd_ios,
716 .get_ro = tifm_sd_ro
717};
718
Alex Dubov8e02f852006-12-08 16:50:51 +1100719static int tifm_sd_initialize_host(struct tifm_sd *host)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700720{
Alex Dubov8e02f852006-12-08 16:50:51 +1100721 int rc;
722 unsigned int host_status = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700723 struct tifm_dev *sock = host->dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700724
Alex Dubov8e02f852006-12-08 16:50:51 +1100725 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
726 mmiowb();
727 host->clk_div = 61;
728 host->clk_freq = 20000000;
729 writel(TIFM_MMCSD_RESET, sock->addr + SOCK_MMCSD_SYSTEM_CONTROL);
730 writel(host->clk_div | TIFM_MMCSD_POWER,
731 sock->addr + SOCK_MMCSD_CONFIG);
732
733 /* wait up to 0.51 sec for reset */
734 for (rc = 2; rc <= 256; rc <<= 1) {
735 if (1 & readl(sock->addr + SOCK_MMCSD_SYSTEM_STATUS)) {
736 rc = 0;
737 break;
738 }
739 msleep(rc);
740 }
741
742 if (rc) {
743 printk(KERN_ERR DRIVER_NAME
744 ": controller failed to reset\n");
745 return -ENODEV;
746 }
747
748 writel(0, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
749 writel(host->clk_div | TIFM_MMCSD_POWER,
750 sock->addr + SOCK_MMCSD_CONFIG);
751 writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
752
753 // command timeout fixed to 64 clocks for now
754 writel(64, sock->addr + SOCK_MMCSD_COMMAND_TO);
755 writel(TIFM_MMCSD_INAB, sock->addr + SOCK_MMCSD_COMMAND);
756
757 /* INAB should take much less than reset */
758 for (rc = 1; rc <= 16; rc <<= 1) {
759 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
760 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
761 if (!(host_status & TIFM_MMCSD_ERRMASK)
762 && (host_status & TIFM_MMCSD_EOC)) {
763 rc = 0;
764 break;
765 }
766 msleep(rc);
767 }
768
769 if (rc) {
770 printk(KERN_ERR DRIVER_NAME
771 ": card not ready - probe failed on initialization\n");
772 return -ENODEV;
773 }
774
775 writel(TIFM_MMCSD_DATAMASK | TIFM_MMCSD_ERRMASK,
776 sock->addr + SOCK_MMCSD_INT_ENABLE);
777 mmiowb();
778
779 return 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700780}
781
782static int tifm_sd_probe(struct tifm_dev *sock)
783{
784 struct mmc_host *mmc;
785 struct tifm_sd *host;
786 int rc = -EIO;
787
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100788 if (!(TIFM_SOCK_STATE_OCCUPIED
789 & readl(sock->addr + SOCK_PRESENT_STATE))) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700790 printk(KERN_WARNING DRIVER_NAME ": card gone, unexpectedly\n");
791 return rc;
792 }
793
794 mmc = mmc_alloc_host(sizeof(struct tifm_sd), &sock->dev);
795 if (!mmc)
796 return -ENOMEM;
797
798 host = mmc_priv(mmc);
Alex Dubov0007d482007-04-12 16:59:23 +1000799 host->no_dma = no_dma;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700800 tifm_set_drvdata(sock, mmc);
Alex Dubov8e02f852006-12-08 16:50:51 +1100801 host->dev = sock;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700802 host->timeout_jiffies = msecs_to_jiffies(1000);
803
Alex Dubovdfef26d2007-04-12 16:59:24 +1000804 tasklet_init(&host->finish_tasklet, tifm_sd_end_cmd,
Alex Dubov8e02f852006-12-08 16:50:51 +1100805 (unsigned long)host);
806 setup_timer(&host->timer, tifm_sd_abort, (unsigned long)host);
807
Alex Dubov4020f2d2006-10-04 02:15:37 -0700808 mmc->ops = &tifm_sd_ops;
809 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Alex Dubov8e02f852006-12-08 16:50:51 +1100810 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700811 mmc->f_min = 20000000 / 60;
812 mmc->f_max = 24000000;
813 mmc->max_hw_segs = 1;
814 mmc->max_phys_segs = 1;
Pierre Ossman55db8902006-11-21 17:55:45 +0100815 // limited by DMA counter - it's safer to stick with
816 // block counter has 11 bits though
817 mmc->max_blk_count = 256;
818 // 2k maximum hw block length
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100819 mmc->max_blk_size = 2048;
Pierre Ossman55db8902006-11-21 17:55:45 +0100820 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
821 mmc->max_seg_size = mmc->max_req_size;
Alex Dubov4552f0c2007-04-12 16:59:12 +1000822 sock->card_event = tifm_sd_card_event;
823 sock->data_event = tifm_sd_data_event;
Alex Dubov8e02f852006-12-08 16:50:51 +1100824 rc = tifm_sd_initialize_host(host);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700825
Alex Dubov8e02f852006-12-08 16:50:51 +1100826 if (!rc)
827 rc = mmc_add_host(mmc);
828 if (rc)
829 goto out_free_mmc;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700830
831 return 0;
Alex Dubov8e02f852006-12-08 16:50:51 +1100832out_free_mmc:
833 mmc_free_host(mmc);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700834 return rc;
835}
836
837static void tifm_sd_remove(struct tifm_dev *sock)
838{
839 struct mmc_host *mmc = tifm_get_drvdata(sock);
840 struct tifm_sd *host = mmc_priv(mmc);
Alex Dubovb039d4a2007-04-12 16:59:21 +1000841 unsigned long flags;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700842
Alex Dubovb039d4a2007-04-12 16:59:21 +1000843 spin_lock_irqsave(&sock->lock, flags);
Alex Dubov0007d482007-04-12 16:59:23 +1000844 host->eject = 1;
Alex Dubov592d3722007-04-12 16:59:22 +1000845 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
846 mmiowb();
847 spin_unlock_irqrestore(&sock->lock, flags);
848
849 tasklet_kill(&host->finish_tasklet);
850
851 spin_lock_irqsave(&sock->lock, flags);
Alex Dubovb039d4a2007-04-12 16:59:21 +1000852 if (host->req) {
853 writel(TIFM_FIFO_INT_SETALL,
854 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
855 writel(0, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
Alex Dubov592d3722007-04-12 16:59:22 +1000856 host->req->cmd->error = MMC_ERR_TIMEOUT;
857 if (host->req->stop)
858 host->req->stop->error = MMC_ERR_TIMEOUT;
Alex Dubovb039d4a2007-04-12 16:59:21 +1000859 tasklet_schedule(&host->finish_tasklet);
860 }
861 spin_unlock_irqrestore(&sock->lock, flags);
Alex Dubov8e02f852006-12-08 16:50:51 +1100862 mmc_remove_host(mmc);
Alex Dubov592d3722007-04-12 16:59:22 +1000863 dev_dbg(&sock->dev, "after remove\n");
Alex Dubov4020f2d2006-10-04 02:15:37 -0700864
865 /* The meaning of the bit majority in this constant is unknown. */
866 writel(0xfff8 & readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100867 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700868
Alex Dubov4020f2d2006-10-04 02:15:37 -0700869 mmc_free_host(mmc);
870}
871
Alex Dubovdba4acc2006-12-11 01:55:38 +1100872#ifdef CONFIG_PM
873
874static int tifm_sd_suspend(struct tifm_dev *sock, pm_message_t state)
875{
876 struct mmc_host *mmc = tifm_get_drvdata(sock);
877 int rc;
878
879 rc = mmc_suspend_host(mmc, state);
880 /* The meaning of the bit majority in this constant is unknown. */
881 writel(0xfff8 & readl(sock->addr + SOCK_CONTROL),
882 sock->addr + SOCK_CONTROL);
883 return rc;
884}
885
886static int tifm_sd_resume(struct tifm_dev *sock)
887{
888 struct mmc_host *mmc = tifm_get_drvdata(sock);
889 struct tifm_sd *host = mmc_priv(mmc);
890
Alex Dubove23f2b82007-04-12 16:59:14 +1000891 if (sock->type != TIFM_TYPE_SD
Alex Dubovdba4acc2006-12-11 01:55:38 +1100892 || tifm_sd_initialize_host(host)) {
893 tifm_eject(sock);
894 return 0;
895 } else {
896 return mmc_resume_host(mmc);
897 }
898}
899
900#else
901
902#define tifm_sd_suspend NULL
903#define tifm_sd_resume NULL
904
905#endif /* CONFIG_PM */
906
Alex Dubove23f2b82007-04-12 16:59:14 +1000907static struct tifm_device_id tifm_sd_id_tbl[] = {
908 { TIFM_TYPE_SD }, { }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700909};
910
911static struct tifm_driver tifm_sd_driver = {
912 .driver = {
913 .name = DRIVER_NAME,
914 .owner = THIS_MODULE
915 },
916 .id_table = tifm_sd_id_tbl,
917 .probe = tifm_sd_probe,
Alex Dubovdba4acc2006-12-11 01:55:38 +1100918 .remove = tifm_sd_remove,
919 .suspend = tifm_sd_suspend,
920 .resume = tifm_sd_resume
Alex Dubov4020f2d2006-10-04 02:15:37 -0700921};
922
923static int __init tifm_sd_init(void)
924{
925 return tifm_register_driver(&tifm_sd_driver);
926}
927
928static void __exit tifm_sd_exit(void)
929{
930 tifm_unregister_driver(&tifm_sd_driver);
931}
932
933MODULE_AUTHOR("Alex Dubov");
934MODULE_DESCRIPTION("TI FlashMedia SD driver");
935MODULE_LICENSE("GPL");
936MODULE_DEVICE_TABLE(tifm, tifm_sd_id_tbl);
937MODULE_VERSION(DRIVER_VERSION);
938
939module_init(tifm_sd_init);
940module_exit(tifm_sd_exit);