1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
|
/*-
* Copyright (c) 1999 Takanori Watanabe
* Copyright (c) 1999, 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: aml_common.c,v 1.9 2000/08/09 14:47:43 iwasaki Exp $
* $FreeBSD$
*/
#include <sys/param.h>
#ifndef _KERNEL
#include <assert.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#else /* _KERNEL */
#include "opt_acpi.h"
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <sys/acpi.h>
#ifndef ACPI_NO_OSDFUNC_INLINE
#include <machine/acpica_osd.h>
#endif /* !ACPI_NO_OSDFUNC_INLINE */
#endif /* !_KERNEL */
#include <dev/acpi/aml/aml_common.h>
#include <dev/acpi/aml/aml_env.h>
#include <dev/acpi/aml/aml_evalobj.h>
#include <dev/acpi/aml/aml_name.h>
#include <dev/acpi/aml/aml_obj.h>
#include <dev/acpi/aml/aml_parse.h>
#include <dev/acpi/aml/aml_status.h>
#include <dev/acpi/aml/aml_store.h>
/* for debugging */
#ifdef AML_DEBUG
int aml_debug = 1;
#else /* !AML_DEBUG */
int aml_debug = 0;
#endif /* AML_DEBUG */
#ifdef _KERNEL
SYSCTL_INT(_debug, OID_AUTO, aml_debug, CTLFLAG_RW, &aml_debug, 1, "");
#endif /* _KERNEL */
static void aml_print_nameseg(u_int8_t *dp);
static void
aml_print_nameseg(u_int8_t *dp)
{
if (dp[3] != '_') {
AML_DEBUGPRINT("%c%c%c%c", dp[0], dp[1], dp[2], dp[3]);
} else if (dp[2] != '_') {
AML_DEBUGPRINT("%c%c%c_", dp[0], dp[1], dp[2]);
} else if (dp[1] != '_') {
AML_DEBUGPRINT("%c%c__", dp[0], dp[1]);
} else if (dp[0] != '_') {
AML_DEBUGPRINT("%c___", dp[0]);
}
}
void
aml_print_namestring(u_int8_t *dp)
{
int segcount;
int i;
if (dp[0] == '\\') {
AML_DEBUGPRINT("%c", dp[0]);
dp++;
} else if (dp[0] == '^') {
while (dp[0] == '^') {
AML_DEBUGPRINT("%c", dp[0]);
dp++;
}
}
if (dp[0] == 0x00) { /* NullName */
/* AML_DEBUGPRINT("<null>"); */
dp++;
} else if (dp[0] == 0x2e) { /* DualNamePrefix */
aml_print_nameseg(dp + 1);
AML_DEBUGPRINT("%c", '.');
aml_print_nameseg(dp + 5);
} else if (dp[0] == 0x2f) { /* MultiNamePrefix */
segcount = dp[1];
for (i = 0, dp += 2; i < segcount; i++, dp += 4) {
if (i > 0) {
AML_DEBUGPRINT("%c", '.');
}
aml_print_nameseg(dp);
}
} else /* NameSeg */
aml_print_nameseg(dp);
}
int
aml_print_curname(struct aml_name *name)
{
struct aml_name *root;
root = aml_get_rootname();
if (name == root) {
AML_DEBUGPRINT("\\");
return (0);
} else {
aml_print_curname(name->parent);
}
aml_print_nameseg(name->name);
AML_DEBUGPRINT(".");
return (0);
}
void
aml_print_indent(int indent)
{
int i;
for (i = 0; i < indent; i++)
AML_DEBUGPRINT(" ");
}
void
aml_showobject(union aml_object * obj)
{
int debug;
int i;
if (obj == NULL) {
printf("NO object\n");
return;
}
debug = aml_debug;
aml_debug = 1;
switch (obj->type) {
case aml_t_num:
printf("Num:0x%x\n", obj->num.number);
break;
case aml_t_processor:
printf("Processor:No %d,Port 0x%x length 0x%x\n",
obj->proc.id, obj->proc.addr, obj->proc.len);
break;
case aml_t_mutex:
printf("Mutex:Level %d\n", obj->mutex.level);
break;
case aml_t_powerres:
printf("PowerResource:Level %d Order %d\n",
obj->pres.level, obj->pres.order);
break;
case aml_t_opregion:
printf("OprationRegion:Busspace%d, Offset 0x%x Length 0x%x\n",
obj->opregion.space, obj->opregion.offset,
obj->opregion.length);
break;
case aml_t_field:
printf("Fieldelement:flag 0x%x offset 0x%x len 0x%x {",
obj->field.flags, obj->field.bitoffset,
obj->field.bitlen);
switch (obj->field.f.ftype) {
case f_t_field:
aml_print_namestring(obj->field.f.fld.regname);
break;
case f_t_index:
aml_print_namestring(obj->field.f.ifld.indexname);
printf(" ");
aml_print_namestring(obj->field.f.ifld.dataname);
break;
case f_t_bank:
aml_print_namestring(obj->field.f.bfld.regname);
printf(" ");
aml_print_namestring(obj->field.f.bfld.bankname);
printf("0x%x", obj->field.f.bfld.bankvalue);
break;
}
printf("}\n");
break;
case aml_t_method:
printf("Method: Arg %d From %p To %p\n", obj->meth.argnum,
obj->meth.from, obj->meth.to);
break;
case aml_t_buffer:
printf("Buffer: size:0x%x Data %p\n", obj->buffer.size,
obj->buffer.data);
break;
case aml_t_device:
printf("Device\n");
break;
case aml_t_bufferfield:
printf("Bufferfield:offset 0x%x len 0x%x Origin %p\n",
obj->bfld.bitoffset, obj->bfld.bitlen, obj->bfld.origin);
break;
case aml_t_string:
printf("String:%s\n", obj->str.string);
break;
case aml_t_package:
printf("Package:elements %d \n", obj->package.elements);
for (i = 0; i < obj->package.elements; i++) {
if (obj->package.objects[i] == NULL) {
break;
}
if (obj->package.objects[i]->type < 0) {
continue;
}
printf(" ");
aml_showobject(obj->package.objects[i]);
}
break;
case aml_t_therm:
printf("Thermalzone\n");
break;
case aml_t_event:
printf("Event\n");
break;
case aml_t_ddbhandle:
printf("DDBHANDLE\n");
break;
case aml_t_objref:
if (obj->objref.alias == 1) {
printf("Alias");
} else {
printf("Object reference");
if (obj->objref.offset >= 0) {
printf(" (offset 0x%x)", obj->objref.offset);
}
}
printf(" of ");
aml_showobject(obj->objref.ref);
break;
default:
printf("UNK ID=%d\n", obj->type);
}
aml_debug = debug;
}
void
aml_showtree(struct aml_name * aname, int lev)
{
int i;
struct aml_name *ptr;
char name[5];
for (i = 0; i < lev; i++) {
printf(" ");
}
strncpy(name, aname->name, 4);
name[4] = 0;
printf("%s ", name);
if (aname->property != NULL) {
aml_showobject(aname->property);
} else {
printf("\n");
}
for (ptr = aname->child; ptr; ptr = ptr->brother)
aml_showtree(ptr, lev + 1);
}
/*
* Common Region I/O Stuff
*/
static __inline u_int64_t
aml_adjust_bitmask(u_int32_t flags, u_int32_t bitlen)
{
u_int64_t bitmask;
switch (AML_FIELDFLAGS_ACCESSTYPE(flags)) {
case AML_FIELDFLAGS_ACCESS_ANYACC:
if (bitlen <= 8) {
bitmask = 0x000000ff;
break;
}
if (bitlen <= 16) {
bitmask = 0x0000ffff;
break;
}
bitmask = 0xffffffff;
break;
case AML_FIELDFLAGS_ACCESS_BYTEACC:
bitmask = 0x000000ff;
break;
case AML_FIELDFLAGS_ACCESS_WORDACC:
bitmask = 0x0000ffff;
break;
case AML_FIELDFLAGS_ACCESS_DWORDACC:
default:
bitmask = 0xffffffff;
break;
}
switch (bitlen) {
case 16:
bitmask |= 0x0000ffff;
break;
case 32:
bitmask |= 0xffffffff;
break;
}
return (bitmask);
}
u_int32_t
aml_adjust_readvalue(u_int32_t flags, u_int32_t bitoffset, u_int32_t bitlen,
u_int32_t orgval)
{
u_int32_t offset, retval;
u_int64_t bitmask;
offset = bitoffset; /* XXX bitoffset may change in this function! */
bitmask = aml_adjust_bitmask(flags, bitlen);
retval = (orgval >> offset) & (~(bitmask << bitlen)) & bitmask;
return (retval);
}
u_int32_t
aml_adjust_updatevalue(u_int32_t flags, u_int32_t bitoffset, u_int32_t bitlen,
u_int32_t orgval, u_int32_t value)
{
u_int32_t offset, retval;
u_int64_t bitmask;
offset = bitoffset; /* XXX bitoffset may change in this function! */
bitmask = aml_adjust_bitmask(flags, bitlen);
retval = orgval;
switch (AML_FIELDFLAGS_UPDATERULE(flags)) {
case AML_FIELDFLAGS_UPDATE_PRESERVE:
retval &= (~(((u_int64_t)1 << bitlen) - 1) << offset) |
(~(bitmask << offset));
break;
case AML_FIELDFLAGS_UPDATE_WRITEASONES:
retval = (~(((u_int64_t)1 << bitlen) - 1) << offset) |
(~(bitmask << offset));
retval &= bitmask; /* trim the upper bits */
break;
case AML_FIELDFLAGS_UPDATE_WRITEASZEROS:
retval = 0;
break;
default:
printf("illegal update rule: %d\n", flags);
return (orgval);
}
retval |= (value << (offset & bitmask));
return (retval);
}
/*
* BufferField I/O
*/
#define AML_BUFFER_INPUT 0
#define AML_BUFFER_OUTPUT 1
static int aml_bufferfield_io(int io, u_int32_t *valuep,
u_int8_t *origin, u_int32_t bitoffset,
u_int32_t bitlen);
static int
aml_bufferfield_io(int io, u_int32_t *valuep, u_int8_t *origin,
u_int32_t bitoffset, u_int32_t bitlen)
{
u_int8_t val, tmp, masklow, maskhigh;
u_int8_t offsetlow, offsethigh;
u_int8_t *addr;
int i;
u_int32_t value, readval;
u_int32_t byteoffset, bytelen;
masklow = maskhigh = 0xff;
val = readval = 0;
value = *valuep;
byteoffset = bitoffset / 8;
bytelen = bitlen / 8 + ((bitlen % 8) ? 1 : 0);
addr = origin + byteoffset;
/* simple I/O ? */
if (bitlen <= 8 || bitlen == 16 || bitlen == 32) {
bcopy(addr, &readval, bytelen);
AML_DEBUGPRINT("\n\t[bufferfield:0x%x@%p:%d,%d]",
readval, addr, bitoffset % 8, bitlen);
switch (io) {
case AML_BUFFER_INPUT:
value = aml_adjust_readvalue(AML_FIELDFLAGS_ACCESS_BYTEACC,
bitoffset % 8, bitlen, readval);
*valuep = value;
AML_DEBUGPRINT("\n[read(bufferfield, %p)&mask:0x%x]\n",
addr, value);
break;
case AML_BUFFER_OUTPUT:
value = aml_adjust_updatevalue(AML_FIELDFLAGS_ACCESS_BYTEACC,
bitoffset % 8, bitlen, readval, value);
bcopy(&value, addr, bytelen);
AML_DEBUGPRINT("->[bufferfield:0x%x@%p:%d,%d]",
value, addr, bitoffset % 8, bitlen);
break;
}
goto out;
}
offsetlow = bitoffset % 8;
if (bytelen > 1) {
offsethigh = (bitlen - (8 - offsetlow)) % 8;
} else {
offsethigh = 0;
}
if (offsetlow) {
masklow = (~((1 << bitlen) - 1) << offsetlow) | ~(0xff << offsetlow);
AML_DEBUGPRINT("\t[offsetlow = 0x%x, masklow = 0x%x, ~masklow = 0x%x]\n",
offsetlow, masklow, ~masklow & 0xff);
}
if (offsethigh) {
maskhigh = 0xff << offsethigh;
AML_DEBUGPRINT("\t[offsethigh = 0x%x, maskhigh = 0x%x, ~maskhigh = 0x%x]\n",
offsethigh, maskhigh, ~maskhigh & 0xff);
}
for (i = bytelen; i > 0; i--, addr++) {
val = *addr;
AML_DEBUGPRINT("\t[bufferfield:0x%02x@%p]", val, addr);
switch (io) {
case AML_BUFFER_INPUT:
tmp = val;
/* the lowest byte? */
if (i == bytelen) {
if (offsetlow) {
readval = tmp & ~masklow;
} else {
readval = tmp;
}
} else {
if (i == 1 && offsethigh) {
tmp = tmp & ~maskhigh;
}
readval = (tmp << (8 * (bytelen - i))) | readval;
}
AML_DEBUGPRINT("\n");
/* goto to next byte... */
if (i > 1) {
continue;
}
/* final adjustment before finishing region access */
if (offsetlow) {
readval = readval >> offsetlow;
}
AML_DEBUGPRINT("[read(bufferfield, %p)&mask:0x%x]\n",
addr, readval);
*valuep = readval;
break;
case AML_BUFFER_OUTPUT:
tmp = value & 0xff;
/* the lowest byte? */
if (i == bytelen) {
if (offsetlow) {
tmp = (val & masklow) | tmp << offsetlow;
}
value = value >> (8 - offsetlow);
} else {
if (i == 1 && offsethigh) {
tmp = (val & maskhigh) | tmp;
}
value = value >> 8;
}
AML_DEBUGPRINT("->[bufferfield:0x%02x@%p]\n",
tmp, addr);
*addr = tmp;
}
}
out:
return (0);
}
u_int32_t
aml_bufferfield_read(u_int8_t *origin, u_int32_t bitoffset,
u_int32_t bitlen)
{
int value;
value = 0;
aml_bufferfield_io(AML_BUFFER_INPUT, &value, origin,
bitoffset, bitlen);
return (value);
}
int
aml_bufferfield_write(u_int32_t value, u_int8_t *origin,
u_int32_t bitoffset, u_int32_t bitlen)
{
int status;
status = aml_bufferfield_io(AML_BUFFER_OUTPUT, &value,
origin, bitoffset, bitlen);
return (status);
}
int
aml_region_handle_alloc(struct aml_environ *env, int regtype, u_int32_t flags,
u_int32_t baseaddr, u_int32_t bitoffset, u_int32_t bitlen,
struct aml_region_handle *h)
{
int state;
struct aml_name *pci_info;
state = 0;
pci_info = NULL;
bzero(h, sizeof(struct aml_region_handle));
h->env = env;
h->regtype = regtype;
h->flags = flags;
h->baseaddr = baseaddr;
h->bitoffset = bitoffset;
h->bitlen = bitlen;
switch (AML_FIELDFLAGS_ACCESSTYPE(flags)) {
case AML_FIELDFLAGS_ACCESS_ANYACC:
if (bitlen <= 8) {
h->unit = 1;
break;
}
if (bitlen <= 16) {
h->unit = 2;
break;
}
h->unit = 4;
break;
case AML_FIELDFLAGS_ACCESS_BYTEACC:
h->unit = 1;
break;
case AML_FIELDFLAGS_ACCESS_WORDACC:
h->unit = 2;
break;
case AML_FIELDFLAGS_ACCESS_DWORDACC:
h->unit = 4;
break;
default:
h->unit = 1;
break;
}
h->addr = baseaddr + h->unit * ((bitoffset / 8) / h->unit);
h->bytelen = baseaddr + ((bitoffset + bitlen) / 8) - h->addr +
((bitlen % 8) ? 1 : 0);
#ifdef _KERNEL
switch (h->regtype) {
case AML_REGION_SYSMEM:
OsdMapMemory((void *)h->addr, h->bytelen, (void **)&h->vaddr);
break;
case AML_REGION_PCICFG:
/* Obtain PCI bus number */
pci_info = aml_search_name(env, "_BBN");
if (pci_info == NULL || pci_info->property->type != aml_t_num) {
AML_DEBUGPRINT("Cannot locate _BBN. Using default 0\n");
h->pci_bus = 0;
} else {
AML_DEBUGPRINT("found _BBN: %d\n",
pci_info->property->num.number);
h->pci_bus = pci_info->property->num.number & 0xff;
}
/* Obtain device & function number */
pci_info = aml_search_name(env, "_ADR");
if (pci_info == NULL || pci_info->property->type != aml_t_num) {
printf("Cannot locate: _ADR\n");
state = -1;
goto out;
}
h->pci_devfunc = pci_info->property->num.number;
AML_DEBUGPRINT("[pci%d.%d]", h->pci_bus, h->pci_devfunc);
break;
default:
break;
}
out:
#endif /* _KERNEL */
return (state);
}
void
aml_region_handle_free(struct aml_region_handle *h)
{
#ifdef _KERNEL
switch (h->regtype) {
case AML_REGION_SYSMEM:
OsdUnMapMemory((void *)h->vaddr, h->bytelen);
break;
default:
break;
}
#endif /* _KERNEL */
}
static int
aml_region_io_simple(struct aml_environ *env, int io, int regtype,
u_int32_t flags, u_int32_t *valuep, u_int32_t baseaddr,
u_int32_t bitoffset, u_int32_t bitlen)
{
int i, state;
u_int32_t readval, value, offset, bytelen;
struct aml_region_handle handle;
state = aml_region_handle_alloc(env, regtype, flags,
baseaddr, bitoffset, bitlen, &handle);
if (state == -1) {
goto out;
}
readval = 0;
offset = bitoffset % (handle.unit * 8);
/* limitation of 32 bits alignment */
bytelen = (handle.bytelen > 4) ? 4 : handle.bytelen;
if (io == AML_REGION_INPUT ||
AML_FIELDFLAGS_UPDATERULE(flags) == AML_FIELDFLAGS_UPDATE_PRESERVE) {
for (i = 0; i < bytelen; i += handle.unit) {
state = aml_region_read_simple(&handle, i, &value);
if (state == -1) {
goto out;
}
readval |= (value << (i * 8));
}
AML_DEBUGPRINT("\t[%d:0x%x@0x%x:%d,%d]",
regtype, readval, handle.addr, offset, bitlen);
}
switch (io) {
case AML_REGION_INPUT:
AML_DEBUGPRINT("\n");
readval = aml_adjust_readvalue(flags, offset, bitlen, readval);
value = readval;
value = aml_region_prompt_read(&handle, value);
state = aml_region_prompt_update_value(readval, value, &handle);
if (state == -1) {
goto out;
}
*valuep = value;
break;
case AML_REGION_OUTPUT:
value = *valuep;
value = aml_adjust_updatevalue(flags, offset,
bitlen, readval, value);
value = aml_region_prompt_write(&handle, value);
AML_DEBUGPRINT("\t->[%d:0x%x@0x%x:%d,%d]\n", regtype, value,
handle.addr, offset, bitlen);
for (i = 0; i < bytelen; i += handle.unit) {
state = aml_region_write_simple(&handle, i, value);
if (state == -1) {
goto out;
}
value = value >> (handle.unit * 8);
}
break;
}
aml_region_handle_free(&handle);
out:
return (state);
}
int
aml_region_io(struct aml_environ *env, int io, int regtype,
u_int32_t flags, u_int32_t *valuep, u_int32_t baseaddr,
u_int32_t bitoffset, u_int32_t bitlen)
{
u_int32_t unit, offset;
u_int32_t offadj, bitadj;
u_int32_t value, readval;
int state, i;
readval = 0;
state = 0;
unit = 4; /* limitation of 32 bits alignment */
offset = bitoffset % (unit * 8);
offadj = 0;
bitadj = 0;
if (offset + bitlen > unit * 8) {
bitadj = bitlen - (unit * 8 - offset);
}
for (i = 0; i < offset + bitlen; i += unit * 8) {
value = (*valuep) >> offadj;
state = aml_region_io_simple(env, io, regtype, flags,
&value, baseaddr, bitoffset + offadj, bitlen - bitadj);
if (state == -1) {
goto out;
}
readval |= value << offadj;
bitadj = offadj = bitlen - bitadj;
}
*valuep = readval;
out:
return (state);
}
|