1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include <common.h>
29#include <config.h>
30#include <command.h>
31#include <image.h>
32
33
34#undef FDC_DEBUG
35
36#ifdef FDC_DEBUG
37#define PRINTF(fmt,args...) printf (fmt ,##args)
38#else
39#define PRINTF(fmt,args...)
40#endif
41
42#ifndef TRUE
43#define TRUE 1
44#endif
45#ifndef FALSE
46#define FALSE 0
47#endif
48
49
50
51
52
53typedef struct {
54 int flags;
55 unsigned long blnr;
56 uchar drive;
57 uchar cmdlen;
58 uchar cmd[16];
59 uchar dma;
60 uchar result[11];
61 uchar resultlen;
62} FDC_COMMAND_STRUCT;
63
64
65
66
67
68
69
70
71
72
73
74
75
76#define COMMAND 0
77#define DRIVE 1
78#define CONFIG0 1
79#define SPEC_HUTSRT 1
80#define TRACK 2
81#define CONFIG1 2
82#define SPEC_HLT 2
83#define HEAD 3
84#define CONFIG2 3
85#define SECTOR 4
86#define SECTOR_SIZE 5
87#define LAST_TRACK 6
88#define GAP 7
89#define DTL 8
90
91#define STATUS_0 0
92#define STATUS_PCN 1
93#define STATUS_1 1
94#define STATUS_2 2
95#define STATUS_TRACK 3
96#define STATUS_HEAD 4
97#define STATUS_SECT 5
98#define STATUS_SECT_SIZE 6
99
100
101
102#define FDC_BASE 0x3F0
103#define FDC_SRA FDC_BASE + 0
104#define FDC_SRB FDC_BASE + 1
105#define FDC_DOR FDC_BASE + 2
106#define FDC_TDR FDC_BASE + 3
107#define FDC_DSR FDC_BASE + 4
108#define FDC_MSR FDC_BASE + 4
109#define FDC_FIFO FDC_BASE + 5
110#define FDC_DIR FDC_BASE + 6
111#define FDC_CCR FDC_BASE + 7
112
113#define FDC_CMD_SENSE_INT 0x08
114#define FDC_CMD_CONFIGURE 0x13
115#define FDC_CMD_SPECIFY 0x03
116#define FDC_CMD_RECALIBRATE 0x07
117#define FDC_CMD_READ 0x06
118#define FDC_CMD_READ_TRACK 0x02
119#define FDC_CMD_READ_ID 0x0A
120#define FDC_CMD_DUMP_REG 0x0E
121#define FDC_CMD_SEEK 0x0F
122
123#define FDC_CMD_SENSE_INT_LEN 0x01
124#define FDC_CMD_CONFIGURE_LEN 0x04
125#define FDC_CMD_SPECIFY_LEN 0x03
126#define FDC_CMD_RECALIBRATE_LEN 0x02
127#define FDC_CMD_READ_LEN 0x09
128#define FDC_CMD_READ_TRACK_LEN 0x09
129#define FDC_CMD_READ_ID_LEN 0x02
130#define FDC_CMD_DUMP_REG_LEN 0x01
131#define FDC_CMD_SEEK_LEN 0x03
132
133#define FDC_FIFO_THR 0x0C
134#define FDC_FIFO_DIS 0x00
135#define FDC_IMPLIED_SEEK 0x01
136#define FDC_POLL_DIS 0x00
137#define FDC_PRE_TRK 0x00
138#define FDC_CONFIGURE FDC_FIFO_THR | (FDC_POLL_DIS<<4) | (FDC_FIFO_DIS<<5) | (FDC_IMPLIED_SEEK << 6)
139#define FDC_MFM_MODE 0x01
140#define FDC_SKIP_MODE 0x00
141
142#define FDC_TIME_OUT 100000
143#define FDC_RW_RETRIES 3
144#define FDC_CAL_RETRIES 3
145
146
147
148typedef struct {
149 unsigned int size;
150 unsigned int sect;
151 unsigned int head;
152 unsigned int track;
153 unsigned int stretch;
154 unsigned char gap;
155 unsigned char rate;
156 unsigned char spec1;
157 unsigned char fmt_gap;
158 unsigned char hlt;
159 unsigned char sect_code;
160 const char * name;
161} FD_GEO_STRUCT;
162
163
164
165const static FD_GEO_STRUCT floppy_type[2] = {
166 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,16,2,"H1440" },
167 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00, 0,0,NULL },
168};
169
170static FDC_COMMAND_STRUCT cmd;
171
172
173#ifndef CONFIG_SYS_FDC_DRIVE_NUMBER
174#define CONFIG_SYS_FDC_DRIVE_NUMBER 0
175#endif
176
177
178#ifndef CONFIG_SYS_ISA_IO_STRIDE
179#define CONFIG_SYS_ISA_IO_STRIDE 1
180#endif
181
182#ifndef CONFIG_SYS_ISA_IO_OFFSET
183#define CONFIG_SYS_ISA_IO_OFFSET 0
184#endif
185
186
187#ifdef CONFIG_AMIGAONEG3SE
188unsigned char INT6_Status;
189
190void fdc_interrupt(void)
191{
192 INT6_Status = 0x80;
193}
194
195
196int wait_for_fdc_int(void)
197{
198 unsigned long timeout;
199 timeout = FDC_TIME_OUT;
200 while(((volatile)INT6_Status & 0x80) == 0) {
201 timeout--;
202 udelay(10);
203 if(timeout == 0)
204 return FALSE;
205 }
206 INT6_Status = 0;
207 return TRUE;
208}
209#endif
210
211
212
213unsigned char read_fdc_reg(unsigned int addr)
214{
215 volatile unsigned char *val =
216 (volatile unsigned char *)(CONFIG_SYS_ISA_IO_BASE_ADDRESS +
217 (addr * CONFIG_SYS_ISA_IO_STRIDE) +
218 CONFIG_SYS_ISA_IO_OFFSET);
219
220 return val [0];
221}
222
223
224void write_fdc_reg(unsigned int addr, unsigned char val)
225{
226 volatile unsigned char *tmp =
227 (volatile unsigned char *)(CONFIG_SYS_ISA_IO_BASE_ADDRESS +
228 (addr * CONFIG_SYS_ISA_IO_STRIDE) +
229 CONFIG_SYS_ISA_IO_OFFSET);
230 tmp[0]=val;
231}
232
233#ifndef CONFIG_AMIGAONEG3SE
234
235int wait_for_fdc_int(void)
236{
237 unsigned long timeout;
238 timeout = FDC_TIME_OUT;
239 while((read_fdc_reg(FDC_SRA)&0x80)==0) {
240 timeout--;
241 udelay(10);
242 if(timeout==0)
243 return FALSE;
244 }
245 return TRUE;
246}
247
248#endif
249
250
251
252int read_fdc_byte(void)
253{
254 unsigned long timeout;
255 timeout = FDC_TIME_OUT;
256 while((read_fdc_reg(FDC_MSR)&0xC0)!=0xC0) {
257
258 udelay(10);
259 timeout--;
260 if(timeout==0)
261 return -1;
262 }
263 return read_fdc_reg(FDC_FIFO);
264}
265
266
267
268int fdc_need_more_output(void)
269{
270 unsigned char c;
271 while((read_fdc_reg(FDC_MSR)&0xC0)==0xC0) {
272 c=(unsigned char)read_fdc_byte();
273 printf("Error: more output: %x\n",c);
274 }
275 return TRUE;
276}
277
278
279
280
281int write_fdc_byte(unsigned char val)
282{
283 unsigned long timeout;
284 timeout = FDC_TIME_OUT;
285 while((read_fdc_reg(FDC_MSR)&0xC0)!=0x80) {
286
287 timeout--;
288 udelay(10);
289 fdc_need_more_output();
290 if(timeout==0)
291 return FALSE;
292 }
293 write_fdc_reg(FDC_FIFO,val);
294 return TRUE;
295}
296
297
298
299
300
301int fdc_issue_cmd(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
302{
303 int i;
304 unsigned long head,track,sect,timeout;
305 track = pCMD->blnr / (pFG->sect * pFG->head);
306 sect = pCMD->blnr % (pFG->sect * pFG->head);
307 head = sect / pFG->sect;
308 sect = sect % pFG->sect;
309 sect++;
310 PRINTF("Cmd 0x%02x Track %ld, Head %ld, Sector %ld, Drive %d (blnr %ld)\n",
311 pCMD->cmd[0],track,head,sect,pCMD->drive,pCMD->blnr);
312
313 if(head|=0) {
314 pCMD->cmd[DRIVE]=pCMD->drive | 0x04;
315 pCMD->cmd[HEAD]=(unsigned char) head;
316 }
317 else {
318 pCMD->cmd[DRIVE]=pCMD->drive;
319 pCMD->cmd[HEAD]=(unsigned char) head;
320 }
321 pCMD->cmd[TRACK]=(unsigned char) track;
322 switch (pCMD->cmd[COMMAND]) {
323 case FDC_CMD_READ:
324 pCMD->cmd[SECTOR]=(unsigned char) sect;
325 pCMD->cmd[SECTOR_SIZE]=pFG->sect_code;
326 pCMD->cmd[LAST_TRACK]=pFG->sect;
327 pCMD->cmd[GAP]=pFG->gap;
328 pCMD->cmd[DTL]=0xFF;
329 pCMD->cmdlen=FDC_CMD_READ_LEN;
330 pCMD->cmd[COMMAND]|=(FDC_MFM_MODE<<6);
331 pCMD->cmd[COMMAND]|=(FDC_SKIP_MODE<<5);
332 pCMD->resultlen=0;
333 break;
334 case FDC_CMD_SEEK:
335 pCMD->cmdlen=FDC_CMD_SEEK_LEN;
336 pCMD->resultlen=0;
337 break;
338 case FDC_CMD_CONFIGURE:
339 pCMD->cmd[CONFIG0]=0;
340 pCMD->cmd[CONFIG1]=FDC_CONFIGURE;
341 pCMD->cmd[CONFIG2]=FDC_PRE_TRK;
342 pCMD->cmdlen=FDC_CMD_CONFIGURE_LEN;
343 pCMD->resultlen=0;
344 break;
345 case FDC_CMD_SPECIFY:
346 pCMD->cmd[SPEC_HUTSRT]=pFG->spec1;
347 pCMD->cmd[SPEC_HLT]=(pFG->hlt)<<1;
348 if(pCMD->dma==0)
349 pCMD->cmd[SPEC_HLT]|=0x1;
350 pCMD->cmdlen=FDC_CMD_SPECIFY_LEN;
351 pCMD->resultlen=0;
352 break;
353 case FDC_CMD_DUMP_REG:
354 pCMD->cmdlen=FDC_CMD_DUMP_REG_LEN;
355 pCMD->resultlen=10;
356 break;
357 case FDC_CMD_READ_ID:
358 pCMD->cmd[COMMAND]|=(FDC_MFM_MODE<<6);
359 pCMD->cmdlen=FDC_CMD_READ_ID_LEN;
360 pCMD->resultlen=7;
361 break;
362 case FDC_CMD_RECALIBRATE:
363 pCMD->cmd[DRIVE]&=0x03;
364 pCMD->cmdlen=FDC_CMD_RECALIBRATE_LEN;
365 pCMD->resultlen=0;
366 break;
367 break;
368 case FDC_CMD_SENSE_INT:
369 pCMD->cmdlen=FDC_CMD_SENSE_INT_LEN;
370 pCMD->resultlen=2;
371 break;
372 }
373 for(i=0;i<pCMD->cmdlen;i++) {
374
375 if(write_fdc_byte(pCMD->cmd[i])==FALSE) {
376 PRINTF("Error: timeout while issue cmd%d\n",i);
377 return FALSE;
378 }
379 }
380 timeout=FDC_TIME_OUT;
381 for(i=0;i<pCMD->resultlen;i++) {
382 while((read_fdc_reg(FDC_MSR)&0xC0)!=0xC0) {
383 timeout--;
384 if(timeout==0) {
385 PRINTF(" timeout while reading result%d MSR=0x%02X\n",i,read_fdc_reg(FDC_MSR));
386 return FALSE;
387 }
388 }
389 pCMD->result[i]=(unsigned char)read_fdc_byte();
390 }
391 return TRUE;
392}
393
394
395
396void select_fdc_drive(FDC_COMMAND_STRUCT *pCMD)
397{
398 unsigned char val;
399
400 val=(1<<(4+pCMD->drive))|pCMD->drive|0xC;
401 if((read_fdc_reg(FDC_DOR)&val)!=val) {
402 write_fdc_reg(FDC_DOR,val);
403 for(val=0;val<255;val++)
404 udelay(500);
405 }
406}
407
408
409void stop_fdc_drive(FDC_COMMAND_STRUCT *pCMD)
410{
411 unsigned char val;
412
413 val=(1<<(4+pCMD->drive))|pCMD->drive;
414 write_fdc_reg(FDC_DOR,(read_fdc_reg(FDC_DOR)&~val));
415}
416
417
418
419int fdc_recalibrate(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
420{
421 pCMD->cmd[COMMAND]=FDC_CMD_RECALIBRATE;
422 if(fdc_issue_cmd(pCMD,pFG)==FALSE)
423 return FALSE;
424 while(wait_for_fdc_int()!=TRUE);
425 pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
426 return(fdc_issue_cmd(pCMD,pFG));
427}
428
429
430
431int fdc_seek(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
432{
433 pCMD->cmd[COMMAND]=FDC_CMD_SEEK;
434 if(fdc_issue_cmd(pCMD,pFG)==FALSE)
435 return FALSE;
436 while(wait_for_fdc_int()!=TRUE);
437 pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
438 return(fdc_issue_cmd(pCMD,pFG));
439}
440
441#ifndef CONFIG_AMIGAONEG3SE
442
443
444int fdc_terminate(FDC_COMMAND_STRUCT *pCMD)
445{
446 int i;
447 for(i=0;i<100;i++)
448 udelay(500);
449 while((read_fdc_reg(FDC_SRA)&0x80)==0x00);
450 for(i=0;i<7;i++) {
451 pCMD->result[i]=(unsigned char)read_fdc_byte();
452 }
453 return TRUE;
454}
455#endif
456#ifdef CONFIG_AMIGAONEG3SE
457int fdc_terminate(FDC_COMMAND_STRUCT *pCMD)
458{
459 int i;
460 for(i=0;i<100;i++)
461 udelay(500);
462 while((INT6_Status&0x80)==0x00);
463 for(i=0;i<7;i++) {
464 pCMD->result[i]=(unsigned char)read_fdc_byte();
465 }
466 INT6_Status = 0;
467 return TRUE;
468}
469
470#endif
471
472#ifdef CONFIG_AMIGAONEG3SE
473#define disable_interrupts() 0
474#define enable_interrupts() (void)0
475#endif
476
477
478int fdc_read_data(unsigned char *buffer, unsigned long blocks,FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
479{
480
481 unsigned long len,lastblk,readblk,i,timeout,ii,offset;
482 unsigned char pcn,c,retriesrw,retriescal;
483 unsigned char *bufferw;
484 int sect_size;
485 int flags;
486
487 flags=disable_interrupts();
488 select_fdc_drive(pCMD);
489 sect_size=0x080<<pFG->sect_code;
490 retriesrw=0;
491 retriescal=0;
492 offset=0;
493 if(fdc_seek(pCMD,pFG)==FALSE) {
494 stop_fdc_drive(pCMD);
495 enable_interrupts();
496 return FALSE;
497 }
498 if((pCMD->result[STATUS_0]&0x20)!=0x20) {
499 printf("Seek error Status: %02X\n",pCMD->result[STATUS_0]);
500 stop_fdc_drive(pCMD);
501 enable_interrupts();
502 return FALSE;
503 }
504 pcn=pCMD->result[STATUS_PCN];
505
506 lastblk=pCMD->blnr + blocks;
507
508 readblk=pFG->sect-(pCMD->blnr%pFG->sect);
509 PRINTF("1st nr of block possible read %ld start %ld\n",readblk,pCMD->blnr);
510 if(readblk>blocks)
511 readblk=blocks;
512 PRINTF("we read %ld blocks start %ld\n",readblk,pCMD->blnr);
513 bufferw = &buffer[0];
514 do {
515retryrw:
516 len=sect_size * readblk;
517 pCMD->cmd[COMMAND]=FDC_CMD_READ;
518 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
519 stop_fdc_drive(pCMD);
520 enable_interrupts();
521 return FALSE;
522 }
523 for (i=0;i<len;i++) {
524 timeout=FDC_TIME_OUT;
525 do {
526 c=read_fdc_reg(FDC_MSR);
527 if((c&0xC0)==0xC0) {
528 bufferw[i]=read_fdc_reg(FDC_FIFO);
529 break;
530 }
531 if((c&0xC0)==0x80) {
532 PRINTF("Transfer error transfered: at %ld, MSR=%02X\n",i,c);
533 if(i>6) {
534 for(ii=0;ii<7;ii++) {
535 pCMD->result[ii]=bufferw[(i-7+ii)];
536 }
537 }
538 if(retriesrw++>FDC_RW_RETRIES) {
539 if (retriescal++>FDC_CAL_RETRIES) {
540 stop_fdc_drive(pCMD);
541 enable_interrupts();
542 return FALSE;
543 }
544 else {
545 PRINTF(" trying to recalibrate Try %d\n",retriescal);
546 if(fdc_recalibrate(pCMD,pFG)==FALSE) {
547 stop_fdc_drive(pCMD);
548 enable_interrupts();
549 return FALSE;
550 }
551 retriesrw=0;
552 goto retrycal;
553 }
554 }
555 else {
556 PRINTF("Read retry %d\n",retriesrw);
557 goto retryrw;
558 }
559 }
560 timeout--;
561 }while(TRUE);
562 }
563
564
565 fdc_terminate(pCMD);
566 offset+=(sect_size*readblk);
567 bufferw = &buffer[offset];
568 pCMD->blnr+=readblk;
569 blocks-=readblk;
570 if(blocks==0)
571 break;
572
573
574 readblk=pFG->sect;
575 if(readblk>blocks)
576 readblk=blocks;
577retrycal:
578
579 if(fdc_seek(pCMD,pFG)==FALSE) {
580 stop_fdc_drive(pCMD);
581 enable_interrupts();
582 return FALSE;
583 }
584 if((pCMD->result[STATUS_0]&0x20)!=0x20) {
585 PRINTF("Seek error Status: %02X\n",pCMD->result[STATUS_0]);
586 stop_fdc_drive(pCMD);
587 return FALSE;
588 }
589 pcn=pCMD->result[STATUS_PCN];
590 }while(TRUE);
591 stop_fdc_drive(pCMD);
592 enable_interrupts();
593 return TRUE;
594}
595
596#ifdef CONFIG_AMIGAONEG3SE
597#undef disable_interrupts()
598#undef enable_interrupts()
599#endif
600
601
602int fdc_check_drive(FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
603{
604 int i,drives,state;
605
606
607 state=0;
608 for(drives=0;drives<4;drives++) {
609 pCMD->drive=drives;
610 select_fdc_drive(pCMD);
611 pCMD->blnr=0;
612 if(fdc_recalibrate(pCMD,pFG)==FALSE)
613 continue;
614 if((pCMD->result[STATUS_0]&0x10)==0x10)
615 continue;
616
617 state|=(1<<drives);
618 pCMD->blnr=pFG->size;
619 if(fdc_seek(pCMD,pFG)==FALSE)
620 continue;
621 pCMD->blnr=0;
622 if(fdc_recalibrate(pCMD,pFG)==FALSE)
623 continue;
624 pCMD->cmd[COMMAND]=FDC_CMD_READ_ID;
625 if(fdc_issue_cmd(pCMD,pFG)==FALSE)
626 continue;
627 state|=(0x10<<drives);
628 }
629 stop_fdc_drive(pCMD);
630 for(i=0;i<4;i++) {
631 PRINTF("Floppy Drive %d %sconnected %sDisk inserted %s\n",i,
632 ((state&(1<<i))==(1<<i)) ? "":"not ",
633 ((state&(0x10<<i))==(0x10<<i)) ? "":"no ",
634 ((state&(0x10<<i))==(0x10<<i)) ? pFG->name : "");
635 }
636 pCMD->flags=state;
637 return TRUE;
638}
639
640
641
642
643
644
645
646int fdc_setup(int drive, FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
647{
648 int i;
649
650#ifdef CONFIG_AMIGAONEG3SE
651 irq_install_handler(6, (interrupt_handler_t *)fdc_interrupt, NULL);
652 i8259_unmask_irq(6);
653#endif
654
655#ifdef CONFIG_SYS_FDC_HW_INIT
656 fdc_hw_init ();
657#endif
658
659 write_fdc_reg(FDC_DOR,0x00);
660 for(i=0; i<255; i++)
661 udelay(500);
662
663 pCMD->drive=drive;
664 select_fdc_drive(pCMD);
665
666 write_fdc_reg(FDC_CCR,pFG->rate);
667
668 write_fdc_reg(FDC_DSR,pFG->rate);
669 if(wait_for_fdc_int()==FALSE) {
670 PRINTF("Time Out after writing CCR\n");
671 return FALSE;
672 }
673
674
675 pCMD->dma=0;
676 for(i=0;i<4;i++) {
677
678 pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
679 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
680 PRINTF("Sense Interrupt for drive %d failed\n",i);
681 }
682 }
683
684 pCMD->drive=drive;
685 select_fdc_drive(pCMD);
686 pCMD->cmd[COMMAND]=FDC_CMD_CONFIGURE;
687 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
688 PRINTF(" configure timeout\n");
689 stop_fdc_drive(pCMD);
690 return FALSE;
691 }
692
693 pCMD->cmd[COMMAND]=FDC_CMD_SPECIFY;
694 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
695 PRINTF(" specify timeout\n");
696 stop_fdc_drive(pCMD);
697 return FALSE;
698
699 }
700
701
702
703
704 return TRUE;
705}
706
707#if defined(CONFIG_CMD_FDOS)
708
709
710
711
712
713
714
715
716int fdc_fdos_init (int drive)
717{
718 FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
719 FDC_COMMAND_STRUCT *pCMD = &cmd;
720
721
722 if(fdc_setup(drive,pCMD,pFG)==FALSE) {
723 printf("\n** Error in setup FDC **\n");
724 return FALSE;
725 }
726 if(fdc_check_drive(pCMD,pFG)==FALSE) {
727 printf("\n** Error in check_drives **\n");
728 return FALSE;
729 }
730 if((pCMD->flags&(1<<drive))==0) {
731
732 printf("\n** Drive %d not available **\n",drive);
733 return FALSE;
734 }
735 if((pCMD->flags&(0x10<<drive))==0) {
736
737 printf("\n** No disk inserted in drive %d **\n",drive);
738 return FALSE;
739 }
740
741 pCMD->drive=drive;
742
743
744 pCMD->blnr=0;
745 return TRUE;
746}
747
748
749
750
751int fdc_fdos_seek (int where)
752{
753 FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
754 FDC_COMMAND_STRUCT *pCMD = &cmd;
755
756 pCMD -> blnr = where ;
757 return (fdc_seek (pCMD, pFG));
758}
759
760
761
762
763int fdc_fdos_read (void *buffer, int len)
764{
765 FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
766 FDC_COMMAND_STRUCT *pCMD = &cmd;
767
768 return (fdc_read_data (buffer, len, pCMD, pFG));
769}
770#endif
771
772#if defined(CONFIG_CMD_FDC)
773
774
775
776int do_fdcboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
777{
778 FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
779 FDC_COMMAND_STRUCT *pCMD = &cmd;
780 unsigned long addr,imsize;
781 image_header_t *hdr;
782 unsigned char boot_drive;
783 int i,nrofblk;
784 char *ep;
785 int rcode = 0;
786#if defined(CONFIG_FIT)
787 const void *fit_hdr = NULL;
788#endif
789
790 switch (argc) {
791 case 1:
792 addr = CONFIG_SYS_LOAD_ADDR;
793 boot_drive=CONFIG_SYS_FDC_DRIVE_NUMBER;
794 break;
795 case 2:
796 addr = simple_strtoul(argv[1], NULL, 16);
797 boot_drive=CONFIG_SYS_FDC_DRIVE_NUMBER;
798 break;
799 case 3:
800 addr = simple_strtoul(argv[1], NULL, 16);
801 boot_drive=simple_strtoul(argv[2], NULL, 10);
802 break;
803 default:
804 cmd_usage(cmdtp);
805 return 1;
806 }
807
808 if(fdc_setup(boot_drive,pCMD,pFG)==FALSE) {
809 printf("\n** Error in setup FDC **\n");
810 return 1;
811 }
812 if(fdc_check_drive(pCMD,pFG)==FALSE) {
813 printf("\n** Error in check_drives **\n");
814 return 1;
815 }
816 if((pCMD->flags&(1<<boot_drive))==0) {
817
818 printf("\n** Drive %d not availabe **\n",boot_drive);
819 return 1;
820 }
821 if((pCMD->flags&(0x10<<boot_drive))==0) {
822
823 printf("\n** No disk inserted in drive %d **\n",boot_drive);
824 return 1;
825 }
826
827 pCMD->drive=boot_drive;
828
829 pCMD->blnr=0;
830 if(fdc_read_data((unsigned char *)addr,1,pCMD,pFG)==FALSE) {
831 printf("\nRead error:");
832 for(i=0;i<7;i++)
833 printf("result%d: 0x%02X\n",i,pCMD->result[i]);
834 return 1;
835 }
836
837 switch (genimg_get_format ((void *)addr)) {
838 case IMAGE_FORMAT_LEGACY:
839 hdr = (image_header_t *)addr;
840 image_print_contents (hdr);
841
842 imsize = image_get_image_size (hdr);
843 break;
844#if defined(CONFIG_FIT)
845 case IMAGE_FORMAT_FIT:
846 fit_hdr = (const void *)addr;
847 puts ("Fit image detected...\n");
848
849 imsize = fit_get_size (fit_hdr);
850 break;
851#endif
852 default:
853 puts ("** Unknown image type\n");
854 return 1;
855 }
856
857 nrofblk=imsize/512;
858 if((imsize%512)>0)
859 nrofblk++;
860 printf("Loading %ld Bytes (%d blocks) at 0x%08lx..\n",imsize,nrofblk,addr);
861 pCMD->blnr=0;
862 if(fdc_read_data((unsigned char *)addr,nrofblk,pCMD,pFG)==FALSE) {
863
864 printf("\nRead error:");
865 for(i=0;i<7;i++)
866 printf("result%d: 0x%02X\n",i,pCMD->result[i]);
867 return 1;
868 }
869 printf("OK %ld Bytes loaded.\n",imsize);
870
871 flush_cache (addr, imsize);
872
873#if defined(CONFIG_FIT)
874
875 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
876 if (!fit_check_format (fit_hdr)) {
877 puts ("** Bad FIT image format\n");
878 return 1;
879 }
880 fit_print_contents (fit_hdr);
881 }
882#endif
883
884
885 load_addr = addr;
886
887
888 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
889 char *local_args[2];
890 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
891
892 local_args[0] = argv[0];
893 local_args[1] = NULL;
894
895 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
896
897 do_bootm (cmdtp, 0, 1, local_args);
898 rcode ++;
899 }
900 return rcode;
901}
902
903U_BOOT_CMD(
904 fdcboot, 3, 1, do_fdcboot,
905 "boot from floppy device",
906 "loadAddr drive\n"
907);
908#endif
909