/* Copyright (C) 2021 Rain */
/* This file is part of XNIX. */
/*
XNIX is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) and later version.
*/
/*
XNIX is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
/*
You should have received a copy of the GNU General Public License
along with XNIX. If not, see .
*/
#include
#include
#include
struct ahci_hba_portctl {
__uint32_t cmd_listl;
__uint32_t cmd_listh;
__uint32_t fisl;
__uint32_t fish;
__uint32_t int_status;
__uint32_t int_enable;
__uint32_t cmd;
__uint32_t rsv0;
__uint32_t tfd;
#define SIG_ATA 0x00000101
#define SIG_ATAPI 0xeb140101
#define SIG_SEMB 0xc33c0101
#define SIG_PORTMUL 0x96690101
__uint32_t sig;
__uint32_t status;
__uint32_t ctl;
__uint32_t error;
__uint32_t active;
__uint32_t ci;
__uint32_t sntf;
__uint32_t fbs;
__uint32_t rsv1[11];
__uint32_t vendor[4];
} __attribute__ ((packed));
/* the pcihdr.bar5 points to this */
struct ahci_hba {
/* general host control */
__uint32_t cap0;
__uint32_t ghc;
__uint32_t int_status;
__uint32_t implted_ports;
__uint32_t version;
__uint32_t ccc_ctl, ccc_pts;
__uint32_t em_loc, em_ctl;
__uint32_t cap1;
__uint32_t bohc;
char rsvd[0x74];
/* 1-31 is optional */
struct ahci_hba_portctl portctl[32];
} __attribute__ ((packed));
extern volatile struct ahci_hba *hba;
#define ahci_info(fmt, ...) printk(yellow, "ahci: " fmt, ##__VA_ARGS__)
#define FIS_HOSTTODEV 0x27
#define FIS_DEVTOHOST 0x34
#define FIS_DMA_ACT 0x39
#define FIS_DMA_SETUP 0x41
#define FIS_DATA 0x46
#define FIS_BIST 0x58
#define FIS_PIO_SETUP 0x5f
#define FIS_DEV_BITS 0xa1
#define DEV_NODEV 0x00
#define DEV_SATA 0x01
#define DEV_ATAPI 0x02
#define DEV_PORTMUL 0x03
#define DEV_SEMB 0x04
struct fis_h2d {
__uint8_t fis_type;
__uint8_t pmport : 4;
__uint8_t rsvd0 : 3;
/* cmd / ctrl */
__uint8_t cmd : 1;
__uint8_t command;
__uint8_t feature_low;
__uint8_t lba0, lba1, lba2;
__uint8_t dev;
__uint8_t lba3, lba4, lba5;
__uint8_t feature_high;
__uint8_t count_low;
__uint8_t count_high;
__uint8_t icc;
__uint8_t ctl;
__uint32_t rsvd1;
} __attribute__ ((packed));
struct fis_d2h {
__uint8_t fis_type;
__uint8_t pmport : 4;
__uint8_t rsvd0 : 2;
__uint8_t irq : 1;
__uint8_t rsvd1 : 1;
__uint8_t status;
__uint8_t error;
__uint8_t lba0, lba1, lba2;
__uint8_t dev;
__uint8_t lba3, lba4, lba5;
__uint8_t rsvd2;
__uint8_t count_low, count_high;
__uint16_t rsvd3;
__uint32_t rsvd4;
} __attribute__ ((packed));
struct fis_data {
__uint8_t fis_type;
__uint8_t pmport : 4;
__uint8_t rsvd0 : 4;
__uint16_t rsvd1;
__uint32_t data[0];
} __attribute__ ((packed));
/* the command slot (in the command list) */
struct cmd_header {
/* fis length in __uint32_t */
__uint8_t fis_len : 5;
__uint8_t atapi : 1;
__uint8_t wr : 1;
__uint8_t pre : 1;
__uint8_t rst : 1;
__uint8_t bist : 1;
__uint8_t clear : 1;
__uint8_t rsvd0 : 1;
__uint8_t pmport : 4;
/* prdt length (in entries) */
__uint16_t prdt_len;
__uint32_t bytes_transm;
__uint32_t cmd_table_low;
__uint32_t cmd_table_high;
__uint32_t rsvd1[4];
} __attribute__ ((packed));
struct prdt_entry {
__uint32_t buf_low;
__uint32_t buf_high;
__uint32_t rsvd0;
__uint32_t nbytes : 22;
__uint32_t rsvd1 : 9;
__uint32_t irq : 1;
} __attribute__ ((packed));
/* this is points from the cmd_header */
struct cmd_table {
__uint8_t cmd_fis[64];
__uint8_t atapi_cmd[16];
__uint8_t rsvd0[48];
/* max 65536 */
struct prdt_entry prdts[65536];
} __attribute__ ((packed));
void do_ahci_intr(void);
void set_base(volatile struct ahci_hba_portctl *, int);
void ahci_read(volatile struct ahci_hba_portctl *, __uint64_t, __uint16_t, char *);
__uint64_t ahci_buffer;
#define HBA_PORT_CMD_ST 0x0001
#define HBA_PORT_CMD_FRE 0x0010
#define HBA_PORT_CMD_FR 0x4000
#define HBA_PORT_CMD_CR 0x8000
#define ATACMD_RD 0x20
#define ATACMD_RD48 0x24
#define ATACMD_WR 0x30
#define ATACMD_WR48 0x34
#define ATACMD_RDDMA 0xc8
#define ATACMD_RDDMA48 0x25
#define ATACMD_WRDMA 0xca
#define ATACMD_WRDMA48 0x35
#define ATACMD_ID 0xec