sunxi_hdmiphy.c revision 1.3 1 1.3 jmcneill /* $NetBSD: sunxi_hdmiphy.c,v 1.3 2019/11/23 12:30:45 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.3 jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_hdmiphy.c,v 1.3 2019/11/23 12:30:45 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.3 jmcneill struct clk *sc_clk_bus;
132 1.3 jmcneill struct clk *sc_clk_mod;
133 1.1 jmcneill struct clk *sc_clk_pll0;
134 1.1 jmcneill
135 1.1 jmcneill u_int sc_rcalib;
136 1.1 jmcneill };
137 1.1 jmcneill
138 1.1 jmcneill #define PHY_READ(sc, reg) \
139 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
140 1.1 jmcneill #define PHY_WRITE(sc, reg, val) \
141 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
142 1.1 jmcneill #define PHY_SET_CLEAR(sc, reg, set, clr) \
143 1.1 jmcneill do { \
144 1.1 jmcneill uint32_t _tval = PHY_READ((sc), (reg)); \
145 1.1 jmcneill _tval &= ~(clr); \
146 1.1 jmcneill _tval |= (set); \
147 1.1 jmcneill PHY_WRITE((sc), (reg), _tval); \
148 1.1 jmcneill } while (0)
149 1.1 jmcneill #define PHY_SET(sc, reg, set) \
150 1.1 jmcneill PHY_SET_CLEAR(sc, reg, set, 0)
151 1.1 jmcneill #define PHY_CLEAR(sc, reg, clr) \
152 1.1 jmcneill PHY_SET_CLEAR(sc, reg, 0, clr)
153 1.1 jmcneill
154 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_hdmiphy, sizeof(struct sunxi_hdmiphy_softc),
155 1.1 jmcneill sunxi_hdmiphy_match, sunxi_hdmiphy_attach, NULL, NULL);
156 1.1 jmcneill
157 1.1 jmcneill static void *
158 1.1 jmcneill sunxi_hdmiphy_acquire(device_t dev, const void *data, size_t len)
159 1.1 jmcneill {
160 1.1 jmcneill struct sunxi_hdmiphy_softc * const sc = device_private(dev);
161 1.1 jmcneill
162 1.1 jmcneill if (len != 0)
163 1.1 jmcneill return NULL;
164 1.1 jmcneill
165 1.1 jmcneill return sc;
166 1.1 jmcneill }
167 1.1 jmcneill
168 1.1 jmcneill static void
169 1.1 jmcneill sunxi_hdmiphy_release(device_t dev, void *priv)
170 1.1 jmcneill {
171 1.1 jmcneill }
172 1.1 jmcneill
173 1.1 jmcneill static int
174 1.1 jmcneill sunxi_hdmiphy_enable(device_t dev, void *priv, bool enable)
175 1.1 jmcneill {
176 1.1 jmcneill struct sunxi_hdmiphy_softc * const sc = priv;
177 1.1 jmcneill
178 1.1 jmcneill if (enable) {
179 1.1 jmcneill sc->sc_data->init(sc);
180 1.1 jmcneill } else {
181 1.1 jmcneill sc->sc_data->config(sc, 0);
182 1.1 jmcneill }
183 1.1 jmcneill
184 1.1 jmcneill return 0;
185 1.1 jmcneill }
186 1.1 jmcneill
187 1.1 jmcneill static const struct fdtbus_phy_controller_func sunxi_hdmiphy_funcs = {
188 1.1 jmcneill .acquire = sunxi_hdmiphy_acquire,
189 1.1 jmcneill .release = sunxi_hdmiphy_release,
190 1.1 jmcneill .enable = sunxi_hdmiphy_enable,
191 1.1 jmcneill };
192 1.1 jmcneill
193 1.1 jmcneill #ifdef SUNXI_HDMIPHY_DEBUG
194 1.1 jmcneill static void
195 1.1 jmcneill sunxi_hdmiphy_dump(struct sunxi_hdmiphy_softc *sc)
196 1.1 jmcneill {
197 1.1 jmcneill device_printf(sc->sc_dev, "ANA_CFG1: %#x\tANA_CFG2: %#x\tANA_CFG3: %#x\n",
198 1.1 jmcneill PHY_READ(sc, ANA_CFG1), PHY_READ(sc, ANA_CFG2), PHY_READ(sc, ANA_CFG3));
199 1.1 jmcneill device_printf(sc->sc_dev, "PLL_CFG1: %#x\tPLL_CFG2: %#x\tPLL_CFG3: %#x\n",
200 1.1 jmcneill PHY_READ(sc, PLL_CFG1), PHY_READ(sc, PLL_CFG2), PHY_READ(sc, PLL_CFG3));
201 1.1 jmcneill device_printf(sc->sc_dev, "DBG_CTRL: %#x\tANA_STS: %#x\n",
202 1.1 jmcneill PHY_READ(sc, DBG_CTRL), PHY_READ(sc, ANA_STS));
203 1.1 jmcneill }
204 1.1 jmcneill #endif
205 1.1 jmcneill
206 1.1 jmcneill static void
207 1.2 jmcneill sun8i_h3_hdmiphy_init(struct sunxi_hdmiphy_softc *sc)
208 1.1 jmcneill {
209 1.1 jmcneill uint32_t val;
210 1.1 jmcneill int retry;
211 1.1 jmcneill
212 1.1 jmcneill PHY_WRITE(sc, ANA_CFG1, 0);
213 1.1 jmcneill
214 1.1 jmcneill PHY_SET(sc, ANA_CFG1, ANA_CFG1_ENBI);
215 1.1 jmcneill delay(5);
216 1.1 jmcneill
217 1.1 jmcneill /* Enable TMDS clock */
218 1.1 jmcneill PHY_SET(sc, ANA_CFG1, ANA_CFG1_TMDSCLK_EN);
219 1.1 jmcneill
220 1.1 jmcneill /* Enable common voltage reference bias module */
221 1.1 jmcneill PHY_SET(sc, ANA_CFG1, ANA_CFG1_ENVBS);
222 1.1 jmcneill delay(20);
223 1.1 jmcneill
224 1.1 jmcneill /* Enable internal LDO */
225 1.1 jmcneill PHY_SET(sc, ANA_CFG1, ANA_CFG1_LDOEN);
226 1.1 jmcneill delay(5);
227 1.1 jmcneill
228 1.1 jmcneill /* Enable common clock module */
229 1.1 jmcneill PHY_SET(sc, ANA_CFG1, ANA_CFG1_CKEN);
230 1.1 jmcneill delay(100);
231 1.1 jmcneill
232 1.1 jmcneill /* Enable resistance calibration analog and digital modules */
233 1.1 jmcneill PHY_SET(sc, ANA_CFG1, ANA_CFG1_ENRCAL);
234 1.1 jmcneill delay(200);
235 1.1 jmcneill PHY_SET(sc, ANA_CFG1, ANA_CFG1_ENCALOG);
236 1.1 jmcneill
237 1.1 jmcneill /* P2S module enable for TMDS data lane */
238 1.1 jmcneill PHY_SET_CLEAR(sc, ANA_CFG1, __SHIFTIN(0x7, ANA_CFG1_ENP2S), ANA_CFG1_ENP2S);
239 1.1 jmcneill
240 1.1 jmcneill /* Wait for resistance calibration to finish */
241 1.1 jmcneill for (retry = 2000; retry > 0; retry--) {
242 1.1 jmcneill if ((PHY_READ(sc, ANA_STS) & ANA_STS_RCALEND2D) != 0)
243 1.1 jmcneill break;
244 1.1 jmcneill delay(1);
245 1.1 jmcneill }
246 1.1 jmcneill if (retry == 0)
247 1.1 jmcneill aprint_error_dev(sc->sc_dev, "HDMI PHY resistance calibration timed out\n");
248 1.1 jmcneill
249 1.1 jmcneill /* Enable current and voltage module */
250 1.1 jmcneill PHY_SET_CLEAR(sc, ANA_CFG1, __SHIFTIN(0xf, ANA_CFG1_BIASEN), ANA_CFG1_BIASEN);
251 1.1 jmcneill
252 1.1 jmcneill /* P2S module enable for TMDS clock lane */
253 1.1 jmcneill PHY_SET_CLEAR(sc, ANA_CFG1, __SHIFTIN(0xf, ANA_CFG1_ENP2S), ANA_CFG1_ENP2S);
254 1.1 jmcneill
255 1.1 jmcneill /* Enable DDC */
256 1.1 jmcneill PHY_SET(sc, ANA_CFG3, ANA_CFG3_REG_SDAEN | ANA_CFG3_REG_SCLEN);
257 1.1 jmcneill
258 1.1 jmcneill /* Set parent clock to videopll0 */
259 1.1 jmcneill PHY_CLEAR(sc, PLL_CFG1, PLL_CFG1_CKIN_SEL);
260 1.1 jmcneill
261 1.1 jmcneill /* Clear software control of CEC pins */
262 1.1 jmcneill PHY_CLEAR(sc, CEC, CEC_CONTROL_SEL);
263 1.1 jmcneill
264 1.1 jmcneill /* Read calibration value for source termination resistors */
265 1.1 jmcneill val = PHY_READ(sc, ANA_STS);
266 1.1 jmcneill sc->sc_rcalib = __SHIFTOUT(val, ANA_STS_RESDO2D);
267 1.1 jmcneill }
268 1.1 jmcneill
269 1.1 jmcneill /*
270 1.1 jmcneill * The following table is based on data from the "HDMI TX PHY S40 Specification".
271 1.1 jmcneill */
272 1.2 jmcneill static const struct sun8i_h3_hdmiphy_init {
273 1.1 jmcneill /* PLL Recommended Configuration */
274 1.1 jmcneill uint32_t pll_cfg1;
275 1.1 jmcneill uint32_t pll_cfg2;
276 1.1 jmcneill uint32_t pll_cfg3;
277 1.1 jmcneill /* TMDS Characteristics Recommended Configuration */
278 1.1 jmcneill uint32_t ana_cfg1;
279 1.1 jmcneill uint32_t ana_cfg2;
280 1.1 jmcneill uint32_t ana_cfg3;
281 1.1 jmcneill bool ana_cfg2_rcal_200;
282 1.1 jmcneill u_int b_offset;
283 1.2 jmcneill } sun8i_h3_hdmiphy_inittab[] = {
284 1.1 jmcneill /* 27 MHz */
285 1.1 jmcneill [0] = {
286 1.1 jmcneill .pll_cfg1 = 0x3ddc5040, .pll_cfg2 = 0x8008430a, .pll_cfg3 = 0x1,
287 1.1 jmcneill .ana_cfg1 = 0x11ffff7f, .ana_cfg2 = 0x80623000, .ana_cfg3 = 0x0f80c285,
288 1.1 jmcneill .ana_cfg2_rcal_200 = true,
289 1.1 jmcneill },
290 1.1 jmcneill /* 74.25 MHz */
291 1.1 jmcneill [1] = {
292 1.1 jmcneill .pll_cfg1 = 0x3ddc5040, .pll_cfg2 = 0x80084343, .pll_cfg3 = 0x1,
293 1.1 jmcneill .ana_cfg1 = 0x11ffff7f, .ana_cfg2 = 0x80623000, .ana_cfg3 = 0x0f814385,
294 1.1 jmcneill .ana_cfg2_rcal_200 = true,
295 1.1 jmcneill },
296 1.1 jmcneill /* 148.5 MHz */
297 1.1 jmcneill [2] = {
298 1.1 jmcneill .pll_cfg1 = 0x3ddc5040, .pll_cfg2 = 0x80084381, .pll_cfg3 = 0x1,
299 1.1 jmcneill .ana_cfg1 = 0x01ffff7f, .ana_cfg2 = 0x8063a800, .ana_cfg3 = 0x0f81c485,
300 1.1 jmcneill },
301 1.1 jmcneill /* 297 MHz */
302 1.1 jmcneill [3] = {
303 1.1 jmcneill .pll_cfg1 = 0x35dc5fc0, .pll_cfg2 = 0x800863c0, .pll_cfg3 = 0x1,
304 1.1 jmcneill .ana_cfg1 = 0x01ffff7f, .ana_cfg2 = 0x8063b000, .ana_cfg3 = 0x0f8246b5,
305 1.1 jmcneill .b_offset = 2,
306 1.1 jmcneill },
307 1.1 jmcneill };
308 1.1 jmcneill
309 1.1 jmcneill static int
310 1.2 jmcneill sun8i_h3_hdmiphy_config(struct sunxi_hdmiphy_softc *sc, u_int rate)
311 1.1 jmcneill {
312 1.2 jmcneill const struct sun8i_h3_hdmiphy_init *inittab;
313 1.1 jmcneill u_int init_index, b_out, prediv;
314 1.1 jmcneill uint32_t val, rcalib;
315 1.1 jmcneill
316 1.1 jmcneill if (rate == 0) {
317 1.1 jmcneill /* Disable the PHY */
318 1.1 jmcneill PHY_WRITE(sc, ANA_CFG1, ANA_CFG1_LDOEN | ANA_CFG1_ENVBS | ANA_CFG1_ENBI);
319 1.1 jmcneill PHY_WRITE(sc, PLL_CFG1, 0);
320 1.1 jmcneill return 0;
321 1.1 jmcneill }
322 1.1 jmcneill
323 1.1 jmcneill init_index = 0;
324 1.1 jmcneill if (rate > 27000000)
325 1.1 jmcneill init_index++;
326 1.1 jmcneill if (rate > 74250000)
327 1.1 jmcneill init_index++;
328 1.1 jmcneill if (rate > 148500000)
329 1.1 jmcneill init_index++;
330 1.2 jmcneill inittab = &sun8i_h3_hdmiphy_inittab[init_index];
331 1.1 jmcneill
332 1.1 jmcneill val = PHY_READ(sc, PLL_CFG2);
333 1.1 jmcneill prediv = val & PLL_CFG2_PREDIV;
334 1.1 jmcneill
335 1.1 jmcneill /* Config PLL */
336 1.1 jmcneill PHY_WRITE(sc, PLL_CFG1, inittab->pll_cfg1 & ~PLL_CFG1_CKIN_SEL);
337 1.1 jmcneill PHY_WRITE(sc, PLL_CFG2, (inittab->pll_cfg2 & ~PLL_CFG2_PREDIV) | prediv);
338 1.1 jmcneill delay(15000);
339 1.1 jmcneill PHY_WRITE(sc, PLL_CFG3, inittab->pll_cfg3);
340 1.1 jmcneill
341 1.1 jmcneill /* Enable PLL */
342 1.1 jmcneill PHY_SET(sc, PLL_CFG1, PLL_CFG1_PLLEN);
343 1.1 jmcneill delay(100000);
344 1.1 jmcneill
345 1.1 jmcneill /* Config PLL */
346 1.1 jmcneill val = PHY_READ(sc, ANA_STS);
347 1.1 jmcneill b_out = __SHIFTOUT(val, ANA_STS_B_OUT);
348 1.1 jmcneill b_out = MIN(b_out + inittab->b_offset, __SHIFTOUT_MASK(ANA_STS_B_OUT));
349 1.1 jmcneill
350 1.1 jmcneill PHY_SET(sc, PLL_CFG1, PLL_CFG1_REG_OD1 | PLL_CFG1_REG_OD0);
351 1.1 jmcneill PHY_SET(sc, PLL_CFG1, __SHIFTIN(b_out, PLL_CFG1_B_IN));
352 1.1 jmcneill delay(100000);
353 1.1 jmcneill
354 1.1 jmcneill /* Config TMDS characteristics */
355 1.1 jmcneill if (inittab->ana_cfg2_rcal_200)
356 1.1 jmcneill rcalib = sc->sc_rcalib >> 2;
357 1.1 jmcneill else
358 1.1 jmcneill rcalib = 0;
359 1.1 jmcneill PHY_WRITE(sc, ANA_CFG1, inittab->ana_cfg1);
360 1.1 jmcneill PHY_WRITE(sc, ANA_CFG2, inittab->ana_cfg2 | rcalib);
361 1.1 jmcneill PHY_WRITE(sc, ANA_CFG3, inittab->ana_cfg3);
362 1.1 jmcneill
363 1.1 jmcneill #ifdef SUNXI_HDMIPHY_DEBUG
364 1.1 jmcneill sunxi_hdmiphy_dump(sc);
365 1.1 jmcneill #endif
366 1.1 jmcneill
367 1.1 jmcneill return 0;
368 1.1 jmcneill }
369 1.1 jmcneill
370 1.1 jmcneill static int
371 1.1 jmcneill sunxi_hdmiphy_set_rate(struct sunxi_hdmiphy_softc *sc, u_int new_rate)
372 1.1 jmcneill {
373 1.1 jmcneill u_int prediv, best_prediv, best_rate;
374 1.1 jmcneill
375 1.1 jmcneill if (sc->sc_clk_pll0 == NULL)
376 1.1 jmcneill return 0;
377 1.1 jmcneill
378 1.1 jmcneill const u_int parent_rate = clk_get_rate(sc->sc_clk_pll0);
379 1.1 jmcneill
380 1.1 jmcneill best_rate = 0;
381 1.1 jmcneill
382 1.1 jmcneill for (prediv = 0; prediv <= __SHIFTOUT_MASK(PLL_CFG2_PREDIV); prediv++) {
383 1.1 jmcneill const u_int tmp_rate = parent_rate / (prediv + 1);
384 1.1 jmcneill const int diff = new_rate - tmp_rate;
385 1.1 jmcneill if (diff >= 0 && tmp_rate > best_rate) {
386 1.1 jmcneill best_rate = tmp_rate;
387 1.1 jmcneill best_prediv = prediv;
388 1.1 jmcneill }
389 1.1 jmcneill }
390 1.1 jmcneill
391 1.1 jmcneill if (best_rate == 0)
392 1.1 jmcneill return ERANGE;
393 1.1 jmcneill
394 1.1 jmcneill PHY_SET_CLEAR(sc, PLL_CFG2, __SHIFTIN(best_prediv, PLL_CFG2_PREDIV), PLL_CFG2_PREDIV);
395 1.1 jmcneill
396 1.1 jmcneill return 0;
397 1.1 jmcneill }
398 1.1 jmcneill
399 1.1 jmcneill static int
400 1.1 jmcneill sunxi_hdmiphy_match(device_t parent, cfdata_t cf, void *aux)
401 1.1 jmcneill {
402 1.1 jmcneill struct fdt_attach_args * const faa = aux;
403 1.1 jmcneill
404 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
405 1.1 jmcneill }
406 1.1 jmcneill
407 1.1 jmcneill static void
408 1.1 jmcneill sunxi_hdmiphy_attach(device_t parent, device_t self, void *aux)
409 1.1 jmcneill {
410 1.1 jmcneill struct sunxi_hdmiphy_softc * const sc = device_private(self);
411 1.1 jmcneill struct fdt_attach_args * const faa = aux;
412 1.1 jmcneill const int phandle = faa->faa_phandle;
413 1.1 jmcneill struct clk *clk_bus, *clk_mod, *clk_pll0;
414 1.1 jmcneill struct fdtbus_reset *rst;
415 1.1 jmcneill bus_addr_t addr;
416 1.1 jmcneill bus_size_t size;
417 1.1 jmcneill
418 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
419 1.1 jmcneill aprint_error(": couldn't get registers\n");
420 1.1 jmcneill return;
421 1.1 jmcneill }
422 1.1 jmcneill
423 1.1 jmcneill rst = fdtbus_reset_get(phandle, "phy");
424 1.1 jmcneill if (rst == NULL || fdtbus_reset_deassert(rst) != 0) {
425 1.1 jmcneill aprint_error(": couldn't de-assert reset\n");
426 1.1 jmcneill return;
427 1.1 jmcneill }
428 1.1 jmcneill
429 1.1 jmcneill clk_bus = fdtbus_clock_get(phandle, "bus");
430 1.1 jmcneill clk_mod = fdtbus_clock_get(phandle, "mod");
431 1.1 jmcneill clk_pll0 = fdtbus_clock_get(phandle, "pll-0");
432 1.3 jmcneill if (clk_bus == NULL || clk_mod == NULL || clk_pll0 == NULL) {
433 1.3 jmcneill aprint_error(": couldn't get clocks\n");
434 1.1 jmcneill return;
435 1.1 jmcneill }
436 1.1 jmcneill
437 1.1 jmcneill sc->sc_dev = self;
438 1.1 jmcneill sc->sc_bst = faa->faa_bst;
439 1.1 jmcneill sc->sc_data = (void *)of_search_compatible(phandle, compat_data)->data;
440 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
441 1.1 jmcneill aprint_error(": couldn't map registers\n");
442 1.1 jmcneill return;
443 1.1 jmcneill }
444 1.3 jmcneill sc->sc_clk_bus = clk_bus;
445 1.3 jmcneill sc->sc_clk_mod = clk_mod;
446 1.1 jmcneill sc->sc_clk_pll0 = clk_pll0;
447 1.1 jmcneill
448 1.1 jmcneill aprint_naive("\n");
449 1.1 jmcneill aprint_normal(": HDMI PHY\n");
450 1.1 jmcneill
451 1.1 jmcneill fdtbus_register_phy_controller(self, phandle, &sunxi_hdmiphy_funcs);
452 1.3 jmcneill }
453 1.3 jmcneill
454 1.3 jmcneill void
455 1.3 jmcneill sunxi_hdmiphy_init(struct fdtbus_phy *phy)
456 1.3 jmcneill {
457 1.3 jmcneill device_t dev = fdtbus_phy_device(phy);
458 1.3 jmcneill struct sunxi_hdmiphy_softc * const sc = device_private(dev);
459 1.3 jmcneill
460 1.3 jmcneill clk_enable(sc->sc_clk_bus);
461 1.3 jmcneill clk_enable(sc->sc_clk_mod);
462 1.3 jmcneill clk_enable(sc->sc_clk_pll0);
463 1.1 jmcneill
464 1.1 jmcneill PHY_WRITE(sc, READ_EN, READ_EN_MAGIC);
465 1.1 jmcneill PHY_WRITE(sc, UNSCRAMBLE, UNSCRAMBLE_MAGIC);
466 1.1 jmcneill
467 1.1 jmcneill #ifdef SUNXI_HDMIPHY_DEBUG
468 1.1 jmcneill sunxi_hdmiphy_dump(sc);
469 1.1 jmcneill #endif
470 1.1 jmcneill }
471 1.1 jmcneill
472 1.1 jmcneill int
473 1.1 jmcneill sunxi_hdmiphy_config(struct fdtbus_phy *phy, struct drm_display_mode *mode)
474 1.1 jmcneill {
475 1.1 jmcneill device_t dev = fdtbus_phy_device(phy);
476 1.1 jmcneill struct sunxi_hdmiphy_softc * const sc = device_private(dev);
477 1.1 jmcneill u_int pol;
478 1.1 jmcneill int error;
479 1.1 jmcneill
480 1.1 jmcneill pol = 0;
481 1.1 jmcneill if ((mode->flags & DRM_MODE_FLAG_NHSYNC) != 0)
482 1.1 jmcneill pol |= __SHIFTIN(DBG_CTRL_POL_NHSYNC, DBG_CTRL_POL);
483 1.1 jmcneill if ((mode->flags & DRM_MODE_FLAG_NVSYNC) != 0)
484 1.1 jmcneill pol |= __SHIFTIN(DBG_CTRL_POL_NVSYNC, DBG_CTRL_POL);
485 1.1 jmcneill
486 1.1 jmcneill PHY_SET_CLEAR(sc, DBG_CTRL, pol, DBG_CTRL_POL);
487 1.1 jmcneill
488 1.1 jmcneill error = sunxi_hdmiphy_set_rate(sc, mode->crtc_clock * 1000);
489 1.1 jmcneill if (error != 0) {
490 1.1 jmcneill aprint_error_dev(dev, "failed to set HDMI PHY clock: %d\n", error);
491 1.1 jmcneill return error;
492 1.1 jmcneill }
493 1.1 jmcneill
494 1.1 jmcneill return sc->sc_data->config(sc, mode->crtc_clock * 1000);
495 1.1 jmcneill }
496 1.1 jmcneill
497 1.1 jmcneill bool
498 1.1 jmcneill sunxi_hdmiphy_detect(struct fdtbus_phy *phy, bool force)
499 1.1 jmcneill {
500 1.1 jmcneill device_t dev = fdtbus_phy_device(phy);
501 1.1 jmcneill struct sunxi_hdmiphy_softc * const sc = device_private(dev);
502 1.1 jmcneill uint32_t val;
503 1.1 jmcneill
504 1.1 jmcneill val = PHY_READ(sc, ANA_STS);
505 1.1 jmcneill
506 1.1 jmcneill return ISSET(val, ANA_STS_HPDO);
507 1.1 jmcneill }
508