ata.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* ATA PIO driver: IDENTIFY + LBA28/LBA48 read/write, polled (no IRQ).
  2. * The ATA IRQs (14/15) stay masked at boot, so we poll BSY/DRQ/ERR. */
  3. #include "ata.h"
  4. #include "pci.h"
  5. #include "timer.h"
  6. static const unsigned short ata_base[2] = { 0x1F0, 0x170 };
  7. static const unsigned short ata_ctrl[2] = { 0x3F6, 0x376 };
  8. /* Register offsets from the I/O base. */
  9. #define REG_DATA 0
  10. #define REG_ERROR 1
  11. #define REG_COUNT 2
  12. #define REG_LBA_LO 3
  13. #define REG_LBA_MID 4
  14. #define REG_LBA_HI 5
  15. #define REG_DRIVE 6
  16. #define REG_CMD 7
  17. /* Status bits (status = base+7, alt-status = ctrl+0). */
  18. #define SR_BSY 0x80
  19. #define SR_DRDY 0x40
  20. #define SR_DRQ 0x08
  21. #define SR_ERR 0x01
  22. /* Commands. */
  23. #define CMD_IDENTIFY 0xEC
  24. #define CMD_READ 0x20 /* LBA28 */
  25. #define CMD_WRITE 0x30 /* LBA28 */
  26. #define CMD_READ_EXT 0x24 /* LBA48 */
  27. #define CMD_WRITE_EXT 0x34 /* LBA48 */
  28. /* Delay 400 ns by reading the alt-status register a few times. */
  29. static void ata_400ns(unsigned short ctrl)
  30. {
  31. (void)port_inb(ctrl);
  32. (void)port_inb(ctrl);
  33. (void)port_inb(ctrl);
  34. (void)port_inb(ctrl);
  35. }
  36. static int ata_wait_not_busy(unsigned short ctrl, unsigned int timeout_ms)
  37. {
  38. unsigned int t0 = get_ticks_ms();
  39. while (get_ticks_ms() - t0 < timeout_ms) {
  40. if (!(port_inb(ctrl) & SR_BSY))
  41. return 0;
  42. }
  43. return -1;
  44. }
  45. static int ata_wait_drq(unsigned short base, unsigned int timeout_ms)
  46. {
  47. unsigned int t0 = get_ticks_ms();
  48. while (get_ticks_ms() - t0 < timeout_ms) {
  49. unsigned char st = port_inb(base + REG_CMD);
  50. if (st & SR_ERR)
  51. return -1;
  52. if (st & SR_DRQ)
  53. return 0;
  54. }
  55. return -1;
  56. }
  57. static int ata_identify(int bus, int unit, unsigned short *id)
  58. {
  59. unsigned short base = ata_base[bus];
  60. unsigned short ctrl = ata_ctrl[bus];
  61. unsigned char st;
  62. unsigned int t0;
  63. unsigned int i;
  64. port_outb(base + REG_DRIVE, (unsigned char)(0xA0 | (unit << 4)));
  65. port_outb(base + REG_COUNT, 0);
  66. port_outb(base + REG_LBA_LO, 0);
  67. port_outb(base + REG_LBA_MID, 0);
  68. port_outb(base + REG_LBA_HI, 0);
  69. port_outb(base + REG_CMD, CMD_IDENTIFY);
  70. ata_400ns(ctrl);
  71. t0 = get_ticks_ms();
  72. for (;;) {
  73. st = port_inb(base + REG_CMD);
  74. if (st == 0 || st == 0xFF) /* floating bus: no device */
  75. return -1;
  76. if (!(st & SR_BSY))
  77. break;
  78. if (get_ticks_ms() - t0 > 4000)
  79. return -1;
  80. }
  81. if (!(st & SR_DRQ))
  82. return -1;
  83. for (i = 0; i < 256; i++)
  84. id[i] = port_inw(base + REG_DATA);
  85. return 0;
  86. }
  87. int ata_probe(int bus, int unit, unsigned int *total_sectors, char model[41])
  88. {
  89. static unsigned short id[256];
  90. unsigned int sectors, lo, hi, i;
  91. if (ata_identify(bus, unit, id) != 0)
  92. return -1;
  93. if (id[0] & 0x8000) /* ATAPI (cdrom/zip): not a data disk */
  94. return -1;
  95. /* Model string: words 27..46, byte-swapped. */
  96. for (i = 0; i < 20; i++) {
  97. unsigned short w = id[27 + i];
  98. model[i * 2] = (char)(w >> 8);
  99. model[i * 2 + 1] = (char)(w & 0xFF);
  100. }
  101. model[40] = '\0';
  102. for (i = 40; i > 0; i--) {
  103. if (model[i - 1] == ' ')
  104. model[i - 1] = '\0';
  105. else
  106. break;
  107. }
  108. sectors = (unsigned int)id[60] | ((unsigned int)id[61] << 16);
  109. if (id[83] & 0x0400) { /* LBA48 supported */
  110. lo = (unsigned int)id[100] | ((unsigned int)id[101] << 16);
  111. hi = (unsigned int)id[102] | ((unsigned int)id[103] << 16);
  112. if (lo || hi)
  113. sectors = lo;
  114. }
  115. *total_sectors = sectors;
  116. return 0;
  117. }
  118. static inline void ata_insw(unsigned short port, void *dst, unsigned int count)
  119. {
  120. __asm__ volatile ("rep insw"
  121. : "+D"(dst), "+c"(count)
  122. : "d"(port)
  123. : "memory");
  124. }
  125. static inline void ata_outsw(unsigned short port, const void *src, unsigned int count)
  126. {
  127. __asm__ volatile ("rep outsw"
  128. : "+S"(src), "+c"(count)
  129. : "d"(port)
  130. : "memory");
  131. }
  132. static int ata_xfer(int bus, int unit, unsigned int lba, void *buf,
  133. unsigned int count, int write)
  134. {
  135. unsigned short base = ata_base[bus];
  136. unsigned short ctrl = ata_ctrl[bus];
  137. unsigned char *p = buf;
  138. unsigned int done = 0;
  139. while (done < count) {
  140. unsigned int chunk = count - done;
  141. int lba48;
  142. unsigned int cur = lba + done;
  143. if (chunk > 255)
  144. chunk = 255;
  145. lba48 = ((cur + chunk - 1) >= 0x10000000u);
  146. if (lba48) {
  147. port_outb(base + REG_DRIVE, (unsigned char)(0x40 | (unit << 4)));
  148. port_outb(base + REG_COUNT, (unsigned char)(chunk >> 8));
  149. port_outb(base + REG_LBA_LO, (unsigned char)(cur >> 24));
  150. port_outb(base + REG_LBA_MID, 0);
  151. port_outb(base + REG_LBA_HI, 0);
  152. port_outb(base + REG_COUNT, (unsigned char)(chunk & 0xFF));
  153. port_outb(base + REG_LBA_LO, (unsigned char)(cur & 0xFF));
  154. port_outb(base + REG_LBA_MID, (unsigned char)((cur >> 8) & 0xFF));
  155. port_outb(base + REG_LBA_HI, (unsigned char)((cur >> 16) & 0xFF));
  156. port_outb(base + REG_CMD, (unsigned char)(write ? CMD_WRITE_EXT : CMD_READ_EXT));
  157. } else {
  158. port_outb(base + REG_DRIVE,
  159. (unsigned char)(0xE0 | (unit << 4) | ((cur >> 24) & 0x0F)));
  160. port_outb(base + REG_COUNT, (unsigned char)(chunk & 0xFF));
  161. port_outb(base + REG_LBA_LO, (unsigned char)(cur & 0xFF));
  162. port_outb(base + REG_LBA_MID, (unsigned char)((cur >> 8) & 0xFF));
  163. port_outb(base + REG_LBA_HI, (unsigned char)((cur >> 16) & 0xFF));
  164. port_outb(base + REG_CMD, (unsigned char)(write ? CMD_WRITE : CMD_READ));
  165. }
  166. ata_400ns(ctrl);
  167. if (ata_wait_not_busy(ctrl, 4000) != 0)
  168. return -1;
  169. if (ata_wait_drq(base, 4000) != 0)
  170. return -1;
  171. if (write) {
  172. ata_outsw(base + REG_DATA, p, chunk * 256);
  173. /* Wait for the drive to finish writing to media (BSY clears
  174. * after the data transfer) before issuing the next command. */
  175. if (ata_wait_not_busy(ctrl, 4000) != 0)
  176. return -1;
  177. if (port_inb(base + REG_CMD) & SR_ERR)
  178. return -1;
  179. } else {
  180. ata_insw(base + REG_DATA, p, chunk * 256);
  181. }
  182. p += chunk * 512;
  183. done += chunk;
  184. }
  185. return 0;
  186. }
  187. int ata_read_sectors(int bus, int unit, unsigned int lba, void *buf, unsigned int count)
  188. {
  189. return ata_xfer(bus, unit, lba, buf, count, 0);
  190. }
  191. int ata_write_sectors(int bus, int unit, unsigned int lba, const void *buf, unsigned int count)
  192. {
  193. return ata_xfer(bus, unit, lba, (void *)buf, count, 1);
  194. }