FreeBSD/Linux Kernel Cross Reference
sys/pci/viapm.c
1 /*-
2 * Copyright (c) 2001 Alcove - Nicolas Souchu
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_isa.h"
31
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/uio.h>
38
39 #include <machine/bus_pio.h>
40 #include <machine/bus_memio.h>
41 #include <machine/bus.h>
42 #include <machine/clock.h> /* for DELAY */
43 #include <machine/resource.h>
44 #include <sys/rman.h>
45
46 #ifdef DEV_ISA
47 #include <isa/isavar.h>
48 #include <isa/isa_common.h>
49 #endif
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pcireg.h>
52
53 #include <dev/iicbus/iiconf.h>
54 #include <dev/iicbus/iicbus.h>
55
56 #include <dev/smbus/smbconf.h>
57 #include <dev/smbus/smbus.h>
58
59 #include "iicbb_if.h"
60 #include "smbus_if.h"
61
62 #define VIAPM_DEBUG(x) if (viapm_debug) (x)
63
64 #ifdef DEBUG
65 static int viapm_debug = 1;
66 #else
67 static int viapm_debug = 0;
68 #endif
69
70 #define VIA_586B_PMU_ID 0x30401106
71 #define VIA_596A_PMU_ID 0x30501106
72 #define VIA_596B_PMU_ID 0x30511106
73 #define VIA_686A_PMU_ID 0x30571106
74 #define VIA_8233_PMU_ID 0x30741106
75 #define VIA_8233A_PMU_ID 0x31471106
76 #define VIA_8235_PMU_ID 0x31771106
77
78 #define VIAPM_INB(port) \
79 ((u_char)bus_space_read_1(viapm->st, viapm->sh, port))
80 #define VIAPM_OUTB(port,val) \
81 (bus_space_write_1(viapm->st, viapm->sh, port, (u_char)(val)))
82
83 #define VIAPM_TYP_UNKNOWN 0
84 #define VIAPM_TYP_586B_3040E 1
85 #define VIAPM_TYP_586B_3040F 2
86 #define VIAPM_TYP_596B 3
87 #define VIAPM_TYP_686A 4
88 #define VIAPM_TYP_8233 5
89
90 struct viapm_softc {
91 int type;
92 u_int32_t base;
93 bus_space_tag_t st;
94 bus_space_handle_t sh;
95 int iorid;
96 int irqrid;
97 struct resource *iores;
98 struct resource *irqres;
99 void *irqih;
100
101 device_t iicbb;
102 device_t smbus;
103 };
104
105 static devclass_t viapm_devclass;
106 static devclass_t viapropm_devclass;
107
108 /*
109 * VT82C586B definitions
110 */
111
112 #define VIAPM_586B_REVID 0x08
113
114 #define VIAPM_586B_3040E_BASE 0x20
115 #define VIAPM_586B_3040E_ACTIV 0x4 /* 16 bits */
116
117 #define VIAPM_586B_3040F_BASE 0x48
118 #define VIAPM_586B_3040F_ACTIV 0x41 /* 8 bits */
119
120 #define VIAPM_586B_OEM_REV_E 0x00
121 #define VIAPM_586B_OEM_REV_F 0x01
122 #define VIAPM_586B_PROD_REV_A 0x10
123
124 #define VIAPM_586B_BA_MASK 0x0000ff00
125
126 #define GPIO_DIR 0x40
127 #define GPIO_VAL 0x42
128 #define EXTSMI_VAL 0x44
129
130 #define VIAPM_SCL 0x02 /* GPIO1_VAL */
131 #define VIAPM_SDA 0x04 /* GPIO2_VAL */
132
133 /*
134 * VIAPRO common definitions
135 */
136
137 #define VIAPM_PRO_BA_MASK 0x0000fff0
138 #define VIAPM_PRO_SMBCTRL 0xd2
139 #define VIAPM_PRO_REVID 0xd6
140
141 /*
142 * VT82C686A definitions
143 */
144
145 #define VIAPM_PRO_BASE 0x90
146
147 #define SMBHST 0x0
148 #define SMBHSL 0x1
149 #define SMBHCTRL 0x2
150 #define SMBHCMD 0x3
151 #define SMBHADDR 0x4
152 #define SMBHDATA0 0x5
153 #define SMBHDATA1 0x6
154 #define SMBHBLOCK 0x7
155
156 #define SMBSST 0x1
157 #define SMBSCTRL 0x8
158 #define SMBSSDWCMD 0x9
159 #define SMBSEVENT 0xa
160 #define SMBSDATA 0xc
161
162 #define SMBHST_RESERVED 0xef /* reserved bits */
163 #define SMBHST_FAILED 0x10 /* failed bus transaction */
164 #define SMBHST_COLLID 0x08 /* bus collision */
165 #define SMBHST_ERROR 0x04 /* device error */
166 #define SMBHST_INTR 0x02 /* command completed */
167 #define SMBHST_BUSY 0x01 /* host busy */
168
169 #define SMBHCTRL_START 0x40 /* start command */
170 #define SMBHCTRL_PROTO 0x1c /* command protocol mask */
171 #define SMBHCTRL_QUICK 0x00
172 #define SMBHCTRL_SENDRECV 0x04
173 #define SMBHCTRL_BYTE 0x08
174 #define SMBHCTRL_WORD 0x0c
175 #define SMBHCTRL_BLOCK 0x14
176 #define SMBHCTRL_KILL 0x02 /* stop the current transaction */
177 #define SMBHCTRL_ENABLE 0x01 /* enable interrupts */
178
179 #define SMBSCTRL_ENABLE 0x01 /* enable slave */
180
181
182 /*
183 * VIA8233 definitions
184 */
185
186 #define VIAPM_8233_BASE 0xD0
187
188 static int
189 viapm_586b_probe(device_t dev)
190 {
191 struct viapm_softc *viapm = (struct viapm_softc *)device_get_softc(dev);
192 u_int32_t l;
193 u_int16_t s;
194 u_int8_t c;
195
196 switch (pci_get_devid(dev)) {
197 case VIA_586B_PMU_ID:
198
199 bzero(viapm, sizeof(struct viapm_softc));
200
201 l = pci_read_config(dev, VIAPM_586B_REVID, 1);
202 switch (l) {
203 case VIAPM_586B_OEM_REV_E:
204 viapm->type = VIAPM_TYP_586B_3040E;
205 viapm->iorid = VIAPM_586B_3040E_BASE;
206
207 /* Activate IO block access */
208 s = pci_read_config(dev, VIAPM_586B_3040E_ACTIV, 2);
209 pci_write_config(dev, VIAPM_586B_3040E_ACTIV, s | 0x1, 2);
210 break;
211
212 case VIAPM_586B_OEM_REV_F:
213 case VIAPM_586B_PROD_REV_A:
214 default:
215 viapm->type = VIAPM_TYP_586B_3040F;
216 viapm->iorid = VIAPM_586B_3040F_BASE;
217
218 /* Activate IO block access */
219 c = pci_read_config(dev, VIAPM_586B_3040F_ACTIV, 1);
220 pci_write_config(dev, VIAPM_586B_3040F_ACTIV, c | 0x80, 1);
221 break;
222 }
223
224 viapm->base = pci_read_config(dev, viapm->iorid, 4) &
225 VIAPM_586B_BA_MASK;
226
227 /*
228 * We have to set the I/O resources by hand because it is
229 * described outside the viapmope of the traditional maps
230 */
231 if (bus_set_resource(dev, SYS_RES_IOPORT, viapm->iorid,
232 viapm->base, 256)) {
233 device_printf(dev, "could not set bus resource\n");
234 return ENXIO;
235 }
236 device_set_desc(dev, "VIA VT82C586B Power Management Unit");
237 return (BUS_PROBE_DEFAULT);
238
239 default:
240 break;
241 }
242
243 return ENXIO;
244 }
245
246
247 static int
248 viapm_pro_probe(device_t dev)
249 {
250 struct viapm_softc *viapm = (struct viapm_softc *)device_get_softc(dev);
251 #ifdef VIAPM_BASE_ADDR
252 u_int32_t l;
253 #endif
254 u_int32_t base_cfgreg;
255 char *desc;
256
257 switch (pci_get_devid(dev)) {
258 case VIA_596A_PMU_ID:
259 desc = "VIA VT82C596A Power Management Unit";
260 viapm->type = VIAPM_TYP_596B;
261 base_cfgreg = VIAPM_PRO_BASE;
262 goto viapro;
263
264 case VIA_596B_PMU_ID:
265 desc = "VIA VT82C596B Power Management Unit";
266 viapm->type = VIAPM_TYP_596B;
267 base_cfgreg = VIAPM_PRO_BASE;
268 goto viapro;
269
270 case VIA_686A_PMU_ID:
271 desc = "VIA VT82C686A Power Management Unit";
272 viapm->type = VIAPM_TYP_686A;
273 base_cfgreg = VIAPM_PRO_BASE;
274 goto viapro;
275
276 case VIA_8233_PMU_ID:
277 case VIA_8233A_PMU_ID:
278 desc = "VIA VT8233 Power Management Unit";
279 viapm->type = VIAPM_TYP_UNKNOWN;
280 base_cfgreg = VIAPM_8233_BASE;
281 goto viapro;
282
283 case VIA_8235_PMU_ID:
284 desc = "VIA VT8235 Power Management Unit";
285 viapm->type = VIAPM_TYP_UNKNOWN;
286 base_cfgreg = VIAPM_8233_BASE;
287 goto viapro;
288
289 viapro:
290
291 #ifdef VIAPM_BASE_ADDR
292 /* force VIAPM I/O base address */
293
294 /* enable the SMBus controller function */
295 l = pci_read_config(dev, VIAPM_PRO_SMBCTRL, 1);
296 pci_write_config(dev, VIAPM_PRO_SMBCTRL, l | 1, 1);
297
298 /* write the base address */
299 pci_write_config(dev, base_cfgreg,
300 VIAPM_BASE_ADDR & VIAPM_PRO_BA_MASK, 4);
301 #endif
302
303 viapm->base = pci_read_config(dev, base_cfgreg, 4) & VIAPM_PRO_BA_MASK;
304
305 /*
306 * We have to set the I/O resources by hand because it is
307 * described outside the viapmope of the traditional maps
308 */
309 viapm->iorid = base_cfgreg;
310 if (bus_set_resource(dev, SYS_RES_IOPORT, viapm->iorid,
311 viapm->base, 16)) {
312 device_printf(dev, "could not set bus resource 0x%x\n",
313 viapm->base);
314 return ENXIO;
315 }
316
317 if (1 || bootverbose) {
318 device_printf(dev, "SMBus I/O base at 0x%x\n", viapm->base);
319 }
320
321 device_set_desc(dev, desc);
322 return (BUS_PROBE_DEFAULT);
323
324 default:
325 break;
326 }
327
328 return ENXIO;
329 }
330
331 static int
332 viapm_pro_attach(device_t dev)
333 {
334 struct viapm_softc *viapm = (struct viapm_softc *)device_get_softc(dev);
335 u_int32_t l;
336
337 if (!(viapm->iores = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
338 &viapm->iorid, RF_ACTIVE))) {
339 device_printf(dev, "could not allocate bus space\n");
340 goto error;
341 }
342 viapm->st = rman_get_bustag(viapm->iores);
343 viapm->sh = rman_get_bushandle(viapm->iores);
344
345 #if notyet
346 /* force irq 9 */
347 l = pci_read_config(dev, VIAPM_PRO_SMBCTRL, 1);
348 pci_write_config(dev, VIAPM_PRO_SMBCTRL, l | 0x80, 1);
349
350 viapm->irqrid = 0;
351 if (!(viapm->irqres = bus_alloc_resource(dev, SYS_RES_IRQ,
352 &viapm->irqrid, 9, 9, 1,
353 RF_SHAREABLE | RF_ACTIVE))) {
354 device_printf(dev, "could not allocate irq\n");
355 goto error;
356 }
357
358 if (bus_setup_intr(dev, viapm->irqres, INTR_TYPE_MISC,
359 (driver_intr_t *) viasmb_intr, viapm, &viapm->irqih)) {
360 device_printf(dev, "could not setup irq\n");
361 goto error;
362 }
363 #endif
364
365 if (1 | bootverbose) {
366 l = pci_read_config(dev, VIAPM_PRO_REVID, 1);
367 device_printf(dev, "SMBus revision code 0x%x\n", l);
368 }
369
370 viapm->smbus = device_add_child(dev, "smbus", -1);
371
372 /* probe and attach the smbus */
373 bus_generic_attach(dev);
374
375 /* disable slave function */
376 VIAPM_OUTB(SMBSCTRL, VIAPM_INB(SMBSCTRL) & ~SMBSCTRL_ENABLE);
377
378 /* enable the SMBus controller function */
379 l = pci_read_config(dev, VIAPM_PRO_SMBCTRL, 1);
380 pci_write_config(dev, VIAPM_PRO_SMBCTRL, l | 1, 1);
381
382 #if notyet
383 /* enable interrupts */
384 VIAPM_OUTB(SMBHCTRL, VIAPM_INB(SMBHCTRL) | SMBHCTRL_ENABLE);
385 #endif
386
387 #ifdef DEV_ISA
388 /* If this device is a PCI-ISA bridge, then attach an ISA bus. */
389 if ((pci_get_class(dev) == PCIC_BRIDGE) &&
390 (pci_get_subclass(dev) == PCIS_BRIDGE_ISA))
391 isab_attach(dev);
392 #endif
393 return 0;
394
395 error:
396 if (viapm->iores)
397 bus_release_resource(dev, SYS_RES_IOPORT, viapm->iorid, viapm->iores);
398 #if notyet
399 if (viapm->irqres)
400 bus_release_resource(dev, SYS_RES_IRQ, viapm->irqrid, viapm->irqres);
401 #endif
402
403 return ENXIO;
404 }
405
406 static int
407 viapm_586b_attach(device_t dev)
408 {
409 struct viapm_softc *viapm = (struct viapm_softc *)device_get_softc(dev);
410
411 if (!(viapm->iores = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
412 &viapm->iorid, RF_ACTIVE | RF_SHAREABLE))) {
413 device_printf(dev, "could not allocate bus resource\n");
414 return ENXIO;
415 }
416 viapm->st = rman_get_bustag(viapm->iores);
417 viapm->sh = rman_get_bushandle(viapm->iores);
418
419 VIAPM_OUTB(GPIO_DIR, VIAPM_INB(GPIO_DIR) | VIAPM_SCL | VIAPM_SDA);
420
421 /* add generic bit-banging code */
422 if (!(viapm->iicbb = device_add_child(dev, "iicbb", -1)))
423 goto error;
424
425 bus_generic_attach(dev);
426
427 return 0;
428
429 error:
430 if (viapm->iores)
431 bus_release_resource(dev, SYS_RES_IOPORT,
432 viapm->iorid, viapm->iores);
433 return ENXIO;
434 }
435
436 static int
437 viapm_586b_detach(device_t dev)
438 {
439 struct viapm_softc *viapm = (struct viapm_softc *)device_get_softc(dev);
440 int error;
441
442 bus_generic_detach(dev);
443 if (viapm->iicbb) {
444 device_delete_child(dev, viapm->iicbb);
445 }
446
447 if (viapm->iores && (error = bus_release_resource(dev, SYS_RES_IOPORT,
448 viapm->iorid, viapm->iores)))
449 return (error);
450
451 return 0;
452 }
453
454 static int
455 viapm_pro_detach(device_t dev)
456 {
457 struct viapm_softc *viapm = (struct viapm_softc *)device_get_softc(dev);
458 int error;
459
460 bus_generic_detach(dev);
461 if (viapm->smbus) {
462 device_delete_child(dev, viapm->smbus);
463 }
464
465 if ((error = bus_release_resource(dev, SYS_RES_IOPORT,
466 viapm->iorid, viapm->iores)))
467 return (error);
468
469 #if notyet
470 if ((error = bus_release_resource(dev, SYS_RES_IRQ,
471 viapm->irqrid, viapm->irqres))
472 return (error);
473 #endif
474
475 return 0;
476 }
477
478 static int
479 viabb_callback(device_t dev, int index, caddr_t *data)
480 {
481 return 0;
482 }
483
484 static void
485 viabb_setscl(device_t dev, int ctrl)
486 {
487 struct viapm_softc *viapm = device_get_softc(dev);
488 u_char val;
489
490 val = VIAPM_INB(GPIO_VAL);
491
492 if (ctrl)
493 val |= VIAPM_SCL;
494 else
495 val &= ~VIAPM_SCL;
496
497 VIAPM_OUTB(GPIO_VAL, val);
498
499 return;
500 }
501
502 static void
503 viabb_setsda(device_t dev, int data)
504 {
505 struct viapm_softc *viapm = device_get_softc(dev);
506 u_char val;
507
508 val = VIAPM_INB(GPIO_VAL);
509
510 if (data)
511 val |= VIAPM_SDA;
512 else
513 val &= ~VIAPM_SDA;
514
515 VIAPM_OUTB(GPIO_VAL, val);
516
517 return;
518 }
519
520 static int
521 viabb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
522 {
523 /* reset bus */
524 viabb_setsda(dev, 1);
525 viabb_setscl(dev, 1);
526
527 return (IIC_ENOADDR);
528 }
529
530 static int
531 viabb_getscl(device_t dev)
532 {
533 struct viapm_softc *viapm = device_get_softc(dev);
534
535 return ((VIAPM_INB(EXTSMI_VAL) & VIAPM_SCL) != 0);
536 }
537
538 static int
539 viabb_getsda(device_t dev)
540 {
541 struct viapm_softc *viapm = device_get_softc(dev);
542
543 return ((VIAPM_INB(EXTSMI_VAL) & VIAPM_SDA) != 0);
544 }
545
546 static int
547 viapm_abort(struct viapm_softc *viapm)
548 {
549 VIAPM_OUTB(SMBHCTRL, SMBHCTRL_KILL);
550 DELAY(10);
551
552 return (0);
553 }
554
555 static int
556 viapm_clear(struct viapm_softc *viapm)
557 {
558 VIAPM_OUTB(SMBHST, SMBHST_FAILED | SMBHST_COLLID |
559 SMBHST_ERROR | SMBHST_INTR);
560 DELAY(10);
561
562 return (0);
563 }
564
565 static int
566 viapm_busy(struct viapm_softc *viapm)
567 {
568 u_char sts;
569
570 sts = VIAPM_INB(SMBHST);
571
572 VIAPM_DEBUG(printf("viapm: idle? STS=0x%x\n", sts));
573
574 return (sts & SMBHST_BUSY);
575 }
576
577 /*
578 * Poll the SMBus controller
579 */
580 static int
581 viapm_wait(struct viapm_softc *viapm)
582 {
583 int count = 10000;
584 u_char sts = 0;
585 int error;
586
587 /* wait for command to complete and SMBus controller is idle */
588 while(count--) {
589 DELAY(10);
590 sts = VIAPM_INB(SMBHST);
591
592 /* check if the controller is processing a command */
593 if (!(sts & SMBHST_BUSY) && (sts & SMBHST_INTR))
594 break;
595 }
596
597 VIAPM_DEBUG(printf("viapm: SMBHST=0x%x\n", sts));
598
599 error = SMB_ENOERR;
600
601 if (!count)
602 error |= SMB_ETIMEOUT;
603
604 if (sts & SMBHST_FAILED)
605 error |= SMB_EABORT;
606
607 if (sts & SMBHST_COLLID)
608 error |= SMB_ENOACK;
609
610 if (sts & SMBHST_ERROR)
611 error |= SMB_EBUSERR;
612
613 if (error != SMB_ENOERR)
614 viapm_abort(viapm);
615
616 viapm_clear(viapm);
617
618 return (error);
619 }
620
621 static int
622 viasmb_callback(device_t dev, int index, caddr_t *data)
623 {
624 int error = 0;
625
626 switch (index) {
627 case SMB_REQUEST_BUS:
628 case SMB_RELEASE_BUS:
629 /* ok, bus allocation accepted */
630 break;
631 default:
632 error = EINVAL;
633 }
634
635 return (error);
636 }
637
638 static int
639 viasmb_quick(device_t dev, u_char slave, int how)
640 {
641 struct viapm_softc *viapm = (struct viapm_softc *)device_get_softc(dev);
642 int error;
643
644 viapm_clear(viapm);
645 if (viapm_busy(viapm))
646 return (EBUSY);
647
648 switch (how) {
649 case SMB_QWRITE:
650 VIAPM_DEBUG(printf("viapm: QWRITE to 0x%x", slave));
651 VIAPM_OUTB(SMBHADDR, slave & ~LSB);
652 break;
653 case SMB_QREAD:
654 VIAPM_DEBUG(printf("viapm: QREAD to 0x%x", slave));
655 VIAPM_OUTB(SMBHADDR, slave | LSB);
656 break;
657 default:
658 panic("%s: unknown QUICK command (%x)!", __func__, how);
659 }
660
661 VIAPM_OUTB(SMBHCTRL, SMBHCTRL_START | SMBHCTRL_QUICK);
662
663 error = viapm_wait(viapm);
664
665 return (error);
666 }
667
668 static int
669 viasmb_sendb(device_t dev, u_char slave, char byte)
670 {
671 struct viapm_softc *viapm = (struct viapm_softc *)device_get_softc(dev);
672 int error;
673
674 viapm_clear(viapm);
675 if (viapm_busy(viapm))
676 return (EBUSY);
677
678 VIAPM_OUTB(SMBHADDR, slave & ~ LSB);
679 VIAPM_OUTB(SMBHCMD, byte);
680
681 VIAPM_OUTB(SMBHCTRL, SMBHCTRL_START | SMBHCTRL_SENDRECV);
682
683 error = viapm_wait(viapm);
684
685 VIAPM_DEBUG(printf("viapm: SENDB to 0x%x, byte=0x%x, error=0x%x\n", slave, byte, error));
686
687 return (error);
688 }
689
690 static int
691 viasmb_recvb(device_t dev, u_char slave, char *byte)
692 {
693 struct viapm_softc *viapm = (struct viapm_softc *)device_get_softc(dev);
694 int error;
695
696 viapm_clear(viapm);
697 if (viapm_busy(viapm))
698 return (EBUSY);
699
700 VIAPM_OUTB(SMBHADDR, slave | LSB);
701
702 VIAPM_OUTB(SMBHCTRL, SMBHCTRL_START | SMBHCTRL_SENDRECV);
703
704 if ((error = viapm_wait(viapm)) == SMB_ENOERR)
705 *byte = VIAPM_INB(SMBHDATA0);
706
707 VIAPM_DEBUG(printf("viapm: RECVB from 0x%x, byte=0x%x, error=0x%x\n", slave, *byte, error));
708
709 return (error);
710 }
711
712 static int
713 viasmb_writeb(device_t dev, u_char slave, char cmd, char byte)
714 {
715 struct viapm_softc *viapm = (struct viapm_softc *)device_get_softc(dev);
716 int error;
717
718 viapm_clear(viapm);
719 if (viapm_busy(viapm))
720 return (EBUSY);
721
722 VIAPM_OUTB(SMBHADDR, slave & ~ LSB);
723 VIAPM_OUTB(SMBHCMD, cmd);
724 VIAPM_OUTB(SMBHDATA0, byte);
725
726 VIAPM_OUTB(SMBHCTRL, SMBHCTRL_START | SMBHCTRL_BYTE);
727
728 error = viapm_wait(viapm);
729
730 VIAPM_DEBUG(printf("viapm: WRITEB to 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, byte, error));
731
732 return (error);
733 }
734
735 static int
736 viasmb_readb(device_t dev, u_char slave, char cmd, char *byte)
737 {
738 struct viapm_softc *viapm = (struct viapm_softc *)device_get_softc(dev);
739 int error;
740
741 viapm_clear(viapm);
742 if (viapm_busy(viapm))
743 return (EBUSY);
744
745 VIAPM_OUTB(SMBHADDR, slave | LSB);
746 VIAPM_OUTB(SMBHCMD, cmd);
747
748 VIAPM_OUTB(SMBHCTRL, SMBHCTRL_START | SMBHCTRL_BYTE);
749
750 if ((error = viapm_wait(viapm)) == SMB_ENOERR)
751 *byte = VIAPM_INB(SMBHDATA0);
752
753 VIAPM_DEBUG(printf("viapm: READB from 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, *byte, error));
754
755 return (error);
756 }
757
758 static int
759 viasmb_writew(device_t dev, u_char slave, char cmd, short word)
760 {
761 struct viapm_softc *viapm = (struct viapm_softc *)device_get_softc(dev);
762 int error;
763
764 viapm_clear(viapm);
765 if (viapm_busy(viapm))
766 return (EBUSY);
767
768 VIAPM_OUTB(SMBHADDR, slave & ~ LSB);
769 VIAPM_OUTB(SMBHCMD, cmd);
770 VIAPM_OUTB(SMBHDATA0, word & 0x00ff);
771 VIAPM_OUTB(SMBHDATA1, (word & 0xff00) >> 8);
772
773 VIAPM_OUTB(SMBHCTRL, SMBHCTRL_START | SMBHCTRL_WORD);
774
775 error = viapm_wait(viapm);
776
777 VIAPM_DEBUG(printf("viapm: WRITEW to 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, word, error));
778
779 return (error);
780 }
781
782 static int
783 viasmb_readw(device_t dev, u_char slave, char cmd, short *word)
784 {
785 struct viapm_softc *viapm = (struct viapm_softc *)device_get_softc(dev);
786 int error;
787 u_char high, low;
788
789 viapm_clear(viapm);
790 if (viapm_busy(viapm))
791 return (EBUSY);
792
793 VIAPM_OUTB(SMBHADDR, slave | LSB);
794 VIAPM_OUTB(SMBHCMD, cmd);
795
796 VIAPM_OUTB(SMBHCTRL, SMBHCTRL_START | SMBHCTRL_WORD);
797
798 if ((error = viapm_wait(viapm)) == SMB_ENOERR) {
799 low = VIAPM_INB(SMBHDATA0);
800 high = VIAPM_INB(SMBHDATA1);
801
802 *word = ((high & 0xff) << 8) | (low & 0xff);
803 }
804
805 VIAPM_DEBUG(printf("viapm: READW from 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, *word, error));
806
807 return (error);
808 }
809
810 static int
811 viasmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
812 {
813 struct viapm_softc *viapm = (struct viapm_softc *)device_get_softc(dev);
814 u_char remain, len, i;
815 int error = SMB_ENOERR;
816
817 viapm_clear(viapm);
818 if (viapm_busy(viapm))
819 return (EBUSY);
820
821 remain = count;
822 while (remain) {
823 len = min(remain, 32);
824
825 VIAPM_OUTB(SMBHADDR, slave & ~LSB);
826 VIAPM_OUTB(SMBHCMD, cmd);
827 VIAPM_OUTB(SMBHDATA0, len);
828 i = VIAPM_INB(SMBHCTRL);
829
830 /* fill the 32-byte internal buffer */
831 for (i=0; i<len; i++) {
832 VIAPM_OUTB(SMBHBLOCK, buf[count-remain+i]);
833 DELAY(2);
834 }
835 VIAPM_OUTB(SMBHCMD, cmd);
836 VIAPM_OUTB(SMBHCTRL, SMBHCTRL_START | SMBHCTRL_BLOCK);
837
838 if ((error = viapm_wait(viapm)) != SMB_ENOERR)
839 goto error;
840
841 remain -= len;
842 }
843
844 error:
845 VIAPM_DEBUG(printf("viapm: WRITEBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
846
847 return (error);
848
849 }
850
851 static int
852 viasmb_bread(device_t dev, u_char slave, char cmd, u_char count, char *buf)
853 {
854 struct viapm_softc *viapm = (struct viapm_softc *)device_get_softc(dev);
855 u_char remain, len, i;
856 int error = SMB_ENOERR;
857
858 viapm_clear(viapm);
859 if (viapm_busy(viapm))
860 return (EBUSY);
861
862 remain = count;
863 while (remain) {
864 VIAPM_OUTB(SMBHADDR, slave | LSB);
865 VIAPM_OUTB(SMBHCMD, cmd);
866 VIAPM_OUTB(SMBHCTRL, SMBHCTRL_START | SMBHCTRL_BLOCK);
867
868 if ((error = viapm_wait(viapm)) != SMB_ENOERR)
869 goto error;
870
871 len = VIAPM_INB(SMBHDATA0);
872 i = VIAPM_INB(SMBHCTRL); /* reset counter */
873
874 len = min(len, remain);
875
876 /* read the 32-byte internal buffer */
877 for (i=0; i<len; i++) {
878 buf[count-remain+i] = VIAPM_INB(SMBHBLOCK);
879 DELAY(2);
880 }
881
882 remain -= len;
883 }
884 error:
885 VIAPM_DEBUG(printf("viapm: READBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
886
887 return (error);
888 }
889
890 static device_method_t viapm_methods[] = {
891 /* device interface */
892 DEVMETHOD(device_probe, viapm_586b_probe),
893 DEVMETHOD(device_attach, viapm_586b_attach),
894 DEVMETHOD(device_detach, viapm_586b_detach),
895
896 /* iicbb interface */
897 DEVMETHOD(iicbb_callback, viabb_callback),
898 DEVMETHOD(iicbb_setscl, viabb_setscl),
899 DEVMETHOD(iicbb_setsda, viabb_setsda),
900 DEVMETHOD(iicbb_getscl, viabb_getscl),
901 DEVMETHOD(iicbb_getsda, viabb_getsda),
902 DEVMETHOD(iicbb_reset, viabb_reset),
903
904 /* Bus interface */
905 DEVMETHOD(bus_print_child, bus_generic_print_child),
906 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
907 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
908 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
909 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
910 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
911 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
912
913 { 0, 0 }
914 };
915
916 static driver_t viapm_driver = {
917 "viapm",
918 viapm_methods,
919 sizeof(struct viapm_softc),
920 };
921
922 static device_method_t viapropm_methods[] = {
923 /* device interface */
924 DEVMETHOD(device_probe, viapm_pro_probe),
925 DEVMETHOD(device_attach, viapm_pro_attach),
926 DEVMETHOD(device_detach, viapm_pro_detach),
927
928 /* smbus interface */
929 DEVMETHOD(smbus_callback, viasmb_callback),
930 DEVMETHOD(smbus_quick, viasmb_quick),
931 DEVMETHOD(smbus_sendb, viasmb_sendb),
932 DEVMETHOD(smbus_recvb, viasmb_recvb),
933 DEVMETHOD(smbus_writeb, viasmb_writeb),
934 DEVMETHOD(smbus_readb, viasmb_readb),
935 DEVMETHOD(smbus_writew, viasmb_writew),
936 DEVMETHOD(smbus_readw, viasmb_readw),
937 DEVMETHOD(smbus_bwrite, viasmb_bwrite),
938 DEVMETHOD(smbus_bread, viasmb_bread),
939
940 /* Bus interface */
941 DEVMETHOD(bus_print_child, bus_generic_print_child),
942 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
943 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
944 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
945 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
946 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
947 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
948
949 { 0, 0 }
950 };
951
952 static driver_t viapropm_driver = {
953 "viapropm",
954 viapropm_methods,
955 sizeof(struct viapm_softc),
956 };
957
958 DRIVER_MODULE(viapm, pci, viapm_driver, viapm_devclass, 0, 0);
959 DRIVER_MODULE(viapropm, pci, viapropm_driver, viapropm_devclass, 0, 0);
960
961 MODULE_DEPEND(viapm, pci, 1, 1, 1);
962 MODULE_DEPEND(viapropm, pci, 1, 1, 1);
963 MODULE_DEPEND(viapm, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
964 MODULE_DEPEND(viapropm, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
965 MODULE_VERSION(viapm, 1);
966
967 #ifdef DEV_ISA
968 DRIVER_MODULE(isa, viapm, isa_driver, isa_devclass, 0, 0);
969 DRIVER_MODULE(isa, viapropm, isa_driver, isa_devclass, 0, 0);
970 MODULE_DEPEND(viapm, isa, 1, 1, 1);
971 MODULE_DEPEND(viapropm, isa, 1, 1, 1);
972 #endif
Cache object: db2219cbb317e8c43e7c203687457250
|