Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_emmc.c revision 1.2
      1 /*	$NetBSD: bcm2835_emmc.c,v 1.2 2012/10/30 20:14:22 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Nick Hudson
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: bcm2835_emmc.c,v 1.2 2012/10/30 20:14:22 skrll Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/device.h>
     38 #include <sys/bus.h>
     39 
     40 #include <arm/broadcom/bcm2835reg.h>
     41 #include <arm/broadcom/bcm_amba.h>
     42 
     43 #include <dev/sdmmc/sdhcreg.h>
     44 #include <dev/sdmmc/sdhcvar.h>
     45 #include <dev/sdmmc/sdmmcvar.h>
     46 
     47 struct bcmemmc_softc {
     48 	struct sdhc_softc	sc;
     49 	device_t		sc_sdmmc;
     50 
     51 	bus_space_tag_t		sc_iot;
     52 	bus_space_handle_t	sc_ioh;
     53 	struct sdhc_host	*sc_hosts[1];
     54 	void			*sc_ih;
     55 };
     56 
     57 static int bcmemmc_match(device_t, struct cfdata *, void *);
     58 static void bcmemmc_attach(device_t, device_t, void *);
     59 
     60 CFATTACH_DECL_NEW(bcmemmc, sizeof(struct bcmemmc_softc),
     61     bcmemmc_match, bcmemmc_attach, NULL, NULL);
     62 
     63 /* ARGSUSED */
     64 static int
     65 bcmemmc_match(device_t parent, struct cfdata *match, void *aux)
     66 {
     67 	struct amba_attach_args *aaa = aux;
     68 
     69 	if (strcmp(aaa->aaa_name, "emmc") != 0)
     70 		return 0;
     71 
     72 	return 1;
     73 }
     74 
     75 /* ARGSUSED */
     76 static void
     77 bcmemmc_attach(device_t parent, device_t self, void *aux)
     78 {
     79 	struct bcmemmc_softc *sc = device_private(self);
     80 	prop_dictionary_t dict = device_properties(self);
     81 	struct amba_attach_args *aaa = aux;
     82 	prop_number_t frequency;
     83 	int error;
     84 
     85 	sc->sc.sc_dev = self;
     86 // 	sc->sc.sc_dmat = aaa->amba_dmat;
     87 	sc->sc.sc_flags = 0;
     88 // 	sc->sc.sc_flags |= SDHC_FLAG_USE_DMA;		/* not available (yet) */
     89 	sc->sc.sc_flags |= SDHC_FLAG_32BIT_ACCESS;
     90 	sc->sc.sc_flags |= SDHC_FLAG_HOSTCAPS;
     91 	sc->sc.sc_caps = SDHC_VOLTAGE_SUPP_3_3V;
     92 	sc->sc.sc_host = sc->sc_hosts;
     93 	sc->sc.sc_clkbase = 50000;	/* Default to 50MHz */
     94 	sc->sc_iot = aaa->aaa_iot;
     95 
     96         /* Fetch the EMMC clock frequency from property if set. */
     97         frequency = prop_dictionary_get(dict, "frequency");
     98         if (frequency != NULL) {
     99 		sc->sc.sc_clkbase = prop_number_integer_value(frequency) / 1000;
    100 	}
    101 
    102 	error = bus_space_map(sc->sc_iot, aaa->aaa_addr, aaa->aaa_size, 0,
    103 	    &sc->sc_ioh);
    104 	if (error) {
    105 		aprint_error_dev(self,
    106 		    "can't map registers for %s: %d\n", aaa->aaa_name, error);
    107 		return;
    108 	}
    109 
    110 	aprint_naive(": SDHC controller\n");
    111 	aprint_normal(": SDHC controller\n");
    112 
    113  	sc->sc_ih = bcm2835_intr_establish(aaa->aaa_intr, IPL_SDMMC, sdhc_intr,
    114  	    &sc->sc);
    115 
    116 	if (sc->sc_ih == NULL) {
    117 		aprint_error_dev(self, "failed to establish interrupt %d\n",
    118 		     aaa->aaa_intr);
    119 		goto fail;
    120 	}
    121 	aprint_normal_dev(self, "interrupting on intr %d\n", aaa->aaa_intr);
    122 
    123 	error = sdhc_host_found(&sc->sc, sc->sc_iot, sc->sc_ioh,
    124  	    aaa->aaa_size);
    125 	if (error != 0) {
    126 		aprint_error_dev(self, "couldn't initialize host, error=%d\n",
    127 		    error);
    128 		goto fail;
    129 	}
    130 	return;
    131 
    132 fail:
    133 	if (sc->sc_ih) {
    134 		intr_disestablish(sc->sc_ih);
    135 		sc->sc_ih = NULL;
    136 	}
    137 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, aaa->aaa_size);
    138 }
    139