Path: blob/main/sys/dev/ata/chipsets/ata-highpoint.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_highpoint_chipinit(device_t dev);53static int ata_highpoint_ch_attach(device_t dev);54static int ata_highpoint_setmode(device_t dev, int target, int mode);55static int ata_highpoint_check_80pin(device_t dev, int mode);5657/* misc defines */58#define HPT_366 059#define HPT_370 160#define HPT_372 261#define HPT_374 362#define HPT_OLD 16364/*65* HighPoint chipset support functions66*/67static int68ata_highpoint_probe(device_t dev)69{70struct ata_pci_controller *ctlr = device_get_softc(dev);71const struct ata_chip_id *idx;72static const struct ata_chip_id ids[] =73{{ ATA_HPT374, 0x07, HPT_374, 0, ATA_UDMA6, "HPT374" },74{ ATA_HPT372, 0x02, HPT_372, 0, ATA_UDMA6, "HPT372N" },75{ ATA_HPT372, 0x01, HPT_372, 0, ATA_UDMA6, "HPT372" },76{ ATA_HPT371, 0x01, HPT_372, 0, ATA_UDMA6, "HPT371" },77{ ATA_HPT366, 0x05, HPT_372, 0, ATA_UDMA6, "HPT372" },78{ ATA_HPT366, 0x03, HPT_370, 0, ATA_UDMA5, "HPT370" },79{ ATA_HPT366, 0x02, HPT_366, 0, ATA_UDMA4, "HPT368" },80{ ATA_HPT366, 0x00, HPT_366, HPT_OLD, ATA_UDMA4, "HPT366" },81{ ATA_HPT302, 0x01, HPT_372, 0, ATA_UDMA6, "HPT302" },82{ 0, 0, 0, 0, 0, 0}};83const char *channel;8485if (pci_get_vendor(dev) != ATA_HIGHPOINT_ID)86return ENXIO;8788if (!(idx = ata_match_chip(dev, ids)))89return ENXIO;9091channel = "";92if (idx->cfg1 == HPT_374) {93if (pci_get_function(dev) == 0)94channel = " (channel 0+1)";95else if (pci_get_function(dev) == 1)96channel = " (channel 2+3)";97}98device_set_descf(dev, "Highpoint %s%s %s controller",99idx->text, channel, ata_mode2str(idx->max_dma));100ctlr->chip = idx;101ctlr->chipinit = ata_highpoint_chipinit;102return (BUS_PROBE_LOW_PRIORITY);103}104105static int106ata_highpoint_chipinit(device_t dev)107{108struct ata_pci_controller *ctlr = device_get_softc(dev);109110if (ata_setup_interrupt(dev, ata_generic_intr))111return ENXIO;112113if (ctlr->chip->cfg2 == HPT_OLD) {114/* disable interrupt prediction */115pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);116}117else {118/* disable interrupt prediction */119pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);120pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);121122/* enable interrupts */123pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);124125/* set clocks etc */126if (ctlr->chip->cfg1 < HPT_372)127pci_write_config(dev, 0x5b, 0x22, 1);128else129pci_write_config(dev, 0x5b,130(pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);131}132ctlr->ch_attach = ata_highpoint_ch_attach;133ctlr->ch_detach = ata_pci_ch_detach;134ctlr->setmode = ata_highpoint_setmode;135return 0;136}137138static int139ata_highpoint_ch_attach(device_t dev)140{141struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));142struct ata_channel *ch = device_get_softc(dev);143144/* setup the usual register normal pci style */145if (ata_pci_ch_attach(dev))146return (ENXIO);147ch->flags |= ATA_ALWAYS_DMASTAT;148ch->flags |= ATA_CHECKS_CABLE;149if (ctlr->chip->cfg1 == HPT_366)150ch->flags |= ATA_NO_ATAPI_DMA;151return (0);152}153154static int155ata_highpoint_setmode(device_t dev, int target, int mode)156{157device_t parent = device_get_parent(dev);158struct ata_pci_controller *ctlr = device_get_softc(parent);159struct ata_channel *ch = device_get_softc(dev);160int devno = (ch->unit << 1) + target;161static const uint32_t timings33[][4] = {162/* HPT366 HPT370 HPT372 HPT374 mode */163{ 0x40d0a7aa, 0x06914e57, 0x0d029d5e, 0x0ac1f48a }, /* PIO 0 */164{ 0x40d0a7a3, 0x06914e43, 0x0d029d26, 0x0ac1f465 }, /* PIO 1 */165{ 0x40d0a753, 0x06514e33, 0x0c829ca6, 0x0a81f454 }, /* PIO 2 */166{ 0x40c8a742, 0x06514e22, 0x0c829c84, 0x0a81f443 }, /* PIO 3 */167{ 0x40c8a731, 0x06514e21, 0x0c829c62, 0x0a81f442 }, /* PIO 4 */168{ 0x20c8a797, 0x26514e97, 0x2c82922e, 0x228082ea }, /* MWDMA 0 */169{ 0x20c8a732, 0x26514e33, 0x2c829266, 0x22808254 }, /* MWDMA 1 */170{ 0x20c8a731, 0x26514e21, 0x2c829262, 0x22808242 }, /* MWDMA 2 */171{ 0x10c8a731, 0x16514e31, 0x1c829c62, 0x121882ea }, /* UDMA 0 */172{ 0x10cba731, 0x164d4e31, 0x1c9a9c62, 0x12148254 }, /* UDMA 1 */173{ 0x10caa731, 0x16494e31, 0x1c929c62, 0x120c8242 }, /* UDMA 2 */174{ 0x10cfa731, 0x166d4e31, 0x1c8e9c62, 0x128c8242 }, /* UDMA 3 */175{ 0x10c9a731, 0x16454e31, 0x1c8a9c62, 0x12ac8242 }, /* UDMA 4 */176{ 0, 0x16454e31, 0x1c8a9c62, 0x12848242 }, /* UDMA 5 */177{ 0, 0, 0x1c869c62, 0x12808242 } /* UDMA 6 */178};179180mode = min(mode, ctlr->chip->max_dma);181mode = ata_highpoint_check_80pin(dev, mode);182/*183* most if not all HPT chips cant really handle that the device is184* running at ATA_UDMA6/ATA133 speed, so we cheat at set the device to185* a max of ATA_UDMA5/ATA100 to guard against suboptimal performance186*/187mode = min(mode, ATA_UDMA5);188pci_write_config(parent, 0x40 + (devno << 2),189timings33[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);190return (mode);191}192193static int194ata_highpoint_check_80pin(device_t dev, int mode)195{196device_t parent = device_get_parent(dev);197struct ata_pci_controller *ctlr = device_get_softc(parent);198struct ata_channel *ch = device_get_softc(dev);199u_int8_t reg, val, res;200201if (ctlr->chip->cfg1 == HPT_374 && pci_get_function(parent) == 1) {202reg = ch->unit ? 0x57 : 0x53;203val = pci_read_config(parent, reg, 1);204pci_write_config(parent, reg, val | 0x80, 1);205}206else {207reg = 0x5b;208val = pci_read_config(parent, reg, 1);209pci_write_config(parent, reg, val & 0xfe, 1);210}211res = pci_read_config(parent, 0x5a, 1) & (ch->unit ? 0x1:0x2);212pci_write_config(parent, reg, val, 1);213214if (ata_dma_check_80pin && mode > ATA_UDMA2 && res) {215ata_print_cable(dev, "controller");216mode = ATA_UDMA2;217}218return mode;219}220221ATA_DECLARE_DRIVER(ata_highpoint);222223224