]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/ata/chipsets/ata-serverworks.c
MFC r362623:
[FreeBSD/stable/8.git] / sys / dev / ata / chipsets / ata-serverworks.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 #ifdef __powerpc__
45 #include <machine/intr_machdep.h>
46 #endif
47 #include <machine/stdarg.h>
48 #include <machine/resource.h>
49 #include <machine/bus.h>
50 #include <sys/rman.h>
51 #include <dev/pci/pcivar.h>
52 #include <dev/pci/pcireg.h>
53 #include <dev/ata/ata-all.h>
54 #include <dev/ata/ata-pci.h>
55 #include <ata_if.h>
56
57 /* local prototypes */
58 static int ata_serverworks_chipinit(device_t dev);
59 static int ata_serverworks_ch_attach(device_t dev);
60 static int ata_serverworks_ch_detach(device_t dev);
61 static void ata_serverworks_tf_read(struct ata_request *request);
62 static void ata_serverworks_tf_write(struct ata_request *request);
63 static int ata_serverworks_setmode(device_t dev, int target, int mode);
64 static void ata_serverworks_sata_reset(device_t dev);
65 static int ata_serverworks_status(device_t dev);
66
67 /* misc defines */
68 #define SWKS_33         0
69 #define SWKS_66         1
70 #define SWKS_100        2
71 #define SWKS_MIO        3
72
73
74 /*
75  * ServerWorks chipset support functions
76  */
77 static int
78 ata_serverworks_probe(device_t dev)
79 {
80     struct ata_pci_controller *ctlr = device_get_softc(dev);
81     static const struct ata_chip_id ids[] =
82     {{ ATA_ROSB4,     0x00, SWKS_33,  0, ATA_WDMA2, "ROSB4" },
83      { ATA_CSB5,      0x92, SWKS_100, 0, ATA_UDMA5, "CSB5" },
84      { ATA_CSB5,      0x00, SWKS_66,  0, ATA_UDMA4, "CSB5" },
85      { ATA_CSB6,      0x00, SWKS_100, 0, ATA_UDMA5, "CSB6" },
86      { ATA_CSB6_1,    0x00, SWKS_66,  0, ATA_UDMA4, "CSB6" },
87      { ATA_HT1000,    0x00, SWKS_100, 0, ATA_UDMA5, "HT1000" },
88      { ATA_HT1000_S1, 0x00, SWKS_MIO, 4, ATA_SA150, "HT1000" },
89      { ATA_HT1000_S2, 0x00, SWKS_MIO, 4, ATA_SA150, "HT1000" },
90      { ATA_K2,        0x00, SWKS_MIO, 4, ATA_SA150, "K2" },
91      { ATA_FRODO4,    0x00, SWKS_MIO, 4, ATA_SA150, "Frodo4" },
92      { ATA_FRODO8,    0x00, SWKS_MIO, 8, ATA_SA150, "Frodo8" },
93      { 0, 0, 0, 0, 0, 0}};
94
95     if (pci_get_vendor(dev) != ATA_SERVERWORKS_ID)
96         return ENXIO;
97
98     if (!(ctlr->chip = ata_match_chip(dev, ids)))
99         return ENXIO;
100
101     ata_set_desc(dev);
102     ctlr->chipinit = ata_serverworks_chipinit;
103     return (BUS_PROBE_DEFAULT);
104 }
105
106 static int
107 ata_serverworks_status(device_t dev)
108 {
109     struct ata_channel *ch = device_get_softc(dev);
110     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
111
112     /*
113      * Check if this interrupt belongs to our channel.
114      */
115     if (!(ATA_INL(ctlr->r_res2, 0x1f80) & (1 << ch->unit)))
116         return (0);
117
118     /*
119      * We need to do a 4-byte read on the status reg before the values
120      * will report correctly
121      */
122
123     ATA_IDX_INL(ch,ATA_STATUS);
124
125     return ata_pci_status(dev);
126 }
127
128 static int
129 ata_serverworks_chipinit(device_t dev)
130 {
131     struct ata_pci_controller *ctlr = device_get_softc(dev);
132
133     if (ata_setup_interrupt(dev, ata_generic_intr))
134         return ENXIO;
135
136     if (ctlr->chip->cfg1 == SWKS_MIO) {
137         ctlr->r_type2 = SYS_RES_MEMORY;
138         ctlr->r_rid2 = PCIR_BAR(5);
139         if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
140                                                     &ctlr->r_rid2, RF_ACTIVE)))
141             return ENXIO;
142
143         ctlr->channels = ctlr->chip->cfg2;
144         ctlr->ch_attach = ata_serverworks_ch_attach;
145         ctlr->ch_detach = ata_serverworks_ch_detach;
146         ctlr->setmode = ata_sata_setmode;
147         ctlr->getrev = ata_sata_getrev;
148         ctlr->reset = ata_serverworks_sata_reset;
149         return 0;
150     }
151     else if (ctlr->chip->cfg1 == SWKS_33) {
152         device_t *children;
153         int nchildren, i;
154
155         /* locate the ISA part in the southbridge and enable UDMA33 */
156         if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
157             for (i = 0; i < nchildren; i++) {
158                 if (pci_get_devid(children[i]) == ATA_ROSB4_ISA) {
159                     pci_write_config(children[i], 0x64,
160                                      (pci_read_config(children[i], 0x64, 4) &
161                                       ~0x00002000) | 0x00004000, 4);
162                     break;
163                 }
164             }
165             free(children, M_TEMP);
166         }
167     }
168     else {
169         pci_write_config(dev, 0x5a,
170                          (pci_read_config(dev, 0x5a, 1) & ~0x40) |
171                          (ctlr->chip->cfg1 == SWKS_100) ? 0x03 : 0x02, 1);
172     }
173     ctlr->setmode = ata_serverworks_setmode;
174     return 0;
175 }
176
177 static int
178 ata_serverworks_ch_attach(device_t dev)
179 {
180     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
181     struct ata_channel *ch = device_get_softc(dev);
182     int ch_offset;
183     int i;
184
185     ch_offset = ch->unit * 0x100;
186
187     for (i = ATA_DATA; i < ATA_MAX_RES; i++)
188         ch->r_io[i].res = ctlr->r_res2;
189
190     /* setup ATA registers */
191     ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
192     ch->r_io[ATA_FEATURE].offset = ch_offset + 0x04;
193     ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
194     ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
195     ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
196     ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
197     ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
198     ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1c;
199     ch->r_io[ATA_CONTROL].offset = ch_offset + 0x20;
200     ata_default_registers(dev);
201
202     /* setup DMA registers */
203     ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x30;
204     ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x32;
205     ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x34;
206
207     /* setup SATA registers */
208     ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x40;
209     ch->r_io[ATA_SERROR].offset = ch_offset + 0x44;
210     ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x48;
211
212     ch->flags |= ATA_NO_SLAVE | ATA_SATA | ATA_KNOWN_PRESENCE;
213     ata_pci_hw(dev);
214     ch->hw.tf_read = ata_serverworks_tf_read;
215     ch->hw.tf_write = ata_serverworks_tf_write;
216 #ifdef __powerpc__
217     /* Make sure that our interrupt is level low */
218     powerpc_config_intr(bus_get_resource_start(device_get_parent(dev),
219         SYS_RES_IRQ, 0), INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
220 #endif
221
222     if (ctlr->chip->chipid == ATA_K2) {
223         /*
224          * Set SICR registers to turn off waiting for a status message
225          * before sending FIS. Values obtained from the Darwin driver.
226          */
227
228         ATA_OUTL(ctlr->r_res2, ch_offset + 0x80,
229             ATA_INL(ctlr->r_res2, ch_offset + 0x80) & ~0x00040000);
230         ATA_OUTL(ctlr->r_res2, ch_offset + 0x88, 0);
231
232         /*
233          * Some controllers have a bug where they will send the command
234          * to the drive before seeing a DMA start, and then can begin
235          * receiving data before the DMA start arrives. The controller
236          * will then become confused and either corrupt the data or crash.
237          * Remedy this by starting DMA before sending the drive command.
238          */
239
240         ch->flags |= ATA_DMA_BEFORE_CMD;
241
242         /*
243          * The status register must be read as a long to fill the other
244          * registers.
245          */
246         
247         ch->hw.status = ata_serverworks_status;
248         ch->flags |= ATA_STATUS_IS_LONG;
249     }
250
251     /* chip does not reliably do 64K DMA transfers */
252     ch->dma.max_iosize = 64 * DEV_BSIZE;
253
254     ata_pci_dmainit(dev);
255
256     return 0;
257 }
258
259 static int
260 ata_serverworks_ch_detach(device_t dev)
261 {
262
263     ata_pci_dmafini(dev);
264     return (0);
265 }
266
267 static void
268 ata_serverworks_tf_read(struct ata_request *request)
269 {
270     struct ata_channel *ch = device_get_softc(request->parent);
271
272     if (request->flags & ATA_R_48BIT) {
273         u_int16_t temp;
274
275         request->u.ata.count = ATA_IDX_INW(ch, ATA_COUNT);
276         temp = ATA_IDX_INW(ch, ATA_SECTOR);
277         request->u.ata.lba = (u_int64_t)(temp & 0x00ff) |
278                              ((u_int64_t)(temp & 0xff00) << 24);
279         temp = ATA_IDX_INW(ch, ATA_CYL_LSB);
280         request->u.ata.lba |= ((u_int64_t)(temp & 0x00ff) << 8) |
281                               ((u_int64_t)(temp & 0xff00) << 32);
282         temp = ATA_IDX_INW(ch, ATA_CYL_MSB);
283         request->u.ata.lba |= ((u_int64_t)(temp & 0x00ff) << 16) |
284                               ((u_int64_t)(temp & 0xff00) << 40);
285     }
286     else {
287         request->u.ata.count = ATA_IDX_INW(ch, ATA_COUNT) & 0x00ff;
288         request->u.ata.lba = (ATA_IDX_INW(ch, ATA_SECTOR) & 0x00ff) |
289                              ((ATA_IDX_INW(ch, ATA_CYL_LSB) & 0x00ff) << 8) |
290                              ((ATA_IDX_INW(ch, ATA_CYL_MSB) & 0x00ff) << 16) |
291                              ((ATA_IDX_INW(ch, ATA_DRIVE) & 0xf) << 24);
292     }
293 }
294
295 static void
296 ata_serverworks_tf_write(struct ata_request *request)
297 {
298     struct ata_channel *ch = device_get_softc(request->parent);
299 #ifndef ATA_CAM
300     struct ata_device *atadev = device_get_softc(request->dev);
301 #endif
302
303     if (request->flags & ATA_R_48BIT) {
304         ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
305         ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
306         ATA_IDX_OUTW(ch, ATA_SECTOR, ((request->u.ata.lba >> 16) & 0xff00) |
307                                       (request->u.ata.lba & 0x00ff));
308         ATA_IDX_OUTW(ch, ATA_CYL_LSB, ((request->u.ata.lba >> 24) & 0xff00) |
309                                        ((request->u.ata.lba >> 8) & 0x00ff));
310         ATA_IDX_OUTW(ch, ATA_CYL_MSB, ((request->u.ata.lba >> 32) & 0xff00) | 
311                                        ((request->u.ata.lba >> 16) & 0x00ff));
312         ATA_IDX_OUTW(ch, ATA_DRIVE, ATA_D_LBA | ATA_DEV(request->unit));
313     }
314     else {
315         ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
316         ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
317 #ifndef ATA_CAM
318         if (atadev->flags & ATA_D_USE_CHS) {
319             int heads, sectors;
320     
321             if (atadev->param.atavalid & ATA_FLAG_54_58) {
322                 heads = atadev->param.current_heads;
323                 sectors = atadev->param.current_sectors;
324             }
325             else {
326                 heads = atadev->param.heads;
327                 sectors = atadev->param.sectors;
328             }
329             ATA_IDX_OUTW(ch, ATA_SECTOR, (request->u.ata.lba % sectors)+1);
330             ATA_IDX_OUTW(ch, ATA_CYL_LSB,
331                          (request->u.ata.lba / (sectors * heads)));
332             ATA_IDX_OUTW(ch, ATA_CYL_MSB,
333                          (request->u.ata.lba / (sectors * heads)) >> 8);
334             ATA_IDX_OUTW(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(request->unit) | 
335                          (((request->u.ata.lba% (sectors * heads)) /
336                            sectors) & 0xf));
337         }
338         else {
339 #endif
340             ATA_IDX_OUTW(ch, ATA_SECTOR, request->u.ata.lba);
341             ATA_IDX_OUTW(ch, ATA_CYL_LSB, request->u.ata.lba >> 8);
342             ATA_IDX_OUTW(ch, ATA_CYL_MSB, request->u.ata.lba >> 16);
343             ATA_IDX_OUTW(ch, ATA_DRIVE,
344                          ATA_D_IBM | ATA_D_LBA | ATA_DEV(request->unit) |
345                          ((request->u.ata.lba >> 24) & 0x0f));
346 #ifndef ATA_CAM
347         }
348 #endif
349     }
350 }
351
352 static int
353 ata_serverworks_setmode(device_t dev, int target, int mode)
354 {
355         device_t parent = device_get_parent(dev);
356         struct ata_pci_controller *ctlr = device_get_softc(parent);
357         struct ata_channel *ch = device_get_softc(dev);
358         int devno = (ch->unit << 1) + target;
359         int offset = (devno ^ 0x01) << 3;
360         int piomode;
361         static const uint8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20 };
362         static const uint8_t dmatimings[] = { 0x77, 0x21, 0x20 };
363
364         mode = min(mode, ctlr->chip->max_dma);
365         if (mode >= ATA_UDMA0) {
366             /* Set UDMA mode, enable UDMA, set WDMA2/PIO4 */
367             pci_write_config(parent, 0x56, 
368                              (pci_read_config(parent, 0x56, 2) &
369                               ~(0xf << (devno << 2))) |
370                              ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
371             pci_write_config(parent, 0x54,
372                              pci_read_config(parent, 0x54, 1) |
373                              (0x01 << devno), 1);
374             pci_write_config(parent, 0x44, 
375                              (pci_read_config(parent, 0x44, 4) &
376                               ~(0xff << offset)) |
377                              (dmatimings[2] << offset), 4);
378             piomode = ATA_PIO4;
379         } else if (mode >= ATA_WDMA0) {
380             /* Disable UDMA, set WDMA mode and timings, calculate PIO. */
381             pci_write_config(parent, 0x54,
382                              pci_read_config(parent, 0x54, 1) &
383                               ~(0x01 << devno), 1);
384             pci_write_config(parent, 0x44, 
385                              (pci_read_config(parent, 0x44, 4) &
386                               ~(0xff << offset)) |
387                              (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
388             piomode = (mode == ATA_WDMA0) ? ATA_PIO0 :
389                 (mode == ATA_WDMA1) ? ATA_PIO3 : ATA_PIO4;
390         } else {
391             /* Disable UDMA, set requested PIO. */
392             pci_write_config(parent, 0x54,
393                              pci_read_config(parent, 0x54, 1) &
394                              ~(0x01 << devno), 1);
395             piomode = mode;
396         }
397         /* Set PIO mode and timings, calculated above. */
398         if (ctlr->chip->cfg1 != SWKS_33) {
399                 pci_write_config(parent, 0x4a,
400                          (pci_read_config(parent, 0x4a, 2) &
401                           ~(0xf << (devno << 2))) |
402                          ((piomode - ATA_PIO0) << (devno<<2)),2);
403         }
404         pci_write_config(parent, 0x40, 
405                          (pci_read_config(parent, 0x40, 4) &
406                           ~(0xff << offset)) |
407                          (piotimings[ata_mode2idx(piomode)] << offset), 4);
408         return (mode);
409 }
410
411 static void
412 ata_serverworks_sata_reset(device_t dev)
413 {
414         struct ata_channel *ch = device_get_softc(dev);
415
416         if (ata_sata_phy_reset(dev, -1, 0))
417                 ata_generic_reset(dev);
418         else
419                 ch->devices = 0;
420 }
421
422 ATA_DECLARE_DRIVER(ata_serverworks);