]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mmc/host/dwmmc_rockchip.c
dwmmc_rockchip: Add support for rk3328-dw-mshc
[FreeBSD/FreeBSD.git] / sys / dev / mmc / host / dwmmc_rockchip.c
1 /*
2  * Copyright 2017 Emmanuel Vadot <manu@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *  1. Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  *  2. Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/bus.h>
34 #include <sys/module.h>
35
36 #include <machine/bus.h>
37
38 #include <dev/mmc/bridge.h>
39
40 #include <dev/ofw/ofw_bus_subr.h>
41
42 #include <dev/mmc/host/dwmmc_var.h>
43
44 enum RKTYPE {
45         RK2928 = 1,
46         RK3328,
47 };
48
49 static struct ofw_compat_data compat_data[] = {
50         {"rockchip,rk2928-dw-mshc",     RK2928},
51         {"rockchip,rk3328-dw-mshc",     RK3328},
52         {NULL,                          0},
53 };
54
55 static int dwmmc_rockchip_update_ios(struct dwmmc_softc *sc, struct mmc_ios *ios);
56
57 static int
58 rockchip_dwmmc_probe(device_t dev)
59 {
60
61         if (!ofw_bus_status_okay(dev))
62                 return (ENXIO);
63
64         if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
65                 return (ENXIO);
66
67         device_set_desc(dev, "Synopsys DesignWare Mobile "
68             "Storage Host Controller (RockChip)");
69
70         return (BUS_PROBE_VENDOR);
71 }
72
73 static int
74 rockchip_dwmmc_attach(device_t dev)
75 {
76         struct dwmmc_softc *sc;
77         int type;
78
79         sc = device_get_softc(dev);
80         sc->hwtype = HWTYPE_ROCKCHIP;
81         type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
82
83         switch (type) {
84         case RK2928:
85                 sc->use_pio = 1;
86                 break;
87         }
88
89         sc->pwren_inverted = 1;
90
91         sc->update_ios = &dwmmc_rockchip_update_ios;
92
93         return (dwmmc_attach(dev));
94 }
95
96 static int
97 dwmmc_rockchip_update_ios(struct dwmmc_softc *sc, struct mmc_ios *ios)
98 {
99         unsigned int clock;
100         int error;
101
102         if (ios->clock && ios->clock != sc->bus_hz) {
103                 sc->bus_hz = clock = ios->clock;
104                 /* Set the MMC clock. */
105                 if (sc->ciu) {
106                         /* 
107                          * Apparently you need to set the ciu clock to
108                          * the double of bus_hz
109                          */
110                         error = clk_set_freq(sc->ciu, clock * 2,
111                             CLK_SET_ROUND_DOWN);
112                         if (error != 0) {
113                                 device_printf(sc->dev,
114                                     "failed to set frequency to %u Hz: %d\n",
115                                     clock, error);
116                                 return (error);
117                         }
118                 }
119         }
120         return (0);
121 }
122
123 static device_method_t rockchip_dwmmc_methods[] = {
124         /* bus interface */
125         DEVMETHOD(device_probe, rockchip_dwmmc_probe),
126         DEVMETHOD(device_attach, rockchip_dwmmc_attach),
127
128         DEVMETHOD_END
129 };
130
131 static devclass_t rockchip_dwmmc_devclass;
132
133 DEFINE_CLASS_1(rockchip_dwmmc, rockchip_dwmmc_driver, rockchip_dwmmc_methods,
134     sizeof(struct dwmmc_softc), dwmmc_driver);
135
136 DRIVER_MODULE(rockchip_dwmmc, simplebus, rockchip_dwmmc_driver,
137     rockchip_dwmmc_devclass, 0, 0);
138 DRIVER_MODULE(rockchip_dwmmc, ofwbus, rockchip_dwmmc_driver,
139     rockchip_dwmmc_devclass, NULL, NULL);
140 #ifndef MMCCAM
141 MMC_DECLARE_BRIDGE(rockchip_dwmmc);
142 #endif