1 /* $NetBSD: amdpm_smbus.c,v 1.15 2008/04/10 19:13:36 cegger Exp $ */
4 * Copyright (c) 2005 Anil Gopinath (anil_public@yahoo.com)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 /* driver for SMBUS 1.0 host controller found in the
32 * AMD-8111 HyperTransport I/O Hub
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: amdpm_smbus.c,v 1.15 2008/04/10 19:13:36 cegger Exp $");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
42 #include <sys/rwlock.h>
44 #include <dev/pci/pcireg.h>
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcidevs.h>
48 #include <dev/i2c/i2cvar.h>
49 #include <dev/i2c/i2c_bitbang.h>
51 #include <dev/pci/amdpmreg.h>
52 #include <dev/pci/amdpmvar.h>
54 #include <dev/pci/amdpm_smbusreg.h>
61 extern int arch_i386_is_xbox
;
64 static int amdpm_smbus_acquire_bus(void *, int);
65 static void amdpm_smbus_release_bus(void *, int);
66 static int amdpm_smbus_exec(void *, i2c_op_t
, i2c_addr_t
, const void *,
67 size_t, void *, size_t, int);
68 static int amdpm_smbus_check_done(struct amdpm_softc
*, i2c_op_t
);
69 static void amdpm_smbus_clear_gsr(struct amdpm_softc
*);
70 static uint16_t amdpm_smbus_get_gsr(struct amdpm_softc
*);
71 static int amdpm_smbus_quick(struct amdpm_softc
*, i2c_op_t
);
72 static int amdpm_smbus_send_1(struct amdpm_softc
*, uint8_t, i2c_op_t
);
73 static int amdpm_smbus_write_1(struct amdpm_softc
*, uint8_t,
75 static int amdpm_smbus_receive_1(struct amdpm_softc
*, i2c_op_t
);
76 static int amdpm_smbus_read_1(struct amdpm_softc
*sc
, uint8_t, i2c_op_t
);
79 static int amdpm_smbus_intr(void *);
83 amdpm_smbus_attach(struct amdpm_softc
*sc
)
85 struct i2cbus_attach_args iba
;
91 /* register with iic */
92 sc
->sc_i2c
.ic_cookie
= sc
;
93 sc
->sc_i2c
.ic_acquire_bus
= amdpm_smbus_acquire_bus
;
94 sc
->sc_i2c
.ic_release_bus
= amdpm_smbus_release_bus
;
95 sc
->sc_i2c
.ic_send_start
= NULL
;
96 sc
->sc_i2c
.ic_send_stop
= NULL
;
97 sc
->sc_i2c
.ic_initiate_xfer
= NULL
;
98 sc
->sc_i2c
.ic_read_byte
= NULL
;
99 sc
->sc_i2c
.ic_write_byte
= NULL
;
100 sc
->sc_i2c
.ic_exec
= amdpm_smbus_exec
;
102 rw_init(&sc
->sc_rwlock
);
105 #define XBOX_SMBA 0x8000
106 #define XBOX_SMSIZE 256
107 #define XBOX_INTRLINE 12
108 #define XBOX_REG_ACPI_PM1a_EN 0x02
109 #define XBOX_REG_ACPI_PM1a_EN_TIMER 0x01
110 /* XXX pci0 dev 1 function 2 "System Management" doesn't probe */
111 if (arch_i386_is_xbox
) {
113 sc
->sc_pa
->pa_intrline
= XBOX_INTRLINE
;
115 if (bus_space_map(sc
->sc_iot
, XBOX_SMBA
, XBOX_SMSIZE
,
116 0, &sc
->sc_sm_ioh
) == 0) {
117 aprint_normal_dev(&sc
->sc_dev
, "system management at 0x%04x\n", XBOX_SMBA
);
119 /* Disable PM ACPI timer SCI interrupt */
120 val
= bus_space_read_2(sc
->sc_iot
, sc
->sc_sm_ioh
,
121 XBOX_REG_ACPI_PM1a_EN
);
122 bus_space_write_2(sc
->sc_iot
, sc
->sc_sm_ioh
,
123 XBOX_REG_ACPI_PM1a_EN
,
124 val
& ~XBOX_REG_ACPI_PM1a_EN_TIMER
);
128 if (pci_intr_map(sc
->sc_pa
, &ih
))
129 aprint_error_dev(&sc
->sc_dev
, "couldn't map interrupt\n");
131 intrstr
= pci_intr_string(sc
->sc_pc
, ih
);
132 sc
->sc_ih
= pci_intr_establish(sc
->sc_pc
, ih
, IPL_BIO
,
133 amdpm_smbus_intr
, sc
);
134 if (sc
->sc_ih
!= NULL
)
135 aprint_normal_dev(&sc
->sc_dev
, "interrupting at %s\n",
140 iba
.iba_tag
= &sc
->sc_i2c
;
141 (void)config_found_ia(&sc
->sc_dev
, "i2cbus", &iba
, iicbus_print
);
146 amdpm_smbus_intr(void *cookie
)
148 struct amdpm_softc
*sc
;
151 sc
= (struct amdpm_softc
*)cookie
;
153 if (arch_i386_is_xbox
) {
154 status
= bus_space_read_4(sc
->sc_iot
, sc
->sc_sm_ioh
, 0x20);
155 bus_space_write_4(sc
->sc_iot
, sc
->sc_sm_ioh
, 0x20, status
);
158 return iic_smbus_intr(&sc
->sc_i2c
);
166 amdpm_smbus_acquire_bus(void *cookie
, int flags
)
168 struct amdpm_softc
*sc
= cookie
;
170 rw_enter(&sc
->sc_rwlock
, RW_WRITER
);
175 amdpm_smbus_release_bus(void *cookie
, int flags
)
177 struct amdpm_softc
*sc
= cookie
;
179 rw_exit(&sc
->sc_rwlock
);
183 amdpm_smbus_exec(void *cookie
, i2c_op_t op
, i2c_addr_t addr
, const void *cmd
,
184 size_t cmdlen
, void *vbuf
, size_t buflen
, int flags
)
186 struct amdpm_softc
*sc
= (struct amdpm_softc
*) cookie
;
187 sc
->sc_smbus_slaveaddr
= addr
;
191 if ((cmdlen
== 0) && (buflen
== 0))
192 return amdpm_smbus_quick(sc
, op
);
194 if (I2C_OP_READ_P(op
) && (cmdlen
== 0) && (buflen
== 1)) {
195 rv
= amdpm_smbus_receive_1(sc
, op
);
202 if ((I2C_OP_READ_P(op
)) && (cmdlen
== 1) && (buflen
== 1)) {
203 rv
= amdpm_smbus_read_1(sc
, *(const uint8_t *)cmd
, op
);
210 if ((I2C_OP_WRITE_P(op
)) && (cmdlen
== 0) && (buflen
== 1))
211 return amdpm_smbus_send_1(sc
, *(uint8_t*)vbuf
, op
);
213 if ((I2C_OP_WRITE_P(op
)) && (cmdlen
== 1) && (buflen
== 1))
214 return amdpm_smbus_write_1(sc
,
215 *(const uint8_t*)cmd
,
223 amdpm_smbus_check_done(struct amdpm_softc
*sc
, i2c_op_t op
)
227 for (i
= 0; i
< 1000; i
++) {
228 /* check gsr and wait till cycle is done */
229 uint16_t data
= amdpm_smbus_get_gsr(sc
);
230 if (data
& AMDPM_8111_GSR_CYCLE_DONE
)
234 if (!(op
& I2C_F_POLL
))
242 amdpm_smbus_clear_gsr(struct amdpm_softc
*sc
)
245 uint16_t data
= 0xFFFF;
246 int off
= (sc
->sc_nforce
? 0xe0 : 0);
247 bus_space_write_2(sc
->sc_iot
, sc
->sc_ioh
,
248 AMDPM_8111_SMBUS_STAT
- off
, data
);
252 amdpm_smbus_get_gsr(struct amdpm_softc
*sc
)
254 int off
= (sc
->sc_nforce
? 0xe0 : 0);
255 return bus_space_read_2(sc
->sc_iot
, sc
->sc_ioh
,
256 AMDPM_8111_SMBUS_STAT
- off
);
260 amdpm_smbus_quick(struct amdpm_softc
*sc
, i2c_op_t op
)
263 int off
= (sc
->sc_nforce
? 0xe0 : 0);
265 /* first clear gsr */
266 amdpm_smbus_clear_gsr(sc
);
268 /* write smbus slave address and read/write bit to register */
269 data
= sc
->sc_smbus_slaveaddr
;
271 if (I2C_OP_READ_P(op
))
272 data
|= AMDPM_8111_SMBUS_READ
;
274 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
,
275 AMDPM_8111_SMBUS_HOSTADDR
- off
, data
);
278 bus_space_write_2(sc
->sc_iot
, sc
->sc_ioh
,
279 AMDPM_8111_SMBUS_CTRL
- off
,
280 AMDPM_8111_SMBUS_GSR_QUICK
);
282 return amdpm_smbus_check_done(sc
, op
);
286 amdpm_smbus_send_1(struct amdpm_softc
*sc
, uint8_t val
, i2c_op_t op
)
289 int off
= (sc
->sc_nforce
? 0xe0 : 0);
291 /* first clear gsr */
292 amdpm_smbus_clear_gsr(sc
);
294 /* write smbus slave address to register */
295 data
= sc
->sc_smbus_slaveaddr
;
297 data
|= AMDPM_8111_SMBUS_SEND
;
298 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
,
299 AMDPM_8111_SMBUS_HOSTADDR
- off
, data
);
303 bus_space_write_2(sc
->sc_iot
, sc
->sc_ioh
,
304 AMDPM_8111_SMBUS_HOSTDATA
- off
, data
);
306 bus_space_write_2(sc
->sc_iot
, sc
->sc_ioh
,
307 AMDPM_8111_SMBUS_CTRL
- off
,
308 AMDPM_8111_SMBUS_GSR_SB
);
310 return amdpm_smbus_check_done(sc
, op
);
315 amdpm_smbus_write_1(struct amdpm_softc
*sc
, uint8_t cmd
, uint8_t val
,
319 int off
= (sc
->sc_nforce
? 0xe0 : 0);
321 /* first clear gsr */
322 amdpm_smbus_clear_gsr(sc
);
324 data
= sc
->sc_smbus_slaveaddr
;
326 data
|= AMDPM_8111_SMBUS_WRITE
;
327 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
,
328 AMDPM_8111_SMBUS_HOSTADDR
- off
, data
);
332 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
,
333 AMDPM_8111_SMBUS_HOSTCMD
- off
, cmd
);
335 bus_space_write_2(sc
->sc_iot
, sc
->sc_ioh
,
336 AMDPM_8111_SMBUS_HOSTDATA
- off
, data
);
338 bus_space_write_2(sc
->sc_iot
, sc
->sc_ioh
,
339 AMDPM_8111_SMBUS_CTRL
- off
, AMDPM_8111_SMBUS_GSR_WB
);
341 return amdpm_smbus_check_done(sc
, op
);
345 amdpm_smbus_receive_1(struct amdpm_softc
*sc
, i2c_op_t op
)
348 int off
= (sc
->sc_nforce
? 0xe0 : 0);
350 /* first clear gsr */
351 amdpm_smbus_clear_gsr(sc
);
353 /* write smbus slave address to register */
354 data
= sc
->sc_smbus_slaveaddr
;
356 data
|= AMDPM_8111_SMBUS_RX
;
357 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
,
358 AMDPM_8111_SMBUS_HOSTADDR
- off
, data
);
360 /* start smbus cycle */
361 bus_space_write_2(sc
->sc_iot
, sc
->sc_ioh
,
362 AMDPM_8111_SMBUS_CTRL
- off
, AMDPM_8111_SMBUS_GSR_RXB
);
364 /* check for errors */
365 if (amdpm_smbus_check_done(sc
, op
) < 0)
369 data
= bus_space_read_2(sc
->sc_iot
, sc
->sc_ioh
,
370 AMDPM_8111_SMBUS_HOSTDATA
- off
);
371 uint8_t ret
= (uint8_t)(data
& 0x00FF);
376 amdpm_smbus_read_1(struct amdpm_softc
*sc
, uint8_t cmd
, i2c_op_t op
)
380 int off
= (sc
->sc_nforce
? 0xe0 : 0);
382 /* first clear gsr */
383 amdpm_smbus_clear_gsr(sc
);
385 /* write smbus slave address to register */
386 data
= sc
->sc_smbus_slaveaddr
;
388 data
|= AMDPM_8111_SMBUS_READ
;
389 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
,
390 AMDPM_8111_SMBUS_HOSTADDR
- off
, data
);
393 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
,
394 AMDPM_8111_SMBUS_HOSTCMD
- off
, cmd
);
396 bus_space_write_2(sc
->sc_iot
, sc
->sc_ioh
,
397 AMDPM_8111_SMBUS_CTRL
- off
, AMDPM_8111_SMBUS_GSR_RB
);
399 /* check for errors */
400 if (amdpm_smbus_check_done(sc
, op
) < 0)
404 data
= bus_space_read_2(sc
->sc_iot
, sc
->sc_ioh
,
405 AMDPM_8111_SMBUS_HOSTDATA
- off
);
406 ret
= (uint8_t)(data
& 0x00FF);