2 * Copyright (c) 1998 Nicolas Souchu, Marc Bouget
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/i386/isa/pcf.c,v 1.14 2000/01/14 00:18:05 nsouch Exp $
27 * $DragonFly: src/sys/bus/iicbus/i386/pcf.c,v 1.11 2008/08/02 01:14:38 dillon Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
35 #include <sys/malloc.h>
37 #include <machine/clock.h>
40 #include <bus/isa/isareg.h>
41 #include <bus/isa/isavar.h>
43 #include <bus/isa/isa_device.h>
45 #include "../iiconf.h"
46 #include "iicbus_if.h"
50 #define TIMEOUT 9999 /* XXX */
52 /* Status bits of S1 register (read only) */
53 #define nBB 0x01 /* busy when low set/reset by STOP/START*/
54 #define LAB 0x02 /* lost arbitration bit in multi-master mode */
55 #define AAS 0x04 /* addressed as slave */
56 #define LRB 0x08 /* last received byte when not AAS */
57 #define AD0 0x08 /* general call received when AAS */
58 #define BER 0x10 /* bus error, misplaced START or STOP */
59 #define STS 0x20 /* STOP detected in slave receiver mode */
60 #define PIN 0x80 /* pending interrupt not (r/w) */
62 /* Control bits of S1 register (write only) */
73 #define SLAVE_TRANSMITTER 0x1
74 #define SLAVE_RECEIVER 0x2
76 #define PCF_DEFAULT_ADDR 0xaa
80 int pcf_base
; /* isa port */
82 u_char pcf_addr
; /* interface I2C address */
84 int pcf_slave_mode
; /* receiver or transmitter */
85 int pcf_started
; /* 1 if start condition sent */
87 device_t iicbus
; /* the corresponding iicbus */
89 int rid_irq
, rid_ioport
;
90 struct resource
*res_irq
, *res_ioport
;
94 static int pcf_probe(device_t
);
95 static int pcf_attach(device_t
);
96 static void pcfintr(void *arg
);
98 static int pcf_print_child(device_t
, device_t
);
100 static int pcf_repeated_start(device_t
, u_char
, int);
101 static int pcf_start(device_t
, u_char
, int);
102 static int pcf_stop(device_t
);
103 static int pcf_write(device_t
, const char *, int, int *, int);
104 static int pcf_read(device_t
, char *, int, int *, int, int);
105 static int pcf_rst_card(device_t
, u_char
, u_char
, u_char
*);
107 static device_method_t pcf_methods
[] = {
108 /* device interface */
109 DEVMETHOD(device_probe
, pcf_probe
),
110 DEVMETHOD(device_attach
, pcf_attach
),
113 DEVMETHOD(bus_print_child
, pcf_print_child
),
115 /* iicbus interface */
116 DEVMETHOD(iicbus_callback
, iicbus_null_callback
),
117 DEVMETHOD(iicbus_repeated_start
, pcf_repeated_start
),
118 DEVMETHOD(iicbus_start
, pcf_start
),
119 DEVMETHOD(iicbus_stop
, pcf_stop
),
120 DEVMETHOD(iicbus_write
, pcf_write
),
121 DEVMETHOD(iicbus_read
, pcf_read
),
122 DEVMETHOD(iicbus_reset
, pcf_rst_card
),
127 static driver_t pcf_driver
= {
130 sizeof(struct pcf_softc
),
133 static devclass_t pcf_devclass
;
135 #define DEVTOSOFTC(dev) ((struct pcf_softc *)device_get_softc(dev))
138 pcf_probe(device_t pcfdev
)
140 struct pcf_softc
*pcf
= DEVTOSOFTC(pcfdev
);
141 device_t parent
= device_get_parent(pcfdev
);
143 device_set_desc(pcfdev
, "PCF8584 I2C bus controller");
145 bzero(pcf
, sizeof(struct pcf_softc
));
147 pcf
->rid_irq
= pcf
->rid_ioport
= 0;
148 pcf
->res_irq
= pcf
->res_ioport
= 0;
150 /* IO port is mandatory */
151 pcf
->res_ioport
= bus_alloc_resource(pcfdev
, SYS_RES_IOPORT
,
152 &pcf
->rid_ioport
, 0ul, ~0ul,
153 IO_PCFSIZE
, RF_ACTIVE
);
154 if (pcf
->res_ioport
== 0) {
155 device_printf(pcfdev
, "cannot reserve I/O port range\n");
158 BUS_READ_IVAR(parent
, pcfdev
, ISA_IVAR_PORT
, &pcf
->pcf_base
);
160 pcf
->pcf_flags
= device_get_flags(pcfdev
);
162 if (!(pcf
->pcf_flags
& IIC_POLLED
)) {
163 pcf
->res_irq
= bus_alloc_resource(pcfdev
, SYS_RES_IRQ
, &pcf
->rid_irq
,
164 0ul, ~0ul, 1, RF_ACTIVE
);
165 if (pcf
->res_irq
== 0) {
166 device_printf(pcfdev
, "can't reserve irq, polled mode.\n");
167 pcf
->pcf_flags
|= IIC_POLLED
;
172 pcf_rst_card(pcfdev
, IIC_FASTEST
, PCF_DEFAULT_ADDR
, NULL
);
176 if (pcf
->res_ioport
!= 0) {
177 bus_deactivate_resource(pcfdev
, SYS_RES_IOPORT
, pcf
->rid_ioport
,
179 bus_release_resource(pcfdev
, SYS_RES_IOPORT
, pcf
->rid_ioport
,
186 pcf_attach(device_t pcfdev
)
188 struct pcf_softc
*pcf
= DEVTOSOFTC(pcfdev
);
189 device_t parent
= device_get_parent(pcfdev
);
193 /* default to the tty mask for registration */ /* XXX */
194 error
= BUS_SETUP_INTR(parent
, pcfdev
, pcf
->res_irq
,
196 &pcf
->intr_cookie
, NULL
);
201 pcf
->iicbus
= iicbus_alloc_bus(pcfdev
);
203 /* probe and attach the iicbus */
204 device_probe_and_attach(pcf
->iicbus
);
210 pcf_print_child(device_t bus
, device_t dev
)
212 struct pcf_softc
*pcf
= (struct pcf_softc
*)device_get_softc(bus
);
215 retval
+= bus_print_child_header(bus
, dev
);
216 retval
+= kprintf(" on %s addr 0x%x\n", device_get_nameunit(bus
),
223 * PCF8584 datasheet : when operate at 8 MHz or more, a minimun time of
224 * 6 clocks cycles must be left between two consecutives access
226 #define pcf_nops() DELAY(10)
228 #define dummy_read(pcf) PCF_GET_S0(pcf)
229 #define dummy_write(pcf) PCF_SET_S0(pcf, 0)
232 * Specific register access to PCF8584
235 PCF_SET_S0(struct pcf_softc
*pcf
, int data
)
237 outb(pcf
->pcf_base
, data
);
242 PCF_SET_S1(struct pcf_softc
*pcf
, int data
)
244 outb(pcf
->pcf_base
+1, data
);
249 PCF_GET_S0(struct pcf_softc
*pcf
)
253 data
= inb(pcf
->pcf_base
);
260 PCF_GET_S1(struct pcf_softc
*pcf
)
264 data
= inb(pcf
->pcf_base
+1);
271 * Polling mode for master operations wait for a new
272 * byte incomming or outgoing
275 pcf_wait_byte(struct pcf_softc
*pcf
)
277 int counter
= TIMEOUT
;
281 if ((PCF_GET_S1(pcf
) & PIN
) == 0)
285 return (IIC_ETIMEOUT
);
289 pcf_stop(device_t pcfdev
)
291 struct pcf_softc
*pcf
= DEVTOSOFTC(pcfdev
);
294 * Send STOP condition iff the START condition was previously sent.
295 * STOP is sent only once even if a iicbus_stop() is called after
296 * an iicbus_read()... see pcf_read(): the pcf needs to send the stop
297 * before the last char is read.
299 if (pcf
->pcf_started
) {
300 /* set stop condition and enable IT */
301 PCF_SET_S1(pcf
, PIN
|ES0
|ENI
|STO
|ACK
);
303 pcf
->pcf_started
= 0;
311 pcf_noack(struct pcf_softc
*pcf
, int timeout
)
317 noack
= PCF_GET_S1(pcf
) & LRB
;
320 DELAY(10); /* XXX wait 10 us */
327 pcf_repeated_start(device_t pcfdev
, u_char slave
, int timeout
)
329 struct pcf_softc
*pcf
= DEVTOSOFTC(pcfdev
);
333 PCF_SET_S1(pcf
, ES0
|STA
|STO
|ACK
);
335 /* set slave address to PCF. Last bit (LSB) must be set correctly
336 * according to transfer direction */
337 PCF_SET_S0(pcf
, slave
);
339 /* wait for address sent, polling */
340 if ((error
= pcf_wait_byte(pcf
)))
344 if (pcf_noack(pcf
, timeout
)) {
357 pcf_start(device_t pcfdev
, u_char slave
, int timeout
)
359 struct pcf_softc
*pcf
= DEVTOSOFTC(pcfdev
);
362 if ((PCF_GET_S1(pcf
) & nBB
) == 0)
363 return (IIC_EBUSBSY
);
365 /* set slave address to PCF. Last bit (LSB) must be set correctly
366 * according to transfer direction */
367 PCF_SET_S0(pcf
, slave
);
370 PCF_SET_S1(pcf
, PIN
|ES0
|STA
|ACK
);
372 pcf
->pcf_started
= 1;
374 /* wait for address sent, polling */
375 if ((error
= pcf_wait_byte(pcf
)))
379 if (pcf_noack(pcf
, timeout
)) {
394 device_t pcfdev
= (device_t
)arg
;
395 struct pcf_softc
*pcf
= DEVTOSOFTC(pcfdev
);
397 char data
, status
, addr
;
400 status
= PCF_GET_S1(pcf
);
403 device_printf(pcfdev
, "spurious interrupt, status=0x%x\n", status
& 0xff);
409 device_printf(pcfdev
, "bus arbitration lost!\n");
413 iicbus_intr(pcf
->iicbus
, INTR_ERROR
, &error
);
419 status
= PCF_GET_S1(pcf
);
421 switch(pcf
->pcf_slave_mode
) {
423 case SLAVE_TRANSMITTER
:
425 /* ack interrupt line */
428 /* no ack, don't send anymore */
429 pcf
->pcf_slave_mode
= SLAVE_RECEIVER
;
431 iicbus_intr(pcf
->iicbus
, INTR_NOACK
, NULL
);
435 /* get data from upper code */
436 iicbus_intr(pcf
->iicbus
, INTR_TRANSMIT
, &data
);
438 PCF_SET_S0(pcf
, data
);
443 addr
= PCF_GET_S0(pcf
);
446 iicbus_intr(pcf
->iicbus
, INTR_GENERAL
, &addr
);
448 iicbus_intr(pcf
->iicbus
, INTR_START
, &addr
);
451 pcf
->pcf_slave_mode
= SLAVE_TRANSMITTER
;
453 /* get the first char from upper code */
454 iicbus_intr(pcf
->iicbus
, INTR_TRANSMIT
, &data
);
456 /* send first data byte */
457 PCF_SET_S0(pcf
, data
);
463 /* stop condition received? */
465 /* ack interrupt line */
468 /* emulate intr stop condition */
469 iicbus_intr(pcf
->iicbus
, INTR_STOP
, NULL
);
472 /* get data, ack interrupt line */
473 data
= PCF_GET_S0(pcf
);
475 /* deliver the character */
476 iicbus_intr(pcf
->iicbus
, INTR_RECEIVE
, &data
);
481 panic("%s: unknown slave mode (%d)!", __func__
,
482 pcf
->pcf_slave_mode
);
485 } while ((PCF_GET_S1(pcf
) & PIN
) == 0);
490 /* unknown event on bus...reset PCF */
491 PCF_SET_S1(pcf
, PIN
|ES0
|ENI
|ACK
);
493 pcf
->pcf_slave_mode
= SLAVE_RECEIVER
;
499 pcf_rst_card(device_t pcfdev
, u_char speed
, u_char addr
, u_char
*oldaddr
)
501 struct pcf_softc
*pcf
= DEVTOSOFTC(pcfdev
);
504 *oldaddr
= pcf
->pcf_addr
;
506 /* retrieve own address from bus level */
508 pcf
->pcf_addr
= PCF_DEFAULT_ADDR
;
510 pcf
->pcf_addr
= addr
;
512 PCF_SET_S1(pcf
, PIN
); /* initialize S1 */
514 /* own address S'O<>0 */
515 PCF_SET_S0(pcf
, pcf
->pcf_addr
>> 1);
517 /* select clock register */
518 PCF_SET_S1(pcf
, PIN
|ES1
);
520 /* select bus speed : 18=90kb, 19=45kb, 1A=11kb, 1B=1.5kb */
523 PCF_SET_S0(pcf
, 0x1b);
527 PCF_SET_S0(pcf
, 0x19);
533 PCF_SET_S0(pcf
, 0x18);
537 /* set bus on, ack=yes, INT=yes */
538 PCF_SET_S1(pcf
, PIN
|ES0
|ENI
|ACK
);
540 pcf
->pcf_slave_mode
= SLAVE_RECEIVER
;
546 pcf_write(device_t pcfdev
, const char *buf
, int len
, int *sent
,
547 int timeout
/* us */)
549 struct pcf_softc
*pcf
= DEVTOSOFTC(pcfdev
);
550 int bytes
, error
= 0;
553 kprintf("pcf%d: >> writing %d bytes\n", device_get_unit(pcfdev
), len
);
559 PCF_SET_S0(pcf
, *buf
++);
561 /* wait for the byte to be send */
562 if ((error
= pcf_wait_byte(pcf
)))
565 /* check if ack received */
566 if (pcf_noack(pcf
, timeout
)) {
579 kprintf("pcf%d: >> %d bytes written (%d)\n",
580 device_get_unit(pcfdev
), bytes
, error
);
587 pcf_read(device_t pcfdev
, char *buf
, int len
, int *read
, int last
,
590 struct pcf_softc
*pcf
= DEVTOSOFTC(pcfdev
);
591 int bytes
, error
= 0;
594 kprintf("pcf%d: << reading %d bytes\n", device_get_unit(pcfdev
), len
);
597 /* trig the bus to get the first data byte in S0 */
599 if (len
== 1 && last
)
600 /* just one byte to read */
601 PCF_SET_S1(pcf
, ES0
); /* no ack */
609 /* XXX delay needed here */
611 /* wait for trigged byte */
612 if ((error
= pcf_wait_byte(pcf
))) {
617 if (len
== 1 && last
)
618 /* ok, last data byte already in S0, no I2C activity
619 * on next PCF_GET_S0() */
622 else if (len
== 2 && last
)
623 /* next trigged byte with no ack */
624 PCF_SET_S1(pcf
, ES0
);
626 /* receive byte, trig next byte */
627 *buf
++ = PCF_GET_S0(pcf
);
637 kprintf("pcf%d: << %d bytes read (%d)\n",
638 device_get_unit(pcfdev
), bytes
, error
);
644 DRIVER_MODULE(pcf
, isa
, pcf_driver
, pcf_devclass
, 0, 0);