xref: /freebsd/sys/dev/ata/chipsets/ata-jmicron.c (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
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 <sys/param.h>
31 #include <sys/module.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/ata.h>
35 #include <sys/bus.h>
36 #include <sys/endian.h>
37 #include <sys/malloc.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/sema.h>
41 #include <sys/taskqueue.h>
42 #include <vm/uma.h>
43 #include <machine/stdarg.h>
44 #include <machine/resource.h>
45 #include <machine/bus.h>
46 #include <sys/rman.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/ata/ata-all.h>
50 #include <dev/ata/ata-pci.h>
51 #include <ata_if.h>
52 
53 /* local prototypes */
54 static int ata_jmicron_chipinit(device_t dev);
55 static int ata_jmicron_ch_attach(device_t dev);
56 static int ata_jmicron_setmode(device_t dev, int target, int mode);
57 
58 /*
59  * JMicron chipset support functions
60  */
61 static int
62 ata_jmicron_probe(device_t dev)
63 {
64     struct ata_pci_controller *ctlr = device_get_softc(dev);
65     const struct ata_chip_id *idx;
66     static const struct ata_chip_id ids[] =
67     {{ ATA_JMB360, 0, 1, 0, ATA_SA300, "JMB360" },
68      { ATA_JMB361, 0, 1, 1, ATA_UDMA6, "JMB361" },
69      { ATA_JMB362, 0, 2, 0, ATA_SA300, "JMB362" },
70      { ATA_JMB363, 0, 2, 1, ATA_UDMA6, "JMB363" },
71      { ATA_JMB365, 0, 1, 2, ATA_UDMA6, "JMB365" },
72      { ATA_JMB366, 0, 2, 2, ATA_UDMA6, "JMB366" },
73      { ATA_JMB368, 0, 0, 1, ATA_UDMA6, "JMB368" },
74      { 0, 0, 0, 0, 0, 0}};
75     char buffer[64];
76 
77     if (pci_get_vendor(dev) != ATA_JMICRON_ID)
78 	return ENXIO;
79 
80     if (!(idx = ata_match_chip(dev, ids)))
81         return ENXIO;
82 
83     sprintf(buffer, "JMicron %s %s controller",
84 	idx->text, ata_mode2str(idx->max_dma));
85     device_set_desc_copy(dev, buffer);
86     ctlr->chip = idx;
87     ctlr->chipinit = ata_jmicron_chipinit;
88     return (BUS_PROBE_DEFAULT);
89 }
90 
91 static int
92 ata_jmicron_chipinit(device_t dev)
93 {
94     struct ata_pci_controller *ctlr = device_get_softc(dev);
95     device_t child;
96 
97     if (ata_setup_interrupt(dev, ata_generic_intr))
98 	return ENXIO;
99 
100     /* do we have multiple PCI functions ? */
101     if (pci_read_config(dev, 0xdf, 1) & 0x40) {
102 	/* are we on the AHCI part ? */
103 	if (ata_ahci_chipinit(dev) != ENXIO)
104 	    return 0;
105 
106 	/* otherwise we are on the PATA part */
107 	ctlr->ch_attach = ata_jmicron_ch_attach;
108 	ctlr->ch_detach = ata_pci_ch_detach;
109 	ctlr->reset = ata_generic_reset;
110 	ctlr->setmode = ata_jmicron_setmode;
111 	ctlr->channels = ctlr->chip->cfg2;
112     }
113     else {
114 	/* set controller configuration to a combined setup we support */
115 	pci_write_config(dev, 0x40, 0x80c0a131, 4);
116 	pci_write_config(dev, 0x80, 0x01200000, 4);
117 	/* Create AHCI subdevice if AHCI part present. */
118 	if (ctlr->chip->cfg1) {
119 	    	child = device_add_child(dev, NULL, -1);
120 		if (child != NULL) {
121 		    device_set_ivars(child, (void *)(intptr_t)-1);
122 		    bus_generic_attach(dev);
123 		}
124 	}
125 	ctlr->ch_attach = ata_jmicron_ch_attach;
126 	ctlr->ch_detach = ata_pci_ch_detach;
127 	ctlr->reset = ata_generic_reset;
128 	ctlr->setmode = ata_jmicron_setmode;
129 	ctlr->channels = ctlr->chip->cfg2;
130     }
131     return 0;
132 }
133 
134 static int
135 ata_jmicron_ch_attach(device_t dev)
136 {
137 	struct ata_channel *ch = device_get_softc(dev);
138 	int error;
139 
140 	error = ata_pci_ch_attach(dev);
141 	ch->flags |= ATA_CHECKS_CABLE;
142 	return (error);
143 }
144 
145 static int
146 ata_jmicron_setmode(device_t dev, int target, int mode)
147 {
148 	struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
149 
150 	mode = min(mode, ctlr->chip->max_dma);
151 	/* check for 80pin cable present */
152 	if (ata_dma_check_80pin && mode > ATA_UDMA2 &&
153 	    pci_read_config(dev, 0x40, 1) & 0x08) {
154 		ata_print_cable(dev, "controller");
155 		mode = ATA_UDMA2;
156 	}
157 	/* Nothing to do to setup mode, the controller snoop SET_FEATURE cmd. */
158 	return (mode);
159 }
160 
161 ATA_DECLARE_DRIVER(ata_jmicron);
162 MODULE_DEPEND(ata_jmicron, ata_ahci, 1, 1, 1);
163