7e607b75f4bc7ffdd862acefaee44c157ad7aac3
[linux-2.6.git] / drivers / mmc / tifm_sd.c
1 /*
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>
17 #include <asm/io.h>
18
19 #define DRIVER_NAME "tifm_sd"
20 #define DRIVER_VERSION "0.7"
21
22 static int no_dma = 0;
23 static int fixed_timeout = 0;
24 module_param(no_dma, bool, 0644);
25 module_param(fixed_timeout, bool, 0644);
26
27 /* Constants here are mostly from OMAP5912 datasheet */
28 #define TIFM_MMCSD_RESET      0x0002
29 #define TIFM_MMCSD_CLKMASK    0x03ff
30 #define TIFM_MMCSD_POWER      0x0800
31 #define TIFM_MMCSD_4BBUS      0x8000
32 #define TIFM_MMCSD_RXDE       0x8000   /* rx dma enable */
33 #define TIFM_MMCSD_TXDE       0x0080   /* tx dma enable */
34 #define TIFM_MMCSD_BUFINT     0x0c00   /* set bits: AE, AF */
35 #define TIFM_MMCSD_DPE        0x0020   /* data timeout counted in kilocycles */
36 #define TIFM_MMCSD_INAB       0x0080   /* abort / initialize command */
37 #define TIFM_MMCSD_READ       0x8000
38
39 #define TIFM_MMCSD_DATAMASK   0x001d   /* set bits: EOFB, BRS, CB, EOC */
40 #define TIFM_MMCSD_ERRMASK    0x41e0   /* set bits: CERR, CCRC, CTO, DCRC, DTO */
41 #define TIFM_MMCSD_EOC        0x0001   /* end of command phase  */
42 #define TIFM_MMCSD_CB         0x0004   /* card enter busy state */
43 #define TIFM_MMCSD_BRS        0x0008   /* block received/sent   */
44 #define TIFM_MMCSD_EOFB       0x0010   /* card exit busy state  */
45 #define TIFM_MMCSD_DTO        0x0020   /* data time-out         */
46 #define TIFM_MMCSD_DCRC       0x0040   /* data crc error        */
47 #define TIFM_MMCSD_CTO        0x0080   /* command time-out      */
48 #define TIFM_MMCSD_CCRC       0x0100   /* command crc error     */
49 #define TIFM_MMCSD_AF         0x0400   /* fifo almost full      */
50 #define TIFM_MMCSD_AE         0x0800   /* fifo almost empty     */
51 #define TIFM_MMCSD_CERR       0x4000   /* card status error     */
52
53 #define TIFM_MMCSD_FIFO_SIZE  0x0020
54
55 #define TIFM_MMCSD_RSP_R0     0x0000
56 #define TIFM_MMCSD_RSP_R1     0x0100
57 #define TIFM_MMCSD_RSP_R2     0x0200
58 #define TIFM_MMCSD_RSP_R3     0x0300
59 #define TIFM_MMCSD_RSP_R4     0x0400
60 #define TIFM_MMCSD_RSP_R5     0x0500
61 #define TIFM_MMCSD_RSP_R6     0x0600
62
63 #define TIFM_MMCSD_RSP_BUSY   0x0800
64
65 #define TIFM_MMCSD_CMD_BC     0x0000
66 #define TIFM_MMCSD_CMD_BCR    0x1000
67 #define TIFM_MMCSD_CMD_AC     0x2000
68 #define TIFM_MMCSD_CMD_ADTC   0x3000
69
70 typedef enum {
71         IDLE = 0,
72         CMD,    /* main command ended                   */
73         BRS,    /* block transfer finished              */
74         SCMD,   /* stop command ended                   */
75         CARD,   /* card left busy state                 */
76         FIFO,   /* FIFO operation completed (uncertain) */
77         READY
78 } card_state_t;
79
80 enum {
81         FIFO_RDY   = 0x0001,     /* hardware dependent value */
82         EJECT      = 0x0004,
83         EJECT_DONE = 0x0008,
84         CARD_BUSY  = 0x0010,
85         OPENDRAIN  = 0x0040,     /* hardware dependent value */
86         CARD_EVENT = 0x0100,     /* hardware dependent value */
87         CARD_RO    = 0x0200,     /* hardware dependent value */
88         FIFO_EVENT = 0x10000 };  /* hardware dependent value */
89
90 struct tifm_sd {
91         struct tifm_dev     *dev;
92
93         unsigned int        flags;
94         card_state_t        state;
95         unsigned int        clk_freq;
96         unsigned int        clk_div;
97         unsigned long       timeout_jiffies;
98
99         struct tasklet_struct finish_tasklet;
100         struct timer_list     timer;
101         struct mmc_request    *req;
102         wait_queue_head_t     notify;
103
104         size_t                written_blocks;
105         size_t                buffer_size;
106         size_t                buffer_pos;
107
108 };
109
110 static char* tifm_sd_kmap_atomic(struct mmc_data *data)
111 {
112         return kmap_atomic(data->sg->page, KM_BIO_SRC_IRQ) + data->sg->offset;
113 }
114
115 static void tifm_sd_kunmap_atomic(char *buffer, struct mmc_data *data)
116 {
117         kunmap_atomic(buffer - data->sg->offset, KM_BIO_SRC_IRQ);
118 }
119
120 static int tifm_sd_transfer_data(struct tifm_dev *sock, struct tifm_sd *host,
121                                  unsigned int host_status)
122 {
123         struct mmc_command *cmd = host->req->cmd;
124         unsigned int t_val = 0, cnt = 0;
125         char *buffer;
126
127         if (host_status & TIFM_MMCSD_BRS) {
128                 /* in non-dma rx mode BRS fires when fifo is still not empty */
129                 if (no_dma && (cmd->data->flags & MMC_DATA_READ)) {
130                         buffer = tifm_sd_kmap_atomic(host->req->data);
131                         while (host->buffer_size > host->buffer_pos) {
132                                 t_val = readl(sock->addr + SOCK_MMCSD_DATA);
133                                 buffer[host->buffer_pos++] = t_val & 0xff;
134                                 buffer[host->buffer_pos++] =
135                                                         (t_val >> 8) & 0xff;
136                         }
137                         tifm_sd_kunmap_atomic(buffer, host->req->data);
138                 }
139                 return 1;
140         } else if (no_dma) {
141                 buffer = tifm_sd_kmap_atomic(host->req->data);
142                 if ((cmd->data->flags & MMC_DATA_READ) &&
143                                 (host_status & TIFM_MMCSD_AF)) {
144                         for (cnt = 0; cnt < TIFM_MMCSD_FIFO_SIZE; cnt++) {
145                                 t_val = readl(sock->addr + SOCK_MMCSD_DATA);
146                                 if (host->buffer_size > host->buffer_pos) {
147                                         buffer[host->buffer_pos++] =
148                                                         t_val & 0xff;
149                                         buffer[host->buffer_pos++] =
150                                                         (t_val >> 8) & 0xff;
151                                 }
152                         }
153                 } else if ((cmd->data->flags & MMC_DATA_WRITE)
154                            && (host_status & TIFM_MMCSD_AE)) {
155                         for (cnt = 0; cnt < TIFM_MMCSD_FIFO_SIZE; cnt++) {
156                                 if (host->buffer_size > host->buffer_pos) {
157                                         t_val = buffer[host->buffer_pos++]
158                                                 & 0x00ff;
159                                         t_val |= ((buffer[host->buffer_pos++])
160                                                   << 8) & 0xff00;
161                                         writel(t_val,
162                                                sock->addr + SOCK_MMCSD_DATA);
163                                 }
164                         }
165                 }
166                 tifm_sd_kunmap_atomic(buffer, host->req->data);
167         }
168         return 0;
169 }
170
171 static unsigned int tifm_sd_op_flags(struct mmc_command *cmd)
172 {
173         unsigned int rc = 0;
174
175         switch (mmc_resp_type(cmd)) {
176         case MMC_RSP_NONE:
177                 rc |= TIFM_MMCSD_RSP_R0;
178                 break;
179         case MMC_RSP_R1B:
180                 rc |= TIFM_MMCSD_RSP_BUSY; // deliberate fall-through
181         case MMC_RSP_R1:
182                 rc |= TIFM_MMCSD_RSP_R1;
183                 break;
184         case MMC_RSP_R2:
185                 rc |= TIFM_MMCSD_RSP_R2;
186                 break;
187         case MMC_RSP_R3:
188                 rc |= TIFM_MMCSD_RSP_R3;
189                 break;
190         default:
191                 BUG();
192         }
193
194         switch (mmc_cmd_type(cmd)) {
195         case MMC_CMD_BC:
196                 rc |= TIFM_MMCSD_CMD_BC;
197                 break;
198         case MMC_CMD_BCR:
199                 rc |= TIFM_MMCSD_CMD_BCR;
200                 break;
201         case MMC_CMD_AC:
202                 rc |= TIFM_MMCSD_CMD_AC;
203                 break;
204         case MMC_CMD_ADTC:
205                 rc |= TIFM_MMCSD_CMD_ADTC;
206                 break;
207         default:
208                 BUG();
209         }
210         return rc;
211 }
212
213 static void tifm_sd_exec(struct tifm_sd *host, struct mmc_command *cmd)
214 {
215         struct tifm_dev *sock = host->dev;
216         unsigned int cmd_mask = tifm_sd_op_flags(cmd) |
217                                 (host->flags & OPENDRAIN);
218
219         if (cmd->data && (cmd->data->flags & MMC_DATA_READ))
220                 cmd_mask |= TIFM_MMCSD_READ;
221
222         dev_dbg(&sock->dev, "executing opcode 0x%x, arg: 0x%x, mask: 0x%x\n",
223                 cmd->opcode, cmd->arg, cmd_mask);
224
225         writel((cmd->arg >> 16) & 0xffff, sock->addr + SOCK_MMCSD_ARG_HIGH);
226         writel(cmd->arg & 0xffff, sock->addr + SOCK_MMCSD_ARG_LOW);
227         writel(cmd->opcode | cmd_mask, sock->addr + SOCK_MMCSD_COMMAND);
228 }
229
230 static void tifm_sd_fetch_resp(struct mmc_command *cmd, struct tifm_dev *sock)
231 {
232         cmd->resp[0] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x1c) << 16)
233                        | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x18);
234         cmd->resp[1] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x14) << 16)
235                        | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x10);
236         cmd->resp[2] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x0c) << 16)
237                        | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x08);
238         cmd->resp[3] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x04) << 16)
239                        | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x00);
240 }
241
242 static void tifm_sd_process_cmd(struct tifm_dev *sock, struct tifm_sd *host,
243                                        unsigned int host_status)
244 {
245         struct mmc_command *cmd = host->req->cmd;
246
247 change_state:
248         switch (host->state) {
249         case IDLE:
250                 return;
251         case CMD:
252                 if (host_status & TIFM_MMCSD_EOC) {
253                         tifm_sd_fetch_resp(cmd, sock);
254                         if (cmd->data) {
255                                 host->state = BRS;
256                         } else {
257                                 host->state = READY;
258                         }
259                         goto change_state;
260                 }
261                 break;
262         case BRS:
263                 if (tifm_sd_transfer_data(sock, host, host_status)) {
264                         if (cmd->data->flags & MMC_DATA_WRITE) {
265                                 host->state = CARD;
266                         } else {
267                                 if (no_dma) {
268                                         if (host->req->stop) {
269                                                 tifm_sd_exec(host, host->req->stop);
270                                                 host->state = SCMD;
271                                         } else {
272                                                 host->state = READY;
273                                         }
274                                 } else {
275                                         host->state = FIFO;
276                                 }
277                         }
278                         goto change_state;
279                 }
280                 break;
281         case SCMD:
282                 if (host_status & TIFM_MMCSD_EOC) {
283                         tifm_sd_fetch_resp(host->req->stop, sock);
284                         host->state = READY;
285                         goto change_state;
286                 }
287                 break;
288         case CARD:
289                 dev_dbg(&sock->dev, "waiting for CARD, have %zd blocks\n",
290                         host->written_blocks);
291                 if (!(host->flags & CARD_BUSY)
292                     && (host->written_blocks == cmd->data->blocks)) {
293                         if (no_dma) {
294                                 if (host->req->stop) {
295                                         tifm_sd_exec(host, host->req->stop);
296                                         host->state = SCMD;
297                                 } else {
298                                         host->state = READY;
299                                 }
300                         } else {
301                                 host->state = FIFO;
302                         }
303                         goto change_state;
304                 }
305                 break;
306         case FIFO:
307                 if (host->flags & FIFO_RDY) {
308                         host->flags &= ~FIFO_RDY;
309                         if (host->req->stop) {
310                                 tifm_sd_exec(host, host->req->stop);
311                                 host->state = SCMD;
312                         } else {
313                                 host->state = READY;
314                         }
315                         goto change_state;
316                 }
317                 break;
318         case READY:
319                 tasklet_schedule(&host->finish_tasklet);
320                 return;
321         }
322
323 }
324
325 /* Called from interrupt handler */
326 static void tifm_sd_signal_irq(struct tifm_dev *sock,
327                                unsigned int sock_irq_status)
328 {
329         struct tifm_sd *host;
330         unsigned int host_status = 0, fifo_status = 0;
331         int error_code = 0;
332
333         spin_lock(&sock->lock);
334         host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock));
335
336         if (sock_irq_status & FIFO_EVENT) {
337                 fifo_status = readl(sock->addr + SOCK_DMA_FIFO_STATUS);
338                 writel(fifo_status, sock->addr + SOCK_DMA_FIFO_STATUS);
339
340                 host->flags |= fifo_status & FIFO_RDY;
341         }
342
343         if (sock_irq_status & CARD_EVENT) {
344                 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
345                 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
346
347                 if (!host->req)
348                         goto done;
349
350                 if (host_status & TIFM_MMCSD_ERRMASK) {
351                         if (host_status & TIFM_MMCSD_CERR)
352                                 error_code = MMC_ERR_FAILED;
353                         else if (host_status
354                                  & (TIFM_MMCSD_CTO | TIFM_MMCSD_DTO))
355                                 error_code = MMC_ERR_TIMEOUT;
356                         else if (host_status
357                                  & (TIFM_MMCSD_CCRC | TIFM_MMCSD_DCRC))
358                                 error_code = MMC_ERR_BADCRC;
359
360                         writel(TIFM_FIFO_INT_SETALL,
361                                sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
362                         writel(TIFM_DMA_RESET, sock->addr + SOCK_DMA_CONTROL);
363
364                         if (host->req->stop) {
365                                 if (host->state == SCMD) {
366                                         host->req->stop->error = error_code;
367                                 } else if (host->state == BRS
368                                            || host->state == CARD
369                                            || host->state == FIFO) {
370                                         host->req->cmd->error = error_code;
371                                         tifm_sd_exec(host, host->req->stop);
372                                         host->state = SCMD;
373                                         goto done;
374                                 } else {
375                                         host->req->cmd->error = error_code;
376                                 }
377                         } else {
378                                 host->req->cmd->error = error_code;
379                         }
380                         host->state = READY;
381                 }
382
383                 if (host_status & TIFM_MMCSD_CB)
384                         host->flags |= CARD_BUSY;
385                 if ((host_status & TIFM_MMCSD_EOFB)
386                     && (host->flags & CARD_BUSY)) {
387                         host->written_blocks++;
388                         host->flags &= ~CARD_BUSY;
389                 }
390         }
391
392         if (host->req)
393                 tifm_sd_process_cmd(sock, host, host_status);
394 done:
395         dev_dbg(&sock->dev, "host_status %x, fifo_status %x\n",
396                 host_status, fifo_status);
397         spin_unlock(&sock->lock);
398 }
399
400 static void tifm_sd_prepare_data(struct tifm_sd *host, struct mmc_command *cmd)
401 {
402         struct tifm_dev *sock = host->dev;
403         unsigned int dest_cnt;
404
405         /* DMA style IO */
406         dev_dbg(&sock->dev, "setting dma for %d blocks\n",
407                 cmd->data->blocks);
408         writel(TIFM_FIFO_INT_SETALL,
409                sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
410         writel(ilog2(cmd->data->blksz) - 2,
411                sock->addr + SOCK_FIFO_PAGE_SIZE);
412         writel(TIFM_FIFO_ENABLE, sock->addr + SOCK_FIFO_CONTROL);
413         writel(TIFM_FIFO_INTMASK, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
414
415         dest_cnt = (cmd->data->blocks) << 8;
416
417         writel(sg_dma_address(cmd->data->sg), sock->addr + SOCK_DMA_ADDRESS);
418
419         writel(cmd->data->blocks - 1, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
420         writel(cmd->data->blksz - 1, sock->addr + SOCK_MMCSD_BLOCK_LEN);
421
422         if (cmd->data->flags & MMC_DATA_WRITE) {
423                 writel(TIFM_MMCSD_TXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
424                 writel(dest_cnt | TIFM_DMA_TX | TIFM_DMA_EN,
425                        sock->addr + SOCK_DMA_CONTROL);
426         } else {
427                 writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
428                 writel(dest_cnt | TIFM_DMA_EN, sock->addr + SOCK_DMA_CONTROL);
429         }
430 }
431
432 static void tifm_sd_set_data_timeout(struct tifm_sd *host,
433                                      struct mmc_data *data)
434 {
435         struct tifm_dev *sock = host->dev;
436         unsigned int data_timeout = data->timeout_clks;
437
438         if (fixed_timeout)
439                 return;
440
441         data_timeout += data->timeout_ns /
442                         ((1000000000UL / host->clk_freq) * host->clk_div);
443
444         if (data_timeout < 0xffff) {
445                 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
446                 writel((~TIFM_MMCSD_DPE)
447                        & readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
448                        sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
449         } else {
450                 data_timeout = (data_timeout >> 10) + 1;
451                 if (data_timeout > 0xffff)
452                         data_timeout = 0;       /* set to unlimited */
453                 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
454                 writel(TIFM_MMCSD_DPE
455                        | readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
456                        sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
457         }
458 }
459
460 static void tifm_sd_request(struct mmc_host *mmc, struct mmc_request *mrq)
461 {
462         struct tifm_sd *host = mmc_priv(mmc);
463         struct tifm_dev *sock = host->dev;
464         unsigned long flags;
465         int sg_count = 0;
466         struct mmc_data *r_data = mrq->cmd->data;
467
468         spin_lock_irqsave(&sock->lock, flags);
469         if (host->flags & EJECT) {
470                 spin_unlock_irqrestore(&sock->lock, flags);
471                 goto err_out;
472         }
473
474         if (host->req) {
475                 printk(KERN_ERR DRIVER_NAME ": unfinished request detected\n");
476                 spin_unlock_irqrestore(&sock->lock, flags);
477                 goto err_out;
478         }
479
480         if (r_data) {
481                 tifm_sd_set_data_timeout(host, r_data);
482
483                 sg_count = tifm_map_sg(sock, r_data->sg, r_data->sg_len,
484                                        mrq->cmd->flags & MMC_DATA_WRITE
485                                        ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
486                 if (sg_count != 1) {
487                         printk(KERN_ERR DRIVER_NAME
488                                 ": scatterlist map failed\n");
489                         spin_unlock_irqrestore(&sock->lock, flags);
490                         goto err_out;
491                 }
492
493                 host->written_blocks = 0;
494                 host->flags &= ~CARD_BUSY;
495                 tifm_sd_prepare_data(host, mrq->cmd);
496         }
497
498         host->req = mrq;
499         mod_timer(&host->timer, jiffies + host->timeout_jiffies);
500         host->state = CMD;
501         writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
502                sock->addr + SOCK_CONTROL);
503         tifm_sd_exec(host, mrq->cmd);
504         spin_unlock_irqrestore(&sock->lock, flags);
505         return;
506
507 err_out:
508         if (sg_count > 0)
509                 tifm_unmap_sg(sock, r_data->sg, r_data->sg_len,
510                               (r_data->flags & MMC_DATA_WRITE)
511                               ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
512
513         mrq->cmd->error = MMC_ERR_TIMEOUT;
514         mmc_request_done(mmc, mrq);
515 }
516
517 static void tifm_sd_end_cmd(unsigned long data)
518 {
519         struct tifm_sd *host = (struct tifm_sd*)data;
520         struct tifm_dev *sock = host->dev;
521         struct mmc_host *mmc = tifm_get_drvdata(sock);
522         struct mmc_request *mrq;
523         struct mmc_data *r_data = NULL;
524         unsigned long flags;
525
526         spin_lock_irqsave(&sock->lock, flags);
527
528         del_timer(&host->timer);
529         mrq = host->req;
530         host->req = NULL;
531         host->state = IDLE;
532
533         if (!mrq) {
534                 printk(KERN_ERR DRIVER_NAME ": no request to complete?\n");
535                 spin_unlock_irqrestore(&sock->lock, flags);
536                 return;
537         }
538
539         r_data = mrq->cmd->data;
540         if (r_data) {
541                 if (r_data->flags & MMC_DATA_WRITE) {
542                         r_data->bytes_xfered = host->written_blocks
543                                                * r_data->blksz;
544                 } else {
545                         r_data->bytes_xfered = r_data->blocks -
546                                 readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS) - 1;
547                         r_data->bytes_xfered *= r_data->blksz;
548                         r_data->bytes_xfered += r_data->blksz -
549                                 readl(sock->addr + SOCK_MMCSD_BLOCK_LEN) + 1;
550                 }
551                 tifm_unmap_sg(sock, r_data->sg, r_data->sg_len,
552                               (r_data->flags & MMC_DATA_WRITE)
553                               ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
554         }
555
556         writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
557                sock->addr + SOCK_CONTROL);
558
559         spin_unlock_irqrestore(&sock->lock, flags);
560         mmc_request_done(mmc, mrq);
561 }
562
563 static void tifm_sd_request_nodma(struct mmc_host *mmc, struct mmc_request *mrq)
564 {
565         struct tifm_sd *host = mmc_priv(mmc);
566         struct tifm_dev *sock = host->dev;
567         unsigned long flags;
568         struct mmc_data *r_data = mrq->cmd->data;
569
570         spin_lock_irqsave(&sock->lock, flags);
571         if (host->flags & EJECT) {
572                 spin_unlock_irqrestore(&sock->lock, flags);
573                 goto err_out;
574         }
575
576         if (host->req) {
577                 printk(KERN_ERR DRIVER_NAME ": unfinished request detected\n");
578                 spin_unlock_irqrestore(&sock->lock, flags);
579                 goto err_out;
580         }
581
582         if (r_data) {
583                 tifm_sd_set_data_timeout(host, r_data);
584
585                 host->buffer_size = mrq->cmd->data->blocks
586                                     * mrq->cmd->data->blksz;
587
588                 writel(TIFM_MMCSD_BUFINT
589                        | readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
590                        sock->addr + SOCK_MMCSD_INT_ENABLE);
591                 writel(((TIFM_MMCSD_FIFO_SIZE - 1) << 8)
592                        | (TIFM_MMCSD_FIFO_SIZE - 1),
593                        sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
594
595                 host->written_blocks = 0;
596                 host->flags &= ~CARD_BUSY;
597                 host->buffer_pos = 0;
598                 writel(r_data->blocks - 1, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
599                 writel(r_data->blksz - 1, sock->addr + SOCK_MMCSD_BLOCK_LEN);
600         }
601
602         host->req = mrq;
603         mod_timer(&host->timer, jiffies + host->timeout_jiffies);
604         host->state = CMD;
605         writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
606                sock->addr + SOCK_CONTROL);
607         tifm_sd_exec(host, mrq->cmd);
608         spin_unlock_irqrestore(&sock->lock, flags);
609         return;
610
611 err_out:
612         mrq->cmd->error = MMC_ERR_TIMEOUT;
613         mmc_request_done(mmc, mrq);
614 }
615
616 static void tifm_sd_end_cmd_nodma(unsigned long data)
617 {
618         struct tifm_sd *host = (struct tifm_sd*)data;
619         struct tifm_dev *sock = host->dev;
620         struct mmc_host *mmc = tifm_get_drvdata(sock);
621         struct mmc_request *mrq;
622         struct mmc_data *r_data = NULL;
623         unsigned long flags;
624
625         spin_lock_irqsave(&sock->lock, flags);
626
627         del_timer(&host->timer);
628         mrq = host->req;
629         host->req = NULL;
630         host->state = IDLE;
631
632         if (!mrq) {
633                 printk(KERN_ERR DRIVER_NAME ": no request to complete?\n");
634                 spin_unlock_irqrestore(&sock->lock, flags);
635                 return;
636         }
637
638         r_data = mrq->cmd->data;
639         if (r_data) {
640                 writel((~TIFM_MMCSD_BUFINT) &
641                         readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
642                         sock->addr + SOCK_MMCSD_INT_ENABLE);
643
644                 if (r_data->flags & MMC_DATA_WRITE) {
645                         r_data->bytes_xfered = host->written_blocks
646                                                * r_data->blksz;
647                 } else {
648                         r_data->bytes_xfered = r_data->blocks -
649                                 readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS) - 1;
650                         r_data->bytes_xfered *= r_data->blksz;
651                         r_data->bytes_xfered += r_data->blksz -
652                                 readl(sock->addr + SOCK_MMCSD_BLOCK_LEN) + 1;
653                 }
654                 host->buffer_pos = 0;
655                 host->buffer_size = 0;
656         }
657
658         writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
659                sock->addr + SOCK_CONTROL);
660
661         spin_unlock_irqrestore(&sock->lock, flags);
662
663         mmc_request_done(mmc, mrq);
664 }
665
666 static void tifm_sd_terminate(struct tifm_sd *host)
667 {
668         struct tifm_dev *sock = host->dev;
669         unsigned long flags;
670
671         writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
672         mmiowb();
673         spin_lock_irqsave(&sock->lock, flags);
674         host->flags |= EJECT;
675         if (host->req) {
676                 writel(TIFM_FIFO_INT_SETALL,
677                        sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
678                 writel(0, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
679                 tasklet_schedule(&host->finish_tasklet);
680         }
681         spin_unlock_irqrestore(&sock->lock, flags);
682 }
683
684 static void tifm_sd_abort(unsigned long data)
685 {
686         struct tifm_sd *host = (struct tifm_sd*)data;
687
688         printk(KERN_ERR DRIVER_NAME
689                ": card failed to respond for a long period of time");
690
691         tifm_sd_terminate(host);
692         tifm_eject(host->dev);
693 }
694
695 static void tifm_sd_ios(struct mmc_host *mmc, struct mmc_ios *ios)
696 {
697         struct tifm_sd *host = mmc_priv(mmc);
698         struct tifm_dev *sock = host->dev;
699         unsigned int clk_div1, clk_div2;
700         unsigned long flags;
701
702         spin_lock_irqsave(&sock->lock, flags);
703
704         dev_dbg(&sock->dev, "Setting bus width %d, power %d\n", ios->bus_width,
705                 ios->power_mode);
706         if (ios->bus_width == MMC_BUS_WIDTH_4) {
707                 writel(TIFM_MMCSD_4BBUS | readl(sock->addr + SOCK_MMCSD_CONFIG),
708                        sock->addr + SOCK_MMCSD_CONFIG);
709         } else {
710                 writel((~TIFM_MMCSD_4BBUS)
711                        & readl(sock->addr + SOCK_MMCSD_CONFIG),
712                        sock->addr + SOCK_MMCSD_CONFIG);
713         }
714
715         if (ios->clock) {
716                 clk_div1 = 20000000 / ios->clock;
717                 if (!clk_div1)
718                         clk_div1 = 1;
719
720                 clk_div2 = 24000000 / ios->clock;
721                 if (!clk_div2)
722                         clk_div2 = 1;
723
724                 if ((20000000 / clk_div1) > ios->clock)
725                         clk_div1++;
726                 if ((24000000 / clk_div2) > ios->clock)
727                         clk_div2++;
728                 if ((20000000 / clk_div1) > (24000000 / clk_div2)) {
729                         host->clk_freq = 20000000;
730                         host->clk_div = clk_div1;
731                         writel((~TIFM_CTRL_FAST_CLK)
732                                & readl(sock->addr + SOCK_CONTROL),
733                                sock->addr + SOCK_CONTROL);
734                 } else {
735                         host->clk_freq = 24000000;
736                         host->clk_div = clk_div2;
737                         writel(TIFM_CTRL_FAST_CLK
738                                | readl(sock->addr + SOCK_CONTROL),
739                                sock->addr + SOCK_CONTROL);
740                 }
741         } else {
742                 host->clk_div = 0;
743         }
744         host->clk_div &= TIFM_MMCSD_CLKMASK;
745         writel(host->clk_div
746                | ((~TIFM_MMCSD_CLKMASK)
747                   & readl(sock->addr + SOCK_MMCSD_CONFIG)),
748                sock->addr + SOCK_MMCSD_CONFIG);
749
750         if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
751                 host->flags |= OPENDRAIN;
752         else
753                 host->flags &= ~OPENDRAIN;
754
755         /* chip_select : maybe later */
756         //vdd
757         //power is set before probe / after remove
758         //I believe, power_off when already marked for eject is sufficient to
759         // allow removal.
760         if ((host->flags & EJECT) && ios->power_mode == MMC_POWER_OFF) {
761                 host->flags |= EJECT_DONE;
762                 wake_up_all(&host->notify);
763         }
764
765         spin_unlock_irqrestore(&sock->lock, flags);
766 }
767
768 static int tifm_sd_ro(struct mmc_host *mmc)
769 {
770         int rc;
771         struct tifm_sd *host = mmc_priv(mmc);
772         struct tifm_dev *sock = host->dev;
773         unsigned long flags;
774
775         spin_lock_irqsave(&sock->lock, flags);
776
777         host->flags |= (CARD_RO & readl(sock->addr + SOCK_PRESENT_STATE));
778         rc = (host->flags & CARD_RO) ? 1 : 0;
779
780         spin_unlock_irqrestore(&sock->lock, flags);
781         return rc;
782 }
783
784 static struct mmc_host_ops tifm_sd_ops = {
785         .request = tifm_sd_request,
786         .set_ios = tifm_sd_ios,
787         .get_ro  = tifm_sd_ro
788 };
789
790 static int tifm_sd_initialize_host(struct tifm_sd *host)
791 {
792         int rc;
793         unsigned int host_status = 0;
794         struct tifm_dev *sock = host->dev;
795
796         writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
797         mmiowb();
798         host->clk_div = 61;
799         host->clk_freq = 20000000;
800         writel(TIFM_MMCSD_RESET, sock->addr + SOCK_MMCSD_SYSTEM_CONTROL);
801         writel(host->clk_div | TIFM_MMCSD_POWER,
802                sock->addr + SOCK_MMCSD_CONFIG);
803
804         /* wait up to 0.51 sec for reset */
805         for (rc = 2; rc <= 256; rc <<= 1) {
806                 if (1 & readl(sock->addr + SOCK_MMCSD_SYSTEM_STATUS)) {
807                         rc = 0;
808                         break;
809                 }
810                 msleep(rc);
811         }
812
813         if (rc) {
814                 printk(KERN_ERR DRIVER_NAME
815                        ": controller failed to reset\n");
816                 return -ENODEV;
817         }
818
819         writel(0, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
820         writel(host->clk_div | TIFM_MMCSD_POWER,
821                sock->addr + SOCK_MMCSD_CONFIG);
822         writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
823
824         // command timeout fixed to 64 clocks for now
825         writel(64, sock->addr + SOCK_MMCSD_COMMAND_TO);
826         writel(TIFM_MMCSD_INAB, sock->addr + SOCK_MMCSD_COMMAND);
827
828         /* INAB should take much less than reset */
829         for (rc = 1; rc <= 16; rc <<= 1) {
830                 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
831                 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
832                 if (!(host_status & TIFM_MMCSD_ERRMASK)
833                     && (host_status & TIFM_MMCSD_EOC)) {
834                         rc = 0;
835                         break;
836                 }
837                 msleep(rc);
838         }
839
840         if (rc) {
841                 printk(KERN_ERR DRIVER_NAME
842                        ": card not ready - probe failed on initialization\n");
843                 return -ENODEV;
844         }
845
846         writel(TIFM_MMCSD_DATAMASK | TIFM_MMCSD_ERRMASK,
847                sock->addr + SOCK_MMCSD_INT_ENABLE);
848         mmiowb();
849
850         return 0;
851 }
852
853 static int tifm_sd_probe(struct tifm_dev *sock)
854 {
855         struct mmc_host *mmc;
856         struct tifm_sd *host;
857         int rc = -EIO;
858
859         if (!(TIFM_SOCK_STATE_OCCUPIED
860               & readl(sock->addr + SOCK_PRESENT_STATE))) {
861                 printk(KERN_WARNING DRIVER_NAME ": card gone, unexpectedly\n");
862                 return rc;
863         }
864
865         mmc = mmc_alloc_host(sizeof(struct tifm_sd), &sock->dev);
866         if (!mmc)
867                 return -ENOMEM;
868
869         host = mmc_priv(mmc);
870         tifm_set_drvdata(sock, mmc);
871         host->dev = sock;
872         host->timeout_jiffies = msecs_to_jiffies(1000);
873
874         init_waitqueue_head(&host->notify);
875         tasklet_init(&host->finish_tasklet,
876                      no_dma ? tifm_sd_end_cmd_nodma : tifm_sd_end_cmd,
877                      (unsigned long)host);
878         setup_timer(&host->timer, tifm_sd_abort, (unsigned long)host);
879
880         tifm_sd_ops.request = no_dma ? tifm_sd_request_nodma : tifm_sd_request;
881         mmc->ops = &tifm_sd_ops;
882         mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
883         mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE;
884         mmc->f_min = 20000000 / 60;
885         mmc->f_max = 24000000;
886         mmc->max_hw_segs = 1;
887         mmc->max_phys_segs = 1;
888         // limited by DMA counter - it's safer to stick with
889         // block counter has 11 bits though
890         mmc->max_blk_count = 256;
891         // 2k maximum hw block length
892         mmc->max_blk_size = 2048;
893         mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
894         mmc->max_seg_size = mmc->max_req_size;
895         sock->signal_irq = tifm_sd_signal_irq;
896         rc = tifm_sd_initialize_host(host);
897
898         if (!rc)
899                 rc = mmc_add_host(mmc);
900         if (rc)
901                 goto out_free_mmc;
902
903         return 0;
904 out_free_mmc:
905         mmc_free_host(mmc);
906         return rc;
907 }
908
909 static void tifm_sd_remove(struct tifm_dev *sock)
910 {
911         struct mmc_host *mmc = tifm_get_drvdata(sock);
912         struct tifm_sd *host = mmc_priv(mmc);
913
914         del_timer_sync(&host->timer);
915         tifm_sd_terminate(host);
916         wait_event_timeout(host->notify, host->flags & EJECT_DONE,
917                            host->timeout_jiffies);
918         tasklet_kill(&host->finish_tasklet);
919         mmc_remove_host(mmc);
920
921         /* The meaning of the bit majority in this constant is unknown. */
922         writel(0xfff8 & readl(sock->addr + SOCK_CONTROL),
923                sock->addr + SOCK_CONTROL);
924
925         tifm_set_drvdata(sock, NULL);
926         mmc_free_host(mmc);
927 }
928
929 #ifdef CONFIG_PM
930
931 static int tifm_sd_suspend(struct tifm_dev *sock, pm_message_t state)
932 {
933         struct mmc_host *mmc = tifm_get_drvdata(sock);
934         int rc;
935
936         rc = mmc_suspend_host(mmc, state);
937         /* The meaning of the bit majority in this constant is unknown. */
938         writel(0xfff8 & readl(sock->addr + SOCK_CONTROL),
939                sock->addr + SOCK_CONTROL);
940         return rc;
941 }
942
943 static int tifm_sd_resume(struct tifm_dev *sock)
944 {
945         struct mmc_host *mmc = tifm_get_drvdata(sock);
946         struct tifm_sd *host = mmc_priv(mmc);
947
948         if (sock->media_id != FM_SD
949             || tifm_sd_initialize_host(host)) {
950                 tifm_eject(sock);
951                 return 0;
952         } else {
953                 return mmc_resume_host(mmc);
954         }
955 }
956
957 #else
958
959 #define tifm_sd_suspend NULL
960 #define tifm_sd_resume NULL
961
962 #endif /* CONFIG_PM */
963
964 static tifm_media_id tifm_sd_id_tbl[] = {
965         FM_SD, 0
966 };
967
968 static struct tifm_driver tifm_sd_driver = {
969         .driver = {
970                 .name  = DRIVER_NAME,
971                 .owner = THIS_MODULE
972         },
973         .id_table = tifm_sd_id_tbl,
974         .probe    = tifm_sd_probe,
975         .remove   = tifm_sd_remove,
976         .suspend  = tifm_sd_suspend,
977         .resume   = tifm_sd_resume
978 };
979
980 static int __init tifm_sd_init(void)
981 {
982         return tifm_register_driver(&tifm_sd_driver);
983 }
984
985 static void __exit tifm_sd_exit(void)
986 {
987         tifm_unregister_driver(&tifm_sd_driver);
988 }
989
990 MODULE_AUTHOR("Alex Dubov");
991 MODULE_DESCRIPTION("TI FlashMedia SD driver");
992 MODULE_LICENSE("GPL");
993 MODULE_DEVICE_TABLE(tifm, tifm_sd_id_tbl);
994 MODULE_VERSION(DRIVER_VERSION);
995
996 module_init(tifm_sd_init);
997 module_exit(tifm_sd_exit);