]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Bus/Isa/IsaFloppyPei/FloppyPeim.c
IntelFrameworkModulePkg: Add IsaFloppyPei driver
[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
4Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
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
1021 switch (Status) {\r
1022 case EFI_NO_MEDIA:\r
1023 //\r
1024 // No diskette in floppy.\r
1025 //\r
1026 MediaInfo->MediaPresent = FALSE;\r
1027 break;\r
1028\r
1029 case EFI_MEDIA_CHANGED:\r
1030 case EFI_SUCCESS:\r
1031 //\r
1032 // Diskette exists in floppy.\r
1033 //\r
1034 break;\r
1035\r
1036 default:\r
1037 //\r
1038 // EFI_DEVICE_ERROR\r
1039 //\r
1040 MotorOff (FdcBlkIoDev, Info);\r
1041 return FALSE;\r
1042 }\r
1043\r
1044 MotorOff (FdcBlkIoDev, Info);\r
1045\r
1046 //\r
1047 // Get the base of disk parameter information corresponding to its type.\r
1048 //\r
1049 Para = (DISKET_PARA_TABLE *) ((UINT8 *) DiskPara + sizeof (DISKET_PARA_TABLE) * Info->Type);\r
1050\r
1051 MediaInfo->BlockSize = BytePerSector[Para->Number];\r
1052 MediaInfo->LastBlock = Para->EndOfTrack * 2 * (Para->MaxTrackNum + 1) - 1;\r
1053\r
1054 return TRUE;\r
1055}\r
1056\r
1057/**\r
1058 Enumerate floppy device\r
1059\r
1060 @param FdcBlkIoDev Instance of floppy device controller\r
1061\r
1062 @return Number of FDC devices.\r
1063\r
1064**/\r
1065UINT8\r
1066FdcEnumeration (\r
1067 IN FDC_BLK_IO_DEV *FdcBlkIoDev\r
1068 )\r
1069{\r
1070 UINT8 DevPos;\r
1071 UINT8 DevNo;\r
1072 EFI_PEI_BLOCK_IO_MEDIA MediaInfo;\r
1073 EFI_STATUS Status;\r
1074\r
1075 DevNo = 0;\r
1076\r
1077 //\r
1078 // DevPos=0 means Drive A, 1 means Drive B.\r
1079 //\r
1080 for (DevPos = 0; DevPos < 2; DevPos++) {\r
1081 //\r
1082 // Detecting device presence\r
1083 //\r
1084 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, EFI_PERIPHERAL_REMOVABLE_MEDIA + EFI_P_PC_PRESENCE_DETECT);\r
1085\r
1086 //\r
1087 // Reset FDC\r
1088 //\r
1089 Status = FdcReset (FdcBlkIoDev, DevPos);\r
1090\r
1091 if (EFI_ERROR (Status)) {\r
1092 continue;\r
1093 }\r
1094\r
1095 FdcBlkIoDev->DeviceInfo[DevPos].DevPos = DevPos;\r
1096 FdcBlkIoDev->DeviceInfo[DevPos].Pcn = 0;\r
1097 FdcBlkIoDev->DeviceInfo[DevPos].MotorOn = FALSE;\r
1098 FdcBlkIoDev->DeviceInfo[DevPos].NeedRecalibrate = TRUE;\r
1099 FdcBlkIoDev->DeviceInfo[DevPos].Type = FdcType1440K1440K;\r
1100\r
1101 //\r
1102 // Discover FDC device\r
1103 //\r
1104 if (DiscoverFdcDevice (FdcBlkIoDev, &(FdcBlkIoDev->DeviceInfo[DevPos]), &MediaInfo)) {\r
1105 FdcBlkIoDev->DeviceInfo[DevNo].DevPos = DevPos;\r
1106\r
1107 FdcBlkIoDev->DeviceInfo[DevNo].Pcn = FdcBlkIoDev->DeviceInfo[DevPos].Pcn;\r
1108 FdcBlkIoDev->DeviceInfo[DevNo].MotorOn = FdcBlkIoDev->DeviceInfo[DevPos].MotorOn;\r
1109 FdcBlkIoDev->DeviceInfo[DevNo].NeedRecalibrate = FdcBlkIoDev->DeviceInfo[DevPos].NeedRecalibrate;\r
1110 FdcBlkIoDev->DeviceInfo[DevNo].Type = FdcBlkIoDev->DeviceInfo[DevPos].Type;\r
1111\r
1112 CopyMem (\r
1113 &(FdcBlkIoDev->DeviceInfo[DevNo].MediaInfo),\r
1114 &MediaInfo,\r
1115 sizeof (EFI_PEI_BLOCK_IO_MEDIA)\r
1116 );\r
1117\r
1118 DevNo++;\r
1119 } else {\r
1120 //\r
1121 // Assume controller error\r
1122 //\r
1123 REPORT_STATUS_CODE (\r
1124 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
1125 EFI_PERIPHERAL_REMOVABLE_MEDIA + EFI_P_EC_CONTROLLER_ERROR\r
1126 );\r
1127 }\r
1128 }\r
1129\r
1130 FdcBlkIoDev->DeviceCount = DevNo;\r
1131 return DevNo;\r
1132}\r
1133\r
1134/**\r
1135 Checks result reflected by FDC_RESULT_PACKET.\r
1136\r
1137 @param Result FDC_RESULT_PACKET read from FDC after certain operation.\r
1138 @param Info Information of floppy device.\r
1139\r
1140 @retval EFI_SUCCESS Result is healthy.\r
1141 @retval EFI_DEVICE_ERROR Result is not healthy.\r
1142\r
1143**/\r
1144EFI_STATUS\r
1145CheckResult (\r
1146 IN FDC_RESULT_PACKET *Result,\r
1147 OUT PEI_FLOPPY_DEVICE_INFO *Info\r
1148 )\r
1149{\r
1150 if ((Result->Status0 & STS0_IC) != IC_NT) {\r
1151 if ((Result->Status0 & STS0_SE) == BIT5) {\r
1152 //\r
1153 // Seek error\r
1154 //\r
1155 Info->NeedRecalibrate = TRUE;\r
1156 }\r
1157\r
1158 Info->NeedRecalibrate = TRUE;\r
1159 return EFI_DEVICE_ERROR;\r
1160 }\r
1161 //\r
1162 // Check Status Register1\r
1163 //\r
1164 if ((Result->Status1 & (STS1_EN | STS1_DE | STS1_OR | STS1_ND | STS1_NW | STS1_MA)) != 0) {\r
1165 Info->NeedRecalibrate = TRUE;\r
1166 return EFI_DEVICE_ERROR;\r
1167 }\r
1168 //\r
1169 // Check Status Register2\r
1170 //\r
1171 if ((Result->Status2 & (STS2_CM | STS2_DD | STS2_WC | STS2_BC | STS2_MD)) != 0) {\r
1172 Info->NeedRecalibrate = TRUE;\r
1173 return EFI_DEVICE_ERROR;\r
1174 }\r
1175\r
1176 return EFI_SUCCESS;\r
1177}\r
1178\r
1179/**\r
1180 Fill parameters for command packet.\r
1181\r
1182 @param Info Information of floppy device.\r
1183 @param Lba Logical block address.\r
1184 @param Command Command for which for fill parameters.\r
1185\r
1186**/\r
1187VOID\r
1188FillPara (\r
1189 IN PEI_FLOPPY_DEVICE_INFO *Info,\r
1190 IN EFI_PEI_LBA Lba,\r
1191 OUT FDC_COMMAND_PACKET1 *Command\r
1192 )\r
1193{\r
1194 DISKET_PARA_TABLE *Para;\r
1195 UINT8 EndOfTrack;\r
1196 UINT8 DevPos;\r
1197\r
1198 DevPos = Info->DevPos;\r
1199\r
1200 //\r
1201 // Get the base of disk parameter information corresponding to its type.\r
1202 //\r
1203 Para = (DISKET_PARA_TABLE *) ((UINT8 *) DiskPara + sizeof (DISKET_PARA_TABLE) * Info->Type);\r
1204\r
1205 EndOfTrack = Para->EndOfTrack;\r
1206\r
1207 if (DevPos == 0) {\r
1208 Command->DiskHeadSel = 0;\r
1209 } else {\r
1210 Command->DiskHeadSel = 1;\r
1211 }\r
1212\r
1213 Command->Cylinder = (UINT8) ((UINTN) Lba / EndOfTrack / 2);\r
1214 Command->Head = (UINT8) ((UINTN) Lba / EndOfTrack % 2);\r
1215 Command->Sector = (UINT8) ((UINT8) ((UINTN) Lba % EndOfTrack) + 1);\r
1216 Command->DiskHeadSel = (UINT8) (Command->DiskHeadSel | (Command->Head << 2));\r
1217 Command->Number = Para->Number;\r
1218 Command->EndOfTrack = Para->EndOfTrack;\r
1219 Command->GapLength = Para->GapLength;\r
1220 Command->DataLength = Para->DataLength;\r
1221}\r
1222\r
1223/**\r
1224 Setup specifed FDC device.\r
1225\r
1226 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.\r
1227 @param DevPos Index of FDC device.\r
1228\r
1229 @retval EFI_SUCCESS FDC device successfully set up.\r
1230 @retval EFI_DEVICE_ERROR FDC device has errors.\r
1231\r
1232**/\r
1233EFI_STATUS\r
1234Setup (\r
1235 IN FDC_BLK_IO_DEV *FdcBlkIoDev,\r
1236 IN UINT8 DevPos\r
1237 )\r
1238{\r
1239 EFI_STATUS Status;\r
1240\r
1241 IoWrite8 ((UINT16) (PcdGet16 (PcdFdcBaseAddress) + FDC_REGISTER_CCR), 0x0);\r
1242\r
1243 MicroSecondDelay (FDC_MEDIUM_DELAY);\r
1244\r
1245 Status = Specify (FdcBlkIoDev);\r
1246 return Status;\r
1247}\r
1248\r
1249/**\r
1250 Setup DMA channels to read data.\r
1251\r
1252 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.\r
1253 @param Buffer Memory buffer for DMA transfer.\r
1254 @param BlockSize the number of the bytes in one block.\r
1255 @param NumberOfBlocks Number of blocks to read.\r
1256\r
1257**/\r
1258VOID\r
1259SetDMA (\r
1260 IN FDC_BLK_IO_DEV *FdcBlkIoDev,\r
1261 IN VOID *Buffer,\r
1262 IN UINTN BlockSize,\r
1263 IN UINTN NumberOfBlocks\r
1264 )\r
1265{\r
1266 UINT8 Data;\r
1267 UINTN Count;\r
1268\r
1269 //\r
1270 // Mask DMA channel 2;\r
1271 //\r
1272 IoWrite8 (R_8237_DMA_WRSMSK_CH0_3, B_8237_DMA_WRSMSK_CMS | 2);\r
1273\r
1274 //\r
1275 // Clear first/last flip flop\r
1276 //\r
1277 IoWrite8 (R_8237_DMA_CBPR_CH0_3, B_8237_DMA_WRSMSK_CMS | 2);\r
1278\r
1279 //\r
1280 // Set mode\r
1281 //\r
1282 IoWrite8 (R_8237_DMA_CHMODE_CH0_3, V_8237_DMA_CHMODE_SINGLE | V_8237_DMA_CHMODE_IO2MEM | 2);\r
1283\r
1284 //\r
1285 // Set base address and page register\r
1286 //\r
1287 Data = (UINT8) (UINTN) Buffer;\r
1288 IoWrite8 (R_8237_DMA_BASE_CA_CH2, Data);\r
1289 Data = (UINT8) ((UINTN) Buffer >> 8);\r
1290 IoWrite8 (R_8237_DMA_BASE_CA_CH2, Data);\r
1291\r
1292 Data = (UINT8) ((UINTN) Buffer >> 16);\r
1293 IoWrite8 (R_8237_DMA_MEM_LP_CH2, Data);\r
1294\r
1295 //\r
1296 // Set count register\r
1297 //\r
1298 Count = BlockSize * NumberOfBlocks - 1;\r
1299 Data = (UINT8) (Count & 0xff);\r
1300 IoWrite8 (R_8237_DMA_BASE_CC_CH2, Data);\r
1301 Data = (UINT8) (Count >> 8);\r
1302 IoWrite8 (R_8237_DMA_BASE_CC_CH2, Data);\r
1303\r
1304 //\r
1305 // Clear channel 2 mask\r
1306 //\r
1307 IoWrite8 (R_8237_DMA_WRSMSK_CH0_3, 0x02);\r
1308}\r
1309\r
1310\r
1311/**\r
1312 According to the block range specified by Lba and NumberOfBlocks, calculate\r
1313 the number of blocks in the same sector, which can be transferred in a batch.\r
1314\r
1315 @param Info Information of floppy device.\r
1316 @param Lba Start address of block range.\r
1317 @param NumberOfBlocks Number of blocks of the range.\r
1318\r
1319 @return Number of blocks in the same sector.\r
1320\r
1321**/\r
1322UINTN\r
1323GetTransferBlockCount (\r
1324 IN PEI_FLOPPY_DEVICE_INFO *Info,\r
1325 IN EFI_PEI_LBA Lba,\r
1326 IN UINTN NumberOfBlocks\r
1327 )\r
1328{\r
1329 DISKET_PARA_TABLE *Para;\r
1330 UINT8 EndOfTrack;\r
1331 UINT8 Head;\r
1332 UINT8 SectorsInTrack;\r
1333\r
1334 //\r
1335 // Get the base of disk parameter information corresponding to its type.\r
1336 //\r
1337 Para = (DISKET_PARA_TABLE *) ((UINT8 *) DiskPara + sizeof (DISKET_PARA_TABLE) * Info->Type);\r
1338\r
1339 EndOfTrack = Para->EndOfTrack;\r
1340 Head = (UINT8) ((UINTN) Lba / EndOfTrack % 2);\r
1341\r
1342 SectorsInTrack = (UINT8) (EndOfTrack * (2 - Head) - (UINT8) ((UINTN) Lba % EndOfTrack));\r
1343 if (SectorsInTrack < NumberOfBlocks) {\r
1344 //\r
1345 // Not all the block range locates in the same sector\r
1346 //\r
1347 return SectorsInTrack;\r
1348 } else {\r
1349 //\r
1350 // All the block range is in the same sector.\r
1351 //\r
1352 return NumberOfBlocks;\r
1353 }\r
1354}\r
1355\r
1356/**\r
1357 Read data sector from FDC device.\r
1358\r
1359 @param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.\r
1360 @param Info Information of floppy device.\r
1361 @param Buffer Buffer to setup for DMA.\r
1362 @param Lba The start address to read.\r
1363 @param NumberOfBlocks Number of blocks to read.\r
1364\r
1365 @retval EFI_SUCCESS Data successfully read out.\r
1366 @retval EFI_DEVICE_ERROR FDC device has errors.\r
1367 @retval EFI_TIMEOUT Command does not take effect in time.\r
1368\r
1369**/\r
1370EFI_STATUS\r
1371ReadDataSector (\r
1372 IN FDC_BLK_IO_DEV *FdcBlkIoDev,\r
1373 IN OUT PEI_FLOPPY_DEVICE_INFO *Info,\r
1374 IN VOID *Buffer,\r
1375 IN EFI_PEI_LBA Lba,\r
1376 IN UINTN NumberOfBlocks\r
1377 )\r
1378{\r
1379 EFI_STATUS Status;\r
1380 FDC_COMMAND_PACKET1 Command;\r
1381 FDC_RESULT_PACKET Result;\r
1382 UINTN Index;\r
1383 UINTN Times;\r
1384 UINT8 *Pointer;\r
1385\r
1386 Status = Seek (FdcBlkIoDev, Info, Lba);\r
1387 if (Status != EFI_SUCCESS) {\r
1388 return EFI_DEVICE_ERROR;\r
1389 }\r
1390\r
1391 //\r
1392 // Set up DMA\r
1393 //\r
1394 SetDMA (FdcBlkIoDev, Buffer, Info->MediaInfo.BlockSize, NumberOfBlocks);\r
1395\r
1396 //\r
1397 // Allocate Read command packet\r
1398 //\r
1399 ZeroMem (&Command, sizeof (FDC_COMMAND_PACKET1));\r
1400 Command.CommandCode = READ_DATA_CMD | CMD_MT | CMD_MFM | CMD_SK;\r
1401\r
1402 //\r
1403 // Fill parameters for command.\r
1404 //\r
1405 FillPara (Info, Lba, &Command);\r
1406\r
1407 //\r
1408 // Write command bytes to FDC\r
1409 //\r
1410 Pointer = (UINT8 *) (&Command);\r
1411 for (Index = 0; Index < sizeof (FDC_COMMAND_PACKET1); Index++) {\r
1412 if (DataOutByte (FdcBlkIoDev, Pointer++) != EFI_SUCCESS) {\r
1413 return EFI_DEVICE_ERROR;\r
1414 }\r
1415 }\r
1416\r
1417 //\r
1418 // Wait for some time until command takes effect.\r
1419 //\r
1420 Times = (STALL_1_SECOND / FDC_CHECK_INTERVAL) + 1;\r
1421 do {\r
1422 if ((IoRead8 ((UINT16) (PcdGet16 (PcdFdcBaseAddress) + FDC_REGISTER_MSR)) & 0xc0) == 0xc0) {\r
1423 break;\r
1424 }\r
1425\r
1426 MicroSecondDelay (FDC_SHORT_DELAY);\r
1427 } while (--Times > 0);\r
1428\r
1429 if (Times == 0) {\r
1430 //\r
1431 // Command fails to take effect in time, return EFI_TIMEOUT.\r
1432 //\r
1433 return EFI_TIMEOUT;\r
1434 }\r
1435\r
1436 //\r
1437 // Read result bytes from FDC\r
1438 //\r
1439 Pointer = (UINT8 *) (&Result);\r
1440 for (Index = 0; Index < sizeof (FDC_RESULT_PACKET); Index++) {\r
1441 if (DataInByte (FdcBlkIoDev, Pointer++) != EFI_SUCCESS) {\r
1442 return EFI_DEVICE_ERROR;\r
1443 }\r
1444 }\r
1445\r
1446 return CheckResult (&Result, Info);\r
1447}\r
1448\r
1449/**\r
1450 Gets the count of block I/O devices that one specific block driver detects.\r
1451\r
1452 This function is used for getting the count of block I/O devices that one \r
1453 specific block driver detects. To the PEI ATAPI driver, it returns the number\r
1454 of all the detected ATAPI devices it detects during the enumeration process. \r
1455 To the PEI legacy floppy driver, it returns the number of all the legacy \r
1456 devices it finds during its enumeration process. If no device is detected, \r
1457 then the function will return zero. \r
1458 \r
1459 @param[in] PeiServices General-purpose services that are available \r
1460 to every PEIM.\r
1461 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI \r
1462 instance.\r
1463 @param[out] NumberBlockDevices The number of block I/O devices discovered.\r
1464\r
1465 @retval EFI_SUCCESS Operation performed successfully.\r
1466\r
1467**/\r
1468EFI_STATUS\r
1469EFIAPI\r
1470FdcGetNumberOfBlockDevices (\r
1471 IN EFI_PEI_SERVICES **PeiServices,\r
1472 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,\r
1473 OUT UINTN *NumberBlockDevices\r
1474 )\r
1475{\r
1476 FDC_BLK_IO_DEV *FdcBlkIoDev;\r
1477\r
1478 FdcBlkIoDev = NULL;\r
1479\r
1480 FdcBlkIoDev = PEI_RECOVERY_FDC_FROM_BLKIO_THIS (This);\r
1481\r
1482 *NumberBlockDevices = FdcBlkIoDev->DeviceCount;\r
1483\r
1484 return EFI_SUCCESS;\r
1485}\r
1486\r
1487/**\r
1488 Gets a block device's media information.\r
1489\r
1490 This function will provide the caller with the specified block device's media \r
1491 information. If the media changes, calling this function will update the media \r
1492 information accordingly.\r
1493\r
1494 @param[in] PeiServices General-purpose services that are available to every\r
1495 PEIM\r
1496 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.\r
1497 @param[in] DeviceIndex Specifies the block device to which the function wants \r
1498 to talk. Because the driver that implements Block I/O \r
1499 PPIs will manage multiple block devices, the PPIs that \r
1500 want to talk to a single device must specify the \r
1501 device index that was assigned during the enumeration\r
1502 process. This index is a number from one to \r
1503 NumberBlockDevices.\r
1504 @param[out] MediaInfo The media information of the specified block media. \r
1505 The caller is responsible for the ownership of this \r
1506 data structure.\r
1507 \r
1508 @retval EFI_SUCCESS Media information about the specified block device \r
1509 was obtained successfully.\r
1510 @retval EFI_DEVICE_ERROR Cannot get the media information due to a hardware \r
1511 error.\r
1512 @retval Others Other failure occurs.\r
1513\r
1514**/\r
1515EFI_STATUS\r
1516EFIAPI\r
1517FdcGetBlockDeviceMediaInfo (\r
1518 IN EFI_PEI_SERVICES **PeiServices,\r
1519 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,\r
1520 IN UINTN DeviceIndex,\r
1521 OUT EFI_PEI_BLOCK_IO_MEDIA *MediaInfo\r
1522 )\r
1523{\r
1524 UINTN DeviceCount;\r
1525 FDC_BLK_IO_DEV *FdcBlkIoDev;\r
1526 BOOLEAN Healthy;\r
1527 UINTN Index;\r
1528\r
1529 FdcBlkIoDev = NULL;\r
1530\r
1531 if (This == NULL || MediaInfo == NULL) {\r
1532 return EFI_INVALID_PARAMETER;\r
1533 }\r
1534\r
1535 FdcBlkIoDev = PEI_RECOVERY_FDC_FROM_BLKIO_THIS (This);\r
1536\r
1537 DeviceCount = FdcBlkIoDev->DeviceCount;\r
1538\r
1539 //\r
1540 // DeviceIndex is a value from 1 to NumberBlockDevices.\r
1541 //\r
1542 if ((DeviceIndex < 1) || (DeviceIndex > DeviceCount) || (DeviceIndex > 2)) {\r
1543 return EFI_INVALID_PARAMETER;\r
1544 }\r
1545\r
1546 Index = DeviceIndex - 1;\r
1547 //\r
1548 // Probe media and retrieve latest media information\r
1549 //\r
1550 Healthy = DiscoverFdcDevice (\r
1551 FdcBlkIoDev,\r
1552 &FdcBlkIoDev->DeviceInfo[Index],\r
1553 MediaInfo\r
1554 );\r
1555\r
1556 if (!Healthy) {\r
1557 return EFI_DEVICE_ERROR;\r
1558 }\r
1559\r
1560 CopyMem (\r
1561 &(FdcBlkIoDev->DeviceInfo[Index].MediaInfo),\r
1562 MediaInfo,\r
1563 sizeof (EFI_PEI_BLOCK_IO_MEDIA)\r
1564 );\r
1565\r
1566 return EFI_SUCCESS;\r
1567}\r
1568\r
1569/**\r
1570 Reads the requested number of blocks from the specified block device.\r
1571\r
1572 The function reads the requested number of blocks from the device. All the \r
1573 blocks are read, or an error is returned. If there is no media in the device,\r
1574 the function returns EFI_NO_MEDIA.\r
1575\r
1576 @param[in] PeiServices General-purpose services that are available to \r
1577 every PEIM.\r
1578 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.\r
1579 @param[in] DeviceIndex Specifies the block device to which the function wants \r
1580 to talk. Because the driver that implements Block I/O \r
1581 PPIs will manage multiple block devices, the PPIs that \r
1582 want to talk to a single device must specify the device \r
1583 index that was assigned during the enumeration process. \r
1584 This index is a number from one to NumberBlockDevices.\r
1585 @param[in] StartLBA The starting logical block address (LBA) to read from\r
1586 on the device\r
1587 @param[in] BufferSize The size of the Buffer in bytes. This number must be\r
1588 a multiple of the intrinsic block size of the device.\r
1589 @param[out] Buffer A pointer to the destination buffer for the data.\r
1590 The caller is responsible for the ownership of the \r
1591 buffer.\r
1592 \r
1593 @retval EFI_SUCCESS The data was read correctly from the device.\r
1594 @retval EFI_DEVICE_ERROR The device reported an error while attempting \r
1595 to perform the read operation.\r
1596 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not \r
1597 valid, or the buffer is not properly aligned.\r
1598 @retval EFI_NO_MEDIA There is no media in the device.\r
1599 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of\r
1600 the intrinsic block size of the device.\r
1601\r
1602**/\r
1603EFI_STATUS\r
1604EFIAPI\r
1605FdcReadBlocks (\r
1606 IN EFI_PEI_SERVICES **PeiServices,\r
1607 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,\r
1608 IN UINTN DeviceIndex,\r
1609 IN EFI_PEI_LBA StartLBA,\r
1610 IN UINTN BufferSize,\r
1611 OUT VOID *Buffer\r
1612 )\r
1613{\r
1614 EFI_PEI_BLOCK_IO_MEDIA MediaInfo;\r
1615 EFI_STATUS Status;\r
1616 UINTN Count;\r
1617 UINTN NumberOfBlocks;\r
1618 UINTN BlockSize;\r
1619 FDC_BLK_IO_DEV *FdcBlkIoDev;\r
1620 VOID *MemPage;\r
1621\r
1622 FdcBlkIoDev = NULL;\r
1623\r
1624 if (This == NULL) {\r
1625 return EFI_INVALID_PARAMETER;\r
1626 }\r
1627\r
1628 FdcBlkIoDev = PEI_RECOVERY_FDC_FROM_BLKIO_THIS (This);\r
1629\r
1630 if (Buffer == NULL) {\r
1631 return EFI_INVALID_PARAMETER;\r
1632 }\r
1633\r
1634 Status = FdcGetBlockDeviceMediaInfo (PeiServices, This, DeviceIndex, &MediaInfo);\r
1635 if (Status != EFI_SUCCESS) {\r
1636 return EFI_DEVICE_ERROR;\r
1637 }\r
1638\r
1639 if (!MediaInfo.MediaPresent) {\r
1640 return EFI_NO_MEDIA;\r
1641 }\r
1642\r
1643 BlockSize = MediaInfo.BlockSize;\r
1644\r
1645 //\r
1646 // If BufferSize cannot be divided by block size of FDC device,\r
1647 // return EFI_BAD_BUFFER_SIZE.\r
1648 //\r
1649 if (BufferSize % BlockSize != 0) {\r
1650 return EFI_BAD_BUFFER_SIZE;\r
1651 }\r
1652\r
1653 NumberOfBlocks = BufferSize / BlockSize;\r
1654\r
1655 if ((StartLBA + NumberOfBlocks - 1) > FdcBlkIoDev->DeviceInfo[DeviceIndex - 1].MediaInfo.LastBlock) {\r
1656 return EFI_INVALID_PARAMETER;\r
1657 }\r
1658\r
1659 MemPage = AllocatePages (EFI_SIZE_TO_PAGES (BufferSize));\r
1660 if ((MemPage == NULL) || ((UINTN) MemPage >= ISA_MAX_MEMORY_ADDRESS)) {\r
1661 //\r
1662 // If fail to allocate memory under ISA_MAX_MEMORY_ADDRESS, designate the address space for DMA\r
1663 //\r
1664 MemPage = (VOID *) ((UINTN) (UINT32) 0x0f00000);\r
1665 }\r
1666 Status = MotorOn (FdcBlkIoDev, &(FdcBlkIoDev->DeviceInfo[DeviceIndex - 1]));\r
1667 if (Status != EFI_SUCCESS) {\r
1668 return EFI_DEVICE_ERROR;\r
1669 }\r
1670\r
1671 Status = Setup (FdcBlkIoDev, FdcBlkIoDev->DeviceInfo[DeviceIndex - 1].DevPos);\r
1672 if (Status != EFI_SUCCESS) {\r
1673 MotorOff (FdcBlkIoDev, &(FdcBlkIoDev->DeviceInfo[DeviceIndex - 1]));\r
1674 return EFI_DEVICE_ERROR;\r
1675 }\r
1676 //\r
1677 // Read data in batches.\r
1678 // Blocks in the same cylinder are read out in a batch.\r
1679 //\r
1680 while ((Count = GetTransferBlockCount (\r
1681 &(FdcBlkIoDev->DeviceInfo[DeviceIndex - 1]),\r
1682 StartLBA,\r
1683 NumberOfBlocks\r
1684 )) != 0 && Status == EFI_SUCCESS) {\r
1685 Status = ReadDataSector (\r
1686 FdcBlkIoDev,\r
1687 &(FdcBlkIoDev->DeviceInfo[DeviceIndex - 1]),\r
1688 MemPage,\r
1689 StartLBA,\r
1690 Count\r
1691 );\r
1692 CopyMem (Buffer, MemPage, BlockSize * Count);\r
1693 StartLBA += Count;\r
1694 NumberOfBlocks -= Count;\r
1695 Buffer = (VOID *) ((UINTN) Buffer + Count * BlockSize);\r
1696 }\r
1697\r
1698 MotorOff (FdcBlkIoDev, &(FdcBlkIoDev->DeviceInfo[DeviceIndex - 1]));\r
1699\r
1700 switch (Status) {\r
1701 case EFI_SUCCESS:\r
1702 return EFI_SUCCESS;\r
1703\r
1704 default:\r
1705 FdcReset (FdcBlkIoDev, FdcBlkIoDev->DeviceInfo[DeviceIndex - 1].DevPos);\r
1706 return EFI_DEVICE_ERROR;\r
1707 }\r
1708}\r
1709\r
1710/**\r
1711 Initializes the floppy disk controller and installs FDC Block I/O PPI.\r
1712\r
1713 @param FileHandle Handle of the file being invoked.\r
1714 @param PeiServices Describes the list of possible PEI Services.\r
1715\r
1716 @retval EFI_SUCCESS Successfully initialized FDC and installed PPI.\r
1717 @retval EFI_NOT_FOUND Cannot find FDC device.\r
1718 @retval EFI_OUT_OF_RESOURCES Have no enough memory to create instance or descriptors.\r
1719 @retval Other Fail to install FDC Block I/O PPI.\r
1720\r
1721**/\r
1722EFI_STATUS\r
1723EFIAPI\r
1724FdcPeimEntry (\r
1725 IN EFI_PEI_FILE_HANDLE FileHandle,\r
1726 IN CONST EFI_PEI_SERVICES **PeiServices\r
1727 )\r
1728{\r
1729 EFI_STATUS Status;\r
1730 FDC_BLK_IO_DEV *FdcBlkIoDev;\r
1731 UINTN DeviceCount;\r
1732 UINT32 Index;\r
1733\r
1734 Status = PeiServicesRegisterForShadow (FileHandle);\r
1735 if (!EFI_ERROR (Status)) {\r
1736 return Status;\r
1737 }\r
1738\r
1739 //\r
1740 // Allocate memory for instance of FDC_BLK_IO_DEV and copy initial value\r
1741 // from template to it. \r
1742 //\r
1743 FdcBlkIoDev = AllocatePages (EFI_SIZE_TO_PAGES(sizeof (FDC_BLK_IO_DEV)));\r
1744 if (FdcBlkIoDev == NULL) {\r
1745 return EFI_OUT_OF_RESOURCES;\r
1746 }\r
1747 CopyMem (FdcBlkIoDev, &mBlockIoDevTemplate, sizeof (mBlockIoDevTemplate));\r
1748\r
1749 //\r
1750 // Initialize DMA controller to enable all channels.\r
1751 //\r
1752 for (Index = 0; Index < sizeof (mRegisterTable) / sizeof (PEI_DMA_TABLE); Index++) {\r
1753 IoWrite8 (mRegisterTable[Index].Register, mRegisterTable[Index].Value);\r
1754 }\r
1755 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, EFI_PERIPHERAL_REMOVABLE_MEDIA + EFI_P_PC_INIT);\r
1756\r
1757 //\r
1758 // Enumerate FDC devices.\r
1759 //\r
1760 DeviceCount = FdcEnumeration (FdcBlkIoDev);\r
1761 if (DeviceCount == 0) {\r
1762 return EFI_NOT_FOUND;\r
1763 }\r
1764\r
1765 FdcBlkIoDev->PpiDescriptor.Ppi = &FdcBlkIoDev->FdcBlkIo;\r
1766\r
1767 return PeiServicesInstallPpi (&FdcBlkIoDev->PpiDescriptor);\r
1768}\r