blob: 4fe410dd327d5a1b47d48615dd9dd1df9bf33a00 [file] [log] [blame]
wdenk38635852002-08-27 05:55:31 +00001/*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG, d.peter@mpl.ch.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 *
23 */
24/*
25 * Floppy Disk support
26 */
27
28#include <common.h>
29#include <config.h>
30#include <command.h>
31#include <image.h>
32
33
34#undef FDC_DEBUG
35
36#ifdef FDC_DEBUG
37#define PRINTF(fmt,args...) printf (fmt ,##args)
38#else
39#define PRINTF(fmt,args...)
40#endif
41
42#ifndef TRUE
43#define TRUE 1
44#endif
45#ifndef FALSE
46#define FALSE 0
47#endif
48
Jon Loeliger54324d02007-07-08 17:51:39 -050049/*#if defined(CONFIG_CMD_DATE) */
wdenk57b2d802003-06-27 21:31:46 +000050/*#include <rtc.h> */
51/*#endif */
wdenk38635852002-08-27 05:55:31 +000052
wdenk38635852002-08-27 05:55:31 +000053typedef struct {
Wolfgang Denka1be4762008-05-20 16:00:29 +020054 int flags; /* connected drives ect */
55 unsigned long blnr; /* Logical block nr */
56 uchar drive; /* drive no */
57 uchar cmdlen; /* cmd length */
58 uchar cmd[16]; /* cmd desc */
59 uchar dma; /* if > 0 dma enabled */
60 uchar result[11]; /* status information */
61 uchar resultlen; /* lenght of result */
wdenk38635852002-08-27 05:55:31 +000062} FDC_COMMAND_STRUCT;
Wolfgang Denka1be4762008-05-20 16:00:29 +020063
wdenk38635852002-08-27 05:55:31 +000064/* flags: only the lower 8bit used:
65 * bit 0 if set drive 0 is present
66 * bit 1 if set drive 1 is present
67 * bit 2 if set drive 2 is present
68 * bit 3 if set drive 3 is present
69 * bit 4 if set disk in drive 0 is inserted
70 * bit 5 if set disk in drive 1 is inserted
71 * bit 6 if set disk in drive 2 is inserted
72 * bit 7 if set disk in drive 4 is inserted
73 */
74
wdenk38635852002-08-27 05:55:31 +000075/* cmd indexes */
Wolfgang Denka1be4762008-05-20 16:00:29 +020076#define COMMAND 0
77#define DRIVE 1
wdenk38635852002-08-27 05:55:31 +000078#define CONFIG0 1
Wolfgang Denka1be4762008-05-20 16:00:29 +020079#define SPEC_HUTSRT 1
80#define TRACK 2
wdenk38635852002-08-27 05:55:31 +000081#define CONFIG1 2
82#define SPEC_HLT 2
Wolfgang Denka1be4762008-05-20 16:00:29 +020083#define HEAD 3
wdenk38635852002-08-27 05:55:31 +000084#define CONFIG2 3
Wolfgang Denka1be4762008-05-20 16:00:29 +020085#define SECTOR 4
86#define SECTOR_SIZE 5
87#define LAST_TRACK 6
88#define GAP 7
89#define DTL 8
wdenk38635852002-08-27 05:55:31 +000090/* result indexes */
Wolfgang Denka1be4762008-05-20 16:00:29 +020091#define STATUS_0 0
92#define STATUS_PCN 1
93#define STATUS_1 1
94#define STATUS_2 2
95#define STATUS_TRACK 3
96#define STATUS_HEAD 4
97#define STATUS_SECT 5
98#define STATUS_SECT_SIZE 6
wdenk38635852002-08-27 05:55:31 +000099
100
101/* Register addresses */
102#define FDC_BASE 0x3F0
103#define FDC_SRA FDC_BASE + 0 /* Status Register A */
104#define FDC_SRB FDC_BASE + 1 /* Status Register B */
105#define FDC_DOR FDC_BASE + 2 /* Digital Output Register */
106#define FDC_TDR FDC_BASE + 3 /* Tape Drive Register */
107#define FDC_DSR FDC_BASE + 4 /* Data rate Register */
108#define FDC_MSR FDC_BASE + 4 /* Main Status Register */
109#define FDC_FIFO FDC_BASE + 5 /* FIFO */
110#define FDC_DIR FDC_BASE + 6 /* Digital Input Register */
111#define FDC_CCR FDC_BASE + 7 /* Configuration Control */
112/* Commands */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200113#define FDC_CMD_SENSE_INT 0x08
114#define FDC_CMD_CONFIGURE 0x13
115#define FDC_CMD_SPECIFY 0x03
116#define FDC_CMD_RECALIBRATE 0x07
117#define FDC_CMD_READ 0x06
118#define FDC_CMD_READ_TRACK 0x02
119#define FDC_CMD_READ_ID 0x0A
120#define FDC_CMD_DUMP_REG 0x0E
121#define FDC_CMD_SEEK 0x0F
wdenk38635852002-08-27 05:55:31 +0000122
Wolfgang Denka1be4762008-05-20 16:00:29 +0200123#define FDC_CMD_SENSE_INT_LEN 0x01
124#define FDC_CMD_CONFIGURE_LEN 0x04
125#define FDC_CMD_SPECIFY_LEN 0x03
126#define FDC_CMD_RECALIBRATE_LEN 0x02
127#define FDC_CMD_READ_LEN 0x09
128#define FDC_CMD_READ_TRACK_LEN 0x09
129#define FDC_CMD_READ_ID_LEN 0x02
130#define FDC_CMD_DUMP_REG_LEN 0x01
131#define FDC_CMD_SEEK_LEN 0x03
wdenk38635852002-08-27 05:55:31 +0000132
Wolfgang Denka1be4762008-05-20 16:00:29 +0200133#define FDC_FIFO_THR 0x0C
134#define FDC_FIFO_DIS 0x00
wdenk38635852002-08-27 05:55:31 +0000135#define FDC_IMPLIED_SEEK 0x01
Wolfgang Denka1be4762008-05-20 16:00:29 +0200136#define FDC_POLL_DIS 0x00
137#define FDC_PRE_TRK 0x00
138#define FDC_CONFIGURE FDC_FIFO_THR | (FDC_POLL_DIS<<4) | (FDC_FIFO_DIS<<5) | (FDC_IMPLIED_SEEK << 6)
139#define FDC_MFM_MODE 0x01 /* MFM enable */
140#define FDC_SKIP_MODE 0x00 /* skip enable */
wdenk38635852002-08-27 05:55:31 +0000141
142#define FDC_TIME_OUT 100000 /* time out */
143#define FDC_RW_RETRIES 3 /* read write retries */
144#define FDC_CAL_RETRIES 3 /* calibration and seek retries */
145
146
147/* Disk structure */
148typedef struct {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200149 unsigned int size; /* nr of sectors total */
150 unsigned int sect; /* sectors per track */
151 unsigned int head; /* nr of heads */
152 unsigned int track; /* nr of tracks */
153 unsigned int stretch; /* !=0 means double track steps */
154 unsigned char gap; /* gap1 size */
155 unsigned char rate; /* data rate. |= 0x40 for perpendicular */
156 unsigned char spec1; /* stepping rate, head unload time */
157 unsigned char fmt_gap;/* gap2 size */
158 unsigned char hlt; /* head load time */
159 unsigned char sect_code;/* Sector Size code */
160 const char * name; /* used only for predefined formats */
wdenk38635852002-08-27 05:55:31 +0000161} FD_GEO_STRUCT;
162
163
164/* supported Floppy types (currently only one) */
165const static FD_GEO_STRUCT floppy_type[2] = {
166 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,16,2,"H1440" }, /* 7 1.44MB 3.5" */
167 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00, 0,0,NULL }, /* end of table */
168};
169
170static FDC_COMMAND_STRUCT cmd; /* global command struct */
171
wdenk1272e232002-11-10 22:06:23 +0000172/* If the boot drive number is undefined, we assume it's drive 0 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200173#ifndef CONFIG_SYS_FDC_DRIVE_NUMBER
174#define CONFIG_SYS_FDC_DRIVE_NUMBER 0
wdenk1272e232002-11-10 22:06:23 +0000175#endif
176
177/* Hardware access */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200178#ifndef CONFIG_SYS_ISA_IO_STRIDE
179#define CONFIG_SYS_ISA_IO_STRIDE 1
wdenk1272e232002-11-10 22:06:23 +0000180#endif
181
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200182#ifndef CONFIG_SYS_ISA_IO_OFFSET
183#define CONFIG_SYS_ISA_IO_OFFSET 0
wdenk1272e232002-11-10 22:06:23 +0000184#endif
185
wdenk38635852002-08-27 05:55:31 +0000186/* Supporting Functions */
187/* reads a Register of the FDC */
188unsigned char read_fdc_reg(unsigned int addr)
189{
wdenk591dda52002-11-18 00:14:45 +0000190 volatile unsigned char *val =
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200191 (volatile unsigned char *)(CONFIG_SYS_ISA_IO_BASE_ADDRESS +
192 (addr * CONFIG_SYS_ISA_IO_STRIDE) +
193 CONFIG_SYS_ISA_IO_OFFSET);
wdenk57b2d802003-06-27 21:31:46 +0000194
wdenk1272e232002-11-10 22:06:23 +0000195 return val [0];
wdenk38635852002-08-27 05:55:31 +0000196}
197
198/* writes a Register of the FDC */
199void write_fdc_reg(unsigned int addr, unsigned char val)
200{
wdenk57b2d802003-06-27 21:31:46 +0000201 volatile unsigned char *tmp =
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200202 (volatile unsigned char *)(CONFIG_SYS_ISA_IO_BASE_ADDRESS +
203 (addr * CONFIG_SYS_ISA_IO_STRIDE) +
204 CONFIG_SYS_ISA_IO_OFFSET);
wdenk1272e232002-11-10 22:06:23 +0000205 tmp[0]=val;
wdenk38635852002-08-27 05:55:31 +0000206}
207
208/* waits for an interrupt (polling) */
209int wait_for_fdc_int(void)
210{
211 unsigned long timeout;
212 timeout = FDC_TIME_OUT;
213 while((read_fdc_reg(FDC_SRA)&0x80)==0) {
214 timeout--;
215 udelay(10);
216 if(timeout==0) /* timeout occured */
217 return FALSE;
218 }
219 return TRUE;
220}
221
wdenk38635852002-08-27 05:55:31 +0000222/* reads a byte from the FIFO of the FDC and checks direction and RQM bit
223 of the MSR. returns -1 if timeout, or byte if ok */
224int read_fdc_byte(void)
225{
226 unsigned long timeout;
227 timeout = FDC_TIME_OUT;
228 while((read_fdc_reg(FDC_MSR)&0xC0)!=0xC0) {
229 /* direction out and ready */
230 udelay(10);
231 timeout--;
232 if(timeout==0) /* timeout occured */
233 return -1;
234 }
235 return read_fdc_reg(FDC_FIFO);
236}
237
238/* if the direction of the FIFO is wrong, this routine is used to
239 empty the FIFO. Should _not_ be used */
240int fdc_need_more_output(void)
241{
242 unsigned char c;
243 while((read_fdc_reg(FDC_MSR)&0xC0)==0xC0) {
244 c=(unsigned char)read_fdc_byte();
245 printf("Error: more output: %x\n",c);
246 }
247 return TRUE;
248}
249
250
251/* writes a byte to the FIFO of the FDC and checks direction and RQM bit
252 of the MSR */
253int write_fdc_byte(unsigned char val)
254{
255 unsigned long timeout;
256 timeout = FDC_TIME_OUT;
257 while((read_fdc_reg(FDC_MSR)&0xC0)!=0x80) {
258 /* direction in and ready for byte */
259 timeout--;
260 udelay(10);
261 fdc_need_more_output();
262 if(timeout==0) /* timeout occured */
263 return FALSE;
264 }
265 write_fdc_reg(FDC_FIFO,val);
266 return TRUE;
267}
268
269/* sets up all FDC commands and issues it to the FDC. If
270 the command causes direct results (no Execution Phase)
271 the result is be read as well. */
272
273int fdc_issue_cmd(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
274{
275 int i;
276 unsigned long head,track,sect,timeout;
277 track = pCMD->blnr / (pFG->sect * pFG->head); /* track nr */
278 sect = pCMD->blnr % (pFG->sect * pFG->head); /* remaining blocks */
279 head = sect / pFG->sect; /* head nr */
280 sect = sect % pFG->sect; /* remaining blocks */
281 sect++; /* sectors are 1 based */
wdenk591dda52002-11-18 00:14:45 +0000282 PRINTF("Cmd 0x%02x Track %ld, Head %ld, Sector %ld, Drive %d (blnr %ld)\n",
283 pCMD->cmd[0],track,head,sect,pCMD->drive,pCMD->blnr);
wdenk1272e232002-11-10 22:06:23 +0000284
wdenk38635852002-08-27 05:55:31 +0000285 if(head|=0) { /* max heads = 2 */
286 pCMD->cmd[DRIVE]=pCMD->drive | 0x04; /* head 1 */
287 pCMD->cmd[HEAD]=(unsigned char) head; /* head register */
288 }
289 else {
290 pCMD->cmd[DRIVE]=pCMD->drive; /* head 0 */
291 pCMD->cmd[HEAD]=(unsigned char) head; /* head register */
292 }
293 pCMD->cmd[TRACK]=(unsigned char) track; /* track */
294 switch (pCMD->cmd[COMMAND]) {
295 case FDC_CMD_READ:
296 pCMD->cmd[SECTOR]=(unsigned char) sect; /* sector */
297 pCMD->cmd[SECTOR_SIZE]=pFG->sect_code; /* sector size code */
298 pCMD->cmd[LAST_TRACK]=pFG->sect; /* End of track */
299 pCMD->cmd[GAP]=pFG->gap; /* gap */
300 pCMD->cmd[DTL]=0xFF; /* DTL */
301 pCMD->cmdlen=FDC_CMD_READ_LEN;
302 pCMD->cmd[COMMAND]|=(FDC_MFM_MODE<<6); /* set MFM bit */
303 pCMD->cmd[COMMAND]|=(FDC_SKIP_MODE<<5); /* set Skip bit */
304 pCMD->resultlen=0; /* result only after execution */
305 break;
306 case FDC_CMD_SEEK:
307 pCMD->cmdlen=FDC_CMD_SEEK_LEN;
308 pCMD->resultlen=0; /* no result */
309 break;
310 case FDC_CMD_CONFIGURE:
311 pCMD->cmd[CONFIG0]=0;
312 pCMD->cmd[CONFIG1]=FDC_CONFIGURE; /* FIFO Threshold, Poll, Enable FIFO */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200313 pCMD->cmd[CONFIG2]=FDC_PRE_TRK; /* Precompensation Track */
wdenk38635852002-08-27 05:55:31 +0000314 pCMD->cmdlen=FDC_CMD_CONFIGURE_LEN;
315 pCMD->resultlen=0; /* no result */
316 break;
317 case FDC_CMD_SPECIFY:
318 pCMD->cmd[SPEC_HUTSRT]=pFG->spec1;
319 pCMD->cmd[SPEC_HLT]=(pFG->hlt)<<1; /* head load time */
320 if(pCMD->dma==0)
321 pCMD->cmd[SPEC_HLT]|=0x1; /* no dma */
322 pCMD->cmdlen=FDC_CMD_SPECIFY_LEN;
323 pCMD->resultlen=0; /* no result */
324 break;
325 case FDC_CMD_DUMP_REG:
326 pCMD->cmdlen=FDC_CMD_DUMP_REG_LEN;
327 pCMD->resultlen=10; /* 10 byte result */
328 break;
329 case FDC_CMD_READ_ID:
330 pCMD->cmd[COMMAND]|=(FDC_MFM_MODE<<6); /* set MFM bit */
331 pCMD->cmdlen=FDC_CMD_READ_ID_LEN;
332 pCMD->resultlen=7; /* 7 byte result */
333 break;
334 case FDC_CMD_RECALIBRATE:
335 pCMD->cmd[DRIVE]&=0x03; /* don't set the head bit */
336 pCMD->cmdlen=FDC_CMD_RECALIBRATE_LEN;
337 pCMD->resultlen=0; /* no result */
338 break;
339 break;
340 case FDC_CMD_SENSE_INT:
341 pCMD->cmdlen=FDC_CMD_SENSE_INT_LEN;
342 pCMD->resultlen=2;
343 break;
344 }
345 for(i=0;i<pCMD->cmdlen;i++) {
346 /* PRINTF("write cmd%d = 0x%02X\n",i,pCMD->cmd[i]); */
347 if(write_fdc_byte(pCMD->cmd[i])==FALSE) {
348 PRINTF("Error: timeout while issue cmd%d\n",i);
349 return FALSE;
350 }
351 }
352 timeout=FDC_TIME_OUT;
353 for(i=0;i<pCMD->resultlen;i++) {
354 while((read_fdc_reg(FDC_MSR)&0xC0)!=0xC0) {
355 timeout--;
356 if(timeout==0) {
357 PRINTF(" timeout while reading result%d MSR=0x%02X\n",i,read_fdc_reg(FDC_MSR));
358 return FALSE;
359 }
360 }
361 pCMD->result[i]=(unsigned char)read_fdc_byte();
362 }
363 return TRUE;
364}
365
366/* selects the drive assigned in the cmd structur and
367 switches on the Motor */
368void select_fdc_drive(FDC_COMMAND_STRUCT *pCMD)
369{
370 unsigned char val;
371
372 val=(1<<(4+pCMD->drive))|pCMD->drive|0xC; /* set reset, dma gate and motor bits */
373 if((read_fdc_reg(FDC_DOR)&val)!=val) {
374 write_fdc_reg(FDC_DOR,val);
375 for(val=0;val<255;val++)
376 udelay(500); /* wait some time to start motor */
377 }
378}
379
380/* switches off the Motor of the specified drive */
381void stop_fdc_drive(FDC_COMMAND_STRUCT *pCMD)
382{
383 unsigned char val;
384
385 val=(1<<(4+pCMD->drive))|pCMD->drive; /* sets motor bits */
386 write_fdc_reg(FDC_DOR,(read_fdc_reg(FDC_DOR)&~val));
387}
388
389/* issues a recalibrate command, waits for interrupt and
390 * issues a sense_interrupt */
391int fdc_recalibrate(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
392{
393 pCMD->cmd[COMMAND]=FDC_CMD_RECALIBRATE;
394 if(fdc_issue_cmd(pCMD,pFG)==FALSE)
395 return FALSE;
396 while(wait_for_fdc_int()!=TRUE);
397 pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
398 return(fdc_issue_cmd(pCMD,pFG));
399}
400
401/* issues a recalibrate command, waits for interrupt and
402 * issues a sense_interrupt */
403int fdc_seek(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
404{
405 pCMD->cmd[COMMAND]=FDC_CMD_SEEK;
406 if(fdc_issue_cmd(pCMD,pFG)==FALSE)
407 return FALSE;
408 while(wait_for_fdc_int()!=TRUE);
409 pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
410 return(fdc_issue_cmd(pCMD,pFG));
411}
412
wdenk38635852002-08-27 05:55:31 +0000413/* terminates current command, by not servicing the FIFO
414 * waits for interrupt and fills in the result bytes */
415int fdc_terminate(FDC_COMMAND_STRUCT *pCMD)
416{
417 int i;
418 for(i=0;i<100;i++)
419 udelay(500); /* wait 500usec for fifo overrun */
420 while((read_fdc_reg(FDC_SRA)&0x80)==0x00); /* wait as long as no int has occured */
421 for(i=0;i<7;i++) {
422 pCMD->result[i]=(unsigned char)read_fdc_byte();
423 }
424 return TRUE;
425}
426
427/* reads data from FDC, seek commands are issued automatic */
428int fdc_read_data(unsigned char *buffer, unsigned long blocks,FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
429{
430 /* first seek to start address */
Wolfgang Denkd8c56492011-11-09 09:29:02 +0000431 unsigned long len,readblk,i,timeout,ii,offset;
432 unsigned char c,retriesrw,retriescal;
wdenk38635852002-08-27 05:55:31 +0000433 unsigned char *bufferw; /* working buffer */
434 int sect_size;
435 int flags;
436
437 flags=disable_interrupts(); /* switch off all Interrupts */
438 select_fdc_drive(pCMD); /* switch on drive */
439 sect_size=0x080<<pFG->sect_code;
440 retriesrw=0;
441 retriescal=0;
442 offset=0;
443 if(fdc_seek(pCMD,pFG)==FALSE) {
444 stop_fdc_drive(pCMD);
Wolfgang Denkd8c56492011-11-09 09:29:02 +0000445 if (flags)
446 enable_interrupts();
wdenk38635852002-08-27 05:55:31 +0000447 return FALSE;
448 }
449 if((pCMD->result[STATUS_0]&0x20)!=0x20) {
450 printf("Seek error Status: %02X\n",pCMD->result[STATUS_0]);
451 stop_fdc_drive(pCMD);
Wolfgang Denkd8c56492011-11-09 09:29:02 +0000452 if (flags)
453 enable_interrupts();
wdenk38635852002-08-27 05:55:31 +0000454 return FALSE;
455 }
wdenk38635852002-08-27 05:55:31 +0000456 /* now determine the next seek point */
Wolfgang Denkd8c56492011-11-09 09:29:02 +0000457 /* lastblk=pCMD->blnr + blocks; */
wdenk38635852002-08-27 05:55:31 +0000458 /* readblk=(pFG->head*pFG->sect)-(pCMD->blnr%(pFG->head*pFG->sect)); */
459 readblk=pFG->sect-(pCMD->blnr%pFG->sect);
460 PRINTF("1st nr of block possible read %ld start %ld\n",readblk,pCMD->blnr);
461 if(readblk>blocks) /* is end within 1st track */
462 readblk=blocks; /* yes, correct it */
463 PRINTF("we read %ld blocks start %ld\n",readblk,pCMD->blnr);
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200464 bufferw = &buffer[0]; /* setup working buffer */
wdenk38635852002-08-27 05:55:31 +0000465 do {
466retryrw:
467 len=sect_size * readblk;
468 pCMD->cmd[COMMAND]=FDC_CMD_READ;
469 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
470 stop_fdc_drive(pCMD);
Wolfgang Denkd8c56492011-11-09 09:29:02 +0000471 if (flags)
472 enable_interrupts();
wdenk38635852002-08-27 05:55:31 +0000473 return FALSE;
474 }
475 for (i=0;i<len;i++) {
476 timeout=FDC_TIME_OUT;
477 do {
478 c=read_fdc_reg(FDC_MSR);
479 if((c&0xC0)==0xC0) {
480 bufferw[i]=read_fdc_reg(FDC_FIFO);
481 break;
482 }
483 if((c&0xC0)==0x80) { /* output */
484 PRINTF("Transfer error transfered: at %ld, MSR=%02X\n",i,c);
485 if(i>6) {
486 for(ii=0;ii<7;ii++) {
487 pCMD->result[ii]=bufferw[(i-7+ii)];
488 } /* for */
489 }
490 if(retriesrw++>FDC_RW_RETRIES) {
491 if (retriescal++>FDC_CAL_RETRIES) {
492 stop_fdc_drive(pCMD);
Wolfgang Denkd8c56492011-11-09 09:29:02 +0000493 if (flags)
494 enable_interrupts();
wdenk38635852002-08-27 05:55:31 +0000495 return FALSE;
496 }
497 else {
498 PRINTF(" trying to recalibrate Try %d\n",retriescal);
499 if(fdc_recalibrate(pCMD,pFG)==FALSE) {
500 stop_fdc_drive(pCMD);
Wolfgang Denkd8c56492011-11-09 09:29:02 +0000501 if (flags)
502 enable_interrupts();
wdenk38635852002-08-27 05:55:31 +0000503 return FALSE;
504 }
505 retriesrw=0;
506 goto retrycal;
507 } /* else >FDC_CAL_RETRIES */
508 }
509 else {
510 PRINTF("Read retry %d\n",retriesrw);
511 goto retryrw;
512 } /* else >FDC_RW_RETRIES */
513 }/* if output */
514 timeout--;
515 }while(TRUE);
516 } /* for len */
517 /* the last sector of a track or all data has been read,
518 * we need to get the results */
519 fdc_terminate(pCMD);
520 offset+=(sect_size*readblk); /* set up buffer pointer */
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200521 bufferw = &buffer[offset];
wdenk38635852002-08-27 05:55:31 +0000522 pCMD->blnr+=readblk; /* update current block nr */
523 blocks-=readblk; /* update blocks */
524 if(blocks==0)
525 break; /* we are finish */
526 /* setup new read blocks */
527 /* readblk=pFG->head*pFG->sect; */
528 readblk=pFG->sect;
529 if(readblk>blocks)
530 readblk=blocks;
531retrycal:
532 /* a seek is necessary */
533 if(fdc_seek(pCMD,pFG)==FALSE) {
534 stop_fdc_drive(pCMD);
Wolfgang Denkd8c56492011-11-09 09:29:02 +0000535 if (flags)
536 enable_interrupts();
wdenk38635852002-08-27 05:55:31 +0000537 return FALSE;
538 }
539 if((pCMD->result[STATUS_0]&0x20)!=0x20) {
540 PRINTF("Seek error Status: %02X\n",pCMD->result[STATUS_0]);
541 stop_fdc_drive(pCMD);
542 return FALSE;
543 }
wdenk38635852002-08-27 05:55:31 +0000544 }while(TRUE); /* start over */
545 stop_fdc_drive(pCMD); /* switch off drive */
Wolfgang Denkd8c56492011-11-09 09:29:02 +0000546 if (flags)
547 enable_interrupts();
wdenk38635852002-08-27 05:55:31 +0000548 return TRUE;
549}
550
551/* Scan all drives and check if drive is present and disk is inserted */
552int fdc_check_drive(FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
553{
554 int i,drives,state;
555 /* OK procedure of data book is satisfied.
556 * trying to get some information over the drives */
557 state=0; /* no drives, no disks */
558 for(drives=0;drives<4;drives++) {
559 pCMD->drive=drives;
560 select_fdc_drive(pCMD);
561 pCMD->blnr=0; /* set to the 1st block */
562 if(fdc_recalibrate(pCMD,pFG)==FALSE)
wdenk1272e232002-11-10 22:06:23 +0000563 continue;
wdenk38635852002-08-27 05:55:31 +0000564 if((pCMD->result[STATUS_0]&0x10)==0x10)
wdenk1272e232002-11-10 22:06:23 +0000565 continue;
wdenk38635852002-08-27 05:55:31 +0000566 /* ok drive connected check for disk */
567 state|=(1<<drives);
568 pCMD->blnr=pFG->size; /* set to the last block */
569 if(fdc_seek(pCMD,pFG)==FALSE)
wdenk1272e232002-11-10 22:06:23 +0000570 continue;
wdenk38635852002-08-27 05:55:31 +0000571 pCMD->blnr=0; /* set to the 1st block */
572 if(fdc_recalibrate(pCMD,pFG)==FALSE)
wdenk1272e232002-11-10 22:06:23 +0000573 continue;
wdenk38635852002-08-27 05:55:31 +0000574 pCMD->cmd[COMMAND]=FDC_CMD_READ_ID;
575 if(fdc_issue_cmd(pCMD,pFG)==FALSE)
wdenk1272e232002-11-10 22:06:23 +0000576 continue;
wdenk38635852002-08-27 05:55:31 +0000577 state|=(0x10<<drives);
578 }
579 stop_fdc_drive(pCMD);
580 for(i=0;i<4;i++) {
581 PRINTF("Floppy Drive %d %sconnected %sDisk inserted %s\n",i,
582 ((state&(1<<i))==(1<<i)) ? "":"not ",
583 ((state&(0x10<<i))==(0x10<<i)) ? "":"no ",
584 ((state&(0x10<<i))==(0x10<<i)) ? pFG->name : "");
585 }
586 pCMD->flags=state;
587 return TRUE;
588}
589
590
591/**************************************************************************
592* int fdc_setup
593* setup the fdc according the datasheet
594* assuming in PS2 Mode
595*/
wdenk591dda52002-11-18 00:14:45 +0000596int fdc_setup(int drive, FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
wdenk38635852002-08-27 05:55:31 +0000597{
wdenk38635852002-08-27 05:55:31 +0000598 int i;
wdenk1272e232002-11-10 22:06:23 +0000599
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200600#ifdef CONFIG_SYS_FDC_HW_INIT
wdenk57b2d802003-06-27 21:31:46 +0000601 fdc_hw_init ();
wdenk1272e232002-11-10 22:06:23 +0000602#endif
wdenk38635852002-08-27 05:55:31 +0000603 /* first, we reset the FDC via the DOR */
604 write_fdc_reg(FDC_DOR,0x00);
605 for(i=0; i<255; i++) /* then we wait some time */
606 udelay(500);
607 /* then, we clear the reset in the DOR */
wdenk591dda52002-11-18 00:14:45 +0000608 pCMD->drive=drive;
wdenk38635852002-08-27 05:55:31 +0000609 select_fdc_drive(pCMD);
610 /* initialize the CCR */
611 write_fdc_reg(FDC_CCR,pFG->rate);
612 /* then initialize the DSR */
613 write_fdc_reg(FDC_DSR,pFG->rate);
614 if(wait_for_fdc_int()==FALSE) {
615 PRINTF("Time Out after writing CCR\n");
616 return FALSE;
617 }
618 /* now issue sense Interrupt and status command
619 * assuming only one drive present (drive 0) */
620 pCMD->dma=0; /* we don't use any dma at all */
621 for(i=0;i<4;i++) {
622 /* issue sense interrupt for all 4 possible drives */
623 pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
624 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
625 PRINTF("Sense Interrupt for drive %d failed\n",i);
626 }
627 }
wdenk591dda52002-11-18 00:14:45 +0000628 /* issue the configure command */
629 pCMD->drive=drive;
wdenk38635852002-08-27 05:55:31 +0000630 select_fdc_drive(pCMD);
631 pCMD->cmd[COMMAND]=FDC_CMD_CONFIGURE;
632 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
633 PRINTF(" configure timeout\n");
634 stop_fdc_drive(pCMD);
635 return FALSE;
636 }
637 /* issue specify command */
638 pCMD->cmd[COMMAND]=FDC_CMD_SPECIFY;
639 if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
640 PRINTF(" specify timeout\n");
641 stop_fdc_drive(pCMD);
642 return FALSE;
643
644 }
645 /* then, we clear the reset in the DOR */
646 /* fdc_check_drive(pCMD,pFG); */
647 /* write_fdc_reg(FDC_DOR,0x04); */
wdenk452cfd62002-11-19 11:04:11 +0000648
wdenk38635852002-08-27 05:55:31 +0000649 return TRUE;
650}
wdenk591dda52002-11-18 00:14:45 +0000651
Jon Loeliger54324d02007-07-08 17:51:39 -0500652#if defined(CONFIG_CMD_FDOS)
wdenk591dda52002-11-18 00:14:45 +0000653
654/* Low level functions for the Floppy-DOS layer */
655
656/**************************************************************************
657* int fdc_fdos_init
wdenk57b2d802003-06-27 21:31:46 +0000658* initialize the FDC layer
659*
wdenk591dda52002-11-18 00:14:45 +0000660*/
661int fdc_fdos_init (int drive)
662{
663 FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
664 FDC_COMMAND_STRUCT *pCMD = &cmd;
wdenk57b2d802003-06-27 21:31:46 +0000665
wdenk591dda52002-11-18 00:14:45 +0000666 /* setup FDC and scan for drives */
667 if(fdc_setup(drive,pCMD,pFG)==FALSE) {
668 printf("\n** Error in setup FDC **\n");
669 return FALSE;
670 }
671 if(fdc_check_drive(pCMD,pFG)==FALSE) {
672 printf("\n** Error in check_drives **\n");
673 return FALSE;
674 }
675 if((pCMD->flags&(1<<drive))==0) {
676 /* drive not available */
677 printf("\n** Drive %d not available **\n",drive);
678 return FALSE;
679 }
680 if((pCMD->flags&(0x10<<drive))==0) {
681 /* no disk inserted */
682 printf("\n** No disk inserted in drive %d **\n",drive);
683 return FALSE;
684 }
685 /* ok, we have a valid source */
686 pCMD->drive=drive;
wdenk38635852002-08-27 05:55:31 +0000687
wdenk591dda52002-11-18 00:14:45 +0000688 /* read first block */
689 pCMD->blnr=0;
wdenk57b2d802003-06-27 21:31:46 +0000690 return TRUE;
wdenk591dda52002-11-18 00:14:45 +0000691}
692/**************************************************************************
693* int fdc_fdos_seek
wdenk57b2d802003-06-27 21:31:46 +0000694* parameter is a block number
wdenk591dda52002-11-18 00:14:45 +0000695*/
696int fdc_fdos_seek (int where)
697{
698 FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
699 FDC_COMMAND_STRUCT *pCMD = &cmd;
700
wdenk57b2d802003-06-27 21:31:46 +0000701 pCMD -> blnr = where ;
702 return (fdc_seek (pCMD, pFG));
wdenk591dda52002-11-18 00:14:45 +0000703}
704/**************************************************************************
705* int fdc_fdos_read
706* the length is in block number
707*/
708int fdc_fdos_read (void *buffer, int len)
709{
710 FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
711 FDC_COMMAND_STRUCT *pCMD = &cmd;
712
wdenk57b2d802003-06-27 21:31:46 +0000713 return (fdc_read_data (buffer, len, pCMD, pFG));
wdenk591dda52002-11-18 00:14:45 +0000714}
Jon Loeliger54324d02007-07-08 17:51:39 -0500715#endif
wdenk591dda52002-11-18 00:14:45 +0000716
Jon Loeliger54324d02007-07-08 17:51:39 -0500717#if defined(CONFIG_CMD_FDC)
wdenk38635852002-08-27 05:55:31 +0000718/****************************************************************************
719 * main routine do_fdcboot
720 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200721int do_fdcboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000722{
723 FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
724 FDC_COMMAND_STRUCT *pCMD = &cmd;
wdenk57b2d802003-06-27 21:31:46 +0000725 unsigned long addr,imsize;
wdenk38635852002-08-27 05:55:31 +0000726 image_header_t *hdr; /* used for fdc boot */
727 unsigned char boot_drive;
728 int i,nrofblk;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100729#if defined(CONFIG_FIT)
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200730 const void *fit_hdr = NULL;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100731#endif
wdenk38635852002-08-27 05:55:31 +0000732
733 switch (argc) {
734 case 1:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200735 addr = CONFIG_SYS_LOAD_ADDR;
736 boot_drive=CONFIG_SYS_FDC_DRIVE_NUMBER;
wdenk38635852002-08-27 05:55:31 +0000737 break;
738 case 2:
739 addr = simple_strtoul(argv[1], NULL, 16);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200740 boot_drive=CONFIG_SYS_FDC_DRIVE_NUMBER;
wdenk38635852002-08-27 05:55:31 +0000741 break;
742 case 3:
743 addr = simple_strtoul(argv[1], NULL, 16);
744 boot_drive=simple_strtoul(argv[2], NULL, 10);
745 break;
746 default:
Wolfgang Denk3b683112010-07-17 01:06:04 +0200747 return cmd_usage(cmdtp);
wdenk38635852002-08-27 05:55:31 +0000748 }
749 /* setup FDC and scan for drives */
wdenk591dda52002-11-18 00:14:45 +0000750 if(fdc_setup(boot_drive,pCMD,pFG)==FALSE) {
wdenk38635852002-08-27 05:55:31 +0000751 printf("\n** Error in setup FDC **\n");
752 return 1;
753 }
754 if(fdc_check_drive(pCMD,pFG)==FALSE) {
755 printf("\n** Error in check_drives **\n");
756 return 1;
757 }
758 if((pCMD->flags&(1<<boot_drive))==0) {
759 /* drive not available */
760 printf("\n** Drive %d not availabe **\n",boot_drive);
761 return 1;
762 }
763 if((pCMD->flags&(0x10<<boot_drive))==0) {
764 /* no disk inserted */
765 printf("\n** No disk inserted in drive %d **\n",boot_drive);
766 return 1;
767 }
768 /* ok, we have a valid source */
769 pCMD->drive=boot_drive;
770 /* read first block */
771 pCMD->blnr=0;
772 if(fdc_read_data((unsigned char *)addr,1,pCMD,pFG)==FALSE) {
773 printf("\nRead error:");
774 for(i=0;i<7;i++)
775 printf("result%d: 0x%02X\n",i,pCMD->result[i]);
776 return 1;
777 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100778
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100779 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100780 case IMAGE_FORMAT_LEGACY:
781 hdr = (image_header_t *)addr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100782 image_print_contents (hdr);
783
784 imsize = image_get_image_size (hdr);
785 break;
786#if defined(CONFIG_FIT)
787 case IMAGE_FORMAT_FIT:
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100788 fit_hdr = (const void *)addr;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100789 puts ("Fit image detected...\n");
790
791 imsize = fit_get_size (fit_hdr);
792 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100793#endif
794 default:
795 puts ("** Unknown image type\n");
wdenk38635852002-08-27 05:55:31 +0000796 return 1;
797 }
wdenk38635852002-08-27 05:55:31 +0000798
wdenk38635852002-08-27 05:55:31 +0000799 nrofblk=imsize/512;
800 if((imsize%512)>0)
801 nrofblk++;
802 printf("Loading %ld Bytes (%d blocks) at 0x%08lx..\n",imsize,nrofblk,addr);
803 pCMD->blnr=0;
804 if(fdc_read_data((unsigned char *)addr,nrofblk,pCMD,pFG)==FALSE) {
805 /* read image block */
806 printf("\nRead error:");
807 for(i=0;i<7;i++)
808 printf("result%d: 0x%02X\n",i,pCMD->result[i]);
809 return 1;
810 }
811 printf("OK %ld Bytes loaded.\n",imsize);
812
813 flush_cache (addr, imsize);
wdenk38635852002-08-27 05:55:31 +0000814
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100815#if defined(CONFIG_FIT)
816 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200817 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
818 if (!fit_check_format (fit_hdr)) {
819 puts ("** Bad FIT image format\n");
820 return 1;
821 }
822 fit_print_contents (fit_hdr);
823 }
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100824#endif
825
826 /* Loading ok, update default load address */
wdenk38635852002-08-27 05:55:31 +0000827 load_addr = addr;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100828
Mike Frysinger194c2e82011-06-05 13:43:02 +0000829 return bootm_maybe_autostart(cmdtp, argv[0]);
wdenk38635852002-08-27 05:55:31 +0000830}
831
wdenkf287a242003-07-01 21:06:45 +0000832U_BOOT_CMD(
833 fdcboot, 3, 1, do_fdcboot,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600834 "boot from floppy device",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200835 "loadAddr drive"
wdenk57b2d802003-06-27 21:31:46 +0000836);
837#endif