1 /* $NetBSD: pcfiic_ebus.c,v 1.5 2016/01/03 17:32:17 jdc Exp $ */ 2 /* $OpenBSD: pcfiic_ebus.c,v 1.13 2008/06/08 03:07:40 deraadt Exp $ */ 3 4 /* 5 * Copyright (c) 2006 David Gwynne <dlg@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/cdefs.h> 21 __KERNEL_RCSID(0, "$NetBSD: pcfiic_ebus.c,v 1.5 2016/01/03 17:32:17 jdc Exp $"); 22 23 /* 24 * Device specific driver for the EBus i2c devices found on some sun4u 25 * systems. On systems not having a boot-bus controller the i2c devices 26 * are PCF8584. 27 */ 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/device.h> 32 #include <sys/kernel.h> 33 #include <sys/rwlock.h> 34 35 #include <sys/bus.h> 36 #include <machine/openfirm.h> 37 #include <machine/autoconf.h> 38 39 #include <dev/ebus/ebusreg.h> 40 #include <dev/ebus/ebusvar.h> 41 42 #include <dev/i2c/i2cvar.h> 43 44 #include <dev/ic/pcf8584var.h> 45 #include <dev/ic/pcf8584reg.h> 46 47 int pcfiic_ebus_match(device_t, struct cfdata *, void *); 48 void pcfiic_ebus_attach(device_t, device_t, void *); 49 50 struct pcfiic_ebus_softc { 51 struct pcfiic_softc esc_sc; 52 53 int esc_node; 54 void *esc_ih; 55 }; 56 57 CFATTACH_DECL_NEW(pcfiic, sizeof(struct pcfiic_ebus_softc), 58 pcfiic_ebus_match, pcfiic_ebus_attach, NULL, NULL); 59 60 static prop_array_t create_dict(device_t); 61 static void add_prop(prop_array_t, const char *, const char *, u_int, int); 62 static void envctrl_props(prop_array_t, int); 63 static void envctrltwo_props(prop_array_t, int); 64 65 int 66 pcfiic_ebus_match(device_t parent, struct cfdata *match, void *aux) 67 { 68 struct ebus_attach_args *ea = aux; 69 char compat[32]; 70 71 if (strcmp(ea->ea_name, "SUNW,envctrl") == 0 || 72 strcmp(ea->ea_name, "SUNW,envctrltwo") == 0) 73 return (1); 74 75 if (strcmp(ea->ea_name, "i2c") != 0) 76 return (0); 77 78 if (OF_getprop(ea->ea_node, "compatible", compat, sizeof(compat)) == -1) 79 return (0); 80 81 if (strcmp(compat, "pcf8584") == 0 || 82 strcmp(compat, "i2cpcf,8584") == 0 || 83 strcmp(compat, "SUNW,i2c-pic16f747") == 0 || 84 strcmp(compat, "SUNW,bbc-i2c") == 0) 85 return (1); 86 87 return (0); 88 } 89 90 void 91 pcfiic_ebus_attach(device_t parent, device_t self, void *aux) 92 { 93 struct pcfiic_ebus_softc *esc = device_private(self); 94 struct pcfiic_softc *sc = &esc->esc_sc; 95 struct ebus_attach_args *ea = aux; 96 char compat[32]; 97 u_int64_t addr; 98 u_int8_t clock = PCF8584_CLK_12 | PCF8584_SCL_90; 99 int swapregs = 0; 100 101 if (ea->ea_nreg < 1 || ea->ea_nreg > 2) { 102 printf(": expected 1 or 2 registers, got %d\n", ea->ea_nreg); 103 return; 104 } 105 106 sc->sc_dev = self; 107 if (OF_getprop(ea->ea_node, "compatible", compat, sizeof(compat)) > 0 && 108 strcmp(compat, "SUNW,bbc-i2c") == 0) { 109 /* 110 * On BBC-based machines, Sun swapped the order of 111 * the registers on their clone pcf, plus they feed 112 * it a non-standard clock. 113 */ 114 int clk = prom_getpropint(findroot(), "clock-frequency", 0); 115 116 if (clk < 105000000) 117 clock = PCF8584_CLK_3 | PCF8584_SCL_90; 118 else if (clk < 160000000) 119 clock = PCF8584_CLK_4_43 | PCF8584_SCL_90; 120 swapregs = 1; 121 } 122 123 if (OF_getprop(ea->ea_node, "own-address", &addr, sizeof(addr)) == -1) { 124 addr = 0xaa; 125 } else if (addr == 0x00 || addr > 0xff) { 126 printf(": invalid address on I2C bus"); 127 return; 128 } 129 130 if (bus_space_map(ea->ea_bustag, 131 EBUS_ADDR_FROM_REG(&ea->ea_reg[0]), 132 ea->ea_reg[0].size, 0, &sc->sc_ioh) == 0) { 133 sc->sc_iot = ea->ea_bustag; 134 } else { 135 printf(": can't map register space\n"); 136 return; 137 } 138 139 if (ea->ea_nreg == 2) { 140 /* 141 * Second register only occurs on BBC-based machines, 142 * and is likely not prom mapped 143 */ 144 if (bus_space_map(sc->sc_iot, EBUS_ADDR_FROM_REG(&ea->ea_reg[1]), 145 ea->ea_reg[1].size, 0, &sc->sc_ioh2) != 0) { 146 printf(": can't map 2nd register space\n"); 147 return; 148 } 149 sc->sc_master = 1; 150 printf(": iic mux present"); 151 } 152 153 if (ea->ea_nintr >= 1) 154 esc->esc_ih = bus_intr_establish(sc->sc_iot, ea->ea_intr[0], 155 IPL_BIO, pcfiic_intr, sc); 156 else 157 esc->esc_ih = NULL; 158 159 160 if (esc->esc_ih == NULL) 161 sc->sc_poll = 1; 162 163 if (strcmp(ea->ea_name, "SUNW,envctrl") == 0) { 164 envctrl_props(create_dict(self), ea->ea_node); 165 pcfiic_attach(sc, 0x55, PCF8584_CLK_12 | PCF8584_SCL_45, 0); 166 } else if (strcmp(ea->ea_name, "SUNW,envctrltwo") == 0) { 167 envctrltwo_props(create_dict(self), ea->ea_node); 168 pcfiic_attach(sc, 0x55, PCF8584_CLK_12 | PCF8584_SCL_45, 0); 169 } else 170 pcfiic_attach(sc, (i2c_addr_t)(addr >> 1), clock, swapregs); 171 } 172 173 static prop_array_t 174 create_dict(device_t parent) 175 { 176 prop_dictionary_t props = device_properties(parent); 177 prop_array_t cfg = prop_dictionary_get(props, "i2c-child-devices"); 178 if (cfg) return cfg; 179 cfg = prop_array_create(); 180 prop_dictionary_set(props, "i2c-child-devices", cfg); 181 prop_object_release(cfg); 182 return cfg; 183 } 184 185 static void 186 add_prop(prop_array_t c, const char *name, const char *compat, u_int addr, 187 int node) 188 { 189 prop_dictionary_t dev; 190 prop_data_t data; 191 192 dev = prop_dictionary_create(); 193 prop_dictionary_set_cstring(dev, "name", name); 194 data = prop_data_create_data(compat, strlen(compat)+1); 195 prop_dictionary_set(dev, "compatible", data); 196 prop_object_release(data); 197 prop_dictionary_set_uint32(dev, "addr", addr); 198 prop_dictionary_set_uint64(dev, "cookie", node); 199 prop_array_add(c, dev); 200 prop_object_release(dev); 201 } 202 203 static void 204 envctrl_props(prop_array_t c, int node) 205 { 206 /* Power supply 1 temperature. */ 207 add_prop(c, "PSU-1", "ecadc", 0x48, node); 208 209 /* Power supply 2 termperature. */ 210 add_prop(c, "PSU-2", "ecadc", 0x49, node); 211 212 /* Power supply 3 tempterature. */ 213 add_prop(c, "PSU-3", "ecadc", 0x4a, node); 214 215 /* Ambient tempterature. */ 216 add_prop(c, "ambient", "i2c-lm75", 0x4d, node); 217 218 /* CPU temperatures. */ 219 add_prop(c, "CPU", "ecadc", 0x4f, node); 220 } 221 222 static void 223 envctrltwo_props(prop_array_t c, int node) 224 { 225 add_prop(c, "PSU", "ecadc", 0x4a, node); 226 add_prop(c, "CPU", "ecadc", 0x4f, node); 227 } 228