Home | History | Annotate | Line # | Download | only in altera
      1 /* $NetBSD: cycv_dwcmmc.c,v 1.7 2021/01/29 14:12:01 skrll 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: cycv_dwcmmc.c,v 1.7 2021/01/29 14:12:01 skrll 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 <dev/ic/dwc_mmc_reg.h>
     42 #include <dev/ic/dwc_mmc_var.h>
     43 #include <dev/fdt/fdtvar.h>
     44 
     45 #define	FIFO_REG	0x200
     46 
     47 static int	cycv_dwcmmc_match(device_t, cfdata_t, void *);
     48 static void	cycv_dwcmmc_attach(device_t, device_t, void *);
     49 
     50 static int	cycv_dwcmmc_card_detect(struct dwc_mmc_softc *);
     51 
     52 struct cycv_dwcmmc_softc {
     53 	struct dwc_mmc_softc	sc;
     54 	int			sc_phandle;
     55 	bool			sc_non_removable;
     56 	bool			sc_broken_cd;
     57 	struct fdtbus_gpio_pin	*sc_gpio_cd;
     58 	bool			sc_gpio_cd_inverted;
     59 	struct clk		*sc_clk_biu;
     60 	struct clk		*sc_clk_ciu;
     61 };
     62 
     63 CFATTACH_DECL_NEW(cycv_dwcmmc, sizeof(struct dwc_mmc_softc),
     64 	cycv_dwcmmc_match, cycv_dwcmmc_attach, NULL, NULL);
     65 
     66 static const struct device_compatible_entry compat_data[] = {
     67 	{ .compat = "altr,socfpga-dw-mshc" },
     68 	DEVICE_COMPAT_EOL
     69 };
     70 
     71 static int
     72 cycv_dwcmmc_match(device_t parent, cfdata_t cf, void *aux)
     73 {
     74 	struct fdt_attach_args * const faa = aux;
     75 
     76 	return of_compatible_match(faa->faa_phandle, compat_data);
     77 }
     78 
     79 static void
     80 cycv_dwcmmc_attach(device_t parent, device_t self, void *aux)
     81 {
     82 	struct cycv_dwcmmc_softc *esc = device_private(self);
     83 	struct dwc_mmc_softc *sc = &esc->sc;
     84 	struct fdt_attach_args * const faa = aux;
     85 	const int phandle = faa->faa_phandle;
     86 	char intrstr[128];
     87 	bus_addr_t addr;
     88 	bus_size_t size;
     89 	u_int fifo_depth;
     90 	int error;
     91 
     92 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
     93 		aprint_error(": couldn't get registers\n");
     94 		return;
     95 	}
     96 
     97 	if (of_getprop_uint32(phandle, "fifo-depth", &fifo_depth)) {
     98 		fifo_depth = 64;
     99 	}
    100 
    101 	esc->sc_clk_biu = fdtbus_clock_get(phandle, "biu");
    102 	if (esc->sc_clk_biu == NULL) {
    103 		aprint_error(": couldn't get clock biu\n");
    104 		return;
    105 	}
    106 	esc->sc_clk_ciu = fdtbus_clock_get(phandle, "ciu");
    107 	if (esc->sc_clk_ciu == NULL) {
    108 		aprint_error(": couldn't get clock ciu\n");
    109 		return;
    110 	}
    111 
    112 	error = clk_enable(esc->sc_clk_biu);
    113 	if (error) {
    114 		aprint_error(": couldn't enable clock biu: %d\n", error);
    115 		return;
    116 	}
    117 	error = clk_enable(esc->sc_clk_ciu);
    118 	if (error) {
    119 		aprint_error(": couldn't enable clock ciu: %d\n", error);
    120 		return;
    121 	}
    122 
    123 	sc->sc_dev = self;
    124 	sc->sc_bst = faa->faa_bst;
    125 	sc->sc_dmat = faa->faa_dmat;
    126 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    127 	if (error) {
    128 		aprint_error(": couldn't map %#" PRIxBUSADDR ": %d\n",
    129 		    addr, error);
    130 		return;
    131 	}
    132 
    133 	sc->sc_clock_freq = clk_get_rate(esc->sc_clk_ciu);
    134 	sc->sc_fifo_depth = fifo_depth;
    135 	sc->sc_fifo_reg = FIFO_REG;
    136 	sc->sc_flags = DWC_MMC_F_USE_HOLD_REG | DWC_MMC_F_DMA;
    137 	sc->sc_intr_cardmask = DWC_MMC_INT_SDIO_INT(8);
    138 
    139 	sc->sc_card_detect = cycv_dwcmmc_card_detect;
    140 	sc->sc_write_protect = NULL;
    141 
    142 	esc->sc_phandle = phandle;
    143 	esc->sc_gpio_cd = fdtbus_gpio_acquire(phandle, "cd-gpios", GPIO_PIN_INPUT);
    144 	esc->sc_gpio_cd_inverted = of_hasprop(phandle, "cd-inverted") ? 0 : 1;
    145 
    146 	esc->sc_non_removable = of_hasprop(phandle, "non-removable");
    147 	esc->sc_broken_cd = of_hasprop(phandle, "broken-cd");
    148 
    149 	aprint_naive("\n");
    150 	aprint_normal(": MHS (%u Hz)\n", sc->sc_clock_freq);
    151 
    152 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    153 		aprint_error_dev(self, "failed to decode interrupt\n");
    154 		return;
    155 	}
    156 
    157 	if (dwc_mmc_init(sc) != 0)
    158 		return;
    159 
    160 	sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_BIO, 0,
    161 	    dwc_mmc_intr, sc, device_xname(sc->sc_dev));
    162 	if (sc->sc_ih == NULL) {
    163 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    164 		    intrstr);
    165 		return;
    166 	}
    167 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    168 }
    169 
    170 static int
    171 cycv_dwcmmc_card_detect(struct dwc_mmc_softc *sc)
    172 {
    173 	struct cycv_dwcmmc_softc *esc = device_private(sc->sc_dev);
    174 	int val;
    175 
    176 	if (esc->sc_non_removable || esc->sc_broken_cd) {
    177 		return 1;
    178 	} else if (esc->sc_gpio_cd != NULL) {
    179 		val = fdtbus_gpio_read(esc->sc_gpio_cd);
    180 		if (esc->sc_gpio_cd_inverted)
    181 			val = !val;
    182 		return val;
    183 	} else {
    184 		return 1;
    185 	}
    186 }
    187