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