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