fdc.c 20 KB

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