1 /* $OpenBSD: qcmtx.c,v 1.1 2023/05/17 23:30:58 patrick Exp $ */ 2 /* 3 * Copyright (c) 2023 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/device.h> 21 #include <sys/malloc.h> 22 23 #include <machine/bus.h> 24 #include <machine/fdt.h> 25 26 #include <dev/ofw/openfirm.h> 27 #include <dev/ofw/ofw_misc.h> 28 #include <dev/ofw/fdt.h> 29 30 #define QCMTX_OFF(idx) ((idx) * 0x1000) 31 #define QCMTX_NUM_LOCKS 32 32 #define QCMTX_APPS_PROC_ID 1 33 34 #define HREAD4(sc, reg) \ 35 (bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))) 36 #define HWRITE4(sc, reg, val) \ 37 bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val)) 38 39 struct qcmtx_softc { 40 struct device sc_dev; 41 bus_space_tag_t sc_iot; 42 bus_space_handle_t sc_ioh; 43 int sc_node; 44 45 struct hwlock_device sc_hd; 46 }; 47 48 int qcmtx_match(struct device *, void *, void *); 49 void qcmtx_attach(struct device *, struct device *, void *); 50 51 int qcmtx_lock(void *, uint32_t *, int); 52 53 const struct cfattach qcmtx_ca = { 54 sizeof (struct qcmtx_softc), qcmtx_match, qcmtx_attach 55 }; 56 57 struct cfdriver qcmtx_cd = { 58 NULL, "qcmtx", DV_DULL 59 }; 60 61 int 62 qcmtx_match(struct device *parent, void *match, void *aux) 63 { 64 struct fdt_attach_args *faa = aux; 65 66 return OF_is_compatible(faa->fa_node, "qcom,tcsr-mutex"); 67 } 68 69 void 70 qcmtx_attach(struct device *parent, struct device *self, void *aux) 71 { 72 struct qcmtx_softc *sc = (struct qcmtx_softc *)self; 73 struct fdt_attach_args *faa = aux; 74 75 if (faa->fa_nreg < 1) { 76 printf(": no registers\n"); 77 return; 78 } 79 80 sc->sc_node = faa->fa_node; 81 sc->sc_iot = faa->fa_iot; 82 if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, 83 faa->fa_reg[0].size, 0, &sc->sc_ioh)) { 84 printf(": can't map registers\n"); 85 return; 86 } 87 88 printf("\n"); 89 90 sc->sc_hd.hd_node = faa->fa_node; 91 sc->sc_hd.hd_cookie = sc; 92 sc->sc_hd.hd_lock = qcmtx_lock; 93 hwlock_register(&sc->sc_hd); 94 } 95 96 int 97 qcmtx_lock(void *cookie, uint32_t *cells, int lock) 98 { 99 struct qcmtx_softc *sc = cookie; 100 int idx = cells[0]; 101 102 if (idx >= QCMTX_NUM_LOCKS) 103 return ENXIO; 104 105 if (lock) { 106 HWRITE4(sc, QCMTX_OFF(idx), QCMTX_APPS_PROC_ID); 107 if (HREAD4(sc, QCMTX_OFF(idx)) != 108 QCMTX_APPS_PROC_ID) 109 return EAGAIN; 110 KASSERT(HREAD4(sc, QCMTX_OFF(idx)) == QCMTX_APPS_PROC_ID); 111 } else { 112 KASSERT(HREAD4(sc, QCMTX_OFF(idx)) == QCMTX_APPS_PROC_ID); 113 HWRITE4(sc, QCMTX_OFF(idx), 0); 114 } 115 116 return 0; 117 } 118