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