sunxi_hdmiphy.c revision 1.4 1 1.4 jmcneill /* $NetBSD: sunxi_hdmiphy.c,v 1.4 2019/11/23 18:54:26 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2019 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.1 jmcneill
31 1.4 jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_hdmiphy.c,v 1.4 2019/11/23 18:54:26 jmcneill Exp $");
32 1.1 jmcneill
33 1.1 jmcneill #include <sys/param.h>
34 1.1 jmcneill #include <sys/bus.h>
35 1.1 jmcneill #include <sys/device.h>
36 1.1 jmcneill #include <sys/intr.h>
37 1.1 jmcneill #include <sys/systm.h>
38 1.1 jmcneill
39 1.1 jmcneill #include <dev/fdt/fdtvar.h>
40 1.1 jmcneill
41 1.1 jmcneill #include <arm/sunxi/sunxi_hdmiphy.h>
42 1.1 jmcneill
43 1.1 jmcneill #define DBG_CTRL 0x000
44 1.1 jmcneill #define DBG_CTRL_POL __BITS(15,8)
45 1.1 jmcneill #define DBG_CTRL_POL_NVSYNC 1
46 1.1 jmcneill #define DBG_CTRL_POL_NHSYNC 2
47 1.1 jmcneill
48 1.1 jmcneill #define READ_EN 0x010
49 1.1 jmcneill #define READ_EN_MAGIC 0x54524545 /* "TREE" */
50 1.1 jmcneill
51 1.1 jmcneill #define UNSCRAMBLE 0x014
52 1.1 jmcneill #define UNSCRAMBLE_MAGIC 0x42494E47 /* "BING" */
53 1.1 jmcneill
54 1.1 jmcneill #define ANA_CFG1 0x020
55 1.1 jmcneill #define ANA_CFG1_ENRCAL __BIT(19)
56 1.1 jmcneill #define ANA_CFG1_ENCALOG __BIT(18)
57 1.1 jmcneill #define ANA_CFG1_TMDSCLK_EN __BIT(16)
58 1.1 jmcneill #define ANA_CFG1_TXEN __BITS(15,12)
59 1.1 jmcneill #define ANA_CFG1_BIASEN __BITS(11,8)
60 1.1 jmcneill #define ANA_CFG1_ENP2S __BITS(7,4)
61 1.1 jmcneill #define ANA_CFG1_CKEN __BIT(3)
62 1.1 jmcneill #define ANA_CFG1_LDOEN __BIT(2)
63 1.1 jmcneill #define ANA_CFG1_ENVBS __BIT(1)
64 1.1 jmcneill #define ANA_CFG1_ENBI __BIT(0)
65 1.1 jmcneill
66 1.1 jmcneill #define ANA_CFG2 0x024
67 1.1 jmcneill #define ANA_CFG2_REG_RESDI __BITS(5,0)
68 1.1 jmcneill
69 1.1 jmcneill #define ANA_CFG3 0x028
70 1.1 jmcneill #define ANA_CFG3_REG_SDAEN __BIT(2)
71 1.1 jmcneill #define ANA_CFG3_REG_SCLEN __BIT(0)
72 1.1 jmcneill
73 1.1 jmcneill #define PLL_CFG1 0x02c
74 1.1 jmcneill #define PLL_CFG1_REG_OD1 __BIT(31)
75 1.1 jmcneill #define PLL_CFG1_REG_OD0 __BIT(30)
76 1.1 jmcneill #define PLL_CFG1_CKIN_SEL __BIT(26)
77 1.1 jmcneill #define PLL_CFG1_PLLEN __BIT(25)
78 1.1 jmcneill #define PLL_CFG1_B_IN __BITS(5,0)
79 1.1 jmcneill
80 1.1 jmcneill #define PLL_CFG2 0x030
81 1.1 jmcneill #define PLL_CFG2_PREDIV __BITS(3,0)
82 1.1 jmcneill
83 1.1 jmcneill #define PLL_CFG3 0x034
84 1.1 jmcneill
85 1.1 jmcneill #define ANA_STS 0x038
86 1.1 jmcneill #define ANA_STS_HPDO __BIT(19)
87 1.1 jmcneill #define ANA_STS_B_OUT __BITS(16,11)
88 1.1 jmcneill #define ANA_STS_RCALEND2D __BIT(7)
89 1.1 jmcneill #define ANA_STS_RESDO2D __BITS(5,0)
90 1.1 jmcneill
91 1.1 jmcneill #define CEC 0x03c
92 1.1 jmcneill #define CEC_CONTROL_SEL __BIT(7)
93 1.1 jmcneill #define CEC_INPUT_DATA __BIT(1)
94 1.1 jmcneill #define CEC_OUTPUT_DATA __BIT(0)
95 1.1 jmcneill
96 1.1 jmcneill #define CONTROLLER_VER 0xff8
97 1.1 jmcneill
98 1.1 jmcneill #define PHY_VER 0xffc
99 1.1 jmcneill
100 1.1 jmcneill struct sunxi_hdmiphy_softc;
101 1.1 jmcneill
102 1.1 jmcneill static int sunxi_hdmiphy_match(device_t, cfdata_t, void *);
103 1.1 jmcneill static void sunxi_hdmiphy_attach(device_t, device_t, void *);
104 1.1 jmcneill
105 1.2 jmcneill static void sun8i_h3_hdmiphy_init(struct sunxi_hdmiphy_softc *);
106 1.2 jmcneill static int sun8i_h3_hdmiphy_config(struct sunxi_hdmiphy_softc *, u_int);
107 1.1 jmcneill
108 1.1 jmcneill struct sunxi_hdmiphy_data {
109 1.1 jmcneill void (*init)(struct sunxi_hdmiphy_softc *);
110 1.1 jmcneill int (*config)(struct sunxi_hdmiphy_softc *, u_int);
111 1.1 jmcneill };
112 1.1 jmcneill
113 1.2 jmcneill static const struct sunxi_hdmiphy_data sun8i_h3_hdmiphy_data = {
114 1.2 jmcneill .init = sun8i_h3_hdmiphy_init,
115 1.2 jmcneill .config = sun8i_h3_hdmiphy_config,
116 1.1 jmcneill };
117 1.1 jmcneill
118 1.1 jmcneill static const struct of_compat_data compat_data[] = {
119 1.2 jmcneill { "allwinner,sun8i-h3-hdmi-phy", (uintptr_t)&sun8i_h3_hdmiphy_data },
120 1.2 jmcneill { "allwinner,sun50i-a64-hdmi-phy", (uintptr_t)&sun8i_h3_hdmiphy_data },
121 1.1 jmcneill { NULL }
122 1.1 jmcneill };
123 1.1 jmcneill
124 1.1 jmcneill struct sunxi_hdmiphy_softc {
125 1.1 jmcneill device_t sc_dev;
126 1.1 jmcneill bus_space_tag_t sc_bst;
127 1.1 jmcneill bus_space_handle_t sc_bsh;
128 1.1 jmcneill
129 1.1 jmcneill const struct sunxi_hdmiphy_data *sc_data;
130 1.1 jmcneill
131 1.4 jmcneill struct fdtbus_reset *sc_rst;
132 1.3 jmcneill struct clk *sc_clk_bus;
133 1.3 jmcneill struct clk *sc_clk_mod;
134 1.1 jmcneill struct clk *sc_clk_pll0;
135 1.1 jmcneill
136 1.1 jmcneill u_int sc_rcalib;
137 1.1 jmcneill };
138 1.1 jmcneill
139 1.1 jmcneill #define PHY_READ(sc, reg) \
140 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
141 1.1 jmcneill #define PHY_WRITE(sc, reg, val) \
142 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
143 1.1 jmcneill #define PHY_SET_CLEAR(sc, reg, set, clr) \
144 1.1 jmcneill do { \
145 1.1 jmcneill uint32_t _tval = PHY_READ((sc), (reg)); \
146 1.1 jmcneill _tval &= ~(clr); \
147 1.1 jmcneill _tval |= (set); \
148 1.1 jmcneill PHY_WRITE((sc), (reg), _tval); \
149 1.1 jmcneill } while (0)
150 1.1 jmcneill #define PHY_SET(sc, reg, set) \
151 1.1 jmcneill PHY_SET_CLEAR(sc, reg, set, 0)
152 1.1 jmcneill #define PHY_CLEAR(sc, reg, clr) \
153 1.1 jmcneill PHY_SET_CLEAR(sc, reg, 0, clr)
154 1.1 jmcneill
155 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_hdmiphy, sizeof(struct sunxi_hdmiphy_softc),
156 1.1 jmcneill sunxi_hdmiphy_match, sunxi_hdmiphy_attach, NULL, NULL);
157 1.1 jmcneill
158 1.1 jmcneill static void *
159 1.1 jmcneill sunxi_hdmiphy_acquire(device_t dev, const void *data, size_t len)
160 1.1 jmcneill {
161 1.1 jmcneill struct sunxi_hdmiphy_softc * const sc = device_private(dev);
162 1.1 jmcneill
163 1.1 jmcneill if (len != 0)
164 1.1 jmcneill return NULL;
165 1.1 jmcneill
166 1.1 jmcneill return sc;
167 1.1 jmcneill }
168 1.1 jmcneill
169 1.1 jmcneill static void
170 1.1 jmcneill sunxi_hdmiphy_release(device_t dev, void *priv)
171 1.1 jmcneill {
172 1.1 jmcneill }
173 1.1 jmcneill
174 1.1 jmcneill static int
175 1.1 jmcneill sunxi_hdmiphy_enable(device_t dev, void *priv, bool enable)
176 1.1 jmcneill {
177 1.1 jmcneill return 0;
178 1.1 jmcneill }
179 1.1 jmcneill
180 1.1 jmcneill static const struct fdtbus_phy_controller_func sunxi_hdmiphy_funcs = {
181 1.1 jmcneill .acquire = sunxi_hdmiphy_acquire,
182 1.1 jmcneill .release = sunxi_hdmiphy_release,
183 1.1 jmcneill .enable = sunxi_hdmiphy_enable,
184 1.1 jmcneill };
185 1.1 jmcneill
186 1.1 jmcneill #ifdef SUNXI_HDMIPHY_DEBUG
187 1.1 jmcneill static void
188 1.1 jmcneill sunxi_hdmiphy_dump(struct sunxi_hdmiphy_softc *sc)
189 1.1 jmcneill {
190 1.1 jmcneill device_printf(sc->sc_dev, "ANA_CFG1: %#x\tANA_CFG2: %#x\tANA_CFG3: %#x\n",
191 1.1 jmcneill PHY_READ(sc, ANA_CFG1), PHY_READ(sc, ANA_CFG2), PHY_READ(sc, ANA_CFG3));
192 1.1 jmcneill device_printf(sc->sc_dev, "PLL_CFG1: %#x\tPLL_CFG2: %#x\tPLL_CFG3: %#x\n",
193 1.1 jmcneill PHY_READ(sc, PLL_CFG1), PHY_READ(sc, PLL_CFG2), PHY_READ(sc, PLL_CFG3));
194 1.1 jmcneill device_printf(sc->sc_dev, "DBG_CTRL: %#x\tANA_STS: %#x\n",
195 1.1 jmcneill PHY_READ(sc, DBG_CTRL), PHY_READ(sc, ANA_STS));
196 1.1 jmcneill }
197 1.1 jmcneill #endif
198 1.1 jmcneill
199 1.1 jmcneill static void
200 1.2 jmcneill sun8i_h3_hdmiphy_init(struct sunxi_hdmiphy_softc *sc)
201 1.1 jmcneill {
202 1.1 jmcneill uint32_t val;
203 1.1 jmcneill int retry;
204 1.1 jmcneill
205 1.1 jmcneill PHY_WRITE(sc, ANA_CFG1, 0);
206 1.1 jmcneill
207 1.1 jmcneill PHY_SET(sc, ANA_CFG1, ANA_CFG1_ENBI);
208 1.1 jmcneill delay(5);
209 1.1 jmcneill
210 1.1 jmcneill /* Enable TMDS clock */
211 1.1 jmcneill PHY_SET(sc, ANA_CFG1, ANA_CFG1_TMDSCLK_EN);
212 1.1 jmcneill
213 1.1 jmcneill /* Enable common voltage reference bias module */
214 1.1 jmcneill PHY_SET(sc, ANA_CFG1, ANA_CFG1_ENVBS);
215 1.1 jmcneill delay(20);
216 1.1 jmcneill
217 1.1 jmcneill /* Enable internal LDO */
218 1.1 jmcneill PHY_SET(sc, ANA_CFG1, ANA_CFG1_LDOEN);
219 1.1 jmcneill delay(5);
220 1.1 jmcneill
221 1.1 jmcneill /* Enable common clock module */
222 1.1 jmcneill PHY_SET(sc, ANA_CFG1, ANA_CFG1_CKEN);
223 1.1 jmcneill delay(100);
224 1.1 jmcneill
225 1.1 jmcneill /* Enable resistance calibration analog and digital modules */
226 1.1 jmcneill PHY_SET(sc, ANA_CFG1, ANA_CFG1_ENRCAL);
227 1.1 jmcneill delay(200);
228 1.1 jmcneill PHY_SET(sc, ANA_CFG1, ANA_CFG1_ENCALOG);
229 1.1 jmcneill
230 1.1 jmcneill /* P2S module enable for TMDS data lane */
231 1.1 jmcneill PHY_SET_CLEAR(sc, ANA_CFG1, __SHIFTIN(0x7, ANA_CFG1_ENP2S), ANA_CFG1_ENP2S);
232 1.1 jmcneill
233 1.1 jmcneill /* Wait for resistance calibration to finish */
234 1.1 jmcneill for (retry = 2000; retry > 0; retry--) {
235 1.1 jmcneill if ((PHY_READ(sc, ANA_STS) & ANA_STS_RCALEND2D) != 0)
236 1.1 jmcneill break;
237 1.1 jmcneill delay(1);
238 1.1 jmcneill }
239 1.1 jmcneill if (retry == 0)
240 1.1 jmcneill aprint_error_dev(sc->sc_dev, "HDMI PHY resistance calibration timed out\n");
241 1.1 jmcneill
242 1.1 jmcneill /* Enable current and voltage module */
243 1.1 jmcneill PHY_SET_CLEAR(sc, ANA_CFG1, __SHIFTIN(0xf, ANA_CFG1_BIASEN), ANA_CFG1_BIASEN);
244 1.1 jmcneill
245 1.1 jmcneill /* P2S module enable for TMDS clock lane */
246 1.1 jmcneill PHY_SET_CLEAR(sc, ANA_CFG1, __SHIFTIN(0xf, ANA_CFG1_ENP2S), ANA_CFG1_ENP2S);
247 1.1 jmcneill
248 1.1 jmcneill /* Enable DDC */
249 1.1 jmcneill PHY_SET(sc, ANA_CFG3, ANA_CFG3_REG_SDAEN | ANA_CFG3_REG_SCLEN);
250 1.1 jmcneill
251 1.1 jmcneill /* Set parent clock to videopll0 */
252 1.1 jmcneill PHY_CLEAR(sc, PLL_CFG1, PLL_CFG1_CKIN_SEL);
253 1.1 jmcneill
254 1.1 jmcneill /* Clear software control of CEC pins */
255 1.1 jmcneill PHY_CLEAR(sc, CEC, CEC_CONTROL_SEL);
256 1.1 jmcneill
257 1.1 jmcneill /* Read calibration value for source termination resistors */
258 1.1 jmcneill val = PHY_READ(sc, ANA_STS);
259 1.1 jmcneill sc->sc_rcalib = __SHIFTOUT(val, ANA_STS_RESDO2D);
260 1.1 jmcneill }
261 1.1 jmcneill
262 1.1 jmcneill /*
263 1.1 jmcneill * The following table is based on data from the "HDMI TX PHY S40 Specification".
264 1.1 jmcneill */
265 1.2 jmcneill static const struct sun8i_h3_hdmiphy_init {
266 1.1 jmcneill /* PLL Recommended Configuration */
267 1.1 jmcneill uint32_t pll_cfg1;
268 1.1 jmcneill uint32_t pll_cfg2;
269 1.1 jmcneill uint32_t pll_cfg3;
270 1.1 jmcneill /* TMDS Characteristics Recommended Configuration */
271 1.1 jmcneill uint32_t ana_cfg1;
272 1.1 jmcneill uint32_t ana_cfg2;
273 1.1 jmcneill uint32_t ana_cfg3;
274 1.1 jmcneill bool ana_cfg2_rcal_200;
275 1.1 jmcneill u_int b_offset;
276 1.2 jmcneill } sun8i_h3_hdmiphy_inittab[] = {
277 1.1 jmcneill /* 27 MHz */
278 1.1 jmcneill [0] = {
279 1.1 jmcneill .pll_cfg1 = 0x3ddc5040, .pll_cfg2 = 0x8008430a, .pll_cfg3 = 0x1,
280 1.1 jmcneill .ana_cfg1 = 0x11ffff7f, .ana_cfg2 = 0x80623000, .ana_cfg3 = 0x0f80c285,
281 1.1 jmcneill .ana_cfg2_rcal_200 = true,
282 1.1 jmcneill },
283 1.1 jmcneill /* 74.25 MHz */
284 1.1 jmcneill [1] = {
285 1.1 jmcneill .pll_cfg1 = 0x3ddc5040, .pll_cfg2 = 0x80084343, .pll_cfg3 = 0x1,
286 1.1 jmcneill .ana_cfg1 = 0x11ffff7f, .ana_cfg2 = 0x80623000, .ana_cfg3 = 0x0f814385,
287 1.1 jmcneill .ana_cfg2_rcal_200 = true,
288 1.1 jmcneill },
289 1.1 jmcneill /* 148.5 MHz */
290 1.1 jmcneill [2] = {
291 1.1 jmcneill .pll_cfg1 = 0x3ddc5040, .pll_cfg2 = 0x80084381, .pll_cfg3 = 0x1,
292 1.1 jmcneill .ana_cfg1 = 0x01ffff7f, .ana_cfg2 = 0x8063a800, .ana_cfg3 = 0x0f81c485,
293 1.1 jmcneill },
294 1.1 jmcneill /* 297 MHz */
295 1.1 jmcneill [3] = {
296 1.1 jmcneill .pll_cfg1 = 0x35dc5fc0, .pll_cfg2 = 0x800863c0, .pll_cfg3 = 0x1,
297 1.1 jmcneill .ana_cfg1 = 0x01ffff7f, .ana_cfg2 = 0x8063b000, .ana_cfg3 = 0x0f8246b5,
298 1.1 jmcneill .b_offset = 2,
299 1.1 jmcneill },
300 1.1 jmcneill };
301 1.1 jmcneill
302 1.1 jmcneill static int
303 1.2 jmcneill sun8i_h3_hdmiphy_config(struct sunxi_hdmiphy_softc *sc, u_int rate)
304 1.1 jmcneill {
305 1.2 jmcneill const struct sun8i_h3_hdmiphy_init *inittab;
306 1.1 jmcneill u_int init_index, b_out, prediv;
307 1.1 jmcneill uint32_t val, rcalib;
308 1.1 jmcneill
309 1.1 jmcneill if (rate == 0) {
310 1.1 jmcneill /* Disable the PHY */
311 1.1 jmcneill PHY_WRITE(sc, ANA_CFG1, ANA_CFG1_LDOEN | ANA_CFG1_ENVBS | ANA_CFG1_ENBI);
312 1.1 jmcneill PHY_WRITE(sc, PLL_CFG1, 0);
313 1.1 jmcneill return 0;
314 1.1 jmcneill }
315 1.1 jmcneill
316 1.1 jmcneill init_index = 0;
317 1.1 jmcneill if (rate > 27000000)
318 1.1 jmcneill init_index++;
319 1.1 jmcneill if (rate > 74250000)
320 1.1 jmcneill init_index++;
321 1.1 jmcneill if (rate > 148500000)
322 1.1 jmcneill init_index++;
323 1.2 jmcneill inittab = &sun8i_h3_hdmiphy_inittab[init_index];
324 1.1 jmcneill
325 1.1 jmcneill val = PHY_READ(sc, PLL_CFG2);
326 1.1 jmcneill prediv = val & PLL_CFG2_PREDIV;
327 1.1 jmcneill
328 1.1 jmcneill /* Config PLL */
329 1.1 jmcneill PHY_WRITE(sc, PLL_CFG1, inittab->pll_cfg1 & ~PLL_CFG1_CKIN_SEL);
330 1.1 jmcneill PHY_WRITE(sc, PLL_CFG2, (inittab->pll_cfg2 & ~PLL_CFG2_PREDIV) | prediv);
331 1.1 jmcneill delay(15000);
332 1.1 jmcneill PHY_WRITE(sc, PLL_CFG3, inittab->pll_cfg3);
333 1.1 jmcneill
334 1.1 jmcneill /* Enable PLL */
335 1.1 jmcneill PHY_SET(sc, PLL_CFG1, PLL_CFG1_PLLEN);
336 1.1 jmcneill delay(100000);
337 1.1 jmcneill
338 1.1 jmcneill /* Config PLL */
339 1.1 jmcneill val = PHY_READ(sc, ANA_STS);
340 1.1 jmcneill b_out = __SHIFTOUT(val, ANA_STS_B_OUT);
341 1.1 jmcneill b_out = MIN(b_out + inittab->b_offset, __SHIFTOUT_MASK(ANA_STS_B_OUT));
342 1.1 jmcneill
343 1.1 jmcneill PHY_SET(sc, PLL_CFG1, PLL_CFG1_REG_OD1 | PLL_CFG1_REG_OD0);
344 1.1 jmcneill PHY_SET(sc, PLL_CFG1, __SHIFTIN(b_out, PLL_CFG1_B_IN));
345 1.1 jmcneill delay(100000);
346 1.1 jmcneill
347 1.1 jmcneill /* Config TMDS characteristics */
348 1.1 jmcneill if (inittab->ana_cfg2_rcal_200)
349 1.1 jmcneill rcalib = sc->sc_rcalib >> 2;
350 1.1 jmcneill else
351 1.1 jmcneill rcalib = 0;
352 1.1 jmcneill PHY_WRITE(sc, ANA_CFG1, inittab->ana_cfg1);
353 1.1 jmcneill PHY_WRITE(sc, ANA_CFG2, inittab->ana_cfg2 | rcalib);
354 1.1 jmcneill PHY_WRITE(sc, ANA_CFG3, inittab->ana_cfg3);
355 1.1 jmcneill
356 1.1 jmcneill #ifdef SUNXI_HDMIPHY_DEBUG
357 1.1 jmcneill sunxi_hdmiphy_dump(sc);
358 1.1 jmcneill #endif
359 1.1 jmcneill
360 1.1 jmcneill return 0;
361 1.1 jmcneill }
362 1.1 jmcneill
363 1.1 jmcneill static int
364 1.1 jmcneill sunxi_hdmiphy_set_rate(struct sunxi_hdmiphy_softc *sc, u_int new_rate)
365 1.1 jmcneill {
366 1.1 jmcneill u_int prediv, best_prediv, best_rate;
367 1.1 jmcneill
368 1.1 jmcneill if (sc->sc_clk_pll0 == NULL)
369 1.1 jmcneill return 0;
370 1.1 jmcneill
371 1.1 jmcneill const u_int parent_rate = clk_get_rate(sc->sc_clk_pll0);
372 1.1 jmcneill
373 1.1 jmcneill best_rate = 0;
374 1.1 jmcneill
375 1.1 jmcneill for (prediv = 0; prediv <= __SHIFTOUT_MASK(PLL_CFG2_PREDIV); prediv++) {
376 1.1 jmcneill const u_int tmp_rate = parent_rate / (prediv + 1);
377 1.1 jmcneill const int diff = new_rate - tmp_rate;
378 1.1 jmcneill if (diff >= 0 && tmp_rate > best_rate) {
379 1.1 jmcneill best_rate = tmp_rate;
380 1.1 jmcneill best_prediv = prediv;
381 1.1 jmcneill }
382 1.1 jmcneill }
383 1.1 jmcneill
384 1.1 jmcneill if (best_rate == 0)
385 1.1 jmcneill return ERANGE;
386 1.1 jmcneill
387 1.1 jmcneill PHY_SET_CLEAR(sc, PLL_CFG2, __SHIFTIN(best_prediv, PLL_CFG2_PREDIV), PLL_CFG2_PREDIV);
388 1.1 jmcneill
389 1.1 jmcneill return 0;
390 1.1 jmcneill }
391 1.1 jmcneill
392 1.1 jmcneill static int
393 1.1 jmcneill sunxi_hdmiphy_match(device_t parent, cfdata_t cf, void *aux)
394 1.1 jmcneill {
395 1.1 jmcneill struct fdt_attach_args * const faa = aux;
396 1.1 jmcneill
397 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
398 1.1 jmcneill }
399 1.1 jmcneill
400 1.1 jmcneill static void
401 1.1 jmcneill sunxi_hdmiphy_attach(device_t parent, device_t self, void *aux)
402 1.1 jmcneill {
403 1.1 jmcneill struct sunxi_hdmiphy_softc * const sc = device_private(self);
404 1.1 jmcneill struct fdt_attach_args * const faa = aux;
405 1.1 jmcneill const int phandle = faa->faa_phandle;
406 1.1 jmcneill struct clk *clk_bus, *clk_mod, *clk_pll0;
407 1.1 jmcneill struct fdtbus_reset *rst;
408 1.1 jmcneill bus_addr_t addr;
409 1.1 jmcneill bus_size_t size;
410 1.1 jmcneill
411 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
412 1.1 jmcneill aprint_error(": couldn't get registers\n");
413 1.1 jmcneill return;
414 1.1 jmcneill }
415 1.1 jmcneill
416 1.1 jmcneill rst = fdtbus_reset_get(phandle, "phy");
417 1.4 jmcneill if (rst == NULL) {
418 1.4 jmcneill aprint_error(": couldn't get reset\n");
419 1.1 jmcneill return;
420 1.1 jmcneill }
421 1.1 jmcneill clk_bus = fdtbus_clock_get(phandle, "bus");
422 1.1 jmcneill clk_mod = fdtbus_clock_get(phandle, "mod");
423 1.1 jmcneill clk_pll0 = fdtbus_clock_get(phandle, "pll-0");
424 1.3 jmcneill if (clk_bus == NULL || clk_mod == NULL || clk_pll0 == NULL) {
425 1.3 jmcneill aprint_error(": couldn't get clocks\n");
426 1.1 jmcneill return;
427 1.1 jmcneill }
428 1.1 jmcneill
429 1.1 jmcneill sc->sc_dev = self;
430 1.1 jmcneill sc->sc_bst = faa->faa_bst;
431 1.1 jmcneill sc->sc_data = (void *)of_search_compatible(phandle, compat_data)->data;
432 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
433 1.1 jmcneill aprint_error(": couldn't map registers\n");
434 1.1 jmcneill return;
435 1.1 jmcneill }
436 1.4 jmcneill sc->sc_rst = rst;
437 1.3 jmcneill sc->sc_clk_bus = clk_bus;
438 1.3 jmcneill sc->sc_clk_mod = clk_mod;
439 1.1 jmcneill sc->sc_clk_pll0 = clk_pll0;
440 1.1 jmcneill
441 1.1 jmcneill aprint_naive("\n");
442 1.1 jmcneill aprint_normal(": HDMI PHY\n");
443 1.1 jmcneill
444 1.1 jmcneill fdtbus_register_phy_controller(self, phandle, &sunxi_hdmiphy_funcs);
445 1.3 jmcneill }
446 1.3 jmcneill
447 1.3 jmcneill void
448 1.3 jmcneill sunxi_hdmiphy_init(struct fdtbus_phy *phy)
449 1.3 jmcneill {
450 1.3 jmcneill device_t dev = fdtbus_phy_device(phy);
451 1.3 jmcneill struct sunxi_hdmiphy_softc * const sc = device_private(dev);
452 1.3 jmcneill
453 1.3 jmcneill clk_enable(sc->sc_clk_bus);
454 1.3 jmcneill clk_enable(sc->sc_clk_mod);
455 1.3 jmcneill clk_enable(sc->sc_clk_pll0);
456 1.1 jmcneill
457 1.4 jmcneill fdtbus_reset_deassert(sc->sc_rst);
458 1.4 jmcneill
459 1.4 jmcneill sc->sc_data->init(sc);
460 1.4 jmcneill
461 1.1 jmcneill PHY_WRITE(sc, READ_EN, READ_EN_MAGIC);
462 1.1 jmcneill PHY_WRITE(sc, UNSCRAMBLE, UNSCRAMBLE_MAGIC);
463 1.1 jmcneill
464 1.1 jmcneill #ifdef SUNXI_HDMIPHY_DEBUG
465 1.1 jmcneill sunxi_hdmiphy_dump(sc);
466 1.1 jmcneill #endif
467 1.1 jmcneill }
468 1.1 jmcneill
469 1.1 jmcneill int
470 1.1 jmcneill sunxi_hdmiphy_config(struct fdtbus_phy *phy, struct drm_display_mode *mode)
471 1.1 jmcneill {
472 1.1 jmcneill device_t dev = fdtbus_phy_device(phy);
473 1.1 jmcneill struct sunxi_hdmiphy_softc * const sc = device_private(dev);
474 1.1 jmcneill u_int pol;
475 1.1 jmcneill int error;
476 1.1 jmcneill
477 1.1 jmcneill pol = 0;
478 1.1 jmcneill if ((mode->flags & DRM_MODE_FLAG_NHSYNC) != 0)
479 1.1 jmcneill pol |= __SHIFTIN(DBG_CTRL_POL_NHSYNC, DBG_CTRL_POL);
480 1.1 jmcneill if ((mode->flags & DRM_MODE_FLAG_NVSYNC) != 0)
481 1.1 jmcneill pol |= __SHIFTIN(DBG_CTRL_POL_NVSYNC, DBG_CTRL_POL);
482 1.1 jmcneill
483 1.1 jmcneill PHY_SET_CLEAR(sc, DBG_CTRL, pol, DBG_CTRL_POL);
484 1.1 jmcneill
485 1.1 jmcneill error = sunxi_hdmiphy_set_rate(sc, mode->crtc_clock * 1000);
486 1.1 jmcneill if (error != 0) {
487 1.1 jmcneill aprint_error_dev(dev, "failed to set HDMI PHY clock: %d\n", error);
488 1.1 jmcneill return error;
489 1.1 jmcneill }
490 1.1 jmcneill
491 1.1 jmcneill return sc->sc_data->config(sc, mode->crtc_clock * 1000);
492 1.1 jmcneill }
493 1.1 jmcneill
494 1.1 jmcneill bool
495 1.1 jmcneill sunxi_hdmiphy_detect(struct fdtbus_phy *phy, bool force)
496 1.1 jmcneill {
497 1.1 jmcneill device_t dev = fdtbus_phy_device(phy);
498 1.1 jmcneill struct sunxi_hdmiphy_softc * const sc = device_private(dev);
499 1.1 jmcneill uint32_t val;
500 1.1 jmcneill
501 1.1 jmcneill val = PHY_READ(sc, ANA_STS);
502 1.1 jmcneill
503 1.1 jmcneill return ISSET(val, ANA_STS_HPDO);
504 1.1 jmcneill }
505