amdpm_smbus.c revision 1.1 1 /* $NetBSD: amdpm_smbus.c,v 1.1 2006/02/19 02:24:20 tls Exp $ */
2
3 /*
4 * Copyright (c) 2005 Anil Gopinath (anil_public (at) yahoo.com)
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
17 *
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
28 * SUCH DAMAGE.
29 */
30
31 /* driver for SMBUS 1.0 host controller found in the
32 * AMD-8111 HyperTransport I/O Hub
33 */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/device.h>
38 #include <sys/rnd.h>
39 #include <dev/pci/pcireg.h>
40 #include <dev/pci/pcivar.h>
41 #include <dev/pci/pcidevs.h>
42
43 #include <dev/i2c/i2cvar.h>
44 #include <dev/i2c/i2c_bitbang.h>
45
46 #include <dev/pci/amdpmreg.h>
47 #include <dev/pci/amdpmvar.h>
48
49 #include <dev/pci/amdpm_smbusreg.h>
50
51 static int amdpm_smbus_acquire_bus(void *cookie, int flags);
52 static void amdpm_smbus_release_bus(void *cookie, int flags);
53 static int amdpm_smbus_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
54 const void *cmd, size_t cmdlen, void *vbuf,
55 size_t buflen, int flags);
56 static int amdpm_smbus_check_done(struct amdpm_softc *sc);
57 static void amdpm_smbus_clear_gsr(struct amdpm_softc *sc);
58 static u_int16_t amdpm_smbus_get_gsr(struct amdpm_softc *sc);
59 static int amdpm_smbus_send_1(struct amdpm_softc *sc, u_int8_t val);
60 static int amdpm_smbus_write_1(struct amdpm_softc *sc, u_int8_t cmd, u_int8_t data);
61 static int amdpm_smbus_receive_1(struct amdpm_softc *sc);
62 static int amdpm_smbus_read_1(struct amdpm_softc *sc, u_int8_t cmd);
63
64
65 void
66 amdpm_smbus_attach(struct amdpm_softc *sc)
67 {
68 struct i2cbus_attach_args iba;
69
70 // register with iic
71 sc->sc_i2c.ic_cookie = sc;
72 sc->sc_i2c.ic_acquire_bus = amdpm_smbus_acquire_bus;
73 sc->sc_i2c.ic_release_bus = amdpm_smbus_release_bus;
74 sc->sc_i2c.ic_send_start = NULL;
75 sc->sc_i2c.ic_send_stop = NULL;
76 sc->sc_i2c.ic_initiate_xfer = NULL;
77 sc->sc_i2c.ic_read_byte = NULL;
78 sc->sc_i2c.ic_write_byte = NULL;
79 sc->sc_i2c.ic_exec = amdpm_smbus_exec;
80
81 iba.iba_name = "iic";
82 iba.iba_tag = &sc->sc_i2c;
83 (void) config_found(&sc->sc_dev, &iba, iicbus_print);
84 }
85
86 static int
87 amdpm_smbus_acquire_bus(void *cookie, int flags)
88 {
89 return (0);
90 }
91
92 static void
93 amdpm_smbus_release_bus(void *cookie, int flags)
94 {
95 }
96
97 static int
98 amdpm_smbus_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *cmd,
99 size_t cmdlen, void *vbuf, size_t buflen, int flags)
100 {
101 struct amdpm_softc *sc = (struct amdpm_softc *) cookie;
102 sc->sc_smbus_slaveaddr = addr;
103
104 if (I2C_OP_READ_P(op) && (cmdlen == 0) && (buflen == 1)) {
105 return (amdpm_smbus_receive_1(sc));
106 }
107
108 if ( (I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 1)) {
109 return (amdpm_smbus_read_1(sc, *(const uint8_t*)cmd));
110 }
111
112 if ( (I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 1)) {
113 return (amdpm_smbus_send_1(sc, *(uint8_t*)vbuf));
114 }
115
116 if ( (I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 1)) {
117 return (amdpm_smbus_write_1(sc, *(const uint8_t*)cmd, *(uint8_t*)vbuf));
118 }
119
120 return (-1);
121 }
122
123 static int
124 amdpm_smbus_check_done(struct amdpm_softc *sc)
125 {
126 int i = 0;
127 for (i = 0; i < 1000; i++) {
128 /* check gsr and wait till cycle is done */
129 u_int16_t data = amdpm_smbus_get_gsr(sc);
130 if (data & AMDPM_8111_GSR_CYCLE_DONE) {
131 return (0);
132 }
133 delay(1);
134 }
135 return (-1);
136 }
137
138
139 static void
140 amdpm_smbus_clear_gsr(struct amdpm_softc *sc)
141 {
142 /* clear register */
143 u_int16_t data = 0xFFFF;
144 bus_space_write_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_STAT, data);
145 }
146
147 static u_int16_t
148 amdpm_smbus_get_gsr(struct amdpm_softc *sc)
149 {
150 return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_STAT));
151 }
152
153 static int
154 amdpm_smbus_send_1(struct amdpm_softc *sc, u_int8_t val)
155 {
156 /* first clear gsr */
157 amdpm_smbus_clear_gsr(sc);
158
159 /* write smbus slave address to register */
160 u_int16_t data = 0;
161 data = sc->sc_smbus_slaveaddr;
162 data <<= 1;
163 data |= AMDPM_8111_SMBUS_SEND;
164 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTADDR, data);
165
166 data = val;
167 /* store data */
168 bus_space_write_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTDATA, data);
169 /* host start */
170 bus_space_write_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_CTRL,
171 AMDPM_8111_SMBUS_GSR_SB);
172 return(amdpm_smbus_check_done(sc));
173 }
174
175
176 static int
177 amdpm_smbus_write_1(struct amdpm_softc *sc, u_int8_t cmd, u_int8_t val)
178 {
179 /* first clear gsr */
180 amdpm_smbus_clear_gsr(sc);
181
182 u_int16_t data = 0;
183 data = sc->sc_smbus_slaveaddr;
184 data <<= 1;
185 data |= AMDPM_8111_SMBUS_WRITE;
186 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTADDR, data);
187
188 data = val;
189 /* store cmd */
190 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTCMD, cmd);
191 /* store data */
192 bus_space_write_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTDATA, data);
193 /* host start */
194 bus_space_write_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_CTRL, AMDPM_8111_SMBUS_GSR_WB);
195
196 return (amdpm_smbus_check_done(sc));
197 }
198
199 static int
200 amdpm_smbus_receive_1(struct amdpm_softc *sc)
201 {
202 /* first clear gsr */
203 amdpm_smbus_clear_gsr(sc);
204
205 /* write smbus slave address to register */
206 u_int16_t data = 0;
207 data = sc->sc_smbus_slaveaddr;
208 data <<= 1;
209 data |= AMDPM_8111_SMBUS_RX;
210 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTADDR, data);
211
212 /* start smbus cycle */
213 bus_space_write_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_CTRL, AMDPM_8111_SMBUS_GSR_RXB);
214
215 /* check for errors */
216 if (amdpm_smbus_check_done(sc) < 0)
217 return (-1);
218
219 /* read data */
220 data = bus_space_read_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTDATA);
221 u_int8_t ret = (u_int8_t)(data & 0x00FF);
222 return (ret);
223 }
224
225 static int
226 amdpm_smbus_read_1(struct amdpm_softc *sc, u_int8_t cmd)
227 {
228 /* first clear gsr */
229 amdpm_smbus_clear_gsr(sc);
230
231 /* write smbus slave address to register */
232 u_int16_t data = 0;
233 data = sc->sc_smbus_slaveaddr;
234 data <<= 1;
235 data |= AMDPM_8111_SMBUS_READ;
236 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTADDR, data);
237
238 /* store cmd */
239 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTCMD, cmd);
240 /* host start */
241 bus_space_write_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_CTRL, AMDPM_8111_SMBUS_GSR_RB);
242
243 /* check for errors */
244 if (amdpm_smbus_check_done(sc) < 0)
245 return (-1);
246
247 /* store data */
248 data = bus_space_read_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTDATA);
249 u_int8_t ret = (u_int8_t)(data & 0x00FF);
250 return (ret);
251 }
252