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