rtsx.c revision 1.1.4.2 1 1.1.4.2 rmind /* $NetBSD: rtsx.c,v 1.1.4.2 2014/05/18 17:45:37 rmind Exp $ */
2 1.1.4.2 rmind /* $OpenBSD: rtsx.c,v 1.7 2013/12/08 18:31:03 stsp Exp $ */
3 1.1.4.2 rmind
4 1.1.4.2 rmind /*
5 1.1.4.2 rmind * Copyright (c) 2006 Uwe Stuehler <uwe (at) openbsd.org>
6 1.1.4.2 rmind * Copyright (c) 2012 Stefan Sperling <stsp (at) openbsd.org>
7 1.1.4.2 rmind *
8 1.1.4.2 rmind * Permission to use, copy, modify, and distribute this software for any
9 1.1.4.2 rmind * purpose with or without fee is hereby granted, provided that the above
10 1.1.4.2 rmind * copyright notice and this permission notice appear in all copies.
11 1.1.4.2 rmind *
12 1.1.4.2 rmind * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 1.1.4.2 rmind * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 1.1.4.2 rmind * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 1.1.4.2 rmind * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 1.1.4.2 rmind * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 1.1.4.2 rmind * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 1.1.4.2 rmind * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 1.1.4.2 rmind */
20 1.1.4.2 rmind
21 1.1.4.2 rmind /*
22 1.1.4.2 rmind * Realtek RTS5209/RTS5229 Card Reader driver.
23 1.1.4.2 rmind */
24 1.1.4.2 rmind
25 1.1.4.2 rmind #include <sys/cdefs.h>
26 1.1.4.2 rmind __KERNEL_RCSID(0, "$NetBSD: rtsx.c,v 1.1.4.2 2014/05/18 17:45:37 rmind Exp $");
27 1.1.4.2 rmind
28 1.1.4.2 rmind #include <sys/param.h>
29 1.1.4.2 rmind #include <sys/device.h>
30 1.1.4.2 rmind #include <sys/kernel.h>
31 1.1.4.2 rmind #include <sys/systm.h>
32 1.1.4.2 rmind #include <sys/proc.h>
33 1.1.4.2 rmind #include <sys/mutex.h>
34 1.1.4.2 rmind
35 1.1.4.2 rmind #include <dev/ic/rtsxreg.h>
36 1.1.4.2 rmind #include <dev/ic/rtsxvar.h>
37 1.1.4.2 rmind
38 1.1.4.2 rmind #include <dev/sdmmc/sdmmcvar.h>
39 1.1.4.2 rmind #include <dev/sdmmc/sdmmc_ioreg.h>
40 1.1.4.2 rmind
41 1.1.4.2 rmind /*
42 1.1.4.2 rmind * We use two DMA buffers, a command buffer and a data buffer.
43 1.1.4.2 rmind *
44 1.1.4.2 rmind * The command buffer contains a command queue for the host controller,
45 1.1.4.2 rmind * which describes SD/MMC commands to run, and other parameters. The chip
46 1.1.4.2 rmind * runs the command queue when a special bit in the RTSX_HCBAR register is set
47 1.1.4.2 rmind * and signals completion with the TRANS_OK interrupt.
48 1.1.4.2 rmind * Each command is encoded as a 4 byte sequence containing command number
49 1.1.4.2 rmind * (read, write, or check a host controller register), a register address,
50 1.1.4.2 rmind * and a data bit-mask and value.
51 1.1.4.2 rmind *
52 1.1.4.2 rmind * The data buffer is used to transfer data sectors to or from the SD card.
53 1.1.4.2 rmind * Data transfer is controlled via the RTSX_HDBAR register. Completion is
54 1.1.4.2 rmind * also signalled by the TRANS_OK interrupt.
55 1.1.4.2 rmind *
56 1.1.4.2 rmind * The chip is unable to perform DMA above 4GB.
57 1.1.4.2 rmind *
58 1.1.4.2 rmind * SD/MMC commands which do not transfer any data from/to the card only use
59 1.1.4.2 rmind * the command buffer.
60 1.1.4.2 rmind */
61 1.1.4.2 rmind
62 1.1.4.2 rmind #define RTSX_DMA_MAX_SEGSIZE 0x80000
63 1.1.4.2 rmind #define RTSX_HOSTCMD_MAX 256
64 1.1.4.2 rmind #define RTSX_HOSTCMD_BUFSIZE (sizeof(uint32_t) * RTSX_HOSTCMD_MAX)
65 1.1.4.2 rmind #define RTSX_DMA_DATA_BUFSIZE MAXPHYS
66 1.1.4.2 rmind
67 1.1.4.2 rmind #define READ4(sc, reg) \
68 1.1.4.2 rmind (bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
69 1.1.4.2 rmind #define WRITE4(sc, reg, val) \
70 1.1.4.2 rmind bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
71 1.1.4.2 rmind
72 1.1.4.2 rmind #define RTSX_READ(sc, reg, val) \
73 1.1.4.2 rmind do { \
74 1.1.4.2 rmind int err = rtsx_read((sc), (reg), (val)); \
75 1.1.4.2 rmind if (err) \
76 1.1.4.2 rmind return err; \
77 1.1.4.2 rmind } while (/*CONSTCOND*/0)
78 1.1.4.2 rmind
79 1.1.4.2 rmind #define RTSX_WRITE(sc, reg, val) \
80 1.1.4.2 rmind do { \
81 1.1.4.2 rmind int err = rtsx_write((sc), (reg), 0xff, (val)); \
82 1.1.4.2 rmind if (err) \
83 1.1.4.2 rmind return err; \
84 1.1.4.2 rmind } while (/*CONSTCOND*/0)
85 1.1.4.2 rmind
86 1.1.4.2 rmind #define RTSX_CLR(sc, reg, bits) \
87 1.1.4.2 rmind do { \
88 1.1.4.2 rmind int err = rtsx_write((sc), (reg), (bits), 0); \
89 1.1.4.2 rmind if (err) \
90 1.1.4.2 rmind return err; \
91 1.1.4.2 rmind } while (/*CONSTCOND*/0)
92 1.1.4.2 rmind
93 1.1.4.2 rmind #define RTSX_SET(sc, reg, bits) \
94 1.1.4.2 rmind do { \
95 1.1.4.2 rmind int err = rtsx_write((sc), (reg), (bits), 0xff);\
96 1.1.4.2 rmind if (err) \
97 1.1.4.2 rmind return err; \
98 1.1.4.2 rmind } while (/*CONSTCOND*/0)
99 1.1.4.2 rmind
100 1.1.4.2 rmind static int rtsx_host_reset(sdmmc_chipset_handle_t);
101 1.1.4.2 rmind static uint32_t rtsx_host_ocr(sdmmc_chipset_handle_t);
102 1.1.4.2 rmind static int rtsx_host_maxblklen(sdmmc_chipset_handle_t);
103 1.1.4.2 rmind static int rtsx_card_detect(sdmmc_chipset_handle_t);
104 1.1.4.2 rmind static int rtsx_write_protect(sdmmc_chipset_handle_t);
105 1.1.4.2 rmind static int rtsx_bus_power(sdmmc_chipset_handle_t, uint32_t);
106 1.1.4.2 rmind static int rtsx_bus_clock(sdmmc_chipset_handle_t, int);
107 1.1.4.2 rmind static int rtsx_bus_width(sdmmc_chipset_handle_t, int);
108 1.1.4.2 rmind static int rtsx_bus_rod(sdmmc_chipset_handle_t, int);
109 1.1.4.2 rmind static void rtsx_exec_command(sdmmc_chipset_handle_t,
110 1.1.4.2 rmind struct sdmmc_command *);
111 1.1.4.2 rmind static int rtsx_init(struct rtsx_softc *, int);
112 1.1.4.2 rmind static void rtsx_soft_reset(struct rtsx_softc *);
113 1.1.4.2 rmind static int rtsx_bus_power_off(struct rtsx_softc *);
114 1.1.4.2 rmind static int rtsx_bus_power_on(struct rtsx_softc *);
115 1.1.4.2 rmind static int rtsx_set_bus_width(struct rtsx_softc *, int);
116 1.1.4.2 rmind static int rtsx_stop_sd_clock(struct rtsx_softc *);
117 1.1.4.2 rmind static int rtsx_switch_sd_clock(struct rtsx_softc *, uint8_t, int, int);
118 1.1.4.2 rmind static int rtsx_wait_intr(struct rtsx_softc *, int, int);
119 1.1.4.2 rmind static int rtsx_read(struct rtsx_softc *, uint16_t, uint8_t *);
120 1.1.4.2 rmind static int rtsx_write(struct rtsx_softc *, uint16_t, uint8_t, uint8_t);
121 1.1.4.2 rmind #ifdef notyet
122 1.1.4.2 rmind static int rtsx_read_phy(struct rtsx_softc *, uint8_t, uint16_t *);
123 1.1.4.2 rmind #endif
124 1.1.4.2 rmind static int rtsx_write_phy(struct rtsx_softc *, uint8_t, uint16_t);
125 1.1.4.2 rmind static int rtsx_read_cfg(struct rtsx_softc *, uint8_t, uint16_t,
126 1.1.4.2 rmind uint32_t *);
127 1.1.4.2 rmind #ifdef notyet
128 1.1.4.2 rmind static int rtsx_write_cfg(struct rtsx_softc *, uint8_t, uint16_t, uint32_t,
129 1.1.4.2 rmind uint32_t);
130 1.1.4.2 rmind #endif
131 1.1.4.2 rmind static void rtsx_hostcmd(uint32_t *, int *, uint8_t, uint16_t, uint8_t,
132 1.1.4.2 rmind uint8_t);
133 1.1.4.2 rmind static int rtsx_hostcmd_send(struct rtsx_softc *, int);
134 1.1.4.2 rmind static uint8_t rtsx_response_type(uint16_t);
135 1.1.4.2 rmind static int rtsx_read_ppbuf(struct rtsx_softc *, struct sdmmc_command *,
136 1.1.4.2 rmind uint32_t *);
137 1.1.4.2 rmind static int rtsx_write_ppbuf(struct rtsx_softc *, struct sdmmc_command *,
138 1.1.4.2 rmind uint32_t *);
139 1.1.4.2 rmind static int rtsx_exec_short_xfer(struct rtsx_softc *,
140 1.1.4.2 rmind struct sdmmc_command *, uint32_t *, uint8_t);
141 1.1.4.2 rmind static int rtsx_xfer(struct rtsx_softc *, struct sdmmc_command *,
142 1.1.4.2 rmind uint32_t *);
143 1.1.4.2 rmind static void rtsx_card_insert(struct rtsx_softc *);
144 1.1.4.2 rmind static void rtsx_card_eject(struct rtsx_softc *);
145 1.1.4.2 rmind static int rtsx_led_enable(struct rtsx_softc *);
146 1.1.4.2 rmind static int rtsx_led_disable(struct rtsx_softc *);
147 1.1.4.2 rmind static void rtsx_save_regs(struct rtsx_softc *);
148 1.1.4.2 rmind static void rtsx_restore_regs(struct rtsx_softc *);
149 1.1.4.2 rmind
150 1.1.4.2 rmind #ifdef RTSX_DEBUG
151 1.1.4.2 rmind int rtsxdebug = 0;
152 1.1.4.2 rmind #define DPRINTF(n,s) do { if ((n) <= rtsxdebug) printf s; } while (0)
153 1.1.4.2 rmind #else
154 1.1.4.2 rmind #define DPRINTF(n,s) /**/
155 1.1.4.2 rmind #endif
156 1.1.4.2 rmind
157 1.1.4.2 rmind #define DEVNAME(sc) SDMMCDEVNAME(sc)
158 1.1.4.2 rmind
159 1.1.4.2 rmind static struct sdmmc_chip_functions rtsx_chip_functions = {
160 1.1.4.2 rmind /* host controller reset */
161 1.1.4.2 rmind .host_reset = rtsx_host_reset,
162 1.1.4.2 rmind
163 1.1.4.2 rmind /* host controller capabilities */
164 1.1.4.2 rmind .host_ocr = rtsx_host_ocr,
165 1.1.4.2 rmind .host_maxblklen = rtsx_host_maxblklen,
166 1.1.4.2 rmind
167 1.1.4.2 rmind /* card detection */
168 1.1.4.2 rmind .card_detect = rtsx_card_detect,
169 1.1.4.2 rmind
170 1.1.4.2 rmind /* write protect */
171 1.1.4.2 rmind .write_protect = rtsx_write_protect,
172 1.1.4.2 rmind
173 1.1.4.2 rmind /* bus power, clock frequency, width and ROD(OpenDrain/PushPull) */
174 1.1.4.2 rmind .bus_power = rtsx_bus_power,
175 1.1.4.2 rmind .bus_clock = rtsx_bus_clock,
176 1.1.4.2 rmind .bus_width = rtsx_bus_width,
177 1.1.4.2 rmind .bus_rod = rtsx_bus_rod,
178 1.1.4.2 rmind
179 1.1.4.2 rmind /* command execution */
180 1.1.4.2 rmind .exec_command = rtsx_exec_command,
181 1.1.4.2 rmind
182 1.1.4.2 rmind /* card interrupt */
183 1.1.4.2 rmind .card_enable_intr = NULL,
184 1.1.4.2 rmind .card_intr_ack = NULL,
185 1.1.4.2 rmind };
186 1.1.4.2 rmind
187 1.1.4.2 rmind /*
188 1.1.4.2 rmind * Called by attachment driver.
189 1.1.4.2 rmind */
190 1.1.4.2 rmind int
191 1.1.4.2 rmind rtsx_attach(struct rtsx_softc *sc, bus_space_tag_t iot,
192 1.1.4.2 rmind bus_space_handle_t ioh, bus_size_t iosize, bus_dma_tag_t dmat, int flags)
193 1.1.4.2 rmind {
194 1.1.4.2 rmind struct sdmmcbus_attach_args saa;
195 1.1.4.2 rmind uint32_t sdio_cfg;
196 1.1.4.2 rmind
197 1.1.4.2 rmind sc->sc_iot = iot;
198 1.1.4.2 rmind sc->sc_ioh = ioh;
199 1.1.4.2 rmind sc->sc_iosize = iosize;
200 1.1.4.2 rmind sc->sc_dmat = dmat;
201 1.1.4.2 rmind sc->sc_flags = flags;
202 1.1.4.2 rmind
203 1.1.4.2 rmind mutex_init(&sc->sc_host_mtx, MUTEX_DEFAULT, IPL_SDMMC);
204 1.1.4.2 rmind mutex_init(&sc->sc_intr_mtx, MUTEX_DEFAULT, IPL_SDMMC);
205 1.1.4.2 rmind cv_init(&sc->sc_intr_cv, "rtsxintr");
206 1.1.4.2 rmind
207 1.1.4.2 rmind if (rtsx_init(sc, 1))
208 1.1.4.2 rmind goto error;
209 1.1.4.2 rmind
210 1.1.4.2 rmind if (rtsx_read_cfg(sc, 0, RTSX_SDIOCFG_REG, &sdio_cfg) == 0) {
211 1.1.4.2 rmind if (sdio_cfg & (RTSX_SDIOCFG_SDIO_ONLY|RTSX_SDIOCFG_HAVE_SDIO)){
212 1.1.4.2 rmind sc->sc_flags |= RTSX_F_SDIO_SUPPORT;
213 1.1.4.2 rmind }
214 1.1.4.2 rmind }
215 1.1.4.2 rmind
216 1.1.4.2 rmind if (bus_dmamap_create(sc->sc_dmat, RTSX_HOSTCMD_BUFSIZE, 1,
217 1.1.4.2 rmind RTSX_DMA_MAX_SEGSIZE, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &sc->sc_dmap_cmd) != 0)
218 1.1.4.2 rmind goto error;
219 1.1.4.2 rmind
220 1.1.4.2 rmind /*
221 1.1.4.2 rmind * Attach the generic SD/MMC bus driver. (The bus driver must
222 1.1.4.2 rmind * not invoke any chipset functions before it is attached.)
223 1.1.4.2 rmind */
224 1.1.4.2 rmind memset(&saa, 0, sizeof(saa));
225 1.1.4.2 rmind saa.saa_busname = "sdmmc";
226 1.1.4.2 rmind saa.saa_sct = &rtsx_chip_functions;
227 1.1.4.2 rmind saa.saa_spi_sct = NULL;
228 1.1.4.2 rmind saa.saa_sch = sc;
229 1.1.4.2 rmind saa.saa_dmat = sc->sc_dmat;
230 1.1.4.2 rmind saa.saa_clkmin = SDMMC_SDCLK_400K;
231 1.1.4.2 rmind saa.saa_clkmax = 25000;
232 1.1.4.2 rmind saa.saa_caps = SMC_CAPS_DMA|SMC_CAPS_4BIT_MODE;
233 1.1.4.2 rmind
234 1.1.4.2 rmind sc->sc_sdmmc = config_found(sc->sc_dev, &saa, NULL);
235 1.1.4.2 rmind if (sc->sc_sdmmc == NULL)
236 1.1.4.2 rmind goto destroy_dmamap_cmd;
237 1.1.4.2 rmind
238 1.1.4.2 rmind /* Now handle cards discovered during attachment. */
239 1.1.4.2 rmind if (ISSET(sc->sc_flags, RTSX_F_CARD_PRESENT))
240 1.1.4.2 rmind rtsx_card_insert(sc);
241 1.1.4.2 rmind
242 1.1.4.2 rmind return 0;
243 1.1.4.2 rmind
244 1.1.4.2 rmind destroy_dmamap_cmd:
245 1.1.4.2 rmind bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmap_cmd);
246 1.1.4.2 rmind error:
247 1.1.4.2 rmind cv_destroy(&sc->sc_intr_cv);
248 1.1.4.2 rmind mutex_destroy(&sc->sc_intr_mtx);
249 1.1.4.2 rmind mutex_destroy(&sc->sc_host_mtx);
250 1.1.4.2 rmind return 1;
251 1.1.4.2 rmind }
252 1.1.4.2 rmind
253 1.1.4.2 rmind int
254 1.1.4.2 rmind rtsx_detach(struct rtsx_softc *sc, int flags)
255 1.1.4.2 rmind {
256 1.1.4.2 rmind int rv;
257 1.1.4.2 rmind
258 1.1.4.2 rmind if (sc->sc_sdmmc != NULL) {
259 1.1.4.2 rmind rv = config_detach(sc->sc_sdmmc, flags);
260 1.1.4.2 rmind if (rv != 0)
261 1.1.4.2 rmind return rv;
262 1.1.4.2 rmind sc->sc_sdmmc = NULL;
263 1.1.4.2 rmind }
264 1.1.4.2 rmind
265 1.1.4.2 rmind /* disable interrupts */
266 1.1.4.2 rmind if ((flags & DETACH_FORCE) == 0) {
267 1.1.4.2 rmind WRITE4(sc, RTSX_BIER, 0);
268 1.1.4.2 rmind rtsx_soft_reset(sc);
269 1.1.4.2 rmind }
270 1.1.4.2 rmind
271 1.1.4.2 rmind bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmap_cmd);
272 1.1.4.2 rmind cv_destroy(&sc->sc_intr_cv);
273 1.1.4.2 rmind mutex_destroy(&sc->sc_intr_mtx);
274 1.1.4.2 rmind mutex_destroy(&sc->sc_host_mtx);
275 1.1.4.2 rmind bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
276 1.1.4.2 rmind
277 1.1.4.2 rmind return 0;
278 1.1.4.2 rmind }
279 1.1.4.2 rmind
280 1.1.4.2 rmind bool
281 1.1.4.2 rmind rtsx_suspend(device_t dev, const pmf_qual_t *qual)
282 1.1.4.2 rmind {
283 1.1.4.2 rmind struct rtsx_softc *sc = device_private(dev);
284 1.1.4.2 rmind
285 1.1.4.2 rmind /* Save the host controller state. */
286 1.1.4.2 rmind rtsx_save_regs(sc);
287 1.1.4.2 rmind
288 1.1.4.2 rmind return true;
289 1.1.4.2 rmind }
290 1.1.4.2 rmind
291 1.1.4.2 rmind bool
292 1.1.4.2 rmind rtsx_resume(device_t dev, const pmf_qual_t *qual)
293 1.1.4.2 rmind {
294 1.1.4.2 rmind struct rtsx_softc *sc = device_private(dev);
295 1.1.4.2 rmind
296 1.1.4.2 rmind /* Restore the host controller state. */
297 1.1.4.2 rmind rtsx_restore_regs(sc);
298 1.1.4.2 rmind
299 1.1.4.2 rmind if (READ4(sc, RTSX_BIPR) & RTSX_SD_EXIST)
300 1.1.4.2 rmind rtsx_card_insert(sc);
301 1.1.4.2 rmind else
302 1.1.4.2 rmind rtsx_card_eject(sc);
303 1.1.4.2 rmind
304 1.1.4.2 rmind return true;
305 1.1.4.2 rmind }
306 1.1.4.2 rmind
307 1.1.4.2 rmind bool
308 1.1.4.2 rmind rtsx_shutdown(device_t dev, int flags)
309 1.1.4.2 rmind {
310 1.1.4.2 rmind struct rtsx_softc *sc = device_private(dev);
311 1.1.4.2 rmind
312 1.1.4.2 rmind /* XXX chip locks up if we don't disable it before reboot. */
313 1.1.4.2 rmind (void)rtsx_host_reset(sc);
314 1.1.4.2 rmind
315 1.1.4.2 rmind return true;
316 1.1.4.2 rmind }
317 1.1.4.2 rmind
318 1.1.4.2 rmind static int
319 1.1.4.2 rmind rtsx_init(struct rtsx_softc *sc, int attaching)
320 1.1.4.2 rmind {
321 1.1.4.2 rmind uint32_t status;
322 1.1.4.2 rmind uint8_t ver;
323 1.1.4.2 rmind int error;
324 1.1.4.2 rmind
325 1.1.4.2 rmind /* Read IC version from dummy register. */
326 1.1.4.2 rmind if (sc->sc_flags & RTSX_F_5229) {
327 1.1.4.2 rmind RTSX_READ(sc, RTSX_DUMMY_REG, &ver);
328 1.1.4.2 rmind switch (ver & 0x0f) {
329 1.1.4.2 rmind case RTSX_IC_VERSION_A:
330 1.1.4.2 rmind case RTSX_IC_VERSION_B:
331 1.1.4.2 rmind case RTSX_IC_VERSION_D:
332 1.1.4.2 rmind break;
333 1.1.4.2 rmind case RTSX_IC_VERSION_C:
334 1.1.4.2 rmind sc->sc_flags |= RTSX_F_5229_TYPE_C;
335 1.1.4.2 rmind break;
336 1.1.4.2 rmind default:
337 1.1.4.2 rmind aprint_error_dev(sc->sc_dev, "unknown ic %02x\n", ver);
338 1.1.4.2 rmind return 1;
339 1.1.4.2 rmind }
340 1.1.4.2 rmind }
341 1.1.4.2 rmind
342 1.1.4.2 rmind /* Enable interrupt write-clear (default is read-clear). */
343 1.1.4.2 rmind RTSX_CLR(sc, RTSX_NFTS_TX_CTRL, RTSX_INT_READ_CLR);
344 1.1.4.2 rmind
345 1.1.4.2 rmind /* Clear any pending interrupts. */
346 1.1.4.2 rmind status = READ4(sc, RTSX_BIPR);
347 1.1.4.2 rmind WRITE4(sc, RTSX_BIPR, status);
348 1.1.4.2 rmind
349 1.1.4.2 rmind /* Check for cards already inserted at attach time. */
350 1.1.4.2 rmind if (attaching && (status & RTSX_SD_EXIST))
351 1.1.4.2 rmind sc->sc_flags |= RTSX_F_CARD_PRESENT;
352 1.1.4.2 rmind
353 1.1.4.2 rmind /* Enable interrupts. */
354 1.1.4.2 rmind WRITE4(sc, RTSX_BIER,
355 1.1.4.2 rmind RTSX_TRANS_OK_INT_EN | RTSX_TRANS_FAIL_INT_EN | RTSX_SD_INT_EN);
356 1.1.4.2 rmind
357 1.1.4.2 rmind /* Power on SSC clock. */
358 1.1.4.2 rmind RTSX_CLR(sc, RTSX_FPDCTL, RTSX_SSC_POWER_DOWN);
359 1.1.4.2 rmind delay(200);
360 1.1.4.2 rmind
361 1.1.4.2 rmind /* XXX magic numbers from linux driver */
362 1.1.4.2 rmind if (sc->sc_flags & RTSX_F_5209)
363 1.1.4.2 rmind error = rtsx_write_phy(sc, 0x00, 0xB966);
364 1.1.4.2 rmind else
365 1.1.4.2 rmind error = rtsx_write_phy(sc, 0x00, 0xBA42);
366 1.1.4.2 rmind if (error) {
367 1.1.4.2 rmind aprint_error_dev(sc->sc_dev, "couldn't write phy register\n");
368 1.1.4.2 rmind return 1;
369 1.1.4.2 rmind }
370 1.1.4.2 rmind
371 1.1.4.2 rmind RTSX_SET(sc, RTSX_CLK_DIV, 0x07);
372 1.1.4.2 rmind
373 1.1.4.2 rmind /* Disable sleep mode. */
374 1.1.4.2 rmind RTSX_CLR(sc, RTSX_HOST_SLEEP_STATE,
375 1.1.4.2 rmind RTSX_HOST_ENTER_S1 | RTSX_HOST_ENTER_S3);
376 1.1.4.2 rmind
377 1.1.4.2 rmind /* Disable card clock. */
378 1.1.4.2 rmind RTSX_CLR(sc, RTSX_CARD_CLK_EN, RTSX_CARD_CLK_EN_ALL);
379 1.1.4.2 rmind
380 1.1.4.2 rmind RTSX_CLR(sc, RTSX_CHANGE_LINK_STATE,
381 1.1.4.2 rmind RTSX_FORCE_RST_CORE_EN | RTSX_NON_STICKY_RST_N_DBG | 0x04);
382 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_SD30_DRIVE_SEL, RTSX_SD30_DRIVE_SEL_3V3);
383 1.1.4.2 rmind
384 1.1.4.2 rmind /* Enable SSC clock. */
385 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_SSC_CTL1, RTSX_SSC_8X_EN | RTSX_SSC_SEL_4M);
386 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_SSC_CTL2, 0x12);
387 1.1.4.2 rmind
388 1.1.4.2 rmind RTSX_SET(sc, RTSX_CHANGE_LINK_STATE, RTSX_MAC_PHY_RST_N_DBG);
389 1.1.4.2 rmind RTSX_SET(sc, RTSX_IRQSTAT0, RTSX_LINK_READY_INT);
390 1.1.4.2 rmind
391 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_PERST_GLITCH_WIDTH, 0x80);
392 1.1.4.2 rmind
393 1.1.4.2 rmind /* Set RC oscillator to 400K. */
394 1.1.4.2 rmind RTSX_CLR(sc, RTSX_RCCTL, RTSX_RCCTL_F_2M);
395 1.1.4.2 rmind
396 1.1.4.2 rmind /* Request clock by driving CLKREQ pin to zero. */
397 1.1.4.2 rmind RTSX_SET(sc, RTSX_PETXCFG, RTSX_PETXCFG_CLKREQ_PIN);
398 1.1.4.2 rmind
399 1.1.4.2 rmind /* Set up LED GPIO. */
400 1.1.4.2 rmind if (sc->sc_flags & RTSX_F_5209) {
401 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CARD_GPIO, 0x03);
402 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CARD_GPIO_DIR, 0x03);
403 1.1.4.2 rmind } else {
404 1.1.4.2 rmind RTSX_SET(sc, RTSX_GPIO_CTL, RTSX_GPIO_LED_ON);
405 1.1.4.2 rmind /* Switch LDO3318 source from DV33 to 3V3. */
406 1.1.4.2 rmind RTSX_CLR(sc, RTSX_LDO_PWR_SEL, RTSX_LDO_PWR_SEL_DV33);
407 1.1.4.2 rmind RTSX_SET(sc, RTSX_LDO_PWR_SEL, RTSX_LDO_PWR_SEL_3V3);
408 1.1.4.2 rmind /* Set default OLT blink period. */
409 1.1.4.2 rmind RTSX_SET(sc, RTSX_OLT_LED_CTL, RTSX_OLT_LED_PERIOD);
410 1.1.4.2 rmind }
411 1.1.4.2 rmind
412 1.1.4.2 rmind return 0;
413 1.1.4.2 rmind }
414 1.1.4.2 rmind
415 1.1.4.2 rmind int
416 1.1.4.2 rmind rtsx_led_enable(struct rtsx_softc *sc)
417 1.1.4.2 rmind {
418 1.1.4.2 rmind
419 1.1.4.2 rmind if (sc->sc_flags & RTSX_F_5209) {
420 1.1.4.2 rmind RTSX_CLR(sc, RTSX_CARD_GPIO, RTSX_CARD_GPIO_LED_OFF);
421 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CARD_AUTO_BLINK,
422 1.1.4.2 rmind RTSX_LED_BLINK_EN | RTSX_LED_BLINK_SPEED);
423 1.1.4.2 rmind } else {
424 1.1.4.2 rmind RTSX_SET(sc, RTSX_GPIO_CTL, RTSX_GPIO_LED_ON);
425 1.1.4.2 rmind RTSX_SET(sc, RTSX_OLT_LED_CTL, RTSX_OLT_LED_AUTOBLINK);
426 1.1.4.2 rmind }
427 1.1.4.2 rmind
428 1.1.4.2 rmind return 0;
429 1.1.4.2 rmind }
430 1.1.4.2 rmind
431 1.1.4.2 rmind int
432 1.1.4.2 rmind rtsx_led_disable(struct rtsx_softc *sc)
433 1.1.4.2 rmind {
434 1.1.4.2 rmind
435 1.1.4.2 rmind if (sc->sc_flags & RTSX_F_5209) {
436 1.1.4.2 rmind RTSX_CLR(sc, RTSX_CARD_AUTO_BLINK, RTSX_LED_BLINK_EN);
437 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CARD_GPIO, RTSX_CARD_GPIO_LED_OFF);
438 1.1.4.2 rmind } else {
439 1.1.4.2 rmind RTSX_CLR(sc, RTSX_OLT_LED_CTL, RTSX_OLT_LED_AUTOBLINK);
440 1.1.4.2 rmind RTSX_CLR(sc, RTSX_GPIO_CTL, RTSX_GPIO_LED_ON);
441 1.1.4.2 rmind }
442 1.1.4.2 rmind
443 1.1.4.2 rmind return 0;
444 1.1.4.2 rmind }
445 1.1.4.2 rmind
446 1.1.4.2 rmind /*
447 1.1.4.2 rmind * Reset the host controller. Called during initialization, when
448 1.1.4.2 rmind * cards are removed, upon resume, and during error recovery.
449 1.1.4.2 rmind */
450 1.1.4.2 rmind int
451 1.1.4.2 rmind rtsx_host_reset(sdmmc_chipset_handle_t sch)
452 1.1.4.2 rmind {
453 1.1.4.2 rmind struct rtsx_softc *sc = sch;
454 1.1.4.2 rmind int error;
455 1.1.4.2 rmind
456 1.1.4.2 rmind DPRINTF(1,("%s: host reset\n", DEVNAME(sc)));
457 1.1.4.2 rmind
458 1.1.4.2 rmind mutex_enter(&sc->sc_host_mtx);
459 1.1.4.2 rmind
460 1.1.4.2 rmind if (ISSET(sc->sc_flags, RTSX_F_CARD_PRESENT))
461 1.1.4.2 rmind rtsx_soft_reset(sc);
462 1.1.4.2 rmind
463 1.1.4.2 rmind error = rtsx_init(sc, 0);
464 1.1.4.2 rmind
465 1.1.4.2 rmind mutex_exit(&sc->sc_host_mtx);
466 1.1.4.2 rmind
467 1.1.4.2 rmind return error;
468 1.1.4.2 rmind }
469 1.1.4.2 rmind
470 1.1.4.2 rmind static uint32_t
471 1.1.4.2 rmind rtsx_host_ocr(sdmmc_chipset_handle_t sch)
472 1.1.4.2 rmind {
473 1.1.4.2 rmind
474 1.1.4.2 rmind return RTSX_SUPPORT_VOLTAGE;
475 1.1.4.2 rmind }
476 1.1.4.2 rmind
477 1.1.4.2 rmind static int
478 1.1.4.2 rmind rtsx_host_maxblklen(sdmmc_chipset_handle_t sch)
479 1.1.4.2 rmind {
480 1.1.4.2 rmind
481 1.1.4.2 rmind return 512;
482 1.1.4.2 rmind }
483 1.1.4.2 rmind
484 1.1.4.2 rmind /*
485 1.1.4.2 rmind * Return non-zero if the card is currently inserted.
486 1.1.4.2 rmind */
487 1.1.4.2 rmind static int
488 1.1.4.2 rmind rtsx_card_detect(sdmmc_chipset_handle_t sch)
489 1.1.4.2 rmind {
490 1.1.4.2 rmind struct rtsx_softc *sc = sch;
491 1.1.4.2 rmind
492 1.1.4.2 rmind return ISSET(sc->sc_flags, RTSX_F_CARD_PRESENT);
493 1.1.4.2 rmind }
494 1.1.4.2 rmind
495 1.1.4.2 rmind static int
496 1.1.4.2 rmind rtsx_write_protect(sdmmc_chipset_handle_t sch)
497 1.1.4.2 rmind {
498 1.1.4.2 rmind
499 1.1.4.2 rmind return 0; /* XXX */
500 1.1.4.2 rmind }
501 1.1.4.2 rmind
502 1.1.4.2 rmind /*
503 1.1.4.2 rmind * Notice that the meaning of RTSX_PWR_GATE_CTRL changes between RTS5209 and
504 1.1.4.2 rmind * RTS5229. In RTS5209 it is a mask of disabled power gates, while in RTS5229
505 1.1.4.2 rmind * it is a mask of *enabled* gates.
506 1.1.4.2 rmind */
507 1.1.4.2 rmind
508 1.1.4.2 rmind static int
509 1.1.4.2 rmind rtsx_bus_power_off(struct rtsx_softc *sc)
510 1.1.4.2 rmind {
511 1.1.4.2 rmind int error;
512 1.1.4.2 rmind uint8_t disable3;
513 1.1.4.2 rmind
514 1.1.4.2 rmind error = rtsx_stop_sd_clock(sc);
515 1.1.4.2 rmind if (error)
516 1.1.4.2 rmind return error;
517 1.1.4.2 rmind
518 1.1.4.2 rmind /* Disable SD output. */
519 1.1.4.2 rmind RTSX_CLR(sc, RTSX_CARD_OE, RTSX_CARD_OUTPUT_EN);
520 1.1.4.2 rmind
521 1.1.4.2 rmind /* Turn off power. */
522 1.1.4.2 rmind disable3 = RTSX_PULL_CTL_DISABLE3;
523 1.1.4.2 rmind if (sc->sc_flags & RTSX_F_5209)
524 1.1.4.2 rmind RTSX_SET(sc, RTSX_PWR_GATE_CTRL, RTSX_LDO3318_OFF);
525 1.1.4.2 rmind else {
526 1.1.4.2 rmind RTSX_CLR(sc, RTSX_PWR_GATE_CTRL, RTSX_LDO3318_VCC1 |
527 1.1.4.2 rmind RTSX_LDO3318_VCC2);
528 1.1.4.2 rmind if (sc->sc_flags & RTSX_F_5229_TYPE_C)
529 1.1.4.2 rmind disable3 = RTSX_PULL_CTL_DISABLE3_TYPE_C;
530 1.1.4.2 rmind }
531 1.1.4.2 rmind
532 1.1.4.2 rmind RTSX_SET(sc, RTSX_CARD_PWR_CTL, RTSX_SD_PWR_OFF);
533 1.1.4.2 rmind RTSX_CLR(sc, RTSX_CARD_PWR_CTL, RTSX_PMOS_STRG_800mA);
534 1.1.4.2 rmind
535 1.1.4.2 rmind /* Disable pull control. */
536 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CARD_PULL_CTL1, RTSX_PULL_CTL_DISABLE12);
537 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CARD_PULL_CTL2, RTSX_PULL_CTL_DISABLE12);
538 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CARD_PULL_CTL3, disable3);
539 1.1.4.2 rmind
540 1.1.4.2 rmind return 0;
541 1.1.4.2 rmind }
542 1.1.4.2 rmind
543 1.1.4.2 rmind static int
544 1.1.4.2 rmind rtsx_bus_power_on(struct rtsx_softc *sc)
545 1.1.4.2 rmind {
546 1.1.4.2 rmind uint8_t enable3;
547 1.1.4.2 rmind
548 1.1.4.2 rmind /* Select SD card. */
549 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CARD_SELECT, RTSX_SD_MOD_SEL);
550 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CARD_SHARE_MODE, RTSX_CARD_SHARE_48_SD);
551 1.1.4.2 rmind RTSX_SET(sc, RTSX_CARD_CLK_EN, RTSX_SD_CLK_EN);
552 1.1.4.2 rmind
553 1.1.4.2 rmind /* Enable pull control. */
554 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CARD_PULL_CTL1, RTSX_PULL_CTL_ENABLE12);
555 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CARD_PULL_CTL2, RTSX_PULL_CTL_ENABLE12);
556 1.1.4.2 rmind if (sc->sc_flags & RTSX_F_5229_TYPE_C)
557 1.1.4.2 rmind enable3 = RTSX_PULL_CTL_ENABLE3_TYPE_C;
558 1.1.4.2 rmind else
559 1.1.4.2 rmind enable3 = RTSX_PULL_CTL_ENABLE3;
560 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CARD_PULL_CTL3, enable3);
561 1.1.4.2 rmind
562 1.1.4.2 rmind /*
563 1.1.4.2 rmind * To avoid a current peak, enable card power in two phases with a
564 1.1.4.2 rmind * delay in between.
565 1.1.4.2 rmind */
566 1.1.4.2 rmind
567 1.1.4.2 rmind /* Partial power. */
568 1.1.4.2 rmind RTSX_SET(sc, RTSX_CARD_PWR_CTL, RTSX_SD_PARTIAL_PWR_ON);
569 1.1.4.2 rmind if (sc->sc_flags & RTSX_F_5209)
570 1.1.4.2 rmind RTSX_SET(sc, RTSX_PWR_GATE_CTRL, RTSX_LDO3318_SUSPEND);
571 1.1.4.2 rmind else
572 1.1.4.2 rmind RTSX_SET(sc, RTSX_PWR_GATE_CTRL, RTSX_LDO3318_VCC1);
573 1.1.4.2 rmind
574 1.1.4.2 rmind delay(200);
575 1.1.4.2 rmind
576 1.1.4.2 rmind /* Full power. */
577 1.1.4.2 rmind RTSX_CLR(sc, RTSX_CARD_PWR_CTL, RTSX_SD_PWR_OFF);
578 1.1.4.2 rmind if (sc->sc_flags & RTSX_F_5209)
579 1.1.4.2 rmind RTSX_CLR(sc, RTSX_PWR_GATE_CTRL, RTSX_LDO3318_OFF);
580 1.1.4.2 rmind else
581 1.1.4.2 rmind RTSX_SET(sc, RTSX_PWR_GATE_CTRL, RTSX_LDO3318_VCC2);
582 1.1.4.2 rmind
583 1.1.4.2 rmind /* Enable SD card output. */
584 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CARD_OE, RTSX_SD_OUTPUT_EN);
585 1.1.4.2 rmind
586 1.1.4.2 rmind return 0;
587 1.1.4.2 rmind }
588 1.1.4.2 rmind
589 1.1.4.2 rmind static int
590 1.1.4.2 rmind rtsx_set_bus_width(struct rtsx_softc *sc, int width)
591 1.1.4.2 rmind {
592 1.1.4.2 rmind uint32_t bus_width;
593 1.1.4.2 rmind
594 1.1.4.2 rmind DPRINTF(1,("%s: bus width=%d\n", DEVNAME(sc), width));
595 1.1.4.2 rmind
596 1.1.4.2 rmind switch (width) {
597 1.1.4.2 rmind case 8:
598 1.1.4.2 rmind bus_width = RTSX_BUS_WIDTH_8;
599 1.1.4.2 rmind break;
600 1.1.4.2 rmind case 4:
601 1.1.4.2 rmind bus_width = RTSX_BUS_WIDTH_4;
602 1.1.4.2 rmind break;
603 1.1.4.2 rmind case 1:
604 1.1.4.2 rmind bus_width = RTSX_BUS_WIDTH_1;
605 1.1.4.2 rmind break;
606 1.1.4.2 rmind default:
607 1.1.4.2 rmind return EINVAL;
608 1.1.4.2 rmind }
609 1.1.4.2 rmind
610 1.1.4.2 rmind if (bus_width == RTSX_BUS_WIDTH_1)
611 1.1.4.2 rmind RTSX_CLR(sc, RTSX_SD_CFG1, RTSX_BUS_WIDTH_MASK);
612 1.1.4.2 rmind else
613 1.1.4.2 rmind RTSX_SET(sc, RTSX_SD_CFG1, bus_width);
614 1.1.4.2 rmind
615 1.1.4.2 rmind return 0;
616 1.1.4.2 rmind }
617 1.1.4.2 rmind
618 1.1.4.2 rmind static int
619 1.1.4.2 rmind rtsx_stop_sd_clock(struct rtsx_softc *sc)
620 1.1.4.2 rmind {
621 1.1.4.2 rmind
622 1.1.4.2 rmind RTSX_CLR(sc, RTSX_CARD_CLK_EN, RTSX_CARD_CLK_EN_ALL);
623 1.1.4.2 rmind RTSX_SET(sc, RTSX_SD_BUS_STAT, RTSX_SD_CLK_FORCE_STOP);
624 1.1.4.2 rmind
625 1.1.4.2 rmind return 0;
626 1.1.4.2 rmind }
627 1.1.4.2 rmind
628 1.1.4.2 rmind static int
629 1.1.4.2 rmind rtsx_switch_sd_clock(struct rtsx_softc *sc, uint8_t n, int div, int mcu)
630 1.1.4.2 rmind {
631 1.1.4.2 rmind
632 1.1.4.2 rmind /* Enable SD 2.0 mode. */
633 1.1.4.2 rmind RTSX_CLR(sc, RTSX_SD_CFG1, RTSX_SD_MODE_MASK);
634 1.1.4.2 rmind
635 1.1.4.2 rmind RTSX_SET(sc, RTSX_CLK_CTL, RTSX_CLK_LOW_FREQ);
636 1.1.4.2 rmind
637 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CARD_CLK_SOURCE,
638 1.1.4.2 rmind RTSX_CRC_FIX_CLK | RTSX_SD30_VAR_CLK0 | RTSX_SAMPLE_VAR_CLK1);
639 1.1.4.2 rmind RTSX_CLR(sc, RTSX_SD_SAMPLE_POINT_CTL, RTSX_SD20_RX_SEL_MASK);
640 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_SD_PUSH_POINT_CTL, RTSX_SD20_TX_NEG_EDGE);
641 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CLK_DIV, (div << 4) | mcu);
642 1.1.4.2 rmind RTSX_CLR(sc, RTSX_SSC_CTL1, RTSX_RSTB);
643 1.1.4.2 rmind RTSX_CLR(sc, RTSX_SSC_CTL2, RTSX_SSC_DEPTH_MASK);
644 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_SSC_DIV_N_0, n);
645 1.1.4.2 rmind RTSX_SET(sc, RTSX_SSC_CTL1, RTSX_RSTB);
646 1.1.4.2 rmind delay(100);
647 1.1.4.2 rmind
648 1.1.4.2 rmind RTSX_CLR(sc, RTSX_CLK_CTL, RTSX_CLK_LOW_FREQ);
649 1.1.4.2 rmind
650 1.1.4.2 rmind return 0;
651 1.1.4.2 rmind }
652 1.1.4.2 rmind
653 1.1.4.2 rmind /*
654 1.1.4.2 rmind * Set or change SD bus voltage and enable or disable SD bus power.
655 1.1.4.2 rmind * Return zero on success.
656 1.1.4.2 rmind */
657 1.1.4.2 rmind static int
658 1.1.4.2 rmind rtsx_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
659 1.1.4.2 rmind {
660 1.1.4.2 rmind struct rtsx_softc *sc = sch;
661 1.1.4.2 rmind int error = 0;
662 1.1.4.2 rmind
663 1.1.4.2 rmind DPRINTF(1,("%s: voltage change ocr=0x%x\n", DEVNAME(sc), ocr));
664 1.1.4.2 rmind
665 1.1.4.2 rmind mutex_enter(&sc->sc_host_mtx);
666 1.1.4.2 rmind
667 1.1.4.2 rmind /*
668 1.1.4.2 rmind * Disable bus power before voltage change.
669 1.1.4.2 rmind */
670 1.1.4.2 rmind error = rtsx_bus_power_off(sc);
671 1.1.4.2 rmind if (error)
672 1.1.4.2 rmind goto ret;
673 1.1.4.2 rmind
674 1.1.4.2 rmind delay(200);
675 1.1.4.2 rmind
676 1.1.4.2 rmind /* If power is disabled, reset the host and return now. */
677 1.1.4.2 rmind if (ocr == 0) {
678 1.1.4.2 rmind mutex_exit(&sc->sc_host_mtx);
679 1.1.4.2 rmind (void)rtsx_host_reset(sc);
680 1.1.4.2 rmind return 0;
681 1.1.4.2 rmind }
682 1.1.4.2 rmind
683 1.1.4.2 rmind if (!ISSET(ocr, RTSX_SUPPORT_VOLTAGE)) {
684 1.1.4.2 rmind /* Unsupported voltage level requested. */
685 1.1.4.2 rmind DPRINTF(1,("%s: unsupported voltage ocr=0x%x\n",
686 1.1.4.2 rmind DEVNAME(sc), ocr));
687 1.1.4.2 rmind error = EINVAL;
688 1.1.4.2 rmind goto ret;
689 1.1.4.2 rmind }
690 1.1.4.2 rmind
691 1.1.4.2 rmind error = rtsx_set_bus_width(sc, 1);
692 1.1.4.2 rmind if (error)
693 1.1.4.2 rmind goto ret;
694 1.1.4.2 rmind
695 1.1.4.2 rmind error = rtsx_bus_power_on(sc);
696 1.1.4.2 rmind ret:
697 1.1.4.2 rmind mutex_exit(&sc->sc_host_mtx);
698 1.1.4.2 rmind
699 1.1.4.2 rmind return error;
700 1.1.4.2 rmind }
701 1.1.4.2 rmind
702 1.1.4.2 rmind /*
703 1.1.4.2 rmind * Set or change SDCLK frequency or disable the SD clock.
704 1.1.4.2 rmind * Return zero on success.
705 1.1.4.2 rmind */
706 1.1.4.2 rmind static int
707 1.1.4.2 rmind rtsx_bus_clock(sdmmc_chipset_handle_t sch, int freq)
708 1.1.4.2 rmind {
709 1.1.4.2 rmind struct rtsx_softc *sc = sch;
710 1.1.4.2 rmind uint8_t n;
711 1.1.4.2 rmind int div;
712 1.1.4.2 rmind int mcu;
713 1.1.4.2 rmind int error = 0;
714 1.1.4.2 rmind
715 1.1.4.2 rmind DPRINTF(1,("%s: bus clock change freq=%d\n", DEVNAME(sc), freq));
716 1.1.4.2 rmind
717 1.1.4.2 rmind mutex_enter(&sc->sc_host_mtx);
718 1.1.4.2 rmind
719 1.1.4.2 rmind if (freq == SDMMC_SDCLK_OFF) {
720 1.1.4.2 rmind error = rtsx_stop_sd_clock(sc);
721 1.1.4.2 rmind goto ret;
722 1.1.4.2 rmind }
723 1.1.4.2 rmind
724 1.1.4.2 rmind /*
725 1.1.4.2 rmind * Configure the clock frequency.
726 1.1.4.2 rmind */
727 1.1.4.2 rmind switch (freq) {
728 1.1.4.2 rmind case SDMMC_SDCLK_400K:
729 1.1.4.2 rmind n = 80; /* minimum */
730 1.1.4.2 rmind div = RTSX_CLK_DIV_8;
731 1.1.4.2 rmind mcu = 7;
732 1.1.4.2 rmind RTSX_SET(sc, RTSX_SD_CFG1, RTSX_CLK_DIVIDE_128);
733 1.1.4.2 rmind break;
734 1.1.4.2 rmind case 20000:
735 1.1.4.2 rmind n = 80;
736 1.1.4.2 rmind div = RTSX_CLK_DIV_4;
737 1.1.4.2 rmind mcu = 7;
738 1.1.4.2 rmind RTSX_CLR(sc, RTSX_SD_CFG1, RTSX_CLK_DIVIDE_MASK);
739 1.1.4.2 rmind break;
740 1.1.4.2 rmind case 25000:
741 1.1.4.2 rmind n = 100;
742 1.1.4.2 rmind div = RTSX_CLK_DIV_4;
743 1.1.4.2 rmind mcu = 7;
744 1.1.4.2 rmind RTSX_CLR(sc, RTSX_SD_CFG1, RTSX_CLK_DIVIDE_MASK);
745 1.1.4.2 rmind break;
746 1.1.4.2 rmind case 30000:
747 1.1.4.2 rmind n = 120;
748 1.1.4.2 rmind div = RTSX_CLK_DIV_4;
749 1.1.4.2 rmind mcu = 7;
750 1.1.4.2 rmind RTSX_CLR(sc, RTSX_SD_CFG1, RTSX_CLK_DIVIDE_MASK);
751 1.1.4.2 rmind break;
752 1.1.4.2 rmind case 40000:
753 1.1.4.2 rmind n = 80;
754 1.1.4.2 rmind div = RTSX_CLK_DIV_2;
755 1.1.4.2 rmind mcu = 7;
756 1.1.4.2 rmind RTSX_CLR(sc, RTSX_SD_CFG1, RTSX_CLK_DIVIDE_MASK);
757 1.1.4.2 rmind break;
758 1.1.4.2 rmind case 50000:
759 1.1.4.2 rmind n = 100;
760 1.1.4.2 rmind div = RTSX_CLK_DIV_2;
761 1.1.4.2 rmind mcu = 6;
762 1.1.4.2 rmind RTSX_CLR(sc, RTSX_SD_CFG1, RTSX_CLK_DIVIDE_MASK);
763 1.1.4.2 rmind break;
764 1.1.4.2 rmind default:
765 1.1.4.2 rmind error = EINVAL;
766 1.1.4.2 rmind goto ret;
767 1.1.4.2 rmind }
768 1.1.4.2 rmind
769 1.1.4.2 rmind /*
770 1.1.4.2 rmind * Enable SD clock.
771 1.1.4.2 rmind */
772 1.1.4.2 rmind error = rtsx_switch_sd_clock(sc, n, div, mcu);
773 1.1.4.2 rmind ret:
774 1.1.4.2 rmind mutex_exit(&sc->sc_host_mtx);
775 1.1.4.2 rmind
776 1.1.4.2 rmind return error;
777 1.1.4.2 rmind }
778 1.1.4.2 rmind
779 1.1.4.2 rmind static int
780 1.1.4.2 rmind rtsx_bus_width(sdmmc_chipset_handle_t sch, int width)
781 1.1.4.2 rmind {
782 1.1.4.2 rmind struct rtsx_softc *sc = sch;
783 1.1.4.2 rmind
784 1.1.4.2 rmind return rtsx_set_bus_width(sc, width);
785 1.1.4.2 rmind }
786 1.1.4.2 rmind
787 1.1.4.2 rmind static int
788 1.1.4.2 rmind rtsx_bus_rod(sdmmc_chipset_handle_t sch, int on)
789 1.1.4.2 rmind {
790 1.1.4.2 rmind
791 1.1.4.2 rmind /* Not support */
792 1.1.4.2 rmind return -1;
793 1.1.4.2 rmind }
794 1.1.4.2 rmind
795 1.1.4.2 rmind static int
796 1.1.4.2 rmind rtsx_read(struct rtsx_softc *sc, uint16_t addr, uint8_t *val)
797 1.1.4.2 rmind {
798 1.1.4.2 rmind int tries = 1024;
799 1.1.4.2 rmind uint32_t reg;
800 1.1.4.2 rmind
801 1.1.4.2 rmind WRITE4(sc, RTSX_HAIMR, RTSX_HAIMR_BUSY |
802 1.1.4.2 rmind (uint32_t)((addr & 0x3FFF) << 16));
803 1.1.4.2 rmind
804 1.1.4.2 rmind while (tries--) {
805 1.1.4.2 rmind reg = READ4(sc, RTSX_HAIMR);
806 1.1.4.2 rmind if (!(reg & RTSX_HAIMR_BUSY))
807 1.1.4.2 rmind break;
808 1.1.4.2 rmind }
809 1.1.4.2 rmind
810 1.1.4.2 rmind *val = (reg & 0xff);
811 1.1.4.2 rmind return (tries == 0) ? ETIMEDOUT : 0;
812 1.1.4.2 rmind }
813 1.1.4.2 rmind
814 1.1.4.2 rmind static int
815 1.1.4.2 rmind rtsx_write(struct rtsx_softc *sc, uint16_t addr, uint8_t mask, uint8_t val)
816 1.1.4.2 rmind {
817 1.1.4.2 rmind int tries = 1024;
818 1.1.4.2 rmind uint32_t reg;
819 1.1.4.2 rmind
820 1.1.4.2 rmind WRITE4(sc, RTSX_HAIMR,
821 1.1.4.2 rmind RTSX_HAIMR_BUSY | RTSX_HAIMR_WRITE |
822 1.1.4.2 rmind (uint32_t)(((addr & 0x3FFF) << 16) |
823 1.1.4.2 rmind (mask << 8) | val));
824 1.1.4.2 rmind
825 1.1.4.2 rmind while (tries--) {
826 1.1.4.2 rmind reg = READ4(sc, RTSX_HAIMR);
827 1.1.4.2 rmind if (!(reg & RTSX_HAIMR_BUSY)) {
828 1.1.4.2 rmind if (val != (reg & 0xff))
829 1.1.4.2 rmind return EIO;
830 1.1.4.2 rmind return 0;
831 1.1.4.2 rmind }
832 1.1.4.2 rmind }
833 1.1.4.2 rmind return ETIMEDOUT;
834 1.1.4.2 rmind }
835 1.1.4.2 rmind
836 1.1.4.2 rmind #ifdef notyet
837 1.1.4.2 rmind static int
838 1.1.4.2 rmind rtsx_read_phy(struct rtsx_softc *sc, uint8_t addr, uint16_t *val)
839 1.1.4.2 rmind {
840 1.1.4.2 rmind int timeout = 100000;
841 1.1.4.2 rmind uint8_t data0;
842 1.1.4.2 rmind uint8_t data1;
843 1.1.4.2 rmind uint8_t rwctl;
844 1.1.4.2 rmind
845 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_PHY_ADDR, addr);
846 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_PHY_RWCTL, RTSX_PHY_BUSY|RTSX_PHY_READ);
847 1.1.4.2 rmind
848 1.1.4.2 rmind while (timeout--) {
849 1.1.4.2 rmind RTSX_READ(sc, RTSX_PHY_RWCTL, &rwctl);
850 1.1.4.2 rmind if (!(rwctl & RTSX_PHY_BUSY))
851 1.1.4.2 rmind break;
852 1.1.4.2 rmind }
853 1.1.4.2 rmind if (timeout == 0)
854 1.1.4.2 rmind return ETIMEDOUT;
855 1.1.4.2 rmind
856 1.1.4.2 rmind RTSX_READ(sc, RTSX_PHY_DATA0, &data0);
857 1.1.4.2 rmind RTSX_READ(sc, RTSX_PHY_DATA1, &data1);
858 1.1.4.2 rmind *val = data0 | (data1 << 8);
859 1.1.4.2 rmind
860 1.1.4.2 rmind return 0;
861 1.1.4.2 rmind }
862 1.1.4.2 rmind #endif
863 1.1.4.2 rmind
864 1.1.4.2 rmind static int
865 1.1.4.2 rmind rtsx_write_phy(struct rtsx_softc *sc, uint8_t addr, uint16_t val)
866 1.1.4.2 rmind {
867 1.1.4.2 rmind int timeout = 100000;
868 1.1.4.2 rmind uint8_t rwctl;
869 1.1.4.2 rmind
870 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_PHY_DATA0, val);
871 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_PHY_DATA1, val >> 8);
872 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_PHY_ADDR, addr);
873 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_PHY_RWCTL, RTSX_PHY_BUSY|RTSX_PHY_WRITE);
874 1.1.4.2 rmind
875 1.1.4.2 rmind while (timeout--) {
876 1.1.4.2 rmind RTSX_READ(sc, RTSX_PHY_RWCTL, &rwctl);
877 1.1.4.2 rmind if (!(rwctl & RTSX_PHY_BUSY))
878 1.1.4.2 rmind break;
879 1.1.4.2 rmind }
880 1.1.4.2 rmind if (timeout == 0)
881 1.1.4.2 rmind return ETIMEDOUT;
882 1.1.4.2 rmind
883 1.1.4.2 rmind return 0;
884 1.1.4.2 rmind }
885 1.1.4.2 rmind
886 1.1.4.2 rmind static int
887 1.1.4.2 rmind rtsx_read_cfg(struct rtsx_softc *sc, uint8_t func, uint16_t addr, uint32_t *val)
888 1.1.4.2 rmind {
889 1.1.4.2 rmind int tries = 1024;
890 1.1.4.2 rmind uint8_t data0, data1, data2, data3, rwctl;
891 1.1.4.2 rmind
892 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CFGADDR0, addr);
893 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CFGADDR1, addr >> 8);
894 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CFGRWCTL, RTSX_CFG_BUSY | (func & 0x03 << 4));
895 1.1.4.2 rmind
896 1.1.4.2 rmind while (tries--) {
897 1.1.4.2 rmind RTSX_READ(sc, RTSX_CFGRWCTL, &rwctl);
898 1.1.4.2 rmind if (!(rwctl & RTSX_CFG_BUSY))
899 1.1.4.2 rmind break;
900 1.1.4.2 rmind }
901 1.1.4.2 rmind if (tries == 0)
902 1.1.4.2 rmind return EIO;
903 1.1.4.2 rmind
904 1.1.4.2 rmind RTSX_READ(sc, RTSX_CFGDATA0, &data0);
905 1.1.4.2 rmind RTSX_READ(sc, RTSX_CFGDATA1, &data1);
906 1.1.4.2 rmind RTSX_READ(sc, RTSX_CFGDATA2, &data2);
907 1.1.4.2 rmind RTSX_READ(sc, RTSX_CFGDATA3, &data3);
908 1.1.4.2 rmind *val = (data3 << 24) | (data2 << 16) | (data1 << 8) | data0;
909 1.1.4.2 rmind
910 1.1.4.2 rmind return 0;
911 1.1.4.2 rmind }
912 1.1.4.2 rmind
913 1.1.4.2 rmind #ifdef notyet
914 1.1.4.2 rmind static int
915 1.1.4.2 rmind rtsx_write_cfg(struct rtsx_softc *sc, uint8_t func, uint16_t addr,
916 1.1.4.2 rmind uint32_t mask, uint32_t val)
917 1.1.4.2 rmind {
918 1.1.4.2 rmind uint32_t writemask = 0;
919 1.1.4.2 rmind int i, tries = 1024;
920 1.1.4.2 rmind uint8_t rwctl;
921 1.1.4.2 rmind
922 1.1.4.2 rmind for (i = 0; i < 4; i++) {
923 1.1.4.2 rmind if (mask & 0xff) {
924 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CFGDATA0 + i, val & mask & 0xff);
925 1.1.4.2 rmind writemask |= (1 << i);
926 1.1.4.2 rmind }
927 1.1.4.2 rmind mask >>= 8;
928 1.1.4.2 rmind val >>= 8;
929 1.1.4.2 rmind }
930 1.1.4.2 rmind
931 1.1.4.2 rmind if (writemask) {
932 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CFGADDR0, addr);
933 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CFGADDR1, addr >> 8);
934 1.1.4.2 rmind RTSX_WRITE(sc, RTSX_CFGRWCTL,
935 1.1.4.2 rmind RTSX_CFG_BUSY | writemask | (func & 0x03 << 4));
936 1.1.4.2 rmind }
937 1.1.4.2 rmind
938 1.1.4.2 rmind while (tries--) {
939 1.1.4.2 rmind RTSX_READ(sc, RTSX_CFGRWCTL, &rwctl);
940 1.1.4.2 rmind if (!(rwctl & RTSX_CFG_BUSY))
941 1.1.4.2 rmind break;
942 1.1.4.2 rmind }
943 1.1.4.2 rmind if (tries == 0)
944 1.1.4.2 rmind return EIO;
945 1.1.4.2 rmind
946 1.1.4.2 rmind return 0;
947 1.1.4.2 rmind }
948 1.1.4.2 rmind #endif
949 1.1.4.2 rmind
950 1.1.4.2 rmind /* Append a properly encoded host command to the host command buffer. */
951 1.1.4.2 rmind static void
952 1.1.4.2 rmind rtsx_hostcmd(uint32_t *cmdbuf, int *n, uint8_t cmd, uint16_t reg,
953 1.1.4.2 rmind uint8_t mask, uint8_t data)
954 1.1.4.2 rmind {
955 1.1.4.2 rmind
956 1.1.4.2 rmind KASSERT(*n < RTSX_HOSTCMD_MAX);
957 1.1.4.2 rmind
958 1.1.4.2 rmind cmdbuf[(*n)++] = htole32((uint32_t)(cmd & 0x3) << 30) |
959 1.1.4.2 rmind ((uint32_t)(reg & 0x3fff) << 16) |
960 1.1.4.2 rmind ((uint32_t)(mask) << 8) |
961 1.1.4.2 rmind ((uint32_t)data);
962 1.1.4.2 rmind }
963 1.1.4.2 rmind
964 1.1.4.2 rmind static void
965 1.1.4.2 rmind rtsx_save_regs(struct rtsx_softc *sc)
966 1.1.4.2 rmind {
967 1.1.4.2 rmind int i;
968 1.1.4.2 rmind uint16_t reg;
969 1.1.4.2 rmind
970 1.1.4.2 rmind mutex_enter(&sc->sc_host_mtx);
971 1.1.4.2 rmind
972 1.1.4.2 rmind i = 0;
973 1.1.4.2 rmind for (reg = 0xFDA0; reg < 0xFDAE; reg++)
974 1.1.4.2 rmind (void)rtsx_read(sc, reg, &sc->sc_regs[i++]);
975 1.1.4.2 rmind for (reg = 0xFD52; reg < 0xFD69; reg++)
976 1.1.4.2 rmind (void)rtsx_read(sc, reg, &sc->sc_regs[i++]);
977 1.1.4.2 rmind for (reg = 0xFE20; reg < 0xFE34; reg++)
978 1.1.4.2 rmind (void)rtsx_read(sc, reg, &sc->sc_regs[i++]);
979 1.1.4.2 rmind
980 1.1.4.2 rmind sc->sc_regs4[0] = READ4(sc, RTSX_HCBAR);
981 1.1.4.2 rmind sc->sc_regs4[1] = READ4(sc, RTSX_HCBCTLR);
982 1.1.4.2 rmind sc->sc_regs4[2] = READ4(sc, RTSX_HDBAR);
983 1.1.4.2 rmind sc->sc_regs4[3] = READ4(sc, RTSX_HDBCTLR);
984 1.1.4.2 rmind sc->sc_regs4[4] = READ4(sc, RTSX_HAIMR);
985 1.1.4.2 rmind sc->sc_regs4[5] = READ4(sc, RTSX_BIER);
986 1.1.4.2 rmind /* Not saving RTSX_BIPR. */
987 1.1.4.2 rmind
988 1.1.4.2 rmind mutex_exit(&sc->sc_host_mtx);
989 1.1.4.2 rmind }
990 1.1.4.2 rmind
991 1.1.4.2 rmind static void
992 1.1.4.2 rmind rtsx_restore_regs(struct rtsx_softc *sc)
993 1.1.4.2 rmind {
994 1.1.4.2 rmind int i;
995 1.1.4.2 rmind uint16_t reg;
996 1.1.4.2 rmind
997 1.1.4.2 rmind mutex_enter(&sc->sc_host_mtx);
998 1.1.4.2 rmind
999 1.1.4.2 rmind WRITE4(sc, RTSX_HCBAR, sc->sc_regs4[0]);
1000 1.1.4.2 rmind WRITE4(sc, RTSX_HCBCTLR, sc->sc_regs4[1]);
1001 1.1.4.2 rmind WRITE4(sc, RTSX_HDBAR, sc->sc_regs4[2]);
1002 1.1.4.2 rmind WRITE4(sc, RTSX_HDBCTLR, sc->sc_regs4[3]);
1003 1.1.4.2 rmind WRITE4(sc, RTSX_HAIMR, sc->sc_regs4[4]);
1004 1.1.4.2 rmind WRITE4(sc, RTSX_BIER, sc->sc_regs4[5]);
1005 1.1.4.2 rmind /* Not writing RTSX_BIPR since doing so would clear it. */
1006 1.1.4.2 rmind
1007 1.1.4.2 rmind i = 0;
1008 1.1.4.2 rmind for (reg = 0xFDA0; reg < 0xFDAE; reg++)
1009 1.1.4.2 rmind (void)rtsx_write(sc, reg, 0xff, sc->sc_regs[i++]);
1010 1.1.4.2 rmind for (reg = 0xFD52; reg < 0xFD69; reg++)
1011 1.1.4.2 rmind (void)rtsx_write(sc, reg, 0xff, sc->sc_regs[i++]);
1012 1.1.4.2 rmind for (reg = 0xFE20; reg < 0xFE34; reg++)
1013 1.1.4.2 rmind (void)rtsx_write(sc, reg, 0xff, sc->sc_regs[i++]);
1014 1.1.4.2 rmind
1015 1.1.4.2 rmind mutex_exit(&sc->sc_host_mtx);
1016 1.1.4.2 rmind }
1017 1.1.4.2 rmind
1018 1.1.4.2 rmind static uint8_t
1019 1.1.4.2 rmind rtsx_response_type(uint16_t sdmmc_rsp)
1020 1.1.4.2 rmind {
1021 1.1.4.2 rmind static const struct rsp_type {
1022 1.1.4.2 rmind uint16_t sdmmc_rsp;
1023 1.1.4.2 rmind uint8_t rtsx_rsp;
1024 1.1.4.2 rmind } rsp_types[] = {
1025 1.1.4.2 rmind { SCF_RSP_R0, RTSX_SD_RSP_TYPE_R0 },
1026 1.1.4.2 rmind { SCF_RSP_R1, RTSX_SD_RSP_TYPE_R1 },
1027 1.1.4.2 rmind { SCF_RSP_R1B, RTSX_SD_RSP_TYPE_R1B },
1028 1.1.4.2 rmind { SCF_RSP_R2, RTSX_SD_RSP_TYPE_R2 },
1029 1.1.4.2 rmind { SCF_RSP_R3, RTSX_SD_RSP_TYPE_R3 },
1030 1.1.4.2 rmind { SCF_RSP_R4, RTSX_SD_RSP_TYPE_R4 },
1031 1.1.4.2 rmind { SCF_RSP_R5, RTSX_SD_RSP_TYPE_R5 },
1032 1.1.4.2 rmind { SCF_RSP_R6, RTSX_SD_RSP_TYPE_R6 },
1033 1.1.4.2 rmind { SCF_RSP_R7, RTSX_SD_RSP_TYPE_R7 }
1034 1.1.4.2 rmind };
1035 1.1.4.2 rmind size_t i;
1036 1.1.4.2 rmind
1037 1.1.4.2 rmind for (i = 0; i < __arraycount(rsp_types); i++) {
1038 1.1.4.2 rmind if (sdmmc_rsp == rsp_types[i].sdmmc_rsp)
1039 1.1.4.2 rmind return rsp_types[i].rtsx_rsp;
1040 1.1.4.2 rmind }
1041 1.1.4.2 rmind return 0;
1042 1.1.4.2 rmind }
1043 1.1.4.2 rmind
1044 1.1.4.2 rmind static int
1045 1.1.4.2 rmind rtsx_hostcmd_send(struct rtsx_softc *sc, int ncmd)
1046 1.1.4.2 rmind {
1047 1.1.4.2 rmind
1048 1.1.4.2 rmind bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap_cmd, 0, RTSX_HOSTCMD_BUFSIZE,
1049 1.1.4.2 rmind BUS_DMASYNC_PREWRITE);
1050 1.1.4.2 rmind
1051 1.1.4.2 rmind mutex_enter(&sc->sc_host_mtx);
1052 1.1.4.2 rmind
1053 1.1.4.2 rmind /* Tell the chip where the command buffer is and run the commands. */
1054 1.1.4.2 rmind WRITE4(sc, RTSX_HCBAR, sc->sc_dmap_cmd->dm_segs[0].ds_addr);
1055 1.1.4.2 rmind WRITE4(sc, RTSX_HCBCTLR,
1056 1.1.4.2 rmind ((ncmd * 4) & 0x00ffffff) | RTSX_START_CMD | RTSX_HW_AUTO_RSP);
1057 1.1.4.2 rmind
1058 1.1.4.2 rmind mutex_exit(&sc->sc_host_mtx);
1059 1.1.4.2 rmind
1060 1.1.4.2 rmind return 0;
1061 1.1.4.2 rmind }
1062 1.1.4.2 rmind
1063 1.1.4.2 rmind static int
1064 1.1.4.2 rmind rtsx_read_ppbuf(struct rtsx_softc *sc, struct sdmmc_command *cmd,
1065 1.1.4.2 rmind uint32_t *cmdbuf)
1066 1.1.4.2 rmind {
1067 1.1.4.2 rmind uint8_t *ptr;
1068 1.1.4.2 rmind int ncmd, remain;
1069 1.1.4.2 rmind uint16_t reg;
1070 1.1.4.2 rmind int error;
1071 1.1.4.2 rmind int i, j;
1072 1.1.4.2 rmind
1073 1.1.4.2 rmind DPRINTF(3,("%s: read %d bytes from ppbuf2\n", DEVNAME(sc),
1074 1.1.4.2 rmind cmd->c_datalen));
1075 1.1.4.2 rmind
1076 1.1.4.2 rmind reg = RTSX_PPBUF_BASE2;
1077 1.1.4.2 rmind ptr = cmd->c_data;
1078 1.1.4.2 rmind remain = cmd->c_datalen;
1079 1.1.4.2 rmind for (j = 0; j < cmd->c_datalen / RTSX_HOSTCMD_MAX; j++) {
1080 1.1.4.2 rmind ncmd = 0;
1081 1.1.4.2 rmind for (i = 0; i < RTSX_HOSTCMD_MAX; i++) {
1082 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_READ_REG_CMD, reg++,
1083 1.1.4.2 rmind 0, 0);
1084 1.1.4.2 rmind }
1085 1.1.4.2 rmind error = rtsx_hostcmd_send(sc, ncmd);
1086 1.1.4.2 rmind if (error == 0)
1087 1.1.4.2 rmind error = rtsx_wait_intr(sc, RTSX_TRANS_OK_INT, hz / 4);
1088 1.1.4.2 rmind if (error)
1089 1.1.4.2 rmind goto ret;
1090 1.1.4.2 rmind bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap_cmd, 0,
1091 1.1.4.2 rmind RTSX_HOSTCMD_BUFSIZE, BUS_DMASYNC_POSTREAD);
1092 1.1.4.2 rmind memcpy(ptr, cmdbuf, RTSX_HOSTCMD_MAX);
1093 1.1.4.2 rmind ptr += RTSX_HOSTCMD_MAX;
1094 1.1.4.2 rmind remain -= RTSX_HOSTCMD_MAX;
1095 1.1.4.2 rmind }
1096 1.1.4.2 rmind if (remain > 0) {
1097 1.1.4.2 rmind ncmd = 0;
1098 1.1.4.2 rmind for (i = 0; i < remain; i++) {
1099 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_READ_REG_CMD, reg++,
1100 1.1.4.2 rmind 0, 0);
1101 1.1.4.2 rmind }
1102 1.1.4.2 rmind error = rtsx_hostcmd_send(sc, ncmd);
1103 1.1.4.2 rmind if (error == 0)
1104 1.1.4.2 rmind error = rtsx_wait_intr(sc, RTSX_TRANS_OK_INT, hz / 4);
1105 1.1.4.2 rmind if (error)
1106 1.1.4.2 rmind goto ret;
1107 1.1.4.2 rmind bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap_cmd, 0,
1108 1.1.4.2 rmind RTSX_HOSTCMD_BUFSIZE, BUS_DMASYNC_POSTREAD);
1109 1.1.4.2 rmind memcpy(ptr, cmdbuf, remain);
1110 1.1.4.2 rmind }
1111 1.1.4.2 rmind ret:
1112 1.1.4.2 rmind return error;
1113 1.1.4.2 rmind }
1114 1.1.4.2 rmind
1115 1.1.4.2 rmind static int
1116 1.1.4.2 rmind rtsx_write_ppbuf(struct rtsx_softc *sc, struct sdmmc_command *cmd,
1117 1.1.4.2 rmind uint32_t *cmdbuf)
1118 1.1.4.2 rmind {
1119 1.1.4.2 rmind const uint8_t *ptr;
1120 1.1.4.2 rmind int ncmd, remain;
1121 1.1.4.2 rmind uint16_t reg;
1122 1.1.4.2 rmind int error;
1123 1.1.4.2 rmind int i, j;
1124 1.1.4.2 rmind
1125 1.1.4.2 rmind DPRINTF(3,("%s: write %d bytes to ppbuf2\n", DEVNAME(sc),
1126 1.1.4.2 rmind cmd->c_datalen));
1127 1.1.4.2 rmind
1128 1.1.4.2 rmind reg = RTSX_PPBUF_BASE2;
1129 1.1.4.2 rmind ptr = cmd->c_data;
1130 1.1.4.2 rmind remain = cmd->c_datalen;
1131 1.1.4.2 rmind for (j = 0; j < cmd->c_datalen / RTSX_HOSTCMD_MAX; j++) {
1132 1.1.4.2 rmind ncmd = 0;
1133 1.1.4.2 rmind for (i = 0; i < RTSX_HOSTCMD_MAX; i++) {
1134 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, reg++,
1135 1.1.4.2 rmind 0xff, *ptr++);
1136 1.1.4.2 rmind }
1137 1.1.4.2 rmind error = rtsx_hostcmd_send(sc, ncmd);
1138 1.1.4.2 rmind if (error == 0)
1139 1.1.4.2 rmind error = rtsx_wait_intr(sc, RTSX_TRANS_OK_INT, hz / 4);
1140 1.1.4.2 rmind if (error)
1141 1.1.4.2 rmind goto ret;
1142 1.1.4.2 rmind remain -= RTSX_HOSTCMD_MAX;
1143 1.1.4.2 rmind }
1144 1.1.4.2 rmind if (remain > 0) {
1145 1.1.4.2 rmind ncmd = 0;
1146 1.1.4.2 rmind for (i = 0; i < remain; i++) {
1147 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, reg++,
1148 1.1.4.2 rmind 0xff, *ptr++);
1149 1.1.4.2 rmind }
1150 1.1.4.2 rmind error = rtsx_hostcmd_send(sc, ncmd);
1151 1.1.4.2 rmind if (error == 0)
1152 1.1.4.2 rmind error = rtsx_wait_intr(sc, RTSX_TRANS_OK_INT, hz / 4);
1153 1.1.4.2 rmind if (error)
1154 1.1.4.2 rmind goto ret;
1155 1.1.4.2 rmind }
1156 1.1.4.2 rmind ret:
1157 1.1.4.2 rmind return error;
1158 1.1.4.2 rmind }
1159 1.1.4.2 rmind
1160 1.1.4.2 rmind static int
1161 1.1.4.2 rmind rtsx_exec_short_xfer(struct rtsx_softc *sc, struct sdmmc_command *cmd,
1162 1.1.4.2 rmind uint32_t *cmdbuf, uint8_t rsp_type)
1163 1.1.4.2 rmind {
1164 1.1.4.2 rmind int read = ISSET(cmd->c_flags, SCF_CMD_READ);
1165 1.1.4.2 rmind int ncmd;
1166 1.1.4.2 rmind uint8_t tmode = read ? RTSX_TM_NORMAL_READ : RTSX_TM_AUTO_WRITE2;
1167 1.1.4.2 rmind int error;
1168 1.1.4.2 rmind
1169 1.1.4.2 rmind DPRINTF(3,("%s: %s short xfer: %d bytes with block size %d\n",
1170 1.1.4.2 rmind DEVNAME(sc), read ? "read" : "write", cmd->c_datalen,
1171 1.1.4.2 rmind cmd->c_blklen));
1172 1.1.4.2 rmind
1173 1.1.4.2 rmind if (cmd->c_datalen > 512) {
1174 1.1.4.2 rmind DPRINTF(3, ("%s: cmd->c_datalen too large: %d > %d\n",
1175 1.1.4.2 rmind DEVNAME(sc), cmd->c_datalen, 512));
1176 1.1.4.2 rmind return ENOMEM;
1177 1.1.4.2 rmind }
1178 1.1.4.2 rmind
1179 1.1.4.2 rmind if (!read && cmd->c_data != NULL && cmd->c_datalen > 0) {
1180 1.1.4.2 rmind error = rtsx_write_ppbuf(sc, cmd, cmdbuf);
1181 1.1.4.2 rmind if (error)
1182 1.1.4.2 rmind goto ret;
1183 1.1.4.2 rmind }
1184 1.1.4.2 rmind
1185 1.1.4.2 rmind /* The command buffer queues commands the host controller will
1186 1.1.4.2 rmind * run asynchronously. */
1187 1.1.4.2 rmind ncmd = 0;
1188 1.1.4.2 rmind
1189 1.1.4.2 rmind /* Queue commands to set SD command index and argument. */
1190 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_CMD0,
1191 1.1.4.2 rmind 0xff, 0x40 | cmd->c_opcode);
1192 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_CMD1,
1193 1.1.4.2 rmind 0xff, cmd->c_arg >> 24);
1194 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_CMD2,
1195 1.1.4.2 rmind 0xff, cmd->c_arg >> 16);
1196 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_CMD3,
1197 1.1.4.2 rmind 0xff, cmd->c_arg >> 8);
1198 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_CMD4,
1199 1.1.4.2 rmind 0xff, cmd->c_arg);
1200 1.1.4.2 rmind
1201 1.1.4.2 rmind /* Queue commands to configure data transfer size. */
1202 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_BYTE_CNT_L,
1203 1.1.4.2 rmind 0xff, cmd->c_datalen);
1204 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_BYTE_CNT_H,
1205 1.1.4.2 rmind 0xff, cmd->c_datalen >> 8);
1206 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_BLOCK_CNT_L,
1207 1.1.4.2 rmind 0xff, 0x01);
1208 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_BLOCK_CNT_H,
1209 1.1.4.2 rmind 0xff, 0x00);
1210 1.1.4.2 rmind
1211 1.1.4.2 rmind /* Queue command to set response type. */
1212 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_CFG2,
1213 1.1.4.2 rmind 0xff, rsp_type);
1214 1.1.4.2 rmind
1215 1.1.4.2 rmind if (tmode == RTSX_TM_NORMAL_READ) {
1216 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD,
1217 1.1.4.2 rmind RTSX_CARD_DATA_SOURCE, 0x01, RTSX_PINGPONG_BUFFER);
1218 1.1.4.2 rmind }
1219 1.1.4.2 rmind
1220 1.1.4.2 rmind /* Queue commands to perform SD transfer. */
1221 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_TRANSFER,
1222 1.1.4.2 rmind 0xff, tmode | RTSX_SD_TRANSFER_START);
1223 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_CHECK_REG_CMD, RTSX_SD_TRANSFER,
1224 1.1.4.2 rmind RTSX_SD_TRANSFER_END, RTSX_SD_TRANSFER_END);
1225 1.1.4.2 rmind
1226 1.1.4.2 rmind /* Run the command queue and wait for completion. */
1227 1.1.4.2 rmind error = rtsx_hostcmd_send(sc, ncmd);
1228 1.1.4.2 rmind if (error == 0)
1229 1.1.4.2 rmind error = rtsx_wait_intr(sc, RTSX_TRANS_OK_INT, 2 * hz);
1230 1.1.4.2 rmind if (error)
1231 1.1.4.2 rmind goto ret;
1232 1.1.4.2 rmind
1233 1.1.4.2 rmind if (read && cmd->c_data != NULL && cmd->c_datalen > 0)
1234 1.1.4.2 rmind error = rtsx_read_ppbuf(sc, cmd, cmdbuf);
1235 1.1.4.2 rmind ret:
1236 1.1.4.2 rmind DPRINTF(3,("%s: short xfer done, error=%d\n", DEVNAME(sc), error));
1237 1.1.4.2 rmind return error;
1238 1.1.4.2 rmind }
1239 1.1.4.2 rmind
1240 1.1.4.2 rmind static int
1241 1.1.4.2 rmind rtsx_xfer(struct rtsx_softc *sc, struct sdmmc_command *cmd, uint32_t *cmdbuf)
1242 1.1.4.2 rmind {
1243 1.1.4.2 rmind int ncmd, dma_dir, error, tmode;
1244 1.1.4.2 rmind int read = ISSET(cmd->c_flags, SCF_CMD_READ);
1245 1.1.4.2 rmind uint8_t cfg2;
1246 1.1.4.2 rmind
1247 1.1.4.2 rmind DPRINTF(3,("%s: %s xfer: %d bytes with block size %d\n", DEVNAME(sc),
1248 1.1.4.2 rmind read ? "read" : "write", cmd->c_datalen, cmd->c_blklen));
1249 1.1.4.2 rmind
1250 1.1.4.2 rmind if (cmd->c_datalen > RTSX_DMA_DATA_BUFSIZE) {
1251 1.1.4.2 rmind DPRINTF(3, ("%s: cmd->c_datalen too large: %d > %d\n",
1252 1.1.4.2 rmind DEVNAME(sc), cmd->c_datalen, RTSX_DMA_DATA_BUFSIZE));
1253 1.1.4.2 rmind return ENOMEM;
1254 1.1.4.2 rmind }
1255 1.1.4.2 rmind
1256 1.1.4.2 rmind /* Configure DMA transfer mode parameters. */
1257 1.1.4.2 rmind cfg2 = RTSX_SD_NO_CHECK_WAIT_CRC_TO | RTSX_SD_CHECK_CRC16 |
1258 1.1.4.2 rmind RTSX_SD_NO_WAIT_BUSY_END | RTSX_SD_RSP_LEN_0;
1259 1.1.4.2 rmind if (read) {
1260 1.1.4.2 rmind dma_dir = RTSX_DMA_DIR_FROM_CARD;
1261 1.1.4.2 rmind /* Use transfer mode AUTO_READ3, which assumes we've already
1262 1.1.4.2 rmind * sent the read command and gotten the response, and will
1263 1.1.4.2 rmind * send CMD 12 manually after reading multiple blocks. */
1264 1.1.4.2 rmind tmode = RTSX_TM_AUTO_READ3;
1265 1.1.4.2 rmind cfg2 |= RTSX_SD_CALCULATE_CRC7 | RTSX_SD_CHECK_CRC7;
1266 1.1.4.2 rmind } else {
1267 1.1.4.2 rmind dma_dir = RTSX_DMA_DIR_TO_CARD;
1268 1.1.4.2 rmind /* Use transfer mode AUTO_WRITE3, which assumes we've already
1269 1.1.4.2 rmind * sent the write command and gotten the response, and will
1270 1.1.4.2 rmind * send CMD 12 manually after writing multiple blocks. */
1271 1.1.4.2 rmind tmode = RTSX_TM_AUTO_WRITE3;
1272 1.1.4.2 rmind cfg2 |= RTSX_SD_NO_CALCULATE_CRC7 | RTSX_SD_NO_CHECK_CRC7;
1273 1.1.4.2 rmind }
1274 1.1.4.2 rmind
1275 1.1.4.2 rmind /* The command buffer queues commands the host controller will
1276 1.1.4.2 rmind * run asynchronously. */
1277 1.1.4.2 rmind ncmd = 0;
1278 1.1.4.2 rmind
1279 1.1.4.2 rmind /* Queue command to set response type. */
1280 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_CFG2,
1281 1.1.4.2 rmind 0xff, cfg2);
1282 1.1.4.2 rmind
1283 1.1.4.2 rmind /* Queue commands to configure data transfer size. */
1284 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_BYTE_CNT_L,
1285 1.1.4.2 rmind 0xff, 0x00);
1286 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_BYTE_CNT_H,
1287 1.1.4.2 rmind 0xff, 0x02);
1288 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_BLOCK_CNT_L,
1289 1.1.4.2 rmind 0xff, cmd->c_datalen / cmd->c_blklen);
1290 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_BLOCK_CNT_H,
1291 1.1.4.2 rmind 0xff, (cmd->c_datalen / cmd->c_blklen) >> 8);
1292 1.1.4.2 rmind
1293 1.1.4.2 rmind /* Use the DMA ring buffer for commands which transfer data. */
1294 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_CARD_DATA_SOURCE,
1295 1.1.4.2 rmind 0x01, RTSX_RING_BUFFER);
1296 1.1.4.2 rmind
1297 1.1.4.2 rmind /* Configure DMA controller. */
1298 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_IRQSTAT0,
1299 1.1.4.2 rmind RTSX_DMA_DONE_INT, RTSX_DMA_DONE_INT);
1300 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_DMATC3,
1301 1.1.4.2 rmind 0xff, cmd->c_datalen >> 24);
1302 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_DMATC2,
1303 1.1.4.2 rmind 0xff, cmd->c_datalen >> 16);
1304 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_DMATC1,
1305 1.1.4.2 rmind 0xff, cmd->c_datalen >> 8);
1306 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_DMATC0,
1307 1.1.4.2 rmind 0xff, cmd->c_datalen);
1308 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_DMACTL,
1309 1.1.4.2 rmind RTSX_DMA_EN | RTSX_DMA_DIR | RTSX_DMA_PACK_SIZE_MASK,
1310 1.1.4.2 rmind RTSX_DMA_EN | dma_dir | RTSX_DMA_512);
1311 1.1.4.2 rmind
1312 1.1.4.2 rmind /* Queue commands to perform SD transfer. */
1313 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_TRANSFER,
1314 1.1.4.2 rmind 0xff, tmode | RTSX_SD_TRANSFER_START);
1315 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_CHECK_REG_CMD, RTSX_SD_TRANSFER,
1316 1.1.4.2 rmind RTSX_SD_TRANSFER_END, RTSX_SD_TRANSFER_END);
1317 1.1.4.2 rmind
1318 1.1.4.2 rmind error = rtsx_hostcmd_send(sc, ncmd);
1319 1.1.4.2 rmind if (error)
1320 1.1.4.2 rmind goto ret;
1321 1.1.4.2 rmind
1322 1.1.4.2 rmind mutex_enter(&sc->sc_host_mtx);
1323 1.1.4.2 rmind
1324 1.1.4.2 rmind /* Tell the chip where the data buffer is and run the transfer. */
1325 1.1.4.2 rmind WRITE4(sc, RTSX_HDBAR, cmd->c_dmamap->dm_segs[0].ds_addr);
1326 1.1.4.2 rmind WRITE4(sc, RTSX_HDBCTLR, RTSX_TRIG_DMA | (read ? RTSX_DMA_READ : 0) |
1327 1.1.4.2 rmind (cmd->c_dmamap->dm_segs[0].ds_len & 0x00ffffff));
1328 1.1.4.2 rmind
1329 1.1.4.2 rmind mutex_exit(&sc->sc_host_mtx);
1330 1.1.4.2 rmind
1331 1.1.4.2 rmind /* Wait for completion. */
1332 1.1.4.2 rmind error = rtsx_wait_intr(sc, RTSX_TRANS_OK_INT, 10*hz);
1333 1.1.4.2 rmind ret:
1334 1.1.4.2 rmind DPRINTF(3,("%s: xfer done, error=%d\n", DEVNAME(sc), error));
1335 1.1.4.2 rmind return error;
1336 1.1.4.2 rmind }
1337 1.1.4.2 rmind
1338 1.1.4.2 rmind static void
1339 1.1.4.2 rmind rtsx_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
1340 1.1.4.2 rmind {
1341 1.1.4.2 rmind struct rtsx_softc *sc = sch;
1342 1.1.4.2 rmind bus_dma_segment_t segs[1];
1343 1.1.4.2 rmind int rsegs;
1344 1.1.4.2 rmind void *cmdkvap;
1345 1.1.4.2 rmind uint32_t *cmdbuf;
1346 1.1.4.2 rmind uint8_t rsp_type;
1347 1.1.4.2 rmind uint16_t r;
1348 1.1.4.2 rmind int ncmd;
1349 1.1.4.2 rmind int error = 0;
1350 1.1.4.2 rmind
1351 1.1.4.2 rmind DPRINTF(3,("%s: executing cmd %hu\n", DEVNAME(sc), cmd->c_opcode));
1352 1.1.4.2 rmind
1353 1.1.4.2 rmind /* Refuse SDIO probe if the chip doesn't support SDIO. */
1354 1.1.4.2 rmind if (cmd->c_opcode == SD_IO_SEND_OP_COND &&
1355 1.1.4.2 rmind !ISSET(sc->sc_flags, RTSX_F_SDIO_SUPPORT)) {
1356 1.1.4.2 rmind error = ENOTSUP;
1357 1.1.4.2 rmind goto ret;
1358 1.1.4.2 rmind }
1359 1.1.4.2 rmind
1360 1.1.4.2 rmind rsp_type = rtsx_response_type(cmd->c_flags & SCF_RSP_MASK);
1361 1.1.4.2 rmind if (rsp_type == 0) {
1362 1.1.4.2 rmind aprint_error_dev(sc->sc_dev, "unknown response type 0x%x\n",
1363 1.1.4.2 rmind cmd->c_flags & SCF_RSP_MASK);
1364 1.1.4.2 rmind error = EINVAL;
1365 1.1.4.2 rmind goto ret;
1366 1.1.4.2 rmind }
1367 1.1.4.2 rmind
1368 1.1.4.2 rmind /* Allocate and map the host command buffer. */
1369 1.1.4.2 rmind error = bus_dmamem_alloc(sc->sc_dmat, RTSX_HOSTCMD_BUFSIZE, 0, 0,
1370 1.1.4.2 rmind segs, 1, &rsegs, BUS_DMA_WAITOK);
1371 1.1.4.2 rmind if (error)
1372 1.1.4.2 rmind goto ret;
1373 1.1.4.2 rmind error = bus_dmamem_map(sc->sc_dmat, segs, rsegs, RTSX_HOSTCMD_BUFSIZE,
1374 1.1.4.2 rmind &cmdkvap, BUS_DMA_WAITOK|BUS_DMA_COHERENT);
1375 1.1.4.2 rmind if (error)
1376 1.1.4.2 rmind goto free_cmdbuf;
1377 1.1.4.2 rmind
1378 1.1.4.2 rmind /* Load command DMA buffer. */
1379 1.1.4.2 rmind error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmap_cmd, cmdkvap,
1380 1.1.4.2 rmind RTSX_HOSTCMD_BUFSIZE, NULL, BUS_DMA_WAITOK);
1381 1.1.4.2 rmind if (error)
1382 1.1.4.2 rmind goto unmap_cmdbuf;
1383 1.1.4.2 rmind
1384 1.1.4.2 rmind /* Use another transfer method when data size < 512. */
1385 1.1.4.2 rmind if (cmd->c_data != NULL && cmd->c_datalen < 512) {
1386 1.1.4.2 rmind error = rtsx_exec_short_xfer(sch, cmd, cmdkvap, rsp_type);
1387 1.1.4.2 rmind goto unload_cmdbuf;
1388 1.1.4.2 rmind }
1389 1.1.4.2 rmind
1390 1.1.4.2 rmind /* The command buffer queues commands the host controller will
1391 1.1.4.2 rmind * run asynchronously. */
1392 1.1.4.2 rmind cmdbuf = cmdkvap;
1393 1.1.4.2 rmind ncmd = 0;
1394 1.1.4.2 rmind
1395 1.1.4.2 rmind /* Queue commands to set SD command index and argument. */
1396 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_CMD0,
1397 1.1.4.2 rmind 0xff, 0x40 | cmd->c_opcode);
1398 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_CMD1,
1399 1.1.4.2 rmind 0xff, cmd->c_arg >> 24);
1400 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_CMD2,
1401 1.1.4.2 rmind 0xff, cmd->c_arg >> 16);
1402 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_CMD3,
1403 1.1.4.2 rmind 0xff, cmd->c_arg >> 8);
1404 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_CMD4,
1405 1.1.4.2 rmind 0xff, cmd->c_arg);
1406 1.1.4.2 rmind
1407 1.1.4.2 rmind /* Queue command to set response type. */
1408 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_CFG2,
1409 1.1.4.2 rmind 0xff, rsp_type);
1410 1.1.4.2 rmind
1411 1.1.4.2 rmind /* Use the ping-pong buffer for commands which do not transfer data. */
1412 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_CARD_DATA_SOURCE,
1413 1.1.4.2 rmind 0x01, RTSX_PINGPONG_BUFFER);
1414 1.1.4.2 rmind
1415 1.1.4.2 rmind /* Queue commands to perform SD transfer. */
1416 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_WRITE_REG_CMD, RTSX_SD_TRANSFER,
1417 1.1.4.2 rmind 0xff, RTSX_TM_CMD_RSP | RTSX_SD_TRANSFER_START);
1418 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_CHECK_REG_CMD, RTSX_SD_TRANSFER,
1419 1.1.4.2 rmind RTSX_SD_TRANSFER_END | RTSX_SD_STAT_IDLE,
1420 1.1.4.2 rmind RTSX_SD_TRANSFER_END | RTSX_SD_STAT_IDLE);
1421 1.1.4.2 rmind
1422 1.1.4.2 rmind /* Queue commands to read back card status response.*/
1423 1.1.4.2 rmind if (rsp_type == RTSX_SD_RSP_TYPE_R2) {
1424 1.1.4.2 rmind for (r = RTSX_PPBUF_BASE2 + 15; r > RTSX_PPBUF_BASE2; r--)
1425 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_READ_REG_CMD, r, 0, 0);
1426 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_READ_REG_CMD, RTSX_SD_CMD5,
1427 1.1.4.2 rmind 0, 0);
1428 1.1.4.2 rmind } else if (rsp_type != RTSX_SD_RSP_TYPE_R0) {
1429 1.1.4.2 rmind for (r = RTSX_SD_CMD0; r <= RTSX_SD_CMD4; r++)
1430 1.1.4.2 rmind rtsx_hostcmd(cmdbuf, &ncmd, RTSX_READ_REG_CMD, r, 0, 0);
1431 1.1.4.2 rmind }
1432 1.1.4.2 rmind
1433 1.1.4.2 rmind /* Run the command queue and wait for completion. */
1434 1.1.4.2 rmind error = rtsx_hostcmd_send(sc, ncmd);
1435 1.1.4.2 rmind if (error == 0)
1436 1.1.4.2 rmind error = rtsx_wait_intr(sc, RTSX_TRANS_OK_INT, hz);
1437 1.1.4.2 rmind if (error)
1438 1.1.4.2 rmind goto unload_cmdbuf;
1439 1.1.4.2 rmind
1440 1.1.4.2 rmind bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap_cmd, 0, RTSX_HOSTCMD_BUFSIZE,
1441 1.1.4.2 rmind BUS_DMASYNC_POSTREAD);
1442 1.1.4.2 rmind
1443 1.1.4.2 rmind /* Copy card response into sdmmc response buffer. */
1444 1.1.4.2 rmind if (ISSET(cmd->c_flags, SCF_RSP_PRESENT)) {
1445 1.1.4.2 rmind /* Copy bytes like sdhc(4), which on little-endian uses
1446 1.1.4.2 rmind * different byte order for short and long responses... */
1447 1.1.4.2 rmind if (ISSET(cmd->c_flags, SCF_RSP_136)) {
1448 1.1.4.2 rmind uint8_t *resp = cmdkvap;
1449 1.1.4.2 rmind memcpy(cmd->c_resp, resp + 1, sizeof(cmd->c_resp));
1450 1.1.4.2 rmind } else {
1451 1.1.4.2 rmind /* First byte is CHECK_REG_CMD return value, second
1452 1.1.4.2 rmind * one is the command op code -- we skip those. */
1453 1.1.4.2 rmind cmd->c_resp[0] =
1454 1.1.4.2 rmind ((be32toh(cmdbuf[0]) & 0x0000ffff) << 16) |
1455 1.1.4.2 rmind ((be32toh(cmdbuf[1]) & 0xffff0000) >> 16);
1456 1.1.4.2 rmind }
1457 1.1.4.2 rmind }
1458 1.1.4.2 rmind
1459 1.1.4.2 rmind if (cmd->c_data) {
1460 1.1.4.2 rmind error = rtsx_xfer(sc, cmd, cmdbuf);
1461 1.1.4.2 rmind if (error) {
1462 1.1.4.2 rmind uint8_t stat1;
1463 1.1.4.2 rmind if (rtsx_read(sc, RTSX_SD_STAT1, &stat1) == 0 &&
1464 1.1.4.2 rmind (stat1 & RTSX_SD_CRC_ERR)) {
1465 1.1.4.2 rmind aprint_error_dev(sc->sc_dev,
1466 1.1.4.2 rmind "CRC error (stat=0x%x)\n", stat1);
1467 1.1.4.2 rmind }
1468 1.1.4.2 rmind }
1469 1.1.4.2 rmind }
1470 1.1.4.2 rmind
1471 1.1.4.2 rmind unload_cmdbuf:
1472 1.1.4.2 rmind bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap_cmd);
1473 1.1.4.2 rmind unmap_cmdbuf:
1474 1.1.4.2 rmind bus_dmamem_unmap(sc->sc_dmat, cmdkvap, RTSX_HOSTCMD_BUFSIZE);
1475 1.1.4.2 rmind free_cmdbuf:
1476 1.1.4.2 rmind bus_dmamem_free(sc->sc_dmat, segs, rsegs);
1477 1.1.4.2 rmind ret:
1478 1.1.4.2 rmind SET(cmd->c_flags, SCF_ITSDONE);
1479 1.1.4.2 rmind cmd->c_error = error;
1480 1.1.4.2 rmind }
1481 1.1.4.2 rmind
1482 1.1.4.2 rmind /* Prepare for another command. */
1483 1.1.4.2 rmind static void
1484 1.1.4.2 rmind rtsx_soft_reset(struct rtsx_softc *sc)
1485 1.1.4.2 rmind {
1486 1.1.4.2 rmind
1487 1.1.4.2 rmind DPRINTF(1,("%s: soft reset\n", DEVNAME(sc)));
1488 1.1.4.2 rmind
1489 1.1.4.2 rmind /* Stop command transfer. */
1490 1.1.4.2 rmind WRITE4(sc, RTSX_HCBCTLR, RTSX_STOP_CMD);
1491 1.1.4.2 rmind
1492 1.1.4.2 rmind (void)rtsx_write(sc, RTSX_CARD_STOP, RTSX_SD_STOP|RTSX_SD_CLR_ERR,
1493 1.1.4.2 rmind RTSX_SD_STOP|RTSX_SD_CLR_ERR);
1494 1.1.4.2 rmind
1495 1.1.4.2 rmind /* Stop DMA transfer. */
1496 1.1.4.2 rmind WRITE4(sc, RTSX_HDBCTLR, RTSX_STOP_DMA);
1497 1.1.4.2 rmind (void)rtsx_write(sc, RTSX_DMACTL, RTSX_DMA_RST, RTSX_DMA_RST);
1498 1.1.4.2 rmind
1499 1.1.4.2 rmind (void)rtsx_write(sc, RTSX_RBCTL, RTSX_RB_FLUSH, RTSX_RB_FLUSH);
1500 1.1.4.2 rmind }
1501 1.1.4.2 rmind
1502 1.1.4.2 rmind static int
1503 1.1.4.2 rmind rtsx_wait_intr(struct rtsx_softc *sc, int mask, int timo)
1504 1.1.4.2 rmind {
1505 1.1.4.2 rmind int status;
1506 1.1.4.2 rmind int error = 0;
1507 1.1.4.2 rmind
1508 1.1.4.2 rmind mask |= RTSX_TRANS_FAIL_INT;
1509 1.1.4.2 rmind
1510 1.1.4.2 rmind mutex_enter(&sc->sc_intr_mtx);
1511 1.1.4.2 rmind
1512 1.1.4.2 rmind status = sc->sc_intr_status & mask;
1513 1.1.4.2 rmind while (status == 0) {
1514 1.1.4.2 rmind if (cv_timedwait(&sc->sc_intr_cv, &sc->sc_intr_mtx, timo)
1515 1.1.4.2 rmind == EWOULDBLOCK) {
1516 1.1.4.2 rmind rtsx_soft_reset(sc);
1517 1.1.4.2 rmind error = ETIMEDOUT;
1518 1.1.4.2 rmind break;
1519 1.1.4.2 rmind }
1520 1.1.4.2 rmind status = sc->sc_intr_status & mask;
1521 1.1.4.2 rmind }
1522 1.1.4.2 rmind sc->sc_intr_status &= ~status;
1523 1.1.4.2 rmind
1524 1.1.4.2 rmind /* Has the card disappeared? */
1525 1.1.4.2 rmind if (!ISSET(sc->sc_flags, RTSX_F_CARD_PRESENT))
1526 1.1.4.2 rmind error = ENODEV;
1527 1.1.4.2 rmind
1528 1.1.4.2 rmind mutex_exit(&sc->sc_intr_mtx);
1529 1.1.4.2 rmind
1530 1.1.4.2 rmind if (error == 0 && (status & RTSX_TRANS_FAIL_INT))
1531 1.1.4.2 rmind error = EIO;
1532 1.1.4.2 rmind return error;
1533 1.1.4.2 rmind }
1534 1.1.4.2 rmind
1535 1.1.4.2 rmind static void
1536 1.1.4.2 rmind rtsx_card_insert(struct rtsx_softc *sc)
1537 1.1.4.2 rmind {
1538 1.1.4.2 rmind
1539 1.1.4.2 rmind DPRINTF(1, ("%s: card inserted\n", DEVNAME(sc)));
1540 1.1.4.2 rmind
1541 1.1.4.2 rmind sc->sc_flags |= RTSX_F_CARD_PRESENT;
1542 1.1.4.2 rmind (void)rtsx_led_enable(sc);
1543 1.1.4.2 rmind
1544 1.1.4.2 rmind /* Schedule card discovery task. */
1545 1.1.4.2 rmind sdmmc_needs_discover(sc->sc_sdmmc);
1546 1.1.4.2 rmind }
1547 1.1.4.2 rmind
1548 1.1.4.2 rmind static void
1549 1.1.4.2 rmind rtsx_card_eject(struct rtsx_softc *sc)
1550 1.1.4.2 rmind {
1551 1.1.4.2 rmind
1552 1.1.4.2 rmind DPRINTF(1, ("%s: card ejected\n", DEVNAME(sc)));
1553 1.1.4.2 rmind
1554 1.1.4.2 rmind sc->sc_flags &= ~RTSX_F_CARD_PRESENT;
1555 1.1.4.2 rmind (void)rtsx_led_disable(sc);
1556 1.1.4.2 rmind
1557 1.1.4.2 rmind /* Schedule card discovery task. */
1558 1.1.4.2 rmind sdmmc_needs_discover(sc->sc_sdmmc);
1559 1.1.4.2 rmind }
1560 1.1.4.2 rmind
1561 1.1.4.2 rmind /*
1562 1.1.4.2 rmind * Established by attachment driver at interrupt priority IPL_SDMMC.
1563 1.1.4.2 rmind */
1564 1.1.4.2 rmind int
1565 1.1.4.2 rmind rtsx_intr(void *arg)
1566 1.1.4.2 rmind {
1567 1.1.4.2 rmind struct rtsx_softc *sc = arg;
1568 1.1.4.2 rmind uint32_t enabled, status;
1569 1.1.4.2 rmind
1570 1.1.4.2 rmind enabled = READ4(sc, RTSX_BIER);
1571 1.1.4.2 rmind status = READ4(sc, RTSX_BIPR);
1572 1.1.4.2 rmind
1573 1.1.4.2 rmind /* Ack interrupts. */
1574 1.1.4.2 rmind WRITE4(sc, RTSX_BIPR, status);
1575 1.1.4.2 rmind
1576 1.1.4.2 rmind if (((enabled & status) == 0) || status == 0xffffffff)
1577 1.1.4.2 rmind return 0;
1578 1.1.4.2 rmind
1579 1.1.4.2 rmind mutex_enter(&sc->sc_intr_mtx);
1580 1.1.4.2 rmind
1581 1.1.4.2 rmind if (status & RTSX_SD_INT) {
1582 1.1.4.2 rmind if (status & RTSX_SD_EXIST) {
1583 1.1.4.2 rmind if (!ISSET(sc->sc_flags, RTSX_F_CARD_PRESENT))
1584 1.1.4.2 rmind rtsx_card_insert(sc);
1585 1.1.4.2 rmind } else {
1586 1.1.4.2 rmind rtsx_card_eject(sc);
1587 1.1.4.2 rmind }
1588 1.1.4.2 rmind }
1589 1.1.4.2 rmind
1590 1.1.4.2 rmind if (status & (RTSX_TRANS_OK_INT | RTSX_TRANS_FAIL_INT)) {
1591 1.1.4.2 rmind sc->sc_intr_status |= status;
1592 1.1.4.2 rmind cv_broadcast(&sc->sc_intr_cv);
1593 1.1.4.2 rmind }
1594 1.1.4.2 rmind
1595 1.1.4.2 rmind mutex_exit(&sc->sc_intr_mtx);
1596 1.1.4.2 rmind
1597 1.1.4.2 rmind return 1;
1598 1.1.4.2 rmind }
1599