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