2 * drivers/mmc/host/via-sdmmc.c - VIA SD/MMC Card Reader driver
3 * Copyright (c) 2008, VIA Technologies Inc. All Rights Reserved.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
11 #include <linux/pci.h>
12 #include <linux/module.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/highmem.h>
15 #include <linux/delay.h>
17 #include <linux/mmc/host.h>
19 #define DRV_NAME "via_sdmmc"
21 #define PCI_DEVICE_ID_VIA_9530 0x9530
23 #define VIA_CRDR_SDC_OFF 0x200
24 #define VIA_CRDR_DDMA_OFF 0x400
25 #define VIA_CRDR_PCICTRL_OFF 0x600
27 #define VIA_CRDR_MIN_CLOCK 375000
28 #define VIA_CRDR_MAX_CLOCK 48000000
34 #define VIA_CRDR_PCI_WORK_MODE 0x40
35 #define VIA_CRDR_PCI_DBG_MODE 0x41
41 #define VIA_CRDR_SDCTRL 0x0
42 #define VIA_CRDR_SDCTRL_START 0x01
43 #define VIA_CRDR_SDCTRL_WRITE 0x04
44 #define VIA_CRDR_SDCTRL_SINGLE_WR 0x10
45 #define VIA_CRDR_SDCTRL_SINGLE_RD 0x20
46 #define VIA_CRDR_SDCTRL_MULTI_WR 0x30
47 #define VIA_CRDR_SDCTRL_MULTI_RD 0x40
48 #define VIA_CRDR_SDCTRL_STOP 0x70
50 #define VIA_CRDR_SDCTRL_RSP_NONE 0x0
51 #define VIA_CRDR_SDCTRL_RSP_R1 0x10000
52 #define VIA_CRDR_SDCTRL_RSP_R2 0x20000
53 #define VIA_CRDR_SDCTRL_RSP_R3 0x30000
54 #define VIA_CRDR_SDCTRL_RSP_R1B 0x90000
56 #define VIA_CRDR_SDCARG 0x4
58 #define VIA_CRDR_SDBUSMODE 0x8
59 #define VIA_CRDR_SDMODE_4BIT 0x02
60 #define VIA_CRDR_SDMODE_CLK_ON 0x40
62 #define VIA_CRDR_SDBLKLEN 0xc
64 * Bit 0 -Bit 10 : Block length. So, the maximum block length should be 2048.
65 * Bit 11 - Bit 13 : Reserved.
66 * GPIDET : Select GPI pin to detect card, GPI means CR_CD# in top design.
67 * INTEN : Enable SD host interrupt.
68 * Bit 16 - Bit 31 : Block count. So, the maximun block count should be 65536.
70 #define VIA_CRDR_SDBLKLEN_GPIDET 0x2000
71 #define VIA_CRDR_SDBLKLEN_INTEN 0x8000
72 #define VIA_CRDR_MAX_BLOCK_COUNT 65536
73 #define VIA_CRDR_MAX_BLOCK_LENGTH 2048
75 #define VIA_CRDR_SDRESP0 0x10
76 #define VIA_CRDR_SDRESP1 0x14
77 #define VIA_CRDR_SDRESP2 0x18
78 #define VIA_CRDR_SDRESP3 0x1c
80 #define VIA_CRDR_SDCURBLKCNT 0x20
82 #define VIA_CRDR_SDINTMASK 0x24
84 * MBDIE : Multiple Blocks transfer Done Interrupt Enable
85 * BDDIE : Block Data transfer Done Interrupt Enable
86 * CIRIE : Card Insertion or Removal Interrupt Enable
87 * CRDIE : Command-Response transfer Done Interrupt Enable
88 * CRTOIE : Command-Response response TimeOut Interrupt Enable
89 * ASCRDIE : Auto Stop Command-Response transfer Done Interrupt Enable
90 * DTIE : Data access Timeout Interrupt Enable
91 * SCIE : reSponse CRC error Interrupt Enable
92 * RCIE : Read data CRC error Interrupt Enable
93 * WCIE : Write data CRC error Interrupt Enable
95 #define VIA_CRDR_SDINTMASK_MBDIE 0x10
96 #define VIA_CRDR_SDINTMASK_BDDIE 0x20
97 #define VIA_CRDR_SDINTMASK_CIRIE 0x80
98 #define VIA_CRDR_SDINTMASK_CRDIE 0x200
99 #define VIA_CRDR_SDINTMASK_CRTOIE 0x400
100 #define VIA_CRDR_SDINTMASK_ASCRDIE 0x800
101 #define VIA_CRDR_SDINTMASK_DTIE 0x1000
102 #define VIA_CRDR_SDINTMASK_SCIE 0x2000
103 #define VIA_CRDR_SDINTMASK_RCIE 0x4000
104 #define VIA_CRDR_SDINTMASK_WCIE 0x8000
106 #define VIA_CRDR_SDACTIVE_INTMASK \
107 (VIA_CRDR_SDINTMASK_MBDIE | VIA_CRDR_SDINTMASK_CIRIE \
108 | VIA_CRDR_SDINTMASK_CRDIE | VIA_CRDR_SDINTMASK_CRTOIE \
109 | VIA_CRDR_SDINTMASK_DTIE | VIA_CRDR_SDINTMASK_SCIE \
110 | VIA_CRDR_SDINTMASK_RCIE | VIA_CRDR_SDINTMASK_WCIE)
112 #define VIA_CRDR_SDSTATUS 0x28
115 * WP : SD card Write Protect status
117 * SLOTG : SD SLOT status(Gpi pin status)
118 * MBD : Multiple Blocks transfer Done interrupt status
119 * BDD : Block Data transfer Done interrupt status
121 * CIR : Card Insertion or Removal interrupt detected on GPI pin
123 * CRD : Command-Response transfer Done interrupt status
124 * CRTO : Command-Response response TimeOut interrupt status
125 * ASCRDIE : Auto Stop Command-Response transfer Done interrupt status
126 * DT : Data access Timeout interrupt status
127 * SC : reSponse CRC error interrupt status
128 * RC : Read data CRC error interrupt status
129 * WC : Write data CRC error interrupt status
131 #define VIA_CRDR_SDSTS_CECC 0x01
132 #define VIA_CRDR_SDSTS_WP 0x02
133 #define VIA_CRDR_SDSTS_SLOTD 0x04
134 #define VIA_CRDR_SDSTS_SLOTG 0x08
135 #define VIA_CRDR_SDSTS_MBD 0x10
136 #define VIA_CRDR_SDSTS_BDD 0x20
137 #define VIA_CRDR_SDSTS_CD 0x40
138 #define VIA_CRDR_SDSTS_CIR 0x80
139 #define VIA_CRDR_SDSTS_IO 0x100
140 #define VIA_CRDR_SDSTS_CRD 0x200
141 #define VIA_CRDR_SDSTS_CRTO 0x400
142 #define VIA_CRDR_SDSTS_ASCRDIE 0x800
143 #define VIA_CRDR_SDSTS_DT 0x1000
144 #define VIA_CRDR_SDSTS_SC 0x2000
145 #define VIA_CRDR_SDSTS_RC 0x4000
146 #define VIA_CRDR_SDSTS_WC 0x8000
148 #define VIA_CRDR_SDSTS_IGN_MASK\
149 (VIA_CRDR_SDSTS_BDD | VIA_CRDR_SDSTS_ASCRDIE | VIA_CRDR_SDSTS_IO)
150 #define VIA_CRDR_SDSTS_INT_MASK \
151 (VIA_CRDR_SDSTS_MBD | VIA_CRDR_SDSTS_BDD | VIA_CRDR_SDSTS_CD \
152 | VIA_CRDR_SDSTS_CIR | VIA_CRDR_SDSTS_IO | VIA_CRDR_SDSTS_CRD \
153 | VIA_CRDR_SDSTS_CRTO | VIA_CRDR_SDSTS_ASCRDIE | VIA_CRDR_SDSTS_DT \
154 | VIA_CRDR_SDSTS_SC | VIA_CRDR_SDSTS_RC | VIA_CRDR_SDSTS_WC)
155 #define VIA_CRDR_SDSTS_W1C_MASK \
156 (VIA_CRDR_SDSTS_CECC | VIA_CRDR_SDSTS_MBD | VIA_CRDR_SDSTS_BDD \
157 | VIA_CRDR_SDSTS_CD | VIA_CRDR_SDSTS_CIR | VIA_CRDR_SDSTS_CRD \
158 | VIA_CRDR_SDSTS_CRTO | VIA_CRDR_SDSTS_ASCRDIE | VIA_CRDR_SDSTS_DT \
159 | VIA_CRDR_SDSTS_SC | VIA_CRDR_SDSTS_RC | VIA_CRDR_SDSTS_WC)
160 #define VIA_CRDR_SDSTS_CMD_MASK \
161 (VIA_CRDR_SDSTS_CRD | VIA_CRDR_SDSTS_CRTO | VIA_CRDR_SDSTS_SC)
162 #define VIA_CRDR_SDSTS_DATA_MASK\
163 (VIA_CRDR_SDSTS_MBD | VIA_CRDR_SDSTS_DT \
164 | VIA_CRDR_SDSTS_RC | VIA_CRDR_SDSTS_WC)
166 #define VIA_CRDR_SDSTATUS2 0x2a
168 * CFE : Enable SD host automatic Clock FReezing
170 #define VIA_CRDR_SDSTS_CFE 0x80
172 #define VIA_CRDR_SDRSPTMO 0x2C
174 #define VIA_CRDR_SDCLKSEL 0x30
176 #define VIA_CRDR_SDEXTCTRL 0x34
177 #define VIS_CRDR_SDEXTCTRL_AUTOSTOP_SD 0x01
178 #define VIS_CRDR_SDEXTCTRL_SHIFT_9 0x02
179 #define VIS_CRDR_SDEXTCTRL_MMC_8BIT 0x04
180 #define VIS_CRDR_SDEXTCTRL_RELD_BLK 0x08
181 #define VIS_CRDR_SDEXTCTRL_BAD_CMDA 0x10
182 #define VIS_CRDR_SDEXTCTRL_BAD_DATA 0x20
183 #define VIS_CRDR_SDEXTCTRL_AUTOSTOP_SPI 0x40
184 #define VIA_CRDR_SDEXTCTRL_HISPD 0x80
185 /* 0x38-0xFF reserved */
188 * Data DMA Control Registers
191 #define VIA_CRDR_DMABASEADD 0x0
192 #define VIA_CRDR_DMACOUNTER 0x4
194 #define VIA_CRDR_DMACTRL 0x8
196 * DIR :Transaction Direction
197 * 0 : From card to memory
198 * 1 : From memory to card
200 #define VIA_CRDR_DMACTRL_DIR 0x100
201 #define VIA_CRDR_DMACTRL_ENIRQ 0x10000
202 #define VIA_CRDR_DMACTRL_SFTRST 0x1000000
204 #define VIA_CRDR_DMASTS 0xc
206 #define VIA_CRDR_DMASTART 0x10
207 /*0x14-0xFF reserved*/
210 * PCI Control Registers
213 /*0x0 - 0x1 reserved*/
214 #define VIA_CRDR_PCICLKGATT 0x2
217 * 0 : Soft reset all the controller and it will be de-asserted automatically
218 * 1 : Soft reset is de-asserted
220 #define VIA_CRDR_PCICLKGATT_SFTRST 0x01
222 * 3V3 : Pad power select
225 * NOTE : No mater what the actual value should be, this bit always
226 * read as 0. This is a hardware bug.
228 #define VIA_CRDR_PCICLKGATT_3V3 0x10
230 * PAD_PWRON : Pad Power on/off select
233 * NOTE : No mater what the actual value should be, this bit always
234 * read as 0. This is a hardware bug.
236 #define VIA_CRDR_PCICLKGATT_PAD_PWRON 0x20
238 #define VIA_CRDR_PCISDCCLK 0x5
240 #define VIA_CRDR_PCIDMACLK 0x7
241 #define VIA_CRDR_PCIDMACLK_SDC 0x2
243 #define VIA_CRDR_PCIINTCTRL 0x8
244 #define VIA_CRDR_PCIINTCTRL_SDCIRQEN 0x04
246 #define VIA_CRDR_PCIINTSTATUS 0x9
247 #define VIA_CRDR_PCIINTSTATUS_SDC 0x04
249 #define VIA_CRDR_PCITMOCTRL 0xa
250 #define VIA_CRDR_PCITMOCTRL_NO 0x0
251 #define VIA_CRDR_PCITMOCTRL_32US 0x1
252 #define VIA_CRDR_PCITMOCTRL_256US 0x2
253 #define VIA_CRDR_PCITMOCTRL_1024US 0x3
254 #define VIA_CRDR_PCITMOCTRL_256MS 0x4
255 #define VIA_CRDR_PCITMOCTRL_512MS 0x5
256 #define VIA_CRDR_PCITMOCTRL_1024MS 0x6
258 /*0xB-0xFF reserved*/
260 enum PCI_HOST_CLK_CONTROL
{
298 struct via_crdr_mmc_host
{
299 struct mmc_host
*mmc
;
300 struct mmc_request
*mrq
;
301 struct mmc_command
*cmd
;
302 struct mmc_data
*data
;
304 void __iomem
*mmiobase
;
305 void __iomem
*sdhc_mmiobase
;
306 void __iomem
*ddma_mmiobase
;
307 void __iomem
*pcictrl_mmiobase
;
309 struct pcictrlreg pm_pcictrl_reg
;
310 struct sdhcreg pm_sdhc_reg
;
312 struct work_struct carddet_work
;
313 struct tasklet_struct finish_tasklet
;
315 struct timer_list timer
;
322 /* some devices need a very long delay for power to stabilize */
323 #define VIA_CRDR_QUIRK_300MS_PWRDELAY 0x0001
325 static struct pci_device_id via_ids
[] = {
326 {PCI_VENDOR_ID_VIA
, PCI_DEVICE_ID_VIA_9530
,
327 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0,},
331 MODULE_DEVICE_TABLE(pci
, via_ids
);
333 static void via_print_sdchc(struct via_crdr_mmc_host
*host
)
335 void __iomem
*addrbase
= host
->sdhc_mmiobase
;
337 pr_debug("SDC MMIO Registers:\n");
338 pr_debug("SDCONTROL=%08x, SDCMDARG=%08x, SDBUSMODE=%08x\n",
339 readl(addrbase
+ VIA_CRDR_SDCTRL
),
340 readl(addrbase
+ VIA_CRDR_SDCARG
),
341 readl(addrbase
+ VIA_CRDR_SDBUSMODE
));
342 pr_debug("SDBLKLEN=%08x, SDCURBLKCNT=%08x, SDINTMASK=%08x\n",
343 readl(addrbase
+ VIA_CRDR_SDBLKLEN
),
344 readl(addrbase
+ VIA_CRDR_SDCURBLKCNT
),
345 readl(addrbase
+ VIA_CRDR_SDINTMASK
));
346 pr_debug("SDSTATUS=%08x, SDCLKSEL=%08x, SDEXTCTRL=%08x\n",
347 readl(addrbase
+ VIA_CRDR_SDSTATUS
),
348 readl(addrbase
+ VIA_CRDR_SDCLKSEL
),
349 readl(addrbase
+ VIA_CRDR_SDEXTCTRL
));
352 static void via_print_pcictrl(struct via_crdr_mmc_host
*host
)
354 void __iomem
*addrbase
= host
->pcictrl_mmiobase
;
356 pr_debug("PCI Control Registers:\n");
357 pr_debug("PCICLKGATT=%02x, PCISDCCLK=%02x, PCIDMACLK=%02x\n",
358 readb(addrbase
+ VIA_CRDR_PCICLKGATT
),
359 readb(addrbase
+ VIA_CRDR_PCISDCCLK
),
360 readb(addrbase
+ VIA_CRDR_PCIDMACLK
));
361 pr_debug("PCIINTCTRL=%02x, PCIINTSTATUS=%02x\n",
362 readb(addrbase
+ VIA_CRDR_PCIINTCTRL
),
363 readb(addrbase
+ VIA_CRDR_PCIINTSTATUS
));
366 static void via_save_pcictrlreg(struct via_crdr_mmc_host
*host
)
368 struct pcictrlreg
*pm_pcictrl_reg
;
369 void __iomem
*addrbase
;
371 pm_pcictrl_reg
= &(host
->pm_pcictrl_reg
);
372 addrbase
= host
->pcictrl_mmiobase
;
374 pm_pcictrl_reg
->pciclkgat_reg
= readb(addrbase
+ VIA_CRDR_PCICLKGATT
);
375 pm_pcictrl_reg
->pciclkgat_reg
|=
376 VIA_CRDR_PCICLKGATT_3V3
| VIA_CRDR_PCICLKGATT_PAD_PWRON
;
377 pm_pcictrl_reg
->pcisdclk_reg
= readb(addrbase
+ VIA_CRDR_PCISDCCLK
);
378 pm_pcictrl_reg
->pcidmaclk_reg
= readb(addrbase
+ VIA_CRDR_PCIDMACLK
);
379 pm_pcictrl_reg
->pciintctrl_reg
= readb(addrbase
+ VIA_CRDR_PCIINTCTRL
);
380 pm_pcictrl_reg
->pciintstatus_reg
=
381 readb(addrbase
+ VIA_CRDR_PCIINTSTATUS
);
382 pm_pcictrl_reg
->pcitmoctrl_reg
= readb(addrbase
+ VIA_CRDR_PCITMOCTRL
);
385 static void via_restore_pcictrlreg(struct via_crdr_mmc_host
*host
)
387 struct pcictrlreg
*pm_pcictrl_reg
;
388 void __iomem
*addrbase
;
390 pm_pcictrl_reg
= &(host
->pm_pcictrl_reg
);
391 addrbase
= host
->pcictrl_mmiobase
;
393 writeb(pm_pcictrl_reg
->pciclkgat_reg
, addrbase
+ VIA_CRDR_PCICLKGATT
);
394 writeb(pm_pcictrl_reg
->pcisdclk_reg
, addrbase
+ VIA_CRDR_PCISDCCLK
);
395 writeb(pm_pcictrl_reg
->pcidmaclk_reg
, addrbase
+ VIA_CRDR_PCIDMACLK
);
396 writeb(pm_pcictrl_reg
->pciintctrl_reg
, addrbase
+ VIA_CRDR_PCIINTCTRL
);
397 writeb(pm_pcictrl_reg
->pciintstatus_reg
,
398 addrbase
+ VIA_CRDR_PCIINTSTATUS
);
399 writeb(pm_pcictrl_reg
->pcitmoctrl_reg
, addrbase
+ VIA_CRDR_PCITMOCTRL
);
402 static void via_save_sdcreg(struct via_crdr_mmc_host
*host
)
404 struct sdhcreg
*pm_sdhc_reg
;
405 void __iomem
*addrbase
;
407 pm_sdhc_reg
= &(host
->pm_sdhc_reg
);
408 addrbase
= host
->sdhc_mmiobase
;
410 pm_sdhc_reg
->sdcontrol_reg
= readl(addrbase
+ VIA_CRDR_SDCTRL
);
411 pm_sdhc_reg
->sdcmdarg_reg
= readl(addrbase
+ VIA_CRDR_SDCARG
);
412 pm_sdhc_reg
->sdbusmode_reg
= readl(addrbase
+ VIA_CRDR_SDBUSMODE
);
413 pm_sdhc_reg
->sdblklen_reg
= readl(addrbase
+ VIA_CRDR_SDBLKLEN
);
414 pm_sdhc_reg
->sdcurblkcnt_reg
= readl(addrbase
+ VIA_CRDR_SDCURBLKCNT
);
415 pm_sdhc_reg
->sdintmask_reg
= readl(addrbase
+ VIA_CRDR_SDINTMASK
);
416 pm_sdhc_reg
->sdstatus_reg
= readl(addrbase
+ VIA_CRDR_SDSTATUS
);
417 pm_sdhc_reg
->sdrsptmo_reg
= readl(addrbase
+ VIA_CRDR_SDRSPTMO
);
418 pm_sdhc_reg
->sdclksel_reg
= readl(addrbase
+ VIA_CRDR_SDCLKSEL
);
419 pm_sdhc_reg
->sdextctrl_reg
= readl(addrbase
+ VIA_CRDR_SDEXTCTRL
);
422 static void via_restore_sdcreg(struct via_crdr_mmc_host
*host
)
424 struct sdhcreg
*pm_sdhc_reg
;
425 void __iomem
*addrbase
;
427 pm_sdhc_reg
= &(host
->pm_sdhc_reg
);
428 addrbase
= host
->sdhc_mmiobase
;
430 writel(pm_sdhc_reg
->sdcontrol_reg
, addrbase
+ VIA_CRDR_SDCTRL
);
431 writel(pm_sdhc_reg
->sdcmdarg_reg
, addrbase
+ VIA_CRDR_SDCARG
);
432 writel(pm_sdhc_reg
->sdbusmode_reg
, addrbase
+ VIA_CRDR_SDBUSMODE
);
433 writel(pm_sdhc_reg
->sdblklen_reg
, addrbase
+ VIA_CRDR_SDBLKLEN
);
434 writel(pm_sdhc_reg
->sdcurblkcnt_reg
, addrbase
+ VIA_CRDR_SDCURBLKCNT
);
435 writel(pm_sdhc_reg
->sdintmask_reg
, addrbase
+ VIA_CRDR_SDINTMASK
);
436 writel(pm_sdhc_reg
->sdstatus_reg
, addrbase
+ VIA_CRDR_SDSTATUS
);
437 writel(pm_sdhc_reg
->sdrsptmo_reg
, addrbase
+ VIA_CRDR_SDRSPTMO
);
438 writel(pm_sdhc_reg
->sdclksel_reg
, addrbase
+ VIA_CRDR_SDCLKSEL
);
439 writel(pm_sdhc_reg
->sdextctrl_reg
, addrbase
+ VIA_CRDR_SDEXTCTRL
);
442 static void via_pwron_sleep(struct via_crdr_mmc_host
*sdhost
)
444 if (sdhost
->quirks
& VIA_CRDR_QUIRK_300MS_PWRDELAY
)
450 static void via_set_ddma(struct via_crdr_mmc_host
*host
,
451 dma_addr_t dmaaddr
, u32 count
, int dir
, int enirq
)
453 void __iomem
*addrbase
;
457 ctrl_data
|= VIA_CRDR_DMACTRL_ENIRQ
;
460 ctrl_data
|= VIA_CRDR_DMACTRL_DIR
;
462 addrbase
= host
->ddma_mmiobase
;
464 writel(dmaaddr
, addrbase
+ VIA_CRDR_DMABASEADD
);
465 writel(count
, addrbase
+ VIA_CRDR_DMACOUNTER
);
466 writel(ctrl_data
, addrbase
+ VIA_CRDR_DMACTRL
);
467 writel(0x01, addrbase
+ VIA_CRDR_DMASTART
);
469 /* It seems that our DMA can not work normally with 375kHz clock */
470 /* FIXME: don't brute-force 8MHz but use PIO at 375kHz !! */
471 addrbase
= host
->pcictrl_mmiobase
;
472 if (readb(addrbase
+ VIA_CRDR_PCISDCCLK
) == PCI_CLK_375K
) {
473 dev_info(host
->mmc
->parent
, "forcing card speed to 8MHz\n");
474 writeb(PCI_CLK_8M
, addrbase
+ VIA_CRDR_PCISDCCLK
);
478 static void via_sdc_preparedata(struct via_crdr_mmc_host
*host
,
479 struct mmc_data
*data
)
481 void __iomem
*addrbase
;
488 BUG_ON(data
->blksz
> host
->mmc
->max_blk_size
);
489 BUG_ON(data
->blocks
> host
->mmc
->max_blk_count
);
493 count
= dma_map_sg(mmc_dev(host
->mmc
), data
->sg
, data
->sg_len
,
494 ((data
->flags
& MMC_DATA_READ
) ?
495 PCI_DMA_FROMDEVICE
: PCI_DMA_TODEVICE
));
498 via_set_ddma(host
, sg_dma_address(data
->sg
), sg_dma_len(data
->sg
),
499 (data
->flags
& MMC_DATA_WRITE
) ? 1 : 0, 1);
501 addrbase
= host
->sdhc_mmiobase
;
503 blk_reg
= data
->blksz
- 1;
504 blk_reg
|= VIA_CRDR_SDBLKLEN_GPIDET
| VIA_CRDR_SDBLKLEN_INTEN
;
505 blk_reg
|= (data
->blocks
) << 16;
507 writel(blk_reg
, addrbase
+ VIA_CRDR_SDBLKLEN
);
510 static void via_sdc_get_response(struct via_crdr_mmc_host
*host
,
511 struct mmc_command
*cmd
)
513 void __iomem
*addrbase
= host
->sdhc_mmiobase
;
514 u32 dwdata0
= readl(addrbase
+ VIA_CRDR_SDRESP0
);
515 u32 dwdata1
= readl(addrbase
+ VIA_CRDR_SDRESP1
);
516 u32 dwdata2
= readl(addrbase
+ VIA_CRDR_SDRESP2
);
517 u32 dwdata3
= readl(addrbase
+ VIA_CRDR_SDRESP3
);
519 if (cmd
->flags
& MMC_RSP_136
) {
520 cmd
->resp
[0] = ((u8
) (dwdata1
)) |
521 (((u8
) (dwdata0
>> 24)) << 8) |
522 (((u8
) (dwdata0
>> 16)) << 16) |
523 (((u8
) (dwdata0
>> 8)) << 24);
525 cmd
->resp
[1] = ((u8
) (dwdata2
)) |
526 (((u8
) (dwdata1
>> 24)) << 8) |
527 (((u8
) (dwdata1
>> 16)) << 16) |
528 (((u8
) (dwdata1
>> 8)) << 24);
530 cmd
->resp
[2] = ((u8
) (dwdata3
)) |
531 (((u8
) (dwdata2
>> 24)) << 8) |
532 (((u8
) (dwdata2
>> 16)) << 16) |
533 (((u8
) (dwdata2
>> 8)) << 24);
535 cmd
->resp
[3] = 0xff |
536 ((((u8
) (dwdata3
>> 24))) << 8) |
537 (((u8
) (dwdata3
>> 16)) << 16) |
538 (((u8
) (dwdata3
>> 8)) << 24);
541 cmd
->resp
[0] = ((dwdata0
& 0xff) << 24) |
542 (((dwdata0
>> 8) & 0xff) << 16) |
543 (((dwdata0
>> 16) & 0xff) << 8) | (dwdata1
& 0xff);
546 cmd
->resp
[1] = ((dwdata1
& 0xff) << 24) |
547 (((dwdata1
>> 8) & 0xff) << 16) |
548 (((dwdata1
>> 16) & 0xff) << 8);
552 static void via_sdc_send_command(struct via_crdr_mmc_host
*host
,
553 struct mmc_command
*cmd
)
555 void __iomem
*addrbase
;
556 struct mmc_data
*data
;
562 mod_timer(&host
->timer
, jiffies
+ HZ
);
566 cmdctrl
= cmd
->opcode
<< 8;
569 switch (mmc_resp_type(cmd
)) {
571 cmdctrl
|= VIA_CRDR_SDCTRL_RSP_NONE
;
574 cmdctrl
|= VIA_CRDR_SDCTRL_RSP_R1
;
577 cmdctrl
|= VIA_CRDR_SDCTRL_RSP_R1B
;
580 cmdctrl
|= VIA_CRDR_SDCTRL_RSP_R2
;
583 cmdctrl
|= VIA_CRDR_SDCTRL_RSP_R3
;
586 pr_err("%s: cmd->flag is not valid\n", mmc_hostname(host
->mmc
));
593 via_sdc_preparedata(host
, data
);
596 if (data
->blocks
> 1) {
597 if (data
->flags
& MMC_DATA_WRITE
) {
598 cmdctrl
|= VIA_CRDR_SDCTRL_WRITE
;
599 cmdctrl
|= VIA_CRDR_SDCTRL_MULTI_WR
;
601 cmdctrl
|= VIA_CRDR_SDCTRL_MULTI_RD
;
604 if (data
->flags
& MMC_DATA_WRITE
) {
605 cmdctrl
|= VIA_CRDR_SDCTRL_WRITE
;
606 cmdctrl
|= VIA_CRDR_SDCTRL_SINGLE_WR
;
608 cmdctrl
|= VIA_CRDR_SDCTRL_SINGLE_RD
;
613 if (cmd
== host
->mrq
->stop
)
614 cmdctrl
|= VIA_CRDR_SDCTRL_STOP
;
616 cmdctrl
|= VIA_CRDR_SDCTRL_START
;
618 addrbase
= host
->sdhc_mmiobase
;
619 writel(cmd
->arg
, addrbase
+ VIA_CRDR_SDCARG
);
620 writel(cmdctrl
, addrbase
+ VIA_CRDR_SDCTRL
);
623 static void via_sdc_finish_data(struct via_crdr_mmc_host
*host
)
625 struct mmc_data
*data
;
633 data
->bytes_xfered
= 0;
635 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
637 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
, data
->sg_len
,
638 ((data
->flags
& MMC_DATA_READ
) ?
639 PCI_DMA_FROMDEVICE
: PCI_DMA_TODEVICE
));
642 via_sdc_send_command(host
, data
->stop
);
644 tasklet_schedule(&host
->finish_tasklet
);
647 static void via_sdc_finish_command(struct via_crdr_mmc_host
*host
)
649 via_sdc_get_response(host
, host
->cmd
);
651 host
->cmd
->error
= 0;
653 if (!host
->cmd
->data
)
654 tasklet_schedule(&host
->finish_tasklet
);
659 static void via_sdc_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
661 void __iomem
*addrbase
;
662 struct via_crdr_mmc_host
*host
;
666 host
= mmc_priv(mmc
);
668 spin_lock_irqsave(&host
->lock
, flags
);
670 addrbase
= host
->pcictrl_mmiobase
;
671 writeb(VIA_CRDR_PCIDMACLK_SDC
, addrbase
+ VIA_CRDR_PCIDMACLK
);
673 status
= readw(host
->sdhc_mmiobase
+ VIA_CRDR_SDSTATUS
);
674 status
&= VIA_CRDR_SDSTS_W1C_MASK
;
675 writew(status
, host
->sdhc_mmiobase
+ VIA_CRDR_SDSTATUS
);
677 WARN_ON(host
->mrq
!= NULL
);
680 status
= readw(host
->sdhc_mmiobase
+ VIA_CRDR_SDSTATUS
);
681 if (!(status
& VIA_CRDR_SDSTS_SLOTG
) || host
->reject
) {
682 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
683 tasklet_schedule(&host
->finish_tasklet
);
685 via_sdc_send_command(host
, mrq
->cmd
);
689 spin_unlock_irqrestore(&host
->lock
, flags
);
692 static void via_sdc_set_power(struct via_crdr_mmc_host
*host
,
693 unsigned short power
, unsigned int on
)
698 spin_lock_irqsave(&host
->lock
, flags
);
700 host
->power
= (1 << power
);
702 gatt
= readb(host
->pcictrl_mmiobase
+ VIA_CRDR_PCICLKGATT
);
703 if (host
->power
== MMC_VDD_165_195
)
704 gatt
&= ~VIA_CRDR_PCICLKGATT_3V3
;
706 gatt
|= VIA_CRDR_PCICLKGATT_3V3
;
708 gatt
|= VIA_CRDR_PCICLKGATT_PAD_PWRON
;
710 gatt
&= ~VIA_CRDR_PCICLKGATT_PAD_PWRON
;
711 writeb(gatt
, host
->pcictrl_mmiobase
+ VIA_CRDR_PCICLKGATT
);
714 spin_unlock_irqrestore(&host
->lock
, flags
);
716 via_pwron_sleep(host
);
719 static void via_sdc_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
721 struct via_crdr_mmc_host
*host
;
723 void __iomem
*addrbase
;
724 u32 org_data
, sdextctrl
;
727 host
= mmc_priv(mmc
);
729 spin_lock_irqsave(&host
->lock
, flags
);
731 addrbase
= host
->sdhc_mmiobase
;
732 org_data
= readl(addrbase
+ VIA_CRDR_SDBUSMODE
);
733 sdextctrl
= readl(addrbase
+ VIA_CRDR_SDEXTCTRL
);
735 if (ios
->bus_width
== MMC_BUS_WIDTH_1
)
736 org_data
&= ~VIA_CRDR_SDMODE_4BIT
;
738 org_data
|= VIA_CRDR_SDMODE_4BIT
;
740 if (ios
->power_mode
== MMC_POWER_OFF
)
741 org_data
&= ~VIA_CRDR_SDMODE_CLK_ON
;
743 org_data
|= VIA_CRDR_SDMODE_CLK_ON
;
745 if (ios
->timing
== MMC_TIMING_SD_HS
)
746 sdextctrl
|= VIA_CRDR_SDEXTCTRL_HISPD
;
748 sdextctrl
&= ~VIA_CRDR_SDEXTCTRL_HISPD
;
750 writel(org_data
, addrbase
+ VIA_CRDR_SDBUSMODE
);
751 writel(sdextctrl
, addrbase
+ VIA_CRDR_SDEXTCTRL
);
753 if (ios
->clock
>= 48000000)
755 else if (ios
->clock
>= 33000000)
757 else if (ios
->clock
>= 24000000)
759 else if (ios
->clock
>= 16000000)
761 else if (ios
->clock
>= 12000000)
763 else if (ios
->clock
>= 8000000)
766 clock
= PCI_CLK_375K
;
768 addrbase
= host
->pcictrl_mmiobase
;
769 if (readb(addrbase
+ VIA_CRDR_PCISDCCLK
) != clock
)
770 writeb(clock
, addrbase
+ VIA_CRDR_PCISDCCLK
);
773 spin_unlock_irqrestore(&host
->lock
, flags
);
775 if (ios
->power_mode
!= MMC_POWER_OFF
)
776 via_sdc_set_power(host
, ios
->vdd
, 1);
778 via_sdc_set_power(host
, ios
->vdd
, 0);
781 static int via_sdc_get_ro(struct mmc_host
*mmc
)
783 struct via_crdr_mmc_host
*host
;
787 host
= mmc_priv(mmc
);
789 spin_lock_irqsave(&host
->lock
, flags
);
791 status
= readw(host
->sdhc_mmiobase
+ VIA_CRDR_SDSTATUS
);
793 spin_unlock_irqrestore(&host
->lock
, flags
);
795 return !(status
& VIA_CRDR_SDSTS_WP
);
798 static const struct mmc_host_ops via_sdc_ops
= {
799 .request
= via_sdc_request
,
800 .set_ios
= via_sdc_set_ios
,
801 .get_ro
= via_sdc_get_ro
,
804 static void via_reset_pcictrl(struct via_crdr_mmc_host
*host
)
809 spin_lock_irqsave(&host
->lock
, flags
);
811 via_save_pcictrlreg(host
);
812 via_save_sdcreg(host
);
814 spin_unlock_irqrestore(&host
->lock
, flags
);
816 gatt
= VIA_CRDR_PCICLKGATT_PAD_PWRON
;
817 if (host
->power
== MMC_VDD_165_195
)
818 gatt
&= VIA_CRDR_PCICLKGATT_3V3
;
820 gatt
|= VIA_CRDR_PCICLKGATT_3V3
;
821 writeb(gatt
, host
->pcictrl_mmiobase
+ VIA_CRDR_PCICLKGATT
);
822 via_pwron_sleep(host
);
823 gatt
|= VIA_CRDR_PCICLKGATT_SFTRST
;
824 writeb(gatt
, host
->pcictrl_mmiobase
+ VIA_CRDR_PCICLKGATT
);
827 spin_lock_irqsave(&host
->lock
, flags
);
829 via_restore_pcictrlreg(host
);
830 via_restore_sdcreg(host
);
833 spin_unlock_irqrestore(&host
->lock
, flags
);
836 static void via_sdc_cmd_isr(struct via_crdr_mmc_host
*host
, u16 intmask
)
838 BUG_ON(intmask
== 0);
841 pr_err("%s: Got command interrupt 0x%x even "
842 "though no command operation was in progress.\n",
843 mmc_hostname(host
->mmc
), intmask
);
847 if (intmask
& VIA_CRDR_SDSTS_CRTO
)
848 host
->cmd
->error
= -ETIMEDOUT
;
849 else if (intmask
& VIA_CRDR_SDSTS_SC
)
850 host
->cmd
->error
= -EILSEQ
;
852 if (host
->cmd
->error
)
853 tasklet_schedule(&host
->finish_tasklet
);
854 else if (intmask
& VIA_CRDR_SDSTS_CRD
)
855 via_sdc_finish_command(host
);
858 static void via_sdc_data_isr(struct via_crdr_mmc_host
*host
, u16 intmask
)
860 BUG_ON(intmask
== 0);
862 if (intmask
& VIA_CRDR_SDSTS_DT
)
863 host
->data
->error
= -ETIMEDOUT
;
864 else if (intmask
& (VIA_CRDR_SDSTS_RC
| VIA_CRDR_SDSTS_WC
))
865 host
->data
->error
= -EILSEQ
;
867 via_sdc_finish_data(host
);
870 static irqreturn_t
via_sdc_isr(int irq
, void *dev_id
)
872 struct via_crdr_mmc_host
*sdhost
= dev_id
;
873 void __iomem
*addrbase
;
881 spin_lock(&sdhost
->lock
);
883 addrbase
= sdhost
->pcictrl_mmiobase
;
884 pci_status
= readb(addrbase
+ VIA_CRDR_PCIINTSTATUS
);
885 if (!(pci_status
& VIA_CRDR_PCIINTSTATUS_SDC
)) {
890 addrbase
= sdhost
->sdhc_mmiobase
;
891 sd_status
= readw(addrbase
+ VIA_CRDR_SDSTATUS
);
892 sd_status
&= VIA_CRDR_SDSTS_INT_MASK
;
893 sd_status
&= ~VIA_CRDR_SDSTS_IGN_MASK
;
899 if (sd_status
& VIA_CRDR_SDSTS_CIR
) {
900 writew(sd_status
& VIA_CRDR_SDSTS_CIR
,
901 addrbase
+ VIA_CRDR_SDSTATUS
);
903 schedule_work(&sdhost
->carddet_work
);
906 sd_status
&= ~VIA_CRDR_SDSTS_CIR
;
907 if (sd_status
& VIA_CRDR_SDSTS_CMD_MASK
) {
908 writew(sd_status
& VIA_CRDR_SDSTS_CMD_MASK
,
909 addrbase
+ VIA_CRDR_SDSTATUS
);
910 via_sdc_cmd_isr(sdhost
, sd_status
& VIA_CRDR_SDSTS_CMD_MASK
);
912 if (sd_status
& VIA_CRDR_SDSTS_DATA_MASK
) {
913 writew(sd_status
& VIA_CRDR_SDSTS_DATA_MASK
,
914 addrbase
+ VIA_CRDR_SDSTATUS
);
915 via_sdc_data_isr(sdhost
, sd_status
& VIA_CRDR_SDSTS_DATA_MASK
);
918 sd_status
&= ~(VIA_CRDR_SDSTS_CMD_MASK
| VIA_CRDR_SDSTS_DATA_MASK
);
920 pr_err("%s: Unexpected interrupt 0x%x\n",
921 mmc_hostname(sdhost
->mmc
), sd_status
);
922 writew(sd_status
, addrbase
+ VIA_CRDR_SDSTATUS
);
925 result
= IRQ_HANDLED
;
929 spin_unlock(&sdhost
->lock
);
934 static void via_sdc_timeout(unsigned long ulongdata
)
936 struct via_crdr_mmc_host
*sdhost
;
939 sdhost
= (struct via_crdr_mmc_host
*)ulongdata
;
941 spin_lock_irqsave(&sdhost
->lock
, flags
);
944 pr_err("%s: Timeout waiting for hardware interrupt."
945 "cmd:0x%x\n", mmc_hostname(sdhost
->mmc
),
946 sdhost
->mrq
->cmd
->opcode
);
949 writel(VIA_CRDR_DMACTRL_SFTRST
,
950 sdhost
->ddma_mmiobase
+ VIA_CRDR_DMACTRL
);
951 sdhost
->data
->error
= -ETIMEDOUT
;
952 via_sdc_finish_data(sdhost
);
955 sdhost
->cmd
->error
= -ETIMEDOUT
;
957 sdhost
->mrq
->cmd
->error
= -ETIMEDOUT
;
958 tasklet_schedule(&sdhost
->finish_tasklet
);
963 spin_unlock_irqrestore(&sdhost
->lock
, flags
);
966 static void via_sdc_tasklet_finish(unsigned long param
)
968 struct via_crdr_mmc_host
*host
;
970 struct mmc_request
*mrq
;
972 host
= (struct via_crdr_mmc_host
*)param
;
974 spin_lock_irqsave(&host
->lock
, flags
);
976 del_timer(&host
->timer
);
982 spin_unlock_irqrestore(&host
->lock
, flags
);
984 mmc_request_done(host
->mmc
, mrq
);
987 static void via_sdc_card_detect(struct work_struct
*work
)
989 struct via_crdr_mmc_host
*host
;
990 void __iomem
*addrbase
;
994 host
= container_of(work
, struct via_crdr_mmc_host
, carddet_work
);
996 addrbase
= host
->ddma_mmiobase
;
997 writel(VIA_CRDR_DMACTRL_SFTRST
, addrbase
+ VIA_CRDR_DMACTRL
);
999 spin_lock_irqsave(&host
->lock
, flags
);
1001 addrbase
= host
->pcictrl_mmiobase
;
1002 writeb(VIA_CRDR_PCIDMACLK_SDC
, addrbase
+ VIA_CRDR_PCIDMACLK
);
1004 addrbase
= host
->sdhc_mmiobase
;
1005 status
= readw(addrbase
+ VIA_CRDR_SDSTATUS
);
1006 if (!(status
& VIA_CRDR_SDSTS_SLOTG
)) {
1008 pr_err("%s: Card removed during transfer!\n",
1009 mmc_hostname(host
->mmc
));
1010 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1011 tasklet_schedule(&host
->finish_tasklet
);
1015 spin_unlock_irqrestore(&host
->lock
, flags
);
1017 via_reset_pcictrl(host
);
1019 spin_lock_irqsave(&host
->lock
, flags
);
1023 spin_unlock_irqrestore(&host
->lock
, flags
);
1025 via_print_pcictrl(host
);
1026 via_print_sdchc(host
);
1028 mmc_detect_change(host
->mmc
, msecs_to_jiffies(500));
1031 static void via_init_mmc_host(struct via_crdr_mmc_host
*host
)
1033 struct mmc_host
*mmc
= host
->mmc
;
1034 void __iomem
*addrbase
;
1038 init_timer(&host
->timer
);
1039 host
->timer
.data
= (unsigned long)host
;
1040 host
->timer
.function
= via_sdc_timeout
;
1042 spin_lock_init(&host
->lock
);
1044 mmc
->f_min
= VIA_CRDR_MIN_CLOCK
;
1045 mmc
->f_max
= VIA_CRDR_MAX_CLOCK
;
1046 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
| MMC_VDD_165_195
;
1047 mmc
->caps
= MMC_CAP_4_BIT_DATA
| MMC_CAP_SD_HIGHSPEED
;
1048 mmc
->ops
= &via_sdc_ops
;
1050 /*Hardware cannot do scatter lists*/
1053 mmc
->max_blk_size
= VIA_CRDR_MAX_BLOCK_LENGTH
;
1054 mmc
->max_blk_count
= VIA_CRDR_MAX_BLOCK_COUNT
;
1056 mmc
->max_seg_size
= mmc
->max_blk_size
* mmc
->max_blk_count
;
1057 mmc
->max_req_size
= mmc
->max_seg_size
;
1059 INIT_WORK(&host
->carddet_work
, via_sdc_card_detect
);
1061 tasklet_init(&host
->finish_tasklet
, via_sdc_tasklet_finish
,
1062 (unsigned long)host
);
1064 addrbase
= host
->sdhc_mmiobase
;
1065 writel(0x0, addrbase
+ VIA_CRDR_SDINTMASK
);
1068 lenreg
= VIA_CRDR_SDBLKLEN_GPIDET
| VIA_CRDR_SDBLKLEN_INTEN
;
1069 writel(lenreg
, addrbase
+ VIA_CRDR_SDBLKLEN
);
1071 status
= readw(addrbase
+ VIA_CRDR_SDSTATUS
);
1072 status
&= VIA_CRDR_SDSTS_W1C_MASK
;
1073 writew(status
, addrbase
+ VIA_CRDR_SDSTATUS
);
1075 status
= readw(addrbase
+ VIA_CRDR_SDSTATUS2
);
1076 status
|= VIA_CRDR_SDSTS_CFE
;
1077 writew(status
, addrbase
+ VIA_CRDR_SDSTATUS2
);
1079 writeb(0x0, addrbase
+ VIA_CRDR_SDEXTCTRL
);
1081 writel(VIA_CRDR_SDACTIVE_INTMASK
, addrbase
+ VIA_CRDR_SDINTMASK
);
1085 static int __devinit
via_sd_probe(struct pci_dev
*pcidev
,
1086 const struct pci_device_id
*id
)
1088 struct mmc_host
*mmc
;
1089 struct via_crdr_mmc_host
*sdhost
;
1095 ": VIA SDMMC controller found at %s [%04x:%04x] (rev %x)\n",
1096 pci_name(pcidev
), (int)pcidev
->vendor
, (int)pcidev
->device
,
1097 (int)pcidev
->revision
);
1099 ret
= pci_enable_device(pcidev
);
1103 ret
= pci_request_regions(pcidev
, DRV_NAME
);
1107 pci_write_config_byte(pcidev
, VIA_CRDR_PCI_WORK_MODE
, 0);
1108 pci_write_config_byte(pcidev
, VIA_CRDR_PCI_DBG_MODE
, 0);
1110 mmc
= mmc_alloc_host(sizeof(struct via_crdr_mmc_host
), &pcidev
->dev
);
1116 sdhost
= mmc_priv(mmc
);
1118 dev_set_drvdata(&pcidev
->dev
, sdhost
);
1120 len
= pci_resource_len(pcidev
, 0);
1121 base
= pci_resource_start(pcidev
, 0);
1122 sdhost
->mmiobase
= ioremap_nocache(base
, len
);
1123 if (!sdhost
->mmiobase
) {
1128 sdhost
->sdhc_mmiobase
=
1129 sdhost
->mmiobase
+ VIA_CRDR_SDC_OFF
;
1130 sdhost
->ddma_mmiobase
=
1131 sdhost
->mmiobase
+ VIA_CRDR_DDMA_OFF
;
1132 sdhost
->pcictrl_mmiobase
=
1133 sdhost
->mmiobase
+ VIA_CRDR_PCICTRL_OFF
;
1135 sdhost
->power
= MMC_VDD_165_195
;
1137 gatt
= VIA_CRDR_PCICLKGATT_3V3
| VIA_CRDR_PCICLKGATT_PAD_PWRON
;
1138 writeb(gatt
, sdhost
->pcictrl_mmiobase
+ VIA_CRDR_PCICLKGATT
);
1139 via_pwron_sleep(sdhost
);
1140 gatt
|= VIA_CRDR_PCICLKGATT_SFTRST
;
1141 writeb(gatt
, sdhost
->pcictrl_mmiobase
+ VIA_CRDR_PCICLKGATT
);
1144 via_init_mmc_host(sdhost
);
1147 request_irq(pcidev
->irq
, via_sdc_isr
, IRQF_SHARED
, DRV_NAME
,
1152 writeb(VIA_CRDR_PCIINTCTRL_SDCIRQEN
,
1153 sdhost
->pcictrl_mmiobase
+ VIA_CRDR_PCIINTCTRL
);
1154 writeb(VIA_CRDR_PCITMOCTRL_1024MS
,
1155 sdhost
->pcictrl_mmiobase
+ VIA_CRDR_PCITMOCTRL
);
1157 /* device-specific quirks */
1158 if (pcidev
->subsystem_vendor
== PCI_VENDOR_ID_LENOVO
&&
1159 pcidev
->subsystem_device
== 0x3891)
1160 sdhost
->quirks
= VIA_CRDR_QUIRK_300MS_PWRDELAY
;
1167 iounmap(sdhost
->mmiobase
);
1169 dev_set_drvdata(&pcidev
->dev
, NULL
);
1172 pci_release_regions(pcidev
);
1174 pci_disable_device(pcidev
);
1179 static void __devexit
via_sd_remove(struct pci_dev
*pcidev
)
1181 struct via_crdr_mmc_host
*sdhost
= pci_get_drvdata(pcidev
);
1182 unsigned long flags
;
1185 spin_lock_irqsave(&sdhost
->lock
, flags
);
1187 /* Ensure we don't accept more commands from mmc layer */
1190 /* Disable generating further interrupts */
1191 writeb(0x0, sdhost
->pcictrl_mmiobase
+ VIA_CRDR_PCIINTCTRL
);
1195 printk(KERN_ERR
"%s: Controller removed during "
1196 "transfer\n", mmc_hostname(sdhost
->mmc
));
1198 /* make sure all DMA is stopped */
1199 writel(VIA_CRDR_DMACTRL_SFTRST
,
1200 sdhost
->ddma_mmiobase
+ VIA_CRDR_DMACTRL
);
1202 sdhost
->mrq
->cmd
->error
= -ENOMEDIUM
;
1203 if (sdhost
->mrq
->stop
)
1204 sdhost
->mrq
->stop
->error
= -ENOMEDIUM
;
1205 tasklet_schedule(&sdhost
->finish_tasklet
);
1207 spin_unlock_irqrestore(&sdhost
->lock
, flags
);
1209 mmc_remove_host(sdhost
->mmc
);
1211 free_irq(pcidev
->irq
, sdhost
);
1213 del_timer_sync(&sdhost
->timer
);
1215 tasklet_kill(&sdhost
->finish_tasklet
);
1217 /* switch off power */
1218 gatt
= readb(sdhost
->pcictrl_mmiobase
+ VIA_CRDR_PCICLKGATT
);
1219 gatt
&= ~VIA_CRDR_PCICLKGATT_PAD_PWRON
;
1220 writeb(gatt
, sdhost
->pcictrl_mmiobase
+ VIA_CRDR_PCICLKGATT
);
1222 iounmap(sdhost
->mmiobase
);
1223 dev_set_drvdata(&pcidev
->dev
, NULL
);
1224 mmc_free_host(sdhost
->mmc
);
1225 pci_release_regions(pcidev
);
1226 pci_disable_device(pcidev
);
1229 ": VIA SDMMC controller at %s [%04x:%04x] has been removed\n",
1230 pci_name(pcidev
), (int)pcidev
->vendor
, (int)pcidev
->device
);
1235 static void via_init_sdc_pm(struct via_crdr_mmc_host
*host
)
1237 struct sdhcreg
*pm_sdhcreg
;
1238 void __iomem
*addrbase
;
1242 pm_sdhcreg
= &(host
->pm_sdhc_reg
);
1243 addrbase
= host
->sdhc_mmiobase
;
1245 writel(0x0, addrbase
+ VIA_CRDR_SDINTMASK
);
1247 lenreg
= VIA_CRDR_SDBLKLEN_GPIDET
| VIA_CRDR_SDBLKLEN_INTEN
;
1248 writel(lenreg
, addrbase
+ VIA_CRDR_SDBLKLEN
);
1250 status
= readw(addrbase
+ VIA_CRDR_SDSTATUS
);
1251 status
&= VIA_CRDR_SDSTS_W1C_MASK
;
1252 writew(status
, addrbase
+ VIA_CRDR_SDSTATUS
);
1254 status
= readw(addrbase
+ VIA_CRDR_SDSTATUS2
);
1255 status
|= VIA_CRDR_SDSTS_CFE
;
1256 writew(status
, addrbase
+ VIA_CRDR_SDSTATUS2
);
1258 writel(pm_sdhcreg
->sdcontrol_reg
, addrbase
+ VIA_CRDR_SDCTRL
);
1259 writel(pm_sdhcreg
->sdcmdarg_reg
, addrbase
+ VIA_CRDR_SDCARG
);
1260 writel(pm_sdhcreg
->sdintmask_reg
, addrbase
+ VIA_CRDR_SDINTMASK
);
1261 writel(pm_sdhcreg
->sdrsptmo_reg
, addrbase
+ VIA_CRDR_SDRSPTMO
);
1262 writel(pm_sdhcreg
->sdclksel_reg
, addrbase
+ VIA_CRDR_SDCLKSEL
);
1263 writel(pm_sdhcreg
->sdextctrl_reg
, addrbase
+ VIA_CRDR_SDEXTCTRL
);
1265 via_print_pcictrl(host
);
1266 via_print_sdchc(host
);
1269 static int via_sd_suspend(struct pci_dev
*pcidev
, pm_message_t state
)
1271 struct via_crdr_mmc_host
*host
;
1274 host
= pci_get_drvdata(pcidev
);
1276 via_save_pcictrlreg(host
);
1277 via_save_sdcreg(host
);
1279 ret
= mmc_suspend_host(host
->mmc
);
1281 pci_save_state(pcidev
);
1282 pci_enable_wake(pcidev
, pci_choose_state(pcidev
, state
), 0);
1283 pci_disable_device(pcidev
);
1284 pci_set_power_state(pcidev
, pci_choose_state(pcidev
, state
));
1289 static int via_sd_resume(struct pci_dev
*pcidev
)
1291 struct via_crdr_mmc_host
*sdhost
;
1295 sdhost
= pci_get_drvdata(pcidev
);
1297 gatt
= VIA_CRDR_PCICLKGATT_PAD_PWRON
;
1298 if (sdhost
->power
== MMC_VDD_165_195
)
1299 gatt
&= ~VIA_CRDR_PCICLKGATT_3V3
;
1301 gatt
|= VIA_CRDR_PCICLKGATT_3V3
;
1302 writeb(gatt
, sdhost
->pcictrl_mmiobase
+ VIA_CRDR_PCICLKGATT
);
1303 via_pwron_sleep(sdhost
);
1304 gatt
|= VIA_CRDR_PCICLKGATT_SFTRST
;
1305 writeb(gatt
, sdhost
->pcictrl_mmiobase
+ VIA_CRDR_PCICLKGATT
);
1310 pci_set_power_state(pcidev
, PCI_D0
);
1311 pci_restore_state(pcidev
);
1312 ret
= pci_enable_device(pcidev
);
1316 via_restore_pcictrlreg(sdhost
);
1317 via_init_sdc_pm(sdhost
);
1319 ret
= mmc_resume_host(sdhost
->mmc
);
1324 #else /* CONFIG_PM */
1326 #define via_sd_suspend NULL
1327 #define via_sd_resume NULL
1329 #endif /* CONFIG_PM */
1331 static struct pci_driver via_sd_driver
= {
1333 .id_table
= via_ids
,
1334 .probe
= via_sd_probe
,
1335 .remove
= __devexit_p(via_sd_remove
),
1336 .suspend
= via_sd_suspend
,
1337 .resume
= via_sd_resume
,
1340 static int __init
via_sd_drv_init(void)
1342 pr_info(DRV_NAME
": VIA SD/MMC Card Reader driver "
1343 "(C) 2008 VIA Technologies, Inc.\n");
1345 return pci_register_driver(&via_sd_driver
);
1348 static void __exit
via_sd_drv_exit(void)
1350 pci_unregister_driver(&via_sd_driver
);
1353 module_init(via_sd_drv_init
);
1354 module_exit(via_sd_drv_exit
);
1356 MODULE_LICENSE("GPL");
1357 MODULE_AUTHOR("VIA Technologies Inc.");
1358 MODULE_DESCRIPTION("VIA SD/MMC Card Interface driver");