12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f0bf7f61SHarald Welte /*
3f0bf7f61SHarald Welte * drivers/mmc/host/via-sdmmc.c - VIA SD/MMC Card Reader driver
4f0bf7f61SHarald Welte * Copyright (c) 2008, VIA Technologies Inc. All Rights Reserved.
5f0bf7f61SHarald Welte */
6f0bf7f61SHarald Welte
7f0bf7f61SHarald Welte #include <linux/pci.h>
888b47679SPaul Gortmaker #include <linux/module.h>
9f0bf7f61SHarald Welte #include <linux/dma-mapping.h>
10f0bf7f61SHarald Welte #include <linux/highmem.h>
11f0bf7f61SHarald Welte #include <linux/delay.h>
12b8789ec4SUlf Hansson #include <linux/interrupt.h>
13f0bf7f61SHarald Welte
14f0bf7f61SHarald Welte #include <linux/mmc/host.h>
15f0bf7f61SHarald Welte
16f0bf7f61SHarald Welte #define DRV_NAME "via_sdmmc"
17f0bf7f61SHarald Welte
18f0bf7f61SHarald Welte #define PCI_DEVICE_ID_VIA_9530 0x9530
19f0bf7f61SHarald Welte
20f0bf7f61SHarald Welte #define VIA_CRDR_SDC_OFF 0x200
21f0bf7f61SHarald Welte #define VIA_CRDR_DDMA_OFF 0x400
22f0bf7f61SHarald Welte #define VIA_CRDR_PCICTRL_OFF 0x600
23f0bf7f61SHarald Welte
24f0bf7f61SHarald Welte #define VIA_CRDR_MIN_CLOCK 375000
25f0bf7f61SHarald Welte #define VIA_CRDR_MAX_CLOCK 48000000
26f0bf7f61SHarald Welte
27f0bf7f61SHarald Welte /*
28f0bf7f61SHarald Welte * PCI registers
29f0bf7f61SHarald Welte */
30f0bf7f61SHarald Welte
31f0bf7f61SHarald Welte #define VIA_CRDR_PCI_WORK_MODE 0x40
32f0bf7f61SHarald Welte #define VIA_CRDR_PCI_DBG_MODE 0x41
33f0bf7f61SHarald Welte
34f0bf7f61SHarald Welte /*
35f0bf7f61SHarald Welte * SDC MMIO Registers
36f0bf7f61SHarald Welte */
37f0bf7f61SHarald Welte
38f0bf7f61SHarald Welte #define VIA_CRDR_SDCTRL 0x0
39f0bf7f61SHarald Welte #define VIA_CRDR_SDCTRL_START 0x01
40f0bf7f61SHarald Welte #define VIA_CRDR_SDCTRL_WRITE 0x04
41f0bf7f61SHarald Welte #define VIA_CRDR_SDCTRL_SINGLE_WR 0x10
42f0bf7f61SHarald Welte #define VIA_CRDR_SDCTRL_SINGLE_RD 0x20
43f0bf7f61SHarald Welte #define VIA_CRDR_SDCTRL_MULTI_WR 0x30
44f0bf7f61SHarald Welte #define VIA_CRDR_SDCTRL_MULTI_RD 0x40
45f0bf7f61SHarald Welte #define VIA_CRDR_SDCTRL_STOP 0x70
46f0bf7f61SHarald Welte
47f0bf7f61SHarald Welte #define VIA_CRDR_SDCTRL_RSP_NONE 0x0
48f0bf7f61SHarald Welte #define VIA_CRDR_SDCTRL_RSP_R1 0x10000
49f0bf7f61SHarald Welte #define VIA_CRDR_SDCTRL_RSP_R2 0x20000
50f0bf7f61SHarald Welte #define VIA_CRDR_SDCTRL_RSP_R3 0x30000
51f0bf7f61SHarald Welte #define VIA_CRDR_SDCTRL_RSP_R1B 0x90000
52f0bf7f61SHarald Welte
53f0bf7f61SHarald Welte #define VIA_CRDR_SDCARG 0x4
54f0bf7f61SHarald Welte
55f0bf7f61SHarald Welte #define VIA_CRDR_SDBUSMODE 0x8
56f0bf7f61SHarald Welte #define VIA_CRDR_SDMODE_4BIT 0x02
57f0bf7f61SHarald Welte #define VIA_CRDR_SDMODE_CLK_ON 0x40
58f0bf7f61SHarald Welte
59f0bf7f61SHarald Welte #define VIA_CRDR_SDBLKLEN 0xc
60f0bf7f61SHarald Welte /*
61f0bf7f61SHarald Welte * Bit 0 -Bit 10 : Block length. So, the maximum block length should be 2048.
62f0bf7f61SHarald Welte * Bit 11 - Bit 13 : Reserved.
63f0bf7f61SHarald Welte * GPIDET : Select GPI pin to detect card, GPI means CR_CD# in top design.
64f0bf7f61SHarald Welte * INTEN : Enable SD host interrupt.
65f0bf7f61SHarald Welte * Bit 16 - Bit 31 : Block count. So, the maximun block count should be 65536.
66f0bf7f61SHarald Welte */
67f0bf7f61SHarald Welte #define VIA_CRDR_SDBLKLEN_GPIDET 0x2000
68f0bf7f61SHarald Welte #define VIA_CRDR_SDBLKLEN_INTEN 0x8000
69f0bf7f61SHarald Welte #define VIA_CRDR_MAX_BLOCK_COUNT 65536
70f0bf7f61SHarald Welte #define VIA_CRDR_MAX_BLOCK_LENGTH 2048
71f0bf7f61SHarald Welte
72f0bf7f61SHarald Welte #define VIA_CRDR_SDRESP0 0x10
73f0bf7f61SHarald Welte #define VIA_CRDR_SDRESP1 0x14
74f0bf7f61SHarald Welte #define VIA_CRDR_SDRESP2 0x18
75f0bf7f61SHarald Welte #define VIA_CRDR_SDRESP3 0x1c
76f0bf7f61SHarald Welte
77f0bf7f61SHarald Welte #define VIA_CRDR_SDCURBLKCNT 0x20
78f0bf7f61SHarald Welte
79f0bf7f61SHarald Welte #define VIA_CRDR_SDINTMASK 0x24
80f0bf7f61SHarald Welte /*
81f0bf7f61SHarald Welte * MBDIE : Multiple Blocks transfer Done Interrupt Enable
82f0bf7f61SHarald Welte * BDDIE : Block Data transfer Done Interrupt Enable
83f0bf7f61SHarald Welte * CIRIE : Card Insertion or Removal Interrupt Enable
84f0bf7f61SHarald Welte * CRDIE : Command-Response transfer Done Interrupt Enable
85f0bf7f61SHarald Welte * CRTOIE : Command-Response response TimeOut Interrupt Enable
86f0bf7f61SHarald Welte * ASCRDIE : Auto Stop Command-Response transfer Done Interrupt Enable
87f0bf7f61SHarald Welte * DTIE : Data access Timeout Interrupt Enable
88f0bf7f61SHarald Welte * SCIE : reSponse CRC error Interrupt Enable
89f0bf7f61SHarald Welte * RCIE : Read data CRC error Interrupt Enable
90f0bf7f61SHarald Welte * WCIE : Write data CRC error Interrupt Enable
91f0bf7f61SHarald Welte */
92f0bf7f61SHarald Welte #define VIA_CRDR_SDINTMASK_MBDIE 0x10
93f0bf7f61SHarald Welte #define VIA_CRDR_SDINTMASK_BDDIE 0x20
94f0bf7f61SHarald Welte #define VIA_CRDR_SDINTMASK_CIRIE 0x80
95f0bf7f61SHarald Welte #define VIA_CRDR_SDINTMASK_CRDIE 0x200
96f0bf7f61SHarald Welte #define VIA_CRDR_SDINTMASK_CRTOIE 0x400
97f0bf7f61SHarald Welte #define VIA_CRDR_SDINTMASK_ASCRDIE 0x800
98f0bf7f61SHarald Welte #define VIA_CRDR_SDINTMASK_DTIE 0x1000
99f0bf7f61SHarald Welte #define VIA_CRDR_SDINTMASK_SCIE 0x2000
100f0bf7f61SHarald Welte #define VIA_CRDR_SDINTMASK_RCIE 0x4000
101f0bf7f61SHarald Welte #define VIA_CRDR_SDINTMASK_WCIE 0x8000
102f0bf7f61SHarald Welte
103f0bf7f61SHarald Welte #define VIA_CRDR_SDACTIVE_INTMASK \
104f0bf7f61SHarald Welte (VIA_CRDR_SDINTMASK_MBDIE | VIA_CRDR_SDINTMASK_CIRIE \
105f0bf7f61SHarald Welte | VIA_CRDR_SDINTMASK_CRDIE | VIA_CRDR_SDINTMASK_CRTOIE \
106f0bf7f61SHarald Welte | VIA_CRDR_SDINTMASK_DTIE | VIA_CRDR_SDINTMASK_SCIE \
107f0bf7f61SHarald Welte | VIA_CRDR_SDINTMASK_RCIE | VIA_CRDR_SDINTMASK_WCIE)
108f0bf7f61SHarald Welte
109f0bf7f61SHarald Welte #define VIA_CRDR_SDSTATUS 0x28
110f0bf7f61SHarald Welte /*
111f0bf7f61SHarald Welte * CECC : Reserved
112f0bf7f61SHarald Welte * WP : SD card Write Protect status
113f0bf7f61SHarald Welte * SLOTD : Reserved
114f0bf7f61SHarald Welte * SLOTG : SD SLOT status(Gpi pin status)
115f0bf7f61SHarald Welte * MBD : Multiple Blocks transfer Done interrupt status
116f0bf7f61SHarald Welte * BDD : Block Data transfer Done interrupt status
117f0bf7f61SHarald Welte * CD : Reserved
118f0bf7f61SHarald Welte * CIR : Card Insertion or Removal interrupt detected on GPI pin
119f0bf7f61SHarald Welte * IO : Reserved
120f0bf7f61SHarald Welte * CRD : Command-Response transfer Done interrupt status
121f0bf7f61SHarald Welte * CRTO : Command-Response response TimeOut interrupt status
122f0bf7f61SHarald Welte * ASCRDIE : Auto Stop Command-Response transfer Done interrupt status
123f0bf7f61SHarald Welte * DT : Data access Timeout interrupt status
124f0bf7f61SHarald Welte * SC : reSponse CRC error interrupt status
125f0bf7f61SHarald Welte * RC : Read data CRC error interrupt status
126f0bf7f61SHarald Welte * WC : Write data CRC error interrupt status
127f0bf7f61SHarald Welte */
128f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_CECC 0x01
129f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_WP 0x02
130f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_SLOTD 0x04
131f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_SLOTG 0x08
132f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_MBD 0x10
133f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_BDD 0x20
134f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_CD 0x40
135f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_CIR 0x80
136f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_IO 0x100
137f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_CRD 0x200
138f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_CRTO 0x400
139f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_ASCRDIE 0x800
140f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_DT 0x1000
141f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_SC 0x2000
142f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_RC 0x4000
143f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_WC 0x8000
144f0bf7f61SHarald Welte
145f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_IGN_MASK\
146f0bf7f61SHarald Welte (VIA_CRDR_SDSTS_BDD | VIA_CRDR_SDSTS_ASCRDIE | VIA_CRDR_SDSTS_IO)
147f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_INT_MASK \
148f0bf7f61SHarald Welte (VIA_CRDR_SDSTS_MBD | VIA_CRDR_SDSTS_BDD | VIA_CRDR_SDSTS_CD \
149f0bf7f61SHarald Welte | VIA_CRDR_SDSTS_CIR | VIA_CRDR_SDSTS_IO | VIA_CRDR_SDSTS_CRD \
150f0bf7f61SHarald Welte | VIA_CRDR_SDSTS_CRTO | VIA_CRDR_SDSTS_ASCRDIE | VIA_CRDR_SDSTS_DT \
151f0bf7f61SHarald Welte | VIA_CRDR_SDSTS_SC | VIA_CRDR_SDSTS_RC | VIA_CRDR_SDSTS_WC)
152f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_W1C_MASK \
153f0bf7f61SHarald Welte (VIA_CRDR_SDSTS_CECC | VIA_CRDR_SDSTS_MBD | VIA_CRDR_SDSTS_BDD \
154f0bf7f61SHarald Welte | VIA_CRDR_SDSTS_CD | VIA_CRDR_SDSTS_CIR | VIA_CRDR_SDSTS_CRD \
155f0bf7f61SHarald Welte | VIA_CRDR_SDSTS_CRTO | VIA_CRDR_SDSTS_ASCRDIE | VIA_CRDR_SDSTS_DT \
156f0bf7f61SHarald Welte | VIA_CRDR_SDSTS_SC | VIA_CRDR_SDSTS_RC | VIA_CRDR_SDSTS_WC)
157f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_CMD_MASK \
158f0bf7f61SHarald Welte (VIA_CRDR_SDSTS_CRD | VIA_CRDR_SDSTS_CRTO | VIA_CRDR_SDSTS_SC)
159f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_DATA_MASK\
160f0bf7f61SHarald Welte (VIA_CRDR_SDSTS_MBD | VIA_CRDR_SDSTS_DT \
161f0bf7f61SHarald Welte | VIA_CRDR_SDSTS_RC | VIA_CRDR_SDSTS_WC)
162f0bf7f61SHarald Welte
163f0bf7f61SHarald Welte #define VIA_CRDR_SDSTATUS2 0x2a
164f0bf7f61SHarald Welte /*
165f0bf7f61SHarald Welte * CFE : Enable SD host automatic Clock FReezing
166f0bf7f61SHarald Welte */
167f0bf7f61SHarald Welte #define VIA_CRDR_SDSTS_CFE 0x80
168f0bf7f61SHarald Welte
169f0bf7f61SHarald Welte #define VIA_CRDR_SDRSPTMO 0x2C
170f0bf7f61SHarald Welte
171f0bf7f61SHarald Welte #define VIA_CRDR_SDCLKSEL 0x30
172f0bf7f61SHarald Welte
173f0bf7f61SHarald Welte #define VIA_CRDR_SDEXTCTRL 0x34
174f0bf7f61SHarald Welte #define VIS_CRDR_SDEXTCTRL_AUTOSTOP_SD 0x01
175f0bf7f61SHarald Welte #define VIS_CRDR_SDEXTCTRL_SHIFT_9 0x02
176f0bf7f61SHarald Welte #define VIS_CRDR_SDEXTCTRL_MMC_8BIT 0x04
177f0bf7f61SHarald Welte #define VIS_CRDR_SDEXTCTRL_RELD_BLK 0x08
178f0bf7f61SHarald Welte #define VIS_CRDR_SDEXTCTRL_BAD_CMDA 0x10
179f0bf7f61SHarald Welte #define VIS_CRDR_SDEXTCTRL_BAD_DATA 0x20
180f0bf7f61SHarald Welte #define VIS_CRDR_SDEXTCTRL_AUTOSTOP_SPI 0x40
181f0bf7f61SHarald Welte #define VIA_CRDR_SDEXTCTRL_HISPD 0x80
182f0bf7f61SHarald Welte /* 0x38-0xFF reserved */
183f0bf7f61SHarald Welte
184f0bf7f61SHarald Welte /*
185f0bf7f61SHarald Welte * Data DMA Control Registers
186f0bf7f61SHarald Welte */
187f0bf7f61SHarald Welte
188f0bf7f61SHarald Welte #define VIA_CRDR_DMABASEADD 0x0
189f0bf7f61SHarald Welte #define VIA_CRDR_DMACOUNTER 0x4
190f0bf7f61SHarald Welte
191f0bf7f61SHarald Welte #define VIA_CRDR_DMACTRL 0x8
192f0bf7f61SHarald Welte /*
193f0bf7f61SHarald Welte * DIR :Transaction Direction
194f0bf7f61SHarald Welte * 0 : From card to memory
195f0bf7f61SHarald Welte * 1 : From memory to card
196f0bf7f61SHarald Welte */
197f0bf7f61SHarald Welte #define VIA_CRDR_DMACTRL_DIR 0x100
198f0bf7f61SHarald Welte #define VIA_CRDR_DMACTRL_ENIRQ 0x10000
199f0bf7f61SHarald Welte #define VIA_CRDR_DMACTRL_SFTRST 0x1000000
200f0bf7f61SHarald Welte
201f0bf7f61SHarald Welte #define VIA_CRDR_DMASTS 0xc
202f0bf7f61SHarald Welte
203f0bf7f61SHarald Welte #define VIA_CRDR_DMASTART 0x10
204f0bf7f61SHarald Welte /*0x14-0xFF reserved*/
205f0bf7f61SHarald Welte
206f0bf7f61SHarald Welte /*
207f0bf7f61SHarald Welte * PCI Control Registers
208f0bf7f61SHarald Welte */
209f0bf7f61SHarald Welte
210f0bf7f61SHarald Welte /*0x0 - 0x1 reserved*/
211f0bf7f61SHarald Welte #define VIA_CRDR_PCICLKGATT 0x2
212f0bf7f61SHarald Welte /*
213f0bf7f61SHarald Welte * SFTRST :
214f0bf7f61SHarald Welte * 0 : Soft reset all the controller and it will be de-asserted automatically
215f0bf7f61SHarald Welte * 1 : Soft reset is de-asserted
216f0bf7f61SHarald Welte */
217f0bf7f61SHarald Welte #define VIA_CRDR_PCICLKGATT_SFTRST 0x01
218f0bf7f61SHarald Welte /*
219f0bf7f61SHarald Welte * 3V3 : Pad power select
220f0bf7f61SHarald Welte * 0 : 1.8V
221f0bf7f61SHarald Welte * 1 : 3.3V
222f0bf7f61SHarald Welte * NOTE : No mater what the actual value should be, this bit always
223f0bf7f61SHarald Welte * read as 0. This is a hardware bug.
224f0bf7f61SHarald Welte */
225f0bf7f61SHarald Welte #define VIA_CRDR_PCICLKGATT_3V3 0x10
226f0bf7f61SHarald Welte /*
227f0bf7f61SHarald Welte * PAD_PWRON : Pad Power on/off select
228f0bf7f61SHarald Welte * 0 : Power off
229f0bf7f61SHarald Welte * 1 : Power on
230f0bf7f61SHarald Welte * NOTE : No mater what the actual value should be, this bit always
231f0bf7f61SHarald Welte * read as 0. This is a hardware bug.
232f0bf7f61SHarald Welte */
233f0bf7f61SHarald Welte #define VIA_CRDR_PCICLKGATT_PAD_PWRON 0x20
234f0bf7f61SHarald Welte
235f0bf7f61SHarald Welte #define VIA_CRDR_PCISDCCLK 0x5
236f0bf7f61SHarald Welte
237f0bf7f61SHarald Welte #define VIA_CRDR_PCIDMACLK 0x7
238f0bf7f61SHarald Welte #define VIA_CRDR_PCIDMACLK_SDC 0x2
239f0bf7f61SHarald Welte
240f0bf7f61SHarald Welte #define VIA_CRDR_PCIINTCTRL 0x8
241f0bf7f61SHarald Welte #define VIA_CRDR_PCIINTCTRL_SDCIRQEN 0x04
242f0bf7f61SHarald Welte
243f0bf7f61SHarald Welte #define VIA_CRDR_PCIINTSTATUS 0x9
244f0bf7f61SHarald Welte #define VIA_CRDR_PCIINTSTATUS_SDC 0x04
245f0bf7f61SHarald Welte
246f0bf7f61SHarald Welte #define VIA_CRDR_PCITMOCTRL 0xa
247f0bf7f61SHarald Welte #define VIA_CRDR_PCITMOCTRL_NO 0x0
248f0bf7f61SHarald Welte #define VIA_CRDR_PCITMOCTRL_32US 0x1
249f0bf7f61SHarald Welte #define VIA_CRDR_PCITMOCTRL_256US 0x2
250f0bf7f61SHarald Welte #define VIA_CRDR_PCITMOCTRL_1024US 0x3
251f0bf7f61SHarald Welte #define VIA_CRDR_PCITMOCTRL_256MS 0x4
252f0bf7f61SHarald Welte #define VIA_CRDR_PCITMOCTRL_512MS 0x5
253f0bf7f61SHarald Welte #define VIA_CRDR_PCITMOCTRL_1024MS 0x6
254f0bf7f61SHarald Welte
255f0bf7f61SHarald Welte /*0xB-0xFF reserved*/
256f0bf7f61SHarald Welte
257f0bf7f61SHarald Welte enum PCI_HOST_CLK_CONTROL {
258f0bf7f61SHarald Welte PCI_CLK_375K = 0x03,
259f0bf7f61SHarald Welte PCI_CLK_8M = 0x04,
260f0bf7f61SHarald Welte PCI_CLK_12M = 0x00,
261f0bf7f61SHarald Welte PCI_CLK_16M = 0x05,
262f0bf7f61SHarald Welte PCI_CLK_24M = 0x01,
263f0bf7f61SHarald Welte PCI_CLK_33M = 0x06,
264f0bf7f61SHarald Welte PCI_CLK_48M = 0x02
265f0bf7f61SHarald Welte };
266f0bf7f61SHarald Welte
267f0bf7f61SHarald Welte struct sdhcreg {
268f0bf7f61SHarald Welte u32 sdcontrol_reg;
269f0bf7f61SHarald Welte u32 sdcmdarg_reg;
270f0bf7f61SHarald Welte u32 sdbusmode_reg;
271f0bf7f61SHarald Welte u32 sdblklen_reg;
272f0bf7f61SHarald Welte u32 sdresp_reg[4];
273f0bf7f61SHarald Welte u32 sdcurblkcnt_reg;
274f0bf7f61SHarald Welte u32 sdintmask_reg;
275f0bf7f61SHarald Welte u32 sdstatus_reg;
276f0bf7f61SHarald Welte u32 sdrsptmo_reg;
277f0bf7f61SHarald Welte u32 sdclksel_reg;
278f0bf7f61SHarald Welte u32 sdextctrl_reg;
279f0bf7f61SHarald Welte };
280f0bf7f61SHarald Welte
281f0bf7f61SHarald Welte struct pcictrlreg {
282f0bf7f61SHarald Welte u8 reserve[2];
283f0bf7f61SHarald Welte u8 pciclkgat_reg;
284f0bf7f61SHarald Welte u8 pcinfcclk_reg;
285f0bf7f61SHarald Welte u8 pcimscclk_reg;
286f0bf7f61SHarald Welte u8 pcisdclk_reg;
287f0bf7f61SHarald Welte u8 pcicaclk_reg;
288f0bf7f61SHarald Welte u8 pcidmaclk_reg;
289f0bf7f61SHarald Welte u8 pciintctrl_reg;
290f0bf7f61SHarald Welte u8 pciintstatus_reg;
291f0bf7f61SHarald Welte u8 pcitmoctrl_reg;
292f0bf7f61SHarald Welte u8 Resv;
293f0bf7f61SHarald Welte };
294f0bf7f61SHarald Welte
295f0bf7f61SHarald Welte struct via_crdr_mmc_host {
296f0bf7f61SHarald Welte struct mmc_host *mmc;
297f0bf7f61SHarald Welte struct mmc_request *mrq;
298f0bf7f61SHarald Welte struct mmc_command *cmd;
299f0bf7f61SHarald Welte struct mmc_data *data;
300f0bf7f61SHarald Welte
301f0bf7f61SHarald Welte void __iomem *mmiobase;
302f0bf7f61SHarald Welte void __iomem *sdhc_mmiobase;
303f0bf7f61SHarald Welte void __iomem *ddma_mmiobase;
304f0bf7f61SHarald Welte void __iomem *pcictrl_mmiobase;
305f0bf7f61SHarald Welte
306f0bf7f61SHarald Welte struct pcictrlreg pm_pcictrl_reg;
307f0bf7f61SHarald Welte struct sdhcreg pm_sdhc_reg;
308f0bf7f61SHarald Welte
309f0bf7f61SHarald Welte struct work_struct carddet_work;
310f0bf7f61SHarald Welte struct tasklet_struct finish_tasklet;
311f0bf7f61SHarald Welte
312f0bf7f61SHarald Welte struct timer_list timer;
313f0bf7f61SHarald Welte spinlock_t lock;
314f0bf7f61SHarald Welte u8 power;
315f0bf7f61SHarald Welte int reject;
316f0bf7f61SHarald Welte unsigned int quirks;
317f0bf7f61SHarald Welte };
318f0bf7f61SHarald Welte
319f0bf7f61SHarald Welte /* some devices need a very long delay for power to stabilize */
320f0bf7f61SHarald Welte #define VIA_CRDR_QUIRK_300MS_PWRDELAY 0x0001
321f0bf7f61SHarald Welte
322966244ccSUlf Hansson #define VIA_CMD_TIMEOUT_MS 1000
323966244ccSUlf Hansson
324adb7434aSArvind Yadav static const struct pci_device_id via_ids[] = {
325f0bf7f61SHarald Welte {PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_9530,
326f0bf7f61SHarald Welte PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0,},
327f0bf7f61SHarald Welte {0,}
328f0bf7f61SHarald Welte };
329f0bf7f61SHarald Welte
330f0bf7f61SHarald Welte MODULE_DEVICE_TABLE(pci, via_ids);
331f0bf7f61SHarald Welte
via_print_sdchc(struct via_crdr_mmc_host * host)332f0bf7f61SHarald Welte static void via_print_sdchc(struct via_crdr_mmc_host *host)
333f0bf7f61SHarald Welte {
334f0bf7f61SHarald Welte void __iomem *addrbase = host->sdhc_mmiobase;
335f0bf7f61SHarald Welte
336f0bf7f61SHarald Welte pr_debug("SDC MMIO Registers:\n");
337f0bf7f61SHarald Welte pr_debug("SDCONTROL=%08x, SDCMDARG=%08x, SDBUSMODE=%08x\n",
338f0bf7f61SHarald Welte readl(addrbase + VIA_CRDR_SDCTRL),
339f0bf7f61SHarald Welte readl(addrbase + VIA_CRDR_SDCARG),
340f0bf7f61SHarald Welte readl(addrbase + VIA_CRDR_SDBUSMODE));
341f0bf7f61SHarald Welte pr_debug("SDBLKLEN=%08x, SDCURBLKCNT=%08x, SDINTMASK=%08x\n",
342f0bf7f61SHarald Welte readl(addrbase + VIA_CRDR_SDBLKLEN),
343f0bf7f61SHarald Welte readl(addrbase + VIA_CRDR_SDCURBLKCNT),
344f0bf7f61SHarald Welte readl(addrbase + VIA_CRDR_SDINTMASK));
345f0bf7f61SHarald Welte pr_debug("SDSTATUS=%08x, SDCLKSEL=%08x, SDEXTCTRL=%08x\n",
346f0bf7f61SHarald Welte readl(addrbase + VIA_CRDR_SDSTATUS),
347f0bf7f61SHarald Welte readl(addrbase + VIA_CRDR_SDCLKSEL),
348f0bf7f61SHarald Welte readl(addrbase + VIA_CRDR_SDEXTCTRL));
349f0bf7f61SHarald Welte }
350f0bf7f61SHarald Welte
via_print_pcictrl(struct via_crdr_mmc_host * host)351f0bf7f61SHarald Welte static void via_print_pcictrl(struct via_crdr_mmc_host *host)
352f0bf7f61SHarald Welte {
353f0bf7f61SHarald Welte void __iomem *addrbase = host->pcictrl_mmiobase;
354f0bf7f61SHarald Welte
355f0bf7f61SHarald Welte pr_debug("PCI Control Registers:\n");
356f0bf7f61SHarald Welte pr_debug("PCICLKGATT=%02x, PCISDCCLK=%02x, PCIDMACLK=%02x\n",
357f0bf7f61SHarald Welte readb(addrbase + VIA_CRDR_PCICLKGATT),
358f0bf7f61SHarald Welte readb(addrbase + VIA_CRDR_PCISDCCLK),
359f0bf7f61SHarald Welte readb(addrbase + VIA_CRDR_PCIDMACLK));
360f0bf7f61SHarald Welte pr_debug("PCIINTCTRL=%02x, PCIINTSTATUS=%02x\n",
361f0bf7f61SHarald Welte readb(addrbase + VIA_CRDR_PCIINTCTRL),
362f0bf7f61SHarald Welte readb(addrbase + VIA_CRDR_PCIINTSTATUS));
363f0bf7f61SHarald Welte }
364f0bf7f61SHarald Welte
via_save_pcictrlreg(struct via_crdr_mmc_host * host)365f0bf7f61SHarald Welte static void via_save_pcictrlreg(struct via_crdr_mmc_host *host)
366f0bf7f61SHarald Welte {
367f0bf7f61SHarald Welte struct pcictrlreg *pm_pcictrl_reg;
368f0bf7f61SHarald Welte void __iomem *addrbase;
369f0bf7f61SHarald Welte
370f0bf7f61SHarald Welte pm_pcictrl_reg = &(host->pm_pcictrl_reg);
371f0bf7f61SHarald Welte addrbase = host->pcictrl_mmiobase;
372f0bf7f61SHarald Welte
373f0bf7f61SHarald Welte pm_pcictrl_reg->pciclkgat_reg = readb(addrbase + VIA_CRDR_PCICLKGATT);
374f0bf7f61SHarald Welte pm_pcictrl_reg->pciclkgat_reg |=
375f0bf7f61SHarald Welte VIA_CRDR_PCICLKGATT_3V3 | VIA_CRDR_PCICLKGATT_PAD_PWRON;
376f0bf7f61SHarald Welte pm_pcictrl_reg->pcisdclk_reg = readb(addrbase + VIA_CRDR_PCISDCCLK);
377f0bf7f61SHarald Welte pm_pcictrl_reg->pcidmaclk_reg = readb(addrbase + VIA_CRDR_PCIDMACLK);
378f0bf7f61SHarald Welte pm_pcictrl_reg->pciintctrl_reg = readb(addrbase + VIA_CRDR_PCIINTCTRL);
379f0bf7f61SHarald Welte pm_pcictrl_reg->pciintstatus_reg =
380f0bf7f61SHarald Welte readb(addrbase + VIA_CRDR_PCIINTSTATUS);
381f0bf7f61SHarald Welte pm_pcictrl_reg->pcitmoctrl_reg = readb(addrbase + VIA_CRDR_PCITMOCTRL);
382f0bf7f61SHarald Welte }
383f0bf7f61SHarald Welte
via_restore_pcictrlreg(struct via_crdr_mmc_host * host)384f0bf7f61SHarald Welte static void via_restore_pcictrlreg(struct via_crdr_mmc_host *host)
385f0bf7f61SHarald Welte {
386f0bf7f61SHarald Welte struct pcictrlreg *pm_pcictrl_reg;
387f0bf7f61SHarald Welte void __iomem *addrbase;
388f0bf7f61SHarald Welte
389f0bf7f61SHarald Welte pm_pcictrl_reg = &(host->pm_pcictrl_reg);
390f0bf7f61SHarald Welte addrbase = host->pcictrl_mmiobase;
391f0bf7f61SHarald Welte
392f0bf7f61SHarald Welte writeb(pm_pcictrl_reg->pciclkgat_reg, addrbase + VIA_CRDR_PCICLKGATT);
393f0bf7f61SHarald Welte writeb(pm_pcictrl_reg->pcisdclk_reg, addrbase + VIA_CRDR_PCISDCCLK);
394f0bf7f61SHarald Welte writeb(pm_pcictrl_reg->pcidmaclk_reg, addrbase + VIA_CRDR_PCIDMACLK);
395f0bf7f61SHarald Welte writeb(pm_pcictrl_reg->pciintctrl_reg, addrbase + VIA_CRDR_PCIINTCTRL);
396f0bf7f61SHarald Welte writeb(pm_pcictrl_reg->pciintstatus_reg,
397f0bf7f61SHarald Welte addrbase + VIA_CRDR_PCIINTSTATUS);
398f0bf7f61SHarald Welte writeb(pm_pcictrl_reg->pcitmoctrl_reg, addrbase + VIA_CRDR_PCITMOCTRL);
399f0bf7f61SHarald Welte }
400f0bf7f61SHarald Welte
via_save_sdcreg(struct via_crdr_mmc_host * host)401f0bf7f61SHarald Welte static void via_save_sdcreg(struct via_crdr_mmc_host *host)
402f0bf7f61SHarald Welte {
403f0bf7f61SHarald Welte struct sdhcreg *pm_sdhc_reg;
404f0bf7f61SHarald Welte void __iomem *addrbase;
405f0bf7f61SHarald Welte
406f0bf7f61SHarald Welte pm_sdhc_reg = &(host->pm_sdhc_reg);
407f0bf7f61SHarald Welte addrbase = host->sdhc_mmiobase;
408f0bf7f61SHarald Welte
409f0bf7f61SHarald Welte pm_sdhc_reg->sdcontrol_reg = readl(addrbase + VIA_CRDR_SDCTRL);
410f0bf7f61SHarald Welte pm_sdhc_reg->sdcmdarg_reg = readl(addrbase + VIA_CRDR_SDCARG);
411f0bf7f61SHarald Welte pm_sdhc_reg->sdbusmode_reg = readl(addrbase + VIA_CRDR_SDBUSMODE);
412f0bf7f61SHarald Welte pm_sdhc_reg->sdblklen_reg = readl(addrbase + VIA_CRDR_SDBLKLEN);
413f0bf7f61SHarald Welte pm_sdhc_reg->sdcurblkcnt_reg = readl(addrbase + VIA_CRDR_SDCURBLKCNT);
414f0bf7f61SHarald Welte pm_sdhc_reg->sdintmask_reg = readl(addrbase + VIA_CRDR_SDINTMASK);
415f0bf7f61SHarald Welte pm_sdhc_reg->sdstatus_reg = readl(addrbase + VIA_CRDR_SDSTATUS);
416f0bf7f61SHarald Welte pm_sdhc_reg->sdrsptmo_reg = readl(addrbase + VIA_CRDR_SDRSPTMO);
417f0bf7f61SHarald Welte pm_sdhc_reg->sdclksel_reg = readl(addrbase + VIA_CRDR_SDCLKSEL);
418f0bf7f61SHarald Welte pm_sdhc_reg->sdextctrl_reg = readl(addrbase + VIA_CRDR_SDEXTCTRL);
419f0bf7f61SHarald Welte }
420f0bf7f61SHarald Welte
via_restore_sdcreg(struct via_crdr_mmc_host * host)421f0bf7f61SHarald Welte static void via_restore_sdcreg(struct via_crdr_mmc_host *host)
422f0bf7f61SHarald Welte {
423f0bf7f61SHarald Welte struct sdhcreg *pm_sdhc_reg;
424f0bf7f61SHarald Welte void __iomem *addrbase;
425f0bf7f61SHarald Welte
426f0bf7f61SHarald Welte pm_sdhc_reg = &(host->pm_sdhc_reg);
427f0bf7f61SHarald Welte addrbase = host->sdhc_mmiobase;
428f0bf7f61SHarald Welte
429f0bf7f61SHarald Welte writel(pm_sdhc_reg->sdcontrol_reg, addrbase + VIA_CRDR_SDCTRL);
430f0bf7f61SHarald Welte writel(pm_sdhc_reg->sdcmdarg_reg, addrbase + VIA_CRDR_SDCARG);
431f0bf7f61SHarald Welte writel(pm_sdhc_reg->sdbusmode_reg, addrbase + VIA_CRDR_SDBUSMODE);
432f0bf7f61SHarald Welte writel(pm_sdhc_reg->sdblklen_reg, addrbase + VIA_CRDR_SDBLKLEN);
433f0bf7f61SHarald Welte writel(pm_sdhc_reg->sdcurblkcnt_reg, addrbase + VIA_CRDR_SDCURBLKCNT);
434f0bf7f61SHarald Welte writel(pm_sdhc_reg->sdintmask_reg, addrbase + VIA_CRDR_SDINTMASK);
435f0bf7f61SHarald Welte writel(pm_sdhc_reg->sdstatus_reg, addrbase + VIA_CRDR_SDSTATUS);
436f0bf7f61SHarald Welte writel(pm_sdhc_reg->sdrsptmo_reg, addrbase + VIA_CRDR_SDRSPTMO);
437f0bf7f61SHarald Welte writel(pm_sdhc_reg->sdclksel_reg, addrbase + VIA_CRDR_SDCLKSEL);
438f0bf7f61SHarald Welte writel(pm_sdhc_reg->sdextctrl_reg, addrbase + VIA_CRDR_SDEXTCTRL);
439f0bf7f61SHarald Welte }
440f0bf7f61SHarald Welte
via_pwron_sleep(struct via_crdr_mmc_host * sdhost)441f0bf7f61SHarald Welte static void via_pwron_sleep(struct via_crdr_mmc_host *sdhost)
442f0bf7f61SHarald Welte {
443f0bf7f61SHarald Welte if (sdhost->quirks & VIA_CRDR_QUIRK_300MS_PWRDELAY)
444f0bf7f61SHarald Welte msleep(300);
445f0bf7f61SHarald Welte else
446f0bf7f61SHarald Welte msleep(3);
447f0bf7f61SHarald Welte }
448f0bf7f61SHarald Welte
via_set_ddma(struct via_crdr_mmc_host * host,dma_addr_t dmaaddr,u32 count,int dir,int enirq)449f0bf7f61SHarald Welte static void via_set_ddma(struct via_crdr_mmc_host *host,
450f0bf7f61SHarald Welte dma_addr_t dmaaddr, u32 count, int dir, int enirq)
451f0bf7f61SHarald Welte {
452f0bf7f61SHarald Welte void __iomem *addrbase;
453f0bf7f61SHarald Welte u32 ctrl_data = 0;
454f0bf7f61SHarald Welte
455f0bf7f61SHarald Welte if (enirq)
456f0bf7f61SHarald Welte ctrl_data |= VIA_CRDR_DMACTRL_ENIRQ;
457f0bf7f61SHarald Welte
458f0bf7f61SHarald Welte if (dir)
459f0bf7f61SHarald Welte ctrl_data |= VIA_CRDR_DMACTRL_DIR;
460f0bf7f61SHarald Welte
461f0bf7f61SHarald Welte addrbase = host->ddma_mmiobase;
462f0bf7f61SHarald Welte
463f0bf7f61SHarald Welte writel(dmaaddr, addrbase + VIA_CRDR_DMABASEADD);
464f0bf7f61SHarald Welte writel(count, addrbase + VIA_CRDR_DMACOUNTER);
465f0bf7f61SHarald Welte writel(ctrl_data, addrbase + VIA_CRDR_DMACTRL);
466f0bf7f61SHarald Welte writel(0x01, addrbase + VIA_CRDR_DMASTART);
467f0bf7f61SHarald Welte
468f0bf7f61SHarald Welte /* It seems that our DMA can not work normally with 375kHz clock */
469f0bf7f61SHarald Welte /* FIXME: don't brute-force 8MHz but use PIO at 375kHz !! */
470f0bf7f61SHarald Welte addrbase = host->pcictrl_mmiobase;
471f0bf7f61SHarald Welte if (readb(addrbase + VIA_CRDR_PCISDCCLK) == PCI_CLK_375K) {
472f0bf7f61SHarald Welte dev_info(host->mmc->parent, "forcing card speed to 8MHz\n");
473f0bf7f61SHarald Welte writeb(PCI_CLK_8M, addrbase + VIA_CRDR_PCISDCCLK);
474f0bf7f61SHarald Welte }
475f0bf7f61SHarald Welte }
476f0bf7f61SHarald Welte
via_sdc_preparedata(struct via_crdr_mmc_host * host,struct mmc_data * data)477f0bf7f61SHarald Welte static void via_sdc_preparedata(struct via_crdr_mmc_host *host,
478f0bf7f61SHarald Welte struct mmc_data *data)
479f0bf7f61SHarald Welte {
480f0bf7f61SHarald Welte void __iomem *addrbase;
481f0bf7f61SHarald Welte u32 blk_reg;
482f0bf7f61SHarald Welte int count;
483f0bf7f61SHarald Welte
484f0bf7f61SHarald Welte WARN_ON(host->data);
485f0bf7f61SHarald Welte
486f0bf7f61SHarald Welte /* Sanity checks */
487f0bf7f61SHarald Welte BUG_ON(data->blksz > host->mmc->max_blk_size);
488f0bf7f61SHarald Welte BUG_ON(data->blocks > host->mmc->max_blk_count);
489f0bf7f61SHarald Welte
490f0bf7f61SHarald Welte host->data = data;
491f0bf7f61SHarald Welte
492f0bf7f61SHarald Welte count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
493f0bf7f61SHarald Welte ((data->flags & MMC_DATA_READ) ?
494b048457cSChristophe JAILLET DMA_FROM_DEVICE : DMA_TO_DEVICE));
495f0bf7f61SHarald Welte BUG_ON(count != 1);
496f0bf7f61SHarald Welte
497f0bf7f61SHarald Welte via_set_ddma(host, sg_dma_address(data->sg), sg_dma_len(data->sg),
498f0bf7f61SHarald Welte (data->flags & MMC_DATA_WRITE) ? 1 : 0, 1);
499f0bf7f61SHarald Welte
500f0bf7f61SHarald Welte addrbase = host->sdhc_mmiobase;
501f0bf7f61SHarald Welte
502f0bf7f61SHarald Welte blk_reg = data->blksz - 1;
503f0bf7f61SHarald Welte blk_reg |= VIA_CRDR_SDBLKLEN_GPIDET | VIA_CRDR_SDBLKLEN_INTEN;
504f0bf7f61SHarald Welte blk_reg |= (data->blocks) << 16;
505f0bf7f61SHarald Welte
506f0bf7f61SHarald Welte writel(blk_reg, addrbase + VIA_CRDR_SDBLKLEN);
507f0bf7f61SHarald Welte }
508f0bf7f61SHarald Welte
via_sdc_get_response(struct via_crdr_mmc_host * host,struct mmc_command * cmd)509f0bf7f61SHarald Welte static void via_sdc_get_response(struct via_crdr_mmc_host *host,
510f0bf7f61SHarald Welte struct mmc_command *cmd)
511f0bf7f61SHarald Welte {
512f0bf7f61SHarald Welte void __iomem *addrbase = host->sdhc_mmiobase;
513f0bf7f61SHarald Welte u32 dwdata0 = readl(addrbase + VIA_CRDR_SDRESP0);
514f0bf7f61SHarald Welte u32 dwdata1 = readl(addrbase + VIA_CRDR_SDRESP1);
515f0bf7f61SHarald Welte u32 dwdata2 = readl(addrbase + VIA_CRDR_SDRESP2);
516f0bf7f61SHarald Welte u32 dwdata3 = readl(addrbase + VIA_CRDR_SDRESP3);
517f0bf7f61SHarald Welte
518f0bf7f61SHarald Welte if (cmd->flags & MMC_RSP_136) {
519f0bf7f61SHarald Welte cmd->resp[0] = ((u8) (dwdata1)) |
520f0bf7f61SHarald Welte (((u8) (dwdata0 >> 24)) << 8) |
521f0bf7f61SHarald Welte (((u8) (dwdata0 >> 16)) << 16) |
522f0bf7f61SHarald Welte (((u8) (dwdata0 >> 8)) << 24);
523f0bf7f61SHarald Welte
524f0bf7f61SHarald Welte cmd->resp[1] = ((u8) (dwdata2)) |
525f0bf7f61SHarald Welte (((u8) (dwdata1 >> 24)) << 8) |
526f0bf7f61SHarald Welte (((u8) (dwdata1 >> 16)) << 16) |
527f0bf7f61SHarald Welte (((u8) (dwdata1 >> 8)) << 24);
528f0bf7f61SHarald Welte
529f0bf7f61SHarald Welte cmd->resp[2] = ((u8) (dwdata3)) |
530f0bf7f61SHarald Welte (((u8) (dwdata2 >> 24)) << 8) |
531f0bf7f61SHarald Welte (((u8) (dwdata2 >> 16)) << 16) |
532f0bf7f61SHarald Welte (((u8) (dwdata2 >> 8)) << 24);
533f0bf7f61SHarald Welte
534f0bf7f61SHarald Welte cmd->resp[3] = 0xff |
535f0bf7f61SHarald Welte ((((u8) (dwdata3 >> 24))) << 8) |
536f0bf7f61SHarald Welte (((u8) (dwdata3 >> 16)) << 16) |
537f0bf7f61SHarald Welte (((u8) (dwdata3 >> 8)) << 24);
538f0bf7f61SHarald Welte } else {
539f0bf7f61SHarald Welte dwdata0 >>= 8;
540f0bf7f61SHarald Welte cmd->resp[0] = ((dwdata0 & 0xff) << 24) |
541f0bf7f61SHarald Welte (((dwdata0 >> 8) & 0xff) << 16) |
542f0bf7f61SHarald Welte (((dwdata0 >> 16) & 0xff) << 8) | (dwdata1 & 0xff);
543f0bf7f61SHarald Welte
544f0bf7f61SHarald Welte dwdata1 >>= 8;
545f0bf7f61SHarald Welte cmd->resp[1] = ((dwdata1 & 0xff) << 24) |
546f0bf7f61SHarald Welte (((dwdata1 >> 8) & 0xff) << 16) |
547f0bf7f61SHarald Welte (((dwdata1 >> 16) & 0xff) << 8);
548f0bf7f61SHarald Welte }
549f0bf7f61SHarald Welte }
550f0bf7f61SHarald Welte
via_sdc_send_command(struct via_crdr_mmc_host * host,struct mmc_command * cmd)551f0bf7f61SHarald Welte static void via_sdc_send_command(struct via_crdr_mmc_host *host,
552f0bf7f61SHarald Welte struct mmc_command *cmd)
553f0bf7f61SHarald Welte {
554f0bf7f61SHarald Welte void __iomem *addrbase;
555f0bf7f61SHarald Welte struct mmc_data *data;
556966244ccSUlf Hansson unsigned int timeout_ms;
557f0bf7f61SHarald Welte u32 cmdctrl = 0;
558f0bf7f61SHarald Welte
559f0bf7f61SHarald Welte WARN_ON(host->cmd);
560f0bf7f61SHarald Welte
561f0bf7f61SHarald Welte data = cmd->data;
562f0bf7f61SHarald Welte host->cmd = cmd;
563f0bf7f61SHarald Welte
564966244ccSUlf Hansson timeout_ms = cmd->busy_timeout ? cmd->busy_timeout : VIA_CMD_TIMEOUT_MS;
565966244ccSUlf Hansson mod_timer(&host->timer, jiffies + msecs_to_jiffies(timeout_ms));
566966244ccSUlf Hansson
567f0bf7f61SHarald Welte /*Command index*/
568f0bf7f61SHarald Welte cmdctrl = cmd->opcode << 8;
569f0bf7f61SHarald Welte
570f0bf7f61SHarald Welte /*Response type*/
571f0bf7f61SHarald Welte switch (mmc_resp_type(cmd)) {
572f0bf7f61SHarald Welte case MMC_RSP_NONE:
573f0bf7f61SHarald Welte cmdctrl |= VIA_CRDR_SDCTRL_RSP_NONE;
574f0bf7f61SHarald Welte break;
575f0bf7f61SHarald Welte case MMC_RSP_R1:
576f0bf7f61SHarald Welte cmdctrl |= VIA_CRDR_SDCTRL_RSP_R1;
577f0bf7f61SHarald Welte break;
578f0bf7f61SHarald Welte case MMC_RSP_R1B:
579f0bf7f61SHarald Welte cmdctrl |= VIA_CRDR_SDCTRL_RSP_R1B;
580f0bf7f61SHarald Welte break;
581f0bf7f61SHarald Welte case MMC_RSP_R2:
582f0bf7f61SHarald Welte cmdctrl |= VIA_CRDR_SDCTRL_RSP_R2;
583f0bf7f61SHarald Welte break;
584f0bf7f61SHarald Welte case MMC_RSP_R3:
585f0bf7f61SHarald Welte cmdctrl |= VIA_CRDR_SDCTRL_RSP_R3;
586f0bf7f61SHarald Welte break;
587f0bf7f61SHarald Welte default:
588f0bf7f61SHarald Welte pr_err("%s: cmd->flag is not valid\n", mmc_hostname(host->mmc));
589f0bf7f61SHarald Welte break;
590f0bf7f61SHarald Welte }
591f0bf7f61SHarald Welte
592f0bf7f61SHarald Welte if (!(cmd->data))
593f0bf7f61SHarald Welte goto nodata;
594f0bf7f61SHarald Welte
595f0bf7f61SHarald Welte via_sdc_preparedata(host, data);
596f0bf7f61SHarald Welte
597f0bf7f61SHarald Welte /*Command control*/
598f0bf7f61SHarald Welte if (data->blocks > 1) {
599f0bf7f61SHarald Welte if (data->flags & MMC_DATA_WRITE) {
600f0bf7f61SHarald Welte cmdctrl |= VIA_CRDR_SDCTRL_WRITE;
601f0bf7f61SHarald Welte cmdctrl |= VIA_CRDR_SDCTRL_MULTI_WR;
602f0bf7f61SHarald Welte } else {
603f0bf7f61SHarald Welte cmdctrl |= VIA_CRDR_SDCTRL_MULTI_RD;
604f0bf7f61SHarald Welte }
605f0bf7f61SHarald Welte } else {
606f0bf7f61SHarald Welte if (data->flags & MMC_DATA_WRITE) {
607f0bf7f61SHarald Welte cmdctrl |= VIA_CRDR_SDCTRL_WRITE;
608f0bf7f61SHarald Welte cmdctrl |= VIA_CRDR_SDCTRL_SINGLE_WR;
609f0bf7f61SHarald Welte } else {
610f0bf7f61SHarald Welte cmdctrl |= VIA_CRDR_SDCTRL_SINGLE_RD;
611f0bf7f61SHarald Welte }
612f0bf7f61SHarald Welte }
613f0bf7f61SHarald Welte
614f0bf7f61SHarald Welte nodata:
615f0bf7f61SHarald Welte if (cmd == host->mrq->stop)
616f0bf7f61SHarald Welte cmdctrl |= VIA_CRDR_SDCTRL_STOP;
617f0bf7f61SHarald Welte
618f0bf7f61SHarald Welte cmdctrl |= VIA_CRDR_SDCTRL_START;
619f0bf7f61SHarald Welte
620f0bf7f61SHarald Welte addrbase = host->sdhc_mmiobase;
621f0bf7f61SHarald Welte writel(cmd->arg, addrbase + VIA_CRDR_SDCARG);
622f0bf7f61SHarald Welte writel(cmdctrl, addrbase + VIA_CRDR_SDCTRL);
623f0bf7f61SHarald Welte }
624f0bf7f61SHarald Welte
via_sdc_finish_data(struct via_crdr_mmc_host * host)625f0bf7f61SHarald Welte static void via_sdc_finish_data(struct via_crdr_mmc_host *host)
626f0bf7f61SHarald Welte {
627f0bf7f61SHarald Welte struct mmc_data *data;
628f0bf7f61SHarald Welte
629f0bf7f61SHarald Welte BUG_ON(!host->data);
630f0bf7f61SHarald Welte
631f0bf7f61SHarald Welte data = host->data;
632f0bf7f61SHarald Welte host->data = NULL;
633f0bf7f61SHarald Welte
634f0bf7f61SHarald Welte if (data->error)
635f0bf7f61SHarald Welte data->bytes_xfered = 0;
636f0bf7f61SHarald Welte else
637f0bf7f61SHarald Welte data->bytes_xfered = data->blocks * data->blksz;
638f0bf7f61SHarald Welte
639f0bf7f61SHarald Welte dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
640f0bf7f61SHarald Welte ((data->flags & MMC_DATA_READ) ?
641b048457cSChristophe JAILLET DMA_FROM_DEVICE : DMA_TO_DEVICE));
642f0bf7f61SHarald Welte
643f0bf7f61SHarald Welte if (data->stop)
644f0bf7f61SHarald Welte via_sdc_send_command(host, data->stop);
645f0bf7f61SHarald Welte else
646f0bf7f61SHarald Welte tasklet_schedule(&host->finish_tasklet);
647f0bf7f61SHarald Welte }
648f0bf7f61SHarald Welte
via_sdc_finish_command(struct via_crdr_mmc_host * host)649f0bf7f61SHarald Welte static void via_sdc_finish_command(struct via_crdr_mmc_host *host)
650f0bf7f61SHarald Welte {
651f0bf7f61SHarald Welte via_sdc_get_response(host, host->cmd);
652f0bf7f61SHarald Welte
653f0bf7f61SHarald Welte host->cmd->error = 0;
654f0bf7f61SHarald Welte
655f0bf7f61SHarald Welte if (!host->cmd->data)
656f0bf7f61SHarald Welte tasklet_schedule(&host->finish_tasklet);
657f0bf7f61SHarald Welte
658f0bf7f61SHarald Welte host->cmd = NULL;
659f0bf7f61SHarald Welte }
660f0bf7f61SHarald Welte
via_sdc_request(struct mmc_host * mmc,struct mmc_request * mrq)661f0bf7f61SHarald Welte static void via_sdc_request(struct mmc_host *mmc, struct mmc_request *mrq)
662f0bf7f61SHarald Welte {
663f0bf7f61SHarald Welte void __iomem *addrbase;
664f0bf7f61SHarald Welte struct via_crdr_mmc_host *host;
665f0bf7f61SHarald Welte unsigned long flags;
666f0bf7f61SHarald Welte u16 status;
667f0bf7f61SHarald Welte
668f0bf7f61SHarald Welte host = mmc_priv(mmc);
669f0bf7f61SHarald Welte
670f0bf7f61SHarald Welte spin_lock_irqsave(&host->lock, flags);
671f0bf7f61SHarald Welte
672f0bf7f61SHarald Welte addrbase = host->pcictrl_mmiobase;
673f0bf7f61SHarald Welte writeb(VIA_CRDR_PCIDMACLK_SDC, addrbase + VIA_CRDR_PCIDMACLK);
674f0bf7f61SHarald Welte
675f0bf7f61SHarald Welte status = readw(host->sdhc_mmiobase + VIA_CRDR_SDSTATUS);
676f0bf7f61SHarald Welte status &= VIA_CRDR_SDSTS_W1C_MASK;
677f0bf7f61SHarald Welte writew(status, host->sdhc_mmiobase + VIA_CRDR_SDSTATUS);
678f0bf7f61SHarald Welte
679f0bf7f61SHarald Welte WARN_ON(host->mrq != NULL);
680f0bf7f61SHarald Welte host->mrq = mrq;
681f0bf7f61SHarald Welte
682f0bf7f61SHarald Welte status = readw(host->sdhc_mmiobase + VIA_CRDR_SDSTATUS);
683f0bf7f61SHarald Welte if (!(status & VIA_CRDR_SDSTS_SLOTG) || host->reject) {
684f0bf7f61SHarald Welte host->mrq->cmd->error = -ENOMEDIUM;
685f0bf7f61SHarald Welte tasklet_schedule(&host->finish_tasklet);
686f0bf7f61SHarald Welte } else {
687f0bf7f61SHarald Welte via_sdc_send_command(host, mrq->cmd);
688f0bf7f61SHarald Welte }
689f0bf7f61SHarald Welte
690f0bf7f61SHarald Welte spin_unlock_irqrestore(&host->lock, flags);
691f0bf7f61SHarald Welte }
692f0bf7f61SHarald Welte
via_sdc_set_power(struct via_crdr_mmc_host * host,unsigned short power,unsigned int on)693f0bf7f61SHarald Welte static void via_sdc_set_power(struct via_crdr_mmc_host *host,
694f0bf7f61SHarald Welte unsigned short power, unsigned int on)
695f0bf7f61SHarald Welte {
696f0bf7f61SHarald Welte unsigned long flags;
697f0bf7f61SHarald Welte u8 gatt;
698f0bf7f61SHarald Welte
699f0bf7f61SHarald Welte spin_lock_irqsave(&host->lock, flags);
700f0bf7f61SHarald Welte
701f0bf7f61SHarald Welte host->power = (1 << power);
702f0bf7f61SHarald Welte
703f0bf7f61SHarald Welte gatt = readb(host->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
704f0bf7f61SHarald Welte if (host->power == MMC_VDD_165_195)
705f0bf7f61SHarald Welte gatt &= ~VIA_CRDR_PCICLKGATT_3V3;
706f0bf7f61SHarald Welte else
707f0bf7f61SHarald Welte gatt |= VIA_CRDR_PCICLKGATT_3V3;
708f0bf7f61SHarald Welte if (on)
709f0bf7f61SHarald Welte gatt |= VIA_CRDR_PCICLKGATT_PAD_PWRON;
710f0bf7f61SHarald Welte else
711f0bf7f61SHarald Welte gatt &= ~VIA_CRDR_PCICLKGATT_PAD_PWRON;
712f0bf7f61SHarald Welte writeb(gatt, host->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
713f0bf7f61SHarald Welte
714f0bf7f61SHarald Welte spin_unlock_irqrestore(&host->lock, flags);
715f0bf7f61SHarald Welte
716f0bf7f61SHarald Welte via_pwron_sleep(host);
717f0bf7f61SHarald Welte }
718f0bf7f61SHarald Welte
via_sdc_set_ios(struct mmc_host * mmc,struct mmc_ios * ios)719f0bf7f61SHarald Welte static void via_sdc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
720f0bf7f61SHarald Welte {
721f0bf7f61SHarald Welte struct via_crdr_mmc_host *host;
722f0bf7f61SHarald Welte unsigned long flags;
723f0bf7f61SHarald Welte void __iomem *addrbase;
724f0bf7f61SHarald Welte u32 org_data, sdextctrl;
725f0bf7f61SHarald Welte u8 clock;
726f0bf7f61SHarald Welte
727f0bf7f61SHarald Welte host = mmc_priv(mmc);
728f0bf7f61SHarald Welte
729f0bf7f61SHarald Welte spin_lock_irqsave(&host->lock, flags);
730f0bf7f61SHarald Welte
731f0bf7f61SHarald Welte addrbase = host->sdhc_mmiobase;
732f0bf7f61SHarald Welte org_data = readl(addrbase + VIA_CRDR_SDBUSMODE);
733f0bf7f61SHarald Welte sdextctrl = readl(addrbase + VIA_CRDR_SDEXTCTRL);
734f0bf7f61SHarald Welte
735f0bf7f61SHarald Welte if (ios->bus_width == MMC_BUS_WIDTH_1)
736f0bf7f61SHarald Welte org_data &= ~VIA_CRDR_SDMODE_4BIT;
737f0bf7f61SHarald Welte else
738f0bf7f61SHarald Welte org_data |= VIA_CRDR_SDMODE_4BIT;
739f0bf7f61SHarald Welte
740f0bf7f61SHarald Welte if (ios->power_mode == MMC_POWER_OFF)
741f0bf7f61SHarald Welte org_data &= ~VIA_CRDR_SDMODE_CLK_ON;
742f0bf7f61SHarald Welte else
743f0bf7f61SHarald Welte org_data |= VIA_CRDR_SDMODE_CLK_ON;
744f0bf7f61SHarald Welte
745f0bf7f61SHarald Welte if (ios->timing == MMC_TIMING_SD_HS)
746f0bf7f61SHarald Welte sdextctrl |= VIA_CRDR_SDEXTCTRL_HISPD;
747f0bf7f61SHarald Welte else
748f0bf7f61SHarald Welte sdextctrl &= ~VIA_CRDR_SDEXTCTRL_HISPD;
749f0bf7f61SHarald Welte
750f0bf7f61SHarald Welte writel(org_data, addrbase + VIA_CRDR_SDBUSMODE);
751f0bf7f61SHarald Welte writel(sdextctrl, addrbase + VIA_CRDR_SDEXTCTRL);
752f0bf7f61SHarald Welte
753f0bf7f61SHarald Welte if (ios->clock >= 48000000)
754f0bf7f61SHarald Welte clock = PCI_CLK_48M;
755f0bf7f61SHarald Welte else if (ios->clock >= 33000000)
756f0bf7f61SHarald Welte clock = PCI_CLK_33M;
757f0bf7f61SHarald Welte else if (ios->clock >= 24000000)
758f0bf7f61SHarald Welte clock = PCI_CLK_24M;
759f0bf7f61SHarald Welte else if (ios->clock >= 16000000)
760f0bf7f61SHarald Welte clock = PCI_CLK_16M;
761f0bf7f61SHarald Welte else if (ios->clock >= 12000000)
762f0bf7f61SHarald Welte clock = PCI_CLK_12M;
763f0bf7f61SHarald Welte else if (ios->clock >= 8000000)
764f0bf7f61SHarald Welte clock = PCI_CLK_8M;
765f0bf7f61SHarald Welte else
766f0bf7f61SHarald Welte clock = PCI_CLK_375K;
767f0bf7f61SHarald Welte
768f0bf7f61SHarald Welte addrbase = host->pcictrl_mmiobase;
769f0bf7f61SHarald Welte if (readb(addrbase + VIA_CRDR_PCISDCCLK) != clock)
770f0bf7f61SHarald Welte writeb(clock, addrbase + VIA_CRDR_PCISDCCLK);
771f0bf7f61SHarald Welte
772f0bf7f61SHarald Welte spin_unlock_irqrestore(&host->lock, flags);
773f0bf7f61SHarald Welte
774f0bf7f61SHarald Welte if (ios->power_mode != MMC_POWER_OFF)
775f0bf7f61SHarald Welte via_sdc_set_power(host, ios->vdd, 1);
776f0bf7f61SHarald Welte else
777f0bf7f61SHarald Welte via_sdc_set_power(host, ios->vdd, 0);
778f0bf7f61SHarald Welte }
779f0bf7f61SHarald Welte
via_sdc_get_ro(struct mmc_host * mmc)780f0bf7f61SHarald Welte static int via_sdc_get_ro(struct mmc_host *mmc)
781f0bf7f61SHarald Welte {
782f0bf7f61SHarald Welte struct via_crdr_mmc_host *host;
783f0bf7f61SHarald Welte unsigned long flags;
784f0bf7f61SHarald Welte u16 status;
785f0bf7f61SHarald Welte
786f0bf7f61SHarald Welte host = mmc_priv(mmc);
787f0bf7f61SHarald Welte
788f0bf7f61SHarald Welte spin_lock_irqsave(&host->lock, flags);
789f0bf7f61SHarald Welte
790f0bf7f61SHarald Welte status = readw(host->sdhc_mmiobase + VIA_CRDR_SDSTATUS);
791f0bf7f61SHarald Welte
792f0bf7f61SHarald Welte spin_unlock_irqrestore(&host->lock, flags);
793f0bf7f61SHarald Welte
794f0bf7f61SHarald Welte return !(status & VIA_CRDR_SDSTS_WP);
795f0bf7f61SHarald Welte }
796f0bf7f61SHarald Welte
797f0bf7f61SHarald Welte static const struct mmc_host_ops via_sdc_ops = {
798f0bf7f61SHarald Welte .request = via_sdc_request,
799f0bf7f61SHarald Welte .set_ios = via_sdc_set_ios,
800f0bf7f61SHarald Welte .get_ro = via_sdc_get_ro,
801f0bf7f61SHarald Welte };
802f0bf7f61SHarald Welte
via_reset_pcictrl(struct via_crdr_mmc_host * host)803f0bf7f61SHarald Welte static void via_reset_pcictrl(struct via_crdr_mmc_host *host)
804f0bf7f61SHarald Welte {
805f0bf7f61SHarald Welte unsigned long flags;
806f0bf7f61SHarald Welte u8 gatt;
807f0bf7f61SHarald Welte
808f0bf7f61SHarald Welte spin_lock_irqsave(&host->lock, flags);
809f0bf7f61SHarald Welte
810f0bf7f61SHarald Welte via_save_pcictrlreg(host);
811f0bf7f61SHarald Welte via_save_sdcreg(host);
812f0bf7f61SHarald Welte
813f0bf7f61SHarald Welte spin_unlock_irqrestore(&host->lock, flags);
814f0bf7f61SHarald Welte
815f0bf7f61SHarald Welte gatt = VIA_CRDR_PCICLKGATT_PAD_PWRON;
816f0bf7f61SHarald Welte if (host->power == MMC_VDD_165_195)
817f0bf7f61SHarald Welte gatt &= VIA_CRDR_PCICLKGATT_3V3;
818f0bf7f61SHarald Welte else
819f0bf7f61SHarald Welte gatt |= VIA_CRDR_PCICLKGATT_3V3;
820f0bf7f61SHarald Welte writeb(gatt, host->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
821f0bf7f61SHarald Welte via_pwron_sleep(host);
822f0bf7f61SHarald Welte gatt |= VIA_CRDR_PCICLKGATT_SFTRST;
823f0bf7f61SHarald Welte writeb(gatt, host->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
824f0bf7f61SHarald Welte msleep(3);
825f0bf7f61SHarald Welte
826f0bf7f61SHarald Welte spin_lock_irqsave(&host->lock, flags);
827f0bf7f61SHarald Welte
828f0bf7f61SHarald Welte via_restore_pcictrlreg(host);
829f0bf7f61SHarald Welte via_restore_sdcreg(host);
830f0bf7f61SHarald Welte
831f0bf7f61SHarald Welte spin_unlock_irqrestore(&host->lock, flags);
832f0bf7f61SHarald Welte }
833f0bf7f61SHarald Welte
via_sdc_cmd_isr(struct via_crdr_mmc_host * host,u16 intmask)834f0bf7f61SHarald Welte static void via_sdc_cmd_isr(struct via_crdr_mmc_host *host, u16 intmask)
835f0bf7f61SHarald Welte {
836f0bf7f61SHarald Welte BUG_ON(intmask == 0);
837f0bf7f61SHarald Welte
838f0bf7f61SHarald Welte if (!host->cmd) {
839f0bf7f61SHarald Welte pr_err("%s: Got command interrupt 0x%x even "
840f0bf7f61SHarald Welte "though no command operation was in progress.\n",
841f0bf7f61SHarald Welte mmc_hostname(host->mmc), intmask);
842f0bf7f61SHarald Welte return;
843f0bf7f61SHarald Welte }
844f0bf7f61SHarald Welte
845f0bf7f61SHarald Welte if (intmask & VIA_CRDR_SDSTS_CRTO)
846f0bf7f61SHarald Welte host->cmd->error = -ETIMEDOUT;
847f0bf7f61SHarald Welte else if (intmask & VIA_CRDR_SDSTS_SC)
848f0bf7f61SHarald Welte host->cmd->error = -EILSEQ;
849f0bf7f61SHarald Welte
850f0bf7f61SHarald Welte if (host->cmd->error)
851f0bf7f61SHarald Welte tasklet_schedule(&host->finish_tasklet);
852f0bf7f61SHarald Welte else if (intmask & VIA_CRDR_SDSTS_CRD)
853f0bf7f61SHarald Welte via_sdc_finish_command(host);
854f0bf7f61SHarald Welte }
855f0bf7f61SHarald Welte
via_sdc_data_isr(struct via_crdr_mmc_host * host,u16 intmask)856f0bf7f61SHarald Welte static void via_sdc_data_isr(struct via_crdr_mmc_host *host, u16 intmask)
857f0bf7f61SHarald Welte {
858f0bf7f61SHarald Welte BUG_ON(intmask == 0);
859f0bf7f61SHarald Welte
86045c8ddd0SZheyu Ma if (!host->data)
86145c8ddd0SZheyu Ma return;
86245c8ddd0SZheyu Ma
863f0bf7f61SHarald Welte if (intmask & VIA_CRDR_SDSTS_DT)
864f0bf7f61SHarald Welte host->data->error = -ETIMEDOUT;
865f0bf7f61SHarald Welte else if (intmask & (VIA_CRDR_SDSTS_RC | VIA_CRDR_SDSTS_WC))
866f0bf7f61SHarald Welte host->data->error = -EILSEQ;
867f0bf7f61SHarald Welte
868f0bf7f61SHarald Welte via_sdc_finish_data(host);
869f0bf7f61SHarald Welte }
870f0bf7f61SHarald Welte
via_sdc_isr(int irq,void * dev_id)871f0bf7f61SHarald Welte static irqreturn_t via_sdc_isr(int irq, void *dev_id)
872f0bf7f61SHarald Welte {
873f0bf7f61SHarald Welte struct via_crdr_mmc_host *sdhost = dev_id;
874f0bf7f61SHarald Welte void __iomem *addrbase;
875f0bf7f61SHarald Welte u8 pci_status;
876f0bf7f61SHarald Welte u16 sd_status;
877f0bf7f61SHarald Welte irqreturn_t result;
878f0bf7f61SHarald Welte
879f0bf7f61SHarald Welte if (!sdhost)
880f0bf7f61SHarald Welte return IRQ_NONE;
881f0bf7f61SHarald Welte
882f0bf7f61SHarald Welte spin_lock(&sdhost->lock);
883f0bf7f61SHarald Welte
884f0bf7f61SHarald Welte addrbase = sdhost->pcictrl_mmiobase;
885f0bf7f61SHarald Welte pci_status = readb(addrbase + VIA_CRDR_PCIINTSTATUS);
886f0bf7f61SHarald Welte if (!(pci_status & VIA_CRDR_PCIINTSTATUS_SDC)) {
887f0bf7f61SHarald Welte result = IRQ_NONE;
888f0bf7f61SHarald Welte goto out;
889f0bf7f61SHarald Welte }
890f0bf7f61SHarald Welte
891f0bf7f61SHarald Welte addrbase = sdhost->sdhc_mmiobase;
892f0bf7f61SHarald Welte sd_status = readw(addrbase + VIA_CRDR_SDSTATUS);
893f0bf7f61SHarald Welte sd_status &= VIA_CRDR_SDSTS_INT_MASK;
894f0bf7f61SHarald Welte sd_status &= ~VIA_CRDR_SDSTS_IGN_MASK;
895f0bf7f61SHarald Welte if (!sd_status) {
896f0bf7f61SHarald Welte result = IRQ_NONE;
897f0bf7f61SHarald Welte goto out;
898f0bf7f61SHarald Welte }
899f0bf7f61SHarald Welte
900f0bf7f61SHarald Welte if (sd_status & VIA_CRDR_SDSTS_CIR) {
901f0bf7f61SHarald Welte writew(sd_status & VIA_CRDR_SDSTS_CIR,
902f0bf7f61SHarald Welte addrbase + VIA_CRDR_SDSTATUS);
903f0bf7f61SHarald Welte
904f0bf7f61SHarald Welte schedule_work(&sdhost->carddet_work);
905f0bf7f61SHarald Welte }
906f0bf7f61SHarald Welte
907f0bf7f61SHarald Welte sd_status &= ~VIA_CRDR_SDSTS_CIR;
908f0bf7f61SHarald Welte if (sd_status & VIA_CRDR_SDSTS_CMD_MASK) {
909f0bf7f61SHarald Welte writew(sd_status & VIA_CRDR_SDSTS_CMD_MASK,
910f0bf7f61SHarald Welte addrbase + VIA_CRDR_SDSTATUS);
911f0bf7f61SHarald Welte via_sdc_cmd_isr(sdhost, sd_status & VIA_CRDR_SDSTS_CMD_MASK);
912f0bf7f61SHarald Welte }
913f0bf7f61SHarald Welte if (sd_status & VIA_CRDR_SDSTS_DATA_MASK) {
914f0bf7f61SHarald Welte writew(sd_status & VIA_CRDR_SDSTS_DATA_MASK,
915f0bf7f61SHarald Welte addrbase + VIA_CRDR_SDSTATUS);
916f0bf7f61SHarald Welte via_sdc_data_isr(sdhost, sd_status & VIA_CRDR_SDSTS_DATA_MASK);
917f0bf7f61SHarald Welte }
918f0bf7f61SHarald Welte
919f0bf7f61SHarald Welte sd_status &= ~(VIA_CRDR_SDSTS_CMD_MASK | VIA_CRDR_SDSTS_DATA_MASK);
920f0bf7f61SHarald Welte if (sd_status) {
921f0bf7f61SHarald Welte pr_err("%s: Unexpected interrupt 0x%x\n",
922f0bf7f61SHarald Welte mmc_hostname(sdhost->mmc), sd_status);
923f0bf7f61SHarald Welte writew(sd_status, addrbase + VIA_CRDR_SDSTATUS);
924f0bf7f61SHarald Welte }
925f0bf7f61SHarald Welte
926f0bf7f61SHarald Welte result = IRQ_HANDLED;
927f0bf7f61SHarald Welte
928f0bf7f61SHarald Welte out:
929f0bf7f61SHarald Welte spin_unlock(&sdhost->lock);
930f0bf7f61SHarald Welte
931f0bf7f61SHarald Welte return result;
932f0bf7f61SHarald Welte }
933f0bf7f61SHarald Welte
via_sdc_timeout(struct timer_list * t)9342ee4f620SKees Cook static void via_sdc_timeout(struct timer_list *t)
935f0bf7f61SHarald Welte {
936f0bf7f61SHarald Welte struct via_crdr_mmc_host *sdhost;
937f0bf7f61SHarald Welte unsigned long flags;
938f0bf7f61SHarald Welte
9392ee4f620SKees Cook sdhost = from_timer(sdhost, t, timer);
940f0bf7f61SHarald Welte
941f0bf7f61SHarald Welte spin_lock_irqsave(&sdhost->lock, flags);
942f0bf7f61SHarald Welte
943f0bf7f61SHarald Welte if (sdhost->mrq) {
944f0bf7f61SHarald Welte pr_err("%s: Timeout waiting for hardware interrupt."
945f0bf7f61SHarald Welte "cmd:0x%x\n", mmc_hostname(sdhost->mmc),
946f0bf7f61SHarald Welte sdhost->mrq->cmd->opcode);
947f0bf7f61SHarald Welte
948f0bf7f61SHarald Welte if (sdhost->data) {
949f0bf7f61SHarald Welte writel(VIA_CRDR_DMACTRL_SFTRST,
950f0bf7f61SHarald Welte sdhost->ddma_mmiobase + VIA_CRDR_DMACTRL);
951f0bf7f61SHarald Welte sdhost->data->error = -ETIMEDOUT;
952f0bf7f61SHarald Welte via_sdc_finish_data(sdhost);
953f0bf7f61SHarald Welte } else {
954f0bf7f61SHarald Welte if (sdhost->cmd)
955f0bf7f61SHarald Welte sdhost->cmd->error = -ETIMEDOUT;
956f0bf7f61SHarald Welte else
957f0bf7f61SHarald Welte sdhost->mrq->cmd->error = -ETIMEDOUT;
958f0bf7f61SHarald Welte tasklet_schedule(&sdhost->finish_tasklet);
959f0bf7f61SHarald Welte }
960f0bf7f61SHarald Welte }
961f0bf7f61SHarald Welte
962f0bf7f61SHarald Welte spin_unlock_irqrestore(&sdhost->lock, flags);
963f0bf7f61SHarald Welte }
964f0bf7f61SHarald Welte
via_sdc_tasklet_finish(struct tasklet_struct * t)965e821afd8SEmil Renner Berthing static void via_sdc_tasklet_finish(struct tasklet_struct *t)
966f0bf7f61SHarald Welte {
967e821afd8SEmil Renner Berthing struct via_crdr_mmc_host *host = from_tasklet(host, t, finish_tasklet);
968f0bf7f61SHarald Welte unsigned long flags;
969f0bf7f61SHarald Welte struct mmc_request *mrq;
970f0bf7f61SHarald Welte
971f0bf7f61SHarald Welte spin_lock_irqsave(&host->lock, flags);
972f0bf7f61SHarald Welte
973f0bf7f61SHarald Welte del_timer(&host->timer);
974f0bf7f61SHarald Welte mrq = host->mrq;
975f0bf7f61SHarald Welte host->mrq = NULL;
976f0bf7f61SHarald Welte host->cmd = NULL;
977f0bf7f61SHarald Welte host->data = NULL;
978f0bf7f61SHarald Welte
979f0bf7f61SHarald Welte spin_unlock_irqrestore(&host->lock, flags);
980f0bf7f61SHarald Welte
981f0bf7f61SHarald Welte mmc_request_done(host->mmc, mrq);
982f0bf7f61SHarald Welte }
983f0bf7f61SHarald Welte
via_sdc_card_detect(struct work_struct * work)984f0bf7f61SHarald Welte static void via_sdc_card_detect(struct work_struct *work)
985f0bf7f61SHarald Welte {
986f0bf7f61SHarald Welte struct via_crdr_mmc_host *host;
987f0bf7f61SHarald Welte void __iomem *addrbase;
988f0bf7f61SHarald Welte unsigned long flags;
989f0bf7f61SHarald Welte u16 status;
990f0bf7f61SHarald Welte
991f0bf7f61SHarald Welte host = container_of(work, struct via_crdr_mmc_host, carddet_work);
992f0bf7f61SHarald Welte
993f0bf7f61SHarald Welte addrbase = host->ddma_mmiobase;
994f0bf7f61SHarald Welte writel(VIA_CRDR_DMACTRL_SFTRST, addrbase + VIA_CRDR_DMACTRL);
995f0bf7f61SHarald Welte
996f0bf7f61SHarald Welte spin_lock_irqsave(&host->lock, flags);
997f0bf7f61SHarald Welte
998f0bf7f61SHarald Welte addrbase = host->pcictrl_mmiobase;
999f0bf7f61SHarald Welte writeb(VIA_CRDR_PCIDMACLK_SDC, addrbase + VIA_CRDR_PCIDMACLK);
1000f0bf7f61SHarald Welte
1001f0bf7f61SHarald Welte addrbase = host->sdhc_mmiobase;
1002f0bf7f61SHarald Welte status = readw(addrbase + VIA_CRDR_SDSTATUS);
1003f0bf7f61SHarald Welte if (!(status & VIA_CRDR_SDSTS_SLOTG)) {
1004f0bf7f61SHarald Welte if (host->mrq) {
1005f0bf7f61SHarald Welte pr_err("%s: Card removed during transfer!\n",
1006f0bf7f61SHarald Welte mmc_hostname(host->mmc));
1007f0bf7f61SHarald Welte host->mrq->cmd->error = -ENOMEDIUM;
1008f0bf7f61SHarald Welte tasklet_schedule(&host->finish_tasklet);
1009f0bf7f61SHarald Welte }
1010f0bf7f61SHarald Welte
1011f0bf7f61SHarald Welte spin_unlock_irqrestore(&host->lock, flags);
1012f0bf7f61SHarald Welte
1013f0bf7f61SHarald Welte via_reset_pcictrl(host);
1014f0bf7f61SHarald Welte
1015f0bf7f61SHarald Welte spin_lock_irqsave(&host->lock, flags);
1016f0bf7f61SHarald Welte }
1017f0bf7f61SHarald Welte
1018f0bf7f61SHarald Welte spin_unlock_irqrestore(&host->lock, flags);
1019f0bf7f61SHarald Welte
1020f0bf7f61SHarald Welte via_print_pcictrl(host);
1021f0bf7f61SHarald Welte via_print_sdchc(host);
1022f0bf7f61SHarald Welte
1023f0bf7f61SHarald Welte mmc_detect_change(host->mmc, msecs_to_jiffies(500));
1024f0bf7f61SHarald Welte }
1025f0bf7f61SHarald Welte
via_init_mmc_host(struct via_crdr_mmc_host * host)1026f0bf7f61SHarald Welte static void via_init_mmc_host(struct via_crdr_mmc_host *host)
1027f0bf7f61SHarald Welte {
1028f0bf7f61SHarald Welte struct mmc_host *mmc = host->mmc;
1029f0bf7f61SHarald Welte void __iomem *addrbase;
1030f0bf7f61SHarald Welte u32 lenreg;
1031f0bf7f61SHarald Welte u32 status;
1032f0bf7f61SHarald Welte
10332ee4f620SKees Cook timer_setup(&host->timer, via_sdc_timeout, 0);
1034f0bf7f61SHarald Welte
1035f0bf7f61SHarald Welte spin_lock_init(&host->lock);
1036f0bf7f61SHarald Welte
1037f0bf7f61SHarald Welte mmc->f_min = VIA_CRDR_MIN_CLOCK;
1038f0bf7f61SHarald Welte mmc->f_max = VIA_CRDR_MAX_CLOCK;
1039f0bf7f61SHarald Welte mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
1040f0bf7f61SHarald Welte mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SD_HIGHSPEED;
1041f0bf7f61SHarald Welte mmc->ops = &via_sdc_ops;
1042f0bf7f61SHarald Welte
1043f0bf7f61SHarald Welte /*Hardware cannot do scatter lists*/
1044a36274e0SMartin K. Petersen mmc->max_segs = 1;
1045f0bf7f61SHarald Welte
1046f0bf7f61SHarald Welte mmc->max_blk_size = VIA_CRDR_MAX_BLOCK_LENGTH;
1047f0bf7f61SHarald Welte mmc->max_blk_count = VIA_CRDR_MAX_BLOCK_COUNT;
1048f0bf7f61SHarald Welte
1049f0bf7f61SHarald Welte mmc->max_seg_size = mmc->max_blk_size * mmc->max_blk_count;
1050f0bf7f61SHarald Welte mmc->max_req_size = mmc->max_seg_size;
1051f0bf7f61SHarald Welte
1052f0bf7f61SHarald Welte INIT_WORK(&host->carddet_work, via_sdc_card_detect);
1053f0bf7f61SHarald Welte
1054e821afd8SEmil Renner Berthing tasklet_setup(&host->finish_tasklet, via_sdc_tasklet_finish);
1055f0bf7f61SHarald Welte
1056f0bf7f61SHarald Welte addrbase = host->sdhc_mmiobase;
1057f0bf7f61SHarald Welte writel(0x0, addrbase + VIA_CRDR_SDINTMASK);
1058f0bf7f61SHarald Welte msleep(1);
1059f0bf7f61SHarald Welte
1060f0bf7f61SHarald Welte lenreg = VIA_CRDR_SDBLKLEN_GPIDET | VIA_CRDR_SDBLKLEN_INTEN;
1061f0bf7f61SHarald Welte writel(lenreg, addrbase + VIA_CRDR_SDBLKLEN);
1062f0bf7f61SHarald Welte
1063f0bf7f61SHarald Welte status = readw(addrbase + VIA_CRDR_SDSTATUS);
1064f0bf7f61SHarald Welte status &= VIA_CRDR_SDSTS_W1C_MASK;
1065f0bf7f61SHarald Welte writew(status, addrbase + VIA_CRDR_SDSTATUS);
1066f0bf7f61SHarald Welte
1067f0bf7f61SHarald Welte status = readw(addrbase + VIA_CRDR_SDSTATUS2);
1068f0bf7f61SHarald Welte status |= VIA_CRDR_SDSTS_CFE;
1069f0bf7f61SHarald Welte writew(status, addrbase + VIA_CRDR_SDSTATUS2);
1070f0bf7f61SHarald Welte
1071f0bf7f61SHarald Welte writeb(0x0, addrbase + VIA_CRDR_SDEXTCTRL);
1072f0bf7f61SHarald Welte
1073f0bf7f61SHarald Welte writel(VIA_CRDR_SDACTIVE_INTMASK, addrbase + VIA_CRDR_SDINTMASK);
1074f0bf7f61SHarald Welte msleep(1);
1075f0bf7f61SHarald Welte }
1076f0bf7f61SHarald Welte
via_sd_probe(struct pci_dev * pcidev,const struct pci_device_id * id)1077c3be1efdSBill Pemberton static int via_sd_probe(struct pci_dev *pcidev,
1078f0bf7f61SHarald Welte const struct pci_device_id *id)
1079f0bf7f61SHarald Welte {
1080f0bf7f61SHarald Welte struct mmc_host *mmc;
1081f0bf7f61SHarald Welte struct via_crdr_mmc_host *sdhost;
1082f0bf7f61SHarald Welte u32 base, len;
1083cf5e23e1SSergei Shtylyov u8 gatt;
1084f0bf7f61SHarald Welte int ret;
1085f0bf7f61SHarald Welte
1086f0bf7f61SHarald Welte pr_info(DRV_NAME
1087f0bf7f61SHarald Welte ": VIA SDMMC controller found at %s [%04x:%04x] (rev %x)\n",
1088f0bf7f61SHarald Welte pci_name(pcidev), (int)pcidev->vendor, (int)pcidev->device,
1089cf5e23e1SSergei Shtylyov (int)pcidev->revision);
1090f0bf7f61SHarald Welte
1091f0bf7f61SHarald Welte ret = pci_enable_device(pcidev);
1092f0bf7f61SHarald Welte if (ret)
1093f0bf7f61SHarald Welte return ret;
1094f0bf7f61SHarald Welte
1095f0bf7f61SHarald Welte ret = pci_request_regions(pcidev, DRV_NAME);
1096f0bf7f61SHarald Welte if (ret)
1097f0bf7f61SHarald Welte goto disable;
1098f0bf7f61SHarald Welte
1099f0bf7f61SHarald Welte pci_write_config_byte(pcidev, VIA_CRDR_PCI_WORK_MODE, 0);
1100f0bf7f61SHarald Welte pci_write_config_byte(pcidev, VIA_CRDR_PCI_DBG_MODE, 0);
1101f0bf7f61SHarald Welte
1102f0bf7f61SHarald Welte mmc = mmc_alloc_host(sizeof(struct via_crdr_mmc_host), &pcidev->dev);
1103f0bf7f61SHarald Welte if (!mmc) {
1104f0bf7f61SHarald Welte ret = -ENOMEM;
1105f0bf7f61SHarald Welte goto release;
1106f0bf7f61SHarald Welte }
1107f0bf7f61SHarald Welte
1108f0bf7f61SHarald Welte sdhost = mmc_priv(mmc);
1109f0bf7f61SHarald Welte sdhost->mmc = mmc;
1110f0bf7f61SHarald Welte dev_set_drvdata(&pcidev->dev, sdhost);
1111f0bf7f61SHarald Welte
1112f0bf7f61SHarald Welte len = pci_resource_len(pcidev, 0);
1113f0bf7f61SHarald Welte base = pci_resource_start(pcidev, 0);
11144bdc0d67SChristoph Hellwig sdhost->mmiobase = ioremap(base, len);
1115f0bf7f61SHarald Welte if (!sdhost->mmiobase) {
1116f0bf7f61SHarald Welte ret = -ENOMEM;
1117f0bf7f61SHarald Welte goto free_mmc_host;
1118f0bf7f61SHarald Welte }
1119f0bf7f61SHarald Welte
1120f0bf7f61SHarald Welte sdhost->sdhc_mmiobase =
1121f0bf7f61SHarald Welte sdhost->mmiobase + VIA_CRDR_SDC_OFF;
1122f0bf7f61SHarald Welte sdhost->ddma_mmiobase =
1123f0bf7f61SHarald Welte sdhost->mmiobase + VIA_CRDR_DDMA_OFF;
1124f0bf7f61SHarald Welte sdhost->pcictrl_mmiobase =
1125f0bf7f61SHarald Welte sdhost->mmiobase + VIA_CRDR_PCICTRL_OFF;
1126f0bf7f61SHarald Welte
1127f0bf7f61SHarald Welte sdhost->power = MMC_VDD_165_195;
1128f0bf7f61SHarald Welte
1129f0bf7f61SHarald Welte gatt = VIA_CRDR_PCICLKGATT_3V3 | VIA_CRDR_PCICLKGATT_PAD_PWRON;
1130f0bf7f61SHarald Welte writeb(gatt, sdhost->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
1131f0bf7f61SHarald Welte via_pwron_sleep(sdhost);
1132f0bf7f61SHarald Welte gatt |= VIA_CRDR_PCICLKGATT_SFTRST;
1133f0bf7f61SHarald Welte writeb(gatt, sdhost->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
1134f0bf7f61SHarald Welte msleep(3);
1135f0bf7f61SHarald Welte
1136f0bf7f61SHarald Welte via_init_mmc_host(sdhost);
1137f0bf7f61SHarald Welte
1138f0bf7f61SHarald Welte ret =
1139f0bf7f61SHarald Welte request_irq(pcidev->irq, via_sdc_isr, IRQF_SHARED, DRV_NAME,
1140f0bf7f61SHarald Welte sdhost);
1141f0bf7f61SHarald Welte if (ret)
1142f0bf7f61SHarald Welte goto unmap;
1143f0bf7f61SHarald Welte
1144f0bf7f61SHarald Welte writeb(VIA_CRDR_PCIINTCTRL_SDCIRQEN,
1145f0bf7f61SHarald Welte sdhost->pcictrl_mmiobase + VIA_CRDR_PCIINTCTRL);
1146f0bf7f61SHarald Welte writeb(VIA_CRDR_PCITMOCTRL_1024MS,
1147f0bf7f61SHarald Welte sdhost->pcictrl_mmiobase + VIA_CRDR_PCITMOCTRL);
1148f0bf7f61SHarald Welte
1149f0bf7f61SHarald Welte /* device-specific quirks */
1150f0bf7f61SHarald Welte if (pcidev->subsystem_vendor == PCI_VENDOR_ID_LENOVO &&
1151f0bf7f61SHarald Welte pcidev->subsystem_device == 0x3891)
1152f0bf7f61SHarald Welte sdhost->quirks = VIA_CRDR_QUIRK_300MS_PWRDELAY;
1153f0bf7f61SHarald Welte
1154*e4e46fb6SYang Yingliang ret = mmc_add_host(mmc);
1155*e4e46fb6SYang Yingliang if (ret)
1156*e4e46fb6SYang Yingliang goto unmap;
1157f0bf7f61SHarald Welte
1158f0bf7f61SHarald Welte return 0;
1159f0bf7f61SHarald Welte
1160f0bf7f61SHarald Welte unmap:
1161f0bf7f61SHarald Welte iounmap(sdhost->mmiobase);
1162f0bf7f61SHarald Welte free_mmc_host:
1163f0bf7f61SHarald Welte mmc_free_host(mmc);
1164f0bf7f61SHarald Welte release:
1165f0bf7f61SHarald Welte pci_release_regions(pcidev);
1166f0bf7f61SHarald Welte disable:
1167f0bf7f61SHarald Welte pci_disable_device(pcidev);
1168f0bf7f61SHarald Welte
1169f0bf7f61SHarald Welte return ret;
1170f0bf7f61SHarald Welte }
1171f0bf7f61SHarald Welte
via_sd_remove(struct pci_dev * pcidev)11726e0ee714SBill Pemberton static void via_sd_remove(struct pci_dev *pcidev)
1173f0bf7f61SHarald Welte {
1174f0bf7f61SHarald Welte struct via_crdr_mmc_host *sdhost = pci_get_drvdata(pcidev);
1175f0bf7f61SHarald Welte unsigned long flags;
1176f0bf7f61SHarald Welte u8 gatt;
1177f0bf7f61SHarald Welte
1178f0bf7f61SHarald Welte spin_lock_irqsave(&sdhost->lock, flags);
1179f0bf7f61SHarald Welte
1180f0bf7f61SHarald Welte /* Ensure we don't accept more commands from mmc layer */
1181f0bf7f61SHarald Welte sdhost->reject = 1;
1182f0bf7f61SHarald Welte
1183f0bf7f61SHarald Welte /* Disable generating further interrupts */
1184f0bf7f61SHarald Welte writeb(0x0, sdhost->pcictrl_mmiobase + VIA_CRDR_PCIINTCTRL);
1185f0bf7f61SHarald Welte
1186f0bf7f61SHarald Welte if (sdhost->mrq) {
1187a3c76eb9SGirish K S pr_err("%s: Controller removed during "
1188f0bf7f61SHarald Welte "transfer\n", mmc_hostname(sdhost->mmc));
1189f0bf7f61SHarald Welte
1190f0bf7f61SHarald Welte /* make sure all DMA is stopped */
1191f0bf7f61SHarald Welte writel(VIA_CRDR_DMACTRL_SFTRST,
1192f0bf7f61SHarald Welte sdhost->ddma_mmiobase + VIA_CRDR_DMACTRL);
1193f0bf7f61SHarald Welte sdhost->mrq->cmd->error = -ENOMEDIUM;
1194f0bf7f61SHarald Welte if (sdhost->mrq->stop)
1195f0bf7f61SHarald Welte sdhost->mrq->stop->error = -ENOMEDIUM;
1196f0bf7f61SHarald Welte tasklet_schedule(&sdhost->finish_tasklet);
1197f0bf7f61SHarald Welte }
1198f0bf7f61SHarald Welte spin_unlock_irqrestore(&sdhost->lock, flags);
1199f0bf7f61SHarald Welte
1200f0bf7f61SHarald Welte mmc_remove_host(sdhost->mmc);
1201f0bf7f61SHarald Welte
1202f0bf7f61SHarald Welte free_irq(pcidev->irq, sdhost);
1203f0bf7f61SHarald Welte
1204f0bf7f61SHarald Welte del_timer_sync(&sdhost->timer);
1205f0bf7f61SHarald Welte
1206f0bf7f61SHarald Welte tasklet_kill(&sdhost->finish_tasklet);
1207f0bf7f61SHarald Welte
1208f0bf7f61SHarald Welte /* switch off power */
1209f0bf7f61SHarald Welte gatt = readb(sdhost->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
1210f0bf7f61SHarald Welte gatt &= ~VIA_CRDR_PCICLKGATT_PAD_PWRON;
1211f0bf7f61SHarald Welte writeb(gatt, sdhost->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
1212f0bf7f61SHarald Welte
1213f0bf7f61SHarald Welte iounmap(sdhost->mmiobase);
1214f0bf7f61SHarald Welte mmc_free_host(sdhost->mmc);
1215f0bf7f61SHarald Welte pci_release_regions(pcidev);
1216f0bf7f61SHarald Welte pci_disable_device(pcidev);
1217f0bf7f61SHarald Welte
1218f0bf7f61SHarald Welte pr_info(DRV_NAME
1219f0bf7f61SHarald Welte ": VIA SDMMC controller at %s [%04x:%04x] has been removed\n",
1220f0bf7f61SHarald Welte pci_name(pcidev), (int)pcidev->vendor, (int)pcidev->device);
1221f0bf7f61SHarald Welte }
1222f0bf7f61SHarald Welte
via_init_sdc_pm(struct via_crdr_mmc_host * host)12230d776e5bSVaibhav Gupta static void __maybe_unused via_init_sdc_pm(struct via_crdr_mmc_host *host)
1224f0bf7f61SHarald Welte {
1225f0bf7f61SHarald Welte struct sdhcreg *pm_sdhcreg;
1226f0bf7f61SHarald Welte void __iomem *addrbase;
1227f0bf7f61SHarald Welte u32 lenreg;
1228f0bf7f61SHarald Welte u16 status;
1229f0bf7f61SHarald Welte
1230f0bf7f61SHarald Welte pm_sdhcreg = &(host->pm_sdhc_reg);
1231f0bf7f61SHarald Welte addrbase = host->sdhc_mmiobase;
1232f0bf7f61SHarald Welte
1233f0bf7f61SHarald Welte writel(0x0, addrbase + VIA_CRDR_SDINTMASK);
1234f0bf7f61SHarald Welte
1235f0bf7f61SHarald Welte lenreg = VIA_CRDR_SDBLKLEN_GPIDET | VIA_CRDR_SDBLKLEN_INTEN;
1236f0bf7f61SHarald Welte writel(lenreg, addrbase + VIA_CRDR_SDBLKLEN);
1237f0bf7f61SHarald Welte
1238f0bf7f61SHarald Welte status = readw(addrbase + VIA_CRDR_SDSTATUS);
1239f0bf7f61SHarald Welte status &= VIA_CRDR_SDSTS_W1C_MASK;
1240f0bf7f61SHarald Welte writew(status, addrbase + VIA_CRDR_SDSTATUS);
1241f0bf7f61SHarald Welte
1242f0bf7f61SHarald Welte status = readw(addrbase + VIA_CRDR_SDSTATUS2);
1243f0bf7f61SHarald Welte status |= VIA_CRDR_SDSTS_CFE;
1244f0bf7f61SHarald Welte writew(status, addrbase + VIA_CRDR_SDSTATUS2);
1245f0bf7f61SHarald Welte
1246f0bf7f61SHarald Welte writel(pm_sdhcreg->sdcontrol_reg, addrbase + VIA_CRDR_SDCTRL);
1247f0bf7f61SHarald Welte writel(pm_sdhcreg->sdcmdarg_reg, addrbase + VIA_CRDR_SDCARG);
1248f0bf7f61SHarald Welte writel(pm_sdhcreg->sdintmask_reg, addrbase + VIA_CRDR_SDINTMASK);
1249f0bf7f61SHarald Welte writel(pm_sdhcreg->sdrsptmo_reg, addrbase + VIA_CRDR_SDRSPTMO);
1250f0bf7f61SHarald Welte writel(pm_sdhcreg->sdclksel_reg, addrbase + VIA_CRDR_SDCLKSEL);
1251f0bf7f61SHarald Welte writel(pm_sdhcreg->sdextctrl_reg, addrbase + VIA_CRDR_SDEXTCTRL);
1252f0bf7f61SHarald Welte
1253f0bf7f61SHarald Welte via_print_pcictrl(host);
1254f0bf7f61SHarald Welte via_print_sdchc(host);
1255f0bf7f61SHarald Welte }
1256f0bf7f61SHarald Welte
via_sd_suspend(struct device * dev)12570d776e5bSVaibhav Gupta static int __maybe_unused via_sd_suspend(struct device *dev)
1258f0bf7f61SHarald Welte {
1259f0bf7f61SHarald Welte struct via_crdr_mmc_host *host;
126087d7ad08SMadhuparna Bhowmik unsigned long flags;
1261f0bf7f61SHarald Welte
12620d776e5bSVaibhav Gupta host = dev_get_drvdata(dev);
1263f0bf7f61SHarald Welte
126487d7ad08SMadhuparna Bhowmik spin_lock_irqsave(&host->lock, flags);
1265f0bf7f61SHarald Welte via_save_pcictrlreg(host);
1266f0bf7f61SHarald Welte via_save_sdcreg(host);
126787d7ad08SMadhuparna Bhowmik spin_unlock_irqrestore(&host->lock, flags);
1268f0bf7f61SHarald Welte
12690d776e5bSVaibhav Gupta device_wakeup_enable(dev);
1270f0bf7f61SHarald Welte
1271ccad9b97SUlf Hansson return 0;
1272f0bf7f61SHarald Welte }
1273f0bf7f61SHarald Welte
via_sd_resume(struct device * dev)12740d776e5bSVaibhav Gupta static int __maybe_unused via_sd_resume(struct device *dev)
1275f0bf7f61SHarald Welte {
1276f0bf7f61SHarald Welte struct via_crdr_mmc_host *sdhost;
1277f0bf7f61SHarald Welte u8 gatt;
1278f0bf7f61SHarald Welte
12790d776e5bSVaibhav Gupta sdhost = dev_get_drvdata(dev);
1280f0bf7f61SHarald Welte
1281f0bf7f61SHarald Welte gatt = VIA_CRDR_PCICLKGATT_PAD_PWRON;
1282f0bf7f61SHarald Welte if (sdhost->power == MMC_VDD_165_195)
1283f0bf7f61SHarald Welte gatt &= ~VIA_CRDR_PCICLKGATT_3V3;
1284f0bf7f61SHarald Welte else
1285f0bf7f61SHarald Welte gatt |= VIA_CRDR_PCICLKGATT_3V3;
1286f0bf7f61SHarald Welte writeb(gatt, sdhost->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
1287f0bf7f61SHarald Welte via_pwron_sleep(sdhost);
1288f0bf7f61SHarald Welte gatt |= VIA_CRDR_PCICLKGATT_SFTRST;
1289f0bf7f61SHarald Welte writeb(gatt, sdhost->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
1290f0bf7f61SHarald Welte msleep(3);
1291f0bf7f61SHarald Welte
1292f0bf7f61SHarald Welte msleep(100);
1293f0bf7f61SHarald Welte
1294f0bf7f61SHarald Welte via_restore_pcictrlreg(sdhost);
1295f0bf7f61SHarald Welte via_init_sdc_pm(sdhost);
1296f0bf7f61SHarald Welte
12973a75283aSYang Li return 0;
1298f0bf7f61SHarald Welte }
1299f0bf7f61SHarald Welte
13000d776e5bSVaibhav Gupta static SIMPLE_DEV_PM_OPS(via_sd_pm_ops, via_sd_suspend, via_sd_resume);
1301f0bf7f61SHarald Welte
1302f0bf7f61SHarald Welte static struct pci_driver via_sd_driver = {
1303f0bf7f61SHarald Welte .name = DRV_NAME,
1304f0bf7f61SHarald Welte .id_table = via_ids,
1305f0bf7f61SHarald Welte .probe = via_sd_probe,
13060433c143SBill Pemberton .remove = via_sd_remove,
13070d776e5bSVaibhav Gupta .driver.pm = &via_sd_pm_ops,
1308f0bf7f61SHarald Welte };
1309f0bf7f61SHarald Welte
13100d4de8f5SSachin Kamat module_pci_driver(via_sd_driver);
1311f0bf7f61SHarald Welte
1312f0bf7f61SHarald Welte MODULE_LICENSE("GPL");
1313f0bf7f61SHarald Welte MODULE_AUTHOR("VIA Technologies Inc.");
1314f0bf7f61SHarald Welte MODULE_DESCRIPTION("VIA SD/MMC Card Interface driver");
1315