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