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 */
5313014ca0SSøren Schmidt static int ata_jmicron_chipinit(device_t dev);
54066f913aSAlexander Motin static int ata_jmicron_ch_attach(device_t dev);
55066f913aSAlexander Motin static int ata_jmicron_setmode(device_t dev, int target, int mode);
5613014ca0SSøren Schmidt
5713014ca0SSøren Schmidt /*
5813014ca0SSøren Schmidt * JMicron chipset support functions
5913014ca0SSøren Schmidt */
6013014ca0SSøren Schmidt static int
ata_jmicron_probe(device_t dev)6113014ca0SSøren Schmidt ata_jmicron_probe(device_t dev)
6213014ca0SSøren Schmidt {
6313014ca0SSøren Schmidt struct ata_pci_controller *ctlr = device_get_softc(dev);
645187458fSMarius Strobl const struct ata_chip_id *idx;
6529658c96SDimitry Andric static const struct ata_chip_id ids[] =
6613014ca0SSøren Schmidt {{ ATA_JMB360, 0, 1, 0, ATA_SA300, "JMB360" },
676bd8779bSAlexander Motin { ATA_JMB361, 0, 1, 1, ATA_UDMA6, "JMB361" },
6849a4a59aSAlexander Motin { ATA_JMB362, 0, 2, 0, ATA_SA300, "JMB362" },
696bd8779bSAlexander Motin { ATA_JMB363, 0, 2, 1, ATA_UDMA6, "JMB363" },
706bd8779bSAlexander Motin { ATA_JMB365, 0, 1, 2, ATA_UDMA6, "JMB365" },
716bd8779bSAlexander Motin { ATA_JMB366, 0, 2, 2, ATA_UDMA6, "JMB366" },
7213014ca0SSøren Schmidt { ATA_JMB368, 0, 0, 1, ATA_UDMA6, "JMB368" },
737bac11c7SAlexander Motin { ATA_JMB368_2, 0, 0, 1, ATA_UDMA6, "JMB368" },
7413014ca0SSøren Schmidt { 0, 0, 0, 0, 0, 0}};
7513014ca0SSøren Schmidt
7613014ca0SSøren Schmidt if (pci_get_vendor(dev) != ATA_JMICRON_ID)
7713014ca0SSøren Schmidt return ENXIO;
7813014ca0SSøren Schmidt
7913014ca0SSøren Schmidt if (!(idx = ata_match_chip(dev, ids)))
8013014ca0SSøren Schmidt return ENXIO;
8113014ca0SSøren Schmidt
8225b839dfSMark Johnston device_set_descf(dev, "JMicron %s %s controller",
8313014ca0SSøren Schmidt idx->text, ata_mode2str(idx->max_dma));
8413014ca0SSøren Schmidt ctlr->chip = idx;
8513014ca0SSøren Schmidt ctlr->chipinit = ata_jmicron_chipinit;
863036de3cSAlexander Motin return (BUS_PROBE_LOW_PRIORITY);
8713014ca0SSøren Schmidt }
8813014ca0SSøren Schmidt
8913014ca0SSøren Schmidt static int
ata_jmicron_chipinit(device_t dev)9013014ca0SSøren Schmidt ata_jmicron_chipinit(device_t dev)
9113014ca0SSøren Schmidt {
9213014ca0SSøren Schmidt struct ata_pci_controller *ctlr = device_get_softc(dev);
936bd8779bSAlexander Motin device_t child;
9413014ca0SSøren Schmidt
9513014ca0SSøren Schmidt if (ata_setup_interrupt(dev, ata_generic_intr))
9613014ca0SSøren Schmidt return ENXIO;
9713014ca0SSøren Schmidt
9813014ca0SSøren Schmidt /* do we have multiple PCI functions ? */
9913014ca0SSøren Schmidt if (pci_read_config(dev, 0xdf, 1) & 0x40) {
100cdc58367SAlexander Motin /* If this was not claimed by AHCI, then we are on the PATA part */
101066f913aSAlexander Motin ctlr->ch_attach = ata_jmicron_ch_attach;
10278d15416SAlexander Motin ctlr->ch_detach = ata_pci_ch_detach;
10313014ca0SSøren Schmidt ctlr->reset = ata_generic_reset;
10413014ca0SSøren Schmidt ctlr->setmode = ata_jmicron_setmode;
10513014ca0SSøren Schmidt ctlr->channels = ctlr->chip->cfg2;
10613014ca0SSøren Schmidt }
10713014ca0SSøren Schmidt else {
10813014ca0SSøren Schmidt /* set controller configuration to a combined setup we support */
10913014ca0SSøren Schmidt pci_write_config(dev, 0x40, 0x80c0a131, 4);
11013014ca0SSøren Schmidt pci_write_config(dev, 0x80, 0x01200000, 4);
1116bd8779bSAlexander Motin /* Create AHCI subdevice if AHCI part present. */
1126bd8779bSAlexander Motin if (ctlr->chip->cfg1) {
1135b56413dSWarner Losh child = device_add_child(dev, NULL, DEVICE_UNIT_ANY);
1146bd8779bSAlexander Motin if (child != NULL) {
1156bd8779bSAlexander Motin device_set_ivars(child, (void *)(intptr_t)-1);
116*18250ec6SJohn Baldwin bus_attach_children(dev);
1176bd8779bSAlexander Motin }
1186bd8779bSAlexander Motin }
119066f913aSAlexander Motin ctlr->ch_attach = ata_jmicron_ch_attach;
1206bd8779bSAlexander Motin ctlr->ch_detach = ata_pci_ch_detach;
1216bd8779bSAlexander Motin ctlr->reset = ata_generic_reset;
12213014ca0SSøren Schmidt ctlr->setmode = ata_jmicron_setmode;
1236bd8779bSAlexander Motin ctlr->channels = ctlr->chip->cfg2;
12413014ca0SSøren Schmidt }
12513014ca0SSøren Schmidt return 0;
12613014ca0SSøren Schmidt }
12713014ca0SSøren Schmidt
128066f913aSAlexander Motin static int
ata_jmicron_ch_attach(device_t dev)129066f913aSAlexander Motin ata_jmicron_ch_attach(device_t dev)
13013014ca0SSøren Schmidt {
131066f913aSAlexander Motin struct ata_channel *ch = device_get_softc(dev);
132066f913aSAlexander Motin int error;
13313014ca0SSøren Schmidt
134066f913aSAlexander Motin error = ata_pci_ch_attach(dev);
135066f913aSAlexander Motin ch->flags |= ATA_CHECKS_CABLE;
136066f913aSAlexander Motin return (error);
137066f913aSAlexander Motin }
138066f913aSAlexander Motin
139066f913aSAlexander Motin static int
ata_jmicron_setmode(device_t dev,int target,int mode)140066f913aSAlexander Motin ata_jmicron_setmode(device_t dev, int target, int mode)
141066f913aSAlexander Motin {
142d7754273SAlexander Motin device_t parent = device_get_parent(dev);
143d7754273SAlexander Motin struct ata_pci_controller *ctlr = device_get_softc(parent);
144066f913aSAlexander Motin
145066f913aSAlexander Motin mode = min(mode, ctlr->chip->max_dma);
14613014ca0SSøren Schmidt /* check for 80pin cable present */
1479a9bce34SAlexander Motin if (ata_dma_check_80pin && mode > ATA_UDMA2 &&
148d7754273SAlexander Motin pci_read_config(parent, 0x40, 1) & 0x08) {
149066f913aSAlexander Motin ata_print_cable(dev, "controller");
150066f913aSAlexander Motin mode = ATA_UDMA2;
151066f913aSAlexander Motin }
152066f913aSAlexander Motin /* Nothing to do to setup mode, the controller snoop SET_FEATURE cmd. */
153066f913aSAlexander Motin return (mode);
15413014ca0SSøren Schmidt }
15513014ca0SSøren Schmidt
15613014ca0SSøren Schmidt ATA_DECLARE_DRIVER(ata_jmicron);
157