2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
5 * Copyright (c) 2012 Stefan Sperling <stsp@openbsd.org>
6 * Copyright (c) 2020 Henri Hennebert <hlh@restart.be>
7 * Copyright (c) 2020 Gary Jennejohn <gj@freebsd.org>
8 * Copyright (c) 2020 Jesper Schmitz Mouridsen <jsm@FreeBSD.org>
12 * - Lutz Bichler <Lutz.Bichler@gmail.com>
14 * Base on OpenBSD /sys/dev/pci/rtsx_pci.c & /dev/ic/rtsx.c
15 * on Linux /drivers/mmc/host/rtsx_pci_sdmmc.c,
16 * /include/linux/rtsx_pci.h &
17 * /drivers/misc/cardreader/rtsx_pcr.c
18 * on NetBSD /sys/dev/ic/rtsx.c
20 * Permission to use, copy, modify, and distribute this software for any
21 * purpose with or without fee is hereby granted, provided that the above
22 * copyright notice and this permission notice appear in all copies.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/module.h>
42 #include <sys/systm.h> /* For FreeBSD 11 */
43 #include <sys/types.h> /* For FreeBSD 11 */
44 #include <sys/errno.h>
45 #include <sys/kernel.h>
47 #include <sys/endian.h>
48 #include <machine/bus.h>
49 #include <sys/mutex.h>
50 #include <sys/malloc.h>
52 #include <sys/queue.h>
53 #include <sys/taskqueue.h>
54 #include <sys/sysctl.h>
55 #include <dev/pci/pcivar.h>
56 #include <dev/pci/pcireg.h>
57 #include <dev/mmc/bridge.h>
58 #include <dev/mmc/mmcreg.h>
59 #include <dev/mmc/mmcbrvar.h>
60 #include <machine/_inttypes.h>
62 #include "opt_mmccam.h"
66 #include <cam/cam_ccb.h>
67 #include <cam/cam_debug.h>
68 #include <cam/cam_sim.h>
69 #include <cam/cam_xpt_sim.h>
70 #include <cam/mmc/mmc_sim.h>
71 #include "mmc_sim_if.h"
76 /* The softc holds our per-instance data. */
78 struct mtx rtsx_mtx; /* device mutex */
79 device_t rtsx_dev; /* device */
80 uint16_t rtsx_flags; /* device flags */
81 uint16_t rtsx_device_id; /* device ID */
82 device_t rtsx_mmc_dev; /* device of mmc bus */
83 uint32_t rtsx_intr_enabled; /* enabled interrupts */
84 uint32_t rtsx_intr_status; /* soft interrupt status */
85 int rtsx_irq_res_id; /* bus IRQ resource id */
86 struct resource *rtsx_irq_res; /* bus IRQ resource */
87 void *rtsx_irq_cookie; /* bus IRQ resource cookie */
88 struct callout rtsx_timeout_callout; /* callout for timeout */
89 int rtsx_timeout_cmd; /* interrupt timeout for setup commands */
90 int rtsx_timeout_io; /* interrupt timeout for I/O commands */
91 void (*rtsx_intr_trans_ok)(struct rtsx_softc *sc);
92 /* function to call if transfer succeed */
93 void (*rtsx_intr_trans_ko)(struct rtsx_softc *sc);
94 /* function to call if transfer fail */
97 rtsx_card_insert_task; /* card insert delayed task */
98 struct task rtsx_card_remove_task; /* card remove task */
100 int rtsx_res_id; /* bus memory resource id */
101 struct resource *rtsx_res; /* bus memory resource */
102 int rtsx_res_type; /* bus memory resource type */
103 bus_space_tag_t rtsx_btag; /* host register set tag */
104 bus_space_handle_t rtsx_bhandle; /* host register set handle */
106 bus_dma_tag_t rtsx_cmd_dma_tag; /* DMA tag for command transfer */
107 bus_dmamap_t rtsx_cmd_dmamap; /* DMA map for command transfer */
108 void *rtsx_cmd_dmamem; /* DMA mem for command transfer */
109 bus_addr_t rtsx_cmd_buffer; /* device visible address of the DMA segment */
110 int rtsx_cmd_index; /* index in rtsx_cmd_buffer */
112 bus_dma_tag_t rtsx_data_dma_tag; /* DMA tag for data transfer */
113 bus_dmamap_t rtsx_data_dmamap; /* DMA map for data transfer */
114 void *rtsx_data_dmamem; /* DMA mem for data transfer */
115 bus_addr_t rtsx_data_buffer; /* device visible address of the DMA segment */
118 union ccb *rtsx_ccb; /* CAM control block */
119 struct mmc_sim rtsx_mmc_sim; /* CAM generic sim */
120 struct mmc_request rtsx_cam_req; /* CAM MMC request */
123 struct mmc_request *rtsx_req; /* MMC request */
124 struct mmc_host rtsx_host; /* host parameters */
125 int rtsx_pcie_cap; /* PCIe capability offset */
126 int8_t rtsx_bus_busy; /* bus busy status */
127 int8_t rtsx_ios_bus_width; /* current host.ios.bus_width */
128 int32_t rtsx_ios_clock; /* current host.ios.clock */
129 int8_t rtsx_ios_power_mode; /* current host.ios.power mode */
130 int8_t rtsx_ios_timing; /* current host.ios.timing */
131 int8_t rtsx_ios_vccq; /* current host.ios.vccq */
132 uint8_t rtsx_read_only; /* card read only status */
133 uint8_t rtsx_inversion; /* inversion of card detection and read only status */
134 uint8_t rtsx_force_timing; /* force bus_timing_uhs_sdr50 */
135 uint8_t rtsx_debug_mask; /* debugging mask */
136 #define RTSX_DEBUG_BASIC 0x01 /* debug basic flow */
137 #define RTSX_TRACE_SD_CMD 0x02 /* trace SD commands */
138 #define RTSX_DEBUG_TUNING 0x04 /* debug tuning */
140 uint8_t rtsx_cam_status; /* CAM status - 1 if card in use */
142 uint64_t rtsx_read_count; /* count of read operations */
143 uint64_t rtsx_write_count; /* count of write operations */
144 bool rtsx_discovery_mode; /* are we in discovery mode? */
145 bool rtsx_tuning_mode; /* are we tuning */
146 bool rtsx_double_clk; /* double clock freqency */
147 bool rtsx_vpclk; /* voltage at Pulse-width Modulation(PWM) clock? */
148 uint8_t rtsx_ssc_depth; /* Spread spectrum clocking depth */
149 uint8_t rtsx_card_drive_sel; /* value for RTSX_CARD_DRIVE_SEL */
150 uint8_t rtsx_sd30_drive_sel_3v3;/* value for RTSX_SD30_DRIVE_SEL */
153 /* rtsx_flags values */
154 #define RTSX_F_DEFAULT 0x0000
155 #define RTSX_F_CARD_PRESENT 0x0001
156 #define RTSX_F_SDIO_SUPPORT 0x0002
157 #define RTSX_F_VERSION_A 0x0004
158 #define RTSX_F_VERSION_B 0x0008
159 #define RTSX_F_VERSION_C 0x0010
160 #define RTSX_F_VERSION_D 0x0020
161 #define RTSX_F_8411B_QFN48 0x0040
162 #define RTSX_F_REVERSE_SOCKET 0x0080
164 #define RTSX_REALTEK 0x10ec
165 #define RTSX_RTS5209 0x5209
166 #define RTSX_RTS5227 0x5227
167 #define RTSX_RTS5229 0x5229
168 #define RTSX_RTS522A 0x522a
169 #define RTSX_RTS525A 0x525a
170 #define RTSX_RTS5249 0x5249
171 #define RTSX_RTS5260 0x5260
172 #define RTSX_RTL8402 0x5286
173 #define RTSX_RTL8411 0x5289
174 #define RTSX_RTL8411B 0x5287
176 #define RTSX_VERSION "2.1d"
178 static const struct rtsx_device {
183 { RTSX_REALTEK, RTSX_RTS5209, RTSX_VERSION " Realtek RTS5209 PCIe MMC/SD Card Reader"},
184 { RTSX_REALTEK, RTSX_RTS5227, RTSX_VERSION " Realtek RTS5227 PCIe MMC/SD Card Reader"},
185 { RTSX_REALTEK, RTSX_RTS5229, RTSX_VERSION " Realtek RTS5229 PCIe MMC/SD Card Reader"},
186 { RTSX_REALTEK, RTSX_RTS522A, RTSX_VERSION " Realtek RTS522A PCIe MMC/SD Card Reader"},
187 { RTSX_REALTEK, RTSX_RTS525A, RTSX_VERSION " Realtek RTS525A PCIe MMC/SD Card Reader"},
188 { RTSX_REALTEK, RTSX_RTS5249, RTSX_VERSION " Realtek RTS5249 PCIe MMC/SD Card Reader"},
189 { RTSX_REALTEK, RTSX_RTS5260, RTSX_VERSION " Realtek RTS5260 PCIe MMC/SD Card Reader"},
190 { RTSX_REALTEK, RTSX_RTL8402, RTSX_VERSION " Realtek RTL8402 PCIe MMC/SD Card Reader"},
191 { RTSX_REALTEK, RTSX_RTL8411, RTSX_VERSION " Realtek RTL8411 PCIe MMC/SD Card Reader"},
192 { RTSX_REALTEK, RTSX_RTL8411B, RTSX_VERSION " Realtek RTL8411B PCIe MMC/SD Card Reader"},
196 /* See `kenv | grep smbios.system` */
197 static const struct rtsx_inversion_model {
201 } rtsx_inversion_models[] = {
202 { "LENOVO", "ThinkPad T470p", "20J7S0PM00"},
206 static int rtsx_dma_alloc(struct rtsx_softc *sc);
207 static void rtsx_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error);
208 static void rtsx_dma_free(struct rtsx_softc *sc);
209 static void rtsx_intr(void *arg);
210 static void rtsx_handle_card_present(struct rtsx_softc *sc);
211 static void rtsx_card_task(void *arg, int pending __unused);
212 static bool rtsx_is_card_present(struct rtsx_softc *sc);
213 static int rtsx_init(struct rtsx_softc *sc);
214 static int rtsx_map_sd_drive(int index);
215 static int rtsx_rts5227_fill_driving(struct rtsx_softc *sc);
216 static int rtsx_rts5249_fill_driving(struct rtsx_softc *sc);
217 static int rtsx_rts5260_fill_driving(struct rtsx_softc *sc);
218 static int rtsx_read(struct rtsx_softc *, uint16_t, uint8_t *);
219 static int rtsx_read_cfg(struct rtsx_softc *sc, uint8_t func, uint16_t addr, uint32_t *val);
220 static int rtsx_write(struct rtsx_softc *sc, uint16_t addr, uint8_t mask, uint8_t val);
221 static int rtsx_read_phy(struct rtsx_softc *sc, uint8_t addr, uint16_t *val);
222 static int rtsx_write_phy(struct rtsx_softc *sc, uint8_t addr, uint16_t val);
223 static int rtsx_bus_power_off(struct rtsx_softc *sc);
224 static int rtsx_bus_power_on(struct rtsx_softc *sc);
225 static int rtsx_set_bus_width(struct rtsx_softc *sc, enum mmc_bus_width width);
226 static int rtsx_set_sd_timing(struct rtsx_softc *sc, enum mmc_bus_timing timing);
227 static int rtsx_set_sd_clock(struct rtsx_softc *sc, uint32_t freq);
228 static int rtsx_stop_sd_clock(struct rtsx_softc *sc);
229 static int rtsx_switch_sd_clock(struct rtsx_softc *sc, uint8_t clk, uint8_t n, uint8_t div, uint8_t mcu);
231 static void rtsx_sd_change_tx_phase(struct rtsx_softc *sc, uint8_t sample_point);
232 static void rtsx_sd_change_rx_phase(struct rtsx_softc *sc, uint8_t sample_point);
233 static void rtsx_sd_tuning_rx_phase(struct rtsx_softc *sc, uint32_t *phase_map);
234 static int rtsx_sd_tuning_rx_cmd(struct rtsx_softc *sc, uint8_t sample_point);
235 static int rtsx_sd_tuning_rx_cmd_wait(struct rtsx_softc *sc, struct mmc_command *cmd);
236 static void rtsx_sd_tuning_rx_cmd_wakeup(struct rtsx_softc *sc);
237 static void rtsx_sd_wait_data_idle(struct rtsx_softc *sc);
238 static uint8_t rtsx_sd_search_final_rx_phase(struct rtsx_softc *sc, uint32_t phase_map);
239 static int rtsx_sd_get_rx_phase_len(uint32_t phase_map, int start_bit);
242 static int rtsx_led_enable(struct rtsx_softc *sc);
243 static int rtsx_led_disable(struct rtsx_softc *sc);
245 static uint8_t rtsx_response_type(uint16_t mmc_rsp);
246 static void rtsx_init_cmd(struct rtsx_softc *sc, struct mmc_command *cmd);
247 static void rtsx_push_cmd(struct rtsx_softc *sc, uint8_t cmd, uint16_t reg,
248 uint8_t mask, uint8_t data);
249 static void rtsx_set_cmd_data_len(struct rtsx_softc *sc, uint16_t block_cnt, uint16_t byte_cnt);
250 static void rtsx_send_cmd(struct rtsx_softc *sc);
251 static void rtsx_ret_resp(struct rtsx_softc *sc);
252 static void rtsx_set_resp(struct rtsx_softc *sc, struct mmc_command *cmd);
253 static void rtsx_stop_cmd(struct rtsx_softc *sc);
254 static void rtsx_clear_error(struct rtsx_softc *sc);
255 static void rtsx_req_done(struct rtsx_softc *sc);
256 static int rtsx_send_req(struct rtsx_softc *sc, struct mmc_command *cmd);
257 static int rtsx_xfer_short(struct rtsx_softc *sc, struct mmc_command *cmd);
258 static void rtsx_ask_ppbuf_part1(struct rtsx_softc *sc);
259 static void rtsx_get_ppbuf_part1(struct rtsx_softc *sc);
260 static void rtsx_get_ppbuf_part2(struct rtsx_softc *sc);
261 static void rtsx_put_ppbuf_part1(struct rtsx_softc *sc);
262 static void rtsx_put_ppbuf_part2(struct rtsx_softc *sc);
263 static void rtsx_write_ppbuf(struct rtsx_softc *sc);
264 static int rtsx_xfer(struct rtsx_softc *sc, struct mmc_command *cmd);
265 static void rtsx_xfer_begin(struct rtsx_softc *sc);
266 static void rtsx_xfer_start(struct rtsx_softc *sc);
267 static void rtsx_xfer_finish(struct rtsx_softc *sc);
268 static void rtsx_timeout(void *arg);
271 static int rtsx_get_tran_settings(device_t dev, struct ccb_trans_settings_mmc *cts);
272 static int rtsx_set_tran_settings(device_t dev, struct ccb_trans_settings_mmc *cts);
273 static int rtsx_cam_request(device_t dev, union ccb *ccb);
276 static int rtsx_read_ivar(device_t bus, device_t child, int which, uintptr_t *result);
277 static int rtsx_write_ivar(device_t bus, device_t child, int which, uintptr_t value);
279 static int rtsx_mmcbr_update_ios(device_t bus, device_t child __unused);
280 static int rtsx_mmcbr_switch_vccq(device_t bus, device_t child __unused);
281 static int rtsx_mmcbr_request(device_t bus, device_t child __unused, struct mmc_request *req);
283 static int rtsx_mmcbr_tune(device_t bus, device_t child __unused, bool hs400 __unused);
284 static int rtsx_mmcbr_retune(device_t bus, device_t child __unused, bool reset __unused);
285 static int rtsx_mmcbr_get_ro(device_t bus, device_t child __unused);
286 static int rtsx_mmcbr_acquire_host(device_t bus, device_t child __unused);
287 static int rtsx_mmcbr_release_host(device_t bus, device_t child __unused);
290 static int rtsx_probe(device_t dev);
291 static int rtsx_attach(device_t dev);
292 static int rtsx_detach(device_t dev);
293 static int rtsx_shutdown(device_t dev);
294 static int rtsx_suspend(device_t dev);
295 static int rtsx_resume(device_t dev);
297 #define RTSX_LOCK_INIT(_sc) mtx_init(&(_sc)->rtsx_mtx, \
298 device_get_nameunit(sc->rtsx_dev), "rtsx", MTX_DEF)
299 #define RTSX_LOCK(_sc) mtx_lock(&(_sc)->rtsx_mtx)
300 #define RTSX_UNLOCK(_sc) mtx_unlock(&(_sc)->rtsx_mtx)
301 #define RTSX_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->rtsx_mtx)
303 #define RTSX_SDCLK_OFF 0
304 #define RTSX_SDCLK_250KHZ 250000
305 #define RTSX_SDCLK_400KHZ 400000
306 #define RTSX_SDCLK_25MHZ 25000000
307 #define RTSX_SDCLK_50MHZ 50000000
308 #define RTSX_SDCLK_100MHZ 100000000
309 #define RTSX_SDCLK_208MHZ 208000000
311 #define RTSX_MIN_DIV_N 80
312 #define RTSX_MAX_DIV_N 208
314 #define RTSX_MAX_DATA_BLKLEN 512
316 #define RTSX_DMA_ALIGN 4
317 #define RTSX_HOSTCMD_MAX 256
318 #define RTSX_DMA_CMD_BIFSIZE (sizeof(uint32_t) * RTSX_HOSTCMD_MAX)
319 #define RTSX_DMA_DATA_BUFSIZE MAXPHYS
321 #define ISSET(t, f) ((t) & (f))
323 #define READ4(sc, reg) \
324 (bus_space_read_4((sc)->rtsx_btag, (sc)->rtsx_bhandle, (reg)))
325 #define WRITE4(sc, reg, val) \
326 (bus_space_write_4((sc)->rtsx_btag, (sc)->rtsx_bhandle, (reg), (val)))
328 #define RTSX_READ(sc, reg, val) \
330 int err = rtsx_read((sc), (reg), (val)); \
335 #define RTSX_WRITE(sc, reg, val) \
337 int err = rtsx_write((sc), (reg), 0xff, (val)); \
341 #define RTSX_CLR(sc, reg, bits) \
343 int err = rtsx_write((sc), (reg), (bits), 0); \
348 #define RTSX_SET(sc, reg, bits) \
350 int err = rtsx_write((sc), (reg), (bits), 0xff);\
355 #define RTSX_BITOP(sc, reg, mask, bits) \
357 int err = rtsx_write((sc), (reg), (mask), (bits)); \
363 * We use two DMA buffers: a command buffer and a data buffer.
365 * The command buffer contains a command queue for the host controller,
366 * which describes SD/MMC commands to run, and other parameters. The chip
367 * runs the command queue when a special bit in the RTSX_HCBAR register is
368 * set and signals completion with the RTSX_TRANS_OK_INT interrupt.
369 * Each command is encoded as a 4 byte sequence containing command number
370 * (read, write, or check a host controller register), a register address,
371 * and a data bit-mask and value.
372 * SD/MMC commands which do not transfer any data from/to the card only use
373 * the command buffer.
375 * The data buffer is used for transfer longer than 512. Data transfer is
376 * controlled via the RTSX_HDBAR register and completion is signalled by
377 * the RTSX_TRANS_OK_INT interrupt.
379 * The chip is unable to perform DMA above 4GB.
383 * Main commands in the usual seqence used:
386 * CMD8 Send interface condition
387 * CMD55 Application Command for next ACMD
388 * ACMD41 Send Operation Conditions Register (OCR: voltage profile of the card)
389 * CMD2 Send Card Identification (CID) Register
390 * CMD3 Send relative address
391 * CMD9 Send Card Specific Data (CSD)
392 * CMD13 Send status (32 bits - bit 25: card password protected)
393 * CMD7 Select card (before Get card SCR)
394 * ACMD51 Send SCR (SD CARD Configuration Register - [51:48]: Bus widths supported)
395 * CMD6 SD switch function
396 * ACMD13 Send SD status (512 bits)
397 * ACMD42 Set/Clear card detect
398 * ACMD6 Set bus width
399 * CMD19 Send tuning block
400 * CMD12 Stop transmission
402 * CMD17 Read single block (<=512)
403 * CMD18 Read multiple blocks (>512)
404 * CMD24 Write single block (<=512)
405 * CMD25 Write multiple blocks (>512)
407 * CMD52 IO R/W direct
408 * CMD5 Send Operation Conditions
412 rtsx_dma_alloc(struct rtsx_softc *sc)
416 error = bus_dma_tag_create(bus_get_dma_tag(sc->rtsx_dev), /* inherit from parent */
417 RTSX_DMA_ALIGN, 0, /* alignment, boundary */
418 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
419 BUS_SPACE_MAXADDR, /* highaddr */
420 NULL, NULL, /* filter, filterarg */
421 RTSX_DMA_CMD_BIFSIZE, 1, /* maxsize, nsegments */
422 RTSX_DMA_CMD_BIFSIZE, /* maxsegsize */
424 NULL, NULL, /* lockfunc, lockarg */
425 &sc->rtsx_cmd_dma_tag);
427 device_printf(sc->rtsx_dev,
428 "Can't create cmd parent DMA tag\n");
431 error = bus_dmamem_alloc(sc->rtsx_cmd_dma_tag, /* DMA tag */
432 &sc->rtsx_cmd_dmamem, /* will hold the KVA pointer */
433 BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, /* flags */
434 &sc->rtsx_cmd_dmamap); /* DMA map */
436 device_printf(sc->rtsx_dev,
437 "Can't create DMA map for command transfer\n");
438 goto destroy_cmd_dma_tag;
441 error = bus_dmamap_load(sc->rtsx_cmd_dma_tag, /* DMA tag */
442 sc->rtsx_cmd_dmamap, /* DMA map */
443 sc->rtsx_cmd_dmamem, /* KVA pointer to be mapped */
444 RTSX_DMA_CMD_BIFSIZE, /* size of buffer */
445 rtsx_dmamap_cb, /* callback */
446 &sc->rtsx_cmd_buffer, /* first arg of callback */
448 if (error || sc->rtsx_cmd_buffer == 0) {
449 device_printf(sc->rtsx_dev,
450 "Can't load DMA memory for command transfer\n");
451 error = (error) ? error : EFAULT;
452 goto destroy_cmd_dmamem_alloc;
455 error = bus_dma_tag_create(bus_get_dma_tag(sc->rtsx_dev), /* inherit from parent */
456 RTSX_DMA_DATA_BUFSIZE, 0, /* alignment, boundary */
457 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
458 BUS_SPACE_MAXADDR, /* highaddr */
459 NULL, NULL, /* filter, filterarg */
460 RTSX_DMA_DATA_BUFSIZE, 1, /* maxsize, nsegments */
461 RTSX_DMA_DATA_BUFSIZE, /* maxsegsize */
463 NULL, NULL, /* lockfunc, lockarg */
464 &sc->rtsx_data_dma_tag);
466 device_printf(sc->rtsx_dev,
467 "Can't create data parent DMA tag\n");
468 goto destroy_cmd_dmamap_load;
470 error = bus_dmamem_alloc(sc->rtsx_data_dma_tag, /* DMA tag */
471 &sc->rtsx_data_dmamem, /* will hold the KVA pointer */
472 BUS_DMA_WAITOK | BUS_DMA_ZERO, /* flags */
473 &sc->rtsx_data_dmamap); /* DMA map */
475 device_printf(sc->rtsx_dev,
476 "Can't create DMA map for data transfer\n");
477 goto destroy_data_dma_tag;
479 error = bus_dmamap_load(sc->rtsx_data_dma_tag, /* DMA tag */
480 sc->rtsx_data_dmamap, /* DMA map */
481 sc->rtsx_data_dmamem, /* KVA pointer to be mapped */
482 RTSX_DMA_DATA_BUFSIZE, /* size of buffer */
483 rtsx_dmamap_cb, /* callback */
484 &sc->rtsx_data_buffer, /* first arg of callback */
486 if (error || sc->rtsx_data_buffer == 0) {
487 device_printf(sc->rtsx_dev,
488 "Can't load DMA memory for data transfer\n");
489 error = (error) ? error : EFAULT;
490 goto destroy_data_dmamem_alloc;
494 destroy_data_dmamem_alloc:
495 bus_dmamem_free(sc->rtsx_data_dma_tag, sc->rtsx_data_dmamem, sc->rtsx_data_dmamap);
496 destroy_data_dma_tag:
497 bus_dma_tag_destroy(sc->rtsx_data_dma_tag);
498 destroy_cmd_dmamap_load:
499 bus_dmamap_unload(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamap);
500 destroy_cmd_dmamem_alloc:
501 bus_dmamem_free(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamem, sc->rtsx_cmd_dmamap);
503 bus_dma_tag_destroy(sc->rtsx_cmd_dma_tag);
509 rtsx_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
512 printf("rtsx_dmamap_cb: error %d\n", error);
515 *(bus_addr_t *)arg = segs[0].ds_addr;
519 rtsx_dma_free(struct rtsx_softc *sc)
521 if (sc->rtsx_cmd_dma_tag != NULL) {
522 if (sc->rtsx_cmd_dmamap != NULL)
523 bus_dmamap_unload(sc->rtsx_cmd_dma_tag,
524 sc->rtsx_cmd_dmamap);
525 if (sc->rtsx_cmd_dmamem != NULL)
526 bus_dmamem_free(sc->rtsx_cmd_dma_tag,
528 sc->rtsx_cmd_dmamap);
529 sc->rtsx_cmd_dmamap = NULL;
530 sc->rtsx_cmd_dmamem = NULL;
531 sc->rtsx_cmd_buffer = 0;
532 bus_dma_tag_destroy(sc->rtsx_cmd_dma_tag);
533 sc->rtsx_cmd_dma_tag = NULL;
535 if (sc->rtsx_data_dma_tag != NULL) {
536 if (sc->rtsx_data_dmamap != NULL)
537 bus_dmamap_unload(sc->rtsx_data_dma_tag,
538 sc->rtsx_data_dmamap);
539 if (sc->rtsx_data_dmamem != NULL)
540 bus_dmamem_free(sc->rtsx_data_dma_tag,
541 sc->rtsx_data_dmamem,
542 sc->rtsx_data_dmamap);
543 sc->rtsx_data_dmamap = NULL;
544 sc->rtsx_data_dmamem = NULL;
545 sc->rtsx_data_buffer = 0;
546 bus_dma_tag_destroy(sc->rtsx_data_dma_tag);
547 sc->rtsx_data_dma_tag = NULL;
554 struct rtsx_softc *sc = arg;
560 enabled = sc->rtsx_intr_enabled;
561 status = READ4(sc, RTSX_BIPR); /* read Bus Interrupt Pending Register */
562 sc->rtsx_intr_status = status;
564 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
565 device_printf(sc->rtsx_dev, "Interrupt handler - enabled: 0x%08x, status: 0x%08x\n", enabled, status);
567 /* Ack interrupts. */
568 WRITE4(sc, RTSX_BIPR, status);
570 if (((enabled & status) == 0) || status == 0xffffffff) {
571 device_printf(sc->rtsx_dev, "Spurious interrupt - enabled: 0x%08x, status: 0x%08x\n", enabled, status);
576 /* Detect write protect. */
577 if (status & RTSX_SD_WRITE_PROTECT)
578 sc->rtsx_read_only = 1;
580 sc->rtsx_read_only = 0;
582 /* Start task to handle SD card status change (from dwmmc.c). */
583 if (status & RTSX_SD_INT) {
584 device_printf(sc->rtsx_dev, "Interrupt card inserted/removed\n");
585 rtsx_handle_card_present(sc);
588 if (sc->rtsx_req == NULL) {
593 if (status & RTSX_TRANS_OK_INT) {
594 sc->rtsx_req->cmd->error = MMC_ERR_NONE;
595 if (sc->rtsx_intr_trans_ok != NULL)
596 sc->rtsx_intr_trans_ok(sc);
597 } else if (status & RTSX_TRANS_FAIL_INT) {
599 sc->rtsx_req->cmd->error = MMC_ERR_FAILED;
600 if (rtsx_read(sc, RTSX_SD_STAT1, &stat1) == 0 &&
601 (stat1 & RTSX_SD_CRC_ERR)) {
602 device_printf(sc->rtsx_dev, "CRC error\n");
603 sc->rtsx_req->cmd->error = MMC_ERR_BADCRC;
605 if (!sc->rtsx_tuning_mode)
606 device_printf(sc->rtsx_dev, "Transfer fail - status: 0x%08x\n", status);
608 if (sc->rtsx_intr_trans_ko != NULL)
609 sc->rtsx_intr_trans_ko(sc);
616 * Function called from the IRQ handler (from dwmmc.c).
619 rtsx_handle_card_present(struct rtsx_softc *sc)
625 was_present = sc->rtsx_cam_status;
627 was_present = sc->rtsx_mmc_dev != NULL;
629 is_present = rtsx_is_card_present(sc);
631 device_printf(sc->rtsx_dev, "Card present\n");
633 device_printf(sc->rtsx_dev, "Card absent\n");
635 if (!was_present && is_present) {
637 * The delay is to debounce the card insert
638 * (sometimes the card detect pin stabilizes
639 * before the other pins have made good contact).
641 taskqueue_enqueue_timeout(taskqueue_swi_giant,
642 &sc->rtsx_card_insert_task, -hz);
643 } else if (was_present && !is_present) {
644 taskqueue_enqueue(taskqueue_swi_giant, &sc->rtsx_card_remove_task);
649 * This function is called at startup.
652 rtsx_card_task(void *arg, int pending __unused)
654 struct rtsx_softc *sc = arg;
656 if (rtsx_is_card_present(sc)) {
657 sc->rtsx_flags |= RTSX_F_CARD_PRESENT;
658 /* Card is present, attach if necessary. */
660 if (sc->rtsx_cam_status == 0) {
662 if (sc->rtsx_mmc_dev == NULL) {
664 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
665 device_printf(sc->rtsx_dev, "Card inserted\n");
667 sc->rtsx_read_count = sc->rtsx_write_count = 0;
669 sc->rtsx_cam_status = 1;
670 mmc_cam_sim_discover(&sc->rtsx_mmc_sim);
673 sc->rtsx_mmc_dev = device_add_child(sc->rtsx_dev, "mmc", -1);
675 if (sc->rtsx_mmc_dev == NULL) {
676 device_printf(sc->rtsx_dev, "Adding MMC bus failed\n");
678 device_set_ivars(sc->rtsx_mmc_dev, sc);
679 device_probe_and_attach(sc->rtsx_mmc_dev);
684 sc->rtsx_flags &= ~RTSX_F_CARD_PRESENT;
685 /* Card isn't present, detach if necessary. */
687 if (sc->rtsx_cam_status != 0) {
689 if (sc->rtsx_mmc_dev != NULL) {
691 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
692 device_printf(sc->rtsx_dev, "Card removed\n");
694 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
695 device_printf(sc->rtsx_dev, "Read count: %" PRIu64 ", write count: %" PRIu64 "\n",
696 sc->rtsx_read_count, sc->rtsx_write_count);
698 sc->rtsx_cam_status = 0;
699 mmc_cam_sim_discover(&sc->rtsx_mmc_sim);
701 if (device_delete_child(sc->rtsx_dev, sc->rtsx_mmc_dev))
702 device_printf(sc->rtsx_dev, "Detaching MMC bus failed\n");
703 sc->rtsx_mmc_dev = NULL;
710 rtsx_is_card_present(struct rtsx_softc *sc)
714 status = READ4(sc, RTSX_BIPR);
715 if (sc->rtsx_inversion == 0)
716 return (status & RTSX_SD_EXIST);
718 return !(status & RTSX_SD_EXIST);
722 rtsx_init(struct rtsx_softc *sc)
728 sc->rtsx_host.host_ocr = RTSX_SUPPORTED_VOLTAGE;
729 sc->rtsx_host.f_min = RTSX_SDCLK_250KHZ;
730 sc->rtsx_host.f_max = RTSX_SDCLK_208MHZ;
731 sc->rtsx_host.caps = MMC_CAP_4_BIT_DATA | MMC_CAP_HSPEED |
732 MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
734 sc->rtsx_host.caps |= MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104;
735 if (sc->rtsx_device_id == RTSX_RTS5209)
736 sc->rtsx_host.caps |= MMC_CAP_8_BIT_DATA;
737 pci_find_cap(sc->rtsx_dev, PCIY_EXPRESS, &(sc->rtsx_pcie_cap));
742 switch (sc->rtsx_device_id) {
744 /* Read IC version from dummy register. */
745 RTSX_READ(sc, RTSX_DUMMY_REG, &version);
746 if ((version & 0x0F) == RTSX_IC_VERSION_C)
747 sc->rtsx_flags |= RTSX_F_VERSION_C;
750 /* Read IC version from dummy register. */
751 RTSX_READ(sc, RTSX_DUMMY_REG, &version);
752 if ((version & 0x0F) == RTSX_IC_VERSION_A)
753 sc->rtsx_flags |= RTSX_F_VERSION_A;
756 /* Read IC version from dummy register. */
757 RTSX_READ(sc, RTSX_DUMMY_REG, &version);
758 if ((version & 0x0F) == RTSX_IC_VERSION_A)
759 sc->rtsx_flags |= RTSX_F_VERSION_A;
762 RTSX_READ(sc, RTSX_RTL8411B_PACKAGE, &version);
763 if (version & RTSX_RTL8411B_QFN48)
764 sc->rtsx_flags |= RTSX_F_8411B_QFN48;
769 * Fetch vendor settings.
772 * Normally OEMs will set vendor setting to the config space
773 * of Realtek card reader in BIOS stage. This statement reads
774 * the setting and configure the internal registers according
775 * to it, to improve card reader's compatibility condition.
777 sc->rtsx_card_drive_sel = RTSX_CARD_DRIVE_DEFAULT;
778 switch (sc->rtsx_device_id) {
783 sc->rtsx_card_drive_sel = RTSX_RTS5209_CARD_DRIVE_DEFAULT;
784 sc->rtsx_sd30_drive_sel_3v3 = RTSX_DRIVER_TYPE_D;
785 reg = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG2, 4);
787 sc->rtsx_card_drive_sel = (reg >> 8) & 0x3F;
788 sc->rtsx_sd30_drive_sel_3v3 = reg & 0x07;
789 } else if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) {
790 device_printf(sc->rtsx_dev, "pci_read_config() error - reg: 0x%08x\n", reg);
792 if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
793 device_printf(sc->rtsx_dev, "card_drive_sel: 0x%02x, sd30_drive_sel_3v3: 0x%02x\n",
794 sc->rtsx_card_drive_sel, sc->rtsx_sd30_drive_sel_3v3);
798 sc->rtsx_sd30_drive_sel_3v3 = RTSX_CFG_DRIVER_TYPE_B;
799 reg = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG1, 4);
800 if (!(reg & 0x1000000)) {
801 sc->rtsx_card_drive_sel &= 0x3F;
802 sc->rtsx_card_drive_sel |= ((reg >> 25) & 0x01) << 6;
803 reg = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG2, 4);
804 sc->rtsx_sd30_drive_sel_3v3 = (reg >> 5) & 0x03;
806 sc->rtsx_flags |= RTSX_F_REVERSE_SOCKET;
807 } else if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) {
808 device_printf(sc->rtsx_dev, "pci_read_config() error - reg: 0x%08x\n", reg);
810 if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
811 device_printf(sc->rtsx_dev,
812 "card_drive_sel: 0x%02x, sd30_drive_sel_3v3: 0x%02x, reverse_socket is %s\n",
813 sc->rtsx_card_drive_sel, sc->rtsx_sd30_drive_sel_3v3,
814 (sc->rtsx_flags & RTSX_F_REVERSE_SOCKET) ? "true" : "false");
817 sc->rtsx_sd30_drive_sel_3v3 = RTSX_DRIVER_TYPE_D;
818 reg = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG1, 4);
819 if (!(reg & 0x1000000)) {
820 sc->rtsx_card_drive_sel &= 0x3F;
821 sc->rtsx_card_drive_sel |= ((reg >> 25) & 0x01) << 6;
822 reg = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG2, 4);
823 sc->rtsx_sd30_drive_sel_3v3 = rtsx_map_sd_drive((reg >> 5) & 0x03);
824 } else if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) {
825 device_printf(sc->rtsx_dev, "pci_read_config() error - reg: 0x%08x\n", reg);
827 if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
828 device_printf(sc->rtsx_dev, "card_drive_sel: 0x%02x, sd30_drive_sel_3v3: 0x%02x\n",
829 sc->rtsx_card_drive_sel, sc->rtsx_sd30_drive_sel_3v3);
834 sc->rtsx_sd30_drive_sel_3v3 = RTSX_CFG_DRIVER_TYPE_B;
835 reg = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG1, 4);
836 if ((reg & 0x1000000)) {
837 sc->rtsx_card_drive_sel &= 0x3F;
838 sc->rtsx_card_drive_sel |= ((reg >> 25) & 0x01) << 6;
839 reg = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG2, 4);
840 sc->rtsx_sd30_drive_sel_3v3 = (reg >> 5) & 0x03;
842 sc->rtsx_flags |= RTSX_F_REVERSE_SOCKET;
843 } else if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) {
844 device_printf(sc->rtsx_dev, "pci_read_config() error - reg: 0x%08x\n", reg);
846 if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
847 device_printf(sc->rtsx_dev,
848 "card_drive_sel = 0x%02x, sd30_drive_sel_3v3: 0x%02x, reverse_socket is %s\n",
849 sc->rtsx_card_drive_sel, sc->rtsx_sd30_drive_sel_3v3,
850 (sc->rtsx_flags & RTSX_F_REVERSE_SOCKET) ? "true" : "false");
854 sc->rtsx_card_drive_sel = RTSX_RTL8411_CARD_DRIVE_DEFAULT;
855 sc->rtsx_sd30_drive_sel_3v3 = RTSX_DRIVER_TYPE_D;
856 reg1 = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG1, 4);
857 if (reg1 & 0x1000000) {
858 sc->rtsx_card_drive_sel &= 0x3F;
859 sc->rtsx_card_drive_sel |= ((reg1 >> 25) & 0x01) << 6;
860 reg3 = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG3, 1);
861 sc->rtsx_sd30_drive_sel_3v3 = (reg3 >> 5) & 0x07;
862 } else if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) {
863 device_printf(sc->rtsx_dev, "pci_read_config() error - reg1: 0x%08x\n", reg1);
865 if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
866 device_printf(sc->rtsx_dev,
867 "card_drive_sel: 0x%02x, sd30_drive_sel_3v3: 0x%02x\n",
868 sc->rtsx_card_drive_sel, sc->rtsx_sd30_drive_sel_3v3);
871 sc->rtsx_card_drive_sel = RTSX_RTL8411_CARD_DRIVE_DEFAULT;
872 sc->rtsx_sd30_drive_sel_3v3 = RTSX_DRIVER_TYPE_D;
873 reg = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG1, 4);
874 if (!(reg & 0x1000000)) {
875 sc->rtsx_sd30_drive_sel_3v3 = rtsx_map_sd_drive(reg & 0x03);
876 } else if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) {
877 device_printf(sc->rtsx_dev, "pci_read_config() error - reg: 0x%08x\n", reg);
879 if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
880 device_printf(sc->rtsx_dev,
881 "card_drive_sel: 0x%02x, sd30_drive_sel_3v3: 0x%02x\n",
882 sc->rtsx_card_drive_sel, sc->rtsx_sd30_drive_sel_3v3);
886 if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
887 device_printf(sc->rtsx_dev, "rtsx_init() rtsx_flags: 0x%04x\n", sc->rtsx_flags);
889 /* Enable interrupts. */
890 sc->rtsx_intr_enabled = RTSX_TRANS_OK_INT_EN | RTSX_TRANS_FAIL_INT_EN | RTSX_SD_INT_EN | RTSX_MS_INT_EN;
891 WRITE4(sc, RTSX_BIER, sc->rtsx_intr_enabled);
893 /* Power on SSC clock. */
894 RTSX_CLR(sc, RTSX_FPDCTL, RTSX_SSC_POWER_DOWN);
895 /* Wait SSC power stable. */
899 val = pci_read_config(sc->rtsx_dev, sc->rtsx_pcie_cap + PCIER_LINK_CTL, 1);
900 pci_write_config(sc->rtsx_dev, sc->rtsx_pcie_cap + PCIER_LINK_CTL, val & 0xfc, 1);
905 switch (sc->rtsx_device_id) {
907 /* Some magic numbers from Linux driver. */
908 if ((error = rtsx_write_phy(sc, 0x00, 0xB966)))
912 RTSX_CLR(sc, RTSX_PM_CTRL3, RTSX_D3_DELINK_MODE_EN);
914 /* Optimize RX sensitivity. */
915 if ((error = rtsx_write_phy(sc, 0x00, 0xBA42)))
919 /* Optimize RX sensitivity. */
920 if ((error = rtsx_write_phy(sc, 0x00, 0xBA42)))
924 RTSX_CLR(sc, RTSX_RTS522A_PM_CTRL3, RTSX_D3_DELINK_MODE_EN);
925 if (sc->rtsx_flags & RTSX_F_VERSION_A) {
926 if ((error = rtsx_write_phy(sc, RTSX_PHY_RCR2, RTSX_PHY_RCR2_INIT_27S)))
929 if ((error = rtsx_write_phy(sc, RTSX_PHY_RCR1, RTSX_PHY_RCR1_INIT_27S)))
931 if ((error = rtsx_write_phy(sc, RTSX_PHY_FLD0, RTSX_PHY_FLD0_INIT_27S)))
933 if ((error = rtsx_write_phy(sc, RTSX_PHY_FLD3, RTSX_PHY_FLD3_INIT_27S)))
935 if ((error = rtsx_write_phy(sc, RTSX_PHY_FLD4, RTSX_PHY_FLD4_INIT_27S)))
939 if ((error = rtsx_write_phy(sc, RTSX__PHY_FLD0,
940 RTSX__PHY_FLD0_CLK_REQ_20C | RTSX__PHY_FLD0_RX_IDLE_EN |
941 RTSX__PHY_FLD0_BIT_ERR_RSTN | RTSX__PHY_FLD0_BER_COUNT |
942 RTSX__PHY_FLD0_BER_TIMER | RTSX__PHY_FLD0_CHECK_EN)))
944 if ((error = rtsx_write_phy(sc, RTSX__PHY_ANA03,
945 RTSX__PHY_ANA03_TIMER_MAX | RTSX__PHY_ANA03_OOBS_DEB_EN |
946 RTSX__PHY_CMU_DEBUG_EN)))
948 if (sc->rtsx_flags & RTSX_F_VERSION_A)
949 if ((error = rtsx_write_phy(sc, RTSX__PHY_REV0,
950 RTSX__PHY_REV0_FILTER_OUT | RTSX__PHY_REV0_CDR_BYPASS_PFD |
951 RTSX__PHY_REV0_CDR_RX_IDLE_BYPASS)))
955 RTSX_CLR(sc, RTSX_PM_CTRL3, RTSX_D3_DELINK_MODE_EN);
956 if ((error = rtsx_write_phy(sc, RTSX_PHY_REV,
957 RTSX_PHY_REV_RESV | RTSX_PHY_REV_RXIDLE_LATCHED |
958 RTSX_PHY_REV_P1_EN | RTSX_PHY_REV_RXIDLE_EN |
959 RTSX_PHY_REV_CLKREQ_TX_EN | RTSX_PHY_REV_RX_PWST |
960 RTSX_PHY_REV_CLKREQ_DT_1_0 | RTSX_PHY_REV_STOP_CLKRD |
961 RTSX_PHY_REV_STOP_CLKWR)))
964 if ((error = rtsx_write_phy(sc, RTSX_PHY_BPCR,
965 RTSX_PHY_BPCR_IBRXSEL | RTSX_PHY_BPCR_IBTXSEL |
966 RTSX_PHY_BPCR_IB_FILTER | RTSX_PHY_BPCR_CMIRROR_EN)))
968 if ((error = rtsx_write_phy(sc, RTSX_PHY_PCR,
969 RTSX_PHY_PCR_FORCE_CODE | RTSX_PHY_PCR_OOBS_CALI_50 |
970 RTSX_PHY_PCR_OOBS_VCM_08 | RTSX_PHY_PCR_OOBS_SEN_90 |
971 RTSX_PHY_PCR_RSSI_EN | RTSX_PHY_PCR_RX10K)))
973 if ((error = rtsx_write_phy(sc, RTSX_PHY_RCR2,
974 RTSX_PHY_RCR2_EMPHASE_EN | RTSX_PHY_RCR2_NADJR |
975 RTSX_PHY_RCR2_CDR_SR_2 | RTSX_PHY_RCR2_FREQSEL_12 |
976 RTSX_PHY_RCR2_CDR_SC_12P | RTSX_PHY_RCR2_CALIB_LATE)))
978 if ((error = rtsx_write_phy(sc, RTSX_PHY_FLD4,
979 RTSX_PHY_FLD4_FLDEN_SEL | RTSX_PHY_FLD4_REQ_REF |
980 RTSX_PHY_FLD4_RXAMP_OFF | RTSX_PHY_FLD4_REQ_ADDA |
981 RTSX_PHY_FLD4_BER_COUNT | RTSX_PHY_FLD4_BER_TIMER |
982 RTSX_PHY_FLD4_BER_CHK_EN)))
984 if ((error = rtsx_write_phy(sc, RTSX_PHY_RDR,
985 RTSX_PHY_RDR_RXDSEL_1_9 | RTSX_PHY_SSC_AUTO_PWD)))
987 if ((error = rtsx_write_phy(sc, RTSX_PHY_RCR1,
988 RTSX_PHY_RCR1_ADP_TIME_4 | RTSX_PHY_RCR1_VCO_COARSE)))
990 if ((error = rtsx_write_phy(sc, RTSX_PHY_FLD3,
991 RTSX_PHY_FLD3_TIMER_4 | RTSX_PHY_FLD3_TIMER_6 |
992 RTSX_PHY_FLD3_RXDELINK)))
994 if ((error = rtsx_write_phy(sc, RTSX_PHY_TUNE,
995 RTSX_PHY_TUNE_TUNEREF_1_0 | RTSX_PHY_TUNE_VBGSEL_1252 |
996 RTSX_PHY_TUNE_SDBUS_33 | RTSX_PHY_TUNE_TUNED18 |
997 RTSX_PHY_TUNE_TUNED12 | RTSX_PHY_TUNE_TUNEA12)))
1002 /* Set mcu_cnt to 7 to ensure data can be sampled properly. */
1003 RTSX_BITOP(sc, RTSX_CLK_DIV, 0x07, 0x07);
1005 /* Disable sleep mode. */
1006 RTSX_CLR(sc, RTSX_HOST_SLEEP_STATE,
1007 RTSX_HOST_ENTER_S1 | RTSX_HOST_ENTER_S3);
1009 /* Disable card clock. */
1010 RTSX_CLR(sc, RTSX_CARD_CLK_EN, RTSX_CARD_CLK_EN_ALL);
1012 /* Reset delink mode. */
1013 RTSX_CLR(sc, RTSX_CHANGE_LINK_STATE,
1014 RTSX_FORCE_RST_CORE_EN | RTSX_NON_STICKY_RST_N_DBG);
1016 /* Card driving select. */
1017 RTSX_WRITE(sc, RTSX_CARD_DRIVE_SEL, sc->rtsx_card_drive_sel);
1019 /* Enable SSC clock. */
1020 RTSX_WRITE(sc, RTSX_SSC_CTL1, RTSX_SSC_8X_EN | RTSX_SSC_SEL_4M);
1021 RTSX_WRITE(sc, RTSX_SSC_CTL2, 0x12);
1023 /* Disable cd_pwr_save. */
1024 RTSX_BITOP(sc, RTSX_CHANGE_LINK_STATE, 0x16, RTSX_MAC_PHY_RST_N_DBG);
1026 /* Clear Link Ready Interrupt. */
1027 RTSX_BITOP(sc, RTSX_IRQSTAT0, RTSX_LINK_READY_INT, RTSX_LINK_READY_INT);
1029 /* Enlarge the estimation window of PERST# glitch
1030 * to reduce the chance of invalid card interrupt. */
1031 RTSX_WRITE(sc, RTSX_PERST_GLITCH_WIDTH, 0x80);
1033 /* Set RC oscillator to 400K. */
1034 RTSX_CLR(sc, RTSX_RCCTL, RTSX_RCCTL_F_2M);
1036 /* Enable interrupt write-clear (default is read-clear). */
1037 RTSX_CLR(sc, RTSX_NFTS_TX_CTRL, RTSX_INT_READ_CLR);
1039 if (sc->rtsx_device_id == RTSX_RTS525A)
1040 RTSX_BITOP(sc, RTSX_PM_CLK_FORCE_CTL, 1, 1);
1042 /* OC power down. */
1043 RTSX_BITOP(sc, RTSX_FPDCTL, RTSX_SD_OC_POWER_DOWN, RTSX_SD_OC_POWER_DOWN);
1045 /* Enable clk_request_n to enable clock power management */
1046 pci_write_config(sc->rtsx_dev, sc->rtsx_pcie_cap + PCIER_LINK_CTL + 1, 1, 1);
1048 /* Enter L1 when host tx idle */
1049 pci_write_config(sc->rtsx_dev, 0x70F, 0x5B, 1);
1052 * Specific extra init.
1054 switch (sc->rtsx_device_id) {
1058 RTSX_WRITE(sc, RTSX_CARD_GPIO, 0x03);
1059 /* Reset ASPM state to default value. */
1060 RTSX_CLR(sc, RTSX_ASPM_FORCE_CTL, RTSX_ASPM_FORCE_MASK);
1061 /* Force CLKREQ# PIN to drive 0 to request clock. */
1062 RTSX_BITOP(sc, RTSX_PETXCFG, 0x08, 0x08);
1063 /* Configure GPIO as output. */
1064 RTSX_WRITE(sc, RTSX_CARD_GPIO_DIR, 0x03);
1065 /* Configure driving. */
1066 RTSX_WRITE(sc, RTSX_SD30_CMD_DRIVE_SEL, sc->rtsx_sd30_drive_sel_3v3);
1069 /* Configure GPIO as output. */
1070 RTSX_BITOP(sc, RTSX_GPIO_CTL, RTSX_GPIO_LED_ON, RTSX_GPIO_LED_ON);
1071 /* Reset ASPM state to default value. */
1072 RTSX_BITOP(sc, RTSX_ASPM_FORCE_CTL, RTSX_ASPM_FORCE_MASK, RTSX_FORCE_ASPM_NO_ASPM);
1073 /* Switch LDO3318 source from DV33 to 3V3. */
1074 RTSX_CLR(sc, RTSX_LDO_PWR_SEL, RTSX_LDO_PWR_SEL_DV33);
1075 RTSX_BITOP(sc, RTSX_LDO_PWR_SEL, RTSX_LDO_PWR_SEL_DV33, RTSX_LDO_PWR_SEL_3V3);
1076 /* Set default OLT blink period. */
1077 RTSX_BITOP(sc, RTSX_OLT_LED_CTL, 0x0F, RTSX_OLT_LED_PERIOD);
1078 /* Configure LTR. */
1079 cap = pci_read_config(sc->rtsx_dev, sc->rtsx_pcie_cap + PCIER_DEVICE_CTL2, 2);
1080 if (cap & PCIEM_CTL2_LTR_ENABLE)
1081 RTSX_WRITE(sc, RTSX_LTR_CTL, 0xa3);
1082 /* Configure OBFF. */
1083 RTSX_BITOP(sc, RTSX_OBFF_CFG, RTSX_OBFF_EN_MASK, RTSX_OBFF_ENABLE);
1084 /* Configure driving. */
1085 if ((error = rtsx_rts5227_fill_driving(sc)))
1087 /* Configure force_clock_req. */
1088 if (sc->rtsx_flags & RTSX_F_REVERSE_SOCKET)
1089 RTSX_BITOP(sc, RTSX_PETXCFG, 0xB8, 0xB8);
1091 RTSX_BITOP(sc, RTSX_PETXCFG, 0xB8, 0x88);
1092 RTSX_CLR(sc, RTSX_PM_CTRL3, RTSX_D3_DELINK_MODE_EN);
1093 /*!!! Added for reboot after Windows. */
1094 RTSX_BITOP(sc, RTSX_PM_CTRL3, RTSX_PM_WAKE_EN, RTSX_PM_WAKE_EN);
1097 /* Configure GPIO as output. */
1098 RTSX_BITOP(sc, RTSX_GPIO_CTL, RTSX_GPIO_LED_ON, RTSX_GPIO_LED_ON);
1099 /* Reset ASPM state to default value. */
1100 /* With this reset: dd if=/dev/random of=/dev/mmcsd0 encounter a timeout. */
1101 //!!! RTSX_BITOP(sc, RTSX_ASPM_FORCE_CTL, RTSX_ASPM_FORCE_MASK, RTSX_FORCE_ASPM_NO_ASPM);
1102 /* Force CLKREQ# PIN to drive 0 to request clock. */
1103 RTSX_BITOP(sc, RTSX_PETXCFG, 0x08, 0x08);
1104 /* Switch LDO3318 source from DV33 to card_3v3. */
1105 RTSX_CLR(sc, RTSX_LDO_PWR_SEL, RTSX_LDO_PWR_SEL_DV33);
1106 RTSX_BITOP(sc, RTSX_LDO_PWR_SEL, RTSX_LDO_PWR_SEL_DV33, RTSX_LDO_PWR_SEL_3V3);
1107 /* Set default OLT blink period. */
1108 RTSX_BITOP(sc, RTSX_OLT_LED_CTL, 0x0F, RTSX_OLT_LED_PERIOD);
1109 /* Configure driving. */
1110 RTSX_WRITE(sc, RTSX_SD30_CMD_DRIVE_SEL, sc->rtsx_sd30_drive_sel_3v3);
1113 /* Add specific init from RTS5227. */
1114 /* Configure GPIO as output. */
1115 RTSX_BITOP(sc, RTSX_GPIO_CTL, RTSX_GPIO_LED_ON, RTSX_GPIO_LED_ON);
1116 /* Reset ASPM state to default value. */
1117 RTSX_BITOP(sc, RTSX_ASPM_FORCE_CTL, RTSX_ASPM_FORCE_MASK, RTSX_FORCE_ASPM_NO_ASPM);
1118 /* Switch LDO3318 source from DV33 to 3V3. */
1119 RTSX_CLR(sc, RTSX_LDO_PWR_SEL, RTSX_LDO_PWR_SEL_DV33);
1120 RTSX_BITOP(sc, RTSX_LDO_PWR_SEL, RTSX_LDO_PWR_SEL_DV33, RTSX_LDO_PWR_SEL_3V3);
1121 /* Set default OLT blink period. */
1122 RTSX_BITOP(sc, RTSX_OLT_LED_CTL, 0x0F, RTSX_OLT_LED_PERIOD);
1123 /* Configure LTR. */
1124 cap = pci_read_config(sc->rtsx_dev, sc->rtsx_pcie_cap + PCIER_DEVICE_CTL2, 2);
1125 if (cap & PCIEM_CTL2_LTR_ENABLE)
1126 RTSX_WRITE(sc, RTSX_LTR_CTL, 0xa3);
1127 /* Configure OBFF. */
1128 RTSX_BITOP(sc, RTSX_OBFF_CFG, RTSX_OBFF_EN_MASK, RTSX_OBFF_ENABLE);
1129 /* Configure driving. */
1130 if ((error = rtsx_rts5227_fill_driving(sc)))
1132 /* Configure force_clock_req. */
1133 if (sc->rtsx_flags & RTSX_F_REVERSE_SOCKET)
1134 RTSX_BITOP(sc, RTSX_PETXCFG, 0xB8, 0xB8);
1136 RTSX_BITOP(sc, RTSX_PETXCFG, 0xB8, 0x88);
1137 RTSX_CLR(sc, RTSX_RTS522A_PM_CTRL3, 0x10);
1139 /* specific for RTS522A. */
1140 RTSX_BITOP(sc, RTSX_FUNC_FORCE_CTL,
1141 RTSX_FUNC_FORCE_UPME_XMT_DBG, RTSX_FUNC_FORCE_UPME_XMT_DBG);
1142 RTSX_BITOP(sc, RTSX_PCLK_CTL, 0x04, 0x04);
1143 RTSX_BITOP(sc, RTSX_PM_EVENT_DEBUG,
1144 RTSX_PME_DEBUG_0, RTSX_PME_DEBUG_0);
1145 RTSX_WRITE(sc, RTSX_PM_CLK_FORCE_CTL, 0x11);
1148 /* Add specific init from RTS5249. */
1149 /* Rest L1SUB Config. */
1150 RTSX_CLR(sc, RTSX_L1SUB_CONFIG3, 0xff);
1151 /* Configure GPIO as output. */
1152 RTSX_BITOP(sc, RTSX_GPIO_CTL, RTSX_GPIO_LED_ON, RTSX_GPIO_LED_ON);
1153 /* Reset ASPM state to default value. */
1154 RTSX_BITOP(sc, RTSX_ASPM_FORCE_CTL, RTSX_ASPM_FORCE_MASK, RTSX_FORCE_ASPM_NO_ASPM);
1155 /* Switch LDO3318 source from DV33 to 3V3. */
1156 RTSX_CLR(sc, RTSX_LDO_PWR_SEL, RTSX_LDO_PWR_SEL_DV33);
1157 RTSX_BITOP(sc, RTSX_LDO_PWR_SEL, RTSX_LDO_PWR_SEL_DV33, RTSX_LDO_PWR_SEL_3V3);
1158 /* Set default OLT blink period. */
1159 RTSX_BITOP(sc, RTSX_OLT_LED_CTL, 0x0F, RTSX_OLT_LED_PERIOD);
1160 /* Configure driving. */
1161 if ((error = rtsx_rts5249_fill_driving(sc)))
1163 /* Configure force_clock_req. */
1164 if (sc->rtsx_flags & RTSX_F_REVERSE_SOCKET)
1165 RTSX_BITOP(sc, RTSX_PETXCFG, 0xB0, 0xB0);
1167 RTSX_BITOP(sc, RTSX_PETXCFG, 0xB0, 0x80);
1169 /* Specifc for RTS525A. */
1170 RTSX_BITOP(sc, RTSX_PCLK_CTL, RTSX_PCLK_MODE_SEL, RTSX_PCLK_MODE_SEL);
1171 if (sc->rtsx_flags & RTSX_F_VERSION_A) {
1172 RTSX_WRITE(sc, RTSX_L1SUB_CONFIG2, RTSX_L1SUB_AUTO_CFG);
1173 RTSX_BITOP(sc, RTSX_RREF_CFG,
1174 RTSX_RREF_VBGSEL_MASK, RTSX_RREF_VBGSEL_1V25);
1175 RTSX_BITOP(sc, RTSX_LDO_VIO_CFG,
1176 RTSX_LDO_VIO_TUNE_MASK, RTSX_LDO_VIO_1V7);
1177 RTSX_BITOP(sc, RTSX_LDO_DV12S_CFG,
1178 RTSX_LDO_D12_TUNE_MASK, RTSX_LDO_D12_TUNE_DF);
1179 RTSX_BITOP(sc, RTSX_LDO_AV12S_CFG,
1180 RTSX_LDO_AV12S_TUNE_MASK, RTSX_LDO_AV12S_TUNE_DF);
1181 RTSX_BITOP(sc, RTSX_LDO_VCC_CFG0,
1182 RTSX_LDO_VCC_LMTVTH_MASK, RTSX_LDO_VCC_LMTVTH_2A);
1183 RTSX_BITOP(sc, RTSX_OOBS_CONFIG,
1184 RTSX_OOBS_AUTOK_DIS | RTSX_OOBS_VAL_MASK, 0x89);
1188 /* Rest L1SUB Config. */
1189 RTSX_CLR(sc, RTSX_L1SUB_CONFIG3, 0xff);
1190 /* Configure GPIO as output. */
1191 RTSX_BITOP(sc, RTSX_GPIO_CTL, RTSX_GPIO_LED_ON, RTSX_GPIO_LED_ON);
1192 /* Reset ASPM state to default value. */
1193 RTSX_BITOP(sc, RTSX_ASPM_FORCE_CTL, RTSX_ASPM_FORCE_MASK, RTSX_FORCE_ASPM_NO_ASPM);
1194 /* Switch LDO3318 source from DV33 to 3V3. */
1195 RTSX_CLR(sc, RTSX_LDO_PWR_SEL, RTSX_LDO_PWR_SEL_DV33);
1196 RTSX_BITOP(sc, RTSX_LDO_PWR_SEL, RTSX_LDO_PWR_SEL_DV33, RTSX_LDO_PWR_SEL_3V3);
1197 /* Set default OLT blink period. */
1198 RTSX_BITOP(sc, RTSX_OLT_LED_CTL, 0x0F, RTSX_OLT_LED_PERIOD);
1199 /* Configure driving. */
1200 if ((error = rtsx_rts5249_fill_driving(sc)))
1202 /* Configure force_clock_req. */
1203 if (sc->rtsx_flags & RTSX_F_REVERSE_SOCKET)
1204 RTSX_BITOP(sc, RTSX_PETXCFG, 0xB0, 0xB0);
1206 RTSX_BITOP(sc, RTSX_PETXCFG, 0xB0, 0x80);
1209 /* Set mcu_cnt to 7 to ensure data can be sampled properly. */
1210 RTSX_BITOP(sc, RTSX_CLK_DIV, 0x07, 0x07);
1211 RTSX_WRITE(sc, RTSX_SSC_DIV_N_0, 0x5D);
1213 RTSX_WRITE(sc, RTSX_RTS5260_AUTOLOAD_CFG4, RTSX_RTS5260_MIMO_DISABLE);
1214 /*Modify SDVCC Tune Default Parameters!*/
1215 RTSX_BITOP(sc, RTSX_LDO_VCC_CFG0, RTSX_RTS5260_DVCC_TUNE_MASK, RTSX_RTS5260_DVCC_33);
1216 RTSX_BITOP(sc, RTSX_PCLK_CTL, RTSX_PCLK_MODE_SEL, RTSX_PCLK_MODE_SEL);
1217 RTSX_BITOP(sc, RTSX_L1SUB_CONFIG1, RTSX_AUX_CLK_ACTIVE_SEL_MASK, RTSX_MAC_CKSW_DONE);
1218 /* Rest L1SUB Config */
1219 RTSX_CLR(sc, RTSX_L1SUB_CONFIG3, 0xFF);
1220 RTSX_BITOP(sc, RTSX_PM_CLK_FORCE_CTL, RTSX_CLK_PM_EN, RTSX_CLK_PM_EN);
1221 RTSX_BITOP(sc, RTSX_PWR_GATE_CTRL, RTSX_PWR_GATE_EN, RTSX_PWR_GATE_EN);
1222 RTSX_BITOP(sc, RTSX_RB_FLUSH, RTSX_U_AUTO_DMA_EN_MASK, RTSX_U_AUTO_DMA_DISABLE);
1223 if (sc->rtsx_flags & RTSX_F_REVERSE_SOCKET)
1224 RTSX_BITOP(sc, RTSX_PETXCFG, 0xB0, 0xB0);
1226 RTSX_BITOP(sc, RTSX_PETXCFG, 0xB0, 0x80);
1227 RTSX_BITOP(sc, RTSX_OBFF_CFG, RTSX_OBFF_EN_MASK, RTSX_OBFF_DISABLE);
1231 RTSX_WRITE(sc, RTSX_SD30_CMD_DRIVE_SEL, sc->rtsx_sd30_drive_sel_3v3);
1232 RTSX_BITOP(sc, RTSX_CARD_PAD_CTL, RTSX_CD_DISABLE_MASK | RTSX_CD_AUTO_DISABLE,
1236 if (sc->rtsx_flags & RTSX_F_8411B_QFN48)
1237 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL3, 0xf5);
1238 RTSX_WRITE(sc, RTSX_SD30_CMD_DRIVE_SEL, sc->rtsx_sd30_drive_sel_3v3);
1239 /* Enable SD interrupt. */
1240 RTSX_BITOP(sc, RTSX_CARD_PAD_CTL, RTSX_CD_DISABLE_MASK | RTSX_CD_AUTO_DISABLE,
1242 /* Clear hw_pfm_en to disable hardware PFM mode. */
1243 RTSX_BITOP(sc, RTSX_FUNC_FORCE_CTL, 0x06, 0x00);
1247 /*!!! Added for reboot after Windows. */
1248 rtsx_bus_power_off(sc);
1249 rtsx_set_sd_timing(sc, bus_timing_normal);
1250 rtsx_set_sd_clock(sc, 0);
1251 /*!!! Added for reboot after Windows. */
1257 rtsx_map_sd_drive(int index)
1259 uint8_t sd_drive[4] =
1266 return (sd_drive[index]);
1269 /* For voltage 3v3. */
1271 rtsx_rts5227_fill_driving(struct rtsx_softc *sc)
1273 u_char driving_3v3[4][3] = {
1279 RTSX_WRITE(sc, RTSX_SD30_CLK_DRIVE_SEL, driving_3v3[sc->rtsx_sd30_drive_sel_3v3][0]);
1280 RTSX_WRITE(sc, RTSX_SD30_CMD_DRIVE_SEL, driving_3v3[sc->rtsx_sd30_drive_sel_3v3][1]);
1281 RTSX_WRITE(sc, RTSX_SD30_DAT_DRIVE_SEL, driving_3v3[sc->rtsx_sd30_drive_sel_3v3][2]);
1286 /* For voltage 3v3. */
1288 rtsx_rts5249_fill_driving(struct rtsx_softc *sc)
1290 u_char driving_3v3[4][3] = {
1296 RTSX_WRITE(sc, RTSX_SD30_CLK_DRIVE_SEL, driving_3v3[sc->rtsx_sd30_drive_sel_3v3][0]);
1297 RTSX_WRITE(sc, RTSX_SD30_CMD_DRIVE_SEL, driving_3v3[sc->rtsx_sd30_drive_sel_3v3][1]);
1298 RTSX_WRITE(sc, RTSX_SD30_DAT_DRIVE_SEL, driving_3v3[sc->rtsx_sd30_drive_sel_3v3][2]);
1304 rtsx_rts5260_fill_driving(struct rtsx_softc *sc)
1306 u_char driving_3v3[4][3] = {
1312 RTSX_WRITE(sc, RTSX_SD30_CLK_DRIVE_SEL, driving_3v3[sc->rtsx_sd30_drive_sel_3v3][0]);
1313 RTSX_WRITE(sc, RTSX_SD30_CMD_DRIVE_SEL, driving_3v3[sc->rtsx_sd30_drive_sel_3v3][1]);
1314 RTSX_WRITE(sc, RTSX_SD30_DAT_DRIVE_SEL, driving_3v3[sc->rtsx_sd30_drive_sel_3v3][2]);
1320 rtsx_read(struct rtsx_softc *sc, uint16_t addr, uint8_t *val)
1325 WRITE4(sc, RTSX_HAIMR, RTSX_HAIMR_BUSY |
1326 (uint32_t)((addr & 0x3FFF) << 16));
1329 reg = READ4(sc, RTSX_HAIMR);
1330 if (!(reg & RTSX_HAIMR_BUSY))
1333 *val = (reg & 0xff);
1335 return ((tries == 0) ? ETIMEDOUT : 0);
1339 rtsx_read_cfg(struct rtsx_softc *sc, uint8_t func, uint16_t addr, uint32_t *val)
1342 uint8_t data0, data1, data2, data3, rwctl;
1344 RTSX_WRITE(sc, RTSX_CFGADDR0, addr);
1345 RTSX_WRITE(sc, RTSX_CFGADDR1, addr >> 8);
1346 RTSX_WRITE(sc, RTSX_CFGRWCTL, RTSX_CFG_BUSY | (func & 0x03 << 4));
1349 RTSX_READ(sc, RTSX_CFGRWCTL, &rwctl);
1350 if (!(rwctl & RTSX_CFG_BUSY))
1357 RTSX_READ(sc, RTSX_CFGDATA0, &data0);
1358 RTSX_READ(sc, RTSX_CFGDATA1, &data1);
1359 RTSX_READ(sc, RTSX_CFGDATA2, &data2);
1360 RTSX_READ(sc, RTSX_CFGDATA3, &data3);
1362 *val = (data3 << 24) | (data2 << 16) | (data1 << 8) | data0;
1368 rtsx_write(struct rtsx_softc *sc, uint16_t addr, uint8_t mask, uint8_t val)
1373 WRITE4(sc, RTSX_HAIMR,
1374 RTSX_HAIMR_BUSY | RTSX_HAIMR_WRITE |
1375 (uint32_t)(((addr & 0x3FFF) << 16) |
1376 (mask << 8) | val));
1379 reg = READ4(sc, RTSX_HAIMR);
1380 if (!(reg & RTSX_HAIMR_BUSY)) {
1381 if (val != (reg & 0xff))
1391 rtsx_read_phy(struct rtsx_softc *sc, uint8_t addr, uint16_t *val)
1394 uint8_t data0, data1, rwctl;
1396 RTSX_WRITE(sc, RTSX_PHY_ADDR, addr);
1397 RTSX_WRITE(sc, RTSX_PHY_RWCTL, RTSX_PHY_BUSY | RTSX_PHY_READ);
1400 RTSX_READ(sc, RTSX_PHY_RWCTL, &rwctl);
1401 if (!(rwctl & RTSX_PHY_BUSY))
1407 RTSX_READ(sc, RTSX_PHY_DATA0, &data0);
1408 RTSX_READ(sc, RTSX_PHY_DATA1, &data1);
1409 *val = data1 << 8 | data0;
1415 rtsx_write_phy(struct rtsx_softc *sc, uint8_t addr, uint16_t val)
1420 RTSX_WRITE(sc, RTSX_PHY_DATA0, val);
1421 RTSX_WRITE(sc, RTSX_PHY_DATA1, val >> 8);
1422 RTSX_WRITE(sc, RTSX_PHY_ADDR, addr);
1423 RTSX_WRITE(sc, RTSX_PHY_RWCTL, RTSX_PHY_BUSY | RTSX_PHY_WRITE);
1426 RTSX_READ(sc, RTSX_PHY_RWCTL, &rwctl);
1427 if (!(rwctl & RTSX_PHY_BUSY))
1431 return ((tries == 0) ? ETIMEDOUT : 0);
1435 * Notice that the meaning of RTSX_PWR_GATE_CTRL changes between RTS5209 and
1436 * RTS5229. In RTS5209 it is a mask of disabled power gates, while in RTS5229
1437 * it is a mask of *enabled* gates.
1440 rtsx_bus_power_off(struct rtsx_softc *sc)
1442 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
1443 device_printf(sc->rtsx_dev, "rtsx_bus_power_off()\n");
1445 /* Disable SD clock. */
1446 RTSX_CLR(sc, RTSX_CARD_CLK_EN, RTSX_SD_CLK_EN);
1448 /* Disable SD output. */
1449 RTSX_CLR(sc, RTSX_CARD_OE, RTSX_SD_OUTPUT_EN);
1451 /* Turn off power. */
1452 switch (sc->rtsx_device_id) {
1454 RTSX_BITOP(sc, RTSX_CARD_PWR_CTL, RTSX_SD_PWR_MASK | RTSX_PMOS_STRG_MASK,
1455 RTSX_SD_PWR_OFF | RTSX_PMOS_STRG_400mA);
1456 RTSX_SET(sc, RTSX_PWR_GATE_CTRL, RTSX_LDO3318_OFF);
1461 RTSX_BITOP(sc, RTSX_CARD_PWR_CTL, RTSX_SD_PWR_MASK | RTSX_PMOS_STRG_MASK,
1462 RTSX_SD_PWR_OFF | RTSX_PMOS_STRG_400mA);
1463 RTSX_CLR(sc, RTSX_PWR_GATE_CTRL, RTSX_LDO3318_PWR_MASK);
1466 RTSX_BITOP(sc, RTSX_LDO_VCC_CFG1, RTSX_LDO_POW_SDVDD1_MASK, RTSX_LDO_POW_SDVDD1_OFF);
1467 RTSX_BITOP(sc, RTSX_LDO_CONFIG2, RTSX_DV331812_POWERON, RTSX_DV331812_POWEROFF);
1472 RTSX_BITOP(sc, RTSX_CARD_PWR_CTL, RTSX_BPP_POWER_MASK,
1473 RTSX_BPP_POWER_OFF);
1474 RTSX_BITOP(sc, RTSX_LDO_CTL, RTSX_BPP_LDO_POWB,
1475 RTSX_BPP_LDO_SUSPEND);
1478 RTSX_CLR(sc, RTSX_PWR_GATE_CTRL, RTSX_LDO3318_PWR_MASK);
1479 RTSX_SET(sc, RTSX_CARD_PWR_CTL, RTSX_SD_PWR_OFF);
1480 RTSX_CLR(sc, RTSX_CARD_PWR_CTL, RTSX_PMOS_STRG_800mA);
1484 /* Disable pull control. */
1485 switch (sc->rtsx_device_id) {
1487 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL1, RTSX_PULL_CTL_DISABLE12);
1488 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL2, RTSX_PULL_CTL_DISABLE12);
1489 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL3, RTSX_PULL_CTL_DISABLE3);
1493 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL2, RTSX_PULL_CTL_DISABLE12);
1494 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL3, RTSX_PULL_CTL_DISABLE3);
1497 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL2, RTSX_PULL_CTL_DISABLE12);
1498 if (sc->rtsx_flags & RTSX_F_VERSION_C)
1499 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL3, RTSX_PULL_CTL_DISABLE3_TYPE_C);
1501 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL3, RTSX_PULL_CTL_DISABLE3);
1506 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL1, 0x66);
1507 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL2, RTSX_PULL_CTL_DISABLE12);
1508 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL3, RTSX_PULL_CTL_DISABLE3);
1509 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL4, 0x55);
1513 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL1, 0x65);
1514 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL2, 0x55);
1515 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL3, 0x95);
1516 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL4, 0x09);
1517 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL5, 0x05);
1518 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL6, 0x04);
1521 if (sc->rtsx_flags & RTSX_F_8411B_QFN48) {
1522 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL2, 0x55);
1523 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL3, 0xf5);
1524 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL6, 0x15);
1526 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL1, 0x65);
1527 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL2, 0x55);
1528 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL3, 0xd5);
1529 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL4, 0x59);
1530 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL5, 0x55);
1531 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL6, 0x15);
1540 rtsx_bus_power_on(struct rtsx_softc *sc)
1542 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
1543 device_printf(sc->rtsx_dev, "rtsx_bus_power_on()\n");
1545 /* Select SD card. */
1546 RTSX_BITOP(sc, RTSX_CARD_SELECT, 0x07, RTSX_SD_MOD_SEL);
1547 RTSX_BITOP(sc, RTSX_CARD_SHARE_MODE, RTSX_CARD_SHARE_MASK, RTSX_CARD_SHARE_48_SD);
1549 /* Enable SD clock. */
1550 RTSX_BITOP(sc, RTSX_CARD_CLK_EN, RTSX_SD_CLK_EN, RTSX_SD_CLK_EN);
1552 /* Enable pull control. */
1553 switch (sc->rtsx_device_id) {
1555 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL1, RTSX_PULL_CTL_ENABLE12);
1556 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL2, RTSX_PULL_CTL_ENABLE12);
1557 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL3, RTSX_PULL_CTL_ENABLE3);
1561 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL2, RTSX_PULL_CTL_ENABLE12);
1562 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL3, RTSX_PULL_CTL_ENABLE3);
1565 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL2, RTSX_PULL_CTL_ENABLE12);
1566 if (sc->rtsx_flags & RTSX_F_VERSION_C)
1567 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL3, RTSX_PULL_CTL_ENABLE3_TYPE_C);
1569 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL3, RTSX_PULL_CTL_ENABLE3);
1574 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL1, 0x66);
1575 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL2, RTSX_PULL_CTL_ENABLE12);
1576 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL3, RTSX_PULL_CTL_ENABLE3);
1577 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL4, 0xaa);
1581 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL1, 0xaa);
1582 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL2, 0xaa);
1583 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL3, 0xa9);
1584 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL4, 0x09);
1585 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL5, 0x09);
1586 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL6, 0x04);
1589 if (sc->rtsx_flags & RTSX_F_8411B_QFN48) {
1590 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL2, 0xaa);
1591 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL3, 0xf9);
1592 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL6, 0x19);
1594 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL1, 0xaa);
1595 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL2, 0xaa);
1596 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL3, 0xd9);
1597 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL4, 0x59);
1598 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL5, 0x55);
1599 RTSX_WRITE(sc, RTSX_CARD_PULL_CTL6, 0x15);
1605 * To avoid a current peak, enable card power in two phases
1606 * with a delay in between.
1608 switch (sc->rtsx_device_id) {
1610 /* Partial power. */
1611 RTSX_BITOP(sc, RTSX_CARD_PWR_CTL, RTSX_SD_PWR_MASK, RTSX_SD_PARTIAL_PWR_ON);
1612 RTSX_BITOP(sc, RTSX_PWR_GATE_CTRL, RTSX_LDO3318_PWR_MASK, RTSX_LDO3318_VCC2);
1617 RTSX_BITOP(sc, RTSX_CARD_PWR_CTL, RTSX_SD_PWR_MASK, RTSX_SD_PWR_ON);
1618 RTSX_BITOP(sc, RTSX_PWR_GATE_CTRL, RTSX_LDO3318_PWR_MASK, RTSX_LDO3318_ON);
1622 /* Partial power. */
1623 RTSX_BITOP(sc, RTSX_CARD_PWR_CTL, RTSX_SD_PWR_MASK, RTSX_SD_PARTIAL_PWR_ON);
1624 RTSX_BITOP(sc, RTSX_PWR_GATE_CTRL, RTSX_LDO3318_PWR_MASK, RTSX_LDO3318_VCC1);
1629 RTSX_BITOP(sc, RTSX_CARD_PWR_CTL, RTSX_SD_PWR_MASK, RTSX_SD_PWR_ON);
1630 RTSX_BITOP(sc, RTSX_PWR_GATE_CTRL, RTSX_LDO3318_PWR_MASK,
1631 RTSX_LDO3318_VCC1 | RTSX_LDO3318_VCC2);
1632 RTSX_BITOP(sc, RTSX_CARD_OE, RTSX_SD_OUTPUT_EN, RTSX_SD_OUTPUT_EN);
1633 RTSX_BITOP(sc, RTSX_CARD_OE, RTSX_MS_OUTPUT_EN, RTSX_MS_OUTPUT_EN);
1636 /* Partial power. */
1637 RTSX_BITOP(sc, RTSX_CARD_PWR_CTL, RTSX_SD_PWR_MASK, RTSX_SD_PARTIAL_PWR_ON);
1638 RTSX_BITOP(sc, RTSX_PWR_GATE_CTRL, RTSX_LDO3318_PWR_MASK, RTSX_LDO3318_VCC1);
1643 RTSX_BITOP(sc, RTSX_CARD_PWR_CTL, RTSX_SD_PWR_MASK, RTSX_SD_PWR_ON);
1644 RTSX_BITOP(sc, RTSX_PWR_GATE_CTRL, RTSX_LDO3318_PWR_MASK,
1645 RTSX_LDO3318_VCC1 | RTSX_LDO3318_VCC2);
1648 RTSX_BITOP(sc, RTSX_LDO_VCC_CFG1, RTSX_LDO_VCC_TUNE_MASK, RTSX_LDO_VCC_3V3);
1650 /* Partial power. */
1651 RTSX_BITOP(sc, RTSX_CARD_PWR_CTL, RTSX_SD_PWR_MASK, RTSX_SD_PARTIAL_PWR_ON);
1652 RTSX_BITOP(sc, RTSX_PWR_GATE_CTRL, RTSX_LDO3318_PWR_MASK, RTSX_LDO3318_VCC1);
1657 RTSX_BITOP(sc, RTSX_CARD_PWR_CTL, RTSX_SD_PWR_MASK, RTSX_SD_PWR_ON);
1658 RTSX_BITOP(sc, RTSX_PWR_GATE_CTRL, RTSX_LDO3318_PWR_MASK,
1659 RTSX_LDO3318_VCC1 | RTSX_LDO3318_VCC2);
1662 RTSX_BITOP(sc, RTSX_LDO_CONFIG2, RTSX_DV331812_VDD1, RTSX_DV331812_VDD1);
1663 RTSX_BITOP(sc, RTSX_LDO_VCC_CFG0, RTSX_RTS5260_DVCC_TUNE_MASK, RTSX_RTS5260_DVCC_33);
1664 RTSX_BITOP(sc, RTSX_LDO_VCC_CFG1, RTSX_LDO_POW_SDVDD1_MASK, RTSX_LDO_POW_SDVDD1_ON);
1665 RTSX_BITOP(sc, RTSX_LDO_CONFIG2, RTSX_DV331812_POWERON, RTSX_DV331812_POWERON);
1669 /* Initialize SD_CFG1 register */
1670 RTSX_WRITE(sc, RTSX_SD_CFG1, RTSX_CLK_DIVIDE_128 | RTSX_SD20_MODE);
1671 RTSX_WRITE(sc, RTSX_SD_SAMPLE_POINT_CTL, RTSX_SD20_RX_POS_EDGE);
1672 RTSX_CLR(sc, RTSX_SD_PUSH_POINT_CTL, 0xff);
1673 RTSX_BITOP(sc, RTSX_CARD_STOP, RTSX_MS_STOP | RTSX_SD_CLR_ERR,
1674 RTSX_MS_STOP | RTSX_SD_CLR_ERR);
1675 /* Reset SD_CFG3 register */
1676 RTSX_CLR(sc, RTSX_SD_CFG3, RTSX_SD30_CLK_END_EN);
1677 RTSX_CLR(sc, RTSX_REG_SD_STOP_SDCLK_CFG,
1678 RTSX_SD30_CLK_STOP_CFG_EN | RTSX_SD30_CLK_STOP_CFG0 | RTSX_SD30_CLK_STOP_CFG1);
1679 RTSX_CLR(sc, RTSX_REG_PRE_RW_MODE, RTSX_EN_INFINITE_MODE);
1684 RTSX_BITOP(sc, RTSX_CARD_PWR_CTL, RTSX_BPP_POWER_MASK,
1685 RTSX_BPP_POWER_5_PERCENT_ON);
1686 RTSX_BITOP(sc, RTSX_LDO_CTL, RTSX_BPP_LDO_POWB,
1687 RTSX_BPP_LDO_SUSPEND);
1689 RTSX_BITOP(sc, RTSX_CARD_PWR_CTL, RTSX_BPP_POWER_MASK,
1690 RTSX_BPP_POWER_10_PERCENT_ON);
1692 RTSX_BITOP(sc, RTSX_CARD_PWR_CTL, RTSX_BPP_POWER_MASK,
1693 RTSX_BPP_POWER_15_PERCENT_ON);
1695 RTSX_BITOP(sc, RTSX_CARD_PWR_CTL, RTSX_BPP_POWER_MASK,
1697 RTSX_BITOP(sc, RTSX_LDO_CTL, RTSX_BPP_LDO_POWB,
1702 /* Enable SD card output. */
1703 RTSX_WRITE(sc, RTSX_CARD_OE, RTSX_SD_OUTPUT_EN);
1714 rtsx_set_bus_width(struct rtsx_softc *sc, enum mmc_bus_width width)
1720 bus_width = RTSX_BUS_WIDTH_1;
1723 bus_width = RTSX_BUS_WIDTH_4;
1726 bus_width = RTSX_BUS_WIDTH_8;
1729 return (MMC_ERR_INVALID);
1731 RTSX_BITOP(sc, RTSX_SD_CFG1, RTSX_BUS_WIDTH_MASK, bus_width);
1733 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) {
1739 device_printf(sc->rtsx_dev, "Setting bus width to %s\n", busw[bus_width]);
1745 rtsx_set_sd_timing(struct rtsx_softc *sc, enum mmc_bus_timing timing)
1747 if (timing == bus_timing_hs && sc->rtsx_force_timing) {
1748 timing = bus_timing_uhs_sdr50;
1749 sc->rtsx_ios_timing = timing;
1752 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
1753 device_printf(sc->rtsx_dev, "rtsx_set_sd_timing(%u)\n", timing);
1756 case bus_timing_uhs_sdr50:
1757 case bus_timing_uhs_sdr104:
1758 sc->rtsx_double_clk = false;
1759 sc->rtsx_vpclk = true;
1760 RTSX_BITOP(sc, RTSX_SD_CFG1, 0x0c | RTSX_SD_ASYNC_FIFO_NOT_RST,
1761 RTSX_SD30_MODE | RTSX_SD_ASYNC_FIFO_NOT_RST);
1762 RTSX_BITOP(sc, RTSX_CLK_CTL, RTSX_CLK_LOW_FREQ, RTSX_CLK_LOW_FREQ);
1763 RTSX_WRITE(sc, RTSX_CARD_CLK_SOURCE,
1764 RTSX_CRC_VAR_CLK0 | RTSX_SD30_FIX_CLK | RTSX_SAMPLE_VAR_CLK1);
1765 RTSX_CLR(sc, RTSX_CLK_CTL, RTSX_CLK_LOW_FREQ);
1768 RTSX_BITOP(sc, RTSX_SD_CFG1, RTSX_SD_MODE_MASK, RTSX_SD20_MODE);
1769 RTSX_BITOP(sc, RTSX_CLK_CTL, RTSX_CLK_LOW_FREQ, RTSX_CLK_LOW_FREQ);
1770 RTSX_WRITE(sc, RTSX_CARD_CLK_SOURCE,
1771 RTSX_CRC_FIX_CLK | RTSX_SD30_VAR_CLK0 | RTSX_SAMPLE_VAR_CLK1);
1772 RTSX_CLR(sc, RTSX_CLK_CTL, RTSX_CLK_LOW_FREQ);
1774 RTSX_BITOP(sc, RTSX_SD_PUSH_POINT_CTL,
1775 RTSX_SD20_TX_SEL_MASK, RTSX_SD20_TX_14_AHEAD);
1776 RTSX_BITOP(sc, RTSX_SD_SAMPLE_POINT_CTL,
1777 RTSX_SD20_RX_SEL_MASK, RTSX_SD20_RX_14_DELAY);
1780 RTSX_BITOP(sc, RTSX_SD_CFG1, RTSX_SD_MODE_MASK, RTSX_SD20_MODE);
1781 RTSX_BITOP(sc, RTSX_CLK_CTL, RTSX_CLK_LOW_FREQ, RTSX_CLK_LOW_FREQ);
1782 RTSX_WRITE(sc, RTSX_CARD_CLK_SOURCE,
1783 RTSX_CRC_FIX_CLK | RTSX_SD30_VAR_CLK0 | RTSX_SAMPLE_VAR_CLK1);
1784 RTSX_CLR(sc, RTSX_CLK_CTL, RTSX_CLK_LOW_FREQ);
1786 RTSX_WRITE(sc, RTSX_SD_PUSH_POINT_CTL, RTSX_SD20_TX_NEG_EDGE);
1787 RTSX_BITOP(sc, RTSX_SD_SAMPLE_POINT_CTL,
1788 RTSX_SD20_RX_SEL_MASK, RTSX_SD20_RX_POS_EDGE);
1796 * Set or change SDCLK frequency or disable the SD clock.
1797 * Return zero on success.
1800 rtsx_set_sd_clock(struct rtsx_softc *sc, uint32_t freq)
1803 uint8_t clk_divider, n, div, mcu;
1806 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
1807 device_printf(sc->rtsx_dev, "rtsx_set_sd_clock(%u)\n", freq);
1809 if (freq == RTSX_SDCLK_OFF) {
1810 error = rtsx_stop_sd_clock(sc);
1814 sc->rtsx_ssc_depth = RTSX_SSC_DEPTH_500K;
1815 sc->rtsx_discovery_mode = (freq <= 1000000) ? true : false;
1817 if (sc->rtsx_discovery_mode) {
1818 /* We use 250k(around) here, in discovery stage. */
1819 clk_divider = RTSX_CLK_DIVIDE_128;
1822 clk_divider = RTSX_CLK_DIVIDE_0;
1824 RTSX_BITOP(sc, RTSX_SD_CFG1, RTSX_CLK_DIVIDE_MASK, clk_divider);
1827 if (sc->rtsx_discovery_mode || !sc->rtsx_double_clk)
1832 switch (sc->rtsx_device_id) {
1836 n = clk * 4 / 5 - 2;
1842 if ((clk <= 2) || (n > RTSX_MAX_DIV_N))
1843 return (MMC_ERR_INVALID);
1845 mcu = 125 / clk + 3;
1849 /* Make sure that the SSC clock div_n is not less than RTSX_MIN_DIV_N. */
1850 div = RTSX_CLK_DIV_1;
1851 while ((n < RTSX_MIN_DIV_N) && (div < RTSX_CLK_DIV_8)) {
1852 switch (sc->rtsx_device_id) {
1856 n = (((n + 2) * 5 / 4) * 2) * 4 / 5 - 2;
1859 n = (n + 2) * 2 - 2;
1865 if (sc->rtsx_double_clk && sc->rtsx_ssc_depth > 1)
1866 sc->rtsx_ssc_depth -= 1;
1868 if (div > RTSX_CLK_DIV_1) {
1869 if (sc->rtsx_ssc_depth > (div - 1))
1870 sc->rtsx_ssc_depth -= (div - 1);
1872 sc->rtsx_ssc_depth = RTSX_SSC_DEPTH_4M;
1875 /* Enable SD clock. */
1876 error = rtsx_switch_sd_clock(sc, clk, n, div, mcu);
1882 rtsx_stop_sd_clock(struct rtsx_softc *sc)
1884 RTSX_CLR(sc, RTSX_CARD_CLK_EN, RTSX_CARD_CLK_EN_ALL);
1885 RTSX_SET(sc, RTSX_SD_BUS_STAT, RTSX_SD_CLK_FORCE_STOP);
1891 rtsx_switch_sd_clock(struct rtsx_softc *sc, uint8_t clk, uint8_t n, uint8_t div, uint8_t mcu)
1893 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) {
1894 device_printf(sc->rtsx_dev, "rtsx_switch_sd_clock() - discovery-mode is %s, ssc_depth: %d\n",
1895 (sc->rtsx_discovery_mode) ? "true" : "false", sc->rtsx_ssc_depth);
1896 device_printf(sc->rtsx_dev, "rtsx_switch_sd_clock() - clk: %d, n: %d, div: %d, mcu: %d\n",
1900 RTSX_BITOP(sc, RTSX_CLK_CTL, RTSX_CLK_LOW_FREQ, RTSX_CLK_LOW_FREQ);
1901 RTSX_WRITE(sc, RTSX_CLK_DIV, (div << 4) | mcu);
1902 RTSX_CLR(sc, RTSX_SSC_CTL1, RTSX_RSTB);
1903 RTSX_BITOP(sc, RTSX_SSC_CTL2, RTSX_SSC_DEPTH_MASK, sc->rtsx_ssc_depth);
1904 RTSX_WRITE(sc, RTSX_SSC_DIV_N_0, n);
1905 RTSX_BITOP(sc, RTSX_SSC_CTL1, RTSX_RSTB, RTSX_RSTB);
1906 if (sc->rtsx_vpclk) {
1907 RTSX_CLR(sc, RTSX_SD_VPCLK0_CTL, RTSX_PHASE_NOT_RESET);
1908 RTSX_BITOP(sc, RTSX_SD_VPCLK0_CTL, RTSX_PHASE_NOT_RESET, RTSX_PHASE_NOT_RESET);
1911 /* Wait SSC clock stable. */
1914 RTSX_CLR(sc, RTSX_CLK_CTL, RTSX_CLK_LOW_FREQ);
1921 rtsx_sd_change_tx_phase(struct rtsx_softc *sc, uint8_t sample_point)
1923 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
1924 device_printf(sc->rtsx_dev, "rtsx_sd_change_tx_phase() - sample_point: %d\n", sample_point);
1926 rtsx_write(sc, RTSX_CLK_CTL, RTSX_CHANGE_CLK, RTSX_CHANGE_CLK);
1927 rtsx_write(sc, RTSX_SD_VPCLK0_CTL, RTSX_PHASE_SELECT_MASK, sample_point);
1928 rtsx_write(sc, RTSX_SD_VPCLK0_CTL, RTSX_PHASE_NOT_RESET, 0);
1929 rtsx_write(sc, RTSX_SD_VPCLK0_CTL, RTSX_PHASE_NOT_RESET, RTSX_PHASE_NOT_RESET);
1930 rtsx_write(sc, RTSX_CLK_CTL, RTSX_CHANGE_CLK, 0);
1931 rtsx_write(sc, RTSX_SD_CFG1, RTSX_SD_ASYNC_FIFO_NOT_RST, 0);
1935 rtsx_sd_change_rx_phase(struct rtsx_softc *sc, uint8_t sample_point)
1937 if (sc->rtsx_debug_mask & RTSX_DEBUG_TUNING)
1938 device_printf(sc->rtsx_dev, "rtsx_sd_change_rx_phase() - sample_point: %d\n", sample_point);
1940 rtsx_write(sc, RTSX_CLK_CTL, RTSX_CHANGE_CLK, RTSX_CHANGE_CLK);
1941 rtsx_write(sc, RTSX_SD_VPCLK1_CTL, RTSX_PHASE_SELECT_MASK, sample_point);
1942 rtsx_write(sc, RTSX_SD_VPCLK1_CTL, RTSX_PHASE_NOT_RESET, 0);
1943 rtsx_write(sc, RTSX_SD_VPCLK1_CTL, RTSX_PHASE_NOT_RESET, RTSX_PHASE_NOT_RESET);
1944 rtsx_write(sc, RTSX_CLK_CTL, RTSX_CHANGE_CLK, 0);
1945 rtsx_write(sc, RTSX_SD_CFG1, RTSX_SD_ASYNC_FIFO_NOT_RST, 0);
1949 rtsx_sd_tuning_rx_phase(struct rtsx_softc *sc, uint32_t *phase_map)
1951 uint32_t raw_phase_map = 0;
1955 for (i = 0; i < RTSX_RX_PHASE_MAX; i++) {
1956 error = rtsx_sd_tuning_rx_cmd(sc, (uint8_t)i);
1958 raw_phase_map |= 1 << i;
1960 if (phase_map != NULL)
1961 *phase_map = raw_phase_map;
1965 rtsx_sd_tuning_rx_cmd(struct rtsx_softc *sc, uint8_t sample_point)
1967 struct mmc_request req = {};
1968 struct mmc_command cmd = {};
1971 cmd.opcode = MMC_SEND_TUNING_BLOCK;
1977 sc->rtsx_req = &req;
1979 rtsx_sd_change_rx_phase(sc, sample_point);
1981 rtsx_write(sc, RTSX_SD_CFG3, RTSX_SD_RSP_80CLK_TIMEOUT_EN,
1982 RTSX_SD_RSP_80CLK_TIMEOUT_EN);
1984 rtsx_init_cmd(sc, &cmd);
1985 rtsx_set_cmd_data_len(sc, 1, 0x40);
1986 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_SD_CFG2, 0xff,
1987 RTSX_SD_CALCULATE_CRC7 | RTSX_SD_CHECK_CRC16 |
1988 RTSX_SD_NO_WAIT_BUSY_END | RTSX_SD_CHECK_CRC7 | RTSX_SD_RSP_LEN_6);
1989 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_SD_TRANSFER,
1990 0xff, RTSX_TM_AUTO_TUNING | RTSX_SD_TRANSFER_START);
1991 rtsx_push_cmd(sc, RTSX_CHECK_REG_CMD, RTSX_SD_TRANSFER,
1992 RTSX_SD_TRANSFER_END, RTSX_SD_TRANSFER_END);
1994 /* Set interrupt post processing */
1995 sc->rtsx_intr_trans_ok = rtsx_sd_tuning_rx_cmd_wakeup;
1996 sc->rtsx_intr_trans_ko = rtsx_sd_tuning_rx_cmd_wakeup;
1998 /* Run the command queue. */
2001 error = rtsx_sd_tuning_rx_cmd_wait(sc, &cmd);
2004 if (sc->rtsx_debug_mask & RTSX_DEBUG_TUNING)
2005 device_printf(sc->rtsx_dev, "rtsx_sd_tuning_rx_cmd() - error: %d\n", error);
2006 rtsx_sd_wait_data_idle(sc);
2007 rtsx_clear_error(sc);
2009 rtsx_write(sc, RTSX_SD_CFG3, RTSX_SD_RSP_80CLK_TIMEOUT_EN, 0);
2011 sc->rtsx_req = NULL;
2019 rtsx_sd_tuning_rx_cmd_wait(struct rtsx_softc *sc, struct mmc_command *cmd)
2022 int mask = RTSX_TRANS_OK_INT | RTSX_TRANS_FAIL_INT;
2024 status = sc->rtsx_intr_status & mask;
2025 while (status == 0) {
2026 if (msleep(&sc->rtsx_intr_status, &sc->rtsx_mtx, 0, "rtsxintr", sc->rtsx_timeout_cmd) == EWOULDBLOCK) {
2027 cmd->error = MMC_ERR_TIMEOUT;
2028 return (MMC_ERR_TIMEOUT);
2030 status = sc->rtsx_intr_status & mask;
2032 return (cmd->error);
2036 rtsx_sd_tuning_rx_cmd_wakeup(struct rtsx_softc *sc)
2038 wakeup(&sc->rtsx_intr_status);
2042 rtsx_sd_wait_data_idle(struct rtsx_softc *sc)
2047 for (i = 0; i < 100; i++) {
2048 rtsx_read(sc, RTSX_SD_DATA_STATE, &val);
2049 if (val & RTSX_SD_DATA_IDLE)
2056 rtsx_sd_search_final_rx_phase(struct rtsx_softc *sc, uint32_t phase_map)
2058 int start = 0, len = 0;
2059 int start_final = 0, len_final = 0;
2060 uint8_t final_phase = 0xff;
2062 while (start < RTSX_RX_PHASE_MAX) {
2063 len = rtsx_sd_get_rx_phase_len(phase_map, start);
2064 if (len_final < len) {
2065 start_final = start;
2068 start += len ? len : 1;
2071 final_phase = (start_final + len_final / 2) % RTSX_RX_PHASE_MAX;
2073 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
2074 device_printf(sc->rtsx_dev,
2075 "rtsx_sd_search_final_rx_phase() - phase_map: %x, start_final: %d, len_final: %d, final_phase: %d\n",
2076 phase_map, start_final, len_final, final_phase);
2082 rtsx_sd_get_rx_phase_len(uint32_t phase_map, int start_bit)
2086 for (i = 0; i < RTSX_RX_PHASE_MAX; i++) {
2087 if ((phase_map & (1 << (start_bit + i) % RTSX_RX_PHASE_MAX)) == 0)
2090 return RTSX_RX_PHASE_MAX;
2092 #endif /* !MMCCAM */
2096 rtsx_led_enable(struct rtsx_softc *sc)
2098 switch (sc->rtsx_device_id) {
2100 RTSX_CLR(sc, RTSX_CARD_GPIO, RTSX_CARD_GPIO_LED_OFF);
2101 RTSX_WRITE(sc, RTSX_CARD_AUTO_BLINK,
2102 RTSX_LED_BLINK_EN | RTSX_LED_BLINK_SPEED);
2105 RTSX_CLR(sc, RTSX_GPIO_CTL, 0x01);
2106 RTSX_WRITE(sc, RTSX_CARD_AUTO_BLINK,
2107 RTSX_LED_BLINK_EN | RTSX_LED_BLINK_SPEED);
2110 RTSX_SET(sc, RTSX_GPIO_CTL, RTSX_GPIO_LED_ON);
2111 RTSX_SET(sc, RTSX_OLT_LED_CTL, RTSX_OLT_LED_AUTOBLINK);
2119 rtsx_led_disable(struct rtsx_softc *sc)
2121 switch (sc->rtsx_device_id) {
2123 RTSX_CLR(sc, RTSX_CARD_AUTO_BLINK, RTSX_LED_BLINK_EN);
2124 RTSX_WRITE(sc, RTSX_CARD_GPIO, RTSX_CARD_GPIO_LED_OFF);
2127 RTSX_CLR(sc, RTSX_CARD_AUTO_BLINK, RTSX_LED_BLINK_EN);
2128 RTSX_SET(sc, RTSX_GPIO_CTL, 0x01);
2131 RTSX_CLR(sc, RTSX_OLT_LED_CTL, RTSX_OLT_LED_AUTOBLINK);
2132 RTSX_CLR(sc, RTSX_GPIO_CTL, RTSX_GPIO_LED_ON);
2138 #endif /* For led */
2141 rtsx_response_type(uint16_t mmc_rsp)
2148 { MMC_RSP_NONE, RTSX_SD_RSP_TYPE_R0 },
2149 { MMC_RSP_R1, RTSX_SD_RSP_TYPE_R1 },
2150 { MMC_RSP_R1B, RTSX_SD_RSP_TYPE_R1B },
2151 { MMC_RSP_R2, RTSX_SD_RSP_TYPE_R2 },
2152 { MMC_RSP_R3, RTSX_SD_RSP_TYPE_R3 },
2153 { MMC_RSP_R4, RTSX_SD_RSP_TYPE_R4 },
2154 { MMC_RSP_R5, RTSX_SD_RSP_TYPE_R5 },
2155 { MMC_RSP_R6, RTSX_SD_RSP_TYPE_R6 },
2156 { MMC_RSP_R7, RTSX_SD_RSP_TYPE_R7 }
2159 for (i = 0; i < nitems(rsp_types); i++) {
2160 if (mmc_rsp == rsp_types[i].mmc_rsp)
2161 return (rsp_types[i].rtsx_rsp);
2168 * Init command buffer with SD command index and argument.
2171 rtsx_init_cmd(struct rtsx_softc *sc, struct mmc_command *cmd)
2173 sc->rtsx_cmd_index = 0;
2174 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_SD_CMD0,
2175 0xff, RTSX_SD_CMD_START | cmd->opcode);
2176 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_SD_CMD1,
2177 0xff, cmd->arg >> 24);
2178 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_SD_CMD2,
2179 0xff, cmd->arg >> 16);
2180 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_SD_CMD3,
2181 0xff, cmd->arg >> 8);
2182 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_SD_CMD4,
2187 * Append a properly encoded host command to the host command buffer.
2190 rtsx_push_cmd(struct rtsx_softc *sc, uint8_t cmd, uint16_t reg,
2191 uint8_t mask, uint8_t data)
2193 KASSERT(sc->rtsx_cmd_index < RTSX_HOSTCMD_MAX,
2194 ("rtsx: Too many host commands (%d)\n", sc->rtsx_cmd_index));
2196 uint32_t *cmd_buffer = (uint32_t *)(sc->rtsx_cmd_dmamem);
2197 cmd_buffer[sc->rtsx_cmd_index++] =
2198 htole32((uint32_t)(cmd & 0x3) << 30) |
2199 ((uint32_t)(reg & 0x3fff) << 16) |
2200 ((uint32_t)(mask) << 8) |
2205 * Queue commands to configure data transfer size.
2208 rtsx_set_cmd_data_len(struct rtsx_softc *sc, uint16_t block_cnt, uint16_t byte_cnt)
2210 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_SD_BLOCK_CNT_L,
2211 0xff, block_cnt & 0xff);
2212 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_SD_BLOCK_CNT_H,
2213 0xff, block_cnt >> 8);
2214 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_SD_BYTE_CNT_L,
2215 0xff, byte_cnt & 0xff);
2216 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_SD_BYTE_CNT_H,
2217 0xff, byte_cnt >> 8);
2221 * Run the command queue.
2224 rtsx_send_cmd(struct rtsx_softc *sc)
2226 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
2227 device_printf(sc->rtsx_dev, "rtsx_send_cmd()\n");
2229 sc->rtsx_intr_status = 0;
2231 /* Sync command DMA buffer. */
2232 bus_dmamap_sync(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamap, BUS_DMASYNC_PREREAD);
2233 bus_dmamap_sync(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamap, BUS_DMASYNC_PREWRITE);
2235 /* Tell the chip where the command buffer is and run the commands. */
2236 WRITE4(sc, RTSX_HCBAR, (uint32_t)sc->rtsx_cmd_buffer);
2237 WRITE4(sc, RTSX_HCBCTLR,
2238 ((sc->rtsx_cmd_index * 4) & 0x00ffffff) | RTSX_START_CMD | RTSX_HW_AUTO_RSP);
2242 * Stop previous command.
2245 rtsx_stop_cmd(struct rtsx_softc *sc)
2247 /* Stop command transfer. */
2248 WRITE4(sc, RTSX_HCBCTLR, RTSX_STOP_CMD);
2250 /* Stop DMA transfer. */
2251 WRITE4(sc, RTSX_HDBCTLR, RTSX_STOP_DMA);
2253 switch (sc->rtsx_device_id) {
2255 rtsx_write(sc, RTSX_RTS5260_DMA_RST_CTL_0,
2256 RTSX_RTS5260_DMA_RST | RTSX_RTS5260_ADMA3_RST,
2257 RTSX_RTS5260_DMA_RST | RTSX_RTS5260_ADMA3_RST);
2258 rtsx_write(sc, RTSX_RBCTL, RTSX_RB_FLUSH, RTSX_RB_FLUSH);
2262 rtsx_write(sc, RTSX_DMACTL, RTSX_DMA_RST, RTSX_DMA_RST);
2264 rtsx_write(sc, RTSX_RBCTL, RTSX_RB_FLUSH, RTSX_RB_FLUSH);
2271 rtsx_clear_error(struct rtsx_softc *sc)
2274 rtsx_write(sc, RTSX_CARD_STOP, RTSX_SD_STOP | RTSX_SD_CLR_ERR,
2275 RTSX_SD_STOP | RTSX_SD_CLR_ERR);
2279 * Signal end of request to mmc/mmcsd.
2282 rtsx_req_done(struct rtsx_softc *sc)
2287 struct mmc_request *req;
2290 if (req->cmd->error == MMC_ERR_NONE) {
2291 if (req->cmd->opcode == MMC_READ_SINGLE_BLOCK ||
2292 req->cmd->opcode == MMC_READ_MULTIPLE_BLOCK)
2293 sc->rtsx_read_count++;
2294 else if (req->cmd->opcode == MMC_WRITE_BLOCK ||
2295 req->cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK)
2296 sc->rtsx_write_count++;
2298 rtsx_clear_error(sc);
2300 callout_stop(&sc->rtsx_timeout_callout);
2301 sc->rtsx_req = NULL;
2304 sc->rtsx_ccb = NULL;
2305 ccb->ccb_h.status = (req->cmd->error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR);
2316 rtsx_send_req(struct rtsx_softc *sc, struct mmc_command *cmd)
2321 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
2322 device_printf(sc->rtsx_dev, "rtsx_send_req() - CMD%d\n", cmd->opcode);
2324 /* Convert response type. */
2325 rsp_type = rtsx_response_type(cmd->flags & MMC_RSP_MASK);
2326 if (rsp_type == 0) {
2327 device_printf(sc->rtsx_dev, "Unknown rsp_type: 0x%lx\n", (cmd->flags & MMC_RSP_MASK));
2328 cmd->error = MMC_ERR_INVALID;
2329 return (MMC_ERR_INVALID);
2332 rtsx_init_cmd(sc, cmd);
2334 /* Queue command to set response type. */
2335 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_SD_CFG2, 0xff, rsp_type);
2337 /* Use the ping-pong buffer (cmd buffer) for commands which do not transfer data. */
2338 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_CARD_DATA_SOURCE,
2339 0x01, RTSX_PINGPONG_BUFFER);
2341 /* Queue commands to perform SD transfer. */
2342 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_SD_TRANSFER,
2343 0xff, RTSX_TM_CMD_RSP | RTSX_SD_TRANSFER_START);
2344 rtsx_push_cmd(sc, RTSX_CHECK_REG_CMD, RTSX_SD_TRANSFER,
2345 RTSX_SD_TRANSFER_END|RTSX_SD_STAT_IDLE,
2346 RTSX_SD_TRANSFER_END|RTSX_SD_STAT_IDLE);
2348 /* If needed queue commands to read back card status response. */
2349 if (rsp_type == RTSX_SD_RSP_TYPE_R2) {
2350 /* Read data from ping-pong buffer. */
2351 for (reg = RTSX_PPBUF_BASE2; reg < RTSX_PPBUF_BASE2 + 16; reg++)
2352 rtsx_push_cmd(sc, RTSX_READ_REG_CMD, reg, 0, 0);
2353 } else if (rsp_type != RTSX_SD_RSP_TYPE_R0) {
2354 /* Read data from SD_CMDx registers. */
2355 for (reg = RTSX_SD_CMD0; reg <= RTSX_SD_CMD4; reg++)
2356 rtsx_push_cmd(sc, RTSX_READ_REG_CMD, reg, 0, 0);
2358 rtsx_push_cmd(sc, RTSX_READ_REG_CMD, RTSX_SD_STAT1, 0, 0);
2360 /* Set transfer OK function. */
2361 if (sc->rtsx_intr_trans_ok == NULL)
2362 sc->rtsx_intr_trans_ok = rtsx_ret_resp;
2364 /* Run the command queue. */
2371 * Return response of previous command (case cmd->data == NULL) and complete resquest.
2372 * This Function is called by the interrupt handler via sc->rtsx_intr_trans_ok.
2375 rtsx_ret_resp(struct rtsx_softc *sc)
2377 struct mmc_command *cmd;
2379 cmd = sc->rtsx_req->cmd;
2380 rtsx_set_resp(sc, cmd);
2385 * Set response of previous command.
2388 rtsx_set_resp(struct rtsx_softc *sc, struct mmc_command *cmd)
2392 rsp_type = rtsx_response_type(cmd->flags & MMC_RSP_MASK);
2394 /* Sync command DMA buffer. */
2395 bus_dmamap_sync(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamap, BUS_DMASYNC_POSTREAD);
2396 bus_dmamap_sync(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamap, BUS_DMASYNC_POSTWRITE);
2398 /* Copy card response into mmc response buffer. */
2399 if (ISSET(cmd->flags, MMC_RSP_PRESENT)) {
2400 uint32_t *cmd_buffer = (uint32_t *)(sc->rtsx_cmd_dmamem);
2402 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD) {
2403 device_printf(sc->rtsx_dev, "cmd_buffer: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
2404 cmd_buffer[0], cmd_buffer[1], cmd_buffer[2], cmd_buffer[3], cmd_buffer[4]);
2407 if (rsp_type == RTSX_SD_RSP_TYPE_R2) {
2408 /* First byte is CHECK_REG_CMD return value, skip it. */
2409 unsigned char *ptr = (unsigned char *)cmd_buffer + 1;
2413 * The controller offloads the last byte {CRC-7, end bit 1}
2414 * of response type R2. Assign dummy CRC, 0, and end bit to this
2415 * byte (ptr[16], goes into the LSB of resp[3] later).
2418 /* The second byte is the status of response, skip it. */
2419 for (i = 0; i < 4; i++)
2420 cmd->resp[i] = be32dec(ptr + 1 + i * 4);
2423 * First byte is CHECK_REG_CMD return value, second
2424 * one is the command op code -- we skip those.
2427 ((be32toh(cmd_buffer[0]) & 0x0000ffff) << 16) |
2428 ((be32toh(cmd_buffer[1]) & 0xffff0000) >> 16);
2431 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
2432 device_printf(sc->rtsx_dev, "cmd->resp: 0x%08x 0x%08x 0x%08x 0x%08x\n",
2433 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
2438 * Use the ping-pong buffer (cmd buffer) for transfer <= 512 bytes.
2441 rtsx_xfer_short(struct rtsx_softc *sc, struct mmc_command *cmd)
2445 if (cmd->data == NULL || cmd->data->len == 0) {
2446 cmd->error = MMC_ERR_INVALID;
2447 return (MMC_ERR_INVALID);
2449 cmd->data->xfer_len = (cmd->data->len > RTSX_MAX_DATA_BLKLEN) ?
2450 RTSX_MAX_DATA_BLKLEN : cmd->data->len;
2452 read = ISSET(cmd->data->flags, MMC_DATA_READ);
2454 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
2455 device_printf(sc->rtsx_dev, "rtsx_xfer_short() - %s xfer: %ld bytes with block size %ld\n",
2456 read ? "Read" : "Write",
2457 (unsigned long)cmd->data->len, (unsigned long)cmd->data->xfer_len);
2459 if (cmd->data->len > 512) {
2460 device_printf(sc->rtsx_dev, "rtsx_xfer_short() - length too large: %ld > 512\n",
2461 (unsigned long)cmd->data->len);
2462 cmd->error = MMC_ERR_INVALID;
2463 return (MMC_ERR_INVALID);
2467 if (sc->rtsx_discovery_mode)
2468 rtsx_write(sc, RTSX_SD_CFG1, RTSX_CLK_DIVIDE_MASK, RTSX_CLK_DIVIDE_0);
2470 rtsx_init_cmd(sc, cmd);
2472 /* Queue commands to configure data transfer size. */
2473 rtsx_set_cmd_data_len(sc, cmd->data->len / cmd->data->xfer_len, cmd->data->xfer_len);
2475 /* From Linux: rtsx_pci_sdmmc.c sd_read_data(). */
2476 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_SD_CFG2, 0xff,
2477 RTSX_SD_CALCULATE_CRC7 | RTSX_SD_CHECK_CRC16 |
2478 RTSX_SD_NO_WAIT_BUSY_END | RTSX_SD_CHECK_CRC7 | RTSX_SD_RSP_LEN_6);
2480 /* Use the ping-pong buffer (cmd buffer). */
2481 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_CARD_DATA_SOURCE,
2482 0x01, RTSX_PINGPONG_BUFFER);
2484 /* Queue commands to perform SD transfer. */
2485 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_SD_TRANSFER,
2486 0xff, RTSX_TM_NORMAL_READ | RTSX_SD_TRANSFER_START);
2487 rtsx_push_cmd(sc, RTSX_CHECK_REG_CMD, RTSX_SD_TRANSFER,
2488 RTSX_SD_TRANSFER_END, RTSX_SD_TRANSFER_END);
2490 /* Set transfer OK function. */
2491 sc->rtsx_intr_trans_ok = rtsx_ask_ppbuf_part1;
2493 /* Run the command queue. */
2496 /* Set transfer OK function. */
2497 sc->rtsx_intr_trans_ok = rtsx_put_ppbuf_part1;
2499 /* Run the command queue. */
2500 rtsx_send_req(sc, cmd);
2507 * Use the ping-pong buffer (cmd buffer) for the transfer - first part <= 256 bytes.
2508 * This Function is called by the interrupt handler via sc->rtsx_intr_trans_ok.
2511 rtsx_ask_ppbuf_part1(struct rtsx_softc *sc)
2513 struct mmc_command *cmd;
2514 uint16_t reg = RTSX_PPBUF_BASE2;
2518 cmd = sc->rtsx_req->cmd;
2519 len = (cmd->data->len > RTSX_HOSTCMD_MAX) ? RTSX_HOSTCMD_MAX : cmd->data->len;
2521 sc->rtsx_cmd_index = 0;
2522 for (i = 0; i < len; i++) {
2523 rtsx_push_cmd(sc, RTSX_READ_REG_CMD, reg++, 0, 0);
2526 /* Set transfer OK function. */
2527 sc->rtsx_intr_trans_ok = rtsx_get_ppbuf_part1;
2529 /* Run the command queue. */
2534 * Get the data from the ping-pong buffer (cmd buffer) - first part <= 256 bytes.
2535 * This Function is called by the interrupt handler via sc->rtsx_intr_trans_ok.
2538 rtsx_get_ppbuf_part1(struct rtsx_softc *sc)
2540 struct mmc_command *cmd;
2544 cmd = sc->rtsx_req->cmd;
2545 ptr = cmd->data->data;
2546 len = (cmd->data->len > RTSX_HOSTCMD_MAX) ? RTSX_HOSTCMD_MAX : cmd->data->len;
2548 /* Sync command DMA buffer. */
2549 bus_dmamap_sync(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamap, BUS_DMASYNC_POSTREAD);
2550 bus_dmamap_sync(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamap, BUS_DMASYNC_POSTWRITE);
2552 memcpy(ptr, sc->rtsx_cmd_dmamem, len);
2554 len = (cmd->data->len > RTSX_HOSTCMD_MAX) ? cmd->data->len - RTSX_HOSTCMD_MAX : 0;
2556 /* Use the ping-pong buffer (cmd buffer) for the transfer - second part > 256 bytes. */
2558 uint16_t reg = RTSX_PPBUF_BASE2 + RTSX_HOSTCMD_MAX;
2561 sc->rtsx_cmd_index = 0;
2562 for (i = 0; i < len; i++) {
2563 rtsx_push_cmd(sc, RTSX_READ_REG_CMD, reg++, 0, 0);
2566 /* Set transfer OK function. */
2567 sc->rtsx_intr_trans_ok = rtsx_get_ppbuf_part2;
2569 /* Run the command queue. */
2572 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD && cmd->opcode == ACMD_SEND_SCR) {
2573 uint8_t *ptr = cmd->data->data;
2574 device_printf(sc->rtsx_dev, "SCR: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2575 ptr[0], ptr[1], ptr[2], ptr[3],
2576 ptr[4], ptr[5], ptr[6], ptr[7]);
2579 if (sc->rtsx_discovery_mode)
2580 rtsx_write(sc, RTSX_SD_CFG1, RTSX_CLK_DIVIDE_MASK, RTSX_CLK_DIVIDE_128);
2587 * Get the data from the ping-pong buffer (cmd buffer) - second part > 256 bytes.
2588 * This Function is called by the interrupt handler via sc->rtsx_intr_trans_ok.
2591 rtsx_get_ppbuf_part2(struct rtsx_softc *sc)
2593 struct mmc_command *cmd;
2597 cmd = sc->rtsx_req->cmd;
2598 ptr = cmd->data->data;
2599 ptr += RTSX_HOSTCMD_MAX;
2600 len = cmd->data->len - RTSX_HOSTCMD_MAX;
2602 /* Sync command DMA buffer. */
2603 bus_dmamap_sync(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamap, BUS_DMASYNC_POSTREAD);
2604 bus_dmamap_sync(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamap, BUS_DMASYNC_POSTWRITE);
2606 memcpy(ptr, sc->rtsx_cmd_dmamem, len);
2608 if (sc->rtsx_discovery_mode)
2609 rtsx_write(sc, RTSX_SD_CFG1, RTSX_CLK_DIVIDE_MASK, RTSX_CLK_DIVIDE_128);
2615 * Use the ping-pong buffer (cmd buffer) for transfer - first part <= 256 bytes.
2616 * This Function is called by the interrupt handler via sc->rtsx_intr_trans_ok.
2619 rtsx_put_ppbuf_part1(struct rtsx_softc *sc)
2621 struct mmc_command *cmd;
2622 uint16_t reg = RTSX_PPBUF_BASE2;
2627 cmd = sc->rtsx_req->cmd;
2628 ptr = cmd->data->data;
2629 len = (cmd->data->len > RTSX_HOSTCMD_MAX) ? RTSX_HOSTCMD_MAX : cmd->data->len;
2631 rtsx_set_resp(sc, cmd);
2633 sc->rtsx_cmd_index = 0;
2634 for (i = 0; i < len; i++) {
2635 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, reg++, 0xff, *ptr);
2639 /* Set transfer OK function. */
2640 if (cmd->data->len > RTSX_HOSTCMD_MAX)
2641 sc->rtsx_intr_trans_ok = rtsx_put_ppbuf_part2;
2643 sc->rtsx_intr_trans_ok = rtsx_write_ppbuf;
2645 /* Run the command queue. */
2650 * Use the ping-pong buffer (cmd buffer) for transfer - second part > 256 bytes.
2651 * This Function is called by the interrupt handler via sc->rtsx_intr_trans_ok.
2654 rtsx_put_ppbuf_part2(struct rtsx_softc *sc)
2656 struct mmc_command *cmd;
2657 uint16_t reg = RTSX_PPBUF_BASE2 + RTSX_HOSTCMD_MAX;
2662 cmd = sc->rtsx_req->cmd;
2663 ptr = cmd->data->data;
2664 ptr += RTSX_HOSTCMD_MAX;
2665 len = cmd->data->len - RTSX_HOSTCMD_MAX;
2667 sc->rtsx_cmd_index = 0;
2668 for (i = 0; i < len; i++) {
2669 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, reg++, 0xff, *ptr);
2673 /* Set transfer OK function. */
2674 sc->rtsx_intr_trans_ok = rtsx_write_ppbuf;
2676 /* Run the command queue. */
2681 * Write the data previously given via the ping-pong buffer on the card.
2682 * This Function is called by the interrupt handler via sc->rtsx_intr_trans_ok.
2685 rtsx_write_ppbuf(struct rtsx_softc *sc)
2687 struct mmc_command *cmd;
2689 cmd = sc->rtsx_req->cmd;
2691 sc->rtsx_cmd_index = 0;
2693 /* Queue commands to configure data transfer size. */
2694 rtsx_set_cmd_data_len(sc, cmd->data->len / cmd->data->xfer_len, cmd->data->xfer_len);
2696 /* From Linux: rtsx_pci_sdmmc.c sd_write_data(). */
2697 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_SD_CFG2, 0xff,
2698 RTSX_SD_CALCULATE_CRC7 | RTSX_SD_CHECK_CRC16 |
2699 RTSX_SD_NO_WAIT_BUSY_END | RTSX_SD_CHECK_CRC7 | RTSX_SD_RSP_LEN_0);
2701 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_SD_TRANSFER, 0xff,
2702 RTSX_TM_AUTO_WRITE3 | RTSX_SD_TRANSFER_START);
2703 rtsx_push_cmd(sc, RTSX_CHECK_REG_CMD, RTSX_SD_TRANSFER,
2704 RTSX_SD_TRANSFER_END, RTSX_SD_TRANSFER_END);
2706 /* Set transfer OK function. */
2707 sc->rtsx_intr_trans_ok = rtsx_req_done;
2709 /* Run the command queue. */
2714 * Use the data buffer for transfer > 512 bytes.
2717 rtsx_xfer(struct rtsx_softc *sc, struct mmc_command *cmd)
2719 int read = ISSET(cmd->data->flags, MMC_DATA_READ);
2721 cmd->data->xfer_len = (cmd->data->len > RTSX_MAX_DATA_BLKLEN) ?
2722 RTSX_MAX_DATA_BLKLEN : cmd->data->len;
2724 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
2725 device_printf(sc->rtsx_dev, "rtsx_xfer() - %s xfer: %ld bytes with block size %ld\n",
2726 read ? "Read" : "Write",
2727 (unsigned long)cmd->data->len, (unsigned long)cmd->data->xfer_len);
2729 if (cmd->data->len > RTSX_DMA_DATA_BUFSIZE) {
2730 device_printf(sc->rtsx_dev, "rtsx_xfer() length too large: %ld > %d\n",
2731 (unsigned long)cmd->data->len, RTSX_DMA_DATA_BUFSIZE);
2732 cmd->error = MMC_ERR_INVALID;
2733 return (MMC_ERR_INVALID);
2737 /* Set transfer OK function. */
2738 sc->rtsx_intr_trans_ok = rtsx_xfer_begin;
2740 /* Run the command queue. */
2741 rtsx_send_req(sc, cmd);
2743 rtsx_xfer_start(sc);
2750 * Get request response and start dma data transfer (write command).
2751 * This Function is called by the interrupt handler via sc->rtsx_intr_trans_ok.
2754 rtsx_xfer_begin(struct rtsx_softc *sc)
2756 struct mmc_command *cmd;
2758 cmd = sc->rtsx_req->cmd;
2760 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
2761 device_printf(sc->rtsx_dev, "rtsx_xfer_begin() - CMD%d\n", cmd->opcode);
2763 rtsx_set_resp(sc, cmd);
2764 rtsx_xfer_start(sc);
2768 * Start dma data transfer.
2771 rtsx_xfer_start(struct rtsx_softc *sc)
2773 struct mmc_command *cmd;
2779 cmd = sc->rtsx_req->cmd;
2780 read = ISSET(cmd->data->flags, MMC_DATA_READ);
2782 /* Configure DMA transfer mode parameters. */
2783 if (cmd->opcode == MMC_READ_MULTIPLE_BLOCK)
2784 cfg2 = RTSX_SD_CHECK_CRC16 | RTSX_SD_NO_WAIT_BUSY_END | RTSX_SD_RSP_LEN_6;
2786 cfg2 = RTSX_SD_CHECK_CRC16 | RTSX_SD_NO_WAIT_BUSY_END | RTSX_SD_RSP_LEN_0;
2788 dma_dir = RTSX_DMA_DIR_FROM_CARD;
2790 * Use transfer mode AUTO_READ1, which assume we not
2791 * already send the read command and don't need to send
2792 * CMD 12 manually after read.
2794 tmode = RTSX_TM_AUTO_READ1;
2795 cfg2 |= RTSX_SD_CALCULATE_CRC7 | RTSX_SD_CHECK_CRC7;
2797 rtsx_init_cmd(sc, cmd);
2799 dma_dir = RTSX_DMA_DIR_TO_CARD;
2801 * Use transfer mode AUTO_WRITE3, wich assumes we've already
2802 * sent the write command and gotten the response, and will
2803 * send CMD 12 manually after writing.
2805 tmode = RTSX_TM_AUTO_WRITE3;
2806 cfg2 |= RTSX_SD_NO_CALCULATE_CRC7 | RTSX_SD_NO_CHECK_CRC7;
2808 sc->rtsx_cmd_index = 0;
2811 /* Queue commands to configure data transfer size. */
2812 rtsx_set_cmd_data_len(sc, cmd->data->len / cmd->data->xfer_len, cmd->data->xfer_len);
2814 /* Configure DMA controller. */
2815 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_IRQSTAT0,
2816 RTSX_DMA_DONE_INT, RTSX_DMA_DONE_INT);
2817 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_DMATC3,
2818 0xff, cmd->data->len >> 24);
2819 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_DMATC2,
2820 0xff, cmd->data->len >> 16);
2821 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_DMATC1,
2822 0xff, cmd->data->len >> 8);
2823 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_DMATC0,
2824 0xff, cmd->data->len);
2825 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_DMACTL,
2826 RTSX_DMA_EN | RTSX_DMA_DIR | RTSX_DMA_PACK_SIZE_MASK,
2827 RTSX_DMA_EN | dma_dir | RTSX_DMA_512);
2829 /* Use the DMA ring buffer for commands which transfer data. */
2830 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_CARD_DATA_SOURCE,
2831 0x01, RTSX_RING_BUFFER);
2833 /* Queue command to set response type. */
2834 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_SD_CFG2, 0xff, cfg2);
2836 /* Queue commands to perform SD transfer. */
2837 rtsx_push_cmd(sc, RTSX_WRITE_REG_CMD, RTSX_SD_TRANSFER,
2838 0xff, tmode | RTSX_SD_TRANSFER_START);
2839 rtsx_push_cmd(sc, RTSX_CHECK_REG_CMD, RTSX_SD_TRANSFER,
2840 RTSX_SD_TRANSFER_END, RTSX_SD_TRANSFER_END);
2842 /* Run the command queue. */
2846 memcpy(sc->rtsx_data_dmamem, cmd->data->data, cmd->data->len);
2848 /* Sync data DMA buffer. */
2849 bus_dmamap_sync(sc->rtsx_data_dma_tag, sc->rtsx_data_dmamap, BUS_DMASYNC_PREREAD);
2850 bus_dmamap_sync(sc->rtsx_data_dma_tag, sc->rtsx_data_dmamap, BUS_DMASYNC_PREWRITE);
2852 /* Set transfer OK function. */
2853 sc->rtsx_intr_trans_ok = rtsx_xfer_finish;
2855 /* Tell the chip where the data buffer is and run the transfer. */
2856 WRITE4(sc, RTSX_HDBAR, sc->rtsx_data_buffer);
2857 WRITE4(sc, RTSX_HDBCTLR, RTSX_TRIG_DMA | (read ? RTSX_DMA_READ : 0) |
2858 (cmd->data->len & 0x00ffffff));
2862 * Finish dma data transfer.
2863 * This Function is called by the interrupt handler via sc->rtsx_intr_trans_ok.
2866 rtsx_xfer_finish(struct rtsx_softc *sc)
2868 struct mmc_command *cmd;
2871 cmd = sc->rtsx_req->cmd;
2873 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
2874 device_printf(sc->rtsx_dev, "rtsx_xfer_finish() - CMD%d\n", cmd->opcode);
2876 read = ISSET(cmd->data->flags, MMC_DATA_READ);
2878 /* Sync data DMA buffer. */
2879 bus_dmamap_sync(sc->rtsx_data_dma_tag, sc->rtsx_data_dmamap, BUS_DMASYNC_POSTREAD);
2880 bus_dmamap_sync(sc->rtsx_data_dma_tag, sc->rtsx_data_dmamap, BUS_DMASYNC_POSTWRITE);
2883 memcpy(cmd->data->data, sc->rtsx_data_dmamem, cmd->data->len);
2886 /* Send CMD12 after AUTO_WRITE3 (see mmcsd_rw() in mmcsd.c) */
2887 /* and complete request. */
2888 sc->rtsx_intr_trans_ok = NULL;
2889 rtsx_send_req(sc, sc->rtsx_req->stop);
2894 * Manage request timeout.
2897 rtsx_timeout(void *arg)
2899 struct rtsx_softc *sc;
2901 sc = (struct rtsx_softc *)arg;
2902 if (sc->rtsx_req != NULL) {
2903 device_printf(sc->rtsx_dev, "Controller timeout for CMD%u\n",
2904 sc->rtsx_req->cmd->opcode);
2905 sc->rtsx_req->cmd->error = MMC_ERR_TIMEOUT;
2909 device_printf(sc->rtsx_dev, "Controller timeout!\n");
2915 rtsx_get_tran_settings(device_t dev, struct ccb_trans_settings_mmc *cts)
2917 struct rtsx_softc *sc;
2919 sc = device_get_softc(dev);
2921 cts->host_ocr = sc->rtsx_host.host_ocr;
2922 cts->host_f_min = sc->rtsx_host.f_min;
2923 cts->host_f_max = sc->rtsx_host.f_max;
2924 cts->host_caps = sc->rtsx_host.caps;
2925 cts->host_max_data = RTSX_DMA_DATA_BUFSIZE / MMC_SECTOR_SIZE;
2926 memcpy(&cts->ios, &sc->rtsx_host.ios, sizeof(struct mmc_ios));
2932 * Apply settings and return status accordingly.
2935 rtsx_set_tran_settings(device_t dev, struct ccb_trans_settings_mmc *cts)
2937 struct rtsx_softc *sc;
2938 struct mmc_ios *ios;
2939 struct mmc_ios *new_ios;
2941 sc = device_get_softc(dev);
2943 ios = &sc->rtsx_host.ios;
2944 new_ios = &cts->ios;
2946 /* Update only requested fields */
2947 if (cts->ios_valid & MMC_CLK) {
2948 ios->clock = new_ios->clock;
2949 sc->rtsx_ios_clock = -1; /* To be updated by rtsx_mmcbr_update_ios(). */
2950 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
2951 device_printf(sc->rtsx_dev, "rtsx_set_tran_settings() - clock: %u\n", ios->clock);
2953 if (cts->ios_valid & MMC_VDD) {
2954 ios->vdd = new_ios->vdd;
2955 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
2956 device_printf(sc->rtsx_dev, "rtsx_set_tran_settings() - vdd: %d\n", ios->vdd);
2958 if (cts->ios_valid & MMC_CS) {
2959 ios->chip_select = new_ios->chip_select;
2960 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
2961 device_printf(sc->rtsx_dev, "rtsx_set_tran_settings() - chip_select: %d\n", ios->chip_select);
2963 if (cts->ios_valid & MMC_BW) {
2964 ios->bus_width = new_ios->bus_width;
2965 sc->rtsx_ios_bus_width = -1; /* To be updated by rtsx_mmcbr_update_ios(). */
2966 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
2967 device_printf(sc->rtsx_dev, "rtsx_set_tran_settings() - bus width: %d\n", ios->bus_width);
2969 if (cts->ios_valid & MMC_PM) {
2970 ios->power_mode = new_ios->power_mode;
2971 sc->rtsx_ios_power_mode = -1; /* To be updated by rtsx_mmcbr_update_ios(). */
2972 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
2973 device_printf(sc->rtsx_dev, "rtsx_set_tran_settings() - power mode: %d\n", ios->power_mode);
2975 if (cts->ios_valid & MMC_BT) {
2976 ios->timing = new_ios->timing;
2977 sc->rtsx_ios_timing = -1; /* To be updated by rtsx_mmcbr_update_ios(). */
2978 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
2979 device_printf(sc->rtsx_dev, "rtsx_set_tran_settings() - timing: %d\n", ios->timing);
2981 if (cts->ios_valid & MMC_BM) {
2982 ios->bus_mode = new_ios->bus_mode;
2983 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
2984 device_printf(sc->rtsx_dev, "rtsx_set_tran_settings() - bus mode: %d\n", ios->bus_mode);
2986 #if __FreeBSD_version >= 1300000
2987 if (cts->ios_valid & MMC_VCCQ) {
2988 ios->vccq = new_ios->vccq;
2989 sc->rtsx_ios_vccq = -1; /* To be updated by rtsx_mmcbr_update_ios(). */
2990 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
2991 device_printf(sc->rtsx_dev, "rtsx_set_tran_settings() - vccq: %d\n", ios->vccq);
2993 #endif /* __FreeBSD_version >= 1300000 */
2994 if (rtsx_mmcbr_update_ios(sc->rtsx_dev, NULL) == 0)
2995 return (CAM_REQ_CMP);
2997 return (CAM_REQ_CMP_ERR);
3001 * Build a request and run it.
3004 rtsx_cam_request(device_t dev, union ccb *ccb)
3006 struct rtsx_softc *sc;
3008 sc = device_get_softc(dev);
3011 if (sc->rtsx_ccb != NULL) {
3016 sc->rtsx_cam_req.cmd = &ccb->mmcio.cmd;
3017 sc->rtsx_cam_req.stop = &ccb->mmcio.stop;
3020 rtsx_mmcbr_request(sc->rtsx_dev, NULL, &sc->rtsx_cam_req);
3026 rtsx_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
3028 struct rtsx_softc *sc;
3030 sc = device_get_softc(bus);
3032 case MMCBR_IVAR_BUS_MODE: /* ivar 0 - 1 = opendrain, 2 = pushpull */
3033 *result = sc->rtsx_host.ios.bus_mode;
3035 case MMCBR_IVAR_BUS_WIDTH: /* ivar 1 - 0 = 1b 2 = 4b, 3 = 8b */
3036 *result = sc->rtsx_host.ios.bus_width;
3038 case MMCBR_IVAR_CHIP_SELECT: /* ivar 2 - O = dontcare, 1 = cs_high, 2 = cs_low */
3039 *result = sc->rtsx_host.ios.chip_select;
3041 case MMCBR_IVAR_CLOCK: /* ivar 3 - clock in Hz */
3042 *result = sc->rtsx_host.ios.clock;
3044 case MMCBR_IVAR_F_MIN: /* ivar 4 */
3045 *result = sc->rtsx_host.f_min;
3047 case MMCBR_IVAR_F_MAX: /* ivar 5 */
3048 *result = sc->rtsx_host.f_max;
3050 case MMCBR_IVAR_HOST_OCR: /* ivar 6 - host operation conditions register */
3051 *result = sc->rtsx_host.host_ocr;
3053 case MMCBR_IVAR_MODE: /* ivar 7 - 0 = mode_mmc, 1 = mode_sd */
3054 *result = sc->rtsx_host.mode;
3056 case MMCBR_IVAR_OCR: /* ivar 8 - operation conditions register */
3057 *result = sc->rtsx_host.ocr;
3059 case MMCBR_IVAR_POWER_MODE: /* ivar 9 - 0 = off, 1 = up, 2 = on */
3060 *result = sc->rtsx_host.ios.power_mode;
3062 case MMCBR_IVAR_VDD: /* ivar 11 - voltage power pin */
3063 *result = sc->rtsx_host.ios.vdd;
3065 case MMCBR_IVAR_VCCQ: /* ivar 12 - signaling: 0 = 1.20V, 1 = 1.80V, 2 = 3.30V */
3066 *result = sc->rtsx_host.ios.vccq;
3068 case MMCBR_IVAR_CAPS: /* ivar 13 */
3069 *result = sc->rtsx_host.caps;
3071 case MMCBR_IVAR_TIMING: /* ivar 14 - 0 = normal, 1 = timing_hs, ... */
3072 *result = sc->rtsx_host.ios.timing;
3074 case MMCBR_IVAR_MAX_DATA: /* ivar 15 */
3075 *result = RTSX_DMA_DATA_BUFSIZE / MMC_SECTOR_SIZE;
3077 case MMCBR_IVAR_RETUNE_REQ: /* ivar 10 */
3078 case MMCBR_IVAR_MAX_BUSY_TIMEOUT: /* ivar 16 */
3083 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
3084 device_printf(bus, "Read ivar #%d, value %#x / #%d\n",
3085 which, *(int *)result, *(int *)result);
3091 rtsx_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
3093 struct rtsx_softc *sc;
3095 sc = device_get_softc(bus);
3096 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
3097 device_printf(bus, "Write ivar #%d, value %#x / #%d\n",
3098 which, (int)value, (int)value);
3101 case MMCBR_IVAR_BUS_MODE: /* ivar 0 - 1 = opendrain, 2 = pushpull */
3102 sc->rtsx_host.ios.bus_mode = value;
3104 case MMCBR_IVAR_BUS_WIDTH: /* ivar 1 - 0 = 1b 2 = 4b, 3 = 8b */
3105 sc->rtsx_host.ios.bus_width = value;
3106 sc->rtsx_ios_bus_width = -1; /* To be updated on next rtsx_mmcbr_update_ios(). */
3108 case MMCBR_IVAR_CHIP_SELECT: /* ivar 2 - O = dontcare, 1 = cs_high, 2 = cs_low */
3109 sc->rtsx_host.ios.chip_select = value;
3111 case MMCBR_IVAR_CLOCK: /* ivar 3 - clock in Hz */
3112 sc->rtsx_host.ios.clock = value;
3113 sc->rtsx_ios_clock = -1; /* To be updated on next rtsx_mmcbr_update_ios(). */
3115 case MMCBR_IVAR_MODE: /* ivar 7 - 0 = mode_mmc, 1 = mode_sd */
3116 sc->rtsx_host.mode = value;
3118 case MMCBR_IVAR_OCR: /* ivar 8 - operation conditions register */
3119 sc->rtsx_host.ocr = value;
3121 case MMCBR_IVAR_POWER_MODE: /* ivar 9 - 0 = off, 1 = up, 2 = on */
3122 sc->rtsx_host.ios.power_mode = value;
3123 sc->rtsx_ios_power_mode = -1; /* To be updated on next rtsx_mmcbr_update_ios(). */
3125 case MMCBR_IVAR_VDD: /* ivar 11 - voltage power pin */
3126 sc->rtsx_host.ios.vdd = value;
3128 case MMCBR_IVAR_VCCQ: /* ivar 12 - signaling: 0 = 1.20V, 1 = 1.80V, 2 = 3.30V */
3129 sc->rtsx_host.ios.vccq = value;
3130 sc->rtsx_ios_vccq = value; /* rtsx_mmcbr_switch_vccq() will be called by mmc.c (MMCCAM undef). */
3132 case MMCBR_IVAR_TIMING: /* ivar 14 - 0 = normal, 1 = timing_hs, ... */
3133 sc->rtsx_host.ios.timing = value;
3134 sc->rtsx_ios_timing = -1; /* To be updated on next rtsx_mmcbr_update_ios(). */
3136 /* These are read-only. */
3137 case MMCBR_IVAR_F_MIN: /* ivar 4 */
3138 case MMCBR_IVAR_F_MAX: /* ivar 5 */
3139 case MMCBR_IVAR_HOST_OCR: /* ivar 6 - host operation conditions register */
3140 case MMCBR_IVAR_RETUNE_REQ: /* ivar 10 */
3141 case MMCBR_IVAR_CAPS: /* ivar 13 */
3142 case MMCBR_IVAR_MAX_DATA: /* ivar 15 */
3143 case MMCBR_IVAR_MAX_BUSY_TIMEOUT: /* ivar 16 */
3152 rtsx_mmcbr_update_ios(device_t bus, device_t child__unused)
3154 struct rtsx_softc *sc;
3155 struct mmc_ios *ios;
3158 sc = device_get_softc(bus);
3159 ios = &sc->rtsx_host.ios;
3161 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
3162 device_printf(bus, "rtsx_mmcbr_update_ios()\n");
3164 /* if MMCBR_IVAR_BUS_WIDTH updated. */
3165 if (sc->rtsx_ios_bus_width < 0) {
3166 sc->rtsx_ios_bus_width = ios->bus_width;
3167 if ((error = rtsx_set_bus_width(sc, ios->bus_width)))
3171 /* if MMCBR_IVAR_POWER_MODE updated. */
3172 if (sc->rtsx_ios_power_mode < 0) {
3173 sc->rtsx_ios_power_mode = ios->power_mode;
3174 switch (ios->power_mode) {
3176 if ((error = rtsx_bus_power_off(sc)))
3180 if ((error = rtsx_bus_power_on(sc)))
3184 if ((error = rtsx_bus_power_on(sc)))
3190 sc->rtsx_double_clk = true;
3191 sc->rtsx_vpclk = false;
3193 /* if MMCBR_IVAR_TIMING updated. */
3194 if (sc->rtsx_ios_timing < 0) {
3195 sc->rtsx_ios_timing = ios->timing;
3196 if ((error = rtsx_set_sd_timing(sc, ios->timing)))
3200 /* if MMCBR_IVAR_CLOCK updated, must be after rtsx_set_sd_timing() */
3201 if (sc->rtsx_ios_clock < 0) {
3202 sc->rtsx_ios_clock = ios->clock;
3203 if ((error = rtsx_set_sd_clock(sc, ios->clock)))
3207 /* if MMCCAM and vccq updated */
3208 if (sc->rtsx_ios_vccq < 0) {
3209 sc->rtsx_ios_vccq = ios->vccq;
3210 if ((error = rtsx_mmcbr_switch_vccq(sc->rtsx_dev, NULL)))
3218 * Set output stage logic power voltage.
3221 rtsx_mmcbr_switch_vccq(device_t bus, device_t child __unused)
3223 struct rtsx_softc *sc;
3227 sc = device_get_softc(bus);
3229 switch (sc->rtsx_host.ios.vccq) {
3240 /* It seems it is always vccq_330. */
3242 switch (sc->rtsx_device_id) {
3245 if ((error = rtsx_write_phy(sc, 0x08, 0x4FE4)))
3247 if ((error = rtsx_rts5227_fill_driving(sc)))
3252 RTSX_BITOP(sc, RTSX_SD30_CMD_DRIVE_SEL, RTSX_SD30_DRIVE_SEL_MASK, sc->rtsx_sd30_drive_sel_3v3);
3253 if ((error = rtsx_write_phy(sc, 0x08, 0x4FE4)))
3257 if ((error = rtsx_write_phy(sc, 0x08, 0x57E4)))
3259 if ((error = rtsx_rts5227_fill_driving(sc)))
3263 RTSX_BITOP(sc, RTSX_LDO_CONFIG2, RTSX_LDO_D3318_MASK, RTSX_LDO_D3318_33V);
3264 RTSX_BITOP(sc, RTSX_SD_PAD_CTL, RTSX_SD_IO_USING_1V8, 0);
3265 if ((error = rtsx_rts5249_fill_driving(sc)))
3269 if ((error = rtsx_read_phy(sc, RTSX_PHY_TUNE, &val)))
3271 if ((error = rtsx_write_phy(sc, RTSX_PHY_TUNE,
3272 (val & RTSX_PHY_TUNE_VOLTAGE_MASK) | RTSX_PHY_TUNE_VOLTAGE_3V3)))
3274 if ((error = rtsx_rts5249_fill_driving(sc)))
3278 RTSX_BITOP(sc, RTSX_LDO_CONFIG2, RTSX_DV331812_VDD1, RTSX_DV331812_VDD1);
3279 RTSX_BITOP(sc, RTSX_LDO_DV18_CFG, RTSX_DV331812_MASK, RTSX_DV331812_33);
3280 if ((error = rtsx_rts5260_fill_driving(sc)))
3284 RTSX_BITOP(sc, RTSX_SD30_CMD_DRIVE_SEL, RTSX_SD30_DRIVE_SEL_MASK, sc->rtsx_sd30_drive_sel_3v3);
3285 RTSX_BITOP(sc, RTSX_LDO_CTL,
3286 (RTSX_BPP_ASIC_MASK << RTSX_BPP_SHIFT_8402) | RTSX_BPP_PAD_MASK,
3287 (RTSX_BPP_ASIC_3V3 << RTSX_BPP_SHIFT_8402) | RTSX_BPP_PAD_3V3);
3291 RTSX_BITOP(sc, RTSX_SD30_CMD_DRIVE_SEL, RTSX_SD30_DRIVE_SEL_MASK, sc->rtsx_sd30_drive_sel_3v3);
3292 RTSX_BITOP(sc, RTSX_LDO_CTL,
3293 (RTSX_BPP_ASIC_MASK << RTSX_BPP_SHIFT_8411) | RTSX_BPP_PAD_MASK,
3294 (RTSX_BPP_ASIC_3V3 << RTSX_BPP_SHIFT_8411) | RTSX_BPP_PAD_3V3);
3300 if (sc->rtsx_debug_mask & (RTSX_DEBUG_BASIC | RTSX_TRACE_SD_CMD))
3301 device_printf(sc->rtsx_dev, "rtsx_mmcbr_switch_vccq(%d)\n", vccq);
3308 * Tune card if bus_timing_uhs_sdr50.
3311 rtsx_mmcbr_tune(device_t bus, device_t child __unused, bool hs400)
3313 struct rtsx_softc *sc;
3314 uint32_t raw_phase_map[RTSX_RX_TUNING_CNT] = {0};
3316 uint8_t final_phase;
3319 sc = device_get_softc(bus);
3321 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
3322 device_printf(sc->rtsx_dev, "rtsx_mmcbr_tune() - hs400 is %s\n",
3323 (hs400) ? "true" : "false");
3325 if (sc->rtsx_ios_timing != bus_timing_uhs_sdr50)
3328 sc->rtsx_tuning_mode = true;
3330 switch (sc->rtsx_device_id) {
3333 rtsx_sd_change_tx_phase(sc, 27);
3336 rtsx_sd_change_tx_phase(sc, 20);
3339 rtsx_sd_change_tx_phase(sc, 27);
3343 rtsx_sd_change_tx_phase(sc, 29);
3348 rtsx_sd_change_tx_phase(sc, 7);
3352 /* trying rx tuning for bus_timing_uhs_sdr50. */
3353 for (i = 0; i < RTSX_RX_TUNING_CNT; i++) {
3354 rtsx_sd_tuning_rx_phase(sc, &(raw_phase_map[i]));
3355 if (raw_phase_map[i] == 0)
3359 phase_map = 0xffffffff;
3360 for (i = 0; i < RTSX_RX_TUNING_CNT; i++) {
3361 if (sc->rtsx_debug_mask & (RTSX_DEBUG_BASIC | RTSX_DEBUG_TUNING))
3362 device_printf(sc->rtsx_dev, "rtsx_mmcbr_tune() - RX raw_phase_map[%d]: 0x%08x\n",
3363 i, raw_phase_map[i]);
3364 phase_map &= raw_phase_map[i];
3366 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
3367 device_printf(sc->rtsx_dev, "rtsx_mmcbr_tune() - RX phase_map: 0x%08x\n", phase_map);
3370 final_phase = rtsx_sd_search_final_rx_phase(sc, phase_map);
3371 if (final_phase != 0xff) {
3372 rtsx_sd_change_rx_phase(sc, final_phase);
3376 sc->rtsx_tuning_mode = false;
3382 rtsx_mmcbr_retune(device_t bus, device_t child __unused, bool reset __unused)
3384 struct rtsx_softc *sc;
3386 sc = device_get_softc(bus);
3388 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
3389 device_printf(sc->rtsx_dev, "rtsx_mmcbr_retune()\n");
3393 #endif /* !MMCCAM */
3396 rtsx_mmcbr_request(device_t bus, device_t child __unused, struct mmc_request *req)
3398 struct rtsx_softc *sc;
3399 struct mmc_command *cmd;
3403 sc = device_get_softc(bus);
3406 if (sc->rtsx_req != NULL) {
3412 cmd->error = error = MMC_ERR_NONE;
3413 sc->rtsx_intr_status = 0;
3414 sc->rtsx_intr_trans_ok = NULL;
3415 sc->rtsx_intr_trans_ko = rtsx_req_done;
3417 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
3418 device_printf(sc->rtsx_dev, "rtsx_mmcbr_request(CMD%u arg %#x, flags %#x, dlen %u, dflags %#x)\n",
3419 cmd->opcode, cmd->arg, cmd->flags,
3420 cmd->data != NULL ? (unsigned int)cmd->data->len : 0,
3421 cmd->data != NULL ? cmd->data->flags : 0);
3423 /* Check if card present. */
3424 if (!ISSET(sc->rtsx_flags, RTSX_F_CARD_PRESENT)) {
3425 cmd->error = error = MMC_ERR_FAILED;
3429 /* Refuse SDIO probe if the chip doesn't support SDIO. */
3430 if (cmd->opcode == IO_SEND_OP_COND &&
3431 !ISSET(sc->rtsx_flags, RTSX_F_SDIO_SUPPORT)) {
3432 cmd->error = error = MMC_ERR_INVALID;
3436 /* Return MMC_ERR_TIMEOUT for SD_IO_RW_DIRECT and IO_SEND_OP_COND. */
3437 if (cmd->opcode == SD_IO_RW_DIRECT || cmd->opcode == IO_SEND_OP_COND) {
3438 cmd->error = error = MMC_ERR_TIMEOUT;
3442 /* Select SD card. */
3443 RTSX_BITOP(sc, RTSX_CARD_SELECT, 0x07, RTSX_SD_MOD_SEL);
3444 RTSX_BITOP(sc, RTSX_CARD_SHARE_MODE, RTSX_CARD_SHARE_MASK, RTSX_CARD_SHARE_48_SD);
3446 if (cmd->data == NULL) {
3448 timeout = sc->rtsx_timeout_cmd;
3449 error = rtsx_send_req(sc, cmd);
3450 } else if (cmd->data->len <= 512) {
3451 timeout = sc->rtsx_timeout_io;
3452 error = rtsx_xfer_short(sc, cmd);
3454 timeout = sc->rtsx_timeout_io;
3455 error = rtsx_xfer(sc, cmd);
3458 if (error == MMC_ERR_NONE) {
3459 callout_reset(&sc->rtsx_timeout_callout, timeout * hz, rtsx_timeout, sc);
3470 rtsx_mmcbr_get_ro(device_t bus, device_t child __unused)
3472 struct rtsx_softc *sc;
3474 sc = device_get_softc(bus);
3476 if (sc->rtsx_inversion == 0)
3477 return (sc->rtsx_read_only);
3479 return !(sc->rtsx_read_only);
3483 rtsx_mmcbr_acquire_host(device_t bus, device_t child __unused)
3485 struct rtsx_softc *sc;
3487 sc = device_get_softc(bus);
3488 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
3489 device_printf(bus, "rtsx_mmcbr_acquire_host()\n");
3492 while (sc->rtsx_bus_busy)
3493 msleep(&sc->rtsx_bus_busy, &sc->rtsx_mtx, 0, "rtsxah", 0);
3494 sc->rtsx_bus_busy++;
3501 rtsx_mmcbr_release_host(device_t bus, device_t child __unused)
3503 struct rtsx_softc *sc;
3505 sc = device_get_softc(bus);
3506 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
3507 device_printf(bus, "rtsx_mmcbr_release_host()\n");
3510 sc->rtsx_bus_busy--;
3511 wakeup(&sc->rtsx_bus_busy);
3516 #endif /* !MMCCAM */
3520 * PCI Support Functions
3525 * Compare the device ID (chip) of this device against the IDs that this driver
3526 * supports. If there is a match, set the description and return success.
3529 rtsx_probe(device_t dev)
3531 struct rtsx_softc *sc;
3537 vendor_id = pci_get_vendor(dev);
3538 device_id = pci_get_device(dev);
3541 for (i = 0; rtsx_devices[i].vendor_id != 0; i++) {
3542 if (rtsx_devices[i].vendor_id == vendor_id &&
3543 rtsx_devices[i].device_id == device_id) {
3544 device_set_desc(dev, rtsx_devices[i].desc);
3545 sc = device_get_softc(dev);
3546 sc->rtsx_device_id = device_id;
3547 result = BUS_PROBE_DEFAULT;
3556 * Attach function is only called if the probe is successful.
3559 rtsx_attach(device_t dev)
3561 struct rtsx_softc *sc = device_get_softc(dev);
3562 struct sysctl_ctx_list *ctx;
3563 struct sysctl_oid_list *tree;
3573 device_printf(dev, "Attach - Vendor ID: 0x%x - Device ID: 0x%x\n",
3574 pci_get_vendor(dev), pci_get_device(dev));
3577 sc->rtsx_req = NULL;
3578 sc->rtsx_timeout_cmd = 1;
3579 sc->rtsx_timeout_io = 10;
3580 sc->rtsx_read_only = 0;
3581 sc->rtsx_inversion = 0;
3582 sc->rtsx_force_timing = 0;
3583 sc->rtsx_debug_mask = 0;
3584 sc->rtsx_read_count = 0;
3585 sc->rtsx_write_count = 0;
3587 maker = kern_getenv("smbios.system.maker");
3588 family = kern_getenv("smbios.system.family");
3589 product = kern_getenv("smbios.system.product");
3590 for (i = 0; rtsx_inversion_models[i].maker != NULL; i++) {
3591 if (strcmp(rtsx_inversion_models[i].maker, maker) == 0 &&
3592 strcmp(rtsx_inversion_models[i].family, family) == 0 &&
3593 strcmp(rtsx_inversion_models[i].product, product) == 0) {
3594 device_printf(dev, "Inversion activated for %s/%s/%s, see BUG in rtsx(4)\n", maker, family, product);
3595 device_printf(dev, "If a card is detected without an SD card present,"
3596 " add dev.rtsx.0.inversion=0 in loader.conf(5)\n");
3597 sc->rtsx_inversion = 1;
3603 ctx = device_get_sysctl_ctx(dev);
3604 tree = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
3605 SYSCTL_ADD_INT(ctx, tree, OID_AUTO, "timeout_io", CTLFLAG_RW,
3606 &sc->rtsx_timeout_io, 0, "Request timeout for I/O commands in seconds");
3607 SYSCTL_ADD_INT(ctx, tree, OID_AUTO, "timeout_cmd", CTLFLAG_RW,
3608 &sc->rtsx_timeout_cmd, 0, "Request timeout for setup commands in seconds");
3609 SYSCTL_ADD_U8(ctx, tree, OID_AUTO, "read_only", CTLFLAG_RD,
3610 &sc->rtsx_read_only, 0, "Card is write protected");
3611 SYSCTL_ADD_U8(ctx, tree, OID_AUTO, "inversion", CTLFLAG_RWTUN,
3612 &sc->rtsx_inversion, 0, "Inversion of card detection and read only status");
3613 SYSCTL_ADD_U8(ctx, tree, OID_AUTO, "force_timing", CTLFLAG_RW,
3614 &sc->rtsx_force_timing, 0, "Force bus_timing_uhs_sdr50");
3615 SYSCTL_ADD_U8(ctx, tree, OID_AUTO, "debug_mask", CTLFLAG_RWTUN,
3616 &sc->rtsx_debug_mask, 0, "debugging mask, see rtsx(4)");
3617 SYSCTL_ADD_U64(ctx, tree, OID_AUTO, "read_count", CTLFLAG_RD | CTLFLAG_STATS,
3618 &sc->rtsx_read_count, 0, "Count of read operations");
3619 SYSCTL_ADD_U64(ctx, tree, OID_AUTO, "write_count", CTLFLAG_RD | CTLFLAG_STATS,
3620 &sc->rtsx_write_count, 0, "Count of write operations");
3622 if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
3623 device_printf(dev, "We are running with inversion: %d\n", sc->rtsx_inversion);
3626 sc->rtsx_irq_res_id = 0;
3627 if (pci_alloc_msi(dev, &msi_count) == 0)
3628 sc->rtsx_irq_res_id = 1;
3629 sc->rtsx_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->rtsx_irq_res_id,
3630 RF_ACTIVE | (sc->rtsx_irq_res_id != 0 ? 0 : RF_SHAREABLE));
3631 if (sc->rtsx_irq_res == NULL) {
3632 device_printf(dev, "Can't allocate IRQ resources for %d\n", sc->rtsx_irq_res_id);
3633 pci_release_msi(dev);
3637 callout_init_mtx(&sc->rtsx_timeout_callout, &sc->rtsx_mtx, 0);
3639 /* Allocate memory resource. */
3640 if (sc->rtsx_device_id == RTSX_RTS525A)
3641 sc->rtsx_res_id = PCIR_BAR(1);
3643 sc->rtsx_res_id = PCIR_BAR(0);
3644 sc->rtsx_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rtsx_res_id, RF_ACTIVE);
3645 if (sc->rtsx_res == NULL) {
3646 device_printf(dev, "Can't allocate memory resource for %d\n", sc->rtsx_res_id);
3647 goto destroy_rtsx_irq_res;
3651 device_printf(dev, "rtsx_irq_res_id: %d, rtsx_res_id: %d\n",
3652 sc->rtsx_irq_res_id, sc->rtsx_res_id);
3654 sc->rtsx_btag = rman_get_bustag(sc->rtsx_res);
3655 sc->rtsx_bhandle = rman_get_bushandle(sc->rtsx_res);
3657 TIMEOUT_TASK_INIT(taskqueue_swi_giant, &sc->rtsx_card_insert_task, 0,
3658 rtsx_card_task, sc);
3659 TASK_INIT(&sc->rtsx_card_remove_task, 0, rtsx_card_task, sc);
3661 /* Allocate two DMA buffers: a command buffer and a data buffer. */
3662 error = rtsx_dma_alloc(sc);
3664 goto destroy_rtsx_irq_res;
3666 /* Activate the interrupt. */
3667 error = bus_setup_intr(dev, sc->rtsx_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
3668 NULL, rtsx_intr, sc, &sc->rtsx_irq_cookie);
3670 device_printf(dev, "Can't set up irq [0x%x]!\n", error);
3671 goto destroy_rtsx_res;
3673 pci_enable_busmaster(dev);
3675 if (rtsx_read_cfg(sc, 0, RTSX_SDIOCFG_REG, &sdio_cfg) == 0) {
3676 if ((sdio_cfg & RTSX_SDIOCFG_SDIO_ONLY) ||
3677 (sdio_cfg & RTSX_SDIOCFG_HAVE_SDIO))
3678 sc->rtsx_flags |= RTSX_F_SDIO_SUPPORT;
3682 sc->rtsx_ccb = NULL;
3683 sc->rtsx_cam_status = 0;
3685 SYSCTL_ADD_U8(ctx, tree, OID_AUTO, "cam_status", CTLFLAG_RD,
3686 &sc->rtsx_cam_status, 0, "driver cam card present");
3688 if (mmc_cam_sim_alloc(dev, "rtsx_mmc", &sc->rtsx_mmc_sim) != 0) {
3689 device_printf(dev, "Can't allocate CAM SIM\n");
3690 goto destroy_rtsx_irq;
3694 /* Initialize device. */
3695 if (rtsx_init(sc)) {
3696 device_printf(dev, "Error during rtsx_init()\n");
3697 goto destroy_rtsx_irq;
3701 * Schedule a card detection as we won't get an interrupt
3702 * if the card is inserted when we attach. We wait a quarter
3703 * of a second to allow for a "spontaneous" interrupt which may
3704 * change the card presence state. This delay avoid a panic
3705 * on some configuration (e.g. Lenovo T540p).
3708 if (rtsx_is_card_present(sc))
3709 device_printf(sc->rtsx_dev, "A card is detected\n");
3711 device_printf(sc->rtsx_dev, "No card is detected\n");
3712 rtsx_card_task(sc, 0);
3715 device_printf(dev, "Device attached\n");
3720 bus_teardown_intr(dev, sc->rtsx_irq_res, sc->rtsx_irq_cookie);
3722 bus_release_resource(dev, SYS_RES_MEMORY, sc->rtsx_res_id,
3725 destroy_rtsx_irq_res:
3726 callout_drain(&sc->rtsx_timeout_callout);
3727 bus_release_resource(dev, SYS_RES_IRQ, sc->rtsx_irq_res_id,
3729 pci_release_msi(dev);
3730 RTSX_LOCK_DESTROY(sc);
3736 rtsx_detach(device_t dev)
3738 struct rtsx_softc *sc = device_get_softc(dev);
3742 device_printf(dev, "Detach - Vendor ID: 0x%x - Device ID: 0x%x\n",
3743 pci_get_vendor(dev), pci_get_device(dev));
3745 /* Disable interrupts. */
3746 sc->rtsx_intr_enabled = 0;
3747 WRITE4(sc, RTSX_BIER, sc->rtsx_intr_enabled);
3750 error = device_delete_children(sc->rtsx_dev);
3751 sc->rtsx_mmc_dev = NULL;
3755 taskqueue_drain_timeout(taskqueue_swi_giant, &sc->rtsx_card_insert_task);
3756 taskqueue_drain(taskqueue_swi_giant, &sc->rtsx_card_remove_task);
3758 /* Teardown the state in our softc created in our attach routine. */
3760 if (sc->rtsx_res != NULL)
3761 bus_release_resource(dev, SYS_RES_MEMORY, sc->rtsx_res_id,
3763 if (sc->rtsx_irq_cookie != NULL)
3764 bus_teardown_intr(dev, sc->rtsx_irq_res, sc->rtsx_irq_cookie);
3765 if (sc->rtsx_irq_res != NULL) {
3766 callout_drain(&sc->rtsx_timeout_callout);
3767 bus_release_resource(dev, SYS_RES_IRQ, sc->rtsx_irq_res_id,
3769 pci_release_msi(dev);
3771 RTSX_LOCK_DESTROY(sc);
3773 mmc_cam_sim_free(&sc->rtsx_mmc_sim);
3780 rtsx_shutdown(device_t dev)
3783 device_printf(dev, "Shutdown\n");
3791 * Device suspend routine.
3794 rtsx_suspend(device_t dev)
3796 struct rtsx_softc *sc = device_get_softc(dev);
3798 device_printf(dev, "Suspend\n");
3801 if (sc->rtsx_ccb != NULL) {
3802 device_printf(dev, "Request in progress: CMD%u, rtsr_intr_status: 0x%08x\n",
3803 sc->rtsx_ccb->mmcio.cmd.opcode, sc->rtsx_intr_status);
3806 if (sc->rtsx_req != NULL) {
3807 device_printf(dev, "Request in progress: CMD%u, rtsr_intr_status: 0x%08x\n",
3808 sc->rtsx_req->cmd->opcode, sc->rtsx_intr_status);
3812 bus_generic_suspend(dev);
3818 * Device resume routine.
3821 rtsx_resume(device_t dev)
3823 device_printf(dev, "Resume\n");
3825 bus_generic_resume(dev);
3830 static device_method_t rtsx_methods[] = {
3831 /* Device interface */
3832 DEVMETHOD(device_probe, rtsx_probe),
3833 DEVMETHOD(device_attach, rtsx_attach),
3834 DEVMETHOD(device_detach, rtsx_detach),
3835 DEVMETHOD(device_shutdown, rtsx_shutdown),
3836 DEVMETHOD(device_suspend, rtsx_suspend),
3837 DEVMETHOD(device_resume, rtsx_resume),
3840 DEVMETHOD(bus_read_ivar, rtsx_read_ivar),
3841 DEVMETHOD(bus_write_ivar, rtsx_write_ivar),
3844 /* MMC bridge interface */
3845 DEVMETHOD(mmcbr_update_ios, rtsx_mmcbr_update_ios),
3846 DEVMETHOD(mmcbr_switch_vccq, rtsx_mmcbr_switch_vccq),
3847 DEVMETHOD(mmcbr_tune, rtsx_mmcbr_tune),
3848 DEVMETHOD(mmcbr_retune, rtsx_mmcbr_retune),
3849 DEVMETHOD(mmcbr_request, rtsx_mmcbr_request),
3850 DEVMETHOD(mmcbr_get_ro, rtsx_mmcbr_get_ro),
3851 DEVMETHOD(mmcbr_acquire_host, rtsx_mmcbr_acquire_host),
3852 DEVMETHOD(mmcbr_release_host, rtsx_mmcbr_release_host),
3853 #endif /* !MMCCAM */
3856 /* MMCCAM interface */
3857 DEVMETHOD(mmc_sim_get_tran_settings, rtsx_get_tran_settings),
3858 DEVMETHOD(mmc_sim_set_tran_settings, rtsx_set_tran_settings),
3859 DEVMETHOD(mmc_sim_cam_request, rtsx_cam_request),
3865 static devclass_t rtsx_devclass;
3867 DEFINE_CLASS_0(rtsx, rtsx_driver, rtsx_methods, sizeof(struct rtsx_softc));
3868 DRIVER_MODULE(rtsx, pci, rtsx_driver, rtsx_devclass, NULL, NULL);
3870 MMC_DECLARE_BRIDGE(rtsx);
3871 #endif /* !MMCCAM */