Home | History | Annotate | Line # | Download | only in pci
      1   1.1   msaitoh /*-
      2   1.1   msaitoh  * Copyright (c) 2016 The NetBSD Foundation, Inc.
      3   1.1   msaitoh  * All rights reserved.
      4   1.1   msaitoh  *
      5   1.1   msaitoh  * This code is derived from software contributed to The NetBSD Foundation
      6   1.1   msaitoh  * by Masanobu SAITOH.
      7   1.1   msaitoh  *
      8   1.1   msaitoh  * Redistribution and use in source and binary forms, with or without
      9   1.1   msaitoh  * modification, are permitted provided that the following conditions
     10   1.1   msaitoh  * are met:
     11   1.1   msaitoh  * 1. Redistributions of source code must retain the above copyright
     12   1.1   msaitoh  *    notice, this list of conditions and the following disclaimer.
     13   1.1   msaitoh  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1   msaitoh  *    notice, this list of conditions and the following disclaimer in the
     15   1.1   msaitoh  *    documentation and/or other materials provided with the distribution.
     16   1.1   msaitoh  *
     17   1.1   msaitoh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18   1.1   msaitoh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19   1.1   msaitoh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20   1.1   msaitoh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21   1.1   msaitoh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22   1.1   msaitoh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23   1.1   msaitoh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24   1.1   msaitoh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25   1.1   msaitoh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26   1.1   msaitoh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27   1.1   msaitoh  * POSSIBILITY OF SUCH DAMAGE.
     28   1.1   msaitoh  */
     29   1.1   msaitoh 
     30   1.1   msaitoh /*-
     31   1.1   msaitoh  * Copyright (C) 2014 Intel Corporation
     32   1.1   msaitoh  * All rights reserved.
     33   1.1   msaitoh  *
     34   1.1   msaitoh  * Redistribution and use in source and binary forms, with or without
     35   1.1   msaitoh  * modification, are permitted provided that the following conditions
     36   1.1   msaitoh  * are met:
     37   1.1   msaitoh  * 1. Redistributions of source code must retain the above copyright
     38   1.1   msaitoh  *    notice, this list of conditions and the following disclaimer.
     39   1.1   msaitoh  * 2. Redistributions in binary form must reproduce the above copyright
     40   1.1   msaitoh  *    notice, this list of conditions and the following disclaimer in the
     41   1.1   msaitoh  *    documentation and/or other materials provided with the distribution.
     42   1.1   msaitoh  * 3. Neither the name of Intel Corporation nor the names of its
     43   1.1   msaitoh  *    contributors may be used to endorse or promote products derived from
     44   1.1   msaitoh  *    this software without specific prior written permission.
     45   1.1   msaitoh  *
     46   1.1   msaitoh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     47   1.1   msaitoh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     48   1.1   msaitoh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     49   1.1   msaitoh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     50   1.1   msaitoh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     51   1.1   msaitoh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     52   1.1   msaitoh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     53   1.1   msaitoh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     54   1.1   msaitoh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     55   1.1   msaitoh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     56   1.1   msaitoh  * SUCH DAMAGE.
     57   1.1   msaitoh  */
     58   1.1   msaitoh 
     59   1.1   msaitoh #include <sys/cdefs.h>
     60   1.1   msaitoh #if 0
     61   1.1   msaitoh __FBSDID("$FreeBSD: head/sys/dev/ismt/ismt.c 266474 2014-05-20 19:55:06Z jimharris $");
     62   1.1   msaitoh #endif
     63  1.12   thorpej __KERNEL_RCSID(0, "$NetBSD: ismt.c,v 1.12 2025/09/15 13:23:03 thorpej Exp $");
     64   1.1   msaitoh 
     65   1.1   msaitoh #include <sys/param.h>
     66   1.1   msaitoh #include <sys/systm.h>
     67   1.1   msaitoh #include <sys/device.h>
     68   1.1   msaitoh #include <sys/errno.h>
     69   1.1   msaitoh #include <sys/kernel.h>
     70   1.1   msaitoh #include <sys/module.h>
     71   1.1   msaitoh #include <sys/proc.h>
     72   1.1   msaitoh 
     73   1.1   msaitoh #include <sys/bus.h>
     74   1.1   msaitoh 
     75   1.1   msaitoh #include <dev/pci/pcidevs.h>
     76   1.1   msaitoh #include <dev/pci/pcireg.h>
     77   1.1   msaitoh #include <dev/pci/pcivar.h>
     78   1.1   msaitoh 
     79   1.1   msaitoh #include <dev/i2c/i2cvar.h>
     80   1.1   msaitoh 
     81   1.1   msaitoh #define ISMT_DESC_ENTRIES	32
     82   1.1   msaitoh 
     83   1.1   msaitoh /* Hardware Descriptor Constants - Control Field */
     84   1.1   msaitoh #define ISMT_DESC_CWRL	0x01	/* Command/Write Length */
     85   1.1   msaitoh #define ISMT_DESC_BLK	0X04	/* Perform Block Transaction */
     86   1.1   msaitoh #define ISMT_DESC_FAIR	0x08	/* Set fairness flag upon successful arbit. */
     87   1.1   msaitoh #define ISMT_DESC_PEC	0x10	/* Packet Error Code */
     88   1.1   msaitoh #define ISMT_DESC_I2C	0x20	/* I2C Enable */
     89   1.1   msaitoh #define ISMT_DESC_INT	0x40	/* Interrupt */
     90   1.1   msaitoh #define ISMT_DESC_SOE	0x80	/* Stop On Error */
     91   1.1   msaitoh 
     92   1.1   msaitoh /* Hardware Descriptor Constants - Status Field */
     93   1.1   msaitoh #define ISMT_DESC_SCS	0x01	/* Success */
     94   1.1   msaitoh #define ISMT_DESC_DLTO	0x04	/* Data Low Time Out */
     95   1.1   msaitoh #define ISMT_DESC_NAK	0x08	/* NAK Received */
     96   1.1   msaitoh #define ISMT_DESC_CRC	0x10	/* CRC Error */
     97   1.1   msaitoh #define ISMT_DESC_CLTO	0x20	/* Clock Low Time Out */
     98   1.1   msaitoh #define ISMT_DESC_COL	0x40	/* Collisions */
     99   1.1   msaitoh #define ISMT_DESC_LPR	0x80	/* Large Packet Received */
    100   1.1   msaitoh 
    101   1.1   msaitoh /* Macros */
    102   1.3   msaitoh #define ISMT_DESC_ADDR_RW(addr, is_read) (((addr) << 1) | (is_read))
    103   1.1   msaitoh 
    104   1.1   msaitoh /* iSMT General Register address offsets (SMBBAR + <addr>) */
    105   1.1   msaitoh #define ISMT_GR_GCTRL		0x000	/* General Control */
    106   1.1   msaitoh #define ISMT_GR_SMTICL		0x008	/* SMT Interrupt Cause Location */
    107   1.1   msaitoh #define ISMT_GR_ERRINTMSK	0x010	/* Error Interrupt Mask */
    108   1.1   msaitoh #define ISMT_GR_ERRAERMSK	0x014	/* Error AER Mask */
    109   1.1   msaitoh #define ISMT_GR_ERRSTS		0x018	/* Error Status */
    110   1.1   msaitoh #define ISMT_GR_ERRINFO		0x01c	/* Error Information */
    111   1.1   msaitoh 
    112   1.1   msaitoh /* iSMT Master Registers */
    113   1.1   msaitoh #define ISMT_MSTR_MDBA		0x100	/* Master Descriptor Base Address */
    114   1.1   msaitoh #define ISMT_MSTR_MCTRL		0x108	/* Master Control */
    115   1.1   msaitoh #define ISMT_MSTR_MSTS		0x10c	/* Master Status */
    116   1.1   msaitoh #define ISMT_MSTR_MDS		0x110	/* Master Descriptor Size */
    117   1.1   msaitoh #define ISMT_MSTR_RPOLICY	0x114	/* Retry Policy */
    118   1.1   msaitoh 
    119   1.1   msaitoh /* iSMT Miscellaneous Registers */
    120   1.1   msaitoh #define ISMT_SPGT	0x300	/* SMBus PHY Global Timing */
    121   1.1   msaitoh 
    122   1.1   msaitoh /* General Control Register (GCTRL) bit definitions */
    123   1.1   msaitoh #define ISMT_GCTRL_TRST	0x04	/* Target Reset */
    124   1.1   msaitoh #define ISMT_GCTRL_KILL	0x08	/* Kill */
    125   1.1   msaitoh #define ISMT_GCTRL_SRST	0x40	/* Soft Reset */
    126   1.1   msaitoh 
    127   1.1   msaitoh /* Master Control Register (MCTRL) bit definitions */
    128   1.1   msaitoh #define ISMT_MCTRL_SS	0x01		/* Start/Stop */
    129   1.1   msaitoh #define ISMT_MCTRL_MEIE	0x10		/* Master Error Interrupt Enable */
    130   1.1   msaitoh #define ISMT_MCTRL_FMHP	0x00ff0000	/* Firmware Master Head Ptr (FMHP) */
    131   1.1   msaitoh 
    132   1.1   msaitoh /* Master Status Register (MSTS) bit definitions */
    133   1.1   msaitoh #define ISMT_MSTS_HMTP	0xff0000	/* HW Master Tail Pointer (HMTP) */
    134   1.1   msaitoh #define ISMT_MSTS_MIS	0x20		/* Master Interrupt Status (MIS) */
    135   1.1   msaitoh #define ISMT_MSTS_MEIS	0x10		/* Master Error Int Status (MEIS) */
    136   1.1   msaitoh #define ISMT_MSTS_IP	0x01		/* In Progress */
    137   1.1   msaitoh 
    138   1.1   msaitoh /* Master Descriptor Size (MDS) bit definitions */
    139   1.1   msaitoh #define ISMT_MDS_MASK	0xff	/* Master Descriptor Size mask (MDS) */
    140   1.1   msaitoh 
    141   1.1   msaitoh /* SMBus PHY Global Timing Register (SPGT) bit definitions */
    142   1.1   msaitoh #define ISMT_SPGT_SPD_MASK	0xc0000000	/* SMBus Speed mask */
    143   1.1   msaitoh #define ISMT_SPGT_SPD_80K	0x00		/* 80 kHz */
    144   1.1   msaitoh #define ISMT_SPGT_SPD_100K	(0x1 << 30)	/* 100 kHz */
    145   1.1   msaitoh #define ISMT_SPGT_SPD_400K	(0x2 << 30)	/* 400 kHz */
    146   1.1   msaitoh #define ISMT_SPGT_SPD_1M	(0x3 << 30)	/* 1 MHz */
    147   1.1   msaitoh 
    148   1.1   msaitoh /* MSI Control Register (MSICTL) bit definitions */
    149   1.1   msaitoh #define ISMT_MSICTL_MSIE	0x01	/* MSI Enable */
    150   1.1   msaitoh 
    151   1.1   msaitoh #define ISMT_MAX_BLOCK_SIZE	32 /* per SMBus spec */
    152   1.1   msaitoh 
    153   1.1   msaitoh #define ISMT_INTR_TIMEOUT	(hz / 50) /* 0.02s */
    154   1.1   msaitoh #define ISMT_POLL_DELAY		100	/* 100usec */
    155   1.1   msaitoh #define ISMT_POLL_COUNT		200	/* 100usec * 200 = 0.02s */
    156   1.1   msaitoh 
    157   1.1   msaitoh //#define ISMT_DEBUG	aprint_debug_dev
    158   1.1   msaitoh #ifndef ISMT_DEBUG
    159   1.1   msaitoh #define ISMT_DEBUG(...)
    160   1.1   msaitoh #endif
    161   1.1   msaitoh 
    162   1.2  christos #define ISMT_LOW(a)	((a) & 0xFFFFFFFFULL)
    163   1.2  christos #define ISMT_HIGH(a)	(((uint64_t)(a) >> 32) & 0xFFFFFFFFFULL)
    164   1.2  christos 
    165   1.1   msaitoh /* iSMT Hardware Descriptor */
    166   1.1   msaitoh struct ismt_desc {
    167   1.1   msaitoh 	uint8_t tgtaddr_rw;	/* target address & r/w bit */
    168   1.1   msaitoh 	uint8_t wr_len_cmd;	/* write length in bytes or a command */
    169   1.1   msaitoh 	uint8_t rd_len;		/* read length */
    170   1.1   msaitoh 	uint8_t control;	/* control bits */
    171   1.1   msaitoh 	uint8_t status;		/* status bits */
    172   1.1   msaitoh 	uint8_t retry;		/* collision retry and retry count */
    173   1.1   msaitoh 	uint8_t rxbytes;	/* received bytes */
    174   1.1   msaitoh 	uint8_t txbytes;	/* transmitted bytes */
    175   1.1   msaitoh 	uint32_t dptr_low;	/* lower 32 bit of the data pointer */
    176   1.1   msaitoh 	uint32_t dptr_high;	/* upper 32 bit of the data pointer */
    177   1.1   msaitoh } __packed;
    178   1.1   msaitoh 
    179   1.1   msaitoh #define DESC_SIZE	(ISMT_DESC_ENTRIES * sizeof(struct ismt_desc))
    180   1.1   msaitoh 
    181   1.1   msaitoh #define DMA_BUFFER_SIZE	64
    182   1.1   msaitoh 
    183   1.1   msaitoh struct ismt_softc {
    184   1.1   msaitoh 	device_t		pcidev;
    185   1.1   msaitoh 	device_t		smbdev;
    186   1.1   msaitoh 
    187   1.1   msaitoh 	struct i2c_controller	sc_i2c_tag;
    188   1.1   msaitoh 
    189   1.1   msaitoh 	pci_chipset_tag_t	sc_pc;
    190   1.1   msaitoh 	pcitag_t		sc_pcitag;
    191   1.1   msaitoh 	pci_intr_handle_t	*sc_pihp;
    192   1.1   msaitoh 	void			*sc_ih;
    193   1.1   msaitoh 
    194   1.1   msaitoh 	bus_space_tag_t		mmio_tag;
    195   1.1   msaitoh 	bus_space_handle_t	mmio_handle;
    196   1.1   msaitoh 	bus_size_t		mmio_size;
    197   1.1   msaitoh 
    198   1.1   msaitoh 	uint8_t			head;
    199   1.1   msaitoh 
    200   1.1   msaitoh 	struct ismt_desc	*desc;
    201   1.1   msaitoh 	bus_dma_tag_t		desc_dma_tag;
    202   1.1   msaitoh 	bus_dmamap_t		desc_dma_map;
    203   1.1   msaitoh 	bus_dma_segment_t	desc_dma_seg;
    204   1.1   msaitoh 	int			desc_rseg;
    205   1.1   msaitoh 
    206   1.1   msaitoh 	uint8_t			*dma_buffer;
    207   1.1   msaitoh 	bus_dma_tag_t		dma_buffer_dma_tag;
    208   1.1   msaitoh 	bus_dmamap_t		dma_buffer_dma_map;
    209   1.1   msaitoh 	bus_dma_segment_t	dma_buffer_dma_seg;
    210   1.1   msaitoh 	int			dma_buffer_rseg;
    211   1.1   msaitoh 
    212   1.1   msaitoh 	uint8_t			using_msi;
    213   1.1   msaitoh };
    214   1.1   msaitoh 
    215   1.1   msaitoh static int	ismt_intr(void *);
    216   1.1   msaitoh static int	ismt_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
    217   1.1   msaitoh     size_t, void *, size_t, int);
    218   1.1   msaitoh static struct ismt_desc *ismt_alloc_desc(struct ismt_softc *);
    219   1.1   msaitoh static int	ismt_submit(struct ismt_softc *, struct ismt_desc *,
    220   1.1   msaitoh     i2c_addr_t, uint8_t, int);
    221   1.1   msaitoh static int	ismt_quick(struct ismt_softc *, i2c_addr_t, i2c_op_t, int);
    222   1.1   msaitoh static int	ismt_sendb(struct ismt_softc *, i2c_addr_t, i2c_op_t, char,
    223   1.1   msaitoh     int);
    224   1.1   msaitoh static int	ismt_recvb(struct ismt_softc *, i2c_addr_t, i2c_op_t, int);
    225   1.1   msaitoh static int	ismt_writeb(struct ismt_softc *, i2c_addr_t, i2c_op_t, uint8_t,
    226   1.1   msaitoh     char, int);
    227   1.1   msaitoh static int	ismt_writew(struct ismt_softc *, i2c_addr_t, i2c_op_t, uint8_t,
    228   1.1   msaitoh     uint16_t, int);
    229   1.1   msaitoh static int	ismt_readb(struct ismt_softc *, i2c_addr_t, i2c_op_t, char,
    230   1.1   msaitoh     int);
    231   1.1   msaitoh static int	ismt_readw(struct ismt_softc *, i2c_addr_t, i2c_op_t, char,
    232   1.1   msaitoh     int);
    233   1.1   msaitoh 
    234   1.1   msaitoh static int	ismt_match(device_t, cfdata_t, void *);
    235   1.1   msaitoh static void	ismt_attach(device_t, device_t, void *);
    236   1.1   msaitoh static int	ismt_detach(device_t, int);
    237   1.1   msaitoh static int	ismt_rescan(device_t, const char *, const int *);
    238   1.1   msaitoh static void	ismt_config_interrupts(device_t);
    239   1.1   msaitoh static void	ismt_chdet(device_t, device_t);
    240   1.1   msaitoh 
    241   1.1   msaitoh CFATTACH_DECL3_NEW(ismt, sizeof(struct ismt_softc),
    242   1.1   msaitoh     ismt_match, ismt_attach, ismt_detach, NULL, ismt_rescan, ismt_chdet,
    243   1.1   msaitoh     DVF_DETACH_SHUTDOWN);
    244   1.1   msaitoh 
    245   1.1   msaitoh static int
    246   1.1   msaitoh ismt_intr(void *arg)
    247   1.1   msaitoh {
    248   1.1   msaitoh 	struct ismt_softc *sc = arg;
    249   1.1   msaitoh 	uint32_t val;
    250   1.1   msaitoh 
    251   1.1   msaitoh 	val = bus_space_read_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MSTS);
    252   1.1   msaitoh 	if ((sc->using_msi == 0)
    253   1.1   msaitoh 	    && (val & (ISMT_MSTS_MIS | ISMT_MSTS_MEIS)) == 0)
    254   1.1   msaitoh 		return 0; /* Not for me */
    255   1.1   msaitoh 
    256   1.1   msaitoh 	ISMT_DEBUG(sc->pcidev, "%s MSTS = 0x%08x\n", __func__, val);
    257   1.1   msaitoh 
    258   1.1   msaitoh 	val |= (ISMT_MSTS_MIS | ISMT_MSTS_MEIS);
    259   1.1   msaitoh 	bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MSTS, val);
    260   1.1   msaitoh 
    261   1.1   msaitoh 	if (sc->using_msi)
    262   1.1   msaitoh 		wakeup(sc);
    263   1.1   msaitoh 
    264   1.1   msaitoh 	return 1;
    265   1.1   msaitoh }
    266   1.1   msaitoh 
    267   1.1   msaitoh static int
    268   1.1   msaitoh ismt_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
    269   1.1   msaitoh     const void *cmd, size_t cmdlen, void *buf, size_t buflen, int flags)
    270   1.1   msaitoh {
    271   1.1   msaitoh 	struct ismt_softc *sc = cookie;
    272   1.1   msaitoh 	uint8_t *p = buf;
    273   1.1   msaitoh 	int rv;
    274   1.1   msaitoh 
    275   1.1   msaitoh 	ISMT_DEBUG(sc->pcidev, "exec: op %d, addr 0x%02x, cmdlen %zu, "
    276   1.1   msaitoh  	    " buflen %zu, flags 0x%02x\n", op, addr, cmdlen, buflen, flags);
    277   1.1   msaitoh 
    278   1.1   msaitoh 	if ((cmdlen == 0) && (buflen == 0))
    279   1.1   msaitoh 		return ismt_quick(sc, addr, op, flags);
    280   1.1   msaitoh 
    281   1.1   msaitoh 	if (I2C_OP_READ_P(op) && (cmdlen == 0) && (buflen == 1)) {
    282   1.1   msaitoh 		rv = ismt_recvb(sc, addr, op, flags);
    283   1.1   msaitoh 		if (rv == -1)
    284   1.1   msaitoh 			return -1;
    285   1.1   msaitoh 		*p = (uint8_t)rv;
    286   1.1   msaitoh 		return 0;
    287   1.1   msaitoh 	}
    288   1.1   msaitoh 
    289   1.1   msaitoh 	if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 1)) {
    290   1.1   msaitoh 		rv = ismt_readb(sc, addr, op, *(const uint8_t*)cmd, flags);
    291   1.1   msaitoh 		if (rv == -1)
    292   1.1   msaitoh 			return -1;
    293   1.1   msaitoh 		*p = (uint8_t)rv;
    294   1.1   msaitoh 		return 0;
    295   1.1   msaitoh 	}
    296   1.1   msaitoh 
    297   1.1   msaitoh 	if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 2)) {
    298   1.1   msaitoh 		rv = ismt_readw(sc, addr, op, *(const uint8_t*)cmd, flags);
    299   1.1   msaitoh 		if (rv == -1)
    300   1.1   msaitoh 			return -1;
    301   1.1   msaitoh 		*(uint16_t *)p = (uint16_t)rv;
    302   1.1   msaitoh 		return 0;
    303   1.1   msaitoh 	}
    304   1.1   msaitoh 
    305   1.1   msaitoh 	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 1))
    306   1.1   msaitoh 		return ismt_sendb(sc, addr, op, *(uint8_t*)buf, flags);
    307   1.1   msaitoh 
    308   1.1   msaitoh 	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 1))
    309   1.1   msaitoh 		return ismt_writeb(sc, addr, op, *(const uint8_t*)cmd,
    310   1.1   msaitoh 		    *(uint8_t*)buf, flags);
    311   1.1   msaitoh 
    312   1.1   msaitoh 	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 2))
    313   1.1   msaitoh 		return ismt_writew(sc, addr, op,
    314   1.1   msaitoh 		    *(const uint8_t*)cmd, *((uint16_t *)buf), flags);
    315   1.1   msaitoh 
    316   1.1   msaitoh 	return -1;
    317   1.1   msaitoh }
    318   1.1   msaitoh 
    319   1.1   msaitoh static struct ismt_desc *
    320   1.1   msaitoh ismt_alloc_desc(struct ismt_softc *sc)
    321   1.1   msaitoh {
    322   1.1   msaitoh 	struct ismt_desc *desc;
    323   1.1   msaitoh 
    324   1.1   msaitoh 	desc = &sc->desc[sc->head++];
    325   1.1   msaitoh 	if (sc->head == ISMT_DESC_ENTRIES)
    326   1.1   msaitoh 		sc->head = 0;
    327   1.1   msaitoh 
    328   1.1   msaitoh 	memset(desc, 0, sizeof(*desc));
    329   1.1   msaitoh 
    330   1.1   msaitoh 	return (desc);
    331   1.1   msaitoh }
    332   1.1   msaitoh 
    333   1.1   msaitoh static int
    334   1.1   msaitoh ismt_submit(struct ismt_softc *sc, struct ismt_desc *desc, i2c_addr_t slave,
    335   1.1   msaitoh     uint8_t is_read, int flags)
    336   1.1   msaitoh {
    337   1.1   msaitoh 	uint32_t err, fmhp, val;
    338   1.1   msaitoh 	int timeout, i;
    339   1.1   msaitoh 
    340   1.1   msaitoh 	if (sc->using_msi == 0)
    341   1.1   msaitoh 		flags |= I2C_F_POLL;
    342   1.1   msaitoh 	desc->control |= ISMT_DESC_FAIR;
    343   1.1   msaitoh 	if ((flags & I2C_F_POLL) == 0)
    344   1.1   msaitoh 		desc->control |= ISMT_DESC_INT;
    345   1.1   msaitoh 
    346   1.1   msaitoh 	desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(slave, is_read);
    347   1.2  christos 	desc->dptr_low = ISMT_LOW(sc->dma_buffer_dma_map->dm_segs[0].ds_addr);
    348   1.2  christos 	desc->dptr_high = ISMT_HIGH(sc->dma_buffer_dma_map->dm_segs[0].ds_addr);
    349   1.1   msaitoh 
    350   1.1   msaitoh 	bus_dmamap_sync(sc->desc_dma_tag, sc->desc_dma_map,
    351   1.1   msaitoh 	    desc - &sc->desc[0], sizeof(struct ismt_desc),
    352   1.1   msaitoh 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    353   1.1   msaitoh 
    354   1.1   msaitoh 	fmhp = sc->head << 16;
    355   1.1   msaitoh 	val = bus_space_read_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MCTRL);
    356   1.1   msaitoh 	val &= ~ISMT_MCTRL_FMHP;
    357   1.1   msaitoh 	val |= fmhp;
    358   1.1   msaitoh 	bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MCTRL, val);
    359   1.1   msaitoh 
    360   1.1   msaitoh 	/* set the start bit */
    361   1.1   msaitoh 	val = bus_space_read_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MCTRL);
    362   1.1   msaitoh 	val |= ISMT_MCTRL_SS;
    363   1.1   msaitoh 	bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MCTRL, val);
    364   1.1   msaitoh 
    365   1.1   msaitoh 	i = 0;
    366   1.1   msaitoh 	if ((flags & I2C_F_POLL) == 0) {
    367   1.1   msaitoh 		timeout = ISMT_INTR_TIMEOUT;
    368   1.1   msaitoh 		if (timeout == 0)
    369   1.1   msaitoh 			timeout = 1;
    370   1.1   msaitoh 		err = tsleep(sc, PWAIT, "ismt_wait", timeout);
    371   1.1   msaitoh 		if (err != 0) {
    372   1.1   msaitoh 			ISMT_DEBUG(sc->pcidev, "%s timeout\n", __func__);
    373   1.1   msaitoh 			return -1;
    374   1.1   msaitoh 		}
    375   1.1   msaitoh 	} else {
    376   1.1   msaitoh 		/* Polling */
    377   1.1   msaitoh 		for (i = 0; i < ISMT_POLL_COUNT; i++) {
    378   1.1   msaitoh 			val = bus_space_read_4(sc->mmio_tag, sc->mmio_handle,
    379   1.1   msaitoh 			    ISMT_MSTR_MSTS);
    380   1.1   msaitoh 			if ((val & (ISMT_MSTS_MIS | ISMT_MSTS_MEIS)) != 0) {
    381   1.1   msaitoh 				ismt_intr(sc);
    382   1.1   msaitoh 				err = 0;
    383   1.1   msaitoh 				break;
    384   1.1   msaitoh 			}
    385   1.1   msaitoh 			delay(ISMT_POLL_DELAY);
    386   1.1   msaitoh 		}
    387   1.1   msaitoh 		if (i >= ISMT_POLL_COUNT) {
    388   1.1   msaitoh 			ISMT_DEBUG(sc->pcidev, "%s polling timeout. "
    389   1.1   msaitoh 			    "MSTS = %08x\n", __func__, val);
    390   1.1   msaitoh 			return -1;
    391   1.1   msaitoh 		}
    392   1.1   msaitoh 	}
    393   1.1   msaitoh 
    394   1.1   msaitoh 	bus_dmamap_sync(sc->desc_dma_tag, sc->desc_dma_map,
    395   1.1   msaitoh 	    desc - &sc->desc[0], sizeof(struct ismt_desc),
    396   1.1   msaitoh 		BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    397   1.1   msaitoh 	ISMT_DEBUG(sc->pcidev, "%s status=0x%02x\n", __func__, desc->status);
    398   1.1   msaitoh 
    399   1.1   msaitoh 	if (desc->status & ISMT_DESC_SCS)
    400   1.1   msaitoh 		return 0;
    401   1.1   msaitoh 
    402   1.1   msaitoh 	if (desc->status & ISMT_DESC_NAK)
    403   1.1   msaitoh 		return -1;
    404   1.1   msaitoh 
    405   1.1   msaitoh 	if (desc->status & ISMT_DESC_CRC)
    406   1.1   msaitoh 		return -1;
    407   1.1   msaitoh 
    408   1.1   msaitoh 	if (desc->status & ISMT_DESC_COL)
    409   1.1   msaitoh 		return -1;
    410   1.1   msaitoh 
    411   1.1   msaitoh 	if (desc->status & ISMT_DESC_LPR)
    412   1.1   msaitoh 		return -1;
    413   1.1   msaitoh 
    414   1.1   msaitoh 	if (desc->status & (ISMT_DESC_DLTO | ISMT_DESC_CLTO))
    415   1.1   msaitoh 		return -1;
    416   1.1   msaitoh 
    417   1.1   msaitoh 	return -1;
    418   1.1   msaitoh }
    419   1.1   msaitoh 
    420   1.1   msaitoh static int
    421   1.1   msaitoh ismt_quick(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, int flags)
    422   1.1   msaitoh {
    423   1.1   msaitoh 	struct ismt_desc	*desc;
    424   1.1   msaitoh 	int			is_read;
    425   1.1   msaitoh 
    426   1.1   msaitoh 	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);
    427   1.1   msaitoh 
    428   1.1   msaitoh 	desc = ismt_alloc_desc(sc);
    429   1.1   msaitoh 	is_read = I2C_OP_READ_P(op);
    430   1.1   msaitoh 	return (ismt_submit(sc, desc, slave, is_read, flags));
    431   1.1   msaitoh }
    432   1.1   msaitoh 
    433   1.1   msaitoh static int
    434   1.1   msaitoh ismt_sendb(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, char byte,
    435   1.1   msaitoh     int flags)
    436   1.1   msaitoh {
    437   1.1   msaitoh 	struct ismt_desc	*desc;
    438   1.1   msaitoh 
    439   1.1   msaitoh 	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);
    440   1.1   msaitoh 
    441   1.1   msaitoh 	desc = ismt_alloc_desc(sc);
    442   1.1   msaitoh 	desc->control = ISMT_DESC_CWRL;
    443   1.1   msaitoh 	desc->wr_len_cmd = byte;
    444   1.1   msaitoh 
    445   1.1   msaitoh 	return (ismt_submit(sc, desc, slave, 0, flags));
    446   1.1   msaitoh }
    447   1.1   msaitoh 
    448   1.1   msaitoh static int
    449   1.1   msaitoh ismt_recvb(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, int flags)
    450   1.1   msaitoh {
    451   1.1   msaitoh 	struct ismt_desc	*desc;
    452   1.1   msaitoh 	int			err;
    453   1.1   msaitoh 
    454   1.1   msaitoh 	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);
    455   1.1   msaitoh 
    456   1.1   msaitoh 	desc = ismt_alloc_desc(sc);
    457   1.1   msaitoh 	desc->rd_len = 1;
    458   1.1   msaitoh 
    459   1.1   msaitoh 	err = ismt_submit(sc, desc, slave, 1, flags);
    460   1.1   msaitoh 
    461   1.1   msaitoh 	if (err != 0)
    462   1.1   msaitoh 		return (err);
    463   1.1   msaitoh 
    464   1.1   msaitoh 	return sc->dma_buffer[0];
    465   1.1   msaitoh }
    466   1.1   msaitoh 
    467   1.1   msaitoh static int
    468   1.1   msaitoh ismt_writeb(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, uint8_t cmd,
    469   1.1   msaitoh     char byte, int flags)
    470   1.1   msaitoh {
    471   1.1   msaitoh 	struct ismt_desc	*desc;
    472   1.1   msaitoh 
    473   1.1   msaitoh 	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);
    474   1.1   msaitoh 
    475   1.1   msaitoh 	desc = ismt_alloc_desc(sc);
    476   1.1   msaitoh 	desc->wr_len_cmd = 2;
    477   1.1   msaitoh 	sc->dma_buffer[0] = cmd;
    478   1.1   msaitoh 	sc->dma_buffer[1] = byte;
    479   1.1   msaitoh 
    480   1.1   msaitoh 	return (ismt_submit(sc, desc, slave, 0, flags));
    481   1.1   msaitoh }
    482   1.1   msaitoh 
    483   1.1   msaitoh static int
    484   1.1   msaitoh ismt_writew(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, uint8_t cmd,
    485   1.1   msaitoh     uint16_t word, int flags)
    486   1.1   msaitoh {
    487   1.1   msaitoh 	struct ismt_desc	*desc;
    488   1.1   msaitoh 
    489   1.1   msaitoh 	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);
    490   1.1   msaitoh 
    491   1.1   msaitoh 	desc = ismt_alloc_desc(sc);
    492   1.1   msaitoh 	desc->wr_len_cmd = 3;
    493   1.1   msaitoh 	sc->dma_buffer[0] = cmd;
    494   1.1   msaitoh 	sc->dma_buffer[1] = word & 0xFF;
    495   1.1   msaitoh 	sc->dma_buffer[2] = word >> 8;
    496   1.1   msaitoh 
    497   1.1   msaitoh 	return (ismt_submit(sc, desc, slave, 0, flags));
    498   1.1   msaitoh }
    499   1.1   msaitoh 
    500   1.1   msaitoh static int
    501   1.1   msaitoh ismt_readb(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, char cmd,
    502   1.1   msaitoh     int flags)
    503   1.1   msaitoh {
    504   1.1   msaitoh 	struct ismt_desc	*desc;
    505   1.1   msaitoh 	int			err;
    506   1.1   msaitoh 
    507   1.1   msaitoh 	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);
    508   1.1   msaitoh 
    509   1.1   msaitoh 	desc = ismt_alloc_desc(sc);
    510   1.1   msaitoh 	desc->control = ISMT_DESC_CWRL;
    511   1.1   msaitoh 	desc->wr_len_cmd = cmd;
    512   1.1   msaitoh 	desc->rd_len = 1;
    513   1.1   msaitoh 
    514   1.1   msaitoh 	err = ismt_submit(sc, desc, slave, 1, flags);
    515   1.1   msaitoh 
    516   1.1   msaitoh 	if (err != 0)
    517   1.1   msaitoh 		return (err);
    518   1.1   msaitoh 
    519   1.1   msaitoh 	return sc->dma_buffer[0];
    520   1.1   msaitoh }
    521   1.1   msaitoh 
    522   1.1   msaitoh static int
    523   1.1   msaitoh ismt_readw(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, char cmd,
    524   1.1   msaitoh     int flags)
    525   1.1   msaitoh {
    526   1.1   msaitoh 	struct ismt_desc	*desc;
    527   1.1   msaitoh 	uint16_t		word;
    528   1.1   msaitoh 	int			err;
    529   1.1   msaitoh 
    530   1.1   msaitoh 	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);
    531   1.1   msaitoh 
    532   1.1   msaitoh 	desc = ismt_alloc_desc(sc);
    533   1.1   msaitoh 	desc->control = ISMT_DESC_CWRL;
    534   1.1   msaitoh 	desc->wr_len_cmd = cmd;
    535   1.1   msaitoh 	desc->rd_len = 2;
    536   1.1   msaitoh 
    537   1.1   msaitoh 	err = ismt_submit(sc, desc, slave, 1, flags);
    538   1.1   msaitoh 
    539   1.1   msaitoh 	if (err != 0)
    540   1.1   msaitoh 		return (err);
    541   1.1   msaitoh 
    542   1.1   msaitoh 	word = sc->dma_buffer[0] | (sc->dma_buffer[1] << 8);
    543   1.1   msaitoh 
    544   1.1   msaitoh 	return word;
    545   1.1   msaitoh }
    546   1.1   msaitoh 
    547   1.1   msaitoh #if 0
    548   1.1   msaitoh static int
    549   1.1   msaitoh ismt_pcall(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, char cmd,
    550   1.1   msaitoh     uint16_t sdata, uint16_t *rdata, int flags)
    551   1.1   msaitoh {
    552   1.1   msaitoh 	struct ismt_desc	*desc;
    553   1.1   msaitoh 	int			err;
    554   1.1   msaitoh 
    555   1.1   msaitoh 	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);
    556   1.1   msaitoh 
    557   1.1   msaitoh 	desc = ismt_alloc_desc(sc);
    558   1.1   msaitoh 	desc->wr_len_cmd = 3;
    559   1.1   msaitoh 	desc->rd_len = 2;
    560   1.1   msaitoh 	sc->dma_buffer[0] = cmd;
    561   1.1   msaitoh 	sc->dma_buffer[1] = sdata & 0xff;
    562   1.1   msaitoh 	sc->dma_buffer[2] = sdata >> 8;
    563   1.1   msaitoh 
    564   1.1   msaitoh 	err = ismt_submit(sc, desc, slave, 0, flags);
    565   1.1   msaitoh 
    566   1.1   msaitoh 	if (err != 0)
    567   1.1   msaitoh 		return (err);
    568   1.1   msaitoh 
    569   1.1   msaitoh 	*rdata = sc->dma_buffer[0] | (sc->dma_buffer[1] << 8);
    570   1.1   msaitoh 
    571   1.1   msaitoh 	return (err);
    572   1.1   msaitoh }
    573   1.1   msaitoh 
    574   1.1   msaitoh static int
    575   1.1   msaitoh ismt_bwrite(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, char cmd,
    576   1.1   msaitoh     u_char count, char *buf, int flags)
    577   1.1   msaitoh {
    578   1.1   msaitoh 	struct ismt_desc	*desc;
    579   1.1   msaitoh 
    580   1.1   msaitoh 	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);
    581   1.1   msaitoh 
    582   1.1   msaitoh 	if (count == 0 || count > ISMT_MAX_BLOCK_SIZE)
    583   1.1   msaitoh 		return -1;
    584   1.1   msaitoh 
    585   1.1   msaitoh 	desc = ismt_alloc_desc(sc);
    586   1.1   msaitoh 	desc->control = ISMT_DESC_I2C;
    587   1.1   msaitoh 	desc->wr_len_cmd = count + 1;
    588   1.1   msaitoh 	sc->dma_buffer[0] = cmd;
    589   1.1   msaitoh 	memcpy(&sc->dma_buffer[1], buf, count);
    590   1.1   msaitoh 
    591   1.1   msaitoh 	return (ismt_submit(sc, desc, slave, 0, flags));
    592   1.1   msaitoh }
    593   1.1   msaitoh 
    594   1.1   msaitoh static int
    595   1.1   msaitoh ismt_bread(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, char cmd,
    596   1.1   msaitoh     u_char *count, char *buf, int flags)
    597   1.1   msaitoh {
    598   1.1   msaitoh 	struct ismt_desc	*desc;
    599   1.1   msaitoh 	int			err;
    600   1.1   msaitoh 
    601   1.1   msaitoh 	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);
    602   1.1   msaitoh 
    603   1.1   msaitoh 	if (*count == 0 || *count > ISMT_MAX_BLOCK_SIZE)
    604   1.1   msaitoh 		return -1;
    605   1.1   msaitoh 
    606   1.1   msaitoh 	desc = ismt_alloc_desc(sc);
    607   1.1   msaitoh 	desc->control = ISMT_DESC_I2C | ISMT_DESC_CWRL;
    608   1.1   msaitoh 	desc->wr_len_cmd = cmd;
    609   1.1   msaitoh 	desc->rd_len = *count;
    610   1.1   msaitoh 
    611   1.1   msaitoh 	err = ismt_submit(sc, desc, slave, 0, flags);
    612   1.1   msaitoh 
    613   1.1   msaitoh 	if (err != 0)
    614   1.1   msaitoh 		return (err);
    615   1.1   msaitoh 
    616   1.1   msaitoh 	memcpy(buf, sc->dma_buffer, desc->rxbytes);
    617   1.1   msaitoh 	*count = desc->rxbytes;
    618   1.1   msaitoh 
    619   1.1   msaitoh 	return (err);
    620   1.1   msaitoh }
    621   1.1   msaitoh #endif
    622   1.1   msaitoh 
    623   1.1   msaitoh static int
    624   1.1   msaitoh ismt_detach(device_t self, int flags)
    625   1.1   msaitoh {
    626  1.10  riastrad 	struct ismt_softc *sc = device_private(self);
    627  1.10  riastrad 	int error;
    628   1.1   msaitoh 
    629   1.1   msaitoh 	ISMT_DEBUG(self, "%s\n", __func__);
    630  1.10  riastrad 	error = config_detach_children(self, flags);
    631  1.10  riastrad 	if (error)
    632  1.10  riastrad 		return error;
    633  1.10  riastrad 
    634   1.1   msaitoh 	if (sc->sc_ih != NULL) {
    635   1.1   msaitoh 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    636   1.1   msaitoh 		sc->sc_ih = NULL;
    637   1.1   msaitoh 	}
    638   1.1   msaitoh 	if (sc->sc_pihp != NULL) {
    639   1.1   msaitoh 		pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
    640   1.1   msaitoh 		sc->sc_pihp = NULL;
    641   1.1   msaitoh 	}
    642   1.1   msaitoh 
    643   1.1   msaitoh 	bus_dmamap_unload(sc->desc_dma_tag, sc->desc_dma_map);
    644   1.1   msaitoh 	bus_dmamap_unload(sc->dma_buffer_dma_tag, sc->dma_buffer_dma_map);
    645   1.1   msaitoh 
    646   1.1   msaitoh 	bus_dmamem_free(sc->desc_dma_tag, &sc->desc_dma_seg, sc->desc_rseg);
    647   1.1   msaitoh 	bus_dmamem_free(sc->dma_buffer_dma_tag, &sc->dma_buffer_dma_seg,
    648   1.1   msaitoh 	    sc->dma_buffer_rseg);
    649   1.1   msaitoh 
    650   1.1   msaitoh 	if (sc->mmio_size)
    651   1.1   msaitoh 		bus_space_unmap(sc->mmio_tag, sc->mmio_handle, sc->mmio_size);
    652   1.1   msaitoh 
    653   1.7   thorpej 	iic_tag_fini(&sc->sc_i2c_tag);
    654  1.10  riastrad 	return 0;
    655   1.1   msaitoh }
    656   1.1   msaitoh 
    657   1.1   msaitoh static void
    658   1.1   msaitoh ismt_attach(device_t parent, device_t self, void *aux)
    659   1.1   msaitoh {
    660   1.1   msaitoh 	struct ismt_softc *sc = device_private(self);
    661   1.1   msaitoh 	struct pci_attach_args *pa = aux;
    662   1.1   msaitoh 	const char *intrstr = NULL;
    663   1.1   msaitoh 	char intrbuf[PCI_INTRSTR_LEN];
    664   1.1   msaitoh 	pcireg_t reg;
    665   1.1   msaitoh 	int val;
    666   1.1   msaitoh 
    667   1.1   msaitoh 	sc->pcidev = self;
    668   1.1   msaitoh 	sc->sc_pc = pa->pa_pc;
    669   1.1   msaitoh 	sc->sc_pcitag = pa->pa_tag;
    670   1.1   msaitoh 
    671   1.1   msaitoh 	/* Enable busmastering */
    672   1.1   msaitoh 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
    673   1.1   msaitoh 	reg |= PCI_COMMAND_MASTER_ENABLE;
    674   1.1   msaitoh 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, reg);
    675   1.1   msaitoh 
    676   1.1   msaitoh 	pci_aprint_devinfo(pa, NULL);
    677   1.1   msaitoh 
    678   1.1   msaitoh 	/* Map mem space */
    679   1.1   msaitoh 	if (pci_mapreg_map(pa, PCI_BAR0, PCI_MAPREG_TYPE_MEM, 0,
    680   1.1   msaitoh 	    &sc->mmio_tag, &sc->mmio_handle, NULL, &sc->mmio_size)) {
    681   1.1   msaitoh 		aprint_error_dev(self, "can't map mem space\n");
    682   1.1   msaitoh 		goto fail;
    683   1.1   msaitoh 	}
    684   1.1   msaitoh 
    685   1.1   msaitoh 	if (pci_dma64_available(pa)) {
    686   1.1   msaitoh 		sc->desc_dma_tag = pa->pa_dmat64;
    687   1.1   msaitoh 		sc->dma_buffer_dma_tag = pa->pa_dmat64;
    688   1.1   msaitoh 	} else {
    689   1.1   msaitoh 		sc->desc_dma_tag = pa->pa_dmat;
    690   1.1   msaitoh 		sc->dma_buffer_dma_tag = pa->pa_dmat;
    691   1.1   msaitoh 	}
    692   1.1   msaitoh 	bus_dmamem_alloc(sc->desc_dma_tag, DESC_SIZE, PAGE_SIZE, 0,
    693   1.1   msaitoh 	    &sc->desc_dma_seg, ISMT_DESC_ENTRIES, &sc->desc_rseg,
    694   1.1   msaitoh 	    BUS_DMA_WAITOK);
    695   1.1   msaitoh 	bus_dmamem_alloc(sc->dma_buffer_dma_tag, DMA_BUFFER_SIZE, PAGE_SIZE, 0,
    696   1.1   msaitoh 	    &sc->dma_buffer_dma_seg, 1, &sc->dma_buffer_rseg, BUS_DMA_WAITOK);
    697   1.1   msaitoh 
    698   1.1   msaitoh 	bus_dmamem_map(sc->desc_dma_tag, &sc->desc_dma_seg,
    699   1.1   msaitoh 	    sc->desc_rseg, DESC_SIZE, (void **)&sc->desc, BUS_DMA_COHERENT);
    700   1.1   msaitoh 	bus_dmamem_map(sc->dma_buffer_dma_tag, &sc->dma_buffer_dma_seg,
    701   1.1   msaitoh 	    sc->dma_buffer_rseg, DMA_BUFFER_SIZE, (void **)&sc->dma_buffer,
    702   1.1   msaitoh 	    BUS_DMA_COHERENT);
    703   1.1   msaitoh 
    704   1.1   msaitoh 	bus_dmamap_create(sc->desc_dma_tag, DESC_SIZE, 1,
    705   1.1   msaitoh 	    DESC_SIZE, 0, 0, &sc->desc_dma_map);
    706   1.1   msaitoh 	bus_dmamap_create(sc->dma_buffer_dma_tag, DMA_BUFFER_SIZE, 1,
    707   1.1   msaitoh 	    DMA_BUFFER_SIZE, 0, 0, &sc->dma_buffer_dma_map);
    708   1.1   msaitoh 
    709   1.1   msaitoh 	bus_dmamap_load(sc->desc_dma_tag,
    710   1.1   msaitoh 	    sc->desc_dma_map, sc->desc, DESC_SIZE, NULL, 0);
    711   1.1   msaitoh 	bus_dmamap_load(sc->dma_buffer_dma_tag,
    712   1.1   msaitoh 	    sc->dma_buffer_dma_map, sc->dma_buffer, DMA_BUFFER_SIZE,
    713   1.1   msaitoh 	    NULL, 0);
    714   1.1   msaitoh 
    715   1.1   msaitoh 	bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MDBA,
    716   1.2  christos 	    ISMT_LOW(sc->desc_dma_map->dm_segs[0].ds_addr));
    717   1.1   msaitoh 	bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MDBA + 4,
    718   1.2  christos 	    ISMT_HIGH(sc->desc_dma_map->dm_segs[0].ds_addr));
    719   1.1   msaitoh 
    720   1.1   msaitoh 	/* initialize the Master Control Register (MCTRL) */
    721   1.1   msaitoh 	bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MCTRL,
    722   1.1   msaitoh 	    ISMT_MCTRL_MEIE);
    723   1.1   msaitoh 
    724   1.1   msaitoh 	/* initialize the Master Status Register (MSTS) */
    725   1.1   msaitoh 	bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MSTS, 0);
    726   1.1   msaitoh 
    727   1.1   msaitoh 	/* initialize the Master Descriptor Size (MDS) */
    728   1.1   msaitoh 	val = bus_space_read_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MDS);
    729   1.1   msaitoh 	val &= ~ISMT_MDS_MASK;
    730   1.1   msaitoh 	val |= (ISMT_DESC_ENTRIES - 1);
    731   1.1   msaitoh 	bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MDS, val);
    732   1.1   msaitoh 
    733   1.1   msaitoh 	if (pci_intr_alloc(pa, &sc->sc_pihp, NULL, 0)) {
    734   1.1   msaitoh 		aprint_error_dev(self, "couldn't map interrupt\n");
    735   1.1   msaitoh 		return;
    736   1.1   msaitoh 	}
    737   1.1   msaitoh 	intrstr = pci_intr_string(pa->pa_pc, sc->sc_pihp[0], intrbuf,
    738   1.1   msaitoh 	    sizeof(intrbuf));
    739   1.5   msaitoh 	sc->sc_ih = pci_intr_establish_xname(pa->pa_pc, sc->sc_pihp[0],
    740   1.5   msaitoh 	    IPL_BIO, ismt_intr, sc, device_xname(self));
    741   1.1   msaitoh 	if (sc->sc_ih == NULL) {
    742   1.1   msaitoh 		aprint_error_dev(sc->pcidev, "unable to establish %s\n",
    743   1.4  knakahar 		    (pci_intr_type(pa->pa_pc, sc->sc_pihp[0])
    744   1.1   msaitoh 			== PCI_INTR_TYPE_MSI) ? "MSI" : "INTx");
    745   1.1   msaitoh 		/* Polling */
    746   1.1   msaitoh 	}
    747   1.1   msaitoh 
    748   1.4  knakahar 	if (pci_intr_type(pa->pa_pc, sc->sc_pihp[0]) == PCI_INTR_TYPE_MSI)
    749   1.1   msaitoh 		sc->using_msi = 1;
    750   1.1   msaitoh 
    751   1.1   msaitoh 	aprint_normal_dev(sc->pcidev, "interrupting at %s\n", intrstr);
    752   1.1   msaitoh 
    753   1.1   msaitoh 	sc->smbdev = NULL;
    754   1.1   msaitoh 	if (!pmf_device_register(self, NULL, NULL))
    755   1.1   msaitoh 		aprint_error_dev(self, "couldn't establish power handler\n");
    756   1.1   msaitoh 
    757   1.1   msaitoh 	config_interrupts(self, ismt_config_interrupts);
    758   1.1   msaitoh 	return;
    759   1.1   msaitoh 
    760   1.1   msaitoh fail:
    761   1.1   msaitoh 	ismt_detach(sc->pcidev, 0);
    762   1.1   msaitoh 
    763   1.1   msaitoh 	return;
    764   1.1   msaitoh }
    765   1.1   msaitoh 
    766   1.1   msaitoh static int
    767   1.1   msaitoh ismt_match(device_t parent, cfdata_t match, void *aux)
    768   1.1   msaitoh {
    769   1.1   msaitoh 	struct pci_attach_args *pa = aux;
    770   1.1   msaitoh 
    771   1.1   msaitoh 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
    772   1.1   msaitoh 		return 0;
    773   1.1   msaitoh 
    774   1.1   msaitoh 	switch (PCI_PRODUCT(pa->pa_id)) {
    775   1.1   msaitoh 	case PCI_PRODUCT_INTEL_S1200_SMBUS_0:
    776   1.1   msaitoh 	case PCI_PRODUCT_INTEL_S1200_SMBUS_1:
    777   1.1   msaitoh 	case PCI_PRODUCT_INTEL_C2000_SMBUS:
    778   1.6   msaitoh 	case PCI_PRODUCT_INTEL_C3K_SMBUS:
    779  1.11   msaitoh 	case PCI_PRODUCT_INTEL_SNR_SMB_HOST:
    780   1.1   msaitoh 		break;
    781   1.1   msaitoh 	default:
    782   1.1   msaitoh 		return 0;
    783   1.1   msaitoh 	}
    784   1.1   msaitoh 
    785   1.1   msaitoh 	return 1;
    786   1.1   msaitoh }
    787   1.1   msaitoh 
    788   1.1   msaitoh static int
    789   1.8   thorpej ismt_rescan(device_t self, const char *ifattr, const int *locators)
    790   1.1   msaitoh {
    791   1.1   msaitoh 	struct ismt_softc *sc = device_private(self);
    792   1.1   msaitoh 
    793   1.8   thorpej 	if (sc->smbdev != NULL)
    794   1.1   msaitoh 		return 0;
    795   1.1   msaitoh 
    796   1.1   msaitoh 	/* Attach I2C bus */
    797   1.7   thorpej 	iic_tag_init(&sc->sc_i2c_tag);
    798   1.1   msaitoh 	sc->sc_i2c_tag.ic_cookie = sc;
    799   1.1   msaitoh 	sc->sc_i2c_tag.ic_exec = ismt_i2c_exec;
    800   1.1   msaitoh 
    801  1.12   thorpej 	sc->smbdev = iicbus_attach(self, &sc->sc_i2c_tag);
    802   1.1   msaitoh 
    803   1.1   msaitoh 	return 0;
    804   1.1   msaitoh }
    805   1.1   msaitoh 
    806   1.1   msaitoh static void
    807   1.1   msaitoh ismt_config_interrupts(device_t self)
    808   1.1   msaitoh {
    809   1.1   msaitoh 
    810   1.8   thorpej 	ismt_rescan(self, NULL, NULL);
    811   1.1   msaitoh }
    812   1.1   msaitoh 
    813   1.1   msaitoh static void
    814   1.1   msaitoh ismt_chdet(device_t self, device_t child)
    815   1.1   msaitoh {
    816   1.1   msaitoh 	struct ismt_softc *sc = device_private(self);
    817   1.1   msaitoh 
    818   1.1   msaitoh 	if (sc->smbdev == child)
    819   1.1   msaitoh 		sc->smbdev = NULL;
    820   1.1   msaitoh 
    821   1.1   msaitoh }
    822   1.1   msaitoh 
    823   1.1   msaitoh MODULE(MODULE_CLASS_DRIVER, ismt, "pci");
    824   1.1   msaitoh 
    825   1.1   msaitoh #ifdef _MODULE
    826   1.1   msaitoh #include "ioconf.c"
    827   1.1   msaitoh #endif
    828   1.1   msaitoh 
    829   1.1   msaitoh static int
    830   1.1   msaitoh ismt_modcmd(modcmd_t cmd, void *opaque)
    831   1.1   msaitoh {
    832   1.1   msaitoh 	int error = 0;
    833   1.1   msaitoh 
    834   1.1   msaitoh 	switch (cmd) {
    835   1.1   msaitoh 	case MODULE_CMD_INIT:
    836   1.1   msaitoh #ifdef _MODULE
    837   1.1   msaitoh 		error = config_init_component(cfdriver_ioconf_ismt,
    838   1.1   msaitoh 		    cfattach_ioconf_ismt, cfdata_ioconf_ismt);
    839   1.1   msaitoh #endif
    840   1.1   msaitoh 		return error;
    841   1.1   msaitoh 	case MODULE_CMD_FINI:
    842   1.1   msaitoh #ifdef _MODULE
    843   1.1   msaitoh 		error = config_fini_component(cfdriver_ioconf_ismt,
    844   1.1   msaitoh 		    cfattach_ioconf_ismt, cfdata_ioconf_ismt);
    845   1.1   msaitoh #endif
    846   1.1   msaitoh 		return error;
    847   1.1   msaitoh 	default:
    848   1.1   msaitoh 		return ENOTTY;
    849   1.1   msaitoh 	}
    850   1.1   msaitoh }
    851