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