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