1 /* $OpenBSD: gscsio.c,v 1.8 2006/01/05 10:43:15 grange Exp $ */ 2 /* 3 * Copyright (c) 2004 Alexander Yurchenko <grange@openbsd.org> 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 /* 19 * National Semiconductor Geode SC1100 Super I/O. 20 * Only ACCESS.bus logical device is supported. 21 */ 22 23 #include <sys/param.h> 24 #include <sys/systm.h> 25 #include <sys/device.h> 26 #include <sys/kernel.h> 27 #include <sys/lock.h> 28 #include <sys/proc.h> 29 30 #include <machine/bus.h> 31 32 #include <dev/i2c/i2cvar.h> 33 34 #include <dev/isa/isareg.h> 35 #include <dev/isa/isavar.h> 36 37 #include <dev/isa/gscsioreg.h> 38 39 struct gscsio_softc { 40 struct device sc_dev; 41 42 bus_space_tag_t sc_iot; 43 bus_space_handle_t sc_ioh; 44 45 int sc_ld_en[GSCSIO_LDNUM]; 46 bus_space_handle_t sc_ld_ioh0[GSCSIO_LDNUM]; 47 bus_space_handle_t sc_ld_ioh1[GSCSIO_LDNUM]; 48 49 /* ACCESS.bus */ 50 struct gscsio_acb { 51 void *sc; 52 bus_space_handle_t ioh; 53 struct lock buslock; 54 } sc_acb[2]; 55 struct i2c_controller sc_acb1_tag; 56 struct i2c_controller sc_acb2_tag; 57 }; 58 59 /* Supported logical devices description */ 60 static const struct { 61 const char *ld_name; 62 int ld_num; 63 int ld_iosize0; 64 int ld_iosize1; 65 } gscsio_ld[] = { 66 { "ACB1", GSCSIO_LDN_ACB1, 6, 0 }, 67 { "ACB2", GSCSIO_LDN_ACB2, 6, 0 }, 68 }; 69 70 int gscsio_probe(struct device *, void *, void *); 71 void gscsio_attach(struct device *, struct device *, void *); 72 73 void gscsio_acb_init(struct gscsio_acb *, i2c_tag_t); 74 int gscsio_acb_wait(struct gscsio_acb *, int, int); 75 void gscsio_acb_reset(struct gscsio_acb *acb); 76 77 int gscsio_acb_acquire_bus(void *, int); 78 void gscsio_acb_release_bus(void *, int); 79 int gscsio_acb_send_start(void *, int); 80 int gscsio_acb_send_stop(void *, int); 81 int gscsio_acb_initiate_xfer(void *, uint16_t, int); 82 int gscsio_acb_read_byte(void *, uint8_t *, int); 83 int gscsio_acb_write_byte(void *, uint8_t, int); 84 85 struct cfattach gscsio_ca = { 86 sizeof(struct gscsio_softc), 87 gscsio_probe, 88 gscsio_attach 89 }; 90 91 struct cfdriver gscsio_cd = { 92 NULL, "gscsio", DV_DULL 93 }; 94 95 #define ACB_READ(reg) \ 96 bus_space_read_1(sc->sc_iot, acb->ioh, (reg)) 97 #define ACB_WRITE(reg, val) \ 98 bus_space_write_1(sc->sc_iot, acb->ioh, (reg), (val)) 99 100 static __inline u_int8_t 101 idxread(bus_space_tag_t iot, bus_space_handle_t ioh, int idx) 102 { 103 bus_space_write_1(iot, ioh, GSCSIO_IDX, idx); 104 105 return (bus_space_read_1(iot, ioh, GSCSIO_DAT)); 106 } 107 108 static __inline void 109 idxwrite(bus_space_tag_t iot, bus_space_handle_t ioh, int idx, u_int8_t data) 110 { 111 bus_space_write_1(iot, ioh, GSCSIO_IDX, idx); 112 bus_space_write_1(iot, ioh, GSCSIO_DAT, data); 113 } 114 115 int 116 gscsio_probe(struct device *parent, void *match, void *aux) 117 { 118 struct isa_attach_args *ia = aux; 119 bus_space_tag_t iot; 120 bus_space_handle_t ioh; 121 int iobase; 122 int rv = 0; 123 124 iot = ia->ia_iot; 125 iobase = ia->ipa_io[0].base; 126 if (bus_space_map(iot, iobase, GSCSIO_IOSIZE, 0, &ioh)) 127 return (0); 128 if (idxread(iot, ioh, GSCSIO_ID) == GSCSIO_ID_SC1100) 129 rv = 1; 130 bus_space_unmap(iot, ioh, GSCSIO_IOSIZE); 131 132 if (rv) { 133 ia->ipa_nio = 1; 134 ia->ipa_io[0].length = GSCSIO_IOSIZE; 135 ia->ipa_nmem = 0; 136 ia->ipa_nirq = 0; 137 ia->ipa_ndrq = 0; 138 } 139 140 return (rv); 141 } 142 143 void 144 gscsio_attach(struct device *parent, struct device *self, void *aux) 145 { 146 struct gscsio_softc *sc = (void *)self; 147 struct isa_attach_args *ia = aux; 148 int i; 149 int iobase; 150 151 sc->sc_iot = ia->ia_iot; 152 if (bus_space_map(sc->sc_iot, ia->ipa_io[0].base, GSCSIO_IOSIZE, 153 0, &sc->sc_ioh)) { 154 printf(": can't map I/O space\n"); 155 return; 156 } 157 printf(": SC1100 SIO rev %d:", 158 idxread(sc->sc_iot, sc->sc_ioh, GSCSIO_REV)); 159 160 /* Configure all supported logical devices */ 161 for (i = 0; i < sizeof (gscsio_ld) / sizeof(gscsio_ld[0]); i++) { 162 sc->sc_ld_en[gscsio_ld[i].ld_num] = 0; 163 164 /* Select the device and check if it's activated */ 165 idxwrite(sc->sc_iot, sc->sc_ioh, GSCSIO_LDN, 166 gscsio_ld[i].ld_num); 167 if ((idxread(sc->sc_iot, sc->sc_ioh, GSCSIO_ACT) & 168 GSCSIO_ACT_EN) == 0) 169 continue; 170 171 /* Map I/O space 0 if necessary */ 172 if (gscsio_ld[i].ld_iosize0 != 0) { 173 iobase = idxread(sc->sc_iot, sc->sc_ioh, 174 GSCSIO_IO0_MSB); 175 iobase <<= 8; 176 iobase |= idxread(sc->sc_iot, sc->sc_ioh, 177 GSCSIO_IO0_LSB); 178 if (bus_space_map(sc->sc_iot, iobase, 179 gscsio_ld[i].ld_iosize0, 0, 180 &sc->sc_ld_ioh0[gscsio_ld[i].ld_num])) 181 continue; 182 } 183 184 /* Map I/O space 1 if necessary */ 185 if (gscsio_ld[i].ld_iosize1 != 0) { 186 iobase = idxread(sc->sc_iot, sc->sc_ioh, 187 GSCSIO_IO1_MSB); 188 iobase <<= 8; 189 iobase |= idxread(sc->sc_iot, sc->sc_ioh, 190 GSCSIO_IO1_LSB); 191 if (bus_space_map(sc->sc_iot, iobase, 192 gscsio_ld[i].ld_iosize1, 0, 193 &sc->sc_ld_ioh0[gscsio_ld[i].ld_num])) { 194 bus_space_unmap(sc->sc_iot, 195 sc->sc_ld_ioh0[gscsio_ld[i].ld_num], 196 gscsio_ld[i].ld_iosize0); 197 continue; 198 } 199 } 200 201 sc->sc_ld_en[gscsio_ld[i].ld_num] = 1; 202 printf(" %s", gscsio_ld[i].ld_name); 203 } 204 printf("\n"); 205 206 /* Initialize ACCESS.bus 1 */ 207 if (sc->sc_ld_en[GSCSIO_LDN_ACB1]) { 208 sc->sc_acb[0].sc = sc; 209 sc->sc_acb[0].ioh = sc->sc_ld_ioh0[GSCSIO_LDN_ACB1]; 210 lockinit(&sc->sc_acb[0].buslock, PRIBIO | PCATCH, 211 "iiclk", 0, 0); 212 gscsio_acb_init(&sc->sc_acb[0], &sc->sc_acb1_tag); 213 } 214 215 /* Initialize ACCESS.bus 2 */ 216 if (sc->sc_ld_en[GSCSIO_LDN_ACB2]) { 217 sc->sc_acb[1].sc = sc; 218 sc->sc_acb[1].ioh = sc->sc_ld_ioh0[GSCSIO_LDN_ACB2]; 219 lockinit(&sc->sc_acb[1].buslock, PRIBIO | PCATCH, 220 "iiclk", 0, 0); 221 gscsio_acb_init(&sc->sc_acb[1], &sc->sc_acb2_tag); 222 } 223 } 224 225 void 226 gscsio_acb_init(struct gscsio_acb *acb, i2c_tag_t tag) 227 { 228 struct gscsio_softc *sc = acb->sc; 229 struct i2cbus_attach_args iba; 230 231 /* Enable ACB and configure clock frequency */ 232 ACB_WRITE(GSCSIO_ACB_CTL2, GSCSIO_ACB_CTL2_EN | 233 (GSCSIO_ACB_FREQ << GSCSIO_ACB_CTL2_FREQ_SHIFT)); 234 235 /* Select polling mode */ 236 ACB_WRITE(GSCSIO_ACB_CTL1, ACB_READ(GSCSIO_ACB_CTL1) & 237 ~GSCSIO_ACB_CTL1_INTEN); 238 239 /* Disable slave address */ 240 ACB_WRITE(GSCSIO_ACB_ADDR, ACB_READ(GSCSIO_ACB_ADDR) & 241 ~GSCSIO_ACB_ADDR_SAEN); 242 243 /* Attach I2C framework */ 244 tag->ic_cookie = acb; 245 tag->ic_acquire_bus = gscsio_acb_acquire_bus; 246 tag->ic_release_bus = gscsio_acb_release_bus; 247 tag->ic_send_start = gscsio_acb_send_start; 248 tag->ic_send_stop = gscsio_acb_send_stop; 249 tag->ic_initiate_xfer = gscsio_acb_initiate_xfer; 250 tag->ic_read_byte = gscsio_acb_read_byte; 251 tag->ic_write_byte = gscsio_acb_write_byte; 252 253 bzero(&iba, sizeof(iba)); 254 iba.iba_name = "iic"; 255 iba.iba_tag = tag; 256 config_found(&sc->sc_dev, &iba, iicbus_print); 257 } 258 259 int 260 gscsio_acb_wait(struct gscsio_acb *acb, int bits, int flags) 261 { 262 struct gscsio_softc *sc = acb->sc; 263 u_int8_t st; 264 int i; 265 266 for (i = 0; i < 100; i++) { 267 st = ACB_READ(GSCSIO_ACB_ST); 268 if (st & GSCSIO_ACB_ST_BER) { 269 printf("%s: bus error, flags=0x%x\n", 270 sc->sc_dev.dv_xname, flags); 271 gscsio_acb_reset(acb); 272 return (EIO); 273 } 274 if (st & GSCSIO_ACB_ST_NEGACK) { 275 #if 0 276 printf("%s: negative ack, flags=0x%x\n", 277 sc->sc_dev.dv_xname, flags); 278 #endif 279 gscsio_acb_reset(acb); 280 return (EIO); 281 } 282 if ((st & bits) == bits) 283 break; 284 delay(10); 285 } 286 if ((st & bits) != bits) { 287 printf("%s: timeout, flags=0x%x\n", 288 sc->sc_dev.dv_xname, flags); 289 gscsio_acb_reset(acb); 290 return (ETIMEDOUT); 291 } 292 293 return (0); 294 } 295 296 void 297 gscsio_acb_reset(struct gscsio_acb *acb) 298 { 299 struct gscsio_softc *sc = acb->sc; 300 u_int8_t st, ctl; 301 302 /* Clear MASTER, NEGACK and BER */ 303 st = ACB_READ(GSCSIO_ACB_ST); 304 st |= GSCSIO_ACB_ST_MASTER | GSCSIO_ACB_ST_NEGACK | GSCSIO_ACB_ST_BER; 305 ACB_WRITE(GSCSIO_ACB_ST, st); 306 307 /* Disable and re-enable ACB */ 308 ACB_WRITE(GSCSIO_ACB_CTL2, 0); 309 ACB_WRITE(GSCSIO_ACB_CTL2, GSCSIO_ACB_CTL2_EN | 310 (GSCSIO_ACB_FREQ << GSCSIO_ACB_CTL2_FREQ_SHIFT)); 311 312 /* Send stop */ 313 ctl = ACB_READ(GSCSIO_ACB_CTL1); 314 ctl |= GSCSIO_ACB_CTL1_STOP; 315 ACB_WRITE(GSCSIO_ACB_CTL1, ctl); 316 } 317 318 int 319 gscsio_acb_acquire_bus(void *cookie, int flags) 320 { 321 struct gscsio_acb *acb = cookie; 322 323 if (cold || flags & I2C_F_POLL) 324 return (0); 325 326 return (lockmgr(&acb->buslock, LK_EXCLUSIVE, NULL)); 327 } 328 329 void 330 gscsio_acb_release_bus(void *cookie, int flags) 331 { 332 struct gscsio_acb *acb = cookie; 333 334 if (cold || flags & I2C_F_POLL) 335 return; 336 337 lockmgr(&acb->buslock, LK_RELEASE, NULL); 338 } 339 340 int 341 gscsio_acb_send_start(void *cookie, int flags) 342 { 343 struct gscsio_acb *acb = cookie; 344 struct gscsio_softc *sc = acb->sc; 345 u_int8_t ctl; 346 347 ctl = ACB_READ(GSCSIO_ACB_CTL1); 348 ctl |= GSCSIO_ACB_CTL1_START; 349 ACB_WRITE(GSCSIO_ACB_CTL1, ctl); 350 351 return (0); 352 } 353 354 int 355 gscsio_acb_send_stop(void *cookie, int flags) 356 { 357 struct gscsio_acb *acb = cookie; 358 struct gscsio_softc *sc = acb->sc; 359 u_int8_t ctl; 360 361 ctl = ACB_READ(GSCSIO_ACB_CTL1); 362 ctl |= GSCSIO_ACB_CTL1_STOP; 363 ACB_WRITE(GSCSIO_ACB_CTL1, ctl); 364 365 return (0); 366 } 367 368 int 369 gscsio_acb_initiate_xfer(void *cookie, uint16_t addr, int flags) 370 { 371 struct gscsio_acb *acb = cookie; 372 struct gscsio_softc *sc = acb->sc; 373 u_int8_t ctl; 374 int dir; 375 int error; 376 377 /* Issue start condition */ 378 ctl = ACB_READ(GSCSIO_ACB_CTL1); 379 ctl |= GSCSIO_ACB_CTL1_START; 380 ACB_WRITE(GSCSIO_ACB_CTL1, ctl); 381 382 /* Wait for bus mastership */ 383 if ((error = gscsio_acb_wait(acb, 384 GSCSIO_ACB_ST_MASTER | GSCSIO_ACB_ST_SDAST, flags))) 385 return (error); 386 387 /* Send address byte */ 388 dir = (flags & I2C_F_READ ? 1 : 0); 389 ACB_WRITE(GSCSIO_ACB_SDA, (addr << 1) | dir); 390 391 return (0); 392 } 393 394 int 395 gscsio_acb_read_byte(void *cookie, uint8_t *bytep, int flags) 396 { 397 struct gscsio_acb *acb = cookie; 398 struct gscsio_softc *sc = acb->sc; 399 u_int8_t ctl; 400 int error; 401 402 /* Wait for the bus to be ready */ 403 if ((error = gscsio_acb_wait(acb, GSCSIO_ACB_ST_SDAST, flags))) 404 return (error); 405 406 /* Acknowledge the last byte */ 407 if (flags & I2C_F_LAST) { 408 ctl = ACB_READ(GSCSIO_ACB_CTL1); 409 ctl |= GSCSIO_ACB_CTL1_ACK; 410 ACB_WRITE(GSCSIO_ACB_CTL1, ctl); 411 } 412 413 /* Read data byte */ 414 *bytep = ACB_READ(GSCSIO_ACB_SDA); 415 416 return (0); 417 } 418 419 int 420 gscsio_acb_write_byte(void *cookie, uint8_t byte, int flags) 421 { 422 struct gscsio_acb *acb = cookie; 423 struct gscsio_softc *sc = acb->sc; 424 u_int8_t ctl; 425 int error; 426 427 /* Wait for the bus to be ready */ 428 if ((error = gscsio_acb_wait(acb, GSCSIO_ACB_ST_SDAST, flags))) 429 return (error); 430 431 /* Send stop after the last byte */ 432 if (flags & I2C_F_STOP) { 433 ctl = ACB_READ(GSCSIO_ACB_CTL1); 434 ctl |= GSCSIO_ACB_CTL1_STOP; 435 ACB_WRITE(GSCSIO_ACB_CTL1, ctl); 436 } 437 438 /* Write data byte */ 439 ACB_WRITE(GSCSIO_ACB_SDA, byte); 440 441 return (0); 442 } 443