Skip to content

Commit

Permalink
pc-ata: set IO port addresses based on pci BARs
Browse files Browse the repository at this point in the history
Some hardware (tested on ThinkPad E540) use uncommon ATA IO port
addresses instead of typical 0x1f0, 0x3f6, 0x1e8, 0x3ee. In such case,
these addresses are often located in pci BAR0-BAR3 of the storage
device. This change utilizes BARs via platformctl/pctl_get if nothing is
found on common addresses

JIRA: RTOS-922
  • Loading branch information
adamgreloch committed Oct 1, 2024
1 parent 10cca0a commit af82877
Showing 1 changed file with 79 additions and 4 deletions.
83 changes: 79 additions & 4 deletions storage/pc-ata/ata.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#include <sys/list.h>
#include <sys/threads.h>

#include <sys/platform.h>
#include <phoenix/arch/ia32/ia32.h>

#include "ata.h"

#define ATA_SECTORSZ_MAX 4096 /* Max supported sector size */
Expand All @@ -33,6 +36,12 @@ ata_common_t ata_common;
static ata_bus_t *buses;


static int pci_dev_classes[2] = {
0x0101, /* IDE interface */
0x0105, /* ATA controller */
};


static uint32_t ata_readreg(void *base, uint8_t reg, uint8_t size)
{
uintptr_t addr = (uintptr_t)base;
Expand Down Expand Up @@ -610,11 +619,16 @@ static int ata_initbus(void *base, void *ctrl, ata_bus_t *bus)

int ata_init(void)
{
int err;
ata_bus_t *bus1, *bus2;
int bus1_found, bus2_found;
addr_t ata1_base, ata1_ctrl, ata2_base, ata2_ctrl;

ata_common.ndevs = 0;
ata_common.devs = NULL;
buses = NULL;
bus1_found = 0;
bus2_found = 0;

if ((bus1 = (ata_bus_t *)malloc(sizeof(ata_bus_t))) == NULL)
return -ENOMEM;
Expand All @@ -624,11 +638,72 @@ int ata_init(void)
return -ENOMEM;
}

if (ata_initbus((void *)(ATA1_BASE | 0x1), (void *)(ATA1_CTRL | 0x1), bus1) < 0)
free(bus1);
/* try most common adresses first */
ata1_base = ATA1_BASE;
ata1_ctrl = ATA1_CTRL;
ata2_base = ATA2_BASE;
ata2_ctrl = ATA2_CTRL;

if (ata_initbus((void *)(ata1_base | 0x1), (void *)(ata1_ctrl | 0x1), bus1) == EOK) {
bus1_found = 1;
}

if (ata_initbus((void *)(ATA2_BASE | 0x1), (void *)(ATA2_CTRL | 0x1), bus2) < 0)
free(bus2);
if (ata_initbus((void *)(ata2_base | 0x1), (void *)(ata2_ctrl | 0x1), bus2) == EOK) {
bus2_found = 1;
}

if (buses == NULL) {
/* no buses found on common addresses. look through pci config BARs */
platformctl_t pctl = { .action = pctl_get, .type = pctl_pci };

for (uint8_t i = 0; i < sizeof(pci_dev_classes); i++) {
pctl.pci.id.vendor = PCI_ANY;
pctl.pci.id.device = PCI_ANY;
pctl.pci.id.subvendor = PCI_ANY;
pctl.pci.id.subdevice = PCI_ANY;
pctl.pci.dev.bus = 0;
pctl.pci.dev.dev = 0;
pctl.pci.dev.func = 0;
pctl.pci.caps = NULL;

pctl.pci.id.cl = pci_dev_classes[i];
err = platformctl(&pctl);
if (err < 0) {
/* try next device class */
continue;
}

ata1_base = pctl.pci.dev.resources[0].base;
ata1_ctrl = pctl.pci.dev.resources[1].base;
ata2_base = pctl.pci.dev.resources[2].base;
ata2_ctrl = pctl.pci.dev.resources[3].base;

if (ata_initbus((void *)(ata1_base | 0x1), (void *)(ata1_ctrl | 0x1), bus1) == EOK) {
bus1_found = 1;
}

if (ata_initbus((void *)(ata2_base | 0x1), (void *)(ata2_ctrl | 0x1), bus2) == EOK) {
bus2_found = 1;
}

if (buses != NULL) {
/* at least one bus found via pci config */
break;
}
}

if (bus1_found == 0) {
free(bus1);
}

if (bus2_found == 0) {
free(bus2);
}

if (buses == NULL) {
return -ENODEV;
}
}

return EOK;
}

0 comments on commit af82877

Please sign in to comment.