static int ata_ali_chipinit(device_t dev);
static int ata_ali_allocate(device_t dev);
static int ata_ali_sata_allocate(device_t dev);
static void ata_ali_reset(device_t dev);
static void ata_ali_setmode(device_t dev, int mode);
#define ALI_OLD 0x01
#define ALI_NEW 0x02
#define ALI_SATA 0x04
int
ata_ali_ident(device_t dev)
{
struct ata_pci_controller *ctlr = device_get_softc(dev);
static const struct ata_chip_id ids[] =
{{ ATA_ALI_5289, 0x00, 2, ALI_SATA, ATA_SA150, "M5289" },
{ ATA_ALI_5288, 0x00, 4, ALI_SATA, ATA_SA300, "M5288" },
{ ATA_ALI_5287, 0x00, 4, ALI_SATA, ATA_SA150, "M5287" },
{ ATA_ALI_5281, 0x00, 2, ALI_SATA, ATA_SA150, "M5281" },
{ ATA_ALI_5229, 0xc5, 0, ALI_NEW, ATA_UDMA6, "M5229" },
{ ATA_ALI_5229, 0xc4, 0, ALI_NEW, ATA_UDMA5, "M5229" },
{ ATA_ALI_5229, 0xc2, 0, ALI_NEW, ATA_UDMA4, "M5229" },
{ ATA_ALI_5229, 0x20, 0, ALI_OLD, ATA_UDMA2, "M5229" },
{ ATA_ALI_5229, 0x00, 0, ALI_OLD, ATA_WDMA2, "M5229" },
{ 0, 0, 0, 0, 0, 0}};
if (pci_get_vendor(dev) != ATA_ACER_LABS_ID)
return ENXIO;
if (!(ctlr->chip = ata_match_chip(dev, ids)))
return ENXIO;
ata_set_desc(dev);
ctlr->chipinit = ata_ali_chipinit;
return 0;
}
static int
ata_ali_chipinit(device_t dev)
{
struct ata_pci_controller *ctlr = device_get_softc(dev);
if (ata_setup_interrupt(dev, ata_generic_intr))
return ENXIO;
switch (ctlr->chip->cfg2) {
case ALI_SATA:
ctlr->channels = ctlr->chip->cfg1;
ctlr->allocate = ata_ali_sata_allocate;
ctlr->setmode = ata_sata_setmode;
if ((ctlr->chip->chipid == ATA_ALI_5288) &&
(ata_ahci_chipinit(dev) != ENXIO))
return 0;
pci_write_config(dev, PCIR_COMMAND,
pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
break;
case ALI_NEW:
pci_write_config(dev, 0x4a, pci_read_config(dev, 0x4a, 1) | 0x20, 1);
pci_write_config(dev, 0x4b, pci_read_config(dev, 0x4b, 1) | 0x09, 1);
pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x01, 1);
if (ctlr->chip->chiprev <= 0xc4)
device_printf(dev,
"using PIO transfers above 137GB as workaround for "
"48bit DMA access bug, expect reduced performance\n");
ctlr->allocate = ata_ali_allocate;
ctlr->reset = ata_ali_reset;
ctlr->setmode = ata_ali_setmode;
break;
case ALI_OLD:
pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x03, 1);
ctlr->setmode = ata_ali_setmode;
break;
}
return 0;
}
static int
ata_ali_allocate(device_t dev)
{
struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
struct ata_channel *ch = device_get_softc(dev);
if (ata_pci_allocate(dev))
return ENXIO;
if (ctlr->chip->chiprev <= 0xc4) {
ch->flags |= ATA_NO_48BIT_DMA;
if (ch->dma->max_iosize > 256 * 512)
ch->dma->max_iosize = 256 * 512;
}
return 0;
}
static int
ata_ali_sata_allocate(device_t dev)
{
device_t parent = device_get_parent(dev);
struct ata_pci_controller *ctlr = device_get_softc(parent);
struct ata_channel *ch = device_get_softc(dev);
struct resource *io = NULL, *ctlio = NULL;
int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
int i, rid;
rid = PCIR_BAR(0) + (unit01 ? 8 : 0);
io = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
if (!io)
return ENXIO;
rid = PCIR_BAR(1) + (unit01 ? 8 : 0);
ctlio = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
if (!ctlio) {
bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
return ENXIO;
}
for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
ch->r_io[i].res = io;
ch->r_io[i].offset = i + (unit10 ? 8 : 0);
}
ch->r_io[ATA_CONTROL].res = ctlio;
ch->r_io[ATA_CONTROL].offset = 2 + (unit10 ? 4 : 0);
ch->r_io[ATA_IDX_ADDR].res = io;
ata_default_registers(dev);
if (ctlr->r_res1) {
for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
ch->r_io[i].res = ctlr->r_res1;
ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
}
}
ch->flags |= ATA_NO_SLAVE;
ata_pci_hw(dev);
return 0;
}
static void
ata_ali_reset(device_t dev)
{
struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
struct ata_channel *ch = device_get_softc(dev);
device_t *children;
int nchildren, i;
ata_generic_reset(dev);
if (ctlr->chip->chiprev == 0xc3 || ctlr->chip->chiprev == 0xc2) {
if (!device_get_children(GRANDPARENT(dev), &children, &nchildren)) {
for (i = 0; i < nchildren; i++) {
if (pci_get_devid(children[i]) == ATA_ALI_1533) {
pci_write_config(children[i], 0x58,
pci_read_config(children[i], 0x58, 1) &
~(0x04 << ch->unit), 1);
pci_write_config(children[i], 0x58,
pci_read_config(children[i], 0x58, 1) |
(0x04 << ch->unit), 1);
break;
}
}
kfree(children, M_TEMP);
}
}
}
static void
ata_ali_setmode(device_t dev, int mode)
{
device_t gparent = GRANDPARENT(dev);
struct ata_pci_controller *ctlr = device_get_softc(gparent);
struct ata_channel *ch = device_get_softc(device_get_parent(dev));
struct ata_device *atadev = device_get_softc(dev);
int devno = (ch->unit << 1) + atadev->unit;
int error;
static const uint32_t piotimings[] =
{ 0x006d0003, 0x00580002, 0x00440001, 0x00330001,
0x00310001, 0x00440001, 0x00330001, 0x00310001};
static const uint8_t udma[] =
{0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0d};
uint32_t word54;
mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
if (ctlr->chip->cfg2 & ALI_NEW) {
if (mode > ATA_UDMA2 &&
pci_read_config(gparent, 0x4a, 1) & (1 << ch->unit)) {
ata_print_cable(dev, "controller");
mode = ATA_UDMA2;
}
}
else
mode = ata_check_80pin(dev, mode);
if (ctlr->chip->cfg2 & ALI_OLD) {
ch->flags |= ATA_ATAPI_DMA_RO;
if (ch->devices & ATA_ATAPI_MASTER && ch->devices & ATA_ATAPI_SLAVE) {
device_printf(dev, "two atapi devices on this channel, no DMA\n");
mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
}
}
error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
if (bootverbose)
device_printf(dev, "%ssetting %s on %s chip\n",
(error) ? "FAILURE " : "",
ata_mode2str(mode), ctlr->chip->text);
if (!error) {
if (mode >= ATA_UDMA0) {
word54 = pci_read_config(gparent, 0x54, 4);
word54 &= ~(0x000f000f << (devno << 2));
word54 |= (((udma[mode&ATA_MODE_MASK]<<16)|0x05)<<(devno<<2));
pci_write_config(gparent, 0x54, word54, 4);
pci_write_config(gparent, 0x58 + (ch->unit << 2),
0x00310001, 4);
}
else {
pci_write_config(gparent, 0x54, pci_read_config(gparent, 0x54, 4) &
~(0x0008000f << (devno << 2)), 4);
pci_write_config(gparent, 0x58 + (ch->unit << 2),
piotimings[ata_mode2idx(mode)], 4);
}
atadev->mode = mode;
}
}