blob: 1cfb656bc07216304014852de1f2d980db03753f [file] [log] [blame]
wdenk38635852002-08-27 05:55:31 +00001/*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG, d.peter@mpl.ch.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk38635852002-08-27 05:55:31 +00006 */
7/*
8 * Floppy Disk support
9 */
10
11#include <common.h>
12#include <config.h>
13#include <command.h>
14#include <image.h>
15
16
17#undef FDC_DEBUG
18
19#ifdef FDC_DEBUG
20#define PRINTF(fmt,args...) printf (fmt ,##args)
21#else
22#define PRINTF(fmt,args...)
23#endif
24
Jon Loeliger54324d02007-07-08 17:51:39 -050025/*#if defined(CONFIG_CMD_DATE) */
wdenk57b2d802003-06-27 21:31:46 +000026/*#include <rtc.h> */
27/*#endif */
wdenk38635852002-08-27 05:55:31 +000028
wdenk38635852002-08-27 05:55:31 +000029typedef struct {
Wolfgang Denka1be4762008-05-20 16:00:29 +020030 int flags; /* connected drives ect */
31 unsigned long blnr; /* Logical block nr */
32 uchar drive; /* drive no */
33 uchar cmdlen; /* cmd length */
34 uchar cmd[16]; /* cmd desc */
35 uchar dma; /* if > 0 dma enabled */
36 uchar result[11]; /* status information */
37 uchar resultlen; /* lenght of result */
wdenk38635852002-08-27 05:55:31 +000038} FDC_COMMAND_STRUCT;
Wolfgang Denka1be4762008-05-20 16:00:29 +020039
wdenk38635852002-08-27 05:55:31 +000040/* flags: only the lower 8bit used:
41 * bit 0 if set drive 0 is present
42 * bit 1 if set drive 1 is present
43 * bit 2 if set drive 2 is present
44 * bit 3 if set drive 3 is present
45 * bit 4 if set disk in drive 0 is inserted
46 * bit 5 if set disk in drive 1 is inserted
47 * bit 6 if set disk in drive 2 is inserted
48 * bit 7 if set disk in drive 4 is inserted
49 */
50
wdenk38635852002-08-27 05:55:31 +000051/* cmd indexes */
Wolfgang Denka1be4762008-05-20 16:00:29 +020052#define COMMAND 0
53#define DRIVE 1
wdenk38635852002-08-27 05:55:31 +000054#define CONFIG0 1
Wolfgang Denka1be4762008-05-20 16:00:29 +020055#define SPEC_HUTSRT 1
56#define TRACK 2
wdenk38635852002-08-27 05:55:31 +000057#define CONFIG1 2
58#define SPEC_HLT 2
Wolfgang Denka1be4762008-05-20 16:00:29 +020059#define HEAD 3
wdenk38635852002-08-27 05:55:31 +000060#define CONFIG2 3
Wolfgang Denka1be4762008-05-20 16:00:29 +020061#define SECTOR 4
62#define SECTOR_SIZE 5
63#define LAST_TRACK 6
64#define GAP 7
65#define DTL 8
wdenk38635852002-08-27 05:55:31 +000066/* result indexes */
Wolfgang Denka1be4762008-05-20 16:00:29 +020067#define STATUS_0 0
68#define STATUS_PCN 1
69#define STATUS_1 1
70#define STATUS_2 2
71#define STATUS_TRACK 3
72#define STATUS_HEAD 4
73#define STATUS_SECT 5
74#define STATUS_SECT_SIZE 6
wdenk38635852002-08-27 05:55:31 +000075
76
77/* Register addresses */
78#define FDC_BASE 0x3F0
79#define FDC_SRA FDC_BASE + 0 /* Status Register A */
80#define FDC_SRB FDC_BASE + 1 /* Status Register B */
81#define FDC_DOR FDC_BASE + 2 /* Digital Output Register */
82#define FDC_TDR FDC_BASE + 3 /* Tape Drive Register */
83#define FDC_DSR FDC_BASE + 4 /* Data rate Register */
84#define FDC_MSR FDC_BASE + 4 /* Main Status Register */
85#define FDC_FIFO FDC_BASE + 5 /* FIFO */
86#define FDC_DIR FDC_BASE + 6 /* Digital Input Register */
87#define FDC_CCR FDC_BASE + 7 /* Configuration Control */
88/* Commands */
Wolfgang Denka1be4762008-05-20 16:00:29 +020089#define FDC_CMD_SENSE_INT 0x08
90#define FDC_CMD_CONFIGURE 0x13
91#define FDC_CMD_SPECIFY 0x03
92#define FDC_CMD_RECALIBRATE 0x07
93#define FDC_CMD_READ 0x06
94#define FDC_CMD_READ_TRACK 0x02
95#define FDC_CMD_READ_ID 0x0A
96#define FDC_CMD_DUMP_REG 0x0E
97#define FDC_CMD_SEEK 0x0F
wdenk38635852002-08-27 05:55:31 +000098
Wolfgang Denka1be4762008-05-20 16:00:29 +020099#define FDC_CMD_SENSE_INT_LEN 0x01
100#define FDC_CMD_CONFIGURE_LEN 0x04
101#define FDC_CMD_SPECIFY_LEN 0x03
102#define FDC_CMD_RECALIBRATE_LEN 0x02
103#define FDC_CMD_READ_LEN 0x09
104#define FDC_CMD_READ_TRACK_LEN 0x09
105#define FDC_CMD_READ_ID_LEN 0x02
106#define FDC_CMD_DUMP_REG_LEN 0x01
107#define FDC_CMD_SEEK_LEN 0x03
wdenk38635852002-08-27 05:55:31 +0000108
Wolfgang Denka1be4762008-05-20 16:00:29 +0200109#define FDC_FIFO_THR 0x0C
110#define FDC_FIFO_DIS 0x00
wdenk38635852002-08-27 05:55:31 +0000111#define FDC_IMPLIED_SEEK 0x01
Wolfgang Denka1be4762008-05-20 16:00:29 +0200112#define FDC_POLL_DIS 0x00
113#define FDC_PRE_TRK 0x00
114#define FDC_CONFIGURE FDC_FIFO_THR | (FDC_POLL_DIS<<4) | (FDC_FIFO_DIS<<5) | (FDC_IMPLIED_SEEK << 6)
115#define FDC_MFM_MODE 0x01 /* MFM enable */
116#define FDC_SKIP_MODE 0x00 /* skip enable */
wdenk38635852002-08-27 05:55:31 +0000117
118#define FDC_TIME_OUT 100000 /* time out */
119#define FDC_RW_RETRIES 3 /* read write retries */
120#define FDC_CAL_RETRIES 3 /* calibration and seek retries */
121
122
123/* Disk structure */
124typedef struct {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200125 unsigned int size; /* nr of sectors total */
126 unsigned int sect; /* sectors per track */
127 unsigned int head; /* nr of heads */
128 unsigned int track; /* nr of tracks */
129 unsigned int stretch; /* !=0 means double track steps */
130 unsigned char gap; /* gap1 size */
131 unsigned char rate; /* data rate. |= 0x40 for perpendicular */
132 unsigned char spec1; /* stepping rate, head unload time */
133 unsigned char fmt_gap;/* gap2 size */
134 unsigned char hlt; /* head load time */
135 unsigned char sect_code;/* Sector Size code */
136 const char * name; /* used only for predefined formats */
wdenk38635852002-08-27 05:55:31 +0000137} FD_GEO_STRUCT;
138
139
140/* supported Floppy types (currently only one) */
141const static FD_GEO_STRUCT floppy_type[2] = {
142 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,16,2,"H1440" }, /* 7 1.44MB 3.5" */
143 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00, 0,0,NULL }, /* end of table */
144};
145
146static FDC_COMMAND_STRUCT cmd; /* global command struct */
147
wdenk1272e232002-11-10 22:06:23 +0000148/* If the boot drive number is undefined, we assume it's drive 0 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200149#ifndef CONFIG_SYS_FDC_DRIVE_NUMBER
150#define CONFIG_SYS_FDC_DRIVE_NUMBER 0
wdenk1272e232002-11-10 22:06:23 +0000151#endif
152
153/* Hardware access */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200154#ifndef CONFIG_SYS_ISA_IO_STRIDE
155#define CONFIG_SYS_ISA_IO_STRIDE 1
wdenk1272e232002-11-10 22:06:23 +0000156#endif
157
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200158#ifndef CONFIG_SYS_ISA_IO_OFFSET
159#define CONFIG_SYS_ISA_IO_OFFSET 0
wdenk1272e232002-11-10 22:06:23 +0000160#endif
161
wdenk38635852002-08-27 05:55:31 +0000162/* Supporting Functions */
163/* reads a Register of the FDC */
164unsigned char read_fdc_reg(unsigned int addr)
165{
wdenk591dda52002-11-18 00:14:45 +0000166 volatile unsigned char *val =
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200167 (volatile unsigned char *)(CONFIG_SYS_ISA_IO_BASE_ADDRESS +
168 (addr * CONFIG_SYS_ISA_IO_STRIDE) +
169 CONFIG_SYS_ISA_IO_OFFSET);
wdenk57b2d802003-06-27 21:31:46 +0000170
wdenk1272e232002-11-10 22:06:23 +0000171 return val [0];
wdenk38635852002-08-27 05:55:31 +0000172}
173
174/* writes a Register of the FDC */
175void write_fdc_reg(unsigned int addr, unsigned char val)
176{
wdenk57b2d802003-06-27 21:31:46 +0000177 volatile unsigned char *tmp =
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200178 (volatile unsigned char *)(CONFIG_SYS_ISA_IO_BASE_ADDRESS +
179 (addr * CONFIG_SYS_ISA_IO_STRIDE) +
180 CONFIG_SYS_ISA_IO_OFFSET);
wdenk1272e232002-11-10 22:06:23 +0000181 tmp[0]=val;
wdenk38635852002-08-27 05:55:31 +0000182}
183
184/* waits for an interrupt (polling) */
185int wait_for_fdc_int(void)
186{
187 unsigned long timeout;
188 timeout = FDC_TIME_OUT;
189 while((read_fdc_reg(FDC_SRA)&0x80)==0) {
190 timeout--;
191 udelay(10);
192 if(timeout==0) /* timeout occured */
York Sun4a598092013-04-01 11:29:11 -0700193 return false;
wdenk38635852002-08-27 05:55:31 +0000194 }
York Sun4a598092013-04-01 11:29:11 -0700195 return true;
wdenk38635852002-08-27 05:55:31 +0000196}
197
wdenk38635852002-08-27 05:55:31 +0000198/* reads a byte from the FIFO of the FDC and checks direction and RQM bit
199 of the MSR. returns -1 if timeout, or byte if ok */
200int read_fdc_byte(void)
201{
202 unsigned long timeout;
203 timeout = FDC_TIME_OUT;
204 while((read_fdc_reg(FDC_MSR)&0xC0)!=0xC0) {
205 /* direction out and ready */
206 udelay(10);
207 timeout--;
208 if(timeout==0) /* timeout occured */
209 return -1;
210 }
211 return read_fdc_reg(FDC_FIFO);
212}
213
214/* if the direction of the FIFO is wrong, this routine is used to
215 empty the FIFO. Should _not_ be used */
216int fdc_need_more_output(void)
217{
218 unsigned char c;
219 while((read_fdc_reg(FDC_MSR)&0xC0)==0xC0) {
220 c=(unsigned char)read_fdc_byte();
221 printf("Error: more output: %x\n",c);
222 }
York Sun4a598092013-04-01 11:29:11 -0700223 return true;
wdenk38635852002-08-27 05:55:31 +0000224}
225
226
227/* writes a byte to the FIFO of the FDC and checks direction and RQM bit
228 of the MSR */
229int write_fdc_byte(unsigned char val)
230{
231 unsigned long timeout;
232 timeout = FDC_TIME_OUT;
233 while((read_fdc_reg(FDC_MSR)&0xC0)!=0x80) {
234 /* direction in and ready for byte */
235 timeout--;
236 udelay(10);
237 fdc_need_more_output();
238 if(timeout==0) /* timeout occured */
York Sun4a598092013-04-01 11:29:11 -0700239 return false;
wdenk38635852002-08-27 05:55:31 +0000240 }
241 write_fdc_reg(FDC_FIFO,val);
York Sun4a598092013-04-01 11:29:11 -0700242 return true;
wdenk38635852002-08-27 05:55:31 +0000243}
244
245/* sets up all FDC commands and issues it to the FDC. If
246 the command causes direct results (no Execution Phase)
247 the result is be read as well. */
248
249int fdc_issue_cmd(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
250{
251 int i;
252 unsigned long head,track,sect,timeout;
253 track = pCMD->blnr / (pFG->sect * pFG->head); /* track nr */
254 sect = pCMD->blnr % (pFG->sect * pFG->head); /* remaining blocks */
255 head = sect / pFG->sect; /* head nr */
256 sect = sect % pFG->sect; /* remaining blocks */
257 sect++; /* sectors are 1 based */
wdenk591dda52002-11-18 00:14:45 +0000258 PRINTF("Cmd 0x%02x Track %ld, Head %ld, Sector %ld, Drive %d (blnr %ld)\n",
259 pCMD->cmd[0],track,head,sect,pCMD->drive,pCMD->blnr);
wdenk1272e232002-11-10 22:06:23 +0000260
wdenk38635852002-08-27 05:55:31 +0000261 if(head|=0) { /* max heads = 2 */
262 pCMD->cmd[DRIVE]=pCMD->drive | 0x04; /* head 1 */
263 pCMD->cmd[HEAD]=(unsigned char) head; /* head register */
264 }
265 else {
266 pCMD->cmd[DRIVE]=pCMD->drive; /* head 0 */
267 pCMD->cmd[HEAD]=(unsigned char) head; /* head register */
268 }
269 pCMD->cmd[TRACK]=(unsigned char) track; /* track */
270 switch (pCMD->cmd[COMMAND]) {
271 case FDC_CMD_READ:
272 pCMD->cmd[SECTOR]=(unsigned char) sect; /* sector */
273 pCMD->cmd[SECTOR_SIZE]=pFG->sect_code; /* sector size code */
274 pCMD->cmd[LAST_TRACK]=pFG->sect; /* End of track */
275 pCMD->cmd[GAP]=pFG->gap; /* gap */
276 pCMD->cmd[DTL]=0xFF; /* DTL */
277 pCMD->cmdlen=FDC_CMD_READ_LEN;
278 pCMD->cmd[COMMAND]|=(FDC_MFM_MODE<<6); /* set MFM bit */
279 pCMD->cmd[COMMAND]|=(FDC_SKIP_MODE<<5); /* set Skip bit */
280 pCMD->resultlen=0; /* result only after execution */
281 break;
282 case FDC_CMD_SEEK:
283 pCMD->cmdlen=FDC_CMD_SEEK_LEN;
284 pCMD->resultlen=0; /* no result */
285 break;
286 case FDC_CMD_CONFIGURE:
287 pCMD->cmd[CONFIG0]=0;
288 pCMD->cmd[CONFIG1]=FDC_CONFIGURE; /* FIFO Threshold, Poll, Enable FIFO */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200289 pCMD->cmd[CONFIG2]=FDC_PRE_TRK; /* Precompensation Track */
wdenk38635852002-08-27 05:55:31 +0000290 pCMD->cmdlen=FDC_CMD_CONFIGURE_LEN;
291 pCMD->resultlen=0; /* no result */
292 break;
293 case FDC_CMD_SPECIFY:
294 pCMD->cmd[SPEC_HUTSRT]=pFG->spec1;
295 pCMD->cmd[SPEC_HLT]=(pFG->hlt)<<1; /* head load time */
296 if(pCMD->dma==0)
297 pCMD->cmd[SPEC_HLT]|=0x1; /* no dma */
298 pCMD->cmdlen=FDC_CMD_SPECIFY_LEN;
299 pCMD->resultlen=0; /* no result */
300 break;
301 case FDC_CMD_DUMP_REG:
302 pCMD->cmdlen=FDC_CMD_DUMP_REG_LEN;
303 pCMD->resultlen=10; /* 10 byte result */
304 break;
305 case FDC_CMD_READ_ID:
306 pCMD->cmd[COMMAND]|=(FDC_MFM_MODE<<6); /* set MFM bit */
307 pCMD->cmdlen=FDC_CMD_READ_ID_LEN;
308 pCMD->resultlen=7; /* 7 byte result */
309 break;
310 case FDC_CMD_RECALIBRATE:
311 pCMD->cmd[DRIVE]&=0x03; /* don't set the head bit */
312 pCMD->cmdlen=FDC_CMD_RECALIBRATE_LEN;
313 pCMD->resultlen=0; /* no result */
314 break;
315 break;
316 case FDC_CMD_SENSE_INT:
317 pCMD->cmdlen=FDC_CMD_SENSE_INT_LEN;
318 pCMD->resultlen=2;
319 break;
320 }
321 for(i=0;i<pCMD->cmdlen;i++) {
322 /* PRINTF("write cmd%d = 0x%02X\n",i,pCMD->cmd[i]); */
York Sun4a598092013-04-01 11:29:11 -0700323 if (write_fdc_byte(pCMD->cmd[i]) == false) {
wdenk38635852002-08-27 05:55:31 +0000324 PRINTF("Error: timeout while issue cmd%d\n",i);
York Sun4a598092013-04-01 11:29:11 -0700325 return false;
wdenk38635852002-08-27 05:55:31 +0000326 }
327 }
328 timeout=FDC_TIME_OUT;
329 for(i=0;i<pCMD->resultlen;i++) {
330 while((read_fdc_reg(FDC_MSR)&0xC0)!=0xC0) {
331 timeout--;
332 if(timeout==0) {
333 PRINTF(" timeout while reading result%d MSR=0x%02X\n",i,read_fdc_reg(FDC_MSR));
York Sun4a598092013-04-01 11:29:11 -0700334 return false;
wdenk38635852002-08-27 05:55:31 +0000335 }
336 }
337 pCMD->result[i]=(unsigned char)read_fdc_byte();
338 }
York Sun4a598092013-04-01 11:29:11 -0700339 return true;
wdenk38635852002-08-27 05:55:31 +0000340}
341
342/* selects the drive assigned in the cmd structur and
343 switches on the Motor */
344void select_fdc_drive(FDC_COMMAND_STRUCT *pCMD)
345{
346 unsigned char val;
347
348 val=(1<<(4+pCMD->drive))|pCMD->drive|0xC; /* set reset, dma gate and motor bits */
349 if((read_fdc_reg(FDC_DOR)&val)!=val) {
350 write_fdc_reg(FDC_DOR,val);
351 for(val=0;val<255;val++)
352 udelay(500); /* wait some time to start motor */
353 }
354}
355
356/* switches off the Motor of the specified drive */
357void stop_fdc_drive(FDC_COMMAND_STRUCT *pCMD)
358{
359 unsigned char val;
360
361 val=(1<<(4+pCMD->drive))|pCMD->drive; /* sets motor bits */
362 write_fdc_reg(FDC_DOR,(read_fdc_reg(FDC_DOR)&~val));
363}
364
365/* issues a recalibrate command, waits for interrupt and
366 * issues a sense_interrupt */
367int fdc_recalibrate(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
368{
369 pCMD->cmd[COMMAND]=FDC_CMD_RECALIBRATE;
York Sun4a598092013-04-01 11:29:11 -0700370 if (fdc_issue_cmd(pCMD, pFG) == false)
371 return false;
372 while (wait_for_fdc_int() != true);
373
wdenk38635852002-08-27 05:55:31 +0000374 pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
375 return(fdc_issue_cmd(pCMD,pFG));
376}
377
378/* issues a recalibrate command, waits for interrupt and
379 * issues a sense_interrupt */
380int fdc_seek(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
381{
382 pCMD->cmd[COMMAND]=FDC_CMD_SEEK;
York Sun4a598092013-04-01 11:29:11 -0700383 if (fdc_issue_cmd(pCMD, pFG) == false)
384 return false;
385 while (wait_for_fdc_int() != true);
386
wdenk38635852002-08-27 05:55:31 +0000387 pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
388 return(fdc_issue_cmd(pCMD,pFG));
389}
390
wdenk38635852002-08-27 05:55:31 +0000391/* terminates current command, by not servicing the FIFO
392 * waits for interrupt and fills in the result bytes */
393int fdc_terminate(FDC_COMMAND_STRUCT *pCMD)
394{
395 int i;
396 for(i=0;i<100;i++)
397 udelay(500); /* wait 500usec for fifo overrun */
398 while((read_fdc_reg(FDC_SRA)&0x80)==0x00); /* wait as long as no int has occured */
399 for(i=0;i<7;i++) {
400 pCMD->result[i]=(unsigned char)read_fdc_byte();
401 }
York Sun4a598092013-04-01 11:29:11 -0700402 return true;
wdenk38635852002-08-27 05:55:31 +0000403}
404
405/* reads data from FDC, seek commands are issued automatic */
406int fdc_read_data(unsigned char *buffer, unsigned long blocks,FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
407{
408 /* first seek to start address */
Wolfgang Denkd8c56492011-11-09 09:29:02 +0000409 unsigned long len,readblk,i,timeout,ii,offset;
410 unsigned char c,retriesrw,retriescal;
wdenk38635852002-08-27 05:55:31 +0000411 unsigned char *bufferw; /* working buffer */
412 int sect_size;
413 int flags;
414
415 flags=disable_interrupts(); /* switch off all Interrupts */
416 select_fdc_drive(pCMD); /* switch on drive */
417 sect_size=0x080<<pFG->sect_code;
418 retriesrw=0;
419 retriescal=0;
420 offset=0;
York Sun4a598092013-04-01 11:29:11 -0700421 if (fdc_seek(pCMD, pFG) == false) {
wdenk38635852002-08-27 05:55:31 +0000422 stop_fdc_drive(pCMD);
Wolfgang Denkd8c56492011-11-09 09:29:02 +0000423 if (flags)
424 enable_interrupts();
York Sun4a598092013-04-01 11:29:11 -0700425 return false;
wdenk38635852002-08-27 05:55:31 +0000426 }
427 if((pCMD->result[STATUS_0]&0x20)!=0x20) {
428 printf("Seek error Status: %02X\n",pCMD->result[STATUS_0]);
429 stop_fdc_drive(pCMD);
Wolfgang Denkd8c56492011-11-09 09:29:02 +0000430 if (flags)
431 enable_interrupts();
York Sun4a598092013-04-01 11:29:11 -0700432 return false;
wdenk38635852002-08-27 05:55:31 +0000433 }
wdenk38635852002-08-27 05:55:31 +0000434 /* now determine the next seek point */
Wolfgang Denkd8c56492011-11-09 09:29:02 +0000435 /* lastblk=pCMD->blnr + blocks; */
wdenk38635852002-08-27 05:55:31 +0000436 /* readblk=(pFG->head*pFG->sect)-(pCMD->blnr%(pFG->head*pFG->sect)); */
437 readblk=pFG->sect-(pCMD->blnr%pFG->sect);
438 PRINTF("1st nr of block possible read %ld start %ld\n",readblk,pCMD->blnr);
439 if(readblk>blocks) /* is end within 1st track */
440 readblk=blocks; /* yes, correct it */
441 PRINTF("we read %ld blocks start %ld\n",readblk,pCMD->blnr);
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200442 bufferw = &buffer[0]; /* setup working buffer */
wdenk38635852002-08-27 05:55:31 +0000443 do {
444retryrw:
445 len=sect_size * readblk;
446 pCMD->cmd[COMMAND]=FDC_CMD_READ;
York Sun4a598092013-04-01 11:29:11 -0700447 if (fdc_issue_cmd(pCMD, pFG) == false) {
wdenk38635852002-08-27 05:55:31 +0000448 stop_fdc_drive(pCMD);
Wolfgang Denkd8c56492011-11-09 09:29:02 +0000449 if (flags)
450 enable_interrupts();
York Sun4a598092013-04-01 11:29:11 -0700451 return false;
wdenk38635852002-08-27 05:55:31 +0000452 }
453 for (i=0;i<len;i++) {
454 timeout=FDC_TIME_OUT;
455 do {
456 c=read_fdc_reg(FDC_MSR);
457 if((c&0xC0)==0xC0) {
458 bufferw[i]=read_fdc_reg(FDC_FIFO);
459 break;
460 }
461 if((c&0xC0)==0x80) { /* output */
462 PRINTF("Transfer error transfered: at %ld, MSR=%02X\n",i,c);
463 if(i>6) {
464 for(ii=0;ii<7;ii++) {
465 pCMD->result[ii]=bufferw[(i-7+ii)];
466 } /* for */
467 }
468 if(retriesrw++>FDC_RW_RETRIES) {
469 if (retriescal++>FDC_CAL_RETRIES) {
470 stop_fdc_drive(pCMD);
Wolfgang Denkd8c56492011-11-09 09:29:02 +0000471 if (flags)
472 enable_interrupts();
York Sun4a598092013-04-01 11:29:11 -0700473 return false;
wdenk38635852002-08-27 05:55:31 +0000474 }
475 else {
476 PRINTF(" trying to recalibrate Try %d\n",retriescal);
York Sun4a598092013-04-01 11:29:11 -0700477 if (fdc_recalibrate(pCMD, pFG) == false) {
wdenk38635852002-08-27 05:55:31 +0000478 stop_fdc_drive(pCMD);
Wolfgang Denkd8c56492011-11-09 09:29:02 +0000479 if (flags)
480 enable_interrupts();
York Sun4a598092013-04-01 11:29:11 -0700481 return false;
wdenk38635852002-08-27 05:55:31 +0000482 }
483 retriesrw=0;
484 goto retrycal;
485 } /* else >FDC_CAL_RETRIES */
486 }
487 else {
488 PRINTF("Read retry %d\n",retriesrw);
489 goto retryrw;
490 } /* else >FDC_RW_RETRIES */
491 }/* if output */
492 timeout--;
York Sun4a598092013-04-01 11:29:11 -0700493 } while (true);
wdenk38635852002-08-27 05:55:31 +0000494 } /* for len */
495 /* the last sector of a track or all data has been read,
496 * we need to get the results */
497 fdc_terminate(pCMD);
498 offset+=(sect_size*readblk); /* set up buffer pointer */
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200499 bufferw = &buffer[offset];
wdenk38635852002-08-27 05:55:31 +0000500 pCMD->blnr+=readblk; /* update current block nr */
501 blocks-=readblk; /* update blocks */
502 if(blocks==0)
503 break; /* we are finish */
504 /* setup new read blocks */
505 /* readblk=pFG->head*pFG->sect; */
506 readblk=pFG->sect;
507 if(readblk>blocks)
508 readblk=blocks;
509retrycal:
510 /* a seek is necessary */
York Sun4a598092013-04-01 11:29:11 -0700511 if (fdc_seek(pCMD, pFG) == false) {
wdenk38635852002-08-27 05:55:31 +0000512 stop_fdc_drive(pCMD);
Wolfgang Denkd8c56492011-11-09 09:29:02 +0000513 if (flags)
514 enable_interrupts();
York Sun4a598092013-04-01 11:29:11 -0700515 return false;
wdenk38635852002-08-27 05:55:31 +0000516 }
517 if((pCMD->result[STATUS_0]&0x20)!=0x20) {
518 PRINTF("Seek error Status: %02X\n",pCMD->result[STATUS_0]);
519 stop_fdc_drive(pCMD);
York Sun4a598092013-04-01 11:29:11 -0700520 return false;
wdenk38635852002-08-27 05:55:31 +0000521 }
York Sun4a598092013-04-01 11:29:11 -0700522 } while (true); /* start over */
wdenk38635852002-08-27 05:55:31 +0000523 stop_fdc_drive(pCMD); /* switch off drive */
Wolfgang Denkd8c56492011-11-09 09:29:02 +0000524 if (flags)
525 enable_interrupts();
York Sun4a598092013-04-01 11:29:11 -0700526 return true;
wdenk38635852002-08-27 05:55:31 +0000527}
528
529/* Scan all drives and check if drive is present and disk is inserted */
530int fdc_check_drive(FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
531{
532 int i,drives,state;
533 /* OK procedure of data book is satisfied.
534 * trying to get some information over the drives */
535 state=0; /* no drives, no disks */
536 for(drives=0;drives<4;drives++) {
537 pCMD->drive=drives;
538 select_fdc_drive(pCMD);
539 pCMD->blnr=0; /* set to the 1st block */
York Sun4a598092013-04-01 11:29:11 -0700540 if (fdc_recalibrate(pCMD, pFG) == false)
wdenk1272e232002-11-10 22:06:23 +0000541 continue;
wdenk38635852002-08-27 05:55:31 +0000542 if((pCMD->result[STATUS_0]&0x10)==0x10)
wdenk1272e232002-11-10 22:06:23 +0000543 continue;
wdenk38635852002-08-27 05:55:31 +0000544 /* ok drive connected check for disk */
545 state|=(1<<drives);
546 pCMD->blnr=pFG->size; /* set to the last block */
York Sun4a598092013-04-01 11:29:11 -0700547 if (fdc_seek(pCMD, pFG) == false)
wdenk1272e232002-11-10 22:06:23 +0000548 continue;
wdenk38635852002-08-27 05:55:31 +0000549 pCMD->blnr=0; /* set to the 1st block */
York Sun4a598092013-04-01 11:29:11 -0700550 if (fdc_recalibrate(pCMD, pFG) == false)
wdenk1272e232002-11-10 22:06:23 +0000551 continue;
wdenk38635852002-08-27 05:55:31 +0000552 pCMD->cmd[COMMAND]=FDC_CMD_READ_ID;
York Sun4a598092013-04-01 11:29:11 -0700553 if (fdc_issue_cmd(pCMD, pFG) == false)
wdenk1272e232002-11-10 22:06:23 +0000554 continue;
wdenk38635852002-08-27 05:55:31 +0000555 state|=(0x10<<drives);
556 }
557 stop_fdc_drive(pCMD);
558 for(i=0;i<4;i++) {
559 PRINTF("Floppy Drive %d %sconnected %sDisk inserted %s\n",i,
560 ((state&(1<<i))==(1<<i)) ? "":"not ",
561 ((state&(0x10<<i))==(0x10<<i)) ? "":"no ",
562 ((state&(0x10<<i))==(0x10<<i)) ? pFG->name : "");
563 }
564 pCMD->flags=state;
York Sun4a598092013-04-01 11:29:11 -0700565 return true;
wdenk38635852002-08-27 05:55:31 +0000566}
567
568
569/**************************************************************************
570* int fdc_setup
571* setup the fdc according the datasheet
572* assuming in PS2 Mode
573*/
wdenk591dda52002-11-18 00:14:45 +0000574int fdc_setup(int drive, FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
wdenk38635852002-08-27 05:55:31 +0000575{
wdenk38635852002-08-27 05:55:31 +0000576 int i;
wdenk1272e232002-11-10 22:06:23 +0000577
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200578#ifdef CONFIG_SYS_FDC_HW_INIT
wdenk57b2d802003-06-27 21:31:46 +0000579 fdc_hw_init ();
wdenk1272e232002-11-10 22:06:23 +0000580#endif
wdenk38635852002-08-27 05:55:31 +0000581 /* first, we reset the FDC via the DOR */
582 write_fdc_reg(FDC_DOR,0x00);
583 for(i=0; i<255; i++) /* then we wait some time */
584 udelay(500);
585 /* then, we clear the reset in the DOR */
wdenk591dda52002-11-18 00:14:45 +0000586 pCMD->drive=drive;
wdenk38635852002-08-27 05:55:31 +0000587 select_fdc_drive(pCMD);
588 /* initialize the CCR */
589 write_fdc_reg(FDC_CCR,pFG->rate);
590 /* then initialize the DSR */
591 write_fdc_reg(FDC_DSR,pFG->rate);
York Sun4a598092013-04-01 11:29:11 -0700592 if (wait_for_fdc_int() == false) {
wdenk38635852002-08-27 05:55:31 +0000593 PRINTF("Time Out after writing CCR\n");
York Sun4a598092013-04-01 11:29:11 -0700594 return false;
wdenk38635852002-08-27 05:55:31 +0000595 }
596 /* now issue sense Interrupt and status command
597 * assuming only one drive present (drive 0) */
598 pCMD->dma=0; /* we don't use any dma at all */
599 for(i=0;i<4;i++) {
600 /* issue sense interrupt for all 4 possible drives */
601 pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
York Sun4a598092013-04-01 11:29:11 -0700602 if (fdc_issue_cmd(pCMD, pFG) == false) {
wdenk38635852002-08-27 05:55:31 +0000603 PRINTF("Sense Interrupt for drive %d failed\n",i);
604 }
605 }
wdenk591dda52002-11-18 00:14:45 +0000606 /* issue the configure command */
607 pCMD->drive=drive;
wdenk38635852002-08-27 05:55:31 +0000608 select_fdc_drive(pCMD);
609 pCMD->cmd[COMMAND]=FDC_CMD_CONFIGURE;
York Sun4a598092013-04-01 11:29:11 -0700610 if (fdc_issue_cmd(pCMD, pFG) == false) {
wdenk38635852002-08-27 05:55:31 +0000611 PRINTF(" configure timeout\n");
612 stop_fdc_drive(pCMD);
York Sun4a598092013-04-01 11:29:11 -0700613 return false;
wdenk38635852002-08-27 05:55:31 +0000614 }
615 /* issue specify command */
616 pCMD->cmd[COMMAND]=FDC_CMD_SPECIFY;
York Sun4a598092013-04-01 11:29:11 -0700617 if (fdc_issue_cmd(pCMD, pFG) == false) {
wdenk38635852002-08-27 05:55:31 +0000618 PRINTF(" specify timeout\n");
619 stop_fdc_drive(pCMD);
York Sun4a598092013-04-01 11:29:11 -0700620 return false;
wdenk38635852002-08-27 05:55:31 +0000621
622 }
623 /* then, we clear the reset in the DOR */
624 /* fdc_check_drive(pCMD,pFG); */
625 /* write_fdc_reg(FDC_DOR,0x04); */
wdenk452cfd62002-11-19 11:04:11 +0000626
York Sun4a598092013-04-01 11:29:11 -0700627 return true;
wdenk38635852002-08-27 05:55:31 +0000628}
wdenk591dda52002-11-18 00:14:45 +0000629
wdenk38635852002-08-27 05:55:31 +0000630/****************************************************************************
631 * main routine do_fdcboot
632 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200633int do_fdcboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000634{
635 FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
636 FDC_COMMAND_STRUCT *pCMD = &cmd;
wdenk57b2d802003-06-27 21:31:46 +0000637 unsigned long addr,imsize;
wdenk38635852002-08-27 05:55:31 +0000638 image_header_t *hdr; /* used for fdc boot */
639 unsigned char boot_drive;
640 int i,nrofblk;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100641#if defined(CONFIG_FIT)
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200642 const void *fit_hdr = NULL;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100643#endif
wdenk38635852002-08-27 05:55:31 +0000644
645 switch (argc) {
646 case 1:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200647 addr = CONFIG_SYS_LOAD_ADDR;
648 boot_drive=CONFIG_SYS_FDC_DRIVE_NUMBER;
wdenk38635852002-08-27 05:55:31 +0000649 break;
650 case 2:
651 addr = simple_strtoul(argv[1], NULL, 16);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200652 boot_drive=CONFIG_SYS_FDC_DRIVE_NUMBER;
wdenk38635852002-08-27 05:55:31 +0000653 break;
654 case 3:
655 addr = simple_strtoul(argv[1], NULL, 16);
656 boot_drive=simple_strtoul(argv[2], NULL, 10);
657 break;
658 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000659 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000660 }
661 /* setup FDC and scan for drives */
York Sun4a598092013-04-01 11:29:11 -0700662 if (fdc_setup(boot_drive, pCMD, pFG) == false) {
wdenk38635852002-08-27 05:55:31 +0000663 printf("\n** Error in setup FDC **\n");
664 return 1;
665 }
York Sun4a598092013-04-01 11:29:11 -0700666 if (fdc_check_drive(pCMD, pFG) == false) {
wdenk38635852002-08-27 05:55:31 +0000667 printf("\n** Error in check_drives **\n");
668 return 1;
669 }
670 if((pCMD->flags&(1<<boot_drive))==0) {
671 /* drive not available */
672 printf("\n** Drive %d not availabe **\n",boot_drive);
673 return 1;
674 }
675 if((pCMD->flags&(0x10<<boot_drive))==0) {
676 /* no disk inserted */
677 printf("\n** No disk inserted in drive %d **\n",boot_drive);
678 return 1;
679 }
680 /* ok, we have a valid source */
681 pCMD->drive=boot_drive;
682 /* read first block */
683 pCMD->blnr=0;
York Sun4a598092013-04-01 11:29:11 -0700684 if (fdc_read_data((unsigned char *)addr, 1, pCMD, pFG) == false) {
wdenk38635852002-08-27 05:55:31 +0000685 printf("\nRead error:");
686 for(i=0;i<7;i++)
687 printf("result%d: 0x%02X\n",i,pCMD->result[i]);
688 return 1;
689 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100690
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100691 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100692 case IMAGE_FORMAT_LEGACY:
693 hdr = (image_header_t *)addr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100694 image_print_contents (hdr);
695
696 imsize = image_get_image_size (hdr);
697 break;
698#if defined(CONFIG_FIT)
699 case IMAGE_FORMAT_FIT:
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100700 fit_hdr = (const void *)addr;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100701 puts ("Fit image detected...\n");
702
703 imsize = fit_get_size (fit_hdr);
704 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100705#endif
706 default:
707 puts ("** Unknown image type\n");
wdenk38635852002-08-27 05:55:31 +0000708 return 1;
709 }
wdenk38635852002-08-27 05:55:31 +0000710
wdenk38635852002-08-27 05:55:31 +0000711 nrofblk=imsize/512;
712 if((imsize%512)>0)
713 nrofblk++;
714 printf("Loading %ld Bytes (%d blocks) at 0x%08lx..\n",imsize,nrofblk,addr);
715 pCMD->blnr=0;
York Sun4a598092013-04-01 11:29:11 -0700716 if (fdc_read_data((unsigned char *)addr, nrofblk, pCMD, pFG) == false) {
wdenk38635852002-08-27 05:55:31 +0000717 /* read image block */
718 printf("\nRead error:");
719 for(i=0;i<7;i++)
720 printf("result%d: 0x%02X\n",i,pCMD->result[i]);
721 return 1;
722 }
723 printf("OK %ld Bytes loaded.\n",imsize);
724
725 flush_cache (addr, imsize);
wdenk38635852002-08-27 05:55:31 +0000726
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100727#if defined(CONFIG_FIT)
728 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200729 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
730 if (!fit_check_format (fit_hdr)) {
731 puts ("** Bad FIT image format\n");
732 return 1;
733 }
734 fit_print_contents (fit_hdr);
735 }
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100736#endif
737
738 /* Loading ok, update default load address */
wdenk38635852002-08-27 05:55:31 +0000739 load_addr = addr;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100740
Mike Frysinger194c2e82011-06-05 13:43:02 +0000741 return bootm_maybe_autostart(cmdtp, argv[0]);
wdenk38635852002-08-27 05:55:31 +0000742}
743
wdenkf287a242003-07-01 21:06:45 +0000744U_BOOT_CMD(
745 fdcboot, 3, 1, do_fdcboot,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600746 "boot from floppy device",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200747 "loadAddr drive"
wdenk57b2d802003-06-27 21:31:46 +0000748);