blob: 03f4ce6d34c918d295091c8ac384161f0b42b234 [file] [log] [blame]
wdenk38635852002-08-27 05:55:31 +00001/*
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
wdenk8bde7f72003-06-27 21:31:46 +000050/*#if (CONFIG_COMMANDS & CFG_CMD_DATE) */
51/*#include <rtc.h> */
52/*#endif */
wdenk38635852002-08-27 05:55:31 +000053
wdenk2262cfe2002-11-18 00:14:45 +000054#if ((CONFIG_COMMANDS & CFG_CMD_FDC) || (CONFIG_COMMANDS & CFG_CMD_FDOS))
wdenk38635852002-08-27 05:55:31 +000055
56
57typedef 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 */
152typedef 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) */
169const 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
174static FDC_COMMAND_STRUCT cmd; /* global command struct */
175
wdenk7f6c2cb2002-11-10 22:06:23 +0000176/* 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
wdenkc7de8292002-11-19 11:04:11 +0000191#ifdef CONFIG_AMIGAONEG3SE
192unsigned char INT6_Status;
193
194void fdc_interrupt(void)
195{
196 INT6_Status = 0x80;
197}
198
199/* waits for an interrupt (polling) */
200int wait_for_fdc_int(void)
201{
202 unsigned long timeout;
203 timeout = FDC_TIME_OUT;
204 while(((volatile)INT6_Status & 0x80) == 0) {
205 timeout--;
206 udelay(10);
207 if(timeout == 0) /* timeout occured */
208 return FALSE;
209 }
210 INT6_Status = 0;
211 return TRUE;
212}
213#endif
wdenk8bde7f72003-06-27 21:31:46 +0000214
wdenk38635852002-08-27 05:55:31 +0000215/* Supporting Functions */
216/* reads a Register of the FDC */
217unsigned char read_fdc_reg(unsigned int addr)
218{
wdenk2262cfe2002-11-18 00:14:45 +0000219 volatile unsigned char *val =
220 (volatile unsigned char *)(CFG_ISA_IO_BASE_ADDRESS +
221 (addr * CFG_ISA_IO_STRIDE) +
222 CFG_ISA_IO_OFFSET);
wdenk8bde7f72003-06-27 21:31:46 +0000223
wdenk7f6c2cb2002-11-10 22:06:23 +0000224 return val [0];
wdenk38635852002-08-27 05:55:31 +0000225}
226
227/* writes a Register of the FDC */
228void write_fdc_reg(unsigned int addr, unsigned char val)
229{
wdenk8bde7f72003-06-27 21:31:46 +0000230 volatile unsigned char *tmp =
wdenk2262cfe2002-11-18 00:14:45 +0000231 (volatile unsigned char *)(CFG_ISA_IO_BASE_ADDRESS +
232 (addr * CFG_ISA_IO_STRIDE) +
233 CFG_ISA_IO_OFFSET);
wdenk7f6c2cb2002-11-10 22:06:23 +0000234 tmp[0]=val;
wdenk38635852002-08-27 05:55:31 +0000235}
236
wdenkc7de8292002-11-19 11:04:11 +0000237#ifndef CONFIG_AMIGAONEG3SE
wdenk38635852002-08-27 05:55:31 +0000238/* waits for an interrupt (polling) */
239int wait_for_fdc_int(void)
240{
241 unsigned long timeout;
242 timeout = FDC_TIME_OUT;
243 while((read_fdc_reg(FDC_SRA)&0x80)==0) {
244 timeout--;
245 udelay(10);
246 if(timeout==0) /* timeout occured */
247 return FALSE;
248 }
249 return TRUE;
250}
251
wdenkc7de8292002-11-19 11:04:11 +0000252#endif
wdenk38635852002-08-27 05:55:31 +0000253
254/* reads a byte from the FIFO of the FDC and checks direction and RQM bit
255 of the MSR. returns -1 if timeout, or byte if ok */
256int read_fdc_byte(void)
257{
258 unsigned long timeout;
259 timeout = FDC_TIME_OUT;
260 while((read_fdc_reg(FDC_MSR)&0xC0)!=0xC0) {
261 /* direction out and ready */
262 udelay(10);
263 timeout--;
264 if(timeout==0) /* timeout occured */
265 return -1;
266 }
267 return read_fdc_reg(FDC_FIFO);
268}
269
270/* if the direction of the FIFO is wrong, this routine is used to
271 empty the FIFO. Should _not_ be used */
272int fdc_need_more_output(void)
273{
274 unsigned char c;
275 while((read_fdc_reg(FDC_MSR)&0xC0)==0xC0) {
276 c=(unsigned char)read_fdc_byte();
277 printf("Error: more output: %x\n",c);
278 }
279 return TRUE;
280}
281
282
283/* writes a byte to the FIFO of the FDC and checks direction and RQM bit
284 of the MSR */
285int write_fdc_byte(unsigned char val)
286{
287 unsigned long timeout;
288 timeout = FDC_TIME_OUT;
289 while((read_fdc_reg(FDC_MSR)&0xC0)!=0x80) {
290 /* direction in and ready for byte */
291 timeout--;
292 udelay(10);
293 fdc_need_more_output();
294 if(timeout==0) /* timeout occured */
295 return FALSE;
296 }
297 write_fdc_reg(FDC_FIFO,val);
298 return TRUE;
299}
300
301/* sets up all FDC commands and issues it to the FDC. If
302 the command causes direct results (no Execution Phase)
303 the result is be read as well. */
304
305int fdc_issue_cmd(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
306{
307 int i;
308 unsigned long head,track,sect,timeout;
309 track = pCMD->blnr / (pFG->sect * pFG->head); /* track nr */
310 sect = pCMD->blnr % (pFG->sect * pFG->head); /* remaining blocks */
311 head = sect / pFG->sect; /* head nr */
312 sect = sect % pFG->sect; /* remaining blocks */
313 sect++; /* sectors are 1 based */
wdenk2262cfe2002-11-18 00:14:45 +0000314 PRINTF("Cmd 0x%02x Track %ld, Head %ld, Sector %ld, Drive %d (blnr %ld)\n",
315 pCMD->cmd[0],track,head,sect,pCMD->drive,pCMD->blnr);
wdenk7f6c2cb2002-11-10 22:06:23 +0000316
wdenk38635852002-08-27 05:55:31 +0000317 if(head|=0) { /* max heads = 2 */
318 pCMD->cmd[DRIVE]=pCMD->drive | 0x04; /* head 1 */
319 pCMD->cmd[HEAD]=(unsigned char) head; /* head register */
320 }
321 else {
322 pCMD->cmd[DRIVE]=pCMD->drive; /* head 0 */
323 pCMD->cmd[HEAD]=(unsigned char) head; /* head register */
324 }
325 pCMD->cmd[TRACK]=(unsigned char) track; /* track */
326 switch (pCMD->cmd[COMMAND]) {
327 case FDC_CMD_READ:
328 pCMD->cmd[SECTOR]=(unsigned char) sect; /* sector */
329 pCMD->cmd[SECTOR_SIZE]=pFG->sect_code; /* sector size code */
330 pCMD->cmd[LAST_TRACK]=pFG->sect; /* End of track */
331 pCMD->cmd[GAP]=pFG->gap; /* gap */
332 pCMD->cmd[DTL]=0xFF; /* DTL */
333 pCMD->cmdlen=FDC_CMD_READ_LEN;
334 pCMD->cmd[COMMAND]|=(FDC_MFM_MODE<<6); /* set MFM bit */
335 pCMD->cmd[COMMAND]|=(FDC_SKIP_MODE<<5); /* set Skip bit */
336 pCMD->resultlen=0; /* result only after execution */
337 break;
338 case FDC_CMD_SEEK:
339 pCMD->cmdlen=FDC_CMD_SEEK_LEN;
340 pCMD->resultlen=0; /* no result */
341 break;
342 case FDC_CMD_CONFIGURE:
343 pCMD->cmd[CONFIG0]=0;
344 pCMD->cmd[CONFIG1]=FDC_CONFIGURE; /* FIFO Threshold, Poll, Enable FIFO */
345 pCMD->cmd[CONFIG2]=FDC_PRE_TRK; /* Precompensation Track */
346 pCMD->cmdlen=FDC_CMD_CONFIGURE_LEN;
347 pCMD->resultlen=0; /* no result */
348 break;
349 case FDC_CMD_SPECIFY:
350 pCMD->cmd[SPEC_HUTSRT]=pFG->spec1;
351 pCMD->cmd[SPEC_HLT]=(pFG->hlt)<<1; /* head load time */
352 if(pCMD->dma==0)
353 pCMD->cmd[SPEC_HLT]|=0x1; /* no dma */
354 pCMD->cmdlen=FDC_CMD_SPECIFY_LEN;
355 pCMD->resultlen=0; /* no result */
356 break;
357 case FDC_CMD_DUMP_REG:
358 pCMD->cmdlen=FDC_CMD_DUMP_REG_LEN;
359 pCMD->resultlen=10; /* 10 byte result */
360 break;
361 case FDC_CMD_READ_ID:
362 pCMD->cmd[COMMAND]|=(FDC_MFM_MODE<<6); /* set MFM bit */
363 pCMD->cmdlen=FDC_CMD_READ_ID_LEN;
364 pCMD->resultlen=7; /* 7 byte result */
365 break;
366 case FDC_CMD_RECALIBRATE:
367 pCMD->cmd[DRIVE]&=0x03; /* don't set the head bit */
368 pCMD->cmdlen=FDC_CMD_RECALIBRATE_LEN;
369 pCMD->resultlen=0; /* no result */
370 break;
371 break;
372 case FDC_CMD_SENSE_INT:
373 pCMD->cmdlen=FDC_CMD_SENSE_INT_LEN;
374 pCMD->resultlen=2;
375 break;
376 }
377 for(i=0;i<pCMD->cmdlen;i++) {
378 /* PRINTF("write cmd%d = 0x%02X\n",i,pCMD->cmd[i]); */
379 if(write_fdc_byte(pCMD->cmd[i])==FALSE) {
380 PRINTF("Error: timeout while issue cmd%d\n",i);
381 return FALSE;
382 }
383 }
384 timeout=FDC_TIME_OUT;
385 for(i=0;i<pCMD->resultlen;i++) {
386 while((read_fdc_reg(FDC_MSR)&0xC0)!=0xC0) {
387 timeout--;
388 if(timeout==0) {
389 PRINTF(" timeout while reading result%d MSR=0x%02X\n",i,read_fdc_reg(FDC_MSR));
390 return FALSE;
391 }
392 }
393 pCMD->result[i]=(unsigned char)read_fdc_byte();
394 }
395 return TRUE;
396}
397
398/* selects the drive assigned in the cmd structur and
399 switches on the Motor */
400void select_fdc_drive(FDC_COMMAND_STRUCT *pCMD)
401{
402 unsigned char val;
403
404 val=(1<<(4+pCMD->drive))|pCMD->drive|0xC; /* set reset, dma gate and motor bits */
405 if((read_fdc_reg(FDC_DOR)&val)!=val) {
406 write_fdc_reg(FDC_DOR,val);
407 for(val=0;val<255;val++)
408 udelay(500); /* wait some time to start motor */
409 }
410}
411
412/* switches off the Motor of the specified drive */
413void stop_fdc_drive(FDC_COMMAND_STRUCT *pCMD)
414{
415 unsigned char val;
416
417 val=(1<<(4+pCMD->drive))|pCMD->drive; /* sets motor bits */
418 write_fdc_reg(FDC_DOR,(read_fdc_reg(FDC_DOR)&~val));
419}
420
421/* issues a recalibrate command, waits for interrupt and
422 * issues a sense_interrupt */
423int fdc_recalibrate(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
424{
425 pCMD->cmd[COMMAND]=FDC_CMD_RECALIBRATE;
426 if(fdc_issue_cmd(pCMD,pFG)==FALSE)
427 return FALSE;
428 while(wait_for_fdc_int()!=TRUE);
429 pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
430 return(fdc_issue_cmd(pCMD,pFG));
431}
432
433/* issues a recalibrate command, waits for interrupt and
434 * issues a sense_interrupt */
435int fdc_seek(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
436{
437 pCMD->cmd[COMMAND]=FDC_CMD_SEEK;
438 if(fdc_issue_cmd(pCMD,pFG)==FALSE)
439 return FALSE;
440 while(wait_for_fdc_int()!=TRUE);
441 pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
442 return(fdc_issue_cmd(pCMD,pFG));
443}
444
wdenkc7de8292002-11-19 11:04:11 +0000445#ifndef CONFIG_AMIGAONEG3SE
wdenk38635852002-08-27 05:55:31 +0000446/* terminates current command, by not servicing the FIFO
447 * waits for interrupt and fills in the result bytes */
448int fdc_terminate(FDC_COMMAND_STRUCT *pCMD)
449{
450 int i;
451 for(i=0;i<100;i++)
452 udelay(500); /* wait 500usec for fifo overrun */
453 while((read_fdc_reg(FDC_SRA)&0x80)==0x00); /* wait as long as no int has occured */
454 for(i=0;i<7;i++) {
455 pCMD->result[i]=(unsigned char)read_fdc_byte();
456 }
457 return TRUE;
458}
wdenkc7de8292002-11-19 11:04:11 +0000459#endif
460#ifdef CONFIG_AMIGAONEG3SE
461int fdc_terminate(FDC_COMMAND_STRUCT *pCMD)
462{
463 int i;
464 for(i=0;i<100;i++)
465 udelay(500); /* wait 500usec for fifo overrun */
466 while((INT6_Status&0x80)==0x00); /* wait as long as no int has occured */
467 for(i=0;i<7;i++) {
468 pCMD->result[i]=(unsigned char)read_fdc_byte();
469 }
470 INT6_Status = 0;
471 return TRUE;
472}
473
474#endif
475
476#ifdef CONFIG_AMIGAONEG3SE
477#define disable_interrupts() 0
478#define enable_interrupts() (void)0
479#endif
wdenk38635852002-08-27 05:55:31 +0000480
481/* reads data from FDC, seek commands are issued automatic */
482int fdc_read_data(unsigned char *buffer, unsigned long blocks,FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
483{
484 /* first seek to start address */
485 unsigned long len,lastblk,readblk,i,timeout,ii,offset;
486 unsigned char pcn,c,retriesrw,retriescal;
487 unsigned char *bufferw; /* working buffer */
488 int sect_size;
489 int flags;
490
491 flags=disable_interrupts(); /* switch off all Interrupts */
492 select_fdc_drive(pCMD); /* switch on drive */
493 sect_size=0x080<<pFG->sect_code;
494 retriesrw=0;
495 retriescal=0;
496 offset=0;
497 if(fdc_seek(pCMD,pFG)==FALSE) {
498 stop_fdc_drive(pCMD);
499 enable_interrupts();
500 return FALSE;
501 }
502 if((pCMD->result[STATUS_0]&0x20)!=0x20) {
503 printf("Seek error Status: %02X\n",pCMD->result[STATUS_0]);
504 stop_fdc_drive(pCMD);
505 enable_interrupts();
506 return FALSE;
507 }
508 pcn=pCMD->result[STATUS_PCN]; /* current track */
509 /* now determine the next seek point */
510 lastblk=pCMD->blnr + blocks;
511 /* readblk=(pFG->head*pFG->sect)-(pCMD->blnr%(pFG->head*pFG->sect)); */
512 readblk=pFG->sect-(pCMD->blnr%pFG->sect);
513 PRINTF("1st nr of block possible read %ld start %ld\n",readblk,pCMD->blnr);
514 if(readblk>blocks) /* is end within 1st track */
515 readblk=blocks; /* yes, correct it */
516 PRINTF("we read %ld blocks start %ld\n",readblk,pCMD->blnr);
517 bufferw=&buffer[0]; /* setup working buffer */
518 do {
519retryrw:
520 len=sect_size * readblk;
521 pCMD->cmd[COMMAND]=FDC_CMD_READ;
522 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
523 stop_fdc_drive(pCMD);
524 enable_interrupts();
525 return FALSE;
526 }
527 for (i=0;i<len;i++) {
528 timeout=FDC_TIME_OUT;
529 do {
530 c=read_fdc_reg(FDC_MSR);
531 if((c&0xC0)==0xC0) {
532 bufferw[i]=read_fdc_reg(FDC_FIFO);
533 break;
534 }
535 if((c&0xC0)==0x80) { /* output */
536 PRINTF("Transfer error transfered: at %ld, MSR=%02X\n",i,c);
537 if(i>6) {
538 for(ii=0;ii<7;ii++) {
539 pCMD->result[ii]=bufferw[(i-7+ii)];
540 } /* for */
541 }
542 if(retriesrw++>FDC_RW_RETRIES) {
543 if (retriescal++>FDC_CAL_RETRIES) {
544 stop_fdc_drive(pCMD);
545 enable_interrupts();
546 return FALSE;
547 }
548 else {
549 PRINTF(" trying to recalibrate Try %d\n",retriescal);
550 if(fdc_recalibrate(pCMD,pFG)==FALSE) {
551 stop_fdc_drive(pCMD);
552 enable_interrupts();
553 return FALSE;
554 }
555 retriesrw=0;
556 goto retrycal;
557 } /* else >FDC_CAL_RETRIES */
558 }
559 else {
560 PRINTF("Read retry %d\n",retriesrw);
561 goto retryrw;
562 } /* else >FDC_RW_RETRIES */
563 }/* if output */
564 timeout--;
565 }while(TRUE);
566 } /* for len */
567 /* the last sector of a track or all data has been read,
568 * we need to get the results */
569 fdc_terminate(pCMD);
570 offset+=(sect_size*readblk); /* set up buffer pointer */
571 bufferw=&buffer[offset];
572 pCMD->blnr+=readblk; /* update current block nr */
573 blocks-=readblk; /* update blocks */
574 if(blocks==0)
575 break; /* we are finish */
576 /* setup new read blocks */
577 /* readblk=pFG->head*pFG->sect; */
578 readblk=pFG->sect;
579 if(readblk>blocks)
580 readblk=blocks;
581retrycal:
582 /* a seek is necessary */
583 if(fdc_seek(pCMD,pFG)==FALSE) {
584 stop_fdc_drive(pCMD);
585 enable_interrupts();
586 return FALSE;
587 }
588 if((pCMD->result[STATUS_0]&0x20)!=0x20) {
589 PRINTF("Seek error Status: %02X\n",pCMD->result[STATUS_0]);
590 stop_fdc_drive(pCMD);
591 return FALSE;
592 }
593 pcn=pCMD->result[STATUS_PCN]; /* current track */
594 }while(TRUE); /* start over */
595 stop_fdc_drive(pCMD); /* switch off drive */
596 enable_interrupts();
597 return TRUE;
598}
599
wdenkc7de8292002-11-19 11:04:11 +0000600#ifdef CONFIG_AMIGAONEG3SE
601#undef disable_interrupts()
602#undef enable_interrupts()
603#endif
604
wdenk38635852002-08-27 05:55:31 +0000605/* Scan all drives and check if drive is present and disk is inserted */
606int fdc_check_drive(FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
607{
608 int i,drives,state;
609 /* OK procedure of data book is satisfied.
610 * trying to get some information over the drives */
611 state=0; /* no drives, no disks */
612 for(drives=0;drives<4;drives++) {
613 pCMD->drive=drives;
614 select_fdc_drive(pCMD);
615 pCMD->blnr=0; /* set to the 1st block */
616 if(fdc_recalibrate(pCMD,pFG)==FALSE)
wdenk7f6c2cb2002-11-10 22:06:23 +0000617 continue;
wdenk38635852002-08-27 05:55:31 +0000618 if((pCMD->result[STATUS_0]&0x10)==0x10)
wdenk7f6c2cb2002-11-10 22:06:23 +0000619 continue;
wdenk38635852002-08-27 05:55:31 +0000620 /* ok drive connected check for disk */
621 state|=(1<<drives);
622 pCMD->blnr=pFG->size; /* set to the last block */
623 if(fdc_seek(pCMD,pFG)==FALSE)
wdenk7f6c2cb2002-11-10 22:06:23 +0000624 continue;
wdenk38635852002-08-27 05:55:31 +0000625 pCMD->blnr=0; /* set to the 1st block */
626 if(fdc_recalibrate(pCMD,pFG)==FALSE)
wdenk7f6c2cb2002-11-10 22:06:23 +0000627 continue;
wdenk38635852002-08-27 05:55:31 +0000628 pCMD->cmd[COMMAND]=FDC_CMD_READ_ID;
629 if(fdc_issue_cmd(pCMD,pFG)==FALSE)
wdenk7f6c2cb2002-11-10 22:06:23 +0000630 continue;
wdenk38635852002-08-27 05:55:31 +0000631 state|=(0x10<<drives);
632 }
633 stop_fdc_drive(pCMD);
634 for(i=0;i<4;i++) {
635 PRINTF("Floppy Drive %d %sconnected %sDisk inserted %s\n",i,
636 ((state&(1<<i))==(1<<i)) ? "":"not ",
637 ((state&(0x10<<i))==(0x10<<i)) ? "":"no ",
638 ((state&(0x10<<i))==(0x10<<i)) ? pFG->name : "");
639 }
640 pCMD->flags=state;
641 return TRUE;
642}
643
644
645/**************************************************************************
646* int fdc_setup
647* setup the fdc according the datasheet
648* assuming in PS2 Mode
649*/
wdenk2262cfe2002-11-18 00:14:45 +0000650int fdc_setup(int drive, FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
wdenk38635852002-08-27 05:55:31 +0000651{
wdenk38635852002-08-27 05:55:31 +0000652 int i;
wdenk7f6c2cb2002-11-10 22:06:23 +0000653
wdenkc7de8292002-11-19 11:04:11 +0000654#ifdef CONFIG_AMIGAONEG3SE
655 irq_install_handler(6, (interrupt_handler_t *)fdc_interrupt, NULL);
656 i8259_unmask_irq(6);
657#endif
658
wdenk7f6c2cb2002-11-10 22:06:23 +0000659#ifdef CFG_FDC_HW_INIT
wdenk8bde7f72003-06-27 21:31:46 +0000660 fdc_hw_init ();
wdenk7f6c2cb2002-11-10 22:06:23 +0000661#endif
wdenk38635852002-08-27 05:55:31 +0000662 /* first, we reset the FDC via the DOR */
663 write_fdc_reg(FDC_DOR,0x00);
664 for(i=0; i<255; i++) /* then we wait some time */
665 udelay(500);
666 /* then, we clear the reset in the DOR */
wdenk2262cfe2002-11-18 00:14:45 +0000667 pCMD->drive=drive;
wdenk38635852002-08-27 05:55:31 +0000668 select_fdc_drive(pCMD);
669 /* initialize the CCR */
670 write_fdc_reg(FDC_CCR,pFG->rate);
671 /* then initialize the DSR */
672 write_fdc_reg(FDC_DSR,pFG->rate);
673 if(wait_for_fdc_int()==FALSE) {
674 PRINTF("Time Out after writing CCR\n");
675 return FALSE;
676 }
677 /* now issue sense Interrupt and status command
678 * assuming only one drive present (drive 0) */
679 pCMD->dma=0; /* we don't use any dma at all */
680 for(i=0;i<4;i++) {
681 /* issue sense interrupt for all 4 possible drives */
682 pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
683 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
684 PRINTF("Sense Interrupt for drive %d failed\n",i);
685 }
686 }
wdenk2262cfe2002-11-18 00:14:45 +0000687 /* issue the configure command */
688 pCMD->drive=drive;
wdenk38635852002-08-27 05:55:31 +0000689 select_fdc_drive(pCMD);
690 pCMD->cmd[COMMAND]=FDC_CMD_CONFIGURE;
691 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
692 PRINTF(" configure timeout\n");
693 stop_fdc_drive(pCMD);
694 return FALSE;
695 }
696 /* issue specify command */
697 pCMD->cmd[COMMAND]=FDC_CMD_SPECIFY;
698 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
699 PRINTF(" specify timeout\n");
700 stop_fdc_drive(pCMD);
701 return FALSE;
702
703 }
704 /* then, we clear the reset in the DOR */
705 /* fdc_check_drive(pCMD,pFG); */
706 /* write_fdc_reg(FDC_DOR,0x04); */
wdenkc7de8292002-11-19 11:04:11 +0000707
wdenk38635852002-08-27 05:55:31 +0000708 return TRUE;
709}
wdenk2262cfe2002-11-18 00:14:45 +0000710#endif /* ((CONFIG_COMMANDS & CFG_CMD_FDC)||(CONFIG_COMMANDS & CFG_CMD_FDOS))*/
wdenk38635852002-08-27 05:55:31 +0000711
wdenk2262cfe2002-11-18 00:14:45 +0000712#if (CONFIG_COMMANDS & CFG_CMD_FDOS)
713
714/* Low level functions for the Floppy-DOS layer */
715
716/**************************************************************************
717* int fdc_fdos_init
wdenk8bde7f72003-06-27 21:31:46 +0000718* initialize the FDC layer
719*
wdenk2262cfe2002-11-18 00:14:45 +0000720*/
721int fdc_fdos_init (int drive)
722{
723 FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
724 FDC_COMMAND_STRUCT *pCMD = &cmd;
wdenk8bde7f72003-06-27 21:31:46 +0000725
wdenk2262cfe2002-11-18 00:14:45 +0000726 /* setup FDC and scan for drives */
727 if(fdc_setup(drive,pCMD,pFG)==FALSE) {
728 printf("\n** Error in setup FDC **\n");
729 return FALSE;
730 }
731 if(fdc_check_drive(pCMD,pFG)==FALSE) {
732 printf("\n** Error in check_drives **\n");
733 return FALSE;
734 }
735 if((pCMD->flags&(1<<drive))==0) {
736 /* drive not available */
737 printf("\n** Drive %d not available **\n",drive);
738 return FALSE;
739 }
740 if((pCMD->flags&(0x10<<drive))==0) {
741 /* no disk inserted */
742 printf("\n** No disk inserted in drive %d **\n",drive);
743 return FALSE;
744 }
745 /* ok, we have a valid source */
746 pCMD->drive=drive;
747
748 /* read first block */
749 pCMD->blnr=0;
wdenk8bde7f72003-06-27 21:31:46 +0000750 return TRUE;
wdenk2262cfe2002-11-18 00:14:45 +0000751}
752/**************************************************************************
753* int fdc_fdos_seek
wdenk8bde7f72003-06-27 21:31:46 +0000754* parameter is a block number
wdenk2262cfe2002-11-18 00:14:45 +0000755*/
756int fdc_fdos_seek (int where)
757{
758 FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
759 FDC_COMMAND_STRUCT *pCMD = &cmd;
760
wdenk8bde7f72003-06-27 21:31:46 +0000761 pCMD -> blnr = where ;
762 return (fdc_seek (pCMD, pFG));
wdenk2262cfe2002-11-18 00:14:45 +0000763}
764/**************************************************************************
765* int fdc_fdos_read
766* the length is in block number
767*/
768int fdc_fdos_read (void *buffer, int len)
769{
770 FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
771 FDC_COMMAND_STRUCT *pCMD = &cmd;
772
wdenk8bde7f72003-06-27 21:31:46 +0000773 return (fdc_read_data (buffer, len, pCMD, pFG));
wdenk2262cfe2002-11-18 00:14:45 +0000774}
775#endif /* (CONFIG_COMMANDS & CFG_CMD_FDOS) */
776
777#if (CONFIG_COMMANDS & CFG_CMD_FDC)
wdenk38635852002-08-27 05:55:31 +0000778/****************************************************************************
779 * main routine do_fdcboot
780 */
781int do_fdcboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
782{
783 FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
784 FDC_COMMAND_STRUCT *pCMD = &cmd;
wdenk8bde7f72003-06-27 21:31:46 +0000785 unsigned long addr,imsize;
wdenk38635852002-08-27 05:55:31 +0000786 image_header_t *hdr; /* used for fdc boot */
787 unsigned char boot_drive;
788 int i,nrofblk;
789 char *ep;
790 int rcode = 0;
791
792 switch (argc) {
793 case 1:
794 addr = CFG_LOAD_ADDR;
wdenk8bde7f72003-06-27 21:31:46 +0000795 boot_drive=CFG_FDC_DRIVE_NUMBER;
wdenk38635852002-08-27 05:55:31 +0000796 break;
797 case 2:
798 addr = simple_strtoul(argv[1], NULL, 16);
wdenk7f6c2cb2002-11-10 22:06:23 +0000799 boot_drive=CFG_FDC_DRIVE_NUMBER;
wdenk38635852002-08-27 05:55:31 +0000800 break;
801 case 3:
802 addr = simple_strtoul(argv[1], NULL, 16);
803 boot_drive=simple_strtoul(argv[2], NULL, 10);
804 break;
805 default:
806 printf ("Usage:\n%s\n", cmdtp->usage);
807 return 1;
808 }
809 /* setup FDC and scan for drives */
wdenk2262cfe2002-11-18 00:14:45 +0000810 if(fdc_setup(boot_drive,pCMD,pFG)==FALSE) {
wdenk38635852002-08-27 05:55:31 +0000811 printf("\n** Error in setup FDC **\n");
812 return 1;
813 }
814 if(fdc_check_drive(pCMD,pFG)==FALSE) {
815 printf("\n** Error in check_drives **\n");
816 return 1;
817 }
818 if((pCMD->flags&(1<<boot_drive))==0) {
819 /* drive not available */
820 printf("\n** Drive %d not availabe **\n",boot_drive);
821 return 1;
822 }
823 if((pCMD->flags&(0x10<<boot_drive))==0) {
824 /* no disk inserted */
825 printf("\n** No disk inserted in drive %d **\n",boot_drive);
826 return 1;
827 }
828 /* ok, we have a valid source */
829 pCMD->drive=boot_drive;
830 /* read first block */
831 pCMD->blnr=0;
832 if(fdc_read_data((unsigned char *)addr,1,pCMD,pFG)==FALSE) {
833 printf("\nRead error:");
834 for(i=0;i<7;i++)
835 printf("result%d: 0x%02X\n",i,pCMD->result[i]);
836 return 1;
837 }
838 hdr = (image_header_t *)addr;
Wolfgang Denkdc013d42006-03-12 01:59:35 +0100839 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
wdenk38635852002-08-27 05:55:31 +0000840 printf ("Bad Magic Number\n");
841 return 1;
842 }
843 print_image_hdr(hdr);
844
Wolfgang Denkdc013d42006-03-12 01:59:35 +0100845 imsize= ntohl(hdr->ih_size)+sizeof(image_header_t);
wdenk38635852002-08-27 05:55:31 +0000846 nrofblk=imsize/512;
847 if((imsize%512)>0)
848 nrofblk++;
849 printf("Loading %ld Bytes (%d blocks) at 0x%08lx..\n",imsize,nrofblk,addr);
850 pCMD->blnr=0;
851 if(fdc_read_data((unsigned char *)addr,nrofblk,pCMD,pFG)==FALSE) {
852 /* read image block */
853 printf("\nRead error:");
854 for(i=0;i<7;i++)
855 printf("result%d: 0x%02X\n",i,pCMD->result[i]);
856 return 1;
857 }
858 printf("OK %ld Bytes loaded.\n",imsize);
859
860 flush_cache (addr, imsize);
861 /* Loading ok, update default load address */
862
863 load_addr = addr;
864 if(hdr->ih_type == IH_TYPE_KERNEL) {
865 /* Check if we should attempt an auto-start */
866 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
867 char *local_args[2];
868 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
869
870 local_args[0] = argv[0];
871 local_args[1] = NULL;
872
873 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
874
875 do_bootm (cmdtp, 0, 1, local_args);
876 rcode ++;
877 }
878 }
879 return rcode;
880}
881
882
wdenk38635852002-08-27 05:55:31 +0000883#endif /* CONFIG_COMMANDS & CFG_CMD_FDC */
884
885
wdenk8bde7f72003-06-27 21:31:46 +0000886/***************************************************/
887
888
889#if (CONFIG_COMMANDS & CFG_CMD_FDC)
890
wdenk0d498392003-07-01 21:06:45 +0000891U_BOOT_CMD(
892 fdcboot, 3, 1, do_fdcboot,
wdenk8bde7f72003-06-27 21:31:46 +0000893 "fdcboot - boot from floppy device\n",
894 "loadAddr drive\n"
895);
896#endif