]> git.saurik.com Git - apple/xnu.git/blob - iokit/Drivers/platform/drvAppleNVRAM/AppleNVRAM.cpp
27b69da0e4bcb7e75fc7e206a1bc434cc74c1592
[apple/xnu.git] / iokit / Drivers / platform / drvAppleNVRAM / AppleNVRAM.cpp
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 #include <IOKit/IOLib.h>
24 #include "AppleNVRAM.h"
25
26
27 #define super IONVRAMController
28 OSDefineMetaClassAndStructors(AppleNVRAM, IONVRAMController);
29
30
31 // ****************************************************************************
32 // start
33 //
34 // ****************************************************************************
35 bool AppleNVRAM::start(IOService *provider)
36 {
37 IOItemCount numRanges;
38 IOMemoryMap *map;
39
40 numRanges = provider->getDeviceMemoryCount();
41
42 if (numRanges == 1) {
43 _nvramType = kNVRAMTypeIOMem;
44
45 // Get the address of the data register.
46 map = provider->mapDeviceMemoryWithIndex(0);
47 if (map == 0) return false;
48 _nvramData = (UInt8 *)map->getVirtualAddress();
49
50 } else if (numRanges == 2) {
51 _nvramType = kNVRAMTypePort;
52
53 // Get the address of the port register.
54 map = provider->mapDeviceMemoryWithIndex(0);
55 if (map == 0) return false;
56 _nvramPort = (UInt8 *)map->getVirtualAddress();
57
58 // Get the address of the data register.
59 map = provider->mapDeviceMemoryWithIndex(1);
60 if (map == 0) return false;
61 _nvramData = (UInt8 *)map->getVirtualAddress();
62
63 } else {
64 return false;
65 }
66
67 return super::start(provider);
68 }
69
70 // ****************************************************************************
71 // read
72 //
73 // Read data from the NVRAM and return it in buffer.
74 //
75 // ****************************************************************************
76 IOReturn AppleNVRAM::read(IOByteCount offset, UInt8 *buffer,
77 IOByteCount length)
78 {
79 UInt32 cnt;
80
81 if ((buffer == 0) || (length <= 0) || (offset < 0) ||
82 (offset + length > kNVRAMImageSize))
83 return kIOReturnBadArgument;
84
85 switch (_nvramType) {
86 case kNVRAMTypeIOMem :
87 for (cnt = 0; cnt < length; cnt++) {
88 buffer[cnt] = _nvramData[(offset + cnt) << 4];
89 }
90 break;
91
92 case kNVRAMTypePort:
93 for (cnt = 0; cnt < length; cnt++) {
94 *_nvramPort = (offset + length) >> 5;
95 eieio();
96 buffer[cnt] = _nvramData[((offset + length) & 0x1F) << 4];
97 }
98 break;
99
100 default :
101 return kIOReturnNotReady;
102 }
103
104 return kIOReturnSuccess;
105 }
106
107
108 // ****************************************************************************
109 // write
110 //
111 // Write data from buffer into NVRAM.
112 //
113 // ****************************************************************************
114 IOReturn AppleNVRAM::write(IOByteCount offset, UInt8 *buffer,
115 IOByteCount length)
116 {
117 UInt32 cnt;
118
119 if ((buffer == 0) || (length <= 0) || (offset < 0) ||
120 (offset + length > kNVRAMImageSize))
121 return kIOReturnBadArgument;
122
123 switch (_nvramType) {
124 case kNVRAMTypeIOMem :
125 for (cnt = 0; cnt < length; cnt++) {
126 _nvramData[(offset + cnt) << 4] = buffer[cnt];
127 eieio();
128 }
129 break;
130
131 case kNVRAMTypePort:
132 for (cnt = 0; cnt < length; cnt++) {
133 *_nvramPort = (offset + length) >> 5;
134 eieio();
135 _nvramData[((offset + length) & 0x1F) << 4] = buffer[cnt];
136 eieio();
137 }
138 break;
139
140 default :
141 return kIOReturnNotReady;
142 }
143
144 return kIOReturnSuccess;
145 }