1 /*- 2 * Copyright (c) 1998 - 2008 S�ren Schmidt <sos@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification, immediately at the beginning of the file. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_ata.h" 31 #include <sys/param.h> 32 #include <sys/module.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/ata.h> 36 #include <sys/bus.h> 37 #include <sys/endian.h> 38 #include <sys/malloc.h> 39 #include <sys/lock.h> 40 #include <sys/mutex.h> 41 #include <sys/sema.h> 42 #include <sys/taskqueue.h> 43 #include <vm/uma.h> 44 #include <machine/stdarg.h> 45 #include <machine/resource.h> 46 #include <machine/bus.h> 47 #include <sys/rman.h> 48 #include <dev/pci/pcivar.h> 49 #include <dev/pci/pcireg.h> 50 #include <dev/ata/ata-all.h> 51 #include <dev/ata/ata-pci.h> 52 #include <ata_if.h> 53 54 /* local prototypes */ 55 static int ata_jmicron_chipinit(device_t dev); 56 static int ata_jmicron_ch_attach(device_t dev); 57 static int ata_jmicron_ch_detach(device_t dev); 58 static int ata_jmicron_ch_suspend(device_t dev); 59 static int ata_jmicron_ch_resume(device_t dev); 60 static void ata_jmicron_reset(device_t dev); 61 static void ata_jmicron_setmode(device_t dev, int mode); 62 63 /* 64 * JMicron chipset support functions 65 */ 66 static int 67 ata_jmicron_probe(device_t dev) 68 { 69 struct ata_pci_controller *ctlr = device_get_softc(dev); 70 struct ata_chip_id *idx; 71 static struct ata_chip_id ids[] = 72 {{ ATA_JMB360, 0, 1, 0, ATA_SA300, "JMB360" }, 73 { ATA_JMB361, 0, 1, 1, ATA_SA300, "JMB361" }, 74 { ATA_JMB363, 0, 2, 1, ATA_SA300, "JMB363" }, 75 { ATA_JMB365, 0, 1, 2, ATA_SA300, "JMB365" }, 76 { ATA_JMB366, 0, 2, 2, ATA_SA300, "JMB366" }, 77 { ATA_JMB368, 0, 0, 1, ATA_UDMA6, "JMB368" }, 78 { 0, 0, 0, 0, 0, 0}}; 79 char buffer[64]; 80 81 if (pci_get_vendor(dev) != ATA_JMICRON_ID) 82 return ENXIO; 83 84 if (!(idx = ata_match_chip(dev, ids))) 85 return ENXIO; 86 87 if ((pci_read_config(dev, 0xdf, 1) & 0x40) && 88 (pci_get_function(dev) == (pci_read_config(dev, 0x40, 1) & 0x02 >> 1))) 89 sprintf(buffer, "JMicron %s %s controller", 90 idx->text, ata_mode2str(ATA_UDMA6)); 91 else 92 sprintf(buffer, "JMicron %s %s controller", 93 idx->text, ata_mode2str(idx->max_dma)); 94 device_set_desc_copy(dev, buffer); 95 ctlr->chip = idx; 96 ctlr->chipinit = ata_jmicron_chipinit; 97 return (BUS_PROBE_DEFAULT); 98 } 99 100 static int 101 ata_jmicron_chipinit(device_t dev) 102 { 103 struct ata_pci_controller *ctlr = device_get_softc(dev); 104 int error; 105 106 if (ata_setup_interrupt(dev, ata_generic_intr)) 107 return ENXIO; 108 109 /* do we have multiple PCI functions ? */ 110 if (pci_read_config(dev, 0xdf, 1) & 0x40) { 111 /* are we on the AHCI part ? */ 112 if (ata_ahci_chipinit(dev) != ENXIO) 113 return 0; 114 115 /* otherwise we are on the PATA part */ 116 ctlr->ch_attach = ata_pci_ch_attach; 117 ctlr->ch_detach = ata_pci_ch_detach; 118 ctlr->reset = ata_generic_reset; 119 ctlr->setmode = ata_jmicron_setmode; 120 ctlr->channels = ctlr->chip->cfg2; 121 } 122 else { 123 /* set controller configuration to a combined setup we support */ 124 pci_write_config(dev, 0x40, 0x80c0a131, 4); 125 pci_write_config(dev, 0x80, 0x01200000, 4); 126 127 if (ctlr->chip->cfg1 && (error = ata_ahci_chipinit(dev))) 128 return error; 129 130 ctlr->ch_attach = ata_jmicron_ch_attach; 131 ctlr->ch_detach = ata_jmicron_ch_detach; 132 ctlr->ch_suspend = ata_jmicron_ch_suspend; 133 ctlr->ch_resume = ata_jmicron_ch_resume; 134 ctlr->reset = ata_jmicron_reset; 135 ctlr->setmode = ata_jmicron_setmode; 136 137 /* set the number of HW channels */ 138 ctlr->channels = ctlr->chip->cfg1 + ctlr->chip->cfg2; 139 ctlr->ichannels |= ((0xffffffffU >> (32 - ctlr->chip->cfg2)) 140 << ctlr->chip->cfg1); 141 } 142 return 0; 143 } 144 145 static int 146 ata_jmicron_ch_attach(device_t dev) 147 { 148 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 149 struct ata_channel *ch = device_get_softc(dev); 150 int error; 151 152 if (ch->unit >= ctlr->chip->cfg1) { 153 ch->unit -= ctlr->chip->cfg1; 154 error = ata_pci_ch_attach(dev); 155 ch->unit += ctlr->chip->cfg1; 156 } 157 else 158 error = ata_ahci_ch_attach(dev); 159 return error; 160 } 161 162 static int 163 ata_jmicron_ch_detach(device_t dev) 164 { 165 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 166 struct ata_channel *ch = device_get_softc(dev); 167 int error; 168 169 if (ch->unit >= ctlr->chip->cfg1) { 170 ch->unit -= ctlr->chip->cfg1; 171 error = ata_pci_ch_detach(dev); 172 ch->unit += ctlr->chip->cfg1; 173 } 174 else 175 error = ata_ahci_ch_detach(dev); 176 177 return (error); 178 } 179 180 static int 181 ata_jmicron_ch_suspend(device_t dev) 182 { 183 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 184 struct ata_channel *ch = device_get_softc(dev); 185 int error = 0; 186 187 if (ch->unit < ctlr->chip->cfg1) 188 error = ata_ahci_ch_suspend(dev); 189 return error; 190 } 191 192 static int 193 ata_jmicron_ch_resume(device_t dev) 194 { 195 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 196 struct ata_channel *ch = device_get_softc(dev); 197 int error = 0; 198 199 if (ch->unit < ctlr->chip->cfg1) 200 error = ata_ahci_ch_resume(dev); 201 return (error); 202 } 203 204 static void 205 ata_jmicron_reset(device_t dev) 206 { 207 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 208 struct ata_channel *ch = device_get_softc(dev); 209 210 if (ch->unit >= ctlr->chip->cfg1) 211 ata_generic_reset(dev); 212 else 213 ata_ahci_reset(dev); 214 } 215 216 static void 217 ata_jmicron_setmode(device_t dev, int mode) 218 { 219 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev)); 220 struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 221 222 if (pci_read_config(dev, 0xdf, 1) & 0x40 || ch->unit >= ctlr->chip->cfg1) { 223 struct ata_device *atadev = device_get_softc(dev); 224 225 /* check for 80pin cable present */ 226 if (pci_read_config(dev, 0x40, 1) & 0x08) 227 mode = ata_limit_mode(dev, mode, ATA_UDMA2); 228 else 229 mode = ata_limit_mode(dev, mode, ATA_UDMA6); 230 231 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode)) 232 atadev->mode = mode; 233 } 234 else 235 ata_sata_setmode(dev, mode); 236 } 237 238 ATA_DECLARE_DRIVER(ata_jmicron); 239 MODULE_DEPEND(ata_jmicron, ata_ahci, 1, 1, 1); 240