cmd_fdc.c 24 KB

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