1 /* $NetBSD: nfsmb.c,v 1.19 2009/05/09 04:56:39 pgoyette Exp $ */
3 * Copyright (c) 2007 KIYOHARA Takashi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: nfsmb.c,v 1.19 2009/05/09 04:56:39 pgoyette Exp $");
31 #include <sys/param.h>
32 #include <sys/device.h>
33 #include <sys/errno.h>
34 #include <sys/kernel.h>
35 #include <sys/rwlock.h>
40 #include <dev/i2c/i2cvar.h>
42 #include <dev/pci/pcivar.h>
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcidevs.h>
46 #include <dev/pci/nfsmbreg.h>
49 struct nfsmbc_attach_args
{
51 bus_space_tag_t nfsmb_iot
;
59 pci_chipset_tag_t sc_pc
;
61 struct pci_attach_args
*sc_pa
;
63 bus_space_tag_t sc_iot
;
72 bus_space_tag_t sc_iot
;
73 bus_space_handle_t sc_ioh
;
75 struct i2c_controller sc_i2c
; /* i2c controller info */
80 static int nfsmbc_match(device_t
, cfdata_t
, void *);
81 static void nfsmbc_attach(device_t
, device_t
, void *);
82 static int nfsmbc_print(void *, const char *);
84 static int nfsmb_match(device_t
, cfdata_t
, void *);
85 static void nfsmb_attach(device_t
, device_t
, void *);
86 static int nfsmb_acquire_bus(void *, int);
87 static void nfsmb_release_bus(void *, int);
88 static int nfsmb_exec(
89 void *, i2c_op_t
, i2c_addr_t
, const void *, size_t, void *, size_t, int);
90 static int nfsmb_check_done(struct nfsmb_softc
*);
92 nfsmb_send_1(struct nfsmb_softc
*, uint8_t, i2c_addr_t
, i2c_op_t
, int);
93 static int nfsmb_write_1(
94 struct nfsmb_softc
*, uint8_t, uint8_t, i2c_addr_t
, i2c_op_t
, int);
95 static int nfsmb_write_2(
96 struct nfsmb_softc
*, uint8_t, uint16_t, i2c_addr_t
, i2c_op_t
, int);
97 static int nfsmb_receive_1(struct nfsmb_softc
*, i2c_addr_t
, i2c_op_t
, int);
99 nfsmb_read_1(struct nfsmb_softc
*, uint8_t, i2c_addr_t
, i2c_op_t
, int);
101 nfsmb_read_2(struct nfsmb_softc
*, uint8_t, i2c_addr_t
, i2c_op_t
, int);
103 nfsmb_quick(struct nfsmb_softc
*, i2c_addr_t
, i2c_op_t
, int);
105 CFATTACH_DECL_NEW(nfsmbc
, sizeof(struct nfsmbc_softc
),
106 nfsmbc_match
, nfsmbc_attach
, NULL
, NULL
);
109 nfsmbc_match(device_t parent
, cfdata_t match
, void *aux
)
111 struct pci_attach_args
*pa
= aux
;
113 if (PCI_VENDOR(pa
->pa_id
) == PCI_VENDOR_NVIDIA
) {
114 switch (PCI_PRODUCT(pa
->pa_id
)) {
115 case PCI_PRODUCT_NVIDIA_NFORCE2_SMBUS
:
116 case PCI_PRODUCT_NVIDIA_NFORCE2_400_SMBUS
:
117 case PCI_PRODUCT_NVIDIA_NFORCE3_SMBUS
:
118 case PCI_PRODUCT_NVIDIA_NFORCE3_250_SMBUS
:
119 case PCI_PRODUCT_NVIDIA_NFORCE4_SMBUS
:
120 case PCI_PRODUCT_NVIDIA_NFORCE430_SMBUS
:
121 case PCI_PRODUCT_NVIDIA_MCP04_SMBUS
:
122 case PCI_PRODUCT_NVIDIA_MCP55_SMB
:
123 case PCI_PRODUCT_NVIDIA_MCP61_SMB
:
124 case PCI_PRODUCT_NVIDIA_MCP65_SMB
:
125 case PCI_PRODUCT_NVIDIA_MCP67_SMB
:
126 case PCI_PRODUCT_NVIDIA_MCP73_SMB
:
135 nfsmbc_attach(device_t parent
, device_t self
, void *aux
)
137 struct nfsmbc_softc
*sc
= device_private(self
);
138 struct pci_attach_args
*pa
= aux
;
139 struct nfsmbc_attach_args nfsmbca
;
145 pci_devinfo(pa
->pa_id
, pa
->pa_class
, 0, devinfo
, sizeof(devinfo
));
146 aprint_normal(": %s (rev. 0x%02x)\n", devinfo
,
147 PCI_REVISION(pa
->pa_class
));
150 sc
->sc_pc
= pa
->pa_pc
;
151 sc
->sc_tag
= pa
->pa_tag
;
153 sc
->sc_iot
= pa
->pa_iot
;
155 nfsmbca
.nfsmb_iot
= sc
->sc_iot
;
157 switch (PCI_PRODUCT(pa
->pa_id
)) {
158 case PCI_PRODUCT_NVIDIA_NFORCE2_SMBUS
:
159 case PCI_PRODUCT_NVIDIA_NFORCE2_400_SMBUS
:
160 case PCI_PRODUCT_NVIDIA_NFORCE3_SMBUS
:
161 case PCI_PRODUCT_NVIDIA_NFORCE3_250_SMBUS
:
162 case PCI_PRODUCT_NVIDIA_NFORCE4_SMBUS
:
163 baseregs
[0] = NFORCE_OLD_SMB1
;
164 baseregs
[1] = NFORCE_OLD_SMB2
;
167 baseregs
[0] = NFORCE_SMB1
;
168 baseregs
[1] = NFORCE_SMB2
;
172 reg
= pci_conf_read(pa
->pa_pc
, pa
->pa_tag
, baseregs
[0]);
173 nfsmbca
.nfsmb_num
= 1;
174 nfsmbca
.nfsmb_addr
= NFORCE_SMBBASE(reg
);
175 sc
->sc_nfsmb
[0] = config_found(sc
->sc_dev
, &nfsmbca
, nfsmbc_print
);
177 reg
= pci_conf_read(pa
->pa_pc
, pa
->pa_tag
, baseregs
[1]);
178 nfsmbca
.nfsmb_num
= 2;
179 nfsmbca
.nfsmb_addr
= NFORCE_SMBBASE(reg
);
180 sc
->sc_nfsmb
[1] = config_found(sc
->sc_dev
, &nfsmbca
, nfsmbc_print
);
182 /* This driver is similar to an ISA bridge that doesn't
183 * need any special handling. So registering NULL handlers
185 if (!pmf_device_register(self
, NULL
, NULL
))
186 aprint_error_dev(self
, "couldn't establish power handler\n");
190 nfsmbc_print(void *aux
, const char *pnp
)
192 struct nfsmbc_attach_args
*nfsmbcap
= aux
;
195 aprint_normal("nfsmb SMBus %d at %s",
196 nfsmbcap
->nfsmb_num
, pnp
);
198 aprint_normal(" SMBus %d", nfsmbcap
->nfsmb_num
);
203 CFATTACH_DECL_NEW(nfsmb
, sizeof(struct nfsmb_softc
),
204 nfsmb_match
, nfsmb_attach
, NULL
, NULL
);
207 nfsmb_match(device_t parent
, cfdata_t match
, void *aux
)
209 struct nfsmbc_attach_args
*nfsmbcap
= aux
;
211 if (nfsmbcap
->nfsmb_num
== 1 || nfsmbcap
->nfsmb_num
== 2)
217 nfsmb_attach(device_t parent
, device_t self
, void *aux
)
219 struct nfsmb_softc
*sc
= device_private(self
);
220 struct nfsmbc_attach_args
*nfsmbcap
= aux
;
221 struct i2cbus_attach_args iba
;
227 sc
->sc_nfsmbc
= parent
;
228 sc
->sc_num
= nfsmbcap
->nfsmb_num
;
229 sc
->sc_iot
= nfsmbcap
->nfsmb_iot
;
231 /* register with iic */
232 sc
->sc_i2c
.ic_cookie
= sc
;
233 sc
->sc_i2c
.ic_acquire_bus
= nfsmb_acquire_bus
;
234 sc
->sc_i2c
.ic_release_bus
= nfsmb_release_bus
;
235 sc
->sc_i2c
.ic_send_start
= NULL
;
236 sc
->sc_i2c
.ic_send_stop
= NULL
;
237 sc
->sc_i2c
.ic_initiate_xfer
= NULL
;
238 sc
->sc_i2c
.ic_read_byte
= NULL
;
239 sc
->sc_i2c
.ic_write_byte
= NULL
;
240 sc
->sc_i2c
.ic_exec
= nfsmb_exec
;
242 rw_init(&sc
->sc_rwlock
);
244 if (bus_space_map(sc
->sc_iot
, nfsmbcap
->nfsmb_addr
, NFORCE_SMBSIZE
, 0,
246 aprint_error_dev(self
, "failed to map SMBus space\n");
250 iba
.iba_type
= I2C_TYPE_SMBUS
;
251 iba
.iba_tag
= &sc
->sc_i2c
;
252 (void) config_found_ia(sc
->sc_dev
, "i2cbus", &iba
, iicbus_print
);
254 /* This driver is similar to an ISA bridge that doesn't
255 * need any special handling. So registering NULL handlers
257 if (!pmf_device_register(self
, NULL
, NULL
))
258 aprint_error_dev(self
, "couldn't establish power handler\n");
262 nfsmb_acquire_bus(void *cookie
, int flags
)
264 struct nfsmb_softc
*sc
= cookie
;
266 rw_enter(&sc
->sc_rwlock
, RW_WRITER
);
271 nfsmb_release_bus(void *cookie
, int flags
)
273 struct nfsmb_softc
*sc
= cookie
;
275 rw_exit(&sc
->sc_rwlock
);
279 nfsmb_exec(void *cookie
, i2c_op_t op
, i2c_addr_t addr
, const void *cmd
,
280 size_t cmdlen
, void *vbuf
, size_t buflen
, int flags
)
282 struct nfsmb_softc
*sc
= (struct nfsmb_softc
*)cookie
;
286 if ((cmdlen
== 0) && (buflen
== 0)) {
287 return nfsmb_quick(sc
, addr
, op
, flags
);
290 if (I2C_OP_READ_P(op
) && (cmdlen
== 0) && (buflen
== 1)) {
291 rv
= nfsmb_receive_1(sc
, addr
, op
, flags
);
298 if ((I2C_OP_READ_P(op
)) && (cmdlen
== 1) && (buflen
== 1)) {
299 rv
= nfsmb_read_1(sc
, *(const uint8_t*)cmd
, addr
, op
, flags
);
306 if ((I2C_OP_READ_P(op
)) && (cmdlen
== 1) && (buflen
== 2)) {
307 rv
= nfsmb_read_2(sc
, *(const uint8_t*)cmd
, addr
, op
, flags
);
310 *(uint16_t *)p
= (uint16_t)rv
;
314 if ((I2C_OP_WRITE_P(op
)) && (cmdlen
== 0) && (buflen
== 1))
315 return nfsmb_send_1(sc
, *(uint8_t*)vbuf
, addr
, op
, flags
);
317 if ((I2C_OP_WRITE_P(op
)) && (cmdlen
== 1) && (buflen
== 1))
318 return nfsmb_write_1(sc
, *(const uint8_t*)cmd
, *(uint8_t*)vbuf
,
321 if ((I2C_OP_WRITE_P(op
)) && (cmdlen
== 1) && (buflen
== 2))
322 return nfsmb_write_2(sc
,
323 *(const uint8_t*)cmd
, *((uint16_t *)vbuf
), addr
, op
, flags
);
329 nfsmb_check_done(struct nfsmb_softc
*sc
)
334 us
= 10 * 1000; /* XXXX: wait maximum 10 msec */
340 } while (bus_space_read_1(sc
->sc_iot
, sc
->sc_ioh
,
341 NFORCE_SMB_PROTOCOL
) != 0);
343 stat
= bus_space_read_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_STATUS
);
344 if ((stat
& NFORCE_SMB_STATUS_DONE
) &&
345 !(stat
& NFORCE_SMB_STATUS_STATUS
))
352 nfsmb_quick(struct nfsmb_softc
*sc
, i2c_addr_t addr
, i2c_op_t op
, int flags
)
356 /* write smbus slave address to register */
358 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_ADDRESS
, data
);
360 /* write smbus protocol to register */
361 data
= I2C_OP_READ_P(op
) | NFORCE_SMB_PROTOCOL_QUICK
;
362 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_PROTOCOL
, data
);
364 return nfsmb_check_done(sc
);
369 nfsmb_send_1(struct nfsmb_softc
*sc
, uint8_t val
, i2c_addr_t addr
, i2c_op_t op
,
375 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_COMMAND
, val
);
377 /* write smbus slave address to register */
379 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_ADDRESS
, data
);
381 /* write smbus protocol to register */
382 data
= I2C_OP_READ_P(op
) | NFORCE_SMB_PROTOCOL_BYTE
;
383 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_PROTOCOL
, data
);
385 return nfsmb_check_done(sc
);
390 nfsmb_write_1(struct nfsmb_softc
*sc
, uint8_t cmd
, uint8_t val
, i2c_addr_t addr
,
391 i2c_op_t op
, int flags
)
396 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_COMMAND
, cmd
);
399 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_DATA
, val
);
401 /* write smbus slave address to register */
403 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_ADDRESS
, data
);
405 /* write smbus protocol to register */
406 data
= I2C_OP_READ_P(op
) | NFORCE_SMB_PROTOCOL_BYTE_DATA
;
407 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_PROTOCOL
, data
);
409 return nfsmb_check_done(sc
);
413 nfsmb_write_2(struct nfsmb_softc
*sc
, uint8_t cmd
, uint16_t val
,
414 i2c_addr_t addr
, i2c_op_t op
, int flags
)
416 uint8_t data
, low
, high
;
419 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_COMMAND
, cmd
);
423 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_DATA
, low
);
425 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_DATA
+ 1, high
);
427 /* write smbus slave address to register */
429 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_ADDRESS
, data
);
431 /* write smbus protocol to register */
432 data
= I2C_OP_READ_P(op
) | NFORCE_SMB_PROTOCOL_WORD_DATA
;
433 if (flags
& I2C_F_PEC
)
434 data
|= NFORCE_SMB_PROTOCOL_PEC
;
435 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_PROTOCOL
, data
);
437 return nfsmb_check_done(sc
);
442 nfsmb_receive_1(struct nfsmb_softc
*sc
, i2c_addr_t addr
, i2c_op_t op
, int flags
)
446 /* write smbus slave address to register */
448 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_ADDRESS
, data
);
450 /* write smbus protocol to register */
451 data
= I2C_OP_READ_P(op
) | NFORCE_SMB_PROTOCOL_BYTE
;
452 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_PROTOCOL
, data
);
454 /* check for errors */
455 if (nfsmb_check_done(sc
) < 0)
459 return bus_space_read_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_DATA
);
464 nfsmb_read_1(struct nfsmb_softc
*sc
, uint8_t cmd
, i2c_addr_t addr
, i2c_op_t op
,
470 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_COMMAND
, cmd
);
472 /* write smbus slave address to register */
474 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_ADDRESS
, data
);
476 /* write smbus protocol to register */
477 data
= I2C_OP_READ_P(op
) | NFORCE_SMB_PROTOCOL_BYTE_DATA
;
478 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_PROTOCOL
, data
);
480 /* check for errors */
481 if (nfsmb_check_done(sc
) < 0)
485 return bus_space_read_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_DATA
);
489 nfsmb_read_2(struct nfsmb_softc
*sc
, uint8_t cmd
, i2c_addr_t addr
, i2c_op_t op
,
492 uint8_t data
, low
, high
;
495 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_COMMAND
, cmd
);
497 /* write smbus slave address to register */
499 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_ADDRESS
, data
);
501 /* write smbus protocol to register */
502 data
= I2C_OP_READ_P(op
) | NFORCE_SMB_PROTOCOL_WORD_DATA
;
503 if (flags
& I2C_F_PEC
)
504 data
|= NFORCE_SMB_PROTOCOL_PEC
;
505 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_PROTOCOL
, data
);
507 /* check for errors */
508 if (nfsmb_check_done(sc
) < 0)
512 low
= bus_space_read_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_DATA
);
513 high
= bus_space_read_1(sc
->sc_iot
, sc
->sc_ioh
, NFORCE_SMB_DATA
+ 1);
514 return low
| high
<< 8;