amdpm_smbus.c revision 1.15 1 1.15 cegger /* $NetBSD: amdpm_smbus.c,v 1.15 2008/04/10 19:13:36 cegger Exp $ */
2 1.1 tls
3 1.1 tls /*
4 1.1 tls * Copyright (c) 2005 Anil Gopinath (anil_public (at) yahoo.com)
5 1.1 tls * All rights reserved.
6 1.1 tls *
7 1.1 tls * Redistribution and use in source and binary forms, with or without
8 1.1 tls * modification, are permitted provided that the following conditions
9 1.1 tls * are met:
10 1.1 tls * 1. Redistributions of source code must retain the above copyright
11 1.1 tls * notice, this list of conditions and the following disclaimer.
12 1.1 tls * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tls * notice, this list of conditions and the following disclaimer in the
14 1.1 tls * documentation and/or other materials provided with the distribution.
15 1.1 tls * 3. The name of the author may not be used to endorse or promote products
16 1.1 tls * derived from this software without specific prior written permission.
17 1.1 tls *
18 1.1 tls * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 tls * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 tls * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 tls * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 tls * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.1 tls * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.1 tls * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.1 tls * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.1 tls * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 tls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 tls * SUCH DAMAGE.
29 1.1 tls */
30 1.1 tls
31 1.1 tls /* driver for SMBUS 1.0 host controller found in the
32 1.1 tls * AMD-8111 HyperTransport I/O Hub
33 1.1 tls */
34 1.2 xtraeme #include <sys/cdefs.h>
35 1.15 cegger __KERNEL_RCSID(0, "$NetBSD: amdpm_smbus.c,v 1.15 2008/04/10 19:13:36 cegger Exp $");
36 1.2 xtraeme
37 1.1 tls #include <sys/param.h>
38 1.1 tls #include <sys/systm.h>
39 1.1 tls #include <sys/kernel.h>
40 1.1 tls #include <sys/device.h>
41 1.1 tls #include <sys/rnd.h>
42 1.14 xtraeme #include <sys/rwlock.h>
43 1.14 xtraeme
44 1.1 tls #include <dev/pci/pcireg.h>
45 1.1 tls #include <dev/pci/pcivar.h>
46 1.1 tls #include <dev/pci/pcidevs.h>
47 1.1 tls
48 1.1 tls #include <dev/i2c/i2cvar.h>
49 1.1 tls #include <dev/i2c/i2c_bitbang.h>
50 1.1 tls
51 1.1 tls #include <dev/pci/amdpmreg.h>
52 1.1 tls #include <dev/pci/amdpmvar.h>
53 1.1 tls
54 1.1 tls #include <dev/pci/amdpm_smbusreg.h>
55 1.1 tls
56 1.10 jmcneill #ifdef __i386__
57 1.10 jmcneill #include "opt_xbox.h"
58 1.10 jmcneill #endif
59 1.10 jmcneill
60 1.10 jmcneill #ifdef XBOX
61 1.10 jmcneill extern int arch_i386_is_xbox;
62 1.10 jmcneill #endif
63 1.10 jmcneill
64 1.13 xtraeme static int amdpm_smbus_acquire_bus(void *, int);
65 1.13 xtraeme static void amdpm_smbus_release_bus(void *, int);
66 1.13 xtraeme static int amdpm_smbus_exec(void *, i2c_op_t, i2c_addr_t, const void *,
67 1.13 xtraeme size_t, void *, size_t, int);
68 1.13 xtraeme static int amdpm_smbus_check_done(struct amdpm_softc *, i2c_op_t);
69 1.13 xtraeme static void amdpm_smbus_clear_gsr(struct amdpm_softc *);
70 1.13 xtraeme static uint16_t amdpm_smbus_get_gsr(struct amdpm_softc *);
71 1.13 xtraeme static int amdpm_smbus_send_1(struct amdpm_softc *, uint8_t, i2c_op_t);
72 1.13 xtraeme static int amdpm_smbus_write_1(struct amdpm_softc *, uint8_t,
73 1.13 xtraeme uint8_t, i2c_op_t);
74 1.13 xtraeme static int amdpm_smbus_receive_1(struct amdpm_softc *, i2c_op_t);
75 1.13 xtraeme static int amdpm_smbus_read_1(struct amdpm_softc *sc, uint8_t, i2c_op_t);
76 1.1 tls
77 1.12 jmcneill #ifdef XBOX
78 1.10 jmcneill static int amdpm_smbus_intr(void *);
79 1.12 jmcneill #endif
80 1.1 tls
81 1.1 tls void
82 1.1 tls amdpm_smbus_attach(struct amdpm_softc *sc)
83 1.1 tls {
84 1.1 tls struct i2cbus_attach_args iba;
85 1.12 jmcneill #ifdef XBOX
86 1.10 jmcneill pci_intr_handle_t ih;
87 1.10 jmcneill const char *intrstr;
88 1.12 jmcneill #endif
89 1.1 tls
90 1.5 jmcneill /* register with iic */
91 1.1 tls sc->sc_i2c.ic_cookie = sc;
92 1.1 tls sc->sc_i2c.ic_acquire_bus = amdpm_smbus_acquire_bus;
93 1.1 tls sc->sc_i2c.ic_release_bus = amdpm_smbus_release_bus;
94 1.1 tls sc->sc_i2c.ic_send_start = NULL;
95 1.1 tls sc->sc_i2c.ic_send_stop = NULL;
96 1.1 tls sc->sc_i2c.ic_initiate_xfer = NULL;
97 1.1 tls sc->sc_i2c.ic_read_byte = NULL;
98 1.1 tls sc->sc_i2c.ic_write_byte = NULL;
99 1.1 tls sc->sc_i2c.ic_exec = amdpm_smbus_exec;
100 1.3 xtraeme
101 1.14 xtraeme rw_init(&sc->sc_rwlock);
102 1.3 xtraeme
103 1.10 jmcneill #ifdef XBOX
104 1.10 jmcneill #define XBOX_SMBA 0x8000
105 1.10 jmcneill #define XBOX_SMSIZE 256
106 1.10 jmcneill #define XBOX_INTRLINE 12
107 1.11 jmcneill #define XBOX_REG_ACPI_PM1a_EN 0x02
108 1.11 jmcneill #define XBOX_REG_ACPI_PM1a_EN_TIMER 0x01
109 1.10 jmcneill /* XXX pci0 dev 1 function 2 "System Management" doesn't probe */
110 1.10 jmcneill if (arch_i386_is_xbox) {
111 1.11 jmcneill uint16_t val;
112 1.10 jmcneill sc->sc_pa->pa_intrline = XBOX_INTRLINE;
113 1.10 jmcneill
114 1.10 jmcneill if (bus_space_map(sc->sc_iot, XBOX_SMBA, XBOX_SMSIZE,
115 1.11 jmcneill 0, &sc->sc_sm_ioh) == 0) {
116 1.15 cegger aprint_normal_dev(&sc->sc_dev, "system management at 0x%04x\n", XBOX_SMBA);
117 1.10 jmcneill
118 1.11 jmcneill /* Disable PM ACPI timer SCI interrupt */
119 1.11 jmcneill val = bus_space_read_2(sc->sc_iot, sc->sc_sm_ioh,
120 1.11 jmcneill XBOX_REG_ACPI_PM1a_EN);
121 1.11 jmcneill bus_space_write_2(sc->sc_iot, sc->sc_sm_ioh,
122 1.11 jmcneill XBOX_REG_ACPI_PM1a_EN,
123 1.11 jmcneill val & ~XBOX_REG_ACPI_PM1a_EN_TIMER);
124 1.11 jmcneill }
125 1.10 jmcneill }
126 1.10 jmcneill
127 1.10 jmcneill if (pci_intr_map(sc->sc_pa, &ih))
128 1.15 cegger aprint_error_dev(&sc->sc_dev, "couldn't map interrupt\n");
129 1.10 jmcneill else {
130 1.10 jmcneill intrstr = pci_intr_string(sc->sc_pc, ih);
131 1.10 jmcneill sc->sc_ih = pci_intr_establish(sc->sc_pc, ih, IPL_BIO,
132 1.10 jmcneill amdpm_smbus_intr, sc);
133 1.10 jmcneill if (sc->sc_ih != NULL)
134 1.15 cegger aprint_normal_dev(&sc->sc_dev, "interrupting at %s\n",
135 1.15 cegger intrstr);
136 1.10 jmcneill }
137 1.10 jmcneill #endif
138 1.10 jmcneill
139 1.1 tls iba.iba_tag = &sc->sc_i2c;
140 1.13 xtraeme (void)config_found_ia(&sc->sc_dev, "i2cbus", &iba, iicbus_print);
141 1.1 tls }
142 1.1 tls
143 1.12 jmcneill #ifdef XBOX
144 1.1 tls static int
145 1.10 jmcneill amdpm_smbus_intr(void *cookie)
146 1.10 jmcneill {
147 1.10 jmcneill struct amdpm_softc *sc;
148 1.10 jmcneill uint32_t status;
149 1.10 jmcneill
150 1.10 jmcneill sc = (struct amdpm_softc *)cookie;
151 1.10 jmcneill
152 1.10 jmcneill if (arch_i386_is_xbox) {
153 1.10 jmcneill status = bus_space_read_4(sc->sc_iot, sc->sc_sm_ioh, 0x20);
154 1.10 jmcneill bus_space_write_4(sc->sc_iot, sc->sc_sm_ioh, 0x20, status);
155 1.10 jmcneill
156 1.10 jmcneill if (status & 2)
157 1.10 jmcneill return iic_smbus_intr(&sc->sc_i2c);
158 1.10 jmcneill }
159 1.12 jmcneill
160 1.10 jmcneill return 0;
161 1.10 jmcneill }
162 1.12 jmcneill #endif
163 1.10 jmcneill
164 1.10 jmcneill static int
165 1.7 christos amdpm_smbus_acquire_bus(void *cookie, int flags)
166 1.1 tls {
167 1.3 xtraeme struct amdpm_softc *sc = cookie;
168 1.3 xtraeme
169 1.14 xtraeme rw_enter(&sc->sc_rwlock, RW_WRITER);
170 1.14 xtraeme return 0;
171 1.1 tls }
172 1.1 tls
173 1.1 tls static void
174 1.7 christos amdpm_smbus_release_bus(void *cookie, int flags)
175 1.1 tls {
176 1.3 xtraeme struct amdpm_softc *sc = cookie;
177 1.3 xtraeme
178 1.14 xtraeme rw_exit(&sc->sc_rwlock);
179 1.1 tls }
180 1.1 tls
181 1.1 tls static int
182 1.1 tls amdpm_smbus_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *cmd,
183 1.13 xtraeme size_t cmdlen, void *vbuf, size_t buflen, int flags)
184 1.1 tls {
185 1.1 tls struct amdpm_softc *sc = (struct amdpm_softc *) cookie;
186 1.1 tls sc->sc_smbus_slaveaddr = addr;
187 1.13 xtraeme uint8_t *p = vbuf;
188 1.9 jmcneill int rv;
189 1.1 tls
190 1.1 tls if (I2C_OP_READ_P(op) && (cmdlen == 0) && (buflen == 1)) {
191 1.13 xtraeme rv = amdpm_smbus_receive_1(sc, op);
192 1.13 xtraeme if (rv == -1)
193 1.13 xtraeme return -1;
194 1.13 xtraeme *p = (uint8_t)rv;
195 1.13 xtraeme return 0;
196 1.1 tls }
197 1.1 tls
198 1.13 xtraeme if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 1)) {
199 1.13 xtraeme rv = amdpm_smbus_read_1(sc, *(const uint8_t *)cmd, op);
200 1.13 xtraeme if (rv == -1)
201 1.13 xtraeme return -1;
202 1.13 xtraeme *p = (uint8_t)rv;
203 1.13 xtraeme return 0;
204 1.1 tls }
205 1.1 tls
206 1.13 xtraeme if ((I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 1))
207 1.13 xtraeme return amdpm_smbus_send_1(sc, *(uint8_t*)vbuf, op);
208 1.1 tls
209 1.13 xtraeme if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 1))
210 1.13 xtraeme return amdpm_smbus_write_1(sc,
211 1.13 xtraeme *(const uint8_t*)cmd,
212 1.13 xtraeme *(uint8_t*)vbuf,
213 1.13 xtraeme op);
214 1.1 tls
215 1.13 xtraeme return -1;
216 1.1 tls }
217 1.1 tls
218 1.1 tls static int
219 1.10 jmcneill amdpm_smbus_check_done(struct amdpm_softc *sc, i2c_op_t op)
220 1.1 tls {
221 1.13 xtraeme int i;
222 1.13 xtraeme
223 1.1 tls for (i = 0; i < 1000; i++) {
224 1.13 xtraeme /* check gsr and wait till cycle is done */
225 1.13 xtraeme uint16_t data = amdpm_smbus_get_gsr(sc);
226 1.13 xtraeme if (data & AMDPM_8111_GSR_CYCLE_DONE)
227 1.13 xtraeme return 0;
228 1.13 xtraeme }
229 1.13 xtraeme
230 1.13 xtraeme if (!(op & I2C_F_POLL))
231 1.10 jmcneill delay(1);
232 1.13 xtraeme
233 1.13 xtraeme return -1;
234 1.1 tls }
235 1.1 tls
236 1.1 tls
237 1.1 tls static void
238 1.1 tls amdpm_smbus_clear_gsr(struct amdpm_softc *sc)
239 1.1 tls {
240 1.1 tls /* clear register */
241 1.13 xtraeme uint16_t data = 0xFFFF;
242 1.8 jmcneill int off = (sc->sc_nforce ? 0xe0 : 0);
243 1.8 jmcneill bus_space_write_2(sc->sc_iot, sc->sc_ioh,
244 1.8 jmcneill AMDPM_8111_SMBUS_STAT - off, data);
245 1.1 tls }
246 1.1 tls
247 1.13 xtraeme static uint16_t
248 1.1 tls amdpm_smbus_get_gsr(struct amdpm_softc *sc)
249 1.1 tls {
250 1.8 jmcneill int off = (sc->sc_nforce ? 0xe0 : 0);
251 1.13 xtraeme return bus_space_read_2(sc->sc_iot, sc->sc_ioh,
252 1.13 xtraeme AMDPM_8111_SMBUS_STAT - off);
253 1.1 tls }
254 1.1 tls
255 1.1 tls static int
256 1.13 xtraeme amdpm_smbus_send_1(struct amdpm_softc *sc, uint8_t val, i2c_op_t op)
257 1.1 tls {
258 1.13 xtraeme uint16_t data = 0;
259 1.8 jmcneill int off = (sc->sc_nforce ? 0xe0 : 0);
260 1.8 jmcneill
261 1.13 xtraeme /* first clear gsr */
262 1.13 xtraeme amdpm_smbus_clear_gsr(sc);
263 1.1 tls
264 1.1 tls /* write smbus slave address to register */
265 1.1 tls data = sc->sc_smbus_slaveaddr;
266 1.1 tls data <<= 1;
267 1.1 tls data |= AMDPM_8111_SMBUS_SEND;
268 1.8 jmcneill bus_space_write_1(sc->sc_iot, sc->sc_ioh,
269 1.8 jmcneill AMDPM_8111_SMBUS_HOSTADDR - off, data);
270 1.1 tls
271 1.1 tls data = val;
272 1.1 tls /* store data */
273 1.8 jmcneill bus_space_write_2(sc->sc_iot, sc->sc_ioh,
274 1.8 jmcneill AMDPM_8111_SMBUS_HOSTDATA - off, data);
275 1.1 tls /* host start */
276 1.8 jmcneill bus_space_write_2(sc->sc_iot, sc->sc_ioh,
277 1.8 jmcneill AMDPM_8111_SMBUS_CTRL - off,
278 1.8 jmcneill AMDPM_8111_SMBUS_GSR_SB);
279 1.8 jmcneill
280 1.13 xtraeme return amdpm_smbus_check_done(sc, op);
281 1.1 tls }
282 1.1 tls
283 1.1 tls
284 1.1 tls static int
285 1.13 xtraeme amdpm_smbus_write_1(struct amdpm_softc *sc, uint8_t cmd, uint8_t val,
286 1.13 xtraeme i2c_op_t op)
287 1.1 tls {
288 1.13 xtraeme uint16_t data = 0;
289 1.8 jmcneill int off = (sc->sc_nforce ? 0xe0 : 0);
290 1.8 jmcneill
291 1.13 xtraeme /* first clear gsr */
292 1.13 xtraeme amdpm_smbus_clear_gsr(sc);
293 1.1 tls
294 1.1 tls data = sc->sc_smbus_slaveaddr;
295 1.1 tls data <<= 1;
296 1.1 tls data |= AMDPM_8111_SMBUS_WRITE;
297 1.8 jmcneill bus_space_write_1(sc->sc_iot, sc->sc_ioh,
298 1.8 jmcneill AMDPM_8111_SMBUS_HOSTADDR - off, data);
299 1.1 tls
300 1.1 tls data = val;
301 1.1 tls /* store cmd */
302 1.8 jmcneill bus_space_write_1(sc->sc_iot, sc->sc_ioh,
303 1.8 jmcneill AMDPM_8111_SMBUS_HOSTCMD - off, cmd);
304 1.1 tls /* store data */
305 1.8 jmcneill bus_space_write_2(sc->sc_iot, sc->sc_ioh,
306 1.8 jmcneill AMDPM_8111_SMBUS_HOSTDATA - off, data);
307 1.1 tls /* host start */
308 1.8 jmcneill bus_space_write_2(sc->sc_iot, sc->sc_ioh,
309 1.8 jmcneill AMDPM_8111_SMBUS_CTRL - off, AMDPM_8111_SMBUS_GSR_WB);
310 1.1 tls
311 1.13 xtraeme return amdpm_smbus_check_done(sc, op);
312 1.1 tls }
313 1.1 tls
314 1.1 tls static int
315 1.10 jmcneill amdpm_smbus_receive_1(struct amdpm_softc *sc, i2c_op_t op)
316 1.1 tls {
317 1.13 xtraeme uint16_t data = 0;
318 1.8 jmcneill int off = (sc->sc_nforce ? 0xe0 : 0);
319 1.8 jmcneill
320 1.13 xtraeme /* first clear gsr */
321 1.13 xtraeme amdpm_smbus_clear_gsr(sc);
322 1.1 tls
323 1.1 tls /* write smbus slave address to register */
324 1.1 tls data = sc->sc_smbus_slaveaddr;
325 1.1 tls data <<= 1;
326 1.1 tls data |= AMDPM_8111_SMBUS_RX;
327 1.8 jmcneill bus_space_write_1(sc->sc_iot, sc->sc_ioh,
328 1.8 jmcneill AMDPM_8111_SMBUS_HOSTADDR - off, data);
329 1.1 tls
330 1.1 tls /* start smbus cycle */
331 1.8 jmcneill bus_space_write_2(sc->sc_iot, sc->sc_ioh,
332 1.8 jmcneill AMDPM_8111_SMBUS_CTRL - off, AMDPM_8111_SMBUS_GSR_RXB);
333 1.1 tls
334 1.1 tls /* check for errors */
335 1.10 jmcneill if (amdpm_smbus_check_done(sc, op) < 0)
336 1.13 xtraeme return -1;
337 1.1 tls
338 1.1 tls /* read data */
339 1.8 jmcneill data = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
340 1.8 jmcneill AMDPM_8111_SMBUS_HOSTDATA - off);
341 1.13 xtraeme uint8_t ret = (uint8_t)(data & 0x00FF);
342 1.13 xtraeme return ret;
343 1.1 tls }
344 1.1 tls
345 1.1 tls static int
346 1.13 xtraeme amdpm_smbus_read_1(struct amdpm_softc *sc, uint8_t cmd, i2c_op_t op)
347 1.8 jmcneill {
348 1.13 xtraeme uint16_t data = 0;
349 1.13 xtraeme uint8_t ret;
350 1.8 jmcneill int off = (sc->sc_nforce ? 0xe0 : 0);
351 1.8 jmcneill
352 1.13 xtraeme /* first clear gsr */
353 1.13 xtraeme amdpm_smbus_clear_gsr(sc);
354 1.1 tls
355 1.1 tls /* write smbus slave address to register */
356 1.1 tls data = sc->sc_smbus_slaveaddr;
357 1.1 tls data <<= 1;
358 1.1 tls data |= AMDPM_8111_SMBUS_READ;
359 1.8 jmcneill bus_space_write_1(sc->sc_iot, sc->sc_ioh,
360 1.8 jmcneill AMDPM_8111_SMBUS_HOSTADDR - off, data);
361 1.1 tls
362 1.1 tls /* store cmd */
363 1.8 jmcneill bus_space_write_1(sc->sc_iot, sc->sc_ioh,
364 1.8 jmcneill AMDPM_8111_SMBUS_HOSTCMD - off, cmd);
365 1.1 tls /* host start */
366 1.8 jmcneill bus_space_write_2(sc->sc_iot, sc->sc_ioh,
367 1.8 jmcneill AMDPM_8111_SMBUS_CTRL - off, AMDPM_8111_SMBUS_GSR_RB);
368 1.1 tls
369 1.1 tls /* check for errors */
370 1.10 jmcneill if (amdpm_smbus_check_done(sc, op) < 0)
371 1.13 xtraeme return -1;
372 1.1 tls
373 1.1 tls /* store data */
374 1.8 jmcneill data = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
375 1.8 jmcneill AMDPM_8111_SMBUS_HOSTDATA - off);
376 1.13 xtraeme ret = (uint8_t)(data & 0x00FF);
377 1.13 xtraeme return ret;
378 1.1 tls }
379