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