3673b8bf |
1 | /** @file\r |
2 | Common I/O Library routines.\r |
3 | \r |
d958721a |
4 | Copyright (c) 2006 - 2007, Intel Corporation<BR>\r |
3673b8bf |
5 | All rights reserved. This program and the accompanying materials\r |
6 | are licensed and made available under the terms and conditions of the BSD License\r |
7 | which accompanies this distribution. The full text of the license may be found at\r |
8 | http://opensource.org/licenses/bsd-license.php\r |
9 | \r |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r |
12 | \r |
13 | Module Name: IoLibIpf.c\r |
14 | \r |
15 | **/\r |
16 | \r |
3673b8bf |
17 | #define MAP_PORT_BASE_TO_MEM(_Port) \\r |
18 | ((((_Port) & 0xfffc) << 10) | ((_Port) & 0x0fff))\r |
19 | \r |
20 | /**\r |
21 | Reads a 8-bit I/O port.\r |
22 | \r |
23 | Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.\r |
24 | This function must guarantee that all I/O read and write operations are\r |
25 | serialized.\r |
26 | \r |
27 | @param Port The I/O port to read.\r |
28 | \r |
29 | @return The value read.\r |
30 | \r |
31 | **/\r |
32 | UINT8\r |
33 | EFIAPI\r |
34 | IoRead8 (\r |
35 | IN UINT64 Port\r |
36 | )\r |
37 | {\r |
38 | UINT64 Address;\r |
39 | \r |
40 | //\r |
41 | // Add the 64MB aligned IO Port space to the IO address\r |
42 | //\r |
43 | Address = MAP_PORT_BASE_TO_MEM (Port);\r |
44 | Address += PcdGet64(PcdIoBlockBaseAddressForIpf);\r |
45 | \r |
46 | return MmioRead8 (Address);\r |
47 | }\r |
48 | \r |
49 | /**\r |
50 | Reads a 16-bit I/O port.\r |
51 | \r |
52 | Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.\r |
53 | This function must guarantee that all I/O read and write operations are\r |
54 | serialized.\r |
55 | \r |
56 | @param Port The I/O port to read.\r |
57 | \r |
58 | @return The value read.\r |
59 | \r |
60 | **/\r |
61 | UINT16\r |
62 | EFIAPI\r |
63 | IoRead16 (\r |
64 | IN UINT64 Port\r |
65 | )\r |
66 | {\r |
67 | UINT64 Address;\r |
68 | \r |
69 | //\r |
70 | // Add the 64MB aligned IO Port space to the IO address\r |
71 | //\r |
72 | Address = MAP_PORT_BASE_TO_MEM (Port);\r |
73 | Address += PcdGet64(PcdIoBlockBaseAddressForIpf);\r |
74 | \r |
75 | return MmioRead16 (Address);\r |
76 | }\r |
77 | \r |
78 | /**\r |
79 | Reads a 32-bit I/O port.\r |
80 | \r |
81 | Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.\r |
82 | This function must guarantee that all I/O read and write operations are\r |
83 | serialized.\r |
84 | \r |
85 | @param Port The I/O port to read.\r |
86 | \r |
87 | @return The value read.\r |
88 | \r |
89 | **/\r |
90 | UINT32\r |
91 | EFIAPI\r |
92 | IoRead32 (\r |
93 | IN UINT64 Port\r |
94 | )\r |
95 | {\r |
96 | UINT64 Address;\r |
97 | \r |
98 | //\r |
99 | // Add the 64MB aligned IO Port space to the IO address\r |
100 | //\r |
101 | Address = MAP_PORT_BASE_TO_MEM (Port);\r |
102 | Address += PcdGet64(PcdIoBlockBaseAddressForIpf);\r |
103 | \r |
104 | return MmioRead32 (Address);\r |
105 | }\r |
106 | \r |
9cbd688b |
107 | /**\r |
108 | Reads a 64-bit I/O port.\r |
109 | \r |
110 | Reads the 64-bit I/O port specified by Port. The 64-bit read value is returned.\r |
111 | This function must guarantee that all I/O read and write operations are\r |
112 | serialized.\r |
113 | \r |
114 | If 64-bit I/O port operations are not supported, then ASSERT().\r |
115 | \r |
116 | @param Port The I/O port to read.\r |
117 | \r |
118 | @return The value read.\r |
119 | \r |
120 | **/\r |
121 | UINT64\r |
122 | EFIAPI\r |
123 | IoRead64 (\r |
124 | IN UINTN Port\r |
125 | )\r |
126 | {\r |
127 | ASSERT (FALSE);\r |
128 | return 0;\r |
129 | }\r |
130 | \r |
3673b8bf |
131 | /**\r |
132 | Writes a 8-bit I/O port.\r |
133 | \r |
134 | Writes the 8-bit I/O port specified by Port with the value specified by Value\r |
135 | and returns Value. This function must guarantee that all I/O read and write\r |
136 | operations are serialized.\r |
137 | \r |
138 | @param Port The I/O port to write.\r |
139 | @param Value The value to write to the I/O port.\r |
140 | \r |
141 | @return The value written the I/O port.\r |
142 | \r |
143 | **/\r |
144 | UINT8\r |
145 | EFIAPI\r |
146 | IoWrite8 (\r |
147 | IN UINT64 Port,\r |
148 | IN UINT8 Data\r |
149 | )\r |
150 | {\r |
151 | UINT64 Address;\r |
152 | \r |
153 | //\r |
154 | // Add the 64MB aligned IO Port space to the IO address\r |
155 | //\r |
156 | Address = MAP_PORT_BASE_TO_MEM (Port);\r |
157 | Address += PcdGet64(PcdIoBlockBaseAddressForIpf);\r |
158 | \r |
159 | return MmioWrite8 (Address, Data);\r |
160 | }\r |
161 | \r |
162 | /**\r |
163 | Writes a 16-bit I/O port.\r |
164 | \r |
165 | Writes the 16-bit I/O port specified by Port with the value specified by Value\r |
166 | and returns Value. This function must guarantee that all I/O read and write\r |
167 | operations are serialized.\r |
168 | \r |
169 | @param Port The I/O port to write.\r |
170 | @param Value The value to write to the I/O port.\r |
171 | \r |
172 | @return The value written the I/O port.\r |
173 | \r |
174 | **/\r |
175 | UINT16\r |
176 | EFIAPI\r |
177 | IoWrite16 (\r |
178 | IN UINT64 Port,\r |
179 | IN UINT16 Data\r |
180 | )\r |
181 | {\r |
182 | UINT64 Address;\r |
183 | \r |
184 | //\r |
185 | // Add the 64MB aligned IO Port space to the IO address\r |
186 | //\r |
187 | Address = MAP_PORT_BASE_TO_MEM (Port);\r |
188 | Address += PcdGet64(PcdIoBlockBaseAddressForIpf);\r |
189 | \r |
190 | return MmioWrite16 (Address, Data);\r |
191 | }\r |
192 | \r |
193 | /**\r |
194 | Writes a 32-bit I/O port.\r |
195 | \r |
196 | Writes the 32-bit I/O port specified by Port with the value specified by Value\r |
197 | and returns Value. This function must guarantee that all I/O read and write\r |
198 | operations are serialized.\r |
199 | \r |
200 | @param Port The I/O port to write.\r |
201 | @param Value The value to write to the I/O port.\r |
202 | \r |
203 | @return The value written the I/O port.\r |
204 | \r |
205 | **/\r |
206 | UINT32\r |
207 | EFIAPI\r |
208 | IoWrite32 (\r |
209 | IN UINT64 Port,\r |
210 | IN UINT32 Data\r |
211 | )\r |
212 | {\r |
213 | UINT64 Address;\r |
214 | \r |
215 | //\r |
216 | // Add the 64MB aligned IO Port space to the IO address\r |
217 | //\r |
218 | Address = MAP_PORT_BASE_TO_MEM (Port);\r |
219 | Address += PcdGet64(PcdIoBlockBaseAddressForIpf);\r |
220 | \r |
221 | return MmioWrite32 (Address, Data);\r |
222 | }\r |
223 | \r |
9cbd688b |
224 | /**\r |
225 | Writes a 64-bit I/O port.\r |
226 | \r |
227 | Writes the 64-bit I/O port specified by Port with the value specified by Value\r |
228 | and returns Value. This function must guarantee that all I/O read and write\r |
229 | operations are serialized.\r |
230 | \r |
231 | If 64-bit I/O port operations are not supported, then ASSERT().\r |
232 | \r |
233 | @param Port The I/O port to write.\r |
234 | @param Value The value to write to the I/O port.\r |
235 | \r |
236 | @return The value written the I/O port.\r |
237 | \r |
238 | **/\r |
239 | UINT64\r |
240 | EFIAPI\r |
241 | IoWrite64 (\r |
242 | IN UINTN Port,\r |
243 | IN UINT64 Value\r |
244 | )\r |
245 | {\r |
246 | ASSERT (FALSE);\r |
247 | return 0;\r |
248 | }\r |
249 | \r |
3673b8bf |
250 | /**\r |
251 | Reads a 8-bit MMIO register.\r |
252 | \r |
253 | Reads the 8-bit MMIO register specified by Address. The 8-bit read value is\r |
254 | returned. This function must guarantee that all MMIO read and write\r |
255 | operations are serialized.\r |
256 | \r |
257 | @param Address The MMIO register to read.\r |
258 | \r |
259 | @return The value read.\r |
260 | \r |
261 | **/\r |
262 | UINT8\r |
263 | EFIAPI\r |
264 | MmioRead8 (\r |
265 | IN UINT64 Address\r |
266 | )\r |
267 | {\r |
268 | UINT8 Data;\r |
269 | \r |
270 | Address |= BIT63;\r |
271 | \r |
272 | MemoryFence ();\r |
273 | Data = *((volatile UINT8 *) Address);\r |
274 | MemoryFence ();\r |
275 | \r |
276 | return Data;\r |
277 | }\r |
278 | \r |
279 | /**\r |
280 | Reads a 16-bit MMIO register.\r |
281 | \r |
282 | Reads the 16-bit MMIO register specified by Address. The 16-bit read value is\r |
283 | returned. This function must guarantee that all MMIO read and write\r |
284 | operations are serialized.\r |
285 | \r |
286 | @param Address The MMIO register to read.\r |
287 | \r |
288 | @return The value read.\r |
289 | \r |
290 | **/\r |
291 | UINT16\r |
292 | EFIAPI\r |
293 | MmioRead16 (\r |
294 | IN UINT64 Address\r |
295 | )\r |
296 | {\r |
297 | UINT16 Data;\r |
298 | \r |
299 | Address |= BIT63;\r |
300 | \r |
301 | MemoryFence ();\r |
302 | Data = *((volatile UINT16 *) Address);\r |
303 | MemoryFence ();\r |
304 | \r |
305 | return Data;\r |
306 | }\r |
307 | \r |
308 | /**\r |
309 | Reads a 32-bit MMIO register.\r |
310 | \r |
311 | Reads the 32-bit MMIO register specified by Address. The 32-bit read value is\r |
312 | returned. This function must guarantee that all MMIO read and write\r |
313 | operations are serialized.\r |
314 | \r |
315 | @param Address The MMIO register to read.\r |
316 | \r |
317 | @return The value read.\r |
318 | \r |
319 | **/\r |
320 | UINT32\r |
321 | EFIAPI\r |
322 | MmioRead32 (\r |
323 | IN UINT64 Address\r |
324 | )\r |
325 | {\r |
326 | UINT32 Data;\r |
327 | \r |
328 | Address |= BIT63;\r |
329 | \r |
330 | MemoryFence ();\r |
331 | Data = *((volatile UINT32 *) Address);\r |
332 | MemoryFence ();\r |
333 | \r |
334 | return Data;\r |
335 | }\r |
336 | \r |
337 | /**\r |
338 | Reads a 64-bit MMIO register.\r |
339 | \r |
340 | Reads the 64-bit MMIO register specified by Address. The 64-bit read value is\r |
341 | returned. This function must guarantee that all MMIO read and write\r |
342 | operations are serialized.\r |
343 | \r |
344 | @param Address The MMIO register to read.\r |
345 | \r |
346 | @return The value read.\r |
347 | \r |
348 | **/\r |
349 | UINT64\r |
350 | EFIAPI\r |
351 | MmioRead64 (\r |
352 | IN UINT64 Address\r |
353 | )\r |
354 | {\r |
355 | UINT64 Data;\r |
356 | \r |
357 | Address |= BIT63;\r |
358 | \r |
359 | MemoryFence ();\r |
360 | Data = *((volatile UINT64 *) Address);\r |
361 | MemoryFence ();\r |
362 | \r |
363 | return Data;\r |
364 | \r |
365 | }\r |
366 | \r |
367 | /**\r |
368 | Writes a 8-bit MMIO register.\r |
369 | \r |
370 | Writes the 8-bit MMIO register specified by Address with the value specified\r |
371 | by Value and returns Value. This function must guarantee that all MMIO read\r |
372 | and write operations are serialized.\r |
373 | \r |
374 | @param Address The MMIO register to write.\r |
375 | @param Data The value to write to the MMIO register.\r |
376 | \r |
377 | @return The value written the memory address.\r |
378 | \r |
379 | **/\r |
380 | UINT8\r |
381 | EFIAPI\r |
382 | MmioWrite8 (\r |
383 | IN UINT64 Address,\r |
384 | IN UINT8 Data\r |
385 | )\r |
386 | {\r |
387 | Address |= BIT63;\r |
388 | \r |
389 | MemoryFence ();\r |
390 | *((volatile UINT8 *) Address) = Data;\r |
391 | MemoryFence ();\r |
392 | \r |
393 | return Data;\r |
394 | }\r |
395 | \r |
396 | /**\r |
397 | Writes a 16-bit MMIO register.\r |
398 | \r |
399 | Writes the 16-bit MMIO register specified by Address with the value specified\r |
400 | by Value and returns Value. This function must guarantee that all MMIO read\r |
401 | and write operations are serialized.\r |
402 | \r |
403 | @param Address The MMIO register to write.\r |
404 | @param Data The value to write to the MMIO register.\r |
405 | \r |
406 | @return The value written the memory address.\r |
407 | \r |
408 | **/\r |
409 | UINT16\r |
410 | EFIAPI\r |
411 | MmioWrite16 (\r |
412 | IN UINT64 Address,\r |
413 | IN UINT16 Data\r |
414 | )\r |
415 | {\r |
416 | Address |= BIT63;\r |
417 | \r |
418 | MemoryFence ();\r |
419 | *((volatile UINT16 *) Address) = Data;\r |
420 | MemoryFence ();\r |
421 | \r |
422 | return Data;\r |
423 | }\r |
424 | \r |
425 | /**\r |
426 | Writes a 32-bit MMIO register.\r |
427 | \r |
428 | Writes the 32-bit MMIO register specified by Address with the value specified\r |
429 | by Value and returns Value. This function must guarantee that all MMIO read\r |
430 | and write operations are serialized.\r |
431 | \r |
432 | @param Address The MMIO register to write.\r |
433 | @param Data The value to write to the MMIO register.\r |
434 | \r |
435 | @return The value written the memory address.\r |
436 | \r |
437 | **/\r |
438 | UINT32\r |
439 | EFIAPI\r |
440 | MmioWrite32 (\r |
441 | IN UINT64 Address,\r |
442 | IN UINT32 Data\r |
443 | )\r |
444 | {\r |
445 | Address |= BIT63;\r |
446 | \r |
447 | MemoryFence ();\r |
448 | *((volatile UINT32 *) Address) = Data;\r |
449 | MemoryFence ();\r |
450 | \r |
451 | return Data;\r |
452 | }\r |
453 | \r |
454 | /**\r |
455 | Writes a 64-bit MMIO register.\r |
456 | \r |
457 | Writes the 64-bit MMIO register specified by Address with the value specified\r |
458 | by Value and returns Value. This function must guarantee that all MMIO read\r |
459 | and write operations are serialized.\r |
460 | \r |
461 | @param Address The MMIO register to write.\r |
462 | @param Data The value to write to the MMIO register.\r |
463 | \r |
464 | @return The value written the memory address.\r |
465 | \r |
466 | **/\r |
467 | UINT64\r |
468 | EFIAPI\r |
469 | MmioWrite64 (\r |
470 | IN UINT64 Address,\r |
471 | IN UINT64 Data\r |
472 | )\r |
473 | {\r |
474 | Address |= BIT63;\r |
475 | \r |
476 | MemoryFence ();\r |
477 | *((volatile UINT64 *) Address) = Data;\r |
478 | MemoryFence ();\r |
479 | \r |
480 | return Data;\r |
481 | }\r |