]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Bus/Isa/IsaFloppyPei/FloppyPeim.c
IntelFrameworkModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Isa / IsaFloppyPei / FloppyPeim.c
CommitLineData
d8387fa4 1/** @file\r
2Floppy Peim to support Recovery function from Floppy device.\r
3\r
0a6f4824
LG
4Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
5\r
c0a00b14 6SPDX-License-Identifier: BSD-2-Clause-Patent\r
d8387fa4 7\r
8**/\r
9\r
10\r
11#include "FloppyPeim.h"\r
12\r
13\r
14PEI_DMA_TABLE mRegisterTable[] = {\r
15 //\r
16 // DMA2: Clear Byte Ptr, Enable\r
17 //\r
18 {\r
19 R_8237_DMA_CBPR_CH4_7,\r
20 0\r
21 },\r
22 {\r
23 R_8237_DMA_COMMAND_CH4_7,\r
24 0\r
25 },\r
26 //\r
27 // DMA1: Clear Byte Ptr, Enable\r
28 //\r
29 {\r
30 R_8237_DMA_CBPR_CH0_3,\r
31 0\r
32 },\r
33 {\r
34 R_8237_DMA_COMMAND_CH0_3,\r
35 0\r
36 },\r
37 //\r
38 // Configure Channel 4 for Cascade Mode\r
39 // Clear DMA Request and enable DREQ\r
40 //\r
41 {\r
42 R_8237_DMA_CHMODE_CH4_7,\r
43 V_8237_DMA_CHMODE_CASCADE | 0\r
44 },\r
45 {\r
46 R_8237_DMA_STA_CH4_7,\r
47 0\r
48 },\r
49 {\r
50 R_8237_DMA_WRSMSK_CH4_7,\r
51 0\r
52 },\r
53 //\r
54 // Configure DMA1 (Channels 0-3) for Single Mode\r
55 // Clear DMA Request and enable DREQ\r
56 //\r
57 {\r
58 R_8237_DMA_CHMODE_CH0_3,\r
59 V_8237_DMA_CHMODE_SINGLE | 0\r
60 },\r
61 {\r
62 R_8237_DMA_STA_CH0_3,\r
63 0\r
64 },\r
65 {\r
66 R_8237_DMA_WRSMSK_CH0_3,\r
67 0\r
68 },\r
69 {\r
70 R_8237_DMA_CHMODE_CH0_3,\r
71 V_8237_DMA_CHMODE_SINGLE | 1\r
72 },\r
73 {\r
74 R_8237_DMA_STA_CH0_3,\r
75 1\r
76 },\r
77 {\r
78 R_8237_DMA_WRSMSK_CH0_3,\r
79 1\r
80 },\r
81 {\r
82 R_8237_DMA_CHMODE_CH0_3,\r
83 V_8237_DMA_CHMODE_SINGLE | 2\r
84 },\r
85 {\r
86 R_8237_DMA_STA_CH0_3,\r
87 2\r
88 },\r
89 {\r
90 R_8237_DMA_WRSMSK_CH0_3,\r
91 2\r
92 },\r
93 {\r
94 R_8237_DMA_CHMODE_CH0_3,\r
95 V_8237_DMA_CHMODE_SINGLE | 3\r
96 },\r
97 {\r
98 R_8237_DMA_STA_CH0_3,\r
99 3\r
100 },\r
101 {\r
102 R_8237_DMA_WRSMSK_CH0_3,\r
103 3\r
104 },\r
105 //\r
106 // Configure DMA2 (Channels 5-7) for Single Mode\r
107 // Clear DMA Request and enable DREQ\r
108 //\r
109 {\r
110 R_8237_DMA_CHMODE_CH4_7,\r
111 V_8237_DMA_CHMODE_SINGLE | 1\r
112 },\r
113 {\r
114 R_8237_DMA_STA_CH4_7,\r
115 1\r
116 },\r
117 {\r
118 R_8237_DMA_WRSMSK_CH4_7,\r
119 1\r
120 },\r
121 {\r
122 R_8237_DMA_CHMODE_CH4_7,\r
123 V_8237_DMA_CHMODE_SINGLE | 2\r
124 },\r
125 {\r
126 R_8237_DMA_STA_CH4_7,\r
127 2\r
128 },\r
129 {\r
130 R_8237_DMA_WRSMSK_CH4_7,\r
131 2\r
132 },\r
133 {\r
134 R_8237_DMA_CHMODE_CH4_7,\r
135 V_8237_DMA_CHMODE_SINGLE | 3\r
136 },\r
137 {\r
138 R_8237_DMA_STA_CH4_7,\r
139 3\r
140 },\r
141 {\r
142 R_8237_DMA_WRSMSK_CH4_7,\r
143 3\r
144 }\r
145};\r
146\r
147//\r
0a6f4824 148// Table of diskette parameters of various diskette types\r
d8387fa4 149//\r
150DISKET_PARA_TABLE DiskPara[9] = {\r
151 {\r
152 0x09,\r
153 0x50,\r
154 0xff,\r
155 0x2,\r
156 0x27,\r
157 0x4,\r
158 0x25,\r
159 0x14,\r
160 0x80\r
161 },\r
162 {\r
163 0x09,\r
164 0x2a,\r
165 0xff,\r
166 0x2,\r
167 0x27,\r
168 0x4,\r
169 0x25,\r
170 0x0f,\r
171 0x40\r
172 },\r
173 {\r
174 0x0f,\r
175 0x54,\r
176 0xff,\r
177 0x2,\r
178 0x4f,\r
179 0x4,\r
180 0x25,\r
181 0x0f,\r
182 0x0\r
183 },\r
184 {\r
185 0x09,\r
186 0x50,\r
187 0xff,\r
188 0x2,\r
189 0x4f,\r
190 0x4,\r
191 0x25,\r
192 0x0f,\r
193 0x80\r
194 },\r
195 {\r
196 0x09,\r
197 0x2a,\r
198 0xff,\r
199 0x2,\r
200 0x4f,\r
201 0x4,\r
202 0x25,\r
203 0x0f,\r
204 0x80\r
205 },\r
206 {\r
207 0x12,\r
208 0x1b,\r
209 0xff,\r
210 0x2,\r
211 0x4f,\r
212 0x4,\r
213 0x25,\r
214 0x0f,\r
215 0x0\r
216 },\r
217 {\r
218 0x09,\r
219 0x2a,\r
220 0xff,\r
221 0x2,\r
222 0x4f,\r
223 0x4,\r
224 0x25,\r
225 0x0f,\r
226 0x80\r
227 },\r
228 {\r
229 0x12,\r
230 0x1b,\r
231 0xff,\r
232 0x2,\r
233 0x4f,\r
234 0x4,\r
235 0x25,\r
236 0x0f,\r
237 0x0\r
238 },\r
239 {\r
240 0x24,\r
241 0x1b,\r
242 0xff,\r
243 0x2,\r
244 0x4f,\r
245 0x4,\r
246 0x25,\r
247 0x0f,\r
248 0xc0\r
249 }\r
250};\r
251\r
252//\r
253// Byte per sector corresponding to various device types.\r
254//\r
255UINTN BytePerSector[6] = { 0, 256, 512, 1024, 2048, 4096 };\r
256\r
257FDC_BLK_IO_DEV mBlockIoDevTemplate = {\r
258 FDC_BLK_IO_DEV_SIGNATURE,\r
259 {\r
260 FdcGetNumberOfBlockDevices,\r
261 FdcGetBlockDeviceMediaInfo,\r
262 FdcReadBlocks,\r
263 },\r
264 {\r
265 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
266 &gEfiPeiVirtualBlockIoPpiGuid,\r
267 NULL\r
268 },\r
269 0,\r
270 {{0}}\r
271};\r
272\r
273/**\r
274 Wait and check if bits for DIO and RQM of FDC Main Status Register\r
275 indicates FDC is ready for read or write.\r
276\r
277 Before writing to FDC or reading from FDC, the Host must examine\r
278 the bit7(RQM) and bit6(DIO) of the Main Status Register.\r
279 That is to say:\r
280 Command bytes can not be written to Data Register unless RQM is 1 and DIO is 0.\r
281 Result bytes can not be read from Data Register unless RQM is 1 and DIO is 1.\r
282\r
283 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.\r
284 @param DataIn Indicates data input or output.\r
285 TRUE means input.\r
286 FALSE means output.\r
287 @param TimeoutInMseconds Timeout value to wait.\r
0a6f4824 288\r
d8387fa4 289 @retval EFI_SUCCESS FDC is ready.\r
290 @retval EFI_NOT_READY FDC is not ready within the specified time period.\r
291\r
292**/\r
293EFI_STATUS\r
294FdcDRQReady (\r
295 IN FDC_BLK_IO_DEV *FdcBlkIoDev,\r
296 IN BOOLEAN DataIn,\r
297 IN UINTN TimeoutInMseconds\r
298 )\r
299{\r
300 UINTN Delay;\r
301 UINT8 StatusRegister;\r
302 UINT8 BitInOut;\r
303\r
304 //\r
305 // Check bit6 of Main Status Register.\r
306 //\r
307 BitInOut = 0;\r
308 if (DataIn) {\r
309 BitInOut = BIT6;\r
310 }\r
311\r
312 Delay = ((TimeoutInMseconds * STALL_1_MSECOND) / FDC_CHECK_INTERVAL) + 1;\r
313 do {\r
314 StatusRegister = IoRead8 ((UINT16) (PcdGet16 (PcdFdcBaseAddress) + FDC_REGISTER_MSR));\r
315 if ((StatusRegister & MSR_RQM) == MSR_RQM && (StatusRegister & MSR_DIO) == BitInOut) {\r
316 //\r
317 // FDC is ready\r
318 //\r
319 break;\r
320 }\r
321\r
322 MicroSecondDelay (FDC_SHORT_DELAY);\r
323 } while (--Delay > 0);\r
324\r
325 if (Delay == 0) {\r
326 //\r
327 // FDC is not ready within the specified time period\r
328 //\r
329 return EFI_NOT_READY;\r
330 }\r
331\r
332 return EFI_SUCCESS;\r
333}\r
334\r
335/**\r
336 Read a byte from FDC data register.\r
337\r
338 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.\r
339 @param Pointer Pointer to buffer to hold data read from FDC.\r
340\r
341 @retval EFI_SUCCESS Byte successfully read.\r
342 @retval EFI_DEVICE_ERROR FDC is not ready.\r
343\r
344**/\r
345EFI_STATUS\r
346DataInByte (\r
347 IN FDC_BLK_IO_DEV *FdcBlkIoDev,\r
348 OUT UINT8 *Pointer\r
349 )\r
350{\r
351 UINT8 Data;\r
352\r
353 //\r
354 // Wait for 1ms and detect the FDC is ready to be read\r
355 //\r
356 if (FdcDRQReady (FdcBlkIoDev, TRUE, 1) != EFI_SUCCESS) {\r
357 //\r
358 // FDC is not ready.\r
359 //\r
360 return EFI_DEVICE_ERROR;\r
361 }\r
362\r
363 Data = IoRead8 ((UINT16) (PcdGet16 (PcdFdcBaseAddress) + FDC_REGISTER_DTR));\r
364 MicroSecondDelay (FDC_SHORT_DELAY);\r
365 *Pointer = Data;\r
366\r
367 return EFI_SUCCESS;\r
368}\r
369\r
370/**\r
371 Write a byte to FDC data register.\r
372\r
373 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.\r
374 @param Pointer Pointer to data to write.\r
375\r
376 @retval EFI_SUCCESS Byte successfully written.\r
377 @retval EFI_DEVICE_ERROR FDC is not ready.\r
378\r
379**/\r
380EFI_STATUS\r
381DataOutByte (\r
382 IN FDC_BLK_IO_DEV *FdcBlkIoDev,\r
383 IN UINT8 *Pointer\r
384 )\r
385{\r
386 UINT8 Data;\r
387\r
388 //\r
389 // Wait for 1ms and detect the FDC is ready to be written\r
390 //\r
391 if (FdcDRQReady (FdcBlkIoDev, FALSE, 1) != EFI_SUCCESS) {\r
392 //\r
393 // FDC is not ready.\r
394 //\r
395 return EFI_DEVICE_ERROR;\r
396 }\r
397\r
398 Data = *Pointer;\r
399 IoWrite8 ((UINT16) (PcdGet16 (PcdFdcBaseAddress) + FDC_REGISTER_DTR), Data);\r
400 MicroSecondDelay (FDC_SHORT_DELAY);\r
401\r
402 return EFI_SUCCESS;\r
403}\r
404\r
405/**\r
406 Get Sts0 and Pcn status from FDC\r
407\r
408 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV\r
409 @param Sts0 Value of Sts0\r
410 @param Pcn Value of Pcn\r
411\r
412 @retval EFI_SUCCESS Successfully retrieved status value of Sts0 and Pcn.\r
413 @retval EFI_DEVICE_ERROR Fail to send SENSE_INT_STATUS_CMD.\r
414 @retval EFI_DEVICE_ERROR Fail to read Sts0.\r
415 @retval EFI_DEVICE_ERROR Fail to read Pcn.\r
416\r
417**/\r
418EFI_STATUS\r
419SenseIntStatus (\r
420 IN FDC_BLK_IO_DEV *FdcBlkIoDev,\r
421 OUT UINT8 *Sts0,\r
422 OUT UINT8 *Pcn\r
423 )\r
424{\r
425 UINT8 Command;\r
426\r
427 Command = SENSE_INT_STATUS_CMD;\r
428\r
429 if (DataOutByte (FdcBlkIoDev, &Command) != EFI_SUCCESS) {\r
430 return EFI_DEVICE_ERROR;\r
431 }\r
432\r
433 if (DataInByte (FdcBlkIoDev, Sts0) != EFI_SUCCESS) {\r
434 return EFI_DEVICE_ERROR;\r
435 }\r
436\r
437 if (DataInByte (FdcBlkIoDev, Pcn) != EFI_SUCCESS) {\r
438 return EFI_DEVICE_ERROR;\r
439 }\r
440\r
441 return EFI_SUCCESS;\r
442}\r
443\r
444/**\r
445 Issue Specify command.\r
446\r
447 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.\r
448\r
449 @retval EFI_SUCCESS Specify command successfully issued.\r
450 @retval EFI_DEVICE_ERROR FDC device has errors.\r
451\r
452**/\r
453EFI_STATUS\r
454Specify (\r
455 IN FDC_BLK_IO_DEV *FdcBlkIoDev\r
456 )\r
457{\r
458 FDC_SPECIFY_CMD Command;\r
459 UINTN Index;\r
460 UINT8 *Pointer;\r
461\r
462 ZeroMem (&Command, sizeof (FDC_SPECIFY_CMD));\r
463 Command.CommandCode = SPECIFY_CMD;\r
464 //\r
465 // set SRT, HUT\r
466 //\r
467 Command.SrtHut = 0xdf;\r
468 //\r
469 // 0xdf;\r
470 // set HLT and DMA\r
471 //\r
472 Command.HltNd = 0x02;\r
473\r
474 Pointer = (UINT8 *) (&Command);\r
475 for (Index = 0; Index < sizeof (FDC_SPECIFY_CMD); Index++) {\r
476 if (DataOutByte (FdcBlkIoDev, Pointer++) != EFI_SUCCESS) {\r
477 return EFI_DEVICE_ERROR;\r
478 }\r
479 }\r
480\r
481 return EFI_SUCCESS;\r
482}\r
483\r
484/**\r
485 Wait until busy bit is cleared.\r
486\r
487 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.\r
488 @param DevPos Position of FDC (Driver A or B)\r
489 @param TimeoutInMseconds Timeout value to wait.\r
490\r
491 @retval EFI_SUCCESS Busy bit has been cleared before timeout.\r
492 @retval EFI_TIMEOUT Time goes out before busy bit is cleared.\r
493\r
494**/\r
495EFI_STATUS\r
496FdcWaitForBSYClear (\r
497 IN FDC_BLK_IO_DEV *FdcBlkIoDev,\r
498 IN UINT8 DevPos,\r
499 IN UINTN TimeoutInMseconds\r
500 )\r
501{\r
502 UINTN Delay;\r
503 UINT8 StatusRegister;\r
504 UINT8 Mask;\r
505\r
506 //\r
507 // How to determine drive and command are busy or not: by the bits of Main Status Register\r
508 // bit0: Drive 0 busy (drive A)\r
509 // bit1: Drive 1 busy (drive B)\r
510 // bit4: Command busy\r
511 //\r
512 // set mask: for drive A set bit0 & bit4; for drive B set bit1 & bit4\r
513 //\r
514 Mask = (UINT8) ((DevPos == 0 ? MSR_DAB : MSR_DBB) | MSR_CB);\r
515\r
516 Delay = ((TimeoutInMseconds * STALL_1_MSECOND) / FDC_CHECK_INTERVAL) + 1;\r
517\r
518 do {\r
519 StatusRegister = IoRead8 ((UINT16) (PcdGet16 (PcdFdcBaseAddress) + FDC_REGISTER_MSR));\r
520\r
521 if ((StatusRegister & Mask) == 0x00) {\r
522 //\r
523 // not busy\r
524 //\r
525 break;\r
526 }\r
527\r
528 MicroSecondDelay (FDC_SHORT_DELAY);\r
529 } while (--Delay > 0);\r
530\r
531 if (Delay == 0) {\r
532 return EFI_TIMEOUT;\r
533 }\r
534\r
535 return EFI_SUCCESS;\r
536}\r
537\r
538/**\r
539 Reset FDC device.\r
540\r
541 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV\r
542 @param DevPos Index of FDC device.\r
543\r
544 @retval EFI_SUCCESS FDC device successfully reset.\r
545 @retval EFI_DEVICE_ERROR Fail to reset FDC device.\r
546\r
547**/\r
548EFI_STATUS\r
549FdcReset (\r
550 IN FDC_BLK_IO_DEV *FdcBlkIoDev,\r
551 IN UINT8 DevPos\r
552 )\r
553{\r
554 UINT8 Data;\r
555 UINT8 Sts0;\r
556 UINT8 Pcn;\r
557 UINTN Index;\r
558\r
559 //\r
560 // Reset specified Floppy Logic Drive according to Fdd -> Disk\r
561 // Set Digital Output Register(DOR) to do reset work\r
562 // bit0 & bit1 of DOR : Drive Select\r
563 // bit2 : Reset bit\r
564 // bit3 : DMA and Int bit\r
565 // Reset : A "0" written to bit2 resets the FDC, this reset will remain active until\r
566 // a "1" is written to this bit.\r
567 // Reset step 1:\r
568 // use bit0 & bit1 to select the logic drive\r
569 // write "0" to bit2\r
570 //\r
571 Data = 0x0;\r
572 Data = (UINT8) (Data | (SELECT_DRV & DevPos));\r
573 IoWrite8 ((UINT16) (PcdGet16 (PcdFdcBaseAddress) + FDC_REGISTER_DOR), Data);\r
574\r
575 //\r
576 // Wait some time, at least 120us.\r
577 //\r
578 MicroSecondDelay (FDC_RESET_DELAY);\r
579 //\r
580 // Reset step 2:\r
581 // write "1" to bit2\r
582 // write "1" to bit3 : enable DMA\r
583 //\r
584 Data |= 0x0C;\r
585 IoWrite8 ((UINT16) (PcdGet16 (PcdFdcBaseAddress) + FDC_REGISTER_DOR), Data);\r
586\r
587 MicroSecondDelay (FDC_RESET_DELAY);\r
588\r
589 //\r
590 // Wait until specified floppy logic drive is not busy\r
591 //\r
592 if (FdcWaitForBSYClear (FdcBlkIoDev, DevPos, 1) != EFI_SUCCESS) {\r
593 return EFI_DEVICE_ERROR;\r
594 }\r
595 //\r
596 // Set the Transfer Data Rate\r
597 //\r
598 IoWrite8 ((UINT16) (PcdGet16 (PcdFdcBaseAddress) + FDC_REGISTER_CCR), 0x0);\r
599\r
600 MicroSecondDelay (FDC_MEDIUM_DELAY);\r
601\r
602 //\r
603 // Issue Sense interrupt command for each drive (totally 4 drives)\r
604 //\r
605 for (Index = 0; Index < 4; Index++) {\r
606 if (SenseIntStatus (FdcBlkIoDev, &Sts0, &Pcn) != EFI_SUCCESS) {\r
607 return EFI_DEVICE_ERROR;\r
608 }\r
609 }\r
610 //\r
611 // Issue Specify command\r
612 //\r
613 if (Specify (FdcBlkIoDev) != EFI_SUCCESS) {\r
614 return EFI_DEVICE_ERROR;\r
615 }\r
616\r
617 return EFI_SUCCESS;\r
618}\r
619\r
620/**\r
621 Turn on the motor of floppy drive.\r
622\r
623 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.\r
624 @param Info Information of floppy device.\r
625\r
626 @retval EFI_SUCCESS Motor is successfully turned on.\r
627 @retval EFI_SUCCESS Motor is already on.\r
628 @retval EFI_DEVICE_ERROR Busy bit of FDC cannot be cleared.\r
629\r
630**/\r
631EFI_STATUS\r
632MotorOn (\r
633 IN FDC_BLK_IO_DEV *FdcBlkIoDev,\r
634 IN OUT PEI_FLOPPY_DEVICE_INFO *Info\r
635 )\r
636{\r
637 UINT8 Data;\r
638 UINT8 DevPos;\r
639\r
640 //\r
641 // Control of the floppy drive motors is a big pain. If motor is off, you have to turn it\r
642 // on first. But you can not leave the motor on all the time, since that would wear out the\r
643 // disk. On the other hand, if you turn the motor off after each operation, the system performance\r
644 // will be awful. The compromise used in this driver is to leave the motor on for 2 seconds after\r
645 // each operation. If a new operation is started in that interval(2s), the motor need not be\r
646 // turned on again. If no new operation is started, a timer goes off and the motor is turned off.\r
647 //\r
648 DevPos = Info->DevPos;\r
649\r
650 //\r
651 // If the Motor is already on, just return EFI_SUCCESS.\r
652 //\r
653 if (Info->MotorOn) {\r
654 return EFI_SUCCESS;\r
655 }\r
656 //\r
657 // The drive's motor is off, so need turn it on.\r
658 // First check if command and drive are busy or not.\r
659 //\r
660 if (FdcWaitForBSYClear (FdcBlkIoDev, DevPos, 1) != EFI_SUCCESS) {\r
661 return EFI_DEVICE_ERROR;\r
662 }\r
663 //\r
664 // for drive A: 1CH, drive B: 2DH\r
665 //\r
666 Data = 0x0C;\r
667 Data = (UINT8) (Data | (SELECT_DRV & DevPos));\r
668 if (DevPos == 0) {\r
669 Data |= DRVA_MOTOR_ON;\r
670 } else {\r
671 Data |= DRVB_MOTOR_ON;\r
672 }\r
673\r
674 Info->MotorOn = FALSE;\r
675\r
676 //\r
677 // Turn on the motor and wait for some time to ensure it takes effect.\r
678 //\r
679 IoWrite8 ((UINT16) (PcdGet16 (PcdFdcBaseAddress) + FDC_REGISTER_DOR), Data);\r
680 MicroSecondDelay (FDC_LONG_DELAY);\r
681\r
682 Info->MotorOn = TRUE;\r
683\r
684 return EFI_SUCCESS;\r
685}\r
686\r
687/**\r
688 Turn off the motor of floppy drive.\r
689\r
690 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.\r
691 @param Info Information of floppy device.\r
692\r
693**/\r
694VOID\r
695MotorOff (\r
696 IN FDC_BLK_IO_DEV *FdcBlkIoDev,\r
697 IN OUT PEI_FLOPPY_DEVICE_INFO *Info\r
698 )\r
699{\r
700 UINT8 Data;\r
701 UINT8 DevPos;\r
702\r
703 DevPos = Info->DevPos;\r
704\r
705 if (!Info->MotorOn) {\r
706 return;\r
707 }\r
708 //\r
709 // The motor is on, so need motor off\r
710 //\r
711 Data = 0x0C;\r
712 Data = (UINT8) (Data | (SELECT_DRV & DevPos));\r
713\r
714 IoWrite8 ((UINT16) (PcdGet16 (PcdFdcBaseAddress) + FDC_REGISTER_DOR), Data);\r
715 MicroSecondDelay (FDC_SHORT_DELAY);\r
716\r
717 Info->MotorOn = FALSE;\r
718}\r
719\r
720/**\r
721 Recalibrate the FDC device.\r
722\r
723 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.\r
724 @param Info Information of floppy device.\r
725\r
726 @retval EFI_SUCCESS FDC successfully recalibrated.\r
727 @retval EFI_DEVICE_ERROR Fail to send RECALIBRATE_CMD.\r
728 @retval EFI_DEVICE_ERROR Fail to get status value of Sts0 and Pcn.\r
729 @retval EFI_DEVICE_ERROR Fail to recalibrate FDC device.\r
730\r
731**/\r
732EFI_STATUS\r
733Recalibrate (\r
734 IN FDC_BLK_IO_DEV *FdcBlkIoDev,\r
735 IN OUT PEI_FLOPPY_DEVICE_INFO *Info\r
736 )\r
737{\r
738 FDC_COMMAND_PACKET2 Command;\r
739 UINTN Index;\r
740 UINT8 Sts0;\r
741 UINT8 Pcn;\r
742 UINT8 *Pointer;\r
743 UINT8 Count;\r
744 UINT8 DevPos;\r
745\r
746 DevPos = Info->DevPos;\r
747\r
748 //\r
749 // We would try twice.\r
750 //\r
751 Count = 2;\r
752 while (Count > 0) {\r
753 ZeroMem (&Command, sizeof (FDC_COMMAND_PACKET2));\r
754 Command.CommandCode = RECALIBRATE_CMD;\r
755 //\r
756 // drive select\r
757 //\r
758 if (DevPos == 0) {\r
759 Command.DiskHeadSel = 0;\r
760 } else {\r
761 Command.DiskHeadSel = 1;\r
762 }\r
763\r
764 Pointer = (UINT8 *) (&Command);\r
765 for (Index = 0; Index < sizeof (FDC_COMMAND_PACKET2); Index++) {\r
766 if (DataOutByte (FdcBlkIoDev, Pointer++) != EFI_SUCCESS) {\r
767 return EFI_DEVICE_ERROR;\r
768 }\r
769 }\r
770\r
771 MicroSecondDelay (FDC_RECALIBRATE_DELAY);\r
772\r
773 if (SenseIntStatus (FdcBlkIoDev, &Sts0, &Pcn) != EFI_SUCCESS) {\r
774 return EFI_DEVICE_ERROR;\r
775 }\r
776\r
777 if ((Sts0 & 0xf0) == BIT5 && Pcn == 0) {\r
778 //\r
0a6f4824 779 // Recalibration is successful.\r
d8387fa4 780 //\r
781 Info->Pcn = 0;\r
782 Info->NeedRecalibrate = FALSE;\r
783\r
784 return EFI_SUCCESS;\r
785 } else {\r
786 //\r
787 // Recalibration is not successful. Try again.\r
788 // If trial is used out, return EFI_DEVICE_ERROR.\r
789 //\r
790 Count--;\r
791 if (Count == 0) {\r
792 return EFI_DEVICE_ERROR;\r
793 }\r
794 }\r
795 }\r
796\r
797 return EFI_SUCCESS;\r
798}\r
799\r
800/**\r
801 Seek for the cylinder according to given LBA.\r
802\r
803 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.\r
804 @param Info Information of floppy device.\r
805 @param Lba LBA for which to seek for cylinder.\r
806\r
807 @retval EFI_SUCCESS Successfully moved to the destination cylinder.\r
808 @retval EFI_SUCCESS Destination cylinder is just the present cylinder.\r
809 @retval EFI_DEVICE_ERROR Fail to move to the destination cylinder.\r
810\r
811**/\r
812EFI_STATUS\r
813Seek (\r
814 IN FDC_BLK_IO_DEV *FdcBlkIoDev,\r
815 IN OUT PEI_FLOPPY_DEVICE_INFO *Info,\r
816 IN EFI_PEI_LBA Lba\r
817 )\r
818{\r
819 FDC_SEEK_CMD Command;\r
820 DISKET_PARA_TABLE *Para;\r
821 UINT8 EndOfTrack;\r
822 UINT8 Head;\r
823 UINT8 Cylinder;\r
824 UINT8 Sts0;\r
825 UINT8 *Pointer;\r
826 UINT8 Pcn;\r
827 UINTN Index;\r
828 UINT8 Gap;\r
829 UINT8 DevPos;\r
830\r
831 DevPos = Info->DevPos;\r
832 if (Info->NeedRecalibrate) {\r
833 if (Recalibrate (FdcBlkIoDev, Info) != EFI_SUCCESS) {\r
834 return EFI_DEVICE_ERROR;\r
835 }\r
836 //\r
837 // Recalibrate Success\r
838 //\r
839 Info->NeedRecalibrate = FALSE;\r
840 }\r
841\r
842 //\r
843 // Get the base of disk parameter information corresponding to its type.\r
844 //\r
845 Para = (DISKET_PARA_TABLE *) ((UINT8 *) DiskPara + sizeof (DISKET_PARA_TABLE) * Info->Type);\r
846 EndOfTrack = Para->EndOfTrack;\r
847 //\r
848 // Calculate cylinder based on Lba and EOT\r
849 //\r
850 Cylinder = (UINT8) ((UINTN) Lba / EndOfTrack / 2);\r
851\r
852 //\r
853 // If the dest cylinder is the present cylinder, unnecessary to do the seek operation\r
854 //\r
855 if (Info->Pcn == Cylinder) {\r
856 return EFI_SUCCESS;\r
857 }\r
858\r
859 //\r
860 // Calculate the head : 0 or 1\r
861 //\r
862 Head = (UINT8) ((UINTN) Lba / EndOfTrack % 2);\r
863\r
864 ZeroMem (&Command, sizeof (FDC_SEEK_CMD));\r
865 Command.CommandCode = SEEK_CMD;\r
866 if (DevPos == 0) {\r
867 Command.DiskHeadSel = 0;\r
868 } else {\r
869 Command.DiskHeadSel = 1;\r
870 }\r
871\r
872 //\r
873 // Send command to move to destination cylinder.\r
874 //\r
875 Command.DiskHeadSel = (UINT8) (Command.DiskHeadSel | (Head << 2));\r
876 Command.NewCylinder = Cylinder;\r
877\r
878 Pointer = (UINT8 *) (&Command);\r
879 for (Index = 0; Index < sizeof (FDC_SEEK_CMD); Index++) {\r
880 if (DataOutByte (FdcBlkIoDev, Pointer++) != EFI_SUCCESS) {\r
881 return EFI_DEVICE_ERROR;\r
882 }\r
883 }\r
884\r
885 MicroSecondDelay (FDC_SHORT_DELAY);\r
886\r
887 //\r
888 // Calculate waiting time, which is proportional to the gap between destination\r
889 // cylinder and present cylinder.\r
890 //\r
891 if (Info->Pcn > Cylinder) {\r
892 Gap = (UINT8) (Info->Pcn - Cylinder);\r
893 } else {\r
894 Gap = (UINT8) (Cylinder - Info->Pcn);\r
895 }\r
896\r
897 MicroSecondDelay ((Gap + 1) * FDC_LONG_DELAY);\r
898\r
899 //\r
900 // Confirm if the new cylinder is the destination and status is correct.\r
901 //\r
902 if (SenseIntStatus (FdcBlkIoDev, &Sts0, &Pcn) != EFI_SUCCESS) {\r
903 return EFI_DEVICE_ERROR;\r
904 }\r
905\r
906 if ((Sts0 & 0xf0) == BIT5) {\r
907 Info->Pcn = Command.NewCylinder;\r
908 Info->NeedRecalibrate = FALSE;\r
909 return EFI_SUCCESS;\r
910 } else {\r
911 Info->NeedRecalibrate = TRUE;\r
912 return EFI_DEVICE_ERROR;\r
913 }\r
914}\r
915\r
916/**\r
917 Check if diskette is changed.\r
918\r
919 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV\r
920 @param Info Information of floppy device.\r
921\r
922 @retval EFI_SUCCESS Diskette is not changed.\r
923 @retval EFI_MEDIA_CHANGED Diskette is changed.\r
924 @retval EFI_NO_MEDIA No diskette.\r
925 @retval EFI_DEVICE_ERROR Fail to do the seek or recalibrate operation.\r
926\r
927**/\r
928EFI_STATUS\r
929DisketChanged (\r
930 IN FDC_BLK_IO_DEV *FdcBlkIoDev,\r
931 IN OUT PEI_FLOPPY_DEVICE_INFO *Info\r
932 )\r
933{\r
934 EFI_STATUS Status;\r
935 UINT8 Data;\r
936\r
937 //\r
938 // Check change line\r
939 //\r
940 Data = IoRead8 ((UINT16) (PcdGet16 (PcdFdcBaseAddress) + FDC_REGISTER_DIR));\r
941\r
942 MicroSecondDelay (FDC_SHORT_DELAY);\r
943\r
944 if ((Data & DIR_DCL) == DIR_DCL) {\r
945 if (Info->Pcn != 0) {\r
946 Status = Recalibrate (FdcBlkIoDev, Info);\r
947 } else {\r
948 Status = Seek (FdcBlkIoDev, Info, 0x30);\r
949 }\r
950\r
951 if (Status != EFI_SUCCESS) {\r
952 //\r
953 // Fail to do the seek or recalibrate operation\r
954 //\r
955 return EFI_DEVICE_ERROR;\r
956 }\r
957\r
958 Data = IoRead8 ((UINT16) (PcdGet16 (PcdFdcBaseAddress) + FDC_REGISTER_DIR));\r
959\r
960 MicroSecondDelay (FDC_SHORT_DELAY);\r
961\r
962 if ((Data & DIR_DCL) == DIR_DCL) {\r
963 return EFI_NO_MEDIA;\r
964 }\r
965\r
966 return EFI_MEDIA_CHANGED;\r
967 }\r
968\r
969 return EFI_SUCCESS;\r
970}\r
971\r
972/**\r
973 Detects if FDC device exists.\r
974\r
975 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV\r
976 @param Info Information of floppy device.\r
977 @param MediaInfo Information of floppy media.\r
978\r
979 @retval TRUE FDC device exists and is working properly.\r
980 @retval FALSE FDC device does not exist or cannot work properly.\r
981\r
982**/\r
983BOOLEAN\r
984DiscoverFdcDevice (\r
985 IN FDC_BLK_IO_DEV *FdcBlkIoDev,\r
986 IN OUT PEI_FLOPPY_DEVICE_INFO *Info,\r
987 OUT EFI_PEI_BLOCK_IO_MEDIA *MediaInfo\r
988 )\r
989{\r
990 EFI_STATUS Status;\r
991 DISKET_PARA_TABLE *Para;\r
992\r
993 Status = MotorOn (FdcBlkIoDev, Info);\r
994 if (Status != EFI_SUCCESS) {\r
995 return FALSE;\r
996 }\r
997\r
998 Status = Recalibrate (FdcBlkIoDev, Info);\r
999\r
1000 if (Status != EFI_SUCCESS) {\r
1001 MotorOff (FdcBlkIoDev, Info);\r
1002 return FALSE;\r
1003 }\r
1004 //\r
1005 // Set Media Parameter\r
1006 //\r
1007 MediaInfo->DeviceType = LegacyFloppy;\r
1008 MediaInfo->MediaPresent = TRUE;\r
1009\r
1010 //\r
1011 // Check Media\r
1012 //\r
1013 Status = DisketChanged (FdcBlkIoDev, Info);\r
cbcccd2c 1014 if (Status == EFI_NO_MEDIA) {\r
d8387fa4 1015 //\r
1016 // No diskette in floppy.\r
1017 //\r
0a6f4824 1018 MediaInfo->MediaPresent = FALSE;\r
cbcccd2c 1019 } else if (Status != EFI_MEDIA_CHANGED && Status != EFI_SUCCESS) {\r
d8387fa4 1020 //\r
1021 // EFI_DEVICE_ERROR\r
1022 //\r
1023 MotorOff (FdcBlkIoDev, Info);\r
1024 return FALSE;\r
1025 }\r
1026\r
1027 MotorOff (FdcBlkIoDev, Info);\r
1028\r
1029 //\r
1030 // Get the base of disk parameter information corresponding to its type.\r
1031 //\r
1032 Para = (DISKET_PARA_TABLE *) ((UINT8 *) DiskPara + sizeof (DISKET_PARA_TABLE) * Info->Type);\r
1033\r
1034 MediaInfo->BlockSize = BytePerSector[Para->Number];\r
1035 MediaInfo->LastBlock = Para->EndOfTrack * 2 * (Para->MaxTrackNum + 1) - 1;\r
1036\r
1037 return TRUE;\r
1038}\r
1039\r
1040/**\r
1041 Enumerate floppy device\r
1042\r
1043 @param FdcBlkIoDev Instance of floppy device controller\r
1044\r
1045 @return Number of FDC devices.\r
1046\r
1047**/\r
1048UINT8\r
1049FdcEnumeration (\r
1050 IN FDC_BLK_IO_DEV *FdcBlkIoDev\r
1051 )\r
1052{\r
1053 UINT8 DevPos;\r
1054 UINT8 DevNo;\r
1055 EFI_PEI_BLOCK_IO_MEDIA MediaInfo;\r
1056 EFI_STATUS Status;\r
1057\r
1058 DevNo = 0;\r
1059\r
1060 //\r
1061 // DevPos=0 means Drive A, 1 means Drive B.\r
1062 //\r
1063 for (DevPos = 0; DevPos < 2; DevPos++) {\r
1064 //\r
1065 // Detecting device presence\r
1066 //\r
1067 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, EFI_PERIPHERAL_REMOVABLE_MEDIA + EFI_P_PC_PRESENCE_DETECT);\r
1068\r
1069 //\r
1070 // Reset FDC\r
1071 //\r
1072 Status = FdcReset (FdcBlkIoDev, DevPos);\r
1073\r
1074 if (EFI_ERROR (Status)) {\r
1075 continue;\r
1076 }\r
1077\r
1078 FdcBlkIoDev->DeviceInfo[DevPos].DevPos = DevPos;\r
1079 FdcBlkIoDev->DeviceInfo[DevPos].Pcn = 0;\r
1080 FdcBlkIoDev->DeviceInfo[DevPos].MotorOn = FALSE;\r
1081 FdcBlkIoDev->DeviceInfo[DevPos].NeedRecalibrate = TRUE;\r
1082 FdcBlkIoDev->DeviceInfo[DevPos].Type = FdcType1440K1440K;\r
1083\r
1084 //\r
1085 // Discover FDC device\r
1086 //\r
1087 if (DiscoverFdcDevice (FdcBlkIoDev, &(FdcBlkIoDev->DeviceInfo[DevPos]), &MediaInfo)) {\r
1088 FdcBlkIoDev->DeviceInfo[DevNo].DevPos = DevPos;\r
1089\r
1090 FdcBlkIoDev->DeviceInfo[DevNo].Pcn = FdcBlkIoDev->DeviceInfo[DevPos].Pcn;\r
1091 FdcBlkIoDev->DeviceInfo[DevNo].MotorOn = FdcBlkIoDev->DeviceInfo[DevPos].MotorOn;\r
1092 FdcBlkIoDev->DeviceInfo[DevNo].NeedRecalibrate = FdcBlkIoDev->DeviceInfo[DevPos].NeedRecalibrate;\r
1093 FdcBlkIoDev->DeviceInfo[DevNo].Type = FdcBlkIoDev->DeviceInfo[DevPos].Type;\r
1094\r
1095 CopyMem (\r
1096 &(FdcBlkIoDev->DeviceInfo[DevNo].MediaInfo),\r
1097 &MediaInfo,\r
1098 sizeof (EFI_PEI_BLOCK_IO_MEDIA)\r
1099 );\r
1100\r
1101 DevNo++;\r
1102 } else {\r
1103 //\r
1104 // Assume controller error\r
1105 //\r
1106 REPORT_STATUS_CODE (\r
1107 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
1108 EFI_PERIPHERAL_REMOVABLE_MEDIA + EFI_P_EC_CONTROLLER_ERROR\r
1109 );\r
1110 }\r
1111 }\r
1112\r
1113 FdcBlkIoDev->DeviceCount = DevNo;\r
1114 return DevNo;\r
1115}\r
1116\r
1117/**\r
1118 Checks result reflected by FDC_RESULT_PACKET.\r
1119\r
1120 @param Result FDC_RESULT_PACKET read from FDC after certain operation.\r
1121 @param Info Information of floppy device.\r
1122\r
1123 @retval EFI_SUCCESS Result is healthy.\r
1124 @retval EFI_DEVICE_ERROR Result is not healthy.\r
1125\r
1126**/\r
1127EFI_STATUS\r
1128CheckResult (\r
1129 IN FDC_RESULT_PACKET *Result,\r
1130 OUT PEI_FLOPPY_DEVICE_INFO *Info\r
1131 )\r
1132{\r
1133 if ((Result->Status0 & STS0_IC) != IC_NT) {\r
1134 if ((Result->Status0 & STS0_SE) == BIT5) {\r
1135 //\r
1136 // Seek error\r
1137 //\r
1138 Info->NeedRecalibrate = TRUE;\r
1139 }\r
1140\r
1141 Info->NeedRecalibrate = TRUE;\r
1142 return EFI_DEVICE_ERROR;\r
1143 }\r
1144 //\r
1145 // Check Status Register1\r
1146 //\r
1147 if ((Result->Status1 & (STS1_EN | STS1_DE | STS1_OR | STS1_ND | STS1_NW | STS1_MA)) != 0) {\r
1148 Info->NeedRecalibrate = TRUE;\r
1149 return EFI_DEVICE_ERROR;\r
1150 }\r
1151 //\r
1152 // Check Status Register2\r
1153 //\r
1154 if ((Result->Status2 & (STS2_CM | STS2_DD | STS2_WC | STS2_BC | STS2_MD)) != 0) {\r
1155 Info->NeedRecalibrate = TRUE;\r
1156 return EFI_DEVICE_ERROR;\r
1157 }\r
1158\r
1159 return EFI_SUCCESS;\r
1160}\r
1161\r
1162/**\r
1163 Fill parameters for command packet.\r
1164\r
1165 @param Info Information of floppy device.\r
1166 @param Lba Logical block address.\r
1167 @param Command Command for which for fill parameters.\r
1168\r
1169**/\r
1170VOID\r
1171FillPara (\r
1172 IN PEI_FLOPPY_DEVICE_INFO *Info,\r
1173 IN EFI_PEI_LBA Lba,\r
1174 OUT FDC_COMMAND_PACKET1 *Command\r
1175 )\r
1176{\r
1177 DISKET_PARA_TABLE *Para;\r
1178 UINT8 EndOfTrack;\r
1179 UINT8 DevPos;\r
1180\r
1181 DevPos = Info->DevPos;\r
1182\r
1183 //\r
1184 // Get the base of disk parameter information corresponding to its type.\r
1185 //\r
1186 Para = (DISKET_PARA_TABLE *) ((UINT8 *) DiskPara + sizeof (DISKET_PARA_TABLE) * Info->Type);\r
1187\r
1188 EndOfTrack = Para->EndOfTrack;\r
1189\r
1190 if (DevPos == 0) {\r
1191 Command->DiskHeadSel = 0;\r
1192 } else {\r
1193 Command->DiskHeadSel = 1;\r
1194 }\r
1195\r
1196 Command->Cylinder = (UINT8) ((UINTN) Lba / EndOfTrack / 2);\r
1197 Command->Head = (UINT8) ((UINTN) Lba / EndOfTrack % 2);\r
1198 Command->Sector = (UINT8) ((UINT8) ((UINTN) Lba % EndOfTrack) + 1);\r
1199 Command->DiskHeadSel = (UINT8) (Command->DiskHeadSel | (Command->Head << 2));\r
1200 Command->Number = Para->Number;\r
1201 Command->EndOfTrack = Para->EndOfTrack;\r
1202 Command->GapLength = Para->GapLength;\r
1203 Command->DataLength = Para->DataLength;\r
1204}\r
1205\r
1206/**\r
1207 Setup specifed FDC device.\r
1208\r
1209 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.\r
1210 @param DevPos Index of FDC device.\r
1211\r
1212 @retval EFI_SUCCESS FDC device successfully set up.\r
1213 @retval EFI_DEVICE_ERROR FDC device has errors.\r
1214\r
1215**/\r
1216EFI_STATUS\r
1217Setup (\r
1218 IN FDC_BLK_IO_DEV *FdcBlkIoDev,\r
1219 IN UINT8 DevPos\r
1220 )\r
1221{\r
1222 EFI_STATUS Status;\r
1223\r
1224 IoWrite8 ((UINT16) (PcdGet16 (PcdFdcBaseAddress) + FDC_REGISTER_CCR), 0x0);\r
1225\r
1226 MicroSecondDelay (FDC_MEDIUM_DELAY);\r
1227\r
1228 Status = Specify (FdcBlkIoDev);\r
1229 return Status;\r
1230}\r
1231\r
1232/**\r
1233 Setup DMA channels to read data.\r
1234\r
1235 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.\r
1236 @param Buffer Memory buffer for DMA transfer.\r
1237 @param BlockSize the number of the bytes in one block.\r
1238 @param NumberOfBlocks Number of blocks to read.\r
1239\r
1240**/\r
1241VOID\r
1242SetDMA (\r
1243 IN FDC_BLK_IO_DEV *FdcBlkIoDev,\r
1244 IN VOID *Buffer,\r
1245 IN UINTN BlockSize,\r
1246 IN UINTN NumberOfBlocks\r
1247 )\r
1248{\r
1249 UINT8 Data;\r
1250 UINTN Count;\r
1251\r
1252 //\r
1253 // Mask DMA channel 2;\r
1254 //\r
1255 IoWrite8 (R_8237_DMA_WRSMSK_CH0_3, B_8237_DMA_WRSMSK_CMS | 2);\r
1256\r
1257 //\r
1258 // Clear first/last flip flop\r
1259 //\r
1260 IoWrite8 (R_8237_DMA_CBPR_CH0_3, B_8237_DMA_WRSMSK_CMS | 2);\r
1261\r
1262 //\r
1263 // Set mode\r
1264 //\r
1265 IoWrite8 (R_8237_DMA_CHMODE_CH0_3, V_8237_DMA_CHMODE_SINGLE | V_8237_DMA_CHMODE_IO2MEM | 2);\r
1266\r
1267 //\r
1268 // Set base address and page register\r
1269 //\r
1270 Data = (UINT8) (UINTN) Buffer;\r
1271 IoWrite8 (R_8237_DMA_BASE_CA_CH2, Data);\r
1272 Data = (UINT8) ((UINTN) Buffer >> 8);\r
1273 IoWrite8 (R_8237_DMA_BASE_CA_CH2, Data);\r
1274\r
1275 Data = (UINT8) ((UINTN) Buffer >> 16);\r
1276 IoWrite8 (R_8237_DMA_MEM_LP_CH2, Data);\r
1277\r
1278 //\r
1279 // Set count register\r
1280 //\r
1281 Count = BlockSize * NumberOfBlocks - 1;\r
1282 Data = (UINT8) (Count & 0xff);\r
1283 IoWrite8 (R_8237_DMA_BASE_CC_CH2, Data);\r
1284 Data = (UINT8) (Count >> 8);\r
1285 IoWrite8 (R_8237_DMA_BASE_CC_CH2, Data);\r
1286\r
1287 //\r
1288 // Clear channel 2 mask\r
1289 //\r
1290 IoWrite8 (R_8237_DMA_WRSMSK_CH0_3, 0x02);\r
1291}\r
1292\r
1293\r
1294/**\r
1295 According to the block range specified by Lba and NumberOfBlocks, calculate\r
1296 the number of blocks in the same sector, which can be transferred in a batch.\r
1297\r
1298 @param Info Information of floppy device.\r
1299 @param Lba Start address of block range.\r
1300 @param NumberOfBlocks Number of blocks of the range.\r
1301\r
1302 @return Number of blocks in the same sector.\r
1303\r
1304**/\r
1305UINTN\r
1306GetTransferBlockCount (\r
1307 IN PEI_FLOPPY_DEVICE_INFO *Info,\r
1308 IN EFI_PEI_LBA Lba,\r
1309 IN UINTN NumberOfBlocks\r
1310 )\r
1311{\r
1312 DISKET_PARA_TABLE *Para;\r
1313 UINT8 EndOfTrack;\r
1314 UINT8 Head;\r
1315 UINT8 SectorsInTrack;\r
1316\r
1317 //\r
1318 // Get the base of disk parameter information corresponding to its type.\r
1319 //\r
1320 Para = (DISKET_PARA_TABLE *) ((UINT8 *) DiskPara + sizeof (DISKET_PARA_TABLE) * Info->Type);\r
1321\r
1322 EndOfTrack = Para->EndOfTrack;\r
1323 Head = (UINT8) ((UINTN) Lba / EndOfTrack % 2);\r
1324\r
1325 SectorsInTrack = (UINT8) (EndOfTrack * (2 - Head) - (UINT8) ((UINTN) Lba % EndOfTrack));\r
1326 if (SectorsInTrack < NumberOfBlocks) {\r
1327 //\r
1328 // Not all the block range locates in the same sector\r
1329 //\r
1330 return SectorsInTrack;\r
1331 } else {\r
1332 //\r
1333 // All the block range is in the same sector.\r
1334 //\r
1335 return NumberOfBlocks;\r
1336 }\r
1337}\r
1338\r
1339/**\r
1340 Read data sector from FDC device.\r
1341\r
1342 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.\r
1343 @param Info Information of floppy device.\r
1344 @param Buffer Buffer to setup for DMA.\r
1345 @param Lba The start address to read.\r
1346 @param NumberOfBlocks Number of blocks to read.\r
1347\r
1348 @retval EFI_SUCCESS Data successfully read out.\r
1349 @retval EFI_DEVICE_ERROR FDC device has errors.\r
1350 @retval EFI_TIMEOUT Command does not take effect in time.\r
1351\r
1352**/\r
1353EFI_STATUS\r
1354ReadDataSector (\r
1355 IN FDC_BLK_IO_DEV *FdcBlkIoDev,\r
1356 IN OUT PEI_FLOPPY_DEVICE_INFO *Info,\r
1357 IN VOID *Buffer,\r
1358 IN EFI_PEI_LBA Lba,\r
1359 IN UINTN NumberOfBlocks\r
1360 )\r
1361{\r
1362 EFI_STATUS Status;\r
1363 FDC_COMMAND_PACKET1 Command;\r
1364 FDC_RESULT_PACKET Result;\r
1365 UINTN Index;\r
1366 UINTN Times;\r
1367 UINT8 *Pointer;\r
1368\r
1369 Status = Seek (FdcBlkIoDev, Info, Lba);\r
1370 if (Status != EFI_SUCCESS) {\r
1371 return EFI_DEVICE_ERROR;\r
1372 }\r
1373\r
1374 //\r
1375 // Set up DMA\r
1376 //\r
1377 SetDMA (FdcBlkIoDev, Buffer, Info->MediaInfo.BlockSize, NumberOfBlocks);\r
1378\r
1379 //\r
1380 // Allocate Read command packet\r
1381 //\r
1382 ZeroMem (&Command, sizeof (FDC_COMMAND_PACKET1));\r
1383 Command.CommandCode = READ_DATA_CMD | CMD_MT | CMD_MFM | CMD_SK;\r
1384\r
1385 //\r
1386 // Fill parameters for command.\r
1387 //\r
1388 FillPara (Info, Lba, &Command);\r
1389\r
1390 //\r
1391 // Write command bytes to FDC\r
1392 //\r
1393 Pointer = (UINT8 *) (&Command);\r
1394 for (Index = 0; Index < sizeof (FDC_COMMAND_PACKET1); Index++) {\r
1395 if (DataOutByte (FdcBlkIoDev, Pointer++) != EFI_SUCCESS) {\r
1396 return EFI_DEVICE_ERROR;\r
1397 }\r
1398 }\r
1399\r
1400 //\r
1401 // Wait for some time until command takes effect.\r
1402 //\r
1403 Times = (STALL_1_SECOND / FDC_CHECK_INTERVAL) + 1;\r
1404 do {\r
1405 if ((IoRead8 ((UINT16) (PcdGet16 (PcdFdcBaseAddress) + FDC_REGISTER_MSR)) & 0xc0) == 0xc0) {\r
1406 break;\r
1407 }\r
1408\r
1409 MicroSecondDelay (FDC_SHORT_DELAY);\r
1410 } while (--Times > 0);\r
1411\r
1412 if (Times == 0) {\r
1413 //\r
1414 // Command fails to take effect in time, return EFI_TIMEOUT.\r
1415 //\r
1416 return EFI_TIMEOUT;\r
1417 }\r
1418\r
1419 //\r
1420 // Read result bytes from FDC\r
1421 //\r
1422 Pointer = (UINT8 *) (&Result);\r
1423 for (Index = 0; Index < sizeof (FDC_RESULT_PACKET); Index++) {\r
1424 if (DataInByte (FdcBlkIoDev, Pointer++) != EFI_SUCCESS) {\r
1425 return EFI_DEVICE_ERROR;\r
1426 }\r
1427 }\r
1428\r
1429 return CheckResult (&Result, Info);\r
1430}\r
1431\r
1432/**\r
1433 Gets the count of block I/O devices that one specific block driver detects.\r
1434\r
0a6f4824 1435 This function is used for getting the count of block I/O devices that one\r
d8387fa4 1436 specific block driver detects. To the PEI ATAPI driver, it returns the number\r
0a6f4824
LG
1437 of all the detected ATAPI devices it detects during the enumeration process.\r
1438 To the PEI legacy floppy driver, it returns the number of all the legacy\r
1439 devices it finds during its enumeration process. If no device is detected,\r
1440 then the function will return zero.\r
1441\r
1442 @param[in] PeiServices General-purpose services that are available\r
d8387fa4 1443 to every PEIM.\r
0a6f4824 1444 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI\r
d8387fa4 1445 instance.\r
1446 @param[out] NumberBlockDevices The number of block I/O devices discovered.\r
1447\r
1448 @retval EFI_SUCCESS Operation performed successfully.\r
1449\r
1450**/\r
1451EFI_STATUS\r
1452EFIAPI\r
1453FdcGetNumberOfBlockDevices (\r
1454 IN EFI_PEI_SERVICES **PeiServices,\r
1455 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,\r
1456 OUT UINTN *NumberBlockDevices\r
1457 )\r
1458{\r
1459 FDC_BLK_IO_DEV *FdcBlkIoDev;\r
1460\r
1461 FdcBlkIoDev = NULL;\r
1462\r
1463 FdcBlkIoDev = PEI_RECOVERY_FDC_FROM_BLKIO_THIS (This);\r
1464\r
1465 *NumberBlockDevices = FdcBlkIoDev->DeviceCount;\r
1466\r
1467 return EFI_SUCCESS;\r
1468}\r
1469\r
1470/**\r
1471 Gets a block device's media information.\r
1472\r
0a6f4824
LG
1473 This function will provide the caller with the specified block device's media\r
1474 information. If the media changes, calling this function will update the media\r
d8387fa4 1475 information accordingly.\r
1476\r
1477 @param[in] PeiServices General-purpose services that are available to every\r
1478 PEIM\r
1479 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.\r
0a6f4824
LG
1480 @param[in] DeviceIndex Specifies the block device to which the function wants\r
1481 to talk. Because the driver that implements Block I/O\r
1482 PPIs will manage multiple block devices, the PPIs that\r
1483 want to talk to a single device must specify the\r
d8387fa4 1484 device index that was assigned during the enumeration\r
0a6f4824 1485 process. This index is a number from one to\r
d8387fa4 1486 NumberBlockDevices.\r
0a6f4824
LG
1487 @param[out] MediaInfo The media information of the specified block media.\r
1488 The caller is responsible for the ownership of this\r
d8387fa4 1489 data structure.\r
0a6f4824
LG
1490\r
1491 @retval EFI_SUCCESS Media information about the specified block device\r
d8387fa4 1492 was obtained successfully.\r
0a6f4824 1493 @retval EFI_DEVICE_ERROR Cannot get the media information due to a hardware\r
d8387fa4 1494 error.\r
1495 @retval Others Other failure occurs.\r
1496\r
1497**/\r
1498EFI_STATUS\r
1499EFIAPI\r
1500FdcGetBlockDeviceMediaInfo (\r
1501 IN EFI_PEI_SERVICES **PeiServices,\r
1502 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,\r
1503 IN UINTN DeviceIndex,\r
1504 OUT EFI_PEI_BLOCK_IO_MEDIA *MediaInfo\r
1505 )\r
1506{\r
1507 UINTN DeviceCount;\r
1508 FDC_BLK_IO_DEV *FdcBlkIoDev;\r
1509 BOOLEAN Healthy;\r
1510 UINTN Index;\r
1511\r
1512 FdcBlkIoDev = NULL;\r
1513\r
1514 if (This == NULL || MediaInfo == NULL) {\r
1515 return EFI_INVALID_PARAMETER;\r
1516 }\r
1517\r
1518 FdcBlkIoDev = PEI_RECOVERY_FDC_FROM_BLKIO_THIS (This);\r
1519\r
1520 DeviceCount = FdcBlkIoDev->DeviceCount;\r
1521\r
1522 //\r
1523 // DeviceIndex is a value from 1 to NumberBlockDevices.\r
1524 //\r
1525 if ((DeviceIndex < 1) || (DeviceIndex > DeviceCount) || (DeviceIndex > 2)) {\r
1526 return EFI_INVALID_PARAMETER;\r
1527 }\r
1528\r
1529 Index = DeviceIndex - 1;\r
1530 //\r
1531 // Probe media and retrieve latest media information\r
1532 //\r
1533 Healthy = DiscoverFdcDevice (\r
1534 FdcBlkIoDev,\r
1535 &FdcBlkIoDev->DeviceInfo[Index],\r
1536 MediaInfo\r
1537 );\r
1538\r
1539 if (!Healthy) {\r
1540 return EFI_DEVICE_ERROR;\r
1541 }\r
1542\r
1543 CopyMem (\r
1544 &(FdcBlkIoDev->DeviceInfo[Index].MediaInfo),\r
1545 MediaInfo,\r
1546 sizeof (EFI_PEI_BLOCK_IO_MEDIA)\r
1547 );\r
1548\r
1549 return EFI_SUCCESS;\r
1550}\r
1551\r
1552/**\r
1553 Reads the requested number of blocks from the specified block device.\r
1554\r
0a6f4824 1555 The function reads the requested number of blocks from the device. All the\r
d8387fa4 1556 blocks are read, or an error is returned. If there is no media in the device,\r
1557 the function returns EFI_NO_MEDIA.\r
1558\r
0a6f4824 1559 @param[in] PeiServices General-purpose services that are available to\r
d8387fa4 1560 every PEIM.\r
1561 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.\r
0a6f4824
LG
1562 @param[in] DeviceIndex Specifies the block device to which the function wants\r
1563 to talk. Because the driver that implements Block I/O\r
1564 PPIs will manage multiple block devices, the PPIs that\r
1565 want to talk to a single device must specify the device\r
1566 index that was assigned during the enumeration process.\r
d8387fa4 1567 This index is a number from one to NumberBlockDevices.\r
1568 @param[in] StartLBA The starting logical block address (LBA) to read from\r
1569 on the device\r
1570 @param[in] BufferSize The size of the Buffer in bytes. This number must be\r
1571 a multiple of the intrinsic block size of the device.\r
1572 @param[out] Buffer A pointer to the destination buffer for the data.\r
0a6f4824 1573 The caller is responsible for the ownership of the\r
d8387fa4 1574 buffer.\r
0a6f4824 1575\r
d8387fa4 1576 @retval EFI_SUCCESS The data was read correctly from the device.\r
0a6f4824 1577 @retval EFI_DEVICE_ERROR The device reported an error while attempting\r
d8387fa4 1578 to perform the read operation.\r
0a6f4824 1579 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not\r
d8387fa4 1580 valid, or the buffer is not properly aligned.\r
1581 @retval EFI_NO_MEDIA There is no media in the device.\r
1582 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of\r
1583 the intrinsic block size of the device.\r
1584\r
1585**/\r
1586EFI_STATUS\r
1587EFIAPI\r
1588FdcReadBlocks (\r
1589 IN EFI_PEI_SERVICES **PeiServices,\r
1590 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,\r
1591 IN UINTN DeviceIndex,\r
1592 IN EFI_PEI_LBA StartLBA,\r
1593 IN UINTN BufferSize,\r
1594 OUT VOID *Buffer\r
1595 )\r
1596{\r
1597 EFI_PEI_BLOCK_IO_MEDIA MediaInfo;\r
1598 EFI_STATUS Status;\r
1599 UINTN Count;\r
1600 UINTN NumberOfBlocks;\r
1601 UINTN BlockSize;\r
1602 FDC_BLK_IO_DEV *FdcBlkIoDev;\r
1603 VOID *MemPage;\r
1604\r
1605 FdcBlkIoDev = NULL;\r
45295cf5 1606 ZeroMem (&MediaInfo, sizeof (EFI_PEI_BLOCK_IO_MEDIA));\r
d8387fa4 1607\r
1608 if (This == NULL) {\r
1609 return EFI_INVALID_PARAMETER;\r
1610 }\r
1611\r
1612 FdcBlkIoDev = PEI_RECOVERY_FDC_FROM_BLKIO_THIS (This);\r
1613\r
1614 if (Buffer == NULL) {\r
1615 return EFI_INVALID_PARAMETER;\r
1616 }\r
1617\r
1618 Status = FdcGetBlockDeviceMediaInfo (PeiServices, This, DeviceIndex, &MediaInfo);\r
1619 if (Status != EFI_SUCCESS) {\r
1620 return EFI_DEVICE_ERROR;\r
1621 }\r
1622\r
1623 if (!MediaInfo.MediaPresent) {\r
1624 return EFI_NO_MEDIA;\r
1625 }\r
1626\r
1627 BlockSize = MediaInfo.BlockSize;\r
1628\r
1629 //\r
1630 // If BufferSize cannot be divided by block size of FDC device,\r
1631 // return EFI_BAD_BUFFER_SIZE.\r
1632 //\r
1633 if (BufferSize % BlockSize != 0) {\r
1634 return EFI_BAD_BUFFER_SIZE;\r
1635 }\r
1636\r
1637 NumberOfBlocks = BufferSize / BlockSize;\r
1638\r
1639 if ((StartLBA + NumberOfBlocks - 1) > FdcBlkIoDev->DeviceInfo[DeviceIndex - 1].MediaInfo.LastBlock) {\r
1640 return EFI_INVALID_PARAMETER;\r
1641 }\r
1642\r
1643 MemPage = AllocatePages (EFI_SIZE_TO_PAGES (BufferSize));\r
1644 if ((MemPage == NULL) || ((UINTN) MemPage >= ISA_MAX_MEMORY_ADDRESS)) {\r
1645 //\r
1646 // If fail to allocate memory under ISA_MAX_MEMORY_ADDRESS, designate the address space for DMA\r
1647 //\r
1648 MemPage = (VOID *) ((UINTN) (UINT32) 0x0f00000);\r
1649 }\r
1650 Status = MotorOn (FdcBlkIoDev, &(FdcBlkIoDev->DeviceInfo[DeviceIndex - 1]));\r
1651 if (Status != EFI_SUCCESS) {\r
1652 return EFI_DEVICE_ERROR;\r
1653 }\r
1654\r
1655 Status = Setup (FdcBlkIoDev, FdcBlkIoDev->DeviceInfo[DeviceIndex - 1].DevPos);\r
1656 if (Status != EFI_SUCCESS) {\r
1657 MotorOff (FdcBlkIoDev, &(FdcBlkIoDev->DeviceInfo[DeviceIndex - 1]));\r
1658 return EFI_DEVICE_ERROR;\r
1659 }\r
1660 //\r
1661 // Read data in batches.\r
1662 // Blocks in the same cylinder are read out in a batch.\r
1663 //\r
1664 while ((Count = GetTransferBlockCount (\r
1665 &(FdcBlkIoDev->DeviceInfo[DeviceIndex - 1]),\r
1666 StartLBA,\r
1667 NumberOfBlocks\r
1668 )) != 0 && Status == EFI_SUCCESS) {\r
1669 Status = ReadDataSector (\r
1670 FdcBlkIoDev,\r
1671 &(FdcBlkIoDev->DeviceInfo[DeviceIndex - 1]),\r
1672 MemPage,\r
1673 StartLBA,\r
1674 Count\r
1675 );\r
1676 CopyMem (Buffer, MemPage, BlockSize * Count);\r
1677 StartLBA += Count;\r
1678 NumberOfBlocks -= Count;\r
1679 Buffer = (VOID *) ((UINTN) Buffer + Count * BlockSize);\r
1680 }\r
1681\r
1682 MotorOff (FdcBlkIoDev, &(FdcBlkIoDev->DeviceInfo[DeviceIndex - 1]));\r
1683\r
1684 switch (Status) {\r
1685 case EFI_SUCCESS:\r
1686 return EFI_SUCCESS;\r
1687\r
1688 default:\r
1689 FdcReset (FdcBlkIoDev, FdcBlkIoDev->DeviceInfo[DeviceIndex - 1].DevPos);\r
1690 return EFI_DEVICE_ERROR;\r
1691 }\r
1692}\r
1693\r
1694/**\r
1695 Initializes the floppy disk controller and installs FDC Block I/O PPI.\r
1696\r
1697 @param FileHandle Handle of the file being invoked.\r
1698 @param PeiServices Describes the list of possible PEI Services.\r
1699\r
1700 @retval EFI_SUCCESS Successfully initialized FDC and installed PPI.\r
1701 @retval EFI_NOT_FOUND Cannot find FDC device.\r
1702 @retval EFI_OUT_OF_RESOURCES Have no enough memory to create instance or descriptors.\r
1703 @retval Other Fail to install FDC Block I/O PPI.\r
1704\r
1705**/\r
1706EFI_STATUS\r
1707EFIAPI\r
1708FdcPeimEntry (\r
1709 IN EFI_PEI_FILE_HANDLE FileHandle,\r
1710 IN CONST EFI_PEI_SERVICES **PeiServices\r
1711 )\r
1712{\r
1713 EFI_STATUS Status;\r
1714 FDC_BLK_IO_DEV *FdcBlkIoDev;\r
1715 UINTN DeviceCount;\r
1716 UINT32 Index;\r
1717\r
1718 Status = PeiServicesRegisterForShadow (FileHandle);\r
1719 if (!EFI_ERROR (Status)) {\r
1720 return Status;\r
1721 }\r
1722\r
1723 //\r
1724 // Allocate memory for instance of FDC_BLK_IO_DEV and copy initial value\r
0a6f4824 1725 // from template to it.\r
d8387fa4 1726 //\r
1727 FdcBlkIoDev = AllocatePages (EFI_SIZE_TO_PAGES(sizeof (FDC_BLK_IO_DEV)));\r
1728 if (FdcBlkIoDev == NULL) {\r
1729 return EFI_OUT_OF_RESOURCES;\r
1730 }\r
1731 CopyMem (FdcBlkIoDev, &mBlockIoDevTemplate, sizeof (mBlockIoDevTemplate));\r
1732\r
1733 //\r
1734 // Initialize DMA controller to enable all channels.\r
1735 //\r
1736 for (Index = 0; Index < sizeof (mRegisterTable) / sizeof (PEI_DMA_TABLE); Index++) {\r
1737 IoWrite8 (mRegisterTable[Index].Register, mRegisterTable[Index].Value);\r
1738 }\r
1739 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, EFI_PERIPHERAL_REMOVABLE_MEDIA + EFI_P_PC_INIT);\r
1740\r
1741 //\r
1742 // Enumerate FDC devices.\r
1743 //\r
1744 DeviceCount = FdcEnumeration (FdcBlkIoDev);\r
1745 if (DeviceCount == 0) {\r
1746 return EFI_NOT_FOUND;\r
1747 }\r
1748\r
1749 FdcBlkIoDev->PpiDescriptor.Ppi = &FdcBlkIoDev->FdcBlkIo;\r
1750\r
1751 return PeiServicesInstallPpi (&FdcBlkIoDev->PpiDescriptor);\r
1752}\r