Merge branch 'release' of git://lm-sensors.org/kernel/mhoffman/hwmon-2.6
[linux-2.6] / drivers / mmc / host / au1xmmc.c
1 /*
2  * linux/drivers/mmc/host/au1xmmc.c - AU1XX0 MMC driver
3  *
4  *  Copyright (c) 2005, Advanced Micro Devices, Inc.
5  *
6  *  Developed with help from the 2.4.30 MMC AU1XXX controller including
7  *  the following copyright notices:
8  *     Copyright (c) 2003-2004 Embedded Edge, LLC.
9  *     Portions Copyright (C) 2002 Embedix, Inc
10  *     Copyright 2002 Hewlett-Packard Company
11
12  *  2.6 version of this driver inspired by:
13  *     (drivers/mmc/wbsd.c) Copyright (C) 2004-2005 Pierre Ossman,
14  *     All Rights Reserved.
15  *     (drivers/mmc/pxa.c) Copyright (C) 2003 Russell King,
16  *     All Rights Reserved.
17  *
18
19  * This program is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License version 2 as
21  * published by the Free Software Foundation.
22  */
23
24 /* Why is a timer used to detect insert events?
25  *
26  * From the AU1100 MMC application guide:
27  * If the Au1100-based design is intended to support both MultiMediaCards
28  * and 1- or 4-data bit SecureDigital cards, then the solution is to
29  * connect a weak (560KOhm) pull-up resistor to connector pin 1.
30  * In doing so, a MMC card never enters SPI-mode communications,
31  * but now the SecureDigital card-detect feature of CD/DAT3 is ineffective
32  * (the low to high transition will not occur).
33  *
34  * So we use the timer to check the status manually.
35  */
36
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/platform_device.h>
40 #include <linux/mm.h>
41 #include <linux/interrupt.h>
42 #include <linux/dma-mapping.h>
43
44 #include <linux/mmc/host.h>
45 #include <asm/io.h>
46 #include <asm/mach-au1x00/au1000.h>
47 #include <asm/mach-au1x00/au1xxx_dbdma.h>
48 #include <asm/mach-au1x00/au1100_mmc.h>
49 #include <asm/scatterlist.h>
50
51 #include <au1xxx.h>
52 #include "au1xmmc.h"
53
54 #define DRIVER_NAME "au1xxx-mmc"
55
56 /* Set this to enable special debugging macros */
57
58 #ifdef DEBUG
59 #define DBG(fmt, idx, args...) printk("au1xx(%d): DEBUG: " fmt, idx, ##args)
60 #else
61 #define DBG(fmt, idx, args...)
62 #endif
63
64 const struct {
65         u32 iobase;
66         u32 tx_devid, rx_devid;
67         u16 bcsrpwr;
68         u16 bcsrstatus;
69         u16 wpstatus;
70 } au1xmmc_card_table[] = {
71         { SD0_BASE, DSCR_CMD0_SDMS_TX0, DSCR_CMD0_SDMS_RX0,
72           BCSR_BOARD_SD0PWR, BCSR_INT_SD0INSERT, BCSR_STATUS_SD0WP },
73 #ifndef CONFIG_MIPS_DB1200
74         { SD1_BASE, DSCR_CMD0_SDMS_TX1, DSCR_CMD0_SDMS_RX1,
75           BCSR_BOARD_DS1PWR, BCSR_INT_SD1INSERT, BCSR_STATUS_SD1WP }
76 #endif
77 };
78
79 #define AU1XMMC_CONTROLLER_COUNT (ARRAY_SIZE(au1xmmc_card_table))
80
81 /* This array stores pointers for the hosts (used by the IRQ handler) */
82 struct au1xmmc_host *au1xmmc_hosts[AU1XMMC_CONTROLLER_COUNT];
83 static int dma = 1;
84
85 #ifdef MODULE
86 module_param(dma, bool, 0);
87 MODULE_PARM_DESC(dma, "Use DMA engine for data transfers (0 = disabled)");
88 #endif
89
90 static inline void IRQ_ON(struct au1xmmc_host *host, u32 mask)
91 {
92         u32 val = au_readl(HOST_CONFIG(host));
93         val |= mask;
94         au_writel(val, HOST_CONFIG(host));
95         au_sync();
96 }
97
98 static inline void FLUSH_FIFO(struct au1xmmc_host *host)
99 {
100         u32 val = au_readl(HOST_CONFIG2(host));
101
102         au_writel(val | SD_CONFIG2_FF, HOST_CONFIG2(host));
103         au_sync_delay(1);
104
105         /* SEND_STOP will turn off clock control - this re-enables it */
106         val &= ~SD_CONFIG2_DF;
107
108         au_writel(val, HOST_CONFIG2(host));
109         au_sync();
110 }
111
112 static inline void IRQ_OFF(struct au1xmmc_host *host, u32 mask)
113 {
114         u32 val = au_readl(HOST_CONFIG(host));
115         val &= ~mask;
116         au_writel(val, HOST_CONFIG(host));
117         au_sync();
118 }
119
120 static inline void SEND_STOP(struct au1xmmc_host *host)
121 {
122
123         /* We know the value of CONFIG2, so avoid a read we don't need */
124         u32 mask = SD_CONFIG2_EN;
125
126         WARN_ON(host->status != HOST_S_DATA);
127         host->status = HOST_S_STOP;
128
129         au_writel(mask | SD_CONFIG2_DF, HOST_CONFIG2(host));
130         au_sync();
131
132         /* Send the stop commmand */
133         au_writel(STOP_CMD, HOST_CMD(host));
134 }
135
136 static void au1xmmc_set_power(struct au1xmmc_host *host, int state)
137 {
138
139         u32 val = au1xmmc_card_table[host->id].bcsrpwr;
140
141         bcsr->board &= ~val;
142         if (state) bcsr->board |= val;
143
144         au_sync_delay(1);
145 }
146
147 static inline int au1xmmc_card_inserted(struct au1xmmc_host *host)
148 {
149         return (bcsr->sig_status & au1xmmc_card_table[host->id].bcsrstatus)
150                 ? 1 : 0;
151 }
152
153 static int au1xmmc_card_readonly(struct mmc_host *mmc)
154 {
155         struct au1xmmc_host *host = mmc_priv(mmc);
156         return (bcsr->status & au1xmmc_card_table[host->id].wpstatus)
157                 ? 1 : 0;
158 }
159
160 static void au1xmmc_finish_request(struct au1xmmc_host *host)
161 {
162
163         struct mmc_request *mrq = host->mrq;
164
165         host->mrq = NULL;
166         host->flags &= HOST_F_ACTIVE;
167
168         host->dma.len = 0;
169         host->dma.dir = 0;
170
171         host->pio.index  = 0;
172         host->pio.offset = 0;
173         host->pio.len = 0;
174
175         host->status = HOST_S_IDLE;
176
177         bcsr->disk_leds |= (1 << 8);
178
179         mmc_request_done(host->mmc, mrq);
180 }
181
182 static void au1xmmc_tasklet_finish(unsigned long param)
183 {
184         struct au1xmmc_host *host = (struct au1xmmc_host *) param;
185         au1xmmc_finish_request(host);
186 }
187
188 static int au1xmmc_send_command(struct au1xmmc_host *host, int wait,
189                                 struct mmc_command *cmd, struct mmc_data *data)
190 {
191         u32 mmccmd = (cmd->opcode << SD_CMD_CI_SHIFT);
192
193         switch (mmc_resp_type(cmd)) {
194         case MMC_RSP_NONE:
195                 break;
196         case MMC_RSP_R1:
197                 mmccmd |= SD_CMD_RT_1;
198                 break;
199         case MMC_RSP_R1B:
200                 mmccmd |= SD_CMD_RT_1B;
201                 break;
202         case MMC_RSP_R2:
203                 mmccmd |= SD_CMD_RT_2;
204                 break;
205         case MMC_RSP_R3:
206                 mmccmd |= SD_CMD_RT_3;
207                 break;
208         default:
209                 printk(KERN_INFO "au1xmmc: unhandled response type %02x\n",
210                         mmc_resp_type(cmd));
211                 return -EINVAL;
212         }
213
214         if (data) {
215                 if (flags & MMC_DATA_READ) {
216                         if (data->blocks > 1)
217                                 mmccmd |= SD_CMD_CT_4;
218                         else
219                                 mmccmd |= SD_CMD_CT_2;
220                 } else if (flags & MMC_DATA_WRITE) {
221                         if (data->blocks > 1)
222                                 mmccmd |= SD_CMD_CT_3;
223                         else
224                                 mmccmd |= SD_CMD_CT_1;
225                 }
226         }
227
228         au_writel(cmd->arg, HOST_CMDARG(host));
229         au_sync();
230
231         if (wait)
232                 IRQ_OFF(host, SD_CONFIG_CR);
233
234         au_writel((mmccmd | SD_CMD_GO), HOST_CMD(host));
235         au_sync();
236
237         /* Wait for the command to go on the line */
238
239         while(1) {
240                 if (!(au_readl(HOST_CMD(host)) & SD_CMD_GO))
241                         break;
242         }
243
244         /* Wait for the command to come back */
245
246         if (wait) {
247                 u32 status = au_readl(HOST_STATUS(host));
248
249                 while(!(status & SD_STATUS_CR))
250                         status = au_readl(HOST_STATUS(host));
251
252                 /* Clear the CR status */
253                 au_writel(SD_STATUS_CR, HOST_STATUS(host));
254
255                 IRQ_ON(host, SD_CONFIG_CR);
256         }
257
258         return 0;
259 }
260
261 static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
262 {
263
264         struct mmc_request *mrq = host->mrq;
265         struct mmc_data *data;
266         u32 crc;
267
268         WARN_ON(host->status != HOST_S_DATA && host->status != HOST_S_STOP);
269
270         if (host->mrq == NULL)
271                 return;
272
273         data = mrq->cmd->data;
274
275         if (status == 0)
276                 status = au_readl(HOST_STATUS(host));
277
278         /* The transaction is really over when the SD_STATUS_DB bit is clear */
279
280         while((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB))
281                 status = au_readl(HOST_STATUS(host));
282
283         data->error = 0;
284         dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir);
285
286         /* Process any errors */
287
288         crc = (status & (SD_STATUS_WC | SD_STATUS_RC));
289         if (host->flags & HOST_F_XMIT)
290                 crc |= ((status & 0x07) == 0x02) ? 0 : 1;
291
292         if (crc)
293                 data->error = -EILSEQ;
294
295         /* Clear the CRC bits */
296         au_writel(SD_STATUS_WC | SD_STATUS_RC, HOST_STATUS(host));
297
298         data->bytes_xfered = 0;
299
300         if (!data->error) {
301                 if (host->flags & HOST_F_DMA) {
302                         u32 chan = DMA_CHANNEL(host);
303
304                         chan_tab_t *c = *((chan_tab_t **) chan);
305                         au1x_dma_chan_t *cp = c->chan_ptr;
306                         data->bytes_xfered = cp->ddma_bytecnt;
307                 }
308                 else
309                         data->bytes_xfered =
310                                 (data->blocks * data->blksz) -
311                                 host->pio.len;
312         }
313
314         au1xmmc_finish_request(host);
315 }
316
317 static void au1xmmc_tasklet_data(unsigned long param)
318 {
319         struct au1xmmc_host *host = (struct au1xmmc_host *) param;
320
321         u32 status = au_readl(HOST_STATUS(host));
322         au1xmmc_data_complete(host, status);
323 }
324
325 #define AU1XMMC_MAX_TRANSFER 8
326
327 static void au1xmmc_send_pio(struct au1xmmc_host *host)
328 {
329
330         struct mmc_data *data = 0;
331         int sg_len, max, count = 0;
332         unsigned char *sg_ptr;
333         u32 status = 0;
334         struct scatterlist *sg;
335
336         data = host->mrq->data;
337
338         if (!(host->flags & HOST_F_XMIT))
339                 return;
340
341         /* This is the pointer to the data buffer */
342         sg = &data->sg[host->pio.index];
343         sg_ptr = page_address(sg->page) + sg->offset + host->pio.offset;
344
345         /* This is the space left inside the buffer */
346         sg_len = data->sg[host->pio.index].length - host->pio.offset;
347
348         /* Check to if we need less then the size of the sg_buffer */
349
350         max = (sg_len > host->pio.len) ? host->pio.len : sg_len;
351         if (max > AU1XMMC_MAX_TRANSFER) max = AU1XMMC_MAX_TRANSFER;
352
353         for(count = 0; count < max; count++ ) {
354                 unsigned char val;
355
356                 status = au_readl(HOST_STATUS(host));
357
358                 if (!(status & SD_STATUS_TH))
359                         break;
360
361                 val = *sg_ptr++;
362
363                 au_writel((unsigned long) val, HOST_TXPORT(host));
364                 au_sync();
365         }
366
367         host->pio.len -= count;
368         host->pio.offset += count;
369
370         if (count == sg_len) {
371                 host->pio.index++;
372                 host->pio.offset = 0;
373         }
374
375         if (host->pio.len == 0) {
376                 IRQ_OFF(host, SD_CONFIG_TH);
377
378                 if (host->flags & HOST_F_STOP)
379                         SEND_STOP(host);
380
381                 tasklet_schedule(&host->data_task);
382         }
383 }
384
385 static void au1xmmc_receive_pio(struct au1xmmc_host *host)
386 {
387
388         struct mmc_data *data = 0;
389         int sg_len = 0, max = 0, count = 0;
390         unsigned char *sg_ptr = 0;
391         u32 status = 0;
392         struct scatterlist *sg;
393
394         data = host->mrq->data;
395
396         if (!(host->flags & HOST_F_RECV))
397                 return;
398
399         max = host->pio.len;
400
401         if (host->pio.index < host->dma.len) {
402                 sg = &data->sg[host->pio.index];
403                 sg_ptr = page_address(sg->page) + sg->offset + host->pio.offset;
404
405                 /* This is the space left inside the buffer */
406                 sg_len = sg_dma_len(&data->sg[host->pio.index]) - host->pio.offset;
407
408                 /* Check to if we need less then the size of the sg_buffer */
409                 if (sg_len < max) max = sg_len;
410         }
411
412         if (max > AU1XMMC_MAX_TRANSFER)
413                 max = AU1XMMC_MAX_TRANSFER;
414
415         for(count = 0; count < max; count++ ) {
416                 u32 val;
417                 status = au_readl(HOST_STATUS(host));
418
419                 if (!(status & SD_STATUS_NE))
420                         break;
421
422                 if (status & SD_STATUS_RC) {
423                         DBG("RX CRC Error [%d + %d].\n", host->id,
424                                         host->pio.len, count);
425                         break;
426                 }
427
428                 if (status & SD_STATUS_RO) {
429                         DBG("RX Overrun [%d + %d]\n", host->id,
430                                         host->pio.len, count);
431                         break;
432                 }
433                 else if (status & SD_STATUS_RU) {
434                         DBG("RX Underrun [%d + %d]\n", host->id,
435                                         host->pio.len,  count);
436                         break;
437                 }
438
439                 val = au_readl(HOST_RXPORT(host));
440
441                 if (sg_ptr)
442                         *sg_ptr++ = (unsigned char) (val & 0xFF);
443         }
444
445         host->pio.len -= count;
446         host->pio.offset += count;
447
448         if (sg_len && count == sg_len) {
449                 host->pio.index++;
450                 host->pio.offset = 0;
451         }
452
453         if (host->pio.len == 0) {
454                 //IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF);
455                 IRQ_OFF(host, SD_CONFIG_NE);
456
457                 if (host->flags & HOST_F_STOP)
458                         SEND_STOP(host);
459
460                 tasklet_schedule(&host->data_task);
461         }
462 }
463
464 /* static void au1xmmc_cmd_complete
465    This is called when a command has been completed - grab the response
466    and check for errors.  Then start the data transfer if it is indicated.
467 */
468
469 static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
470 {
471
472         struct mmc_request *mrq = host->mrq;
473         struct mmc_command *cmd;
474         int trans;
475
476         if (!host->mrq)
477                 return;
478
479         cmd = mrq->cmd;
480         cmd->error = 0;
481
482         if (cmd->flags & MMC_RSP_PRESENT) {
483                 if (cmd->flags & MMC_RSP_136) {
484                         u32 r[4];
485                         int i;
486
487                         r[0] = au_readl(host->iobase + SD_RESP3);
488                         r[1] = au_readl(host->iobase + SD_RESP2);
489                         r[2] = au_readl(host->iobase + SD_RESP1);
490                         r[3] = au_readl(host->iobase + SD_RESP0);
491
492                         /* The CRC is omitted from the response, so really
493                          * we only got 120 bytes, but the engine expects
494                          * 128 bits, so we have to shift things up
495                          */
496
497                         for(i = 0; i < 4; i++) {
498                                 cmd->resp[i] = (r[i] & 0x00FFFFFF) << 8;
499                                 if (i != 3)
500                                         cmd->resp[i] |= (r[i + 1] & 0xFF000000) >> 24;
501                         }
502                 } else {
503                         /* Techincally, we should be getting all 48 bits of
504                          * the response (SD_RESP1 + SD_RESP2), but because
505                          * our response omits the CRC, our data ends up
506                          * being shifted 8 bits to the right.  In this case,
507                          * that means that the OSR data starts at bit 31,
508                          * so we can just read RESP0 and return that
509                          */
510                         cmd->resp[0] = au_readl(host->iobase + SD_RESP0);
511                 }
512         }
513
514         /* Figure out errors */
515
516         if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC))
517                 cmd->error = -EILSEQ;
518
519         trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);
520
521         if (!trans || cmd->error) {
522
523                 IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA|SD_CONFIG_RF);
524                 tasklet_schedule(&host->finish_task);
525                 return;
526         }
527
528         host->status = HOST_S_DATA;
529
530         if (host->flags & HOST_F_DMA) {
531                 u32 channel = DMA_CHANNEL(host);
532
533                 /* Start the DMA as soon as the buffer gets something in it */
534
535                 if (host->flags & HOST_F_RECV) {
536                         u32 mask = SD_STATUS_DB | SD_STATUS_NE;
537
538                         while((status & mask) != mask)
539                                 status = au_readl(HOST_STATUS(host));
540                 }
541
542                 au1xxx_dbdma_start(channel);
543         }
544 }
545
546 static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate)
547 {
548
549         unsigned int pbus = get_au1x00_speed();
550         unsigned int divisor;
551         u32 config;
552
553         /* From databook:
554            divisor = ((((cpuclock / sbus_divisor) / 2) / mmcclock) / 2) - 1
555         */
556
557         pbus /= ((au_readl(SYS_POWERCTRL) & 0x3) + 2);
558         pbus /= 2;
559
560         divisor = ((pbus / rate) / 2) - 1;
561
562         config = au_readl(HOST_CONFIG(host));
563
564         config &= ~(SD_CONFIG_DIV);
565         config |= (divisor & SD_CONFIG_DIV) | SD_CONFIG_DE;
566
567         au_writel(config, HOST_CONFIG(host));
568         au_sync();
569 }
570
571 static int
572 au1xmmc_prepare_data(struct au1xmmc_host *host, struct mmc_data *data)
573 {
574
575         int datalen = data->blocks * data->blksz;
576
577         if (dma != 0)
578                 host->flags |= HOST_F_DMA;
579
580         if (data->flags & MMC_DATA_READ)
581                 host->flags |= HOST_F_RECV;
582         else
583                 host->flags |= HOST_F_XMIT;
584
585         if (host->mrq->stop)
586                 host->flags |= HOST_F_STOP;
587
588         host->dma.dir = DMA_BIDIRECTIONAL;
589
590         host->dma.len = dma_map_sg(mmc_dev(host->mmc), data->sg,
591                                    data->sg_len, host->dma.dir);
592
593         if (host->dma.len == 0)
594                 return -ETIMEDOUT;
595
596         au_writel(data->blksz - 1, HOST_BLKSIZE(host));
597
598         if (host->flags & HOST_F_DMA) {
599                 int i;
600                 u32 channel = DMA_CHANNEL(host);
601
602                 au1xxx_dbdma_stop(channel);
603
604                 for(i = 0; i < host->dma.len; i++) {
605                         u32 ret = 0, flags = DDMA_FLAGS_NOIE;
606                         struct scatterlist *sg = &data->sg[i];
607                         int sg_len = sg->length;
608
609                         int len = (datalen > sg_len) ? sg_len : datalen;
610
611                         if (i == host->dma.len - 1)
612                                 flags = DDMA_FLAGS_IE;
613
614                         if (host->flags & HOST_F_XMIT){
615                                 ret = au1xxx_dbdma_put_source_flags(channel,
616                                         (void *) (page_address(sg->page) +
617                                                   sg->offset),
618                                         len, flags);
619                         }
620                         else {
621                                 ret = au1xxx_dbdma_put_dest_flags(channel,
622                                         (void *) (page_address(sg->page) +
623                                                   sg->offset),
624                                         len, flags);
625                         }
626
627                         if (!ret)
628                                 goto dataerr;
629
630                         datalen -= len;
631                 }
632         }
633         else {
634                 host->pio.index = 0;
635                 host->pio.offset = 0;
636                 host->pio.len = datalen;
637
638                 if (host->flags & HOST_F_XMIT)
639                         IRQ_ON(host, SD_CONFIG_TH);
640                 else
641                         IRQ_ON(host, SD_CONFIG_NE);
642                         //IRQ_ON(host, SD_CONFIG_RA|SD_CONFIG_RF);
643         }
644
645         return 0;
646
647  dataerr:
648         dma_unmap_sg(mmc_dev(host->mmc),data->sg,data->sg_len,host->dma.dir);
649         return -ETIMEDOUT;
650 }
651
652 /* static void au1xmmc_request
653    This actually starts a command or data transaction
654 */
655
656 static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
657 {
658
659         struct au1xmmc_host *host = mmc_priv(mmc);
660         unsigned int flags = 0;
661         int ret = 0;
662
663         WARN_ON(irqs_disabled());
664         WARN_ON(host->status != HOST_S_IDLE);
665
666         host->mrq = mrq;
667         host->status = HOST_S_CMD;
668
669         bcsr->disk_leds &= ~(1 << 8);
670
671         if (mrq->data) {
672                 FLUSH_FIFO(host);
673                 flags = mrq->data->flags;
674                 ret = au1xmmc_prepare_data(host, mrq->data);
675         }
676
677         if (!ret)
678                 ret = au1xmmc_send_command(host, 0, mrq->cmd, mrq->data);
679
680         if (ret) {
681                 mrq->cmd->error = ret;
682                 au1xmmc_finish_request(host);
683         }
684 }
685
686 static void au1xmmc_reset_controller(struct au1xmmc_host *host)
687 {
688
689         /* Apply the clock */
690         au_writel(SD_ENABLE_CE, HOST_ENABLE(host));
691         au_sync_delay(1);
692
693         au_writel(SD_ENABLE_R | SD_ENABLE_CE, HOST_ENABLE(host));
694         au_sync_delay(5);
695
696         au_writel(~0, HOST_STATUS(host));
697         au_sync();
698
699         au_writel(0, HOST_BLKSIZE(host));
700         au_writel(0x001fffff, HOST_TIMEOUT(host));
701         au_sync();
702
703         au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
704         au_sync();
705
706         au_writel(SD_CONFIG2_EN | SD_CONFIG2_FF, HOST_CONFIG2(host));
707         au_sync_delay(1);
708
709         au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
710         au_sync();
711
712         /* Configure interrupts */
713         au_writel(AU1XMMC_INTERRUPTS, HOST_CONFIG(host));
714         au_sync();
715 }
716
717
718 static void au1xmmc_set_ios(struct mmc_host* mmc, struct mmc_ios* ios)
719 {
720         struct au1xmmc_host *host = mmc_priv(mmc);
721
722         if (ios->power_mode == MMC_POWER_OFF)
723                 au1xmmc_set_power(host, 0);
724         else if (ios->power_mode == MMC_POWER_ON) {
725                 au1xmmc_set_power(host, 1);
726         }
727
728         if (ios->clock && ios->clock != host->clock) {
729                 au1xmmc_set_clock(host, ios->clock);
730                 host->clock = ios->clock;
731         }
732 }
733
734 static void au1xmmc_dma_callback(int irq, void *dev_id)
735 {
736         struct au1xmmc_host *host = (struct au1xmmc_host *) dev_id;
737
738         /* Avoid spurious interrupts */
739
740         if (!host->mrq)
741                 return;
742
743         if (host->flags & HOST_F_STOP)
744                 SEND_STOP(host);
745
746         tasklet_schedule(&host->data_task);
747 }
748
749 #define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
750 #define STATUS_DATA_IN  (SD_STATUS_NE)
751 #define STATUS_DATA_OUT (SD_STATUS_TH)
752
753 static irqreturn_t au1xmmc_irq(int irq, void *dev_id)
754 {
755
756         u32 status;
757         int i, ret = 0;
758
759         disable_irq(AU1100_SD_IRQ);
760
761         for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
762                 struct au1xmmc_host * host = au1xmmc_hosts[i];
763                 u32 handled = 1;
764
765                 status = au_readl(HOST_STATUS(host));
766
767                 if (host->mrq && (status & STATUS_TIMEOUT)) {
768                         if (status & SD_STATUS_RAT)
769                                 host->mrq->cmd->error = -ETIMEDOUT;
770
771                         else if (status & SD_STATUS_DT)
772                                 host->mrq->data->error = -ETIMEDOUT;
773
774                         /* In PIO mode, interrupts might still be enabled */
775                         IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
776
777                         //IRQ_OFF(host, SD_CONFIG_TH|SD_CONFIG_RA|SD_CONFIG_RF);
778                         tasklet_schedule(&host->finish_task);
779                 }
780 #if 0
781                 else if (status & SD_STATUS_DD) {
782
783                         /* Sometimes we get a DD before a NE in PIO mode */
784
785                         if (!(host->flags & HOST_F_DMA) &&
786                                         (status & SD_STATUS_NE))
787                                 au1xmmc_receive_pio(host);
788                         else {
789                                 au1xmmc_data_complete(host, status);
790                                 //tasklet_schedule(&host->data_task);
791                         }
792                 }
793 #endif
794                 else if (status & (SD_STATUS_CR)) {
795                         if (host->status == HOST_S_CMD)
796                                 au1xmmc_cmd_complete(host,status);
797                 }
798                 else if (!(host->flags & HOST_F_DMA)) {
799                         if ((host->flags & HOST_F_XMIT) &&
800                             (status & STATUS_DATA_OUT))
801                                 au1xmmc_send_pio(host);
802                         else if ((host->flags & HOST_F_RECV) &&
803                             (status & STATUS_DATA_IN))
804                                 au1xmmc_receive_pio(host);
805                 }
806                 else if (status & 0x203FBC70) {
807                         DBG("Unhandled status %8.8x\n", host->id, status);
808                         handled = 0;
809                 }
810
811                 au_writel(status, HOST_STATUS(host));
812                 au_sync();
813
814                 ret |= handled;
815         }
816
817         enable_irq(AU1100_SD_IRQ);
818         return ret;
819 }
820
821 static void au1xmmc_poll_event(unsigned long arg)
822 {
823         struct au1xmmc_host *host = (struct au1xmmc_host *) arg;
824
825         int card = au1xmmc_card_inserted(host);
826         int controller = (host->flags & HOST_F_ACTIVE) ? 1 : 0;
827
828         if (card != controller) {
829                 host->flags &= ~HOST_F_ACTIVE;
830                 if (card) host->flags |= HOST_F_ACTIVE;
831                 mmc_detect_change(host->mmc, 0);
832         }
833
834         if (host->mrq != NULL) {
835                 u32 status = au_readl(HOST_STATUS(host));
836                 DBG("PENDING - %8.8x\n", host->id, status);
837         }
838
839         mod_timer(&host->timer, jiffies + AU1XMMC_DETECT_TIMEOUT);
840 }
841
842 static dbdev_tab_t au1xmmc_mem_dbdev =
843 {
844         DSCR_CMD0_ALWAYS, DEV_FLAGS_ANYUSE, 0, 8, 0x00000000, 0, 0
845 };
846
847 static void au1xmmc_init_dma(struct au1xmmc_host *host)
848 {
849
850         u32 rxchan, txchan;
851
852         int txid = au1xmmc_card_table[host->id].tx_devid;
853         int rxid = au1xmmc_card_table[host->id].rx_devid;
854
855         /* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride
856            of 8 bits.  And since devices are shared, we need to create
857            our own to avoid freaking out other devices
858         */
859
860         int memid = au1xxx_ddma_add_device(&au1xmmc_mem_dbdev);
861
862         txchan = au1xxx_dbdma_chan_alloc(memid, txid,
863                                          au1xmmc_dma_callback, (void *) host);
864
865         rxchan = au1xxx_dbdma_chan_alloc(rxid, memid,
866                                          au1xmmc_dma_callback, (void *) host);
867
868         au1xxx_dbdma_set_devwidth(txchan, 8);
869         au1xxx_dbdma_set_devwidth(rxchan, 8);
870
871         au1xxx_dbdma_ring_alloc(txchan, AU1XMMC_DESCRIPTOR_COUNT);
872         au1xxx_dbdma_ring_alloc(rxchan, AU1XMMC_DESCRIPTOR_COUNT);
873
874         host->tx_chan = txchan;
875         host->rx_chan = rxchan;
876 }
877
878 static const struct mmc_host_ops au1xmmc_ops = {
879         .request        = au1xmmc_request,
880         .set_ios        = au1xmmc_set_ios,
881         .get_ro         = au1xmmc_card_readonly,
882 };
883
884 static int __devinit au1xmmc_probe(struct platform_device *pdev)
885 {
886
887         int i, ret = 0;
888
889         /* THe interrupt is shared among all controllers */
890         ret = request_irq(AU1100_SD_IRQ, au1xmmc_irq, IRQF_DISABLED, "MMC", 0);
891
892         if (ret) {
893                 printk(DRIVER_NAME "ERROR: Couldn't get int %d: %d\n",
894                                 AU1100_SD_IRQ, ret);
895                 return -ENXIO;
896         }
897
898         disable_irq(AU1100_SD_IRQ);
899
900         for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
901                 struct mmc_host *mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), &pdev->dev);
902                 struct au1xmmc_host *host = 0;
903
904                 if (!mmc) {
905                         printk(DRIVER_NAME "ERROR: no mem for host %d\n", i);
906                         au1xmmc_hosts[i] = 0;
907                         continue;
908                 }
909
910                 mmc->ops = &au1xmmc_ops;
911
912                 mmc->f_min =   450000;
913                 mmc->f_max = 24000000;
914
915                 mmc->max_seg_size = AU1XMMC_DESCRIPTOR_SIZE;
916                 mmc->max_phys_segs = AU1XMMC_DESCRIPTOR_COUNT;
917
918                 mmc->max_blk_size = 2048;
919                 mmc->max_blk_count = 512;
920
921                 mmc->ocr_avail = AU1XMMC_OCR;
922
923                 host = mmc_priv(mmc);
924                 host->mmc = mmc;
925
926                 host->id = i;
927                 host->iobase = au1xmmc_card_table[host->id].iobase;
928                 host->clock = 0;
929                 host->power_mode = MMC_POWER_OFF;
930
931                 host->flags = au1xmmc_card_inserted(host) ? HOST_F_ACTIVE : 0;
932                 host->status = HOST_S_IDLE;
933
934                 init_timer(&host->timer);
935
936                 host->timer.function = au1xmmc_poll_event;
937                 host->timer.data = (unsigned long) host;
938                 host->timer.expires = jiffies + AU1XMMC_DETECT_TIMEOUT;
939
940                 tasklet_init(&host->data_task, au1xmmc_tasklet_data,
941                                 (unsigned long) host);
942
943                 tasklet_init(&host->finish_task, au1xmmc_tasklet_finish,
944                                 (unsigned long) host);
945
946                 spin_lock_init(&host->lock);
947
948                 if (dma != 0)
949                         au1xmmc_init_dma(host);
950
951                 au1xmmc_reset_controller(host);
952
953                 mmc_add_host(mmc);
954                 au1xmmc_hosts[i] = host;
955
956                 add_timer(&host->timer);
957
958                 printk(KERN_INFO DRIVER_NAME ": MMC Controller %d set up at %8.8X (mode=%s)\n",
959                        host->id, host->iobase, dma ? "dma" : "pio");
960         }
961
962         enable_irq(AU1100_SD_IRQ);
963
964         return 0;
965 }
966
967 static int __devexit au1xmmc_remove(struct platform_device *pdev)
968 {
969
970         int i;
971
972         disable_irq(AU1100_SD_IRQ);
973
974         for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
975                 struct au1xmmc_host *host = au1xmmc_hosts[i];
976                 if (!host) continue;
977
978                 tasklet_kill(&host->data_task);
979                 tasklet_kill(&host->finish_task);
980
981                 del_timer_sync(&host->timer);
982                 au1xmmc_set_power(host, 0);
983
984                 mmc_remove_host(host->mmc);
985
986                 au1xxx_dbdma_chan_free(host->tx_chan);
987                 au1xxx_dbdma_chan_free(host->rx_chan);
988
989                 au_writel(0x0, HOST_ENABLE(host));
990                 au_sync();
991         }
992
993         free_irq(AU1100_SD_IRQ, 0);
994         return 0;
995 }
996
997 static struct platform_driver au1xmmc_driver = {
998         .probe         = au1xmmc_probe,
999         .remove        = au1xmmc_remove,
1000         .suspend       = NULL,
1001         .resume        = NULL,
1002         .driver        = {
1003                 .name  = DRIVER_NAME,
1004         },
1005 };
1006
1007 static int __init au1xmmc_init(void)
1008 {
1009         return platform_driver_register(&au1xmmc_driver);
1010 }
1011
1012 static void __exit au1xmmc_exit(void)
1013 {
1014         platform_driver_unregister(&au1xmmc_driver);
1015 }
1016
1017 module_init(au1xmmc_init);
1018 module_exit(au1xmmc_exit);
1019
1020 #ifdef MODULE
1021 MODULE_AUTHOR("Advanced Micro Devices, Inc");
1022 MODULE_DESCRIPTION("MMC/SD driver for the Alchemy Au1XXX");
1023 MODULE_LICENSE("GPL");
1024 #endif
1025