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