Rev 4545 | Rev 4621 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4545 | Rev 4557 | ||
---|---|---|---|
Line 35... | Line 35... | ||
35 | * @brief ATA disk driver |
35 | * @brief ATA disk driver |
36 | * |
36 | * |
37 | * This driver currently works only with CHS addressing and uses PIO. |
37 | * This driver currently works only with CHS addressing and uses PIO. |
38 | * Currently based on the (now obsolete) ANSI X3.221-1994 (ATA-1) standard. |
38 | * Currently based on the (now obsolete) ANSI X3.221-1994 (ATA-1) standard. |
39 | * At this point only reading is possible, not writing. |
39 | * At this point only reading is possible, not writing. |
- | 40 | * |
|
- | 41 | * The driver services a single controller which can have up to two disks |
|
- | 42 | * attached. |
|
40 | */ |
43 | */ |
41 | 44 | ||
42 | #include <stdio.h> |
45 | #include <stdio.h> |
43 | #include <libarch/ddi.h> |
46 | #include <libarch/ddi.h> |
44 | #include <ddi.h> |
47 | #include <ddi.h> |
45 | #include <ipc/ipc.h> |
48 | #include <ipc/ipc.h> |
46 | #include <ipc/bd.h> |
49 | #include <ipc/bd.h> |
47 | #include <async.h> |
50 | #include <async.h> |
48 | #include <as.h> |
51 | #include <as.h> |
49 | #include <futex.h> |
52 | #include <fibril_sync.h> |
50 | #include <devmap.h> |
53 | #include <devmap.h> |
51 | #include <sys/types.h> |
54 | #include <sys/types.h> |
52 | #include <errno.h> |
55 | #include <errno.h> |
53 | #include <bool.h> |
56 | #include <bool.h> |
54 | 57 | ||
Line 62... | Line 65... | ||
62 | static uintptr_t cmd_physical = 0x1f0; |
65 | static uintptr_t cmd_physical = 0x1f0; |
63 | static uintptr_t ctl_physical = 0x170; |
66 | static uintptr_t ctl_physical = 0x170; |
64 | static ata_cmd_t *cmd; |
67 | static ata_cmd_t *cmd; |
65 | static ata_ctl_t *ctl; |
68 | static ata_ctl_t *ctl; |
66 | 69 | ||
67 | static dev_handle_t dev_handle[MAX_DISKS]; |
70 | /** Per-disk state. */ |
68 | - | ||
69 | static atomic_t dev_futex = FUTEX_INITIALIZER; |
- | |
70 | - | ||
71 | static disk_t disk[MAX_DISKS]; |
71 | static disk_t disk[MAX_DISKS]; |
72 | 72 | ||
73 | static int ata_bd_init(void); |
73 | static int ata_bd_init(void); |
74 | static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall); |
74 | static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall); |
75 | static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, size_t size, |
75 | static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, size_t size, |
Line 87... | Line 87... | ||
87 | int i, rc; |
87 | int i, rc; |
88 | int n_disks; |
88 | int n_disks; |
89 | 89 | ||
90 | printf(NAME ": ATA disk driver\n"); |
90 | printf(NAME ": ATA disk driver\n"); |
91 | 91 | ||
92 | printf("cmd_physical = 0x%x\n", cmd_physical); |
92 | printf("I/O address 0x%x\n", cmd_physical); |
93 | printf("ctl_physical = 0x%x\n", ctl_physical); |
- | |
94 | 93 | ||
95 | if (ata_bd_init() != EOK) |
94 | if (ata_bd_init() != EOK) |
96 | return -1; |
95 | return -1; |
97 | 96 | ||
98 | /* Put drives to reset, disable interrupts. */ |
97 | /* Put drives to reset, disable interrupts. */ |
Line 118... | Line 117... | ||
118 | /* Skip unattached drives. */ |
117 | /* Skip unattached drives. */ |
119 | if (disk[i].present == false) |
118 | if (disk[i].present == false) |
120 | continue; |
119 | continue; |
121 | 120 | ||
122 | snprintf(name, 16, "disk%d", i); |
121 | snprintf(name, 16, "disk%d", i); |
123 | rc = devmap_device_register(name, &dev_handle[i]); |
122 | rc = devmap_device_register(name, &disk[i].dev_handle); |
124 | if (rc != EOK) { |
123 | if (rc != EOK) { |
125 | devmap_hangup_phone(DEVMAP_DRIVER); |
124 | devmap_hangup_phone(DEVMAP_DRIVER); |
126 | printf(NAME ": Unable to register device %s.\n", |
125 | printf(NAME ": Unable to register device %s.\n", |
127 | name); |
126 | name); |
128 | return rc; |
127 | return rc; |
Line 179... | Line 178... | ||
179 | case 3: d->heads = data; break; |
178 | case 3: d->heads = data; break; |
180 | case 6: d->sectors = data; break; |
179 | case 6: d->sectors = data; break; |
181 | } |
180 | } |
182 | } |
181 | } |
183 | 182 | ||
184 | printf("\n\nStatus = 0x%x\n", pio_read_8(&cmd->status)); |
- | |
185 | - | ||
186 | d->blocks = d->cylinders * d->heads * d->sectors; |
183 | d->blocks = d->cylinders * d->heads * d->sectors; |
187 | 184 | ||
188 | printf("Geometry: %u cylinders, %u heads, %u sectors\n", |
185 | printf("Geometry: %u cylinders, %u heads, %u sectors\n", |
189 | d->cylinders, d->heads, d->sectors); |
186 | d->cylinders, d->heads, d->sectors); |
190 | 187 | ||
191 | d->present = true; |
188 | d->present = true; |
- | 189 | fibril_mutex_initialize(&d->lock); |
|
192 | 190 | ||
193 | return EOK; |
191 | return EOK; |
194 | } |
192 | } |
195 | 193 | ||
196 | static int ata_bd_init(void) |
194 | static int ata_bd_init(void) |
Line 241... | Line 239... | ||
241 | dh = IPC_GET_ARG1(*icall); |
239 | dh = IPC_GET_ARG1(*icall); |
242 | 240 | ||
243 | /* Determine which disk device is the client connecting to. */ |
241 | /* Determine which disk device is the client connecting to. */ |
244 | disk_id = -1; |
242 | disk_id = -1; |
245 | for (i = 0; i < MAX_DISKS; i++) |
243 | for (i = 0; i < MAX_DISKS; i++) |
246 | if (dev_handle[i] == dh) |
244 | if (disk[i].dev_handle == dh) |
247 | disk_id = i; |
245 | disk_id = i; |
248 | 246 | ||
249 | if (disk_id < 0 || disk[disk_id].present == false) { |
247 | if (disk_id < 0 || disk[disk_id].present == false) { |
250 | ipc_answer_0(iid, EINVAL); |
248 | ipc_answer_0(iid, EINVAL); |
251 | return; |
249 | return; |
Line 353... | Line 351... | ||
353 | /* New value for Drive/Head register */ |
351 | /* New value for Drive/Head register */ |
354 | drv_head = |
352 | drv_head = |
355 | ((disk_id != 0) ? DHR_DRV : 0) | |
353 | ((disk_id != 0) ? DHR_DRV : 0) | |
356 | (h & 0x0f); |
354 | (h & 0x0f); |
357 | 355 | ||
358 | futex_down(&dev_futex); |
356 | fibril_mutex_lock(&d->lock); |
359 | 357 | ||
360 | /* Program a Read Sectors operation. */ |
358 | /* Program a Read Sectors operation. */ |
361 | 359 | ||
362 | pio_write_8(&cmd->drive_head, drv_head); |
360 | pio_write_8(&cmd->drive_head, drv_head); |
363 | pio_write_8(&cmd->sector_count, 1); |
361 | pio_write_8(&cmd->sector_count, 1); |
Line 375... | Line 373... | ||
375 | 373 | ||
376 | data = pio_read_16(&cmd->data_port); |
374 | data = pio_read_16(&cmd->data_port); |
377 | ((uint16_t *) buf)[i] = data; |
375 | ((uint16_t *) buf)[i] = data; |
378 | } |
376 | } |
379 | 377 | ||
380 | futex_up(&dev_futex); |
378 | fibril_mutex_unlock(&d->lock); |
381 | return EOK; |
379 | return EOK; |
382 | } |
380 | } |
383 | 381 | ||
384 | static int ata_bd_write_block(int disk_id, uint64_t blk_idx, size_t blk_cnt, |
382 | static int ata_bd_write_block(int disk_id, uint64_t blk_idx, size_t blk_cnt, |
385 | const void *buf) |
383 | const void *buf) |
Line 407... | Line 405... | ||
407 | /* New value for Drive/Head register */ |
405 | /* New value for Drive/Head register */ |
408 | drv_head = |
406 | drv_head = |
409 | ((disk_id != 0) ? DHR_DRV : 0) | |
407 | ((disk_id != 0) ? DHR_DRV : 0) | |
410 | (h & 0x0f); |
408 | (h & 0x0f); |
411 | 409 | ||
412 | futex_down(&dev_futex); |
410 | fibril_mutex_lock(&d->lock); |
413 | 411 | ||
414 | /* Program a Read Sectors operation. */ |
412 | /* Program a Read Sectors operation. */ |
415 | 413 | ||
416 | pio_write_8(&cmd->drive_head, drv_head); |
414 | pio_write_8(&cmd->drive_head, drv_head); |
417 | pio_write_8(&cmd->sector_count, 1); |
415 | pio_write_8(&cmd->sector_count, 1); |
Line 428... | Line 426... | ||
428 | } while ((status & SR_DRDY) == 0); |
426 | } while ((status & SR_DRDY) == 0); |
429 | 427 | ||
430 | pio_write_16(&cmd->data_port, ((uint16_t *) buf)[i]); |
428 | pio_write_16(&cmd->data_port, ((uint16_t *) buf)[i]); |
431 | } |
429 | } |
432 | 430 | ||
433 | futex_up(&dev_futex); |
431 | fibril_mutex_unlock(&d->lock); |
434 | return EOK; |
432 | return EOK; |
435 | } |
433 | } |
436 | 434 | ||
437 | 435 | ||
438 | /** |
436 | /** |