228 lines
5.5 KiB
C
228 lines
5.5 KiB
C
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
#include "stdio.h"
|
|
#include <kernel/mem.h>
|
|
#include <kernel/irq.h>
|
|
|
|
#include <dev/ahci.h>
|
|
#include <dev/pci.h>
|
|
|
|
static hbamem_t *abar = NULL;
|
|
|
|
static uint8_t sata_device_count = 0;
|
|
static struct sata_device sata_devices[32] = { 0 };
|
|
|
|
static paddr_t ahci_pbase = 0;
|
|
|
|
static void ahci_irq_handler()
|
|
{
|
|
}
|
|
|
|
static int ahci_port_type(hbaport_t *port)
|
|
{
|
|
uint32_t ssts = port->ssts;
|
|
uint8_t det = ssts & 0x0F;
|
|
|
|
if (det != HBA_PORT_DET_PRESENT)
|
|
return AHCI_DEV_NULL;
|
|
|
|
if (port->clb == 0)
|
|
return AHCI_DEV_NULL;
|
|
|
|
switch (port->sig) {
|
|
case SATA_SIG_ATAPI:
|
|
return AHCI_DEV_SATAPI;
|
|
case SATA_SIG_SEMB:
|
|
return AHCI_DEV_SEMB;
|
|
case SATA_SIG_PM:
|
|
return AHCI_DEV_PM;
|
|
default:
|
|
return AHCI_DEV_SATA;
|
|
}
|
|
}
|
|
|
|
static void halt_cmd(hbaport_t *port)
|
|
{
|
|
port->cmd &= ~HBA_PORT_CMD_ST;
|
|
port->cmd &= ~HBA_PORT_CMD_FRE;
|
|
|
|
while (port->cmd & (HBA_PORT_CMD_FR | HBA_PORT_CMD_CR))
|
|
;
|
|
}
|
|
|
|
static void start_cmd(hbaport_t *port)
|
|
{
|
|
while (port->cmd & HBA_PORT_CMD_CR)
|
|
;
|
|
|
|
port->cmd |= HBA_PORT_CMD_FRE;
|
|
port->cmd |= HBA_PORT_CMD_ST;
|
|
}
|
|
|
|
static void ahci_rebase(hbaport_t *port, int n)
|
|
{
|
|
halt_cmd(port);
|
|
|
|
port->clb = (ahci_pbase) + (n << 10);
|
|
memset((void *)(port->clb | hhdm_start), 0, 1024);
|
|
|
|
port->fb = ahci_pbase + (32 << 10) + (n << 8);
|
|
memset((void *)(port->fb | hhdm_start), 0, 256);
|
|
|
|
struct hba_cmd_header *cmdheader = (struct hba_cmd_header *)(port->clb | hhdm_start);
|
|
for (int i = 0; i < 32; i++) {
|
|
cmdheader[i].prdt_len = 8;
|
|
cmdheader[i].cmd_table_base = (ahci_pbase) + (n << 13) + (40 << 10) + (i << 8);
|
|
|
|
void *ctba = (void *)((uintptr_t)cmdheader[i].cmd_table_base | hhdm_start);
|
|
memset(ctba, 0, 256);
|
|
}
|
|
|
|
start_cmd(port);
|
|
}
|
|
|
|
static void ahci_probe_ports(struct pci_device *dev, hbamem_t *abar)
|
|
{
|
|
uint32_t pi = abar->pi;
|
|
for (int i = 0; i < 32; i++) {
|
|
if (pi & 1) {
|
|
uint32_t type = ahci_port_type(&abar->ports[i]);
|
|
if (type == AHCI_DEV_SATA) {
|
|
printf(LOG_INFO "SATA drive found at port %d\n", i);
|
|
sata_devices[sata_device_count].port = i;
|
|
sata_devices[sata_device_count].controller = dev;
|
|
sata_devices[sata_device_count].abar = abar;
|
|
|
|
ahci_rebase(&abar->ports[i], i);
|
|
sata_device_count++;
|
|
} else if (type == AHCI_DEV_SATAPI) {
|
|
printf(LOG_INFO "SATAPI drive found at port %d\n", i);
|
|
} else if (type == AHCI_DEV_SEMB) {
|
|
printf(LOG_INFO "SEMB drive found at port %d\n", i);
|
|
} else if (type == AHCI_DEV_PM) {
|
|
printf(LOG_INFO "PM drive found at port %d\n", i);
|
|
}
|
|
}
|
|
pi >>= 1;
|
|
}
|
|
}
|
|
|
|
static int cmdslot(hbaport_t *port)
|
|
{
|
|
uint32_t slots = (port->sact | port->ci);
|
|
for (int i = 0; i < 32; i++) {
|
|
if ((slots & 1) == 0)
|
|
return i;
|
|
slots >>= 1;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
bool ahci_read_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf)
|
|
{
|
|
hbaport_t *port = &dev->abar->ports[dev->port];
|
|
|
|
/* clear pending interrupt */
|
|
port->is = (uint32_t)-1;
|
|
|
|
int spin = 0;
|
|
int slot = cmdslot(port);
|
|
if (slot == -1)
|
|
return false;
|
|
|
|
struct hba_cmd_header *cmdheader = (struct hba_cmd_header *)(port->clb | hhdm_start);
|
|
cmdheader += slot;
|
|
cmdheader->cmd_fis_len = sizeof(struct fis_reg_h2d) / sizeof(uint32_t);
|
|
cmdheader->write = 0;
|
|
cmdheader->prdt_len = (uint16_t)((count - 1) >> 4) + 1;
|
|
|
|
struct hba_cmd_tbl *cmdtbl = (struct hba_cmd_tbl *)(cmdheader->cmd_table_base | hhdm_start);
|
|
memset(cmdtbl, 0, sizeof(struct hba_cmd_tbl) + (cmdheader->prdt_len - 1) * sizeof(struct hba_prdt_entry));
|
|
|
|
int i;
|
|
for (i = 0; i < cmdheader->prdt_len - 1; i++) {
|
|
cmdtbl->prdt_entry[i].data_base = buf;
|
|
cmdtbl->prdt_entry[i].byte_count = 8 * 1024 - 1;
|
|
cmdtbl->prdt_entry[i].interrupt = 1;
|
|
buf += 4 * 1024;
|
|
count -= 16;
|
|
}
|
|
|
|
cmdtbl->prdt_entry[i].data_base = buf;
|
|
cmdtbl->prdt_entry[i].byte_count = (count << 9) - 1;
|
|
cmdtbl->prdt_entry[i].interrupt = 1;
|
|
|
|
struct fis_reg_h2d *cmdfis = (struct fis_reg_h2d *)(&cmdtbl->cfis);
|
|
cmdfis->fis_type = FIS_REG_H2D;
|
|
cmdfis->cmd_mode = 1;
|
|
cmdfis->command = ATA_CMD_READ_DMA_EX;
|
|
|
|
cmdfis->lba0 = (uint8_t)lba;
|
|
cmdfis->lba1 = (uint8_t)(lba >> 8);
|
|
cmdfis->lba2 = (uint8_t)(lba >> 16);
|
|
cmdfis->device = 1 << 6; /* LBA mode */
|
|
cmdfis->lba3 = (uint8_t)(lba >> 24);
|
|
cmdfis->lba4 = (uint8_t)(lba >> 32);
|
|
cmdfis->lba5 = (uint8_t)(lba >> 40);
|
|
|
|
cmdfis->count_low = count & 0xff;
|
|
cmdfis->count_high = (count >> 8) & 0xff;
|
|
|
|
/* wait until port is free */
|
|
while ((port->tfd & (ATA_DEV_BUSY | ATA_DEV_DRQ)) && spin < 1000000) {
|
|
spin++;
|
|
}
|
|
|
|
if (spin == 1000000) {
|
|
printf(LOG_ERROR "AHCI: Port is hung\n");
|
|
return false;
|
|
}
|
|
|
|
port->ci = 1 << slot;
|
|
|
|
while (1) {
|
|
if ((port->ci & (1 << slot)) == 0)
|
|
break;
|
|
if (port->is & HBA_PORT_IS_TFES) {
|
|
printf(LOG_ERROR "AHCI: Read disk error\n");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if(port->is & HBA_PORT_IS_TFES) {
|
|
printf(LOG_ERROR "AHCI: Read disk error\n");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void ahci_init()
|
|
{
|
|
struct pci_device *dev = pci_find_device(0x01, 0x06);
|
|
/* check interrupt number */
|
|
if (dev == NULL) {
|
|
printf(LOG_ERROR "Failed to find AHCI device\n");
|
|
return;
|
|
}
|
|
abar = (hbamem_t *)((uintptr_t)dev->bar5 | hhdm_start);
|
|
|
|
if (!(abar->ghc & (1 << 31))) {
|
|
printf(LOG_ERROR "AHCI controller not enabled\n");
|
|
return;
|
|
}
|
|
|
|
ahci_probe_ports(dev, abar);
|
|
ahci_pbase = (paddr_t)buddy_alloc(0x100000); /* 1 MiB */
|
|
|
|
/* setup device irq number */
|
|
uint16_t irq = irq_highest_free();
|
|
uint32_t tmp = pci_config_read_long(dev->bus, dev->slot, dev->func, 0x3C);
|
|
tmp &= 0xFFFFFF00;
|
|
tmp |= irq;
|
|
pci_config_write_long(dev->bus, dev->slot, dev->func, 0x3C, tmp);
|
|
irq_map(irq, ahci_irq_handler);
|
|
|
|
tmp = pci_config_read_long(dev->bus, dev->slot, dev->func, 0x3C);
|
|
|
|
printf(LOG_SUCCESS "AHCI controller ready\n");
|
|
}
|