/
Heathrow.cpp
1121 lines (904 loc) · 34.5 KB
/
Heathrow.cpp
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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* The contents of this file constitute Original Code as defined in and
* are subject to the Apple Public Source License Version 1.1 (the
* "License"). You may not use this file except in compliance with the
* License. Please obtain a copy of the License at
* http://www.apple.com/publicsource and read it before using this file.
*
* This Original Code and all software distributed under the License are
* distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/*
* Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
*
* DRI: Robert Zhang
*
*/
#include <ppc/proc_reg.h>
#include <IOKit/IOLib.h>
#include <IOKit/IODeviceTreeSupport.h>
#include <IOKit/IODeviceMemory.h>
#include <IOKit/IOPlatformExpert.h>
#include <IOKit/platform/ApplePlatformExpert.h>
#include <IOKit/platform/AppleNMI.h>
#include "Heathrow.h"
#include <IOKit/ppc/IODBDMA.h>
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#define super AppleMacIO
OSDefineMetaClassAndStructors(Heathrow, AppleMacIO);
bool Heathrow::start(IOService *provider)
{
bool ret;
// Call MacIO's start.
if (!super::start(provider))
return false;
// callPlatformFunction symbols
heathrow_enableSCC = OSSymbol::withCString("EnableSCC");
heathrow_powerModem = OSSymbol::withCString("PowerModem");
heathrow_modemResetLow = OSSymbol::withCString("ModemResetLow");
heathrow_modemResetHigh = OSSymbol::withCString("ModemResetHigh");
heathrow_sleepState = OSSymbol::withCString("heathrow_sleepState");
heathrow_powerMediaBay = OSSymbol::withCString("powerMediaBay");
heathrow_set_light = OSSymbol::withCString("heathrow_set_light");
heathrow_writeRegUInt8 = OSSymbol::withCString("heathrow_writeRegUInt8");
heathrow_safeWriteRegUInt8 = OSSymbol::withCString("heathrow_safeWriteRegUInt8");
heathrow_safeReadRegUInt8 = OSSymbol::withCString("heathrow_safeReadRegUInt8");
heathrow_safeWriteRegUInt32 = OSSymbol::withCString("heathrow_safeWriteRegUInt32");
heathrow_safeReadRegUInt32 = OSSymbol::withCString("heathrow_safeReadRegUInt32");
// just initializes this:
mediaIsOn = true;
// sets up the mutex lock:
mutex = IOSimpleLockAlloc();
// Figure out which heathrow this is.
if (IODTMatchNubWithKeys(provider, "heathrow"))
heathrowNum = kPrimaryHeathrow;
else if (IODTMatchNubWithKeys(provider, "gatwick"))
heathrowNum = kSecondaryHeathrow;
else return false; // This should not happen.
if (heathrowNum == kPrimaryHeathrow) {
if (getPlatform()->getChipSetType() != kChipSetTypePowerExpress)
getPlatform()->setCPUInterruptProperties(provider);
}
// get the base address of the this heathrow.
heathrowBaseAddress = fMemory->getVirtualAddress();
// Make nubs for the children.
publishBelow( provider );
ret = installInterrupts(provider);
// register iteself so we can find it:
registerService();
// mark the current state as invalid:
savedState.thisStateIsValid = false;
// attach to the power managment tree:
initForPM(provider);
//kprintf("Heathrow::start(%s) %d\n", provider->getName(), ret);
return ret;
}
IOReturn Heathrow::callPlatformFunction(const OSSymbol *functionName,
bool waitForFunction,
void *param1, void *param2,
void *param3, void *param4)
{
if (functionName == heathrow_sleepState)
{
sleepState((bool)param1);
return kIOReturnSuccess;
}
if (functionName == heathrow_powerMediaBay) {
bool powerOn = (param1 != NULL);
powerMediaBay(powerOn, (UInt8)param2);
return kIOReturnSuccess;
}
if (functionName == heathrow_enableSCC)
{
EnableSCC((bool)param1);
return kIOReturnSuccess;
}
if (functionName == heathrow_powerModem)
{
PowerModem((bool)param1);
return kIOReturnSuccess;
}
if (functionName == heathrow_modemResetLow)
{
ModemResetLow();
return kIOReturnSuccess;
}
if (functionName == heathrow_modemResetHigh)
{
ModemResetHigh();
return kIOReturnSuccess;
}
if (functionName == heathrow_set_light)
{
setChassisLightFullpower((bool)param1);
return kIOReturnSuccess;
}
if (functionName == heathrow_writeRegUInt8)
{
writeRegUInt8(*(unsigned long *)param1, (UInt8)param2);
return kIOReturnSuccess;
}
if (functionName == heathrow_safeWriteRegUInt8)
{
safeWriteRegUInt8((unsigned long)param1, (UInt8)param2, (UInt8)param3);
return kIOReturnSuccess;
}
if (functionName == heathrow_safeReadRegUInt8)
{
UInt8 *returnval = (UInt8 *)param2;
*returnval = safeReadRegUInt8((unsigned long)param1);
return kIOReturnSuccess;
}
if (functionName == heathrow_safeWriteRegUInt32)
{
safeWriteRegUInt32((unsigned long)param1, (UInt32)param2, (UInt32)param3);
return kIOReturnSuccess;
}
if (functionName == heathrow_safeReadRegUInt32)
{
UInt32 *returnval = param2;
*returnval = safeReadRegUInt32((unsigned long)param1);
return kIOReturnSuccess;
}
return super::callPlatformFunction(functionName, waitForFunction, param1, param2, param3, param4);
}
void Heathrow::EnableSCC(bool state)
{
IOInterruptState intState;
if (state)
{
if ( mutex != NULL )
intState = IOSimpleLockLockDisableInterrupt(mutex);
// Enables the SCC cell: 0x00420000 (this starts scc clock and enables scca)
*(UInt32*)(heathrowBaseAddress + heathrowFCROffset) |= ( heathrowFCSCCCEn | heathrowFCSCCAEn );
if ( mutex != NULL )
IOSimpleLockUnlockEnableInterrupt(mutex, intState);
if ( mutex != NULL )
intState = IOSimpleLockLockDisableInterrupt(mutex);
// Resets the SCC:
*(UInt32*)(heathrowBaseAddress + heathrowFCROffset) |= heathrowFCResetSCC;
if ( mutex != NULL )
IOSimpleLockUnlockEnableInterrupt(mutex, intState);
IOSleep(15);
if ( mutex != NULL )
intState = IOSimpleLockLockDisableInterrupt(mutex);
*(UInt32*)(heathrowBaseAddress + heathrowFCROffset) &= ~heathrowFCResetSCC;
if ( mutex != NULL )
IOSimpleLockUnlockEnableInterrupt(mutex, intState);
}
else
{
// disable
}
return;
}
void Heathrow::PowerModem(bool state)
{
// On PowerMac 1,2 () the modem is always on and this bit
// disables the nvram. So on Yikes we exit without doing
// anything. Also setting this bit has as bas nvram side
// effects.
IOInterruptState intState;
if (IODTMatchNubWithKeys(getPlatform()->getProvider(), "'PowerMac1,1'") ||
IODTMatchNubWithKeys(getPlatform()->getProvider(), "'PowerMac1,2'"))
return;
if ( mutex != NULL )
intState = IOSimpleLockLockDisableInterrupt(mutex);
if (state)
{
*(UInt32*)(heathrowBaseAddress + heathrowFCROffset) &= ~heathrowFCTrans;
eieio();
}
else
{
*(UInt32*)(heathrowBaseAddress + heathrowFCROffset) |= heathrowFCTrans;
eieio();
}
if ( mutex != NULL )
IOSimpleLockUnlockEnableInterrupt(mutex, intState);
return;
}
void Heathrow::ModemResetLow()
{
IOInterruptState intState;
if ( mutex != NULL )
intState = IOSimpleLockLockDisableInterrupt(mutex);
*(UInt32*)(heathrowBaseAddress + heathrowFCROffset) &= ~( heathrowFCSCCCEn | heathrowFCSCCAEn );
if ( mutex != NULL )
IOSimpleLockUnlockEnableInterrupt(mutex, intState);
}
void Heathrow::ModemResetHigh()
{
IOInterruptState intState;
if ( mutex != NULL )
intState = IOSimpleLockLockDisableInterrupt(mutex);
*(UInt32*)(heathrowBaseAddress + heathrowFCROffset) |= ( heathrowFCSCCCEn | heathrowFCSCCAEn );
if ( mutex != NULL )
IOSimpleLockUnlockEnableInterrupt(mutex, intState);
}
UInt8 Heathrow::readRegUInt8(unsigned long offset)
{
return *(UInt8 *)(heathrowBaseAddress + offset);
}
void Heathrow::writeRegUInt8(unsigned long offset, UInt8 data)
{
*(UInt8 *)(heathrowBaseAddress + offset) = data;
eieio();
}
void Heathrow::safeWriteRegUInt8(unsigned long offset, UInt8 mask, UInt8 data)
{
IOInterruptState intState;
if ( mutex != NULL )
intState = IOSimpleLockLockDisableInterrupt(mutex);
UInt8 currentReg = readRegUInt8(offset);
currentReg = (currentReg & ~mask) | (data & mask);
writeRegUInt8(offset, currentReg);
if ( mutex != NULL )
IOSimpleLockUnlockEnableInterrupt(mutex, intState);
}
UInt8 Heathrow::safeReadRegUInt8(unsigned long offset)
{
IOInterruptState intState;
if ( mutex != NULL )
intState = IOSimpleLockLockDisableInterrupt(mutex);
UInt8 currentReg = readRegUInt8(offset);
if ( mutex != NULL )
IOSimpleLockUnlockEnableInterrupt(mutex, intState);
return (currentReg);
}
UInt32 Heathrow::readRegUInt32(unsigned long offset)
{
return lwbrx(heathrowBaseAddress + offset);
}
void Heathrow::writeRegUInt32(unsigned long offset, UInt32 data)
{
stwbrx(data, heathrowBaseAddress + offset);
eieio();
}
void Heathrow::safeWriteRegUInt32(unsigned long offset, UInt32 mask, UInt32 data)
{
IOInterruptState intState;
if ( mutex != NULL )
intState = IOSimpleLockLockDisableInterrupt(mutex);
UInt32 currentReg = readRegUInt32(offset);
currentReg = (currentReg & ~mask) | (data & mask);
writeRegUInt32(offset, currentReg);
if ( mutex != NULL )
IOSimpleLockUnlockEnableInterrupt(mutex, intState);
}
UInt32 Heathrow::safeReadRegUInt32(unsigned long offset)
{
IOInterruptState intState;
if ( mutex != NULL )
intState = IOSimpleLockLockDisableInterrupt(mutex);
UInt32 currentReg = readRegUInt32(offset);
if ( mutex != NULL )
IOSimpleLockUnlockEnableInterrupt(mutex, intState);
return (currentReg);
}
// --------------------------------------------------------------------------
// Method: initForPM
//
// Purpose:
// initialize the driver for power managment and register ourselves with
// superclass policy-maker
void Heathrow::initForPM (IOService *provider)
{
PMinit(); // initialize superclass variables
provider->joinPMtree(this); // attach into the power management hierarchy
// KeyLargo has only 2 power states::
// 0 OFF
// 1 all ON
// Pwer state fields:
// unsigned long version; // version number of this struct
// IOPMPowerFlags capabilityFlags; // bits that describe (to interested drivers) the capability of the device in this state
// IOPMPowerFlags outputPowerCharacter; // description (to power domain children) of the power provided in this state
// IOPMPowerFlags inputPowerRequirement; // description (to power domain parent) of input power required in this state
// unsigned long staticPower; // average consumption in milliwatts
// unsigned long unbudgetedPower; // additional consumption from separate power supply (mw)
// unsigned long powerToAttain; // additional power to attain this state from next lower state (in mw)
// unsigned long timeToAttain; // time required to enter this state from next lower state (in microseconds)
// unsigned long settleUpTime; // settle time required after entering this state from next lower state (microseconds)
// unsigned long timeToLower; // time required to enter next lower state from this one (in microseconds)
// unsigned long settleDownTime; // settle time required after entering next lower state from this state (microseconds)
// unsigned long powerDomainBudget; // power in mw a domain in this state can deliver to its children
// NOTE: all these values are made up since now I do not have areal clue of what to put.
#define number_of_power_states 2
static IOPMPowerState ourPowerStates[number_of_power_states] = {
{1,0,0,0,0,0,0,0,0,0,0,0},
{1,IOPMDeviceUsable,IOPMPowerOn,IOPMPowerOn,0,0,0,0,0,0,0,0}
};
// register ourselves with ourself as policy-maker
if (pm_vars != NULL)
registerPowerDriver(this, ourPowerStates, number_of_power_states);
}
// Method: setPowerState
//
// VERY IMPORTANT NOTE:
// sleepState(bool) can be called from here or directly. This is NOT an oversight.
// What I am trying to resolve here is a problem with those powerbooks that have
// 2 Heathrow chips. In these machines the main Heathrow should be powered on
// in the CPU driver, and the second here. Since the HeathrowState holds a bit
// to remeber if the state is valid, and such a bit is cleared once the state is
// restored I am sure that I am not going to overwrite a valid state with an (older)
// invalid one.
IOReturn Heathrow::setPowerState(unsigned long powerStateOrdinal, IOService* whatDevice)
{
if ( powerStateOrdinal == 0 ) {
if (heathrowNum == kSecondaryHeathrow) {
kprintf("Gatwick would be powered off here\n");
sleepState(true);
}
else if (heathrowNum == kPrimaryHeathrow) {
kprintf("Heathrow would be powered off here\n");
*((unsigned long*)(heathrowBaseAddress + heathrowFCROffset)) &= ~heathrowFCIOBusEn;
OSSynchronizeIO();
*((unsigned long*)(heathrowBaseAddress + heathrowFCROffset)) &= ~heathrowFCATA0Reset;
OSSynchronizeIO();
}
}
if ( powerStateOrdinal == 1 ) {
if (heathrowNum == kSecondaryHeathrow) {
kprintf("Gatwick would be powered on here\n");
sleepState(false);
}
else if (heathrowNum == kPrimaryHeathrow) {
kprintf("Heathrow would be powered on here\n");
*((unsigned long*)(heathrowBaseAddress + heathrowFCROffset)) |= heathrowFCIOBusEn;
OSSynchronizeIO();
*((unsigned long*)(heathrowBaseAddress + heathrowFCROffset)) |= heathrowFCATA0Reset;
OSSynchronizeIO();
}
}
return IOPMAckImplied;
}
bool Heathrow::installInterrupts(IOService *provider)
{
IORegistryEntry *regEntry;
OSSymbol *interruptControllerName;
//IOInterruptAction is typedefed as a function ptr in xnu/iokit/IOKit/IOService.h
IOInterruptAction handler;
AppleNMI *appleNMI;
long nmiSource;
OSData *nmiData;
IOReturn error;
// Everything below here is for interrupts, return true if
// interrupts are not needed.
if (getPlatform()->getChipSetType() == kChipSetTypePowerExpress) return true;
// get the name of the interrupt controller
if( (regEntry = provider->childFromPath("interrupt-controller",
gIODTPlane))) {
interruptControllerName = (OSSymbol *)IODTInterruptControllerName(regEntry);
regEntry->release();
} else
interruptControllerName = getInterruptControllerName();
// Allocate the interruptController instance.
interruptController = new HeathrowInterruptController;
if (interruptController == NULL) return false;
// call the interruptController's init method.
error = interruptController->initInterruptController(provider, heathrowBaseAddress);
if (error != kIOReturnSuccess) return false;
// and clears the interrupt state:
interruptController->clearAllInterrupts();
handler = interruptController->getInterruptHandlerAddress();
provider->registerInterrupt(0, interruptController, handler, 0);
provider->enableInterrupt(0);
// Register the interrupt controller so clients can find it.
getPlatform()->registerInterruptController(interruptControllerName,
interruptController);
if (heathrowNum != kPrimaryHeathrow) return true;
// Create the NMI Driver.
nmiSource = 20;
nmiData = OSData::withBytes(&nmiSource, sizeof(long));
appleNMI = new AppleNMI;
if ((nmiData != 0) && (appleNMI != 0)) {
appleNMI->initNMI(interruptController, nmiData);
}
return true;
}
OSSymbol *Heathrow::getInterruptControllerName(void)
{
OSSymbol *interruptControllerName;
switch (heathrowNum) {
case kPrimaryHeathrow :
interruptControllerName = (OSSymbol *)gIODTDefaultInterruptController;
break;
case kSecondaryHeathrow :
interruptControllerName = (OSSymbol *)OSSymbol::withCStringNoCopy("SecondaryInterruptController");
break;
default:
interruptControllerName = (OSSymbol *)OSSymbol::withCStringNoCopy("UnknownInterruptController");
break;
}
return interruptControllerName;
}
void Heathrow::enableMBATA()
{
unsigned long heathrowIDs, heathrowFCR;
heathrowIDs = lwbrx(heathrowBaseAddress + heathrowIDOffset);
if ((heathrowIDs & 0x0000FF00) == 0x00003000) {
heathrowFCR = lwbrx(heathrowBaseAddress + heathrowFCROffset);
//this corresponds to heathrowFCATA1Reset in big Endian(bit 23 in little E)
heathrowFCR |= 0x00800000;
stwbrx(heathrowFCR, heathrowBaseAddress + heathrowFCROffset);
IODelay(100);
}
}
void Heathrow::powerMediaBay(bool powerOn, UInt8 deviceOn)
{
unsigned long heathrowIDs;
unsigned long powerDevice = deviceOn;
//kprintf("Heathrow::powerMediaBay(%s) 0x%02x\n", (powerOn ? "TRUE" : "FALSE"), powerDevice);
///kprintf(" 0 Heathrow::powerMediaBay = 0x%08lx\n", lwbrx(heathrowBaseAddress + heathrowFCROffset));
if (mediaIsOn == powerOn)
return;
// Align the bits of the power device:
powerDevice = powerDevice << 26;
powerDevice &= heathrowFCMediaBaybits;
heathrowIDs = lwbrx(heathrowBaseAddress + heathrowIDOffset);
if ((heathrowIDs & 0x0000F000) != 0x00007000) {
unsigned long *heathrowFCR = (unsigned long*)(heathrowBaseAddress + heathrowFCROffset);
kprintf(" 1 Heathrow::powerMediaBay = 0x%08lx\n", *heathrowFCR);
// make sure media bay is in reset (MB reset bit is low)
*heathrowFCR &= ~heathrowFCMBReset;
eieio();
if (powerOn) {
// we are powering on the bay and need a delay between turning on
// media bay power and enabling the bus
*heathrowFCR &= ~heathrowFCMBPwr;
eieio();
IODelay(50000);
}
// to turn on the buses, we ensure all buses are off and then turn on the ata bus
*heathrowFCR &= ~heathrowFCMediaBaybits;
eieio();
*heathrowFCR |= powerDevice;
eieio();
if (!powerOn) {
// turn off media bay power
*heathrowFCR |= heathrowFCMBPwr;
eieio();
}
else {
// take us out of reset
*heathrowFCR |= heathrowFCMBReset;
eieio();
enableMBATA();
}
IODelay(50000);
//kprintf(" 2 Heathrow::powerMediaBay = 0x%08lx\n", *heathrowFCR);
//kprintf(" 3 Heathrow::powerMediaBay = 0x%08lx\n", lwbrx(heathrowBaseAddress + heathrowFCROffset));
}
mediaIsOn = powerOn;
}
void Heathrow::processNub(IOService *nub)
{
int cnt, numSources;
OSArray *controllerNames, *controllerSources;
OSSymbol *interruptControllerName;
char *nubName;
nubName = (char *)nub->getName();
if (!strcmp(nubName, "media-bay")) {
enableMBATA();
}
// change the interrupt controller name for this nub
// if it is on the secondary heathrow.
if (heathrowNum == kPrimaryHeathrow) return;
interruptControllerName = getInterruptControllerName();
if (!strcmp(nubName, "media-bay")) {
controllerSources = OSDynamicCast(OSArray, getProperty("vectors-media-bay"));
} else if (!strcmp(nubName, "ch-a")) {
controllerSources = OSDynamicCast(OSArray, getProperty("vectors-escc-ch-a"));
} else if (!strcmp(nubName, "floppy")) {
controllerSources = OSDynamicCast(OSArray, getProperty("vectors-floppy"));
} else if (!strcmp(nubName, "ata4")) {
controllerSources = OSDynamicCast(OSArray, getProperty("vectors-ata4"));
} else return;
numSources = controllerSources->getCount();
controllerNames = OSArray::withCapacity(numSources);
for (cnt = 0; cnt < numSources; cnt++) {
controllerNames->setObject(interruptControllerName);
}
nub->setProperty(gIOInterruptControllersKey, controllerNames);
nub->setProperty(gIOInterruptSpecifiersKey, controllerSources);
}
// Set the color of the front panel light on desktop Gossamers.
void Heathrow::setChassisLightFullpower(bool fullpwr)
{
if (fullpwr)
{
*(UInt32*)(heathrowBaseAddress + kChassisLightColor) = 0x00000000;
eieio();
}
else
{
*(UInt32*)(heathrowBaseAddress + kChassisLightColor) = 0xffffffff;
eieio();
}
}
// If sleepMe is true places heatrow to sleep,
// Otherwise wakes it up.
void Heathrow::sleepState(bool sleepMe)
{
if (sleepMe) {
// Saves the state and creates the conditions for sleep:
// Disables and saves all the interrupts:
//kprintf("Heathrow::sleepState saveInterruptState\n");
saveInterruptState();
// Saves all the DMA registers:
//kprintf("Heathrow::sleepState saveDMAState\n");
saveDMAState();
// Saves the VIA registers:
//kprintf("Heathrow::sleepState saveVIAState\n");
saveVIAState();
// Saves the GP registers:
//kprintf("Heathrow::sleepState saveGPState\n");
saveGPState();
// Defines the state as valid:
savedState.thisStateIsValid = true;
}
else if (savedState.thisStateIsValid) {
// Restores the GP registers:
//kprintf("Heathrow::sleepState restoreGPState\n");
restoreGPState();
// Wakes up and restores the state:
//kprintf("Heathrow::sleepState restoreVIAState\n");
restoreVIAState();
// Restores the DMA registers:
//kprintf("Heathrow::sleepState restoreDMAState\n");
restoreDMAState();
// Restores and enables the interrupts:
//kprintf("Heathrow::sleepState restoreInterruptState\n");
restoreInterruptState();
// This state is no more valid:
savedState.thisStateIsValid = false;
// Turn on the media bay if necessary.
enableMBATA();
}
}
void Heathrow::saveInterruptState()
{
// Save the interrupt state
savedState.interruptMask1 = *(UInt32*)(heathrowBaseAddress + kMask1Offset);
eieio();
savedState.interruptMask2 = *(UInt32*)(heathrowBaseAddress + kMask2Offset);
eieio();
}
void Heathrow::restoreInterruptState()
{
// Clears all the possible pending interrupts
*(UInt32*)(heathrowBaseAddress + kClear1Offset) = 0xFFFFFFFF;
eieio();
*(UInt32*)(heathrowBaseAddress + kClear2Offset) = 0xFFFFFFFF;
eieio();
// Restores the interrupts
*(UInt32*)(heathrowBaseAddress + kMask1Offset) = savedState.interruptMask1;
eieio();
*(UInt32*)(heathrowBaseAddress + kMask2Offset) = savedState.interruptMask2;
eieio();
// Clears all the possible pending interrupts (again)
*(UInt32*)(heathrowBaseAddress + kClear1Offset) = 0xFFFFFFFF;
eieio();
*(UInt32*)(heathrowBaseAddress + kClear2Offset) = 0xFFFFFFFF;
eieio();
}
void Heathrow::saveGPState()
{
savedState.featureControlReg = *(UInt32*)(heathrowBaseAddress + heathrowFCROffset);
savedState.auxControlReg = *(UInt32*)(heathrowBaseAddress + heathrowAUXFCROffset);
}
void Heathrow::restoreGPState()
{
*(UInt32*)(heathrowBaseAddress + heathrowFCROffset) = savedState.featureControlReg;
eieio();
IODelay(1000);
*(UInt32*)(heathrowBaseAddress + heathrowAUXFCROffset) = savedState.auxControlReg;
eieio();
IODelay(1000);
}
void Heathrow::saveDMAState()
{
int i;
UInt32 channelOffset;
for (i = 0, channelOffset = 0; i <= 12; i++, channelOffset += 0x0100)
{
volatile DBDMAChannelRegisters* currentChannel;
currentChannel = (volatile DBDMAChannelRegisters *) (heathrowBaseAddress + 0x8000 + channelOffset);
savedState.savedDBDMAState[i].commandPtrLo = IOGetDBDMACommandPtr(currentChannel);
savedState.savedDBDMAState[i].interruptSelect = IOGetDBDMAInterruptSelect(currentChannel);
savedState.savedDBDMAState[i].branchSelect = IOGetDBDMABranchSelect(currentChannel);
savedState.savedDBDMAState[i].waitSelect = IOGetDBDMAWaitSelect(currentChannel);
}
}
void Heathrow::restoreDMAState()
{
int i;
UInt32 channelOffset;
for (i = 0, channelOffset = 0; i <=12; i++, channelOffset += 0x0100)
{
volatile DBDMAChannelRegisters* currentChannel;
currentChannel = (volatile DBDMAChannelRegisters *) (heathrowBaseAddress + 0x8000 + channelOffset);
IODBDMAReset((IODBDMAChannelRegisters*)currentChannel);
IOSetDBDMACommandPtr(currentChannel, savedState.savedDBDMAState[i].commandPtrLo);
IOSetDBDMAInterruptSelect(currentChannel, savedState.savedDBDMAState[i].interruptSelect);
IOSetDBDMABranchSelect(currentChannel, savedState.savedDBDMAState[i].branchSelect);
IOSetDBDMAWaitSelect(currentChannel, savedState.savedDBDMAState[i].waitSelect);
}
}
void Heathrow::saveVIAState(void)
{
UInt8* viaBase = (UInt8*)heathrowBaseAddress + heathrowVIAOffset;
UInt8* savedViaState = savedState.savedVIAState;
// Save VIA state. These registers don't seem to get restored to any known state.
savedViaState[0] = *(UInt8*)(viaBase + vBufA);
savedViaState[1] = *(UInt8*)(viaBase + vDIRA);
savedViaState[2] = *(UInt8*)(viaBase + vBufB);
savedViaState[3] = *(UInt8*)(viaBase + vDIRB);
savedViaState[4] = *(UInt8*)(viaBase + vPCR);
savedViaState[5] = *(UInt8*)(viaBase + vACR);
savedViaState[6] = *(UInt8*)(viaBase + vIER);
savedViaState[7] = *(UInt8*)(viaBase + vT1C);
savedViaState[8] = *(UInt8*)(viaBase + vT1CH);
}
void Heathrow::restoreVIAState(void)
{
UInt8* viaBase = (UInt8*)heathrowBaseAddress + heathrowVIAOffset;
UInt8* savedViaState = savedState.savedVIAState;
// Restore VIA state. These registers don't seem to get restored to any known state.
*(UInt8*)(viaBase + vBufA) = savedViaState[0];
eieio();
*(UInt8*)(viaBase + vDIRA) = savedViaState[1];
eieio();
*(UInt8*)(viaBase + vBufB) = savedViaState[2];
eieio();
*(UInt8*)(viaBase + vDIRB) = savedViaState[3];
eieio();
*(UInt8*)(viaBase + vPCR) = savedViaState[4];
eieio();
*(UInt8*)(viaBase + vACR) = savedViaState[5];
eieio();
*(UInt8*)(viaBase + vIER) = savedViaState[6];
eieio();
*(UInt8*)(viaBase + vT1C) = savedViaState[7];
eieio();
*(UInt8*)(viaBase + vT1CH) = savedViaState[8];
eieio();
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#undef super
#define super IOInterruptController
OSDefineMetaClassAndStructors(HeathrowInterruptController, IOInterruptController);
IOReturn HeathrowInterruptController::initInterruptController(IOService *provider, IOLogicalAddress iBase)
{
int cnt;
interruptControllerBase = iBase;
parentNub = provider;
// Allocate the task lock.
taskLock = IOLockAlloc();
if (taskLock == 0) return kIOReturnNoResources;
// Allocate the memory for the vectors
vectors = (IOInterruptVector *)IOMalloc(kNumVectors * sizeof(IOInterruptVector));
if (vectors == NULL) {
IOLockFree(taskLock);
return kIOReturnNoMemory;
}
bzero(vectors, kNumVectors * sizeof(IOInterruptVector));
// Allocate locks
for (cnt = 0; cnt < kNumVectors; cnt++) {
vectors[cnt].interruptLock = IOLockAlloc();
if (vectors[cnt].interruptLock == NULL) {
IOLockFree(taskLock);
for (cnt = 0; cnt < kNumVectors; cnt++) {
if (vectors[cnt].interruptLock != NULL)
IOLockFree(vectors[cnt].interruptLock);
}
return kIOReturnNoResources;
}
}
// Setup the registers accessors
events1Reg = (unsigned long)(interruptControllerBase + kEvents1Offset);
events2Reg = (unsigned long)(interruptControllerBase + kEvents2Offset);
mask1Reg = (unsigned long)(interruptControllerBase + kMask1Offset);
mask2Reg = (unsigned long)(interruptControllerBase + kMask2Offset);
clear1Reg = (unsigned long)(interruptControllerBase + kClear1Offset);
clear2Reg = (unsigned long)(interruptControllerBase + kClear2Offset);
levels1Reg = (unsigned long)(interruptControllerBase + kLevels1Offset);
levels2Reg = (unsigned long)(interruptControllerBase + kLevels2Offset);
return kIOReturnSuccess;
}
void HeathrowInterruptController::clearAllInterrupts(void)
{
// Initialize the registers.
// Disable all interrupts.
stwbrx(0x00000000, mask1Reg);
stwbrx(0x00000000, mask2Reg);
eieio();
// Clear all pending interrupts.
stwbrx(0xFFFFFFFF, clear1Reg);
stwbrx(0xFFFFFFFF, clear2Reg);
eieio();
// Disable all interrupts. (again?)
stwbrx(0x00000000, mask1Reg);
stwbrx(0x00000000, mask2Reg);
eieio();
}
//returns the address of the handler function and casts it into type IOInterruptAction
//IOInterruptAction is typedeffed in iokit/IOKit/IOService.h as
//ypedef void (*IOInterruptAction)( OSObject * target, void * refCon,
// IOService * nub, int source );
IOInterruptAction HeathrowInterruptController::getInterruptHandlerAddress(void)
{
return (IOInterruptAction)&HeathrowInterruptController::handleInterrupt;
}
IOReturn HeathrowInterruptController::handleInterrupt(void * /*refCon*/,
IOService * /*nub*/,
int /*source*/)
{
int done;
long events, vectorNumber;
//Defined as a struct in iokit/IOKit/IOInterruptController.h, one of its elements is handler which is
//of type IOInterruptHandler
IOInterruptVector *vector;
unsigned long maskTmp;
do {
done = 1;
// Do all the sources for events1, plus any pending interrupts.
// Also add in the "level" sensitive sources
maskTmp = lwbrx(mask1Reg);
//clear external interrupts bits InterruptEvents register, which is read-only
events = lwbrx(events1Reg) & ~kTypeLevelMask;
//now takes care of external interrupts bits with consideration of InterruptLevels register
events |= lwbrx(levels1Reg) & maskTmp & kTypeLevelMask;
//now takes care of pending bits
events |= pendingEvents1 & maskTmp;
pendingEvents1 = 0;
eieio();
// Since we have to clear the level'd one clear the current edge's too.
//set external interrupts bits to 1 and as a result clears all the those bits in InterruptEvents register
stwbrx(kTypeLevelMask | events, clear1Reg);
eieio();
if (events) done = 0;
while (events) {
vectorNumber = 31 - cntlzw(events);
events ^= (1 << vectorNumber);
vector = &vectors[vectorNumber];
vector->interruptActive = 1;
sync();
isync();
if (!vector->interruptDisabledSoft) {
isync();
// Call the handler if it exists.
if (vector->interruptRegistered) {
//handler is of type IOInterruptHandler which is typedefed in iokit/IOKit/IOInterrupts.h as a func ptr
vector->handler(vector->target, vector->refCon,
vector->nub, vector->source);
}
} else {
// Hard disable the source.
vector->interruptDisabledHard = 1;
disableVectorHard(vectorNumber, vector);
}
vector->interruptActive = 0;
}
// Do all the sources for events2, plus any pending interrupts.
maskTmp = lwbrx(mask2Reg);
events = lwbrx(events2Reg);
events |= pendingEvents1 & maskTmp;
pendingEvents2 = 0;
eieio();
if (events) {
done = 0;
stwbrx(events, clear2Reg);
eieio();
}
while (events) {
vectorNumber = 31 - cntlzw(events);
events ^= (1 << vectorNumber);
vector = &vectors[vectorNumber + kVectorsPerReg];