cmd_fdc.c 23 KB

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