wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Denis Peter, MPL AG, d.peter@mpl.ch. |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 6 | */ |
| 7 | /* |
| 8 | * Floppy Disk support |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <config.h> |
| 13 | #include <command.h> |
| 14 | #include <image.h> |
| 15 | |
| 16 | |
| 17 | #undef FDC_DEBUG |
| 18 | |
| 19 | #ifdef FDC_DEBUG |
| 20 | #define PRINTF(fmt,args...) printf (fmt ,##args) |
| 21 | #else |
| 22 | #define PRINTF(fmt,args...) |
| 23 | #endif |
| 24 | |
Jon Loeliger | 54324d0 | 2007-07-08 17:51:39 -0500 | [diff] [blame] | 25 | /*#if defined(CONFIG_CMD_DATE) */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 26 | /*#include <rtc.h> */ |
| 27 | /*#endif */ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 28 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 29 | typedef struct { |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 30 | int flags; /* connected drives ect */ |
| 31 | unsigned long blnr; /* Logical block nr */ |
| 32 | uchar drive; /* drive no */ |
| 33 | uchar cmdlen; /* cmd length */ |
| 34 | uchar cmd[16]; /* cmd desc */ |
| 35 | uchar dma; /* if > 0 dma enabled */ |
| 36 | uchar result[11]; /* status information */ |
| 37 | uchar resultlen; /* lenght of result */ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 38 | } FDC_COMMAND_STRUCT; |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 39 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 40 | /* flags: only the lower 8bit used: |
| 41 | * bit 0 if set drive 0 is present |
| 42 | * bit 1 if set drive 1 is present |
| 43 | * bit 2 if set drive 2 is present |
| 44 | * bit 3 if set drive 3 is present |
| 45 | * bit 4 if set disk in drive 0 is inserted |
| 46 | * bit 5 if set disk in drive 1 is inserted |
| 47 | * bit 6 if set disk in drive 2 is inserted |
| 48 | * bit 7 if set disk in drive 4 is inserted |
| 49 | */ |
| 50 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 51 | /* cmd indexes */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 52 | #define COMMAND 0 |
| 53 | #define DRIVE 1 |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 54 | #define CONFIG0 1 |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 55 | #define SPEC_HUTSRT 1 |
| 56 | #define TRACK 2 |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 57 | #define CONFIG1 2 |
| 58 | #define SPEC_HLT 2 |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 59 | #define HEAD 3 |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 60 | #define CONFIG2 3 |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 61 | #define SECTOR 4 |
| 62 | #define SECTOR_SIZE 5 |
| 63 | #define LAST_TRACK 6 |
| 64 | #define GAP 7 |
| 65 | #define DTL 8 |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 66 | /* result indexes */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 67 | #define STATUS_0 0 |
| 68 | #define STATUS_PCN 1 |
| 69 | #define STATUS_1 1 |
| 70 | #define STATUS_2 2 |
| 71 | #define STATUS_TRACK 3 |
| 72 | #define STATUS_HEAD 4 |
| 73 | #define STATUS_SECT 5 |
| 74 | #define STATUS_SECT_SIZE 6 |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 75 | |
| 76 | |
| 77 | /* Register addresses */ |
| 78 | #define FDC_BASE 0x3F0 |
| 79 | #define FDC_SRA FDC_BASE + 0 /* Status Register A */ |
| 80 | #define FDC_SRB FDC_BASE + 1 /* Status Register B */ |
| 81 | #define FDC_DOR FDC_BASE + 2 /* Digital Output Register */ |
| 82 | #define FDC_TDR FDC_BASE + 3 /* Tape Drive Register */ |
| 83 | #define FDC_DSR FDC_BASE + 4 /* Data rate Register */ |
| 84 | #define FDC_MSR FDC_BASE + 4 /* Main Status Register */ |
| 85 | #define FDC_FIFO FDC_BASE + 5 /* FIFO */ |
| 86 | #define FDC_DIR FDC_BASE + 6 /* Digital Input Register */ |
| 87 | #define FDC_CCR FDC_BASE + 7 /* Configuration Control */ |
| 88 | /* Commands */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 89 | #define FDC_CMD_SENSE_INT 0x08 |
| 90 | #define FDC_CMD_CONFIGURE 0x13 |
| 91 | #define FDC_CMD_SPECIFY 0x03 |
| 92 | #define FDC_CMD_RECALIBRATE 0x07 |
| 93 | #define FDC_CMD_READ 0x06 |
| 94 | #define FDC_CMD_READ_TRACK 0x02 |
| 95 | #define FDC_CMD_READ_ID 0x0A |
| 96 | #define FDC_CMD_DUMP_REG 0x0E |
| 97 | #define FDC_CMD_SEEK 0x0F |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 98 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 99 | #define FDC_CMD_SENSE_INT_LEN 0x01 |
| 100 | #define FDC_CMD_CONFIGURE_LEN 0x04 |
| 101 | #define FDC_CMD_SPECIFY_LEN 0x03 |
| 102 | #define FDC_CMD_RECALIBRATE_LEN 0x02 |
| 103 | #define FDC_CMD_READ_LEN 0x09 |
| 104 | #define FDC_CMD_READ_TRACK_LEN 0x09 |
| 105 | #define FDC_CMD_READ_ID_LEN 0x02 |
| 106 | #define FDC_CMD_DUMP_REG_LEN 0x01 |
| 107 | #define FDC_CMD_SEEK_LEN 0x03 |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 108 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 109 | #define FDC_FIFO_THR 0x0C |
| 110 | #define FDC_FIFO_DIS 0x00 |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 111 | #define FDC_IMPLIED_SEEK 0x01 |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 112 | #define FDC_POLL_DIS 0x00 |
| 113 | #define FDC_PRE_TRK 0x00 |
| 114 | #define FDC_CONFIGURE FDC_FIFO_THR | (FDC_POLL_DIS<<4) | (FDC_FIFO_DIS<<5) | (FDC_IMPLIED_SEEK << 6) |
| 115 | #define FDC_MFM_MODE 0x01 /* MFM enable */ |
| 116 | #define FDC_SKIP_MODE 0x00 /* skip enable */ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 117 | |
| 118 | #define FDC_TIME_OUT 100000 /* time out */ |
| 119 | #define FDC_RW_RETRIES 3 /* read write retries */ |
| 120 | #define FDC_CAL_RETRIES 3 /* calibration and seek retries */ |
| 121 | |
| 122 | |
| 123 | /* Disk structure */ |
| 124 | typedef struct { |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 125 | unsigned int size; /* nr of sectors total */ |
| 126 | unsigned int sect; /* sectors per track */ |
| 127 | unsigned int head; /* nr of heads */ |
| 128 | unsigned int track; /* nr of tracks */ |
| 129 | unsigned int stretch; /* !=0 means double track steps */ |
| 130 | unsigned char gap; /* gap1 size */ |
| 131 | unsigned char rate; /* data rate. |= 0x40 for perpendicular */ |
| 132 | unsigned char spec1; /* stepping rate, head unload time */ |
| 133 | unsigned char fmt_gap;/* gap2 size */ |
| 134 | unsigned char hlt; /* head load time */ |
| 135 | unsigned char sect_code;/* Sector Size code */ |
| 136 | const char * name; /* used only for predefined formats */ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 137 | } FD_GEO_STRUCT; |
| 138 | |
| 139 | |
| 140 | /* supported Floppy types (currently only one) */ |
| 141 | const static FD_GEO_STRUCT floppy_type[2] = { |
| 142 | { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,16,2,"H1440" }, /* 7 1.44MB 3.5" */ |
| 143 | { 0, 0,0, 0,0,0x00,0x00,0x00,0x00, 0,0,NULL }, /* end of table */ |
| 144 | }; |
| 145 | |
| 146 | static FDC_COMMAND_STRUCT cmd; /* global command struct */ |
| 147 | |
wdenk | 1272e23 | 2002-11-10 22:06:23 +0000 | [diff] [blame] | 148 | /* If the boot drive number is undefined, we assume it's drive 0 */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 149 | #ifndef CONFIG_SYS_FDC_DRIVE_NUMBER |
| 150 | #define CONFIG_SYS_FDC_DRIVE_NUMBER 0 |
wdenk | 1272e23 | 2002-11-10 22:06:23 +0000 | [diff] [blame] | 151 | #endif |
| 152 | |
| 153 | /* Hardware access */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 154 | #ifndef CONFIG_SYS_ISA_IO_STRIDE |
| 155 | #define CONFIG_SYS_ISA_IO_STRIDE 1 |
wdenk | 1272e23 | 2002-11-10 22:06:23 +0000 | [diff] [blame] | 156 | #endif |
| 157 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 158 | #ifndef CONFIG_SYS_ISA_IO_OFFSET |
| 159 | #define CONFIG_SYS_ISA_IO_OFFSET 0 |
wdenk | 1272e23 | 2002-11-10 22:06:23 +0000 | [diff] [blame] | 160 | #endif |
| 161 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 162 | /* Supporting Functions */ |
| 163 | /* reads a Register of the FDC */ |
| 164 | unsigned char read_fdc_reg(unsigned int addr) |
| 165 | { |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 166 | volatile unsigned char *val = |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 167 | (volatile unsigned char *)(CONFIG_SYS_ISA_IO_BASE_ADDRESS + |
| 168 | (addr * CONFIG_SYS_ISA_IO_STRIDE) + |
| 169 | CONFIG_SYS_ISA_IO_OFFSET); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 170 | |
wdenk | 1272e23 | 2002-11-10 22:06:23 +0000 | [diff] [blame] | 171 | return val [0]; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* writes a Register of the FDC */ |
| 175 | void write_fdc_reg(unsigned int addr, unsigned char val) |
| 176 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 177 | volatile unsigned char *tmp = |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 178 | (volatile unsigned char *)(CONFIG_SYS_ISA_IO_BASE_ADDRESS + |
| 179 | (addr * CONFIG_SYS_ISA_IO_STRIDE) + |
| 180 | CONFIG_SYS_ISA_IO_OFFSET); |
wdenk | 1272e23 | 2002-11-10 22:06:23 +0000 | [diff] [blame] | 181 | tmp[0]=val; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* waits for an interrupt (polling) */ |
| 185 | int wait_for_fdc_int(void) |
| 186 | { |
| 187 | unsigned long timeout; |
| 188 | timeout = FDC_TIME_OUT; |
| 189 | while((read_fdc_reg(FDC_SRA)&0x80)==0) { |
| 190 | timeout--; |
| 191 | udelay(10); |
| 192 | if(timeout==0) /* timeout occured */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 193 | return false; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 194 | } |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 195 | return true; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 196 | } |
| 197 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 198 | /* reads a byte from the FIFO of the FDC and checks direction and RQM bit |
| 199 | of the MSR. returns -1 if timeout, or byte if ok */ |
| 200 | int read_fdc_byte(void) |
| 201 | { |
| 202 | unsigned long timeout; |
| 203 | timeout = FDC_TIME_OUT; |
| 204 | while((read_fdc_reg(FDC_MSR)&0xC0)!=0xC0) { |
| 205 | /* direction out and ready */ |
| 206 | udelay(10); |
| 207 | timeout--; |
| 208 | if(timeout==0) /* timeout occured */ |
| 209 | return -1; |
| 210 | } |
| 211 | return read_fdc_reg(FDC_FIFO); |
| 212 | } |
| 213 | |
| 214 | /* if the direction of the FIFO is wrong, this routine is used to |
| 215 | empty the FIFO. Should _not_ be used */ |
| 216 | int fdc_need_more_output(void) |
| 217 | { |
| 218 | unsigned char c; |
| 219 | while((read_fdc_reg(FDC_MSR)&0xC0)==0xC0) { |
| 220 | c=(unsigned char)read_fdc_byte(); |
| 221 | printf("Error: more output: %x\n",c); |
| 222 | } |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 223 | return true; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | |
| 227 | /* writes a byte to the FIFO of the FDC and checks direction and RQM bit |
| 228 | of the MSR */ |
| 229 | int write_fdc_byte(unsigned char val) |
| 230 | { |
| 231 | unsigned long timeout; |
| 232 | timeout = FDC_TIME_OUT; |
| 233 | while((read_fdc_reg(FDC_MSR)&0xC0)!=0x80) { |
| 234 | /* direction in and ready for byte */ |
| 235 | timeout--; |
| 236 | udelay(10); |
| 237 | fdc_need_more_output(); |
| 238 | if(timeout==0) /* timeout occured */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 239 | return false; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 240 | } |
| 241 | write_fdc_reg(FDC_FIFO,val); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 242 | return true; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /* sets up all FDC commands and issues it to the FDC. If |
| 246 | the command causes direct results (no Execution Phase) |
| 247 | the result is be read as well. */ |
| 248 | |
| 249 | int fdc_issue_cmd(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG) |
| 250 | { |
| 251 | int i; |
| 252 | unsigned long head,track,sect,timeout; |
| 253 | track = pCMD->blnr / (pFG->sect * pFG->head); /* track nr */ |
| 254 | sect = pCMD->blnr % (pFG->sect * pFG->head); /* remaining blocks */ |
| 255 | head = sect / pFG->sect; /* head nr */ |
| 256 | sect = sect % pFG->sect; /* remaining blocks */ |
| 257 | sect++; /* sectors are 1 based */ |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 258 | PRINTF("Cmd 0x%02x Track %ld, Head %ld, Sector %ld, Drive %d (blnr %ld)\n", |
| 259 | pCMD->cmd[0],track,head,sect,pCMD->drive,pCMD->blnr); |
wdenk | 1272e23 | 2002-11-10 22:06:23 +0000 | [diff] [blame] | 260 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 261 | if(head|=0) { /* max heads = 2 */ |
| 262 | pCMD->cmd[DRIVE]=pCMD->drive | 0x04; /* head 1 */ |
| 263 | pCMD->cmd[HEAD]=(unsigned char) head; /* head register */ |
| 264 | } |
| 265 | else { |
| 266 | pCMD->cmd[DRIVE]=pCMD->drive; /* head 0 */ |
| 267 | pCMD->cmd[HEAD]=(unsigned char) head; /* head register */ |
| 268 | } |
| 269 | pCMD->cmd[TRACK]=(unsigned char) track; /* track */ |
| 270 | switch (pCMD->cmd[COMMAND]) { |
| 271 | case FDC_CMD_READ: |
| 272 | pCMD->cmd[SECTOR]=(unsigned char) sect; /* sector */ |
| 273 | pCMD->cmd[SECTOR_SIZE]=pFG->sect_code; /* sector size code */ |
| 274 | pCMD->cmd[LAST_TRACK]=pFG->sect; /* End of track */ |
| 275 | pCMD->cmd[GAP]=pFG->gap; /* gap */ |
| 276 | pCMD->cmd[DTL]=0xFF; /* DTL */ |
| 277 | pCMD->cmdlen=FDC_CMD_READ_LEN; |
| 278 | pCMD->cmd[COMMAND]|=(FDC_MFM_MODE<<6); /* set MFM bit */ |
| 279 | pCMD->cmd[COMMAND]|=(FDC_SKIP_MODE<<5); /* set Skip bit */ |
| 280 | pCMD->resultlen=0; /* result only after execution */ |
| 281 | break; |
| 282 | case FDC_CMD_SEEK: |
| 283 | pCMD->cmdlen=FDC_CMD_SEEK_LEN; |
| 284 | pCMD->resultlen=0; /* no result */ |
| 285 | break; |
| 286 | case FDC_CMD_CONFIGURE: |
| 287 | pCMD->cmd[CONFIG0]=0; |
| 288 | pCMD->cmd[CONFIG1]=FDC_CONFIGURE; /* FIFO Threshold, Poll, Enable FIFO */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 289 | pCMD->cmd[CONFIG2]=FDC_PRE_TRK; /* Precompensation Track */ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 290 | pCMD->cmdlen=FDC_CMD_CONFIGURE_LEN; |
| 291 | pCMD->resultlen=0; /* no result */ |
| 292 | break; |
| 293 | case FDC_CMD_SPECIFY: |
| 294 | pCMD->cmd[SPEC_HUTSRT]=pFG->spec1; |
| 295 | pCMD->cmd[SPEC_HLT]=(pFG->hlt)<<1; /* head load time */ |
| 296 | if(pCMD->dma==0) |
| 297 | pCMD->cmd[SPEC_HLT]|=0x1; /* no dma */ |
| 298 | pCMD->cmdlen=FDC_CMD_SPECIFY_LEN; |
| 299 | pCMD->resultlen=0; /* no result */ |
| 300 | break; |
| 301 | case FDC_CMD_DUMP_REG: |
| 302 | pCMD->cmdlen=FDC_CMD_DUMP_REG_LEN; |
| 303 | pCMD->resultlen=10; /* 10 byte result */ |
| 304 | break; |
| 305 | case FDC_CMD_READ_ID: |
| 306 | pCMD->cmd[COMMAND]|=(FDC_MFM_MODE<<6); /* set MFM bit */ |
| 307 | pCMD->cmdlen=FDC_CMD_READ_ID_LEN; |
| 308 | pCMD->resultlen=7; /* 7 byte result */ |
| 309 | break; |
| 310 | case FDC_CMD_RECALIBRATE: |
| 311 | pCMD->cmd[DRIVE]&=0x03; /* don't set the head bit */ |
| 312 | pCMD->cmdlen=FDC_CMD_RECALIBRATE_LEN; |
| 313 | pCMD->resultlen=0; /* no result */ |
| 314 | break; |
| 315 | break; |
| 316 | case FDC_CMD_SENSE_INT: |
| 317 | pCMD->cmdlen=FDC_CMD_SENSE_INT_LEN; |
| 318 | pCMD->resultlen=2; |
| 319 | break; |
| 320 | } |
| 321 | for(i=0;i<pCMD->cmdlen;i++) { |
| 322 | /* PRINTF("write cmd%d = 0x%02X\n",i,pCMD->cmd[i]); */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 323 | if (write_fdc_byte(pCMD->cmd[i]) == false) { |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 324 | PRINTF("Error: timeout while issue cmd%d\n",i); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 325 | return false; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | timeout=FDC_TIME_OUT; |
| 329 | for(i=0;i<pCMD->resultlen;i++) { |
| 330 | while((read_fdc_reg(FDC_MSR)&0xC0)!=0xC0) { |
| 331 | timeout--; |
| 332 | if(timeout==0) { |
| 333 | PRINTF(" timeout while reading result%d MSR=0x%02X\n",i,read_fdc_reg(FDC_MSR)); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 334 | return false; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | pCMD->result[i]=(unsigned char)read_fdc_byte(); |
| 338 | } |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 339 | return true; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /* selects the drive assigned in the cmd structur and |
| 343 | switches on the Motor */ |
| 344 | void select_fdc_drive(FDC_COMMAND_STRUCT *pCMD) |
| 345 | { |
| 346 | unsigned char val; |
| 347 | |
| 348 | val=(1<<(4+pCMD->drive))|pCMD->drive|0xC; /* set reset, dma gate and motor bits */ |
| 349 | if((read_fdc_reg(FDC_DOR)&val)!=val) { |
| 350 | write_fdc_reg(FDC_DOR,val); |
| 351 | for(val=0;val<255;val++) |
| 352 | udelay(500); /* wait some time to start motor */ |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | /* switches off the Motor of the specified drive */ |
| 357 | void stop_fdc_drive(FDC_COMMAND_STRUCT *pCMD) |
| 358 | { |
| 359 | unsigned char val; |
| 360 | |
| 361 | val=(1<<(4+pCMD->drive))|pCMD->drive; /* sets motor bits */ |
| 362 | write_fdc_reg(FDC_DOR,(read_fdc_reg(FDC_DOR)&~val)); |
| 363 | } |
| 364 | |
| 365 | /* issues a recalibrate command, waits for interrupt and |
| 366 | * issues a sense_interrupt */ |
| 367 | int fdc_recalibrate(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG) |
| 368 | { |
| 369 | pCMD->cmd[COMMAND]=FDC_CMD_RECALIBRATE; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 370 | if (fdc_issue_cmd(pCMD, pFG) == false) |
| 371 | return false; |
| 372 | while (wait_for_fdc_int() != true); |
| 373 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 374 | pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT; |
| 375 | return(fdc_issue_cmd(pCMD,pFG)); |
| 376 | } |
| 377 | |
| 378 | /* issues a recalibrate command, waits for interrupt and |
| 379 | * issues a sense_interrupt */ |
| 380 | int fdc_seek(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG) |
| 381 | { |
| 382 | pCMD->cmd[COMMAND]=FDC_CMD_SEEK; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 383 | if (fdc_issue_cmd(pCMD, pFG) == false) |
| 384 | return false; |
| 385 | while (wait_for_fdc_int() != true); |
| 386 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 387 | pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT; |
| 388 | return(fdc_issue_cmd(pCMD,pFG)); |
| 389 | } |
| 390 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 391 | /* terminates current command, by not servicing the FIFO |
| 392 | * waits for interrupt and fills in the result bytes */ |
| 393 | int fdc_terminate(FDC_COMMAND_STRUCT *pCMD) |
| 394 | { |
| 395 | int i; |
| 396 | for(i=0;i<100;i++) |
| 397 | udelay(500); /* wait 500usec for fifo overrun */ |
| 398 | while((read_fdc_reg(FDC_SRA)&0x80)==0x00); /* wait as long as no int has occured */ |
| 399 | for(i=0;i<7;i++) { |
| 400 | pCMD->result[i]=(unsigned char)read_fdc_byte(); |
| 401 | } |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 402 | return true; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /* reads data from FDC, seek commands are issued automatic */ |
| 406 | int fdc_read_data(unsigned char *buffer, unsigned long blocks,FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG) |
| 407 | { |
| 408 | /* first seek to start address */ |
Wolfgang Denk | d8c5649 | 2011-11-09 09:29:02 +0000 | [diff] [blame] | 409 | unsigned long len,readblk,i,timeout,ii,offset; |
| 410 | unsigned char c,retriesrw,retriescal; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 411 | unsigned char *bufferw; /* working buffer */ |
| 412 | int sect_size; |
| 413 | int flags; |
| 414 | |
| 415 | flags=disable_interrupts(); /* switch off all Interrupts */ |
| 416 | select_fdc_drive(pCMD); /* switch on drive */ |
| 417 | sect_size=0x080<<pFG->sect_code; |
| 418 | retriesrw=0; |
| 419 | retriescal=0; |
| 420 | offset=0; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 421 | if (fdc_seek(pCMD, pFG) == false) { |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 422 | stop_fdc_drive(pCMD); |
Wolfgang Denk | d8c5649 | 2011-11-09 09:29:02 +0000 | [diff] [blame] | 423 | if (flags) |
| 424 | enable_interrupts(); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 425 | return false; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 426 | } |
| 427 | if((pCMD->result[STATUS_0]&0x20)!=0x20) { |
| 428 | printf("Seek error Status: %02X\n",pCMD->result[STATUS_0]); |
| 429 | stop_fdc_drive(pCMD); |
Wolfgang Denk | d8c5649 | 2011-11-09 09:29:02 +0000 | [diff] [blame] | 430 | if (flags) |
| 431 | enable_interrupts(); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 432 | return false; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 433 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 434 | /* now determine the next seek point */ |
Wolfgang Denk | d8c5649 | 2011-11-09 09:29:02 +0000 | [diff] [blame] | 435 | /* lastblk=pCMD->blnr + blocks; */ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 436 | /* readblk=(pFG->head*pFG->sect)-(pCMD->blnr%(pFG->head*pFG->sect)); */ |
| 437 | readblk=pFG->sect-(pCMD->blnr%pFG->sect); |
| 438 | PRINTF("1st nr of block possible read %ld start %ld\n",readblk,pCMD->blnr); |
| 439 | if(readblk>blocks) /* is end within 1st track */ |
| 440 | readblk=blocks; /* yes, correct it */ |
| 441 | PRINTF("we read %ld blocks start %ld\n",readblk,pCMD->blnr); |
Wolfgang Denk | dc770c7 | 2008-07-14 15:19:07 +0200 | [diff] [blame] | 442 | bufferw = &buffer[0]; /* setup working buffer */ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 443 | do { |
| 444 | retryrw: |
| 445 | len=sect_size * readblk; |
| 446 | pCMD->cmd[COMMAND]=FDC_CMD_READ; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 447 | if (fdc_issue_cmd(pCMD, pFG) == false) { |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 448 | stop_fdc_drive(pCMD); |
Wolfgang Denk | d8c5649 | 2011-11-09 09:29:02 +0000 | [diff] [blame] | 449 | if (flags) |
| 450 | enable_interrupts(); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 451 | return false; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 452 | } |
| 453 | for (i=0;i<len;i++) { |
| 454 | timeout=FDC_TIME_OUT; |
| 455 | do { |
| 456 | c=read_fdc_reg(FDC_MSR); |
| 457 | if((c&0xC0)==0xC0) { |
| 458 | bufferw[i]=read_fdc_reg(FDC_FIFO); |
| 459 | break; |
| 460 | } |
| 461 | if((c&0xC0)==0x80) { /* output */ |
Vagrant Cascadian | 53d41e6 | 2016-03-15 12:16:39 -0700 | [diff] [blame] | 462 | PRINTF("Transfer error transferred: at %ld, MSR=%02X\n",i,c); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 463 | if(i>6) { |
| 464 | for(ii=0;ii<7;ii++) { |
| 465 | pCMD->result[ii]=bufferw[(i-7+ii)]; |
| 466 | } /* for */ |
| 467 | } |
| 468 | if(retriesrw++>FDC_RW_RETRIES) { |
| 469 | if (retriescal++>FDC_CAL_RETRIES) { |
| 470 | stop_fdc_drive(pCMD); |
Wolfgang Denk | d8c5649 | 2011-11-09 09:29:02 +0000 | [diff] [blame] | 471 | if (flags) |
| 472 | enable_interrupts(); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 473 | return false; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 474 | } |
| 475 | else { |
| 476 | PRINTF(" trying to recalibrate Try %d\n",retriescal); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 477 | if (fdc_recalibrate(pCMD, pFG) == false) { |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 478 | stop_fdc_drive(pCMD); |
Wolfgang Denk | d8c5649 | 2011-11-09 09:29:02 +0000 | [diff] [blame] | 479 | if (flags) |
| 480 | enable_interrupts(); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 481 | return false; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 482 | } |
| 483 | retriesrw=0; |
| 484 | goto retrycal; |
| 485 | } /* else >FDC_CAL_RETRIES */ |
| 486 | } |
| 487 | else { |
| 488 | PRINTF("Read retry %d\n",retriesrw); |
| 489 | goto retryrw; |
| 490 | } /* else >FDC_RW_RETRIES */ |
| 491 | }/* if output */ |
| 492 | timeout--; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 493 | } while (true); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 494 | } /* for len */ |
| 495 | /* the last sector of a track or all data has been read, |
| 496 | * we need to get the results */ |
| 497 | fdc_terminate(pCMD); |
| 498 | offset+=(sect_size*readblk); /* set up buffer pointer */ |
Wolfgang Denk | dc770c7 | 2008-07-14 15:19:07 +0200 | [diff] [blame] | 499 | bufferw = &buffer[offset]; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 500 | pCMD->blnr+=readblk; /* update current block nr */ |
| 501 | blocks-=readblk; /* update blocks */ |
| 502 | if(blocks==0) |
| 503 | break; /* we are finish */ |
| 504 | /* setup new read blocks */ |
| 505 | /* readblk=pFG->head*pFG->sect; */ |
| 506 | readblk=pFG->sect; |
| 507 | if(readblk>blocks) |
| 508 | readblk=blocks; |
| 509 | retrycal: |
| 510 | /* a seek is necessary */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 511 | if (fdc_seek(pCMD, pFG) == false) { |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 512 | stop_fdc_drive(pCMD); |
Wolfgang Denk | d8c5649 | 2011-11-09 09:29:02 +0000 | [diff] [blame] | 513 | if (flags) |
| 514 | enable_interrupts(); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 515 | return false; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 516 | } |
| 517 | if((pCMD->result[STATUS_0]&0x20)!=0x20) { |
| 518 | PRINTF("Seek error Status: %02X\n",pCMD->result[STATUS_0]); |
| 519 | stop_fdc_drive(pCMD); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 520 | return false; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 521 | } |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 522 | } while (true); /* start over */ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 523 | stop_fdc_drive(pCMD); /* switch off drive */ |
Wolfgang Denk | d8c5649 | 2011-11-09 09:29:02 +0000 | [diff] [blame] | 524 | if (flags) |
| 525 | enable_interrupts(); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 526 | return true; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /* Scan all drives and check if drive is present and disk is inserted */ |
| 530 | int fdc_check_drive(FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG) |
| 531 | { |
| 532 | int i,drives,state; |
| 533 | /* OK procedure of data book is satisfied. |
| 534 | * trying to get some information over the drives */ |
| 535 | state=0; /* no drives, no disks */ |
| 536 | for(drives=0;drives<4;drives++) { |
| 537 | pCMD->drive=drives; |
| 538 | select_fdc_drive(pCMD); |
| 539 | pCMD->blnr=0; /* set to the 1st block */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 540 | if (fdc_recalibrate(pCMD, pFG) == false) |
wdenk | 1272e23 | 2002-11-10 22:06:23 +0000 | [diff] [blame] | 541 | continue; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 542 | if((pCMD->result[STATUS_0]&0x10)==0x10) |
wdenk | 1272e23 | 2002-11-10 22:06:23 +0000 | [diff] [blame] | 543 | continue; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 544 | /* ok drive connected check for disk */ |
| 545 | state|=(1<<drives); |
| 546 | pCMD->blnr=pFG->size; /* set to the last block */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 547 | if (fdc_seek(pCMD, pFG) == false) |
wdenk | 1272e23 | 2002-11-10 22:06:23 +0000 | [diff] [blame] | 548 | continue; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 549 | pCMD->blnr=0; /* set to the 1st block */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 550 | if (fdc_recalibrate(pCMD, pFG) == false) |
wdenk | 1272e23 | 2002-11-10 22:06:23 +0000 | [diff] [blame] | 551 | continue; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 552 | pCMD->cmd[COMMAND]=FDC_CMD_READ_ID; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 553 | if (fdc_issue_cmd(pCMD, pFG) == false) |
wdenk | 1272e23 | 2002-11-10 22:06:23 +0000 | [diff] [blame] | 554 | continue; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 555 | state|=(0x10<<drives); |
| 556 | } |
| 557 | stop_fdc_drive(pCMD); |
| 558 | for(i=0;i<4;i++) { |
| 559 | PRINTF("Floppy Drive %d %sconnected %sDisk inserted %s\n",i, |
| 560 | ((state&(1<<i))==(1<<i)) ? "":"not ", |
| 561 | ((state&(0x10<<i))==(0x10<<i)) ? "":"no ", |
| 562 | ((state&(0x10<<i))==(0x10<<i)) ? pFG->name : ""); |
| 563 | } |
| 564 | pCMD->flags=state; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 565 | return true; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | |
| 569 | /************************************************************************** |
| 570 | * int fdc_setup |
| 571 | * setup the fdc according the datasheet |
| 572 | * assuming in PS2 Mode |
| 573 | */ |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 574 | int fdc_setup(int drive, FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 575 | { |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 576 | int i; |
wdenk | 1272e23 | 2002-11-10 22:06:23 +0000 | [diff] [blame] | 577 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 578 | #ifdef CONFIG_SYS_FDC_HW_INIT |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 579 | fdc_hw_init (); |
wdenk | 1272e23 | 2002-11-10 22:06:23 +0000 | [diff] [blame] | 580 | #endif |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 581 | /* first, we reset the FDC via the DOR */ |
| 582 | write_fdc_reg(FDC_DOR,0x00); |
| 583 | for(i=0; i<255; i++) /* then we wait some time */ |
| 584 | udelay(500); |
| 585 | /* then, we clear the reset in the DOR */ |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 586 | pCMD->drive=drive; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 587 | select_fdc_drive(pCMD); |
| 588 | /* initialize the CCR */ |
| 589 | write_fdc_reg(FDC_CCR,pFG->rate); |
| 590 | /* then initialize the DSR */ |
| 591 | write_fdc_reg(FDC_DSR,pFG->rate); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 592 | if (wait_for_fdc_int() == false) { |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 593 | PRINTF("Time Out after writing CCR\n"); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 594 | return false; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 595 | } |
| 596 | /* now issue sense Interrupt and status command |
| 597 | * assuming only one drive present (drive 0) */ |
| 598 | pCMD->dma=0; /* we don't use any dma at all */ |
| 599 | for(i=0;i<4;i++) { |
| 600 | /* issue sense interrupt for all 4 possible drives */ |
| 601 | pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 602 | if (fdc_issue_cmd(pCMD, pFG) == false) { |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 603 | PRINTF("Sense Interrupt for drive %d failed\n",i); |
| 604 | } |
| 605 | } |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 606 | /* issue the configure command */ |
| 607 | pCMD->drive=drive; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 608 | select_fdc_drive(pCMD); |
| 609 | pCMD->cmd[COMMAND]=FDC_CMD_CONFIGURE; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 610 | if (fdc_issue_cmd(pCMD, pFG) == false) { |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 611 | PRINTF(" configure timeout\n"); |
| 612 | stop_fdc_drive(pCMD); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 613 | return false; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 614 | } |
| 615 | /* issue specify command */ |
| 616 | pCMD->cmd[COMMAND]=FDC_CMD_SPECIFY; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 617 | if (fdc_issue_cmd(pCMD, pFG) == false) { |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 618 | PRINTF(" specify timeout\n"); |
| 619 | stop_fdc_drive(pCMD); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 620 | return false; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 621 | |
| 622 | } |
| 623 | /* then, we clear the reset in the DOR */ |
| 624 | /* fdc_check_drive(pCMD,pFG); */ |
| 625 | /* write_fdc_reg(FDC_DOR,0x04); */ |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 626 | |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 627 | return true; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 628 | } |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 629 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 630 | /**************************************************************************** |
| 631 | * main routine do_fdcboot |
| 632 | */ |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 633 | int do_fdcboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 634 | { |
| 635 | FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type; |
| 636 | FDC_COMMAND_STRUCT *pCMD = &cmd; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 637 | unsigned long addr,imsize; |
Heiko Schocher | 515eb12 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 638 | #if defined(CONFIG_IMAGE_FORMAT_LEGACY) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 639 | image_header_t *hdr; /* used for fdc boot */ |
Heiko Schocher | 515eb12 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 640 | #endif |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 641 | unsigned char boot_drive; |
| 642 | int i,nrofblk; |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 643 | #if defined(CONFIG_FIT) |
Marian Balakowicz | cefb402 | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 644 | const void *fit_hdr = NULL; |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 645 | #endif |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 646 | |
| 647 | switch (argc) { |
| 648 | case 1: |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 649 | addr = CONFIG_SYS_LOAD_ADDR; |
| 650 | boot_drive=CONFIG_SYS_FDC_DRIVE_NUMBER; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 651 | break; |
| 652 | case 2: |
| 653 | addr = simple_strtoul(argv[1], NULL, 16); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 654 | boot_drive=CONFIG_SYS_FDC_DRIVE_NUMBER; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 655 | break; |
| 656 | case 3: |
| 657 | addr = simple_strtoul(argv[1], NULL, 16); |
| 658 | boot_drive=simple_strtoul(argv[2], NULL, 10); |
| 659 | break; |
| 660 | default: |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 661 | return CMD_RET_USAGE; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 662 | } |
| 663 | /* setup FDC and scan for drives */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 664 | if (fdc_setup(boot_drive, pCMD, pFG) == false) { |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 665 | printf("\n** Error in setup FDC **\n"); |
| 666 | return 1; |
| 667 | } |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 668 | if (fdc_check_drive(pCMD, pFG) == false) { |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 669 | printf("\n** Error in check_drives **\n"); |
| 670 | return 1; |
| 671 | } |
| 672 | if((pCMD->flags&(1<<boot_drive))==0) { |
| 673 | /* drive not available */ |
| 674 | printf("\n** Drive %d not availabe **\n",boot_drive); |
| 675 | return 1; |
| 676 | } |
| 677 | if((pCMD->flags&(0x10<<boot_drive))==0) { |
| 678 | /* no disk inserted */ |
| 679 | printf("\n** No disk inserted in drive %d **\n",boot_drive); |
| 680 | return 1; |
| 681 | } |
| 682 | /* ok, we have a valid source */ |
| 683 | pCMD->drive=boot_drive; |
| 684 | /* read first block */ |
| 685 | pCMD->blnr=0; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 686 | if (fdc_read_data((unsigned char *)addr, 1, pCMD, pFG) == false) { |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 687 | printf("\nRead error:"); |
| 688 | for(i=0;i<7;i++) |
| 689 | printf("result%d: 0x%02X\n",i,pCMD->result[i]); |
| 690 | return 1; |
| 691 | } |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 692 | |
Marian Balakowicz | d7c88a4 | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 693 | switch (genimg_get_format ((void *)addr)) { |
Heiko Schocher | 515eb12 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 694 | #if defined(CONFIG_IMAGE_FORMAT_LEGACY) |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 695 | case IMAGE_FORMAT_LEGACY: |
| 696 | hdr = (image_header_t *)addr; |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 697 | image_print_contents (hdr); |
| 698 | |
| 699 | imsize = image_get_image_size (hdr); |
| 700 | break; |
Heiko Schocher | 515eb12 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 701 | #endif |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 702 | #if defined(CONFIG_FIT) |
| 703 | case IMAGE_FORMAT_FIT: |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 704 | fit_hdr = (const void *)addr; |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 705 | puts ("Fit image detected...\n"); |
| 706 | |
| 707 | imsize = fit_get_size (fit_hdr); |
| 708 | break; |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 709 | #endif |
| 710 | default: |
| 711 | puts ("** Unknown image type\n"); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 712 | return 1; |
| 713 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 714 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 715 | nrofblk=imsize/512; |
| 716 | if((imsize%512)>0) |
| 717 | nrofblk++; |
| 718 | printf("Loading %ld Bytes (%d blocks) at 0x%08lx..\n",imsize,nrofblk,addr); |
| 719 | pCMD->blnr=0; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 720 | if (fdc_read_data((unsigned char *)addr, nrofblk, pCMD, pFG) == false) { |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 721 | /* read image block */ |
| 722 | printf("\nRead error:"); |
| 723 | for(i=0;i<7;i++) |
| 724 | printf("result%d: 0x%02X\n",i,pCMD->result[i]); |
| 725 | return 1; |
| 726 | } |
| 727 | printf("OK %ld Bytes loaded.\n",imsize); |
| 728 | |
| 729 | flush_cache (addr, imsize); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 730 | |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 731 | #if defined(CONFIG_FIT) |
| 732 | /* This cannot be done earlier, we need complete FIT image in RAM first */ |
Marian Balakowicz | cefb402 | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 733 | if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) { |
| 734 | if (!fit_check_format (fit_hdr)) { |
| 735 | puts ("** Bad FIT image format\n"); |
| 736 | return 1; |
| 737 | } |
| 738 | fit_print_contents (fit_hdr); |
| 739 | } |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 740 | #endif |
| 741 | |
| 742 | /* Loading ok, update default load address */ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 743 | load_addr = addr; |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 744 | |
Mike Frysinger | 194c2e8 | 2011-06-05 13:43:02 +0000 | [diff] [blame] | 745 | return bootm_maybe_autostart(cmdtp, argv[0]); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 746 | } |
| 747 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 748 | U_BOOT_CMD( |
| 749 | fdcboot, 3, 1, do_fdcboot, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 750 | "boot from floppy device", |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 751 | "loadAddr drive" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 752 | ); |