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