xref: /openbsd-src/sys/dev/isa/gscsio.c (revision 75a5ee09c02962dd597e7a5ab2dcce3a256f45e9)
1 /*	$OpenBSD: gscsio.c,v 1.2 2004/06/05 18:34:04 grange Exp $	*/
2 /*
3  * Copyright (c) 2004 Alexander Yurchenko <grange@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /*
19  * National Semiconductor Geode SC1100 Super I/O.
20  * Only ACCESS.bus logical device is supported.
21  */
22 
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/device.h>
26 #include <sys/kernel.h>
27 #include <sys/lock.h>
28 #include <sys/proc.h>
29 
30 #include <machine/bus.h>
31 
32 #include <dev/i2c/i2cvar.h>
33 
34 #include <dev/isa/isareg.h>
35 #include <dev/isa/isavar.h>
36 
37 #include <dev/isa/gscsioreg.h>
38 
39 struct gscsio_softc {
40 	struct device sc_dev;
41 
42 	bus_space_tag_t sc_iot;
43 	bus_space_handle_t sc_ioh;
44 
45 	int sc_ld_en[GSCSIO_LDNUM];
46 	bus_space_handle_t sc_ld_ioh0[GSCSIO_LDNUM];
47 	bus_space_handle_t sc_ld_ioh1[GSCSIO_LDNUM];
48 
49 	/* ACCESS.bus */
50 	struct gscsio_acb {
51 		void *sc;
52 		bus_space_handle_t ioh;
53 		struct lock buslock;
54 	} sc_acb[2];
55 	struct i2c_controller sc_acb1_tag;
56 	struct i2c_controller sc_acb2_tag;
57 };
58 
59 /* Supported logical devices description */
60 static const struct {
61 	const char *ld_name;
62 	int ld_num;
63 	int ld_iosize0;
64 	int ld_iosize1;
65 } gscsio_ld[] = {
66 	{ "ACB1", GSCSIO_LDN_ACB1, 6, 0 },
67 	{ "ACB2", GSCSIO_LDN_ACB2, 6, 0 },
68 };
69 
70 int	gscsio_probe(struct device *, void *, void *);
71 void	gscsio_attach(struct device *, struct device *, void *);
72 
73 void	gscsio_acb_init(struct gscsio_acb *, i2c_tag_t);
74 int	gscsio_acb_wait(struct gscsio_acb *, int, int);
75 void	gscsio_acb_reset(struct gscsio_acb *acb);
76 
77 int	gscsio_acb_acquire_bus(void *, int);
78 void	gscsio_acb_release_bus(void *, int);
79 int	gscsio_acb_send_start(void *, int);
80 int	gscsio_acb_send_stop(void *, int);
81 int	gscsio_acb_initiate_xfer(void *, uint16_t, int);
82 int	gscsio_acb_read_byte(void *, uint8_t *, int);
83 int	gscsio_acb_write_byte(void *, uint8_t, int);
84 
85 struct cfattach gscsio_ca = {
86 	sizeof(struct gscsio_softc),
87 	gscsio_probe,
88 	gscsio_attach
89 };
90 
91 struct cfdriver gscsio_cd = {
92 	NULL, "gscsio", DV_DULL
93 };
94 
95 #define ACB_READ(reg) \
96 	bus_space_read_1(sc->sc_iot, acb->ioh, (reg))
97 #define ACB_WRITE(reg, val) \
98 	bus_space_write_1(sc->sc_iot, acb->ioh, (reg), (val))
99 
100 static __inline u_int8_t
101 idxread(bus_space_tag_t iot, bus_space_handle_t ioh, int idx)
102 {
103 	bus_space_write_1(iot, ioh, GSCSIO_IDX, idx);
104 
105 	return (bus_space_read_1(iot, ioh, GSCSIO_DAT));
106 }
107 
108 static __inline void
109 idxwrite(bus_space_tag_t iot, bus_space_handle_t ioh, int idx, u_int8_t data)
110 {
111 	bus_space_write_1(iot, ioh, GSCSIO_IDX, idx);
112 	bus_space_write_1(iot, ioh, GSCSIO_DAT, data);
113 }
114 
115 static int
116 ioprobe(bus_space_tag_t iot, int base)
117 {
118 	bus_space_handle_t ioh;
119 	int rv = 0;
120 
121 	if (bus_space_map(iot, base, GSCSIO_IOSIZE, 0, &ioh))
122 		return (0);
123 	if (idxread(iot, ioh, GSCSIO_ID) == GSCSIO_ID_SC1100)
124 		rv = 1;
125 	bus_space_unmap(iot, ioh, GSCSIO_IOSIZE);
126 
127 	return (rv);
128 }
129 
130 int
131 gscsio_probe(struct device *parent, void *match, void *aux)
132 {
133 	struct isa_attach_args *ia = aux;
134 	int iobase;
135 
136 	iobase = GSCSIO_IOBASE1;
137 	if (ioprobe(ia->ia_iot, iobase))
138 		goto found;
139 	iobase = GSCSIO_IOBASE2;
140 	if (ioprobe(ia->ia_iot, iobase))
141 		goto found;
142 
143 	return (0);
144 
145 found:
146 	ia->ipa_nio = 1;
147 	ia->ipa_io[0].base = iobase;
148 	ia->ipa_io[0].length = GSCSIO_IOSIZE;
149 	ia->ipa_nmem = 0;
150 	ia->ipa_nirq = 0;
151 	ia->ipa_ndrq = 0;
152 
153 	return (1);
154 }
155 
156 void
157 gscsio_attach(struct device *parent, struct device *self, void *aux)
158 {
159 	struct gscsio_softc *sc = (void *)self;
160 	struct isa_attach_args *ia = aux;
161 	int i;
162 	int iobase;
163 
164 	sc->sc_iot = ia->ia_iot;
165 	if (bus_space_map(sc->sc_iot, ia->ipa_io[0].base, GSCSIO_IOSIZE,
166 	    0, &sc->sc_ioh)) {
167 		printf(": can't map I/O space\n");
168 		return;
169 	}
170 	printf(": SC1100 SIO rev %d:",
171 	    idxread(sc->sc_iot, sc->sc_ioh, GSCSIO_REV));
172 
173 	/* Configure all supported logical devices */
174 	for (i = 0; i < sizeof (gscsio_ld) / sizeof(gscsio_ld[0]); i++) {
175 		sc->sc_ld_en[gscsio_ld[i].ld_num] = 0;
176 
177 		/* Select the device and check if it's activated */
178 		idxwrite(sc->sc_iot, sc->sc_ioh, GSCSIO_LDN,
179 		    gscsio_ld[i].ld_num);
180 		if ((idxread(sc->sc_iot, sc->sc_ioh, GSCSIO_ACT) &
181 		    GSCSIO_ACT_EN) == 0)
182 			continue;
183 
184 		/* Map I/O space 0 if necessary */
185 		if (gscsio_ld[i].ld_iosize0 != 0) {
186 			iobase = idxread(sc->sc_iot, sc->sc_ioh,
187 			    GSCSIO_IO0_MSB);
188 			iobase <<= 8;
189 			iobase |= idxread(sc->sc_iot, sc->sc_ioh,
190 			    GSCSIO_IO0_LSB);
191 			if (bus_space_map(sc->sc_iot, iobase,
192 			    gscsio_ld[i].ld_iosize0, 0,
193 			    &sc->sc_ld_ioh0[gscsio_ld[i].ld_num]))
194 				continue;
195 		}
196 
197 		/* Map I/O space 1 if necessary */
198 		if (gscsio_ld[i].ld_iosize1 != 0) {
199 			iobase = idxread(sc->sc_iot, sc->sc_ioh,
200 			    GSCSIO_IO1_MSB);
201 			iobase <<= 8;
202 			iobase |= idxread(sc->sc_iot, sc->sc_ioh,
203 			    GSCSIO_IO1_LSB);
204 			if (bus_space_map(sc->sc_iot, iobase,
205 			    gscsio_ld[i].ld_iosize1, 0,
206 			    &sc->sc_ld_ioh0[gscsio_ld[i].ld_num])) {
207 				bus_space_unmap(sc->sc_iot,
208 				    sc->sc_ld_ioh0[gscsio_ld[i].ld_num],
209 				    gscsio_ld[i].ld_iosize0);
210 				continue;
211 			}
212 		}
213 
214 		sc->sc_ld_en[gscsio_ld[i].ld_num] = 1;
215 		printf(" %s", gscsio_ld[i].ld_name);
216 	}
217 	printf("\n");
218 
219 	/* Initialize ACCESS.bus 1 */
220 	if (sc->sc_ld_en[GSCSIO_LDN_ACB1]) {
221 		sc->sc_acb[0].sc = sc;
222 		sc->sc_acb[0].ioh = sc->sc_ld_ioh0[GSCSIO_LDN_ACB1];
223 		lockinit(&sc->sc_acb[0].buslock, PRIBIO | PCATCH,
224 		    "iiclk", 0, 0);
225 		gscsio_acb_init(&sc->sc_acb[0], &sc->sc_acb1_tag);
226 	}
227 
228 	/* Initialize ACCESS.bus 2 */
229 	if (sc->sc_ld_en[GSCSIO_LDN_ACB2]) {
230 		sc->sc_acb[1].sc = sc;
231 		sc->sc_acb[1].ioh = sc->sc_ld_ioh0[GSCSIO_LDN_ACB2];
232 		lockinit(&sc->sc_acb[1].buslock, PRIBIO | PCATCH,
233 		    "iiclk", 0, 0);
234 		gscsio_acb_init(&sc->sc_acb[1], &sc->sc_acb2_tag);
235 	}
236 }
237 
238 void
239 gscsio_acb_init(struct gscsio_acb *acb, i2c_tag_t tag)
240 {
241 	struct gscsio_softc *sc = acb->sc;
242 	struct i2cbus_attach_args iba;
243 
244 	/* Enable ACB and configure clock frequency */
245 	ACB_WRITE(GSCSIO_ACB_CTL2, GSCSIO_ACB_CTL2_EN |
246 	    (GSCSIO_ACB_FREQ << GSCSIO_ACB_CTL2_FREQ_SHIFT));
247 
248 	/* Select polling mode */
249 	ACB_WRITE(GSCSIO_ACB_CTL1, ACB_READ(GSCSIO_ACB_CTL1) &
250 	    ~GSCSIO_ACB_CTL1_INTEN);
251 
252 	/* Disable slave address */
253 	ACB_WRITE(GSCSIO_ACB_ADDR, ACB_READ(GSCSIO_ACB_ADDR) &
254 	    ~GSCSIO_ACB_ADDR_SAEN);
255 
256 	/* Attach I2C framework */
257 	tag->ic_cookie = acb;
258 	tag->ic_acquire_bus = gscsio_acb_acquire_bus;
259 	tag->ic_release_bus = gscsio_acb_release_bus;
260 	tag->ic_send_start = gscsio_acb_send_start;
261 	tag->ic_send_stop = gscsio_acb_send_stop;
262 	tag->ic_initiate_xfer = gscsio_acb_initiate_xfer;
263 	tag->ic_read_byte = gscsio_acb_read_byte;
264 	tag->ic_write_byte = gscsio_acb_write_byte;
265 
266 	iba.iba_name = "iic";
267 	iba.iba_tag = tag;
268 	config_found(&sc->sc_dev, &iba, iicbus_print);
269 }
270 
271 int
272 gscsio_acb_wait(struct gscsio_acb *acb, int bits, int flags)
273 {
274 	struct gscsio_softc *sc = acb->sc;
275 	u_int8_t st;
276 	int i;
277 
278 	for (i = 0; i < 100; i++) {
279 		st = ACB_READ(GSCSIO_ACB_ST);
280 		if (st & GSCSIO_ACB_ST_BER) {
281 			printf("%s: bus error, flags=0x%x\n",
282 			    sc->sc_dev.dv_xname, flags);
283 			gscsio_acb_reset(acb);
284 			return (EIO);
285 		}
286 		if (st & GSCSIO_ACB_ST_NEGACK) {
287 			printf("%s: negative ack, flags=0x%x\n",
288 			    sc->sc_dev.dv_xname, flags);
289 			gscsio_acb_reset(acb);
290 			return (EIO);
291 		}
292 		if ((st & bits) == bits)
293 			break;
294 		delay(10);
295 	}
296 	if ((st & bits) != bits) {
297 		printf("%s: timeout, flags=0x%x\n",
298 		    sc->sc_dev.dv_xname, flags);
299 		gscsio_acb_reset(acb);
300 		return (ETIMEDOUT);
301 	}
302 
303 	return (0);
304 }
305 
306 void
307 gscsio_acb_reset(struct gscsio_acb *acb)
308 {
309 	struct gscsio_softc *sc = acb->sc;
310 	u_int8_t st, ctl;
311 
312 	/* Clear MASTER, NEGACK and BER */
313 	st = ACB_READ(GSCSIO_ACB_ST);
314 	st |= GSCSIO_ACB_ST_MASTER | GSCSIO_ACB_ST_NEGACK | GSCSIO_ACB_ST_BER;
315 	ACB_WRITE(GSCSIO_ACB_ST, st);
316 
317 	/* Disable and re-enable ACB */
318 	ACB_WRITE(GSCSIO_ACB_CTL2, 0);
319 	ACB_WRITE(GSCSIO_ACB_CTL2, GSCSIO_ACB_CTL2_EN |
320 	    (GSCSIO_ACB_FREQ << GSCSIO_ACB_CTL2_FREQ_SHIFT));
321 
322 	/* Send stop */
323 	ctl = ACB_READ(GSCSIO_ACB_CTL1);
324 	ctl |= GSCSIO_ACB_CTL1_STOP;
325 	ACB_WRITE(GSCSIO_ACB_CTL1, ctl);
326 }
327 
328 int
329 gscsio_acb_acquire_bus(void *cookie, int flags)
330 {
331 	struct gscsio_acb *acb = cookie;
332 
333 	if (flags & I2C_F_POLL)
334 		return (0);
335 
336 	return (lockmgr(&acb->buslock, LK_EXCLUSIVE, NULL, curproc));
337 }
338 
339 void
340 gscsio_acb_release_bus(void *cookie, int flags)
341 {
342 	struct gscsio_acb *acb = cookie;
343 
344 	if (flags & I2C_F_POLL)
345 		return;
346 
347 	lockmgr(&acb->buslock, LK_RELEASE, NULL, curproc);
348 }
349 
350 int
351 gscsio_acb_send_start(void *cookie, int flags)
352 {
353 	struct gscsio_acb *acb = cookie;
354 	struct gscsio_softc *sc = acb->sc;
355 	u_int8_t ctl;
356 
357 	ctl = ACB_READ(GSCSIO_ACB_CTL1);
358 	ctl |= GSCSIO_ACB_CTL1_START;
359 	ACB_WRITE(GSCSIO_ACB_CTL1, ctl);
360 
361 	return (0);
362 }
363 
364 int
365 gscsio_acb_send_stop(void *cookie, int flags)
366 {
367 	struct gscsio_acb *acb = cookie;
368 	struct gscsio_softc *sc = acb->sc;
369 	u_int8_t ctl;
370 
371 	ctl = ACB_READ(GSCSIO_ACB_CTL1);
372 	ctl |= GSCSIO_ACB_CTL1_STOP;
373 	ACB_WRITE(GSCSIO_ACB_CTL1, ctl);
374 
375 	return (0);
376 }
377 
378 int
379 gscsio_acb_initiate_xfer(void *cookie, uint16_t addr, int flags)
380 {
381 	struct gscsio_acb *acb = cookie;
382 	struct gscsio_softc *sc = acb->sc;
383 	u_int8_t ctl;
384 	int dir;
385 	int error;
386 
387 	/* Issue start condition */
388 	ctl = ACB_READ(GSCSIO_ACB_CTL1);
389 	ctl |= GSCSIO_ACB_CTL1_START;
390 	ACB_WRITE(GSCSIO_ACB_CTL1, ctl);
391 
392 	/* Wait for bus mastership */
393 	if ((error = gscsio_acb_wait(acb,
394 	    GSCSIO_ACB_ST_MASTER | GSCSIO_ACB_ST_SDAST, flags)))
395 		return (error);
396 
397 	/* Send address byte */
398 	dir = (flags & I2C_F_READ ? 1 : 0);
399 	ACB_WRITE(GSCSIO_ACB_SDA, (addr << 1) | dir);
400 
401 	return (0);
402 }
403 
404 int
405 gscsio_acb_read_byte(void *cookie, uint8_t *bytep, int flags)
406 {
407 	struct gscsio_acb *acb = cookie;
408 	struct gscsio_softc *sc = acb->sc;
409 	u_int8_t ctl;
410 	int error;
411 
412 	/* Wait for the bus to be ready */
413 	if ((error = gscsio_acb_wait(acb, GSCSIO_ACB_ST_SDAST, flags)))
414 		return (error);
415 
416 	/* Acknowledge the last byte */
417 	if (flags & I2C_F_LAST) {
418 		ctl = ACB_READ(GSCSIO_ACB_CTL1);
419 		ctl |= GSCSIO_ACB_CTL1_ACK;
420 		ACB_WRITE(GSCSIO_ACB_CTL1, ctl);
421 	}
422 
423 	/* Read data byte */
424 	*bytep = ACB_READ(GSCSIO_ACB_SDA);
425 
426 	return (0);
427 }
428 
429 int
430 gscsio_acb_write_byte(void *cookie, uint8_t byte, int flags)
431 {
432 	struct gscsio_acb *acb = cookie;
433 	struct gscsio_softc *sc = acb->sc;
434 	u_int8_t ctl;
435 	int error;
436 
437 	/* Wait for the bus to be ready */
438 	if ((error = gscsio_acb_wait(acb, GSCSIO_ACB_ST_SDAST, flags)))
439 		return (error);
440 
441 	/* Send stop after the last byte */
442 	if (flags & I2C_F_STOP) {
443 		ctl = ACB_READ(GSCSIO_ACB_CTL1);
444 		ctl |= GSCSIO_ACB_CTL1_STOP;
445 		ACB_WRITE(GSCSIO_ACB_CTL1, ctl);
446 	}
447 
448 	/* Write data byte */
449 	ACB_WRITE(GSCSIO_ACB_SDA, byte);
450 
451 	return (0);
452 }
453