cmd_fdc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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)
  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. /* Supporting Functions */
  154. /* reads a Register of the FDC */
  155. unsigned char read_fdc_reg(unsigned int addr)
  156. {
  157. volatile unsigned char *val = (volatile unsigned char *)(CFG_ISA_IO_BASE_ADDRESS | addr);
  158. return val[0];
  159. }
  160. /* writes a Register of the FDC */
  161. void write_fdc_reg(unsigned int addr, unsigned char val)
  162. {
  163. volatile unsigned char *tmp = (volatile unsigned char *)(CFG_ISA_IO_BASE_ADDRESS | addr);
  164. tmp[0]=val;
  165. }
  166. /* waits for an interrupt (polling) */
  167. int wait_for_fdc_int(void)
  168. {
  169. unsigned long timeout;
  170. timeout = FDC_TIME_OUT;
  171. while((read_fdc_reg(FDC_SRA)&0x80)==0) {
  172. timeout--;
  173. udelay(10);
  174. if(timeout==0) /* timeout occured */
  175. return FALSE;
  176. }
  177. return TRUE;
  178. }
  179. /* reads a byte from the FIFO of the FDC and checks direction and RQM bit
  180. of the MSR. returns -1 if timeout, or byte if ok */
  181. int read_fdc_byte(void)
  182. {
  183. unsigned long timeout;
  184. timeout = FDC_TIME_OUT;
  185. while((read_fdc_reg(FDC_MSR)&0xC0)!=0xC0) {
  186. /* direction out and ready */
  187. udelay(10);
  188. timeout--;
  189. if(timeout==0) /* timeout occured */
  190. return -1;
  191. }
  192. return read_fdc_reg(FDC_FIFO);
  193. }
  194. /* if the direction of the FIFO is wrong, this routine is used to
  195. empty the FIFO. Should _not_ be used */
  196. int fdc_need_more_output(void)
  197. {
  198. unsigned char c;
  199. while((read_fdc_reg(FDC_MSR)&0xC0)==0xC0) {
  200. c=(unsigned char)read_fdc_byte();
  201. printf("Error: more output: %x\n",c);
  202. }
  203. return TRUE;
  204. }
  205. /* writes a byte to the FIFO of the FDC and checks direction and RQM bit
  206. of the MSR */
  207. int write_fdc_byte(unsigned char val)
  208. {
  209. unsigned long timeout;
  210. timeout = FDC_TIME_OUT;
  211. while((read_fdc_reg(FDC_MSR)&0xC0)!=0x80) {
  212. /* direction in and ready for byte */
  213. timeout--;
  214. udelay(10);
  215. fdc_need_more_output();
  216. if(timeout==0) /* timeout occured */
  217. return FALSE;
  218. }
  219. write_fdc_reg(FDC_FIFO,val);
  220. return TRUE;
  221. }
  222. /* sets up all FDC commands and issues it to the FDC. If
  223. the command causes direct results (no Execution Phase)
  224. the result is be read as well. */
  225. int fdc_issue_cmd(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
  226. {
  227. int i;
  228. unsigned long head,track,sect,timeout;
  229. track = pCMD->blnr / (pFG->sect * pFG->head); /* track nr */
  230. sect = pCMD->blnr % (pFG->sect * pFG->head); /* remaining blocks */
  231. head = sect / pFG->sect; /* head nr */
  232. sect = sect % pFG->sect; /* remaining blocks */
  233. sect++; /* sectors are 1 based */
  234. PRINTF("Track %ld, Head %ld, Sector %ld, Drive %d (blnr %ld)\n",track,head,sect,pCMD->drive,pCMD->blnr);
  235. if(head|=0) { /* max heads = 2 */
  236. pCMD->cmd[DRIVE]=pCMD->drive | 0x04; /* head 1 */
  237. pCMD->cmd[HEAD]=(unsigned char) head; /* head register */
  238. }
  239. else {
  240. pCMD->cmd[DRIVE]=pCMD->drive; /* head 0 */
  241. pCMD->cmd[HEAD]=(unsigned char) head; /* head register */
  242. }
  243. pCMD->cmd[TRACK]=(unsigned char) track; /* track */
  244. switch (pCMD->cmd[COMMAND]) {
  245. case FDC_CMD_READ:
  246. pCMD->cmd[SECTOR]=(unsigned char) sect; /* sector */
  247. pCMD->cmd[SECTOR_SIZE]=pFG->sect_code; /* sector size code */
  248. pCMD->cmd[LAST_TRACK]=pFG->sect; /* End of track */
  249. pCMD->cmd[GAP]=pFG->gap; /* gap */
  250. pCMD->cmd[DTL]=0xFF; /* DTL */
  251. pCMD->cmdlen=FDC_CMD_READ_LEN;
  252. pCMD->cmd[COMMAND]|=(FDC_MFM_MODE<<6); /* set MFM bit */
  253. pCMD->cmd[COMMAND]|=(FDC_SKIP_MODE<<5); /* set Skip bit */
  254. pCMD->resultlen=0; /* result only after execution */
  255. break;
  256. case FDC_CMD_SEEK:
  257. pCMD->cmdlen=FDC_CMD_SEEK_LEN;
  258. pCMD->resultlen=0; /* no result */
  259. break;
  260. case FDC_CMD_CONFIGURE:
  261. pCMD->cmd[CONFIG0]=0;
  262. pCMD->cmd[CONFIG1]=FDC_CONFIGURE; /* FIFO Threshold, Poll, Enable FIFO */
  263. pCMD->cmd[CONFIG2]=FDC_PRE_TRK; /* Precompensation Track */
  264. pCMD->cmdlen=FDC_CMD_CONFIGURE_LEN;
  265. pCMD->resultlen=0; /* no result */
  266. break;
  267. case FDC_CMD_SPECIFY:
  268. pCMD->cmd[SPEC_HUTSRT]=pFG->spec1;
  269. pCMD->cmd[SPEC_HLT]=(pFG->hlt)<<1; /* head load time */
  270. if(pCMD->dma==0)
  271. pCMD->cmd[SPEC_HLT]|=0x1; /* no dma */
  272. pCMD->cmdlen=FDC_CMD_SPECIFY_LEN;
  273. pCMD->resultlen=0; /* no result */
  274. break;
  275. case FDC_CMD_DUMP_REG:
  276. pCMD->cmdlen=FDC_CMD_DUMP_REG_LEN;
  277. pCMD->resultlen=10; /* 10 byte result */
  278. break;
  279. case FDC_CMD_READ_ID:
  280. pCMD->cmd[COMMAND]|=(FDC_MFM_MODE<<6); /* set MFM bit */
  281. pCMD->cmdlen=FDC_CMD_READ_ID_LEN;
  282. pCMD->resultlen=7; /* 7 byte result */
  283. break;
  284. case FDC_CMD_RECALIBRATE:
  285. pCMD->cmd[DRIVE]&=0x03; /* don't set the head bit */
  286. pCMD->cmdlen=FDC_CMD_RECALIBRATE_LEN;
  287. pCMD->resultlen=0; /* no result */
  288. break;
  289. break;
  290. case FDC_CMD_SENSE_INT:
  291. pCMD->cmdlen=FDC_CMD_SENSE_INT_LEN;
  292. pCMD->resultlen=2;
  293. break;
  294. }
  295. for(i=0;i<pCMD->cmdlen;i++) {
  296. /* PRINTF("write cmd%d = 0x%02X\n",i,pCMD->cmd[i]); */
  297. if(write_fdc_byte(pCMD->cmd[i])==FALSE) {
  298. PRINTF("Error: timeout while issue cmd%d\n",i);
  299. return FALSE;
  300. }
  301. }
  302. timeout=FDC_TIME_OUT;
  303. for(i=0;i<pCMD->resultlen;i++) {
  304. while((read_fdc_reg(FDC_MSR)&0xC0)!=0xC0) {
  305. timeout--;
  306. if(timeout==0) {
  307. PRINTF(" timeout while reading result%d MSR=0x%02X\n",i,read_fdc_reg(FDC_MSR));
  308. return FALSE;
  309. }
  310. }
  311. pCMD->result[i]=(unsigned char)read_fdc_byte();
  312. }
  313. return TRUE;
  314. }
  315. /* selects the drive assigned in the cmd structur and
  316. switches on the Motor */
  317. void select_fdc_drive(FDC_COMMAND_STRUCT *pCMD)
  318. {
  319. unsigned char val;
  320. val=(1<<(4+pCMD->drive))|pCMD->drive|0xC; /* set reset, dma gate and motor bits */
  321. if((read_fdc_reg(FDC_DOR)&val)!=val) {
  322. write_fdc_reg(FDC_DOR,val);
  323. for(val=0;val<255;val++)
  324. udelay(500); /* wait some time to start motor */
  325. }
  326. }
  327. /* switches off the Motor of the specified drive */
  328. void stop_fdc_drive(FDC_COMMAND_STRUCT *pCMD)
  329. {
  330. unsigned char val;
  331. val=(1<<(4+pCMD->drive))|pCMD->drive; /* sets motor bits */
  332. write_fdc_reg(FDC_DOR,(read_fdc_reg(FDC_DOR)&~val));
  333. }
  334. /* issues a recalibrate command, waits for interrupt and
  335. * issues a sense_interrupt */
  336. int fdc_recalibrate(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
  337. {
  338. pCMD->cmd[COMMAND]=FDC_CMD_RECALIBRATE;
  339. if(fdc_issue_cmd(pCMD,pFG)==FALSE)
  340. return FALSE;
  341. while(wait_for_fdc_int()!=TRUE);
  342. pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
  343. return(fdc_issue_cmd(pCMD,pFG));
  344. }
  345. /* issues a recalibrate command, waits for interrupt and
  346. * issues a sense_interrupt */
  347. int fdc_seek(FDC_COMMAND_STRUCT *pCMD,FD_GEO_STRUCT *pFG)
  348. {
  349. pCMD->cmd[COMMAND]=FDC_CMD_SEEK;
  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. /* terminates current command, by not servicing the FIFO
  357. * waits for interrupt and fills in the result bytes */
  358. int fdc_terminate(FDC_COMMAND_STRUCT *pCMD)
  359. {
  360. int i;
  361. for(i=0;i<100;i++)
  362. udelay(500); /* wait 500usec for fifo overrun */
  363. while((read_fdc_reg(FDC_SRA)&0x80)==0x00); /* wait as long as no int has occured */
  364. for(i=0;i<7;i++) {
  365. pCMD->result[i]=(unsigned char)read_fdc_byte();
  366. }
  367. return TRUE;
  368. }
  369. /* reads data from FDC, seek commands are issued automatic */
  370. int fdc_read_data(unsigned char *buffer, unsigned long blocks,FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
  371. {
  372. /* first seek to start address */
  373. unsigned long len,lastblk,readblk,i,timeout,ii,offset;
  374. unsigned char pcn,c,retriesrw,retriescal;
  375. unsigned char *bufferw; /* working buffer */
  376. int sect_size;
  377. int flags;
  378. flags=disable_interrupts(); /* switch off all Interrupts */
  379. select_fdc_drive(pCMD); /* switch on drive */
  380. sect_size=0x080<<pFG->sect_code;
  381. retriesrw=0;
  382. retriescal=0;
  383. offset=0;
  384. if(fdc_seek(pCMD,pFG)==FALSE) {
  385. stop_fdc_drive(pCMD);
  386. enable_interrupts();
  387. return FALSE;
  388. }
  389. if((pCMD->result[STATUS_0]&0x20)!=0x20) {
  390. printf("Seek error Status: %02X\n",pCMD->result[STATUS_0]);
  391. stop_fdc_drive(pCMD);
  392. enable_interrupts();
  393. return FALSE;
  394. }
  395. pcn=pCMD->result[STATUS_PCN]; /* current track */
  396. /* now determine the next seek point */
  397. lastblk=pCMD->blnr + blocks;
  398. /* readblk=(pFG->head*pFG->sect)-(pCMD->blnr%(pFG->head*pFG->sect)); */
  399. readblk=pFG->sect-(pCMD->blnr%pFG->sect);
  400. PRINTF("1st nr of block possible read %ld start %ld\n",readblk,pCMD->blnr);
  401. if(readblk>blocks) /* is end within 1st track */
  402. readblk=blocks; /* yes, correct it */
  403. PRINTF("we read %ld blocks start %ld\n",readblk,pCMD->blnr);
  404. bufferw=&buffer[0]; /* setup working buffer */
  405. do {
  406. retryrw:
  407. len=sect_size * readblk;
  408. pCMD->cmd[COMMAND]=FDC_CMD_READ;
  409. if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
  410. stop_fdc_drive(pCMD);
  411. enable_interrupts();
  412. return FALSE;
  413. }
  414. for (i=0;i<len;i++) {
  415. timeout=FDC_TIME_OUT;
  416. do {
  417. c=read_fdc_reg(FDC_MSR);
  418. if((c&0xC0)==0xC0) {
  419. bufferw[i]=read_fdc_reg(FDC_FIFO);
  420. break;
  421. }
  422. if((c&0xC0)==0x80) { /* output */
  423. PRINTF("Transfer error transfered: at %ld, MSR=%02X\n",i,c);
  424. if(i>6) {
  425. for(ii=0;ii<7;ii++) {
  426. pCMD->result[ii]=bufferw[(i-7+ii)];
  427. } /* for */
  428. }
  429. if(retriesrw++>FDC_RW_RETRIES) {
  430. if (retriescal++>FDC_CAL_RETRIES) {
  431. stop_fdc_drive(pCMD);
  432. enable_interrupts();
  433. return FALSE;
  434. }
  435. else {
  436. PRINTF(" trying to recalibrate Try %d\n",retriescal);
  437. if(fdc_recalibrate(pCMD,pFG)==FALSE) {
  438. stop_fdc_drive(pCMD);
  439. enable_interrupts();
  440. return FALSE;
  441. }
  442. retriesrw=0;
  443. goto retrycal;
  444. } /* else >FDC_CAL_RETRIES */
  445. }
  446. else {
  447. PRINTF("Read retry %d\n",retriesrw);
  448. goto retryrw;
  449. } /* else >FDC_RW_RETRIES */
  450. }/* if output */
  451. timeout--;
  452. }while(TRUE);
  453. } /* for len */
  454. /* the last sector of a track or all data has been read,
  455. * we need to get the results */
  456. fdc_terminate(pCMD);
  457. offset+=(sect_size*readblk); /* set up buffer pointer */
  458. bufferw=&buffer[offset];
  459. pCMD->blnr+=readblk; /* update current block nr */
  460. blocks-=readblk; /* update blocks */
  461. if(blocks==0)
  462. break; /* we are finish */
  463. /* setup new read blocks */
  464. /* readblk=pFG->head*pFG->sect; */
  465. readblk=pFG->sect;
  466. if(readblk>blocks)
  467. readblk=blocks;
  468. retrycal:
  469. /* a seek is necessary */
  470. if(fdc_seek(pCMD,pFG)==FALSE) {
  471. stop_fdc_drive(pCMD);
  472. enable_interrupts();
  473. return FALSE;
  474. }
  475. if((pCMD->result[STATUS_0]&0x20)!=0x20) {
  476. PRINTF("Seek error Status: %02X\n",pCMD->result[STATUS_0]);
  477. stop_fdc_drive(pCMD);
  478. return FALSE;
  479. }
  480. pcn=pCMD->result[STATUS_PCN]; /* current track */
  481. }while(TRUE); /* start over */
  482. stop_fdc_drive(pCMD); /* switch off drive */
  483. enable_interrupts();
  484. return TRUE;
  485. }
  486. /* Scan all drives and check if drive is present and disk is inserted */
  487. int fdc_check_drive(FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
  488. {
  489. int i,drives,state;
  490. /* OK procedure of data book is satisfied.
  491. * trying to get some information over the drives */
  492. state=0; /* no drives, no disks */
  493. for(drives=0;drives<4;drives++) {
  494. pCMD->drive=drives;
  495. select_fdc_drive(pCMD);
  496. pCMD->blnr=0; /* set to the 1st block */
  497. if(fdc_recalibrate(pCMD,pFG)==FALSE)
  498. break;
  499. if((pCMD->result[STATUS_0]&0x10)==0x10)
  500. break;
  501. /* ok drive connected check for disk */
  502. state|=(1<<drives);
  503. pCMD->blnr=pFG->size; /* set to the last block */
  504. if(fdc_seek(pCMD,pFG)==FALSE)
  505. break;
  506. pCMD->blnr=0; /* set to the 1st block */
  507. if(fdc_recalibrate(pCMD,pFG)==FALSE)
  508. break;
  509. pCMD->cmd[COMMAND]=FDC_CMD_READ_ID;
  510. if(fdc_issue_cmd(pCMD,pFG)==FALSE)
  511. break;
  512. state|=(0x10<<drives);
  513. }
  514. stop_fdc_drive(pCMD);
  515. for(i=0;i<4;i++) {
  516. PRINTF("Floppy Drive %d %sconnected %sDisk inserted %s\n",i,
  517. ((state&(1<<i))==(1<<i)) ? "":"not ",
  518. ((state&(0x10<<i))==(0x10<<i)) ? "":"no ",
  519. ((state&(0x10<<i))==(0x10<<i)) ? pFG->name : "");
  520. }
  521. pCMD->flags=state;
  522. return TRUE;
  523. }
  524. /**************************************************************************
  525. * int fdc_setup
  526. * setup the fdc according the datasheet
  527. * assuming in PS2 Mode
  528. */
  529. int fdc_setup(FDC_COMMAND_STRUCT *pCMD, FD_GEO_STRUCT *pFG)
  530. {
  531. int i;
  532. /* first, we reset the FDC via the DOR */
  533. write_fdc_reg(FDC_DOR,0x00);
  534. for(i=0; i<255; i++) /* then we wait some time */
  535. udelay(500);
  536. /* then, we clear the reset in the DOR */
  537. pCMD->drive=0;
  538. select_fdc_drive(pCMD);
  539. /* initialize the CCR */
  540. write_fdc_reg(FDC_CCR,pFG->rate);
  541. /* then initialize the DSR */
  542. write_fdc_reg(FDC_DSR,pFG->rate);
  543. if(wait_for_fdc_int()==FALSE) {
  544. PRINTF("Time Out after writing CCR\n");
  545. return FALSE;
  546. }
  547. /* now issue sense Interrupt and status command
  548. * assuming only one drive present (drive 0) */
  549. pCMD->dma=0; /* we don't use any dma at all */
  550. for(i=0;i<4;i++) {
  551. /* issue sense interrupt for all 4 possible drives */
  552. pCMD->cmd[COMMAND]=FDC_CMD_SENSE_INT;
  553. if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
  554. PRINTF("Sense Interrupt for drive %d failed\n",i);
  555. }
  556. }
  557. /* assuming drive 0 for rest of configuration
  558. * issue the configure command */
  559. pCMD->drive=0;
  560. select_fdc_drive(pCMD);
  561. pCMD->cmd[COMMAND]=FDC_CMD_CONFIGURE;
  562. if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
  563. PRINTF(" configure timeout\n");
  564. stop_fdc_drive(pCMD);
  565. return FALSE;
  566. }
  567. /* issue specify command */
  568. pCMD->cmd[COMMAND]=FDC_CMD_SPECIFY;
  569. if(fdc_issue_cmd(pCMD,pFG)==FALSE) {
  570. PRINTF(" specify timeout\n");
  571. stop_fdc_drive(pCMD);
  572. return FALSE;
  573. }
  574. /* then, we clear the reset in the DOR */
  575. /* fdc_check_drive(pCMD,pFG); */
  576. /* write_fdc_reg(FDC_DOR,0x04); */
  577. return TRUE;
  578. }
  579. /****************************************************************************
  580. * main routine do_fdcboot
  581. */
  582. int do_fdcboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  583. {
  584. FD_GEO_STRUCT *pFG = (FD_GEO_STRUCT *)floppy_type;
  585. FDC_COMMAND_STRUCT *pCMD = &cmd;
  586. unsigned long addr,imsize;
  587. image_header_t *hdr; /* used for fdc boot */
  588. unsigned char boot_drive;
  589. int i,nrofblk;
  590. char *ep;
  591. int rcode = 0;
  592. switch (argc) {
  593. case 1:
  594. addr = CFG_LOAD_ADDR;
  595. boot_drive=0; /* default boot from drive 0 */
  596. break;
  597. case 2:
  598. addr = simple_strtoul(argv[1], NULL, 16);
  599. boot_drive=0; /* default boot from drive 0 */
  600. break;
  601. case 3:
  602. addr = simple_strtoul(argv[1], NULL, 16);
  603. boot_drive=simple_strtoul(argv[2], NULL, 10);
  604. break;
  605. default:
  606. printf ("Usage:\n%s\n", cmdtp->usage);
  607. return 1;
  608. }
  609. /* setup FDC and scan for drives */
  610. if(fdc_setup(pCMD,pFG)==FALSE) {
  611. printf("\n** Error in setup FDC **\n");
  612. return 1;
  613. }
  614. if(fdc_check_drive(pCMD,pFG)==FALSE) {
  615. printf("\n** Error in check_drives **\n");
  616. return 1;
  617. }
  618. if((pCMD->flags&(1<<boot_drive))==0) {
  619. /* drive not available */
  620. printf("\n** Drive %d not availabe **\n",boot_drive);
  621. return 1;
  622. }
  623. if((pCMD->flags&(0x10<<boot_drive))==0) {
  624. /* no disk inserted */
  625. printf("\n** No disk inserted in drive %d **\n",boot_drive);
  626. return 1;
  627. }
  628. /* ok, we have a valid source */
  629. pCMD->drive=boot_drive;
  630. /* read first block */
  631. pCMD->blnr=0;
  632. if(fdc_read_data((unsigned char *)addr,1,pCMD,pFG)==FALSE) {
  633. printf("\nRead error:");
  634. for(i=0;i<7;i++)
  635. printf("result%d: 0x%02X\n",i,pCMD->result[i]);
  636. return 1;
  637. }
  638. hdr = (image_header_t *)addr;
  639. if (hdr->ih_magic != IH_MAGIC) {
  640. printf ("Bad Magic Number\n");
  641. return 1;
  642. }
  643. print_image_hdr(hdr);
  644. imsize= hdr->ih_size+sizeof(image_header_t);
  645. nrofblk=imsize/512;
  646. if((imsize%512)>0)
  647. nrofblk++;
  648. printf("Loading %ld Bytes (%d blocks) at 0x%08lx..\n",imsize,nrofblk,addr);
  649. pCMD->blnr=0;
  650. if(fdc_read_data((unsigned char *)addr,nrofblk,pCMD,pFG)==FALSE) {
  651. /* read image block */
  652. printf("\nRead error:");
  653. for(i=0;i<7;i++)
  654. printf("result%d: 0x%02X\n",i,pCMD->result[i]);
  655. return 1;
  656. }
  657. printf("OK %ld Bytes loaded.\n",imsize);
  658. flush_cache (addr, imsize);
  659. /* Loading ok, update default load address */
  660. load_addr = addr;
  661. if(hdr->ih_type == IH_TYPE_KERNEL) {
  662. /* Check if we should attempt an auto-start */
  663. if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
  664. char *local_args[2];
  665. extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
  666. local_args[0] = argv[0];
  667. local_args[1] = NULL;
  668. printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
  669. do_bootm (cmdtp, 0, 1, local_args);
  670. rcode ++;
  671. }
  672. }
  673. return rcode;
  674. }
  675. #endif /* CONFIG_COMMANDS & CFG_CMD_FDC */