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