]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_fdc.c
* Patch by Daniel Engström, 13 Nov 2002:
[karo-tx-uboot.git] / common / cmd_fdc.c
1 /*
2  * (C) Copyright 2001
3  * Denis Peter, MPL AG, d.peter@mpl.ch.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  *
23  */
24 /*
25  * Floppy Disk support
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 #if (CONFIG_COMMANDS & CFG_CMD_DATE)
51 #include <rtc.h>
52 #endif
53
54 #if ((CONFIG_COMMANDS & CFG_CMD_FDC) || (CONFIG_COMMANDS & CFG_CMD_FDOS))
55
56
57 typedef struct {
58         int                                             flags;          /* connected drives ect */
59         unsigned long blnr;                     /* Logical block nr */
60         uchar                                   drive;          /* drive no */
61         uchar                                   cmdlen;         /* cmd length */
62         uchar                                   cmd[16];        /* cmd desc */
63         uchar                                   dma;                    /* if > 0 dma enabled */
64         uchar                                   result[11];/* status information */
65         uchar                                   resultlen; /* lenght of result */
66 } FDC_COMMAND_STRUCT;
67 /* flags: only the lower 8bit used:
68  * bit 0 if set drive 0 is present
69  * bit 1 if set drive 1 is present
70  * bit 2 if set drive 2 is present
71  * bit 3 if set drive 3 is present
72  * bit 4 if set disk in drive 0 is inserted
73  * bit 5 if set disk in drive 1 is inserted
74  * bit 6 if set disk in drive 2 is inserted
75  * bit 7 if set disk in drive 4 is inserted
76  */
77
78
79 /* cmd indexes */
80 #define COMMAND                 0
81 #define DRIVE                   1
82 #define CONFIG0                 1
83 #define SPEC_HUTSRT     1
84 #define TRACK                   2
85 #define CONFIG1                 2
86 #define SPEC_HLT                2
87 #define HEAD                            3
88 #define CONFIG2                 3
89 #define SECTOR                  4
90 #define SECTOR_SIZE     5
91 #define LAST_TRACK      6
92 #define GAP                                     7
93 #define DTL                                     8
94 /* result indexes */
95 #define STATUS_0                                                0
96 #define STATUS_PCN                                      1
97 #define STATUS_1                                                1
98 #define STATUS_2                                                2
99 #define STATUS_TRACK                            3
100 #define STATUS_HEAD                                     4
101 #define STATUS_SECT                                     5
102 #define STATUS_SECT_SIZE                6
103
104
105 /* Register addresses */
106 #define FDC_BASE        0x3F0
107 #define FDC_SRA         FDC_BASE + 0    /* Status Register A */
108 #define FDC_SRB         FDC_BASE + 1    /* Status Register B */
109 #define FDC_DOR         FDC_BASE + 2    /* Digital Output Register */
110 #define FDC_TDR         FDC_BASE + 3    /* Tape Drive Register */
111 #define FDC_DSR         FDC_BASE + 4    /* Data rate Register */
112 #define FDC_MSR         FDC_BASE + 4    /* Main Status Register */
113 #define FDC_FIFO        FDC_BASE + 5    /* FIFO */
114 #define FDC_DIR         FDC_BASE + 6    /* Digital Input Register */
115 #define FDC_CCR         FDC_BASE + 7    /* Configuration Control */
116 /* Commands */
117 #define FDC_CMD_SENSE_INT               0x08
118 #define FDC_CMD_CONFIGURE               0x13
119 #define FDC_CMD_SPECIFY                         0x03
120 #define FDC_CMD_RECALIBRATE     0x07
121 #define FDC_CMD_READ                                    0x06
122 #define FDC_CMD_READ_TRACK              0x02
123 #define FDC_CMD_READ_ID                         0x0A
124 #define FDC_CMD_DUMP_REG                        0x0E
125 #define FDC_CMD_SEEK                                    0x0F
126
127 #define FDC_CMD_SENSE_INT_LEN           0x01
128 #define FDC_CMD_CONFIGURE_LEN           0x04
129 #define FDC_CMD_SPECIFY_LEN                             0x03
130 #define FDC_CMD_RECALIBRATE_LEN         0x02
131 #define FDC_CMD_READ_LEN                                        0x09
132 #define FDC_CMD_READ_TRACK_LEN          0x09
133 #define FDC_CMD_READ_ID_LEN                             0x02
134 #define FDC_CMD_DUMP_REG_LEN                    0x01
135 #define FDC_CMD_SEEK_LEN                                        0x03
136
137 #define FDC_FIFO_THR                    0x0C
138 #define FDC_FIFO_DIS                    0x00
139 #define FDC_IMPLIED_SEEK        0x01
140 #define FDC_POLL_DIS                    0x00
141 #define FDC_PRE_TRK                             0x00
142 #define FDC_CONFIGURE                   FDC_FIFO_THR | (FDC_POLL_DIS<<4) | (FDC_FIFO_DIS<<5) | (FDC_IMPLIED_SEEK << 6)
143 #define FDC_MFM_MODE                    0x01 /* MFM enable */
144 #define FDC_SKIP_MODE                   0x00 /* skip enable */
145
146 #define FDC_TIME_OUT 100000 /* time out */
147 #define FDC_RW_RETRIES          3 /* read write retries */
148 #define FDC_CAL_RETRIES         3 /* calibration and seek retries */
149
150
151 /* Disk structure */
152 typedef struct  {
153         unsigned int size;                      /* nr of sectors total */
154         unsigned int sect;                      /* sectors per track */
155         unsigned int head;                      /* nr of heads */
156         unsigned int track;                     /* nr of tracks */
157         unsigned int stretch;           /* !=0 means double track steps */
158         unsigned char   gap;                    /* gap1 size */
159         unsigned char   rate;                   /* data rate. |= 0x40 for perpendicular */
160         unsigned char   spec1;          /* stepping rate, head unload time */
161         unsigned char   fmt_gap;        /* gap2 size */
162         unsigned char hlt;                      /* head load time */
163         unsigned char sect_code; /* Sector Size code */
164         const char      * name;                 /* used only for predefined formats */
165 } FD_GEO_STRUCT;
166
167
168 /* supported Floppy types (currently only one) */
169 const static FD_GEO_STRUCT floppy_type[2] = {
170         { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,16,2,"H1440" },    /*  7 1.44MB 3.5"   */
171         {    0, 0,0, 0,0,0x00,0x00,0x00,0x00, 0,0,NULL    },    /*  end of table    */
172 };
173
174 static FDC_COMMAND_STRUCT cmd; /* global command struct */
175
176 /* If the boot drive number is undefined, we assume it's drive 0             */
177 #ifndef CFG_FDC_DRIVE_NUMBER
178 #define CFG_FDC_DRIVE_NUMBER 0
179 #endif
180
181 /* Hardware access */
182 #ifndef CFG_ISA_IO_STRIDE
183 #define CFG_ISA_IO_STRIDE 1
184 #endif
185
186 #ifndef CFG_ISA_IO_OFFSET
187 #define CFG_ISA_IO_OFFSET 0
188 #endif
189
190
191 /* Supporting Functions */
192 /* reads a Register of the FDC */
193 unsigned char read_fdc_reg(unsigned int addr)
194 {
195         volatile unsigned char *val =
196                 (volatile unsigned char *)(CFG_ISA_IO_BASE_ADDRESS +
197                                            (addr * CFG_ISA_IO_STRIDE) +
198                                            CFG_ISA_IO_OFFSET);
199         
200         return val [0];
201 }
202
203 /* writes a Register of the FDC */
204 void write_fdc_reg(unsigned int addr, unsigned char val)
205 {
206         volatile unsigned char *tmp =
207                 (volatile unsigned char *)(CFG_ISA_IO_BASE_ADDRESS +
208                                            (addr * CFG_ISA_IO_STRIDE) +
209                                            CFG_ISA_IO_OFFSET);
210         tmp[0]=val;
211 }
212
213 /* waits for an interrupt (polling) */
214 int wait_for_fdc_int(void)
215 {
216         unsigned long timeout;
217         timeout = FDC_TIME_OUT;
218         while((read_fdc_reg(FDC_SRA)&0x80)==0) {
219                 timeout--;
220                 udelay(10);
221                 if(timeout==0) /* timeout occured */
222                         return FALSE;
223         }
224         return TRUE;
225 }
226
227
228 /* reads a byte from the FIFO of the FDC and checks direction and RQM bit
229    of the MSR. returns -1 if timeout, or byte if ok */
230 int read_fdc_byte(void)
231 {
232         unsigned long timeout;
233         timeout = FDC_TIME_OUT;
234         while((read_fdc_reg(FDC_MSR)&0xC0)!=0xC0) {
235                 /* direction out and ready */
236                 udelay(10);
237                 timeout--;
238                 if(timeout==0) /* timeout occured */
239                         return -1;
240         }
241         return read_fdc_reg(FDC_FIFO);
242 }
243
244 /* if the direction of the FIFO is wrong, this routine is used to
245    empty the FIFO. Should _not_ be used */
246 int fdc_need_more_output(void)
247 {
248         unsigned char c;
249         while((read_fdc_reg(FDC_MSR)&0xC0)==0xC0)       {
250                         c=(unsigned char)read_fdc_byte();
251                         printf("Error: more output: %x\n",c);
252         }
253         return TRUE;
254 }
255
256
257 /* writes a byte to the FIFO of the FDC and checks direction and RQM bit
258    of the MSR */
259 int write_fdc_byte(unsigned char val)
260 {
261         unsigned long timeout;
262         timeout = FDC_TIME_OUT;
263         while((read_fdc_reg(FDC_MSR)&0xC0)!=0x80) {
264                 /* direction in and ready for byte */
265                 timeout--;
266                 udelay(10);
267                 fdc_need_more_output();
268                 if(timeout==0) /* timeout occured */
269                         return FALSE;
270         }
271         write_fdc_reg(FDC_FIFO,val);
272         return TRUE;
273 }
274
275 /* sets up all FDC commands and issues it to the FDC. If
276    the command causes direct results (no Execution Phase)
277    the result is be read as well. */
278
279 int fdc_issue_cmd(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
280 {
281         int i;
282         unsigned long head,track,sect,timeout;
283         track = pCMD->blnr / (pFG->sect * pFG->head); /* track nr */
284         sect =  pCMD->blnr % (pFG->sect * pFG->head); /* remaining blocks */
285         head = sect / pFG->sect; /* head nr */
286         sect =  sect % pFG->sect; /* remaining blocks */
287         sect++; /* sectors are 1 based */
288         PRINTF("Cmd 0x%02x Track %ld, Head %ld, Sector %ld, Drive %d (blnr %ld)\n",
289                 pCMD->cmd[0],track,head,sect,pCMD->drive,pCMD->blnr);
290
291         if(head|=0) { /* max heads = 2 */
292                 pCMD->cmd[DRIVE]=pCMD->drive | 0x04; /* head 1 */
293                 pCMD->cmd[HEAD]=(unsigned char) head; /* head register */
294         }
295         else {
296                 pCMD->cmd[DRIVE]=pCMD->drive; /* head 0 */
297                 pCMD->cmd[HEAD]=(unsigned char) head; /* head register */
298         }
299         pCMD->cmd[TRACK]=(unsigned char) track; /* track */
300         switch (pCMD->cmd[COMMAND]) {
301                 case FDC_CMD_READ:
302                         pCMD->cmd[SECTOR]=(unsigned char) sect; /* sector */
303                         pCMD->cmd[SECTOR_SIZE]=pFG->sect_code; /* sector size code */
304                         pCMD->cmd[LAST_TRACK]=pFG->sect; /* End of track */
305                         pCMD->cmd[GAP]=pFG->gap; /* gap */
306                         pCMD->cmd[DTL]=0xFF; /* DTL */
307                         pCMD->cmdlen=FDC_CMD_READ_LEN;
308                         pCMD->cmd[COMMAND]|=(FDC_MFM_MODE<<6); /* set MFM bit */
309                         pCMD->cmd[COMMAND]|=(FDC_SKIP_MODE<<5); /* set Skip bit */
310                         pCMD->resultlen=0;  /* result only after execution */
311                         break;
312                 case FDC_CMD_SEEK:
313                         pCMD->cmdlen=FDC_CMD_SEEK_LEN;
314                         pCMD->resultlen=0;  /* no result */
315                         break;
316                 case FDC_CMD_CONFIGURE:
317                         pCMD->cmd[CONFIG0]=0;
318                         pCMD->cmd[CONFIG1]=FDC_CONFIGURE; /* FIFO Threshold, Poll, Enable FIFO */
319                         pCMD->cmd[CONFIG2]=FDC_PRE_TRK;         /* Precompensation Track */
320                         pCMD->cmdlen=FDC_CMD_CONFIGURE_LEN;
321                         pCMD->resultlen=0;  /* no result */
322                         break;
323                 case FDC_CMD_SPECIFY:
324                         pCMD->cmd[SPEC_HUTSRT]=pFG->spec1;
325                         pCMD->cmd[SPEC_HLT]=(pFG->hlt)<<1; /* head load time */
326                         if(pCMD->dma==0)
327                                 pCMD->cmd[SPEC_HLT]|=0x1; /* no dma */
328                         pCMD->cmdlen=FDC_CMD_SPECIFY_LEN;
329                         pCMD->resultlen=0;  /* no result */
330                         break;
331                 case FDC_CMD_DUMP_REG:
332                         pCMD->cmdlen=FDC_CMD_DUMP_REG_LEN;
333                         pCMD->resultlen=10;  /* 10 byte result */
334                         break;
335                 case FDC_CMD_READ_ID:
336                         pCMD->cmd[COMMAND]|=(FDC_MFM_MODE<<6); /* set MFM bit */
337                         pCMD->cmdlen=FDC_CMD_READ_ID_LEN;
338                         pCMD->resultlen=7;  /* 7 byte result */
339                         break;
340                 case FDC_CMD_RECALIBRATE:
341                         pCMD->cmd[DRIVE]&=0x03; /* don't set the head bit */
342                         pCMD->cmdlen=FDC_CMD_RECALIBRATE_LEN;
343                         pCMD->resultlen=0;  /* no result */
344                         break;
345                         break;
346                 case FDC_CMD_SENSE_INT:
347                         pCMD->cmdlen=FDC_CMD_SENSE_INT_LEN;
348                         pCMD->resultlen=2;
349                         break;
350         }
351         for(i=0;i<pCMD->cmdlen;i++) {
352                 /* PRINTF("write cmd%d = 0x%02X\n",i,pCMD->cmd[i]); */
353                 if(write_fdc_byte(pCMD->cmd[i])==FALSE) {
354                         PRINTF("Error: timeout while issue cmd%d\n",i);
355                         return FALSE;
356                 }
357         }
358         timeout=FDC_TIME_OUT;
359         for(i=0;i<pCMD->resultlen;i++) {
360                 while((read_fdc_reg(FDC_MSR)&0xC0)!=0xC0) {
361                         timeout--;
362                         if(timeout==0) {
363                                 PRINTF(" timeout while reading result%d MSR=0x%02X\n",i,read_fdc_reg(FDC_MSR));
364                                 return FALSE;
365                         }
366                 }
367                 pCMD->result[i]=(unsigned char)read_fdc_byte();
368         }
369         return TRUE;
370 }
371
372 /* selects the drive assigned in the cmd structur and
373    switches on the Motor */
374 void select_fdc_drive(FDC_COMMAND_STRUCT *pCMD)
375 {
376         unsigned char val;
377
378         val=(1<<(4+pCMD->drive))|pCMD->drive|0xC; /* set reset, dma gate and motor bits */
379         if((read_fdc_reg(FDC_DOR)&val)!=val) {
380                 write_fdc_reg(FDC_DOR,val);
381                 for(val=0;val<255;val++)
382                         udelay(500); /* wait some time to start motor */
383         }
384 }
385
386 /* switches off the Motor of the specified drive */
387 void stop_fdc_drive(FDC_COMMAND_STRUCT *pCMD)
388 {
389         unsigned char val;
390
391         val=(1<<(4+pCMD->drive))|pCMD->drive; /* sets motor bits */
392         write_fdc_reg(FDC_DOR,(read_fdc_reg(FDC_DOR)&~val));
393 }
394
395 /* issues a recalibrate command, waits for interrupt and
396  * issues a sense_interrupt */
397 int fdc_recalibrate(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
398 {
399         pCMD->cmd[COMMAND]=FDC_CMD_RECALIBRATE;
400         if(fdc_issue_cmd(pCMD,pFG)==FALSE)
401                 return FALSE;
402         while(wait_for_fdc_int()!=TRUE);
403         pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
404         return(fdc_issue_cmd(pCMD,pFG));
405 }
406
407 /* issues a recalibrate command, waits for interrupt and
408  * issues a sense_interrupt */
409 int fdc_seek(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
410 {
411         pCMD->cmd[COMMAND]=FDC_CMD_SEEK;
412         if(fdc_issue_cmd(pCMD,pFG)==FALSE)
413                 return FALSE;
414         while(wait_for_fdc_int()!=TRUE);
415         pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
416         return(fdc_issue_cmd(pCMD,pFG));
417 }
418
419
420 /* terminates current command, by not servicing the FIFO
421  * waits for interrupt and fills in the result bytes */
422 int fdc_terminate(FDC_COMMAND_STRUCT *pCMD)
423 {
424         int i;
425         for(i=0;i<100;i++)
426                 udelay(500); /* wait 500usec for fifo overrun */
427         while((read_fdc_reg(FDC_SRA)&0x80)==0x00); /* wait as long as no int has occured */
428         for(i=0;i<7;i++) {
429                 pCMD->result[i]=(unsigned char)read_fdc_byte();
430         }
431         return TRUE;
432 }
433
434 /* reads data from FDC, seek commands are issued automatic */
435 int fdc_read_data(unsigned char *buffer, unsigned long blocks,FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
436 {
437   /* first seek to start address */
438         unsigned long len,lastblk,readblk,i,timeout,ii,offset;
439         unsigned char pcn,c,retriesrw,retriescal;
440         unsigned char *bufferw; /* working buffer */
441         int sect_size;
442         int flags;
443
444         flags=disable_interrupts(); /* switch off all Interrupts */
445         select_fdc_drive(pCMD); /* switch on drive */
446         sect_size=0x080<<pFG->sect_code;
447         retriesrw=0;
448         retriescal=0;
449         offset=0;
450         if(fdc_seek(pCMD,pFG)==FALSE) {
451                 stop_fdc_drive(pCMD);
452                 enable_interrupts();
453                 return FALSE;
454         }
455         if((pCMD->result[STATUS_0]&0x20)!=0x20) {
456                 printf("Seek error Status: %02X\n",pCMD->result[STATUS_0]);
457                 stop_fdc_drive(pCMD);
458                 enable_interrupts();
459                 return FALSE;
460         }
461         pcn=pCMD->result[STATUS_PCN]; /* current track */
462         /* now determine the next seek point */
463         lastblk=pCMD->blnr + blocks;
464         /*      readblk=(pFG->head*pFG->sect)-(pCMD->blnr%(pFG->head*pFG->sect)); */
465         readblk=pFG->sect-(pCMD->blnr%pFG->sect);
466         PRINTF("1st nr of block possible read %ld start %ld\n",readblk,pCMD->blnr);
467         if(readblk>blocks) /* is end within 1st track */
468                 readblk=blocks; /* yes, correct it */
469         PRINTF("we read %ld blocks start %ld\n",readblk,pCMD->blnr);
470         bufferw=&buffer[0]; /* setup working buffer */
471         do {
472 retryrw:
473                 len=sect_size * readblk;
474                 pCMD->cmd[COMMAND]=FDC_CMD_READ;
475                 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
476                         stop_fdc_drive(pCMD);
477                         enable_interrupts();
478                         return FALSE;
479                 }
480                 for (i=0;i<len;i++) {
481                         timeout=FDC_TIME_OUT;
482                         do {
483                                 c=read_fdc_reg(FDC_MSR);
484                                 if((c&0xC0)==0xC0) {
485                                         bufferw[i]=read_fdc_reg(FDC_FIFO);
486                                         break;
487                                 }
488                                 if((c&0xC0)==0x80) { /* output */
489                                         PRINTF("Transfer error transfered: at %ld, MSR=%02X\n",i,c);
490                                         if(i>6) {
491                                                 for(ii=0;ii<7;ii++) {
492                                                         pCMD->result[ii]=bufferw[(i-7+ii)];
493                                                 } /* for */
494                                         }
495                                         if(retriesrw++>FDC_RW_RETRIES) {
496                                                 if (retriescal++>FDC_CAL_RETRIES) {
497                                                         stop_fdc_drive(pCMD);
498                                                         enable_interrupts();
499                                                         return FALSE;
500                                                 }
501                                                 else {
502                                                         PRINTF(" trying to recalibrate Try %d\n",retriescal);
503                                                         if(fdc_recalibrate(pCMD,pFG)==FALSE) {
504                                                                 stop_fdc_drive(pCMD);
505                                                                 enable_interrupts();
506                                                                 return FALSE;
507                                                         }
508                                                         retriesrw=0;
509                                                         goto retrycal;
510                                                 } /* else >FDC_CAL_RETRIES */
511                                         }
512                                         else {
513                                                 PRINTF("Read retry %d\n",retriesrw);
514                                                 goto retryrw;
515                                         } /* else >FDC_RW_RETRIES */
516                                 }/* if output */
517                                 timeout--;
518                         }while(TRUE);
519                 } /* for len */
520                 /* the last sector of a track or all data has been read,
521                  * we need to get the results */
522                 fdc_terminate(pCMD);
523                 offset+=(sect_size*readblk); /* set up buffer pointer */
524                 bufferw=&buffer[offset];
525                 pCMD->blnr+=readblk; /* update current block nr */
526                 blocks-=readblk; /* update blocks */
527                 if(blocks==0)
528                         break; /* we are finish */
529                 /* setup new read blocks */
530                 /*      readblk=pFG->head*pFG->sect; */
531                 readblk=pFG->sect;
532                 if(readblk>blocks)
533                         readblk=blocks;
534 retrycal:
535                 /* a seek is necessary */
536                 if(fdc_seek(pCMD,pFG)==FALSE) {
537                         stop_fdc_drive(pCMD);
538                         enable_interrupts();
539                         return FALSE;
540                 }
541                 if((pCMD->result[STATUS_0]&0x20)!=0x20) {
542                         PRINTF("Seek error Status: %02X\n",pCMD->result[STATUS_0]);
543                         stop_fdc_drive(pCMD);
544                         return FALSE;
545                 }
546                 pcn=pCMD->result[STATUS_PCN]; /* current track */
547         }while(TRUE); /* start over */
548         stop_fdc_drive(pCMD); /* switch off drive */
549         enable_interrupts();
550         return TRUE;
551 }
552
553 /* Scan all drives and check if drive is present and disk is inserted */
554 int fdc_check_drive(FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
555 {
556         int i,drives,state;
557   /* OK procedure of data book is satisfied.
558          * trying to get some information over the drives */
559         state=0; /* no drives, no disks */
560         for(drives=0;drives<4;drives++) {
561                 pCMD->drive=drives;
562                 select_fdc_drive(pCMD);
563                 pCMD->blnr=0; /* set to the 1st block */
564                 if(fdc_recalibrate(pCMD,pFG)==FALSE)
565                         continue;
566                 if((pCMD->result[STATUS_0]&0x10)==0x10)
567                         continue;
568                 /* ok drive connected check for disk */
569                 state|=(1<<drives);
570                 pCMD->blnr=pFG->size; /* set to the last block */
571                 if(fdc_seek(pCMD,pFG)==FALSE)
572                         continue;
573                 pCMD->blnr=0; /* set to the 1st block */
574                 if(fdc_recalibrate(pCMD,pFG)==FALSE)
575                         continue;
576                 pCMD->cmd[COMMAND]=FDC_CMD_READ_ID;
577                 if(fdc_issue_cmd(pCMD,pFG)==FALSE)
578                         continue;
579                 state|=(0x10<<drives);
580         }
581         stop_fdc_drive(pCMD);
582         for(i=0;i<4;i++) {
583                 PRINTF("Floppy Drive %d %sconnected %sDisk inserted %s\n",i,
584                         ((state&(1<<i))==(1<<i)) ? "":"not ",
585                         ((state&(0x10<<i))==(0x10<<i)) ? "":"no ",
586                         ((state&(0x10<<i))==(0x10<<i)) ? pFG->name : "");
587         }
588         pCMD->flags=state;
589         return TRUE;
590 }
591
592
593 /**************************************************************************
594 * int fdc_setup
595 * setup the fdc according the datasheet
596 * assuming in PS2 Mode
597 */
598 int fdc_setup(int drive, FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
599 {
600
601         int i;
602
603 #ifdef CFG_FDC_HW_INIT
604         fdc_hw_init ();
605 #endif
606         /* first, we reset the FDC via the DOR */
607         write_fdc_reg(FDC_DOR,0x00);
608         for(i=0; i<255; i++) /* then we wait some time */
609                 udelay(500);
610         /* then, we clear the reset in the DOR */
611         pCMD->drive=drive;
612         select_fdc_drive(pCMD);
613         /* initialize the CCR */
614         write_fdc_reg(FDC_CCR,pFG->rate);
615         /* then initialize the DSR */
616         write_fdc_reg(FDC_DSR,pFG->rate);
617         if(wait_for_fdc_int()==FALSE) {
618                         PRINTF("Time Out after writing CCR\n");
619                         return FALSE;
620         }
621         /* now issue sense Interrupt and status command
622          * assuming only one drive present (drive 0) */
623         pCMD->dma=0; /* we don't use any dma at all */
624         for(i=0;i<4;i++) {
625                 /* issue sense interrupt for all 4 possible drives */
626                 pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
627                 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
628                         PRINTF("Sense Interrupt for drive %d failed\n",i);
629                 }
630         }
631         /* issue the configure command */
632         pCMD->drive=drive;
633         select_fdc_drive(pCMD);
634         pCMD->cmd[COMMAND]=FDC_CMD_CONFIGURE;
635         if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
636                 PRINTF(" configure timeout\n");
637                 stop_fdc_drive(pCMD);
638                 return FALSE;
639         }
640         /* issue specify command */
641         pCMD->cmd[COMMAND]=FDC_CMD_SPECIFY;
642         if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
643                 PRINTF(" specify timeout\n");
644                 stop_fdc_drive(pCMD);
645                 return FALSE;
646
647         }
648         /* then, we clear the reset in the DOR */
649         /* fdc_check_drive(pCMD,pFG);   */
650         /*      write_fdc_reg(FDC_DOR,0x04); */
651         return TRUE;
652 }
653 #endif /* ((CONFIG_COMMANDS & CFG_CMD_FDC)||(CONFIG_COMMANDS & CFG_CMD_FDOS))*/
654
655 #if (CONFIG_COMMANDS & CFG_CMD_FDOS)
656
657 /* Low level functions for the Floppy-DOS layer                              */
658
659 /**************************************************************************
660 * int fdc_fdos_init
661 * initialize the FDC layer 
662
663 */
664 int fdc_fdos_init (int drive)
665 {
666         FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
667         FDC_COMMAND_STRUCT *pCMD = &cmd;
668     
669         /* setup FDC and scan for drives  */
670         if(fdc_setup(drive,pCMD,pFG)==FALSE) {
671                 printf("\n** Error in setup FDC **\n");
672                 return FALSE;
673         }
674         if(fdc_check_drive(pCMD,pFG)==FALSE) {
675                 printf("\n** Error in check_drives **\n");
676                 return FALSE;
677         }
678         if((pCMD->flags&(1<<drive))==0) {
679                 /* drive not available */
680                 printf("\n** Drive %d not available **\n",drive);
681                 return FALSE;
682         }
683         if((pCMD->flags&(0x10<<drive))==0) {
684                 /* no disk inserted */
685                 printf("\n** No disk inserted in drive %d **\n",drive);
686                 return FALSE;
687         }
688         /* ok, we have a valid source */
689         pCMD->drive=drive;
690
691         /* read first block */
692         pCMD->blnr=0;
693         return TRUE;
694 }
695 /**************************************************************************
696 * int fdc_fdos_seek
697 * parameter is a block number 
698 */
699 int fdc_fdos_seek (int where)
700 {
701         FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
702         FDC_COMMAND_STRUCT *pCMD = &cmd;
703
704         pCMD -> blnr = where ;
705         return (fdc_seek (pCMD, pFG));
706 }
707 /**************************************************************************
708 * int fdc_fdos_read
709 *  the length is in block number
710 */
711 int fdc_fdos_read (void *buffer, int len)
712 {
713         FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
714         FDC_COMMAND_STRUCT *pCMD = &cmd;
715
716         return (fdc_read_data (buffer, len, pCMD, pFG));
717 }
718 #endif  /* (CONFIG_COMMANDS & CFG_CMD_FDOS)                                  */
719
720 #if (CONFIG_COMMANDS & CFG_CMD_FDC)
721 /****************************************************************************
722  * main routine do_fdcboot
723  */
724 int do_fdcboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
725 {
726         FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
727         FDC_COMMAND_STRUCT *pCMD = &cmd;
728         unsigned long addr,imsize;
729         image_header_t *hdr;  /* used for fdc boot */
730         unsigned char boot_drive;
731         int i,nrofblk;
732         char *ep;
733         int rcode = 0;
734
735         switch (argc) {
736         case 1:
737                 addr = CFG_LOAD_ADDR;
738                 boot_drive=CFG_FDC_DRIVE_NUMBER; 
739                 break;
740         case 2:
741                 addr = simple_strtoul(argv[1], NULL, 16);
742                 boot_drive=CFG_FDC_DRIVE_NUMBER;
743                 break;
744         case 3:
745                 addr = simple_strtoul(argv[1], NULL, 16);
746                 boot_drive=simple_strtoul(argv[2], NULL, 10);
747                 break;
748         default:
749                 printf ("Usage:\n%s\n", cmdtp->usage);
750                 return 1;
751         }
752         /* setup FDC and scan for drives  */
753         if(fdc_setup(boot_drive,pCMD,pFG)==FALSE) {
754                 printf("\n** Error in setup FDC **\n");
755                 return 1;
756         }
757         if(fdc_check_drive(pCMD,pFG)==FALSE) {
758                 printf("\n** Error in check_drives **\n");
759                 return 1;
760         }
761         if((pCMD->flags&(1<<boot_drive))==0) {
762                 /* drive not available */
763                 printf("\n** Drive %d not availabe **\n",boot_drive);
764                 return 1;
765         }
766         if((pCMD->flags&(0x10<<boot_drive))==0) {
767                 /* no disk inserted */
768                 printf("\n** No disk inserted in drive %d **\n",boot_drive);
769                 return 1;
770         }
771         /* ok, we have a valid source */
772         pCMD->drive=boot_drive;
773         /* read first block */
774         pCMD->blnr=0;
775         if(fdc_read_data((unsigned char *)addr,1,pCMD,pFG)==FALSE) {
776                 printf("\nRead error:");
777                 for(i=0;i<7;i++)
778                         printf("result%d: 0x%02X\n",i,pCMD->result[i]);
779                 return 1;
780         }
781         hdr = (image_header_t *)addr;
782         if (hdr->ih_magic  != IH_MAGIC) {
783                 printf ("Bad Magic Number\n");
784                 return 1;
785         }
786         print_image_hdr(hdr);
787
788         imsize= hdr->ih_size+sizeof(image_header_t);
789         nrofblk=imsize/512;
790         if((imsize%512)>0)
791                 nrofblk++;
792         printf("Loading %ld Bytes (%d blocks) at 0x%08lx..\n",imsize,nrofblk,addr);
793         pCMD->blnr=0;
794         if(fdc_read_data((unsigned char *)addr,nrofblk,pCMD,pFG)==FALSE) {
795                 /* read image block */
796                 printf("\nRead error:");
797                 for(i=0;i<7;i++)
798                         printf("result%d: 0x%02X\n",i,pCMD->result[i]);
799                 return 1;
800         }
801         printf("OK %ld Bytes loaded.\n",imsize);
802
803         flush_cache (addr, imsize);
804         /* Loading ok, update default load address */
805
806         load_addr = addr;
807         if(hdr->ih_type  == IH_TYPE_KERNEL) {
808                 /* Check if we should attempt an auto-start */
809                 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
810                         char *local_args[2];
811                         extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
812
813                         local_args[0] = argv[0];
814                         local_args[1] = NULL;
815
816                         printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
817
818                         do_bootm (cmdtp, 0, 1, local_args);
819                         rcode ++;
820                 }
821         }
822         return rcode;
823 }
824
825
826
827 #endif /* CONFIG_COMMANDS & CFG_CMD_FDC */
828
829