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