cmd_fdc.c 23 KB

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