s3c2440_sdi.c revision 1.1.6.2 1 1.1.6.2 yamt /*-
2 1.1.6.2 yamt * Copyright (c) 2012 The NetBSD Foundation, Inc.
3 1.1.6.2 yamt * All rights reserved.
4 1.1.6.2 yamt *
5 1.1.6.2 yamt * This code is derived from software contributed to The NetBSD Foundation
6 1.1.6.2 yamt * by Paul Fleischer <paul (at) xpg.dk>
7 1.1.6.2 yamt *
8 1.1.6.2 yamt * Redistribution and use in source and binary forms, with or without
9 1.1.6.2 yamt * modification, are permitted provided that the following conditions
10 1.1.6.2 yamt * are met:
11 1.1.6.2 yamt * 1. Redistributions of source code must retain the above copyright
12 1.1.6.2 yamt * notice, this list of conditions and the following disclaimer.
13 1.1.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
14 1.1.6.2 yamt * notice, this list of conditions and the following disclaimer in the
15 1.1.6.2 yamt * documentation and/or other materials provided with the distribution.
16 1.1.6.2 yamt *
17 1.1.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.1.6.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1.6.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1.6.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.1.6.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.1.6.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.1.6.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1.6.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.1.6.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.1.6.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.1.6.2 yamt * POSSIBILITY OF SUCH DAMAGE.
28 1.1.6.2 yamt */
29 1.1.6.2 yamt #include <sys/cdefs.h>
30 1.1.6.2 yamt
31 1.1.6.2 yamt #include <sys/param.h>
32 1.1.6.2 yamt #include <sys/kernel.h>
33 1.1.6.2 yamt #include <sys/systm.h>
34 1.1.6.2 yamt #include <sys/conf.h>
35 1.1.6.2 yamt #include <sys/malloc.h> /* For M_NOWAIT*/
36 1.1.6.2 yamt
37 1.1.6.2 yamt #include <sys/mutex.h>
38 1.1.6.2 yamt #include <sys/condvar.h>
39 1.1.6.2 yamt
40 1.1.6.2 yamt #include <sys/bus.h>
41 1.1.6.2 yamt #include <machine/cpu.h>
42 1.1.6.2 yamt
43 1.1.6.2 yamt #include <arm/s3c2xx0/s3c24x0var.h>
44 1.1.6.2 yamt #include <arm/s3c2xx0/s3c2440var.h>
45 1.1.6.2 yamt #include <arm/s3c2xx0/s3c24x0reg.h>
46 1.1.6.2 yamt #include <arm/s3c2xx0/s3c2440reg.h>
47 1.1.6.2 yamt #include <arm/s3c2xx0/s3c2440_dma.h>
48 1.1.6.2 yamt
49 1.1.6.2 yamt //#include <arm/s3c2xx0/s3c2440_sdi.h>
50 1.1.6.2 yamt
51 1.1.6.2 yamt #include <dev/sdmmc/sdmmcchip.h>
52 1.1.6.2 yamt #include <dev/sdmmc/sdmmcvar.h>
53 1.1.6.2 yamt
54 1.1.6.2 yamt #include <uvm/uvm_extern.h>
55 1.1.6.2 yamt /*#define SSSDI_DEBUG*/
56 1.1.6.2 yamt #ifdef SSSDI_DEBUG
57 1.1.6.2 yamt #define DPRINTF(s) do {printf s; } while (/*CONSTCOND*/0)
58 1.1.6.2 yamt #else
59 1.1.6.2 yamt #define DPRINTF(s) do {} while (/*CONSTCOND*/0)
60 1.1.6.2 yamt #endif
61 1.1.6.2 yamt
62 1.1.6.2 yamt struct sssdi_softc {
63 1.1.6.2 yamt device_t dev;
64 1.1.6.2 yamt
65 1.1.6.2 yamt bus_space_tag_t iot;
66 1.1.6.2 yamt
67 1.1.6.2 yamt bus_space_handle_t ioh;
68 1.1.6.2 yamt bus_space_handle_t card_ioh; /* Card detect I/O*/
69 1.1.6.2 yamt
70 1.1.6.2 yamt device_t sdmmc;
71 1.1.6.2 yamt
72 1.1.6.2 yamt uint32_t caps;
73 1.1.6.2 yamt
74 1.1.6.2 yamt int width; /* Transfer width */
75 1.1.6.2 yamt void *sc_ih; /* SSSDI Interrupt handler */
76 1.1.6.2 yamt
77 1.1.6.2 yamt struct kmutex intr_mtx;
78 1.1.6.2 yamt struct kcondvar intr_cv;
79 1.1.6.2 yamt uint32_t intr_status; /* Set by the interrupt handler */
80 1.1.6.2 yamt
81 1.1.6.2 yamt dmac_xfer_t sc_xfer;
82 1.1.6.2 yamt
83 1.1.6.2 yamt bus_dma_segment_t sc_dr;
84 1.1.6.2 yamt };
85 1.1.6.2 yamt
86 1.1.6.2 yamt /* Basic driver stuff */
87 1.1.6.2 yamt static int sssdi_match(struct device *, struct cfdata *, void *);
88 1.1.6.2 yamt static void sssdi_attach(struct device *, struct device *, void *);
89 1.1.6.2 yamt //static int sssdi_search(struct device *, struct cfdata *, const int *, void *);
90 1.1.6.2 yamt
91 1.1.6.2 yamt CFATTACH_DECL_NEW(sssdi, sizeof(struct sssdi_softc), sssdi_match, sssdi_attach,
92 1.1.6.2 yamt NULL, NULL);
93 1.1.6.2 yamt
94 1.1.6.2 yamt /* SD/MMC chip functions */
95 1.1.6.2 yamt static int sssdi_host_reset(sdmmc_chipset_handle_t);
96 1.1.6.2 yamt static uint32_t sssdi_host_ocr(sdmmc_chipset_handle_t);
97 1.1.6.2 yamt static int sssdi_maxblklen(sdmmc_chipset_handle_t);
98 1.1.6.2 yamt static int sssdi_card_detect(sdmmc_chipset_handle_t);
99 1.1.6.2 yamt static int sssdi_write_protect(sdmmc_chipset_handle_t);
100 1.1.6.2 yamt static int sssdi_bus_power(sdmmc_chipset_handle_t, uint32_t);
101 1.1.6.2 yamt static int sssdi_bus_clock(sdmmc_chipset_handle_t, int);
102 1.1.6.2 yamt static int sssdi_bus_width(sdmmc_chipset_handle_t, int);
103 1.1.6.2 yamt static int sssdi_bus_rod(sdmmc_chipset_handle_t, int);
104 1.1.6.2 yamt static void sssdi_exec_command(sdmmc_chipset_handle_t, struct sdmmc_command *);
105 1.1.6.2 yamt static void sssdi_card_enable_intr(sdmmc_chipset_handle_t, int);
106 1.1.6.2 yamt static void sssdi_card_intr_ack(sdmmc_chipset_handle_t);
107 1.1.6.2 yamt
108 1.1.6.2 yamt /* Interrupt Handlers */
109 1.1.6.2 yamt int sssdi_intr(void *arg);
110 1.1.6.2 yamt int sssdi_intr_card(void *arg);
111 1.1.6.2 yamt
112 1.1.6.2 yamt /* Interrupt helper functions */
113 1.1.6.2 yamt static void sssdi_enable_intr(struct sssdi_softc *, uint32_t );
114 1.1.6.2 yamt void sssdi_disable_intr(struct sssdi_softc *sc, uint32_t i);
115 1.1.6.2 yamt void sssdi_clear_intr(struct sssdi_softc *sc);
116 1.1.6.2 yamt static int sssdi_wait_intr(struct sssdi_softc *sc, uint32_t mask, int timeout);
117 1.1.6.2 yamt
118 1.1.6.2 yamt /* Programmed I/O transfer helpers */
119 1.1.6.2 yamt void sssdi_perform_pio_read(struct sssdi_softc *sc, struct sdmmc_command *cmd);
120 1.1.6.2 yamt void sssdi_perform_pio_write(struct sssdi_softc *sc, struct sdmmc_command *cmd);
121 1.1.6.2 yamt
122 1.1.6.2 yamt /* Interrupt helper defines */
123 1.1.6.2 yamt #define SDI_CMD_SENT SDIINTMASK_CMD_SENT
124 1.1.6.2 yamt #define SDI_CMD_TIMEOUT SDIINTMASK_CMD_TIMEOUT
125 1.1.6.2 yamt #define SDI_RESP_FIN SDIINTMASK_RESP
126 1.1.6.2 yamt #define SDI_FIFO_RX_FULL SDIINTMASK_RF_FULL
127 1.1.6.2 yamt #define SDI_FIFO_RX_LAST SDIINTMASK_RF_LAST
128 1.1.6.2 yamt #define SDI_FIFO_TX_EMPTY SDIINTMASK_TF_EMPTY
129 1.1.6.2 yamt #define SDI_DATA_FIN SDIINTMASK_DATA_FIN
130 1.1.6.2 yamt #define SDI_DATA_TIMEOUT SDIINTMASK_DATA_TIMEOUT
131 1.1.6.2 yamt
132 1.1.6.2 yamt /* Constants */
133 1.1.6.2 yamt #define SDI_DMA_WAIT_TIME 5000 /* ms */
134 1.1.6.2 yamt #define SDI_CMD_WAIT_TIME 5000 /* ms */
135 1.1.6.2 yamt
136 1.1.6.2 yamt /* SDMMC function structure */
137 1.1.6.2 yamt struct sdmmc_chip_functions sssdi_functions = {
138 1.1.6.2 yamt /* host controller reset */
139 1.1.6.2 yamt sssdi_host_reset,
140 1.1.6.2 yamt
141 1.1.6.2 yamt /* host capabilities */
142 1.1.6.2 yamt sssdi_host_ocr,
143 1.1.6.2 yamt sssdi_maxblklen,
144 1.1.6.2 yamt
145 1.1.6.2 yamt /* card detection */
146 1.1.6.2 yamt sssdi_card_detect,
147 1.1.6.2 yamt
148 1.1.6.2 yamt /* write protect */
149 1.1.6.2 yamt sssdi_write_protect,
150 1.1.6.2 yamt
151 1.1.6.2 yamt /* bus power, clock frequency and width */
152 1.1.6.2 yamt sssdi_bus_power,
153 1.1.6.2 yamt sssdi_bus_clock,
154 1.1.6.2 yamt sssdi_bus_width,
155 1.1.6.2 yamt sssdi_bus_rod,
156 1.1.6.2 yamt
157 1.1.6.2 yamt /* command execution */
158 1.1.6.2 yamt sssdi_exec_command,
159 1.1.6.2 yamt
160 1.1.6.2 yamt /* card interrupt */
161 1.1.6.2 yamt sssdi_card_enable_intr,
162 1.1.6.2 yamt sssdi_card_intr_ack
163 1.1.6.2 yamt };
164 1.1.6.2 yamt
165 1.1.6.2 yamt int
166 1.1.6.2 yamt sssdi_match(struct device *parent, struct cfdata *match, void *aux)
167 1.1.6.2 yamt {
168 1.1.6.2 yamt /* struct s3c2xx0_attach_args *sa = aux;*/
169 1.1.6.2 yamt
170 1.1.6.2 yamt /* Not sure how to match here, maybe CPU type? */
171 1.1.6.2 yamt return 1;
172 1.1.6.2 yamt }
173 1.1.6.2 yamt
174 1.1.6.2 yamt void
175 1.1.6.2 yamt sssdi_attach(struct device *parent, struct device *self, void *aux)
176 1.1.6.2 yamt {
177 1.1.6.2 yamt struct sssdi_softc *sc = device_private(self);
178 1.1.6.2 yamt struct s3c2xx0_attach_args *sa = (struct s3c2xx0_attach_args *)aux;
179 1.1.6.2 yamt struct sdmmcbus_attach_args saa;
180 1.1.6.2 yamt bus_space_tag_t iot = sa->sa_iot;
181 1.1.6.2 yamt uint32_t data;
182 1.1.6.2 yamt
183 1.1.6.2 yamt sc->dev = self;
184 1.1.6.2 yamt sc->iot = iot;
185 1.1.6.2 yamt
186 1.1.6.2 yamt if (bus_space_map(iot, S3C2440_SDI_BASE, S3C2440_SDI_SIZE, 0, &sc->ioh) ) {
187 1.1.6.2 yamt printf(": failed to map registers");
188 1.1.6.2 yamt return;
189 1.1.6.2 yamt }
190 1.1.6.2 yamt
191 1.1.6.2 yamt if (bus_space_map(iot, S3C2440_GPIO_BASE, S3C2440_GPIO_SIZE, 0, &sc->card_ioh) ) {
192 1.1.6.2 yamt printf(": failed to map GPIO memory for card detection");
193 1.1.6.2 yamt return;
194 1.1.6.2 yamt }
195 1.1.6.2 yamt
196 1.1.6.2 yamt /* Set GPG8 to EINT[16], as it is the card detect line. */
197 1.1.6.2 yamt data = bus_space_read_4(sc->iot, sc->card_ioh, GPIO_PGCON);
198 1.1.6.2 yamt data = GPIO_SET_FUNC(data, 8, 0x2);
199 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->card_ioh, GPIO_PGCON, data);
200 1.1.6.2 yamt
201 1.1.6.2 yamt /* Set GPH8 to input, as it is used to detect write protection. */
202 1.1.6.2 yamt data = bus_space_read_4(sc->iot, sc->card_ioh, GPIO_PHCON);
203 1.1.6.2 yamt data = GPIO_SET_FUNC(data, 8, 0x00);
204 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->card_ioh, GPIO_PHCON, data);
205 1.1.6.2 yamt
206 1.1.6.2 yamt mutex_init(&sc->intr_mtx, MUTEX_DEFAULT, IPL_SDMMC);
207 1.1.6.2 yamt
208 1.1.6.2 yamt cv_init(&sc->intr_cv, "s3c2440_sdiintr");
209 1.1.6.2 yamt sc->intr_status = 0;
210 1.1.6.2 yamt sc->caps = SMC_CAPS_4BIT_MODE | SMC_CAPS_DMA | SMC_CAPS_MULTI_SEG_DMA;
211 1.1.6.2 yamt
212 1.1.6.2 yamt memset(&saa, 0, sizeof(saa));
213 1.1.6.2 yamt saa.saa_busname = "sdmmc";
214 1.1.6.2 yamt saa.saa_sct = &sssdi_functions;
215 1.1.6.2 yamt saa.saa_sch = sc;
216 1.1.6.2 yamt saa.saa_dmat = sa->sa_dmat;
217 1.1.6.2 yamt saa.saa_clkmin = s3c2xx0_softc->sc_pclk / 256;
218 1.1.6.2 yamt saa.saa_clkmax = s3c2xx0_softc->sc_pclk / 1; /* PCLK/1 or PCLK/2 depending on how the spec is read */
219 1.1.6.2 yamt saa.saa_caps = sc->caps;
220 1.1.6.2 yamt
221 1.1.6.2 yamt /* Attach our interrupt handler */
222 1.1.6.2 yamt sc->sc_ih = s3c24x0_intr_establish(S3C2410_INT_SDI, IPL_SDMMC, IST_EDGE_RISING, sssdi_intr, sc);
223 1.1.6.2 yamt
224 1.1.6.2 yamt /* Attach interrupt handler to detect change in card status */
225 1.1.6.2 yamt s3c2440_extint_establish(16, IPL_SDMMC, IST_EDGE_BOTH, sssdi_intr_card, sc);
226 1.1.6.2 yamt
227 1.1.6.2 yamt data = bus_space_read_4(s3c2xx0_softc->sc_iot, s3c2xx0_softc->sc_clkman_ioh, CLKMAN_CLKCON);
228 1.1.6.2 yamt bus_space_write_4(s3c2xx0_softc->sc_iot, s3c2xx0_softc->sc_clkman_ioh, CLKMAN_CLKCON, data | CLKCON_SDI);
229 1.1.6.2 yamt
230 1.1.6.2 yamt (void) sssdi_host_reset(sc);
231 1.1.6.2 yamt
232 1.1.6.2 yamt printf("\n");
233 1.1.6.2 yamt
234 1.1.6.2 yamt /* Attach to the generic SD/MMC bus */
235 1.1.6.2 yamt /* Is it a good idea to get the private parts of sdmmc ? */
236 1.1.6.2 yamt sc->sdmmc = config_found(sc->dev, &saa, NULL);
237 1.1.6.2 yamt
238 1.1.6.2 yamt sc->sc_xfer = s3c2440_dmac_allocate_xfer(M_NOWAIT);
239 1.1.6.2 yamt sc->sc_dr.ds_addr = S3C2440_SDI_BASE+SDI_DAT_LI_W;
240 1.1.6.2 yamt sc->sc_dr.ds_len = 4;
241 1.1.6.2 yamt }
242 1.1.6.2 yamt
243 1.1.6.2 yamt int
244 1.1.6.2 yamt sssdi_host_reset(sdmmc_chipset_handle_t sch)
245 1.1.6.2 yamt {
246 1.1.6.2 yamt struct sssdi_softc *sc = (struct sssdi_softc*)sch;
247 1.1.6.2 yamt
248 1.1.6.2 yamt /* Note that we do not enable the clock just yet. */
249 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_CON, SDICON_SD_RESET |
250 1.1.6.2 yamt SDICON_CTYP_SD | SDICON_RCV_IO_INT);
251 1.1.6.2 yamt /* bus_space_write_4(sc->iot, sc->ioh, SDI_CMD_STA, SDICMDSTA_RSP_CRC | SDICMDSTA_CMD_SENT |
252 1.1.6.2 yamt SDICMDSTA_CMD_TIMEOUT | SDICMDSTA_RSP_FIN);*/
253 1.1.6.2 yamt
254 1.1.6.2 yamt sssdi_clear_intr(sc);
255 1.1.6.2 yamt sssdi_enable_intr(sc, SDI_CMD_SENT | SDI_CMD_TIMEOUT | SDI_DATA_TIMEOUT
256 1.1.6.2 yamt | SDI_RESP_FIN);
257 1.1.6.2 yamt
258 1.1.6.2 yamt return 0;
259 1.1.6.2 yamt }
260 1.1.6.2 yamt
261 1.1.6.2 yamt uint32_t
262 1.1.6.2 yamt sssdi_host_ocr(sdmmc_chipset_handle_t sch)
263 1.1.6.2 yamt {
264 1.1.6.2 yamt /* This really ought to be made configurable, I guess... */
265 1.1.6.2 yamt return MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V;
266 1.1.6.2 yamt }
267 1.1.6.2 yamt
268 1.1.6.2 yamt int
269 1.1.6.2 yamt sssdi_maxblklen(sdmmc_chipset_handle_t sch)
270 1.1.6.2 yamt {
271 1.1.6.2 yamt /* The S3C2440 user's manual mentions 4095 as a maximum */
272 1.1.6.2 yamt return 4095;
273 1.1.6.2 yamt }
274 1.1.6.2 yamt
275 1.1.6.2 yamt int
276 1.1.6.2 yamt sssdi_card_detect(sdmmc_chipset_handle_t sch)
277 1.1.6.2 yamt {
278 1.1.6.2 yamt struct sssdi_softc *sc = (struct sssdi_softc*)sch;
279 1.1.6.2 yamt uint32_t data;
280 1.1.6.2 yamt
281 1.1.6.2 yamt DPRINTF(("sssdi_card_detect\n"));
282 1.1.6.2 yamt
283 1.1.6.2 yamt data = bus_space_read_4(sc->iot, sc->card_ioh, GPIO_PGDAT);
284 1.1.6.2 yamt
285 1.1.6.2 yamt /* GPIO Port G, pin 8 is high when card is inserted. */
286 1.1.6.2 yamt if ( (data & (1<<8)) == 0) {
287 1.1.6.2 yamt return 1; /* Card Present */
288 1.1.6.2 yamt } else {
289 1.1.6.2 yamt return 0; /* No Card */
290 1.1.6.2 yamt }
291 1.1.6.2 yamt }
292 1.1.6.2 yamt
293 1.1.6.2 yamt int
294 1.1.6.2 yamt sssdi_write_protect(sdmmc_chipset_handle_t sch)
295 1.1.6.2 yamt {
296 1.1.6.2 yamt struct sssdi_softc *sc = (struct sssdi_softc*)sch;
297 1.1.6.2 yamt uint32_t data;
298 1.1.6.2 yamt
299 1.1.6.2 yamt data = bus_space_read_4(sc->iot, sc->card_ioh, GPIO_PHDAT);
300 1.1.6.2 yamt
301 1.1.6.2 yamt
302 1.1.6.2 yamt /* If GPIO Port H Pin 8 is high, the card is write protected. */
303 1.1.6.2 yamt if ( (data & (1<<8)) ) {
304 1.1.6.2 yamt return 1; /* Write protected */
305 1.1.6.2 yamt } else {
306 1.1.6.2 yamt return 0; /* Writable */
307 1.1.6.2 yamt }
308 1.1.6.2 yamt }
309 1.1.6.2 yamt
310 1.1.6.2 yamt int
311 1.1.6.2 yamt sssdi_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
312 1.1.6.2 yamt {
313 1.1.6.2 yamt /* Do nothing, we can't adjust the bus power */
314 1.1.6.2 yamt return 0;
315 1.1.6.2 yamt }
316 1.1.6.2 yamt
317 1.1.6.2 yamt int
318 1.1.6.2 yamt sssdi_bus_clock(sdmmc_chipset_handle_t sch, int freq)
319 1.1.6.2 yamt {
320 1.1.6.2 yamt struct sssdi_softc *sc = (struct sssdi_softc*)sch;
321 1.1.6.2 yamt int div;
322 1.1.6.2 yamt int clock_set = 0;
323 1.1.6.2 yamt int control;
324 1.1.6.2 yamt int pclk = s3c2xx0_softc->sc_pclk/1000; /*Peripheral bus clock in KHz*/
325 1.1.6.2 yamt
326 1.1.6.2 yamt /* Round peripheral bus clock down to nearest MHz */
327 1.1.6.2 yamt pclk = (pclk / 1000) * 1000;
328 1.1.6.2 yamt
329 1.1.6.2 yamt control = bus_space_read_4(sc->iot, sc->ioh, SDI_CON);
330 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_CON, control & ~SDICON_ENCLK);
331 1.1.6.2 yamt
332 1.1.6.2 yamt DPRINTF(("sssdi_bus_clock (freq: %d KHz)\n", freq));
333 1.1.6.2 yamt
334 1.1.6.2 yamt /* If the frequency is zero just keep the clock disabled */
335 1.1.6.2 yamt if (freq == 0)
336 1.1.6.2 yamt return 0;
337 1.1.6.2 yamt
338 1.1.6.2 yamt for (div = 1; div <= 256; div++) {
339 1.1.6.2 yamt if ( pclk / div <= freq) {
340 1.1.6.2 yamt DPRINTF(("Using divisor %d: %d/%d = %d\n", div, pclk,
341 1.1.6.2 yamt div, pclk/div));
342 1.1.6.2 yamt clock_set = 1;
343 1.1.6.2 yamt bus_space_write_1(sc->iot, sc->ioh, SDI_PRE, div-1);
344 1.1.6.2 yamt break;
345 1.1.6.2 yamt }
346 1.1.6.2 yamt }
347 1.1.6.2 yamt
348 1.1.6.2 yamt if (clock_set) {
349 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh,
350 1.1.6.2 yamt SDI_CON, control | SDICON_ENCLK);
351 1.1.6.2 yamt if (div-1 == bus_space_read_4(sc->iot, sc->ioh, SDI_PRE)) {
352 1.1.6.2 yamt /* Clock successfully set, TODO: how do we fail?! */
353 1.1.6.2 yamt }
354 1.1.6.2 yamt
355 1.1.6.2 yamt /* We do not need to wait here, as the sdmmc code will do that
356 1.1.6.2 yamt for us. */
357 1.1.6.2 yamt return 0;
358 1.1.6.2 yamt } else {
359 1.1.6.2 yamt return 1;
360 1.1.6.2 yamt }
361 1.1.6.2 yamt }
362 1.1.6.2 yamt
363 1.1.6.2 yamt int
364 1.1.6.2 yamt sssdi_bus_width(sdmmc_chipset_handle_t sch, int width)
365 1.1.6.2 yamt {
366 1.1.6.2 yamt struct sssdi_softc *sc = (struct sssdi_softc*)sch;
367 1.1.6.2 yamt
368 1.1.6.2 yamt sc->width = width;
369 1.1.6.2 yamt return 0;
370 1.1.6.2 yamt }
371 1.1.6.2 yamt
372 1.1.6.2 yamt int
373 1.1.6.2 yamt sssdi_bus_rod(sdmmc_chipset_handle_t sch, int on)
374 1.1.6.2 yamt {
375 1.1.6.2 yamt return -1;
376 1.1.6.2 yamt }
377 1.1.6.2 yamt
378 1.1.6.2 yamt #define SSSDI_TRANSFER_NONE 0
379 1.1.6.2 yamt #define SSSDI_TRANSFER_READ 1
380 1.1.6.2 yamt #define SSSDI_TRANSFER_WRITE 2
381 1.1.6.2 yamt
382 1.1.6.2 yamt void
383 1.1.6.2 yamt sssdi_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
384 1.1.6.2 yamt {
385 1.1.6.2 yamt struct sssdi_softc *sc = (struct sssdi_softc*)sch;
386 1.1.6.2 yamt uint32_t cmd_control;
387 1.1.6.2 yamt int status = 0;
388 1.1.6.2 yamt uint32_t data_status;
389 1.1.6.2 yamt int transfer = SSSDI_TRANSFER_NONE;
390 1.1.6.2 yamt dmac_xfer_t xfer;
391 1.1.6.2 yamt
392 1.1.6.2 yamt /* Reset all status registers prior to sending a command */
393 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_DAT_FSTA, 0xFFFFFFFF);
394 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_DAT_STA, 0xFFFFFFFF);
395 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_CMD_STA, 0xFFFFFFFF);
396 1.1.6.2 yamt
397 1.1.6.2 yamt /* Set the argument */
398 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_CMD_ARG, cmd->c_arg);
399 1.1.6.2 yamt
400 1.1.6.2 yamt /* Prepare the value for the command control register */
401 1.1.6.2 yamt cmd_control = (cmd->c_opcode & SDICMDCON_CMD_MASK) |
402 1.1.6.2 yamt SDICMDCON_HOST_CMD | SDICMDCON_CMST;
403 1.1.6.2 yamt if (cmd->c_flags & SCF_RSP_PRESENT)
404 1.1.6.2 yamt cmd_control |= SDICMDCON_WAIT_RSP;
405 1.1.6.2 yamt if (cmd->c_flags & SCF_RSP_136)
406 1.1.6.2 yamt cmd_control |= SDICMDCON_LONG_RSP;
407 1.1.6.2 yamt
408 1.1.6.2 yamt if (cmd->c_datalen > 0 && cmd->c_data != NULL) {
409 1.1.6.2 yamt /* TODO: Ensure that the above condition matches the semantics
410 1.1.6.2 yamt of SDICMDCON_WITH_DATA*/
411 1.1.6.2 yamt DPRINTF(("DATA, datalen: %d, blk_size: %d\n", cmd->c_datalen,
412 1.1.6.2 yamt cmd->c_blklen));
413 1.1.6.2 yamt cmd_control |= SDICMDCON_WITH_DATA;
414 1.1.6.2 yamt }
415 1.1.6.2 yamt
416 1.1.6.2 yamt /* Unfortunately we have to set the ABORT_CMD bit when using CMD12 and
417 1.1.6.2 yamt CMD52.
418 1.1.6.2 yamt CMD12 is MMC_STOP_TRANSMISSION. I currently do not know what CMD52
419 1.1.6.2 yamt is, but it is related to SDIO.
420 1.1.6.2 yamt */
421 1.1.6.2 yamt if (cmd->c_opcode == MMC_STOP_TRANSMISSION) {
422 1.1.6.2 yamt cmd_control |= SDICMDCON_ABORT_CMD;
423 1.1.6.2 yamt }
424 1.1.6.2 yamt
425 1.1.6.2 yamt /* Prepare SDI for data transfer */
426 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_BSIZE, cmd->c_blklen);
427 1.1.6.2 yamt
428 1.1.6.2 yamt /* Set maximum transfer timeout */
429 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_DTIMER, 0x007FFFFF);
430 1.1.6.2 yamt
431 1.1.6.2 yamt /* Set the timeout as low as possible to trigger timeouts for debugging purposes */
432 1.1.6.2 yamt /*bus_space_write_4(sc->iot, sc->ioh, SDI_DTIMER, 0x00005000);*/
433 1.1.6.2 yamt
434 1.1.6.2 yamt if ( (cmd->c_flags & SCF_CMD_READ) &&
435 1.1.6.2 yamt (cmd_control & SDICMDCON_WITH_DATA)) {
436 1.1.6.2 yamt uint32_t data_control;
437 1.1.6.2 yamt DPRINTF(("Reading %d bytes\n", cmd->c_datalen));
438 1.1.6.2 yamt transfer = SSSDI_TRANSFER_READ;
439 1.1.6.2 yamt
440 1.1.6.2 yamt data_control = SDIDATCON_DATMODE_RECEIVE | SDIDATCON_RACMD |
441 1.1.6.2 yamt SDIDATCON_DTST | SDIDATCON_BLKMODE |
442 1.1.6.2 yamt ((cmd->c_datalen / cmd->c_blklen) & SDIDATCON_BLKNUM_MASK) |
443 1.1.6.2 yamt SDIDATCON_DATA_WORD;
444 1.1.6.2 yamt
445 1.1.6.2 yamt if (sc->caps & SMC_CAPS_DMA) {
446 1.1.6.2 yamt data_control |= SDIDATCON_ENDMA;
447 1.1.6.2 yamt xfer = sc->sc_xfer;
448 1.1.6.2 yamt xfer->dx_desc[DMAC_DESC_SRC].xd_bus_type = DMAC_BUS_TYPE_PERIPHERAL;
449 1.1.6.2 yamt xfer->dx_desc[DMAC_DESC_SRC].xd_increment = FALSE;
450 1.1.6.2 yamt xfer->dx_desc[DMAC_DESC_SRC].xd_nsegs = 1;
451 1.1.6.2 yamt xfer->dx_desc[DMAC_DESC_SRC].xd_dma_segs = &sc->sc_dr;
452 1.1.6.2 yamt
453 1.1.6.2 yamt xfer->dx_desc[DMAC_DESC_DST].xd_bus_type = DMAC_BUS_TYPE_SYSTEM;
454 1.1.6.2 yamt xfer->dx_desc[DMAC_DESC_DST].xd_increment = TRUE;
455 1.1.6.2 yamt xfer->dx_desc[DMAC_DESC_DST].xd_nsegs = cmd->c_dmamap->dm_nsegs;
456 1.1.6.2 yamt xfer->dx_desc[DMAC_DESC_DST].xd_dma_segs = cmd->c_dmamap->dm_segs;
457 1.1.6.2 yamt
458 1.1.6.2 yamt /* Let the SD/MMC peripheral control the DMA transfer */
459 1.1.6.2 yamt xfer->dx_peripheral = DMAC_PERIPH_SDI;
460 1.1.6.2 yamt xfer->dx_xfer_width = DMAC_XFER_WIDTH_32BIT;
461 1.1.6.2 yamt }
462 1.1.6.2 yamt if (sc->width == 4) {
463 1.1.6.2 yamt data_control |= SDIDATCON_WIDEBUS;
464 1.1.6.2 yamt }
465 1.1.6.2 yamt
466 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_DAT_CON, data_control);
467 1.1.6.2 yamt } else if (cmd_control & SDICMDCON_WITH_DATA) {
468 1.1.6.2 yamt /* Write data */
469 1.1.6.2 yamt
470 1.1.6.2 yamt uint32_t data_control;
471 1.1.6.2 yamt DPRINTF(("Writing %d bytes\n", cmd->c_datalen));
472 1.1.6.2 yamt DPRINTF(("Requesting %d blocks\n",
473 1.1.6.2 yamt cmd->c_datalen / cmd->c_blklen));
474 1.1.6.2 yamt transfer = SSSDI_TRANSFER_WRITE;
475 1.1.6.2 yamt data_control = SDIDATCON_DATMODE_TRANSMIT | SDIDATCON_BLKMODE |
476 1.1.6.2 yamt SDIDATCON_TARSP | SDIDATCON_DTST |
477 1.1.6.2 yamt ((cmd->c_datalen / cmd->c_blklen) & SDIDATCON_BLKNUM_MASK) |
478 1.1.6.2 yamt SDIDATCON_DATA_WORD;
479 1.1.6.2 yamt
480 1.1.6.2 yamt if (sc->caps & SMC_CAPS_DMA) {
481 1.1.6.2 yamt data_control |= SDIDATCON_ENDMA;
482 1.1.6.2 yamt xfer = sc->sc_xfer;
483 1.1.6.2 yamt
484 1.1.6.2 yamt xfer->dx_desc[DMAC_DESC_DST].xd_bus_type = DMAC_BUS_TYPE_PERIPHERAL;
485 1.1.6.2 yamt xfer->dx_desc[DMAC_DESC_DST].xd_increment = FALSE;
486 1.1.6.2 yamt xfer->dx_desc[DMAC_DESC_DST].xd_nsegs = 1;
487 1.1.6.2 yamt xfer->dx_desc[DMAC_DESC_DST].xd_dma_segs = &sc->sc_dr;
488 1.1.6.2 yamt
489 1.1.6.2 yamt xfer->dx_desc[DMAC_DESC_SRC].xd_bus_type = DMAC_BUS_TYPE_SYSTEM;
490 1.1.6.2 yamt xfer->dx_desc[DMAC_DESC_SRC].xd_increment = TRUE;
491 1.1.6.2 yamt xfer->dx_desc[DMAC_DESC_SRC].xd_nsegs = cmd->c_dmamap->dm_nsegs;
492 1.1.6.2 yamt xfer->dx_desc[DMAC_DESC_SRC].xd_dma_segs = cmd->c_dmamap->dm_segs;
493 1.1.6.2 yamt
494 1.1.6.2 yamt /* Let the SD/MMC peripheral control the DMA transfer */
495 1.1.6.2 yamt xfer->dx_peripheral = DMAC_PERIPH_SDI;
496 1.1.6.2 yamt xfer->dx_xfer_width = DMAC_XFER_WIDTH_32BIT;
497 1.1.6.2 yamt }
498 1.1.6.2 yamt if (sc->width == 4) {
499 1.1.6.2 yamt data_control |= SDIDATCON_WIDEBUS;
500 1.1.6.2 yamt }
501 1.1.6.2 yamt
502 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_DAT_CON, data_control);
503 1.1.6.2 yamt }
504 1.1.6.2 yamt
505 1.1.6.2 yamt /* Send command to SDI */
506 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_CMD_CON, cmd_control);
507 1.1.6.2 yamt
508 1.1.6.2 yamt /* Wait for command sent acknowledgement, timeout set to 5000ms */
509 1.1.6.2 yamt status = sssdi_wait_intr(sc, SDI_CMD_SENT | SDI_CMD_TIMEOUT, mstohz(SDI_CMD_WAIT_TIME));
510 1.1.6.2 yamt
511 1.1.6.2 yamt if (status & SDI_CMD_TIMEOUT) {
512 1.1.6.2 yamt DPRINTF(("Timeout waiting for command acknowledgement\n"));
513 1.1.6.2 yamt cmd->c_error = ETIMEDOUT;
514 1.1.6.2 yamt goto out;
515 1.1.6.2 yamt } else if (status & SDICMDSTA_CMD_SENT) {
516 1.1.6.2 yamt /* Interrupt handler has acknowledged already, we do not need
517 1.1.6.2 yamt to do anything further here */
518 1.1.6.2 yamt }
519 1.1.6.2 yamt
520 1.1.6.2 yamt if (!(cmd_control & SDICMDCON_WAIT_RSP)) {
521 1.1.6.2 yamt cmd->c_flags |= SCF_ITSDONE;
522 1.1.6.2 yamt goto out;
523 1.1.6.2 yamt }
524 1.1.6.2 yamt
525 1.1.6.2 yamt DPRINTF(("waiting for response\n"));
526 1.1.6.2 yamt
527 1.1.6.2 yamt status = sssdi_wait_intr(sc, SDI_RESP_FIN | SDI_DATA_TIMEOUT, 100);
528 1.1.6.2 yamt if (status & SDI_CMD_TIMEOUT || status & SDI_DATA_TIMEOUT) {
529 1.1.6.2 yamt cmd->c_error = ETIMEDOUT;
530 1.1.6.2 yamt DPRINTF(("Timeout waiting for response\n"));
531 1.1.6.2 yamt goto out;
532 1.1.6.2 yamt }
533 1.1.6.2 yamt DPRINTF(("Got Response\n"));
534 1.1.6.2 yamt
535 1.1.6.2 yamt
536 1.1.6.2 yamt if (cmd->c_flags & SCF_RSP_136 ) {
537 1.1.6.2 yamt uint32_t w[4];
538 1.1.6.2 yamt
539 1.1.6.2 yamt /* We store the response least significant word first */
540 1.1.6.2 yamt w[0] = bus_space_read_4(sc->iot, sc->ioh, SDI_RSP3);
541 1.1.6.2 yamt w[1] = bus_space_read_4(sc->iot, sc->ioh, SDI_RSP2);
542 1.1.6.2 yamt w[2] = bus_space_read_4(sc->iot, sc->ioh, SDI_RSP1);
543 1.1.6.2 yamt w[3] = bus_space_read_4(sc->iot, sc->ioh, SDI_RSP0);
544 1.1.6.2 yamt
545 1.1.6.2 yamt /* The sdmmc subsystem expects that the response is delivered
546 1.1.6.2 yamt without the lower 8 bits (CRC + '1' bit) */
547 1.1.6.2 yamt cmd->c_resp[0] = (w[0] >> 8) | ((w[1] & 0xFF) << 24);
548 1.1.6.2 yamt cmd->c_resp[1] = (w[1] >> 8) | ((w[2] & 0XFF) << 24);
549 1.1.6.2 yamt cmd->c_resp[2] = (w[2] >> 8) | ((w[3] & 0XFF) << 24);
550 1.1.6.2 yamt cmd->c_resp[3] = (w[3] >> 8);
551 1.1.6.2 yamt
552 1.1.6.2 yamt } else {
553 1.1.6.2 yamt cmd->c_resp[0] = bus_space_read_4(sc->iot, sc->ioh, SDI_RSP0);
554 1.1.6.2 yamt cmd->c_resp[1] = bus_space_read_4(sc->iot, sc->ioh, SDI_RSP1);
555 1.1.6.2 yamt }
556 1.1.6.2 yamt
557 1.1.6.2 yamt DPRINTF(("Response: %X %X %X %X\n",
558 1.1.6.2 yamt cmd->c_resp[0],
559 1.1.6.2 yamt cmd->c_resp[1],
560 1.1.6.2 yamt cmd->c_resp[2],
561 1.1.6.2 yamt cmd->c_resp[3]));
562 1.1.6.2 yamt
563 1.1.6.2 yamt status = bus_space_read_4(sc->iot, sc->ioh, SDI_DAT_CNT);
564 1.1.6.2 yamt
565 1.1.6.2 yamt DPRINTF(("Remaining bytes of current block: %d\n",
566 1.1.6.2 yamt SDIDATCNT_BLK_CNT(status)));
567 1.1.6.2 yamt DPRINTF(("Remaining Block Number : %d\n",
568 1.1.6.2 yamt SDIDATCNT_BLK_NUM_CNT(status)));
569 1.1.6.2 yamt
570 1.1.6.2 yamt data_status = bus_space_read_4(sc->iot, sc->ioh, SDI_DAT_STA);
571 1.1.6.2 yamt #ifdef SSSDI_DEBUG
572 1.1.6.2 yamt printf("SDI Data Status Register Before xfer: 0x%X\n", data_status);
573 1.1.6.2 yamt #endif
574 1.1.6.2 yamt if (transfer == SSSDI_TRANSFER_READ) {
575 1.1.6.2 yamt DPRINTF(("Waiting for transfer to complete\n"));
576 1.1.6.2 yamt
577 1.1.6.2 yamt if (sc->sc_xfer != NULL ) {
578 1.1.6.2 yamt int dma_error = 0;
579 1.1.6.2 yamt /* It might not be very efficient to delay the start of
580 1.1.6.2 yamt the DMA transfer until now, but it works :-).
581 1.1.6.2 yamt */
582 1.1.6.2 yamt s3c2440_dmac_start_xfer(sc->sc_xfer);
583 1.1.6.2 yamt
584 1.1.6.2 yamt /* Wait until the transfer has completed, timeout is
585 1.1.6.2 yamt 500ms */
586 1.1.6.2 yamt dma_error = s3c2440_dmac_wait_xfer(sc->sc_xfer, mstohz(SDI_DMA_WAIT_TIME));
587 1.1.6.2 yamt if (dma_error != 0) {
588 1.1.6.2 yamt //s3c2440_dma_xfer_abort(sc->dma_xfer, mstohz(100)); /* XXX: Handle timeout during abort */
589 1.1.6.2 yamt cmd->c_error = dma_error;
590 1.1.6.2 yamt DPRINTF(("DMA xfer failed: %d\n", dma_error));
591 1.1.6.2 yamt goto out;
592 1.1.6.2 yamt }
593 1.1.6.2 yamt } else {
594 1.1.6.2 yamt DPRINTF(("PIO READ\n"));
595 1.1.6.2 yamt sssdi_perform_pio_read(sc, cmd);
596 1.1.6.2 yamt }
597 1.1.6.2 yamt } else if (transfer == SSSDI_TRANSFER_WRITE) {
598 1.1.6.2 yamt DPRINTF(("Waiting for WRITE transfer to complete\n"));
599 1.1.6.2 yamt
600 1.1.6.2 yamt if (sc->sc_xfer != NULL) {
601 1.1.6.2 yamt int dma_error = 0;
602 1.1.6.2 yamt s3c2440_dmac_start_xfer(sc->sc_xfer);
603 1.1.6.2 yamt
604 1.1.6.2 yamt dma_error = s3c2440_dmac_wait_xfer(sc->sc_xfer, mstohz(SDI_DMA_WAIT_TIME));
605 1.1.6.2 yamt if (dma_error != 0) {
606 1.1.6.2 yamt //s3c2440_dma_xfer_abort(sc->dma_xfer, mstohz(100)); /* XXX: Handle timeout during abort*/
607 1.1.6.2 yamt cmd->c_error = dma_error;
608 1.1.6.2 yamt DPRINTF(("DMA xfer failed: %d\n", dma_error));
609 1.1.6.2 yamt goto out;
610 1.1.6.2 yamt }
611 1.1.6.2 yamt } else {
612 1.1.6.2 yamt DPRINTF(("PIO WRITE\n"));
613 1.1.6.2 yamt sssdi_perform_pio_write(sc, cmd);
614 1.1.6.2 yamt }
615 1.1.6.2 yamt
616 1.1.6.2 yamt if (cmd->c_error == ETIMEDOUT)
617 1.1.6.2 yamt goto out;
618 1.1.6.2 yamt
619 1.1.6.2 yamt DPRINTF(("Waiting for transfer to complete\n"));
620 1.1.6.2 yamt status = sssdi_wait_intr(sc, SDI_DATA_FIN | SDI_DATA_TIMEOUT, 1000);
621 1.1.6.2 yamt if (status & SDI_CMD_TIMEOUT || status & SDI_DATA_TIMEOUT) {
622 1.1.6.2 yamt cmd->c_error = ETIMEDOUT;
623 1.1.6.2 yamt DPRINTF(("Timeout waiting for data to complete\n"));
624 1.1.6.2 yamt goto out;
625 1.1.6.2 yamt }
626 1.1.6.2 yamt DPRINTF(("Done\n"));
627 1.1.6.2 yamt
628 1.1.6.2 yamt }
629 1.1.6.2 yamt
630 1.1.6.2 yamt
631 1.1.6.2 yamt /* Response has been received, and any data transfer needed has been
632 1.1.6.2 yamt performed */
633 1.1.6.2 yamt cmd->c_flags |= SCF_ITSDONE;
634 1.1.6.2 yamt
635 1.1.6.2 yamt out:
636 1.1.6.2 yamt
637 1.1.6.2 yamt #ifdef SSSDI_DEBUG
638 1.1.6.2 yamt data_status = bus_space_read_4(sc->iot, sc->ioh, SDI_DAT_STA);
639 1.1.6.2 yamt printf("SDI Data Status Register after execute: 0x%X\n", data_status);
640 1.1.6.2 yamt #endif
641 1.1.6.2 yamt
642 1.1.6.2 yamt /* Clear status register. Their are cleared on the
643 1.1.6.2 yamt next sssdi_exec_command */
644 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_CMD_STA, 0xFFFFFFFF);
645 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_DAT_CON, 0x0);
646 1.1.6.2 yamt }
647 1.1.6.2 yamt
648 1.1.6.2 yamt void sssdi_perform_pio_read(struct sssdi_softc *sc, struct sdmmc_command *cmd)
649 1.1.6.2 yamt {
650 1.1.6.2 yamt uint32_t status;
651 1.1.6.2 yamt uint32_t fifo_status;
652 1.1.6.2 yamt int count;
653 1.1.6.2 yamt uint32_t written;
654 1.1.6.2 yamt uint32_t *dest = (uint32_t*)cmd->c_data;
655 1.1.6.2 yamt
656 1.1.6.2 yamt written = 0;
657 1.1.6.2 yamt
658 1.1.6.2 yamt while (written < cmd->c_datalen ) {
659 1.1.6.2 yamt /* Wait until the FIFO is full or has the final data.
660 1.1.6.2 yamt In the latter case it might not get filled. */
661 1.1.6.2 yamt status = sssdi_wait_intr(sc, SDI_FIFO_RX_FULL | SDI_FIFO_RX_LAST, 1000);
662 1.1.6.2 yamt
663 1.1.6.2 yamt fifo_status = bus_space_read_4(sc->iot, sc->ioh, SDI_DAT_FSTA);
664 1.1.6.2 yamt count = SDIDATFSTA_FFCNT(fifo_status);
665 1.1.6.2 yamt
666 1.1.6.2 yamt for(int i=0; i<count; i+=4) {
667 1.1.6.2 yamt uint32_t buf;
668 1.1.6.2 yamt
669 1.1.6.2 yamt buf = bus_space_read_4(sc->iot, sc->ioh, SDI_DAT_LI_W);
670 1.1.6.2 yamt *dest = buf;
671 1.1.6.2 yamt written += 4;
672 1.1.6.2 yamt dest++;
673 1.1.6.2 yamt }
674 1.1.6.2 yamt }
675 1.1.6.2 yamt }
676 1.1.6.2 yamt
677 1.1.6.2 yamt void
678 1.1.6.2 yamt sssdi_perform_pio_write(struct sssdi_softc *sc, struct sdmmc_command *cmd)
679 1.1.6.2 yamt {
680 1.1.6.2 yamt uint32_t status;
681 1.1.6.2 yamt uint32_t fifo_status;
682 1.1.6.2 yamt int count;
683 1.1.6.2 yamt uint32_t written;
684 1.1.6.2 yamt uint32_t *dest = (uint32_t*)cmd->c_data;
685 1.1.6.2 yamt
686 1.1.6.2 yamt written = 0;
687 1.1.6.2 yamt
688 1.1.6.2 yamt while (written < cmd->c_datalen ) {
689 1.1.6.2 yamt /* Wait until the FIFO is full or has the final data.
690 1.1.6.2 yamt In the latter case it might not get filled. */
691 1.1.6.2 yamt DPRINTF(("Waiting for FIFO to become empty\n"));
692 1.1.6.2 yamt status = sssdi_wait_intr(sc, SDI_FIFO_TX_EMPTY, 1000);
693 1.1.6.2 yamt
694 1.1.6.2 yamt fifo_status = bus_space_read_4(sc->iot, sc->ioh, SDI_DAT_FSTA);
695 1.1.6.2 yamt DPRINTF(("PIO Write FIFO Status: 0x%X\n", fifo_status));
696 1.1.6.2 yamt count = 64-SDIDATFSTA_FFCNT(fifo_status);
697 1.1.6.2 yamt
698 1.1.6.2 yamt status = bus_space_read_4(sc->iot, sc->ioh, SDI_DAT_CNT);
699 1.1.6.2 yamt DPRINTF(("Remaining bytes of current block: %d\n",
700 1.1.6.2 yamt SDIDATCNT_BLK_CNT(status)));
701 1.1.6.2 yamt DPRINTF(("Remaining Block Number : %d\n",
702 1.1.6.2 yamt SDIDATCNT_BLK_NUM_CNT(status)));
703 1.1.6.2 yamt
704 1.1.6.2 yamt
705 1.1.6.2 yamt status = bus_space_read_4(sc->iot,sc->ioh, SDI_DAT_STA);
706 1.1.6.2 yamt DPRINTF(("PIO Write Data Status: 0x%X\n", status));
707 1.1.6.2 yamt
708 1.1.6.2 yamt if (status & SDIDATSTA_DATA_TIMEOUT) {
709 1.1.6.2 yamt cmd->c_error = ETIMEDOUT;
710 1.1.6.2 yamt /* Acknowledge the timeout*/
711 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_DAT_STA,
712 1.1.6.2 yamt SDIDATSTA_DATA_TIMEOUT);
713 1.1.6.2 yamt printf("%s: Data timeout\n", device_xname(sc->dev));
714 1.1.6.2 yamt break;
715 1.1.6.2 yamt }
716 1.1.6.2 yamt
717 1.1.6.2 yamt DPRINTF(("Filling FIFO with %d bytes\n", count));
718 1.1.6.2 yamt for(int i=0; i<count; i+=4) {
719 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_DAT_LI_W, *dest);
720 1.1.6.2 yamt written += 4;
721 1.1.6.2 yamt dest++;
722 1.1.6.2 yamt }
723 1.1.6.2 yamt }
724 1.1.6.2 yamt }
725 1.1.6.2 yamt
726 1.1.6.2 yamt
727 1.1.6.2 yamt void
728 1.1.6.2 yamt sssdi_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
729 1.1.6.2 yamt {
730 1.1.6.2 yamt printf("sssdi_card_enable_intr not implemented\n");
731 1.1.6.2 yamt }
732 1.1.6.2 yamt
733 1.1.6.2 yamt void
734 1.1.6.2 yamt sssdi_card_intr_ack(sdmmc_chipset_handle_t sch)
735 1.1.6.2 yamt {
736 1.1.6.2 yamt printf("sssdi_card_intr_ack not implemented\n");
737 1.1.6.2 yamt }
738 1.1.6.2 yamt
739 1.1.6.2 yamt int
740 1.1.6.2 yamt sssdi_intr(void *arg)
741 1.1.6.2 yamt {
742 1.1.6.2 yamt struct sssdi_softc *sc = (struct sssdi_softc*)arg;
743 1.1.6.2 yamt uint32_t status;
744 1.1.6.2 yamt uint32_t ack_status;
745 1.1.6.2 yamt
746 1.1.6.2 yamt /* Start by dealing with Command Status */
747 1.1.6.2 yamt ack_status = 0;
748 1.1.6.2 yamt status = bus_space_read_4(sc->iot, sc->ioh, SDI_CMD_STA);
749 1.1.6.2 yamt
750 1.1.6.2 yamt if (status & SDICMDSTA_CMD_TIMEOUT) {
751 1.1.6.2 yamt ack_status |= SDICMDSTA_CMD_TIMEOUT;
752 1.1.6.2 yamt sc->intr_status |= SDI_CMD_TIMEOUT;
753 1.1.6.2 yamt /*sssdi_disable_intr(sc, SDI_CMD_TIMEOUT);*/
754 1.1.6.2 yamt }
755 1.1.6.2 yamt if (status & SDICMDSTA_CMD_SENT) {
756 1.1.6.2 yamt ack_status |= SDICMDSTA_CMD_SENT;
757 1.1.6.2 yamt sc->intr_status |= SDI_CMD_SENT;
758 1.1.6.2 yamt /* sssdi_disable_intr(sc, SDI_CMD_SENT);*/
759 1.1.6.2 yamt }
760 1.1.6.2 yamt if (status & SDICMDSTA_RSP_FIN) {
761 1.1.6.2 yamt ack_status |= SDICMDSTA_RSP_FIN;
762 1.1.6.2 yamt sc->intr_status |= SDI_RESP_FIN;
763 1.1.6.2 yamt /* sssdi_disable_intr(sc, SDI_RESP_FIN);*/
764 1.1.6.2 yamt }
765 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_CMD_STA, ack_status);
766 1.1.6.2 yamt
767 1.1.6.2 yamt /* Next: FIFO Status */
768 1.1.6.2 yamt ack_status = 0;
769 1.1.6.2 yamt status = bus_space_read_4(sc->iot, sc->ioh, SDI_DAT_FSTA);
770 1.1.6.2 yamt if (status & SDIDATFSTA_RF_FULL) {
771 1.1.6.2 yamt ack_status |= SDIDATFSTA_RF_FULL;
772 1.1.6.2 yamt sc->intr_status |= SDI_FIFO_RX_FULL;
773 1.1.6.2 yamt sssdi_disable_intr(sc, SDI_FIFO_RX_FULL);
774 1.1.6.2 yamt }
775 1.1.6.2 yamt if (status & SDIDATFSTA_RF_LAST) {
776 1.1.6.2 yamt ack_status |= SDIDATFSTA_RF_LAST | SDIDATFSTA_RESET;
777 1.1.6.2 yamt sc->intr_status |= SDI_FIFO_RX_LAST;
778 1.1.6.2 yamt sssdi_disable_intr(sc, SDI_FIFO_RX_LAST);
779 1.1.6.2 yamt }
780 1.1.6.2 yamt if (status & SDIDATFSTA_TF_EMPTY) {
781 1.1.6.2 yamt ack_status |= SDIDATFSTA_TF_EMPTY;
782 1.1.6.2 yamt sc->intr_status |= SDI_FIFO_TX_EMPTY;
783 1.1.6.2 yamt sssdi_disable_intr(sc, SDI_FIFO_TX_EMPTY);
784 1.1.6.2 yamt }
785 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_DAT_FSTA, ack_status);
786 1.1.6.2 yamt
787 1.1.6.2 yamt ack_status = 0;
788 1.1.6.2 yamt status = bus_space_read_4(sc->iot, sc->ioh, SDI_DAT_STA);
789 1.1.6.2 yamt if (status & SDIDATSTA_DATA_FIN) {
790 1.1.6.2 yamt DPRINTF(("sssdi_intr: DATA FINISHED\n"));
791 1.1.6.2 yamt ack_status |= SDIDATSTA_DATA_FIN;
792 1.1.6.2 yamt sc->intr_status |= SDI_DATA_FIN;
793 1.1.6.2 yamt sssdi_disable_intr(sc, SDI_DATA_FIN);
794 1.1.6.2 yamt }
795 1.1.6.2 yamt if (status & SDIDATSTA_DATA_TIMEOUT) {
796 1.1.6.2 yamt printf("sssdi_intr: DATA TIMEOUT\n");
797 1.1.6.2 yamt ack_status |= SDIDATSTA_DATA_TIMEOUT;
798 1.1.6.2 yamt sc->intr_status |= SDI_DATA_TIMEOUT;
799 1.1.6.2 yamt /* Data timeout interrupt is always enabled, thus
800 1.1.6.2 yamt we do not disable it when we have received one. */
801 1.1.6.2 yamt /*sssdi_disable_intr(sc, SDI_DATA_TIMEOUT);*/
802 1.1.6.2 yamt
803 1.1.6.2 yamt if (sc->sc_xfer != NULL) {
804 1.1.6.2 yamt s3c2440_dmac_abort_xfer(sc->sc_xfer);
805 1.1.6.2 yamt }
806 1.1.6.2 yamt }
807 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_DAT_STA, ack_status);
808 1.1.6.2 yamt
809 1.1.6.2 yamt mutex_enter(&sc->intr_mtx);
810 1.1.6.2 yamt cv_broadcast(&sc->intr_cv);
811 1.1.6.2 yamt mutex_exit(&sc->intr_mtx);
812 1.1.6.2 yamt
813 1.1.6.2 yamt return 1;
814 1.1.6.2 yamt }
815 1.1.6.2 yamt
816 1.1.6.2 yamt int
817 1.1.6.2 yamt sssdi_intr_card(void *arg)
818 1.1.6.2 yamt {
819 1.1.6.2 yamt struct sssdi_softc *sc = (struct sssdi_softc*)arg;
820 1.1.6.2 yamt
821 1.1.6.2 yamt /* TODO: If card was removed then abort any current command */
822 1.1.6.2 yamt
823 1.1.6.2 yamt sdmmc_needs_discover(sc->sdmmc);
824 1.1.6.2 yamt
825 1.1.6.2 yamt return 1; /* handled */
826 1.1.6.2 yamt }
827 1.1.6.2 yamt
828 1.1.6.2 yamt static void
829 1.1.6.2 yamt sssdi_enable_intr(struct sssdi_softc *sc, uint32_t i)
830 1.1.6.2 yamt {
831 1.1.6.2 yamt uint32_t v = bus_space_read_4(sc->iot, sc->ioh, SDI_INT_MASK);
832 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_INT_MASK, v | i );
833 1.1.6.2 yamt }
834 1.1.6.2 yamt
835 1.1.6.2 yamt void
836 1.1.6.2 yamt sssdi_disable_intr(struct sssdi_softc *sc, uint32_t i)
837 1.1.6.2 yamt {
838 1.1.6.2 yamt uint32_t v = bus_space_read_4(sc->iot, sc->ioh, SDI_INT_MASK);
839 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_INT_MASK, v & ~i );
840 1.1.6.2 yamt }
841 1.1.6.2 yamt
842 1.1.6.2 yamt void
843 1.1.6.2 yamt sssdi_clear_intr(struct sssdi_softc *sc)
844 1.1.6.2 yamt {
845 1.1.6.2 yamt bus_space_write_4(sc->iot, sc->ioh, SDI_INT_MASK, 0x0);
846 1.1.6.2 yamt }
847 1.1.6.2 yamt
848 1.1.6.2 yamt static int
849 1.1.6.2 yamt sssdi_wait_intr(struct sssdi_softc *sc, uint32_t mask, int timeout)
850 1.1.6.2 yamt {
851 1.1.6.2 yamt uint32_t status;
852 1.1.6.2 yamt
853 1.1.6.2 yamt /* Wait until the command has been sent */
854 1.1.6.2 yamt mutex_enter(&sc->intr_mtx);
855 1.1.6.2 yamt sssdi_enable_intr(sc, mask);
856 1.1.6.2 yamt status = sc->intr_status & mask;
857 1.1.6.2 yamt while(status == 0) {
858 1.1.6.2 yamt
859 1.1.6.2 yamt if (cv_timedwait(&sc->intr_cv, &sc->intr_mtx, timeout) ==
860 1.1.6.2 yamt EWOULDBLOCK ) {
861 1.1.6.2 yamt DPRINTF(("Timed out waiting for interrupt from SDI controller\n"));
862 1.1.6.2 yamt status |= SDI_CMD_TIMEOUT;
863 1.1.6.2 yamt break;
864 1.1.6.2 yamt }
865 1.1.6.2 yamt
866 1.1.6.2 yamt status = sc->intr_status & mask;
867 1.1.6.2 yamt }
868 1.1.6.2 yamt
869 1.1.6.2 yamt sc->intr_status &= ~status;
870 1.1.6.2 yamt mutex_exit(&sc->intr_mtx);
871 1.1.6.2 yamt
872 1.1.6.2 yamt return status;
873 1.1.6.2 yamt }
874