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