dwcmmc_fdt.c revision 1.2 1 1.2 jmcneill /* $NetBSD: dwcmmc_fdt.c,v 1.2 2018/06/19 22:44:33 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2015-2018 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.2 jmcneill __KERNEL_RCSID(0, "$NetBSD: dwcmmc_fdt.c,v 1.2 2018/06/19 22:44:33 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/intr.h>
36 1.1 jmcneill #include <sys/systm.h>
37 1.1 jmcneill #include <sys/kernel.h>
38 1.1 jmcneill #include <sys/mutex.h>
39 1.1 jmcneill #include <sys/condvar.h>
40 1.1 jmcneill #include <sys/gpio.h>
41 1.1 jmcneill
42 1.1 jmcneill #include <dev/ic/dwc_mmc_var.h>
43 1.1 jmcneill #include <dev/fdt/fdtvar.h>
44 1.1 jmcneill
45 1.1 jmcneill static int dwcmmc_fdt_match(device_t, cfdata_t, void *);
46 1.1 jmcneill static void dwcmmc_fdt_attach(device_t, device_t, void *);
47 1.1 jmcneill
48 1.1 jmcneill static int dwcmmc_fdt_card_detect(struct dwc_mmc_softc *);
49 1.2 jmcneill static int dwcmmc_fdt_bus_clock(struct dwc_mmc_softc *, int);
50 1.1 jmcneill
51 1.1 jmcneill struct dwcmmc_fdt_config {
52 1.1 jmcneill u_int ciu_div;
53 1.1 jmcneill };
54 1.1 jmcneill
55 1.1 jmcneill static const struct dwcmmc_fdt_config dwcmmc_rk3328_config = {
56 1.1 jmcneill .ciu_div = 2,
57 1.1 jmcneill };
58 1.1 jmcneill
59 1.1 jmcneill static const struct of_compat_data compat_data[] = {
60 1.1 jmcneill { "rockchip,rk3328-dw-mshc", (uintptr_t)&dwcmmc_rk3328_config },
61 1.1 jmcneill { NULL }
62 1.1 jmcneill };
63 1.1 jmcneill
64 1.1 jmcneill struct dwcmmc_fdt_softc {
65 1.1 jmcneill struct dwc_mmc_softc sc;
66 1.1 jmcneill struct clk *sc_clk_biu;
67 1.1 jmcneill struct clk *sc_clk_ciu;
68 1.1 jmcneill struct fdtbus_gpio_pin *sc_pin_cd;
69 1.1 jmcneill const struct dwcmmc_fdt_config *sc_conf;
70 1.2 jmcneill u_int sc_ciu_div;
71 1.1 jmcneill };
72 1.1 jmcneill
73 1.1 jmcneill CFATTACH_DECL_NEW(dwcmmc_fdt, sizeof(struct dwc_mmc_softc),
74 1.1 jmcneill dwcmmc_fdt_match, dwcmmc_fdt_attach, NULL, NULL);
75 1.1 jmcneill
76 1.1 jmcneill static int
77 1.1 jmcneill dwcmmc_fdt_match(device_t parent, cfdata_t cf, void *aux)
78 1.1 jmcneill {
79 1.1 jmcneill struct fdt_attach_args * const faa = aux;
80 1.1 jmcneill
81 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
82 1.1 jmcneill }
83 1.1 jmcneill
84 1.1 jmcneill static void
85 1.1 jmcneill dwcmmc_fdt_attach(device_t parent, device_t self, void *aux)
86 1.1 jmcneill {
87 1.1 jmcneill struct dwcmmc_fdt_softc *esc = device_private(self);
88 1.1 jmcneill struct dwc_mmc_softc *sc = &esc->sc;
89 1.1 jmcneill struct fdt_attach_args * const faa = aux;
90 1.1 jmcneill const int phandle = faa->faa_phandle;
91 1.2 jmcneill u_int fifo_depth, max_freq;
92 1.1 jmcneill char intrstr[128];
93 1.1 jmcneill bus_addr_t addr;
94 1.1 jmcneill bus_size_t size;
95 1.1 jmcneill int error;
96 1.1 jmcneill
97 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
98 1.1 jmcneill aprint_error(": couldn't get registers\n");
99 1.1 jmcneill return;
100 1.1 jmcneill }
101 1.1 jmcneill
102 1.1 jmcneill if (of_getprop_uint32(phandle, "fifo-depth", &fifo_depth))
103 1.1 jmcneill fifo_depth = 0;
104 1.1 jmcneill
105 1.1 jmcneill esc->sc_clk_biu = fdtbus_clock_get(phandle, "biu");
106 1.1 jmcneill if (esc->sc_clk_biu == NULL) {
107 1.1 jmcneill aprint_error(": couldn't get clock biu\n");
108 1.1 jmcneill return;
109 1.1 jmcneill }
110 1.1 jmcneill esc->sc_clk_ciu = fdtbus_clock_get(phandle, "ciu");
111 1.1 jmcneill if (esc->sc_clk_ciu == NULL) {
112 1.1 jmcneill aprint_error(": couldn't get clock ciu\n");
113 1.1 jmcneill return;
114 1.1 jmcneill }
115 1.1 jmcneill
116 1.1 jmcneill error = clk_enable(esc->sc_clk_biu);
117 1.1 jmcneill if (error) {
118 1.1 jmcneill aprint_error(": couldn't enable clock biu: %d\n", error);
119 1.1 jmcneill return;
120 1.1 jmcneill }
121 1.1 jmcneill error = clk_enable(esc->sc_clk_ciu);
122 1.1 jmcneill if (error) {
123 1.1 jmcneill aprint_error(": couldn't enable clock ciu: %d\n", error);
124 1.1 jmcneill return;
125 1.1 jmcneill }
126 1.1 jmcneill
127 1.1 jmcneill sc->sc_dev = self;
128 1.1 jmcneill sc->sc_bst = faa->faa_bst;
129 1.1 jmcneill sc->sc_dmat = faa->faa_dmat;
130 1.1 jmcneill error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
131 1.1 jmcneill if (error) {
132 1.1 jmcneill aprint_error(": couldn't map %#llx: %d\n",
133 1.1 jmcneill (uint64_t)addr, error);
134 1.1 jmcneill return;
135 1.1 jmcneill }
136 1.1 jmcneill esc->sc_conf = of_search_compatible(phandle, compat_data)->data;
137 1.1 jmcneill
138 1.1 jmcneill const u_int ciu_div = esc->sc_conf->ciu_div > 0 ? esc->sc_conf->ciu_div : 1;
139 1.1 jmcneill
140 1.2 jmcneill if (of_getprop_uint32(phandle, "max-frequency", &max_freq) == 0)
141 1.2 jmcneill sc->sc_clock_freq = max_freq;
142 1.2 jmcneill else
143 1.2 jmcneill sc->sc_clock_freq = clk_get_rate(esc->sc_clk_ciu) / ciu_div;
144 1.2 jmcneill
145 1.1 jmcneill sc->sc_fifo_depth = fifo_depth;
146 1.1 jmcneill sc->sc_flags = DWC_MMC_F_USE_HOLD_REG | DWC_MMC_F_DMA;
147 1.2 jmcneill sc->sc_bus_clock = dwcmmc_fdt_bus_clock;
148 1.1 jmcneill
149 1.1 jmcneill esc->sc_pin_cd = fdtbus_gpio_acquire(phandle, "cd-gpios",
150 1.1 jmcneill GPIO_PIN_INPUT);
151 1.1 jmcneill if (esc->sc_pin_cd)
152 1.1 jmcneill sc->sc_card_detect = dwcmmc_fdt_card_detect;
153 1.1 jmcneill
154 1.1 jmcneill aprint_naive("\n");
155 1.2 jmcneill aprint_normal(": DesignWare SD/MMC\n");
156 1.1 jmcneill
157 1.1 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
158 1.1 jmcneill aprint_error_dev(self, "failed to decode interrupt\n");
159 1.1 jmcneill return;
160 1.1 jmcneill }
161 1.1 jmcneill
162 1.1 jmcneill if (dwc_mmc_init(sc) != 0)
163 1.1 jmcneill return;
164 1.1 jmcneill
165 1.1 jmcneill sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_BIO, 0,
166 1.1 jmcneill dwc_mmc_intr, sc);
167 1.1 jmcneill if (sc->sc_ih == NULL) {
168 1.1 jmcneill aprint_error_dev(self, "couldn't establish interrupt on %s\n",
169 1.1 jmcneill intrstr);
170 1.1 jmcneill return;
171 1.1 jmcneill }
172 1.1 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
173 1.1 jmcneill }
174 1.1 jmcneill
175 1.1 jmcneill static int
176 1.1 jmcneill dwcmmc_fdt_card_detect(struct dwc_mmc_softc *sc)
177 1.1 jmcneill {
178 1.1 jmcneill struct dwcmmc_fdt_softc *esc = device_private(sc->sc_dev);
179 1.1 jmcneill
180 1.1 jmcneill KASSERT(esc->sc_pin_cd != NULL);
181 1.1 jmcneill
182 1.1 jmcneill return fdtbus_gpio_read(esc->sc_pin_cd);
183 1.1 jmcneill }
184 1.2 jmcneill
185 1.2 jmcneill static int
186 1.2 jmcneill dwcmmc_fdt_bus_clock(struct dwc_mmc_softc *sc, int rate)
187 1.2 jmcneill {
188 1.2 jmcneill struct dwcmmc_fdt_softc *esc = device_private(sc->sc_dev);
189 1.2 jmcneill const u_int ciu_div = esc->sc_conf->ciu_div > 0 ? esc->sc_conf->ciu_div : 1;
190 1.2 jmcneill int error;
191 1.2 jmcneill
192 1.2 jmcneill error = clk_set_rate(esc->sc_clk_ciu, 1000 * rate * ciu_div);
193 1.2 jmcneill if (error != 0) {
194 1.2 jmcneill aprint_error_dev(sc->sc_dev, "failed to set rate to %u kHz: %d\n",
195 1.2 jmcneill rate * ciu_div, error);
196 1.2 jmcneill return error;
197 1.2 jmcneill }
198 1.2 jmcneill
199 1.2 jmcneill sc->sc_clock_freq = (clk_get_rate(esc->sc_clk_ciu) / ciu_div) / 1000;
200 1.2 jmcneill
201 1.2 jmcneill aprint_debug_dev(sc->sc_dev, "set clock rate to %u kHz (target %u kHz)\n",
202 1.2 jmcneill sc->sc_clock_freq, rate);
203 1.2 jmcneill
204 1.2 jmcneill return 0;
205 1.2 jmcneill }
206