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 /* local prototypes */
28 static int ata_jmicron_chipinit(device_t dev);
29 static int ata_jmicron_allocate(device_t dev);
30 static void ata_jmicron_reset(device_t dev);
31 static void ata_jmicron_dmainit(device_t dev);
32 static void ata_jmicron_setmode(device_t dev, int mode);
33
34 /*
35 * JMicron chipset support functions
36 */
37 int
ata_jmicron_ident(device_t dev)38 ata_jmicron_ident(device_t dev)
39 {
40 struct ata_pci_controller *ctlr = device_get_softc(dev);
41 const struct ata_chip_id *idx;
42 static const struct ata_chip_id ids[] =
43 {{ ATA_JMB360, 0, 1, 0, ATA_SA300, "JMB360" },
44 { ATA_JMB361, 0, 1, 1, ATA_SA300, "JMB361" },
45 { ATA_JMB363, 0, 2, 1, ATA_SA300, "JMB363" },
46 { ATA_JMB365, 0, 1, 2, ATA_SA300, "JMB365" },
47 { ATA_JMB366, 0, 2, 2, ATA_SA300, "JMB366" },
48 { ATA_JMB368, 0, 0, 1, ATA_UDMA6, "JMB368" },
49 { 0, 0, 0, 0, 0, 0}};
50 char buffer[64];
51
52 if (pci_get_vendor(dev) != ATA_JMICRON_ID)
53 return ENXIO;
54
55 if (!(idx = ata_match_chip(dev, ids)))
56 return ENXIO;
57
58 if ((pci_read_config(dev, 0xdf, 1) & 0x40) &&
59 (pci_get_function(dev) == (pci_read_config(dev, 0x40, 1) & 0x02 >> 1)))
60 ksnprintf(buffer, sizeof(buffer), "JMicron %s %s controller",
61 idx->text, ata_mode2str(ATA_UDMA6));
62 else
63 ksnprintf(buffer, sizeof(buffer), "JMicron %s %s controller",
64 idx->text, ata_mode2str(idx->max_dma));
65 device_set_desc_copy(dev, buffer);
66 ctlr->chip = idx;
67 ctlr->chipinit = ata_jmicron_chipinit;
68 return 0;
69 }
70
71 static int
ata_jmicron_chipinit(device_t dev)72 ata_jmicron_chipinit(device_t dev)
73 {
74 struct ata_pci_controller *ctlr = device_get_softc(dev);
75 int error;
76
77 if (ata_setup_interrupt(dev, ata_generic_intr))
78 return ENXIO;
79
80 /* do we have multiple PCI functions ? */
81 if (pci_read_config(dev, 0xdf, 1) & 0x40) {
82 /* are we on the AHCI part ? */
83 if (ata_ahci_chipinit(dev) != ENXIO)
84 return 0;
85
86 /* otherwise we are on the PATA part */
87 ctlr->allocate = ata_pci_allocate;
88 ctlr->reset = ata_generic_reset;
89 ctlr->dmainit = ata_pci_dmainit;
90 ctlr->setmode = ata_jmicron_setmode;
91 ctlr->channels = ctlr->chip->cfg2;
92 }
93 else {
94 /* set controller configuration to a combined setup we support */
95 pci_write_config(dev, 0x40, 0x80c0a131, 4);
96 pci_write_config(dev, 0x80, 0x01200000, 4);
97
98 if (ctlr->chip->cfg1 && (error = ata_ahci_chipinit(dev))) {
99 ata_teardown_interrupt(dev);
100 return error;
101 }
102
103 ctlr->allocate = ata_jmicron_allocate;
104 ctlr->reset = ata_jmicron_reset;
105 ctlr->dmainit = ata_jmicron_dmainit;
106 ctlr->setmode = ata_jmicron_setmode;
107
108 /* set the number of HW channels */
109 ctlr->channels = ctlr->chip->cfg1 + ctlr->chip->cfg2;
110 }
111 return 0;
112 }
113
114 static int
ata_jmicron_allocate(device_t dev)115 ata_jmicron_allocate(device_t dev)
116 {
117 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
118 struct ata_channel *ch = device_get_softc(dev);
119 int error;
120
121 if (ch->unit >= ctlr->chip->cfg1) {
122 ch->unit -= ctlr->chip->cfg1;
123 error = ata_pci_allocate(dev);
124 ch->unit += ctlr->chip->cfg1;
125 }
126 else
127 error = ata_ahci_allocate(dev);
128 return error;
129 }
130
131 static void
ata_jmicron_reset(device_t dev)132 ata_jmicron_reset(device_t dev)
133 {
134 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
135 struct ata_channel *ch = device_get_softc(dev);
136
137 if (ch->unit >= ctlr->chip->cfg1)
138 ata_generic_reset(dev);
139 else
140 ata_ahci_reset(dev);
141 }
142
143 static void
ata_jmicron_dmainit(device_t dev)144 ata_jmicron_dmainit(device_t dev)
145 {
146 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
147 struct ata_channel *ch = device_get_softc(dev);
148
149 if (ch->unit >= ctlr->chip->cfg1)
150 ata_pci_dmainit(dev);
151 else
152 ata_ahci_dmainit(dev);
153 }
154
155 static void
ata_jmicron_setmode(device_t dev,int mode)156 ata_jmicron_setmode(device_t dev, int mode)
157 {
158 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
159 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
160
161 if (pci_read_config(dev, 0xdf, 1) & 0x40 || ch->unit >= ctlr->chip->cfg1) {
162 struct ata_device *atadev = device_get_softc(dev);
163
164 /* check for 80pin cable present */
165 if (pci_read_config(dev, 0x40, 1) & 0x08)
166 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
167 else
168 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
169
170 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
171 atadev->mode = mode;
172 }
173 else
174 ata_sata_setmode(dev, mode);
175 }
176