GCC supports two pseudo variables to get the function name, __FUNCTION__
[dragonfly.git] / sys / dev / powermng / i386 / alpm / alpm.c
1 /*-
2  * Copyright (c) 1998, 1999, 2001 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  * $FreeBSD: src/sys/pci/alpm.c,v 1.15 2001/01/17 00:38:06 peter Exp $
27  * $DragonFly: src/sys/dev/powermng/i386/alpm/alpm.c,v 1.6 2005/02/17 13:59:36 joerg Exp $
28  *
29  */
30
31 /*
32  * Power Management support for the Acer M15x3 chipsets
33  */
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/uio.h>
40
41 #include <machine/bus_pio.h>
42 #include <machine/bus_memio.h>
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45 #include <sys/rman.h>
46
47 #include <bus/pci/pcivar.h>
48 #include <bus/pci/pcireg.h>
49
50 #include <bus/iicbus/iiconf.h>
51 #include <bus/smbus/smbconf.h>
52 #include "smbus_if.h"
53
54 #define ALPM_DEBUG(x)   if (alpm_debug) (x)
55
56 #ifdef DEBUG
57 static int alpm_debug = 1;
58 #else
59 static int alpm_debug = 0;
60 #endif
61
62 #define ACER_M1543_PMU_ID       0x710110b9
63
64 /* Uncomment this line to force another I/O base address for SMB */
65 /* #define ALPM_SMBIO_BASE_ADDR 0x3a80 */
66
67 /* I/O registers offsets - the base address is programmed via the
68  * SMBBA PCI configuration register
69  */
70 #define SMBSTS          0x0     /* SMBus host/slave status register */
71 #define SMBCMD          0x1     /* SMBus host/slave command register */
72 #define SMBSTART        0x2     /* start to generate programmed cycle */
73 #define SMBHADDR        0x3     /* host address register */
74 #define SMBHDATA        0x4     /* data A register for host controller */
75 #define SMBHDATB        0x5     /* data B register for host controller */
76 #define SMBHBLOCK       0x6     /* block register for host controller */
77 #define SMBHCMD         0x7     /* command register for host controller */
78
79 /* SMBSTS masks */
80 #define TERMINATE       0x80
81 #define BUS_COLLI       0x40
82 #define DEVICE_ERR      0x20
83 #define SMI_I_STS       0x10
84 #define HST_BSY         0x08
85 #define IDL_STS         0x04
86 #define HSTSLV_STS      0x02
87 #define HSTSLV_BSY      0x01
88
89 /* SMBCMD masks */
90 #define SMB_BLK_CLR     0x80
91 #define T_OUT_CMD       0x08
92 #define ABORT_HOST      0x04
93
94 /* SMBus commands */
95 #define SMBQUICK        0x00
96 #define SMBSRBYTE       0x10            /* send/receive byte */
97 #define SMBWRBYTE       0x20            /* write/read byte */
98 #define SMBWRWORD       0x30            /* write/read word */
99 #define SMBWRBLOCK      0x40            /* write/read block */
100
101 /* PCI configuration registers and masks
102  */
103 #define COM             0x4
104 #define COM_ENABLE_IO   0x1
105
106 #define SMBBA           0x14
107
108 #define ATPC            0x5b
109 #define ATPC_SMBCTRL    0x04            /* XX linux has this as 0x6 */
110
111 #define SMBHSI          0xe0
112 #define SMBHSI_SLAVE    0x2
113 #define SMBHSI_HOST     0x1
114
115 #define SMBHCBC         0xe2
116 #define SMBHCBC_CLOCK   0x70
117
118 #define SMBCLOCK_149K   0x0
119 #define SMBCLOCK_74K    0x20
120 #define SMBCLOCK_37K    0x40
121 #define SMBCLOCK_223K   0x80
122 #define SMBCLOCK_111K   0xa0
123 #define SMBCLOCK_55K    0xc0
124
125 struct alpm_data {
126         int base;
127         bus_space_tag_t smbst;
128         bus_space_handle_t smbsh;
129 };
130
131 struct alsmb_softc {
132         int base;
133         device_t smbus;
134         struct alpm_data *alpm;
135 };
136
137 #define ALPM_SMBINB(alsmb,register) \
138         (bus_space_read_1(alsmb->alpm->smbst, alsmb->alpm->smbsh, register))
139 #define ALPM_SMBOUTB(alsmb,register,value) \
140         (bus_space_write_1(alsmb->alpm->smbst, alsmb->alpm->smbsh, register, value))
141
142 static int alsmb_probe(device_t);
143 static int alsmb_attach(device_t);
144 static int alsmb_smb_callback(device_t, int, caddr_t *);
145 static int alsmb_smb_quick(device_t dev, u_char slave, int how);
146 static int alsmb_smb_sendb(device_t dev, u_char slave, char byte);
147 static int alsmb_smb_recvb(device_t dev, u_char slave, char *byte);
148 static int alsmb_smb_writeb(device_t dev, u_char slave, char cmd, char byte);
149 static int alsmb_smb_readb(device_t dev, u_char slave, char cmd, char *byte);
150 static int alsmb_smb_writew(device_t dev, u_char slave, char cmd, short word);
151 static int alsmb_smb_readw(device_t dev, u_char slave, char cmd, short *word);
152 static int alsmb_smb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf);
153 static int alsmb_smb_bread(device_t dev, u_char slave, char cmd, u_char count, char *byte);
154
155 static devclass_t alsmb_devclass;
156
157 static device_method_t alsmb_methods[] = {
158         /* device interface */
159         DEVMETHOD(device_probe,         alsmb_probe),
160         DEVMETHOD(device_attach,        alsmb_attach),
161
162         /* bus interface */
163         DEVMETHOD(bus_print_child,      bus_generic_print_child),
164         
165         /* smbus interface */
166         DEVMETHOD(smbus_callback,       alsmb_smb_callback),
167         DEVMETHOD(smbus_quick,          alsmb_smb_quick),
168         DEVMETHOD(smbus_sendb,          alsmb_smb_sendb),
169         DEVMETHOD(smbus_recvb,          alsmb_smb_recvb),
170         DEVMETHOD(smbus_writeb,         alsmb_smb_writeb),
171         DEVMETHOD(smbus_readb,          alsmb_smb_readb),
172         DEVMETHOD(smbus_writew,         alsmb_smb_writew),
173         DEVMETHOD(smbus_readw,          alsmb_smb_readw),
174         DEVMETHOD(smbus_bwrite,         alsmb_smb_bwrite),
175         DEVMETHOD(smbus_bread,          alsmb_smb_bread),
176         
177         { 0, 0 }
178 };
179
180 static driver_t alsmb_driver = {
181         "alsmb",
182         alsmb_methods,
183         sizeof(struct alsmb_softc),
184 };
185
186 static int alpm_pci_probe(device_t dev);
187 static int alpm_pci_attach(device_t dev);
188
189 static devclass_t alpm_devclass;
190
191 static device_method_t alpm_pci_methods[] = {
192         /* device interface */
193         DEVMETHOD(device_probe,         alpm_pci_probe),
194         DEVMETHOD(device_attach,        alpm_pci_attach),
195         
196         { 0, 0 }
197 };
198
199 static driver_t alpm_pci_driver = {
200         "alpm",
201         alpm_pci_methods,
202         sizeof(struct alpm_data)
203 };
204
205 static int
206 alpm_pci_probe(device_t dev)
207 {
208         if(pci_get_devid(dev) == ACER_M1543_PMU_ID) {
209                 device_set_desc(dev, "AcerLabs M15x3 Power Management Unit");
210                 return 0;
211         } else {
212                 return ENXIO;
213         }
214 }
215
216 static int
217 alpm_pci_attach(device_t dev)
218 {
219         int rid, unit;
220         u_int32_t l;
221         struct alpm_data *alpm;
222         struct resource *res;
223         device_t smbinterface;
224
225         alpm = device_get_softc(dev);
226         unit = device_get_unit(dev);
227
228         /* Unlock SMBIO base register access */
229         l = pci_read_config(dev, ATPC, 1);
230         pci_write_config(dev, ATPC, l & ~ATPC_SMBCTRL, 1);
231
232         /*
233          * XX linux sets clock to 74k, should we?
234         l = pci_read_config(dev, SMBHCBC, 1);
235         l &= 0x1f;
236         l |= SMBCLOCK_74K;
237         pci_write_config(dev, SMBHCBC, l, 1)
238          */
239
240         if (bootverbose) {
241                 l = pci_read_config(dev, SMBHSI, 1);
242                 printf("alsmb%d: %s/%s", unit,
243                         (l & SMBHSI_HOST) ? "host":"nohost",
244                         (l & SMBHSI_SLAVE) ? "slave":"noslave");
245
246                 l = pci_read_config(dev, SMBHCBC, 1);
247                 switch (l & SMBHCBC_CLOCK) {
248                 case SMBCLOCK_149K:
249                         printf(" 149K");
250                         break;
251                 case SMBCLOCK_74K:
252                         printf(" 74K");
253                         break;
254                 case SMBCLOCK_37K:
255                         printf(" 37K");
256                         break;
257                 case SMBCLOCK_223K:
258                         printf(" 223K");
259                         break;
260                 case SMBCLOCK_111K:
261                         printf(" 111K");
262                         break;
263                 case SMBCLOCK_55K:
264                         printf(" 55K");
265                         break;
266                 }
267         }
268
269 #ifdef ALPM_SMBIO_BASE_ADDR
270         /* XX will this even work anymore? */
271         /* disable I/O */
272         l = pci_read_config(dev, COM, 2);
273         pci_write_config(dev, COM, l & ~COM_ENABLE_IO, 2);
274
275         /* set the I/O base address */
276         pci_write_config(dev, SMBBA, ALPM_SMBIO_BASE_ADDR | 0x1, 4);
277
278         /* enable I/O */
279         pci_write_config(dev, COM, l | COM_ENABLE_IO, 2);
280
281 #endif
282         rid = SMBBA;
283         res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
284             0, ~0, 1, RF_ACTIVE);
285         if (res == NULL) {
286                 device_printf(dev, "Could not allocate Bus space\n");
287                 return ENXIO;
288         }
289         alpm->smbst = rman_get_bustag(res);
290         alpm->smbsh = rman_get_bushandle(res);
291
292         if (bootverbose)
293                 printf(" at 0x%x\n", alpm->smbsh);
294
295         smbinterface = device_add_child(dev, "alsmb", unit);
296         if (!smbinterface)
297                 device_printf(dev, "could not add SMBus device\n");
298         else
299                 device_probe_and_attach(smbinterface);
300         return 0;
301 }
302
303 /*
304  * Not a real probe, we know the device exists since the device has
305  * been added after the successfull pci probe.
306  */
307 static int
308 alsmb_probe(device_t dev)
309 {
310         struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
311
312         /* allocate a new smbus device */
313         sc->smbus = smbus_alloc_bus(dev);
314         if (!sc->smbus)
315                 return (EINVAL);
316         device_set_desc(dev, "Aladdin IV/V/Pro2 SMBus controller");
317
318         return (0);
319 }
320
321 static int
322 alsmb_attach(device_t dev)
323 {
324         struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
325
326         sc->alpm = device_get_softc(device_get_parent(dev));
327
328         /* probe and attach the smbus */
329         device_probe_and_attach(sc->smbus);
330
331         return (0);
332 }
333
334 static int
335 alsmb_smb_callback(device_t dev, int index, caddr_t *data)
336 {
337         int error = 0;
338
339         switch (index) {
340         case SMB_REQUEST_BUS:
341         case SMB_RELEASE_BUS:
342                 /* ok, bus allocation accepted */
343                 break;
344         default:
345                 error = EINVAL;
346         }
347
348         return (error);
349 }
350
351 static int
352 alsmb_clear(struct alsmb_softc *sc)
353 {
354         ALPM_SMBOUTB(sc, SMBSTS, 0xff);
355         DELAY(10);
356
357         return (0);
358 }
359
360 #if 0
361 static int
362 alsmb_abort(struct alsmb_softc *sc)
363 {
364         ALPM_SMBOUTB(sc, SMBCMD, T_OUT_CMD | ABORT_HOST);
365
366         return (0);
367 }
368 #endif
369
370 static int
371 alsmb_idle(struct alsmb_softc *sc)
372 {
373         u_char sts;
374
375         sts = ALPM_SMBINB(sc, SMBSTS);
376
377         ALPM_DEBUG(printf("alpm: idle? STS=0x%x\n", sts));
378
379         return (sts & IDL_STS);
380 }
381
382 /*
383  * Poll the SMBus controller
384  */
385 static int
386 alsmb_wait(struct alsmb_softc *sc)
387 {
388         int count = 10000;
389         u_char sts = 0;
390         int error;
391
392         /* wait for command to complete and SMBus controller is idle */
393         while(count--) {
394                 DELAY(10);
395                 sts = ALPM_SMBINB(sc, SMBSTS);
396                 if (sts & SMI_I_STS)
397                         break;
398         }
399
400         ALPM_DEBUG(printf("alpm: STS=0x%x\n", sts));
401
402         error = SMB_ENOERR;
403
404         if (!count)
405                 error |= SMB_ETIMEOUT;
406
407         if (sts & TERMINATE)
408                 error |= SMB_EABORT;
409
410         if (sts & BUS_COLLI)
411                 error |= SMB_ENOACK;
412
413         if (sts & DEVICE_ERR)
414                 error |= SMB_EBUSERR;
415
416         if (error != SMB_ENOERR)
417                 alsmb_clear(sc);
418
419         return (error);
420 }
421
422 static int
423 alsmb_smb_quick(device_t dev, u_char slave, int how)
424 {
425         struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
426         int error;
427
428         alsmb_clear(sc);
429         if (!alsmb_idle(sc))
430                 return (EBUSY);
431
432         switch (how) {
433         case SMB_QWRITE:
434                 ALPM_DEBUG(printf("alpm: QWRITE to 0x%x", slave));
435                 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
436                 break;
437         case SMB_QREAD:
438                 ALPM_DEBUG(printf("alpm: QREAD to 0x%x", slave));
439                 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
440                 break;
441         default:
442                 panic("%s: unknown QUICK command (%x)!", __func__, how);
443         }
444         ALPM_SMBOUTB(sc, SMBCMD, SMBQUICK);
445         ALPM_SMBOUTB(sc, SMBSTART, 0xff);
446
447         error = alsmb_wait(sc);
448
449         ALPM_DEBUG(printf(", error=0x%x\n", error));
450
451         return (error);
452 }
453
454 static int
455 alsmb_smb_sendb(device_t dev, u_char slave, char byte)
456 {
457         struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
458         int error;
459
460         alsmb_clear(sc);
461         if (!alsmb_idle(sc))
462                 return (SMB_EBUSY);
463
464         ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
465         ALPM_SMBOUTB(sc, SMBCMD, SMBSRBYTE);
466         ALPM_SMBOUTB(sc, SMBHDATA, byte);
467         ALPM_SMBOUTB(sc, SMBSTART, 0xff);
468
469         error = alsmb_wait(sc);
470
471         ALPM_DEBUG(printf("alpm: SENDB to 0x%x, byte=0x%x, error=0x%x\n", slave, byte, error));
472
473         return (error);
474 }
475
476 static int
477 alsmb_smb_recvb(device_t dev, u_char slave, char *byte)
478 {
479         struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
480         int error;
481
482         alsmb_clear(sc);
483         if (!alsmb_idle(sc))
484                 return (SMB_EBUSY);
485
486         ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
487         ALPM_SMBOUTB(sc, SMBCMD, SMBSRBYTE);
488         ALPM_SMBOUTB(sc, SMBSTART, 0xff);
489
490         if ((error = alsmb_wait(sc)) == SMB_ENOERR)
491                 *byte = ALPM_SMBINB(sc, SMBHDATA);
492
493         ALPM_DEBUG(printf("alpm: RECVB from 0x%x, byte=0x%x, error=0x%x\n", slave, *byte, error));
494
495         return (error);
496 }
497
498 static int
499 alsmb_smb_writeb(device_t dev, u_char slave, char cmd, char byte)
500 {
501         struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
502         int error;
503
504         alsmb_clear(sc);
505         if (!alsmb_idle(sc))
506                 return (SMB_EBUSY);
507
508         ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
509         ALPM_SMBOUTB(sc, SMBCMD, SMBWRBYTE);
510         ALPM_SMBOUTB(sc, SMBHDATA, byte);
511         ALPM_SMBOUTB(sc, SMBHCMD, cmd);
512         ALPM_SMBOUTB(sc, SMBSTART, 0xff);
513
514         error = alsmb_wait(sc);
515
516         ALPM_DEBUG(printf("alpm: WRITEB to 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, byte, error));
517
518         return (error);
519 }
520
521 static int
522 alsmb_smb_readb(device_t dev, u_char slave, char cmd, char *byte)
523 {
524         struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
525         int error;
526
527         alsmb_clear(sc);
528         if (!alsmb_idle(sc))
529                 return (SMB_EBUSY);
530
531         ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
532         ALPM_SMBOUTB(sc, SMBCMD, SMBWRBYTE);
533         ALPM_SMBOUTB(sc, SMBHCMD, cmd);
534         ALPM_SMBOUTB(sc, SMBSTART, 0xff);
535
536         if ((error = alsmb_wait(sc)) == SMB_ENOERR)
537                 *byte = ALPM_SMBINB(sc, SMBHDATA);
538
539         ALPM_DEBUG(printf("alpm: READB from 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, *byte, error));
540
541         return (error);
542 }
543
544 static int
545 alsmb_smb_writew(device_t dev, u_char slave, char cmd, short word)
546 {
547         struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
548         int error;
549
550         alsmb_clear(sc);
551         if (!alsmb_idle(sc))
552                 return (SMB_EBUSY);
553
554         ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
555         ALPM_SMBOUTB(sc, SMBCMD, SMBWRWORD);
556         ALPM_SMBOUTB(sc, SMBHDATA, word & 0x00ff);
557         ALPM_SMBOUTB(sc, SMBHDATB, (word & 0xff00) >> 8);
558         ALPM_SMBOUTB(sc, SMBHCMD, cmd);
559         ALPM_SMBOUTB(sc, SMBSTART, 0xff);
560
561         error = alsmb_wait(sc);
562
563         ALPM_DEBUG(printf("alpm: WRITEW to 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, word, error));
564
565         return (error);
566 }
567
568 static int
569 alsmb_smb_readw(device_t dev, u_char slave, char cmd, short *word)
570 {
571         struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
572         int error;
573         u_char high, low;
574
575         alsmb_clear(sc);
576         if (!alsmb_idle(sc))
577                 return (SMB_EBUSY);
578
579         ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
580         ALPM_SMBOUTB(sc, SMBCMD, SMBWRWORD);
581         ALPM_SMBOUTB(sc, SMBHCMD, cmd);
582         ALPM_SMBOUTB(sc, SMBSTART, 0xff);
583
584         if ((error = alsmb_wait(sc)) == SMB_ENOERR) {
585                 low = ALPM_SMBINB(sc, SMBHDATA);
586                 high = ALPM_SMBINB(sc, SMBHDATB);
587
588                 *word = ((high & 0xff) << 8) | (low & 0xff);
589         }
590
591         ALPM_DEBUG(printf("alpm: READW from 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, *word, error));
592
593         return (error);
594 }
595
596 static int
597 alsmb_smb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
598 {
599         struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
600         u_char remain, len, i;
601         int error = SMB_ENOERR;
602
603         alsmb_clear(sc);
604         if(!alsmb_idle(sc))
605                 return (SMB_EBUSY);
606
607         remain = count;
608         while (remain) {
609                 len = min(remain, 32);
610
611                 ALPM_SMBOUTB(sc, SMBHADDR, slave & ~LSB);
612         
613                 /* set the cmd and reset the
614                  * 32-byte long internal buffer */
615                 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBLOCK | SMB_BLK_CLR);
616
617                 ALPM_SMBOUTB(sc, SMBHDATA, len);
618
619                 /* fill the 32-byte internal buffer */
620                 for (i=0; i<len; i++) {
621                         ALPM_SMBOUTB(sc, SMBHBLOCK, buf[count-remain+i]);
622                         DELAY(2);
623                 }
624                 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
625                 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
626
627                 if ((error = alsmb_wait(sc)) != SMB_ENOERR)
628                         goto error;
629
630                 remain -= len;
631         }
632
633 error:
634         ALPM_DEBUG(printf("alpm: WRITEBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
635
636         return (error);
637 }
638
639 static int
640 alsmb_smb_bread(device_t dev, u_char slave, char cmd, u_char count, char *buf)
641 {
642         struct alsmb_softc *sc = (struct alsmb_softc *)device_get_softc(dev);
643         u_char remain, len, i;
644         int error = SMB_ENOERR;
645
646         alsmb_clear(sc);
647         if (!alsmb_idle(sc))
648                 return (SMB_EBUSY);
649
650         remain = count;
651         while (remain) {
652                 ALPM_SMBOUTB(sc, SMBHADDR, slave | LSB);
653         
654                 /* set the cmd and reset the
655                  * 32-byte long internal buffer */
656                 ALPM_SMBOUTB(sc, SMBCMD, SMBWRBLOCK | SMB_BLK_CLR);
657
658                 ALPM_SMBOUTB(sc, SMBHCMD, cmd);
659                 ALPM_SMBOUTB(sc, SMBSTART, 0xff);
660
661                 if ((error = alsmb_wait(sc)) != SMB_ENOERR)
662                         goto error;
663
664                 len = ALPM_SMBINB(sc, SMBHDATA);
665
666                 /* read the 32-byte internal buffer */
667                 for (i=0; i<len; i++) {
668                         buf[count-remain+i] = ALPM_SMBINB(sc, SMBHBLOCK);
669                         DELAY(2);
670                 }
671
672                 remain -= len;
673         }
674 error:
675         ALPM_DEBUG(printf("alpm: READBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
676
677         return (error);
678 }
679
680 DRIVER_MODULE(alpm, pci, alpm_pci_driver, alpm_devclass, 0, 0);
681 DRIVER_MODULE(alsmb, alpm, alsmb_driver, alsmb_devclass, 0, 0);