1998-09-03 21:01:22 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 Nicolas Souchu, Marc Bouget
|
2004-05-25 07:42:45 +00:00
|
|
|
* Copyright (c) 2004 Joerg Wunsch
|
1998-09-03 21:01:22 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
2003-06-02 16:32:55 +00:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1998-09-03 21:01:22 +00:00
|
|
|
#include <sys/param.h>
|
2008-08-04 20:46:15 +00:00
|
|
|
#include <sys/bus.h>
|
|
|
|
#include <sys/lock.h>
|
1998-09-03 21:01:22 +00:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/module.h>
|
2008-08-04 20:46:15 +00:00
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/systm.h>
|
1998-09-03 21:01:22 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
#include <machine/bus.h>
|
|
|
|
#include <machine/resource.h>
|
|
|
|
|
2004-05-25 07:42:45 +00:00
|
|
|
#include <sys/rman.h>
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
#include <dev/iicbus/iiconf.h>
|
2004-05-25 07:42:45 +00:00
|
|
|
#include <dev/pcf/pcfvar.h>
|
1998-09-03 21:01:22 +00:00
|
|
|
#include "iicbus_if.h"
|
|
|
|
|
2004-05-25 07:42:45 +00:00
|
|
|
/* Not so official debugging option. */
|
|
|
|
/* #define PCFDEBUG */
|
1998-09-03 21:01:22 +00:00
|
|
|
|
2004-05-25 07:42:45 +00:00
|
|
|
static int pcf_wait_byte(struct pcf_softc *pcf);
|
|
|
|
static int pcf_noack(struct pcf_softc *pcf, int timeout);
|
2008-08-04 20:46:15 +00:00
|
|
|
static void pcf_stop_locked(struct pcf_softc *pcf);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Polling mode for master operations wait for a new
|
2008-08-04 20:46:15 +00:00
|
|
|
* byte incoming or outgoing
|
1998-09-03 21:01:22 +00:00
|
|
|
*/
|
2008-08-04 20:46:15 +00:00
|
|
|
static int
|
2004-05-25 07:42:45 +00:00
|
|
|
pcf_wait_byte(struct pcf_softc *sc)
|
1998-09-03 21:01:22 +00:00
|
|
|
{
|
|
|
|
int counter = TIMEOUT;
|
|
|
|
|
2008-08-04 20:46:15 +00:00
|
|
|
PCF_ASSERT_LOCKED(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
while (counter--) {
|
|
|
|
|
2004-05-25 07:42:45 +00:00
|
|
|
if ((pcf_get_S1(sc) & PIN) == 0)
|
1998-09-03 21:01:22 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-05-31 14:24:21 +00:00
|
|
|
#ifdef PCFDEBUG
|
|
|
|
printf("pcf: timeout!\n");
|
|
|
|
#endif
|
|
|
|
|
1998-09-03 21:01:22 +00:00
|
|
|
return (IIC_ETIMEOUT);
|
|
|
|
}
|
|
|
|
|
2008-08-04 20:46:15 +00:00
|
|
|
static void
|
|
|
|
pcf_stop_locked(struct pcf_softc *sc)
|
1998-09-03 21:01:22 +00:00
|
|
|
{
|
|
|
|
|
2008-08-04 20:46:15 +00:00
|
|
|
PCF_ASSERT_LOCKED(sc);
|
2004-05-25 07:42:45 +00:00
|
|
|
#ifdef PCFDEBUG
|
|
|
|
device_printf(dev, " >> stop\n");
|
|
|
|
#endif
|
1998-10-31 11:37:09 +00:00
|
|
|
/*
|
|
|
|
* Send STOP condition iff the START condition was previously sent.
|
2003-01-01 18:49:04 +00:00
|
|
|
* STOP is sent only once even if an iicbus_stop() is called after
|
2004-05-25 07:42:45 +00:00
|
|
|
* an iicbus_read()... see pcf_read(): the PCF needs to send the stop
|
1998-10-31 11:37:09 +00:00
|
|
|
* before the last char is read.
|
|
|
|
*/
|
2004-05-25 07:42:45 +00:00
|
|
|
if (sc->pcf_started) {
|
1998-10-31 11:37:09 +00:00
|
|
|
/* set stop condition and enable IT */
|
2004-05-25 07:42:45 +00:00
|
|
|
pcf_set_S1(sc, PIN|ESO|ENI|STO|ACK);
|
1998-10-31 11:37:09 +00:00
|
|
|
|
2004-05-25 07:42:45 +00:00
|
|
|
sc->pcf_started = 0;
|
1998-10-31 11:37:09 +00:00
|
|
|
}
|
1998-09-03 21:01:22 +00:00
|
|
|
}
|
|
|
|
|
2008-08-04 20:46:15 +00:00
|
|
|
static int
|
2004-05-25 07:42:45 +00:00
|
|
|
pcf_noack(struct pcf_softc *sc, int timeout)
|
1998-10-31 11:37:09 +00:00
|
|
|
{
|
|
|
|
int noack;
|
|
|
|
int k = timeout/10;
|
|
|
|
|
2008-08-04 20:46:15 +00:00
|
|
|
PCF_ASSERT_LOCKED(sc);
|
1998-10-31 11:37:09 +00:00
|
|
|
do {
|
2004-05-25 07:42:45 +00:00
|
|
|
noack = pcf_get_S1(sc) & LRB;
|
1998-10-31 11:37:09 +00:00
|
|
|
if (!noack)
|
|
|
|
break;
|
|
|
|
DELAY(10); /* XXX wait 10 us */
|
|
|
|
} while (k--);
|
|
|
|
|
|
|
|
return (noack);
|
|
|
|
}
|
|
|
|
|
2004-05-25 07:42:45 +00:00
|
|
|
int
|
|
|
|
pcf_repeated_start(device_t dev, u_char slave, int timeout)
|
1998-09-03 21:01:22 +00:00
|
|
|
{
|
2004-05-25 07:42:45 +00:00
|
|
|
struct pcf_softc *sc = DEVTOSOFTC(dev);
|
1998-09-03 21:01:22 +00:00
|
|
|
int error = 0;
|
|
|
|
|
2008-08-04 20:46:15 +00:00
|
|
|
PCF_LOCK(sc);
|
2004-05-25 07:42:45 +00:00
|
|
|
#ifdef PCFDEBUG
|
|
|
|
device_printf(dev, " >> repeated start for slave %#x\n",
|
|
|
|
(unsigned)slave);
|
|
|
|
#endif
|
1998-09-03 21:01:22 +00:00
|
|
|
/* repeated start */
|
2004-05-25 07:42:45 +00:00
|
|
|
pcf_set_S1(sc, ESO|STA|STO|ACK);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
/* set slave address to PCF. Last bit (LSB) must be set correctly
|
|
|
|
* according to transfer direction */
|
2004-05-25 07:42:45 +00:00
|
|
|
pcf_set_S0(sc, slave);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
/* wait for address sent, polling */
|
2004-05-25 07:42:45 +00:00
|
|
|
if ((error = pcf_wait_byte(sc)))
|
1998-09-03 21:01:22 +00:00
|
|
|
goto error;
|
|
|
|
|
1998-10-31 11:37:09 +00:00
|
|
|
/* check for ack */
|
2004-05-25 07:42:45 +00:00
|
|
|
if (pcf_noack(sc, timeout)) {
|
1998-09-03 21:01:22 +00:00
|
|
|
error = IIC_ENOACK;
|
2004-05-31 14:24:21 +00:00
|
|
|
#ifdef PCFDEBUG
|
|
|
|
printf("pcf: no ack on repeated_start!\n");
|
|
|
|
#endif
|
1998-09-03 21:01:22 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2008-08-04 20:46:15 +00:00
|
|
|
PCF_UNLOCK(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
error:
|
2008-08-04 20:46:15 +00:00
|
|
|
pcf_stop_locked(sc);
|
|
|
|
PCF_UNLOCK(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2004-05-25 07:42:45 +00:00
|
|
|
int
|
|
|
|
pcf_start(device_t dev, u_char slave, int timeout)
|
1998-09-03 21:01:22 +00:00
|
|
|
{
|
2004-05-25 07:42:45 +00:00
|
|
|
struct pcf_softc *sc = DEVTOSOFTC(dev);
|
1998-09-03 21:01:22 +00:00
|
|
|
int error = 0;
|
|
|
|
|
2008-08-04 20:46:15 +00:00
|
|
|
PCF_LOCK(sc);
|
2004-05-25 07:42:45 +00:00
|
|
|
#ifdef PCFDEBUG
|
|
|
|
device_printf(dev, " >> start for slave %#x\n", (unsigned)slave);
|
|
|
|
#endif
|
2004-05-31 14:24:21 +00:00
|
|
|
if ((pcf_get_S1(sc) & nBB) == 0) {
|
|
|
|
#ifdef PCFDEBUG
|
|
|
|
printf("pcf: busy!\n");
|
|
|
|
#endif
|
2008-08-04 20:46:15 +00:00
|
|
|
PCF_UNLOCK(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
return (IIC_EBUSBSY);
|
2004-05-31 14:24:21 +00:00
|
|
|
}
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
/* set slave address to PCF. Last bit (LSB) must be set correctly
|
|
|
|
* according to transfer direction */
|
2004-05-25 07:42:45 +00:00
|
|
|
pcf_set_S0(sc, slave);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
/* START only */
|
2004-05-25 07:42:45 +00:00
|
|
|
pcf_set_S1(sc, PIN|ESO|STA|ACK);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
2004-05-25 07:42:45 +00:00
|
|
|
sc->pcf_started = 1;
|
1998-10-31 11:37:09 +00:00
|
|
|
|
1998-09-03 21:01:22 +00:00
|
|
|
/* wait for address sent, polling */
|
2004-05-25 07:42:45 +00:00
|
|
|
if ((error = pcf_wait_byte(sc)))
|
1998-09-03 21:01:22 +00:00
|
|
|
goto error;
|
|
|
|
|
1998-10-31 11:37:09 +00:00
|
|
|
/* check for ACK */
|
2004-05-25 07:42:45 +00:00
|
|
|
if (pcf_noack(sc, timeout)) {
|
1998-09-03 21:01:22 +00:00
|
|
|
error = IIC_ENOACK;
|
2004-05-31 14:24:21 +00:00
|
|
|
#ifdef PCFDEBUG
|
|
|
|
printf("pcf: no ack on start!\n");
|
|
|
|
#endif
|
1998-09-03 21:01:22 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2008-08-04 20:46:15 +00:00
|
|
|
PCF_UNLOCK(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
error:
|
2008-08-04 20:46:15 +00:00
|
|
|
pcf_stop_locked(sc);
|
|
|
|
PCF_UNLOCK(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2008-08-04 20:46:15 +00:00
|
|
|
int
|
|
|
|
pcf_stop(device_t dev)
|
|
|
|
{
|
|
|
|
struct pcf_softc *sc = DEVTOSOFTC(dev);
|
|
|
|
|
|
|
|
#ifdef PCFDEBUG
|
|
|
|
device_printf(dev, " >> stop\n");
|
|
|
|
#endif
|
|
|
|
PCF_LOCK(sc);
|
|
|
|
pcf_stop_locked(sc);
|
|
|
|
PCF_UNLOCK(sc);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-05-25 07:42:45 +00:00
|
|
|
void
|
|
|
|
pcf_intr(void *arg)
|
1998-09-03 21:01:22 +00:00
|
|
|
{
|
2004-05-31 14:24:21 +00:00
|
|
|
struct pcf_softc *sc = arg;
|
1998-09-03 21:01:22 +00:00
|
|
|
char data, status, addr;
|
|
|
|
char error = 0;
|
|
|
|
|
2008-08-04 20:46:15 +00:00
|
|
|
PCF_LOCK(sc);
|
2004-05-25 07:42:45 +00:00
|
|
|
status = pcf_get_S1(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
if (status & PIN) {
|
2004-05-31 14:24:21 +00:00
|
|
|
printf("pcf: spurious interrupt, status=0x%x\n",
|
|
|
|
status & 0xff);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
goto error;
|
2004-05-25 07:42:45 +00:00
|
|
|
}
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
if (status & LAB)
|
2004-05-31 14:24:21 +00:00
|
|
|
printf("pcf: bus arbitration lost!\n");
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
if (status & BER) {
|
|
|
|
error = IIC_EBUSERR;
|
2004-05-25 07:42:45 +00:00
|
|
|
iicbus_intr(sc->iicbus, INTR_ERROR, &error);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2004-05-25 07:42:45 +00:00
|
|
|
status = pcf_get_S1(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
2004-05-25 07:42:45 +00:00
|
|
|
switch(sc->pcf_slave_mode) {
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
case SLAVE_TRANSMITTER:
|
|
|
|
if (status & LRB) {
|
|
|
|
/* ack interrupt line */
|
2004-05-25 07:42:45 +00:00
|
|
|
dummy_write(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
/* no ack, don't send anymore */
|
2004-05-25 07:42:45 +00:00
|
|
|
sc->pcf_slave_mode = SLAVE_RECEIVER;
|
1998-09-03 21:01:22 +00:00
|
|
|
|
2004-05-25 07:42:45 +00:00
|
|
|
iicbus_intr(sc->iicbus, INTR_NOACK, NULL);
|
1998-09-03 21:01:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get data from upper code */
|
2004-05-25 07:42:45 +00:00
|
|
|
iicbus_intr(sc->iicbus, INTR_TRANSMIT, &data);
|
|
|
|
|
|
|
|
pcf_set_S0(sc, data);
|
|
|
|
break;
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
case SLAVE_RECEIVER:
|
|
|
|
if (status & AAS) {
|
2004-05-25 07:42:45 +00:00
|
|
|
addr = pcf_get_S0(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
if (status & AD0)
|
2004-05-25 07:42:45 +00:00
|
|
|
iicbus_intr(sc->iicbus, INTR_GENERAL, &addr);
|
1998-09-03 21:01:22 +00:00
|
|
|
else
|
2004-05-25 07:42:45 +00:00
|
|
|
iicbus_intr(sc->iicbus, INTR_START, &addr);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
if (addr & LSB) {
|
2004-05-25 07:42:45 +00:00
|
|
|
sc->pcf_slave_mode = SLAVE_TRANSMITTER;
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
/* get the first char from upper code */
|
2004-05-25 07:42:45 +00:00
|
|
|
iicbus_intr(sc->iicbus, INTR_TRANSMIT, &data);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
/* send first data byte */
|
2004-05-25 07:42:45 +00:00
|
|
|
pcf_set_S0(sc, data);
|
1998-09-03 21:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stop condition received? */
|
|
|
|
if (status & STS) {
|
|
|
|
/* ack interrupt line */
|
2004-05-25 07:42:45 +00:00
|
|
|
dummy_read(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
/* emulate intr stop condition */
|
2004-05-25 07:42:45 +00:00
|
|
|
iicbus_intr(sc->iicbus, INTR_STOP, NULL);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
/* get data, ack interrupt line */
|
2004-05-25 07:42:45 +00:00
|
|
|
data = pcf_get_S0(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
/* deliver the character */
|
2004-05-25 07:42:45 +00:00
|
|
|
iicbus_intr(sc->iicbus, INTR_RECEIVE, &data);
|
1998-09-03 21:01:22 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2001-12-10 08:09:49 +00:00
|
|
|
panic("%s: unknown slave mode (%d)!", __func__,
|
2004-05-25 07:42:45 +00:00
|
|
|
sc->pcf_slave_mode);
|
1998-09-03 21:01:22 +00:00
|
|
|
}
|
|
|
|
|
2004-05-25 07:42:45 +00:00
|
|
|
} while ((pcf_get_S1(sc) & PIN) == 0);
|
2008-08-04 20:46:15 +00:00
|
|
|
PCF_UNLOCK(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
/* unknown event on bus...reset PCF */
|
2004-05-25 07:42:45 +00:00
|
|
|
pcf_set_S1(sc, PIN|ESO|ENI|ACK);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
2004-05-25 07:42:45 +00:00
|
|
|
sc->pcf_slave_mode = SLAVE_RECEIVER;
|
2008-08-04 20:46:15 +00:00
|
|
|
PCF_UNLOCK(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-05-25 07:42:45 +00:00
|
|
|
int
|
|
|
|
pcf_rst_card(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
|
1998-09-03 21:01:22 +00:00
|
|
|
{
|
2004-05-25 07:42:45 +00:00
|
|
|
struct pcf_softc *sc = DEVTOSOFTC(dev);
|
1998-10-31 11:37:09 +00:00
|
|
|
|
2008-08-04 20:46:15 +00:00
|
|
|
PCF_LOCK(sc);
|
1998-10-31 11:37:09 +00:00
|
|
|
if (oldaddr)
|
2004-05-25 07:42:45 +00:00
|
|
|
*oldaddr = sc->pcf_addr;
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
/* retrieve own address from bus level */
|
1998-10-31 11:37:09 +00:00
|
|
|
if (!addr)
|
2004-05-25 07:42:45 +00:00
|
|
|
sc->pcf_addr = PCF_DEFAULT_ADDR;
|
1998-10-31 11:37:09 +00:00
|
|
|
else
|
2004-05-25 07:42:45 +00:00
|
|
|
sc->pcf_addr = addr;
|
|
|
|
|
|
|
|
pcf_set_S1(sc, PIN); /* initialize S1 */
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
/* own address S'O<>0 */
|
2004-05-25 07:42:45 +00:00
|
|
|
pcf_set_S0(sc, sc->pcf_addr >> 1);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
/* select clock register */
|
2004-05-25 07:42:45 +00:00
|
|
|
pcf_set_S1(sc, PIN|ES1);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
/* select bus speed : 18=90kb, 19=45kb, 1A=11kb, 1B=1.5kb */
|
|
|
|
switch (speed) {
|
|
|
|
case IIC_SLOW:
|
2004-05-25 07:42:45 +00:00
|
|
|
pcf_set_S0(sc, 0x1b); /* XXX Sun uses 0x1f */
|
1998-09-03 21:01:22 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IIC_FAST:
|
2004-05-25 07:42:45 +00:00
|
|
|
pcf_set_S0(sc, 0x19); /* XXX Sun: 0x1d */
|
1998-09-03 21:01:22 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IIC_UNKNOWN:
|
|
|
|
case IIC_FASTEST:
|
|
|
|
default:
|
2004-05-25 07:42:45 +00:00
|
|
|
pcf_set_S0(sc, 0x18); /* XXX Sun: 0x1c */
|
1998-09-03 21:01:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set bus on, ack=yes, INT=yes */
|
2004-05-25 07:42:45 +00:00
|
|
|
pcf_set_S1(sc, PIN|ESO|ENI|ACK);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
2004-05-25 07:42:45 +00:00
|
|
|
sc->pcf_slave_mode = SLAVE_RECEIVER;
|
2008-08-04 20:46:15 +00:00
|
|
|
PCF_UNLOCK(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-05-25 07:42:45 +00:00
|
|
|
int
|
|
|
|
pcf_write(device_t dev, char *buf, int len, int *sent, int timeout /* us */)
|
1998-09-03 21:01:22 +00:00
|
|
|
{
|
2004-05-25 07:42:45 +00:00
|
|
|
struct pcf_softc *sc = DEVTOSOFTC(dev);
|
1998-09-03 21:01:22 +00:00
|
|
|
int bytes, error = 0;
|
|
|
|
|
|
|
|
#ifdef PCFDEBUG
|
2004-05-25 07:42:45 +00:00
|
|
|
device_printf(dev, " >> writing %d bytes: %#x%s\n", len,
|
|
|
|
(unsigned)buf[0], len > 1? "...": "");
|
1998-09-03 21:01:22 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
bytes = 0;
|
2008-08-04 20:46:15 +00:00
|
|
|
PCF_LOCK(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
while (len) {
|
|
|
|
|
2004-05-25 07:42:45 +00:00
|
|
|
pcf_set_S0(sc, *buf++);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
1998-10-31 11:37:09 +00:00
|
|
|
/* wait for the byte to be send */
|
2004-05-25 07:42:45 +00:00
|
|
|
if ((error = pcf_wait_byte(sc)))
|
1998-09-03 21:01:22 +00:00
|
|
|
goto error;
|
|
|
|
|
1998-10-31 11:37:09 +00:00
|
|
|
/* check if ack received */
|
2004-05-25 07:42:45 +00:00
|
|
|
if (pcf_noack(sc, timeout)) {
|
1998-09-03 21:01:22 +00:00
|
|
|
error = IIC_ENOACK;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
len --;
|
|
|
|
bytes ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
*sent = bytes;
|
2008-08-04 20:46:15 +00:00
|
|
|
PCF_UNLOCK(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
#ifdef PCFDEBUG
|
2004-05-25 07:42:45 +00:00
|
|
|
device_printf(dev, " >> %d bytes written (%d)\n", bytes, error);
|
1998-09-03 21:01:22 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2004-05-25 07:42:45 +00:00
|
|
|
int
|
|
|
|
pcf_read(device_t dev, char *buf, int len, int *read, int last,
|
|
|
|
int delay /* us */)
|
1998-09-03 21:01:22 +00:00
|
|
|
{
|
2004-05-25 07:42:45 +00:00
|
|
|
struct pcf_softc *sc = DEVTOSOFTC(dev);
|
1998-09-03 21:01:22 +00:00
|
|
|
int bytes, error = 0;
|
|
|
|
#ifdef PCFDEBUG
|
2004-05-25 07:42:45 +00:00
|
|
|
char *obuf = buf;
|
|
|
|
|
|
|
|
device_printf(dev, " << reading %d bytes\n", len);
|
1998-09-03 21:01:22 +00:00
|
|
|
#endif
|
|
|
|
|
2008-08-04 20:46:15 +00:00
|
|
|
PCF_LOCK(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
/* trig the bus to get the first data byte in S0 */
|
|
|
|
if (len) {
|
1998-10-31 11:37:09 +00:00
|
|
|
if (len == 1 && last)
|
1998-09-03 21:01:22 +00:00
|
|
|
/* just one byte to read */
|
2004-05-25 07:42:45 +00:00
|
|
|
pcf_set_S1(sc, ESO); /* no ack */
|
1998-09-03 21:01:22 +00:00
|
|
|
|
2004-05-25 07:42:45 +00:00
|
|
|
dummy_read(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bytes = 0;
|
|
|
|
while (len) {
|
|
|
|
|
1998-10-31 11:37:09 +00:00
|
|
|
/* XXX delay needed here */
|
|
|
|
|
|
|
|
/* wait for trigged byte */
|
2004-05-25 07:42:45 +00:00
|
|
|
if ((error = pcf_wait_byte(sc))) {
|
2008-08-04 20:46:15 +00:00
|
|
|
pcf_stop_locked(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
1998-10-31 11:37:09 +00:00
|
|
|
if (len == 1 && last)
|
|
|
|
/* ok, last data byte already in S0, no I2C activity
|
2004-05-25 07:42:45 +00:00
|
|
|
* on next pcf_get_S0() */
|
2008-08-04 20:46:15 +00:00
|
|
|
pcf_stop_locked(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
1998-10-31 11:37:09 +00:00
|
|
|
else if (len == 2 && last)
|
|
|
|
/* next trigged byte with no ack */
|
2004-05-25 07:42:45 +00:00
|
|
|
pcf_set_S1(sc, ESO);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
1998-10-31 11:37:09 +00:00
|
|
|
/* receive byte, trig next byte */
|
2004-05-25 07:42:45 +00:00
|
|
|
*buf++ = pcf_get_S0(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
len --;
|
|
|
|
bytes ++;
|
|
|
|
};
|
|
|
|
|
|
|
|
error:
|
|
|
|
*read = bytes;
|
2008-08-04 20:46:15 +00:00
|
|
|
PCF_UNLOCK(sc);
|
1998-09-03 21:01:22 +00:00
|
|
|
|
|
|
|
#ifdef PCFDEBUG
|
2004-05-25 07:42:45 +00:00
|
|
|
device_printf(dev, " << %d bytes read (%d): %#x%s\n", bytes, error,
|
|
|
|
(unsigned)obuf[0], bytes > 1? "...": "");
|
1998-09-03 21:01:22 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|