disk.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* Disk layer: read sectors via direct FDC + ISA DMA in protected mode. */
  2. #include "disk.h"
  3. #include "timer.h"
  4. #define SECTOR_SIZE 512
  5. #define SECTORS_PER_TRACK 18
  6. #define HEADS 2
  7. #define DMA_BUF_ADDR 0x10000
  8. /* FDC I/O ports */
  9. #define FDC_DOR 0x3F2
  10. #define FDC_MSR 0x3F4
  11. #define FDC_FIFO 0x3F5
  12. #define FDC_CCR 0x3F7
  13. /* ISA DMA channel 2 ports */
  14. #define DMA_CH2_ADDR 0x04
  15. #define DMA_CH2_COUNT 0x05
  16. #define DMA_CH2_PAGE 0x81
  17. #define DMA_SINGLEMASK 0x0A
  18. #define DMA_MODE 0x0B
  19. #define DMA_FLIPFLOP 0x0C
  20. /* MSR bits */
  21. #define MSR_RQM 0x80
  22. #define MSR_DIO 0x40
  23. #define MSR_CB 0x10
  24. /* FDC commands */
  25. #define CMD_SPECIFY 0x03
  26. #define CMD_RECALIBRATE 0x07
  27. #define CMD_SENSE_INT 0x08
  28. #define CMD_READ_DATA 0xE6 /* READ | MFM | SK */
  29. #define CMD_SEEK 0x0F
  30. #define MOTOR_DELAY_MS 300
  31. #define IRQ_TIMEOUT_MS 2000
  32. #define MAX_RETRIES 3
  33. /* PIC master mask port */
  34. #define PIC1_DATA 0x21
  35. static inline unsigned char inb(unsigned short port)
  36. {
  37. unsigned char v;
  38. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  39. return v;
  40. }
  41. static inline void outb(unsigned short port, unsigned char v)
  42. {
  43. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  44. }
  45. volatile int fdc_irq_fired;
  46. void fdc_irq(void)
  47. {
  48. fdc_irq_fired = 1;
  49. outb(0x20, 0x20);
  50. }
  51. static int fdc_initialized;
  52. static int motor_on;
  53. static unsigned int cur_cylinder;
  54. static unsigned int current_drive;
  55. /* ---- low-level FDC helpers ---- */
  56. static int fdc_wait_rqm(void)
  57. {
  58. unsigned int t0 = get_ticks_ms();
  59. while ((get_ticks_ms() - t0) < 500) {
  60. if (inb(FDC_MSR) & MSR_RQM)
  61. return 0;
  62. }
  63. return -1;
  64. }
  65. static int fdc_send(unsigned char b)
  66. {
  67. if (fdc_wait_rqm() != 0)
  68. return -1;
  69. if (inb(FDC_MSR) & MSR_DIO)
  70. return -1;
  71. outb(FDC_FIFO, b);
  72. return 0;
  73. }
  74. static int fdc_recv(unsigned char *b)
  75. {
  76. if (fdc_wait_rqm() != 0)
  77. return -1;
  78. if (!(inb(FDC_MSR) & MSR_DIO))
  79. return -1;
  80. *b = inb(FDC_FIFO);
  81. return 0;
  82. }
  83. static int fdc_wait_irq(void)
  84. {
  85. unsigned int t0 = get_ticks_ms();
  86. while (!fdc_irq_fired) {
  87. if ((get_ticks_ms() - t0) >= IRQ_TIMEOUT_MS)
  88. return -1;
  89. __asm__ volatile ("hlt");
  90. }
  91. return 0;
  92. }
  93. static int fdc_sense_interrupt(unsigned char *st0, unsigned char *cyl)
  94. {
  95. if (fdc_send(CMD_SENSE_INT) != 0)
  96. return -1;
  97. if (fdc_recv(st0) != 0)
  98. return -1;
  99. if (fdc_recv(cyl) != 0)
  100. return -1;
  101. return 0;
  102. }
  103. /* ---- motor control ---- */
  104. static void fdc_motor_on(void)
  105. {
  106. if (motor_on)
  107. return;
  108. outb(FDC_DOR, (unsigned char)((1 << (4 + current_drive)) | 0x0C | current_drive));
  109. {
  110. unsigned int t0 = get_ticks_ms();
  111. while ((get_ticks_ms() - t0) < MOTOR_DELAY_MS)
  112. __asm__ volatile ("hlt");
  113. }
  114. motor_on = 1;
  115. }
  116. /* ---- reset ---- */
  117. static int fdc_reset(void)
  118. {
  119. unsigned char st0, cyl;
  120. int i;
  121. /* Pull reset line: DOR = 0 turns off controller + motor */
  122. outb(FDC_DOR, 0x00);
  123. motor_on = 0;
  124. {
  125. unsigned int t0 = get_ticks_ms();
  126. while ((get_ticks_ms() - t0) < 10)
  127. ;
  128. }
  129. /* Release reset: controller on, DMA/IRQ enabled, no motor.
  130. * The FDC fires IRQ 6 once the reset completes. */
  131. fdc_irq_fired = 0;
  132. outb(FDC_DOR, (unsigned char)(0x0C | current_drive));
  133. if (fdc_wait_irq() != 0)
  134. return -1;
  135. for (i = 0; i < 4; i++)
  136. fdc_sense_interrupt(&st0, &cyl);
  137. outb(FDC_CCR, 0x00); /* 500 kbps for 1.44 MB */
  138. /* SPECIFY: SRT=3ms, HUT=240ms, HLT=16ms, DMA mode */
  139. if (fdc_send(CMD_SPECIFY) != 0)
  140. return -1;
  141. if (fdc_send(0xDF) != 0)
  142. return -1;
  143. if (fdc_send(0x02) != 0)
  144. return -1;
  145. return 0;
  146. }
  147. static int fdc_recalibrate(void)
  148. {
  149. unsigned char st0, cyl;
  150. fdc_irq_fired = 0;
  151. if (fdc_send(CMD_RECALIBRATE) != 0)
  152. return -1;
  153. if (fdc_send((unsigned char)current_drive) != 0)
  154. return -1;
  155. if (fdc_wait_irq() != 0)
  156. return -1;
  157. if (fdc_sense_interrupt(&st0, &cyl) != 0)
  158. return -1;
  159. if (st0 & 0xC0)
  160. return -1;
  161. cur_cylinder = 0;
  162. return 0;
  163. }
  164. static int fdc_seek(unsigned int cylinder, unsigned int head)
  165. {
  166. unsigned char st0, cyl;
  167. fdc_irq_fired = 0;
  168. if (fdc_send(CMD_SEEK) != 0)
  169. return -1;
  170. if (fdc_send((unsigned char)((head << 2) | current_drive)) != 0)
  171. return -1;
  172. if (fdc_send((unsigned char)cylinder) != 0)
  173. return -1;
  174. if (fdc_wait_irq() != 0)
  175. return -1;
  176. if (fdc_sense_interrupt(&st0, &cyl) != 0)
  177. return -1;
  178. if ((st0 & 0xC0) || cyl != (unsigned char)cylinder)
  179. return -1;
  180. cur_cylinder = cylinder;
  181. return 0;
  182. }
  183. /* ---- DMA channel 2 setup ---- */
  184. static void dma_setup_read(unsigned int phys, unsigned int len)
  185. {
  186. unsigned int count = len - 1;
  187. outb(DMA_SINGLEMASK, 0x06); /* mask channel 2 */
  188. outb(DMA_FLIPFLOP, 0xFF); /* reset flip-flop */
  189. outb(DMA_CH2_ADDR, (unsigned char)(phys & 0xFF));
  190. outb(DMA_CH2_ADDR, (unsigned char)((phys >> 8) & 0xFF));
  191. outb(DMA_CH2_PAGE, (unsigned char)((phys >> 16) & 0xFF));
  192. outb(DMA_FLIPFLOP, 0xFF); /* reset flip-flop */
  193. outb(DMA_CH2_COUNT, (unsigned char)(count & 0xFF));
  194. outb(DMA_CH2_COUNT, (unsigned char)((count >> 8) & 0xFF));
  195. outb(DMA_MODE, 0x46); /* single, increment, write, ch2 */
  196. outb(DMA_SINGLEMASK, 0x02); /* unmask channel 2 */
  197. }
  198. /* ---- read one sector ---- */
  199. static void lba_to_chs(unsigned int lba,
  200. unsigned int *cyl, unsigned int *head, unsigned int *sec)
  201. {
  202. unsigned int tmp = lba / SECTORS_PER_TRACK;
  203. *sec = (lba % SECTORS_PER_TRACK) + 1;
  204. *head = tmp % HEADS;
  205. *cyl = tmp / HEADS;
  206. }
  207. static int fdc_do_read(unsigned int cyl, unsigned int head, unsigned int sec)
  208. {
  209. unsigned char results[7];
  210. int i;
  211. dma_setup_read(DMA_BUF_ADDR, SECTOR_SIZE);
  212. fdc_irq_fired = 0;
  213. if (fdc_send(CMD_READ_DATA) != 0) return -1;
  214. if (fdc_send((unsigned char)((head << 2) | current_drive)) != 0) return -1;
  215. if (fdc_send((unsigned char)cyl) != 0) return -1;
  216. if (fdc_send((unsigned char)head) != 0) return -1;
  217. if (fdc_send((unsigned char)sec) != 0) return -1;
  218. if (fdc_send(0x02) != 0) return -1; /* 512 bytes/sector */
  219. if (fdc_send((unsigned char)SECTORS_PER_TRACK) != 0) return -1;
  220. if (fdc_send(0x1B) != 0) return -1; /* gap3 */
  221. if (fdc_send(0xFF) != 0) return -1; /* data length */
  222. if (fdc_wait_irq() != 0)
  223. return -1;
  224. for (i = 0; i < 7; i++) {
  225. if (fdc_recv(&results[i]) != 0)
  226. return -1;
  227. }
  228. if (results[0] & 0xC0)
  229. return -1;
  230. return 0;
  231. }
  232. static int fdc_read_one(unsigned int lba)
  233. {
  234. unsigned int cyl, head, sec;
  235. lba_to_chs(lba, &cyl, &head, &sec);
  236. if (cyl != cur_cylinder) {
  237. if (fdc_seek(cyl, head) != 0)
  238. return -1;
  239. }
  240. return fdc_do_read(cyl, head, sec);
  241. }
  242. /* ---- public API ---- */
  243. static int fdc_init_once(void)
  244. {
  245. /* Reset the FDC first (clears any pending interrupt state left by BIOS).
  246. * IRQ 6 is still masked at this point so no spurious interrupts. */
  247. outb(FDC_DOR, 0x00);
  248. motor_on = 0;
  249. {
  250. unsigned int t0 = get_ticks_ms();
  251. while ((get_ticks_ms() - t0) < 10)
  252. ;
  253. }
  254. outb(FDC_DOR, (unsigned char)(0x0C | current_drive));
  255. /* Now that the FDC is in a clean state, unmask IRQ 6 at the PIC.
  256. * The reset completion interrupt is latched and fires immediately. */
  257. fdc_irq_fired = 0;
  258. outb(PIC1_DATA, inb(PIC1_DATA) & ~0x40);
  259. if (fdc_wait_irq() != 0)
  260. return -1;
  261. {
  262. unsigned char st0, cyl;
  263. int i;
  264. for (i = 0; i < 4; i++)
  265. fdc_sense_interrupt(&st0, &cyl);
  266. outb(FDC_CCR, 0x00);
  267. if (fdc_send(CMD_SPECIFY) != 0)
  268. return -1;
  269. if (fdc_send(0xDF) != 0)
  270. return -1;
  271. if (fdc_send(0x02) != 0)
  272. return -1;
  273. }
  274. fdc_motor_on();
  275. if (fdc_recalibrate() != 0)
  276. return -1;
  277. fdc_initialized = 1;
  278. return 0;
  279. }
  280. void disk_set_drive(unsigned int drive)
  281. {
  282. if (drive > 1)
  283. return;
  284. if (drive == current_drive && fdc_initialized)
  285. return;
  286. current_drive = drive;
  287. motor_on = 0;
  288. fdc_initialized = 0;
  289. }
  290. unsigned int disk_get_drive(void)
  291. {
  292. return current_drive;
  293. }
  294. int disk_read_sectors(unsigned int lba, void *buf, unsigned int count)
  295. {
  296. unsigned int i;
  297. if (!fdc_initialized) {
  298. if (fdc_init_once() != 0)
  299. return -1;
  300. }
  301. for (i = 0; i < count; i++) {
  302. int attempt, ok = 0;
  303. for (attempt = 0; attempt < MAX_RETRIES; attempt++) {
  304. if (fdc_read_one(lba + i) == 0) {
  305. ok = 1;
  306. break;
  307. }
  308. fdc_reset();
  309. fdc_motor_on();
  310. fdc_recalibrate();
  311. }
  312. if (!ok)
  313. return -1;
  314. {
  315. unsigned int j;
  316. unsigned char *dst = (unsigned char *)buf + (i * SECTOR_SIZE);
  317. const unsigned char *src = (const unsigned char *)DMA_BUF_ADDR;
  318. for (j = 0; j < SECTOR_SIZE; j++)
  319. dst[j] = src[j];
  320. }
  321. }
  322. return 0;
  323. }