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