blob: 52499548abe834632a3af07b737a8d0fa9a0130b [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
Alex Dubovdfef26d2007-04-12 16:59:24 +1000487 if (host->no_dma) {
488 host->buffer_size = mrq->cmd->data->blocks
489 * mrq->cmd->data->blksz;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700490
Alex Dubovdfef26d2007-04-12 16:59:24 +1000491 writel(TIFM_MMCSD_BUFINT
492 | readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
493 sock->addr + SOCK_MMCSD_INT_ENABLE);
494 writel(((TIFM_MMCSD_FIFO_SIZE - 1) << 8)
495 | (TIFM_MMCSD_FIFO_SIZE - 1),
496 sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
497
498 host->written_blocks = 0;
499 host->cmd_flags &= ~CARD_BUSY;
500 host->buffer_pos = 0;
501 writel(r_data->blocks - 1,
502 sock->addr + SOCK_MMCSD_NUM_BLOCKS);
503 writel(r_data->blksz - 1,
504 sock->addr + SOCK_MMCSD_BLOCK_LEN);
505 } else {
506 sg_count = tifm_map_sg(sock, r_data->sg, r_data->sg_len,
507 mrq->cmd->flags & MMC_DATA_WRITE
508 ? PCI_DMA_TODEVICE
509 : PCI_DMA_FROMDEVICE);
510 if (sg_count != 1) {
511 printk(KERN_ERR DRIVER_NAME
512 ": scatterlist map failed\n");
513 spin_unlock_irqrestore(&sock->lock, flags);
514 goto err_out;
515 }
516
517 host->written_blocks = 0;
518 host->cmd_flags &= ~CARD_BUSY;
519 tifm_sd_prepare_data(host, mrq->cmd);
520 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700521 }
522
523 host->req = mrq;
Alex Dubov0803dd02006-12-08 16:50:49 +1100524 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700525 host->state = CMD;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700526 writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100527 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700528 tifm_sd_exec(host, mrq->cmd);
529 spin_unlock_irqrestore(&sock->lock, flags);
530 return;
531
532err_out:
533 if (sg_count > 0)
534 tifm_unmap_sg(sock, r_data->sg, r_data->sg_len,
535 (r_data->flags & MMC_DATA_WRITE)
536 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
537
538 mrq->cmd->error = MMC_ERR_TIMEOUT;
539 mmc_request_done(mmc, mrq);
540}
541
Alex Dubov8e02f852006-12-08 16:50:51 +1100542static void tifm_sd_end_cmd(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700543{
Alex Dubov8e02f852006-12-08 16:50:51 +1100544 struct tifm_sd *host = (struct tifm_sd*)data;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700545 struct tifm_dev *sock = host->dev;
546 struct mmc_host *mmc = tifm_get_drvdata(sock);
547 struct mmc_request *mrq;
Al Viroe069d792006-10-09 20:29:43 +0100548 struct mmc_data *r_data = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700549 unsigned long flags;
550
551 spin_lock_irqsave(&sock->lock, flags);
552
Alex Dubov0803dd02006-12-08 16:50:49 +1100553 del_timer(&host->timer);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700554 mrq = host->req;
Al Viroe069d792006-10-09 20:29:43 +0100555 host->req = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700556 host->state = IDLE;
557
558 if (!mrq) {
559 printk(KERN_ERR DRIVER_NAME ": no request to complete?\n");
560 spin_unlock_irqrestore(&sock->lock, flags);
561 return;
562 }
563
564 r_data = mrq->cmd->data;
565 if (r_data) {
Alex Dubovdfef26d2007-04-12 16:59:24 +1000566 if (host->no_dma) {
567 writel((~TIFM_MMCSD_BUFINT) &
568 readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
569 sock->addr + SOCK_MMCSD_INT_ENABLE);
570
571 if (r_data->flags & MMC_DATA_WRITE) {
572 r_data->bytes_xfered = host->written_blocks
573 * r_data->blksz;
574 } else {
575 r_data->bytes_xfered = r_data->blocks -
576 readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS)
577 - 1;
578 r_data->bytes_xfered *= r_data->blksz;
579 r_data->bytes_xfered += r_data->blksz
580 - readl(sock->addr + SOCK_MMCSD_BLOCK_LEN)
581 + 1;
582 }
583 host->buffer_pos = 0;
584 host->buffer_size = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700585 } else {
Alex Dubovdfef26d2007-04-12 16:59:24 +1000586 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) - 1;
592 r_data->bytes_xfered *= r_data->blksz;
593 r_data->bytes_xfered += r_data->blksz -
594 readl(sock->addr + SOCK_MMCSD_BLOCK_LEN)
595 + 1;
596 }
597 tifm_unmap_sg(sock, r_data->sg, r_data->sg_len,
598 (r_data->flags & MMC_DATA_WRITE)
599 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700600 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700601 }
602
603 writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100604 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700605
606 spin_unlock_irqrestore(&sock->lock, flags);
607 mmc_request_done(mmc, mrq);
608}
609
Alex Dubov0803dd02006-12-08 16:50:49 +1100610static void tifm_sd_abort(unsigned long data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700611{
Alex Dubov8e02f852006-12-08 16:50:51 +1100612 struct tifm_sd *host = (struct tifm_sd*)data;
613
Alex Dubov4020f2d2006-10-04 02:15:37 -0700614 printk(KERN_ERR DRIVER_NAME
Alex Dubovb039d4a2007-04-12 16:59:21 +1000615 ": card failed to respond for a long period of time\n");
Alex Dubov8e02f852006-12-08 16:50:51 +1100616
Alex Dubov8e02f852006-12-08 16:50:51 +1100617 tifm_eject(host->dev);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700618}
619
620static void tifm_sd_ios(struct mmc_host *mmc, struct mmc_ios *ios)
621{
622 struct tifm_sd *host = mmc_priv(mmc);
623 struct tifm_dev *sock = host->dev;
624 unsigned int clk_div1, clk_div2;
625 unsigned long flags;
626
627 spin_lock_irqsave(&sock->lock, flags);
628
629 dev_dbg(&sock->dev, "Setting bus width %d, power %d\n", ios->bus_width,
630 ios->power_mode);
631 if (ios->bus_width == MMC_BUS_WIDTH_4) {
632 writel(TIFM_MMCSD_4BBUS | readl(sock->addr + SOCK_MMCSD_CONFIG),
633 sock->addr + SOCK_MMCSD_CONFIG);
634 } else {
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100635 writel((~TIFM_MMCSD_4BBUS)
636 & readl(sock->addr + SOCK_MMCSD_CONFIG),
637 sock->addr + SOCK_MMCSD_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700638 }
639
640 if (ios->clock) {
641 clk_div1 = 20000000 / ios->clock;
642 if (!clk_div1)
643 clk_div1 = 1;
644
645 clk_div2 = 24000000 / ios->clock;
646 if (!clk_div2)
647 clk_div2 = 1;
648
649 if ((20000000 / clk_div1) > ios->clock)
650 clk_div1++;
651 if ((24000000 / clk_div2) > ios->clock)
652 clk_div2++;
653 if ((20000000 / clk_div1) > (24000000 / clk_div2)) {
654 host->clk_freq = 20000000;
655 host->clk_div = clk_div1;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100656 writel((~TIFM_CTRL_FAST_CLK)
657 & readl(sock->addr + SOCK_CONTROL),
658 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700659 } else {
660 host->clk_freq = 24000000;
661 host->clk_div = clk_div2;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100662 writel(TIFM_CTRL_FAST_CLK
663 | readl(sock->addr + SOCK_CONTROL),
664 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700665 }
666 } else {
667 host->clk_div = 0;
668 }
669 host->clk_div &= TIFM_MMCSD_CLKMASK;
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100670 writel(host->clk_div
671 | ((~TIFM_MMCSD_CLKMASK)
672 & readl(sock->addr + SOCK_MMCSD_CONFIG)),
673 sock->addr + SOCK_MMCSD_CONFIG);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700674
Alex Dubov0007d482007-04-12 16:59:23 +1000675 host->open_drain = (ios->bus_mode == MMC_BUSMODE_OPENDRAIN);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700676
677 /* chip_select : maybe later */
678 //vdd
679 //power is set before probe / after remove
Alex Dubov4020f2d2006-10-04 02:15:37 -0700680
681 spin_unlock_irqrestore(&sock->lock, flags);
682}
683
684static int tifm_sd_ro(struct mmc_host *mmc)
685{
Alex Dubov0007d482007-04-12 16:59:23 +1000686 int rc = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700687 struct tifm_sd *host = mmc_priv(mmc);
688 struct tifm_dev *sock = host->dev;
689 unsigned long flags;
690
691 spin_lock_irqsave(&sock->lock, flags);
Alex Dubov0007d482007-04-12 16:59:23 +1000692 if (TIFM_MMCSD_CARD_RO & readl(sock->addr + SOCK_PRESENT_STATE))
693 rc = 1;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700694 spin_unlock_irqrestore(&sock->lock, flags);
695 return rc;
696}
697
Alex Dubovdfef26d2007-04-12 16:59:24 +1000698static const struct mmc_host_ops tifm_sd_ops = {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700699 .request = tifm_sd_request,
700 .set_ios = tifm_sd_ios,
701 .get_ro = tifm_sd_ro
702};
703
Alex Dubov8e02f852006-12-08 16:50:51 +1100704static int tifm_sd_initialize_host(struct tifm_sd *host)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700705{
Alex Dubov8e02f852006-12-08 16:50:51 +1100706 int rc;
707 unsigned int host_status = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700708 struct tifm_dev *sock = host->dev;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700709
Alex Dubov8e02f852006-12-08 16:50:51 +1100710 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
711 mmiowb();
712 host->clk_div = 61;
713 host->clk_freq = 20000000;
714 writel(TIFM_MMCSD_RESET, sock->addr + SOCK_MMCSD_SYSTEM_CONTROL);
715 writel(host->clk_div | TIFM_MMCSD_POWER,
716 sock->addr + SOCK_MMCSD_CONFIG);
717
718 /* wait up to 0.51 sec for reset */
719 for (rc = 2; rc <= 256; rc <<= 1) {
720 if (1 & readl(sock->addr + SOCK_MMCSD_SYSTEM_STATUS)) {
721 rc = 0;
722 break;
723 }
724 msleep(rc);
725 }
726
727 if (rc) {
728 printk(KERN_ERR DRIVER_NAME
729 ": controller failed to reset\n");
730 return -ENODEV;
731 }
732
733 writel(0, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
734 writel(host->clk_div | TIFM_MMCSD_POWER,
735 sock->addr + SOCK_MMCSD_CONFIG);
736 writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
737
738 // command timeout fixed to 64 clocks for now
739 writel(64, sock->addr + SOCK_MMCSD_COMMAND_TO);
740 writel(TIFM_MMCSD_INAB, sock->addr + SOCK_MMCSD_COMMAND);
741
742 /* INAB should take much less than reset */
743 for (rc = 1; rc <= 16; rc <<= 1) {
744 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
745 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
746 if (!(host_status & TIFM_MMCSD_ERRMASK)
747 && (host_status & TIFM_MMCSD_EOC)) {
748 rc = 0;
749 break;
750 }
751 msleep(rc);
752 }
753
754 if (rc) {
755 printk(KERN_ERR DRIVER_NAME
756 ": card not ready - probe failed on initialization\n");
757 return -ENODEV;
758 }
759
760 writel(TIFM_MMCSD_DATAMASK | TIFM_MMCSD_ERRMASK,
761 sock->addr + SOCK_MMCSD_INT_ENABLE);
762 mmiowb();
763
764 return 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700765}
766
767static int tifm_sd_probe(struct tifm_dev *sock)
768{
769 struct mmc_host *mmc;
770 struct tifm_sd *host;
771 int rc = -EIO;
772
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100773 if (!(TIFM_SOCK_STATE_OCCUPIED
774 & readl(sock->addr + SOCK_PRESENT_STATE))) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700775 printk(KERN_WARNING DRIVER_NAME ": card gone, unexpectedly\n");
776 return rc;
777 }
778
779 mmc = mmc_alloc_host(sizeof(struct tifm_sd), &sock->dev);
780 if (!mmc)
781 return -ENOMEM;
782
783 host = mmc_priv(mmc);
Alex Dubov0007d482007-04-12 16:59:23 +1000784 host->no_dma = no_dma;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700785 tifm_set_drvdata(sock, mmc);
Alex Dubov8e02f852006-12-08 16:50:51 +1100786 host->dev = sock;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700787 host->timeout_jiffies = msecs_to_jiffies(1000);
788
Alex Dubovdfef26d2007-04-12 16:59:24 +1000789 tasklet_init(&host->finish_tasklet, tifm_sd_end_cmd,
Alex Dubov8e02f852006-12-08 16:50:51 +1100790 (unsigned long)host);
791 setup_timer(&host->timer, tifm_sd_abort, (unsigned long)host);
792
Alex Dubov4020f2d2006-10-04 02:15:37 -0700793 mmc->ops = &tifm_sd_ops;
794 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Alex Dubov8e02f852006-12-08 16:50:51 +1100795 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700796 mmc->f_min = 20000000 / 60;
797 mmc->f_max = 24000000;
798 mmc->max_hw_segs = 1;
799 mmc->max_phys_segs = 1;
Pierre Ossman55db8902006-11-21 17:55:45 +0100800 // limited by DMA counter - it's safer to stick with
801 // block counter has 11 bits though
802 mmc->max_blk_count = 256;
803 // 2k maximum hw block length
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100804 mmc->max_blk_size = 2048;
Pierre Ossman55db8902006-11-21 17:55:45 +0100805 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
806 mmc->max_seg_size = mmc->max_req_size;
Alex Dubov4552f0c2007-04-12 16:59:12 +1000807 sock->card_event = tifm_sd_card_event;
808 sock->data_event = tifm_sd_data_event;
Alex Dubov8e02f852006-12-08 16:50:51 +1100809 rc = tifm_sd_initialize_host(host);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700810
Alex Dubov8e02f852006-12-08 16:50:51 +1100811 if (!rc)
812 rc = mmc_add_host(mmc);
813 if (rc)
814 goto out_free_mmc;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700815
816 return 0;
Alex Dubov8e02f852006-12-08 16:50:51 +1100817out_free_mmc:
818 mmc_free_host(mmc);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700819 return rc;
820}
821
822static void tifm_sd_remove(struct tifm_dev *sock)
823{
824 struct mmc_host *mmc = tifm_get_drvdata(sock);
825 struct tifm_sd *host = mmc_priv(mmc);
Alex Dubovb039d4a2007-04-12 16:59:21 +1000826 unsigned long flags;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700827
Alex Dubovb039d4a2007-04-12 16:59:21 +1000828 spin_lock_irqsave(&sock->lock, flags);
Alex Dubov0007d482007-04-12 16:59:23 +1000829 host->eject = 1;
Alex Dubov592d3722007-04-12 16:59:22 +1000830 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
831 mmiowb();
832 spin_unlock_irqrestore(&sock->lock, flags);
833
834 tasklet_kill(&host->finish_tasklet);
835
836 spin_lock_irqsave(&sock->lock, flags);
Alex Dubovb039d4a2007-04-12 16:59:21 +1000837 if (host->req) {
838 writel(TIFM_FIFO_INT_SETALL,
839 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
840 writel(0, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
Alex Dubov592d3722007-04-12 16:59:22 +1000841 host->req->cmd->error = MMC_ERR_TIMEOUT;
842 if (host->req->stop)
843 host->req->stop->error = MMC_ERR_TIMEOUT;
Alex Dubovb039d4a2007-04-12 16:59:21 +1000844 tasklet_schedule(&host->finish_tasklet);
845 }
846 spin_unlock_irqrestore(&sock->lock, flags);
Alex Dubov8e02f852006-12-08 16:50:51 +1100847 mmc_remove_host(mmc);
Alex Dubov592d3722007-04-12 16:59:22 +1000848 dev_dbg(&sock->dev, "after remove\n");
Alex Dubov4020f2d2006-10-04 02:15:37 -0700849
850 /* The meaning of the bit majority in this constant is unknown. */
851 writel(0xfff8 & readl(sock->addr + SOCK_CONTROL),
Alex Dubov2e8ce5e2006-12-08 16:50:52 +1100852 sock->addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700853
Alex Dubov4020f2d2006-10-04 02:15:37 -0700854 mmc_free_host(mmc);
855}
856
Alex Dubovdba4acc2006-12-11 01:55:38 +1100857#ifdef CONFIG_PM
858
859static int tifm_sd_suspend(struct tifm_dev *sock, pm_message_t state)
860{
861 struct mmc_host *mmc = tifm_get_drvdata(sock);
862 int rc;
863
864 rc = mmc_suspend_host(mmc, state);
865 /* The meaning of the bit majority in this constant is unknown. */
866 writel(0xfff8 & readl(sock->addr + SOCK_CONTROL),
867 sock->addr + SOCK_CONTROL);
868 return rc;
869}
870
871static int tifm_sd_resume(struct tifm_dev *sock)
872{
873 struct mmc_host *mmc = tifm_get_drvdata(sock);
874 struct tifm_sd *host = mmc_priv(mmc);
875
Alex Dubove23f2b82007-04-12 16:59:14 +1000876 if (sock->type != TIFM_TYPE_SD
Alex Dubovdba4acc2006-12-11 01:55:38 +1100877 || tifm_sd_initialize_host(host)) {
878 tifm_eject(sock);
879 return 0;
880 } else {
881 return mmc_resume_host(mmc);
882 }
883}
884
885#else
886
887#define tifm_sd_suspend NULL
888#define tifm_sd_resume NULL
889
890#endif /* CONFIG_PM */
891
Alex Dubove23f2b82007-04-12 16:59:14 +1000892static struct tifm_device_id tifm_sd_id_tbl[] = {
893 { TIFM_TYPE_SD }, { }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700894};
895
896static struct tifm_driver tifm_sd_driver = {
897 .driver = {
898 .name = DRIVER_NAME,
899 .owner = THIS_MODULE
900 },
901 .id_table = tifm_sd_id_tbl,
902 .probe = tifm_sd_probe,
Alex Dubovdba4acc2006-12-11 01:55:38 +1100903 .remove = tifm_sd_remove,
904 .suspend = tifm_sd_suspend,
905 .resume = tifm_sd_resume
Alex Dubov4020f2d2006-10-04 02:15:37 -0700906};
907
908static int __init tifm_sd_init(void)
909{
910 return tifm_register_driver(&tifm_sd_driver);
911}
912
913static void __exit tifm_sd_exit(void)
914{
915 tifm_unregister_driver(&tifm_sd_driver);
916}
917
918MODULE_AUTHOR("Alex Dubov");
919MODULE_DESCRIPTION("TI FlashMedia SD driver");
920MODULE_LICENSE("GPL");
921MODULE_DEVICE_TABLE(tifm, tifm_sd_id_tbl);
922MODULE_VERSION(DRIVER_VERSION);
923
924module_init(tifm_sd_init);
925module_exit(tifm_sd_exit);