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