ismt.c revision 1.1 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.1 msaitoh __KERNEL_RCSID(0, "$NetBSD: ismt.c,v 1.1 2016/01/05 11:24:43 msaitoh 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/mutex.h>
72 1.1 msaitoh #include <sys/proc.h>
73 1.1 msaitoh
74 1.1 msaitoh #include <sys/bus.h>
75 1.1 msaitoh
76 1.1 msaitoh #include <dev/pci/pcidevs.h>
77 1.1 msaitoh #include <dev/pci/pcireg.h>
78 1.1 msaitoh #include <dev/pci/pcivar.h>
79 1.1 msaitoh
80 1.1 msaitoh #include <dev/i2c/i2cvar.h>
81 1.1 msaitoh
82 1.1 msaitoh #define ISMT_DESC_ENTRIES 32
83 1.1 msaitoh
84 1.1 msaitoh /* Hardware Descriptor Constants - Control Field */
85 1.1 msaitoh #define ISMT_DESC_CWRL 0x01 /* Command/Write Length */
86 1.1 msaitoh #define ISMT_DESC_BLK 0X04 /* Perform Block Transaction */
87 1.1 msaitoh #define ISMT_DESC_FAIR 0x08 /* Set fairness flag upon successful arbit. */
88 1.1 msaitoh #define ISMT_DESC_PEC 0x10 /* Packet Error Code */
89 1.1 msaitoh #define ISMT_DESC_I2C 0x20 /* I2C Enable */
90 1.1 msaitoh #define ISMT_DESC_INT 0x40 /* Interrupt */
91 1.1 msaitoh #define ISMT_DESC_SOE 0x80 /* Stop On Error */
92 1.1 msaitoh
93 1.1 msaitoh /* Hardware Descriptor Constants - Status Field */
94 1.1 msaitoh #define ISMT_DESC_SCS 0x01 /* Success */
95 1.1 msaitoh #define ISMT_DESC_DLTO 0x04 /* Data Low Time Out */
96 1.1 msaitoh #define ISMT_DESC_NAK 0x08 /* NAK Received */
97 1.1 msaitoh #define ISMT_DESC_CRC 0x10 /* CRC Error */
98 1.1 msaitoh #define ISMT_DESC_CLTO 0x20 /* Clock Low Time Out */
99 1.1 msaitoh #define ISMT_DESC_COL 0x40 /* Collisions */
100 1.1 msaitoh #define ISMT_DESC_LPR 0x80 /* Large Packet Received */
101 1.1 msaitoh
102 1.1 msaitoh /* Macros */
103 1.1 msaitoh #define ISMT_DESC_ADDR_RW(addr, is_read) ((addr << 1) | (is_read))
104 1.1 msaitoh
105 1.1 msaitoh /* iSMT General Register address offsets (SMBBAR + <addr>) */
106 1.1 msaitoh #define ISMT_GR_GCTRL 0x000 /* General Control */
107 1.1 msaitoh #define ISMT_GR_SMTICL 0x008 /* SMT Interrupt Cause Location */
108 1.1 msaitoh #define ISMT_GR_ERRINTMSK 0x010 /* Error Interrupt Mask */
109 1.1 msaitoh #define ISMT_GR_ERRAERMSK 0x014 /* Error AER Mask */
110 1.1 msaitoh #define ISMT_GR_ERRSTS 0x018 /* Error Status */
111 1.1 msaitoh #define ISMT_GR_ERRINFO 0x01c /* Error Information */
112 1.1 msaitoh
113 1.1 msaitoh /* iSMT Master Registers */
114 1.1 msaitoh #define ISMT_MSTR_MDBA 0x100 /* Master Descriptor Base Address */
115 1.1 msaitoh #define ISMT_MSTR_MCTRL 0x108 /* Master Control */
116 1.1 msaitoh #define ISMT_MSTR_MSTS 0x10c /* Master Status */
117 1.1 msaitoh #define ISMT_MSTR_MDS 0x110 /* Master Descriptor Size */
118 1.1 msaitoh #define ISMT_MSTR_RPOLICY 0x114 /* Retry Policy */
119 1.1 msaitoh
120 1.1 msaitoh /* iSMT Miscellaneous Registers */
121 1.1 msaitoh #define ISMT_SPGT 0x300 /* SMBus PHY Global Timing */
122 1.1 msaitoh
123 1.1 msaitoh /* General Control Register (GCTRL) bit definitions */
124 1.1 msaitoh #define ISMT_GCTRL_TRST 0x04 /* Target Reset */
125 1.1 msaitoh #define ISMT_GCTRL_KILL 0x08 /* Kill */
126 1.1 msaitoh #define ISMT_GCTRL_SRST 0x40 /* Soft Reset */
127 1.1 msaitoh
128 1.1 msaitoh /* Master Control Register (MCTRL) bit definitions */
129 1.1 msaitoh #define ISMT_MCTRL_SS 0x01 /* Start/Stop */
130 1.1 msaitoh #define ISMT_MCTRL_MEIE 0x10 /* Master Error Interrupt Enable */
131 1.1 msaitoh #define ISMT_MCTRL_FMHP 0x00ff0000 /* Firmware Master Head Ptr (FMHP) */
132 1.1 msaitoh
133 1.1 msaitoh /* Master Status Register (MSTS) bit definitions */
134 1.1 msaitoh #define ISMT_MSTS_HMTP 0xff0000 /* HW Master Tail Pointer (HMTP) */
135 1.1 msaitoh #define ISMT_MSTS_MIS 0x20 /* Master Interrupt Status (MIS) */
136 1.1 msaitoh #define ISMT_MSTS_MEIS 0x10 /* Master Error Int Status (MEIS) */
137 1.1 msaitoh #define ISMT_MSTS_IP 0x01 /* In Progress */
138 1.1 msaitoh
139 1.1 msaitoh /* Master Descriptor Size (MDS) bit definitions */
140 1.1 msaitoh #define ISMT_MDS_MASK 0xff /* Master Descriptor Size mask (MDS) */
141 1.1 msaitoh
142 1.1 msaitoh /* SMBus PHY Global Timing Register (SPGT) bit definitions */
143 1.1 msaitoh #define ISMT_SPGT_SPD_MASK 0xc0000000 /* SMBus Speed mask */
144 1.1 msaitoh #define ISMT_SPGT_SPD_80K 0x00 /* 80 kHz */
145 1.1 msaitoh #define ISMT_SPGT_SPD_100K (0x1 << 30) /* 100 kHz */
146 1.1 msaitoh #define ISMT_SPGT_SPD_400K (0x2 << 30) /* 400 kHz */
147 1.1 msaitoh #define ISMT_SPGT_SPD_1M (0x3 << 30) /* 1 MHz */
148 1.1 msaitoh
149 1.1 msaitoh /* MSI Control Register (MSICTL) bit definitions */
150 1.1 msaitoh #define ISMT_MSICTL_MSIE 0x01 /* MSI Enable */
151 1.1 msaitoh
152 1.1 msaitoh #define ISMT_MAX_BLOCK_SIZE 32 /* per SMBus spec */
153 1.1 msaitoh
154 1.1 msaitoh #define ISMT_INTR_TIMEOUT (hz / 50) /* 0.02s */
155 1.1 msaitoh #define ISMT_POLL_DELAY 100 /* 100usec */
156 1.1 msaitoh #define ISMT_POLL_COUNT 200 /* 100usec * 200 = 0.02s */
157 1.1 msaitoh
158 1.1 msaitoh //#define ISMT_DEBUG aprint_debug_dev
159 1.1 msaitoh #ifndef ISMT_DEBUG
160 1.1 msaitoh #define ISMT_DEBUG(...)
161 1.1 msaitoh #endif
162 1.1 msaitoh
163 1.1 msaitoh /* iSMT Hardware Descriptor */
164 1.1 msaitoh struct ismt_desc {
165 1.1 msaitoh uint8_t tgtaddr_rw; /* target address & r/w bit */
166 1.1 msaitoh uint8_t wr_len_cmd; /* write length in bytes or a command */
167 1.1 msaitoh uint8_t rd_len; /* read length */
168 1.1 msaitoh uint8_t control; /* control bits */
169 1.1 msaitoh uint8_t status; /* status bits */
170 1.1 msaitoh uint8_t retry; /* collision retry and retry count */
171 1.1 msaitoh uint8_t rxbytes; /* received bytes */
172 1.1 msaitoh uint8_t txbytes; /* transmitted bytes */
173 1.1 msaitoh uint32_t dptr_low; /* lower 32 bit of the data pointer */
174 1.1 msaitoh uint32_t dptr_high; /* upper 32 bit of the data pointer */
175 1.1 msaitoh } __packed;
176 1.1 msaitoh
177 1.1 msaitoh #define DESC_SIZE (ISMT_DESC_ENTRIES * sizeof(struct ismt_desc))
178 1.1 msaitoh
179 1.1 msaitoh #define DMA_BUFFER_SIZE 64
180 1.1 msaitoh
181 1.1 msaitoh struct ismt_softc {
182 1.1 msaitoh device_t pcidev;
183 1.1 msaitoh device_t smbdev;
184 1.1 msaitoh
185 1.1 msaitoh struct i2c_controller sc_i2c_tag;
186 1.1 msaitoh kmutex_t sc_i2c_mutex;
187 1.1 msaitoh
188 1.1 msaitoh pci_chipset_tag_t sc_pc;
189 1.1 msaitoh pcitag_t sc_pcitag;
190 1.1 msaitoh pci_intr_handle_t *sc_pihp;
191 1.1 msaitoh void *sc_ih;
192 1.1 msaitoh
193 1.1 msaitoh bus_space_tag_t mmio_tag;
194 1.1 msaitoh bus_space_handle_t mmio_handle;
195 1.1 msaitoh bus_size_t mmio_size;
196 1.1 msaitoh
197 1.1 msaitoh uint8_t head;
198 1.1 msaitoh
199 1.1 msaitoh struct ismt_desc *desc;
200 1.1 msaitoh bus_dma_tag_t desc_dma_tag;
201 1.1 msaitoh bus_dmamap_t desc_dma_map;
202 1.1 msaitoh bus_dma_segment_t desc_dma_seg;
203 1.1 msaitoh int desc_rseg;
204 1.1 msaitoh
205 1.1 msaitoh uint8_t *dma_buffer;
206 1.1 msaitoh bus_dma_tag_t dma_buffer_dma_tag;
207 1.1 msaitoh bus_dmamap_t dma_buffer_dma_map;
208 1.1 msaitoh bus_dma_segment_t dma_buffer_dma_seg;
209 1.1 msaitoh int dma_buffer_rseg;
210 1.1 msaitoh
211 1.1 msaitoh uint8_t using_msi;
212 1.1 msaitoh };
213 1.1 msaitoh
214 1.1 msaitoh static int ismt_intr(void *);
215 1.1 msaitoh static int ismt_i2c_acquire_bus(void *, int);
216 1.1 msaitoh static void ismt_i2c_release_bus(void *, int);
217 1.1 msaitoh static int ismt_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
218 1.1 msaitoh size_t, void *, size_t, int);
219 1.1 msaitoh static struct ismt_desc *ismt_alloc_desc(struct ismt_softc *);
220 1.1 msaitoh static int ismt_submit(struct ismt_softc *, struct ismt_desc *,
221 1.1 msaitoh i2c_addr_t, uint8_t, int);
222 1.1 msaitoh static int ismt_quick(struct ismt_softc *, i2c_addr_t, i2c_op_t, int);
223 1.1 msaitoh static int ismt_sendb(struct ismt_softc *, i2c_addr_t, i2c_op_t, char,
224 1.1 msaitoh int);
225 1.1 msaitoh static int ismt_recvb(struct ismt_softc *, i2c_addr_t, i2c_op_t, int);
226 1.1 msaitoh static int ismt_writeb(struct ismt_softc *, i2c_addr_t, i2c_op_t, uint8_t,
227 1.1 msaitoh char, int);
228 1.1 msaitoh static int ismt_writew(struct ismt_softc *, i2c_addr_t, i2c_op_t, uint8_t,
229 1.1 msaitoh uint16_t, int);
230 1.1 msaitoh static int ismt_readb(struct ismt_softc *, i2c_addr_t, i2c_op_t, char,
231 1.1 msaitoh int);
232 1.1 msaitoh static int ismt_readw(struct ismt_softc *, i2c_addr_t, i2c_op_t, char,
233 1.1 msaitoh int);
234 1.1 msaitoh
235 1.1 msaitoh static int ismt_match(device_t, cfdata_t, void *);
236 1.1 msaitoh static void ismt_attach(device_t, device_t, void *);
237 1.1 msaitoh static int ismt_detach(device_t, int);
238 1.1 msaitoh static int ismt_rescan(device_t, const char *, const int *);
239 1.1 msaitoh static void ismt_config_interrupts(device_t);
240 1.1 msaitoh static void ismt_chdet(device_t, device_t);
241 1.1 msaitoh
242 1.1 msaitoh CFATTACH_DECL3_NEW(ismt, sizeof(struct ismt_softc),
243 1.1 msaitoh ismt_match, ismt_attach, ismt_detach, NULL, ismt_rescan, ismt_chdet,
244 1.1 msaitoh DVF_DETACH_SHUTDOWN);
245 1.1 msaitoh
246 1.1 msaitoh static int
247 1.1 msaitoh ismt_intr(void *arg)
248 1.1 msaitoh {
249 1.1 msaitoh struct ismt_softc *sc = arg;
250 1.1 msaitoh uint32_t val;
251 1.1 msaitoh
252 1.1 msaitoh val = bus_space_read_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MSTS);
253 1.1 msaitoh if ((sc->using_msi == 0)
254 1.1 msaitoh && (val & (ISMT_MSTS_MIS | ISMT_MSTS_MEIS)) == 0)
255 1.1 msaitoh return 0; /* Not for me */
256 1.1 msaitoh
257 1.1 msaitoh ISMT_DEBUG(sc->pcidev, "%s MSTS = 0x%08x\n", __func__, val);
258 1.1 msaitoh
259 1.1 msaitoh val |= (ISMT_MSTS_MIS | ISMT_MSTS_MEIS);
260 1.1 msaitoh bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MSTS, val);
261 1.1 msaitoh
262 1.1 msaitoh if (sc->using_msi)
263 1.1 msaitoh wakeup(sc);
264 1.1 msaitoh
265 1.1 msaitoh return 1;
266 1.1 msaitoh }
267 1.1 msaitoh
268 1.1 msaitoh static int
269 1.1 msaitoh ismt_i2c_acquire_bus(void *cookie, int flags)
270 1.1 msaitoh {
271 1.1 msaitoh struct ismt_softc *sc = cookie;
272 1.1 msaitoh
273 1.1 msaitoh mutex_enter(&sc->sc_i2c_mutex);
274 1.1 msaitoh return 0;
275 1.1 msaitoh }
276 1.1 msaitoh
277 1.1 msaitoh static void
278 1.1 msaitoh ismt_i2c_release_bus(void *cookie, int flags)
279 1.1 msaitoh {
280 1.1 msaitoh struct ismt_softc *sc = cookie;
281 1.1 msaitoh
282 1.1 msaitoh mutex_exit(&sc->sc_i2c_mutex);
283 1.1 msaitoh }
284 1.1 msaitoh
285 1.1 msaitoh static int
286 1.1 msaitoh ismt_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
287 1.1 msaitoh const void *cmd, size_t cmdlen, void *buf, size_t buflen, int flags)
288 1.1 msaitoh {
289 1.1 msaitoh struct ismt_softc *sc = cookie;
290 1.1 msaitoh uint8_t *p = buf;
291 1.1 msaitoh int rv;
292 1.1 msaitoh
293 1.1 msaitoh ISMT_DEBUG(sc->pcidev, "exec: op %d, addr 0x%02x, cmdlen %zu, "
294 1.1 msaitoh " buflen %zu, flags 0x%02x\n", op, addr, cmdlen, buflen, flags);
295 1.1 msaitoh
296 1.1 msaitoh if ((cmdlen == 0) && (buflen == 0))
297 1.1 msaitoh return ismt_quick(sc, addr, op, flags);
298 1.1 msaitoh
299 1.1 msaitoh if (I2C_OP_READ_P(op) && (cmdlen == 0) && (buflen == 1)) {
300 1.1 msaitoh rv = ismt_recvb(sc, addr, op, flags);
301 1.1 msaitoh if (rv == -1)
302 1.1 msaitoh return -1;
303 1.1 msaitoh *p = (uint8_t)rv;
304 1.1 msaitoh return 0;
305 1.1 msaitoh }
306 1.1 msaitoh
307 1.1 msaitoh if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 1)) {
308 1.1 msaitoh rv = ismt_readb(sc, addr, op, *(const uint8_t*)cmd, flags);
309 1.1 msaitoh if (rv == -1)
310 1.1 msaitoh return -1;
311 1.1 msaitoh *p = (uint8_t)rv;
312 1.1 msaitoh return 0;
313 1.1 msaitoh }
314 1.1 msaitoh
315 1.1 msaitoh if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 2)) {
316 1.1 msaitoh rv = ismt_readw(sc, addr, op, *(const uint8_t*)cmd, flags);
317 1.1 msaitoh if (rv == -1)
318 1.1 msaitoh return -1;
319 1.1 msaitoh *(uint16_t *)p = (uint16_t)rv;
320 1.1 msaitoh return 0;
321 1.1 msaitoh }
322 1.1 msaitoh
323 1.1 msaitoh if ((I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 1))
324 1.1 msaitoh return ismt_sendb(sc, addr, op, *(uint8_t*)buf, flags);
325 1.1 msaitoh
326 1.1 msaitoh if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 1))
327 1.1 msaitoh return ismt_writeb(sc, addr, op, *(const uint8_t*)cmd,
328 1.1 msaitoh *(uint8_t*)buf, flags);
329 1.1 msaitoh
330 1.1 msaitoh if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 2))
331 1.1 msaitoh return ismt_writew(sc, addr, op,
332 1.1 msaitoh *(const uint8_t*)cmd, *((uint16_t *)buf), flags);
333 1.1 msaitoh
334 1.1 msaitoh return -1;
335 1.1 msaitoh }
336 1.1 msaitoh
337 1.1 msaitoh static struct ismt_desc *
338 1.1 msaitoh ismt_alloc_desc(struct ismt_softc *sc)
339 1.1 msaitoh {
340 1.1 msaitoh struct ismt_desc *desc;
341 1.1 msaitoh
342 1.1 msaitoh KASSERT(mutex_owned(&sc->sc_i2c_mutex));
343 1.1 msaitoh
344 1.1 msaitoh desc = &sc->desc[sc->head++];
345 1.1 msaitoh if (sc->head == ISMT_DESC_ENTRIES)
346 1.1 msaitoh sc->head = 0;
347 1.1 msaitoh
348 1.1 msaitoh memset(desc, 0, sizeof(*desc));
349 1.1 msaitoh
350 1.1 msaitoh return (desc);
351 1.1 msaitoh }
352 1.1 msaitoh
353 1.1 msaitoh static int
354 1.1 msaitoh ismt_submit(struct ismt_softc *sc, struct ismt_desc *desc, i2c_addr_t slave,
355 1.1 msaitoh uint8_t is_read, int flags)
356 1.1 msaitoh {
357 1.1 msaitoh uint32_t err, fmhp, val;
358 1.1 msaitoh int timeout, i;
359 1.1 msaitoh
360 1.1 msaitoh if (sc->using_msi == 0)
361 1.1 msaitoh flags |= I2C_F_POLL;
362 1.1 msaitoh desc->control |= ISMT_DESC_FAIR;
363 1.1 msaitoh if ((flags & I2C_F_POLL) == 0)
364 1.1 msaitoh desc->control |= ISMT_DESC_INT;
365 1.1 msaitoh
366 1.1 msaitoh desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(slave, is_read);
367 1.1 msaitoh desc->dptr_low = (sc->dma_buffer_dma_map->dm_segs[0].ds_addr
368 1.1 msaitoh & 0xFFFFFFFFLL);
369 1.1 msaitoh desc->dptr_high = (sc->dma_buffer_dma_map->dm_segs[0].ds_addr >> 32);
370 1.1 msaitoh
371 1.1 msaitoh bus_dmamap_sync(sc->desc_dma_tag, sc->desc_dma_map,
372 1.1 msaitoh desc - &sc->desc[0], sizeof(struct ismt_desc),
373 1.1 msaitoh BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
374 1.1 msaitoh
375 1.1 msaitoh fmhp = sc->head << 16;
376 1.1 msaitoh val = bus_space_read_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MCTRL);
377 1.1 msaitoh val &= ~ISMT_MCTRL_FMHP;
378 1.1 msaitoh val |= fmhp;
379 1.1 msaitoh bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MCTRL, val);
380 1.1 msaitoh
381 1.1 msaitoh /* set the start bit */
382 1.1 msaitoh val = bus_space_read_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MCTRL);
383 1.1 msaitoh val |= ISMT_MCTRL_SS;
384 1.1 msaitoh bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MCTRL, val);
385 1.1 msaitoh
386 1.1 msaitoh i = 0;
387 1.1 msaitoh if ((flags & I2C_F_POLL) == 0) {
388 1.1 msaitoh timeout = ISMT_INTR_TIMEOUT;
389 1.1 msaitoh if (timeout == 0)
390 1.1 msaitoh timeout = 1;
391 1.1 msaitoh err = tsleep(sc, PWAIT, "ismt_wait", timeout);
392 1.1 msaitoh if (err != 0) {
393 1.1 msaitoh ISMT_DEBUG(sc->pcidev, "%s timeout\n", __func__);
394 1.1 msaitoh return -1;
395 1.1 msaitoh }
396 1.1 msaitoh } else {
397 1.1 msaitoh /* Polling */
398 1.1 msaitoh for (i = 0; i < ISMT_POLL_COUNT; i++) {
399 1.1 msaitoh val = bus_space_read_4(sc->mmio_tag, sc->mmio_handle,
400 1.1 msaitoh ISMT_MSTR_MSTS);
401 1.1 msaitoh if ((val & (ISMT_MSTS_MIS | ISMT_MSTS_MEIS)) != 0) {
402 1.1 msaitoh ismt_intr(sc);
403 1.1 msaitoh err = 0;
404 1.1 msaitoh break;
405 1.1 msaitoh }
406 1.1 msaitoh delay(ISMT_POLL_DELAY);
407 1.1 msaitoh }
408 1.1 msaitoh if (i >= ISMT_POLL_COUNT) {
409 1.1 msaitoh ISMT_DEBUG(sc->pcidev, "%s polling timeout. "
410 1.1 msaitoh "MSTS = %08x\n", __func__, val);
411 1.1 msaitoh return -1;
412 1.1 msaitoh }
413 1.1 msaitoh }
414 1.1 msaitoh
415 1.1 msaitoh bus_dmamap_sync(sc->desc_dma_tag, sc->desc_dma_map,
416 1.1 msaitoh desc - &sc->desc[0], sizeof(struct ismt_desc),
417 1.1 msaitoh BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
418 1.1 msaitoh ISMT_DEBUG(sc->pcidev, "%s status=0x%02x\n", __func__, desc->status);
419 1.1 msaitoh
420 1.1 msaitoh if (desc->status & ISMT_DESC_SCS)
421 1.1 msaitoh return 0;
422 1.1 msaitoh
423 1.1 msaitoh if (desc->status & ISMT_DESC_NAK)
424 1.1 msaitoh return -1;
425 1.1 msaitoh
426 1.1 msaitoh if (desc->status & ISMT_DESC_CRC)
427 1.1 msaitoh return -1;
428 1.1 msaitoh
429 1.1 msaitoh if (desc->status & ISMT_DESC_COL)
430 1.1 msaitoh return -1;
431 1.1 msaitoh
432 1.1 msaitoh if (desc->status & ISMT_DESC_LPR)
433 1.1 msaitoh return -1;
434 1.1 msaitoh
435 1.1 msaitoh if (desc->status & (ISMT_DESC_DLTO | ISMT_DESC_CLTO))
436 1.1 msaitoh return -1;
437 1.1 msaitoh
438 1.1 msaitoh return -1;
439 1.1 msaitoh }
440 1.1 msaitoh
441 1.1 msaitoh static int
442 1.1 msaitoh ismt_quick(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, int flags)
443 1.1 msaitoh {
444 1.1 msaitoh struct ismt_desc *desc;
445 1.1 msaitoh int is_read;
446 1.1 msaitoh
447 1.1 msaitoh ISMT_DEBUG(sc->pcidev, "%s\n", __func__);
448 1.1 msaitoh
449 1.1 msaitoh desc = ismt_alloc_desc(sc);
450 1.1 msaitoh is_read = I2C_OP_READ_P(op);
451 1.1 msaitoh return (ismt_submit(sc, desc, slave, is_read, flags));
452 1.1 msaitoh }
453 1.1 msaitoh
454 1.1 msaitoh static int
455 1.1 msaitoh ismt_sendb(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, char byte,
456 1.1 msaitoh int flags)
457 1.1 msaitoh {
458 1.1 msaitoh struct ismt_desc *desc;
459 1.1 msaitoh
460 1.1 msaitoh ISMT_DEBUG(sc->pcidev, "%s\n", __func__);
461 1.1 msaitoh
462 1.1 msaitoh desc = ismt_alloc_desc(sc);
463 1.1 msaitoh desc->control = ISMT_DESC_CWRL;
464 1.1 msaitoh desc->wr_len_cmd = byte;
465 1.1 msaitoh
466 1.1 msaitoh return (ismt_submit(sc, desc, slave, 0, flags));
467 1.1 msaitoh }
468 1.1 msaitoh
469 1.1 msaitoh static int
470 1.1 msaitoh ismt_recvb(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, int flags)
471 1.1 msaitoh {
472 1.1 msaitoh struct ismt_desc *desc;
473 1.1 msaitoh int err;
474 1.1 msaitoh
475 1.1 msaitoh ISMT_DEBUG(sc->pcidev, "%s\n", __func__);
476 1.1 msaitoh
477 1.1 msaitoh desc = ismt_alloc_desc(sc);
478 1.1 msaitoh desc->rd_len = 1;
479 1.1 msaitoh
480 1.1 msaitoh err = ismt_submit(sc, desc, slave, 1, flags);
481 1.1 msaitoh
482 1.1 msaitoh if (err != 0)
483 1.1 msaitoh return (err);
484 1.1 msaitoh
485 1.1 msaitoh return sc->dma_buffer[0];
486 1.1 msaitoh }
487 1.1 msaitoh
488 1.1 msaitoh static int
489 1.1 msaitoh ismt_writeb(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, uint8_t cmd,
490 1.1 msaitoh char byte, int flags)
491 1.1 msaitoh {
492 1.1 msaitoh struct ismt_desc *desc;
493 1.1 msaitoh
494 1.1 msaitoh ISMT_DEBUG(sc->pcidev, "%s\n", __func__);
495 1.1 msaitoh
496 1.1 msaitoh desc = ismt_alloc_desc(sc);
497 1.1 msaitoh desc->wr_len_cmd = 2;
498 1.1 msaitoh sc->dma_buffer[0] = cmd;
499 1.1 msaitoh sc->dma_buffer[1] = byte;
500 1.1 msaitoh
501 1.1 msaitoh return (ismt_submit(sc, desc, slave, 0, flags));
502 1.1 msaitoh }
503 1.1 msaitoh
504 1.1 msaitoh static int
505 1.1 msaitoh ismt_writew(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, uint8_t cmd,
506 1.1 msaitoh uint16_t word, int flags)
507 1.1 msaitoh {
508 1.1 msaitoh struct ismt_desc *desc;
509 1.1 msaitoh
510 1.1 msaitoh ISMT_DEBUG(sc->pcidev, "%s\n", __func__);
511 1.1 msaitoh
512 1.1 msaitoh desc = ismt_alloc_desc(sc);
513 1.1 msaitoh desc->wr_len_cmd = 3;
514 1.1 msaitoh sc->dma_buffer[0] = cmd;
515 1.1 msaitoh sc->dma_buffer[1] = word & 0xFF;
516 1.1 msaitoh sc->dma_buffer[2] = word >> 8;
517 1.1 msaitoh
518 1.1 msaitoh return (ismt_submit(sc, desc, slave, 0, flags));
519 1.1 msaitoh }
520 1.1 msaitoh
521 1.1 msaitoh static int
522 1.1 msaitoh ismt_readb(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, char cmd,
523 1.1 msaitoh int flags)
524 1.1 msaitoh {
525 1.1 msaitoh struct ismt_desc *desc;
526 1.1 msaitoh int err;
527 1.1 msaitoh
528 1.1 msaitoh ISMT_DEBUG(sc->pcidev, "%s\n", __func__);
529 1.1 msaitoh
530 1.1 msaitoh desc = ismt_alloc_desc(sc);
531 1.1 msaitoh desc->control = ISMT_DESC_CWRL;
532 1.1 msaitoh desc->wr_len_cmd = cmd;
533 1.1 msaitoh desc->rd_len = 1;
534 1.1 msaitoh
535 1.1 msaitoh err = ismt_submit(sc, desc, slave, 1, flags);
536 1.1 msaitoh
537 1.1 msaitoh if (err != 0)
538 1.1 msaitoh return (err);
539 1.1 msaitoh
540 1.1 msaitoh return sc->dma_buffer[0];
541 1.1 msaitoh }
542 1.1 msaitoh
543 1.1 msaitoh static int
544 1.1 msaitoh ismt_readw(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, char cmd,
545 1.1 msaitoh int flags)
546 1.1 msaitoh {
547 1.1 msaitoh struct ismt_desc *desc;
548 1.1 msaitoh uint16_t word;
549 1.1 msaitoh int err;
550 1.1 msaitoh
551 1.1 msaitoh ISMT_DEBUG(sc->pcidev, "%s\n", __func__);
552 1.1 msaitoh
553 1.1 msaitoh desc = ismt_alloc_desc(sc);
554 1.1 msaitoh desc->control = ISMT_DESC_CWRL;
555 1.1 msaitoh desc->wr_len_cmd = cmd;
556 1.1 msaitoh desc->rd_len = 2;
557 1.1 msaitoh
558 1.1 msaitoh err = ismt_submit(sc, desc, slave, 1, flags);
559 1.1 msaitoh
560 1.1 msaitoh if (err != 0)
561 1.1 msaitoh return (err);
562 1.1 msaitoh
563 1.1 msaitoh word = sc->dma_buffer[0] | (sc->dma_buffer[1] << 8);
564 1.1 msaitoh
565 1.1 msaitoh return word;
566 1.1 msaitoh }
567 1.1 msaitoh
568 1.1 msaitoh #if 0
569 1.1 msaitoh static int
570 1.1 msaitoh ismt_pcall(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, char cmd,
571 1.1 msaitoh uint16_t sdata, uint16_t *rdata, int flags)
572 1.1 msaitoh {
573 1.1 msaitoh struct ismt_desc *desc;
574 1.1 msaitoh int err;
575 1.1 msaitoh
576 1.1 msaitoh ISMT_DEBUG(sc->pcidev, "%s\n", __func__);
577 1.1 msaitoh
578 1.1 msaitoh desc = ismt_alloc_desc(sc);
579 1.1 msaitoh desc->wr_len_cmd = 3;
580 1.1 msaitoh desc->rd_len = 2;
581 1.1 msaitoh sc->dma_buffer[0] = cmd;
582 1.1 msaitoh sc->dma_buffer[1] = sdata & 0xff;
583 1.1 msaitoh sc->dma_buffer[2] = sdata >> 8;
584 1.1 msaitoh
585 1.1 msaitoh err = ismt_submit(sc, desc, slave, 0, flags);
586 1.1 msaitoh
587 1.1 msaitoh if (err != 0)
588 1.1 msaitoh return (err);
589 1.1 msaitoh
590 1.1 msaitoh *rdata = sc->dma_buffer[0] | (sc->dma_buffer[1] << 8);
591 1.1 msaitoh
592 1.1 msaitoh return (err);
593 1.1 msaitoh }
594 1.1 msaitoh
595 1.1 msaitoh static int
596 1.1 msaitoh ismt_bwrite(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, char cmd,
597 1.1 msaitoh u_char count, char *buf, int flags)
598 1.1 msaitoh {
599 1.1 msaitoh struct ismt_desc *desc;
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;
608 1.1 msaitoh desc->wr_len_cmd = count + 1;
609 1.1 msaitoh sc->dma_buffer[0] = cmd;
610 1.1 msaitoh memcpy(&sc->dma_buffer[1], buf, count);
611 1.1 msaitoh
612 1.1 msaitoh return (ismt_submit(sc, desc, slave, 0, flags));
613 1.1 msaitoh }
614 1.1 msaitoh
615 1.1 msaitoh static int
616 1.1 msaitoh ismt_bread(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, char cmd,
617 1.1 msaitoh u_char *count, char *buf, int flags)
618 1.1 msaitoh {
619 1.1 msaitoh struct ismt_desc *desc;
620 1.1 msaitoh int err;
621 1.1 msaitoh
622 1.1 msaitoh ISMT_DEBUG(sc->pcidev, "%s\n", __func__);
623 1.1 msaitoh
624 1.1 msaitoh if (*count == 0 || *count > ISMT_MAX_BLOCK_SIZE)
625 1.1 msaitoh return -1;
626 1.1 msaitoh
627 1.1 msaitoh desc = ismt_alloc_desc(sc);
628 1.1 msaitoh desc->control = ISMT_DESC_I2C | ISMT_DESC_CWRL;
629 1.1 msaitoh desc->wr_len_cmd = cmd;
630 1.1 msaitoh desc->rd_len = *count;
631 1.1 msaitoh
632 1.1 msaitoh err = ismt_submit(sc, desc, slave, 0, flags);
633 1.1 msaitoh
634 1.1 msaitoh if (err != 0)
635 1.1 msaitoh return (err);
636 1.1 msaitoh
637 1.1 msaitoh memcpy(buf, sc->dma_buffer, desc->rxbytes);
638 1.1 msaitoh *count = desc->rxbytes;
639 1.1 msaitoh
640 1.1 msaitoh return (err);
641 1.1 msaitoh }
642 1.1 msaitoh #endif
643 1.1 msaitoh
644 1.1 msaitoh static int
645 1.1 msaitoh ismt_detach(device_t self, int flags)
646 1.1 msaitoh {
647 1.1 msaitoh struct ismt_softc *sc;
648 1.1 msaitoh int rv = 0;
649 1.1 msaitoh
650 1.1 msaitoh ISMT_DEBUG(self, "%s\n", __func__);
651 1.1 msaitoh sc = device_private(self);
652 1.1 msaitoh if (sc->smbdev != NULL) {
653 1.1 msaitoh rv = config_detach(sc->smbdev, flags);
654 1.1 msaitoh if (rv != 0)
655 1.1 msaitoh return rv;
656 1.1 msaitoh }
657 1.1 msaitoh if (sc->sc_ih != NULL) {
658 1.1 msaitoh pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
659 1.1 msaitoh sc->sc_ih = NULL;
660 1.1 msaitoh }
661 1.1 msaitoh if (sc->sc_pihp != NULL) {
662 1.1 msaitoh pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
663 1.1 msaitoh sc->sc_pihp = NULL;
664 1.1 msaitoh }
665 1.1 msaitoh
666 1.1 msaitoh bus_dmamap_unload(sc->desc_dma_tag, sc->desc_dma_map);
667 1.1 msaitoh bus_dmamap_unload(sc->dma_buffer_dma_tag, sc->dma_buffer_dma_map);
668 1.1 msaitoh
669 1.1 msaitoh bus_dmamem_free(sc->desc_dma_tag, &sc->desc_dma_seg, sc->desc_rseg);
670 1.1 msaitoh bus_dmamem_free(sc->dma_buffer_dma_tag, &sc->dma_buffer_dma_seg,
671 1.1 msaitoh sc->dma_buffer_rseg);
672 1.1 msaitoh
673 1.1 msaitoh if (sc->mmio_size)
674 1.1 msaitoh bus_space_unmap(sc->mmio_tag, sc->mmio_handle, sc->mmio_size);
675 1.1 msaitoh
676 1.1 msaitoh mutex_destroy(&sc->sc_i2c_mutex);
677 1.1 msaitoh return rv;
678 1.1 msaitoh }
679 1.1 msaitoh
680 1.1 msaitoh static void
681 1.1 msaitoh ismt_attach(device_t parent, device_t self, void *aux)
682 1.1 msaitoh {
683 1.1 msaitoh struct ismt_softc *sc = device_private(self);
684 1.1 msaitoh struct pci_attach_args *pa = aux;
685 1.1 msaitoh const char *intrstr = NULL;
686 1.1 msaitoh char intrbuf[PCI_INTRSTR_LEN];
687 1.1 msaitoh pcireg_t reg;
688 1.1 msaitoh int val;
689 1.1 msaitoh
690 1.1 msaitoh sc->pcidev = self;
691 1.1 msaitoh sc->sc_pc = pa->pa_pc;
692 1.1 msaitoh sc->sc_pcitag = pa->pa_tag;
693 1.1 msaitoh
694 1.1 msaitoh /* Enable busmastering */
695 1.1 msaitoh reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
696 1.1 msaitoh reg |= PCI_COMMAND_MASTER_ENABLE;
697 1.1 msaitoh pci_conf_write(sc->sc_pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, reg);
698 1.1 msaitoh
699 1.1 msaitoh pci_aprint_devinfo(pa, NULL);
700 1.1 msaitoh
701 1.1 msaitoh /* Map mem space */
702 1.1 msaitoh if (pci_mapreg_map(pa, PCI_BAR0, PCI_MAPREG_TYPE_MEM, 0,
703 1.1 msaitoh &sc->mmio_tag, &sc->mmio_handle, NULL, &sc->mmio_size)) {
704 1.1 msaitoh aprint_error_dev(self, "can't map mem space\n");
705 1.1 msaitoh goto fail;
706 1.1 msaitoh }
707 1.1 msaitoh
708 1.1 msaitoh if (pci_dma64_available(pa)) {
709 1.1 msaitoh sc->desc_dma_tag = pa->pa_dmat64;
710 1.1 msaitoh sc->dma_buffer_dma_tag = pa->pa_dmat64;
711 1.1 msaitoh } else {
712 1.1 msaitoh sc->desc_dma_tag = pa->pa_dmat;
713 1.1 msaitoh sc->dma_buffer_dma_tag = pa->pa_dmat;
714 1.1 msaitoh }
715 1.1 msaitoh bus_dmamem_alloc(sc->desc_dma_tag, DESC_SIZE, PAGE_SIZE, 0,
716 1.1 msaitoh &sc->desc_dma_seg, ISMT_DESC_ENTRIES, &sc->desc_rseg,
717 1.1 msaitoh BUS_DMA_WAITOK);
718 1.1 msaitoh bus_dmamem_alloc(sc->dma_buffer_dma_tag, DMA_BUFFER_SIZE, PAGE_SIZE, 0,
719 1.1 msaitoh &sc->dma_buffer_dma_seg, 1, &sc->dma_buffer_rseg, BUS_DMA_WAITOK);
720 1.1 msaitoh
721 1.1 msaitoh bus_dmamem_map(sc->desc_dma_tag, &sc->desc_dma_seg,
722 1.1 msaitoh sc->desc_rseg, DESC_SIZE, (void **)&sc->desc, BUS_DMA_COHERENT);
723 1.1 msaitoh bus_dmamem_map(sc->dma_buffer_dma_tag, &sc->dma_buffer_dma_seg,
724 1.1 msaitoh sc->dma_buffer_rseg, DMA_BUFFER_SIZE, (void **)&sc->dma_buffer,
725 1.1 msaitoh BUS_DMA_COHERENT);
726 1.1 msaitoh
727 1.1 msaitoh bus_dmamap_create(sc->desc_dma_tag, DESC_SIZE, 1,
728 1.1 msaitoh DESC_SIZE, 0, 0, &sc->desc_dma_map);
729 1.1 msaitoh bus_dmamap_create(sc->dma_buffer_dma_tag, DMA_BUFFER_SIZE, 1,
730 1.1 msaitoh DMA_BUFFER_SIZE, 0, 0, &sc->dma_buffer_dma_map);
731 1.1 msaitoh
732 1.1 msaitoh bus_dmamap_load(sc->desc_dma_tag,
733 1.1 msaitoh sc->desc_dma_map, sc->desc, DESC_SIZE, NULL, 0);
734 1.1 msaitoh bus_dmamap_load(sc->dma_buffer_dma_tag,
735 1.1 msaitoh sc->dma_buffer_dma_map, sc->dma_buffer, DMA_BUFFER_SIZE,
736 1.1 msaitoh NULL, 0);
737 1.1 msaitoh
738 1.1 msaitoh bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MDBA,
739 1.1 msaitoh (sc->desc_dma_map->dm_segs[0].ds_addr & 0xFFFFFFFFLL));
740 1.1 msaitoh bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MDBA + 4,
741 1.1 msaitoh (sc->desc_dma_map->dm_segs[0].ds_addr >> 32));
742 1.1 msaitoh
743 1.1 msaitoh /* initialize the Master Control Register (MCTRL) */
744 1.1 msaitoh bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MCTRL,
745 1.1 msaitoh ISMT_MCTRL_MEIE);
746 1.1 msaitoh
747 1.1 msaitoh /* initialize the Master Status Register (MSTS) */
748 1.1 msaitoh bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MSTS, 0);
749 1.1 msaitoh
750 1.1 msaitoh /* initialize the Master Descriptor Size (MDS) */
751 1.1 msaitoh val = bus_space_read_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MDS);
752 1.1 msaitoh val &= ~ISMT_MDS_MASK;
753 1.1 msaitoh val |= (ISMT_DESC_ENTRIES - 1);
754 1.1 msaitoh bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MDS, val);
755 1.1 msaitoh
756 1.1 msaitoh if (pci_intr_alloc(pa, &sc->sc_pihp, NULL, 0)) {
757 1.1 msaitoh aprint_error_dev(self, "couldn't map interrupt\n");
758 1.1 msaitoh return;
759 1.1 msaitoh }
760 1.1 msaitoh intrstr = pci_intr_string(pa->pa_pc, sc->sc_pihp[0], intrbuf,
761 1.1 msaitoh sizeof(intrbuf));
762 1.1 msaitoh sc->sc_ih = pci_intr_establish(pa->pa_pc, sc->sc_pihp[0], IPL_BIO,
763 1.1 msaitoh ismt_intr, sc);
764 1.1 msaitoh if (sc->sc_ih == NULL) {
765 1.1 msaitoh aprint_error_dev(sc->pcidev, "unable to establish %s\n",
766 1.1 msaitoh (pci_intr_type(sc->sc_pihp[0])
767 1.1 msaitoh == PCI_INTR_TYPE_MSI) ? "MSI" : "INTx");
768 1.1 msaitoh /* Polling */
769 1.1 msaitoh }
770 1.1 msaitoh
771 1.1 msaitoh if (pci_intr_type(sc->sc_pihp[0]) == PCI_INTR_TYPE_MSI)
772 1.1 msaitoh sc->using_msi = 1;
773 1.1 msaitoh
774 1.1 msaitoh aprint_normal_dev(sc->pcidev, "interrupting at %s\n", intrstr);
775 1.1 msaitoh
776 1.1 msaitoh sc->smbdev = NULL;
777 1.1 msaitoh mutex_init(&sc->sc_i2c_mutex, MUTEX_DEFAULT, IPL_NONE);
778 1.1 msaitoh if (!pmf_device_register(self, NULL, NULL))
779 1.1 msaitoh aprint_error_dev(self, "couldn't establish power handler\n");
780 1.1 msaitoh
781 1.1 msaitoh config_interrupts(self, ismt_config_interrupts);
782 1.1 msaitoh return;
783 1.1 msaitoh
784 1.1 msaitoh fail:
785 1.1 msaitoh ismt_detach(sc->pcidev, 0);
786 1.1 msaitoh
787 1.1 msaitoh return;
788 1.1 msaitoh }
789 1.1 msaitoh
790 1.1 msaitoh static int
791 1.1 msaitoh ismt_match(device_t parent, cfdata_t match, void *aux)
792 1.1 msaitoh {
793 1.1 msaitoh struct pci_attach_args *pa = aux;
794 1.1 msaitoh
795 1.1 msaitoh if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
796 1.1 msaitoh return 0;
797 1.1 msaitoh
798 1.1 msaitoh switch (PCI_PRODUCT(pa->pa_id)) {
799 1.1 msaitoh case PCI_PRODUCT_INTEL_S1200_SMBUS_0:
800 1.1 msaitoh case PCI_PRODUCT_INTEL_S1200_SMBUS_1:
801 1.1 msaitoh case PCI_PRODUCT_INTEL_C2000_SMBUS:
802 1.1 msaitoh break;
803 1.1 msaitoh default:
804 1.1 msaitoh return 0;
805 1.1 msaitoh }
806 1.1 msaitoh
807 1.1 msaitoh return 1;
808 1.1 msaitoh }
809 1.1 msaitoh
810 1.1 msaitoh static int
811 1.1 msaitoh ismt_rescan(device_t self, const char *ifattr, const int *flags)
812 1.1 msaitoh {
813 1.1 msaitoh struct ismt_softc *sc = device_private(self);
814 1.1 msaitoh struct i2cbus_attach_args iba;
815 1.1 msaitoh
816 1.1 msaitoh if (!ifattr_match(ifattr, "i2cbus"))
817 1.1 msaitoh return 0;
818 1.1 msaitoh
819 1.1 msaitoh if (sc->smbdev)
820 1.1 msaitoh return 0;
821 1.1 msaitoh
822 1.1 msaitoh /* Attach I2C bus */
823 1.1 msaitoh sc->sc_i2c_tag.ic_cookie = sc;
824 1.1 msaitoh sc->sc_i2c_tag.ic_acquire_bus = ismt_i2c_acquire_bus;
825 1.1 msaitoh sc->sc_i2c_tag.ic_release_bus = ismt_i2c_release_bus;
826 1.1 msaitoh sc->sc_i2c_tag.ic_exec = ismt_i2c_exec;
827 1.1 msaitoh
828 1.1 msaitoh memset(&iba, 0, sizeof(iba));
829 1.1 msaitoh iba.iba_type = I2C_TYPE_SMBUS;
830 1.1 msaitoh iba.iba_tag = &sc->sc_i2c_tag;
831 1.1 msaitoh sc->smbdev = config_found_ia(self, ifattr, &iba, iicbus_print);
832 1.1 msaitoh
833 1.1 msaitoh return 0;
834 1.1 msaitoh }
835 1.1 msaitoh
836 1.1 msaitoh static void
837 1.1 msaitoh ismt_config_interrupts(device_t self)
838 1.1 msaitoh {
839 1.1 msaitoh int flags = 0;
840 1.1 msaitoh
841 1.1 msaitoh ismt_rescan(self, "i2cbus", &flags);
842 1.1 msaitoh }
843 1.1 msaitoh
844 1.1 msaitoh static void
845 1.1 msaitoh ismt_chdet(device_t self, device_t child)
846 1.1 msaitoh {
847 1.1 msaitoh struct ismt_softc *sc = device_private(self);
848 1.1 msaitoh
849 1.1 msaitoh if (sc->smbdev == child)
850 1.1 msaitoh sc->smbdev = NULL;
851 1.1 msaitoh
852 1.1 msaitoh }
853 1.1 msaitoh
854 1.1 msaitoh MODULE(MODULE_CLASS_DRIVER, ismt, "pci");
855 1.1 msaitoh
856 1.1 msaitoh #ifdef _MODULE
857 1.1 msaitoh #include "ioconf.c"
858 1.1 msaitoh #endif
859 1.1 msaitoh
860 1.1 msaitoh static int
861 1.1 msaitoh ismt_modcmd(modcmd_t cmd, void *opaque)
862 1.1 msaitoh {
863 1.1 msaitoh int error = 0;
864 1.1 msaitoh
865 1.1 msaitoh switch (cmd) {
866 1.1 msaitoh case MODULE_CMD_INIT:
867 1.1 msaitoh #ifdef _MODULE
868 1.1 msaitoh error = config_init_component(cfdriver_ioconf_ismt,
869 1.1 msaitoh cfattach_ioconf_ismt, cfdata_ioconf_ismt);
870 1.1 msaitoh #endif
871 1.1 msaitoh return error;
872 1.1 msaitoh case MODULE_CMD_FINI:
873 1.1 msaitoh #ifdef _MODULE
874 1.1 msaitoh error = config_fini_component(cfdriver_ioconf_ismt,
875 1.1 msaitoh cfattach_ioconf_ismt, cfdata_ioconf_ismt);
876 1.1 msaitoh #endif
877 1.1 msaitoh return error;
878 1.1 msaitoh default:
879 1.1 msaitoh return ENOTTY;
880 1.1 msaitoh }
881 1.1 msaitoh }
882