disk.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. /* Mask IRQ 6 before reset so the completion interrupt doesn't
  246. * fire before we're ready (IRQ 6 may already be unmasked from
  247. * a previous init). Then clear the flag and release the reset. */
  248. outb(PIC1_DATA, inb(PIC1_DATA) | 0x40);
  249. outb(FDC_DOR, 0x00);
  250. motor_on = 0;
  251. {
  252. unsigned int t0 = get_ticks_ms();
  253. while ((get_ticks_ms() - t0) < 10)
  254. ;
  255. }
  256. fdc_irq_fired = 0;
  257. outb(FDC_DOR, (unsigned char)(0x0C | current_drive));
  258. /* Unmask IRQ 6 — the latched reset-completion interrupt fires now. */
  259. outb(PIC1_DATA, inb(PIC1_DATA) & ~0x40);
  260. if (fdc_wait_irq() != 0)
  261. return -1;
  262. {
  263. unsigned char st0, cyl;
  264. int i;
  265. for (i = 0; i < 4; i++)
  266. fdc_sense_interrupt(&st0, &cyl);
  267. outb(FDC_CCR, 0x00);
  268. if (fdc_send(CMD_SPECIFY) != 0)
  269. return -1;
  270. if (fdc_send(0xDF) != 0)
  271. return -1;
  272. if (fdc_send(0x02) != 0)
  273. return -1;
  274. }
  275. fdc_motor_on();
  276. if (fdc_recalibrate() != 0)
  277. return -1;
  278. fdc_initialized = 1;
  279. return 0;
  280. }
  281. void disk_set_drive(unsigned int drive)
  282. {
  283. if (drive > 1)
  284. return;
  285. if (drive == current_drive && fdc_initialized)
  286. return;
  287. current_drive = drive;
  288. motor_on = 0;
  289. fdc_initialized = 0;
  290. }
  291. unsigned int disk_get_drive(void)
  292. {
  293. return current_drive;
  294. }
  295. int disk_read_sectors(unsigned int lba, void *buf, unsigned int count)
  296. {
  297. unsigned int i;
  298. if (!fdc_initialized) {
  299. if (fdc_init_once() != 0)
  300. return -1;
  301. }
  302. for (i = 0; i < count; i++) {
  303. int attempt, ok = 0;
  304. for (attempt = 0; attempt < MAX_RETRIES; attempt++) {
  305. if (fdc_read_one(lba + i) == 0) {
  306. ok = 1;
  307. break;
  308. }
  309. fdc_reset();
  310. fdc_motor_on();
  311. fdc_recalibrate();
  312. }
  313. if (!ok)
  314. return -1;
  315. {
  316. unsigned int j;
  317. unsigned char *dst = (unsigned char *)buf + (i * SECTOR_SIZE);
  318. const unsigned char *src = (const unsigned char *)DMA_BUF_ADDR;
  319. for (j = 0; j < SECTOR_SIZE; j++)
  320. dst[j] = src[j];
  321. }
  322. }
  323. return 0;
  324. }