Home | History | Annotate | Line # | Download | only in dev
ausmbus_psc.c revision 1.1
      1  1.1  shige /* $NetBSD: ausmbus_psc.c,v 1.1 2006/03/06 17:16:45 shige Exp $ */
      2  1.1  shige 
      3  1.1  shige /*-
      4  1.1  shige  * Copyright (c) 2006 Shigeyuki Fukushima.
      5  1.1  shige  * All rights reserved.
      6  1.1  shige  *
      7  1.1  shige  * Written by Shigeyuki Fukushima.
      8  1.1  shige  *
      9  1.1  shige  * Redistribution and use in source and binary forms, with or without
     10  1.1  shige  * modification, are permitted provided that the following conditions
     11  1.1  shige  * are met:
     12  1.1  shige  * 1. Redistributions of source code must retain the above copyright
     13  1.1  shige  *    notice, this list of conditions and the following disclaimer.
     14  1.1  shige  * 2. Redistributions in binary form must reproduce the above
     15  1.1  shige  *    copyright notice, this list of conditions and the following
     16  1.1  shige  *    disclaimer in the documentation and/or other materials provided
     17  1.1  shige  *    with the distribution.
     18  1.1  shige  * 3. The name of the author may not be used to endorse or promote
     19  1.1  shige  *    products derived from this software without specific prior
     20  1.1  shige  *    written permission.
     21  1.1  shige  *
     22  1.1  shige  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     23  1.1  shige  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     24  1.1  shige  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.1  shige  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     26  1.1  shige  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.1  shige  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     28  1.1  shige  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  1.1  shige  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     30  1.1  shige  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     31  1.1  shige  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     32  1.1  shige  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  1.1  shige  */
     34  1.1  shige 
     35  1.1  shige #include <sys/cdefs.h>
     36  1.1  shige __KERNEL_RCSID(0, "$NetBSD: ausmbus_psc.c,v 1.1 2006/03/06 17:16:45 shige Exp $");
     37  1.1  shige 
     38  1.1  shige #include "locators.h"
     39  1.1  shige 
     40  1.1  shige #include <sys/param.h>
     41  1.1  shige #include <sys/systm.h>
     42  1.1  shige #include <sys/device.h>
     43  1.1  shige #include <sys/errno.h>
     44  1.1  shige 
     45  1.1  shige #include <machine/bus.h>
     46  1.1  shige #include <machine/cpu.h>
     47  1.1  shige 
     48  1.1  shige #include <mips/alchemy/dev/aupscreg.h>
     49  1.1  shige #include <mips/alchemy/dev/aupscvar.h>
     50  1.1  shige #include <mips/alchemy/dev/smbusreg.h>
     51  1.1  shige 
     52  1.1  shige #include <dev/i2c/i2cvar.h>
     53  1.1  shige #include <dev/i2c/i2c_bitbang.h>
     54  1.1  shige 
     55  1.1  shige struct ausmbus_softc {
     56  1.1  shige 	struct device			sc_dev;
     57  1.1  shige 
     58  1.1  shige 	/* protocol comoon fields */
     59  1.1  shige 	struct aupsc_controller		sc_ctrl;
     60  1.1  shige 
     61  1.1  shige 	/* protocol specific fields */
     62  1.1  shige 	struct i2c_controller		sc_i2c;
     63  1.1  shige 	i2c_addr_t			sc_smbus_slave_addr;
     64  1.1  shige 	int				sc_smbus_timeout;
     65  1.1  shige };
     66  1.1  shige 
     67  1.1  shige #define	ausmbus_reg_read(sc, reg) \
     68  1.1  shige 	bus_space_read_4(sc->sc_ctrl.psc_bust, sc->sc_ctrl.psc_bush, reg)
     69  1.1  shige #define	ausmbus_reg_write(sc, reg, val) \
     70  1.1  shige 	bus_space_write_4(sc->sc_ctrl.psc_bust, sc->sc_ctrl.psc_bush, reg, val)
     71  1.1  shige 
     72  1.1  shige static int	ausmbus_match(struct device *, struct cfdata *, void *);
     73  1.1  shige static void	ausmbus_attach(struct device *, struct device *, void *);
     74  1.1  shige 
     75  1.1  shige CFATTACH_DECL(ausmbus, sizeof(struct ausmbus_softc),
     76  1.1  shige 	ausmbus_match, ausmbus_attach, NULL, NULL);
     77  1.1  shige 
     78  1.1  shige /* fuctions for i2c_controller */
     79  1.1  shige static int	ausmbus_acquire_bus(void *, int);
     80  1.1  shige static void	ausmbus_release_bus(void *, int);
     81  1.1  shige static int	ausmbus_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
     82  1.1  shige 				const void *cmd, size_t cmdlen, void *vbuf,
     83  1.1  shige 				size_t buflen, int flags);
     84  1.1  shige 
     85  1.1  shige /* subroutine functions for i2c_controller */
     86  1.1  shige static int	ausmbus_receive_1(struct ausmbus_softc *, uint8_t *);
     87  1.1  shige static int	ausmbus_read_1(struct ausmbus_softc *, uint8_t, uint8_t *);
     88  1.1  shige static int	ausmbus_send_1(struct ausmbus_softc *, uint8_t);
     89  1.1  shige static int	ausmbus_write_1(struct ausmbus_softc *, uint8_t, uint8_t);
     90  1.1  shige static int	ausmbus_wait_mastertx(struct ausmbus_softc *sc);
     91  1.1  shige static int	ausmbus_wait_masterrx(struct ausmbus_softc *sc);
     92  1.1  shige static int	ausmbus_initiate_xfer(void *, i2c_addr_t, int);
     93  1.1  shige static int	ausmbus_read_byte(void *arg, uint8_t *vp, int flags);
     94  1.1  shige static int	ausmbus_write_byte(void *arg, uint8_t v, int flags);
     95  1.1  shige 
     96  1.1  shige 
     97  1.1  shige static int
     98  1.1  shige ausmbus_match(struct device *parent, struct cfdata *cf, void *aux)
     99  1.1  shige {
    100  1.1  shige 	struct aupsc_attach_args *aa = (struct aupsc_attach_args *)aux;
    101  1.1  shige 
    102  1.1  shige 	if (strcmp(aa->aupsc_name, cf->cf_name) != 0)
    103  1.1  shige 		return 0;
    104  1.1  shige 
    105  1.1  shige 	return 1;
    106  1.1  shige }
    107  1.1  shige 
    108  1.1  shige static void
    109  1.1  shige ausmbus_attach(struct device *parent, struct device *self, void *aux)
    110  1.1  shige {
    111  1.1  shige 	struct ausmbus_softc *sc = (struct ausmbus_softc *)self;
    112  1.1  shige 	struct aupsc_attach_args *aa = (struct aupsc_attach_args *)aux;
    113  1.1  shige 	struct i2cbus_attach_args iba;
    114  1.1  shige 
    115  1.1  shige 	aprint_normal(": Alchemy PSC SMBus protocol\n");
    116  1.1  shige 
    117  1.1  shige 	/* Initialize PSC */
    118  1.1  shige 	sc->sc_ctrl = aa->aupsc_ctrl;
    119  1.1  shige 
    120  1.1  shige 	/* Initialize i2c_controller for SMBus */
    121  1.1  shige 	sc->sc_i2c.ic_cookie = sc;
    122  1.1  shige 	sc->sc_i2c.ic_acquire_bus = ausmbus_acquire_bus;
    123  1.1  shige 	sc->sc_i2c.ic_release_bus = ausmbus_release_bus;
    124  1.1  shige 	sc->sc_i2c.ic_send_start = NULL;
    125  1.1  shige 	sc->sc_i2c.ic_send_stop = NULL;
    126  1.1  shige 	sc->sc_i2c.ic_initiate_xfer = NULL;
    127  1.1  shige 	sc->sc_i2c.ic_read_byte = NULL;
    128  1.1  shige 	sc->sc_i2c.ic_write_byte = NULL;
    129  1.1  shige 	sc->sc_i2c.ic_exec = ausmbus_exec;
    130  1.1  shige 	sc->sc_smbus_timeout = 10;
    131  1.1  shige 
    132  1.1  shige 	{
    133  1.1  shige 	uint8_t cmd[1];
    134  1.1  shige 	uint8_t vbuf[1];
    135  1.1  shige 
    136  1.1  shige #if 1
    137  1.1  shige 	ausmbus_acquire_bus(sc, 0);
    138  1.1  shige 	cmd[0] = 0x50;
    139  1.1  shige 	vbuf[0] = 0x86;
    140  1.1  shige 	ausmbus_exec(sc, I2C_OP_WRITE_WITH_STOP, 0x33, cmd,1, vbuf,1, 0);
    141  1.1  shige 	ausmbus_release_bus(sc, 0);
    142  1.1  shige #endif
    143  1.1  shige 
    144  1.1  shige 	ausmbus_acquire_bus(sc, 0);
    145  1.1  shige 	cmd[0] = 0x50;
    146  1.1  shige 	vbuf[0] = 0x0;
    147  1.1  shige 	ausmbus_exec(sc, I2C_OP_READ_WITH_STOP, 0x32, cmd,1, &vbuf[0],1, 0);
    148  1.1  shige 	printf("iic_exec: vbuf[0]=0x%x\n", vbuf[0]);
    149  1.1  shige 	ausmbus_release_bus(sc, 0);
    150  1.1  shige 
    151  1.1  shige 	}
    152  1.1  shige 
    153  1.1  shige 	iba.iba_name = "iic";
    154  1.1  shige 	iba.iba_tag = &sc->sc_i2c;
    155  1.1  shige 	(void) config_found(&sc->sc_dev, &iba, iicbus_print);
    156  1.1  shige }
    157  1.1  shige 
    158  1.1  shige static int
    159  1.1  shige ausmbus_acquire_bus(void *arg, int flags)
    160  1.1  shige {
    161  1.1  shige 	struct ausmbus_softc *sc = arg;
    162  1.1  shige 	uint32_t v;
    163  1.1  shige 
    164  1.1  shige 	/* Select SMBus Protocol & Enable PSC */
    165  1.1  shige 	sc->sc_ctrl.psc_enable(sc, AUPSC_SEL_SMBUS);
    166  1.1  shige 	v = ausmbus_reg_read(sc, AUPSC_SMBSTAT);
    167  1.1  shige 	if ((v & SMBUS_STAT_SR) == 0) {
    168  1.1  shige 		/* PSC is not ready */
    169  1.1  shige 		return -1;
    170  1.1  shige 	}
    171  1.1  shige 
    172  1.1  shige 	/* Setup SMBus Configuration register */
    173  1.1  shige 	v = SMBUS_CFG_DD;				/* Disable DMA */
    174  1.1  shige 	v |= SMBUS_CFG_RT_SET(SMBUS_CFG_RT_FIFO8);	/* Rx FIFO 8data */
    175  1.1  shige 	v |= SMBUS_CFG_TT_SET(SMBUS_CFG_TT_FIFO8);	/* Tx FIFO 8data */
    176  1.1  shige 	v |= SMBUS_CFG_DIV_SET(SMBUS_CFG_DIV8);		/* pscn_mainclk/8 */
    177  1.1  shige 	v &= ~SMBUS_CFG_SFM;				/* Standard Mode */
    178  1.1  shige 	ausmbus_reg_write(sc, AUPSC_SMBCFG, v);
    179  1.1  shige 
    180  1.1  shige 	/* Setup SMBus Protocol Timing register */
    181  1.1  shige 	v = SMBUS_TMR_TH_SET(SMBUS_TMR_STD_TH)
    182  1.1  shige 		| SMBUS_TMR_PS_SET(SMBUS_TMR_STD_PS)
    183  1.1  shige 		| SMBUS_TMR_PU_SET(SMBUS_TMR_STD_PU)
    184  1.1  shige 		| SMBUS_TMR_SH_SET(SMBUS_TMR_STD_SH)
    185  1.1  shige 		| SMBUS_TMR_SU_SET(SMBUS_TMR_STD_SU)
    186  1.1  shige 		| SMBUS_TMR_CL_SET(SMBUS_TMR_STD_CL)
    187  1.1  shige 		| SMBUS_TMR_CH_SET(SMBUS_TMR_STD_CH);
    188  1.1  shige 	ausmbus_reg_write(sc, AUPSC_SMBTMR, v);
    189  1.1  shige 
    190  1.1  shige 	/* Setup SMBus Mask register */
    191  1.1  shige 	v = SMBUS_MSK_ALLMASK;
    192  1.1  shige 	ausmbus_reg_write(sc, AUPSC_SMBMSK, v);
    193  1.1  shige 
    194  1.1  shige 	/* SMBus Enable */
    195  1.1  shige 	v = ausmbus_reg_read(sc, AUPSC_SMBCFG);
    196  1.1  shige 	v |= SMBUS_CFG_DE;
    197  1.1  shige 	ausmbus_reg_write(sc, AUPSC_SMBCFG, v);
    198  1.1  shige 	v = ausmbus_reg_read(sc, AUPSC_SMBSTAT);
    199  1.1  shige 	if ((v & SMBUS_STAT_SR) == 0) {
    200  1.1  shige 		/* SMBus is not ready */
    201  1.1  shige 		return -1;
    202  1.1  shige 	}
    203  1.1  shige 
    204  1.1  shige #ifdef AUSMBUS_PSC_DEBUG
    205  1.1  shige 	aprint_normal("AuSMBus enabled.\n");
    206  1.1  shige 	aprint_normal("AuSMBus smbconfig: 0x%08x\n",
    207  1.1  shige 			ausmbus_reg_read(sc, AUPSC_SMBCFG));
    208  1.1  shige 	aprint_normal("AuSMBus smbstatus: 0x%08x\n",
    209  1.1  shige 			ausmbus_reg_read(sc, AUPSC_SMBSTAT));
    210  1.1  shige 	aprint_normal("AuSMBus smbtmr   : 0x%08x\n",
    211  1.1  shige 			ausmbus_reg_read(sc, AUPSC_SMBTMR));
    212  1.1  shige 	aprint_normal("AuSMBus smbmask  : 0x%08x\n",
    213  1.1  shige 			ausmbus_reg_read(sc, AUPSC_SMBMSK));
    214  1.1  shige #endif
    215  1.1  shige 
    216  1.1  shige 	return 0;
    217  1.1  shige }
    218  1.1  shige 
    219  1.1  shige static void
    220  1.1  shige ausmbus_release_bus(void *arg, int flags)
    221  1.1  shige {
    222  1.1  shige 	struct ausmbus_softc *sc = arg;
    223  1.1  shige 
    224  1.1  shige 	ausmbus_reg_write(sc, AUPSC_SMBCFG, 0);
    225  1.1  shige 	sc->sc_ctrl.psc_disable(sc);
    226  1.1  shige 
    227  1.1  shige 	return;
    228  1.1  shige }
    229  1.1  shige 
    230  1.1  shige static int
    231  1.1  shige ausmbus_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
    232  1.1  shige 	size_t cmdlen, void *vbuf, size_t buflen, int flags)
    233  1.1  shige {
    234  1.1  shige 	struct ausmbus_softc *sc  = (struct ausmbus_softc *)cookie;
    235  1.1  shige 	const uint8_t *cmd = vcmd;
    236  1.1  shige 	uint8_t *buf = vbuf;
    237  1.1  shige 
    238  1.1  shige 	sc->sc_smbus_slave_addr  = addr;
    239  1.1  shige 
    240  1.1  shige 	/* Receive byte */
    241  1.1  shige 	if ((I2C_OP_READ_P(op)) && (cmdlen == 0) && (buflen == 1)) {
    242  1.1  shige 		return ausmbus_receive_1(sc, buf);
    243  1.1  shige 	}
    244  1.1  shige 
    245  1.1  shige 	/* Read byte */
    246  1.1  shige 	if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 1)) {
    247  1.1  shige 		return ausmbus_read_1(sc, *cmd, buf);
    248  1.1  shige 	}
    249  1.1  shige 
    250  1.1  shige 	/* Send byte */
    251  1.1  shige 	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 1)) {
    252  1.1  shige 		return ausmbus_send_1(sc, *buf);
    253  1.1  shige 	}
    254  1.1  shige 
    255  1.1  shige 	/* Write byte */
    256  1.1  shige 	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 1)) {
    257  1.1  shige 		return ausmbus_write_1(sc, *cmd, *buf);
    258  1.1  shige 	}
    259  1.1  shige 
    260  1.1  shige 	/*
    261  1.1  shige 	 * XXX: TODO Please Support other protocols defined in SMBus 2.0
    262  1.1  shige 	 * - Quick Command
    263  1.1  shige 	 * - Write word
    264  1.1  shige 	 * - Read word
    265  1.1  shige 	 * - Process call
    266  1.1  shige 	 * - Block write/read
    267  1.1  shige 	 * - Clock write-block read process cal
    268  1.1  shige 	 * - SMBus host notify protocol
    269  1.1  shige 	 */
    270  1.1  shige 
    271  1.1  shige 	return -1;
    272  1.1  shige }
    273  1.1  shige 
    274  1.1  shige static int
    275  1.1  shige ausmbus_receive_1(struct ausmbus_softc *sc, uint8_t *vp)
    276  1.1  shige {
    277  1.1  shige 	int error;
    278  1.1  shige 
    279  1.1  shige 	error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_READ);
    280  1.1  shige 	if (error != 0) {
    281  1.1  shige 		return error;
    282  1.1  shige 	}
    283  1.1  shige 	error = ausmbus_read_byte(sc, vp, I2C_F_STOP);
    284  1.1  shige 	if (error != 0) {
    285  1.1  shige 		return error;
    286  1.1  shige 	}
    287  1.1  shige 
    288  1.1  shige 	return 0;
    289  1.1  shige }
    290  1.1  shige 
    291  1.1  shige static int
    292  1.1  shige ausmbus_read_1(struct ausmbus_softc *sc, uint8_t cmd, uint8_t *vp)
    293  1.1  shige {
    294  1.1  shige 	int error;
    295  1.1  shige 
    296  1.1  shige 	error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_WRITE);
    297  1.1  shige 	if (error != 0) {
    298  1.1  shige 		return error;
    299  1.1  shige 	}
    300  1.1  shige 
    301  1.1  shige 	error = ausmbus_write_byte(sc, cmd, I2C_F_READ);
    302  1.1  shige 	if (error != 0) {
    303  1.1  shige 		return error;
    304  1.1  shige 	}
    305  1.1  shige 
    306  1.1  shige 	error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_READ);
    307  1.1  shige 	if (error != 0) {
    308  1.1  shige 		return error;
    309  1.1  shige 	}
    310  1.1  shige 
    311  1.1  shige 	error = ausmbus_read_byte(sc, vp, I2C_F_STOP);
    312  1.1  shige 	if (error != 0) {
    313  1.1  shige 		return error;
    314  1.1  shige 	}
    315  1.1  shige 
    316  1.1  shige 	return 0;
    317  1.1  shige }
    318  1.1  shige 
    319  1.1  shige static int
    320  1.1  shige ausmbus_send_1(struct ausmbus_softc *sc, uint8_t val)
    321  1.1  shige {
    322  1.1  shige 	int error;
    323  1.1  shige 
    324  1.1  shige 	error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_WRITE);
    325  1.1  shige 	if (error != 0) {
    326  1.1  shige 		return error;
    327  1.1  shige 	}
    328  1.1  shige 
    329  1.1  shige 	error = ausmbus_write_byte(sc, val, I2C_F_STOP);
    330  1.1  shige 	if (error != 0) {
    331  1.1  shige 		return error;
    332  1.1  shige 	}
    333  1.1  shige 
    334  1.1  shige 	return 0;
    335  1.1  shige }
    336  1.1  shige 
    337  1.1  shige static int
    338  1.1  shige ausmbus_write_1(struct ausmbus_softc *sc, uint8_t cmd, uint8_t val)
    339  1.1  shige {
    340  1.1  shige 	int error;
    341  1.1  shige 
    342  1.1  shige 	error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_WRITE);
    343  1.1  shige 	if (error != 0) {
    344  1.1  shige 		return error;
    345  1.1  shige 	}
    346  1.1  shige 
    347  1.1  shige 	error = ausmbus_write_byte(sc, cmd, 0);
    348  1.1  shige 	if (error != 0) {
    349  1.1  shige 		return error;
    350  1.1  shige 	}
    351  1.1  shige 
    352  1.1  shige 	error = ausmbus_write_byte(sc, val, I2C_F_STOP);
    353  1.1  shige 	if (error != 0) {
    354  1.1  shige 		return error;
    355  1.1  shige 	}
    356  1.1  shige 
    357  1.1  shige 	return 0;
    358  1.1  shige }
    359  1.1  shige 
    360  1.1  shige static int
    361  1.1  shige ausmbus_wait_mastertx(struct ausmbus_softc *sc)
    362  1.1  shige {
    363  1.1  shige 	uint32_t v;
    364  1.1  shige 	int timeout;
    365  1.1  shige 	int txerr = 0;
    366  1.1  shige 
    367  1.1  shige 	timeout = sc->sc_smbus_timeout;
    368  1.1  shige 
    369  1.1  shige 	do {
    370  1.1  shige 		v = ausmbus_reg_read(sc, AUPSC_SMBEVNT);
    371  1.1  shige #ifdef AUSMBUS_PSC_DEBUG
    372  1.1  shige 		aprint_normal("AuSMBus: ausmbus_wait_mastertx(): psc_smbevnt=0x%08x\n", v);
    373  1.1  shige #endif
    374  1.1  shige 		if ((v & SMBUS_EVNT_TU) != 0)
    375  1.1  shige 			break;
    376  1.1  shige 		if ((v & SMBUS_EVNT_MD) != 0)
    377  1.1  shige 			break;
    378  1.1  shige 		if ((v & (SMBUS_EVNT_DN | SMBUS_EVNT_AN | SMBUS_EVNT_AL))
    379  1.1  shige 			!= 0) {
    380  1.1  shige 			txerr = 1;
    381  1.1  shige 			break;
    382  1.1  shige 		}
    383  1.1  shige 		timeout--;
    384  1.1  shige 		delay(1);
    385  1.1  shige 	} while (timeout > 0);
    386  1.1  shige 
    387  1.1  shige 	if (txerr != 0) {
    388  1.1  shige 		ausmbus_reg_write(sc, AUPSC_SMBEVNT,
    389  1.1  shige 			SMBUS_EVNT_DN | SMBUS_EVNT_AN | SMBUS_EVNT_AL);
    390  1.1  shige #ifdef AUSMBUS_PSC_DEBUG
    391  1.1  shige 		aprint_normal("AuSMBus: ausmbus_wait_mastertx(): Tx error\n");
    392  1.1  shige #endif
    393  1.1  shige 		return -1;
    394  1.1  shige 	}
    395  1.1  shige 
    396  1.1  shige 	/* Reset Event TU (Tx Underflow) */
    397  1.1  shige 	ausmbus_reg_write(sc, AUPSC_SMBEVNT, SMBUS_EVNT_TU | SMBUS_EVNT_MD);
    398  1.1  shige 
    399  1.1  shige #ifdef AUSMBUS_PSC_DEBUG
    400  1.1  shige 	v = ausmbus_reg_read(sc, AUPSC_SMBEVNT);
    401  1.1  shige 	aprint_normal("AuSMBus: ausmbus_wait_mastertx(): psc_smbevnt=0x%08x (reset)\n", v);
    402  1.1  shige #endif
    403  1.1  shige 	return 0;
    404  1.1  shige }
    405  1.1  shige 
    406  1.1  shige static int
    407  1.1  shige ausmbus_wait_masterrx(struct ausmbus_softc *sc)
    408  1.1  shige {
    409  1.1  shige 	uint32_t v;
    410  1.1  shige 	int timeout;
    411  1.1  shige 	timeout = sc->sc_smbus_timeout;
    412  1.1  shige 
    413  1.1  shige 	if (ausmbus_wait_mastertx(sc) != 0)
    414  1.1  shige 		return -1;
    415  1.1  shige 
    416  1.1  shige 	do {
    417  1.1  shige 		v = ausmbus_reg_read(sc, AUPSC_SMBSTAT);
    418  1.1  shige #ifdef AUSMBUS_PSC_DEBUG
    419  1.1  shige 		aprint_normal("AuSMBus: ausmbus_wait_masterrx(): psc_smbstat=0x%08x\n", v);
    420  1.1  shige #endif
    421  1.1  shige 		if ((v & SMBUS_STAT_RE) == 0)
    422  1.1  shige 			break;
    423  1.1  shige 		timeout--;
    424  1.1  shige 		delay(1);
    425  1.1  shige 	} while (timeout > 0);
    426  1.1  shige 
    427  1.1  shige 	return 0;
    428  1.1  shige }
    429  1.1  shige 
    430  1.1  shige static int
    431  1.1  shige ausmbus_initiate_xfer(void *arg, i2c_addr_t addr, int flags)
    432  1.1  shige {
    433  1.1  shige 	struct ausmbus_softc *sc = arg;
    434  1.1  shige 	uint32_t v;
    435  1.1  shige 
    436  1.1  shige 	/* Tx/Rx Slave Address */
    437  1.1  shige 	v = (addr << 1) & SMBUS_TXRX_ADDRDATA;
    438  1.1  shige 	if ((flags & I2C_F_READ) != 0)
    439  1.1  shige 		v |= 1;
    440  1.1  shige 	ausmbus_reg_write(sc, AUPSC_SMBTXRX, v);
    441  1.1  shige 
    442  1.1  shige 	/* Master Start */
    443  1.1  shige 	ausmbus_reg_write(sc, AUPSC_SMBPCR, SMBUS_PCR_MS);
    444  1.1  shige 
    445  1.1  shige 	if (ausmbus_wait_mastertx(sc) != 0)
    446  1.1  shige 		return -1;
    447  1.1  shige 
    448  1.1  shige 	return 0;
    449  1.1  shige }
    450  1.1  shige 
    451  1.1  shige static int
    452  1.1  shige ausmbus_read_byte(void *arg, uint8_t *vp, int flags)
    453  1.1  shige {
    454  1.1  shige 	struct ausmbus_softc *sc = arg;
    455  1.1  shige 	uint32_t v;
    456  1.1  shige 
    457  1.1  shige 	if ((flags & I2C_F_STOP) != 0) {
    458  1.1  shige 		ausmbus_reg_write(sc, AUPSC_SMBTXRX, SMBUS_TXRX_STP);
    459  1.1  shige 	} else {
    460  1.1  shige 		ausmbus_reg_write(sc, AUPSC_SMBTXRX, 0);
    461  1.1  shige 	}
    462  1.1  shige 
    463  1.1  shige 	if (ausmbus_wait_masterrx(sc) != 0)
    464  1.1  shige 		return -1;
    465  1.1  shige 
    466  1.1  shige 	v = ausmbus_reg_read(sc, AUPSC_SMBTXRX);
    467  1.1  shige 	*vp = v & SMBUS_TXRX_ADDRDATA;
    468  1.1  shige 	printf("read byte: 0x%02x\n", v);
    469  1.1  shige 
    470  1.1  shige 	return 0;
    471  1.1  shige }
    472  1.1  shige 
    473  1.1  shige static int
    474  1.1  shige ausmbus_write_byte(void *arg, uint8_t v, int flags)
    475  1.1  shige {
    476  1.1  shige 	struct ausmbus_softc *sc = arg;
    477  1.1  shige 
    478  1.1  shige 	if ((flags & I2C_F_STOP) != 0)  {
    479  1.1  shige 		ausmbus_reg_write(sc, AUPSC_SMBTXRX, (v | SMBUS_TXRX_STP));
    480  1.1  shige 	} else if ((flags & I2C_F_READ) != 0) {
    481  1.1  shige 		ausmbus_reg_write(sc, AUPSC_SMBTXRX, (v | SMBUS_TXRX_RSR));
    482  1.1  shige 	} else {
    483  1.1  shige 		ausmbus_reg_write(sc, AUPSC_SMBTXRX, v);
    484  1.1  shige 	}
    485  1.1  shige 
    486  1.1  shige 	if (ausmbus_wait_mastertx(sc) != 0)
    487  1.1  shige 		return -1;
    488  1.1  shige 
    489  1.1  shige 	return 0;
    490  1.1  shige }
    491