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