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