Path: blob/main/sys/dev/ata/chipsets/ata-acerlabs.c
39536 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 1998 - 2008 Søren Schmidt <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer,11* without modification, immediately at the beginning of the file.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR17* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES18* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.19* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,20* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT21* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,22* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY23* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT24* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF25* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.26*/2728#include <sys/param.h>29#include <sys/module.h>30#include <sys/systm.h>31#include <sys/kernel.h>32#include <sys/ata.h>33#include <sys/bus.h>34#include <sys/endian.h>35#include <sys/malloc.h>36#include <sys/lock.h>37#include <sys/mutex.h>38#include <sys/sema.h>39#include <sys/stdarg.h>40#include <sys/taskqueue.h>41#include <vm/uma.h>42#include <machine/resource.h>43#include <machine/bus.h>44#include <sys/rman.h>45#include <dev/pci/pcivar.h>46#include <dev/pci/pcireg.h>47#include <dev/ata/ata-all.h>48#include <dev/ata/ata-pci.h>49#include <ata_if.h>5051/* local prototypes */52static int ata_ali_chipinit(device_t dev);53static int ata_ali_chipdeinit(device_t dev);54static int ata_ali_ch_attach(device_t dev);55static int ata_ali_sata_ch_attach(device_t dev);56static void ata_ali_reset(device_t dev);57static int ata_ali_setmode(device_t dev, int target, int mode);5859/* misc defines */60#define ALI_OLD 0x0161#define ALI_NEW 0x0262#define ALI_SATA 0x046364struct ali_sata_resources {65struct resource *bars[4];66};6768/*69* Acer Labs Inc (ALI) chipset support functions70*/71static int72ata_ali_probe(device_t dev)73{74struct ata_pci_controller *ctlr = device_get_softc(dev);75static const struct ata_chip_id ids[] =76{{ ATA_ALI_5289, 0x00, 2, ALI_SATA, ATA_SA150, "M5289" },77{ ATA_ALI_5288, 0x00, 4, ALI_SATA, ATA_SA300, "M5288" },78{ ATA_ALI_5287, 0x00, 4, ALI_SATA, ATA_SA150, "M5287" },79{ ATA_ALI_5281, 0x00, 2, ALI_SATA, ATA_SA150, "M5281" },80{ ATA_ALI_5228, 0xc5, 0, ALI_NEW, ATA_UDMA6, "M5228" },81{ ATA_ALI_5229, 0xc5, 0, ALI_NEW, ATA_UDMA6, "M5229" },82{ ATA_ALI_5229, 0xc4, 0, ALI_NEW, ATA_UDMA5, "M5229" },83{ ATA_ALI_5229, 0xc2, 0, ALI_NEW, ATA_UDMA4, "M5229" },84{ ATA_ALI_5229, 0x20, 0, ALI_OLD, ATA_UDMA2, "M5229" },85{ ATA_ALI_5229, 0x00, 0, ALI_OLD, ATA_WDMA2, "M5229" },86{ 0, 0, 0, 0, 0, 0}};8788if (pci_get_vendor(dev) != ATA_ACER_LABS_ID)89return ENXIO;9091if (!(ctlr->chip = ata_match_chip(dev, ids)))92return ENXIO;9394ata_set_desc(dev);95ctlr->chipinit = ata_ali_chipinit;96ctlr->chipdeinit = ata_ali_chipdeinit;97return (BUS_PROBE_LOW_PRIORITY);98}99100static int101ata_ali_chipinit(device_t dev)102{103struct ata_pci_controller *ctlr = device_get_softc(dev);104struct ali_sata_resources *res;105int i, rid;106107if (ata_setup_interrupt(dev, ata_generic_intr))108return ENXIO;109110switch (ctlr->chip->cfg2) {111case ALI_SATA:112ctlr->channels = ctlr->chip->cfg1;113ctlr->ch_attach = ata_ali_sata_ch_attach;114ctlr->ch_detach = ata_pci_ch_detach;115ctlr->setmode = ata_sata_setmode;116ctlr->getrev = ata_sata_getrev;117118/* Allocate resources for later use by channel attach routines. */119res = malloc(sizeof(struct ali_sata_resources), M_ATAPCI, M_WAITOK);120for (i = 0; i < 4; i++) {121rid = PCIR_BAR(i);122res->bars[i] = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,123RF_ACTIVE);124if (res->bars[i] == NULL) {125device_printf(dev, "Failed to allocate BAR %d\n", i);126for (i--; i >=0; i--)127bus_release_resource(dev, SYS_RES_IOPORT,128PCIR_BAR(i), res->bars[i]);129free(res, M_ATAPCI);130return ENXIO;131}132}133ctlr->chipset_data = res;134break;135136case ALI_NEW:137/* use device interrupt as byte count end */138pci_write_config(dev, 0x4a, pci_read_config(dev, 0x4a, 1) | 0x20, 1);139140/* enable cable detection and UDMA support on revisions < 0xc7 */141if (ctlr->chip->chiprev < 0xc7)142pci_write_config(dev, 0x4b, pci_read_config(dev, 0x4b, 1) |1430x09, 1);144145/* enable ATAPI UDMA mode (even if we are going to do PIO) */146pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) |147(ctlr->chip->chiprev >= 0xc7 ? 0x03 : 0x01), 1);148149/* only chips with revision > 0xc4 can do 48bit DMA */150if (ctlr->chip->chiprev <= 0xc4)151device_printf(dev,152"using PIO transfers above 137GB as workaround for "153"48bit DMA access bug, expect reduced performance\n");154ctlr->ch_attach = ata_ali_ch_attach;155ctlr->ch_detach = ata_pci_ch_detach;156ctlr->reset = ata_ali_reset;157ctlr->setmode = ata_ali_setmode;158break;159160case ALI_OLD:161/* deactivate the ATAPI FIFO and enable ATAPI UDMA */162pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x03, 1);163ctlr->setmode = ata_ali_setmode;164break;165}166return 0;167}168169static int170ata_ali_chipdeinit(device_t dev)171{172struct ata_pci_controller *ctlr = device_get_softc(dev);173struct ali_sata_resources *res;174int i;175176if (ctlr->chip->cfg2 == ALI_SATA) {177res = ctlr->chipset_data;178for (i = 0; i < 4; i++) {179if (res->bars[i] != NULL) {180bus_release_resource(dev, SYS_RES_IOPORT,181PCIR_BAR(i), res->bars[i]);182}183}184free(res, M_ATAPCI);185ctlr->chipset_data = NULL;186}187return (0);188}189190static int191ata_ali_ch_attach(device_t dev)192{193struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));194struct ata_channel *ch = device_get_softc(dev);195196/* setup the usual register normal pci style */197if (ata_pci_ch_attach(dev))198return ENXIO;199200if (ctlr->chip->cfg2 & ALI_NEW && ctlr->chip->chiprev < 0xc7)201ch->flags |= ATA_CHECKS_CABLE;202/* older chips can't do 48bit DMA transfers */203if (ctlr->chip->chiprev <= 0xc4) {204ch->flags |= ATA_NO_48BIT_DMA;205if (ch->dma.max_iosize > 256 * 512)206ch->dma.max_iosize = 256 * 512;207}208if (ctlr->chip->cfg2 & ALI_NEW)209ch->flags |= ATA_NO_ATAPI_DMA;210211return 0;212}213214static int215ata_ali_sata_ch_attach(device_t dev)216{217device_t parent = device_get_parent(dev);218struct ata_pci_controller *ctlr = device_get_softc(parent);219struct ata_channel *ch = device_get_softc(dev);220struct ali_sata_resources *res;221struct resource *io = NULL, *ctlio = NULL;222int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);223int i;224225res = ctlr->chipset_data;226if (unit01) {227io = res->bars[2];228ctlio = res->bars[3];229} else {230io = res->bars[0];231ctlio = res->bars[1];232}233ata_pci_dmainit(dev);234for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {235ch->r_io[i].res = io;236ch->r_io[i].offset = i + (unit10 ? 8 : 0);237}238ch->r_io[ATA_CONTROL].res = ctlio;239ch->r_io[ATA_CONTROL].offset = 2 + (unit10 ? 4 : 0);240ch->r_io[ATA_IDX_ADDR].res = io;241ata_default_registers(dev);242if (ctlr->r_res1) {243for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {244ch->r_io[i].res = ctlr->r_res1;245ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);246}247}248ch->flags |= ATA_NO_SLAVE;249ch->flags |= ATA_SATA;250251/* XXX SOS PHY handling awkward in ALI chip not supported yet */252ata_pci_hw(dev);253return 0;254}255256static void257ata_ali_reset(device_t dev)258{259struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));260struct ata_channel *ch = device_get_softc(dev);261device_t *children;262int nchildren, i;263264ata_generic_reset(dev);265266/*267* workaround for datacorruption bug found on at least SUN Blade-100268* find the ISA function on the southbridge and disable then enable269* the ATA channel tristate buffer270*/271if (ctlr->chip->chiprev == 0xc3 || ctlr->chip->chiprev == 0xc2) {272if (!device_get_children(GRANDPARENT(dev), &children, &nchildren)) {273for (i = 0; i < nchildren; i++) {274if (pci_get_devid(children[i]) == ATA_ALI_1533) {275pci_write_config(children[i], 0x58,276pci_read_config(children[i], 0x58, 1) &277~(0x04 << ch->unit), 1);278pci_write_config(children[i], 0x58,279pci_read_config(children[i], 0x58, 1) |280(0x04 << ch->unit), 1);281break;282}283}284free(children, M_TEMP);285}286}287}288289static int290ata_ali_setmode(device_t dev, int target, int mode)291{292device_t parent = device_get_parent(dev);293struct ata_pci_controller *ctlr = device_get_softc(parent);294struct ata_channel *ch = device_get_softc(dev);295int devno = (ch->unit << 1) + target;296int piomode;297static const uint32_t piotimings[] =298{ 0x006d0003, 0x00580002, 0x00440001, 0x00330001,2990x00310001, 0x006d0003, 0x00330001, 0x00310001 };300static const uint8_t udma[] = {0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x0f,3010x0d};302uint32_t word54;303304mode = min(mode, ctlr->chip->max_dma);305306if (ctlr->chip->cfg2 & ALI_NEW && ctlr->chip->chiprev < 0xc7) {307if (ata_dma_check_80pin && mode > ATA_UDMA2 &&308pci_read_config(parent, 0x4a, 1) & (1 << ch->unit)) {309ata_print_cable(dev, "controller");310mode = ATA_UDMA2;311}312}313if (ctlr->chip->cfg2 & ALI_OLD) {314/* doesn't support ATAPI DMA on write */315ch->flags |= ATA_ATAPI_DMA_RO;316if (ch->devices & ATA_ATAPI_MASTER &&317ch->devices & ATA_ATAPI_SLAVE) {318/* doesn't support ATAPI DMA on two ATAPI devices */319device_printf(dev, "two atapi devices on this channel,"320" no DMA\n");321mode = min(mode, ATA_PIO_MAX);322}323}324/* Set UDMA mode */325word54 = pci_read_config(parent, 0x54, 4);326if (mode >= ATA_UDMA0) {327word54 &= ~(0x000f000f << (devno << 2));328word54 |= (((udma[mode&ATA_MODE_MASK]<<16)|0x05)<<(devno<<2));329piomode = ATA_PIO4;330}331else {332word54 &= ~(0x0008000f << (devno << 2));333piomode = mode;334}335pci_write_config(parent, 0x54, word54, 4);336/* Set PIO/WDMA mode */337pci_write_config(parent, 0x58 + (ch->unit << 2),338piotimings[ata_mode2idx(piomode)], 4);339return (mode);340}341342ATA_DECLARE_DRIVER(ata_ali);343344345