Home | History | Annotate | Line # | Download | only in fdt
arasan_sdhc_fdt.c revision 1.3
      1  1.3  jmcneill /* $NetBSD: arasan_sdhc_fdt.c,v 1.3 2019/07/03 23:10:43 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.3  jmcneill __KERNEL_RCSID(0, "$NetBSD: arasan_sdhc_fdt.c,v 1.3 2019/07/03 23:10:43 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/systm.h>
     36  1.1  jmcneill #include <sys/sysctl.h>
     37  1.1  jmcneill #include <sys/kmem.h>
     38  1.1  jmcneill 
     39  1.1  jmcneill #include <dev/sdmmc/sdhcreg.h>
     40  1.1  jmcneill #include <dev/sdmmc/sdhcvar.h>
     41  1.1  jmcneill #include <dev/sdmmc/sdmmcvar.h>
     42  1.1  jmcneill 
     43  1.1  jmcneill #include <dev/clk/clk_backend.h>
     44  1.1  jmcneill 
     45  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     46  1.1  jmcneill #include <dev/fdt/syscon.h>
     47  1.1  jmcneill 
     48  1.1  jmcneill #define	RK3399_GRF_EMMCCORE_CON0	0xf000
     49  1.1  jmcneill #define	 RK3399_CORECFG_BASECLKFREQ		__BITS(15,8)
     50  1.1  jmcneill #define	 RK3399_CORECFG_TIMEOUTCLKUNIT		__BIT(7)
     51  1.1  jmcneill #define	 RK3399_CORECFG_TUNINGCOUNT		__BITS(5,0)
     52  1.1  jmcneill #define	RK3399_GRF_EMMCCORE_CON11	0xf02c
     53  1.1  jmcneill #define	 RK3399_CORECFG_CLOCKMULTIPLIER		__BITS(7,0)
     54  1.1  jmcneill 
     55  1.1  jmcneill enum arasan_sdhc_type {
     56  1.1  jmcneill 	AS_TYPE_RK3399 = 1,
     57  1.1  jmcneill };
     58  1.1  jmcneill 
     59  1.1  jmcneill struct arasan_sdhc_softc {
     60  1.1  jmcneill 	struct sdhc_softc	sc_base;
     61  1.1  jmcneill 	struct sdhc_host	*sc_host[1];
     62  1.1  jmcneill 	bus_space_tag_t		sc_bst;
     63  1.1  jmcneill 	bus_space_handle_t	sc_bsh;
     64  1.1  jmcneill 	bus_size_t		sc_bsz;
     65  1.1  jmcneill 	int			sc_phandle;
     66  1.1  jmcneill 	struct fdtbus_phy	*sc_phy;
     67  1.1  jmcneill 	struct syscon		*sc_syscon;
     68  1.1  jmcneill 	struct clk		*sc_clk_xin;
     69  1.1  jmcneill 	struct clk		*sc_clk_ahb;
     70  1.1  jmcneill 	enum arasan_sdhc_type	sc_type;
     71  1.1  jmcneill 	struct clk_domain	sc_clkdom;
     72  1.1  jmcneill 	struct clk		sc_clk_card;
     73  1.1  jmcneill };
     74  1.1  jmcneill 
     75  1.1  jmcneill static const struct of_compat_data compat_data[] = {
     76  1.1  jmcneill 	{ "rockchip,rk3399-sdhci-5.1",		AS_TYPE_RK3399 },
     77  1.1  jmcneill 	{ NULL }
     78  1.1  jmcneill };
     79  1.1  jmcneill 
     80  1.1  jmcneill static struct clk *
     81  1.1  jmcneill arasan_sdhc_clk_decode(device_t dev, int cc_phandle, const void *data, size_t len)
     82  1.1  jmcneill {
     83  1.1  jmcneill 	struct arasan_sdhc_softc * const sc = device_private(dev);
     84  1.1  jmcneill 
     85  1.1  jmcneill 	if (len != 0)
     86  1.1  jmcneill 		return NULL;
     87  1.1  jmcneill 
     88  1.1  jmcneill 	return &sc->sc_clk_card;
     89  1.1  jmcneill }
     90  1.1  jmcneill 
     91  1.1  jmcneill static const struct fdtbus_clock_controller_func arasan_sdhc_fdt_clk_funcs = {
     92  1.1  jmcneill 	.decode = arasan_sdhc_clk_decode,
     93  1.1  jmcneill };
     94  1.1  jmcneill 
     95  1.1  jmcneill static struct clk *
     96  1.1  jmcneill arasan_sdhc_clk_get(void *priv, const char *name)
     97  1.1  jmcneill {
     98  1.1  jmcneill 	struct arasan_sdhc_softc * const sc = priv;
     99  1.1  jmcneill 
    100  1.1  jmcneill 	if (strcmp(name, sc->sc_clk_card.name) != 0)
    101  1.1  jmcneill 		return NULL;
    102  1.1  jmcneill 
    103  1.1  jmcneill 	return &sc->sc_clk_card;
    104  1.1  jmcneill }
    105  1.1  jmcneill 
    106  1.1  jmcneill static u_int
    107  1.1  jmcneill arasan_sdhc_clk_get_rate(void *priv, struct clk *clk)
    108  1.1  jmcneill {
    109  1.1  jmcneill 	struct arasan_sdhc_softc * const sc = priv;
    110  1.1  jmcneill 
    111  1.1  jmcneill 	return clk_get_rate(sc->sc_clk_xin);
    112  1.1  jmcneill }
    113  1.1  jmcneill 
    114  1.1  jmcneill static const struct clk_funcs arasan_sdhc_clk_funcs = {
    115  1.1  jmcneill 	.get = arasan_sdhc_clk_get,
    116  1.1  jmcneill 	.get_rate = arasan_sdhc_clk_get_rate,
    117  1.1  jmcneill };
    118  1.1  jmcneill 
    119  1.1  jmcneill static int
    120  1.1  jmcneill arasan_sdhc_signal_voltage(struct sdhc_softc *sdhc, int signal_voltage)
    121  1.1  jmcneill {
    122  1.1  jmcneill 	if (signal_voltage == SDMMC_SIGNAL_VOLTAGE_180)
    123  1.1  jmcneill 		return 0;
    124  1.1  jmcneill 
    125  1.1  jmcneill 	return EINVAL;
    126  1.1  jmcneill }
    127  1.1  jmcneill 
    128  1.1  jmcneill static int
    129  1.1  jmcneill arasan_sdhc_bus_clock_pre(struct sdhc_softc *sdhc, int freq)
    130  1.1  jmcneill {
    131  1.1  jmcneill 	struct arasan_sdhc_softc * const sc = device_private(sdhc->sc_dev);
    132  1.1  jmcneill 	int error;
    133  1.1  jmcneill 
    134  1.1  jmcneill 	if (sc->sc_phy != NULL) {
    135  1.1  jmcneill 		error = fdtbus_phy_enable(sc->sc_phy, false);
    136  1.1  jmcneill 		if (error != 0)
    137  1.1  jmcneill 			return error;
    138  1.1  jmcneill 	}
    139  1.1  jmcneill 
    140  1.1  jmcneill 	return 0;
    141  1.1  jmcneill }
    142  1.1  jmcneill 
    143  1.1  jmcneill static int
    144  1.1  jmcneill arasan_sdhc_bus_clock_post(struct sdhc_softc *sdhc, int freq)
    145  1.1  jmcneill {
    146  1.1  jmcneill 	struct arasan_sdhc_softc * const sc = device_private(sdhc->sc_dev);
    147  1.1  jmcneill 	int error;
    148  1.1  jmcneill 
    149  1.1  jmcneill 	if (sc->sc_phy != NULL) {
    150  1.1  jmcneill 		error = fdtbus_phy_enable(sc->sc_phy, true);
    151  1.1  jmcneill 		if (error != 0)
    152  1.1  jmcneill 			return error;
    153  1.1  jmcneill 	}
    154  1.1  jmcneill 
    155  1.1  jmcneill 	return 0;
    156  1.1  jmcneill }
    157  1.1  jmcneill 
    158  1.1  jmcneill static void
    159  1.1  jmcneill arasan_sdhc_init_rk3399(struct arasan_sdhc_softc *sc)
    160  1.1  jmcneill {
    161  1.1  jmcneill 	uint32_t mask, val;
    162  1.1  jmcneill 
    163  1.1  jmcneill 	if (sc->sc_syscon == NULL)
    164  1.1  jmcneill 		return;
    165  1.1  jmcneill 
    166  1.1  jmcneill 	syscon_lock(sc->sc_syscon);
    167  1.1  jmcneill 
    168  1.1  jmcneill 	/* Disable clock multiplier */
    169  1.1  jmcneill 	mask = RK3399_CORECFG_CLOCKMULTIPLIER;
    170  1.1  jmcneill 	val = 0;
    171  1.1  jmcneill 	syscon_write_4(sc->sc_syscon, RK3399_GRF_EMMCCORE_CON11, (mask << 16) | val);
    172  1.1  jmcneill 
    173  1.1  jmcneill 	/* Set base clock frequency */
    174  1.1  jmcneill 	const u_int xin_rate = clk_get_rate(sc->sc_clk_xin);
    175  1.1  jmcneill 	mask = RK3399_CORECFG_BASECLKFREQ;
    176  1.1  jmcneill 	val = __SHIFTIN((xin_rate + (1000000 / 2)) / 1000000, RK3399_CORECFG_BASECLKFREQ);
    177  1.1  jmcneill 	syscon_write_4(sc->sc_syscon, RK3399_GRF_EMMCCORE_CON0, (mask << 16) | val);
    178  1.1  jmcneill 
    179  1.1  jmcneill 	syscon_unlock(sc->sc_syscon);
    180  1.1  jmcneill }
    181  1.1  jmcneill 
    182  1.1  jmcneill static void
    183  1.1  jmcneill arasan_sdhc_init(device_t dev)
    184  1.1  jmcneill {
    185  1.1  jmcneill 	struct arasan_sdhc_softc * const sc = device_private(dev);
    186  1.1  jmcneill 	const char * sdhci_5_1_compat[] = { "arasan,sdhci-5.1", NULL };
    187  1.1  jmcneill 	int error;
    188  1.1  jmcneill 
    189  1.1  jmcneill 	if (sc->sc_type == AS_TYPE_RK3399)
    190  1.1  jmcneill 		arasan_sdhc_init_rk3399(sc);
    191  1.1  jmcneill 
    192  1.1  jmcneill 	if (of_match_compatible(sc->sc_phandle, sdhci_5_1_compat)) {
    193  1.1  jmcneill 		sc->sc_phy = fdtbus_phy_get(sc->sc_phandle, "phy_arasan");
    194  1.1  jmcneill 		if (sc->sc_phy == NULL) {
    195  1.1  jmcneill 			aprint_error_dev(dev, "couldn't get PHY\n");
    196  1.1  jmcneill 			return;
    197  1.1  jmcneill 		}
    198  1.1  jmcneill 		sc->sc_base.sc_vendor_signal_voltage = arasan_sdhc_signal_voltage;
    199  1.1  jmcneill 	}
    200  1.1  jmcneill 
    201  1.1  jmcneill 	error = sdhc_host_found(&sc->sc_base, sc->sc_bst, sc->sc_bsh, sc->sc_bsz);
    202  1.1  jmcneill 	if (error != 0) {
    203  1.1  jmcneill 		aprint_error_dev(dev, "couldn't initialize host, error = %d\n", error);
    204  1.1  jmcneill 		return;
    205  1.1  jmcneill 	}
    206  1.1  jmcneill }
    207  1.1  jmcneill 
    208  1.1  jmcneill static int
    209  1.1  jmcneill arasan_sdhc_match(device_t parent, cfdata_t cf, void *aux)
    210  1.1  jmcneill {
    211  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    212  1.1  jmcneill 
    213  1.1  jmcneill 	return of_match_compat_data(faa->faa_phandle, compat_data);
    214  1.1  jmcneill }
    215  1.1  jmcneill 
    216  1.1  jmcneill static void
    217  1.1  jmcneill arasan_sdhc_attach(device_t parent, device_t self, void *aux)
    218  1.1  jmcneill {
    219  1.1  jmcneill 	struct arasan_sdhc_softc * const sc = device_private(self);
    220  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    221  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    222  1.1  jmcneill 	char intrstr[128];
    223  1.1  jmcneill 	const char *clkname;
    224  1.1  jmcneill 	bus_addr_t addr;
    225  1.1  jmcneill 	bus_size_t size;
    226  1.1  jmcneill 	u_int bus_width;
    227  1.2  jmcneill 	int error;
    228  1.1  jmcneill 	void *ih;
    229  1.1  jmcneill 
    230  1.1  jmcneill 	fdtbus_clock_assign(phandle);
    231  1.1  jmcneill 
    232  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    233  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    234  1.1  jmcneill 		return;
    235  1.1  jmcneill 	}
    236  1.1  jmcneill 
    237  1.1  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    238  1.1  jmcneill 		aprint_error(": couldn't decode interrupt\n");
    239  1.1  jmcneill 		return;
    240  1.1  jmcneill 	}
    241  1.1  jmcneill 
    242  1.1  jmcneill 	sc->sc_clk_xin = fdtbus_clock_get(phandle, "clk_xin");
    243  1.1  jmcneill 	sc->sc_clk_ahb = fdtbus_clock_get(phandle, "clk_ahb");
    244  1.1  jmcneill 	if (sc->sc_clk_xin == NULL || sc->sc_clk_ahb == NULL) {
    245  1.1  jmcneill 		aprint_error(": couldn't get clocks\n");
    246  1.1  jmcneill 		return;
    247  1.1  jmcneill 	}
    248  1.1  jmcneill 	if (clk_enable(sc->sc_clk_xin) != 0 || clk_enable(sc->sc_clk_ahb) != 0) {
    249  1.1  jmcneill 		aprint_error(": couldn't enable clocks\n");
    250  1.1  jmcneill 		return;
    251  1.1  jmcneill 	}
    252  1.1  jmcneill 
    253  1.1  jmcneill 	sc->sc_syscon = fdtbus_syscon_acquire(phandle, "arasan,soc-ctl-syscon");
    254  1.1  jmcneill 
    255  1.1  jmcneill 	if (of_getprop_uint32(phandle, "bus-width", &bus_width) != 0)
    256  1.1  jmcneill 		bus_width = 4;
    257  1.1  jmcneill 
    258  1.1  jmcneill 	sc->sc_phandle = phandle;
    259  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    260  1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    261  1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    262  1.1  jmcneill 		return;
    263  1.1  jmcneill 	}
    264  1.1  jmcneill 	sc->sc_bsz = size;
    265  1.1  jmcneill 	sc->sc_type = of_search_compatible(phandle, compat_data)->data;
    266  1.1  jmcneill 
    267  1.2  jmcneill 	const uint32_t caps = bus_space_read_4(sc->sc_bst, sc->sc_bsh, SDHC_CAPABILITIES);
    268  1.2  jmcneill 	if ((caps & (SDHC_ADMA2_SUPP|SDHC_64BIT_SYS_BUS)) == SDHC_ADMA2_SUPP) {
    269  1.2  jmcneill 		error = bus_dmatag_subregion(faa->faa_dmat, 0, 0xffffffff,
    270  1.2  jmcneill 		    &sc->sc_base.sc_dmat, BUS_DMA_WAITOK);
    271  1.2  jmcneill 		if (error != 0) {
    272  1.2  jmcneill 			aprint_error(": couldn't create DMA tag: %d\n", error);
    273  1.2  jmcneill 			return;
    274  1.2  jmcneill 		}
    275  1.2  jmcneill 	} else {
    276  1.2  jmcneill 		sc->sc_base.sc_dmat = faa->faa_dmat;
    277  1.2  jmcneill 	}
    278  1.2  jmcneill 
    279  1.1  jmcneill 	sc->sc_base.sc_dev = self;
    280  1.1  jmcneill 	sc->sc_base.sc_host = sc->sc_host;
    281  1.1  jmcneill 	sc->sc_base.sc_flags = SDHC_FLAG_NO_CLKBASE |
    282  1.3  jmcneill 			       SDHC_FLAG_SINGLE_POWER_WRITE |
    283  1.3  jmcneill 			       SDHC_FLAG_32BIT_ACCESS |
    284  1.1  jmcneill 			       SDHC_FLAG_USE_DMA |
    285  1.1  jmcneill 			       SDHC_FLAG_USE_ADMA2 |
    286  1.1  jmcneill 			       SDHC_FLAG_STOP_WITH_TC;
    287  1.1  jmcneill 	if (bus_width == 8)
    288  1.1  jmcneill 		sc->sc_base.sc_flags |= SDHC_FLAG_8BIT_MODE;
    289  1.1  jmcneill 	sc->sc_base.sc_clkbase = clk_get_rate(sc->sc_clk_xin) / 1000;
    290  1.1  jmcneill 	sc->sc_base.sc_vendor_bus_clock = arasan_sdhc_bus_clock_pre;
    291  1.1  jmcneill 	sc->sc_base.sc_vendor_bus_clock_post = arasan_sdhc_bus_clock_post;
    292  1.1  jmcneill 
    293  1.1  jmcneill 	aprint_naive("\n");
    294  1.1  jmcneill 	aprint_normal(": Arasan SDHCI controller\n");
    295  1.1  jmcneill 
    296  1.1  jmcneill 	clkname = fdtbus_get_string(phandle, "clock-output-names");
    297  1.1  jmcneill 	if (clkname == NULL)
    298  1.1  jmcneill 		clkname = faa->faa_name;
    299  1.1  jmcneill 
    300  1.1  jmcneill 	sc->sc_clkdom.name = device_xname(self);
    301  1.1  jmcneill 	sc->sc_clkdom.funcs = &arasan_sdhc_clk_funcs;
    302  1.1  jmcneill 	sc->sc_clkdom.priv = sc;
    303  1.1  jmcneill 	sc->sc_clk_card.domain = &sc->sc_clkdom;
    304  1.1  jmcneill 	sc->sc_clk_card.name = kmem_asprintf("%s", clkname);
    305  1.1  jmcneill 	clk_attach(&sc->sc_clk_card);
    306  1.1  jmcneill 
    307  1.1  jmcneill 	fdtbus_register_clock_controller(self, phandle, &arasan_sdhc_fdt_clk_funcs);
    308  1.1  jmcneill 
    309  1.1  jmcneill 	ih = fdtbus_intr_establish(phandle, 0, IPL_SDMMC, 0, sdhc_intr, &sc->sc_base);
    310  1.1  jmcneill 	if (ih == NULL) {
    311  1.1  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt on %s\n", intrstr);
    312  1.1  jmcneill 		return;
    313  1.1  jmcneill 	}
    314  1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    315  1.1  jmcneill 
    316  1.1  jmcneill 	arasan_sdhc_init(self);
    317  1.1  jmcneill }
    318  1.1  jmcneill 
    319  1.1  jmcneill CFATTACH_DECL_NEW(arasan_sdhc_fdt, sizeof(struct arasan_sdhc_softc),
    320  1.1  jmcneill 	arasan_sdhc_match, arasan_sdhc_attach, NULL, NULL);
    321