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