1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer, 12 * without modification, immediately at the beginning of the file. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/param.h> 30 #include <sys/module.h> 31 #include <sys/systm.h> 32 #include <sys/kernel.h> 33 #include <sys/ata.h> 34 #include <sys/bus.h> 35 #include <sys/endian.h> 36 #include <sys/malloc.h> 37 #include <sys/lock.h> 38 #include <sys/mutex.h> 39 #include <sys/sema.h> 40 #include <sys/taskqueue.h> 41 #include <vm/uma.h> 42 #include <machine/stdarg.h> 43 #include <machine/resource.h> 44 #include <machine/bus.h> 45 #include <sys/rman.h> 46 #include <dev/pci/pcivar.h> 47 #include <dev/pci/pcireg.h> 48 #include <dev/ata/ata-all.h> 49 #include <dev/ata/ata-pci.h> 50 #include <ata_if.h> 51 52 /* local prototypes */ 53 static int ata_jmicron_chipinit(device_t dev); 54 static int ata_jmicron_ch_attach(device_t dev); 55 static int ata_jmicron_setmode(device_t dev, int target, int mode); 56 57 /* 58 * JMicron chipset support functions 59 */ 60 static int 61 ata_jmicron_probe(device_t dev) 62 { 63 struct ata_pci_controller *ctlr = device_get_softc(dev); 64 const struct ata_chip_id *idx; 65 static const struct ata_chip_id ids[] = 66 {{ ATA_JMB360, 0, 1, 0, ATA_SA300, "JMB360" }, 67 { ATA_JMB361, 0, 1, 1, ATA_UDMA6, "JMB361" }, 68 { ATA_JMB362, 0, 2, 0, ATA_SA300, "JMB362" }, 69 { ATA_JMB363, 0, 2, 1, ATA_UDMA6, "JMB363" }, 70 { ATA_JMB365, 0, 1, 2, ATA_UDMA6, "JMB365" }, 71 { ATA_JMB366, 0, 2, 2, ATA_UDMA6, "JMB366" }, 72 { ATA_JMB368, 0, 0, 1, ATA_UDMA6, "JMB368" }, 73 { ATA_JMB368_2, 0, 0, 1, ATA_UDMA6, "JMB368" }, 74 { 0, 0, 0, 0, 0, 0}}; 75 76 if (pci_get_vendor(dev) != ATA_JMICRON_ID) 77 return ENXIO; 78 79 if (!(idx = ata_match_chip(dev, ids))) 80 return ENXIO; 81 82 device_set_descf(dev, "JMicron %s %s controller", 83 idx->text, ata_mode2str(idx->max_dma)); 84 ctlr->chip = idx; 85 ctlr->chipinit = ata_jmicron_chipinit; 86 return (BUS_PROBE_LOW_PRIORITY); 87 } 88 89 static int 90 ata_jmicron_chipinit(device_t dev) 91 { 92 struct ata_pci_controller *ctlr = device_get_softc(dev); 93 device_t child; 94 95 if (ata_setup_interrupt(dev, ata_generic_intr)) 96 return ENXIO; 97 98 /* do we have multiple PCI functions ? */ 99 if (pci_read_config(dev, 0xdf, 1) & 0x40) { 100 /* If this was not claimed by AHCI, then we are on the PATA part */ 101 ctlr->ch_attach = ata_jmicron_ch_attach; 102 ctlr->ch_detach = ata_pci_ch_detach; 103 ctlr->reset = ata_generic_reset; 104 ctlr->setmode = ata_jmicron_setmode; 105 ctlr->channels = ctlr->chip->cfg2; 106 } 107 else { 108 /* set controller configuration to a combined setup we support */ 109 pci_write_config(dev, 0x40, 0x80c0a131, 4); 110 pci_write_config(dev, 0x80, 0x01200000, 4); 111 /* Create AHCI subdevice if AHCI part present. */ 112 if (ctlr->chip->cfg1) { 113 child = device_add_child(dev, NULL, DEVICE_UNIT_ANY); 114 if (child != NULL) { 115 device_set_ivars(child, (void *)(intptr_t)-1); 116 bus_attach_children(dev); 117 } 118 } 119 ctlr->ch_attach = ata_jmicron_ch_attach; 120 ctlr->ch_detach = ata_pci_ch_detach; 121 ctlr->reset = ata_generic_reset; 122 ctlr->setmode = ata_jmicron_setmode; 123 ctlr->channels = ctlr->chip->cfg2; 124 } 125 return 0; 126 } 127 128 static int 129 ata_jmicron_ch_attach(device_t dev) 130 { 131 struct ata_channel *ch = device_get_softc(dev); 132 int error; 133 134 error = ata_pci_ch_attach(dev); 135 ch->flags |= ATA_CHECKS_CABLE; 136 return (error); 137 } 138 139 static int 140 ata_jmicron_setmode(device_t dev, int target, int mode) 141 { 142 device_t parent = device_get_parent(dev); 143 struct ata_pci_controller *ctlr = device_get_softc(parent); 144 145 mode = min(mode, ctlr->chip->max_dma); 146 /* check for 80pin cable present */ 147 if (ata_dma_check_80pin && mode > ATA_UDMA2 && 148 pci_read_config(parent, 0x40, 1) & 0x08) { 149 ata_print_cable(dev, "controller"); 150 mode = ATA_UDMA2; 151 } 152 /* Nothing to do to setup mode, the controller snoop SET_FEATURE cmd. */ 153 return (mode); 154 } 155 156 ATA_DECLARE_DRIVER(ata_jmicron); 157