miniOS
x86_64 hobby kernel with SMP, VFS, and POSIX process model
Loading...
Searching...
No Matches
ata.h
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2026 Christian Spoo
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23#ifndef _MINIOS_DRIVERS_ATA_H_
24#define _MINIOS_DRIVERS_ATA_H_
25
26#include <miniOS/types.h>
27
28
29/* ATA I/O base ports */
30#define ATA_PRIMARY_BASE 0x1F0
31#define ATA_PRIMARY_CTRL 0x3F6
32#define ATA_SECONDARY_BASE 0x170
33#define ATA_SECONDARY_CTRL 0x376
34
35/* IRQ lines */
36#define ATA_PRIMARY_IRQ 14
37#define ATA_SECONDARY_IRQ 15
38
39/* Drive select patterns */
40#define ATA_MASTER 0xA0
41#define ATA_SLAVE 0xB0
42
43/* ATA register offsets from base port */
44#define ATA_REG_DATA 0x00
45#define ATA_REG_ERROR 0x01 /* read */
46#define ATA_REG_FEATURES 0x01 /* write */
47#define ATA_REG_SECCOUNT0 0x02
48#define ATA_REG_LBA0 0x03
49#define ATA_REG_LBA1 0x04
50#define ATA_REG_LBA2 0x05
51#define ATA_REG_HDDEVSEL 0x06
52#define ATA_REG_STATUS 0x07 /* read */
53#define ATA_REG_COMMAND 0x07 /* write */
54
55/* ATA status register bits */
56#define ATA_SR_BSY 0x80
57#define ATA_SR_DRDY 0x40
58#define ATA_SR_DRQ 0x08
59#define ATA_SR_ERR 0x01
60
61/* Bus Mastering DMA register offsets from BMR base port */
62#define BMR_CMD 0x00 /* bit 0 = start/stop, bit 3 = dir (0=read) */
63#define BMR_STATUS 0x02 /* bit 0 = active, bit 1 = error, bit 2 = IRQ */
64#define BMR_PRDT 0x04 /* 32-bit physical address of PRD table */
65
66/* ATA and DMA size constants */
67#define SECTOR_SIZE 512 /* bytes per ATA sector */
68#define MAX_SECTORS_PER_RW 8 /* 8 x 512 B = 4 KiB bounce buffer */
69
70/* ATA commands */
71#define ATA_CMD_READ_DMA_EXT 0x25 /* READ DMA EXT (48-bit LBA) */
72#define ATA_CMD_WRITE_DMA_EXT 0x35 /* WRITE DMA EXT (48-bit LBA) */
73#define ATA_CMD_CACHE_FLUSH_EXT 0xEA /* CACHE FLUSH EXT — flushes write-back cache */
74
75/* PRD table entry flag */
76#define PRD_LAST_ENTRY 0x8000 /* bit 15 = last entry in PRD table */
77
78/* BMR_STATUS bit masks */
79#define BMR_ACTIVE_BIT 0x01 /* DMA transfer in progress */
80#define BMR_ERROR_BIT 0x02 /* DMA error occurred */
81#define BMR_IRQ_BIT 0x04 /* IRQ fired (DMA complete) */
82#define BMR_START_BIT 0x01 /* BMR_CMD bit 0: start DMA */
83
84/* Physical Region Descriptor entry — 8 bytes, 4-byte aligned */
85typedef struct {
86 uint32_t phys_addr; /* physical base of transfer buffer */
87 uint16_t byte_count; /* 0 means 65536 bytes */
88 uint16_t flags; /* bit 15 = 1 -> last entry in table */
89} __attribute__((packed)) prd_entry_t;
90
103int ata_init(void);
104
121int ata_read_sectors(uint64_t lba, uint16_t count, void *buf);
122
136int ata_write_sectors(uint64_t lba, uint16_t count, const void *buf);
137
147int ata_flush(void);
148
149#endif /* _MINIOS_DRIVERS_ATA_H_ */
int ata_read_sectors(uint64_t lba, uint16_t count, void *buf)
Definition ata.c:162
int ata_write_sectors(uint64_t lba, uint16_t count, const void *buf)
Definition ata.c:248
int ata_flush(void)
Definition ata.c:325
int ata_init(void)
Definition ata.c:99
uint32_t phys_addr
Definition ata.h:86
uint16_t byte_count
Definition ata.h:87
uint16_t flags
Definition ata.h:88
unsigned short uint16_t
Definition types.h:31
unsigned int uint32_t
Definition types.h:34
unsigned long int uint64_t
Definition types.h:37
typedef __attribute__