diff options
author | Ian Moffett <ian@osmora.org> | 2024-05-31 00:20:34 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2024-05-31 00:59:42 -0400 |
commit | 65e5d1448eb10fea727d16d0c43a6929f61455c1 (patch) | |
tree | dde0fdb85d0d9487dedde97459d4c4e8e81c6fc6 /sys/include | |
parent | 8728cc9812524e7f37bb4fbe2f48da724c0d8a88 (diff) |
kernel: ic: Add initial AHCI driver code
This commit introduces the initial implementation of the AHCI driver for
Hyra. Support for reading and writing drives still need to be
implemented and devices haven't been exposed to userland yet.
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'sys/include')
-rw-r--r-- | sys/include/dev/ic/ahciregs.h | 142 | ||||
-rw-r--r-- | sys/include/dev/ic/ahcivar.h | 137 |
2 files changed, 279 insertions, 0 deletions
diff --git a/sys/include/dev/ic/ahciregs.h b/sys/include/dev/ic/ahciregs.h new file mode 100644 index 0000000..b2da5b7 --- /dev/null +++ b/sys/include/dev/ic/ahciregs.h @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _AHCIREGS_H_ +#define _AHCIREGS_H_ + +#include <sys/types.h> +#include <sys/cdefs.h> + +struct hba_port { + volatile uint64_t clb; /* Command list base (1k-byte aligned) */ + volatile uint64_t fb; /* FIS base (256-byte aligned) */ + volatile uint32_t is; /* Interrupt status */ + volatile uint32_t ie; /* Interrupt enable */ + volatile uint32_t cmd; /* Command and status */ + volatile uint32_t rsvd0; /* Reserved */ + volatile uint32_t tfd; /* Task file data */ + volatile uint32_t sig; /* Signature */ + volatile uint32_t ssts; /* SATA status */ + volatile uint32_t sctl; /* SATA control */ + volatile uint32_t serr; /* SATA error */ + volatile uint32_t sact; /* SATA active */ + volatile uint32_t ci; /* Command issue */ + volatile uint32_t sntf; /* SATA notification */ + volatile uint32_t fbs; /* FIS-based switch control */ + volatile uint32_t rsvd1[11]; + volatile uint32_t vendor[4]; +}; + +struct hba_memspace { + volatile uint32_t cap; /* Host Capabilities */ + volatile uint32_t ghc; /* Global host control */ + volatile uint32_t is; /* Interrupt status */ + volatile uint32_t pi; /* Ports implemented */ + volatile uint32_t vs; /* Version */ + volatile uint32_t ccc_ctl; /* Command completion coalescing control */ + volatile uint32_t ccc_pts; /* Command completion coalescing ports */ + volatile uint32_t em_loc; /* Enclosure management location */ + volatile uint32_t em_ctl; /* Enclosure management control */ + volatile uint32_t cap2; /* Host capabilities extended */ + volatile uint32_t bohc; /* BIOS/OS Handoff Control and Status */ + volatile uint8_t rsvd[0x74]; /* Reserved */ + volatile uint8_t vendor[0x60]; /* Vendor specific */ + struct hba_port ports[1]; +}; + +/* Global host control bits */ +#define AHCI_GHC_AE __BIT(31) /* AHCI enable */ +#define AHCI_GHC_IE __BIT(1) /* Interrupt enable */ +#define AHCI_GHC_HR __BIT(0) /* HBA reset */ + +/* AHCI port signatures */ +#define AHCI_SIG_ATA 0x00000101 +#define AHCI_SIG_SEMB 0xC33C0101 +#define AHCI_SIG_PM 0x96690101 + +/* + * Port SATA status + * See section 3.3.10 of the AHCI spec. + */ +#define AHCI_PXSSTS_DET(SSTS) (SSTS & 0xF) +#define AHCI_PXSSTS_IPM(SSTS) ((SSTS >> 8) & 0xF) + +/* + * Port SATA control bits + * See section 3.3.11 of the AHCI spec. + */ +#define AHCI_PXSCTL_DET(SCTL) (SCTL & 0xF) + +/* + * Port command and status bits + * See section 3.3.7 of the AHCI spec. + */ +#define AHCI_PXCMD_ST __BIT(0) /* Start */ +#define AHCI_PXCMD_FRE __BIT(4) /* FIS Receive Enable */ +#define AHCI_PXCMD_FR __BIT(14) /* FIS Receive Running */ +#define AHCI_PXCMD_CR __BIT(15) /* Command List Running */ + +/* + * Interrupt status bits + * See section 3.3.5 of the AHCI spec. + */ +#define AHCI_PXIS_TFES __BIT(31) + +/* + * Task file data bits + * See section 3.3.8 of the AHCI spec. + */ +#define AHCI_PXTFD_ERR __BIT(0) +#define AHCI_PXTFD_DRQ __BIT(3) +#define AHCI_PXTFD_BSY __BIT(7) + +/* + * Capability bits + * See section 3.1.1 of the AHCI spec. + */ +#define AHCI_CAP_NP(CAP) (CAP & 0x1F) /* Number of ports */ +#define AHCI_CAP_NCS(CAP) ((CAP >> 8) & 0x1F) /* Number of command slots */ + +/* + * Device detection (DET) and Interface power + * management (IPM) values + * See section 3.3.10 of the AHCI spec. + */ +#define AHCI_DET_NULL 0 /* No device detected */ +#define AHCI_DET_PRESENT 1 /* Device present (no PHY comm) */ +#define AHCI_DET_COMM 3 /* Device present and phy comm established */ +#define AHCI_IPM_ACTIVE 1 + +/* + * Device detection initialization values + * See section 3.3.11 of the AHCI spec. + */ +#define AHCI_DET_COMRESET 1 + +#endif /* !_AHCIREGS_H_ */ diff --git a/sys/include/dev/ic/ahcivar.h b/sys/include/dev/ic/ahcivar.h new file mode 100644 index 0000000..72ca198 --- /dev/null +++ b/sys/include/dev/ic/ahcivar.h @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _AHCIVAR_H_ +#define _AHCIVAR_H_ + +#include <sys/types.h> +#include <sys/cdefs.h> +#include <sys/mutex.h> +#include <dev/ic/ahciregs.h> + +struct ata_identity { + uint16_t rsvd0 : 1; + uint16_t unused0 : 1; + uint16_t incomplete : 1; + uint16_t unused1 : 3; + uint16_t fixed_dev : 1; + uint16_t removable : 1; + uint16_t unused2 : 7; + uint16_t device_type : 1; + uint16_t ncylinders; + uint16_t specific_config; + uint16_t nheads; + uint16_t unused3[2]; + uint16_t sectors_per_track; + uint16_t vendor[3]; + char serial_number[20]; + uint16_t unused4[2]; + uint16_t unused5; + char firmware_rev[8]; + char model_number[40]; + char pad[256]; +}; + +/* Physical region descriptor table entry */ +struct ahci_prdt_entry { + uintptr_t dba; /* Data base address */ + uint32_t rsvd0; /* Reserved */ + uint32_t dbc : 22; /* Count */ + uint16_t rsvd1 : 9; /* Reserved */ + uint8_t i : 1; /* Interrupt on completion */ +}; + +/* Command header */ +struct ahci_cmd_hdr { + uint8_t cfl : 5; /* Command FIS length */ + uint8_t a : 1; /* ATAPI */ + uint8_t w : 1; /* Write */ + uint8_t p : 1; /* Prefetchable */ + uint8_t r : 1; /* Reset */ + uint8_t c : 1; /* Clear busy upon R_OK */ + uint8_t rsvd0 : 1; /* Reserved */ + uint8_t pmp : 4; /* Port multiplier port */ + uint16_t prdtl; /* PRDT length (in entries) */ + volatile uint32_t prdbc; /* PRDT bytes transferred count */ + uintptr_t ctba; /* Command table descriptor base addr */ + uint32_t rsvd1[4]; /* Reserved */ +}; + +/* Command table */ +struct ahci_cmdtab { + uint8_t cfis[64]; + uint8_t acmd[16]; + uint8_t rsvd[48]; + struct ahci_prdt_entry prdt[1]; +}; + +struct ahci_fis_h2d { + uint8_t type; + uint8_t pmp : 4; + uint8_t rsvd0 : 3; + uint8_t c : 1; + uint8_t command; + uint8_t featurel; + uint8_t lba0; + uint8_t lba1; + uint8_t lba2; + uint8_t device; + uint8_t lba3; + uint8_t lba4; + uint8_t lba5; + uint8_t featureh; + uint8_t countl; + uint8_t counth; + uint8_t icc; + uint8_t control; + uint8_t rsvd1[4]; +}; + +struct ahci_hba { + struct hba_memspace *abar; + struct ahci_cmd_hdr *cmdlist; + uint32_t ncmdslots; + uint32_t nports; + struct mutex lock; +}; + +/* Commands */ +#define ATA_CMD_NOP 0x00 +#define ATA_CMD_IDENTIFY 0xEC + +/* FIS types */ +#define FIS_TYPE_H2D 0x27 +#define FIS_TYPE_D2H 0x34 + +#define AHCI_TIMEOUT 500 +#define AHCI_FIS_SIZE 256 +#define AHCI_CMDTAB_SIZE 256 +#define AHCI_CMDENTRY_SIZE 32 + +#endif /* !_AHCIVAR_H_ */ |