]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ata/chipsets/ata-jmicron.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / sys / dev / ata / chipsets / ata-jmicron.c
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_allocate(device_t dev);
57 static void ata_jmicron_reset(device_t dev);
58 static void ata_jmicron_dmainit(device_t dev);
59 static void ata_jmicron_setmode(device_t dev, int mode);
60
61
62 /*
63  * JMicron chipset support functions
64  */
65 static int
66 ata_jmicron_probe(device_t dev)
67 {
68     struct ata_pci_controller *ctlr = device_get_softc(dev);
69     struct ata_chip_id *idx;
70     static struct ata_chip_id ids[] =
71     {{ ATA_JMB360, 0, 1, 0, ATA_SA300, "JMB360" },
72      { ATA_JMB361, 0, 1, 1, ATA_SA300, "JMB361" },
73      { ATA_JMB363, 0, 2, 1, ATA_SA300, "JMB363" },
74      { ATA_JMB365, 0, 1, 2, ATA_SA300, "JMB365" },
75      { ATA_JMB366, 0, 2, 2, ATA_SA300, "JMB366" },
76      { ATA_JMB368, 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     if ((pci_read_config(dev, 0xdf, 1) & 0x40) &&
87         (pci_get_function(dev) == (pci_read_config(dev, 0x40, 1) & 0x02 >> 1)))
88         sprintf(buffer, "JMicron %s %s controller",
89                 idx->text, ata_mode2str(ATA_UDMA6));
90     else
91         sprintf(buffer, "JMicron %s %s controller",
92                 idx->text, ata_mode2str(idx->max_dma));
93     device_set_desc_copy(dev, buffer);
94     ctlr->chip = idx;
95     ctlr->chipinit = ata_jmicron_chipinit;
96     return 0;
97 }
98
99 static int
100 ata_jmicron_chipinit(device_t dev)
101 {
102     struct ata_pci_controller *ctlr = device_get_softc(dev);
103     int error;
104
105     if (ata_setup_interrupt(dev, ata_generic_intr))
106         return ENXIO;
107
108     /* do we have multiple PCI functions ? */
109     if (pci_read_config(dev, 0xdf, 1) & 0x40) {
110         /* are we on the AHCI part ? */
111         if (ata_ahci_chipinit(dev) != ENXIO)
112             return 0;
113
114         /* otherwise we are on the PATA part */
115         ctlr->allocate = ata_pci_allocate;
116         ctlr->reset = ata_generic_reset;
117         ctlr->dmainit = ata_pci_dmainit;
118         ctlr->setmode = ata_jmicron_setmode;
119         ctlr->channels = ctlr->chip->cfg2;
120     }
121     else {
122         /* set controller configuration to a combined setup we support */
123         pci_write_config(dev, 0x40, 0x80c0a131, 4);
124         pci_write_config(dev, 0x80, 0x01200000, 4);
125
126         if (ctlr->chip->cfg1 && (error = ata_ahci_chipinit(dev)))
127             return error;
128
129         ctlr->allocate = ata_jmicron_allocate;
130         ctlr->reset = ata_jmicron_reset;
131         ctlr->dmainit = ata_jmicron_dmainit;
132         ctlr->setmode = ata_jmicron_setmode;
133
134         /* set the number of HW channels */ 
135         ctlr->channels = ctlr->chip->cfg1 + ctlr->chip->cfg2;
136     }
137     return 0;
138 }
139
140 static int
141 ata_jmicron_allocate(device_t dev)
142 {
143     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
144     struct ata_channel *ch = device_get_softc(dev);
145     int error;
146
147     if (ch->unit >= ctlr->chip->cfg1) {
148         ch->unit -= ctlr->chip->cfg1;
149         error = ata_pci_allocate(dev);
150         ch->unit += ctlr->chip->cfg1;
151     }
152     else
153         error = ata_ahci_allocate(dev);
154     return error;
155 }
156
157 static void
158 ata_jmicron_reset(device_t dev)
159 {
160     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
161     struct ata_channel *ch = device_get_softc(dev);
162
163     if (ch->unit >= ctlr->chip->cfg1)
164         ata_generic_reset(dev);
165     else
166         ata_ahci_reset(dev);
167 }
168
169 static void
170 ata_jmicron_dmainit(device_t dev)
171 {
172     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
173     struct ata_channel *ch = device_get_softc(dev);
174
175     if (ch->unit >= ctlr->chip->cfg1)
176         ata_pci_dmainit(dev);
177     else
178         ata_ahci_dmainit(dev);
179 }
180
181 static void
182 ata_jmicron_setmode(device_t dev, int mode)
183 {
184     struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
185     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
186
187     if (pci_read_config(dev, 0xdf, 1) & 0x40 || ch->unit >= ctlr->chip->cfg1) {
188         struct ata_device *atadev = device_get_softc(dev);
189
190         /* check for 80pin cable present */
191         if (pci_read_config(dev, 0x40, 1) & 0x08)
192             mode = ata_limit_mode(dev, mode, ATA_UDMA2);
193         else
194             mode = ata_limit_mode(dev, mode, ATA_UDMA6);
195
196         if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
197             atadev->mode = mode;
198     }
199     else
200         ata_sata_setmode(dev, mode);
201 }
202
203 ATA_DECLARE_DRIVER(ata_jmicron);
204 MODULE_DEPEND(ata_jmicron, ata_ahci, 1, 1, 1);