Home | History | Annotate | Line # | Download | only in fdt
dwcmmc_fdt.c revision 1.9
      1 /* $NetBSD: dwcmmc_fdt.c,v 1.9 2020/01/01 11:21:15 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015-2018 Jared McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: dwcmmc_fdt.c,v 1.9 2020/01/01 11:21:15 jmcneill Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/device.h>
     35 #include <sys/intr.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 #include <sys/mutex.h>
     39 #include <sys/condvar.h>
     40 #include <sys/gpio.h>
     41 
     42 #include <dev/ic/dwc_mmc_var.h>
     43 #include <dev/sdmmc/sdmmcchip.h>
     44 #include <dev/fdt/fdtvar.h>
     45 
     46 static int	dwcmmc_fdt_match(device_t, cfdata_t, void *);
     47 static void	dwcmmc_fdt_attach(device_t, device_t, void *);
     48 
     49 static void	dwcmmc_fdt_pre_power_on(struct dwc_mmc_softc *);
     50 static void	dwcmmc_fdt_post_power_on(struct dwc_mmc_softc *);
     51 
     52 static int	dwcmmc_fdt_card_detect(struct dwc_mmc_softc *);
     53 static int	dwcmmc_fdt_bus_clock(struct dwc_mmc_softc *, int);
     54 static int	dwcmmc_fdt_signal_voltage(struct dwc_mmc_softc *, int);
     55 
     56 struct dwcmmc_fdt_config {
     57 	u_int		ciu_div;
     58 	u_int		flags;
     59 };
     60 
     61 static const struct dwcmmc_fdt_config dwcmmc_rk3288_config = {
     62 	.ciu_div = 2,
     63 	.flags = DWC_MMC_F_USE_HOLD_REG |
     64 		 DWC_MMC_F_DMA,
     65 };
     66 
     67 static const struct of_compat_data compat_data[] = {
     68 	{ "rockchip,rk3288-dw-mshc",	(uintptr_t)&dwcmmc_rk3288_config },
     69 	{ NULL }
     70 };
     71 
     72 struct dwcmmc_fdt_softc {
     73 	struct dwc_mmc_softc	sc;
     74 	struct clk		*sc_clk_biu;
     75 	struct clk		*sc_clk_ciu;
     76 	struct fdtbus_gpio_pin	*sc_pin_cd;
     77 	const struct dwcmmc_fdt_config *sc_conf;
     78 	u_int			sc_ciu_div;
     79 	struct fdtbus_regulator	*sc_vqmmc;
     80 	struct fdtbus_mmc_pwrseq *sc_pwrseq;
     81 };
     82 
     83 CFATTACH_DECL_NEW(dwcmmc_fdt, sizeof(struct dwcmmc_fdt_softc),
     84 	dwcmmc_fdt_match, dwcmmc_fdt_attach, NULL, NULL);
     85 
     86 static int
     87 dwcmmc_fdt_match(device_t parent, cfdata_t cf, void *aux)
     88 {
     89 	struct fdt_attach_args * const faa = aux;
     90 
     91 	return of_match_compat_data(faa->faa_phandle, compat_data);
     92 }
     93 
     94 static void
     95 dwcmmc_fdt_attach(device_t parent, device_t self, void *aux)
     96 {
     97 	struct dwcmmc_fdt_softc *esc = device_private(self);
     98 	struct dwc_mmc_softc *sc = &esc->sc;
     99 	struct fdt_attach_args * const faa = aux;
    100 	const int phandle = faa->faa_phandle;
    101 	char intrstr[128];
    102 	u_int fifo_depth;
    103 	bus_addr_t addr;
    104 	bus_size_t size;
    105 	int error;
    106 
    107 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    108 		aprint_error(": couldn't get registers\n");
    109 		return;
    110 	}
    111 
    112 	if (of_getprop_uint32(phandle, "fifo-depth", &fifo_depth))
    113 		fifo_depth = 0;
    114 
    115 	fdtbus_clock_assign(phandle);
    116 
    117 	esc->sc_clk_biu = fdtbus_clock_get(phandle, "biu");
    118 	if (esc->sc_clk_biu == NULL) {
    119 		aprint_error(": couldn't get clock biu\n");
    120 		return;
    121 	}
    122 	esc->sc_clk_ciu = fdtbus_clock_get(phandle, "ciu");
    123 	if (esc->sc_clk_ciu == NULL) {
    124 		aprint_error(": couldn't get clock ciu\n");
    125 		return;
    126 	}
    127 
    128 	error = clk_enable(esc->sc_clk_biu);
    129 	if (error) {
    130 		aprint_error(": couldn't enable clock biu: %d\n", error);
    131 		return;
    132 	}
    133 	error = clk_enable(esc->sc_clk_ciu);
    134 	if (error) {
    135 		aprint_error(": couldn't enable clock ciu: %d\n", error);
    136 		return;
    137 	}
    138 
    139 	esc->sc_vqmmc = fdtbus_regulator_acquire(phandle, "vqmmc-supply");
    140 	esc->sc_pwrseq = fdtbus_mmc_pwrseq_get(phandle);
    141 
    142 	sc->sc_dev = self;
    143 	sc->sc_bst = faa->faa_bst;
    144 	sc->sc_dmat = faa->faa_dmat;
    145 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    146 	if (error) {
    147 		aprint_error(": couldn't map %#" PRIx64 ": %d\n",
    148 		    (uint64_t)addr, error);
    149 		return;
    150 	}
    151 	esc->sc_conf = (void *)of_search_compatible(phandle, compat_data)->data;
    152 
    153 	if (of_getprop_uint32(phandle, "max-frequency", &sc->sc_clock_freq) != 0)
    154 		sc->sc_clock_freq = UINT_MAX;
    155 	if (of_getprop_uint32(phandle, "bus-width", &sc->sc_bus_width) != 0)
    156 		sc->sc_bus_width = 4;
    157 
    158 	sc->sc_fifo_depth = fifo_depth;
    159 	sc->sc_flags = esc->sc_conf->flags;
    160 	sc->sc_pre_power_on = dwcmmc_fdt_pre_power_on;
    161 	sc->sc_post_power_on = dwcmmc_fdt_post_power_on;
    162 	sc->sc_bus_clock = dwcmmc_fdt_bus_clock;
    163 	sc->sc_signal_voltage = dwcmmc_fdt_signal_voltage;
    164 
    165 	esc->sc_pin_cd = fdtbus_gpio_acquire(phandle, "cd-gpios",
    166 	    GPIO_PIN_INPUT);
    167 	if (esc->sc_pin_cd)
    168 		sc->sc_card_detect = dwcmmc_fdt_card_detect;
    169 
    170 	aprint_naive("\n");
    171 	aprint_normal(": DesignWare SD/MMC\n");
    172 
    173 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    174 		aprint_error_dev(self, "failed to decode interrupt\n");
    175 		return;
    176 	}
    177 
    178 	if (dwc_mmc_init(sc) != 0)
    179 		return;
    180 
    181 	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_BIO, 0,
    182 	    dwc_mmc_intr, sc);
    183 	if (sc->sc_ih == NULL) {
    184 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    185 		    intrstr);
    186 		return;
    187 	}
    188 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    189 }
    190 
    191 static void
    192 dwcmmc_fdt_pre_power_on(struct dwc_mmc_softc *sc)
    193 {
    194 	struct dwcmmc_fdt_softc *esc = device_private(sc->sc_dev);
    195 
    196 	if (esc->sc_pwrseq != NULL)
    197 		fdtbus_mmc_pwrseq_pre_power_on(esc->sc_pwrseq);
    198 }
    199 
    200 static void
    201 dwcmmc_fdt_post_power_on(struct dwc_mmc_softc *sc)
    202 {
    203 	struct dwcmmc_fdt_softc *esc = device_private(sc->sc_dev);
    204 
    205 	if (esc->sc_pwrseq != NULL)
    206 		fdtbus_mmc_pwrseq_post_power_on(esc->sc_pwrseq);
    207 }
    208 
    209 static int
    210 dwcmmc_fdt_card_detect(struct dwc_mmc_softc *sc)
    211 {
    212 	struct dwcmmc_fdt_softc *esc = device_private(sc->sc_dev);
    213 
    214 	KASSERT(esc->sc_pin_cd != NULL);
    215 
    216 	return fdtbus_gpio_read(esc->sc_pin_cd);
    217 }
    218 
    219 static int
    220 dwcmmc_fdt_bus_clock(struct dwc_mmc_softc *sc, int rate)
    221 {
    222 	struct dwcmmc_fdt_softc *esc = device_private(sc->sc_dev);
    223         const u_int ciu_div = esc->sc_conf->ciu_div > 0 ? esc->sc_conf->ciu_div : 1;
    224 	int error;
    225 
    226 	error = clk_set_rate(esc->sc_clk_ciu, 1000 * rate * ciu_div);
    227 	if (error != 0) {
    228 		aprint_error_dev(sc->sc_dev, "failed to set rate to %u kHz: %d\n",
    229 		    rate * ciu_div, error);
    230 		return error;
    231 	}
    232 
    233 	sc->sc_clock_freq = clk_get_rate(esc->sc_clk_ciu);
    234 
    235 	aprint_debug_dev(sc->sc_dev, "set clock rate to %u kHz (target %u kHz)\n",
    236 	    sc->sc_clock_freq, rate);
    237 
    238 	return 0;
    239 }
    240 
    241 static int
    242 dwcmmc_fdt_signal_voltage(struct dwc_mmc_softc *sc, int signal_voltage)
    243 {
    244 	struct dwcmmc_fdt_softc *esc = device_private(sc->sc_dev);
    245 	u_int uvol;
    246 	int error;
    247 
    248 	if (esc->sc_vqmmc == NULL)
    249 		return 0;
    250 
    251 	switch (signal_voltage) {
    252 	case SDMMC_SIGNAL_VOLTAGE_180:
    253 		uvol = 1800000;
    254 		break;
    255 	case SDMMC_SIGNAL_VOLTAGE_330:
    256 		uvol = 3300000;
    257 		break;
    258 	default:
    259 		return EINVAL;
    260 	}
    261 
    262 	error = fdtbus_regulator_supports_voltage(esc->sc_vqmmc, uvol, uvol);
    263 	if (error != 0)
    264 		return 0;
    265 
    266 	error = fdtbus_regulator_set_voltage(esc->sc_vqmmc, uvol, uvol);
    267 	if (error != 0)
    268 		return error;
    269 
    270 	return fdtbus_regulator_enable(esc->sc_vqmmc);
    271 }
    272