1 /*- 2 * Copyright (c) 1998 Nicolas Souchu, Marc Bouget 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $Id: pcf.c,v 1.3 1998/10/31 11:37:09 nsouch Exp $ 27 * 28 */ 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/kernel.h> 32 #include <sys/module.h> 33 #include <sys/bus.h> 34 #include <sys/conf.h> 35 #include <sys/malloc.h> 36 37 #include <machine/clock.h> 38 39 #include <i386/isa/isa_device.h> 40 41 #include <dev/iicbus/iiconf.h> 42 #include "iicbus_if.h" 43 44 #define TIMEOUT 9999 /* XXX */ 45 46 /* Status bits of S1 register (read only) */ 47 #define nBB 0x01 /* busy when low set/reset by STOP/START*/ 48 #define LAB 0x02 /* lost arbitration bit in multi-master mode */ 49 #define AAS 0x04 /* addressed as slave */ 50 #define LRB 0x08 /* last received byte when not AAS */ 51 #define AD0 0x08 /* general call received when AAS */ 52 #define BER 0x10 /* bus error, misplaced START or STOP */ 53 #define STS 0x20 /* STOP detected in slave receiver mode */ 54 #define PIN 0x80 /* pending interrupt not (r/w) */ 55 56 /* Control bits of S1 register (write only) */ 57 #define ACK 0x01 58 #define STO 0x02 59 #define STA 0x04 60 #define ENI 0x08 61 #define ES2 0x10 62 #define ES1 0x20 63 #define ES0 0x40 64 65 #define BUFSIZE 2048 66 67 #define SLAVE_TRANSMITTER 0x1 68 #define SLAVE_RECEIVER 0x2 69 70 #define PCF_DEFAULT_ADDR 0xaa 71 72 struct pcf_softc { 73 74 int pcf_base; /* isa port */ 75 u_char pcf_addr; /* interface I2C address */ 76 77 int pcf_slave_mode; /* receiver or transmitter */ 78 int pcf_started; /* 1 if start condition sent */ 79 80 device_t iicbus; /* the corresponding iicbus */ 81 }; 82 83 struct pcf_isa_softc { 84 85 int pcf_unit; /* unit of the isa device */ 86 int pcf_base; /* isa port */ 87 int pcf_irq; /* isa irq or null if polled */ 88 89 unsigned int pcf_flags; /* boot flags */ 90 }; 91 92 #define MAXPCF 2 93 94 static struct pcf_isa_softc *pcfdata[MAXPCF]; 95 static npcf = 0; 96 97 static int pcfprobe_isa(struct isa_device *); 98 static int pcfattach_isa(struct isa_device *); 99 100 struct isa_driver pcfdriver = { 101 pcfprobe_isa, pcfattach_isa, "pcf" 102 }; 103 104 static int pcf_probe(device_t); 105 static int pcf_attach(device_t); 106 static void pcf_print_child(device_t, device_t); 107 108 static int pcf_repeated_start(device_t, u_char, int); 109 static int pcf_start(device_t, u_char, int); 110 static int pcf_stop(device_t); 111 static int pcf_write(device_t, char *, int, int *, int); 112 static int pcf_read(device_t, char *, int, int *, int, int); 113 static ointhand2_t pcfintr; 114 static int pcf_rst_card(device_t, u_char, u_char, u_char *); 115 116 static device_method_t pcf_methods[] = { 117 /* device interface */ 118 DEVMETHOD(device_probe, pcf_probe), 119 DEVMETHOD(device_attach, pcf_attach), 120 121 /* bus interface */ 122 DEVMETHOD(bus_print_child, pcf_print_child), 123 124 /* iicbus interface */ 125 DEVMETHOD(iicbus_callback, iicbus_null_callback), 126 DEVMETHOD(iicbus_repeated_start, pcf_repeated_start), 127 DEVMETHOD(iicbus_start, pcf_start), 128 DEVMETHOD(iicbus_stop, pcf_stop), 129 DEVMETHOD(iicbus_write, pcf_write), 130 DEVMETHOD(iicbus_read, pcf_read), 131 DEVMETHOD(iicbus_reset, pcf_rst_card), 132 133 { 0, 0 } 134 }; 135 136 static driver_t pcf_driver = { 137 "pcf", 138 pcf_methods, 139 DRIVER_TYPE_MISC, 140 sizeof(struct pcf_softc), 141 }; 142 143 static devclass_t pcf_devclass; 144 145 #define DEVTOSOFTC(dev) ((struct pcf_softc *)device_get_softc(dev)) 146 147 static int 148 pcfprobe_isa(struct isa_device *dvp) 149 { 150 device_t pcfdev; 151 struct pcf_isa_softc *pcf; 152 int error; 153 154 if (npcf >= MAXPCF) 155 return (0); 156 157 if ((pcf = (struct pcf_isa_softc *)malloc(sizeof(struct pcf_isa_softc), 158 M_DEVBUF, M_NOWAIT)) == NULL) 159 return (0); 160 161 pcf->pcf_base = dvp->id_iobase; /* XXX should be ivars */ 162 pcf->pcf_unit = dvp->id_unit; 163 164 if (!(dvp->id_flags & IIC_POLLED)) 165 pcf->pcf_irq = (dvp->id_irq); 166 167 pcfdata[npcf++] = pcf; 168 169 /* XXX add the pcf device to the root_bus until isa bus exists */ 170 pcfdev = device_add_child(root_bus, "pcf", pcf->pcf_unit, NULL); 171 device_probe_and_attach(pcfdev); 172 173 if (!pcfdev) 174 goto error; 175 176 end_probe: 177 return (1); 178 179 error: 180 free(pcf, M_DEVBUF); 181 return (0); 182 } 183 184 static int 185 pcfattach_isa(struct isa_device *isdp) 186 { 187 isdp->id_ointr = pcfintr; 188 return (1); /* ok */ 189 } 190 191 static int 192 pcf_probe(device_t pcfdev) 193 { 194 struct pcf_softc *pcf = (struct pcf_softc *)device_get_softc(pcfdev); 195 int unit = device_get_unit(pcfdev); 196 197 /* retrieve base address from isa initialization 198 * 199 * XXX should use ivars with isabus 200 */ 201 pcf->pcf_base = pcfdata[unit]->pcf_base; 202 203 /* reset the chip */ 204 pcf_rst_card(pcfdev, IIC_FASTEST, PCF_DEFAULT_ADDR, NULL); 205 206 /* XXX try do detect chipset */ 207 208 device_set_desc(pcfdev, "PCF8584 I2C bus controller"); 209 210 return (0); 211 } 212 213 static int 214 pcf_attach(device_t pcfdev) 215 { 216 struct pcf_softc *pcf = (struct pcf_softc *)device_get_softc(pcfdev); 217 218 pcf->iicbus = iicbus_alloc_bus(pcfdev); 219 220 if (!pcf->iicbus) 221 return (EINVAL); 222 223 /* probe and attach the iicbus */ 224 device_probe_and_attach(pcf->iicbus); 225 226 return (0); 227 } 228 229 static void 230 pcf_print_child(device_t bus, device_t dev) 231 { 232 struct pcf_softc *pcf = (struct pcf_softc *)device_get_softc(bus); 233 234 printf(" on %s%d addr 0x%x", device_get_name(bus), 235 device_get_unit(bus), (int)pcf->pcf_addr); 236 237 return; 238 } 239 240 /* 241 * PCF8584 datasheet : when operate at 8 MHz or more, a minimun time of 242 * 6 clocks cycles must be left between two consecutives access 243 */ 244 #define pcf_nops() DELAY(10) 245 246 #define dummy_read(pcf) PCF_GET_S0(pcf) 247 #define dummy_write(pcf) PCF_SET_S0(pcf, 0) 248 249 /* 250 * Specific register access to PCF8584 251 */ 252 static void PCF_SET_S0(struct pcf_softc *pcf, int data) 253 { 254 outb(pcf->pcf_base, data); 255 pcf_nops(); 256 } 257 258 static void PCF_SET_S1(struct pcf_softc *pcf, int data) 259 { 260 outb(pcf->pcf_base+1, data); 261 pcf_nops(); 262 } 263 264 static char PCF_GET_S0(struct pcf_softc *pcf) 265 { 266 char data; 267 268 data = inb(pcf->pcf_base); 269 pcf_nops(); 270 271 return (data); 272 } 273 274 static char PCF_GET_S1(struct pcf_softc *pcf) 275 { 276 char data; 277 278 data = inb(pcf->pcf_base+1); 279 pcf_nops(); 280 281 return (data); 282 } 283 284 /* 285 * Polling mode for master operations wait for a new 286 * byte incomming or outgoing 287 */ 288 static int pcf_wait_byte(struct pcf_softc *pcf) 289 { 290 int counter = TIMEOUT; 291 292 while (counter--) { 293 294 if ((PCF_GET_S1(pcf) & PIN) == 0) 295 return (0); 296 } 297 298 return (IIC_ETIMEOUT); 299 } 300 301 static int pcf_stop(device_t pcfdev) 302 { 303 struct pcf_softc *pcf = DEVTOSOFTC(pcfdev); 304 305 /* 306 * Send STOP condition iff the START condition was previously sent. 307 * STOP is sent only once even if a iicbus_stop() is called after 308 * an iicbus_read()... see pcf_read(): the pcf needs to send the stop 309 * before the last char is read. 310 */ 311 if (pcf->pcf_started) { 312 /* set stop condition and enable IT */ 313 PCF_SET_S1(pcf, PIN|ES0|ENI|STO|ACK); 314 315 pcf->pcf_started = 0; 316 } 317 318 return (0); 319 } 320 321 322 static int pcf_noack(struct pcf_softc *pcf, int timeout) 323 { 324 int noack; 325 int k = timeout/10; 326 327 do { 328 noack = PCF_GET_S1(pcf) & LRB; 329 if (!noack) 330 break; 331 DELAY(10); /* XXX wait 10 us */ 332 } while (k--); 333 334 return (noack); 335 } 336 337 static int pcf_repeated_start(device_t pcfdev, u_char slave, int timeout) 338 { 339 struct pcf_softc *pcf = DEVTOSOFTC(pcfdev); 340 int error = 0; 341 342 /* repeated start */ 343 PCF_SET_S1(pcf, ES0|STA|STO|ACK); 344 345 /* set slave address to PCF. Last bit (LSB) must be set correctly 346 * according to transfer direction */ 347 PCF_SET_S0(pcf, slave); 348 349 /* wait for address sent, polling */ 350 if ((error = pcf_wait_byte(pcf))) 351 goto error; 352 353 /* check for ack */ 354 if (pcf_noack(pcf, timeout)) { 355 error = IIC_ENOACK; 356 goto error; 357 } 358 359 return (0); 360 361 error: 362 pcf_stop(pcfdev); 363 return (error); 364 } 365 366 static int pcf_start(device_t pcfdev, u_char slave, int timeout) 367 { 368 struct pcf_softc *pcf = DEVTOSOFTC(pcfdev); 369 int error = 0; 370 371 if (PCF_GET_S1(pcf) & nBB == 0) 372 return (IIC_EBUSBSY); 373 374 /* set slave address to PCF. Last bit (LSB) must be set correctly 375 * according to transfer direction */ 376 PCF_SET_S0(pcf, slave); 377 378 /* START only */ 379 PCF_SET_S1(pcf, PIN|ES0|STA|ACK); 380 381 pcf->pcf_started = 1; 382 383 /* wait for address sent, polling */ 384 if ((error = pcf_wait_byte(pcf))) 385 goto error; 386 387 /* check for ACK */ 388 if (pcf_noack(pcf, timeout)) { 389 error = IIC_ENOACK; 390 goto error; 391 } 392 393 return (0); 394 395 error: 396 pcf_stop(pcfdev); 397 return (error); 398 } 399 400 static void 401 pcfintr(unit) 402 { 403 struct pcf_softc *pcf = 404 (struct pcf_softc *)devclass_get_softc(pcf_devclass, unit); 405 406 char data, status, addr; 407 char error = 0; 408 409 status = PCF_GET_S1(pcf); 410 411 if (status & PIN) { 412 printf("pcf%d: spurious interrupt, status=0x%x\n", unit, 413 status & 0xff); 414 415 goto error; 416 } 417 418 if (status & LAB) 419 printf("pcf%d: bus arbitration lost!\n", unit); 420 421 if (status & BER) { 422 error = IIC_EBUSERR; 423 iicbus_intr(pcf->iicbus, INTR_ERROR, &error); 424 425 goto error; 426 } 427 428 do { 429 status = PCF_GET_S1(pcf); 430 431 switch(pcf->pcf_slave_mode) { 432 433 case SLAVE_TRANSMITTER: 434 if (status & LRB) { 435 /* ack interrupt line */ 436 dummy_write(pcf); 437 438 /* no ack, don't send anymore */ 439 pcf->pcf_slave_mode = SLAVE_RECEIVER; 440 441 iicbus_intr(pcf->iicbus, INTR_NOACK, NULL); 442 break; 443 } 444 445 /* get data from upper code */ 446 iicbus_intr(pcf->iicbus, INTR_TRANSMIT, &data); 447 448 PCF_SET_S0(pcf, data); 449 break; 450 451 case SLAVE_RECEIVER: 452 if (status & AAS) { 453 addr = PCF_GET_S0(pcf); 454 455 if (status & AD0) 456 iicbus_intr(pcf->iicbus, INTR_GENERAL, &addr); 457 else 458 iicbus_intr(pcf->iicbus, INTR_START, &addr); 459 460 if (addr & LSB) { 461 pcf->pcf_slave_mode = SLAVE_TRANSMITTER; 462 463 /* get the first char from upper code */ 464 iicbus_intr(pcf->iicbus, INTR_TRANSMIT, &data); 465 466 /* send first data byte */ 467 PCF_SET_S0(pcf, data); 468 } 469 470 break; 471 } 472 473 /* stop condition received? */ 474 if (status & STS) { 475 /* ack interrupt line */ 476 dummy_read(pcf); 477 478 /* emulate intr stop condition */ 479 iicbus_intr(pcf->iicbus, INTR_STOP, NULL); 480 481 } else { 482 /* get data, ack interrupt line */ 483 data = PCF_GET_S0(pcf); 484 485 /* deliver the character */ 486 iicbus_intr(pcf->iicbus, INTR_RECEIVE, &data); 487 } 488 break; 489 490 default: 491 panic("%s: unknown slave mode (%d)!", __FUNCTION__, 492 pcf->pcf_slave_mode); 493 } 494 495 } while ((PCF_GET_S1(pcf) & PIN) == 0); 496 497 return; 498 499 error: 500 /* unknown event on bus...reset PCF */ 501 PCF_SET_S1(pcf, PIN|ES0|ENI|ACK); 502 503 pcf->pcf_slave_mode = SLAVE_RECEIVER; 504 505 return; 506 } 507 508 static int pcf_rst_card(device_t pcfdev, u_char speed, u_char addr, u_char *oldaddr) 509 { 510 struct pcf_softc *pcf = DEVTOSOFTC(pcfdev); 511 512 if (oldaddr) 513 *oldaddr = pcf->pcf_addr; 514 515 /* retrieve own address from bus level */ 516 if (!addr) 517 pcf->pcf_addr = PCF_DEFAULT_ADDR; 518 else 519 pcf->pcf_addr = addr; 520 521 PCF_SET_S1(pcf, PIN); /* initialize S1 */ 522 523 /* own address S'O<>0 */ 524 PCF_SET_S0(pcf, pcf->pcf_addr >> 1); 525 526 /* select clock register */ 527 PCF_SET_S1(pcf, PIN|ES1); 528 529 /* select bus speed : 18=90kb, 19=45kb, 1A=11kb, 1B=1.5kb */ 530 switch (speed) { 531 case IIC_SLOW: 532 PCF_SET_S0(pcf, 0x1b); 533 break; 534 535 case IIC_FAST: 536 PCF_SET_S0(pcf, 0x19); 537 break; 538 539 case IIC_UNKNOWN: 540 case IIC_FASTEST: 541 default: 542 PCF_SET_S0(pcf, 0x18); 543 break; 544 } 545 546 /* set bus on, ack=yes, INT=yes */ 547 PCF_SET_S1(pcf, PIN|ES0|ENI|ACK); 548 549 pcf->pcf_slave_mode = SLAVE_RECEIVER; 550 551 return (0); 552 } 553 554 static int 555 pcf_write(device_t pcfdev, char *buf, int len, int *sent, int timeout /* us */) 556 { 557 struct pcf_softc *pcf = DEVTOSOFTC(pcfdev); 558 int bytes, error = 0; 559 560 #ifdef PCFDEBUG 561 printf("pcf%d: >> writing %d bytes\n", device_get_unit(pcfdev), len); 562 #endif 563 564 bytes = 0; 565 while (len) { 566 567 PCF_SET_S0(pcf, *buf++); 568 569 /* wait for the byte to be send */ 570 if ((error = pcf_wait_byte(pcf))) 571 goto error; 572 573 /* check if ack received */ 574 if (pcf_noack(pcf, timeout)) { 575 error = IIC_ENOACK; 576 goto error; 577 } 578 579 len --; 580 bytes ++; 581 } 582 583 error: 584 *sent = bytes; 585 586 #ifdef PCFDEBUG 587 printf("pcf%d: >> %d bytes written (%d)\n", 588 device_get_unit(pcfdev), bytes, error); 589 #endif 590 591 return (error); 592 } 593 594 static int 595 pcf_read(device_t pcfdev, char *buf, int len, int *read, int last, 596 int delay /* us */) 597 { 598 struct pcf_softc *pcf = DEVTOSOFTC(pcfdev); 599 int bytes, error = 0; 600 601 #ifdef PCFDEBUG 602 printf("pcf%d: << reading %d bytes\n", device_get_unit(pcfdev), len); 603 #endif 604 605 /* trig the bus to get the first data byte in S0 */ 606 if (len) { 607 if (len == 1 && last) 608 /* just one byte to read */ 609 PCF_SET_S1(pcf, ES0); /* no ack */ 610 611 dummy_read(pcf); 612 } 613 614 bytes = 0; 615 while (len) { 616 617 /* XXX delay needed here */ 618 619 /* wait for trigged byte */ 620 if ((error = pcf_wait_byte(pcf))) { 621 pcf_stop(pcfdev); 622 goto error; 623 } 624 625 if (len == 1 && last) 626 /* ok, last data byte already in S0, no I2C activity 627 * on next PCF_GET_S0() */ 628 pcf_stop(pcfdev); 629 630 else if (len == 2 && last) 631 /* next trigged byte with no ack */ 632 PCF_SET_S1(pcf, ES0); 633 634 /* receive byte, trig next byte */ 635 *buf++ = PCF_GET_S0(pcf); 636 637 len --; 638 bytes ++; 639 }; 640 641 error: 642 *read = bytes; 643 644 #ifdef PCFDEBUG 645 printf("pcf%d: << %d bytes read (%d)\n", 646 device_get_unit(pcfdev), bytes, error); 647 #endif 648 649 return (error); 650 } 651 652 DRIVER_MODULE(pcf, root, pcf_driver, pcf_devclass, 0, 0); 653