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.2 2014/02/19 22:21:16 matt Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/device.h>
39 #include <sys/intr.h>
40 #include <sys/systm.h>
41 
42 #include <arm/broadcom/bcm53xx_reg.h>
43 #include <arm/broadcom/bcm53xx_var.h>
44 
45 #include <dev/sdmmc/sdhcreg.h>
46 #include <dev/sdmmc/sdhcvar.h>
47 
48 static int sdhc_ccb_match(device_t, cfdata_t, void *);
49 static void sdhc_ccb_attach(device_t, device_t, void *);
50 
51 struct sdhc_ccb_softc {
52           struct sdhc_softc ccbsc_sc;
53           bus_space_tag_t ccbsc_bst;
54           bus_space_handle_t ccbsc_bsh;
55           struct sdhc_host *ccbsc_hosts[1];
56           void *ccbsc_ih;
57 };
58 
59 CFATTACH_DECL_NEW(sdhc_ccb, sizeof(struct sdhc_ccb_softc),
60           sdhc_ccb_match, sdhc_ccb_attach, NULL, NULL);
61 
62 static int
sdhc_ccb_match(device_t parent,cfdata_t cf,void * aux)63 sdhc_ccb_match(device_t parent, cfdata_t cf, void *aux)
64 {
65           struct bcmccb_attach_args * const ccbaa = aux;
66           const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
67 
68           if (strcmp(cf->cf_name, loc->loc_name))
69                     return 0;
70 
71           KASSERT(cf->cf_loc[BCMCCBCF_PORT] == BCMCCBCF_PORT_DEFAULT);
72 
73           return 1;
74 }
75 
76 static void
sdhc_ccb_attach(device_t parent,device_t self,void * aux)77 sdhc_ccb_attach(device_t parent, device_t self, void *aux)
78 {
79           struct sdhc_ccb_softc * const ccbsc = device_private(self);
80           struct sdhc_softc * const sc = &ccbsc->ccbsc_sc;
81           struct bcmccb_attach_args * const ccbaa = aux;
82           const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
83 
84           ccbsc->ccbsc_bst = ccbaa->ccbaa_ccb_bst;
85           bus_space_subregion(ccbsc->ccbsc_bst, ccbaa->ccbaa_ccb_bsh,
86               loc->loc_offset, loc->loc_size, &ccbsc->ccbsc_bsh);
87 
88           sc->sc_dev = self;
89           sc->sc_dmat = ccbaa->ccbaa_dmat;
90           sc->sc_host = ccbsc->ccbsc_hosts;
91           sc->sc_flags |= SDHC_FLAG_32BIT_ACCESS;
92           sc->sc_flags |= SDHC_FLAG_HAVE_CGM;
93           //sc->sc_flags |= SDHC_FLAG_USE_DMA;
94 
95           aprint_naive(": SDHC controller\n");
96           aprint_normal(": SDHC controller%s\n",
97              (sc->sc_flags & SDHC_FLAG_USE_DMA) ? " (DMA enabled)" : "");
98 
99 #if 1
100           int error = sdhc_host_found(sc, ccbsc->ccbsc_bst, ccbsc->ccbsc_bsh,
101               loc->loc_size);
102           if (error != 0) {
103                     aprint_error_dev(self, "couldn't initialize host, error=%d\n",
104                         error);
105                     goto fail;
106           }
107 #endif
108 
109           ccbsc->ccbsc_ih = intr_establish(loc->loc_intrs[0], IPL_VM, IST_LEVEL,
110               sdhc_intr, sc);
111           if (ccbsc->ccbsc_ih == NULL) {
112                     aprint_error_dev(self, "failed to establish interrupt %d\n",
113                          loc->loc_intrs[0]);
114                     goto fail;
115           }
116           aprint_normal_dev(self, "interrupting on irq %d\n",
117                loc->loc_intrs[0]);
118 
119           return;
120 
121 fail:
122           if (ccbsc->ccbsc_ih) {
123                     intr_disestablish(ccbsc->ccbsc_ih);
124                     ccbsc->ccbsc_ih = NULL;
125           }
126 }
127