kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / dev / powermng / i386 / amdpm / amdpm.c
1 /*-
2  * Copyright (c) 2000 Matthew C. Forman
3  *
4  * Based (heavily) on alpm.c which is:
5  *
6  * Copyright (c) 1998, 1999 Nicolas Souchu
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, 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  * $FreeBSD: src/sys/pci/amdpm.c,v 1.1.2.1 2001/10/10 12:10:26 murray Exp $
31  * $DragonFly: src/sys/dev/powermng/i386/amdpm/amdpm.c,v 1.4 2003/08/07 21:17:07 dillon Exp $
32  *
33  */
34
35 /*
36  * Power management function/SMBus function support for the AMD 756 chip.
37  */
38
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/systm.h>
42 #include <sys/module.h>
43 #include <sys/bus.h>
44 #include <sys/uio.h>
45
46 #include <machine/bus_pio.h>
47 #include <machine/bus_memio.h>
48 #include <machine/bus.h>
49 #include <machine/clock.h>
50 #include <machine/resource.h>
51 #include <sys/rman.h>
52
53 #include <bus/pci/pcivar.h>
54 #include <bus/pci/pcireg.h>
55
56 #include <bus/iicbus/iiconf.h>
57 #include <bus/smbus/smbconf.h>
58 #include "smbus_if.h"
59
60 #define AMDPM_DEBUG(x)  if (amdpm_debug) (x)
61
62 #ifdef DEBUG
63 static int amdpm_debug = 1;
64 #else
65 static int amdpm_debug = 0;
66 #endif
67
68 #define AMDPM_VENDORID_AMD 0x1022
69 #define AMDPM_DEVICEID_AMD756PM 0x740b
70
71 /* PCI Configuration space registers */
72 #define AMDPCI_PMBASE 0x58
73
74 #define AMDPCI_GEN_CONFIG_PM 0x41
75 #define AMDPCI_PMIOEN (1<<7)
76
77 #define AMDPCI_SCIINT_CONFIG_PM 0x42
78 #define AMDPCI_SCISEL_IRQ11 11
79
80 #define AMDPCI_REVID 0x08
81
82 /*
83  * I/O registers.
84  * Base address programmed via AMDPCI_PMBASE.
85  */
86 #define AMDSMB_GLOBAL_STATUS 0xE0
87 #define AMDSMB_GS_TO_STS (1<<5)
88 #define AMDSMB_GS_HCYC_STS (1<<4)
89 #define AMDSMB_GS_HST_STS (1<<3)
90 #define AMDSMB_GS_PRERR_STS (1<<2)
91 #define AMDSMB_GS_COL_STS (1<<1)
92 #define AMDSMB_GS_ABRT_STS (1<<0)
93 #define AMDSMB_GS_CLEAR_STS (AMDSMB_GS_TO_STS|AMDSMB_GS_HCYC_STS|AMDSMB_GS_PRERR_STS|AMDSMB_GS_COL_STS|AMDSMB_GS_ABRT_STS)
94
95 #define AMDSMB_GLOBAL_ENABLE 0xE2
96 #define AMDSMB_GE_ABORT (1<<5)
97 #define AMDSMB_GE_HCYC_EN (1<<4)
98 #define AMDSMB_GE_HOST_STC (1<<3)
99 #define AMDSMB_GE_CYC_QUICK 0
100 #define AMDSMB_GE_CYC_BYTE 1
101 #define AMDSMB_GE_CYC_BDATA 2
102 #define AMDSMB_GE_CYC_WDATA 3
103 #define AMDSMB_GE_CYC_PROCCALL 4
104 #define AMDSMB_GE_CYC_BLOCK 5
105
106 #define AMDSMB_HSTADDR 0xE4
107 #define AMDSMB_HSTDATA 0xE6
108 #define AMDSMB_HSTCMD 0xE8
109 #define AMDSMB_HSTDFIFO 0xE9
110 #define AMDSMB_HSLVDATA 0xEA
111 #define AMDSMB_HSLVDA 0xEC
112 #define AMDSMB_HSLVDDR 0xEE
113 #define AMDSMB_SNPADDR 0xEF
114
115 struct amdpm_softc {
116         int base;
117         int rid;
118         struct resource *res;
119         bus_space_tag_t smbst;
120         bus_space_handle_t smbsh;
121 };
122
123 struct amdsmb_softc {
124         int base;
125         device_t smbus;
126         struct amdpm_softc *amdpm;
127 };
128
129 #define AMDPM_SMBINB(amdsmb,register) \
130         (bus_space_read_1(amdsmb->amdpm->smbst, amdsmb->amdpm->smbsh, register))
131 #define AMDPM_SMBOUTB(amdsmb,register,value) \
132         (bus_space_write_1(amdsmb->amdpm->smbst, amdsmb->amdpm->smbsh, register, value))
133 #define AMDPM_SMBINW(amdsmb,register) \
134         (bus_space_read_2(amdsmb->amdpm->smbst, amdsmb->amdpm->smbsh, register))
135 #define AMDPM_SMBOUTW(amdsmb,register,value) \
136         (bus_space_write_2(amdsmb->amdpm->smbst, amdsmb->amdpm->smbsh, register, value))
137
138 static int amdsmb_probe(device_t);
139 static int amdsmb_attach(device_t);
140 static int amdsmb_smb_callback(device_t, int, caddr_t *);
141 static int amdsmb_smb_quick(device_t dev, u_char slave, int how);
142 static int amdsmb_smb_sendb(device_t dev, u_char slave, char byte);
143 static int amdsmb_smb_recvb(device_t dev, u_char slave, char *byte);
144 static int amdsmb_smb_writeb(device_t dev, u_char slave, char cmd, char byte);
145 static int amdsmb_smb_readb(device_t dev, u_char slave, char cmd, char *byte);
146 static int amdsmb_smb_writew(device_t dev, u_char slave, char cmd, short word);
147 static int amdsmb_smb_readw(device_t dev, u_char slave, char cmd, short *word);
148 static int amdsmb_smb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf);
149 static int amdsmb_smb_bread(device_t dev, u_char slave, char cmd, u_char count, char *byte);
150
151 static int amdpm_probe(device_t);
152 static int amdpm_attach(device_t);
153
154
155 static int
156 amdpm_probe(device_t dev)
157 {
158         u_long base;
159         
160         if ((pci_get_vendor(dev) == AMDPM_VENDORID_AMD) &&
161             (pci_get_device(dev) == AMDPM_DEVICEID_AMD756PM)) {
162               device_set_desc(dev, "AMD 756 Power Management Controller");
163               
164               /* 
165                * We have to do this, since the BIOS won't give us the
166                * resource info (not mine, anyway).
167                */
168               base = pci_read_config(dev, AMDPCI_PMBASE, 4);
169               base &= 0xff00;
170               bus_set_resource(dev, SYS_RES_IOPORT, AMDPCI_PMBASE, base, 256);
171               return (0);
172         }
173         return ENXIO;
174 }
175
176 static int
177 amdpm_attach(device_t dev)
178 {
179         struct amdpm_softc *amdpm_sc = device_get_softc(dev);
180         u_char val_b;
181         int unit = device_get_unit(dev);
182         device_t smbinterface;
183         
184         /* Enable I/O block access */
185         val_b = pci_read_config(dev, AMDPCI_GEN_CONFIG_PM, 1);
186         pci_write_config(dev, AMDPCI_GEN_CONFIG_PM, val_b | AMDPCI_PMIOEN, 1);
187
188         /* Allocate I/O space */
189         amdpm_sc->rid = AMDPCI_PMBASE;
190         amdpm_sc->res = bus_alloc_resource(dev, SYS_RES_IOPORT, &amdpm_sc->rid, 0, ~0, 1, RF_ACTIVE);
191         
192         if (amdpm_sc->res == NULL) {
193                 device_printf(dev, "could not map i/o space\n");
194                 return (ENXIO);
195         }            
196
197         amdpm_sc->smbst = rman_get_bustag(amdpm_sc->res);
198         amdpm_sc->smbsh = rman_get_bushandle(amdpm_sc->res);
199         
200         smbinterface = device_add_child(dev, "amdsmb", unit);
201         if (!smbinterface)
202                 device_printf(dev, "could not add SMBus device\n");
203         else
204                 device_probe_and_attach(smbinterface);
205
206         return (0);
207 }
208
209 static int
210 amdsmb_probe(device_t dev)
211 {
212         struct amdsmb_softc *amdsmb_sc = (struct amdsmb_softc *)device_get_softc(dev);
213
214         /* Allocate a new smbus device */
215         amdsmb_sc->smbus = smbus_alloc_bus(dev);
216         if (!amdsmb_sc->smbus)
217                 return (EINVAL);
218
219         device_set_desc(dev, "AMD 756 SMBus interface");
220         device_printf(dev, "AMD 756 SMBus interface\n");
221
222         return (0);
223 }
224
225 static int
226 amdsmb_attach(device_t dev)
227 {
228         struct amdsmb_softc *amdsmb_sc = (struct amdsmb_softc *)device_get_softc(dev);
229
230         amdsmb_sc->amdpm = device_get_softc(device_get_parent(dev));
231         
232         /* Probe and attach the smbus */
233         device_probe_and_attach(amdsmb_sc->smbus);
234
235         return (0);
236 }
237
238 static int
239 amdsmb_smb_callback(device_t dev, int index, caddr_t *data)
240 {
241         int error = 0;
242
243         switch (index) {
244         case SMB_REQUEST_BUS:
245         case SMB_RELEASE_BUS:
246                 break;
247         default:
248                 error = EINVAL;
249         }
250
251         return (error);
252 }
253
254 static int
255 amdsmb_clear(struct amdsmb_softc *sc)
256 {
257         AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_STATUS, AMDSMB_GS_CLEAR_STS);
258         DELAY(10);
259
260         return (0);
261 }
262
263 #if 0
264 static int
265 amdsmb_abort(struct amdsmb_softc *sc)
266 {
267         u_short l;
268         
269         l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
270         AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE, l | AMDSMB_GE_ABORT);
271
272         return (0);
273 }
274 #endif
275
276 static int
277 amdsmb_idle(struct amdsmb_softc *sc)
278 {
279         u_short sts;
280
281         sts = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_STATUS);
282
283         AMDPM_DEBUG(printf("amdpm: busy? STS=0x%x\n", sts));
284
285         return (~(sts & AMDSMB_GS_HST_STS));
286 }
287
288 /*
289  * Poll the SMBus controller
290  */
291 static int
292 amdsmb_wait(struct amdsmb_softc *sc)
293 {
294         int count = 10000;
295         u_short sts = 0;
296         int error;
297
298         /* Wait for command to complete (SMBus controller is idle) */
299         while(count--) {
300                 DELAY(10);
301                 sts = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_STATUS);
302                 if (!(sts & AMDSMB_GS_HST_STS))
303                         break;
304         }
305
306         AMDPM_DEBUG(printf("amdpm: STS=0x%x (count=%d)\n", sts, count));
307
308         error = SMB_ENOERR;
309
310         if (!count)
311                 error |= SMB_ETIMEOUT;
312
313         if (sts & AMDSMB_GS_ABRT_STS)
314                 error |= SMB_EABORT;
315
316         if (sts & AMDSMB_GS_COL_STS)
317                 error |= SMB_ENOACK;
318
319         if (sts & AMDSMB_GS_PRERR_STS)
320                 error |= SMB_EBUSERR;
321
322         if (error != SMB_ENOERR)
323                 amdsmb_clear(sc);
324
325         return (error);
326 }
327
328 static int
329 amdsmb_smb_quick(device_t dev, u_char slave, int how)
330 {
331         struct amdsmb_softc *sc = (struct amdsmb_softc *)device_get_softc(dev);
332         int error;
333         u_short l;
334
335         amdsmb_clear(sc);
336         if (!amdsmb_idle(sc))
337                 return (EBUSY);
338
339         switch (how) {
340         case SMB_QWRITE:
341                 AMDPM_DEBUG(printf("amdpm: QWRITE to 0x%x", slave));
342                 AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave & ~LSB);
343                 break;
344         case SMB_QREAD:
345                 AMDPM_DEBUG(printf("amdpm: QREAD to 0x%x", slave));
346                 AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave | LSB);
347                 break;
348         default:
349                 panic("%s: unknown QUICK command (%x)!", __FUNCTION__, how);
350         }
351         l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
352         AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE, (l & 0xfff8) | AMDSMB_GE_CYC_QUICK | AMDSMB_GE_HOST_STC);
353
354         error = amdsmb_wait(sc);
355
356         AMDPM_DEBUG(printf(", error=0x%x\n", error));
357
358         return (error);
359 }
360
361 static int
362 amdsmb_smb_sendb(device_t dev, u_char slave, char byte)
363 {
364         struct amdsmb_softc *sc = (struct amdsmb_softc *)device_get_softc(dev);
365         int error;
366         u_short l;
367
368         amdsmb_clear(sc);
369         if (!amdsmb_idle(sc))
370                 return (SMB_EBUSY);
371
372         AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave & ~LSB);
373         AMDPM_SMBOUTW(sc, AMDSMB_HSTDATA, byte);
374         l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
375         AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE, (l & 0xfff8) | AMDSMB_GE_CYC_BYTE | AMDSMB_GE_HOST_STC);
376
377         error = amdsmb_wait(sc);
378
379         AMDPM_DEBUG(printf("amdpm: SENDB to 0x%x, byte=0x%x, error=0x%x\n", slave, byte, error));
380
381         return (error);
382 }
383
384 static int
385 amdsmb_smb_recvb(device_t dev, u_char slave, char *byte)
386 {
387         struct amdsmb_softc *sc = (struct amdsmb_softc *)device_get_softc(dev);
388         int error;
389         u_short l;
390
391         amdsmb_clear(sc);
392         if (!amdsmb_idle(sc))
393                 return (SMB_EBUSY);
394
395         AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave | LSB);
396         l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
397         AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE, (l & 0xfff8) | AMDSMB_GE_CYC_BYTE | AMDSMB_GE_HOST_STC);
398
399         if ((error = amdsmb_wait(sc)) == SMB_ENOERR)
400                 *byte = AMDPM_SMBINW(sc, AMDSMB_HSTDATA);
401
402         AMDPM_DEBUG(printf("amdpm: RECVB from 0x%x, byte=0x%x, error=0x%x\n", slave, *byte, error));
403
404         return (error);
405 }
406
407 static int
408 amdsmb_smb_writeb(device_t dev, u_char slave, char cmd, char byte)
409 {
410         struct amdsmb_softc *sc = (struct amdsmb_softc *)device_get_softc(dev);
411         int error;
412         u_short l;
413
414         amdsmb_clear(sc);
415         if (!amdsmb_idle(sc))
416                 return (SMB_EBUSY);
417
418         AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave & ~LSB);
419         AMDPM_SMBOUTW(sc, AMDSMB_HSTDATA, byte);
420         AMDPM_SMBOUTB(sc, AMDSMB_HSTCMD, cmd);
421         l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
422         AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE, (l & 0xfff8) | AMDSMB_GE_CYC_BDATA | AMDSMB_GE_HOST_STC);
423
424         error = amdsmb_wait(sc);
425
426         AMDPM_DEBUG(printf("amdpm: WRITEB to 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, byte, error));
427
428         return (error);
429 }
430
431 static int
432 amdsmb_smb_readb(device_t dev, u_char slave, char cmd, char *byte)
433 {
434         struct amdsmb_softc *sc = (struct amdsmb_softc *)device_get_softc(dev);
435         int error;
436         u_short l;
437
438         amdsmb_clear(sc);
439         if (!amdsmb_idle(sc))
440                 return (SMB_EBUSY);
441
442         AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave | LSB);
443         AMDPM_SMBOUTB(sc, AMDSMB_HSTCMD, cmd);
444         l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
445         AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE, (l & 0xfff8) | AMDSMB_GE_CYC_BDATA | AMDSMB_GE_HOST_STC);
446
447         if ((error = amdsmb_wait(sc)) == SMB_ENOERR)
448                 *byte = AMDPM_SMBINW(sc, AMDSMB_HSTDATA);
449
450         AMDPM_DEBUG(printf("amdpm: READB from 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, *byte, error));
451
452         return (error);
453 }
454
455 static int
456 amdsmb_smb_writew(device_t dev, u_char slave, char cmd, short word)
457 {
458         struct amdsmb_softc *sc = (struct amdsmb_softc *)device_get_softc(dev);
459         int error;
460         u_short l;
461
462         amdsmb_clear(sc);
463         if (!amdsmb_idle(sc))
464                 return (SMB_EBUSY);
465
466         AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave & ~LSB);
467         AMDPM_SMBOUTW(sc, AMDSMB_HSTDATA, word);
468         AMDPM_SMBOUTB(sc, AMDSMB_HSTCMD, cmd);
469         l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
470         AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE, (l & 0xfff8) | AMDSMB_GE_CYC_WDATA | AMDSMB_GE_HOST_STC);
471
472         error = amdsmb_wait(sc);
473
474         AMDPM_DEBUG(printf("amdpm: WRITEW to 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, word, error));
475
476         return (error);
477 }
478
479 static int
480 amdsmb_smb_readw(device_t dev, u_char slave, char cmd, short *word)
481 {
482         struct amdsmb_softc *sc = (struct amdsmb_softc *)device_get_softc(dev);
483         int error;
484         u_short l;
485
486         amdsmb_clear(sc);
487         if (!amdsmb_idle(sc))
488                 return (SMB_EBUSY);
489
490         AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave | LSB);
491         AMDPM_SMBOUTB(sc, AMDSMB_HSTCMD, cmd);
492         l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
493         AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE, (l & 0xfff8) | AMDSMB_GE_CYC_WDATA | AMDSMB_GE_HOST_STC);
494
495         if ((error = amdsmb_wait(sc)) == SMB_ENOERR)
496                 *word = AMDPM_SMBINW(sc, AMDSMB_HSTDATA);
497
498         AMDPM_DEBUG(printf("amdpm: READW from 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, *word, error));
499
500         return (error);
501 }
502
503 static int
504 amdsmb_smb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
505 {
506         struct amdsmb_softc *sc = (struct amdsmb_softc *)device_get_softc(dev);
507         u_char remain, len, i;
508         int error = SMB_ENOERR;
509         u_short l;
510
511         amdsmb_clear(sc);
512         if(!amdsmb_idle(sc))
513                 return (SMB_EBUSY);
514
515         remain = count;
516         while (remain) {
517                 len = min(remain, 32);
518
519                 AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave & ~LSB);
520         
521                 /*
522                  * Do we have to reset the internal 32-byte buffer?
523                  * Can't see how to do this from the data sheet.
524                  */
525
526                 AMDPM_SMBOUTW(sc, AMDSMB_HSTDATA, len);
527
528                 /* Fill the 32-byte internal buffer */
529                 for (i=0; i<len; i++) {
530                         AMDPM_SMBOUTB(sc, AMDSMB_HSTDFIFO, buf[count-remain+i]);
531                         DELAY(2);
532                 }
533                 AMDPM_SMBOUTB(sc, AMDSMB_HSTCMD, cmd);
534                 l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
535                 AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE, (l & 0xfff8) | AMDSMB_GE_CYC_BLOCK | AMDSMB_GE_HOST_STC);
536
537                 if ((error = amdsmb_wait(sc)) != SMB_ENOERR)
538                         goto error;
539
540                 remain -= len;
541         }
542
543 error:
544         AMDPM_DEBUG(printf("amdpm: WRITEBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
545
546         return (error);
547 }
548
549 static int
550 amdsmb_smb_bread(device_t dev, u_char slave, char cmd, u_char count, char *buf)
551 {
552         struct amdsmb_softc *sc = (struct amdsmb_softc *)device_get_softc(dev);
553         u_char remain, len, i;
554         int error = SMB_ENOERR;
555         u_short l;
556
557         amdsmb_clear(sc);
558         if (!amdsmb_idle(sc))
559                 return (SMB_EBUSY);
560
561         remain = count;
562         while (remain) {
563                 AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave | LSB);
564         
565                 AMDPM_SMBOUTB(sc, AMDSMB_HSTCMD, cmd);
566
567                 l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
568                 AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE, (l & 0xfff8) | AMDSMB_GE_CYC_BLOCK | AMDSMB_GE_HOST_STC);
569                 
570                 if ((error = amdsmb_wait(sc)) != SMB_ENOERR)
571                         goto error;
572
573                 len = AMDPM_SMBINW(sc, AMDSMB_HSTDATA);
574
575                 /* Read the 32-byte internal buffer */
576                 for (i=0; i<len; i++) {
577                         buf[count-remain+i] = AMDPM_SMBINB(sc, AMDSMB_HSTDFIFO);
578                         DELAY(2);
579                 }
580
581                 remain -= len;
582         }
583 error:
584         AMDPM_DEBUG(printf("amdpm: READBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
585
586         return (error);
587 }
588
589 static devclass_t amdpm_devclass;
590
591 static device_method_t amdpm_methods[] = {
592         /* Device interface */
593         DEVMETHOD(device_probe,         amdpm_probe),
594         DEVMETHOD(device_attach,        amdpm_attach),
595         
596         { 0, 0 }
597 };
598
599 static driver_t amdpm_driver = {
600         "amdpm",
601         amdpm_methods,
602         sizeof(struct amdpm_softc),
603 };
604
605 static devclass_t amdsmb_devclass;
606
607 static device_method_t amdsmb_methods[] = {
608         /* Device interface */
609         DEVMETHOD(device_probe,         amdsmb_probe),
610         DEVMETHOD(device_attach,        amdsmb_attach),
611
612         /* Bus interface */
613         DEVMETHOD(bus_print_child,      bus_generic_print_child),
614         
615         /* SMBus interface */
616         DEVMETHOD(smbus_callback,       amdsmb_smb_callback),
617         DEVMETHOD(smbus_quick,          amdsmb_smb_quick),
618         DEVMETHOD(smbus_sendb,          amdsmb_smb_sendb),
619         DEVMETHOD(smbus_recvb,          amdsmb_smb_recvb),
620         DEVMETHOD(smbus_writeb,         amdsmb_smb_writeb),
621         DEVMETHOD(smbus_readb,          amdsmb_smb_readb),
622         DEVMETHOD(smbus_writew,         amdsmb_smb_writew),
623         DEVMETHOD(smbus_readw,          amdsmb_smb_readw),
624         DEVMETHOD(smbus_bwrite,         amdsmb_smb_bwrite),
625         DEVMETHOD(smbus_bread,          amdsmb_smb_bread),
626         
627         { 0, 0 }
628 };
629
630 static driver_t amdsmb_driver = {
631         "amdsmb",
632         amdsmb_methods,
633         sizeof(struct amdsmb_softc),
634 };
635
636 DRIVER_MODULE(amdpm, pci, amdpm_driver, amdpm_devclass, 0, 0);
637 DRIVER_MODULE(amdsmb, amdpm, amdsmb_driver, amdsmb_devclass, 0, 0);