Home | History | Annotate | Line # | Download | only in dev
pq3sdhc.c revision 1.2
      1 /*	$NetBSD: pq3sdhc.c,v 1.2 2011/01/18 01:02:53 matt Exp $	*/
      2 /*-
      3  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Matt Thomas of 3am Software Foundry.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: pq3sdhc.c,v 1.2 2011/01/18 01:02:53 matt Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/device.h>
     37 #include <sys/kernel.h>
     38 #include <sys/proc.h>
     39 #include <sys/queue.h>
     40 
     41 #include <sys/bus.h>
     42 
     43 #include <powerpc/booke/cpuvar.h>
     44 #include <powerpc/booke/e500var.h>
     45 #include <powerpc/booke/e500reg.h>
     46 
     47 #include <dev/sdmmc/sdhcreg.h>
     48 #include <dev/sdmmc/sdhcvar.h>
     49 
     50 static int pq3sdhc_match(device_t, cfdata_t, void *);
     51 static void pq3sdhc_attach(device_t, device_t, void *);
     52 
     53 struct pq3sdhc_softc {
     54 	struct sdhc_softc	sc;
     55 	bus_space_tag_t		sc_bst;
     56 	bus_space_handle_t	sc_bsh;
     57 	struct sdhc_host	*sc_hosts[1];
     58 	void 			*sc_ih;		/* interrupt vectoring */
     59 };
     60 
     61 CFATTACH_DECL_NEW(pq3sdhc, sizeof(struct pq3sdhc_softc),
     62     pq3sdhc_match, pq3sdhc_attach, NULL, NULL);
     63 
     64 static int
     65 pq3sdhc_match(device_t parent, cfdata_t cf, void *aux)
     66 {
     67 
     68         if (!e500_cpunode_submatch(parent, cf, cf->cf_name, aux))
     69                 return 0;
     70 
     71         return 1;
     72 }
     73 
     74 static void
     75 pq3sdhc_attach(device_t parent, device_t self, void *aux)
     76 {
     77 	struct cpunode_softc * const psc = device_private(parent);
     78 	struct pq3sdhc_softc * const sc = device_private(self);
     79 	struct cpunode_attach_args * const cna = aux;
     80 	struct cpunode_locators * const cnl = &cna->cna_locs;
     81 	int error;
     82 
     83 	psc->sc_children |= cna->cna_childmask;
     84 	sc->sc.sc_dmat = cna->cna_dmat;
     85 	sc->sc.sc_dev = self;
     86 	//sc->sc.sc_flags |= SDHC_FLAG_USE_DMA;
     87 	sc->sc.sc_host = sc->sc_hosts;
     88 	sc->sc_bst = cna->cna_memt;
     89 
     90 	error = bus_space_map(sc->sc_bst, cnl->cnl_addr, cnl->cnl_size, 0,
     91 	    &sc->sc_bsh);
     92 	if (error) {
     93 		aprint_error_dev(self,
     94 		    "can't map registers for %s: %d\n", cnl->cnl_name, error);
     95 		return;
     96 	}
     97 
     98 	aprint_naive(": SDHC controller\n");
     99 	aprint_normal(": SDHC controller\n");
    100 
    101 	sc->sc_ih = intr_establish(cnl->cnl_intrs[0], IPL_VM, IST_ONCHIP,
    102 	    sdhc_intr, &sc->sc);
    103 	if (sc->sc_ih == NULL) {
    104 		aprint_error_dev(self, "failed to establish interrupt %d\n",
    105 		     cnl->cnl_intrs[0]);
    106 		goto fail;
    107 	}
    108 	aprint_normal_dev(self, "interrupting on irq %d\n",
    109 	     cnl->cnl_intrs[0]);
    110 
    111 	error = sdhc_host_found(&sc->sc, sc->sc_bst, sc->sc_bsh, cnl->cnl_size);
    112 	if (error != 0) {
    113 		aprint_error_dev(self, "couldn't initialize host, error=%d\n",
    114 		    error);
    115 		goto fail;
    116 	}
    117 	return;
    118 
    119 fail:
    120 	if (sc->sc_ih) {
    121 		intr_disestablish(sc->sc_ih);
    122 		sc->sc_ih = NULL;
    123 	}
    124 	bus_space_unmap(sc->sc_bst, sc->sc_bsh, cnl->cnl_size);
    125 }
    126