blob: fe8cb1aa681f9dc1bd526ef79debaea8aee3cf55 [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
73typedef enum {
74 IDLE = 0,
75 CMD, /* main command ended */
76 BRS, /* block transfer finished */
77 SCMD, /* stop command ended */
78 CARD, /* card left busy state */
79 FIFO, /* FIFO operation completed (uncertain) */
80 READY
81} card_state_t;
82
83enum {
84 FIFO_RDY = 0x0001, /* hardware dependent value */
Alex Dubov0007d482007-04-12 16:59:23 +100085 CARD_BUSY = 0x0010
86};
Alex Dubov4020f2d2006-10-04 02:15:37 -070087
88struct tifm_sd {
89 struct tifm_dev *dev;
90
Alex Dubov0007d482007-04-12 16:59:23 +100091 unsigned short eject:1,
92 open_drain:1,
93 no_dma:1;
94 unsigned short cmd_flags;
Alex Dubov4020f2d2006-10-04 02:15:37 -070095 card_state_t state;
96 unsigned int clk_freq;
97 unsigned int clk_div;
Alex Dubov0803dd02006-12-08 16:50:49 +110098 unsigned long timeout_jiffies;
Alex Dubov4020f2d2006-10-04 02:15:37 -070099
Alex Dubov8e02f852006-12-08 16:50:51 +1100100 struct tasklet_struct finish_tasklet;
Alex Dubov0803dd02006-12-08 16:50:49 +1100101 struct timer_list timer;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700102 struct mmc_request *req;
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 Dubov0007d482007-04-12 16:59:23 +1000124 if (host->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 Dubov0007d482007-04-12 16:59:23 +1000134 } else if (host->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;
Alex Dubov0007d482007-04-12 16:59:23 +1000209 unsigned int cmd_mask = tifm_sd_op_flags(cmd);
210
211 if (host->open_drain)
212 cmd_mask |= TIFM_MMCSD_ODTO;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700213
214 if (cmd->data && (cmd->data->flags & MMC_DATA_READ))
215 cmd_mask |= TIFM_MMCSD_READ;
216
217 dev_dbg(&sock->dev, "executing opcode 0x%x, arg: 0x%x, mask: 0x%x\n",
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100218 cmd->opcode, cmd->arg, cmd_mask);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700219
220 writel((cmd->arg >> 16) & 0xffff, sock->addr + SOCK_MMCSD_ARG_HIGH);
221 writel(cmd->arg & 0xffff, sock->addr + SOCK_MMCSD_ARG_LOW);
222 writel(cmd->opcode | cmd_mask, sock->addr + SOCK_MMCSD_COMMAND);
223}
224
225static void tifm_sd_fetch_resp(struct mmc_command *cmd, struct tifm_dev *sock)
226{
227 cmd->resp[0] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x1c) << 16)
228 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x18);
229 cmd->resp[1] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x14) << 16)
230 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x10);
231 cmd->resp[2] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x0c) << 16)
232 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x08);
233 cmd->resp[3] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x04) << 16)
234 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x00);
235}
236
237static void tifm_sd_process_cmd(struct tifm_dev *sock, struct tifm_sd *host,
238 unsigned int host_status)
239{
240 struct mmc_command *cmd = host->req->cmd;
241
242change_state:
243 switch (host->state) {
244 case IDLE:
245 return;
246 case CMD:
Alex Dubov12c83452007-02-10 21:16:40 -0800247 if (host_status & (TIFM_MMCSD_EOC | TIFM_MMCSD_CERR)) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700248 tifm_sd_fetch_resp(cmd, sock);
249 if (cmd->data) {
250 host->state = BRS;
Alex Dubov12893352006-12-08 16:50:47 +1100251 } else {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700252 host->state = READY;
Alex Dubov12893352006-12-08 16:50:47 +1100253 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700254 goto change_state;
255 }
256 break;
257 case BRS:
258 if (tifm_sd_transfer_data(sock, host, host_status)) {
Alex Dubov12893352006-12-08 16:50:47 +1100259 if (cmd->data->flags & MMC_DATA_WRITE) {
260 host->state = CARD;
261 } else {
Alex Dubov0007d482007-04-12 16:59:23 +1000262 if (host->no_dma) {
Alex Dubov12893352006-12-08 16:50:47 +1100263 if (host->req->stop) {
264 tifm_sd_exec(host, host->req->stop);
265 host->state = SCMD;
266 } else {
267 host->state = READY;
268 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700269 } else {
Alex Dubov12893352006-12-08 16:50:47 +1100270 host->state = FIFO;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700271 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700272 }
Alex Dubov12893352006-12-08 16:50:47 +1100273 goto change_state;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700274 }
275 break;
276 case SCMD:
277 if (host_status & TIFM_MMCSD_EOC) {
278 tifm_sd_fetch_resp(host->req->stop, sock);
Alex Dubov12893352006-12-08 16:50:47 +1100279 host->state = READY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700280 goto change_state;
281 }
282 break;
283 case CARD:
Alex Dubov12893352006-12-08 16:50:47 +1100284 dev_dbg(&sock->dev, "waiting for CARD, have %zd blocks\n",
285 host->written_blocks);
Alex Dubov0007d482007-04-12 16:59:23 +1000286 if (!(host->cmd_flags & CARD_BUSY)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700287 && (host->written_blocks == cmd->data->blocks)) {
Alex Dubov0007d482007-04-12 16:59:23 +1000288 if (host->no_dma) {
Alex Dubov12893352006-12-08 16:50:47 +1100289 if (host->req->stop) {
290 tifm_sd_exec(host, host->req->stop);
291 host->state = SCMD;
292 } else {
293 host->state = READY;
294 }
295 } else {
296 host->state = FIFO;
297 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700298 goto change_state;
299 }
300 break;
301 case FIFO:
Alex Dubov0007d482007-04-12 16:59:23 +1000302 if (host->cmd_flags & FIFO_RDY) {
303 host->cmd_flags &= ~FIFO_RDY;
Alex Dubov12893352006-12-08 16:50:47 +1100304 if (host->req->stop) {
305 tifm_sd_exec(host, host->req->stop);
306 host->state = SCMD;
307 } else {
308 host->state = READY;
309 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700310 goto change_state;
311 }
312 break;
313 case READY:
Alex Dubov8e02f852006-12-08 16:50:51 +1100314 tasklet_schedule(&host->finish_tasklet);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700315 return;
316 }
317
Alex Dubov4020f2d2006-10-04 02:15:37 -0700318}
319
320/* Called from interrupt handler */
Alex Dubov4552f0c2007-04-12 16:59:12 +1000321static void tifm_sd_data_event(struct tifm_dev *sock)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700322{
323 struct tifm_sd *host;
Alex Dubov4552f0c2007-04-12 16:59:12 +1000324 unsigned int fifo_status = 0;
325
326 spin_lock(&sock->lock);
327 host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock));
328
329 fifo_status = readl(sock->addr + SOCK_DMA_FIFO_STATUS);
330 writel(fifo_status, sock->addr + SOCK_DMA_FIFO_STATUS);
331
Alex Dubov0007d482007-04-12 16:59:23 +1000332 host->cmd_flags |= fifo_status & FIFO_RDY;
Alex Dubov4552f0c2007-04-12 16:59:12 +1000333
334 if (host->req)
335 tifm_sd_process_cmd(sock, host, 0);
336
337 dev_dbg(&sock->dev, "fifo_status %x\n", fifo_status);
338 spin_unlock(&sock->lock);
339
340}
341
342/* Called from interrupt handler */
343static void tifm_sd_card_event(struct tifm_dev *sock)
344{
345 struct tifm_sd *host;
346 unsigned int host_status = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700347 int error_code = 0;
348
349 spin_lock(&sock->lock);
350 host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock));
Alex Dubov4020f2d2006-10-04 02:15:37 -0700351
Alex Dubov4020f2d2006-10-04 02:15:37 -0700352
Alex Dubov4020f2d2006-10-04 02:15:37 -0700353 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
354 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
355
Alex Dubov4020f2d2006-10-04 02:15:37 -0700356 if (!host->req)
357 goto done;
358
359 if (host_status & TIFM_MMCSD_ERRMASK) {
Alex Dubov12c83452007-02-10 21:16:40 -0800360 if (host_status & (TIFM_MMCSD_CTO | TIFM_MMCSD_DTO))
Alex Dubov4020f2d2006-10-04 02:15:37 -0700361 error_code = MMC_ERR_TIMEOUT;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100362 else if (host_status
363 & (TIFM_MMCSD_CCRC | TIFM_MMCSD_DCRC))
Alex Dubov4020f2d2006-10-04 02:15:37 -0700364 error_code = MMC_ERR_BADCRC;
365
366 writel(TIFM_FIFO_INT_SETALL,
367 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
368 writel(TIFM_DMA_RESET, sock->addr + SOCK_DMA_CONTROL);
369
370 if (host->req->stop) {
371 if (host->state == SCMD) {
372 host->req->stop->error = error_code;
Alex Dubov12893352006-12-08 16:50:47 +1100373 } else if (host->state == BRS
374 || host->state == CARD
375 || host->state == FIFO) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700376 host->req->cmd->error = error_code;
377 tifm_sd_exec(host, host->req->stop);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700378 host->state = SCMD;
379 goto done;
380 } else {
381 host->req->cmd->error = error_code;
382 }
383 } else {
384 host->req->cmd->error = error_code;
385 }
386 host->state = READY;
387 }
388
389 if (host_status & TIFM_MMCSD_CB)
Alex Dubov0007d482007-04-12 16:59:23 +1000390 host->cmd_flags |= CARD_BUSY;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100391 if ((host_status & TIFM_MMCSD_EOFB)
Alex Dubov0007d482007-04-12 16:59:23 +1000392 && (host->cmd_flags & CARD_BUSY)) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700393 host->written_blocks++;
Alex Dubov0007d482007-04-12 16:59:23 +1000394 host->cmd_flags &= ~CARD_BUSY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700395 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700396
397 if (host->req)
398 tifm_sd_process_cmd(sock, host, host_status);
399done:
Alex Dubov4552f0c2007-04-12 16:59:12 +1000400 dev_dbg(&sock->dev, "host_status %x\n", host_status);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700401 spin_unlock(&sock->lock);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700402}
403
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100404static void tifm_sd_prepare_data(struct tifm_sd *host, struct mmc_command *cmd)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700405{
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100406 struct tifm_dev *sock = host->dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700407 unsigned int dest_cnt;
408
409 /* DMA style IO */
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100410 dev_dbg(&sock->dev, "setting dma for %d blocks\n",
411 cmd->data->blocks);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700412 writel(TIFM_FIFO_INT_SETALL,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100413 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
David Howellsf0d1b0b2006-12-08 02:37:49 -0800414 writel(ilog2(cmd->data->blksz) - 2,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100415 sock->addr + SOCK_FIFO_PAGE_SIZE);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700416 writel(TIFM_FIFO_ENABLE, sock->addr + SOCK_FIFO_CONTROL);
417 writel(TIFM_FIFO_INTMASK, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
418
419 dest_cnt = (cmd->data->blocks) << 8;
420
421 writel(sg_dma_address(cmd->data->sg), sock->addr + SOCK_DMA_ADDRESS);
422
423 writel(cmd->data->blocks - 1, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
424 writel(cmd->data->blksz - 1, sock->addr + SOCK_MMCSD_BLOCK_LEN);
425
426 if (cmd->data->flags & MMC_DATA_WRITE) {
427 writel(TIFM_MMCSD_TXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
428 writel(dest_cnt | TIFM_DMA_TX | TIFM_DMA_EN,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100429 sock->addr + SOCK_DMA_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700430 } else {
431 writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
432 writel(dest_cnt | TIFM_DMA_EN, sock->addr + SOCK_DMA_CONTROL);
433 }
434}
435
436static void tifm_sd_set_data_timeout(struct tifm_sd *host,
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100437 struct mmc_data *data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700438{
439 struct tifm_dev *sock = host->dev;
440 unsigned int data_timeout = data->timeout_clks;
441
442 if (fixed_timeout)
443 return;
444
445 data_timeout += data->timeout_ns /
Alex Dubov83d420b2006-12-08 16:50:50 +1100446 ((1000000000UL / host->clk_freq) * host->clk_div);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700447
448 if (data_timeout < 0xffff) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700449 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
Alex Dubov83d420b2006-12-08 16:50:50 +1100450 writel((~TIFM_MMCSD_DPE)
451 & readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
452 sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700453 } else {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700454 data_timeout = (data_timeout >> 10) + 1;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100455 if (data_timeout > 0xffff)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700456 data_timeout = 0; /* set to unlimited */
457 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
Alex Dubov83d420b2006-12-08 16:50:50 +1100458 writel(TIFM_MMCSD_DPE
459 | readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
460 sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700461 }
462}
463
464static void tifm_sd_request(struct mmc_host *mmc, struct mmc_request *mrq)
465{
466 struct tifm_sd *host = mmc_priv(mmc);
467 struct tifm_dev *sock = host->dev;
468 unsigned long flags;
469 int sg_count = 0;
470 struct mmc_data *r_data = mrq->cmd->data;
471
472 spin_lock_irqsave(&sock->lock, flags);
Alex Dubov0007d482007-04-12 16:59:23 +1000473 if (host->eject) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700474 spin_unlock_irqrestore(&sock->lock, flags);
475 goto err_out;
476 }
477
478 if (host->req) {
479 printk(KERN_ERR DRIVER_NAME ": unfinished request detected\n");
480 spin_unlock_irqrestore(&sock->lock, flags);
481 goto err_out;
482 }
483
484 if (r_data) {
485 tifm_sd_set_data_timeout(host, r_data);
486
487 sg_count = tifm_map_sg(sock, r_data->sg, r_data->sg_len,
488 mrq->cmd->flags & MMC_DATA_WRITE
489 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
490 if (sg_count != 1) {
491 printk(KERN_ERR DRIVER_NAME
492 ": scatterlist map failed\n");
493 spin_unlock_irqrestore(&sock->lock, flags);
494 goto err_out;
495 }
496
497 host->written_blocks = 0;
Alex Dubov0007d482007-04-12 16:59:23 +1000498 host->cmd_flags &= ~CARD_BUSY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700499 tifm_sd_prepare_data(host, mrq->cmd);
500 }
501
502 host->req = mrq;
Alex Dubov0803dd02006-12-08 16:50:49 +1100503 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700504 host->state = CMD;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700505 writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100506 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700507 tifm_sd_exec(host, mrq->cmd);
508 spin_unlock_irqrestore(&sock->lock, flags);
509 return;
510
511err_out:
512 if (sg_count > 0)
513 tifm_unmap_sg(sock, r_data->sg, r_data->sg_len,
514 (r_data->flags & MMC_DATA_WRITE)
515 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
516
517 mrq->cmd->error = MMC_ERR_TIMEOUT;
518 mmc_request_done(mmc, mrq);
519}
520
Alex Dubov8e02f852006-12-08 16:50:51 +1100521static void tifm_sd_end_cmd(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700522{
Alex Dubov8e02f852006-12-08 16:50:51 +1100523 struct tifm_sd *host = (struct tifm_sd*)data;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700524 struct tifm_dev *sock = host->dev;
525 struct mmc_host *mmc = tifm_get_drvdata(sock);
526 struct mmc_request *mrq;
Al Viroe069d792006-10-09 20:29:43 +0100527 struct mmc_data *r_data = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700528 unsigned long flags;
529
530 spin_lock_irqsave(&sock->lock, flags);
531
Alex Dubov0803dd02006-12-08 16:50:49 +1100532 del_timer(&host->timer);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700533 mrq = host->req;
Al Viroe069d792006-10-09 20:29:43 +0100534 host->req = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700535 host->state = IDLE;
536
537 if (!mrq) {
538 printk(KERN_ERR DRIVER_NAME ": no request to complete?\n");
539 spin_unlock_irqrestore(&sock->lock, flags);
540 return;
541 }
542
543 r_data = mrq->cmd->data;
544 if (r_data) {
545 if (r_data->flags & MMC_DATA_WRITE) {
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100546 r_data->bytes_xfered = host->written_blocks
547 * r_data->blksz;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700548 } else {
549 r_data->bytes_xfered = r_data->blocks -
550 readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS) - 1;
551 r_data->bytes_xfered *= r_data->blksz;
552 r_data->bytes_xfered += r_data->blksz -
553 readl(sock->addr + SOCK_MMCSD_BLOCK_LEN) + 1;
554 }
555 tifm_unmap_sg(sock, r_data->sg, r_data->sg_len,
556 (r_data->flags & MMC_DATA_WRITE)
557 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
558 }
559
560 writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100561 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700562
563 spin_unlock_irqrestore(&sock->lock, flags);
564 mmc_request_done(mmc, mrq);
565}
566
567static void tifm_sd_request_nodma(struct mmc_host *mmc, struct mmc_request *mrq)
568{
569 struct tifm_sd *host = mmc_priv(mmc);
570 struct tifm_dev *sock = host->dev;
571 unsigned long flags;
572 struct mmc_data *r_data = mrq->cmd->data;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700573
574 spin_lock_irqsave(&sock->lock, flags);
Alex Dubov0007d482007-04-12 16:59:23 +1000575 if (host->eject) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700576 spin_unlock_irqrestore(&sock->lock, flags);
577 goto err_out;
578 }
579
580 if (host->req) {
581 printk(KERN_ERR DRIVER_NAME ": unfinished request detected\n");
582 spin_unlock_irqrestore(&sock->lock, flags);
583 goto err_out;
584 }
585
586 if (r_data) {
587 tifm_sd_set_data_timeout(host, r_data);
588
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100589 host->buffer_size = mrq->cmd->data->blocks
590 * mrq->cmd->data->blksz;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700591
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100592 writel(TIFM_MMCSD_BUFINT
593 | readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
Alex Dubov4020f2d2006-10-04 02:15:37 -0700594 sock->addr + SOCK_MMCSD_INT_ENABLE);
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100595 writel(((TIFM_MMCSD_FIFO_SIZE - 1) << 8)
596 | (TIFM_MMCSD_FIFO_SIZE - 1),
Alex Dubov4020f2d2006-10-04 02:15:37 -0700597 sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
598
599 host->written_blocks = 0;
Alex Dubov0007d482007-04-12 16:59:23 +1000600 host->cmd_flags &= ~CARD_BUSY;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700601 host->buffer_pos = 0;
602 writel(r_data->blocks - 1, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
603 writel(r_data->blksz - 1, sock->addr + SOCK_MMCSD_BLOCK_LEN);
604 }
605
606 host->req = mrq;
Alex Dubov0803dd02006-12-08 16:50:49 +1100607 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700608 host->state = CMD;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700609 writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100610 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700611 tifm_sd_exec(host, mrq->cmd);
612 spin_unlock_irqrestore(&sock->lock, flags);
613 return;
614
615err_out:
Alex Dubov4020f2d2006-10-04 02:15:37 -0700616 mrq->cmd->error = MMC_ERR_TIMEOUT;
617 mmc_request_done(mmc, mrq);
618}
619
Alex Dubov8e02f852006-12-08 16:50:51 +1100620static void tifm_sd_end_cmd_nodma(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700621{
Alex Dubov8e02f852006-12-08 16:50:51 +1100622 struct tifm_sd *host = (struct tifm_sd*)data;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700623 struct tifm_dev *sock = host->dev;
624 struct mmc_host *mmc = tifm_get_drvdata(sock);
625 struct mmc_request *mrq;
Al Viroe069d792006-10-09 20:29:43 +0100626 struct mmc_data *r_data = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700627 unsigned long flags;
628
629 spin_lock_irqsave(&sock->lock, flags);
630
Alex Dubov0803dd02006-12-08 16:50:49 +1100631 del_timer(&host->timer);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700632 mrq = host->req;
Al Viroe069d792006-10-09 20:29:43 +0100633 host->req = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700634 host->state = IDLE;
635
636 if (!mrq) {
637 printk(KERN_ERR DRIVER_NAME ": no request to complete?\n");
638 spin_unlock_irqrestore(&sock->lock, flags);
639 return;
640 }
641
642 r_data = mrq->cmd->data;
643 if (r_data) {
644 writel((~TIFM_MMCSD_BUFINT) &
645 readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
646 sock->addr + SOCK_MMCSD_INT_ENABLE);
647
648 if (r_data->flags & MMC_DATA_WRITE) {
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100649 r_data->bytes_xfered = host->written_blocks
650 * r_data->blksz;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700651 } else {
652 r_data->bytes_xfered = r_data->blocks -
653 readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS) - 1;
654 r_data->bytes_xfered *= r_data->blksz;
655 r_data->bytes_xfered += r_data->blksz -
656 readl(sock->addr + SOCK_MMCSD_BLOCK_LEN) + 1;
657 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700658 host->buffer_pos = 0;
659 host->buffer_size = 0;
660 }
661
662 writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100663 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700664
665 spin_unlock_irqrestore(&sock->lock, flags);
666
Alex Dubov4020f2d2006-10-04 02:15:37 -0700667 mmc_request_done(mmc, mrq);
668}
669
Alex Dubov0803dd02006-12-08 16:50:49 +1100670static void tifm_sd_abort(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700671{
Alex Dubov8e02f852006-12-08 16:50:51 +1100672 struct tifm_sd *host = (struct tifm_sd*)data;
673
Alex Dubov4020f2d2006-10-04 02:15:37 -0700674 printk(KERN_ERR DRIVER_NAME
Alex Dubovb039d4a2007-04-12 16:59:21 +1000675 ": card failed to respond for a long period of time\n");
Alex Dubov8e02f852006-12-08 16:50:51 +1100676
Alex Dubov8e02f852006-12-08 16:50:51 +1100677 tifm_eject(host->dev);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700678}
679
680static void tifm_sd_ios(struct mmc_host *mmc, struct mmc_ios *ios)
681{
682 struct tifm_sd *host = mmc_priv(mmc);
683 struct tifm_dev *sock = host->dev;
684 unsigned int clk_div1, clk_div2;
685 unsigned long flags;
686
687 spin_lock_irqsave(&sock->lock, flags);
688
689 dev_dbg(&sock->dev, "Setting bus width %d, power %d\n", ios->bus_width,
690 ios->power_mode);
691 if (ios->bus_width == MMC_BUS_WIDTH_4) {
692 writel(TIFM_MMCSD_4BBUS | readl(sock->addr + SOCK_MMCSD_CONFIG),
693 sock->addr + SOCK_MMCSD_CONFIG);
694 } else {
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100695 writel((~TIFM_MMCSD_4BBUS)
696 & readl(sock->addr + SOCK_MMCSD_CONFIG),
697 sock->addr + SOCK_MMCSD_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700698 }
699
700 if (ios->clock) {
701 clk_div1 = 20000000 / ios->clock;
702 if (!clk_div1)
703 clk_div1 = 1;
704
705 clk_div2 = 24000000 / ios->clock;
706 if (!clk_div2)
707 clk_div2 = 1;
708
709 if ((20000000 / clk_div1) > ios->clock)
710 clk_div1++;
711 if ((24000000 / clk_div2) > ios->clock)
712 clk_div2++;
713 if ((20000000 / clk_div1) > (24000000 / clk_div2)) {
714 host->clk_freq = 20000000;
715 host->clk_div = clk_div1;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100716 writel((~TIFM_CTRL_FAST_CLK)
717 & readl(sock->addr + SOCK_CONTROL),
718 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700719 } else {
720 host->clk_freq = 24000000;
721 host->clk_div = clk_div2;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100722 writel(TIFM_CTRL_FAST_CLK
723 | readl(sock->addr + SOCK_CONTROL),
724 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700725 }
726 } else {
727 host->clk_div = 0;
728 }
729 host->clk_div &= TIFM_MMCSD_CLKMASK;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100730 writel(host->clk_div
731 | ((~TIFM_MMCSD_CLKMASK)
732 & readl(sock->addr + SOCK_MMCSD_CONFIG)),
733 sock->addr + SOCK_MMCSD_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700734
Alex Dubov0007d482007-04-12 16:59:23 +1000735 host->open_drain = (ios->bus_mode == MMC_BUSMODE_OPENDRAIN);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700736
737 /* chip_select : maybe later */
738 //vdd
739 //power is set before probe / after remove
Alex Dubov4020f2d2006-10-04 02:15:37 -0700740
741 spin_unlock_irqrestore(&sock->lock, flags);
742}
743
744static int tifm_sd_ro(struct mmc_host *mmc)
745{
Alex Dubov0007d482007-04-12 16:59:23 +1000746 int rc = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700747 struct tifm_sd *host = mmc_priv(mmc);
748 struct tifm_dev *sock = host->dev;
749 unsigned long flags;
750
751 spin_lock_irqsave(&sock->lock, flags);
Alex Dubov0007d482007-04-12 16:59:23 +1000752 if (TIFM_MMCSD_CARD_RO & readl(sock->addr + SOCK_PRESENT_STATE))
753 rc = 1;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700754 spin_unlock_irqrestore(&sock->lock, flags);
755 return rc;
756}
757
758static struct mmc_host_ops tifm_sd_ops = {
759 .request = tifm_sd_request,
760 .set_ios = tifm_sd_ios,
761 .get_ro = tifm_sd_ro
762};
763
Alex Dubov8e02f852006-12-08 16:50:51 +1100764static int tifm_sd_initialize_host(struct tifm_sd *host)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700765{
Alex Dubov8e02f852006-12-08 16:50:51 +1100766 int rc;
767 unsigned int host_status = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700768 struct tifm_dev *sock = host->dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700769
Alex Dubov8e02f852006-12-08 16:50:51 +1100770 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
771 mmiowb();
772 host->clk_div = 61;
773 host->clk_freq = 20000000;
774 writel(TIFM_MMCSD_RESET, sock->addr + SOCK_MMCSD_SYSTEM_CONTROL);
775 writel(host->clk_div | TIFM_MMCSD_POWER,
776 sock->addr + SOCK_MMCSD_CONFIG);
777
778 /* wait up to 0.51 sec for reset */
779 for (rc = 2; rc <= 256; rc <<= 1) {
780 if (1 & readl(sock->addr + SOCK_MMCSD_SYSTEM_STATUS)) {
781 rc = 0;
782 break;
783 }
784 msleep(rc);
785 }
786
787 if (rc) {
788 printk(KERN_ERR DRIVER_NAME
789 ": controller failed to reset\n");
790 return -ENODEV;
791 }
792
793 writel(0, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
794 writel(host->clk_div | TIFM_MMCSD_POWER,
795 sock->addr + SOCK_MMCSD_CONFIG);
796 writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
797
798 // command timeout fixed to 64 clocks for now
799 writel(64, sock->addr + SOCK_MMCSD_COMMAND_TO);
800 writel(TIFM_MMCSD_INAB, sock->addr + SOCK_MMCSD_COMMAND);
801
802 /* INAB should take much less than reset */
803 for (rc = 1; rc <= 16; rc <<= 1) {
804 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
805 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
806 if (!(host_status & TIFM_MMCSD_ERRMASK)
807 && (host_status & TIFM_MMCSD_EOC)) {
808 rc = 0;
809 break;
810 }
811 msleep(rc);
812 }
813
814 if (rc) {
815 printk(KERN_ERR DRIVER_NAME
816 ": card not ready - probe failed on initialization\n");
817 return -ENODEV;
818 }
819
820 writel(TIFM_MMCSD_DATAMASK | TIFM_MMCSD_ERRMASK,
821 sock->addr + SOCK_MMCSD_INT_ENABLE);
822 mmiowb();
823
824 return 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700825}
826
827static int tifm_sd_probe(struct tifm_dev *sock)
828{
829 struct mmc_host *mmc;
830 struct tifm_sd *host;
831 int rc = -EIO;
832
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100833 if (!(TIFM_SOCK_STATE_OCCUPIED
834 & readl(sock->addr + SOCK_PRESENT_STATE))) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700835 printk(KERN_WARNING DRIVER_NAME ": card gone, unexpectedly\n");
836 return rc;
837 }
838
839 mmc = mmc_alloc_host(sizeof(struct tifm_sd), &sock->dev);
840 if (!mmc)
841 return -ENOMEM;
842
843 host = mmc_priv(mmc);
Alex Dubov0007d482007-04-12 16:59:23 +1000844 host->no_dma = no_dma;
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,
Alex Dubov0007d482007-04-12 16:59:23 +1000850 host->no_dma ? tifm_sd_end_cmd_nodma : tifm_sd_end_cmd,
Alex Dubov8e02f852006-12-08 16:50:51 +1100851 (unsigned long)host);
852 setup_timer(&host->timer, tifm_sd_abort, (unsigned long)host);
853
Alex Dubov0007d482007-04-12 16:59:23 +1000854 tifm_sd_ops.request = host->no_dma ? tifm_sd_request_nodma : tifm_sd_request;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700855 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);
Alex Dubov0007d482007-04-12 16:59:23 +1000891 host->eject = 1;
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);