| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: tegra_sdhc.c,v 1.2 2015/05/02 14:10:03 jmcneill Exp $ */ | | 1 | /* $NetBSD: tegra_sdhc.c,v 1.3 2015/05/02 17:07:55 jmcneill Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca> | | 4 | * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca> |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
| @@ -19,94 +19,119 @@ | | | @@ -19,94 +19,119 @@ |
19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | | 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | | 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | | 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | | 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
25 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 25 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
26 | * SUCH DAMAGE. | | 26 | * SUCH DAMAGE. |
27 | */ | | 27 | */ |
28 | | | 28 | |
29 | #include "locators.h" | | 29 | #include "locators.h" |
30 | | | 30 | |
31 | #include <sys/cdefs.h> | | 31 | #include <sys/cdefs.h> |
32 | __KERNEL_RCSID(0, "$NetBSD: tegra_sdhc.c,v 1.2 2015/05/02 14:10:03 jmcneill Exp $"); | | 32 | __KERNEL_RCSID(0, "$NetBSD: tegra_sdhc.c,v 1.3 2015/05/02 17:07:55 jmcneill Exp $"); |
33 | | | 33 | |
34 | #include <sys/param.h> | | 34 | #include <sys/param.h> |
35 | #include <sys/bus.h> | | 35 | #include <sys/bus.h> |
36 | #include <sys/device.h> | | 36 | #include <sys/device.h> |
37 | #include <sys/intr.h> | | 37 | #include <sys/intr.h> |
38 | #include <sys/systm.h> | | 38 | #include <sys/systm.h> |
39 | #include <sys/kernel.h> | | 39 | #include <sys/kernel.h> |
40 | | | 40 | |
41 | #include <dev/sdmmc/sdhcreg.h> | | 41 | #include <dev/sdmmc/sdhcreg.h> |
42 | #include <dev/sdmmc/sdhcvar.h> | | 42 | #include <dev/sdmmc/sdhcvar.h> |
43 | #include <dev/sdmmc/sdmmcvar.h> | | 43 | #include <dev/sdmmc/sdmmcvar.h> |
44 | | | 44 | |
45 | #include <arm/nvidia/tegra_var.h> | | 45 | #include <arm/nvidia/tegra_var.h> |
46 | | | 46 | |
47 | /* 8-bit eMMC is supported on SDMMC2 and SDMMC4 */ | | 47 | /* 8-bit eMMC is supported on SDMMC2 and SDMMC4 */ |
48 | #define SDMMC_8BIT_P(port) ((port) == 1 || (port) == 3) | | 48 | #define SDMMC_8BIT_P(port) ((port) == 1 || (port) == 3) |
49 | | | 49 | |
50 | static int tegra_sdhc_match(device_t, cfdata_t, void *); | | 50 | static int tegra_sdhc_match(device_t, cfdata_t, void *); |
51 | static void tegra_sdhc_attach(device_t, device_t, void *); | | 51 | static void tegra_sdhc_attach(device_t, device_t, void *); |
52 | | | 52 | |
| | | 53 | static int tegra_sdhc_card_detect(struct sdhc_softc *); |
| | | 54 | static int tegra_sdhc_write_protect(struct sdhc_softc *); |
| | | 55 | |
53 | struct tegra_sdhc_softc { | | 56 | struct tegra_sdhc_softc { |
54 | struct sdhc_softc sc; | | 57 | struct sdhc_softc sc; |
55 | | | 58 | |
56 | u_int sc_port; | | 59 | u_int sc_port; |
57 | | | 60 | |
58 | bus_space_tag_t sc_bst; | | 61 | bus_space_tag_t sc_bst; |
59 | bus_space_handle_t sc_bsh; | | 62 | bus_space_handle_t sc_bsh; |
60 | bus_size_t sc_bsz; | | 63 | bus_size_t sc_bsz; |
61 | struct sdhc_host *sc_host; | | 64 | struct sdhc_host *sc_host; |
62 | void *sc_ih; | | 65 | void *sc_ih; |
| | | 66 | |
| | | 67 | struct tegra_gpio_pin *sc_pin_cd; |
| | | 68 | struct tegra_gpio_pin *sc_pin_power; |
| | | 69 | struct tegra_gpio_pin *sc_pin_wp; |
63 | }; | | 70 | }; |
64 | | | 71 | |
65 | CFATTACH_DECL_NEW(tegra_sdhc, sizeof(struct tegra_sdhc_softc), | | 72 | CFATTACH_DECL_NEW(tegra_sdhc, sizeof(struct tegra_sdhc_softc), |
66 | tegra_sdhc_match, tegra_sdhc_attach, NULL, NULL); | | 73 | tegra_sdhc_match, tegra_sdhc_attach, NULL, NULL); |
67 | | | 74 | |
68 | static int | | 75 | static int |
69 | tegra_sdhc_match(device_t parent, cfdata_t cf, void *aux) | | 76 | tegra_sdhc_match(device_t parent, cfdata_t cf, void *aux) |
70 | { | | 77 | { |
71 | return 1; | | 78 | return 1; |
72 | } | | 79 | } |
73 | | | 80 | |
74 | static void | | 81 | static void |
75 | tegra_sdhc_attach(device_t parent, device_t self, void *aux) | | 82 | tegra_sdhc_attach(device_t parent, device_t self, void *aux) |
76 | { | | 83 | { |
77 | struct tegra_sdhc_softc * const sc = device_private(self); | | 84 | struct tegra_sdhc_softc * const sc = device_private(self); |
78 | struct tegraio_attach_args * const tio = aux; | | 85 | struct tegraio_attach_args * const tio = aux; |
79 | const struct tegra_locators * const loc = &tio->tio_loc; | | 86 | const struct tegra_locators * const loc = &tio->tio_loc; |
| | | 87 | prop_dictionary_t prop = device_properties(self); |
| | | 88 | const char *pin; |
80 | int error; | | 89 | int error; |
81 | | | 90 | |
82 | sc->sc.sc_dev = self; | | 91 | sc->sc.sc_dev = self; |
83 | sc->sc.sc_dmat = tio->tio_dmat; | | 92 | sc->sc.sc_dmat = tio->tio_dmat; |
84 | sc->sc.sc_flags = SDHC_FLAG_32BIT_ACCESS | | | 93 | sc->sc.sc_flags = SDHC_FLAG_32BIT_ACCESS | |
85 | SDHC_FLAG_NO_PWR0 | | | 94 | SDHC_FLAG_NO_PWR0 | |
86 | SDHC_FLAG_NO_HS_BIT | | | 95 | SDHC_FLAG_NO_HS_BIT | |
87 | SDHC_FLAG_NO_CLKBASE | | | 96 | SDHC_FLAG_NO_CLKBASE | |
88 | SDHC_FLAG_USE_DMA; | | 97 | SDHC_FLAG_USE_DMA; |
89 | if (SDMMC_8BIT_P(loc->loc_port)) { | | 98 | if (SDMMC_8BIT_P(loc->loc_port)) { |
90 | sc->sc.sc_flags |= SDHC_FLAG_8BIT_MODE; | | 99 | sc->sc.sc_flags |= SDHC_FLAG_8BIT_MODE; |
91 | } | | 100 | } |
92 | sc->sc.sc_host = &sc->sc_host; | | 101 | sc->sc.sc_host = &sc->sc_host; |
93 | | | 102 | |
94 | sc->sc_bst = tio->tio_bst; | | 103 | sc->sc_bst = tio->tio_bst; |
95 | bus_space_subregion(tio->tio_bst, tio->tio_bsh, | | 104 | bus_space_subregion(tio->tio_bst, tio->tio_bsh, |
96 | loc->loc_offset, loc->loc_size, &sc->sc_bsh); | | 105 | loc->loc_offset, loc->loc_size, &sc->sc_bsh); |
97 | sc->sc_bsz = loc->loc_size; | | 106 | sc->sc_bsz = loc->loc_size; |
98 | sc->sc_port = loc->loc_port; | | 107 | sc->sc_port = loc->loc_port; |
99 | | | 108 | |
| | | 109 | if (prop_dictionary_get_cstring_nocopy(prop, "power-gpio", &pin)) { |
| | | 110 | sc->sc_pin_power = tegra_gpio_acquire(pin, GPIO_PIN_OUTPUT); |
| | | 111 | if (sc->sc_pin_power) |
| | | 112 | tegra_gpio_write(sc->sc_pin_power, 1); |
| | | 113 | } |
| | | 114 | |
| | | 115 | if (prop_dictionary_get_cstring_nocopy(prop, "cd-gpio", &pin)) |
| | | 116 | sc->sc_pin_cd = tegra_gpio_acquire(pin, GPIO_PIN_INPUT); |
| | | 117 | if (prop_dictionary_get_cstring_nocopy(prop, "wp-gpio", &pin)) |
| | | 118 | sc->sc_pin_wp = tegra_gpio_acquire(pin, GPIO_PIN_INPUT); |
| | | 119 | |
| | | 120 | if (sc->sc_pin_cd) |
| | | 121 | sc->sc.sc_vendor_card_detect = tegra_sdhc_card_detect; |
| | | 122 | if (sc->sc_pin_wp) |
| | | 123 | sc->sc.sc_vendor_write_protect = tegra_sdhc_write_protect; |
| | | 124 | |
100 | /* | | 125 | /* |
101 | * The controller supports SDR104 speeds (208 MHz). With PLLP (408 Mhz) | | 126 | * The controller supports SDR104 speeds (208 MHz). With PLLP (408 Mhz) |
102 | * as input and div=2 we can get a reasonable 204 MHz for the SDHC. | | 127 | * as input and div=2 we can get a reasonable 204 MHz for the SDHC. |
103 | */ | | 128 | */ |
104 | const u_int div = howmany(tegra_car_pllp0_rate() / 1000, 208000); | | 129 | const u_int div = howmany(tegra_car_pllp0_rate() / 1000, 208000); |
105 | tegra_car_periph_sdmmc_set_div(sc->sc_port, div); | | 130 | tegra_car_periph_sdmmc_set_div(sc->sc_port, div); |
106 | sc->sc.sc_clkbase = tegra_car_periph_sdmmc_rate(sc->sc_port) / 1000; | | 131 | sc->sc.sc_clkbase = tegra_car_periph_sdmmc_rate(sc->sc_port) / 1000; |
107 | | | 132 | |
108 | aprint_naive("\n"); | | 133 | aprint_naive("\n"); |
109 | aprint_normal(": SDMMC%d\n", loc->loc_port + 1); | | 134 | aprint_normal(": SDMMC%d\n", loc->loc_port + 1); |
110 | | | 135 | |
111 | if (sc->sc.sc_clkbase == 0) { | | 136 | if (sc->sc.sc_clkbase == 0) { |
112 | aprint_error_dev(self, "couldn't determine frequency\n"); | | 137 | aprint_error_dev(self, "couldn't determine frequency\n"); |
| @@ -121,13 +146,33 @@ tegra_sdhc_attach(device_t parent, devic | | | @@ -121,13 +146,33 @@ tegra_sdhc_attach(device_t parent, devic |
121 | return; | | 146 | return; |
122 | } | | 147 | } |
123 | aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intr); | | 148 | aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intr); |
124 | | | 149 | |
125 | error = sdhc_host_found(&sc->sc, sc->sc_bst, sc->sc_bsh, sc->sc_bsz); | | 150 | error = sdhc_host_found(&sc->sc, sc->sc_bst, sc->sc_bsh, sc->sc_bsz); |
126 | if (error) { | | 151 | if (error) { |
127 | aprint_error_dev(self, "couldn't initialize host, error = %d\n", | | 152 | aprint_error_dev(self, "couldn't initialize host, error = %d\n", |
128 | error); | | 153 | error); |
129 | intr_disestablish(sc->sc_ih); | | 154 | intr_disestablish(sc->sc_ih); |
130 | sc->sc_ih = NULL; | | 155 | sc->sc_ih = NULL; |
131 | return; | | 156 | return; |
132 | } | | 157 | } |
133 | } | | 158 | } |
| | | 159 | |
| | | 160 | static int |
| | | 161 | tegra_sdhc_card_detect(struct sdhc_softc *ssc) |
| | | 162 | { |
| | | 163 | struct tegra_sdhc_softc *sc = device_private(ssc->sc_dev); |
| | | 164 | |
| | | 165 | KASSERT(sc->sc_pin_cd != NULL); |
| | | 166 | |
| | | 167 | return !tegra_gpio_read(sc->sc_pin_cd); |
| | | 168 | } |
| | | 169 | |
| | | 170 | static int |
| | | 171 | tegra_sdhc_write_protect(struct sdhc_softc *ssc) |
| | | 172 | { |
| | | 173 | struct tegra_sdhc_softc *sc = device_private(ssc->sc_dev); |
| | | 174 | |
| | | 175 | KASSERT(sc->sc_pin_wp != NULL); |
| | | 176 | |
| | | 177 | return tegra_gpio_read(sc->sc_pin_wp); |
| | | 178 | } |