1 /* $OpenBSD: dwiic_fdt.c,v 1.2 2024/03/29 22:08:09 kettenis 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/device.h> 20 #include <sys/kernel.h> 21 #include <sys/systm.h> 22 23 #include <machine/bus.h> 24 #include <machine/fdt.h> 25 26 #include <dev/ofw/openfirm.h> 27 #include <dev/ofw/ofw_clock.h> 28 #include <dev/ofw/ofw_pinctrl.h> 29 #include <dev/ofw/fdt.h> 30 31 #include <dev/ic/dwiicvar.h> 32 33 static inline uint32_t 34 round_closest(uint64_t num, uint64_t den) 35 { 36 return (num + (den / 2)) / den; 37 } 38 39 struct dwiic_fdt_softc { 40 struct dwiic_softc sc_sc; 41 int sc_node; 42 }; 43 44 int dwiic_fdt_match(struct device *, void *, void *); 45 void dwiic_fdt_attach(struct device *, struct device *, void *); 46 47 const struct cfattach dwiic_fdt_ca = { 48 sizeof(struct dwiic_fdt_softc), dwiic_fdt_match, dwiic_fdt_attach 49 }; 50 51 void dwiic_fdt_calc_timings(struct dwiic_fdt_softc *); 52 void dwiic_fdt_bus_scan(struct device *, struct i2cbus_attach_args *, 53 void *); 54 55 int 56 dwiic_fdt_match(struct device *parent, void *match, void *aux) 57 { 58 struct fdt_attach_args *faa = aux; 59 60 return OF_is_compatible(faa->fa_node, "snps,designware-i2c"); 61 } 62 63 void 64 dwiic_fdt_attach(struct device *parent, struct device *self, void *aux) 65 { 66 struct dwiic_fdt_softc *fsc = (struct dwiic_fdt_softc *)self; 67 struct dwiic_softc *sc = &fsc->sc_sc; 68 struct fdt_attach_args *faa = aux; 69 struct i2cbus_attach_args iba; 70 71 if (faa->fa_nreg < 1) 72 return; 73 74 sc->sc_iot = faa->fa_iot; 75 fsc->sc_node = faa->fa_node; 76 if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, 77 faa->fa_reg[0].size, 0, &sc->sc_ioh)) { 78 printf(": can't map registers\n"); 79 return; 80 } 81 82 pinctrl_byname(faa->fa_node, "default"); 83 reset_deassert_all(faa->fa_node); 84 clock_enable(faa->fa_node, NULL); 85 86 dwiic_fdt_calc_timings(fsc); 87 88 if (dwiic_init(sc)) { 89 printf(": can't initialize\n"); 90 bus_space_unmap(sc->sc_iot, sc->sc_ioh, faa->fa_reg[0].size); 91 return; 92 } 93 94 /* leave the controller disabled */ 95 dwiic_write(sc, DW_IC_INTR_MASK, 0); 96 dwiic_enable(sc, 0); 97 dwiic_read(sc, DW_IC_CLR_INTR); 98 99 sc->sc_ih = fdt_intr_establish(faa->fa_node, IPL_BIO, 100 dwiic_intr, sc, sc->sc_dev.dv_xname); 101 102 printf("\n"); 103 104 rw_init(&sc->sc_i2c_lock, "dwiic"); 105 106 sc->sc_i2c_tag.ic_cookie = sc; 107 sc->sc_i2c_tag.ic_acquire_bus = dwiic_i2c_acquire_bus; 108 sc->sc_i2c_tag.ic_release_bus = dwiic_i2c_release_bus; 109 sc->sc_i2c_tag.ic_exec = dwiic_i2c_exec; 110 111 bzero(&iba, sizeof iba); 112 iba.iba_name = "iic"; 113 iba.iba_tag = &sc->sc_i2c_tag; 114 iba.iba_bus_scan = dwiic_fdt_bus_scan; 115 iba.iba_bus_scan_arg = &fsc->sc_node; 116 config_found(&sc->sc_dev, &iba, iicbus_print); 117 } 118 119 void 120 dwiic_fdt_calc_timings(struct dwiic_fdt_softc *fsc) 121 { 122 struct dwiic_softc *sc = &fsc->sc_sc; 123 uint32_t sda_hold, sda_fall, scl_fall; 124 uint64_t freq; 125 126 freq = clock_get_frequency(fsc->sc_node, NULL); 127 if (freq == 0) 128 return; 129 130 sda_hold = OF_getpropint(fsc->sc_node, "i2c-sda-hold-time-ns", 300); 131 sda_fall = OF_getpropint(fsc->sc_node, "i2c-sda-falling-time-ns", 300); 132 scl_fall = OF_getpropint(fsc->sc_node, "i2c-scl-falling-time-ns", 300); 133 134 sc->sda_hold_time = round_closest(freq * sda_hold, 1000000000); 135 136 /* Standard-mode: tHIGH = 4.0 us; tLOW = 4.7 us */ 137 sc->ss_hcnt = round_closest(freq * (4000 + sda_fall), 1000000000) - 3; 138 sc->ss_lcnt = round_closest(freq * (4700 + scl_fall), 1000000000) - 1; 139 /* Fast-mode: tHIGH = 0.6 us; tLOW = 1.3 us */ 140 sc->fs_hcnt = round_closest(freq * (600 + sda_fall), 1000000000) - 3; 141 sc->fs_lcnt = round_closest(freq * (1300 + scl_fall), 1000000000) - 1; 142 } 143 144 void 145 dwiic_fdt_bus_scan(struct device *self, struct i2cbus_attach_args *iba, 146 void *aux) 147 { 148 int iba_node = *(int *)aux; 149 extern int iic_print(void *, const char *); 150 struct i2c_attach_args ia; 151 char name[32], status[32]; 152 uint32_t reg[1]; 153 int node; 154 155 for (node = OF_child(iba_node); node; node = OF_peer(node)) { 156 memset(name, 0, sizeof(name)); 157 memset(status, 0, sizeof(status)); 158 memset(reg, 0, sizeof(reg)); 159 160 if (OF_getprop(node, "compatible", name, sizeof(name)) == -1) 161 continue; 162 if (name[0] == '\0') 163 continue; 164 165 if (OF_getprop(node, "status", status, sizeof(status)) > 0 && 166 strcmp(status, "disabled") == 0) 167 continue; 168 169 if (OF_getprop(node, "reg", ®, sizeof(reg)) != sizeof(reg)) 170 continue; 171 172 memset(&ia, 0, sizeof(ia)); 173 ia.ia_tag = iba->iba_tag; 174 ia.ia_addr = bemtoh32(®[0]); 175 ia.ia_name = name; 176 ia.ia_cookie = &node; 177 178 config_found(self, &ia, iic_print); 179 } 180 } 181