mesongx_mmc.c revision 1.1 1 1.1 jmcneill /* $NetBSD: mesongx_mmc.c,v 1.1 2019/02/25 19:30:17 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2019 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: mesongx_mmc.c,v 1.1 2019/02/25 19:30:17 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/intr.h>
36 1.1 jmcneill #include <sys/systm.h>
37 1.1 jmcneill #include <sys/kernel.h>
38 1.1 jmcneill #include <sys/bitops.h>
39 1.1 jmcneill #include <sys/gpio.h>
40 1.1 jmcneill
41 1.1 jmcneill #include <dev/sdmmc/sdmmcvar.h>
42 1.1 jmcneill #include <dev/sdmmc/sdmmcchip.h>
43 1.1 jmcneill #include <dev/sdmmc/sdmmc_ioreg.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <dev/fdt/fdtvar.h>
46 1.1 jmcneill
47 1.1 jmcneill #define SD_EMMC_CLOCK 0x00
48 1.1 jmcneill #define CLOCK_CFG_IRQ_SDIO_SLEEP __BIT(25)
49 1.1 jmcneill #define CLOCK_CFG_ALWAYS_ON __BIT(24)
50 1.1 jmcneill #define CLOCK_CFG_RX_DELAY __BITS(23,20)
51 1.1 jmcneill #define CLOCK_CFG_TX_DELAY __BITS(19,16)
52 1.1 jmcneill #define CLOCK_CFG_SRAM_PD __BITS(15,14)
53 1.1 jmcneill #define CLOCK_CFG_RX_PHASE __BITS(13,12)
54 1.1 jmcneill #define CLOCK_CFG_TX_PHASE __BITS(11,10)
55 1.1 jmcneill #define CLOCK_CFG_CO_PHASE __BITS(9,8)
56 1.1 jmcneill #define CLOCK_CFG_SRC __BITS(7,6)
57 1.1 jmcneill #define CLOCK_CFG_DIV __BITS(5,0)
58 1.1 jmcneill #define SD_EMMC_DELAY 0x04
59 1.1 jmcneill #define SD_EMMC_ADJUST 0x08
60 1.1 jmcneill #define ADJUST_ADJ_DELAY __BITS(21,16)
61 1.1 jmcneill #define ADJUST_CALI_RISE __BIT(14)
62 1.1 jmcneill #define ADJUST_ADJ_ENABLE __BIT(13)
63 1.1 jmcneill #define ADJUST_CALI_ENABLE __BIT(12)
64 1.1 jmcneill #define ADJUST_CALI_SEL __BITS(11,8)
65 1.1 jmcneill #define SD_EMMC_CALOUT 0x10
66 1.1 jmcneill #define CALOUT_CALI_SETUP __BITS(15,8)
67 1.1 jmcneill #define CALOUT_CALI_VLD __BIT(7)
68 1.1 jmcneill #define CALOUT_CALI_IDX __BITS(5,0)
69 1.1 jmcneill #define SD_EMMC_START 0x40
70 1.1 jmcneill #define START_DESC_ADDR __BITS(31,2)
71 1.1 jmcneill #define START_DESC_BUSY __BIT(1)
72 1.1 jmcneill #define START_DESC_INT __BIT(0)
73 1.1 jmcneill #define SD_EMMC_CFG 0x44
74 1.1 jmcneill #define CFG_IP_TXD_ADJ __BITS(31,28)
75 1.1 jmcneill #define CFG_ERR_ABORT __BIT(27)
76 1.1 jmcneill #define CFG_IRQ_DS __BIT(26)
77 1.1 jmcneill #define CFG_TXD_RETRY __BIT(25)
78 1.1 jmcneill #define CFG_TXD_ADD_ERR __BIT(24)
79 1.1 jmcneill #define CFG_AUTO_CLK __BIT(23)
80 1.1 jmcneill #define CFG_STOP_CLK __BIT(22)
81 1.1 jmcneill #define CFG_CMD_LOW __BIT(21)
82 1.1 jmcneill #define CFG_CHK_DS __BIT(20)
83 1.1 jmcneill #define CFG_IGNORE_OWNER __BIT(19)
84 1.1 jmcneill #define CFG_SDCLK_ALWAYS_ON __BIT(18)
85 1.1 jmcneill #define CFG_BLK_GAP_IP __BIT(17)
86 1.1 jmcneill #define CFG_OUT_FALL __BIT(16)
87 1.1 jmcneill #define CFG_RC_CC __BITS(15,12)
88 1.1 jmcneill #define CFG_RESP_TIMEOUT __BIT(11,8)
89 1.1 jmcneill #define CFG_BL_LEN __BITS(7,4)
90 1.1 jmcneill #define CFG_DC_UGT __BIT(3)
91 1.1 jmcneill #define CFG_DDR __BIT(2)
92 1.1 jmcneill #define CFG_BUS_WIDTH __BITS(1,0)
93 1.1 jmcneill #define CFG_BUS_WIDTH_1 0
94 1.1 jmcneill #define CFG_BUS_WIDTH_4 1
95 1.1 jmcneill #define CFG_BUS_WIDTH_8 2
96 1.1 jmcneill #define SD_EMMC_STATUS 0x48
97 1.1 jmcneill #define STATUS_CORE_BUSY __BIT(31)
98 1.1 jmcneill #define STATUS_DESC_BUSY __BIT(30)
99 1.1 jmcneill #define STATUS_BUS_FSM __BIT(29,26)
100 1.1 jmcneill #define STATUS_DS __BIT(25)
101 1.1 jmcneill #define STATUS_CMD_I __BIT(24)
102 1.1 jmcneill #define STATUS_DAT_I __BITS(23,16)
103 1.1 jmcneill #define STATUS_IRQ_SDIO __BIT(15)
104 1.1 jmcneill #define STATUS_RESP_STATUS __BIT(14)
105 1.1 jmcneill #define STATUS_END_OF_CHAIN __BIT(13)
106 1.1 jmcneill #define STATUS_DESC_TIMEOUT __BIT(12)
107 1.1 jmcneill #define STATUS_RESP_TIMEOUT __BIT(11)
108 1.1 jmcneill #define STATUS_RESP_ERR __BIT(10)
109 1.1 jmcneill #define STATUS_DESC_ERR __BIT(9)
110 1.1 jmcneill #define STATUS_TXD_ERR __BIT(8)
111 1.1 jmcneill #define STATUS_RXD_ERR __BITS(7,0)
112 1.1 jmcneill #define STATUS_TIMEOUT (STATUS_DESC_TIMEOUT | STATUS_RESP_TIMEOUT)
113 1.1 jmcneill #define STATUS_ERROR (STATUS_RESP_ERR | STATUS_DESC_ERR | STATUS_RXD_ERR | STATUS_TXD_ERR)
114 1.1 jmcneill #define SD_EMMC_IRQ_EN 0x4c
115 1.1 jmcneill #define IRQ_EN_CFG_SECURE __BIT(16)
116 1.1 jmcneill #define IRQ_EN_IRQ_SDIO __BIT(15)
117 1.1 jmcneill #define IRQ_EN_RESP_STATUS __BIT(14)
118 1.1 jmcneill #define IRQ_EN_END_OF_CHAIN __BIT(13)
119 1.1 jmcneill #define IRQ_EN_DESC_TIMEOUT __BIT(12)
120 1.1 jmcneill #define IRQ_EN_RESP_TIMEOUT __BIT(11)
121 1.1 jmcneill #define IRQ_EN_RESP_ERR __BIT(10)
122 1.1 jmcneill #define IRQ_EN_DESC_ERR __BIT(9)
123 1.1 jmcneill #define IRQ_EN_TXD_ERR __BIT(8)
124 1.1 jmcneill #define IRQ_EN_RXD_ERR __BITS(7,0)
125 1.1 jmcneill #define SD_EMMC_CMD_CFG 0x50
126 1.1 jmcneill #define SD_EMMC_CMD_ARG 0x54
127 1.1 jmcneill #define SD_EMMC_CMD_DAT 0x58
128 1.1 jmcneill #define SD_EMMC_CMD_RSP 0x5c
129 1.1 jmcneill #define SD_EMMC_CMD_RSP1 0x60
130 1.1 jmcneill #define SD_EMMC_CMD_RSP2 0x64
131 1.1 jmcneill #define SD_EMMC_CMD_RSP3 0x68
132 1.1 jmcneill
133 1.1 jmcneill struct mesongx_mmc_desc {
134 1.1 jmcneill uint32_t flags;
135 1.1 jmcneill #define MESONGX_MMC_FLAGS_OWNER __BIT(31)
136 1.1 jmcneill #define MESONGX_MMC_FLAGS_ERROR __BIT(30)
137 1.1 jmcneill #define MESONGX_MMC_FLAGS_CMD_INDEX __BITS(29,24)
138 1.1 jmcneill #define MESONGX_MMC_FLAGS_DATA_NUM __BIT(23)
139 1.1 jmcneill #define MESONGX_MMC_FLAGS_RESP_NUM __BIT(22)
140 1.1 jmcneill #define MESONGX_MMC_FLAGS_RESP_128 __BIT(21)
141 1.1 jmcneill #define MESONGX_MMC_FLAGS_RESP_NOCRC __BIT(20)
142 1.1 jmcneill #define MESONGX_MMC_FLAGS_DATA_WR __BIT(19)
143 1.1 jmcneill #define MESONGX_MMC_FLAGS_DATA_IO __BIT(18)
144 1.1 jmcneill #define MESONGX_MMC_FLAGS_NO_CMD __BIT(17)
145 1.1 jmcneill #define MESONGX_MMC_FLAGS_NO_RESP __BIT(16)
146 1.1 jmcneill #define MESONGX_MMC_FLAGS_TIMEOUT __BITS(15,12)
147 1.1 jmcneill #define MESONGX_MMC_FLAGS_END_OF_CHAIN __BIT(11)
148 1.1 jmcneill #define MESONGX_MMC_FLAGS_R1B __BIT(10)
149 1.1 jmcneill #define MESONGX_MMC_FLAGS_BLOCK_MODE __BIT(9)
150 1.1 jmcneill #define MESONGX_MMC_FLAGS_LENGTH __BITS(8,0)
151 1.1 jmcneill uint32_t arg;
152 1.1 jmcneill uint32_t data;
153 1.1 jmcneill #define MESONGX_MMC_DATA_BIG_ENDIAN __BIT(1)
154 1.1 jmcneill #define MESONGX_MMC_DATA_SRAM __BIT(0)
155 1.1 jmcneill uint32_t resp;
156 1.1 jmcneill #define MESONGX_MMC_RESP_SRAM __BIT(0)
157 1.1 jmcneill } __packed;
158 1.1 jmcneill
159 1.1 jmcneill #define MESONGX_MMC_NDESC 256
160 1.1 jmcneill
161 1.1 jmcneill struct mesongx_mmc_softc;
162 1.1 jmcneill
163 1.1 jmcneill static int mesongx_mmc_match(device_t, cfdata_t, void *);
164 1.1 jmcneill static void mesongx_mmc_attach(device_t, device_t, void *);
165 1.1 jmcneill static void mesongx_mmc_attach_i(device_t);
166 1.1 jmcneill
167 1.1 jmcneill static int mesongx_mmc_intr(void *);
168 1.1 jmcneill static int mesongx_mmc_dma_setup(struct mesongx_mmc_softc *);
169 1.1 jmcneill static int mesongx_mmc_dmabounce_setup(struct mesongx_mmc_softc *);
170 1.1 jmcneill
171 1.1 jmcneill static int mesongx_mmc_host_reset(sdmmc_chipset_handle_t);
172 1.1 jmcneill static uint32_t mesongx_mmc_host_ocr(sdmmc_chipset_handle_t);
173 1.1 jmcneill static int mesongx_mmc_host_maxblklen(sdmmc_chipset_handle_t);
174 1.1 jmcneill static int mesongx_mmc_card_detect(sdmmc_chipset_handle_t);
175 1.1 jmcneill static int mesongx_mmc_write_protect(sdmmc_chipset_handle_t);
176 1.1 jmcneill static int mesongx_mmc_bus_power(sdmmc_chipset_handle_t, uint32_t);
177 1.1 jmcneill static int mesongx_mmc_bus_clock(sdmmc_chipset_handle_t, int, bool);
178 1.1 jmcneill static int mesongx_mmc_bus_width(sdmmc_chipset_handle_t, int);
179 1.1 jmcneill static int mesongx_mmc_bus_rod(sdmmc_chipset_handle_t, int);
180 1.1 jmcneill static int mesongx_mmc_signal_voltage(sdmmc_chipset_handle_t, int);
181 1.1 jmcneill static int mesongx_mmc_execute_tuning(sdmmc_chipset_handle_t, int);
182 1.1 jmcneill static void mesongx_mmc_exec_command(sdmmc_chipset_handle_t,
183 1.1 jmcneill struct sdmmc_command *);
184 1.1 jmcneill static void mesongx_mmc_card_enable_intr(sdmmc_chipset_handle_t, int);
185 1.1 jmcneill static void mesongx_mmc_card_intr_ack(sdmmc_chipset_handle_t);
186 1.1 jmcneill
187 1.1 jmcneill static struct sdmmc_chip_functions mesongx_mmc_chip_functions = {
188 1.1 jmcneill .host_reset = mesongx_mmc_host_reset,
189 1.1 jmcneill .host_ocr = mesongx_mmc_host_ocr,
190 1.1 jmcneill .host_maxblklen = mesongx_mmc_host_maxblklen,
191 1.1 jmcneill .card_detect = mesongx_mmc_card_detect,
192 1.1 jmcneill .write_protect = mesongx_mmc_write_protect,
193 1.1 jmcneill .bus_power = mesongx_mmc_bus_power,
194 1.1 jmcneill .bus_clock_ddr = mesongx_mmc_bus_clock,
195 1.1 jmcneill .bus_width = mesongx_mmc_bus_width,
196 1.1 jmcneill .bus_rod = mesongx_mmc_bus_rod,
197 1.1 jmcneill .signal_voltage = mesongx_mmc_signal_voltage,
198 1.1 jmcneill .execute_tuning = mesongx_mmc_execute_tuning,
199 1.1 jmcneill .exec_command = mesongx_mmc_exec_command,
200 1.1 jmcneill .card_enable_intr = mesongx_mmc_card_enable_intr,
201 1.1 jmcneill .card_intr_ack = mesongx_mmc_card_intr_ack,
202 1.1 jmcneill };
203 1.1 jmcneill
204 1.1 jmcneill struct mesongx_mmc_softc {
205 1.1 jmcneill device_t sc_dev;
206 1.1 jmcneill bus_space_tag_t sc_bst;
207 1.1 jmcneill bus_space_handle_t sc_bsh;
208 1.1 jmcneill bus_dma_tag_t sc_dmat;
209 1.1 jmcneill int sc_phandle;
210 1.1 jmcneill
211 1.1 jmcneill void *sc_ih;
212 1.1 jmcneill kmutex_t sc_intr_lock;
213 1.1 jmcneill kcondvar_t sc_intr_cv;
214 1.1 jmcneill
215 1.1 jmcneill device_t sc_sdmmc_dev;
216 1.1 jmcneill uint32_t sc_host_ocr;
217 1.1 jmcneill
218 1.1 jmcneill struct sdmmc_command *sc_cmd;
219 1.1 jmcneill
220 1.1 jmcneill bus_dma_segment_t sc_desc_segs[1];
221 1.1 jmcneill int sc_desc_nsegs;
222 1.1 jmcneill bus_size_t sc_desc_size;
223 1.1 jmcneill bus_dmamap_t sc_desc_map;
224 1.1 jmcneill int sc_desc_ndesc;
225 1.1 jmcneill void *sc_desc_desc;
226 1.1 jmcneill
227 1.1 jmcneill bus_dmamap_t sc_dmabounce_map;
228 1.1 jmcneill void *sc_dmabounce_buf;
229 1.1 jmcneill size_t sc_dmabounce_buflen;
230 1.1 jmcneill
231 1.1 jmcneill struct clk *sc_clk_core;
232 1.1 jmcneill struct clk *sc_clk_clkin[2];
233 1.1 jmcneill
234 1.1 jmcneill struct fdtbus_reset *sc_rst;
235 1.1 jmcneill
236 1.1 jmcneill struct fdtbus_gpio_pin *sc_gpio_cd;
237 1.1 jmcneill int sc_gpio_cd_inverted;
238 1.1 jmcneill struct fdtbus_gpio_pin *sc_gpio_wp;
239 1.1 jmcneill int sc_gpio_wp_inverted;
240 1.1 jmcneill
241 1.1 jmcneill struct fdtbus_regulator *sc_reg_vmmc;
242 1.1 jmcneill struct fdtbus_regulator *sc_reg_vqmmc;
243 1.1 jmcneill
244 1.1 jmcneill struct fdtbus_mmc_pwrseq *sc_pwrseq;
245 1.1 jmcneill
246 1.1 jmcneill u_int sc_max_frequency;
247 1.1 jmcneill bool sc_non_removable;
248 1.1 jmcneill bool sc_broken_cd;
249 1.1 jmcneill };
250 1.1 jmcneill
251 1.1 jmcneill CFATTACH_DECL_NEW(mesongx_mmc, sizeof(struct mesongx_mmc_softc),
252 1.1 jmcneill mesongx_mmc_match, mesongx_mmc_attach, NULL, NULL);
253 1.1 jmcneill
254 1.1 jmcneill #define MMC_WRITE(sc, reg, val) \
255 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
256 1.1 jmcneill #define MMC_READ(sc, reg) \
257 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
258 1.1 jmcneill
259 1.1 jmcneill static const struct of_compat_data compat_data[] = {
260 1.1 jmcneill { "amlogic,meson-gx-mmc", 1 },
261 1.1 jmcneill { "amlogic,meson-gxbb-mmc", 1 },
262 1.1 jmcneill { NULL }
263 1.1 jmcneill };
264 1.1 jmcneill
265 1.1 jmcneill static int
266 1.1 jmcneill mesongx_mmc_match(device_t parent, cfdata_t cf, void *aux)
267 1.1 jmcneill {
268 1.1 jmcneill struct fdt_attach_args * const faa = aux;
269 1.1 jmcneill
270 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
271 1.1 jmcneill }
272 1.1 jmcneill
273 1.1 jmcneill static void
274 1.1 jmcneill mesongx_mmc_attach(device_t parent, device_t self, void *aux)
275 1.1 jmcneill {
276 1.1 jmcneill struct mesongx_mmc_softc * const sc = device_private(self);
277 1.1 jmcneill struct fdt_attach_args * const faa = aux;
278 1.1 jmcneill const int phandle = faa->faa_phandle;
279 1.1 jmcneill char intrstr[128];
280 1.1 jmcneill bus_addr_t addr;
281 1.1 jmcneill bus_size_t size;
282 1.1 jmcneill
283 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
284 1.1 jmcneill aprint_error(": couldn't get registers\n");
285 1.1 jmcneill return;
286 1.1 jmcneill }
287 1.1 jmcneill
288 1.1 jmcneill sc->sc_clk_core = fdtbus_clock_get(phandle, "core");
289 1.1 jmcneill sc->sc_clk_clkin[0] = fdtbus_clock_get(phandle, "clkin0");
290 1.1 jmcneill sc->sc_clk_clkin[1] = fdtbus_clock_get(phandle, "clkin1");
291 1.1 jmcneill
292 1.1 jmcneill if (sc->sc_clk_core == NULL || sc->sc_clk_clkin[0] == NULL ||
293 1.1 jmcneill sc->sc_clk_clkin[1] == NULL) {
294 1.1 jmcneill aprint_error(": couldn't get clocks\n");
295 1.1 jmcneill return;
296 1.1 jmcneill }
297 1.1 jmcneill
298 1.1 jmcneill sc->sc_rst = fdtbus_reset_get_index(phandle, 0);
299 1.1 jmcneill if (sc->sc_rst == NULL) {
300 1.1 jmcneill aprint_error(": couldn't get reset\n");
301 1.1 jmcneill return;
302 1.1 jmcneill }
303 1.1 jmcneill
304 1.1 jmcneill sc->sc_pwrseq = fdtbus_mmc_pwrseq_get(phandle);
305 1.1 jmcneill
306 1.1 jmcneill if (clk_enable(sc->sc_clk_core) != 0) {
307 1.1 jmcneill aprint_error(": couldn't enable core clock\n");
308 1.1 jmcneill return;
309 1.1 jmcneill }
310 1.1 jmcneill if (clk_enable(sc->sc_clk_clkin[0]) != 0 ||
311 1.1 jmcneill clk_enable(sc->sc_clk_clkin[1]) != 0) {
312 1.1 jmcneill aprint_error(": couldn't enable clkin clocks\n");
313 1.1 jmcneill return;
314 1.1 jmcneill }
315 1.1 jmcneill
316 1.1 jmcneill if (fdtbus_reset_deassert(sc->sc_rst) != 0) {
317 1.1 jmcneill aprint_error(": couldn't de-assert reset\n");
318 1.1 jmcneill return;
319 1.1 jmcneill }
320 1.1 jmcneill
321 1.1 jmcneill sc->sc_dev = self;
322 1.1 jmcneill sc->sc_phandle = phandle;
323 1.1 jmcneill sc->sc_bst = faa->faa_bst;
324 1.1 jmcneill sc->sc_dmat = faa->faa_dmat;
325 1.1 jmcneill mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_BIO);
326 1.1 jmcneill cv_init(&sc->sc_intr_cv, "gxmmcirq");
327 1.1 jmcneill
328 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
329 1.1 jmcneill aprint_error(": couldn't map registers\n");
330 1.1 jmcneill return;
331 1.1 jmcneill }
332 1.1 jmcneill
333 1.1 jmcneill aprint_naive("\n");
334 1.1 jmcneill aprint_normal(": eMMC/SD/SDIO controller\n");
335 1.1 jmcneill
336 1.1 jmcneill sc->sc_reg_vmmc = fdtbus_regulator_acquire(phandle, "vmmc-supply");
337 1.1 jmcneill sc->sc_reg_vqmmc = fdtbus_regulator_acquire(phandle, "vqmmc-supply");
338 1.1 jmcneill
339 1.1 jmcneill sc->sc_gpio_cd = fdtbus_gpio_acquire(phandle, "cd-gpios",
340 1.1 jmcneill GPIO_PIN_INPUT);
341 1.1 jmcneill sc->sc_gpio_wp = fdtbus_gpio_acquire(phandle, "wp-gpios",
342 1.1 jmcneill GPIO_PIN_INPUT);
343 1.1 jmcneill
344 1.1 jmcneill sc->sc_gpio_cd_inverted = of_hasprop(phandle, "cd-inverted") ? 1 : 0;
345 1.1 jmcneill sc->sc_gpio_wp_inverted = of_hasprop(phandle, "wp-inverted") ? 1 : 0;
346 1.1 jmcneill
347 1.1 jmcneill sc->sc_non_removable = of_hasprop(phandle, "non-removable");
348 1.1 jmcneill sc->sc_broken_cd = of_hasprop(phandle, "broken-cd");
349 1.1 jmcneill
350 1.1 jmcneill if (of_getprop_uint32(phandle, "max-frequency", &sc->sc_max_frequency))
351 1.1 jmcneill sc->sc_max_frequency = 52000000;
352 1.1 jmcneill
353 1.1 jmcneill if (mesongx_mmc_dma_setup(sc) != 0 ||
354 1.1 jmcneill mesongx_mmc_dmabounce_setup(sc) != 0) {
355 1.1 jmcneill aprint_error_dev(self, "failed to setup DMA\n");
356 1.1 jmcneill return;
357 1.1 jmcneill }
358 1.1 jmcneill
359 1.1 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
360 1.1 jmcneill aprint_error_dev(self, "failed to decode interrupt\n");
361 1.1 jmcneill return;
362 1.1 jmcneill }
363 1.1 jmcneill
364 1.1 jmcneill sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_BIO, FDT_INTR_MPSAFE,
365 1.1 jmcneill mesongx_mmc_intr, sc);
366 1.1 jmcneill if (sc->sc_ih == NULL) {
367 1.1 jmcneill aprint_error_dev(self, "failed to establish interrupt on %s\n",
368 1.1 jmcneill intrstr);
369 1.1 jmcneill return;
370 1.1 jmcneill }
371 1.1 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
372 1.1 jmcneill
373 1.1 jmcneill config_interrupts(self, mesongx_mmc_attach_i);
374 1.1 jmcneill }
375 1.1 jmcneill
376 1.1 jmcneill static int
377 1.1 jmcneill mesongx_mmc_dma_setup(struct mesongx_mmc_softc *sc)
378 1.1 jmcneill {
379 1.1 jmcneill int error;
380 1.1 jmcneill
381 1.1 jmcneill sc->sc_desc_ndesc = MESONGX_MMC_NDESC;
382 1.1 jmcneill sc->sc_desc_size = sizeof(struct mesongx_mmc_desc) *
383 1.1 jmcneill sc->sc_desc_ndesc;
384 1.1 jmcneill error = bus_dmamem_alloc(sc->sc_dmat, sc->sc_desc_size,
385 1.1 jmcneill sizeof(struct mesongx_mmc_desc),
386 1.1 jmcneill sc->sc_desc_size, sc->sc_desc_segs, 1,
387 1.1 jmcneill &sc->sc_desc_nsegs, BUS_DMA_WAITOK);
388 1.1 jmcneill if (error)
389 1.1 jmcneill return error;
390 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, sc->sc_desc_segs,
391 1.1 jmcneill sc->sc_desc_nsegs, sc->sc_desc_size,
392 1.1 jmcneill &sc->sc_desc_desc, BUS_DMA_WAITOK);
393 1.1 jmcneill if (error)
394 1.1 jmcneill goto free;
395 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, sc->sc_desc_size, 1,
396 1.1 jmcneill sc->sc_desc_size, 0, BUS_DMA_WAITOK, &sc->sc_desc_map);
397 1.1 jmcneill if (error)
398 1.1 jmcneill goto unmap;
399 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, sc->sc_desc_map,
400 1.1 jmcneill sc->sc_desc_desc, sc->sc_desc_size, NULL, BUS_DMA_WAITOK);
401 1.1 jmcneill if (error)
402 1.1 jmcneill goto destroy;
403 1.1 jmcneill return 0;
404 1.1 jmcneill
405 1.1 jmcneill destroy:
406 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, sc->sc_desc_map);
407 1.1 jmcneill unmap:
408 1.1 jmcneill bus_dmamem_unmap(sc->sc_dmat, sc->sc_desc_desc, sc->sc_desc_size);
409 1.1 jmcneill free:
410 1.1 jmcneill bus_dmamem_free(sc->sc_dmat, sc->sc_desc_segs, sc->sc_desc_nsegs);
411 1.1 jmcneill return error;
412 1.1 jmcneill }
413 1.1 jmcneill
414 1.1 jmcneill static int
415 1.1 jmcneill mesongx_mmc_dmabounce_setup(struct mesongx_mmc_softc *sc)
416 1.1 jmcneill {
417 1.1 jmcneill bus_dma_segment_t ds[1];
418 1.1 jmcneill int error, rseg;
419 1.1 jmcneill
420 1.1 jmcneill sc->sc_dmabounce_buflen = MAXPHYS;
421 1.1 jmcneill error = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dmabounce_buflen, 0,
422 1.1 jmcneill sc->sc_dmabounce_buflen, ds, 1, &rseg, BUS_DMA_WAITOK);
423 1.1 jmcneill if (error)
424 1.1 jmcneill return error;
425 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, ds, 1, sc->sc_dmabounce_buflen,
426 1.1 jmcneill &sc->sc_dmabounce_buf, BUS_DMA_WAITOK);
427 1.1 jmcneill if (error)
428 1.1 jmcneill goto free;
429 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, sc->sc_dmabounce_buflen, 1,
430 1.1 jmcneill sc->sc_dmabounce_buflen, 0, BUS_DMA_WAITOK, &sc->sc_dmabounce_map);
431 1.1 jmcneill if (error)
432 1.1 jmcneill goto unmap;
433 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmabounce_map,
434 1.1 jmcneill sc->sc_dmabounce_buf, sc->sc_dmabounce_buflen, NULL,
435 1.1 jmcneill BUS_DMA_WAITOK);
436 1.1 jmcneill if (error)
437 1.1 jmcneill goto destroy;
438 1.1 jmcneill return 0;
439 1.1 jmcneill
440 1.1 jmcneill destroy:
441 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmabounce_map);
442 1.1 jmcneill unmap:
443 1.1 jmcneill bus_dmamem_unmap(sc->sc_dmat, sc->sc_dmabounce_buf,
444 1.1 jmcneill sc->sc_dmabounce_buflen);
445 1.1 jmcneill free:
446 1.1 jmcneill bus_dmamem_free(sc->sc_dmat, ds, rseg);
447 1.1 jmcneill return error;
448 1.1 jmcneill }
449 1.1 jmcneill
450 1.1 jmcneill static int
451 1.1 jmcneill mesongx_mmc_set_clock(struct mesongx_mmc_softc *sc, u_int freq, bool ddr)
452 1.1 jmcneill {
453 1.1 jmcneill int best_diff, best_sel, best_div, sel, div;
454 1.1 jmcneill uint32_t val;
455 1.1 jmcneill
456 1.1 jmcneill if (freq == 0)
457 1.1 jmcneill freq = SDMMC_SDCLK_400K;
458 1.1 jmcneill
459 1.1 jmcneill best_diff = INT_MAX;
460 1.1 jmcneill best_sel = 0;
461 1.1 jmcneill best_div = 0;
462 1.1 jmcneill
463 1.1 jmcneill const u_int target_rate = (freq * 1000) >> ddr;
464 1.1 jmcneill for (sel = 0; sel <= 1; sel++) {
465 1.1 jmcneill const u_int parent_rate = clk_get_rate(sc->sc_clk_clkin[sel]);
466 1.1 jmcneill for (div = 1; div <= 63; div++) {
467 1.1 jmcneill const u_int rate = parent_rate / div;
468 1.1 jmcneill if (rate > target_rate)
469 1.1 jmcneill continue;
470 1.1 jmcneill const int diff = target_rate - rate;
471 1.1 jmcneill if (diff < best_diff) {
472 1.1 jmcneill best_diff = diff;
473 1.1 jmcneill best_sel = sel;
474 1.1 jmcneill best_div = div;
475 1.1 jmcneill }
476 1.1 jmcneill }
477 1.1 jmcneill }
478 1.1 jmcneill
479 1.1 jmcneill if (best_diff == INT_MAX)
480 1.1 jmcneill return ERANGE;
481 1.1 jmcneill
482 1.1 jmcneill val = MMC_READ(sc, SD_EMMC_CLOCK);
483 1.1 jmcneill val |= CLOCK_CFG_ALWAYS_ON;
484 1.1 jmcneill val &= ~CLOCK_CFG_RX_PHASE;
485 1.1 jmcneill val |= __SHIFTIN(0, CLOCK_CFG_RX_PHASE);
486 1.1 jmcneill val &= ~CLOCK_CFG_TX_PHASE;
487 1.1 jmcneill val |= __SHIFTIN(2, CLOCK_CFG_TX_PHASE);
488 1.1 jmcneill val &= ~CLOCK_CFG_CO_PHASE;
489 1.1 jmcneill val |= __SHIFTIN(3, CLOCK_CFG_CO_PHASE);
490 1.1 jmcneill val &= ~CLOCK_CFG_SRC;
491 1.1 jmcneill val |= __SHIFTIN(best_sel, CLOCK_CFG_SRC);
492 1.1 jmcneill val &= ~CLOCK_CFG_DIV;
493 1.1 jmcneill val |= __SHIFTIN(best_div, CLOCK_CFG_DIV);
494 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_CLOCK, val);
495 1.1 jmcneill
496 1.1 jmcneill return 0;
497 1.1 jmcneill }
498 1.1 jmcneill
499 1.1 jmcneill static void
500 1.1 jmcneill mesongx_mmc_attach_i(device_t self)
501 1.1 jmcneill {
502 1.1 jmcneill struct mesongx_mmc_softc * const sc = device_private(self);
503 1.1 jmcneill struct sdmmcbus_attach_args saa;
504 1.1 jmcneill uint32_t width;
505 1.1 jmcneill
506 1.1 jmcneill if (sc->sc_pwrseq)
507 1.1 jmcneill fdtbus_mmc_pwrseq_pre_power_on(sc->sc_pwrseq);
508 1.1 jmcneill
509 1.1 jmcneill mesongx_mmc_bus_clock(sc, SDMMC_SDCLK_400K, false);
510 1.1 jmcneill mesongx_mmc_host_reset(sc);
511 1.1 jmcneill mesongx_mmc_bus_width(sc, 1);
512 1.1 jmcneill
513 1.1 jmcneill if (sc->sc_pwrseq)
514 1.1 jmcneill fdtbus_mmc_pwrseq_post_power_on(sc->sc_pwrseq);
515 1.1 jmcneill
516 1.1 jmcneill if (of_getprop_uint32(sc->sc_phandle, "bus-width", &width) != 0)
517 1.1 jmcneill width = 4;
518 1.1 jmcneill
519 1.1 jmcneill memset(&saa, 0, sizeof(saa));
520 1.1 jmcneill saa.saa_busname = "sdmmc";
521 1.1 jmcneill saa.saa_sct = &mesongx_mmc_chip_functions;
522 1.1 jmcneill saa.saa_sch = sc;
523 1.1 jmcneill saa.saa_dmat = sc->sc_dmat;
524 1.1 jmcneill saa.saa_clkmin = SDMMC_SDCLK_400K;
525 1.1 jmcneill saa.saa_clkmax = sc->sc_max_frequency / 1000;
526 1.1 jmcneill saa.saa_caps = SMC_CAPS_DMA |
527 1.1 jmcneill SMC_CAPS_MULTI_SEG_DMA;
528 1.1 jmcneill
529 1.1 jmcneill sc->sc_host_ocr = MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V;
530 1.1 jmcneill
531 1.1 jmcneill if (of_getprop_bool(sc->sc_phandle, "cap-sd-highspeed")) {
532 1.1 jmcneill saa.saa_caps |= SMC_CAPS_SD_HIGHSPEED;
533 1.1 jmcneill sc->sc_host_ocr |= MMC_OCR_HCS;
534 1.1 jmcneill }
535 1.1 jmcneill if (of_getprop_bool(sc->sc_phandle, "cap-mmc-highspeed"))
536 1.1 jmcneill saa.saa_caps |= SMC_CAPS_MMC_HIGHSPEED;
537 1.1 jmcneill
538 1.1 jmcneill if (sc->sc_reg_vqmmc != NULL) {
539 1.1 jmcneill if (of_getprop_bool(sc->sc_phandle, "mmc-ddr-1_8v")) {
540 1.1 jmcneill saa.saa_caps |= SMC_CAPS_MMC_DDR52;
541 1.1 jmcneill sc->sc_host_ocr |= MMC_OCR_S18A;
542 1.1 jmcneill }
543 1.1 jmcneill if (of_getprop_bool(sc->sc_phandle, "mmc-hs200-1_8v")) {
544 1.1 jmcneill saa.saa_caps |= SMC_CAPS_MMC_HS200;
545 1.1 jmcneill sc->sc_host_ocr |= MMC_OCR_S18A;
546 1.1 jmcneill }
547 1.1 jmcneill }
548 1.1 jmcneill
549 1.1 jmcneill if (width == 4)
550 1.1 jmcneill saa.saa_caps |= SMC_CAPS_4BIT_MODE;
551 1.1 jmcneill if (width == 8)
552 1.1 jmcneill saa.saa_caps |= SMC_CAPS_8BIT_MODE;
553 1.1 jmcneill
554 1.1 jmcneill if (sc->sc_gpio_cd)
555 1.1 jmcneill saa.saa_caps |= SMC_CAPS_POLL_CARD_DET;
556 1.1 jmcneill
557 1.1 jmcneill sc->sc_sdmmc_dev = config_found(self, &saa, NULL);
558 1.1 jmcneill }
559 1.1 jmcneill
560 1.1 jmcneill static int
561 1.1 jmcneill mesongx_mmc_intr(void *priv)
562 1.1 jmcneill {
563 1.1 jmcneill struct mesongx_mmc_softc * const sc = priv;
564 1.1 jmcneill struct sdmmc_command *cmd;
565 1.1 jmcneill int rv = 0;
566 1.1 jmcneill
567 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
568 1.1 jmcneill
569 1.1 jmcneill const uint32_t irq_en = MMC_READ(sc, SD_EMMC_IRQ_EN);
570 1.1 jmcneill const uint32_t status = MMC_READ(sc, SD_EMMC_STATUS) & irq_en;
571 1.1 jmcneill
572 1.1 jmcneill if ((status & STATUS_IRQ_SDIO) != 0) {
573 1.1 jmcneill rv = 1;
574 1.1 jmcneill sdmmc_card_intr(sc->sc_sdmmc_dev);
575 1.1 jmcneill }
576 1.1 jmcneill
577 1.1 jmcneill cmd = sc->sc_cmd;
578 1.1 jmcneill if (cmd == NULL) {
579 1.1 jmcneill device_printf(sc->sc_dev, "WARNING: IRQ with no active command, status %#x\n", status);
580 1.1 jmcneill goto done;
581 1.1 jmcneill }
582 1.1 jmcneill
583 1.1 jmcneill if ((status & STATUS_TIMEOUT) != 0) {
584 1.1 jmcneill rv = 1;
585 1.1 jmcneill cmd->c_error = ETIMEDOUT;
586 1.1 jmcneill goto done;
587 1.1 jmcneill }
588 1.1 jmcneill
589 1.1 jmcneill if ((status & STATUS_ERROR) != 0) {
590 1.1 jmcneill rv = 1;
591 1.1 jmcneill cmd->c_error = EIO;
592 1.1 jmcneill goto done;
593 1.1 jmcneill }
594 1.1 jmcneill
595 1.1 jmcneill if ((status & STATUS_END_OF_CHAIN) != 0 && (cmd->c_flags & SCF_ITSDONE) == 0) {
596 1.1 jmcneill rv = 1;
597 1.1 jmcneill if ((cmd->c_flags & SCF_RSP_PRESENT) != 0) {
598 1.1 jmcneill if (cmd->c_flags & SCF_RSP_136) {
599 1.1 jmcneill cmd->c_resp[0] = MMC_READ(sc, SD_EMMC_CMD_RSP);
600 1.1 jmcneill cmd->c_resp[1] = MMC_READ(sc, SD_EMMC_CMD_RSP1);
601 1.1 jmcneill cmd->c_resp[2] = MMC_READ(sc, SD_EMMC_CMD_RSP2);
602 1.1 jmcneill cmd->c_resp[3] = MMC_READ(sc, SD_EMMC_CMD_RSP3);
603 1.1 jmcneill if (cmd->c_flags & SCF_RSP_CRC) {
604 1.1 jmcneill cmd->c_resp[0] = (cmd->c_resp[0] >> 8) |
605 1.1 jmcneill (cmd->c_resp[1] << 24);
606 1.1 jmcneill cmd->c_resp[1] = (cmd->c_resp[1] >> 8) |
607 1.1 jmcneill (cmd->c_resp[2] << 24);
608 1.1 jmcneill cmd->c_resp[2] = (cmd->c_resp[2] >> 8) |
609 1.1 jmcneill (cmd->c_resp[3] << 24);
610 1.1 jmcneill cmd->c_resp[3] = (cmd->c_resp[3] >> 8);
611 1.1 jmcneill }
612 1.1 jmcneill } else {
613 1.1 jmcneill cmd->c_resp[0] = MMC_READ(sc, SD_EMMC_CMD_RSP);
614 1.1 jmcneill }
615 1.1 jmcneill }
616 1.1 jmcneill cmd->c_flags |= SCF_ITSDONE;
617 1.1 jmcneill cmd->c_error = 0;
618 1.1 jmcneill goto done;
619 1.1 jmcneill }
620 1.1 jmcneill
621 1.1 jmcneill done:
622 1.1 jmcneill if (rv) {
623 1.1 jmcneill cv_broadcast(&sc->sc_intr_cv);
624 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_STATUS, irq_en);
625 1.1 jmcneill }
626 1.1 jmcneill
627 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
628 1.1 jmcneill
629 1.1 jmcneill return rv;
630 1.1 jmcneill }
631 1.1 jmcneill
632 1.1 jmcneill static int
633 1.1 jmcneill mesongx_mmc_host_reset(sdmmc_chipset_handle_t sch)
634 1.1 jmcneill {
635 1.1 jmcneill struct mesongx_mmc_softc * const sc = sch;
636 1.1 jmcneill uint32_t val;
637 1.1 jmcneill
638 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_START, 0);
639 1.1 jmcneill
640 1.1 jmcneill val = MMC_READ(sc, SD_EMMC_CFG);
641 1.1 jmcneill val &= ~CFG_RC_CC;
642 1.1 jmcneill val |= __SHIFTIN(ilog2(16), CFG_RC_CC);
643 1.1 jmcneill val |= CFG_SDCLK_ALWAYS_ON;
644 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_CFG, val);
645 1.1 jmcneill
646 1.1 jmcneill return 0;
647 1.1 jmcneill }
648 1.1 jmcneill
649 1.1 jmcneill static uint32_t
650 1.1 jmcneill mesongx_mmc_host_ocr(sdmmc_chipset_handle_t sch)
651 1.1 jmcneill {
652 1.1 jmcneill struct mesongx_mmc_softc * const sc = sch;
653 1.1 jmcneill
654 1.1 jmcneill return sc->sc_host_ocr;
655 1.1 jmcneill }
656 1.1 jmcneill
657 1.1 jmcneill static int
658 1.1 jmcneill mesongx_mmc_host_maxblklen(sdmmc_chipset_handle_t sch)
659 1.1 jmcneill {
660 1.1 jmcneill return 512;
661 1.1 jmcneill }
662 1.1 jmcneill
663 1.1 jmcneill static int
664 1.1 jmcneill mesongx_mmc_card_detect(sdmmc_chipset_handle_t sch)
665 1.1 jmcneill {
666 1.1 jmcneill struct mesongx_mmc_softc * const sc = sch;
667 1.1 jmcneill int val;
668 1.1 jmcneill
669 1.1 jmcneill if (sc->sc_non_removable || sc->sc_broken_cd) {
670 1.1 jmcneill /*
671 1.1 jmcneill * Non-removable or broken card detect flag set in
672 1.1 jmcneill * DT, assume always present
673 1.1 jmcneill */
674 1.1 jmcneill return 1;
675 1.1 jmcneill } else if (sc->sc_gpio_cd != NULL) {
676 1.1 jmcneill val = fdtbus_gpio_read(sc->sc_gpio_cd);
677 1.1 jmcneill if (sc->sc_gpio_cd_inverted)
678 1.1 jmcneill val = !val;
679 1.1 jmcneill return val;
680 1.1 jmcneill } else {
681 1.1 jmcneill return 1;
682 1.1 jmcneill }
683 1.1 jmcneill }
684 1.1 jmcneill
685 1.1 jmcneill static int
686 1.1 jmcneill mesongx_mmc_write_protect(sdmmc_chipset_handle_t sch)
687 1.1 jmcneill {
688 1.1 jmcneill struct mesongx_mmc_softc * const sc = sch;
689 1.1 jmcneill int val;
690 1.1 jmcneill
691 1.1 jmcneill if (sc->sc_gpio_wp != NULL) {
692 1.1 jmcneill val = fdtbus_gpio_read(sc->sc_gpio_wp);
693 1.1 jmcneill if (sc->sc_gpio_wp_inverted)
694 1.1 jmcneill val = !val;
695 1.1 jmcneill return val;
696 1.1 jmcneill }
697 1.1 jmcneill
698 1.1 jmcneill return 0;
699 1.1 jmcneill }
700 1.1 jmcneill
701 1.1 jmcneill static int
702 1.1 jmcneill mesongx_mmc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
703 1.1 jmcneill {
704 1.1 jmcneill return 0;
705 1.1 jmcneill }
706 1.1 jmcneill
707 1.1 jmcneill static int
708 1.1 jmcneill mesongx_mmc_bus_clock(sdmmc_chipset_handle_t sch, int freq, bool ddr)
709 1.1 jmcneill {
710 1.1 jmcneill struct mesongx_mmc_softc * const sc = sch;
711 1.1 jmcneill uint32_t val;
712 1.1 jmcneill int error;
713 1.1 jmcneill
714 1.1 jmcneill error = mesongx_mmc_set_clock(sc, freq, ddr);
715 1.1 jmcneill if (error != 0)
716 1.1 jmcneill return error;
717 1.1 jmcneill
718 1.1 jmcneill val = MMC_READ(sc, SD_EMMC_CFG);
719 1.1 jmcneill if (ddr)
720 1.1 jmcneill val |= CFG_DDR;
721 1.1 jmcneill else
722 1.1 jmcneill val &= ~CFG_DDR;
723 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_CFG, val);
724 1.1 jmcneill
725 1.1 jmcneill return 0;
726 1.1 jmcneill }
727 1.1 jmcneill
728 1.1 jmcneill static int
729 1.1 jmcneill mesongx_mmc_bus_width(sdmmc_chipset_handle_t sch, int width)
730 1.1 jmcneill {
731 1.1 jmcneill struct mesongx_mmc_softc *sc = sch;
732 1.1 jmcneill uint32_t val;
733 1.1 jmcneill
734 1.1 jmcneill val = MMC_READ(sc, SD_EMMC_CFG);
735 1.1 jmcneill val &= ~CFG_BUS_WIDTH;
736 1.1 jmcneill
737 1.1 jmcneill switch (width) {
738 1.1 jmcneill case 1:
739 1.1 jmcneill val |= __SHIFTIN(CFG_BUS_WIDTH_1, CFG_BUS_WIDTH);
740 1.1 jmcneill break;
741 1.1 jmcneill case 4:
742 1.1 jmcneill val |= __SHIFTIN(CFG_BUS_WIDTH_4, CFG_BUS_WIDTH);
743 1.1 jmcneill break;
744 1.1 jmcneill case 8:
745 1.1 jmcneill val |= __SHIFTIN(CFG_BUS_WIDTH_8, CFG_BUS_WIDTH);
746 1.1 jmcneill break;
747 1.1 jmcneill default:
748 1.1 jmcneill return EINVAL;
749 1.1 jmcneill }
750 1.1 jmcneill
751 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_CFG, val);
752 1.1 jmcneill
753 1.1 jmcneill return 0;
754 1.1 jmcneill }
755 1.1 jmcneill
756 1.1 jmcneill static int
757 1.1 jmcneill mesongx_mmc_bus_rod(sdmmc_chipset_handle_t sch, int on)
758 1.1 jmcneill {
759 1.1 jmcneill return -1;
760 1.1 jmcneill }
761 1.1 jmcneill
762 1.1 jmcneill static int
763 1.1 jmcneill mesongx_mmc_signal_voltage(sdmmc_chipset_handle_t sch, int signal_voltage)
764 1.1 jmcneill {
765 1.1 jmcneill struct mesongx_mmc_softc *sc = sch;
766 1.1 jmcneill u_int uvol;
767 1.1 jmcneill int error;
768 1.1 jmcneill
769 1.1 jmcneill if (sc->sc_reg_vqmmc == NULL)
770 1.1 jmcneill return 0;
771 1.1 jmcneill
772 1.1 jmcneill switch (signal_voltage) {
773 1.1 jmcneill case SDMMC_SIGNAL_VOLTAGE_330:
774 1.1 jmcneill uvol = 3300000;
775 1.1 jmcneill break;
776 1.1 jmcneill case SDMMC_SIGNAL_VOLTAGE_180:
777 1.1 jmcneill uvol = 1800000;
778 1.1 jmcneill break;
779 1.1 jmcneill default:
780 1.1 jmcneill return EINVAL;
781 1.1 jmcneill }
782 1.1 jmcneill
783 1.1 jmcneill error = fdtbus_regulator_supports_voltage(sc->sc_reg_vqmmc, uvol, uvol);
784 1.1 jmcneill if (error != 0)
785 1.1 jmcneill return 0;
786 1.1 jmcneill
787 1.1 jmcneill error = fdtbus_regulator_set_voltage(sc->sc_reg_vqmmc, uvol, uvol);
788 1.1 jmcneill if (error != 0)
789 1.1 jmcneill return error;
790 1.1 jmcneill
791 1.1 jmcneill return fdtbus_regulator_enable(sc->sc_reg_vqmmc);
792 1.1 jmcneill }
793 1.1 jmcneill
794 1.1 jmcneill static int
795 1.1 jmcneill mesongx_mmc_execute_tuning(sdmmc_chipset_handle_t sch, int timing)
796 1.1 jmcneill {
797 1.1 jmcneill switch (timing) {
798 1.1 jmcneill case SDMMC_TIMING_MMC_HS200:
799 1.1 jmcneill break;
800 1.1 jmcneill default:
801 1.1 jmcneill return EINVAL;
802 1.1 jmcneill }
803 1.1 jmcneill
804 1.1 jmcneill return 0;
805 1.1 jmcneill }
806 1.1 jmcneill
807 1.1 jmcneill static int
808 1.1 jmcneill mesongx_mmc_dma_prepare(struct mesongx_mmc_softc *sc, struct sdmmc_command *cmd, uint32_t cmdflags)
809 1.1 jmcneill {
810 1.1 jmcneill struct mesongx_mmc_desc *dma = sc->sc_desc_desc;
811 1.1 jmcneill bus_dmamap_t map = cmd->c_dmamap;
812 1.1 jmcneill u_int xferlen, blen, resid;
813 1.1 jmcneill bus_size_t off;
814 1.1 jmcneill uint32_t flags;
815 1.1 jmcneill int desc, seg;
816 1.1 jmcneill
817 1.1 jmcneill if (cmd->c_blklen > 512) {
818 1.1 jmcneill device_printf(sc->sc_dev, "block length %d not supported\n", cmd->c_blklen);
819 1.1 jmcneill return EINVAL;
820 1.1 jmcneill }
821 1.1 jmcneill
822 1.1 jmcneill for (seg = 0; seg < map->dm_nsegs; seg++) {
823 1.1 jmcneill if (map->dm_segs[seg].ds_len % cmd->c_blklen != 0) {
824 1.1 jmcneill /* Force DMA bounce for unaligned transfers */
825 1.1 jmcneill map = NULL;
826 1.1 jmcneill break;
827 1.1 jmcneill }
828 1.1 jmcneill }
829 1.1 jmcneill
830 1.1 jmcneill if (map == NULL) {
831 1.1 jmcneill map = sc->sc_dmabounce_map;
832 1.1 jmcneill cmd->c_flags |= SCF_NEED_BOUNCE;
833 1.1 jmcneill
834 1.1 jmcneill if ((cmd->c_flags & SCF_CMD_READ) != 0) {
835 1.1 jmcneill memset(sc->sc_dmabounce_buf, 0, cmd->c_datalen);
836 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
837 1.1 jmcneill 0, cmd->c_datalen, BUS_DMASYNC_PREREAD);
838 1.1 jmcneill } else {
839 1.1 jmcneill memcpy(sc->sc_dmabounce_buf, cmd->c_data, cmd->c_datalen);
840 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
841 1.1 jmcneill 0, cmd->c_datalen, BUS_DMASYNC_PREWRITE);
842 1.1 jmcneill }
843 1.1 jmcneill }
844 1.1 jmcneill
845 1.1 jmcneill desc = 0;
846 1.1 jmcneill for (seg = 0; seg < map->dm_nsegs; seg++) {
847 1.1 jmcneill bus_addr_t paddr = map->dm_segs[seg].ds_addr;
848 1.1 jmcneill bus_size_t len = map->dm_segs[seg].ds_len;
849 1.1 jmcneill resid = uimin(len, cmd->c_resid);
850 1.1 jmcneill off = 0;
851 1.1 jmcneill while (resid > 0) {
852 1.1 jmcneill if (desc == sc->sc_desc_ndesc)
853 1.1 jmcneill break;
854 1.1 jmcneill
855 1.1 jmcneill flags = cmdflags;
856 1.1 jmcneill
857 1.1 jmcneill if (resid >= cmd->c_blklen) {
858 1.1 jmcneill xferlen = (resid / cmd->c_blklen) * cmd->c_blklen;
859 1.1 jmcneill blen = xferlen / cmd->c_blklen;
860 1.1 jmcneill flags |= MESONGX_MMC_FLAGS_BLOCK_MODE;
861 1.1 jmcneill } else {
862 1.1 jmcneill blen = xferlen = resid;
863 1.1 jmcneill }
864 1.1 jmcneill KASSERT(xferlen > 0);
865 1.1 jmcneill KASSERT(blen <= 512);
866 1.1 jmcneill
867 1.1 jmcneill flags |= __SHIFTIN(blen % 512, MESONGX_MMC_FLAGS_LENGTH);
868 1.1 jmcneill if (desc > 0)
869 1.1 jmcneill flags |= MESONGX_MMC_FLAGS_NO_CMD;
870 1.1 jmcneill if (cmd->c_resid == xferlen)
871 1.1 jmcneill flags |= MESONGX_MMC_FLAGS_END_OF_CHAIN;
872 1.1 jmcneill
873 1.1 jmcneill dma[desc].flags = htole32(flags);
874 1.1 jmcneill dma[desc].arg = htole32(cmd->c_arg);
875 1.1 jmcneill dma[desc].data = htole32(paddr + off);
876 1.1 jmcneill dma[desc].resp = 0;
877 1.1 jmcneill
878 1.1 jmcneill cmd->c_resid -= xferlen;
879 1.1 jmcneill resid -= xferlen;
880 1.1 jmcneill off += xferlen;
881 1.1 jmcneill
882 1.1 jmcneill if (cmd->c_resid == 0)
883 1.1 jmcneill break;
884 1.1 jmcneill
885 1.1 jmcneill ++desc;
886 1.1 jmcneill }
887 1.1 jmcneill }
888 1.1 jmcneill if (desc == sc->sc_desc_ndesc) {
889 1.1 jmcneill device_printf(sc->sc_dev,
890 1.1 jmcneill "not enough descriptors for %d byte transfer (%d segs)!\n",
891 1.1 jmcneill cmd->c_datalen, map->dm_nsegs);
892 1.1 jmcneill return EIO;
893 1.1 jmcneill }
894 1.1 jmcneill
895 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_desc_map, 0,
896 1.1 jmcneill sc->sc_desc_size, BUS_DMASYNC_PREWRITE);
897 1.1 jmcneill
898 1.1 jmcneill return 0;
899 1.1 jmcneill }
900 1.1 jmcneill
901 1.1 jmcneill static void
902 1.1 jmcneill mesongx_mmc_dma_complete(struct mesongx_mmc_softc *sc, struct sdmmc_command *cmd)
903 1.1 jmcneill {
904 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_desc_map, 0,
905 1.1 jmcneill sc->sc_desc_size, BUS_DMASYNC_POSTWRITE);
906 1.1 jmcneill
907 1.1 jmcneill if ((cmd->c_flags & SCF_NEED_BOUNCE) != 0) {
908 1.1 jmcneill if ((cmd->c_flags & SCF_CMD_READ) != 0) {
909 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
910 1.1 jmcneill 0, cmd->c_datalen, BUS_DMASYNC_POSTREAD);
911 1.1 jmcneill memcpy(cmd->c_data, sc->sc_dmabounce_buf, cmd->c_datalen);
912 1.1 jmcneill } else {
913 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
914 1.1 jmcneill 0, cmd->c_datalen, BUS_DMASYNC_POSTWRITE);
915 1.1 jmcneill }
916 1.1 jmcneill }
917 1.1 jmcneill }
918 1.1 jmcneill
919 1.1 jmcneill static void
920 1.1 jmcneill mesongx_mmc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
921 1.1 jmcneill {
922 1.1 jmcneill struct mesongx_mmc_softc *sc = sch;
923 1.1 jmcneill uint32_t cmdflags, val;
924 1.1 jmcneill int error;
925 1.1 jmcneill
926 1.1 jmcneill const uint32_t irq_mask = IRQ_EN_RESP_STATUS |
927 1.1 jmcneill IRQ_EN_END_OF_CHAIN |
928 1.1 jmcneill IRQ_EN_DESC_TIMEOUT |
929 1.1 jmcneill IRQ_EN_RESP_TIMEOUT |
930 1.1 jmcneill IRQ_EN_RESP_ERR |
931 1.1 jmcneill IRQ_EN_DESC_ERR |
932 1.1 jmcneill IRQ_EN_TXD_ERR |
933 1.1 jmcneill IRQ_EN_RXD_ERR;
934 1.1 jmcneill
935 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
936 1.1 jmcneill
937 1.1 jmcneill while (sc->sc_cmd != NULL)
938 1.1 jmcneill cv_wait(&sc->sc_intr_cv, &sc->sc_intr_lock);
939 1.1 jmcneill sc->sc_cmd = cmd;
940 1.1 jmcneill
941 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_START, 0);
942 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_STATUS, MMC_READ(sc, SD_EMMC_STATUS));
943 1.1 jmcneill
944 1.1 jmcneill val = MMC_READ(sc, SD_EMMC_IRQ_EN);
945 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_IRQ_EN, val | irq_mask);
946 1.1 jmcneill
947 1.1 jmcneill cmdflags = MESONGX_MMC_FLAGS_OWNER;
948 1.1 jmcneill cmdflags |= __SHIFTIN(12, MESONGX_MMC_FLAGS_TIMEOUT); /* 2^12 = 4096 ms timeout */
949 1.1 jmcneill cmdflags |= __SHIFTIN(cmd->c_opcode, MESONGX_MMC_FLAGS_CMD_INDEX);
950 1.1 jmcneill
951 1.1 jmcneill if ((cmd->c_flags & SCF_RSP_PRESENT) == 0) {
952 1.1 jmcneill cmdflags |= MESONGX_MMC_FLAGS_NO_RESP;
953 1.1 jmcneill } else {
954 1.1 jmcneill cmdflags |= MESONGX_MMC_FLAGS_RESP_NUM;
955 1.1 jmcneill if ((cmd->c_flags & SCF_RSP_136) != 0)
956 1.1 jmcneill cmdflags |= MESONGX_MMC_FLAGS_RESP_128;
957 1.1 jmcneill if ((cmd->c_flags & SCF_RSP_CRC) == 0)
958 1.1 jmcneill cmdflags |= MESONGX_MMC_FLAGS_RESP_NOCRC;
959 1.1 jmcneill if (__SHIFTOUT(cmd->c_flags, SCF_RSP_MASK) == SCF_RSP_R1B)
960 1.1 jmcneill cmdflags |= MESONGX_MMC_FLAGS_R1B;
961 1.1 jmcneill }
962 1.1 jmcneill
963 1.1 jmcneill if (cmd->c_datalen > 0) {
964 1.1 jmcneill cmdflags |= MESONGX_MMC_FLAGS_DATA_IO;
965 1.1 jmcneill if ((cmd->c_flags & SCF_CMD_READ) == 0)
966 1.1 jmcneill cmdflags |= MESONGX_MMC_FLAGS_DATA_WR;
967 1.1 jmcneill
968 1.1 jmcneill val = MMC_READ(sc, SD_EMMC_CFG);
969 1.1 jmcneill val &= ~CFG_BL_LEN;
970 1.1 jmcneill val |= __SHIFTIN(ilog2(cmd->c_blklen), CFG_BL_LEN);
971 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_CFG, val);
972 1.1 jmcneill
973 1.1 jmcneill cmd->c_resid = cmd->c_datalen;
974 1.1 jmcneill cmd->c_error = mesongx_mmc_dma_prepare(sc, cmd, cmdflags);
975 1.1 jmcneill if (cmd->c_error != 0)
976 1.1 jmcneill goto done;
977 1.1 jmcneill
978 1.1 jmcneill const bus_addr_t desc_paddr = sc->sc_desc_map->dm_segs[0].ds_addr;
979 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_START, desc_paddr | START_DESC_BUSY); /* starts transfer */
980 1.1 jmcneill } else {
981 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_CMD_CFG, cmdflags | MESONGX_MMC_FLAGS_END_OF_CHAIN);
982 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_CMD_DAT, 0);
983 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_CMD_ARG, cmd->c_arg); /* starts transfer */
984 1.1 jmcneill }
985 1.1 jmcneill
986 1.1 jmcneill struct bintime timeout = { .sec = 5, .frac = 0 };
987 1.1 jmcneill const struct bintime epsilon = { .sec = 1, .frac = 0 };
988 1.1 jmcneill
989 1.1 jmcneill while ((cmd->c_flags & SCF_ITSDONE) == 0 && cmd->c_error == 0) {
990 1.1 jmcneill error = cv_timedwaitbt(&sc->sc_intr_cv, &sc->sc_intr_lock, &timeout, &epsilon);
991 1.1 jmcneill if (error != 0) {
992 1.1 jmcneill cmd->c_error = error;
993 1.1 jmcneill goto done;
994 1.1 jmcneill }
995 1.1 jmcneill }
996 1.1 jmcneill
997 1.1 jmcneill if (cmd->c_error == 0 && cmd->c_datalen > 0)
998 1.1 jmcneill mesongx_mmc_dma_complete(sc, cmd);
999 1.1 jmcneill
1000 1.1 jmcneill done:
1001 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_START, 0);
1002 1.1 jmcneill
1003 1.1 jmcneill val = MMC_READ(sc, SD_EMMC_IRQ_EN);
1004 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_IRQ_EN, val & ~irq_mask);
1005 1.1 jmcneill
1006 1.1 jmcneill sc->sc_cmd = NULL;
1007 1.1 jmcneill cv_broadcast(&sc->sc_intr_cv);
1008 1.1 jmcneill
1009 1.1 jmcneill #ifdef MESONGX_MMC_DEBUG
1010 1.1 jmcneill if (cmd->c_error != 0) {
1011 1.1 jmcneill for (u_int reg = 0x00; reg < 0x100; reg += 0x10) {
1012 1.1 jmcneill device_printf(sc->sc_dev, " %02x: %08x %08x %08x %08x\n", reg,
1013 1.1 jmcneill MMC_READ(sc, reg + 0),
1014 1.1 jmcneill MMC_READ(sc, reg + 4),
1015 1.1 jmcneill MMC_READ(sc, reg + 8),
1016 1.1 jmcneill MMC_READ(sc, reg + 12));
1017 1.1 jmcneill }
1018 1.1 jmcneill }
1019 1.1 jmcneill #endif
1020 1.1 jmcneill
1021 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
1022 1.1 jmcneill }
1023 1.1 jmcneill
1024 1.1 jmcneill static void
1025 1.1 jmcneill mesongx_mmc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
1026 1.1 jmcneill {
1027 1.1 jmcneill struct mesongx_mmc_softc * const sc = sch;
1028 1.1 jmcneill uint32_t val;
1029 1.1 jmcneill
1030 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
1031 1.1 jmcneill
1032 1.1 jmcneill val = MMC_READ(sc, SD_EMMC_IRQ_EN);
1033 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_IRQ_EN, val | IRQ_EN_IRQ_SDIO);
1034 1.1 jmcneill
1035 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
1036 1.1 jmcneill }
1037 1.1 jmcneill
1038 1.1 jmcneill static void
1039 1.1 jmcneill mesongx_mmc_card_intr_ack(sdmmc_chipset_handle_t sch)
1040 1.1 jmcneill {
1041 1.1 jmcneill struct mesongx_mmc_softc *sc = sch;
1042 1.1 jmcneill
1043 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_STATUS, STATUS_IRQ_SDIO);
1044 1.1 jmcneill }
1045