blob: 8493defe93473dfd13f78b5aa211ddb621695478 [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
Jon Loeligerbaa26db2007-07-08 17:51:39 -050049/*#if defined(CONFIG_CMD_DATE) */
wdenk8bde7f72003-06-27 21:31:46 +000050/*#include <rtc.h> */
51/*#endif */
wdenk38635852002-08-27 05:55:31 +000052
Jon Loeligerbaa26db2007-07-08 17:51:39 -050053#if defined(CONFIG_CMD_FDC) || defined(CONFIG_CMD_FDOS)
wdenk38635852002-08-27 05:55:31 +000054
wdenk38635852002-08-27 05:55:31 +000055typedef struct {
Wolfgang Denk53677ef2008-05-20 16:00:29 +020056 int flags; /* connected drives ect */
57 unsigned long blnr; /* Logical block nr */
58 uchar drive; /* drive no */
59 uchar cmdlen; /* cmd length */
60 uchar cmd[16]; /* cmd desc */
61 uchar dma; /* if > 0 dma enabled */
62 uchar result[11]; /* status information */
63 uchar resultlen; /* lenght of result */
wdenk38635852002-08-27 05:55:31 +000064} FDC_COMMAND_STRUCT;
Wolfgang Denk53677ef2008-05-20 16:00:29 +020065
wdenk38635852002-08-27 05:55:31 +000066/* flags: only the lower 8bit used:
67 * bit 0 if set drive 0 is present
68 * bit 1 if set drive 1 is present
69 * bit 2 if set drive 2 is present
70 * bit 3 if set drive 3 is present
71 * bit 4 if set disk in drive 0 is inserted
72 * bit 5 if set disk in drive 1 is inserted
73 * bit 6 if set disk in drive 2 is inserted
74 * bit 7 if set disk in drive 4 is inserted
75 */
76
wdenk38635852002-08-27 05:55:31 +000077/* cmd indexes */
Wolfgang Denk53677ef2008-05-20 16:00:29 +020078#define COMMAND 0
79#define DRIVE 1
wdenk38635852002-08-27 05:55:31 +000080#define CONFIG0 1
Wolfgang Denk53677ef2008-05-20 16:00:29 +020081#define SPEC_HUTSRT 1
82#define TRACK 2
wdenk38635852002-08-27 05:55:31 +000083#define CONFIG1 2
84#define SPEC_HLT 2
Wolfgang Denk53677ef2008-05-20 16:00:29 +020085#define HEAD 3
wdenk38635852002-08-27 05:55:31 +000086#define CONFIG2 3
Wolfgang Denk53677ef2008-05-20 16:00:29 +020087#define SECTOR 4
88#define SECTOR_SIZE 5
89#define LAST_TRACK 6
90#define GAP 7
91#define DTL 8
wdenk38635852002-08-27 05:55:31 +000092/* result indexes */
Wolfgang Denk53677ef2008-05-20 16:00:29 +020093#define STATUS_0 0
94#define STATUS_PCN 1
95#define STATUS_1 1
96#define STATUS_2 2
97#define STATUS_TRACK 3
98#define STATUS_HEAD 4
99#define STATUS_SECT 5
100#define STATUS_SECT_SIZE 6
wdenk38635852002-08-27 05:55:31 +0000101
102
103/* Register addresses */
104#define FDC_BASE 0x3F0
105#define FDC_SRA FDC_BASE + 0 /* Status Register A */
106#define FDC_SRB FDC_BASE + 1 /* Status Register B */
107#define FDC_DOR FDC_BASE + 2 /* Digital Output Register */
108#define FDC_TDR FDC_BASE + 3 /* Tape Drive Register */
109#define FDC_DSR FDC_BASE + 4 /* Data rate Register */
110#define FDC_MSR FDC_BASE + 4 /* Main Status Register */
111#define FDC_FIFO FDC_BASE + 5 /* FIFO */
112#define FDC_DIR FDC_BASE + 6 /* Digital Input Register */
113#define FDC_CCR FDC_BASE + 7 /* Configuration Control */
114/* Commands */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200115#define FDC_CMD_SENSE_INT 0x08
116#define FDC_CMD_CONFIGURE 0x13
117#define FDC_CMD_SPECIFY 0x03
118#define FDC_CMD_RECALIBRATE 0x07
119#define FDC_CMD_READ 0x06
120#define FDC_CMD_READ_TRACK 0x02
121#define FDC_CMD_READ_ID 0x0A
122#define FDC_CMD_DUMP_REG 0x0E
123#define FDC_CMD_SEEK 0x0F
wdenk38635852002-08-27 05:55:31 +0000124
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200125#define FDC_CMD_SENSE_INT_LEN 0x01
126#define FDC_CMD_CONFIGURE_LEN 0x04
127#define FDC_CMD_SPECIFY_LEN 0x03
128#define FDC_CMD_RECALIBRATE_LEN 0x02
129#define FDC_CMD_READ_LEN 0x09
130#define FDC_CMD_READ_TRACK_LEN 0x09
131#define FDC_CMD_READ_ID_LEN 0x02
132#define FDC_CMD_DUMP_REG_LEN 0x01
133#define FDC_CMD_SEEK_LEN 0x03
wdenk38635852002-08-27 05:55:31 +0000134
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200135#define FDC_FIFO_THR 0x0C
136#define FDC_FIFO_DIS 0x00
wdenk38635852002-08-27 05:55:31 +0000137#define FDC_IMPLIED_SEEK 0x01
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200138#define FDC_POLL_DIS 0x00
139#define FDC_PRE_TRK 0x00
140#define FDC_CONFIGURE FDC_FIFO_THR | (FDC_POLL_DIS<<4) | (FDC_FIFO_DIS<<5) | (FDC_IMPLIED_SEEK << 6)
141#define FDC_MFM_MODE 0x01 /* MFM enable */
142#define FDC_SKIP_MODE 0x00 /* skip enable */
wdenk38635852002-08-27 05:55:31 +0000143
144#define FDC_TIME_OUT 100000 /* time out */
145#define FDC_RW_RETRIES 3 /* read write retries */
146#define FDC_CAL_RETRIES 3 /* calibration and seek retries */
147
148
149/* Disk structure */
150typedef struct {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200151 unsigned int size; /* nr of sectors total */
152 unsigned int sect; /* sectors per track */
153 unsigned int head; /* nr of heads */
154 unsigned int track; /* nr of tracks */
155 unsigned int stretch; /* !=0 means double track steps */
156 unsigned char gap; /* gap1 size */
157 unsigned char rate; /* data rate. |= 0x40 for perpendicular */
158 unsigned char spec1; /* stepping rate, head unload time */
159 unsigned char fmt_gap;/* gap2 size */
160 unsigned char hlt; /* head load time */
161 unsigned char sect_code;/* Sector Size code */
162 const char * name; /* used only for predefined formats */
wdenk38635852002-08-27 05:55:31 +0000163} FD_GEO_STRUCT;
164
165
166/* supported Floppy types (currently only one) */
167const static FD_GEO_STRUCT floppy_type[2] = {
168 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,16,2,"H1440" }, /* 7 1.44MB 3.5" */
169 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00, 0,0,NULL }, /* end of table */
170};
171
172static FDC_COMMAND_STRUCT cmd; /* global command struct */
173
wdenk7f6c2cb2002-11-10 22:06:23 +0000174/* If the boot drive number is undefined, we assume it's drive 0 */
175#ifndef CFG_FDC_DRIVE_NUMBER
176#define CFG_FDC_DRIVE_NUMBER 0
177#endif
178
179/* Hardware access */
180#ifndef CFG_ISA_IO_STRIDE
181#define CFG_ISA_IO_STRIDE 1
182#endif
183
184#ifndef CFG_ISA_IO_OFFSET
185#define CFG_ISA_IO_OFFSET 0
186#endif
187
188
wdenkc7de8292002-11-19 11:04:11 +0000189#ifdef CONFIG_AMIGAONEG3SE
190unsigned char INT6_Status;
191
192void fdc_interrupt(void)
193{
194 INT6_Status = 0x80;
195}
196
197/* waits for an interrupt (polling) */
198int wait_for_fdc_int(void)
199{
200 unsigned long timeout;
201 timeout = FDC_TIME_OUT;
202 while(((volatile)INT6_Status & 0x80) == 0) {
203 timeout--;
204 udelay(10);
205 if(timeout == 0) /* timeout occured */
206 return FALSE;
207 }
208 INT6_Status = 0;
209 return TRUE;
210}
211#endif
wdenk8bde7f72003-06-27 21:31:46 +0000212
wdenk38635852002-08-27 05:55:31 +0000213/* Supporting Functions */
214/* reads a Register of the FDC */
215unsigned char read_fdc_reg(unsigned int addr)
216{
wdenk2262cfe2002-11-18 00:14:45 +0000217 volatile unsigned char *val =
218 (volatile unsigned char *)(CFG_ISA_IO_BASE_ADDRESS +
219 (addr * CFG_ISA_IO_STRIDE) +
220 CFG_ISA_IO_OFFSET);
wdenk8bde7f72003-06-27 21:31:46 +0000221
wdenk7f6c2cb2002-11-10 22:06:23 +0000222 return val [0];
wdenk38635852002-08-27 05:55:31 +0000223}
224
225/* writes a Register of the FDC */
226void write_fdc_reg(unsigned int addr, unsigned char val)
227{
wdenk8bde7f72003-06-27 21:31:46 +0000228 volatile unsigned char *tmp =
wdenk2262cfe2002-11-18 00:14:45 +0000229 (volatile unsigned char *)(CFG_ISA_IO_BASE_ADDRESS +
230 (addr * CFG_ISA_IO_STRIDE) +
231 CFG_ISA_IO_OFFSET);
wdenk7f6c2cb2002-11-10 22:06:23 +0000232 tmp[0]=val;
wdenk38635852002-08-27 05:55:31 +0000233}
234
wdenkc7de8292002-11-19 11:04:11 +0000235#ifndef CONFIG_AMIGAONEG3SE
wdenk38635852002-08-27 05:55:31 +0000236/* waits for an interrupt (polling) */
237int wait_for_fdc_int(void)
238{
239 unsigned long timeout;
240 timeout = FDC_TIME_OUT;
241 while((read_fdc_reg(FDC_SRA)&0x80)==0) {
242 timeout--;
243 udelay(10);
244 if(timeout==0) /* timeout occured */
245 return FALSE;
246 }
247 return TRUE;
248}
249
wdenkc7de8292002-11-19 11:04:11 +0000250#endif
wdenk38635852002-08-27 05:55:31 +0000251
252/* reads a byte from the FIFO of the FDC and checks direction and RQM bit
253 of the MSR. returns -1 if timeout, or byte if ok */
254int read_fdc_byte(void)
255{
256 unsigned long timeout;
257 timeout = FDC_TIME_OUT;
258 while((read_fdc_reg(FDC_MSR)&0xC0)!=0xC0) {
259 /* direction out and ready */
260 udelay(10);
261 timeout--;
262 if(timeout==0) /* timeout occured */
263 return -1;
264 }
265 return read_fdc_reg(FDC_FIFO);
266}
267
268/* if the direction of the FIFO is wrong, this routine is used to
269 empty the FIFO. Should _not_ be used */
270int fdc_need_more_output(void)
271{
272 unsigned char c;
273 while((read_fdc_reg(FDC_MSR)&0xC0)==0xC0) {
274 c=(unsigned char)read_fdc_byte();
275 printf("Error: more output: %x\n",c);
276 }
277 return TRUE;
278}
279
280
281/* writes a byte to the FIFO of the FDC and checks direction and RQM bit
282 of the MSR */
283int write_fdc_byte(unsigned char val)
284{
285 unsigned long timeout;
286 timeout = FDC_TIME_OUT;
287 while((read_fdc_reg(FDC_MSR)&0xC0)!=0x80) {
288 /* direction in and ready for byte */
289 timeout--;
290 udelay(10);
291 fdc_need_more_output();
292 if(timeout==0) /* timeout occured */
293 return FALSE;
294 }
295 write_fdc_reg(FDC_FIFO,val);
296 return TRUE;
297}
298
299/* sets up all FDC commands and issues it to the FDC. If
300 the command causes direct results (no Execution Phase)
301 the result is be read as well. */
302
303int fdc_issue_cmd(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
304{
305 int i;
306 unsigned long head,track,sect,timeout;
307 track = pCMD->blnr / (pFG->sect * pFG->head); /* track nr */
308 sect = pCMD->blnr % (pFG->sect * pFG->head); /* remaining blocks */
309 head = sect / pFG->sect; /* head nr */
310 sect = sect % pFG->sect; /* remaining blocks */
311 sect++; /* sectors are 1 based */
wdenk2262cfe2002-11-18 00:14:45 +0000312 PRINTF("Cmd 0x%02x Track %ld, Head %ld, Sector %ld, Drive %d (blnr %ld)\n",
313 pCMD->cmd[0],track,head,sect,pCMD->drive,pCMD->blnr);
wdenk7f6c2cb2002-11-10 22:06:23 +0000314
wdenk38635852002-08-27 05:55:31 +0000315 if(head|=0) { /* max heads = 2 */
316 pCMD->cmd[DRIVE]=pCMD->drive | 0x04; /* head 1 */
317 pCMD->cmd[HEAD]=(unsigned char) head; /* head register */
318 }
319 else {
320 pCMD->cmd[DRIVE]=pCMD->drive; /* head 0 */
321 pCMD->cmd[HEAD]=(unsigned char) head; /* head register */
322 }
323 pCMD->cmd[TRACK]=(unsigned char) track; /* track */
324 switch (pCMD->cmd[COMMAND]) {
325 case FDC_CMD_READ:
326 pCMD->cmd[SECTOR]=(unsigned char) sect; /* sector */
327 pCMD->cmd[SECTOR_SIZE]=pFG->sect_code; /* sector size code */
328 pCMD->cmd[LAST_TRACK]=pFG->sect; /* End of track */
329 pCMD->cmd[GAP]=pFG->gap; /* gap */
330 pCMD->cmd[DTL]=0xFF; /* DTL */
331 pCMD->cmdlen=FDC_CMD_READ_LEN;
332 pCMD->cmd[COMMAND]|=(FDC_MFM_MODE<<6); /* set MFM bit */
333 pCMD->cmd[COMMAND]|=(FDC_SKIP_MODE<<5); /* set Skip bit */
334 pCMD->resultlen=0; /* result only after execution */
335 break;
336 case FDC_CMD_SEEK:
337 pCMD->cmdlen=FDC_CMD_SEEK_LEN;
338 pCMD->resultlen=0; /* no result */
339 break;
340 case FDC_CMD_CONFIGURE:
341 pCMD->cmd[CONFIG0]=0;
342 pCMD->cmd[CONFIG1]=FDC_CONFIGURE; /* FIFO Threshold, Poll, Enable FIFO */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200343 pCMD->cmd[CONFIG2]=FDC_PRE_TRK; /* Precompensation Track */
wdenk38635852002-08-27 05:55:31 +0000344 pCMD->cmdlen=FDC_CMD_CONFIGURE_LEN;
345 pCMD->resultlen=0; /* no result */
346 break;
347 case FDC_CMD_SPECIFY:
348 pCMD->cmd[SPEC_HUTSRT]=pFG->spec1;
349 pCMD->cmd[SPEC_HLT]=(pFG->hlt)<<1; /* head load time */
350 if(pCMD->dma==0)
351 pCMD->cmd[SPEC_HLT]|=0x1; /* no dma */
352 pCMD->cmdlen=FDC_CMD_SPECIFY_LEN;
353 pCMD->resultlen=0; /* no result */
354 break;
355 case FDC_CMD_DUMP_REG:
356 pCMD->cmdlen=FDC_CMD_DUMP_REG_LEN;
357 pCMD->resultlen=10; /* 10 byte result */
358 break;
359 case FDC_CMD_READ_ID:
360 pCMD->cmd[COMMAND]|=(FDC_MFM_MODE<<6); /* set MFM bit */
361 pCMD->cmdlen=FDC_CMD_READ_ID_LEN;
362 pCMD->resultlen=7; /* 7 byte result */
363 break;
364 case FDC_CMD_RECALIBRATE:
365 pCMD->cmd[DRIVE]&=0x03; /* don't set the head bit */
366 pCMD->cmdlen=FDC_CMD_RECALIBRATE_LEN;
367 pCMD->resultlen=0; /* no result */
368 break;
369 break;
370 case FDC_CMD_SENSE_INT:
371 pCMD->cmdlen=FDC_CMD_SENSE_INT_LEN;
372 pCMD->resultlen=2;
373 break;
374 }
375 for(i=0;i<pCMD->cmdlen;i++) {
376 /* PRINTF("write cmd%d = 0x%02X\n",i,pCMD->cmd[i]); */
377 if(write_fdc_byte(pCMD->cmd[i])==FALSE) {
378 PRINTF("Error: timeout while issue cmd%d\n",i);
379 return FALSE;
380 }
381 }
382 timeout=FDC_TIME_OUT;
383 for(i=0;i<pCMD->resultlen;i++) {
384 while((read_fdc_reg(FDC_MSR)&0xC0)!=0xC0) {
385 timeout--;
386 if(timeout==0) {
387 PRINTF(" timeout while reading result%d MSR=0x%02X\n",i,read_fdc_reg(FDC_MSR));
388 return FALSE;
389 }
390 }
391 pCMD->result[i]=(unsigned char)read_fdc_byte();
392 }
393 return TRUE;
394}
395
396/* selects the drive assigned in the cmd structur and
397 switches on the Motor */
398void select_fdc_drive(FDC_COMMAND_STRUCT *pCMD)
399{
400 unsigned char val;
401
402 val=(1<<(4+pCMD->drive))|pCMD->drive|0xC; /* set reset, dma gate and motor bits */
403 if((read_fdc_reg(FDC_DOR)&val)!=val) {
404 write_fdc_reg(FDC_DOR,val);
405 for(val=0;val<255;val++)
406 udelay(500); /* wait some time to start motor */
407 }
408}
409
410/* switches off the Motor of the specified drive */
411void stop_fdc_drive(FDC_COMMAND_STRUCT *pCMD)
412{
413 unsigned char val;
414
415 val=(1<<(4+pCMD->drive))|pCMD->drive; /* sets motor bits */
416 write_fdc_reg(FDC_DOR,(read_fdc_reg(FDC_DOR)&~val));
417}
418
419/* issues a recalibrate command, waits for interrupt and
420 * issues a sense_interrupt */
421int fdc_recalibrate(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
422{
423 pCMD->cmd[COMMAND]=FDC_CMD_RECALIBRATE;
424 if(fdc_issue_cmd(pCMD,pFG)==FALSE)
425 return FALSE;
426 while(wait_for_fdc_int()!=TRUE);
427 pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
428 return(fdc_issue_cmd(pCMD,pFG));
429}
430
431/* issues a recalibrate command, waits for interrupt and
432 * issues a sense_interrupt */
433int fdc_seek(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
434{
435 pCMD->cmd[COMMAND]=FDC_CMD_SEEK;
436 if(fdc_issue_cmd(pCMD,pFG)==FALSE)
437 return FALSE;
438 while(wait_for_fdc_int()!=TRUE);
439 pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
440 return(fdc_issue_cmd(pCMD,pFG));
441}
442
wdenkc7de8292002-11-19 11:04:11 +0000443#ifndef CONFIG_AMIGAONEG3SE
wdenk38635852002-08-27 05:55:31 +0000444/* terminates current command, by not servicing the FIFO
445 * waits for interrupt and fills in the result bytes */
446int fdc_terminate(FDC_COMMAND_STRUCT *pCMD)
447{
448 int i;
449 for(i=0;i<100;i++)
450 udelay(500); /* wait 500usec for fifo overrun */
451 while((read_fdc_reg(FDC_SRA)&0x80)==0x00); /* wait as long as no int has occured */
452 for(i=0;i<7;i++) {
453 pCMD->result[i]=(unsigned char)read_fdc_byte();
454 }
455 return TRUE;
456}
wdenkc7de8292002-11-19 11:04:11 +0000457#endif
458#ifdef CONFIG_AMIGAONEG3SE
459int fdc_terminate(FDC_COMMAND_STRUCT *pCMD)
460{
461 int i;
462 for(i=0;i<100;i++)
463 udelay(500); /* wait 500usec for fifo overrun */
464 while((INT6_Status&0x80)==0x00); /* wait as long as no int has occured */
465 for(i=0;i<7;i++) {
466 pCMD->result[i]=(unsigned char)read_fdc_byte();
467 }
468 INT6_Status = 0;
469 return TRUE;
470}
471
472#endif
473
474#ifdef CONFIG_AMIGAONEG3SE
475#define disable_interrupts() 0
476#define enable_interrupts() (void)0
477#endif
wdenk38635852002-08-27 05:55:31 +0000478
479/* reads data from FDC, seek commands are issued automatic */
480int fdc_read_data(unsigned char *buffer, unsigned long blocks,FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
481{
482 /* first seek to start address */
483 unsigned long len,lastblk,readblk,i,timeout,ii,offset;
484 unsigned char pcn,c,retriesrw,retriescal;
485 unsigned char *bufferw; /* working buffer */
486 int sect_size;
487 int flags;
488
489 flags=disable_interrupts(); /* switch off all Interrupts */
490 select_fdc_drive(pCMD); /* switch on drive */
491 sect_size=0x080<<pFG->sect_code;
492 retriesrw=0;
493 retriescal=0;
494 offset=0;
495 if(fdc_seek(pCMD,pFG)==FALSE) {
496 stop_fdc_drive(pCMD);
497 enable_interrupts();
498 return FALSE;
499 }
500 if((pCMD->result[STATUS_0]&0x20)!=0x20) {
501 printf("Seek error Status: %02X\n",pCMD->result[STATUS_0]);
502 stop_fdc_drive(pCMD);
503 enable_interrupts();
504 return FALSE;
505 }
506 pcn=pCMD->result[STATUS_PCN]; /* current track */
507 /* now determine the next seek point */
508 lastblk=pCMD->blnr + blocks;
509 /* readblk=(pFG->head*pFG->sect)-(pCMD->blnr%(pFG->head*pFG->sect)); */
510 readblk=pFG->sect-(pCMD->blnr%pFG->sect);
511 PRINTF("1st nr of block possible read %ld start %ld\n",readblk,pCMD->blnr);
512 if(readblk>blocks) /* is end within 1st track */
513 readblk=blocks; /* yes, correct it */
514 PRINTF("we read %ld blocks start %ld\n",readblk,pCMD->blnr);
Wolfgang Denkd0ff51b2008-07-14 15:19:07 +0200515 bufferw = &buffer[0]; /* setup working buffer */
wdenk38635852002-08-27 05:55:31 +0000516 do {
517retryrw:
518 len=sect_size * readblk;
519 pCMD->cmd[COMMAND]=FDC_CMD_READ;
520 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
521 stop_fdc_drive(pCMD);
522 enable_interrupts();
523 return FALSE;
524 }
525 for (i=0;i<len;i++) {
526 timeout=FDC_TIME_OUT;
527 do {
528 c=read_fdc_reg(FDC_MSR);
529 if((c&0xC0)==0xC0) {
530 bufferw[i]=read_fdc_reg(FDC_FIFO);
531 break;
532 }
533 if((c&0xC0)==0x80) { /* output */
534 PRINTF("Transfer error transfered: at %ld, MSR=%02X\n",i,c);
535 if(i>6) {
536 for(ii=0;ii<7;ii++) {
537 pCMD->result[ii]=bufferw[(i-7+ii)];
538 } /* for */
539 }
540 if(retriesrw++>FDC_RW_RETRIES) {
541 if (retriescal++>FDC_CAL_RETRIES) {
542 stop_fdc_drive(pCMD);
543 enable_interrupts();
544 return FALSE;
545 }
546 else {
547 PRINTF(" trying to recalibrate Try %d\n",retriescal);
548 if(fdc_recalibrate(pCMD,pFG)==FALSE) {
549 stop_fdc_drive(pCMD);
550 enable_interrupts();
551 return FALSE;
552 }
553 retriesrw=0;
554 goto retrycal;
555 } /* else >FDC_CAL_RETRIES */
556 }
557 else {
558 PRINTF("Read retry %d\n",retriesrw);
559 goto retryrw;
560 } /* else >FDC_RW_RETRIES */
561 }/* if output */
562 timeout--;
563 }while(TRUE);
564 } /* for len */
565 /* the last sector of a track or all data has been read,
566 * we need to get the results */
567 fdc_terminate(pCMD);
568 offset+=(sect_size*readblk); /* set up buffer pointer */
Wolfgang Denkd0ff51b2008-07-14 15:19:07 +0200569 bufferw = &buffer[offset];
wdenk38635852002-08-27 05:55:31 +0000570 pCMD->blnr+=readblk; /* update current block nr */
571 blocks-=readblk; /* update blocks */
572 if(blocks==0)
573 break; /* we are finish */
574 /* setup new read blocks */
575 /* readblk=pFG->head*pFG->sect; */
576 readblk=pFG->sect;
577 if(readblk>blocks)
578 readblk=blocks;
579retrycal:
580 /* a seek is necessary */
581 if(fdc_seek(pCMD,pFG)==FALSE) {
582 stop_fdc_drive(pCMD);
583 enable_interrupts();
584 return FALSE;
585 }
586 if((pCMD->result[STATUS_0]&0x20)!=0x20) {
587 PRINTF("Seek error Status: %02X\n",pCMD->result[STATUS_0]);
588 stop_fdc_drive(pCMD);
589 return FALSE;
590 }
591 pcn=pCMD->result[STATUS_PCN]; /* current track */
592 }while(TRUE); /* start over */
593 stop_fdc_drive(pCMD); /* switch off drive */
594 enable_interrupts();
595 return TRUE;
596}
597
wdenkc7de8292002-11-19 11:04:11 +0000598#ifdef CONFIG_AMIGAONEG3SE
599#undef disable_interrupts()
600#undef enable_interrupts()
601#endif
602
wdenk38635852002-08-27 05:55:31 +0000603/* Scan all drives and check if drive is present and disk is inserted */
604int fdc_check_drive(FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
605{
606 int i,drives,state;
607 /* OK procedure of data book is satisfied.
608 * trying to get some information over the drives */
609 state=0; /* no drives, no disks */
610 for(drives=0;drives<4;drives++) {
611 pCMD->drive=drives;
612 select_fdc_drive(pCMD);
613 pCMD->blnr=0; /* set to the 1st block */
614 if(fdc_recalibrate(pCMD,pFG)==FALSE)
wdenk7f6c2cb2002-11-10 22:06:23 +0000615 continue;
wdenk38635852002-08-27 05:55:31 +0000616 if((pCMD->result[STATUS_0]&0x10)==0x10)
wdenk7f6c2cb2002-11-10 22:06:23 +0000617 continue;
wdenk38635852002-08-27 05:55:31 +0000618 /* ok drive connected check for disk */
619 state|=(1<<drives);
620 pCMD->blnr=pFG->size; /* set to the last block */
621 if(fdc_seek(pCMD,pFG)==FALSE)
wdenk7f6c2cb2002-11-10 22:06:23 +0000622 continue;
wdenk38635852002-08-27 05:55:31 +0000623 pCMD->blnr=0; /* set to the 1st block */
624 if(fdc_recalibrate(pCMD,pFG)==FALSE)
wdenk7f6c2cb2002-11-10 22:06:23 +0000625 continue;
wdenk38635852002-08-27 05:55:31 +0000626 pCMD->cmd[COMMAND]=FDC_CMD_READ_ID;
627 if(fdc_issue_cmd(pCMD,pFG)==FALSE)
wdenk7f6c2cb2002-11-10 22:06:23 +0000628 continue;
wdenk38635852002-08-27 05:55:31 +0000629 state|=(0x10<<drives);
630 }
631 stop_fdc_drive(pCMD);
632 for(i=0;i<4;i++) {
633 PRINTF("Floppy Drive %d %sconnected %sDisk inserted %s\n",i,
634 ((state&(1<<i))==(1<<i)) ? "":"not ",
635 ((state&(0x10<<i))==(0x10<<i)) ? "":"no ",
636 ((state&(0x10<<i))==(0x10<<i)) ? pFG->name : "");
637 }
638 pCMD->flags=state;
639 return TRUE;
640}
641
642
643/**************************************************************************
644* int fdc_setup
645* setup the fdc according the datasheet
646* assuming in PS2 Mode
647*/
wdenk2262cfe2002-11-18 00:14:45 +0000648int fdc_setup(int drive, FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
wdenk38635852002-08-27 05:55:31 +0000649{
wdenk38635852002-08-27 05:55:31 +0000650 int i;
wdenk7f6c2cb2002-11-10 22:06:23 +0000651
wdenkc7de8292002-11-19 11:04:11 +0000652#ifdef CONFIG_AMIGAONEG3SE
653 irq_install_handler(6, (interrupt_handler_t *)fdc_interrupt, NULL);
654 i8259_unmask_irq(6);
655#endif
656
wdenk7f6c2cb2002-11-10 22:06:23 +0000657#ifdef CFG_FDC_HW_INIT
wdenk8bde7f72003-06-27 21:31:46 +0000658 fdc_hw_init ();
wdenk7f6c2cb2002-11-10 22:06:23 +0000659#endif
wdenk38635852002-08-27 05:55:31 +0000660 /* first, we reset the FDC via the DOR */
661 write_fdc_reg(FDC_DOR,0x00);
662 for(i=0; i<255; i++) /* then we wait some time */
663 udelay(500);
664 /* then, we clear the reset in the DOR */
wdenk2262cfe2002-11-18 00:14:45 +0000665 pCMD->drive=drive;
wdenk38635852002-08-27 05:55:31 +0000666 select_fdc_drive(pCMD);
667 /* initialize the CCR */
668 write_fdc_reg(FDC_CCR,pFG->rate);
669 /* then initialize the DSR */
670 write_fdc_reg(FDC_DSR,pFG->rate);
671 if(wait_for_fdc_int()==FALSE) {
672 PRINTF("Time Out after writing CCR\n");
673 return FALSE;
674 }
675 /* now issue sense Interrupt and status command
676 * assuming only one drive present (drive 0) */
677 pCMD->dma=0; /* we don't use any dma at all */
678 for(i=0;i<4;i++) {
679 /* issue sense interrupt for all 4 possible drives */
680 pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
681 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
682 PRINTF("Sense Interrupt for drive %d failed\n",i);
683 }
684 }
wdenk2262cfe2002-11-18 00:14:45 +0000685 /* issue the configure command */
686 pCMD->drive=drive;
wdenk38635852002-08-27 05:55:31 +0000687 select_fdc_drive(pCMD);
688 pCMD->cmd[COMMAND]=FDC_CMD_CONFIGURE;
689 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
690 PRINTF(" configure timeout\n");
691 stop_fdc_drive(pCMD);
692 return FALSE;
693 }
694 /* issue specify command */
695 pCMD->cmd[COMMAND]=FDC_CMD_SPECIFY;
696 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
697 PRINTF(" specify timeout\n");
698 stop_fdc_drive(pCMD);
699 return FALSE;
700
701 }
702 /* then, we clear the reset in the DOR */
703 /* fdc_check_drive(pCMD,pFG); */
704 /* write_fdc_reg(FDC_DOR,0x04); */
wdenkc7de8292002-11-19 11:04:11 +0000705
wdenk38635852002-08-27 05:55:31 +0000706 return TRUE;
707}
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500708#endif
wdenk38635852002-08-27 05:55:31 +0000709
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500710#if defined(CONFIG_CMD_FDOS)
wdenk2262cfe2002-11-18 00:14:45 +0000711
712/* Low level functions for the Floppy-DOS layer */
713
714/**************************************************************************
715* int fdc_fdos_init
wdenk8bde7f72003-06-27 21:31:46 +0000716* initialize the FDC layer
717*
wdenk2262cfe2002-11-18 00:14:45 +0000718*/
719int fdc_fdos_init (int drive)
720{
721 FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
722 FDC_COMMAND_STRUCT *pCMD = &cmd;
wdenk8bde7f72003-06-27 21:31:46 +0000723
wdenk2262cfe2002-11-18 00:14:45 +0000724 /* setup FDC and scan for drives */
725 if(fdc_setup(drive,pCMD,pFG)==FALSE) {
726 printf("\n** Error in setup FDC **\n");
727 return FALSE;
728 }
729 if(fdc_check_drive(pCMD,pFG)==FALSE) {
730 printf("\n** Error in check_drives **\n");
731 return FALSE;
732 }
733 if((pCMD->flags&(1<<drive))==0) {
734 /* drive not available */
735 printf("\n** Drive %d not available **\n",drive);
736 return FALSE;
737 }
738 if((pCMD->flags&(0x10<<drive))==0) {
739 /* no disk inserted */
740 printf("\n** No disk inserted in drive %d **\n",drive);
741 return FALSE;
742 }
743 /* ok, we have a valid source */
744 pCMD->drive=drive;
745
746 /* read first block */
747 pCMD->blnr=0;
wdenk8bde7f72003-06-27 21:31:46 +0000748 return TRUE;
wdenk2262cfe2002-11-18 00:14:45 +0000749}
750/**************************************************************************
751* int fdc_fdos_seek
wdenk8bde7f72003-06-27 21:31:46 +0000752* parameter is a block number
wdenk2262cfe2002-11-18 00:14:45 +0000753*/
754int fdc_fdos_seek (int where)
755{
756 FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
757 FDC_COMMAND_STRUCT *pCMD = &cmd;
758
wdenk8bde7f72003-06-27 21:31:46 +0000759 pCMD -> blnr = where ;
760 return (fdc_seek (pCMD, pFG));
wdenk2262cfe2002-11-18 00:14:45 +0000761}
762/**************************************************************************
763* int fdc_fdos_read
764* the length is in block number
765*/
766int fdc_fdos_read (void *buffer, int len)
767{
768 FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
769 FDC_COMMAND_STRUCT *pCMD = &cmd;
770
wdenk8bde7f72003-06-27 21:31:46 +0000771 return (fdc_read_data (buffer, len, pCMD, pFG));
wdenk2262cfe2002-11-18 00:14:45 +0000772}
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500773#endif
wdenk2262cfe2002-11-18 00:14:45 +0000774
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500775#if defined(CONFIG_CMD_FDC)
wdenk38635852002-08-27 05:55:31 +0000776/****************************************************************************
777 * main routine do_fdcboot
778 */
779int do_fdcboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
780{
781 FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
782 FDC_COMMAND_STRUCT *pCMD = &cmd;
wdenk8bde7f72003-06-27 21:31:46 +0000783 unsigned long addr,imsize;
wdenk38635852002-08-27 05:55:31 +0000784 image_header_t *hdr; /* used for fdc boot */
785 unsigned char boot_drive;
786 int i,nrofblk;
787 char *ep;
788 int rcode = 0;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100789#if defined(CONFIG_FIT)
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200790 const void *fit_hdr = NULL;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100791#endif
wdenk38635852002-08-27 05:55:31 +0000792
793 switch (argc) {
794 case 1:
795 addr = CFG_LOAD_ADDR;
wdenk8bde7f72003-06-27 21:31:46 +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 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100839
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100840 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100841 case IMAGE_FORMAT_LEGACY:
842 hdr = (image_header_t *)addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100843 image_print_contents (hdr);
844
845 imsize = image_get_image_size (hdr);
846 break;
847#if defined(CONFIG_FIT)
848 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100849 fit_hdr = (const void *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100850 puts ("Fit image detected...\n");
851
852 imsize = fit_get_size (fit_hdr);
853 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100854#endif
855 default:
856 puts ("** Unknown image type\n");
wdenk38635852002-08-27 05:55:31 +0000857 return 1;
858 }
wdenk38635852002-08-27 05:55:31 +0000859
wdenk38635852002-08-27 05:55:31 +0000860 nrofblk=imsize/512;
861 if((imsize%512)>0)
862 nrofblk++;
863 printf("Loading %ld Bytes (%d blocks) at 0x%08lx..\n",imsize,nrofblk,addr);
864 pCMD->blnr=0;
865 if(fdc_read_data((unsigned char *)addr,nrofblk,pCMD,pFG)==FALSE) {
866 /* read image block */
867 printf("\nRead error:");
868 for(i=0;i<7;i++)
869 printf("result%d: 0x%02X\n",i,pCMD->result[i]);
870 return 1;
871 }
872 printf("OK %ld Bytes loaded.\n",imsize);
873
874 flush_cache (addr, imsize);
wdenk38635852002-08-27 05:55:31 +0000875
Marian Balakowicz09475f72008-03-12 10:33:01 +0100876#if defined(CONFIG_FIT)
877 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200878 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
879 if (!fit_check_format (fit_hdr)) {
880 puts ("** Bad FIT image format\n");
881 return 1;
882 }
883 fit_print_contents (fit_hdr);
884 }
Marian Balakowicz09475f72008-03-12 10:33:01 +0100885#endif
886
887 /* Loading ok, update default load address */
wdenk38635852002-08-27 05:55:31 +0000888 load_addr = addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100889
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100890 /* Check if we should attempt an auto-start */
891 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
892 char *local_args[2];
893 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
wdenk38635852002-08-27 05:55:31 +0000894
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100895 local_args[0] = argv[0];
896 local_args[1] = NULL;
wdenk38635852002-08-27 05:55:31 +0000897
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100898 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
wdenk38635852002-08-27 05:55:31 +0000899
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100900 do_bootm (cmdtp, 0, 1, local_args);
901 rcode ++;
wdenk38635852002-08-27 05:55:31 +0000902 }
903 return rcode;
904}
905
906
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500907#endif
wdenk38635852002-08-27 05:55:31 +0000908
909
wdenk8bde7f72003-06-27 21:31:46 +0000910/***************************************************/
911
912
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500913#if defined(CONFIG_CMD_FDC)
wdenk8bde7f72003-06-27 21:31:46 +0000914
wdenk0d498392003-07-01 21:06:45 +0000915U_BOOT_CMD(
916 fdcboot, 3, 1, do_fdcboot,
wdenk8bde7f72003-06-27 21:31:46 +0000917 "fdcboot - boot from floppy device\n",
918 "loadAddr drive\n"
919);
920#endif