bcm2835_emmc.c revision 1.5 1 /* $NetBSD: bcm2835_emmc.c,v 1.5 2013/01/07 13:23:46 jmcneill 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.5 2013/01/07 13:23:46 jmcneill 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->aaa_dmat;
87 sc->sc.sc_flags = 0;
88 sc->sc.sc_flags |= SDHC_FLAG_32BIT_ACCESS;
89 sc->sc.sc_flags |= SDHC_FLAG_HOSTCAPS;
90 sc->sc.sc_caps = SDHC_VOLTAGE_SUPP_3_3V | SDHC_HIGH_SPEED_SUPP |
91 SDHC_MAX_BLK_LEN_1024;
92 #if notyet
93 sc->sc.sc_flags |= SDHC_FLAG_USE_DMA;
94 sc->sc.sc_caps |= SDHC_DMA_SUPPORT;
95 #endif
96 sc->sc.sc_host = sc->sc_hosts;
97 sc->sc.sc_clkbase = 50000; /* Default to 50MHz */
98 sc->sc_iot = aaa->aaa_iot;
99
100 /* Fetch the EMMC clock frequency from property if set. */
101 frequency = prop_dictionary_get(dict, "frequency");
102 if (frequency != NULL) {
103 sc->sc.sc_clkbase = prop_number_integer_value(frequency) / 1000;
104 }
105
106 error = bus_space_map(sc->sc_iot, aaa->aaa_addr, aaa->aaa_size, 0,
107 &sc->sc_ioh);
108 if (error) {
109 aprint_error_dev(self,
110 "can't map registers for %s: %d\n", aaa->aaa_name, error);
111 return;
112 }
113
114 aprint_naive(": SDHC controller\n");
115 aprint_normal(": SDHC controller\n");
116
117 sc->sc_ih = bcm2835_intr_establish(aaa->aaa_intr, IPL_SDMMC, sdhc_intr,
118 &sc->sc);
119
120 if (sc->sc_ih == NULL) {
121 aprint_error_dev(self, "failed to establish interrupt %d\n",
122 aaa->aaa_intr);
123 goto fail;
124 }
125 aprint_normal_dev(self, "interrupting on intr %d\n", aaa->aaa_intr);
126
127 error = sdhc_host_found(&sc->sc, sc->sc_iot, sc->sc_ioh,
128 aaa->aaa_size);
129 if (error != 0) {
130 aprint_error_dev(self, "couldn't initialize host, error=%d\n",
131 error);
132 goto fail;
133 }
134 return;
135
136 fail:
137 if (sc->sc_ih) {
138 intr_disestablish(sc->sc_ih);
139 sc->sc_ih = NULL;
140 }
141 bus_space_unmap(sc->sc_iot, sc->sc_ioh, aaa->aaa_size);
142 }
143