xref: /netbsd-src/sys/arch/arm/broadcom/bcm53xx_sdhc.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*-
2  * Copyright (c) 2012 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Matt Thomas of 3am Software Foundry.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "locators.h"
31 
32 #include <sys/cdefs.h>
33 
34 __KERNEL_RCSID(1, "$NetBSD: bcm53xx_sdhc.c,v 1.1 2012/09/01 00:04:44 matt Exp $");
35 
36 #include <sys/bus.h>
37 #include <sys/device.h>
38 #include <sys/intr.h>
39 #include <sys/systm.h>
40 
41 #include <arm/broadcom/bcm53xx_reg.h>
42 #include <arm/broadcom/bcm53xx_var.h>
43 
44 #include <dev/sdmmc/sdhcreg.h>
45 #include <dev/sdmmc/sdhcvar.h>
46 
47 static int sdhc_ccb_match(device_t, cfdata_t, void *);
48 static void sdhc_ccb_attach(device_t, device_t, void *);
49 
50 struct sdhc_ccb_softc {
51 	struct sdhc_softc ccbsc_sc;
52 	bus_space_tag_t ccbsc_bst;
53 	bus_space_handle_t ccbsc_bsh;
54 	struct sdhc_host *ccbsc_hosts[1];
55 	void *ccbsc_ih;
56 };
57 
58 CFATTACH_DECL_NEW(sdhc_ccb, sizeof(struct sdhc_ccb_softc),
59 	sdhc_ccb_match, sdhc_ccb_attach, NULL, NULL);
60 
61 static int
62 sdhc_ccb_match(device_t parent, cfdata_t cf, void *aux)
63 {
64 	struct bcmccb_attach_args * const ccbaa = aux;
65 	const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
66 
67 	if (strcmp(cf->cf_name, loc->loc_name))
68 		return 0;
69 
70 	KASSERT(cf->cf_loc[BCMCCBCF_PORT] == BCMCCBCF_PORT_DEFAULT);
71 
72 	return 1;
73 }
74 
75 static void
76 sdhc_ccb_attach(device_t parent, device_t self, void *aux)
77 {
78 	struct sdhc_ccb_softc * const ccbsc = device_private(self);
79 	struct sdhc_softc * const sc = &ccbsc->ccbsc_sc;
80 	struct bcmccb_attach_args * const ccbaa = aux;
81 	const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
82 
83 	ccbsc->ccbsc_bst = ccbaa->ccbaa_ccb_bst;
84 	bus_space_subregion(ccbsc->ccbsc_bst, ccbaa->ccbaa_ccb_bsh,
85 	    loc->loc_offset, loc->loc_size, &ccbsc->ccbsc_bsh);
86 
87 	sc->sc_dev = self;
88 	sc->sc_dmat = ccbaa->ccbaa_dmat;
89 	sc->sc_host = ccbsc->ccbsc_hosts;
90 	sc->sc_flags |= SDHC_FLAG_32BIT_ACCESS;
91 	sc->sc_flags |= SDHC_FLAG_HAVE_CGM;
92 	//sc->sc_flags |= SDHC_FLAG_USE_DMA;
93 
94 	aprint_naive(": SDHC controller\n");
95 	aprint_normal(": SDHC controller%s\n",
96 	   (sc->sc_flags & SDHC_FLAG_USE_DMA) ? " (DMA enabled)" : "");
97 
98 #if 1
99 	int error = sdhc_host_found(sc, ccbsc->ccbsc_bst, ccbsc->ccbsc_bsh,
100 	    loc->loc_size);
101 	if (error != 0) {
102 		aprint_error_dev(self, "couldn't initialize host, error=%d\n",
103 		    error);
104 		goto fail;
105 	}
106 #endif
107 
108 	ccbsc->ccbsc_ih = intr_establish(loc->loc_intrs[0], IPL_VM, IST_LEVEL,
109 	    sdhc_intr, sc);
110 	if (ccbsc->ccbsc_ih == NULL) {
111 		aprint_error_dev(self, "failed to establish interrupt %d\n",
112 		     loc->loc_intrs[0]);
113 		goto fail;
114 	}
115 	aprint_normal_dev(self, "interrupting on irq %d\n",
116 	     loc->loc_intrs[0]);
117 
118 	return;
119 
120 fail:
121 	if (ccbsc->ccbsc_ih) {
122 		intr_disestablish(ccbsc->ccbsc_ih);
123 		ccbsc->ccbsc_ih = NULL;
124 	}
125 }
126