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