Home | History | Annotate | Line # | Download | only in fdt
dwcmmc_fdt.c revision 1.17
      1  1.17  jmcneill /* $NetBSD: dwcmmc_fdt.c,v 1.17 2021/03/23 22:27:38 jmcneill Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2015-2018 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.17  jmcneill __KERNEL_RCSID(0, "$NetBSD: dwcmmc_fdt.c,v 1.17 2021/03/23 22:27:38 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/mutex.h>
     39   1.1  jmcneill #include <sys/condvar.h>
     40   1.1  jmcneill #include <sys/gpio.h>
     41   1.1  jmcneill 
     42   1.1  jmcneill #include <dev/ic/dwc_mmc_var.h>
     43   1.9  jmcneill #include <dev/sdmmc/sdmmcchip.h>
     44   1.1  jmcneill #include <dev/fdt/fdtvar.h>
     45   1.1  jmcneill 
     46   1.1  jmcneill static int	dwcmmc_fdt_match(device_t, cfdata_t, void *);
     47   1.1  jmcneill static void	dwcmmc_fdt_attach(device_t, device_t, void *);
     48   1.1  jmcneill 
     49   1.9  jmcneill static void	dwcmmc_fdt_pre_power_on(struct dwc_mmc_softc *);
     50   1.9  jmcneill static void	dwcmmc_fdt_post_power_on(struct dwc_mmc_softc *);
     51   1.9  jmcneill 
     52   1.1  jmcneill static int	dwcmmc_fdt_card_detect(struct dwc_mmc_softc *);
     53   1.2  jmcneill static int	dwcmmc_fdt_bus_clock(struct dwc_mmc_softc *, int);
     54   1.9  jmcneill static int	dwcmmc_fdt_signal_voltage(struct dwc_mmc_softc *, int);
     55   1.1  jmcneill 
     56   1.1  jmcneill struct dwcmmc_fdt_config {
     57   1.1  jmcneill 	u_int		ciu_div;
     58   1.9  jmcneill 	u_int		flags;
     59  1.11  jmcneill 	uint32_t	intr_cardmask;
     60   1.1  jmcneill };
     61   1.1  jmcneill 
     62   1.7  jmcneill static const struct dwcmmc_fdt_config dwcmmc_rk3288_config = {
     63   1.1  jmcneill 	.ciu_div = 2,
     64   1.9  jmcneill 	.flags = DWC_MMC_F_USE_HOLD_REG |
     65   1.9  jmcneill 		 DWC_MMC_F_DMA,
     66  1.11  jmcneill 	.intr_cardmask = __BIT(24),
     67   1.1  jmcneill };
     68   1.1  jmcneill 
     69  1.17  jmcneill static const struct dwmmc_fdt_config dwmmc_default_config = {
     70  1.17  jmcneill 	.flags = DWC_MMC_F_USE_HOLD_REG |
     71  1.17  jmcneill 		 DWC_MMC_F_DMA,
     72  1.17  jmcneill };
     73  1.17  jmcneill 
     74  1.13   thorpej static const struct device_compatible_entry compat_data[] = {
     75  1.13   thorpej 	{ .compat = "rockchip,rk3288-dw-mshc",	.data = &dwcmmc_rk3288_config },
     76  1.17  jmcneill 	{ .compat = "snps,dw-mshc",		.data = &dwmmc_default_config },
     77  1.15   thorpej 	DEVICE_COMPAT_EOL
     78   1.1  jmcneill };
     79   1.1  jmcneill 
     80   1.1  jmcneill struct dwcmmc_fdt_softc {
     81   1.1  jmcneill 	struct dwc_mmc_softc	sc;
     82   1.1  jmcneill 	struct clk		*sc_clk_biu;
     83   1.1  jmcneill 	struct clk		*sc_clk_ciu;
     84   1.1  jmcneill 	struct fdtbus_gpio_pin	*sc_pin_cd;
     85   1.1  jmcneill 	const struct dwcmmc_fdt_config *sc_conf;
     86   1.2  jmcneill 	u_int			sc_ciu_div;
     87   1.9  jmcneill 	struct fdtbus_regulator	*sc_vqmmc;
     88   1.9  jmcneill 	struct fdtbus_mmc_pwrseq *sc_pwrseq;
     89   1.1  jmcneill };
     90   1.1  jmcneill 
     91   1.8  jmcneill CFATTACH_DECL_NEW(dwcmmc_fdt, sizeof(struct dwcmmc_fdt_softc),
     92   1.1  jmcneill 	dwcmmc_fdt_match, dwcmmc_fdt_attach, NULL, NULL);
     93   1.1  jmcneill 
     94   1.1  jmcneill static int
     95   1.1  jmcneill dwcmmc_fdt_match(device_t parent, cfdata_t cf, void *aux)
     96   1.1  jmcneill {
     97   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
     98   1.1  jmcneill 
     99  1.16   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    100   1.1  jmcneill }
    101   1.1  jmcneill 
    102   1.1  jmcneill static void
    103   1.1  jmcneill dwcmmc_fdt_attach(device_t parent, device_t self, void *aux)
    104   1.1  jmcneill {
    105   1.1  jmcneill 	struct dwcmmc_fdt_softc *esc = device_private(self);
    106   1.1  jmcneill 	struct dwc_mmc_softc *sc = &esc->sc;
    107   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    108   1.1  jmcneill 	const int phandle = faa->faa_phandle;
    109   1.1  jmcneill 	char intrstr[128];
    110   1.3  jmcneill 	u_int fifo_depth;
    111   1.1  jmcneill 	bus_addr_t addr;
    112   1.1  jmcneill 	bus_size_t size;
    113   1.1  jmcneill 	int error;
    114   1.1  jmcneill 
    115   1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    116   1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    117   1.1  jmcneill 		return;
    118   1.1  jmcneill 	}
    119   1.1  jmcneill 
    120   1.1  jmcneill 	if (of_getprop_uint32(phandle, "fifo-depth", &fifo_depth))
    121   1.1  jmcneill 		fifo_depth = 0;
    122   1.1  jmcneill 
    123   1.7  jmcneill 	fdtbus_clock_assign(phandle);
    124   1.7  jmcneill 
    125   1.1  jmcneill 	esc->sc_clk_biu = fdtbus_clock_get(phandle, "biu");
    126   1.1  jmcneill 	if (esc->sc_clk_biu == NULL) {
    127   1.1  jmcneill 		aprint_error(": couldn't get clock biu\n");
    128   1.1  jmcneill 		return;
    129   1.1  jmcneill 	}
    130   1.1  jmcneill 	esc->sc_clk_ciu = fdtbus_clock_get(phandle, "ciu");
    131   1.1  jmcneill 	if (esc->sc_clk_ciu == NULL) {
    132   1.1  jmcneill 		aprint_error(": couldn't get clock ciu\n");
    133   1.1  jmcneill 		return;
    134   1.1  jmcneill 	}
    135   1.1  jmcneill 
    136   1.1  jmcneill 	error = clk_enable(esc->sc_clk_biu);
    137   1.1  jmcneill 	if (error) {
    138   1.1  jmcneill 		aprint_error(": couldn't enable clock biu: %d\n", error);
    139   1.1  jmcneill 		return;
    140   1.1  jmcneill 	}
    141   1.1  jmcneill 	error = clk_enable(esc->sc_clk_ciu);
    142   1.1  jmcneill 	if (error) {
    143   1.1  jmcneill 		aprint_error(": couldn't enable clock ciu: %d\n", error);
    144   1.1  jmcneill 		return;
    145   1.1  jmcneill 	}
    146   1.1  jmcneill 
    147   1.9  jmcneill 	esc->sc_vqmmc = fdtbus_regulator_acquire(phandle, "vqmmc-supply");
    148   1.9  jmcneill 	esc->sc_pwrseq = fdtbus_mmc_pwrseq_get(phandle);
    149   1.9  jmcneill 
    150   1.1  jmcneill 	sc->sc_dev = self;
    151   1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    152   1.1  jmcneill 	sc->sc_dmat = faa->faa_dmat;
    153   1.1  jmcneill 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    154   1.1  jmcneill 	if (error) {
    155   1.6  christos 		aprint_error(": couldn't map %#" PRIx64 ": %d\n",
    156   1.1  jmcneill 		    (uint64_t)addr, error);
    157   1.1  jmcneill 		return;
    158   1.1  jmcneill 	}
    159  1.16   thorpej 	esc->sc_conf = of_compatible_lookup(phandle, compat_data)->data;
    160   1.1  jmcneill 
    161   1.3  jmcneill 	if (of_getprop_uint32(phandle, "max-frequency", &sc->sc_clock_freq) != 0)
    162   1.3  jmcneill 		sc->sc_clock_freq = UINT_MAX;
    163   1.9  jmcneill 	if (of_getprop_uint32(phandle, "bus-width", &sc->sc_bus_width) != 0)
    164   1.9  jmcneill 		sc->sc_bus_width = 4;
    165   1.2  jmcneill 
    166   1.1  jmcneill 	sc->sc_fifo_depth = fifo_depth;
    167  1.17  jmcneill 	sc->sc_entr_cardmask = esc->sc_conf->intr_cardmask;
    168  1.10  jmcneill 	sc->sc_ciu_div = esc->sc_conf->ciu_div;
    169   1.9  jmcneill 	sc->sc_flags = esc->sc_conf->flags;
    170   1.9  jmcneill 	sc->sc_pre_power_on = dwcmmc_fdt_pre_power_on;
    171   1.9  jmcneill 	sc->sc_post_power_on = dwcmmc_fdt_post_power_on;
    172   1.2  jmcneill 	sc->sc_bus_clock = dwcmmc_fdt_bus_clock;
    173   1.9  jmcneill 	sc->sc_signal_voltage = dwcmmc_fdt_signal_voltage;
    174   1.1  jmcneill 
    175   1.1  jmcneill 	esc->sc_pin_cd = fdtbus_gpio_acquire(phandle, "cd-gpios",
    176   1.1  jmcneill 	    GPIO_PIN_INPUT);
    177   1.1  jmcneill 	if (esc->sc_pin_cd)
    178   1.1  jmcneill 		sc->sc_card_detect = dwcmmc_fdt_card_detect;
    179   1.1  jmcneill 
    180   1.1  jmcneill 	aprint_naive("\n");
    181   1.2  jmcneill 	aprint_normal(": DesignWare SD/MMC\n");
    182   1.1  jmcneill 
    183   1.1  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    184   1.1  jmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
    185   1.1  jmcneill 		return;
    186   1.1  jmcneill 	}
    187   1.1  jmcneill 
    188   1.1  jmcneill 	if (dwc_mmc_init(sc) != 0)
    189   1.1  jmcneill 		return;
    190   1.1  jmcneill 
    191  1.12       ryo 	sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_BIO, 0,
    192  1.12       ryo 	    dwc_mmc_intr, sc, device_xname(self));
    193   1.1  jmcneill 	if (sc->sc_ih == NULL) {
    194   1.1  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    195   1.1  jmcneill 		    intrstr);
    196   1.1  jmcneill 		return;
    197   1.1  jmcneill 	}
    198   1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    199   1.1  jmcneill }
    200   1.1  jmcneill 
    201   1.9  jmcneill static void
    202   1.9  jmcneill dwcmmc_fdt_pre_power_on(struct dwc_mmc_softc *sc)
    203   1.9  jmcneill {
    204   1.9  jmcneill 	struct dwcmmc_fdt_softc *esc = device_private(sc->sc_dev);
    205   1.9  jmcneill 
    206   1.9  jmcneill 	if (esc->sc_pwrseq != NULL)
    207   1.9  jmcneill 		fdtbus_mmc_pwrseq_pre_power_on(esc->sc_pwrseq);
    208   1.9  jmcneill }
    209   1.9  jmcneill 
    210   1.9  jmcneill static void
    211   1.9  jmcneill dwcmmc_fdt_post_power_on(struct dwc_mmc_softc *sc)
    212   1.9  jmcneill {
    213   1.9  jmcneill 	struct dwcmmc_fdt_softc *esc = device_private(sc->sc_dev);
    214   1.9  jmcneill 
    215   1.9  jmcneill 	if (esc->sc_pwrseq != NULL)
    216   1.9  jmcneill 		fdtbus_mmc_pwrseq_post_power_on(esc->sc_pwrseq);
    217   1.9  jmcneill }
    218   1.9  jmcneill 
    219   1.1  jmcneill static int
    220   1.1  jmcneill dwcmmc_fdt_card_detect(struct dwc_mmc_softc *sc)
    221   1.1  jmcneill {
    222   1.1  jmcneill 	struct dwcmmc_fdt_softc *esc = device_private(sc->sc_dev);
    223   1.1  jmcneill 
    224   1.1  jmcneill 	KASSERT(esc->sc_pin_cd != NULL);
    225   1.1  jmcneill 
    226   1.1  jmcneill 	return fdtbus_gpio_read(esc->sc_pin_cd);
    227   1.1  jmcneill }
    228   1.2  jmcneill 
    229   1.2  jmcneill static int
    230   1.2  jmcneill dwcmmc_fdt_bus_clock(struct dwc_mmc_softc *sc, int rate)
    231   1.2  jmcneill {
    232   1.2  jmcneill 	struct dwcmmc_fdt_softc *esc = device_private(sc->sc_dev);
    233  1.10  jmcneill         const u_int ciu_div = sc->sc_ciu_div > 0 ? sc->sc_ciu_div : 1;
    234   1.2  jmcneill 	int error;
    235   1.2  jmcneill 
    236   1.2  jmcneill 	error = clk_set_rate(esc->sc_clk_ciu, 1000 * rate * ciu_div);
    237   1.2  jmcneill 	if (error != 0) {
    238   1.2  jmcneill 		aprint_error_dev(sc->sc_dev, "failed to set rate to %u kHz: %d\n",
    239   1.2  jmcneill 		    rate * ciu_div, error);
    240   1.2  jmcneill 		return error;
    241   1.2  jmcneill 	}
    242   1.2  jmcneill 
    243   1.9  jmcneill 	sc->sc_clock_freq = clk_get_rate(esc->sc_clk_ciu);
    244   1.2  jmcneill 
    245   1.2  jmcneill 	aprint_debug_dev(sc->sc_dev, "set clock rate to %u kHz (target %u kHz)\n",
    246   1.2  jmcneill 	    sc->sc_clock_freq, rate);
    247   1.2  jmcneill 
    248   1.2  jmcneill 	return 0;
    249   1.2  jmcneill }
    250   1.9  jmcneill 
    251   1.9  jmcneill static int
    252   1.9  jmcneill dwcmmc_fdt_signal_voltage(struct dwc_mmc_softc *sc, int signal_voltage)
    253   1.9  jmcneill {
    254   1.9  jmcneill 	struct dwcmmc_fdt_softc *esc = device_private(sc->sc_dev);
    255   1.9  jmcneill 	u_int uvol;
    256   1.9  jmcneill 	int error;
    257   1.9  jmcneill 
    258   1.9  jmcneill 	if (esc->sc_vqmmc == NULL)
    259   1.9  jmcneill 		return 0;
    260   1.9  jmcneill 
    261   1.9  jmcneill 	switch (signal_voltage) {
    262   1.9  jmcneill 	case SDMMC_SIGNAL_VOLTAGE_180:
    263   1.9  jmcneill 		uvol = 1800000;
    264   1.9  jmcneill 		break;
    265   1.9  jmcneill 	case SDMMC_SIGNAL_VOLTAGE_330:
    266   1.9  jmcneill 		uvol = 3300000;
    267   1.9  jmcneill 		break;
    268   1.9  jmcneill 	default:
    269   1.9  jmcneill 		return EINVAL;
    270   1.9  jmcneill 	}
    271   1.9  jmcneill 
    272   1.9  jmcneill 	error = fdtbus_regulator_supports_voltage(esc->sc_vqmmc, uvol, uvol);
    273   1.9  jmcneill 	if (error != 0)
    274   1.9  jmcneill 		return 0;
    275   1.9  jmcneill 
    276   1.9  jmcneill 	error = fdtbus_regulator_set_voltage(esc->sc_vqmmc, uvol, uvol);
    277   1.9  jmcneill 	if (error != 0)
    278   1.9  jmcneill 		return error;
    279   1.9  jmcneill 
    280   1.9  jmcneill 	return fdtbus_regulator_enable(esc->sc_vqmmc);
    281   1.9  jmcneill }
    282