cmd_fdc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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,lastblk,readblk,i,timeout,ii,offset;
  391. unsigned char pcn,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. enable_interrupts();
  404. return FALSE;
  405. }
  406. if((pCMD->result[STATUS_0]&0x20)!=0x20) {
  407. printf("Seek error Status: %02X\n",pCMD->result[STATUS_0]);
  408. stop_fdc_drive(pCMD);
  409. enable_interrupts();
  410. return FALSE;
  411. }
  412. pcn=pCMD->result[STATUS_PCN]; /* current track */
  413. /* now determine the next seek point */
  414. lastblk=pCMD->blnr + blocks;
  415. /* readblk=(pFG->head*pFG->sect)-(pCMD->blnr%(pFG->head*pFG->sect)); */
  416. readblk=pFG->sect-(pCMD->blnr%pFG->sect);
  417. PRINTF("1st nr of block possible read %ld start %ld\n",readblk,pCMD->blnr);
  418. if(readblk>blocks) /* is end within 1st track */
  419. readblk=blocks; /* yes, correct it */
  420. PRINTF("we read %ld blocks start %ld\n",readblk,pCMD->blnr);
  421. bufferw = &buffer[0]; /* setup working buffer */
  422. do {
  423. retryrw:
  424. len=sect_size * readblk;
  425. pCMD->cmd[COMMAND]=FDC_CMD_READ;
  426. if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
  427. stop_fdc_drive(pCMD);
  428. enable_interrupts();
  429. return FALSE;
  430. }
  431. for (i=0;i<len;i++) {
  432. timeout=FDC_TIME_OUT;
  433. do {
  434. c=read_fdc_reg(FDC_MSR);
  435. if((c&0xC0)==0xC0) {
  436. bufferw[i]=read_fdc_reg(FDC_FIFO);
  437. break;
  438. }
  439. if((c&0xC0)==0x80) { /* output */
  440. PRINTF("Transfer error transfered: at %ld, MSR=%02X\n",i,c);
  441. if(i>6) {
  442. for(ii=0;ii<7;ii++) {
  443. pCMD->result[ii]=bufferw[(i-7+ii)];
  444. } /* for */
  445. }
  446. if(retriesrw++>FDC_RW_RETRIES) {
  447. if (retriescal++>FDC_CAL_RETRIES) {
  448. stop_fdc_drive(pCMD);
  449. enable_interrupts();
  450. return FALSE;
  451. }
  452. else {
  453. PRINTF(" trying to recalibrate Try %d\n",retriescal);
  454. if(fdc_recalibrate(pCMD,pFG)==FALSE) {
  455. stop_fdc_drive(pCMD);
  456. enable_interrupts();
  457. return FALSE;
  458. }
  459. retriesrw=0;
  460. goto retrycal;
  461. } /* else >FDC_CAL_RETRIES */
  462. }
  463. else {
  464. PRINTF("Read retry %d\n",retriesrw);
  465. goto retryrw;
  466. } /* else >FDC_RW_RETRIES */
  467. }/* if output */
  468. timeout--;
  469. }while(TRUE);
  470. } /* for len */
  471. /* the last sector of a track or all data has been read,
  472. * we need to get the results */
  473. fdc_terminate(pCMD);
  474. offset+=(sect_size*readblk); /* set up buffer pointer */
  475. bufferw = &buffer[offset];
  476. pCMD->blnr+=readblk; /* update current block nr */
  477. blocks-=readblk; /* update blocks */
  478. if(blocks==0)
  479. break; /* we are finish */
  480. /* setup new read blocks */
  481. /* readblk=pFG->head*pFG->sect; */
  482. readblk=pFG->sect;
  483. if(readblk>blocks)
  484. readblk=blocks;
  485. retrycal:
  486. /* a seek is necessary */
  487. if(fdc_seek(pCMD,pFG)==FALSE) {
  488. stop_fdc_drive(pCMD);
  489. enable_interrupts();
  490. return FALSE;
  491. }
  492. if((pCMD->result[STATUS_0]&0x20)!=0x20) {
  493. PRINTF("Seek error Status: %02X\n",pCMD->result[STATUS_0]);
  494. stop_fdc_drive(pCMD);
  495. return FALSE;
  496. }
  497. pcn=pCMD->result[STATUS_PCN]; /* current track */
  498. }while(TRUE); /* start over */
  499. stop_fdc_drive(pCMD); /* switch off drive */
  500. enable_interrupts();
  501. return TRUE;
  502. }
  503. /* Scan all drives and check if drive is present and disk is inserted */
  504. int fdc_check_drive(FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
  505. {
  506. int i,drives,state;
  507. /* OK procedure of data book is satisfied.
  508. * trying to get some information over the drives */
  509. state=0; /* no drives, no disks */
  510. for(drives=0;drives<4;drives++) {
  511. pCMD->drive=drives;
  512. select_fdc_drive(pCMD);
  513. pCMD->blnr=0; /* set to the 1st block */
  514. if(fdc_recalibrate(pCMD,pFG)==FALSE)
  515. continue;
  516. if((pCMD->result[STATUS_0]&0x10)==0x10)
  517. continue;
  518. /* ok drive connected check for disk */
  519. state|=(1<<drives);
  520. pCMD->blnr=pFG->size; /* set to the last block */
  521. if(fdc_seek(pCMD,pFG)==FALSE)
  522. continue;
  523. pCMD->blnr=0; /* set to the 1st block */
  524. if(fdc_recalibrate(pCMD,pFG)==FALSE)
  525. continue;
  526. pCMD->cmd[COMMAND]=FDC_CMD_READ_ID;
  527. if(fdc_issue_cmd(pCMD,pFG)==FALSE)
  528. continue;
  529. state|=(0x10<<drives);
  530. }
  531. stop_fdc_drive(pCMD);
  532. for(i=0;i<4;i++) {
  533. PRINTF("Floppy Drive %d %sconnected %sDisk inserted %s\n",i,
  534. ((state&(1<<i))==(1<<i)) ? "":"not ",
  535. ((state&(0x10<<i))==(0x10<<i)) ? "":"no ",
  536. ((state&(0x10<<i))==(0x10<<i)) ? pFG->name : "");
  537. }
  538. pCMD->flags=state;
  539. return TRUE;
  540. }
  541. /**************************************************************************
  542. * int fdc_setup
  543. * setup the fdc according the datasheet
  544. * assuming in PS2 Mode
  545. */
  546. int fdc_setup(int drive, FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
  547. {
  548. int i;
  549. #ifdef CONFIG_SYS_FDC_HW_INIT
  550. fdc_hw_init ();
  551. #endif
  552. /* first, we reset the FDC via the DOR */
  553. write_fdc_reg(FDC_DOR,0x00);
  554. for(i=0; i<255; i++) /* then we wait some time */
  555. udelay(500);
  556. /* then, we clear the reset in the DOR */
  557. pCMD->drive=drive;
  558. select_fdc_drive(pCMD);
  559. /* initialize the CCR */
  560. write_fdc_reg(FDC_CCR,pFG->rate);
  561. /* then initialize the DSR */
  562. write_fdc_reg(FDC_DSR,pFG->rate);
  563. if(wait_for_fdc_int()==FALSE) {
  564. PRINTF("Time Out after writing CCR\n");
  565. return FALSE;
  566. }
  567. /* now issue sense Interrupt and status command
  568. * assuming only one drive present (drive 0) */
  569. pCMD->dma=0; /* we don't use any dma at all */
  570. for(i=0;i<4;i++) {
  571. /* issue sense interrupt for all 4 possible drives */
  572. pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
  573. if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
  574. PRINTF("Sense Interrupt for drive %d failed\n",i);
  575. }
  576. }
  577. /* issue the configure command */
  578. pCMD->drive=drive;
  579. select_fdc_drive(pCMD);
  580. pCMD->cmd[COMMAND]=FDC_CMD_CONFIGURE;
  581. if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
  582. PRINTF(" configure timeout\n");
  583. stop_fdc_drive(pCMD);
  584. return FALSE;
  585. }
  586. /* issue specify command */
  587. pCMD->cmd[COMMAND]=FDC_CMD_SPECIFY;
  588. if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
  589. PRINTF(" specify timeout\n");
  590. stop_fdc_drive(pCMD);
  591. return FALSE;
  592. }
  593. /* then, we clear the reset in the DOR */
  594. /* fdc_check_drive(pCMD,pFG); */
  595. /* write_fdc_reg(FDC_DOR,0x04); */
  596. return TRUE;
  597. }
  598. #if defined(CONFIG_CMD_FDOS)
  599. /* Low level functions for the Floppy-DOS layer */
  600. /**************************************************************************
  601. * int fdc_fdos_init
  602. * initialize the FDC layer
  603. *
  604. */
  605. int fdc_fdos_init (int drive)
  606. {
  607. FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
  608. FDC_COMMAND_STRUCT *pCMD = &cmd;
  609. /* setup FDC and scan for drives */
  610. if(fdc_setup(drive,pCMD,pFG)==FALSE) {
  611. printf("\n** Error in setup FDC **\n");
  612. return FALSE;
  613. }
  614. if(fdc_check_drive(pCMD,pFG)==FALSE) {
  615. printf("\n** Error in check_drives **\n");
  616. return FALSE;
  617. }
  618. if((pCMD->flags&(1<<drive))==0) {
  619. /* drive not available */
  620. printf("\n** Drive %d not available **\n",drive);
  621. return FALSE;
  622. }
  623. if((pCMD->flags&(0x10<<drive))==0) {
  624. /* no disk inserted */
  625. printf("\n** No disk inserted in drive %d **\n",drive);
  626. return FALSE;
  627. }
  628. /* ok, we have a valid source */
  629. pCMD->drive=drive;
  630. /* read first block */
  631. pCMD->blnr=0;
  632. return TRUE;
  633. }
  634. /**************************************************************************
  635. * int fdc_fdos_seek
  636. * parameter is a block number
  637. */
  638. int fdc_fdos_seek (int where)
  639. {
  640. FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
  641. FDC_COMMAND_STRUCT *pCMD = &cmd;
  642. pCMD -> blnr = where ;
  643. return (fdc_seek (pCMD, pFG));
  644. }
  645. /**************************************************************************
  646. * int fdc_fdos_read
  647. * the length is in block number
  648. */
  649. int fdc_fdos_read (void *buffer, int len)
  650. {
  651. FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
  652. FDC_COMMAND_STRUCT *pCMD = &cmd;
  653. return (fdc_read_data (buffer, len, pCMD, pFG));
  654. }
  655. #endif
  656. #if defined(CONFIG_CMD_FDC)
  657. /****************************************************************************
  658. * main routine do_fdcboot
  659. */
  660. int do_fdcboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  661. {
  662. FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
  663. FDC_COMMAND_STRUCT *pCMD = &cmd;
  664. unsigned long addr,imsize;
  665. image_header_t *hdr; /* used for fdc boot */
  666. unsigned char boot_drive;
  667. int i,nrofblk;
  668. char *ep;
  669. int rcode = 0;
  670. #if defined(CONFIG_FIT)
  671. const void *fit_hdr = NULL;
  672. #endif
  673. switch (argc) {
  674. case 1:
  675. addr = CONFIG_SYS_LOAD_ADDR;
  676. boot_drive=CONFIG_SYS_FDC_DRIVE_NUMBER;
  677. break;
  678. case 2:
  679. addr = simple_strtoul(argv[1], NULL, 16);
  680. boot_drive=CONFIG_SYS_FDC_DRIVE_NUMBER;
  681. break;
  682. case 3:
  683. addr = simple_strtoul(argv[1], NULL, 16);
  684. boot_drive=simple_strtoul(argv[2], NULL, 10);
  685. break;
  686. default:
  687. return cmd_usage(cmdtp);
  688. }
  689. /* setup FDC and scan for drives */
  690. if(fdc_setup(boot_drive,pCMD,pFG)==FALSE) {
  691. printf("\n** Error in setup FDC **\n");
  692. return 1;
  693. }
  694. if(fdc_check_drive(pCMD,pFG)==FALSE) {
  695. printf("\n** Error in check_drives **\n");
  696. return 1;
  697. }
  698. if((pCMD->flags&(1<<boot_drive))==0) {
  699. /* drive not available */
  700. printf("\n** Drive %d not availabe **\n",boot_drive);
  701. return 1;
  702. }
  703. if((pCMD->flags&(0x10<<boot_drive))==0) {
  704. /* no disk inserted */
  705. printf("\n** No disk inserted in drive %d **\n",boot_drive);
  706. return 1;
  707. }
  708. /* ok, we have a valid source */
  709. pCMD->drive=boot_drive;
  710. /* read first block */
  711. pCMD->blnr=0;
  712. if(fdc_read_data((unsigned char *)addr,1,pCMD,pFG)==FALSE) {
  713. printf("\nRead error:");
  714. for(i=0;i<7;i++)
  715. printf("result%d: 0x%02X\n",i,pCMD->result[i]);
  716. return 1;
  717. }
  718. switch (genimg_get_format ((void *)addr)) {
  719. case IMAGE_FORMAT_LEGACY:
  720. hdr = (image_header_t *)addr;
  721. image_print_contents (hdr);
  722. imsize = image_get_image_size (hdr);
  723. break;
  724. #if defined(CONFIG_FIT)
  725. case IMAGE_FORMAT_FIT:
  726. fit_hdr = (const void *)addr;
  727. puts ("Fit image detected...\n");
  728. imsize = fit_get_size (fit_hdr);
  729. break;
  730. #endif
  731. default:
  732. puts ("** Unknown image type\n");
  733. return 1;
  734. }
  735. nrofblk=imsize/512;
  736. if((imsize%512)>0)
  737. nrofblk++;
  738. printf("Loading %ld Bytes (%d blocks) at 0x%08lx..\n",imsize,nrofblk,addr);
  739. pCMD->blnr=0;
  740. if(fdc_read_data((unsigned char *)addr,nrofblk,pCMD,pFG)==FALSE) {
  741. /* read image block */
  742. printf("\nRead error:");
  743. for(i=0;i<7;i++)
  744. printf("result%d: 0x%02X\n",i,pCMD->result[i]);
  745. return 1;
  746. }
  747. printf("OK %ld Bytes loaded.\n",imsize);
  748. flush_cache (addr, imsize);
  749. #if defined(CONFIG_FIT)
  750. /* This cannot be done earlier, we need complete FIT image in RAM first */
  751. if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
  752. if (!fit_check_format (fit_hdr)) {
  753. puts ("** Bad FIT image format\n");
  754. return 1;
  755. }
  756. fit_print_contents (fit_hdr);
  757. }
  758. #endif
  759. /* Loading ok, update default load address */
  760. load_addr = addr;
  761. /* Check if we should attempt an auto-start */
  762. if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
  763. char *local_args[2];
  764. extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
  765. local_args[0] = argv[0];
  766. local_args[1] = NULL;
  767. printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
  768. do_bootm (cmdtp, 0, 1, local_args);
  769. rcode ++;
  770. }
  771. return rcode;
  772. }
  773. U_BOOT_CMD(
  774. fdcboot, 3, 1, do_fdcboot,
  775. "boot from floppy device",
  776. "loadAddr drive"
  777. );
  778. #endif