rev |
line source |
nuclear@78
|
1 #include <stdio.h>
|
nuclear@78
|
2 #include <stdlib.h>
|
nuclear@78
|
3 #include <string.h>
|
nuclear@83
|
4 #include <ctype.h>
|
nuclear@78
|
5 #include <inttypes.h>
|
nuclear@78
|
6 #include <assert.h>
|
nuclear@78
|
7 #include "ata.h"
|
nuclear@78
|
8 #include "intr.h"
|
nuclear@78
|
9 #include "asmops.h"
|
nuclear@87
|
10 #include "sched.h"
|
nuclear@86
|
11 #include "mutex.h"
|
nuclear@78
|
12
|
nuclear@78
|
13 /* registers */
|
nuclear@78
|
14 #define REG_DATA 0 /* R/W */
|
nuclear@78
|
15 #define REG_ERROR 1 /* R */
|
nuclear@78
|
16 #define REG_FEATURES 1 /* W */
|
nuclear@78
|
17 #define REG_COUNT 2 /* R/W */
|
nuclear@78
|
18 #define REG_LBA0 3 /* R/W */
|
nuclear@78
|
19 #define REG_LBA1 4 /* R/W */
|
nuclear@78
|
20 #define REG_LBA2 5 /* R/W */
|
nuclear@78
|
21 #define REG_DEVICE 6 /* R/W */
|
nuclear@78
|
22 #define REG_CMD 7 /* W */
|
nuclear@78
|
23 #define REG_STATUS 7 /* R */
|
nuclear@78
|
24
|
nuclear@78
|
25 #define REG_CTL 518
|
nuclear@78
|
26 #define REG_ALTSTAT 518
|
nuclear@78
|
27
|
nuclear@78
|
28 /* status bit fields */
|
nuclear@78
|
29 #define ST_ERR (1 << 0)
|
nuclear@78
|
30 #define ST_DRQ (1 << 3)
|
nuclear@78
|
31 #define ST_DRDY (1 << 6)
|
nuclear@78
|
32 #define ST_BSY (1 << 7)
|
nuclear@78
|
33
|
nuclear@78
|
34 /* device select bit in control register */
|
nuclear@78
|
35 #define DEV_SEL(x) (((x) & 1) << 4)
|
nuclear@86
|
36 #define DEV_LBA (1 << 6)
|
nuclear@78
|
37
|
nuclear@78
|
38 /* ATA commands */
|
nuclear@78
|
39 #define CMD_IDENTIFY 0xec
|
nuclear@86
|
40 #define CMD_READ 0x20
|
nuclear@86
|
41 #define CMD_READ48 0x24
|
nuclear@86
|
42 #define CMD_WRITE 0x30
|
nuclear@86
|
43 #define CMD_WRITE48 0x34
|
nuclear@78
|
44
|
nuclear@78
|
45
|
nuclear@78
|
46 struct device {
|
nuclear@78
|
47 int id; /* id of the device on its ATA interface (0 master, 1 slave) */
|
nuclear@78
|
48 int iface; /* ATA interface for this device (0 or 1) */
|
nuclear@78
|
49 int port_base; /* interface I/O port base */
|
nuclear@78
|
50
|
nuclear@78
|
51 uint32_t nsect_lba;
|
nuclear@78
|
52 uint64_t nsect_lba48;
|
nuclear@78
|
53 };
|
nuclear@78
|
54
|
nuclear@78
|
55
|
nuclear@78
|
56 static int identify(struct device *dev, int iface, int id);
|
nuclear@78
|
57 static void select_dev(struct device *dev);
|
nuclear@78
|
58 static int wait_busy(struct device *dev);
|
nuclear@78
|
59 static int wait_drq(struct device *dev);
|
nuclear@78
|
60 static void read_data(struct device *dev, void *buf);
|
nuclear@78
|
61 static inline uint8_t read_reg8(struct device *dev, int reg);
|
nuclear@78
|
62 static inline uint16_t read_reg16(struct device *dev, int reg);
|
nuclear@78
|
63 static inline void write_reg8(struct device *dev, int reg, uint8_t val);
|
nuclear@78
|
64 static inline void write_reg16(struct device *dev, int reg, uint16_t val);
|
nuclear@78
|
65 static void ata_intr(int inum);
|
nuclear@80
|
66 static void *atastr(void *res, void *src, int n);
|
nuclear@78
|
67 static char *size_str(uint64_t nsect, char *buf);
|
nuclear@87
|
68 static void print_error(int devid, int wr, uint32_t high, uint32_t low, unsigned char err);
|
nuclear@78
|
69
|
nuclear@78
|
70 /* last drive selected on each bus */
|
nuclear@78
|
71 static int drvsel[2] = {-1, -1};
|
nuclear@78
|
72
|
nuclear@78
|
73 /* 4 possible devices: 2 ATA interfaces with 2 devices each.
|
nuclear@78
|
74 * this will never change unless we start querying the PCI config space
|
nuclear@78
|
75 * for additional drives (in which case this whole init code must be
|
nuclear@78
|
76 * rewritten anyway), but I like it spelt out like this.
|
nuclear@78
|
77 */
|
nuclear@78
|
78 #define MAX_IFACES 2
|
nuclear@78
|
79 #define MAX_DEV (MAX_IFACES * 2)
|
nuclear@86
|
80 static struct device devices[MAX_DEV];
|
nuclear@86
|
81
|
nuclear@86
|
82 static int use_irq;
|
nuclear@86
|
83
|
nuclear@86
|
84 /* This serves as a sync point for I/O. While the mutex is held,
|
nuclear@86
|
85 * some process is doing I/O and all the others must wait.
|
nuclear@86
|
86 */
|
nuclear@86
|
87 static mutex_t pending;
|
nuclear@78
|
88
|
nuclear@78
|
89
|
nuclear@78
|
90 void init_ata(void)
|
nuclear@78
|
91 {
|
nuclear@78
|
92 int i;
|
nuclear@78
|
93
|
nuclear@78
|
94 interrupt(IRQ_TO_INTR(15), ata_intr);
|
nuclear@78
|
95
|
nuclear@78
|
96 for(i=0; i<MAX_DEV; i++) {
|
nuclear@78
|
97 int iface = i / MAX_IFACES;
|
nuclear@78
|
98 int id = i % MAX_IFACES;
|
nuclear@78
|
99
|
nuclear@86
|
100 if(identify(devices + i, iface, id) == -1) {
|
nuclear@86
|
101 devices[i].id = -1;
|
nuclear@78
|
102 }
|
nuclear@78
|
103 }
|
nuclear@86
|
104
|
nuclear@86
|
105 /* init code done, from now on use the irq sleep/wakeup mechanism */
|
nuclear@86
|
106 use_irq = 1;
|
nuclear@86
|
107 }
|
nuclear@86
|
108
|
nuclear@86
|
109 int ata_read_pio(int devno, uint64_t sect, void *buf)
|
nuclear@86
|
110 {
|
nuclear@86
|
111 int cmd, st, res = -1;
|
nuclear@87
|
112 uint32_t sect_low, sect_high;
|
nuclear@87
|
113 struct device *dev = devices + devno;
|
nuclear@86
|
114
|
nuclear@86
|
115 if(dev->id == -1) {
|
nuclear@86
|
116 return -1;
|
nuclear@86
|
117 }
|
nuclear@86
|
118
|
nuclear@86
|
119 if(use_irq) {
|
nuclear@86
|
120 /* wait for the interface to become available */
|
nuclear@86
|
121 mutex_lock(&pending);
|
nuclear@86
|
122 }
|
nuclear@86
|
123
|
nuclear@86
|
124 select_dev(dev);
|
nuclear@86
|
125
|
nuclear@86
|
126 /* LBA48 requires the high-order bits first */
|
nuclear@86
|
127 if(sect >= dev->nsect_lba) {
|
nuclear@87
|
128 sect_high = (uint32_t)(sect >> 24);
|
nuclear@86
|
129 sect_low = (uint32_t)sect & 0xffffff;
|
nuclear@86
|
130
|
nuclear@86
|
131 if(sect >= dev->nsect_lba48) {
|
nuclear@86
|
132 goto end;
|
nuclear@86
|
133 }
|
nuclear@86
|
134 cmd = CMD_READ48;
|
nuclear@86
|
135
|
nuclear@86
|
136 write_reg8(dev, REG_COUNT, 0);
|
nuclear@86
|
137 write_reg8(dev, REG_LBA0, sect_high & 0xff);
|
nuclear@86
|
138 write_reg8(dev, REG_LBA1, (sect_high >> 8) & 0xff);
|
nuclear@86
|
139 write_reg8(dev, REG_LBA2, (sect_high >> 16) & 0xff);
|
nuclear@86
|
140 } else {
|
nuclear@86
|
141 cmd = CMD_READ;
|
nuclear@87
|
142 sect_high = 0;
|
nuclear@87
|
143 sect_low = (uint32_t)sect & 0xfffffff;
|
nuclear@86
|
144 }
|
nuclear@86
|
145
|
nuclear@86
|
146 write_reg8(dev, REG_COUNT, 1);
|
nuclear@86
|
147 write_reg8(dev, REG_LBA0, sect_low & 0xff);
|
nuclear@86
|
148 write_reg8(dev, REG_LBA1, (sect_low >> 8) & 0xff);
|
nuclear@86
|
149 write_reg8(dev, REG_LBA2, (sect_low >> 16) & 0xff);
|
nuclear@87
|
150 write_reg8(dev, REG_DEVICE, ((sect_low >> 24) & 0xf) | DEV_LBA | DEV_SEL(dev->id));
|
nuclear@86
|
151 /* execute */
|
nuclear@86
|
152 write_reg8(dev, REG_CMD, cmd);
|
nuclear@86
|
153
|
nuclear@86
|
154 /* wait for the data to become available */
|
nuclear@86
|
155 do {
|
nuclear@86
|
156 if(use_irq) {
|
nuclear@86
|
157 /* also sleep on the mutex if we're called from userspace */
|
nuclear@86
|
158 wait(&pending);
|
nuclear@86
|
159 }
|
nuclear@87
|
160 } while(((st = read_reg8(dev, REG_ALTSTAT)) & (ST_DRQ | ST_ERR)) == 0);
|
nuclear@86
|
161
|
nuclear@86
|
162 if(st & ST_ERR) {
|
nuclear@87
|
163 //print_error(int devid, int wr, uint32_t high, uint32_t low, unsigned char err);
|
nuclear@87
|
164 unsigned char err;
|
nuclear@87
|
165
|
nuclear@87
|
166 err = read_reg8(dev, REG_ERROR);
|
nuclear@87
|
167 print_error(devno, 0, sect_high, sect_low, err);
|
nuclear@86
|
168 goto end;
|
nuclear@86
|
169 }
|
nuclear@86
|
170
|
nuclear@86
|
171 /* read the data and we're done */
|
nuclear@86
|
172 read_data(dev, buf);
|
nuclear@86
|
173 res = 0;
|
nuclear@86
|
174 end:
|
nuclear@86
|
175 if(use_irq) {
|
nuclear@86
|
176 mutex_unlock(&pending);
|
nuclear@86
|
177 }
|
nuclear@86
|
178 return res;
|
nuclear@86
|
179 }
|
nuclear@86
|
180
|
nuclear@86
|
181 int ata_write_pio(int devno, uint64_t sect, void *buf)
|
nuclear@86
|
182 {
|
nuclear@87
|
183 if(devices[devno].id == -1) {
|
nuclear@86
|
184 return -1;
|
nuclear@86
|
185 }
|
nuclear@87
|
186 return -1;
|
nuclear@78
|
187 }
|
nuclear@78
|
188
|
nuclear@78
|
189 static int identify(struct device *dev, int iface, int id)
|
nuclear@78
|
190 {
|
nuclear@78
|
191 /* base address of the two ATA interfaces */
|
nuclear@78
|
192 static const int port_base[] = {0x1f0, 0x170};
|
nuclear@78
|
193 unsigned char st;
|
nuclear@78
|
194 uint16_t *info;
|
nuclear@78
|
195 char textbuf[42]; /* at most we need 40 chars for ident strings */
|
nuclear@78
|
196
|
nuclear@78
|
197 dev->id = id;
|
nuclear@78
|
198 dev->iface = iface;
|
nuclear@78
|
199 dev->port_base = port_base[iface];
|
nuclear@78
|
200
|
nuclear@78
|
201 /* a status of 0xff means there's no drive on the interface */
|
nuclear@78
|
202 if((st = read_reg8(dev, REG_ALTSTAT)) == 0xff) {
|
nuclear@78
|
203 return -1;
|
nuclear@78
|
204 }
|
nuclear@78
|
205
|
nuclear@78
|
206 select_dev(dev);
|
nuclear@78
|
207
|
nuclear@78
|
208 write_reg8(dev, REG_CMD, CMD_IDENTIFY);
|
nuclear@78
|
209
|
nuclear@78
|
210 if(!(st = read_reg8(dev, REG_ALTSTAT)) || (st & ST_ERR)) {
|
nuclear@78
|
211 /* does not exist */
|
nuclear@78
|
212 return -1;
|
nuclear@78
|
213 }
|
nuclear@78
|
214 if(wait_busy(dev) == -1) {
|
nuclear@78
|
215 /* got ST_ERR, not ATA */
|
nuclear@78
|
216 return -1;
|
nuclear@78
|
217 }
|
nuclear@78
|
218
|
nuclear@78
|
219 info = malloc(512);
|
nuclear@78
|
220 assert(info);
|
nuclear@78
|
221
|
nuclear@78
|
222 /* read the device information */
|
nuclear@78
|
223 read_data(dev, info);
|
nuclear@78
|
224
|
nuclear@78
|
225 /* print model and serial */
|
nuclear@83
|
226 printf("ata%d: %s", (dev->iface << 1) | dev->id, atastr(textbuf, info + 27, 40));
|
nuclear@83
|
227 printf(" [s/n: %s]", atastr(textbuf, info + 10, 20));
|
nuclear@78
|
228
|
nuclear@78
|
229 dev->nsect_lba = *(uint32_t*)(info + 60);
|
nuclear@78
|
230 dev->nsect_lba48 = *(uint64_t*)(info + 100) & 0xffffffffffffull;
|
nuclear@78
|
231
|
nuclear@78
|
232 if(!dev->nsect_lba) {
|
nuclear@78
|
233 printf(" drive does not support LBA, ignoring!\n");
|
nuclear@78
|
234 free(info);
|
nuclear@78
|
235 return -1;
|
nuclear@78
|
236 }
|
nuclear@78
|
237
|
nuclear@78
|
238 if(dev->nsect_lba48) {
|
nuclear@78
|
239 size_str(dev->nsect_lba48, textbuf);
|
nuclear@78
|
240 } else {
|
nuclear@78
|
241 size_str(dev->nsect_lba, textbuf);
|
nuclear@78
|
242 }
|
nuclear@83
|
243 printf(" size: %s\n", textbuf);
|
nuclear@78
|
244
|
nuclear@78
|
245 free(info);
|
nuclear@78
|
246 return 0;
|
nuclear@78
|
247 }
|
nuclear@78
|
248
|
nuclear@78
|
249 static void select_dev(struct device *dev)
|
nuclear@78
|
250 {
|
nuclear@78
|
251 /* if this is the currently selected device, thy work is done */
|
nuclear@78
|
252 if(drvsel[dev->iface] == dev->id)
|
nuclear@78
|
253 return;
|
nuclear@78
|
254
|
nuclear@78
|
255 /* wait for BSY and DRQ to clear */
|
nuclear@78
|
256 while(read_reg8(dev, REG_ALTSTAT) & (ST_BSY | ST_DRQ));
|
nuclear@78
|
257
|
nuclear@78
|
258 /* set the correct device bit to the device register */
|
nuclear@78
|
259 write_reg8(dev, REG_DEVICE, DEV_SEL(dev->id));
|
nuclear@86
|
260
|
nuclear@86
|
261 /* wait a bit to allow the device time to respond */
|
nuclear@86
|
262 iodelay(); iodelay(); iodelay(); iodelay();
|
nuclear@78
|
263 }
|
nuclear@78
|
264
|
nuclear@78
|
265 static int wait_busy(struct device *dev)
|
nuclear@78
|
266 {
|
nuclear@78
|
267 unsigned char st;
|
nuclear@78
|
268
|
nuclear@78
|
269 do {
|
nuclear@78
|
270 st = read_reg8(dev, REG_ALTSTAT);
|
nuclear@78
|
271 } while((st & ST_BSY) && !(st & ST_ERR));
|
nuclear@78
|
272
|
nuclear@78
|
273 return st & ST_ERR ? -1 : 0;
|
nuclear@78
|
274 }
|
nuclear@78
|
275
|
nuclear@78
|
276 static int wait_drq(struct device *dev)
|
nuclear@78
|
277 {
|
nuclear@78
|
278 unsigned char st;
|
nuclear@78
|
279
|
nuclear@78
|
280 do {
|
nuclear@78
|
281 st = read_reg8(dev, REG_ALTSTAT);
|
nuclear@78
|
282 } while(!(st & (ST_DRQ | ST_ERR)));
|
nuclear@78
|
283
|
nuclear@78
|
284 return st & ST_ERR ? -1 : 0;
|
nuclear@78
|
285 }
|
nuclear@78
|
286
|
nuclear@78
|
287 static void read_data(struct device *dev, void *buf)
|
nuclear@78
|
288 {
|
nuclear@78
|
289 int i;
|
nuclear@78
|
290 uint16_t *ptr = buf;
|
nuclear@78
|
291
|
nuclear@78
|
292 /* wait for the data request from the drive */
|
nuclear@78
|
293 wait_drq(dev);
|
nuclear@78
|
294
|
nuclear@78
|
295 /* ready to transfer */
|
nuclear@78
|
296 for(i=0; i<256; i++) {
|
nuclear@78
|
297 *ptr++ = read_reg16(dev, REG_DATA);
|
nuclear@78
|
298 }
|
nuclear@78
|
299 }
|
nuclear@78
|
300
|
nuclear@78
|
301 static inline uint8_t read_reg8(struct device *dev, int reg)
|
nuclear@78
|
302 {
|
nuclear@78
|
303 uint8_t val;
|
nuclear@78
|
304 inb(val, dev->port_base + reg);
|
nuclear@78
|
305 return val;
|
nuclear@78
|
306 }
|
nuclear@78
|
307
|
nuclear@78
|
308 static inline uint16_t read_reg16(struct device *dev, int reg)
|
nuclear@78
|
309 {
|
nuclear@78
|
310 uint16_t val;
|
nuclear@78
|
311 inw(val, dev->port_base + reg);
|
nuclear@78
|
312 return val;
|
nuclear@78
|
313 }
|
nuclear@78
|
314
|
nuclear@78
|
315 static inline void write_reg8(struct device *dev, int reg, uint8_t val)
|
nuclear@78
|
316 {
|
nuclear@78
|
317 outb(val, dev->port_base + reg);
|
nuclear@78
|
318 }
|
nuclear@78
|
319
|
nuclear@78
|
320 static inline void write_reg16(struct device *dev, int reg, uint16_t val)
|
nuclear@78
|
321 {
|
nuclear@78
|
322 outw(val, dev->port_base + reg);
|
nuclear@78
|
323 }
|
nuclear@78
|
324
|
nuclear@78
|
325 static void ata_intr(int inum)
|
nuclear@78
|
326 {
|
nuclear@78
|
327 printf("ATA interrupt\n");
|
nuclear@78
|
328 }
|
nuclear@78
|
329
|
nuclear@80
|
330 static void *atastr(void *res, void *src, int n)
|
nuclear@80
|
331 {
|
nuclear@80
|
332 int i;
|
nuclear@80
|
333 uint16_t *sptr = (uint16_t*)src;
|
nuclear@80
|
334 char *dptr = res;
|
nuclear@80
|
335
|
nuclear@80
|
336 for(i=0; i<n/2; i++) {
|
nuclear@80
|
337 *dptr++ = (*sptr & 0xff00) >> 8;
|
nuclear@80
|
338 *dptr++ = *sptr++ & 0xff;
|
nuclear@80
|
339 }
|
nuclear@83
|
340
|
nuclear@83
|
341 while(isspace(*--dptr));
|
nuclear@83
|
342 *++dptr = 0;
|
nuclear@80
|
343 return res;
|
nuclear@80
|
344 }
|
nuclear@80
|
345
|
nuclear@78
|
346 static char *size_str(uint64_t nsect, char *buf)
|
nuclear@78
|
347 {
|
nuclear@78
|
348 static const char *suffix[] = {"kb", "mb", "gb", "tb", "pb", 0};
|
nuclear@78
|
349 int i;
|
nuclear@82
|
350 unsigned int rem;
|
nuclear@78
|
351
|
nuclear@78
|
352 /* start with kilobytes */
|
nuclear@78
|
353 nsect /= 2;
|
nuclear@78
|
354
|
nuclear@78
|
355 for(i=0; nsect >= 1024 && suffix[i + 1]; i++) {
|
nuclear@82
|
356 rem = nsect % 1024;
|
nuclear@78
|
357 nsect /= 1024;
|
nuclear@78
|
358 }
|
nuclear@82
|
359 sprintf(buf, "%u.%u%s", (unsigned int)nsect, 100 * rem / 1024, suffix[i]);
|
nuclear@78
|
360 return buf;
|
nuclear@78
|
361 }
|
nuclear@78
|
362
|
nuclear@87
|
363 #define ERR_NM (1 << 1)
|
nuclear@87
|
364 #define ERR_ABRT (1 << 2)
|
nuclear@87
|
365 #define ERR_MCR (1 << 3)
|
nuclear@87
|
366 #define ERR_IDNF (1 << 4)
|
nuclear@87
|
367 #define ERR_MC (1 << 5)
|
nuclear@87
|
368 #define ERR_UNC (1 << 6)
|
nuclear@87
|
369
|
nuclear@87
|
370 static void print_error(int devid, int wr, uint32_t high, uint32_t low, unsigned char err)
|
nuclear@87
|
371 {
|
nuclear@87
|
372 printf("ata%d %s %serror ", devid, wr ? "write" : "read", err & ERR_UNC ? "uncorrectable " : "");
|
nuclear@87
|
373 printf("at sector %x%x: ", high, low);
|
nuclear@87
|
374
|
nuclear@87
|
375 if(err & ERR_ABRT)
|
nuclear@87
|
376 printf("abort ");
|
nuclear@87
|
377 if(err & ERR_IDNF)
|
nuclear@87
|
378 printf("invalid address ");
|
nuclear@87
|
379 if(err & ERR_NM)
|
nuclear@87
|
380 printf("no media ");
|
nuclear@87
|
381
|
nuclear@87
|
382 printf("(%x)\n", (unsigned int)err);
|
nuclear@87
|
383 }
|