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