Initial revision
[platform/kernel/u-boot.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)
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 /* Supporting Functions */
177 /* reads a Register of the FDC */
178 unsigned char read_fdc_reg(unsigned int addr)
179 {
180         volatile unsigned char *val = (volatile unsigned char *)(CFG_ISA_IO_BASE_ADDRESS | addr);
181         return val[0];
182 }
183
184 /* writes a Register of the FDC */
185 void write_fdc_reg(unsigned int addr, unsigned char val)
186 {
187                 volatile unsigned char *tmp = (volatile unsigned char *)(CFG_ISA_IO_BASE_ADDRESS | addr);
188                 tmp[0]=val;
189 }
190
191 /* waits for an interrupt (polling) */
192 int wait_for_fdc_int(void)
193 {
194         unsigned long timeout;
195         timeout = FDC_TIME_OUT;
196         while((read_fdc_reg(FDC_SRA)&0x80)==0) {
197                 timeout--;
198                 udelay(10);
199                 if(timeout==0) /* timeout occured */
200                         return FALSE;
201         }
202         return TRUE;
203 }
204
205
206 /* reads a byte from the FIFO of the FDC and checks direction and RQM bit
207    of the MSR. returns -1 if timeout, or byte if ok */
208 int read_fdc_byte(void)
209 {
210         unsigned long timeout;
211         timeout = FDC_TIME_OUT;
212         while((read_fdc_reg(FDC_MSR)&0xC0)!=0xC0) {
213                 /* direction out and ready */
214                 udelay(10);
215                 timeout--;
216                 if(timeout==0) /* timeout occured */
217                         return -1;
218         }
219         return read_fdc_reg(FDC_FIFO);
220 }
221
222 /* if the direction of the FIFO is wrong, this routine is used to
223    empty the FIFO. Should _not_ be used */
224 int fdc_need_more_output(void)
225 {
226         unsigned char c;
227         while((read_fdc_reg(FDC_MSR)&0xC0)==0xC0)       {
228                         c=(unsigned char)read_fdc_byte();
229                         printf("Error: more output: %x\n",c);
230         }
231         return TRUE;
232 }
233
234
235 /* writes a byte to the FIFO of the FDC and checks direction and RQM bit
236    of the MSR */
237 int write_fdc_byte(unsigned char val)
238 {
239         unsigned long timeout;
240         timeout = FDC_TIME_OUT;
241         while((read_fdc_reg(FDC_MSR)&0xC0)!=0x80) {
242                 /* direction in and ready for byte */
243                 timeout--;
244                 udelay(10);
245                 fdc_need_more_output();
246                 if(timeout==0) /* timeout occured */
247                         return FALSE;
248         }
249         write_fdc_reg(FDC_FIFO,val);
250         return TRUE;
251 }
252
253 /* sets up all FDC commands and issues it to the FDC. If
254    the command causes direct results (no Execution Phase)
255    the result is be read as well. */
256
257 int fdc_issue_cmd(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
258 {
259         int i;
260         unsigned long head,track,sect,timeout;
261         track = pCMD->blnr / (pFG->sect * pFG->head); /* track nr */
262         sect =  pCMD->blnr % (pFG->sect * pFG->head); /* remaining blocks */
263         head = sect / pFG->sect; /* head nr */
264         sect =  sect % pFG->sect; /* remaining blocks */
265         sect++; /* sectors are 1 based */
266         PRINTF("Track %ld, Head %ld, Sector %ld, Drive %d (blnr %ld)\n",track,head,sect,pCMD->drive,pCMD->blnr);
267         if(head|=0) { /* max heads = 2 */
268                 pCMD->cmd[DRIVE]=pCMD->drive | 0x04; /* head 1 */
269                 pCMD->cmd[HEAD]=(unsigned char) head; /* head register */
270         }
271         else {
272                 pCMD->cmd[DRIVE]=pCMD->drive; /* head 0 */
273                 pCMD->cmd[HEAD]=(unsigned char) head; /* head register */
274         }
275         pCMD->cmd[TRACK]=(unsigned char) track; /* track */
276         switch (pCMD->cmd[COMMAND]) {
277                 case FDC_CMD_READ:
278                         pCMD->cmd[SECTOR]=(unsigned char) sect; /* sector */
279                         pCMD->cmd[SECTOR_SIZE]=pFG->sect_code; /* sector size code */
280                         pCMD->cmd[LAST_TRACK]=pFG->sect; /* End of track */
281                         pCMD->cmd[GAP]=pFG->gap; /* gap */
282                         pCMD->cmd[DTL]=0xFF; /* DTL */
283                         pCMD->cmdlen=FDC_CMD_READ_LEN;
284                         pCMD->cmd[COMMAND]|=(FDC_MFM_MODE<<6); /* set MFM bit */
285                         pCMD->cmd[COMMAND]|=(FDC_SKIP_MODE<<5); /* set Skip bit */
286                         pCMD->resultlen=0;  /* result only after execution */
287                         break;
288                 case FDC_CMD_SEEK:
289                         pCMD->cmdlen=FDC_CMD_SEEK_LEN;
290                         pCMD->resultlen=0;  /* no result */
291                         break;
292                 case FDC_CMD_CONFIGURE:
293                         pCMD->cmd[CONFIG0]=0;
294                         pCMD->cmd[CONFIG1]=FDC_CONFIGURE; /* FIFO Threshold, Poll, Enable FIFO */
295                         pCMD->cmd[CONFIG2]=FDC_PRE_TRK;         /* Precompensation Track */
296                         pCMD->cmdlen=FDC_CMD_CONFIGURE_LEN;
297                         pCMD->resultlen=0;  /* no result */
298                         break;
299                 case FDC_CMD_SPECIFY:
300                         pCMD->cmd[SPEC_HUTSRT]=pFG->spec1;
301                         pCMD->cmd[SPEC_HLT]=(pFG->hlt)<<1; /* head load time */
302                         if(pCMD->dma==0)
303                                 pCMD->cmd[SPEC_HLT]|=0x1; /* no dma */
304                         pCMD->cmdlen=FDC_CMD_SPECIFY_LEN;
305                         pCMD->resultlen=0;  /* no result */
306                         break;
307                 case FDC_CMD_DUMP_REG:
308                         pCMD->cmdlen=FDC_CMD_DUMP_REG_LEN;
309                         pCMD->resultlen=10;  /* 10 byte result */
310                         break;
311                 case FDC_CMD_READ_ID:
312                         pCMD->cmd[COMMAND]|=(FDC_MFM_MODE<<6); /* set MFM bit */
313                         pCMD->cmdlen=FDC_CMD_READ_ID_LEN;
314                         pCMD->resultlen=7;  /* 7 byte result */
315                         break;
316                 case FDC_CMD_RECALIBRATE:
317                         pCMD->cmd[DRIVE]&=0x03; /* don't set the head bit */
318                         pCMD->cmdlen=FDC_CMD_RECALIBRATE_LEN;
319                         pCMD->resultlen=0;  /* no result */
320                         break;
321                         break;
322                 case FDC_CMD_SENSE_INT:
323                         pCMD->cmdlen=FDC_CMD_SENSE_INT_LEN;
324                         pCMD->resultlen=2;
325                         break;
326         }
327         for(i=0;i<pCMD->cmdlen;i++) {
328                 /* PRINTF("write cmd%d = 0x%02X\n",i,pCMD->cmd[i]); */
329                 if(write_fdc_byte(pCMD->cmd[i])==FALSE) {
330                         PRINTF("Error: timeout while issue cmd%d\n",i);
331                         return FALSE;
332                 }
333         }
334         timeout=FDC_TIME_OUT;
335         for(i=0;i<pCMD->resultlen;i++) {
336                 while((read_fdc_reg(FDC_MSR)&0xC0)!=0xC0) {
337                         timeout--;
338                         if(timeout==0) {
339                                 PRINTF(" timeout while reading result%d MSR=0x%02X\n",i,read_fdc_reg(FDC_MSR));
340                                 return FALSE;
341                         }
342                 }
343                 pCMD->result[i]=(unsigned char)read_fdc_byte();
344         }
345         return TRUE;
346 }
347
348 /* selects the drive assigned in the cmd structur and
349    switches on the Motor */
350 void select_fdc_drive(FDC_COMMAND_STRUCT *pCMD)
351 {
352         unsigned char val;
353
354         val=(1<<(4+pCMD->drive))|pCMD->drive|0xC; /* set reset, dma gate and motor bits */
355         if((read_fdc_reg(FDC_DOR)&val)!=val) {
356                 write_fdc_reg(FDC_DOR,val);
357                 for(val=0;val<255;val++)
358                         udelay(500); /* wait some time to start motor */
359         }
360 }
361
362 /* switches off the Motor of the specified drive */
363 void stop_fdc_drive(FDC_COMMAND_STRUCT *pCMD)
364 {
365         unsigned char val;
366
367         val=(1<<(4+pCMD->drive))|pCMD->drive; /* sets motor bits */
368         write_fdc_reg(FDC_DOR,(read_fdc_reg(FDC_DOR)&~val));
369 }
370
371 /* issues a recalibrate command, waits for interrupt and
372  * issues a sense_interrupt */
373 int fdc_recalibrate(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
374 {
375         pCMD->cmd[COMMAND]=FDC_CMD_RECALIBRATE;
376         if(fdc_issue_cmd(pCMD,pFG)==FALSE)
377                 return FALSE;
378         while(wait_for_fdc_int()!=TRUE);
379         pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
380         return(fdc_issue_cmd(pCMD,pFG));
381 }
382
383 /* issues a recalibrate command, waits for interrupt and
384  * issues a sense_interrupt */
385 int fdc_seek(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
386 {
387         pCMD->cmd[COMMAND]=FDC_CMD_SEEK;
388         if(fdc_issue_cmd(pCMD,pFG)==FALSE)
389                 return FALSE;
390         while(wait_for_fdc_int()!=TRUE);
391         pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
392         return(fdc_issue_cmd(pCMD,pFG));
393 }
394
395
396 /* terminates current command, by not servicing the FIFO
397  * waits for interrupt and fills in the result bytes */
398 int fdc_terminate(FDC_COMMAND_STRUCT *pCMD)
399 {
400         int i;
401         for(i=0;i<100;i++)
402                 udelay(500); /* wait 500usec for fifo overrun */
403         while((read_fdc_reg(FDC_SRA)&0x80)==0x00); /* wait as long as no int has occured */
404         for(i=0;i<7;i++) {
405                 pCMD->result[i]=(unsigned char)read_fdc_byte();
406         }
407         return TRUE;
408 }
409
410 /* reads data from FDC, seek commands are issued automatic */
411 int fdc_read_data(unsigned char *buffer, unsigned long blocks,FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
412 {
413   /* first seek to start address */
414         unsigned long len,lastblk,readblk,i,timeout,ii,offset;
415         unsigned char pcn,c,retriesrw,retriescal;
416         unsigned char *bufferw; /* working buffer */
417         int sect_size;
418         int flags;
419
420         flags=disable_interrupts(); /* switch off all Interrupts */
421         select_fdc_drive(pCMD); /* switch on drive */
422         sect_size=0x080<<pFG->sect_code;
423         retriesrw=0;
424         retriescal=0;
425         offset=0;
426         if(fdc_seek(pCMD,pFG)==FALSE) {
427                 stop_fdc_drive(pCMD);
428                 enable_interrupts();
429                 return FALSE;
430         }
431         if((pCMD->result[STATUS_0]&0x20)!=0x20) {
432                 printf("Seek error Status: %02X\n",pCMD->result[STATUS_0]);
433                 stop_fdc_drive(pCMD);
434                 enable_interrupts();
435                 return FALSE;
436         }
437         pcn=pCMD->result[STATUS_PCN]; /* current track */
438         /* now determine the next seek point */
439         lastblk=pCMD->blnr + blocks;
440         /*      readblk=(pFG->head*pFG->sect)-(pCMD->blnr%(pFG->head*pFG->sect)); */
441         readblk=pFG->sect-(pCMD->blnr%pFG->sect);
442         PRINTF("1st nr of block possible read %ld start %ld\n",readblk,pCMD->blnr);
443         if(readblk>blocks) /* is end within 1st track */
444                 readblk=blocks; /* yes, correct it */
445         PRINTF("we read %ld blocks start %ld\n",readblk,pCMD->blnr);
446         bufferw=&buffer[0]; /* setup working buffer */
447         do {
448 retryrw:
449                 len=sect_size * readblk;
450                 pCMD->cmd[COMMAND]=FDC_CMD_READ;
451                 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
452                         stop_fdc_drive(pCMD);
453                         enable_interrupts();
454                         return FALSE;
455                 }
456                 for (i=0;i<len;i++) {
457                         timeout=FDC_TIME_OUT;
458                         do {
459                                 c=read_fdc_reg(FDC_MSR);
460                                 if((c&0xC0)==0xC0) {
461                                         bufferw[i]=read_fdc_reg(FDC_FIFO);
462                                         break;
463                                 }
464                                 if((c&0xC0)==0x80) { /* output */
465                                         PRINTF("Transfer error transfered: at %ld, MSR=%02X\n",i,c);
466                                         if(i>6) {
467                                                 for(ii=0;ii<7;ii++) {
468                                                         pCMD->result[ii]=bufferw[(i-7+ii)];
469                                                 } /* for */
470                                         }
471                                         if(retriesrw++>FDC_RW_RETRIES) {
472                                                 if (retriescal++>FDC_CAL_RETRIES) {
473                                                         stop_fdc_drive(pCMD);
474                                                         enable_interrupts();
475                                                         return FALSE;
476                                                 }
477                                                 else {
478                                                         PRINTF(" trying to recalibrate Try %d\n",retriescal);
479                                                         if(fdc_recalibrate(pCMD,pFG)==FALSE) {
480                                                                 stop_fdc_drive(pCMD);
481                                                                 enable_interrupts();
482                                                                 return FALSE;
483                                                         }
484                                                         retriesrw=0;
485                                                         goto retrycal;
486                                                 } /* else >FDC_CAL_RETRIES */
487                                         }
488                                         else {
489                                                 PRINTF("Read retry %d\n",retriesrw);
490                                                 goto retryrw;
491                                         } /* else >FDC_RW_RETRIES */
492                                 }/* if output */
493                                 timeout--;
494                         }while(TRUE);
495                 } /* for len */
496                 /* the last sector of a track or all data has been read,
497                  * we need to get the results */
498                 fdc_terminate(pCMD);
499                 offset+=(sect_size*readblk); /* set up buffer pointer */
500                 bufferw=&buffer[offset];
501                 pCMD->blnr+=readblk; /* update current block nr */
502                 blocks-=readblk; /* update blocks */
503                 if(blocks==0)
504                         break; /* we are finish */
505                 /* setup new read blocks */
506                 /*      readblk=pFG->head*pFG->sect; */
507                 readblk=pFG->sect;
508                 if(readblk>blocks)
509                         readblk=blocks;
510 retrycal:
511                 /* a seek is necessary */
512                 if(fdc_seek(pCMD,pFG)==FALSE) {
513                         stop_fdc_drive(pCMD);
514                         enable_interrupts();
515                         return FALSE;
516                 }
517                 if((pCMD->result[STATUS_0]&0x20)!=0x20) {
518                         PRINTF("Seek error Status: %02X\n",pCMD->result[STATUS_0]);
519                         stop_fdc_drive(pCMD);
520                         return FALSE;
521                 }
522                 pcn=pCMD->result[STATUS_PCN]; /* current track */
523         }while(TRUE); /* start over */
524         stop_fdc_drive(pCMD); /* switch off drive */
525         enable_interrupts();
526         return TRUE;
527 }
528
529 /* Scan all drives and check if drive is present and disk is inserted */
530 int fdc_check_drive(FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
531 {
532         int i,drives,state;
533   /* OK procedure of data book is satisfied.
534          * trying to get some information over the drives */
535         state=0; /* no drives, no disks */
536         for(drives=0;drives<4;drives++) {
537                 pCMD->drive=drives;
538                 select_fdc_drive(pCMD);
539                 pCMD->blnr=0; /* set to the 1st block */
540                 if(fdc_recalibrate(pCMD,pFG)==FALSE)
541                         break;
542                 if((pCMD->result[STATUS_0]&0x10)==0x10)
543                         break;
544                 /* ok drive connected check for disk */
545                 state|=(1<<drives);
546                 pCMD->blnr=pFG->size; /* set to the last block */
547                 if(fdc_seek(pCMD,pFG)==FALSE)
548                         break;
549                 pCMD->blnr=0; /* set to the 1st block */
550                 if(fdc_recalibrate(pCMD,pFG)==FALSE)
551                         break;
552                 pCMD->cmd[COMMAND]=FDC_CMD_READ_ID;
553                 if(fdc_issue_cmd(pCMD,pFG)==FALSE)
554                         break;
555                 state|=(0x10<<drives);
556         }
557         stop_fdc_drive(pCMD);
558         for(i=0;i<4;i++) {
559                 PRINTF("Floppy Drive %d %sconnected %sDisk inserted %s\n",i,
560                         ((state&(1<<i))==(1<<i)) ? "":"not ",
561                         ((state&(0x10<<i))==(0x10<<i)) ? "":"no ",
562                         ((state&(0x10<<i))==(0x10<<i)) ? pFG->name : "");
563         }
564         pCMD->flags=state;
565         return TRUE;
566 }
567
568
569 /**************************************************************************
570 * int fdc_setup
571 * setup the fdc according the datasheet
572 * assuming in PS2 Mode
573 */
574 int fdc_setup(FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
575 {
576
577         int i;
578         /* first, we reset the FDC via the DOR */
579         write_fdc_reg(FDC_DOR,0x00);
580         for(i=0; i<255; i++) /* then we wait some time */
581                 udelay(500);
582         /* then, we clear the reset in the DOR */
583         pCMD->drive=0;
584         select_fdc_drive(pCMD);
585         /* initialize the CCR */
586         write_fdc_reg(FDC_CCR,pFG->rate);
587         /* then initialize the DSR */
588         write_fdc_reg(FDC_DSR,pFG->rate);
589         if(wait_for_fdc_int()==FALSE) {
590                         PRINTF("Time Out after writing CCR\n");
591                         return FALSE;
592         }
593         /* now issue sense Interrupt and status command
594          * assuming only one drive present (drive 0) */
595         pCMD->dma=0; /* we don't use any dma at all */
596         for(i=0;i<4;i++) {
597                 /* issue sense interrupt for all 4 possible drives */
598                 pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
599                 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
600                         PRINTF("Sense Interrupt for drive %d failed\n",i);
601                 }
602         }
603         /* assuming drive 0 for rest of configuration
604          * issue the configure command */
605         pCMD->drive=0;
606         select_fdc_drive(pCMD);
607         pCMD->cmd[COMMAND]=FDC_CMD_CONFIGURE;
608         if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
609                 PRINTF(" configure timeout\n");
610                 stop_fdc_drive(pCMD);
611                 return FALSE;
612         }
613         /* issue specify command */
614         pCMD->cmd[COMMAND]=FDC_CMD_SPECIFY;
615         if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
616                 PRINTF(" specify timeout\n");
617                 stop_fdc_drive(pCMD);
618                 return FALSE;
619
620         }
621         /* then, we clear the reset in the DOR */
622         /* fdc_check_drive(pCMD,pFG);   */
623         /*      write_fdc_reg(FDC_DOR,0x04); */
624         return TRUE;
625 }
626
627 /****************************************************************************
628  * main routine do_fdcboot
629  */
630 int do_fdcboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
631 {
632         FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
633         FDC_COMMAND_STRUCT *pCMD = &cmd;
634         unsigned long addr,imsize;
635         image_header_t *hdr;  /* used for fdc boot */
636         unsigned char boot_drive;
637         int i,nrofblk;
638         char *ep;
639         int rcode = 0;
640
641         switch (argc) {
642         case 1:
643                 addr = CFG_LOAD_ADDR;
644                 boot_drive=0; /* default boot from drive 0 */
645                 break;
646         case 2:
647                 addr = simple_strtoul(argv[1], NULL, 16);
648                 boot_drive=0; /* default boot from drive 0 */
649                 break;
650         case 3:
651                 addr = simple_strtoul(argv[1], NULL, 16);
652                 boot_drive=simple_strtoul(argv[2], NULL, 10);
653                 break;
654         default:
655                 printf ("Usage:\n%s\n", cmdtp->usage);
656                 return 1;
657         }
658         /* setup FDC and scan for drives  */
659         if(fdc_setup(pCMD,pFG)==FALSE) {
660                 printf("\n** Error in setup FDC **\n");
661                 return 1;
662         }
663         if(fdc_check_drive(pCMD,pFG)==FALSE) {
664                 printf("\n** Error in check_drives **\n");
665                 return 1;
666         }
667         if((pCMD->flags&(1<<boot_drive))==0) {
668                 /* drive not available */
669                 printf("\n** Drive %d not availabe **\n",boot_drive);
670                 return 1;
671         }
672         if((pCMD->flags&(0x10<<boot_drive))==0) {
673                 /* no disk inserted */
674                 printf("\n** No disk inserted in drive %d **\n",boot_drive);
675                 return 1;
676         }
677         /* ok, we have a valid source */
678         pCMD->drive=boot_drive;
679         /* read first block */
680         pCMD->blnr=0;
681         if(fdc_read_data((unsigned char *)addr,1,pCMD,pFG)==FALSE) {
682                 printf("\nRead error:");
683                 for(i=0;i<7;i++)
684                         printf("result%d: 0x%02X\n",i,pCMD->result[i]);
685                 return 1;
686         }
687         hdr = (image_header_t *)addr;
688         if (hdr->ih_magic  != IH_MAGIC) {
689                 printf ("Bad Magic Number\n");
690                 return 1;
691         }
692         print_image_hdr(hdr);
693
694         imsize= hdr->ih_size+sizeof(image_header_t);
695         nrofblk=imsize/512;
696         if((imsize%512)>0)
697                 nrofblk++;
698         printf("Loading %ld Bytes (%d blocks) at 0x%08lx..\n",imsize,nrofblk,addr);
699         pCMD->blnr=0;
700         if(fdc_read_data((unsigned char *)addr,nrofblk,pCMD,pFG)==FALSE) {
701                 /* read image block */
702                 printf("\nRead error:");
703                 for(i=0;i<7;i++)
704                         printf("result%d: 0x%02X\n",i,pCMD->result[i]);
705                 return 1;
706         }
707         printf("OK %ld Bytes loaded.\n",imsize);
708
709         flush_cache (addr, imsize);
710         /* Loading ok, update default load address */
711
712         load_addr = addr;
713         if(hdr->ih_type  == IH_TYPE_KERNEL) {
714                 /* Check if we should attempt an auto-start */
715                 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
716                         char *local_args[2];
717                         extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
718
719                         local_args[0] = argv[0];
720                         local_args[1] = NULL;
721
722                         printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
723
724                         do_bootm (cmdtp, 0, 1, local_args);
725                         rcode ++;
726                 }
727         }
728         return rcode;
729 }
730
731
732
733 #endif /* CONFIG_COMMANDS & CFG_CMD_FDC */
734
735