rk_emmcphy.c revision 1.3.4.2 1 1.3.4.2 christos /* $NetBSD: rk_emmcphy.c,v 1.3.4.2 2019/06/10 22:05:55 christos Exp $ */
2 1.3.4.2 christos
3 1.3.4.2 christos /*-
4 1.3.4.2 christos * Copyright (c) 2019 Jared McNeill <jmcneill (at) invisible.ca>
5 1.3.4.2 christos * All rights reserved.
6 1.3.4.2 christos *
7 1.3.4.2 christos * Redistribution and use in source and binary forms, with or without
8 1.3.4.2 christos * modification, are permitted provided that the following conditions
9 1.3.4.2 christos * are met:
10 1.3.4.2 christos * 1. Redistributions of source code must retain the above copyright
11 1.3.4.2 christos * notice, this list of conditions and the following disclaimer.
12 1.3.4.2 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.3.4.2 christos * notice, this list of conditions and the following disclaimer in the
14 1.3.4.2 christos * documentation and/or other materials provided with the distribution.
15 1.3.4.2 christos *
16 1.3.4.2 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.3.4.2 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.3.4.2 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.3.4.2 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.3.4.2 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.3.4.2 christos * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.3.4.2 christos * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.3.4.2 christos * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.3.4.2 christos * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.3.4.2 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.3.4.2 christos * SUCH DAMAGE.
27 1.3.4.2 christos */
28 1.3.4.2 christos
29 1.3.4.2 christos #include <sys/cdefs.h>
30 1.3.4.2 christos __KERNEL_RCSID(0, "$NetBSD: rk_emmcphy.c,v 1.3.4.2 2019/06/10 22:05:55 christos Exp $");
31 1.3.4.2 christos
32 1.3.4.2 christos #include <sys/param.h>
33 1.3.4.2 christos #include <sys/bus.h>
34 1.3.4.2 christos #include <sys/device.h>
35 1.3.4.2 christos #include <sys/intr.h>
36 1.3.4.2 christos #include <sys/systm.h>
37 1.3.4.2 christos #include <sys/mutex.h>
38 1.3.4.2 christos #include <sys/kmem.h>
39 1.3.4.2 christos
40 1.3.4.2 christos #include <dev/fdt/fdtvar.h>
41 1.3.4.2 christos #include <dev/fdt/syscon.h>
42 1.3.4.2 christos
43 1.3.4.2 christos #define GRF_EMMCPHY_CON0 0x00
44 1.3.4.2 christos #define PHYCTRL_FRQSEL __BITS(13,12)
45 1.3.4.2 christos #define PHYCTRL_FRQSEL_200M 0
46 1.3.4.2 christos #define PHYCTRL_FRQSEL_50M 1
47 1.3.4.2 christos #define PHYCTRL_FRQSEL_100M 2
48 1.3.4.2 christos #define PHYCTRL_FRQSEL_150M 3
49 1.3.4.2 christos #define PHYCTRL_OTAPDLYENA __BIT(11)
50 1.3.4.2 christos #define PHYCTRL_OTAPDLYSEL __BITS(10,7)
51 1.3.4.2 christos #define PHYCTRL_ITAPCHGWIN __BIT(6)
52 1.3.4.2 christos #define PHYCTRL_ITAPDLYSEL __BITS(5,1)
53 1.3.4.2 christos #define PHYCTRL_ITAPDLYENA __BIT(0)
54 1.3.4.2 christos #define GRF_EMMCPHY_CON1 0x04
55 1.3.4.2 christos #define PHYCTRL_CLKBUFSEL __BITS(8,6)
56 1.3.4.2 christos #define PHYCTRL_SELDLYTXCLK __BIT(5)
57 1.3.4.2 christos #define PHYCTRL_SELDLYRXCLK __BIT(4)
58 1.3.4.2 christos #define PHYCTRL_STRBSEL __BITS(3,0)
59 1.3.4.2 christos #define GRF_EMMCPHY_CON2 0x08
60 1.3.4.2 christos #define PHYCTRL_REN_STRB __BIT(9)
61 1.3.4.2 christos #define PHYCTRL_REN_CMD __BIT(8)
62 1.3.4.2 christos #define PHYCTRL_REN_DAT __BITS(7,0)
63 1.3.4.2 christos #define GRF_EMMCPHY_CON3 0x0c
64 1.3.4.2 christos #define PHYCTRL_PU_STRB __BIT(9)
65 1.3.4.2 christos #define PHYCTRL_PU_CMD __BIT(8)
66 1.3.4.2 christos #define PHYCTRL_PU_DAT __BITS(7,0)
67 1.3.4.2 christos #define GRF_EMMCPHY_CON4 0x10
68 1.3.4.2 christos #define PHYCTRL_OD_RELEASE_CMD __BIT(9)
69 1.3.4.2 christos #define PHYCTRL_OD_RELEASE_STRB __BIT(8)
70 1.3.4.2 christos #define PHYCTRL_OD_RELEASE_DAT __BITS(7,0)
71 1.3.4.2 christos #define GRF_EMMCPHY_CON5 0x14
72 1.3.4.2 christos #define PHYCTRL_ODEN_STRB __BIT(9)
73 1.3.4.2 christos #define PHYCTRL_ODEN_CMD __BIT(8)
74 1.3.4.2 christos #define PHYCTRL_ODEN_DAT __BITS(7,0)
75 1.3.4.2 christos #define GRF_EMMCPHY_CON6 0x18
76 1.3.4.2 christos #define PHYCTRL_DLL_TRM_ICP __BITS(12,9)
77 1.3.4.2 christos #define PHYCTRL_EN_RTRIM __BIT(8)
78 1.3.4.2 christos #define PHYCTRL_RETRIM __BIT(7)
79 1.3.4.2 christos #define PHYCTRL_DR_TY __BITS(6,4)
80 1.3.4.2 christos #define PHYCTRL_RETENB __BIT(3)
81 1.3.4.2 christos #define PHYCTRL_RETEN __BIT(2)
82 1.3.4.2 christos #define PHYCTRL_ENDLL __BIT(1)
83 1.3.4.2 christos #define PHYCTRL_PDB __BIT(0)
84 1.3.4.2 christos #define GRF_EMMCPHY_STATUS 0x20
85 1.3.4.2 christos #define PHYCTRL_CALDONE __BIT(6)
86 1.3.4.2 christos #define PHYCTRL_DLLRDY __BIT(5)
87 1.3.4.2 christos #define PHYCTRL_RTRIM __BITS(4,1)
88 1.3.4.2 christos #define PHYCTRL_EXR_NINST __BIT(0)
89 1.3.4.2 christos
90 1.3.4.2 christos static const char * const compatible[] = {
91 1.3.4.2 christos "rockchip,rk3399-emmc-phy",
92 1.3.4.2 christos NULL
93 1.3.4.2 christos };
94 1.3.4.2 christos
95 1.3.4.2 christos struct rk_emmcphy_softc {
96 1.3.4.2 christos device_t sc_dev;
97 1.3.4.2 christos struct syscon *sc_syscon;
98 1.3.4.2 christos bus_addr_t sc_regbase;
99 1.3.4.2 christos int sc_phandle;
100 1.3.4.2 christos struct clk *sc_clk;
101 1.3.4.2 christos };
102 1.3.4.2 christos
103 1.3.4.2 christos #define RD4(sc, reg) \
104 1.3.4.2 christos syscon_read_4((sc)->sc_syscon, (sc)->sc_regbase + (reg))
105 1.3.4.2 christos #define WR4(sc, reg, val) \
106 1.3.4.2 christos syscon_write_4((sc)->sc_syscon, (sc)->sc_regbase + (reg), (val))
107 1.3.4.2 christos
108 1.3.4.2 christos static int rk_emmcphy_match(device_t, cfdata_t, void *);
109 1.3.4.2 christos static void rk_emmcphy_attach(device_t, device_t, void *);
110 1.3.4.2 christos
111 1.3.4.2 christos CFATTACH_DECL_NEW(rkemmcphy, sizeof(struct rk_emmcphy_softc),
112 1.3.4.2 christos rk_emmcphy_match, rk_emmcphy_attach, NULL, NULL);
113 1.3.4.2 christos
114 1.3.4.2 christos static void *
115 1.3.4.2 christos rk_emmcphy_acquire(device_t dev, const void *data, size_t len)
116 1.3.4.2 christos {
117 1.3.4.2 christos struct rk_emmcphy_softc * const sc = device_private(dev);
118 1.3.4.2 christos
119 1.3.4.2 christos if (len != 0)
120 1.3.4.2 christos return NULL;
121 1.3.4.2 christos
122 1.3.4.2 christos if (sc->sc_clk == NULL)
123 1.3.4.2 christos sc->sc_clk = fdtbus_clock_get(sc->sc_phandle, "emmcclk");
124 1.3.4.2 christos
125 1.3.4.2 christos return sc;
126 1.3.4.2 christos }
127 1.3.4.2 christos
128 1.3.4.2 christos static void
129 1.3.4.2 christos rk_emmcphy_release(device_t dev, void *priv)
130 1.3.4.2 christos {
131 1.3.4.2 christos }
132 1.3.4.2 christos
133 1.3.4.2 christos static int
134 1.3.4.2 christos rk_emmcphy_enable(device_t dev, void *priv, bool enable)
135 1.3.4.2 christos {
136 1.3.4.2 christos struct rk_emmcphy_softc * const sc = device_private(dev);
137 1.3.4.2 christos uint32_t mask, val;
138 1.3.4.2 christos u_int rate, frqsel;
139 1.3.4.2 christos
140 1.3.4.2 christos syscon_lock(sc->sc_syscon);
141 1.3.4.2 christos
142 1.3.4.2 christos if (enable) {
143 1.3.4.2 christos /* Drive strength */
144 1.3.4.2 christos mask = PHYCTRL_DR_TY;
145 1.3.4.2 christos val = __SHIFTIN(0, PHYCTRL_DR_TY);
146 1.3.4.2 christos WR4(sc, GRF_EMMCPHY_CON6, (mask << 16) | val);
147 1.3.4.2 christos
148 1.3.4.2 christos /* Enable output tap delay */
149 1.3.4.2 christos mask = PHYCTRL_OTAPDLYENA | PHYCTRL_OTAPDLYSEL;
150 1.3.4.2 christos val = PHYCTRL_OTAPDLYENA | __SHIFTIN(4, PHYCTRL_OTAPDLYSEL);
151 1.3.4.2 christos WR4(sc, GRF_EMMCPHY_CON0, (mask << 16) | val);
152 1.3.4.2 christos }
153 1.3.4.2 christos
154 1.3.4.2 christos /* Power down PHY and disable DLL before making changes */
155 1.3.4.2 christos mask = PHYCTRL_ENDLL | PHYCTRL_PDB;
156 1.3.4.2 christos val = 0;
157 1.3.4.2 christos WR4(sc, GRF_EMMCPHY_CON6, (mask << 16) | val);
158 1.3.4.2 christos
159 1.3.4.2 christos if (enable == false) {
160 1.3.4.2 christos syscon_unlock(sc->sc_syscon);
161 1.3.4.2 christos return 0;
162 1.3.4.2 christos }
163 1.3.4.2 christos
164 1.3.4.2 christos rate = sc->sc_clk ? clk_get_rate(sc->sc_clk) : 0;
165 1.3.4.2 christos
166 1.3.4.2 christos if (rate != 0) {
167 1.3.4.2 christos if (rate < 75000000)
168 1.3.4.2 christos frqsel = PHYCTRL_FRQSEL_50M;
169 1.3.4.2 christos else if (rate < 125000000)
170 1.3.4.2 christos frqsel = PHYCTRL_FRQSEL_100M;
171 1.3.4.2 christos else if (rate < 175000000)
172 1.3.4.2 christos frqsel = PHYCTRL_FRQSEL_150M;
173 1.3.4.2 christos else
174 1.3.4.2 christos frqsel = PHYCTRL_FRQSEL_200M;
175 1.3.4.2 christos } else {
176 1.3.4.2 christos frqsel = PHYCTRL_FRQSEL_200M;
177 1.3.4.2 christos }
178 1.3.4.2 christos
179 1.3.4.2 christos delay(3);
180 1.3.4.2 christos
181 1.3.4.2 christos /* Power up PHY */
182 1.3.4.2 christos mask = PHYCTRL_PDB;
183 1.3.4.2 christos val = PHYCTRL_PDB;
184 1.3.4.2 christos WR4(sc, GRF_EMMCPHY_CON6, (mask << 16) | val);
185 1.3.4.2 christos
186 1.3.4.2 christos /* Wait for calibration */
187 1.3.4.2 christos delay(10);
188 1.3.4.2 christos val = RD4(sc, GRF_EMMCPHY_STATUS);
189 1.3.4.2 christos if ((val & PHYCTRL_CALDONE) == 0) {
190 1.3.4.2 christos device_printf(dev, "PHY calibration did not complete\n");
191 1.3.4.2 christos syscon_unlock(sc->sc_syscon);
192 1.3.4.2 christos return EIO;
193 1.3.4.2 christos }
194 1.3.4.2 christos
195 1.3.4.2 christos /* Set DLL frequency */
196 1.3.4.2 christos mask = PHYCTRL_FRQSEL;
197 1.3.4.2 christos val = __SHIFTIN(frqsel, PHYCTRL_FRQSEL);
198 1.3.4.2 christos WR4(sc, GRF_EMMCPHY_CON0, (mask << 16) | val);
199 1.3.4.2 christos
200 1.3.4.2 christos /* Enable DLL */
201 1.3.4.2 christos mask = PHYCTRL_ENDLL;
202 1.3.4.2 christos val = PHYCTRL_ENDLL;
203 1.3.4.2 christos WR4(sc, GRF_EMMCPHY_CON6, (mask << 16) | val);
204 1.3.4.2 christos
205 1.3.4.2 christos if (rate != 0) {
206 1.3.4.2 christos /* Wait for DLL ready */
207 1.3.4.2 christos delay(50000);
208 1.3.4.2 christos val = RD4(sc, GRF_EMMCPHY_STATUS);
209 1.3.4.2 christos if ((val & PHYCTRL_DLLRDY) == 0) {
210 1.3.4.2 christos device_printf(dev, "DLL loop failed to lock\n");
211 1.3.4.2 christos syscon_unlock(sc->sc_syscon);
212 1.3.4.2 christos return EIO;
213 1.3.4.2 christos }
214 1.3.4.2 christos }
215 1.3.4.2 christos
216 1.3.4.2 christos syscon_unlock(sc->sc_syscon);
217 1.3.4.2 christos
218 1.3.4.2 christos return 0;
219 1.3.4.2 christos }
220 1.3.4.2 christos
221 1.3.4.2 christos static const struct fdtbus_phy_controller_func rk_emmcphy_funcs = {
222 1.3.4.2 christos .acquire = rk_emmcphy_acquire,
223 1.3.4.2 christos .release = rk_emmcphy_release,
224 1.3.4.2 christos .enable = rk_emmcphy_enable,
225 1.3.4.2 christos };
226 1.3.4.2 christos
227 1.3.4.2 christos static int
228 1.3.4.2 christos rk_emmcphy_match(device_t parent, cfdata_t cf, void *aux)
229 1.3.4.2 christos {
230 1.3.4.2 christos struct fdt_attach_args * const faa = aux;
231 1.3.4.2 christos
232 1.3.4.2 christos return of_match_compatible(faa->faa_phandle, compatible);
233 1.3.4.2 christos }
234 1.3.4.2 christos
235 1.3.4.2 christos static void
236 1.3.4.2 christos rk_emmcphy_attach(device_t parent, device_t self, void *aux)
237 1.3.4.2 christos {
238 1.3.4.2 christos struct rk_emmcphy_softc * const sc = device_private(self);
239 1.3.4.2 christos struct fdt_attach_args * const faa = aux;
240 1.3.4.2 christos const int phandle = faa->faa_phandle;
241 1.3.4.2 christos bus_addr_t addr;
242 1.3.4.2 christos bus_size_t size;
243 1.3.4.2 christos
244 1.3.4.2 christos if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
245 1.3.4.2 christos aprint_error(": couldn't get registers\n");
246 1.3.4.2 christos return;
247 1.3.4.2 christos }
248 1.3.4.2 christos
249 1.3.4.2 christos sc->sc_dev = self;
250 1.3.4.2 christos sc->sc_phandle = phandle;
251 1.3.4.2 christos sc->sc_syscon = fdtbus_syscon_lookup(OF_parent(phandle));
252 1.3.4.2 christos if (sc->sc_syscon == NULL) {
253 1.3.4.2 christos aprint_error(": couldn't get syscon\n");
254 1.3.4.2 christos return;
255 1.3.4.2 christos }
256 1.3.4.2 christos sc->sc_regbase = addr;
257 1.3.4.2 christos
258 1.3.4.2 christos aprint_naive("\n");
259 1.3.4.2 christos aprint_normal(": eMMC PHY\n");
260 1.3.4.2 christos
261 1.3.4.2 christos fdtbus_register_phy_controller(self, phandle, &rk_emmcphy_funcs);
262 1.3.4.2 christos }
263