nfsmb.c revision 1.1.4.5 1 1.1.4.5 ad /* $NetBSD: nfsmb.c,v 1.1.4.5 2007/10/23 20:09:10 ad Exp $ */
2 1.1.4.2 ad /*
3 1.1.4.2 ad * Copyright (c) 2007 KIYOHARA Takashi
4 1.1.4.2 ad * All rights reserved.
5 1.1.4.2 ad *
6 1.1.4.2 ad * Redistribution and use in source and binary forms, with or without
7 1.1.4.2 ad * modification, are permitted provided that the following conditions
8 1.1.4.2 ad * are met:
9 1.1.4.2 ad * 1. Redistributions of source code must retain the above copyright
10 1.1.4.2 ad * notice, this list of conditions and the following disclaimer.
11 1.1.4.2 ad * 2. Redistributions in binary form must reproduce the above copyright
12 1.1.4.2 ad * notice, this list of conditions and the following disclaimer in the
13 1.1.4.2 ad * documentation and/or other materials provided with the distribution.
14 1.1.4.2 ad *
15 1.1.4.2 ad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1.4.2 ad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1.4.2 ad * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1.4.2 ad * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 1.1.4.2 ad * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 1.1.4.2 ad * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1.4.2 ad * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1.4.2 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 1.1.4.2 ad * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 1.1.4.2 ad * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1.4.2 ad * POSSIBILITY OF SUCH DAMAGE.
26 1.1.4.2 ad *
27 1.1.4.2 ad */
28 1.1.4.2 ad #include <sys/cdefs.h>
29 1.1.4.5 ad __KERNEL_RCSID(0, "$NetBSD: nfsmb.c,v 1.1.4.5 2007/10/23 20:09:10 ad Exp $");
30 1.1.4.2 ad
31 1.1.4.2 ad #include <sys/param.h>
32 1.1.4.2 ad #include <sys/device.h>
33 1.1.4.2 ad #include <sys/errno.h>
34 1.1.4.2 ad #include <sys/kernel.h>
35 1.1.4.4 ad #include <sys/rwlock.h>
36 1.1.4.2 ad #include <sys/proc.h>
37 1.1.4.2 ad
38 1.1.4.5 ad #include <sys/bus.h>
39 1.1.4.2 ad
40 1.1.4.2 ad #include <dev/i2c/i2cvar.h>
41 1.1.4.2 ad
42 1.1.4.2 ad #include <dev/pci/pcivar.h>
43 1.1.4.2 ad #include <dev/pci/pcireg.h>
44 1.1.4.2 ad #include <dev/pci/pcidevs.h>
45 1.1.4.2 ad
46 1.1.4.2 ad #include <dev/pci/nfsmbreg.h>
47 1.1.4.2 ad
48 1.1.4.2 ad
49 1.1.4.2 ad struct nfsmbc_attach_args {
50 1.1.4.2 ad int nfsmb_num;
51 1.1.4.2 ad bus_space_tag_t nfsmb_iot;
52 1.1.4.2 ad int nfsmb_addr;
53 1.1.4.2 ad };
54 1.1.4.2 ad
55 1.1.4.2 ad struct nfsmb_softc;
56 1.1.4.2 ad struct nfsmbc_softc {
57 1.1.4.2 ad struct device sc_dev;
58 1.1.4.2 ad
59 1.1.4.2 ad pci_chipset_tag_t sc_pc;
60 1.1.4.2 ad pcitag_t sc_tag;
61 1.1.4.2 ad struct pci_attach_args *sc_pa;
62 1.1.4.2 ad
63 1.1.4.2 ad bus_space_tag_t sc_iot;
64 1.1.4.2 ad struct device *sc_nfsmb[2];
65 1.1.4.2 ad };
66 1.1.4.2 ad
67 1.1.4.2 ad struct nfsmb_softc {
68 1.1.4.2 ad struct device sc_dev;
69 1.1.4.2 ad int sc_num;
70 1.1.4.2 ad struct device *sc_nfsmbc;
71 1.1.4.2 ad
72 1.1.4.2 ad bus_space_tag_t sc_iot;
73 1.1.4.2 ad bus_space_handle_t sc_ioh;
74 1.1.4.2 ad
75 1.1.4.2 ad struct i2c_controller sc_i2c; /* i2c controller info */
76 1.1.4.4 ad krwlock_t sc_rwlock;
77 1.1.4.2 ad };
78 1.1.4.2 ad
79 1.1.4.2 ad
80 1.1.4.2 ad static int nfsmbc_match(struct device *, struct cfdata *, void *);
81 1.1.4.2 ad static void nfsmbc_attach(struct device *, struct device *, void *);
82 1.1.4.2 ad static int nfsmbc_print(void *, const char *);
83 1.1.4.2 ad
84 1.1.4.2 ad static int nfsmb_match(struct device *, struct cfdata *, void *);
85 1.1.4.2 ad static void nfsmb_attach(struct device *, struct device *, void *);
86 1.1.4.2 ad static int nfsmb_acquire_bus(void *, int);
87 1.1.4.2 ad static void nfsmb_release_bus(void *, int);
88 1.1.4.2 ad static int nfsmb_exec(
89 1.1.4.2 ad void *, i2c_op_t, i2c_addr_t, const void *, size_t, void *, size_t, int);
90 1.1.4.2 ad static int nfsmb_check_done(struct nfsmb_softc *);
91 1.1.4.2 ad static int
92 1.1.4.2 ad nfsmb_send_1(struct nfsmb_softc *, uint8_t, i2c_addr_t, i2c_op_t, int);
93 1.1.4.2 ad static int nfsmb_write_1(
94 1.1.4.2 ad struct nfsmb_softc *, uint8_t, uint8_t, i2c_addr_t, i2c_op_t, int);
95 1.1.4.2 ad static int nfsmb_write_2(
96 1.1.4.2 ad struct nfsmb_softc *, uint8_t, uint16_t, i2c_addr_t, i2c_op_t, int);
97 1.1.4.2 ad static int nfsmb_receive_1(struct nfsmb_softc *, i2c_addr_t, i2c_op_t, int);
98 1.1.4.2 ad static int
99 1.1.4.2 ad nfsmb_read_1(struct nfsmb_softc *, uint8_t, i2c_addr_t, i2c_op_t, int);
100 1.1.4.2 ad static int
101 1.1.4.2 ad nfsmb_read_2(struct nfsmb_softc *, uint8_t, i2c_addr_t, i2c_op_t, int);
102 1.1.4.2 ad
103 1.1.4.2 ad
104 1.1.4.2 ad CFATTACH_DECL(nfsmbc, sizeof(struct nfsmbc_softc),
105 1.1.4.2 ad nfsmbc_match, nfsmbc_attach, NULL, NULL);
106 1.1.4.2 ad
107 1.1.4.2 ad static int
108 1.1.4.2 ad nfsmbc_match(struct device *parent, struct cfdata *match, void *aux)
109 1.1.4.2 ad {
110 1.1.4.2 ad struct pci_attach_args *pa = aux;
111 1.1.4.2 ad
112 1.1.4.2 ad if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NVIDIA) {
113 1.1.4.2 ad switch (PCI_PRODUCT(pa->pa_id)) {
114 1.1.4.2 ad case PCI_PRODUCT_NVIDIA_NFORCE2_SMBUS:
115 1.1.4.2 ad case PCI_PRODUCT_NVIDIA_NFORCE2_400_SMBUS:
116 1.1.4.2 ad case PCI_PRODUCT_NVIDIA_NFORCE3_SMBUS:
117 1.1.4.2 ad case PCI_PRODUCT_NVIDIA_NFORCE3_250_SMBUS:
118 1.1.4.2 ad case PCI_PRODUCT_NVIDIA_NFORCE4_SMBUS:
119 1.1.4.2 ad case PCI_PRODUCT_NVIDIA_MCP04_SMBUS:
120 1.1.4.2 ad return 1;
121 1.1.4.2 ad }
122 1.1.4.2 ad }
123 1.1.4.2 ad
124 1.1.4.2 ad return 0;
125 1.1.4.2 ad }
126 1.1.4.2 ad
127 1.1.4.2 ad static void
128 1.1.4.2 ad nfsmbc_attach(struct device *parent, struct device *self, void *aux)
129 1.1.4.2 ad {
130 1.1.4.2 ad struct nfsmbc_softc *sc = (struct nfsmbc_softc *) self;
131 1.1.4.2 ad struct pci_attach_args *pa = aux;
132 1.1.4.2 ad struct nfsmbc_attach_args nfsmbca;
133 1.1.4.3 ad pcireg_t reg;
134 1.1.4.2 ad char devinfo[256];
135 1.1.4.2 ad
136 1.1.4.2 ad aprint_naive("\n");
137 1.1.4.2 ad pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
138 1.1.4.2 ad aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
139 1.1.4.2 ad PCI_REVISION(pa->pa_class));
140 1.1.4.2 ad
141 1.1.4.2 ad sc->sc_pc = pa->pa_pc;
142 1.1.4.2 ad sc->sc_tag = pa->pa_tag;
143 1.1.4.2 ad sc->sc_pa = pa;
144 1.1.4.2 ad sc->sc_iot = pa->pa_iot;
145 1.1.4.2 ad
146 1.1.4.2 ad nfsmbca.nfsmb_iot = sc->sc_iot;
147 1.1.4.2 ad
148 1.1.4.2 ad reg = pci_conf_read(pa->pa_pc, pa->pa_tag, NFORCE_SMB1);
149 1.1.4.2 ad nfsmbca.nfsmb_num = 1;
150 1.1.4.2 ad nfsmbca.nfsmb_addr = NFORCE_SMBBASE(reg);
151 1.1.4.2 ad sc->sc_nfsmb[0] = config_found(&sc->sc_dev, &nfsmbca, nfsmbc_print);
152 1.1.4.2 ad
153 1.1.4.2 ad reg = pci_conf_read(pa->pa_pc, pa->pa_tag, NFORCE_SMB2);
154 1.1.4.2 ad nfsmbca.nfsmb_num = 2;
155 1.1.4.2 ad nfsmbca.nfsmb_addr = NFORCE_SMBBASE(reg);
156 1.1.4.2 ad sc->sc_nfsmb[1] = config_found(&sc->sc_dev, &nfsmbca, nfsmbc_print);
157 1.1.4.2 ad }
158 1.1.4.2 ad
159 1.1.4.2 ad static int
160 1.1.4.2 ad nfsmbc_print(void *aux, const char *pnp)
161 1.1.4.2 ad {
162 1.1.4.2 ad struct nfsmbc_attach_args *nfsmbcap = aux;
163 1.1.4.2 ad
164 1.1.4.2 ad if (pnp)
165 1.1.4.2 ad aprint_normal("nfsmb SMBus %d at %s",
166 1.1.4.2 ad nfsmbcap->nfsmb_num, pnp);
167 1.1.4.2 ad else
168 1.1.4.2 ad aprint_normal(" SMBus %d", nfsmbcap->nfsmb_num);
169 1.1.4.2 ad return UNCONF;
170 1.1.4.2 ad }
171 1.1.4.2 ad
172 1.1.4.2 ad
173 1.1.4.2 ad CFATTACH_DECL(nfsmb, sizeof(struct nfsmb_softc),
174 1.1.4.2 ad nfsmb_match, nfsmb_attach, NULL, NULL);
175 1.1.4.2 ad
176 1.1.4.2 ad static int
177 1.1.4.2 ad nfsmb_match(struct device *parent, struct cfdata *match, void *aux)
178 1.1.4.2 ad {
179 1.1.4.2 ad struct nfsmbc_attach_args *nfsmbcap = aux;
180 1.1.4.2 ad
181 1.1.4.2 ad if (nfsmbcap->nfsmb_num == 1 || nfsmbcap->nfsmb_num == 2)
182 1.1.4.2 ad return 1;
183 1.1.4.2 ad return 0;
184 1.1.4.2 ad }
185 1.1.4.2 ad
186 1.1.4.2 ad static void
187 1.1.4.2 ad nfsmb_attach(struct device *parent, struct device *self, void *aux)
188 1.1.4.2 ad {
189 1.1.4.2 ad struct nfsmb_softc *sc = (struct nfsmb_softc *) self;
190 1.1.4.2 ad struct nfsmbc_attach_args *nfsmbcap = aux;
191 1.1.4.2 ad struct i2cbus_attach_args iba;
192 1.1.4.2 ad
193 1.1.4.2 ad aprint_naive("\n");
194 1.1.4.2 ad aprint_normal("\n");
195 1.1.4.2 ad
196 1.1.4.2 ad sc->sc_nfsmbc = parent;
197 1.1.4.2 ad sc->sc_num = nfsmbcap->nfsmb_num;
198 1.1.4.2 ad sc->sc_iot = nfsmbcap->nfsmb_iot;
199 1.1.4.2 ad
200 1.1.4.2 ad /* register with iic */
201 1.1.4.2 ad sc->sc_i2c.ic_cookie = sc;
202 1.1.4.2 ad sc->sc_i2c.ic_acquire_bus = nfsmb_acquire_bus;
203 1.1.4.2 ad sc->sc_i2c.ic_release_bus = nfsmb_release_bus;
204 1.1.4.2 ad sc->sc_i2c.ic_send_start = NULL;
205 1.1.4.2 ad sc->sc_i2c.ic_send_stop = NULL;
206 1.1.4.2 ad sc->sc_i2c.ic_initiate_xfer = NULL;
207 1.1.4.2 ad sc->sc_i2c.ic_read_byte = NULL;
208 1.1.4.2 ad sc->sc_i2c.ic_write_byte = NULL;
209 1.1.4.2 ad sc->sc_i2c.ic_exec = nfsmb_exec;
210 1.1.4.2 ad
211 1.1.4.4 ad rw_init(&sc->sc_rwlock);
212 1.1.4.2 ad
213 1.1.4.2 ad if (bus_space_map(sc->sc_iot, nfsmbcap->nfsmb_addr, NFORCE_SMBSIZE, 0,
214 1.1.4.3 ad &sc->sc_ioh) != 0) {
215 1.1.4.2 ad aprint_error("%s: failed to map SMBus space\n",
216 1.1.4.2 ad sc->sc_dev.dv_xname);
217 1.1.4.2 ad return;
218 1.1.4.2 ad }
219 1.1.4.2 ad
220 1.1.4.4 ad iba.iba_type = I2C_TYPE_SMBUS;
221 1.1.4.2 ad iba.iba_tag = &sc->sc_i2c;
222 1.1.4.2 ad (void) config_found_ia(&sc->sc_dev, "i2cbus", &iba, iicbus_print);
223 1.1.4.2 ad }
224 1.1.4.2 ad
225 1.1.4.2 ad static int
226 1.1.4.2 ad nfsmb_acquire_bus(void *cookie, int flags)
227 1.1.4.2 ad {
228 1.1.4.2 ad struct nfsmb_softc *sc = cookie;
229 1.1.4.2 ad
230 1.1.4.4 ad rw_enter(&sc->sc_rwlock, RW_WRITER);
231 1.1.4.4 ad return 0;
232 1.1.4.2 ad }
233 1.1.4.2 ad
234 1.1.4.2 ad static void
235 1.1.4.2 ad nfsmb_release_bus(void *cookie, int flags)
236 1.1.4.2 ad {
237 1.1.4.2 ad struct nfsmb_softc *sc = cookie;
238 1.1.4.2 ad
239 1.1.4.4 ad rw_exit(&sc->sc_rwlock);
240 1.1.4.2 ad }
241 1.1.4.2 ad
242 1.1.4.2 ad static int
243 1.1.4.2 ad nfsmb_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *cmd,
244 1.1.4.2 ad size_t cmdlen, void *vbuf, size_t buflen, int flags)
245 1.1.4.2 ad {
246 1.1.4.2 ad struct nfsmb_softc *sc = (struct nfsmb_softc *)cookie;
247 1.1.4.2 ad uint8_t *p = vbuf;
248 1.1.4.2 ad int rv;
249 1.1.4.2 ad
250 1.1.4.2 ad if (I2C_OP_READ_P(op) && (cmdlen == 0) && (buflen == 1)) {
251 1.1.4.2 ad rv = nfsmb_receive_1(sc, addr, op, flags);
252 1.1.4.2 ad if (rv == -1)
253 1.1.4.2 ad return -1;
254 1.1.4.2 ad *p = (uint8_t)rv;
255 1.1.4.2 ad return 0;
256 1.1.4.2 ad }
257 1.1.4.2 ad
258 1.1.4.2 ad if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 1)) {
259 1.1.4.2 ad rv = nfsmb_read_1(sc, *(const uint8_t*)cmd, addr, op, flags);
260 1.1.4.2 ad if (rv == -1)
261 1.1.4.2 ad return -1;
262 1.1.4.2 ad *p = (uint8_t)rv;
263 1.1.4.2 ad return 0;
264 1.1.4.2 ad }
265 1.1.4.3 ad
266 1.1.4.2 ad if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 2)) {
267 1.1.4.2 ad rv = nfsmb_read_2(sc, *(const uint8_t*)cmd, addr, op, flags);
268 1.1.4.2 ad if (rv == -1)
269 1.1.4.2 ad return -1;
270 1.1.4.2 ad *p = (uint8_t)rv;
271 1.1.4.2 ad return 0;
272 1.1.4.2 ad }
273 1.1.4.2 ad
274 1.1.4.2 ad if ((I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 1))
275 1.1.4.2 ad return nfsmb_send_1(sc, *(uint8_t*)vbuf, addr, op, flags);
276 1.1.4.2 ad
277 1.1.4.2 ad if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 1))
278 1.1.4.2 ad return nfsmb_write_1(sc, *(const uint8_t*)cmd, *(uint8_t*)vbuf,
279 1.1.4.2 ad addr, op, flags);
280 1.1.4.2 ad
281 1.1.4.2 ad if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 2))
282 1.1.4.2 ad return nfsmb_write_2(sc,
283 1.1.4.2 ad *(const uint8_t*)cmd, *((uint16_t *)vbuf), addr, op, flags);
284 1.1.4.2 ad
285 1.1.4.3 ad return -1;
286 1.1.4.2 ad }
287 1.1.4.2 ad
288 1.1.4.2 ad static int
289 1.1.4.2 ad nfsmb_check_done(struct nfsmb_softc *sc)
290 1.1.4.2 ad {
291 1.1.4.3 ad int us;
292 1.1.4.2 ad uint8_t stat;
293 1.1.4.2 ad
294 1.1.4.3 ad us = 10 * 1000; /* XXXX: wait maximum 10 msec */
295 1.1.4.3 ad do {
296 1.1.4.3 ad delay(10);
297 1.1.4.3 ad us -= 10;
298 1.1.4.3 ad if (us <= 0)
299 1.1.4.3 ad return -1;
300 1.1.4.3 ad } while (bus_space_read_1(sc->sc_iot, sc->sc_ioh,
301 1.1.4.3 ad NFORCE_SMB_PROTOCOL) != 0);
302 1.1.4.2 ad
303 1.1.4.3 ad stat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_STATUS);
304 1.1.4.2 ad if ((stat & NFORCE_SMB_STATUS_DONE) &&
305 1.1.4.2 ad !(stat & NFORCE_SMB_STATUS_STATUS))
306 1.1.4.2 ad return 0;
307 1.1.4.2 ad return -1;
308 1.1.4.2 ad }
309 1.1.4.2 ad
310 1.1.4.2 ad /* ARGSUSED */
311 1.1.4.2 ad static int
312 1.1.4.2 ad nfsmb_send_1(struct nfsmb_softc *sc, uint8_t val, i2c_addr_t addr, i2c_op_t op,
313 1.1.4.2 ad int flags)
314 1.1.4.2 ad {
315 1.1.4.2 ad uint8_t data;
316 1.1.4.2 ad
317 1.1.4.2 ad /* store cmd */
318 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_COMMAND, val);
319 1.1.4.2 ad
320 1.1.4.2 ad /* write smbus slave address to register */
321 1.1.4.2 ad data = addr << 1;
322 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_ADDRESS, data);
323 1.1.4.2 ad
324 1.1.4.2 ad /* write smbus protocol to register */
325 1.1.4.2 ad data = I2C_OP_READ_P(op) | NFORCE_SMB_PROTOCOL_BYTE;
326 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_PROTOCOL, data);
327 1.1.4.2 ad
328 1.1.4.2 ad return nfsmb_check_done(sc);
329 1.1.4.2 ad }
330 1.1.4.2 ad
331 1.1.4.2 ad /* ARGSUSED */
332 1.1.4.2 ad static int
333 1.1.4.2 ad nfsmb_write_1(struct nfsmb_softc *sc, uint8_t cmd, uint8_t val, i2c_addr_t addr,
334 1.1.4.2 ad i2c_op_t op, int flags)
335 1.1.4.2 ad {
336 1.1.4.2 ad uint8_t data;
337 1.1.4.2 ad
338 1.1.4.2 ad /* store cmd */
339 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_COMMAND, cmd);
340 1.1.4.2 ad
341 1.1.4.2 ad /* store data */
342 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_DATA, val);
343 1.1.4.2 ad
344 1.1.4.2 ad /* write smbus slave address to register */
345 1.1.4.2 ad data = addr << 1;
346 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_ADDRESS, data);
347 1.1.4.2 ad
348 1.1.4.2 ad /* write smbus protocol to register */
349 1.1.4.2 ad data = I2C_OP_READ_P(op) | NFORCE_SMB_PROTOCOL_BYTE_DATA;
350 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_PROTOCOL, data);
351 1.1.4.2 ad
352 1.1.4.2 ad return nfsmb_check_done(sc);
353 1.1.4.2 ad }
354 1.1.4.2 ad
355 1.1.4.2 ad static int
356 1.1.4.2 ad nfsmb_write_2(struct nfsmb_softc *sc, uint8_t cmd, uint16_t val,
357 1.1.4.2 ad i2c_addr_t addr, i2c_op_t op, int flags)
358 1.1.4.2 ad {
359 1.1.4.2 ad uint8_t data, low, high;
360 1.1.4.2 ad
361 1.1.4.2 ad /* store cmd */
362 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_COMMAND, cmd);
363 1.1.4.2 ad
364 1.1.4.2 ad /* store data */
365 1.1.4.2 ad low = val;
366 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_DATA, low);
367 1.1.4.2 ad high = val >> 8;
368 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_DATA, high);
369 1.1.4.2 ad
370 1.1.4.2 ad /* write smbus slave address to register */
371 1.1.4.2 ad data = addr << 1;
372 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_ADDRESS, data);
373 1.1.4.2 ad
374 1.1.4.2 ad /* write smbus protocol to register */
375 1.1.4.2 ad data = I2C_OP_READ_P(op) | NFORCE_SMB_PROTOCOL_WORD_DATA;
376 1.1.4.2 ad if (flags & I2C_F_PEC)
377 1.1.4.2 ad data |= NFORCE_SMB_PROTOCOL_PEC;
378 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_PROTOCOL, data);
379 1.1.4.2 ad
380 1.1.4.2 ad return nfsmb_check_done(sc);
381 1.1.4.2 ad }
382 1.1.4.2 ad
383 1.1.4.2 ad /* ARGSUSED */
384 1.1.4.2 ad static int
385 1.1.4.2 ad nfsmb_receive_1(struct nfsmb_softc *sc, i2c_addr_t addr, i2c_op_t op, int flags)
386 1.1.4.2 ad {
387 1.1.4.2 ad uint8_t data;
388 1.1.4.2 ad
389 1.1.4.2 ad /* write smbus slave address to register */
390 1.1.4.2 ad data = addr << 1;
391 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_ADDRESS, data);
392 1.1.4.2 ad
393 1.1.4.2 ad /* write smbus protocol to register */
394 1.1.4.2 ad data = I2C_OP_READ_P(op) | NFORCE_SMB_PROTOCOL_BYTE;
395 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_PROTOCOL, data);
396 1.1.4.2 ad
397 1.1.4.2 ad /* check for errors */
398 1.1.4.2 ad if (nfsmb_check_done(sc) < 0)
399 1.1.4.2 ad return -1;
400 1.1.4.2 ad
401 1.1.4.2 ad /* read data */
402 1.1.4.2 ad return bus_space_read_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_DATA);
403 1.1.4.2 ad }
404 1.1.4.2 ad
405 1.1.4.2 ad /* ARGSUSED */
406 1.1.4.2 ad static int
407 1.1.4.2 ad nfsmb_read_1(struct nfsmb_softc *sc, uint8_t cmd, i2c_addr_t addr, i2c_op_t op,
408 1.1.4.2 ad int flags)
409 1.1.4.2 ad {
410 1.1.4.2 ad uint8_t data;
411 1.1.4.2 ad
412 1.1.4.2 ad /* store cmd */
413 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_COMMAND, cmd);
414 1.1.4.2 ad
415 1.1.4.2 ad /* write smbus slave address to register */
416 1.1.4.2 ad data = addr << 1;
417 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_ADDRESS, data);
418 1.1.4.2 ad
419 1.1.4.2 ad /* write smbus protocol to register */
420 1.1.4.2 ad data = I2C_OP_READ_P(op) | NFORCE_SMB_PROTOCOL_BYTE_DATA;
421 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_PROTOCOL, data);
422 1.1.4.2 ad
423 1.1.4.2 ad /* check for errors */
424 1.1.4.2 ad if (nfsmb_check_done(sc) < 0)
425 1.1.4.3 ad return -1;
426 1.1.4.2 ad
427 1.1.4.2 ad /* read data */
428 1.1.4.2 ad return bus_space_read_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_DATA);
429 1.1.4.2 ad }
430 1.1.4.2 ad
431 1.1.4.2 ad static int
432 1.1.4.2 ad nfsmb_read_2(struct nfsmb_softc *sc, uint8_t cmd, i2c_addr_t addr, i2c_op_t op,
433 1.1.4.2 ad int flags)
434 1.1.4.2 ad {
435 1.1.4.2 ad uint8_t data, low, high;
436 1.1.4.2 ad
437 1.1.4.2 ad /* store cmd */
438 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_COMMAND, cmd);
439 1.1.4.2 ad
440 1.1.4.2 ad /* write smbus slave address to register */
441 1.1.4.2 ad data = addr << 1;
442 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_ADDRESS, data);
443 1.1.4.2 ad
444 1.1.4.2 ad /* write smbus protocol to register */
445 1.1.4.2 ad data = I2C_OP_READ_P(op) | NFORCE_SMB_PROTOCOL_BYTE_DATA;
446 1.1.4.2 ad if (flags & I2C_F_PEC)
447 1.1.4.2 ad data |= NFORCE_SMB_PROTOCOL_PEC;
448 1.1.4.2 ad bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_PROTOCOL, data);
449 1.1.4.2 ad
450 1.1.4.2 ad /* check for errors */
451 1.1.4.2 ad if (nfsmb_check_done(sc) < 0)
452 1.1.4.3 ad return -1;
453 1.1.4.2 ad
454 1.1.4.2 ad /* read data */
455 1.1.4.2 ad low = bus_space_read_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_DATA);
456 1.1.4.2 ad high = bus_space_read_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_DATA);
457 1.1.4.2 ad return low | high << 8;
458 1.1.4.2 ad }
459