113014ca0SSøren Schmidt /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
49a14aa01SUlrich Spörlein * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
513014ca0SSøren Schmidt * All rights reserved.
613014ca0SSøren Schmidt *
713014ca0SSøren Schmidt * Redistribution and use in source and binary forms, with or without
813014ca0SSøren Schmidt * modification, are permitted provided that the following conditions
913014ca0SSøren Schmidt * are met:
1013014ca0SSøren Schmidt * 1. Redistributions of source code must retain the above copyright
1113014ca0SSøren Schmidt * notice, this list of conditions and the following disclaimer,
1213014ca0SSøren Schmidt * without modification, immediately at the beginning of the file.
1313014ca0SSøren Schmidt * 2. Redistributions in binary form must reproduce the above copyright
1413014ca0SSøren Schmidt * notice, this list of conditions and the following disclaimer in the
1513014ca0SSøren Schmidt * documentation and/or other materials provided with the distribution.
1613014ca0SSøren Schmidt *
1713014ca0SSøren Schmidt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1813014ca0SSøren Schmidt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1913014ca0SSøren Schmidt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2013014ca0SSøren Schmidt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2113014ca0SSøren Schmidt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2213014ca0SSøren Schmidt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2313014ca0SSøren Schmidt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2413014ca0SSøren Schmidt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2513014ca0SSøren Schmidt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2613014ca0SSøren Schmidt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2713014ca0SSøren Schmidt */
2813014ca0SSøren Schmidt
2913014ca0SSøren Schmidt #include <sys/param.h>
3013014ca0SSøren Schmidt #include <sys/module.h>
3113014ca0SSøren Schmidt #include <sys/systm.h>
3213014ca0SSøren Schmidt #include <sys/kernel.h>
3313014ca0SSøren Schmidt #include <sys/ata.h>
3413014ca0SSøren Schmidt #include <sys/bus.h>
3513014ca0SSøren Schmidt #include <sys/endian.h>
3613014ca0SSøren Schmidt #include <sys/malloc.h>
3713014ca0SSøren Schmidt #include <sys/lock.h>
3813014ca0SSøren Schmidt #include <sys/mutex.h>
3913014ca0SSøren Schmidt #include <sys/sema.h>
4013014ca0SSøren Schmidt #include <sys/taskqueue.h>
4113014ca0SSøren Schmidt #include <vm/uma.h>
4213014ca0SSøren Schmidt #include <machine/stdarg.h>
4313014ca0SSøren Schmidt #include <machine/resource.h>
4413014ca0SSøren Schmidt #include <machine/bus.h>
4513014ca0SSøren Schmidt #include <sys/rman.h>
4613014ca0SSøren Schmidt #include <dev/pci/pcivar.h>
4713014ca0SSøren Schmidt #include <dev/pci/pcireg.h>
4813014ca0SSøren Schmidt #include <dev/ata/ata-all.h>
4913014ca0SSøren Schmidt #include <dev/ata/ata-pci.h>
5013014ca0SSøren Schmidt #include <ata_if.h>
5113014ca0SSøren Schmidt
5213014ca0SSøren Schmidt /* local prototypes */
533f809d7aSAlexander Motin static int ata_marvell_chipinit(device_t dev);
543f809d7aSAlexander Motin static int ata_marvell_ch_attach(device_t dev);
55066f913aSAlexander Motin static int ata_marvell_setmode(device_t dev, int target, int mode);
566268666cSAlexander Motin static int ata_marvell_dummy_chipinit(device_t dev);
5713014ca0SSøren Schmidt
5813014ca0SSøren Schmidt /* misc defines */
5913014ca0SSøren Schmidt #define MV_61XX 61
606268666cSAlexander Motin #define MV_91XX 91
6113014ca0SSøren Schmidt
6213014ca0SSøren Schmidt /*
6313014ca0SSøren Schmidt * Marvell chipset support functions
6413014ca0SSøren Schmidt */
6513014ca0SSøren Schmidt #define ATA_MV_HOST_BASE(ch) \
6613014ca0SSøren Schmidt ((ch->unit & 3) * 0x0100) + (ch->unit > 3 ? 0x30000 : 0x20000)
6713014ca0SSøren Schmidt #define ATA_MV_EDMA_BASE(ch) \
6813014ca0SSøren Schmidt ((ch->unit & 3) * 0x2000) + (ch->unit > 3 ? 0x30000 : 0x20000)
6913014ca0SSøren Schmidt
7013014ca0SSøren Schmidt struct ata_marvell_response {
7113014ca0SSøren Schmidt u_int16_t tag;
7213014ca0SSøren Schmidt u_int8_t edma_status;
7313014ca0SSøren Schmidt u_int8_t dev_status;
7413014ca0SSøren Schmidt u_int32_t timestamp;
7513014ca0SSøren Schmidt };
7613014ca0SSøren Schmidt
7713014ca0SSøren Schmidt struct ata_marvell_dma_prdentry {
7813014ca0SSøren Schmidt u_int32_t addrlo;
7913014ca0SSøren Schmidt u_int32_t count;
8013014ca0SSøren Schmidt u_int32_t addrhi;
8113014ca0SSøren Schmidt u_int32_t reserved;
8213014ca0SSøren Schmidt };
8313014ca0SSøren Schmidt
8413014ca0SSøren Schmidt static int
ata_marvell_probe(device_t dev)8513014ca0SSøren Schmidt ata_marvell_probe(device_t dev)
8613014ca0SSøren Schmidt {
8713014ca0SSøren Schmidt struct ata_pci_controller *ctlr = device_get_softc(dev);
8829658c96SDimitry Andric static const struct ata_chip_id ids[] =
89cdc58367SAlexander Motin {{ ATA_M88SE6101, 0, 0, MV_61XX, ATA_UDMA6, "88SE6101" },
9093644902SAlexander Motin { ATA_M88SE6102, 0, 0, MV_61XX, ATA_UDMA6, "88SE6102" },
9193644902SAlexander Motin { ATA_M88SE6111, 0, 1, MV_61XX, ATA_UDMA6, "88SE6111" },
9293644902SAlexander Motin { ATA_M88SE6121, 0, 2, MV_61XX, ATA_UDMA6, "88SE6121" },
9393644902SAlexander Motin { ATA_M88SE6141, 0, 4, MV_61XX, ATA_UDMA6, "88SE6141" },
9493644902SAlexander Motin { ATA_M88SE6145, 0, 4, MV_61XX, ATA_UDMA6, "88SE6145" },
956268666cSAlexander Motin { 0x91a41b4b, 0, 0, MV_91XX, ATA_UDMA6, "88SE912x" },
9613014ca0SSøren Schmidt { 0, 0, 0, 0, 0, 0}};
9713014ca0SSøren Schmidt
986268666cSAlexander Motin if (pci_get_vendor(dev) != ATA_MARVELL_ID &&
996268666cSAlexander Motin pci_get_vendor(dev) != ATA_MARVELL2_ID)
10013014ca0SSøren Schmidt return ENXIO;
10113014ca0SSøren Schmidt
10213014ca0SSøren Schmidt if (!(ctlr->chip = ata_match_chip(dev, ids)))
10313014ca0SSøren Schmidt return ENXIO;
10413014ca0SSøren Schmidt
10513014ca0SSøren Schmidt ata_set_desc(dev);
10613014ca0SSøren Schmidt
10713014ca0SSøren Schmidt switch (ctlr->chip->cfg2) {
10813014ca0SSøren Schmidt case MV_61XX:
1093f809d7aSAlexander Motin ctlr->chipinit = ata_marvell_chipinit;
11013014ca0SSøren Schmidt break;
1116268666cSAlexander Motin case MV_91XX:
1126268666cSAlexander Motin ctlr->chipinit = ata_marvell_dummy_chipinit;
1136268666cSAlexander Motin break;
11413014ca0SSøren Schmidt }
1153036de3cSAlexander Motin return (BUS_PROBE_LOW_PRIORITY);
11613014ca0SSøren Schmidt }
11713014ca0SSøren Schmidt
11813014ca0SSøren Schmidt static int
ata_marvell_chipinit(device_t dev)1193f809d7aSAlexander Motin ata_marvell_chipinit(device_t dev)
12013014ca0SSøren Schmidt {
12113014ca0SSøren Schmidt struct ata_pci_controller *ctlr = device_get_softc(dev);
1226bd8779bSAlexander Motin device_t child;
12313014ca0SSøren Schmidt
12413014ca0SSøren Schmidt if (ata_setup_interrupt(dev, ata_generic_intr))
12513014ca0SSøren Schmidt return ENXIO;
1266bd8779bSAlexander Motin /* Create AHCI subdevice if AHCI part present. */
1276bd8779bSAlexander Motin if (ctlr->chip->cfg1) {
1285b56413dSWarner Losh child = device_add_child(dev, NULL, DEVICE_UNIT_ANY);
1296bd8779bSAlexander Motin if (child != NULL) {
1306bd8779bSAlexander Motin device_set_ivars(child, (void *)(intptr_t)-1);
131*18250ec6SJohn Baldwin bus_attach_children(dev);
1326bd8779bSAlexander Motin }
1336bd8779bSAlexander Motin }
1343f809d7aSAlexander Motin ctlr->ch_attach = ata_marvell_ch_attach;
1356bd8779bSAlexander Motin ctlr->ch_detach = ata_pci_ch_detach;
1366bd8779bSAlexander Motin ctlr->reset = ata_generic_reset;
1373f809d7aSAlexander Motin ctlr->setmode = ata_marvell_setmode;
1386bd8779bSAlexander Motin ctlr->channels = 1;
1393f809d7aSAlexander Motin return (0);
14013014ca0SSøren Schmidt }
14113014ca0SSøren Schmidt
14213014ca0SSøren Schmidt static int
ata_marvell_ch_attach(device_t dev)1433f809d7aSAlexander Motin ata_marvell_ch_attach(device_t dev)
14413014ca0SSøren Schmidt {
14513014ca0SSøren Schmidt struct ata_channel *ch = device_get_softc(dev);
1463f809d7aSAlexander Motin int error;
14713014ca0SSøren Schmidt
1483f809d7aSAlexander Motin error = ata_pci_ch_attach(dev);
14913014ca0SSøren Schmidt /* dont use 32 bit PIO transfers */
15013014ca0SSøren Schmidt ch->flags |= ATA_USE_16BIT;
151066f913aSAlexander Motin ch->flags |= ATA_CHECKS_CABLE;
1523f809d7aSAlexander Motin return (error);
1533f809d7aSAlexander Motin }
15413014ca0SSøren Schmidt
155066f913aSAlexander Motin static int
ata_marvell_setmode(device_t dev,int target,int mode)156066f913aSAlexander Motin ata_marvell_setmode(device_t dev, int target, int mode)
15713014ca0SSøren Schmidt {
158066f913aSAlexander Motin struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
159066f913aSAlexander Motin struct ata_channel *ch = device_get_softc(dev);
16013014ca0SSøren Schmidt
161066f913aSAlexander Motin mode = min(mode, ctlr->chip->max_dma);
162066f913aSAlexander Motin /* Check for 80pin cable present. */
1639a9bce34SAlexander Motin if (ata_dma_check_80pin && mode > ATA_UDMA2 &&
1649a9bce34SAlexander Motin ATA_IDX_INB(ch, ATA_BMDEVSPEC_0) & 0x01) {
165066f913aSAlexander Motin ata_print_cable(dev, "controller");
166066f913aSAlexander Motin mode = ATA_UDMA2;
167066f913aSAlexander Motin }
168066f913aSAlexander Motin /* Nothing to do to setup mode, the controller snoop SET_FEATURE cmd. */
169066f913aSAlexander Motin return (mode);
17013014ca0SSøren Schmidt }
17113014ca0SSøren Schmidt
1726268666cSAlexander Motin static int
ata_marvell_dummy_chipinit(device_t dev)1736268666cSAlexander Motin ata_marvell_dummy_chipinit(device_t dev)
1746268666cSAlexander Motin {
1756268666cSAlexander Motin struct ata_pci_controller *ctlr = device_get_softc(dev);
1766268666cSAlexander Motin
1776268666cSAlexander Motin ctlr->channels = 0;
1786268666cSAlexander Motin return (0);
1796268666cSAlexander Motin }
1806268666cSAlexander Motin
18113014ca0SSøren Schmidt ATA_DECLARE_DRIVER(ata_marvell);
182