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