2 * Copyright (c) 1998 Nicolas Souchu, Marc Bouget
3 * Copyright (c) 2004 Joerg Wunsch
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/systm.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
44 #include <dev/iicbus/iicbus.h>
45 #include <dev/iicbus/iiconf.h>
46 #include <dev/pcf/pcfvar.h>
47 #include "iicbus_if.h"
49 /* Not so official debugging option. */
50 /* #define PCFDEBUG */
52 static int pcf_wait_byte(struct pcf_softc *pcf);
53 static int pcf_noack(struct pcf_softc *pcf, int timeout);
54 static void pcf_stop_locked(struct pcf_softc *pcf);
57 * Polling mode for master operations wait for a new
58 * byte incoming or outgoing
61 pcf_wait_byte(struct pcf_softc *sc)
63 int counter = TIMEOUT;
65 PCF_ASSERT_LOCKED(sc);
68 if ((pcf_get_S1(sc) & PIN) == 0)
73 printf("pcf: timeout!\n");
76 return (IIC_ETIMEOUT);
80 pcf_stop_locked(struct pcf_softc *sc)
83 PCF_ASSERT_LOCKED(sc);
85 device_printf(dev, " >> stop\n");
88 * Send STOP condition iff the START condition was previously sent.
89 * STOP is sent only once even if an iicbus_stop() is called after
90 * an iicbus_read()... see pcf_read(): the PCF needs to send the stop
91 * before the last char is read.
93 if (sc->pcf_started) {
94 /* set stop condition and enable IT */
95 pcf_set_S1(sc, PIN|ESO|ENI|STO|ACK);
102 pcf_noack(struct pcf_softc *sc, int timeout)
107 PCF_ASSERT_LOCKED(sc);
109 noack = pcf_get_S1(sc) & LRB;
112 DELAY(10); /* XXX wait 10 us */
119 pcf_repeated_start(device_t dev, u_char slave, int timeout)
121 struct pcf_softc *sc = DEVTOSOFTC(dev);
126 device_printf(dev, " >> repeated start for slave %#x\n",
130 pcf_set_S1(sc, ESO|STA|STO|ACK);
132 /* set slave address to PCF. Last bit (LSB) must be set correctly
133 * according to transfer direction */
134 pcf_set_S0(sc, slave);
136 /* wait for address sent, polling */
137 if ((error = pcf_wait_byte(sc)))
141 if (pcf_noack(sc, timeout)) {
144 printf("pcf: no ack on repeated_start!\n");
159 pcf_start(device_t dev, u_char slave, int timeout)
161 struct pcf_softc *sc = DEVTOSOFTC(dev);
166 device_printf(dev, " >> start for slave %#x\n", (unsigned)slave);
168 if ((pcf_get_S1(sc) & nBB) == 0) {
170 printf("pcf: busy!\n");
173 return (IIC_EBUSBSY);
176 /* set slave address to PCF. Last bit (LSB) must be set correctly
177 * according to transfer direction */
178 pcf_set_S0(sc, slave);
181 pcf_set_S1(sc, PIN|ESO|STA|ACK);
185 /* wait for address sent, polling */
186 if ((error = pcf_wait_byte(sc)))
190 if (pcf_noack(sc, timeout)) {
193 printf("pcf: no ack on start!\n");
208 pcf_stop(device_t dev)
210 struct pcf_softc *sc = DEVTOSOFTC(dev);
213 device_printf(dev, " >> stop\n");
225 struct pcf_softc *sc = arg;
226 char data, status, addr;
230 status = pcf_get_S1(sc);
233 printf("pcf: spurious interrupt, status=0x%x\n",
240 printf("pcf: bus arbitration lost!\n");
244 iicbus_intr(sc->iicbus, INTR_ERROR, &error);
250 status = pcf_get_S1(sc);
252 switch(sc->pcf_slave_mode) {
254 case SLAVE_TRANSMITTER:
256 /* ack interrupt line */
259 /* no ack, don't send anymore */
260 sc->pcf_slave_mode = SLAVE_RECEIVER;
262 iicbus_intr(sc->iicbus, INTR_NOACK, NULL);
266 /* get data from upper code */
267 iicbus_intr(sc->iicbus, INTR_TRANSMIT, &data);
269 pcf_set_S0(sc, data);
274 addr = pcf_get_S0(sc);
277 iicbus_intr(sc->iicbus, INTR_GENERAL, &addr);
279 iicbus_intr(sc->iicbus, INTR_START, &addr);
282 sc->pcf_slave_mode = SLAVE_TRANSMITTER;
284 /* get the first char from upper code */
285 iicbus_intr(sc->iicbus, INTR_TRANSMIT, &data);
287 /* send first data byte */
288 pcf_set_S0(sc, data);
294 /* stop condition received? */
296 /* ack interrupt line */
299 /* emulate intr stop condition */
300 iicbus_intr(sc->iicbus, INTR_STOP, NULL);
303 /* get data, ack interrupt line */
304 data = pcf_get_S0(sc);
306 /* deliver the character */
307 iicbus_intr(sc->iicbus, INTR_RECEIVE, &data);
312 panic("%s: unknown slave mode (%d)!", __func__,
316 } while ((pcf_get_S1(sc) & PIN) == 0);
322 /* unknown event on bus...reset PCF */
323 pcf_set_S1(sc, PIN|ESO|ENI|ACK);
325 sc->pcf_slave_mode = SLAVE_RECEIVER;
332 pcf_rst_card(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
334 struct pcf_softc *sc = DEVTOSOFTC(dev);
338 *oldaddr = sc->pcf_addr;
340 /* retrieve own address from bus level */
342 sc->pcf_addr = PCF_DEFAULT_ADDR;
346 pcf_set_S1(sc, PIN); /* initialize S1 */
348 /* own address S'O<>0 */
349 pcf_set_S0(sc, sc->pcf_addr >> 1);
351 /* select clock register */
352 pcf_set_S1(sc, PIN|ES1);
354 /* select bus speed : 18=90kb, 19=45kb, 1A=11kb, 1B=1.5kb */
357 pcf_set_S0(sc, 0x1b); /* XXX Sun uses 0x1f */
361 pcf_set_S0(sc, 0x19); /* XXX Sun: 0x1d */
367 pcf_set_S0(sc, 0x18); /* XXX Sun: 0x1c */
371 /* set bus on, ack=yes, INT=yes */
372 pcf_set_S1(sc, PIN|ESO|ENI|ACK);
374 sc->pcf_slave_mode = SLAVE_RECEIVER;
381 pcf_write(device_t dev, const char *buf, int len, int *sent, int timeout /* us */)
383 struct pcf_softc *sc = DEVTOSOFTC(dev);
384 int bytes, error = 0;
387 device_printf(dev, " >> writing %d bytes: %#x%s\n", len,
388 (unsigned)buf[0], len > 1? "...": "");
395 pcf_set_S0(sc, *buf++);
397 /* wait for the byte to be send */
398 if ((error = pcf_wait_byte(sc)))
401 /* check if ack received */
402 if (pcf_noack(sc, timeout)) {
416 device_printf(dev, " >> %d bytes written (%d)\n", bytes, error);
423 pcf_read(device_t dev, char *buf, int len, int *read, int last,
426 struct pcf_softc *sc = DEVTOSOFTC(dev);
427 int bytes, error = 0;
431 device_printf(dev, " << reading %d bytes\n", len);
435 /* trig the bus to get the first data byte in S0 */
437 if (len == 1 && last)
438 /* just one byte to read */
439 pcf_set_S1(sc, ESO); /* no ack */
447 /* XXX delay needed here */
449 /* wait for trigged byte */
450 if ((error = pcf_wait_byte(sc))) {
455 if (len == 1 && last)
456 /* ok, last data byte already in S0, no I2C activity
457 * on next pcf_get_S0() */
460 else if (len == 2 && last)
461 /* next trigged byte with no ack */
464 /* receive byte, trig next byte */
465 *buf++ = pcf_get_S0(sc);
476 device_printf(dev, " << %d bytes read (%d): %#x%s\n", bytes, error,
477 (unsigned)obuf[0], bytes > 1? "...": "");
483 DRIVER_MODULE(iicbus, pcf, iicbus_driver, iicbus_devclass, 0, 0);
484 MODULE_DEPEND(pcf, iicbus, PCF_MINVER, PCF_PREFVER, PCF_MAXVER);
485 MODULE_VERSION(pcf, PCF_MODVER);