summaryrefslogtreecommitdiff
path: root/src/kernel/ata.c
blob: 0e6cc4ec242764ff95fbc95a347ff03e5fbbafee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdint.h>
#include "panic.h"
#include "util.h"
#include "ata.h"

enum {
  AP_PRIMARY   = 0x01f0,
  AP_SECONDARY = 0x0170,

  AP_DATA  = 0x0,
  AP_ERROR = 0x1,
  AP_FEAT  = 0x1,
  AP_COUNT = 0x2,
  AP_SEC   = 0x3,
  AP_CYL_L = 0x4,
  AP_CYL_H = 0x5,
  AP_HD_DR = 0x6,
  AP_LBA0  = 0x3,
  AP_LBA1  = 0x4,
  AP_LBA2  = 0x5,
  AP_L3_DR = 0x6,
  AP_STAT  = 0x7,
  AP_CMD   = 0x7,

  APC_STAT = 0x0206,
  APC_CTRL = 0x0206,
  APC_ADDR = 0x0207
};

enum {
  ADR_BASE  = 0xa0,
  ADR_CASCD = 0x10,
  ADR_LBA   = 0x40
};

enum {
  AS_ERROR   = 0x01,
  AS_INDEX   = 0x02,
  AS_CORRECT = 0x04,
  AS_DREADY  = 0x08,
  AS_SERVICE = 0x10,
  AS_FAULT   = 0x20,
  AS_READY   = 0x40,
  AS_BUSY    = 0x80
};

enum {
  AC_READ  = 0x20,
  AC_WRITE = 0x30,
  AC_FLUSH = 0xe7
};

void ata_ldelay() {
  uint16_t i = 65535;
  while (i--)
    ;
}

void ata_sdelay() {
  uint8_t i = 255;
  while (i--)
    ;
}

void ata_dpoll() {
  uint8_t s;
  while ((s = inb(AP_PRIMARY | AP_STAT) & (AS_BUSY | AS_DREADY)) != AS_DREADY)
    if (s & (AS_ERROR | AS_FAULT))
      panic("ATA controller error or fault.");
}

uint8_t read_sectors(uint16_t start, uint8_t count, void *buffer) {
  outb(AP_PRIMARY | AP_L3_DR, ADR_BASE | ADR_LBA);
  outb(AP_PRIMARY | AP_LBA2, 0);
  outb(AP_PRIMARY | AP_LBA1, start >> 8);
  outb(AP_PRIMARY | AP_LBA0, start & 0xff);
  outb(AP_PRIMARY | AP_COUNT, count);
  outb(AP_PRIMARY | AP_CMD, AC_READ);
  uint8_t i = count - 1;
  uint16_t *ptr = buffer;
  do {
    ata_dpoll();
    uint8_t j = 255;
    do
      *(ptr++) = inw(AP_PRIMARY | AP_DATA);
    while (j--);
    ata_ldelay();
  } while (i--);
  return count;//TODO: return early if necessary
}

uint8_t write_sectors(uint16_t start, uint8_t count, void *buffer) {
  outb(AP_PRIMARY | AP_L3_DR, ADR_BASE | ADR_LBA);
  outb(AP_PRIMARY | AP_LBA2, 0);
  outb(AP_PRIMARY | AP_LBA1, start >> 8);
  outb(AP_PRIMARY | AP_LBA0, start & 0xff);
  outb(AP_PRIMARY | AP_COUNT, count);
  outb(AP_PRIMARY | AP_CMD, AC_WRITE);
  uint8_t i = 0;
  uint16_t *ptr = buffer;
  do {
    ata_dpoll();
    uint8_t j = 0;
    do {
      ata_sdelay();
      outw(AP_PRIMARY | AP_DATA, *(ptr++));
    }
    while (++j);
    ata_ldelay();
  } while (++i != count);
  outb(AP_PRIMARY | AP_CMD, AC_FLUSH);
  return count;//TODO: return early if necessary
}