Home | History | Annotate | Line # | Download | only in samsung
exynos_dwcmmc.c revision 1.10
      1  1.10     skrll /* $NetBSD: exynos_dwcmmc.c,v 1.10 2020/03/20 06:23:51 skrll Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2015 Jared D. 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.10     skrll __KERNEL_RCSID(0, "$NetBSD: exynos_dwcmmc.c,v 1.10 2020/03/20 06:23:51 skrll 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 
     41   1.1  jmcneill #include <arm/samsung/exynos_var.h>
     42   1.1  jmcneill 
     43  1.10     skrll #include <dev/ic/dwc_mmc_reg.h>
     44   1.1  jmcneill #include <dev/ic/dwc_mmc_var.h>
     45   1.1  jmcneill #include <dev/fdt/fdtvar.h>
     46   1.1  jmcneill 
     47   1.4  jmcneill #define	MPS_BEGIN	0x200
     48   1.4  jmcneill #define	MPS_END		0x204
     49   1.4  jmcneill #define	MPS_CTRL	0x20c
     50   1.4  jmcneill #define	 MPS_CTRL_SECURE_WRITE		__BIT(6)
     51   1.4  jmcneill #define	 MPS_CTRL_NON_SECURE_READ	__BIT(5)
     52   1.4  jmcneill #define	 MPS_CTRL_NON_SECURE_WRITE	__BIT(4)
     53   1.4  jmcneill #define	 MPS_CTRL_VALID			__BIT(0)
     54   1.4  jmcneill 
     55   1.1  jmcneill static int	exynos_dwcmmc_match(device_t, cfdata_t, void *);
     56   1.1  jmcneill static void	exynos_dwcmmc_attach(device_t, device_t, void *);
     57   1.1  jmcneill 
     58   1.1  jmcneill static int	exynos_dwcmmc_card_detect(struct dwc_mmc_softc *);
     59   1.6  jmcneill static int	exynos_dwcmmc_bus_clock(struct dwc_mmc_softc *, int);
     60   1.1  jmcneill 
     61   1.1  jmcneill struct exynos_dwcmmc_softc {
     62   1.1  jmcneill 	struct dwc_mmc_softc	sc;
     63   1.1  jmcneill 	struct clk		*sc_clk_biu;
     64   1.1  jmcneill 	struct clk		*sc_clk_ciu;
     65   1.1  jmcneill 	struct fdtbus_gpio_pin	*sc_pin_cd;
     66   1.6  jmcneill 	u_int			sc_ciu_div;
     67   1.1  jmcneill };
     68   1.1  jmcneill 
     69   1.8     skrll CFATTACH_DECL_NEW(exynos_dwcmmc, sizeof(struct exynos_dwcmmc_softc),
     70   1.1  jmcneill 	exynos_dwcmmc_match, exynos_dwcmmc_attach, NULL, NULL);
     71   1.1  jmcneill 
     72   1.1  jmcneill static const char * const exynos_dwcmmc_compat[] = {
     73   1.5     skrll 	"samsung,exynos5250-dw-mshc",
     74   1.1  jmcneill 	"samsung,exynos5420-dw-mshc-smu",
     75   1.1  jmcneill 	"samsung,exynos5420-dw-mshc",
     76   1.1  jmcneill 	NULL
     77   1.1  jmcneill };
     78   1.1  jmcneill 
     79   1.1  jmcneill static int
     80   1.1  jmcneill exynos_dwcmmc_match(device_t parent, cfdata_t cf, void *aux)
     81   1.1  jmcneill {
     82   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
     83   1.1  jmcneill 
     84   1.1  jmcneill 	return of_match_compatible(faa->faa_phandle, exynos_dwcmmc_compat);
     85   1.1  jmcneill }
     86   1.1  jmcneill 
     87   1.1  jmcneill static void
     88   1.1  jmcneill exynos_dwcmmc_attach(device_t parent, device_t self, void *aux)
     89   1.1  jmcneill {
     90   1.1  jmcneill 	struct exynos_dwcmmc_softc *esc = device_private(self);
     91   1.1  jmcneill 	struct dwc_mmc_softc *sc = &esc->sc;
     92   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
     93   1.1  jmcneill 	const int phandle = faa->faa_phandle;
     94   1.1  jmcneill 	char intrstr[128];
     95   1.1  jmcneill 	bus_addr_t addr;
     96   1.1  jmcneill 	bus_size_t size;
     97   1.1  jmcneill 	int error;
     98   1.1  jmcneill 
     99   1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    100   1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    101   1.1  jmcneill 		return;
    102   1.1  jmcneill 	}
    103   1.1  jmcneill 
    104   1.6  jmcneill 	if (of_getprop_uint32(phandle, "samsung,dw-mshc-ciu-div", &esc->sc_ciu_div)) {
    105   1.1  jmcneill 		aprint_error(": missing samsung,dw-mshc-ciu-div property\n");
    106   1.1  jmcneill 		return;
    107   1.1  jmcneill 	}
    108   1.1  jmcneill 
    109   1.1  jmcneill 	esc->sc_clk_biu = fdtbus_clock_get(phandle, "biu");
    110   1.1  jmcneill 	if (esc->sc_clk_biu == NULL) {
    111   1.1  jmcneill 		aprint_error(": couldn't get clock biu\n");
    112   1.1  jmcneill 		return;
    113   1.1  jmcneill 	}
    114   1.1  jmcneill 	esc->sc_clk_ciu = fdtbus_clock_get(phandle, "ciu");
    115   1.1  jmcneill 	if (esc->sc_clk_ciu == NULL) {
    116   1.1  jmcneill 		aprint_error(": couldn't get clock ciu\n");
    117   1.1  jmcneill 		return;
    118   1.1  jmcneill 	}
    119   1.2  jmcneill 
    120   1.2  jmcneill 	error = clk_enable(esc->sc_clk_biu);
    121   1.1  jmcneill 	if (error) {
    122   1.2  jmcneill 		aprint_error(": couldn't enable clock biu: %d\n", error);
    123   1.1  jmcneill 		return;
    124   1.1  jmcneill 	}
    125   1.1  jmcneill 	error = clk_enable(esc->sc_clk_ciu);
    126   1.1  jmcneill 	if (error) {
    127   1.1  jmcneill 		aprint_error(": couldn't enable clock ciu: %d\n", error);
    128   1.1  jmcneill 		return;
    129   1.1  jmcneill 	}
    130   1.1  jmcneill 
    131   1.1  jmcneill 	sc->sc_dev = self;
    132   1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    133   1.1  jmcneill 	sc->sc_dmat = faa->faa_dmat;
    134  1.10     skrll 	sc->sc_intr_cardmask = DWC_MMC_INT_SDIO_INT(8);
    135   1.1  jmcneill 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    136   1.1  jmcneill 	if (error) {
    137   1.9     skrll 		aprint_error(": couldn't map %#" PRIxBUSADDR ": %d\n",
    138   1.9     skrll 		    addr, error);
    139   1.1  jmcneill 		return;
    140   1.1  jmcneill 	}
    141   1.1  jmcneill 
    142   1.6  jmcneill 	sc->sc_clock_freq = clk_get_rate(esc->sc_clk_ciu) / (esc->sc_ciu_div + 1);
    143   1.7  jmcneill 	of_getprop_uint32(phandle, "fifo-depth", &sc->sc_fifo_depth);
    144   1.7  jmcneill 	sc->sc_flags = DWC_MMC_F_DMA;
    145   1.6  jmcneill 	sc->sc_bus_clock = exynos_dwcmmc_bus_clock;
    146   1.1  jmcneill 
    147   1.1  jmcneill 	esc->sc_pin_cd = fdtbus_gpio_acquire(phandle, "cd-gpios",
    148   1.1  jmcneill 	    GPIO_PIN_INPUT);
    149   1.4  jmcneill 	if (esc->sc_pin_cd)
    150   1.1  jmcneill 		sc->sc_card_detect = exynos_dwcmmc_card_detect;
    151   1.1  jmcneill 
    152   1.1  jmcneill 	aprint_naive("\n");
    153   1.1  jmcneill 	aprint_normal(": MHS (%u Hz)\n", sc->sc_clock_freq);
    154   1.1  jmcneill 
    155   1.1  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    156   1.1  jmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
    157   1.1  jmcneill 		return;
    158   1.1  jmcneill 	}
    159   1.1  jmcneill 
    160   1.4  jmcneill 	if (dwc_mmc_init(sc) != 0)
    161   1.4  jmcneill 		return;
    162   1.4  jmcneill 
    163   1.1  jmcneill 	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_BIO, 0,
    164   1.1  jmcneill 	    dwc_mmc_intr, sc);
    165   1.1  jmcneill 	if (sc->sc_ih == NULL) {
    166   1.1  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    167   1.1  jmcneill 		    intrstr);
    168   1.1  jmcneill 		return;
    169   1.1  jmcneill 	}
    170   1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    171   1.1  jmcneill 
    172   1.4  jmcneill 	/* Disable encryption mode */
    173   1.4  jmcneill 	const char * compat_enc[] = { "samsung,exynos5420-dw-mshc-smu", NULL };
    174   1.4  jmcneill 	if (of_match_compatible(phandle, compat_enc)) {
    175   1.4  jmcneill 		bus_space_write_4(sc->sc_bst, sc->sc_bsh, MPS_BEGIN, 0);
    176   1.4  jmcneill 		bus_space_write_4(sc->sc_bst, sc->sc_bsh, MPS_END, ~0U);
    177   1.4  jmcneill 		bus_space_write_4(sc->sc_bst, sc->sc_bsh, MPS_CTRL,
    178   1.4  jmcneill 		    MPS_CTRL_NON_SECURE_READ | MPS_CTRL_NON_SECURE_WRITE |
    179   1.4  jmcneill 		    MPS_CTRL_SECURE_WRITE | MPS_CTRL_VALID);
    180   1.4  jmcneill 	}
    181   1.1  jmcneill }
    182   1.1  jmcneill 
    183   1.1  jmcneill static int
    184   1.1  jmcneill exynos_dwcmmc_card_detect(struct dwc_mmc_softc *sc)
    185   1.1  jmcneill {
    186   1.1  jmcneill 	struct exynos_dwcmmc_softc *esc = device_private(sc->sc_dev);
    187   1.1  jmcneill 
    188   1.1  jmcneill 	KASSERT(esc->sc_pin_cd != NULL);
    189   1.1  jmcneill 
    190   1.1  jmcneill 	return fdtbus_gpio_read(esc->sc_pin_cd);
    191   1.1  jmcneill }
    192   1.6  jmcneill 
    193   1.6  jmcneill static int
    194   1.6  jmcneill exynos_dwcmmc_bus_clock(struct dwc_mmc_softc *sc, int rate)
    195   1.6  jmcneill {
    196   1.6  jmcneill 	struct exynos_dwcmmc_softc *esc = device_private(sc->sc_dev);
    197   1.6  jmcneill 	const int ciu_div = esc->sc_ciu_div + 1;
    198   1.6  jmcneill 	int error;
    199   1.6  jmcneill 
    200   1.6  jmcneill 	error = clk_set_rate(esc->sc_clk_ciu, 1000 * rate * ciu_div);
    201   1.6  jmcneill 	if (error != 0) {
    202   1.6  jmcneill 		aprint_error_dev(sc->sc_dev, "failed to set rate to %u Hz: %d\n",
    203   1.6  jmcneill 		    rate * ciu_div * 1000, error);
    204   1.6  jmcneill 		return error;
    205   1.6  jmcneill 	}
    206   1.6  jmcneill 
    207   1.6  jmcneill 	sc->sc_clock_freq = clk_get_rate(esc->sc_clk_ciu) / ciu_div;
    208   1.6  jmcneill 
    209   1.6  jmcneill 	aprint_debug_dev(sc->sc_dev, "set clock rate to %u Hz (target %u Hz)\n",
    210   1.6  jmcneill 	    sc->sc_clock_freq, rate * 1000);
    211   1.6  jmcneill 
    212   1.6  jmcneill 	return 0;
    213   1.6  jmcneill }
    214