blob: 3ee342fcfc06b838dd9e6b60fbd50b6a7848f627 [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
50#if (CONFIG_COMMANDS & CFG_CMD_DATE)
51#include <rtc.h>
52#endif
53
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
214
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);
wdenk7f6c2cb2002-11-10 22:06:23 +0000223
224 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{
wdenk2262cfe2002-11-18 00:14:45 +0000230 volatile unsigned char *tmp =
231 (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
wdenkc7de8292002-11-19 11:04:11 +0000645
wdenk38635852002-08-27 05:55:31 +0000646/**************************************************************************
647* int fdc_setup
648* setup the fdc according the datasheet
649* assuming in PS2 Mode
650*/
wdenk2262cfe2002-11-18 00:14:45 +0000651int fdc_setup(int drive, FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
wdenk38635852002-08-27 05:55:31 +0000652{
wdenk38635852002-08-27 05:55:31 +0000653 int i;
wdenk7f6c2cb2002-11-10 22:06:23 +0000654
wdenkc7de8292002-11-19 11:04:11 +0000655#ifdef CONFIG_AMIGAONEG3SE
656 irq_install_handler(6, (interrupt_handler_t *)fdc_interrupt, NULL);
657 i8259_unmask_irq(6);
658#endif
659
wdenk7f6c2cb2002-11-10 22:06:23 +0000660#ifdef CFG_FDC_HW_INIT
661 fdc_hw_init ();
662#endif
wdenk38635852002-08-27 05:55:31 +0000663 /* 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 */
wdenk2262cfe2002-11-18 00:14:45 +0000668 pCMD->drive=drive;
wdenk38635852002-08-27 05:55:31 +0000669 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 }
wdenk2262cfe2002-11-18 00:14:45 +0000688 /* issue the configure command */
689 pCMD->drive=drive;
wdenk38635852002-08-27 05:55:31 +0000690 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); */
wdenkc7de8292002-11-19 11:04:11 +0000708
wdenk38635852002-08-27 05:55:31 +0000709 return TRUE;
710}
wdenk2262cfe2002-11-18 00:14:45 +0000711#endif /* ((CONFIG_COMMANDS & CFG_CMD_FDC)||(CONFIG_COMMANDS & CFG_CMD_FDOS))*/
wdenk38635852002-08-27 05:55:31 +0000712
wdenk2262cfe2002-11-18 00:14:45 +0000713#if (CONFIG_COMMANDS & CFG_CMD_FDOS)
714
715/* Low level functions for the Floppy-DOS layer */
716
717/**************************************************************************
718* int fdc_fdos_init
719* initialize the FDC layer
720*
721*/
722int fdc_fdos_init (int drive)
723{
724 FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
725 FDC_COMMAND_STRUCT *pCMD = &cmd;
726
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;
751 return TRUE;
752}
753/**************************************************************************
754* int fdc_fdos_seek
755* parameter is a block number
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
762 pCMD -> blnr = where ;
763 return (fdc_seek (pCMD, pFG));
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
774 return (fdc_read_data (buffer, len, pCMD, pFG));
775}
776#endif /* (CONFIG_COMMANDS & CFG_CMD_FDOS) */
777
778#if (CONFIG_COMMANDS & CFG_CMD_FDC)
wdenk38635852002-08-27 05:55:31 +0000779/****************************************************************************
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;
786 unsigned long addr,imsize;
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;
wdenk7f6c2cb2002-11-10 22:06:23 +0000796 boot_drive=CFG_FDC_DRIVE_NUMBER;
wdenk38635852002-08-27 05:55:31 +0000797 break;
798 case 2:
799 addr = simple_strtoul(argv[1], NULL, 16);
wdenk7f6c2cb2002-11-10 22:06:23 +0000800 boot_drive=CFG_FDC_DRIVE_NUMBER;
wdenk38635852002-08-27 05:55:31 +0000801 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 */
wdenk2262cfe2002-11-18 00:14:45 +0000811 if(fdc_setup(boot_drive,pCMD,pFG)==FALSE) {
wdenk38635852002-08-27 05:55:31 +0000812 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;
840 if (hdr->ih_magic != IH_MAGIC) {
841 printf ("Bad Magic Number\n");
842 return 1;
843 }
844 print_image_hdr(hdr);
845
846 imsize= hdr->ih_size+sizeof(image_header_t);
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
884
885#endif /* CONFIG_COMMANDS & CFG_CMD_FDC */
886
887