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