cmd_fdc.c 23 KB

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