ata.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. *
  4. * Channel port bases default to the legacy primary/secondary ports and are
  5. * replaced by the PCI IDE interface's BARs when it runs in native mode
  6. * (nVidia MCP79 "compatibility mode" and friends). */
  7. #include "ata.h"
  8. #include "pci.h"
  9. #include "serial.h"
  10. #include "timer.h"
  11. static unsigned short ata_base[2] = { 0x1F0, 0x170 };
  12. static unsigned short ata_ctrl[2] = { 0x3F6, 0x376 };
  13. /* Register offsets from the I/O base. */
  14. #define REG_DATA 0
  15. #define REG_ERROR 1
  16. #define REG_COUNT 2
  17. #define REG_LBA_LO 3
  18. #define REG_LBA_MID 4
  19. #define REG_LBA_HI 5
  20. #define REG_DRIVE 6
  21. #define REG_CMD 7
  22. /* Status bits (status = base+7, alt-status = ctrl+0). */
  23. #define SR_BSY 0x80
  24. #define SR_DRDY 0x40
  25. #define SR_DRQ 0x08
  26. #define SR_ERR 0x01
  27. /* Commands. */
  28. #define CMD_IDENTIFY 0xEC
  29. #define CMD_READ 0x20 /* LBA28 */
  30. #define CMD_WRITE 0x30 /* LBA28 */
  31. #define CMD_READ_EXT 0x24 /* LBA48 */
  32. #define CMD_WRITE_EXT 0x34 /* LBA48 */
  33. /* Point the primary/secondary channels at the PCI IDE interface's native
  34. * per-channel I/O BARs when it uses native mode, keeping the legacy ports
  35. * otherwise. Runs once; without a PCI IDE function the defaults stay. */
  36. void ata_init_ports(void)
  37. {
  38. static int inited;
  39. unsigned int bus, dev, func;
  40. if (inited)
  41. return;
  42. inited = 1;
  43. for (bus = 0; bus < 256; bus++) {
  44. for (dev = 0; dev < 32; dev++) {
  45. unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);
  46. unsigned int htype, nfunc;
  47. if (id == 0xFFFFFFFFu || id == 0)
  48. continue;
  49. htype = (pci_cfg_read32(bus, dev, 0, 0x0C) >> 16) & 0xFFu;
  50. nfunc = (htype & 0x80u) ? 8u : 1u;
  51. for (func = 0; func < nfunc; func++) {
  52. unsigned int idf = pci_cfg_read32(bus, dev, func, 0x00);
  53. unsigned int crev, cls, sub, prog;
  54. unsigned int bar0, bar1, bar2, bar3, cmd;
  55. unsigned short b0, c0, b1, c1;
  56. if (idf == 0xFFFFFFFFu || idf == 0)
  57. continue;
  58. crev = pci_cfg_read32(bus, dev, func, 0x08);
  59. cls = (crev >> 24) & 0xFFu;
  60. sub = (crev >> 16) & 0xFFu;
  61. prog = (crev >> 8) & 0xFFu;
  62. if (cls != 0x01 || sub != 0x01)
  63. continue;
  64. cmd = pci_cfg_read32(bus, dev, func, 0x04);
  65. pci_cfg_write32(bus, dev, func, 0x04, cmd | 0x0005u);
  66. bar0 = pci_cfg_read32(bus, dev, func, 0x10);
  67. bar1 = pci_cfg_read32(bus, dev, func, 0x14);
  68. bar2 = pci_cfg_read32(bus, dev, func, 0x18);
  69. bar3 = pci_cfg_read32(bus, dev, func, 0x1C);
  70. /* prog-if bit 0/2: primary/secondary native mode. */
  71. if ((prog & 0x01u) && (bar0 & 0x1u)) {
  72. b0 = (unsigned short)(bar0 & 0xFFFCu);
  73. c0 = (bar1 & 0x1u)
  74. ? (unsigned short)(bar1 & 0xFFFCu)
  75. : (unsigned short)(b0 + 8);
  76. } else {
  77. b0 = 0x1F0; c0 = 0x3F6;
  78. }
  79. if ((prog & 0x04u) && (bar2 & 0x1u)) {
  80. b1 = (unsigned short)(bar2 & 0xFFFCu);
  81. c1 = (bar3 & 0x1u)
  82. ? (unsigned short)(bar3 & 0xFFFCu)
  83. : (unsigned short)(b1 + 8);
  84. } else {
  85. b1 = 0x170; c1 = 0x376;
  86. }
  87. ata_base[0] = b0; ata_ctrl[0] = c0;
  88. ata_base[1] = b1; ata_ctrl[1] = c1;
  89. serial_puts(SERIAL_COM1, "ata: pci ide ");
  90. serial_putdec(SERIAL_COM1, bus);
  91. serial_putc(SERIAL_COM1, ':');
  92. serial_putdec(SERIAL_COM1, dev);
  93. serial_putc(SERIAL_COM1, '.');
  94. serial_putdec(SERIAL_COM1, func);
  95. serial_puts(SERIAL_COM1, " prog ");
  96. serial_puthex(SERIAL_COM1, prog);
  97. serial_puts(SERIAL_COM1, " ch0 ");
  98. serial_puthex(SERIAL_COM1, b0);
  99. serial_putc(SERIAL_COM1, '-');
  100. serial_puthex(SERIAL_COM1, c0);
  101. serial_puts(SERIAL_COM1, " ch1 ");
  102. serial_puthex(SERIAL_COM1, b1);
  103. serial_putc(SERIAL_COM1, '-');
  104. serial_puthex(SERIAL_COM1, c1);
  105. serial_putc(SERIAL_COM1, '\n');
  106. return;
  107. }
  108. }
  109. }
  110. serial_puts(SERIAL_COM1, "ata: no PCI IDE function, legacy ports\n");
  111. }
  112. /* Delay 400 ns by reading the alt-status register a few times. */
  113. static void ata_400ns(unsigned short ctrl)
  114. {
  115. (void)port_inb(ctrl);
  116. (void)port_inb(ctrl);
  117. (void)port_inb(ctrl);
  118. (void)port_inb(ctrl);
  119. }
  120. static int ata_wait_not_busy(unsigned short ctrl, unsigned int timeout_ms)
  121. {
  122. unsigned int t0 = get_ticks_ms();
  123. while (get_ticks_ms() - t0 < timeout_ms) {
  124. if (!(port_inb(ctrl) & SR_BSY))
  125. return 0;
  126. }
  127. return -1;
  128. }
  129. static int ata_wait_drq(unsigned short base, unsigned int timeout_ms)
  130. {
  131. unsigned int t0 = get_ticks_ms();
  132. while (get_ticks_ms() - t0 < timeout_ms) {
  133. unsigned char st = port_inb(base + REG_CMD);
  134. if (st & SR_ERR)
  135. return -1;
  136. if (st & SR_DRQ)
  137. return 0;
  138. }
  139. return -1;
  140. }
  141. static int ata_identify(int bus, int unit, unsigned short *id)
  142. {
  143. unsigned short base = ata_base[bus];
  144. unsigned short ctrl = ata_ctrl[bus];
  145. unsigned char st;
  146. unsigned int t0;
  147. unsigned int i;
  148. port_outb(base + REG_DRIVE, (unsigned char)(0xA0 | (unit << 4)));
  149. port_outb(base + REG_COUNT, 0);
  150. port_outb(base + REG_LBA_LO, 0);
  151. port_outb(base + REG_LBA_MID, 0);
  152. port_outb(base + REG_LBA_HI, 0);
  153. port_outb(base + REG_CMD, CMD_IDENTIFY);
  154. ata_400ns(ctrl);
  155. t0 = get_ticks_ms();
  156. for (;;) {
  157. st = port_inb(base + REG_CMD);
  158. if (st == 0 || st == 0xFF) /* floating bus: no device */
  159. return -1;
  160. if (!(st & SR_BSY))
  161. break;
  162. if (get_ticks_ms() - t0 > 4000)
  163. return -1;
  164. }
  165. if (!(st & SR_DRQ))
  166. return -1;
  167. for (i = 0; i < 256; i++)
  168. id[i] = port_inw(base + REG_DATA);
  169. return 0;
  170. }
  171. int ata_identify_parse(const unsigned short *id, unsigned int *total_sectors, char model[41])
  172. {
  173. unsigned int sectors, lo, hi, i;
  174. if (id[0] & 0x8000) /* ATAPI (cdrom/zip): not a data disk */
  175. return -1;
  176. /* Model string: words 27..46, byte-swapped. */
  177. for (i = 0; i < 20; i++) {
  178. unsigned short w = id[27 + i];
  179. model[i * 2] = (char)(w >> 8);
  180. model[i * 2 + 1] = (char)(w & 0xFF);
  181. }
  182. model[40] = '\0';
  183. for (i = 40; i > 0; i--) {
  184. if (model[i - 1] == ' ')
  185. model[i - 1] = '\0';
  186. else
  187. break;
  188. }
  189. sectors = (unsigned int)id[60] | ((unsigned int)id[61] << 16);
  190. if (id[83] & 0x0400) { /* LBA48 supported */
  191. lo = (unsigned int)id[100] | ((unsigned int)id[101] << 16);
  192. hi = (unsigned int)id[102] | ((unsigned int)id[103] << 16);
  193. if (lo || hi)
  194. sectors = lo;
  195. if (hi)
  196. sectors = 0xFFFFFFFFu; /* > 2 TB: clamp to 32-bit LBA */
  197. }
  198. *total_sectors = sectors;
  199. return 0;
  200. }
  201. int ata_probe(int bus, int unit, unsigned int *total_sectors, char model[41])
  202. {
  203. static unsigned short id[256];
  204. if (ata_identify(bus, unit, id) != 0)
  205. return -1;
  206. return ata_identify_parse(id, total_sectors, model);
  207. }
  208. static inline void ata_insw(unsigned short port, void *dst, unsigned int count)
  209. {
  210. __asm__ volatile ("rep insw"
  211. : "+D"(dst), "+c"(count)
  212. : "d"(port)
  213. : "memory");
  214. }
  215. static inline void ata_outsw(unsigned short port, const void *src, unsigned int count)
  216. {
  217. __asm__ volatile ("rep outsw"
  218. : "+S"(src), "+c"(count)
  219. : "d"(port)
  220. : "memory");
  221. }
  222. static int ata_xfer(int bus, int unit, unsigned int lba, void *buf,
  223. unsigned int count, int write)
  224. {
  225. unsigned short base = ata_base[bus];
  226. unsigned short ctrl = ata_ctrl[bus];
  227. unsigned char *p = buf;
  228. unsigned int done = 0;
  229. while (done < count) {
  230. unsigned int chunk = count - done;
  231. int lba48;
  232. unsigned int cur = lba + done;
  233. if (chunk > 255)
  234. chunk = 255;
  235. lba48 = ((cur + chunk - 1) >= 0x10000000u);
  236. if (lba48) {
  237. port_outb(base + REG_DRIVE, (unsigned char)(0x40 | (unit << 4)));
  238. port_outb(base + REG_COUNT, (unsigned char)(chunk >> 8));
  239. port_outb(base + REG_LBA_LO, (unsigned char)(cur >> 24));
  240. port_outb(base + REG_LBA_MID, 0);
  241. port_outb(base + REG_LBA_HI, 0);
  242. port_outb(base + REG_COUNT, (unsigned char)(chunk & 0xFF));
  243. port_outb(base + REG_LBA_LO, (unsigned char)(cur & 0xFF));
  244. port_outb(base + REG_LBA_MID, (unsigned char)((cur >> 8) & 0xFF));
  245. port_outb(base + REG_LBA_HI, (unsigned char)((cur >> 16) & 0xFF));
  246. port_outb(base + REG_CMD, (unsigned char)(write ? CMD_WRITE_EXT : CMD_READ_EXT));
  247. } else {
  248. port_outb(base + REG_DRIVE,
  249. (unsigned char)(0xE0 | (unit << 4) | ((cur >> 24) & 0x0F)));
  250. port_outb(base + REG_COUNT, (unsigned char)(chunk & 0xFF));
  251. port_outb(base + REG_LBA_LO, (unsigned char)(cur & 0xFF));
  252. port_outb(base + REG_LBA_MID, (unsigned char)((cur >> 8) & 0xFF));
  253. port_outb(base + REG_LBA_HI, (unsigned char)((cur >> 16) & 0xFF));
  254. port_outb(base + REG_CMD, (unsigned char)(write ? CMD_WRITE : CMD_READ));
  255. }
  256. ata_400ns(ctrl);
  257. if (ata_wait_not_busy(ctrl, 4000) != 0)
  258. return -1;
  259. if (ata_wait_drq(base, 4000) != 0)
  260. return -1;
  261. if (write) {
  262. ata_outsw(base + REG_DATA, p, chunk * 256);
  263. /* Wait for the drive to finish writing to media (BSY clears
  264. * after the data transfer) before issuing the next command. */
  265. if (ata_wait_not_busy(ctrl, 4000) != 0)
  266. return -1;
  267. if (port_inb(base + REG_CMD) & SR_ERR)
  268. return -1;
  269. } else {
  270. ata_insw(base + REG_DATA, p, chunk * 256);
  271. }
  272. p += chunk * 512;
  273. done += chunk;
  274. }
  275. return 0;
  276. }
  277. int ata_read_sectors(int bus, int unit, unsigned int lba, void *buf, unsigned int count)
  278. {
  279. return ata_xfer(bus, unit, lba, buf, count, 0);
  280. }
  281. int ata_write_sectors(int bus, int unit, unsigned int lba, const void *buf, unsigned int count)
  282. {
  283. return ata_xfer(bus, unit, lba, (void *)buf, count, 1);
  284. }