1 /* $OpenBSD: expower.c,v 1.4 2016/07/26 22:10:10 patrick Exp $ */ 2 /* 3 * Copyright (c) 2012-2013 Patrick Wildt <patrick@blueri.se> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/param.h> 19 #include <sys/systm.h> 20 #include <sys/queue.h> 21 #include <sys/malloc.h> 22 #include <sys/sysctl.h> 23 #include <sys/device.h> 24 #include <sys/evcount.h> 25 #include <sys/socket.h> 26 #include <sys/timeout.h> 27 #include <machine/intr.h> 28 #include <machine/bus.h> 29 #if NFDT > 0 30 #include <machine/fdt.h> 31 #endif 32 #include <armv7/armv7/armv7var.h> 33 #include <armv7/exynos/expowervar.h> 34 35 /* registers */ 36 #define POWER_PHY_CTRL 0x708 37 38 /* bits and bytes */ 39 #define POWER_PHY_CTRL_USB_HOST_EN (1 << 0) 40 41 #define HREAD4(sc, reg) \ 42 (bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))) 43 #define HWRITE4(sc, reg, val) \ 44 bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val)) 45 #define HSET4(sc, reg, bits) \ 46 HWRITE4((sc), (reg), HREAD4((sc), (reg)) | (bits)) 47 #define HCLR4(sc, reg, bits) \ 48 HWRITE4((sc), (reg), HREAD4((sc), (reg)) & ~(bits)) 49 50 struct expower_softc { 51 struct device sc_dev; 52 bus_space_tag_t sc_iot; 53 bus_space_handle_t sc_ioh; 54 }; 55 56 struct expower_softc *expower_sc; 57 58 int expower_match(struct device *parent, void *v, void *aux); 59 void expower_attach(struct device *parent, struct device *self, void *args); 60 61 struct cfattach expower_ca = { 62 sizeof (struct expower_softc), NULL, expower_attach 63 }; 64 struct cfattach expower_fdt_ca = { 65 sizeof (struct expower_softc), expower_match, expower_attach 66 }; 67 68 struct cfdriver expower_cd = { 69 NULL, "expower", DV_DULL 70 }; 71 72 int 73 expower_match(struct device *parent, void *v, void *aux) 74 { 75 #if NFDT > 0 76 struct armv7_attach_args *aa = aux; 77 78 if (fdt_node_compatible("samsung,exynos5250-pmu", aa->aa_node)) 79 return 1; 80 #endif 81 82 return 0; 83 } 84 85 void 86 expower_attach(struct device *parent, struct device *self, void *args) 87 { 88 struct armv7_attach_args *aa = args; 89 struct expower_softc *sc = (struct expower_softc *) self; 90 struct armv7mem mem; 91 92 sc->sc_iot = aa->aa_iot; 93 #if NFDT > 0 94 if (aa->aa_node) { 95 struct fdt_reg reg; 96 if (fdt_get_reg(aa->aa_node, 0, ®)) 97 panic("%s: could not extract memory data from FDT", 98 __func__); 99 mem.addr = reg.addr; 100 mem.size = reg.size; 101 } else 102 #endif 103 { 104 mem.addr = aa->aa_dev->mem[0].addr; 105 mem.size = aa->aa_dev->mem[0].size; 106 } 107 if (bus_space_map(sc->sc_iot, mem.addr, mem.size, 0, &sc->sc_ioh)) 108 panic("%s: bus_space_map failed!", __func__); 109 110 printf("\n"); 111 112 expower_sc = sc; 113 } 114 115 void 116 expower_usbhost_phy_ctrl(int on) 117 { 118 struct expower_softc *sc = expower_sc; 119 120 if (on) 121 HSET4(sc, POWER_PHY_CTRL, POWER_PHY_CTRL_USB_HOST_EN); 122 else 123 HCLR4(sc, POWER_PHY_CTRL, POWER_PHY_CTRL_USB_HOST_EN); 124 } 125