cmd_fdc.c 24 KB

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