ausmbus_psc.c revision 1.11 1 1.11 kiyohara /* $NetBSD: ausmbus_psc.c,v 1.11 2012/01/03 07:36:02 kiyohara Exp $ */
2 1.1 shige
3 1.1 shige /*-
4 1.1 shige * Copyright (c) 2006 Shigeyuki Fukushima.
5 1.1 shige * All rights reserved.
6 1.1 shige *
7 1.1 shige * Written by Shigeyuki Fukushima.
8 1.1 shige *
9 1.1 shige * Redistribution and use in source and binary forms, with or without
10 1.1 shige * modification, are permitted provided that the following conditions
11 1.1 shige * are met:
12 1.1 shige * 1. Redistributions of source code must retain the above copyright
13 1.1 shige * notice, this list of conditions and the following disclaimer.
14 1.1 shige * 2. Redistributions in binary form must reproduce the above
15 1.1 shige * copyright notice, this list of conditions and the following
16 1.1 shige * disclaimer in the documentation and/or other materials provided
17 1.1 shige * with the distribution.
18 1.1 shige * 3. The name of the author may not be used to endorse or promote
19 1.1 shige * products derived from this software without specific prior
20 1.1 shige * written permission.
21 1.1 shige *
22 1.1 shige * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
23 1.1 shige * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 1.1 shige * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 shige * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
26 1.1 shige * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 shige * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28 1.1 shige * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.1 shige * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 1.1 shige * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 1.1 shige * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 1.1 shige * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 shige */
34 1.1 shige
35 1.1 shige #include <sys/cdefs.h>
36 1.11 kiyohara __KERNEL_RCSID(0, "$NetBSD: ausmbus_psc.c,v 1.11 2012/01/03 07:36:02 kiyohara Exp $");
37 1.1 shige
38 1.1 shige #include "locators.h"
39 1.1 shige
40 1.1 shige #include <sys/param.h>
41 1.1 shige #include <sys/systm.h>
42 1.1 shige #include <sys/device.h>
43 1.1 shige #include <sys/errno.h>
44 1.1 shige
45 1.10 dyoung #include <sys/bus.h>
46 1.1 shige #include <machine/cpu.h>
47 1.1 shige
48 1.1 shige #include <mips/alchemy/dev/aupscreg.h>
49 1.1 shige #include <mips/alchemy/dev/aupscvar.h>
50 1.3 shige #include <mips/alchemy/dev/ausmbus_pscreg.h>
51 1.1 shige
52 1.1 shige #include <dev/i2c/i2cvar.h>
53 1.1 shige #include <dev/i2c/i2c_bitbang.h>
54 1.1 shige
55 1.1 shige struct ausmbus_softc {
56 1.11 kiyohara device_t sc_dev;
57 1.1 shige
58 1.1 shige /* protocol comoon fields */
59 1.1 shige struct aupsc_controller sc_ctrl;
60 1.1 shige
61 1.1 shige /* protocol specific fields */
62 1.1 shige struct i2c_controller sc_i2c;
63 1.1 shige i2c_addr_t sc_smbus_slave_addr;
64 1.1 shige int sc_smbus_timeout;
65 1.1 shige };
66 1.1 shige
67 1.1 shige #define ausmbus_reg_read(sc, reg) \
68 1.1 shige bus_space_read_4(sc->sc_ctrl.psc_bust, sc->sc_ctrl.psc_bush, reg)
69 1.1 shige #define ausmbus_reg_write(sc, reg, val) \
70 1.4 shige bus_space_write_4(sc->sc_ctrl.psc_bust, sc->sc_ctrl.psc_bush, reg, \
71 1.4 shige val); \
72 1.4 shige delay(100);
73 1.1 shige
74 1.11 kiyohara static int ausmbus_match(device_t, struct cfdata *, void *);
75 1.11 kiyohara static void ausmbus_attach(device_t, device_t, void *);
76 1.1 shige
77 1.11 kiyohara CFATTACH_DECL_NEW(ausmbus, sizeof(struct ausmbus_softc),
78 1.1 shige ausmbus_match, ausmbus_attach, NULL, NULL);
79 1.1 shige
80 1.1 shige /* fuctions for i2c_controller */
81 1.1 shige static int ausmbus_acquire_bus(void *, int);
82 1.1 shige static void ausmbus_release_bus(void *, int);
83 1.1 shige static int ausmbus_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
84 1.1 shige const void *cmd, size_t cmdlen, void *vbuf,
85 1.1 shige size_t buflen, int flags);
86 1.1 shige
87 1.1 shige /* subroutine functions for i2c_controller */
88 1.9 pgoyette static int ausmbus_quick_write(struct ausmbus_softc *);
89 1.9 pgoyette static int ausmbus_quick_read(struct ausmbus_softc *);
90 1.1 shige static int ausmbus_receive_1(struct ausmbus_softc *, uint8_t *);
91 1.1 shige static int ausmbus_read_1(struct ausmbus_softc *, uint8_t, uint8_t *);
92 1.5 kiyohara static int ausmbus_read_2(struct ausmbus_softc *, uint8_t, uint16_t *);
93 1.1 shige static int ausmbus_send_1(struct ausmbus_softc *, uint8_t);
94 1.1 shige static int ausmbus_write_1(struct ausmbus_softc *, uint8_t, uint8_t);
95 1.5 kiyohara static int ausmbus_write_2(struct ausmbus_softc *, uint8_t, uint16_t);
96 1.1 shige static int ausmbus_wait_mastertx(struct ausmbus_softc *sc);
97 1.1 shige static int ausmbus_wait_masterrx(struct ausmbus_softc *sc);
98 1.1 shige static int ausmbus_initiate_xfer(void *, i2c_addr_t, int);
99 1.1 shige static int ausmbus_read_byte(void *arg, uint8_t *vp, int flags);
100 1.1 shige static int ausmbus_write_byte(void *arg, uint8_t v, int flags);
101 1.1 shige
102 1.1 shige
103 1.1 shige static int
104 1.11 kiyohara ausmbus_match(device_t parent, struct cfdata *cf, void *aux)
105 1.1 shige {
106 1.1 shige struct aupsc_attach_args *aa = (struct aupsc_attach_args *)aux;
107 1.1 shige
108 1.1 shige if (strcmp(aa->aupsc_name, cf->cf_name) != 0)
109 1.1 shige return 0;
110 1.1 shige
111 1.1 shige return 1;
112 1.1 shige }
113 1.1 shige
114 1.1 shige static void
115 1.11 kiyohara ausmbus_attach(device_t parent, device_t self, void *aux)
116 1.1 shige {
117 1.11 kiyohara struct ausmbus_softc *sc = device_private(self);
118 1.1 shige struct aupsc_attach_args *aa = (struct aupsc_attach_args *)aux;
119 1.1 shige struct i2cbus_attach_args iba;
120 1.1 shige
121 1.1 shige aprint_normal(": Alchemy PSC SMBus protocol\n");
122 1.1 shige
123 1.11 kiyohara sc->sc_dev = self;
124 1.11 kiyohara
125 1.1 shige /* Initialize PSC */
126 1.1 shige sc->sc_ctrl = aa->aupsc_ctrl;
127 1.1 shige
128 1.1 shige /* Initialize i2c_controller for SMBus */
129 1.1 shige sc->sc_i2c.ic_cookie = sc;
130 1.1 shige sc->sc_i2c.ic_acquire_bus = ausmbus_acquire_bus;
131 1.1 shige sc->sc_i2c.ic_release_bus = ausmbus_release_bus;
132 1.1 shige sc->sc_i2c.ic_send_start = NULL;
133 1.1 shige sc->sc_i2c.ic_send_stop = NULL;
134 1.1 shige sc->sc_i2c.ic_initiate_xfer = NULL;
135 1.1 shige sc->sc_i2c.ic_read_byte = NULL;
136 1.1 shige sc->sc_i2c.ic_write_byte = NULL;
137 1.1 shige sc->sc_i2c.ic_exec = ausmbus_exec;
138 1.1 shige sc->sc_smbus_timeout = 10;
139 1.7 kiyohara
140 1.1 shige iba.iba_tag = &sc->sc_i2c;
141 1.11 kiyohara (void) config_found_ia(self, "i2cbus", &iba, iicbus_print);
142 1.1 shige }
143 1.1 shige
144 1.1 shige static int
145 1.1 shige ausmbus_acquire_bus(void *arg, int flags)
146 1.1 shige {
147 1.1 shige struct ausmbus_softc *sc = arg;
148 1.1 shige uint32_t v;
149 1.1 shige
150 1.1 shige /* Select SMBus Protocol & Enable PSC */
151 1.1 shige sc->sc_ctrl.psc_enable(sc, AUPSC_SEL_SMBUS);
152 1.1 shige v = ausmbus_reg_read(sc, AUPSC_SMBSTAT);
153 1.1 shige if ((v & SMBUS_STAT_SR) == 0) {
154 1.1 shige /* PSC is not ready */
155 1.1 shige return -1;
156 1.1 shige }
157 1.1 shige
158 1.1 shige /* Setup SMBus Configuration register */
159 1.1 shige v = SMBUS_CFG_DD; /* Disable DMA */
160 1.1 shige v |= SMBUS_CFG_RT_SET(SMBUS_CFG_RT_FIFO8); /* Rx FIFO 8data */
161 1.1 shige v |= SMBUS_CFG_TT_SET(SMBUS_CFG_TT_FIFO8); /* Tx FIFO 8data */
162 1.1 shige v |= SMBUS_CFG_DIV_SET(SMBUS_CFG_DIV8); /* pscn_mainclk/8 */
163 1.1 shige v &= ~SMBUS_CFG_SFM; /* Standard Mode */
164 1.1 shige ausmbus_reg_write(sc, AUPSC_SMBCFG, v);
165 1.1 shige
166 1.1 shige /* Setup SMBus Protocol Timing register */
167 1.1 shige v = SMBUS_TMR_TH_SET(SMBUS_TMR_STD_TH)
168 1.1 shige | SMBUS_TMR_PS_SET(SMBUS_TMR_STD_PS)
169 1.1 shige | SMBUS_TMR_PU_SET(SMBUS_TMR_STD_PU)
170 1.1 shige | SMBUS_TMR_SH_SET(SMBUS_TMR_STD_SH)
171 1.1 shige | SMBUS_TMR_SU_SET(SMBUS_TMR_STD_SU)
172 1.1 shige | SMBUS_TMR_CL_SET(SMBUS_TMR_STD_CL)
173 1.1 shige | SMBUS_TMR_CH_SET(SMBUS_TMR_STD_CH);
174 1.1 shige ausmbus_reg_write(sc, AUPSC_SMBTMR, v);
175 1.1 shige
176 1.1 shige /* Setup SMBus Mask register */
177 1.1 shige v = SMBUS_MSK_ALLMASK;
178 1.1 shige ausmbus_reg_write(sc, AUPSC_SMBMSK, v);
179 1.1 shige
180 1.1 shige /* SMBus Enable */
181 1.1 shige v = ausmbus_reg_read(sc, AUPSC_SMBCFG);
182 1.1 shige v |= SMBUS_CFG_DE;
183 1.1 shige ausmbus_reg_write(sc, AUPSC_SMBCFG, v);
184 1.1 shige v = ausmbus_reg_read(sc, AUPSC_SMBSTAT);
185 1.1 shige if ((v & SMBUS_STAT_SR) == 0) {
186 1.1 shige /* SMBus is not ready */
187 1.1 shige return -1;
188 1.1 shige }
189 1.1 shige
190 1.1 shige #ifdef AUSMBUS_PSC_DEBUG
191 1.1 shige aprint_normal("AuSMBus enabled.\n");
192 1.1 shige aprint_normal("AuSMBus smbconfig: 0x%08x\n",
193 1.1 shige ausmbus_reg_read(sc, AUPSC_SMBCFG));
194 1.1 shige aprint_normal("AuSMBus smbstatus: 0x%08x\n",
195 1.1 shige ausmbus_reg_read(sc, AUPSC_SMBSTAT));
196 1.1 shige aprint_normal("AuSMBus smbtmr : 0x%08x\n",
197 1.1 shige ausmbus_reg_read(sc, AUPSC_SMBTMR));
198 1.1 shige aprint_normal("AuSMBus smbmask : 0x%08x\n",
199 1.1 shige ausmbus_reg_read(sc, AUPSC_SMBMSK));
200 1.1 shige #endif
201 1.1 shige
202 1.1 shige return 0;
203 1.1 shige }
204 1.1 shige
205 1.1 shige static void
206 1.1 shige ausmbus_release_bus(void *arg, int flags)
207 1.1 shige {
208 1.1 shige struct ausmbus_softc *sc = arg;
209 1.1 shige
210 1.1 shige ausmbus_reg_write(sc, AUPSC_SMBCFG, 0);
211 1.1 shige sc->sc_ctrl.psc_disable(sc);
212 1.1 shige
213 1.1 shige return;
214 1.1 shige }
215 1.1 shige
216 1.1 shige static int
217 1.1 shige ausmbus_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
218 1.1 shige size_t cmdlen, void *vbuf, size_t buflen, int flags)
219 1.1 shige {
220 1.1 shige struct ausmbus_softc *sc = (struct ausmbus_softc *)cookie;
221 1.1 shige const uint8_t *cmd = vcmd;
222 1.1 shige
223 1.1 shige sc->sc_smbus_slave_addr = addr;
224 1.1 shige
225 1.1 shige /* Receive byte */
226 1.1 shige if ((I2C_OP_READ_P(op)) && (cmdlen == 0) && (buflen == 1)) {
227 1.5 kiyohara return ausmbus_receive_1(sc, (uint8_t *)vbuf);
228 1.1 shige }
229 1.1 shige
230 1.1 shige /* Read byte */
231 1.1 shige if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 1)) {
232 1.5 kiyohara return ausmbus_read_1(sc, *cmd, (uint8_t *)vbuf);
233 1.5 kiyohara }
234 1.5 kiyohara
235 1.5 kiyohara /* Read word */
236 1.5 kiyohara if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 2)) {
237 1.5 kiyohara return ausmbus_read_2(sc, *cmd, (uint16_t *)vbuf);
238 1.1 shige }
239 1.1 shige
240 1.9 pgoyette /* Read quick */
241 1.9 pgoyette if ((I2C_OP_READ_P(op)) && (cmdlen == 0) && (buflen == 0)) {
242 1.9 pgoyette return ausmbus_quick_read(sc);
243 1.9 pgoyette }
244 1.9 pgoyette
245 1.1 shige /* Send byte */
246 1.1 shige if ((I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 1)) {
247 1.5 kiyohara return ausmbus_send_1(sc, *((uint8_t *)vbuf));
248 1.1 shige }
249 1.1 shige
250 1.1 shige /* Write byte */
251 1.1 shige if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 1)) {
252 1.5 kiyohara return ausmbus_write_1(sc, *cmd, *((uint8_t *)vbuf));
253 1.5 kiyohara }
254 1.5 kiyohara
255 1.5 kiyohara /* Write word */
256 1.5 kiyohara if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 2)) {
257 1.5 kiyohara return ausmbus_write_2(sc, *cmd, *((uint16_t *)vbuf));
258 1.1 shige }
259 1.1 shige
260 1.9 pgoyette /* Write quick */
261 1.9 pgoyette if ((I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 0)) {
262 1.9 pgoyette return ausmbus_quick_write(sc);
263 1.9 pgoyette }
264 1.9 pgoyette
265 1.1 shige /*
266 1.7 kiyohara * XXX: TODO Please Support other protocols defined in SMBus 2.0
267 1.1 shige * - Process call
268 1.1 shige * - Block write/read
269 1.1 shige * - Clock write-block read process cal
270 1.1 shige * - SMBus host notify protocol
271 1.9 pgoyette *
272 1.9 pgoyette * - Read quick and write quick have not been tested!
273 1.1 shige */
274 1.1 shige
275 1.1 shige return -1;
276 1.1 shige }
277 1.1 shige
278 1.1 shige static int
279 1.1 shige ausmbus_receive_1(struct ausmbus_softc *sc, uint8_t *vp)
280 1.1 shige {
281 1.1 shige int error;
282 1.1 shige
283 1.1 shige error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_READ);
284 1.1 shige if (error != 0) {
285 1.1 shige return error;
286 1.1 shige }
287 1.1 shige error = ausmbus_read_byte(sc, vp, I2C_F_STOP);
288 1.1 shige if (error != 0) {
289 1.1 shige return error;
290 1.1 shige }
291 1.1 shige
292 1.1 shige return 0;
293 1.1 shige }
294 1.1 shige
295 1.1 shige static int
296 1.1 shige ausmbus_read_1(struct ausmbus_softc *sc, uint8_t cmd, uint8_t *vp)
297 1.1 shige {
298 1.1 shige int error;
299 1.1 shige
300 1.1 shige error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_WRITE);
301 1.1 shige if (error != 0) {
302 1.1 shige return error;
303 1.1 shige }
304 1.1 shige
305 1.1 shige error = ausmbus_write_byte(sc, cmd, I2C_F_READ);
306 1.1 shige if (error != 0) {
307 1.1 shige return error;
308 1.1 shige }
309 1.1 shige
310 1.1 shige error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_READ);
311 1.1 shige if (error != 0) {
312 1.1 shige return error;
313 1.1 shige }
314 1.1 shige
315 1.1 shige error = ausmbus_read_byte(sc, vp, I2C_F_STOP);
316 1.1 shige if (error != 0) {
317 1.1 shige return error;
318 1.1 shige }
319 1.1 shige
320 1.1 shige return 0;
321 1.1 shige }
322 1.1 shige
323 1.1 shige static int
324 1.5 kiyohara ausmbus_read_2(struct ausmbus_softc *sc, uint8_t cmd, uint16_t *vp)
325 1.5 kiyohara {
326 1.5 kiyohara int error;
327 1.5 kiyohara uint8_t high, low;
328 1.5 kiyohara
329 1.5 kiyohara error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_WRITE);
330 1.5 kiyohara if (error != 0) {
331 1.5 kiyohara return error;
332 1.5 kiyohara }
333 1.5 kiyohara
334 1.5 kiyohara error = ausmbus_write_byte(sc, cmd, I2C_F_READ);
335 1.5 kiyohara if (error != 0) {
336 1.5 kiyohara return error;
337 1.5 kiyohara }
338 1.5 kiyohara
339 1.5 kiyohara error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_READ);
340 1.5 kiyohara if (error != 0) {
341 1.5 kiyohara return error;
342 1.5 kiyohara }
343 1.5 kiyohara
344 1.5 kiyohara error = ausmbus_read_byte(sc, &low, 0);
345 1.5 kiyohara if (error != 0) {
346 1.5 kiyohara return error;
347 1.5 kiyohara }
348 1.5 kiyohara
349 1.5 kiyohara error = ausmbus_read_byte(sc, &high, I2C_F_STOP);
350 1.5 kiyohara if (error != 0) {
351 1.5 kiyohara return error;
352 1.5 kiyohara }
353 1.5 kiyohara
354 1.5 kiyohara *vp = (high << 8) | low;
355 1.5 kiyohara
356 1.5 kiyohara return 0;
357 1.5 kiyohara }
358 1.5 kiyohara
359 1.5 kiyohara static int
360 1.1 shige ausmbus_send_1(struct ausmbus_softc *sc, uint8_t val)
361 1.1 shige {
362 1.1 shige int error;
363 1.1 shige
364 1.1 shige error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_WRITE);
365 1.1 shige if (error != 0) {
366 1.1 shige return error;
367 1.1 shige }
368 1.1 shige
369 1.1 shige error = ausmbus_write_byte(sc, val, I2C_F_STOP);
370 1.1 shige if (error != 0) {
371 1.1 shige return error;
372 1.1 shige }
373 1.1 shige
374 1.1 shige return 0;
375 1.1 shige }
376 1.1 shige
377 1.1 shige static int
378 1.1 shige ausmbus_write_1(struct ausmbus_softc *sc, uint8_t cmd, uint8_t val)
379 1.1 shige {
380 1.1 shige int error;
381 1.1 shige
382 1.1 shige error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_WRITE);
383 1.1 shige if (error != 0) {
384 1.1 shige return error;
385 1.1 shige }
386 1.1 shige
387 1.1 shige error = ausmbus_write_byte(sc, cmd, 0);
388 1.1 shige if (error != 0) {
389 1.1 shige return error;
390 1.1 shige }
391 1.1 shige
392 1.1 shige error = ausmbus_write_byte(sc, val, I2C_F_STOP);
393 1.1 shige if (error != 0) {
394 1.1 shige return error;
395 1.1 shige }
396 1.1 shige
397 1.1 shige return 0;
398 1.1 shige }
399 1.1 shige
400 1.1 shige static int
401 1.5 kiyohara ausmbus_write_2(struct ausmbus_softc *sc, uint8_t cmd, uint16_t val)
402 1.5 kiyohara {
403 1.5 kiyohara int error;
404 1.5 kiyohara uint8_t high, low;
405 1.5 kiyohara
406 1.5 kiyohara high = (val >> 8) & 0xff;
407 1.5 kiyohara low = val & 0xff;
408 1.5 kiyohara
409 1.5 kiyohara error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_WRITE);
410 1.5 kiyohara if (error != 0) {
411 1.5 kiyohara return error;
412 1.5 kiyohara }
413 1.5 kiyohara
414 1.5 kiyohara error = ausmbus_write_byte(sc, cmd, 0);
415 1.5 kiyohara if (error != 0) {
416 1.5 kiyohara return error;
417 1.5 kiyohara }
418 1.5 kiyohara
419 1.5 kiyohara error = ausmbus_write_byte(sc, low, 0);
420 1.5 kiyohara if (error != 0) {
421 1.5 kiyohara return error;
422 1.5 kiyohara }
423 1.5 kiyohara
424 1.5 kiyohara error = ausmbus_write_byte(sc, high, I2C_F_STOP);
425 1.5 kiyohara if (error != 0) {
426 1.5 kiyohara return error;
427 1.5 kiyohara }
428 1.5 kiyohara
429 1.5 kiyohara return 0;
430 1.5 kiyohara }
431 1.5 kiyohara
432 1.9 pgoyette /*
433 1.9 pgoyette * XXX The quick_write() and quick_read() routines have not been tested!
434 1.9 pgoyette */
435 1.9 pgoyette static int
436 1.9 pgoyette ausmbus_quick_write(struct ausmbus_softc *sc)
437 1.9 pgoyette {
438 1.9 pgoyette return ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr,
439 1.9 pgoyette I2C_F_STOP | I2C_F_WRITE);
440 1.9 pgoyette }
441 1.9 pgoyette
442 1.9 pgoyette static int
443 1.9 pgoyette ausmbus_quick_read(struct ausmbus_softc *sc)
444 1.9 pgoyette {
445 1.9 pgoyette return ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr,
446 1.9 pgoyette I2C_F_STOP | I2C_F_READ);
447 1.9 pgoyette }
448 1.9 pgoyette
449 1.5 kiyohara static int
450 1.1 shige ausmbus_wait_mastertx(struct ausmbus_softc *sc)
451 1.1 shige {
452 1.1 shige uint32_t v;
453 1.1 shige int timeout;
454 1.1 shige int txerr = 0;
455 1.1 shige
456 1.1 shige timeout = sc->sc_smbus_timeout;
457 1.1 shige
458 1.1 shige do {
459 1.1 shige v = ausmbus_reg_read(sc, AUPSC_SMBEVNT);
460 1.1 shige #ifdef AUSMBUS_PSC_DEBUG
461 1.1 shige aprint_normal("AuSMBus: ausmbus_wait_mastertx(): psc_smbevnt=0x%08x\n", v);
462 1.1 shige #endif
463 1.1 shige if ((v & SMBUS_EVNT_TU) != 0)
464 1.1 shige break;
465 1.1 shige if ((v & SMBUS_EVNT_MD) != 0)
466 1.1 shige break;
467 1.1 shige if ((v & (SMBUS_EVNT_DN | SMBUS_EVNT_AN | SMBUS_EVNT_AL))
468 1.1 shige != 0) {
469 1.1 shige txerr = 1;
470 1.1 shige break;
471 1.1 shige }
472 1.1 shige timeout--;
473 1.1 shige delay(1);
474 1.1 shige } while (timeout > 0);
475 1.1 shige
476 1.1 shige if (txerr != 0) {
477 1.1 shige ausmbus_reg_write(sc, AUPSC_SMBEVNT,
478 1.1 shige SMBUS_EVNT_DN | SMBUS_EVNT_AN | SMBUS_EVNT_AL);
479 1.1 shige #ifdef AUSMBUS_PSC_DEBUG
480 1.1 shige aprint_normal("AuSMBus: ausmbus_wait_mastertx(): Tx error\n");
481 1.1 shige #endif
482 1.1 shige return -1;
483 1.1 shige }
484 1.1 shige
485 1.1 shige /* Reset Event TU (Tx Underflow) */
486 1.1 shige ausmbus_reg_write(sc, AUPSC_SMBEVNT, SMBUS_EVNT_TU | SMBUS_EVNT_MD);
487 1.1 shige
488 1.1 shige #ifdef AUSMBUS_PSC_DEBUG
489 1.1 shige v = ausmbus_reg_read(sc, AUPSC_SMBEVNT);
490 1.1 shige aprint_normal("AuSMBus: ausmbus_wait_mastertx(): psc_smbevnt=0x%08x (reset)\n", v);
491 1.1 shige #endif
492 1.1 shige return 0;
493 1.1 shige }
494 1.1 shige
495 1.1 shige static int
496 1.1 shige ausmbus_wait_masterrx(struct ausmbus_softc *sc)
497 1.1 shige {
498 1.1 shige uint32_t v;
499 1.1 shige int timeout;
500 1.1 shige timeout = sc->sc_smbus_timeout;
501 1.1 shige
502 1.1 shige if (ausmbus_wait_mastertx(sc) != 0)
503 1.1 shige return -1;
504 1.1 shige
505 1.1 shige do {
506 1.1 shige v = ausmbus_reg_read(sc, AUPSC_SMBSTAT);
507 1.1 shige #ifdef AUSMBUS_PSC_DEBUG
508 1.1 shige aprint_normal("AuSMBus: ausmbus_wait_masterrx(): psc_smbstat=0x%08x\n", v);
509 1.1 shige #endif
510 1.1 shige if ((v & SMBUS_STAT_RE) == 0)
511 1.1 shige break;
512 1.1 shige timeout--;
513 1.1 shige delay(1);
514 1.1 shige } while (timeout > 0);
515 1.1 shige
516 1.1 shige return 0;
517 1.1 shige }
518 1.1 shige
519 1.1 shige static int
520 1.1 shige ausmbus_initiate_xfer(void *arg, i2c_addr_t addr, int flags)
521 1.1 shige {
522 1.1 shige struct ausmbus_softc *sc = arg;
523 1.1 shige uint32_t v;
524 1.1 shige
525 1.1 shige /* Tx/Rx Slave Address */
526 1.1 shige v = (addr << 1) & SMBUS_TXRX_ADDRDATA;
527 1.7 kiyohara if ((flags & I2C_F_READ) != 0)
528 1.1 shige v |= 1;
529 1.9 pgoyette if ((flags & I2C_F_STOP) != 0)
530 1.9 pgoyette v |= SMBUS_TXRX_STP;
531 1.1 shige ausmbus_reg_write(sc, AUPSC_SMBTXRX, v);
532 1.1 shige
533 1.1 shige /* Master Start */
534 1.1 shige ausmbus_reg_write(sc, AUPSC_SMBPCR, SMBUS_PCR_MS);
535 1.1 shige
536 1.1 shige if (ausmbus_wait_mastertx(sc) != 0)
537 1.1 shige return -1;
538 1.1 shige
539 1.1 shige return 0;
540 1.1 shige }
541 1.1 shige
542 1.1 shige static int
543 1.1 shige ausmbus_read_byte(void *arg, uint8_t *vp, int flags)
544 1.1 shige {
545 1.1 shige struct ausmbus_softc *sc = arg;
546 1.1 shige uint32_t v;
547 1.1 shige
548 1.1 shige if ((flags & I2C_F_STOP) != 0) {
549 1.1 shige ausmbus_reg_write(sc, AUPSC_SMBTXRX, SMBUS_TXRX_STP);
550 1.1 shige } else {
551 1.1 shige ausmbus_reg_write(sc, AUPSC_SMBTXRX, 0);
552 1.1 shige }
553 1.1 shige
554 1.1 shige if (ausmbus_wait_masterrx(sc) != 0)
555 1.1 shige return -1;
556 1.1 shige
557 1.1 shige v = ausmbus_reg_read(sc, AUPSC_SMBTXRX);
558 1.1 shige *vp = v & SMBUS_TXRX_ADDRDATA;
559 1.1 shige
560 1.1 shige return 0;
561 1.1 shige }
562 1.1 shige
563 1.1 shige static int
564 1.1 shige ausmbus_write_byte(void *arg, uint8_t v, int flags)
565 1.1 shige {
566 1.1 shige struct ausmbus_softc *sc = arg;
567 1.1 shige
568 1.1 shige if ((flags & I2C_F_STOP) != 0) {
569 1.1 shige ausmbus_reg_write(sc, AUPSC_SMBTXRX, (v | SMBUS_TXRX_STP));
570 1.1 shige } else if ((flags & I2C_F_READ) != 0) {
571 1.1 shige ausmbus_reg_write(sc, AUPSC_SMBTXRX, (v | SMBUS_TXRX_RSR));
572 1.1 shige } else {
573 1.1 shige ausmbus_reg_write(sc, AUPSC_SMBTXRX, v);
574 1.1 shige }
575 1.1 shige
576 1.1 shige if (ausmbus_wait_mastertx(sc) != 0)
577 1.1 shige return -1;
578 1.1 shige
579 1.1 shige return 0;
580 1.1 shige }
581