cmd_fdc.c 24 KB

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