]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ata/chipsets/ata-promise.c
As soon as they called in only same one place (ata_pcichannel_attach()),
[FreeBSD/FreeBSD.git] / sys / dev / ata / chipsets / ata-promise.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_promise_chipinit(device_t dev);
56 static int ata_promise_ch_attach(device_t dev);
57 static int ata_promise_status(device_t dev);
58 static int ata_promise_dmastart(struct ata_request *request);
59 static int ata_promise_dmastop(struct ata_request *request);
60 static void ata_promise_dmareset(device_t dev);
61 static void ata_promise_dmainit(device_t dev);
62 static void ata_promise_setmode(device_t dev, int mode);
63 static int ata_promise_tx2_ch_attach(device_t dev);
64 static int ata_promise_tx2_status(device_t dev);
65 static int ata_promise_mio_ch_attach(device_t dev);
66 static void ata_promise_mio_intr(void *data);
67 static int ata_promise_mio_status(device_t dev);
68 static int ata_promise_mio_command(struct ata_request *request);
69 static void ata_promise_mio_reset(device_t dev);
70 static int ata_promise_mio_pm_read(device_t dev, int port, int reg, u_int32_t *result);
71 static int ata_promise_mio_pm_write(device_t dev, int port, int reg, u_int32_t result);
72 static u_int32_t ata_promise_mio_softreset(device_t dev, int port);
73 static void ata_promise_mio_dmainit(device_t dev);
74 static void ata_promise_mio_setprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
75 static void ata_promise_mio_setmode(device_t dev, int mode);
76 static void ata_promise_sx4_intr(void *data);
77 static int ata_promise_sx4_command(struct ata_request *request);
78 static int ata_promise_apkt(u_int8_t *bytep, struct ata_request *request);
79 static void ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt);
80 static void ata_promise_next_hpkt(struct ata_pci_controller *ctlr);
81
82 /* misc defines */
83 #define PR_OLD          0
84 #define PR_NEW          1
85 #define PR_TX           2
86 #define PR_MIO          3
87 #define PR_TX4          0x01
88 #define PR_SX4X         0x02
89 #define PR_SX6K         0x04
90 #define PR_PATA         0x08
91 #define PR_CMBO         0x10
92 #define PR_CMBO2        0x20
93 #define PR_SATA         0x40
94 #define PR_SATA2        0x80
95
96
97 /*
98  * Promise chipset support functions
99  */
100 #define ATA_PDC_APKT_OFFSET     0x00000010 
101 #define ATA_PDC_HPKT_OFFSET     0x00000040
102 #define ATA_PDC_ASG_OFFSET      0x00000080
103 #define ATA_PDC_LSG_OFFSET      0x000000c0
104 #define ATA_PDC_HSG_OFFSET      0x00000100
105 #define ATA_PDC_CHN_OFFSET      0x00000400
106 #define ATA_PDC_BUF_BASE        0x00400000
107 #define ATA_PDC_BUF_OFFSET      0x00100000
108 #define ATA_PDC_MAX_HPKT        8
109 #define ATA_PDC_WRITE_REG       0x00
110 #define ATA_PDC_WRITE_CTL       0x0e
111 #define ATA_PDC_WRITE_END       0x08
112 #define ATA_PDC_WAIT_NBUSY      0x10
113 #define ATA_PDC_WAIT_READY      0x18
114 #define ATA_PDC_1B              0x20
115 #define ATA_PDC_2B              0x40
116
117 struct host_packet {
118     u_int32_t                   addr;
119     TAILQ_ENTRY(host_packet)    chain;
120 };
121
122 struct ata_promise_sx4 {
123     struct mtx                  mtx;
124     TAILQ_HEAD(, host_packet)   queue;
125     int                         busy;
126 };
127
128 static int
129 ata_promise_probe(device_t dev)
130 {
131     struct ata_pci_controller *ctlr = device_get_softc(dev);
132     struct ata_chip_id *idx;
133     static struct ata_chip_id ids[] =
134     {{ ATA_PDC20246,  0, PR_OLD, 0x00,     ATA_UDMA2, "PDC20246" },
135      { ATA_PDC20262,  0, PR_NEW, 0x00,     ATA_UDMA4, "PDC20262" },
136      { ATA_PDC20263,  0, PR_NEW, 0x00,     ATA_UDMA4, "PDC20263" },
137      { ATA_PDC20265,  0, PR_NEW, 0x00,     ATA_UDMA5, "PDC20265" },
138      { ATA_PDC20267,  0, PR_NEW, 0x00,     ATA_UDMA5, "PDC20267" },
139      { ATA_PDC20268,  0, PR_TX,  PR_TX4,   ATA_UDMA5, "PDC20268" },
140      { ATA_PDC20269,  0, PR_TX,  0x00,     ATA_UDMA6, "PDC20269" },
141      { ATA_PDC20270,  0, PR_TX,  PR_TX4,   ATA_UDMA5, "PDC20270" },
142      { ATA_PDC20271,  0, PR_TX,  0x00,     ATA_UDMA6, "PDC20271" },
143      { ATA_PDC20275,  0, PR_TX,  0x00,     ATA_UDMA6, "PDC20275" },
144      { ATA_PDC20276,  0, PR_TX,  PR_SX6K,  ATA_UDMA6, "PDC20276" },
145      { ATA_PDC20277,  0, PR_TX,  0x00,     ATA_UDMA6, "PDC20277" },
146      { ATA_PDC20318,  0, PR_MIO, PR_SATA,  ATA_SA150, "PDC20318" },
147      { ATA_PDC20319,  0, PR_MIO, PR_SATA,  ATA_SA150, "PDC20319" },
148      { ATA_PDC20371,  0, PR_MIO, PR_CMBO,  ATA_SA150, "PDC20371" },
149      { ATA_PDC20375,  0, PR_MIO, PR_CMBO,  ATA_SA150, "PDC20375" },
150      { ATA_PDC20376,  0, PR_MIO, PR_CMBO,  ATA_SA150, "PDC20376" },
151      { ATA_PDC20377,  0, PR_MIO, PR_CMBO,  ATA_SA150, "PDC20377" },
152      { ATA_PDC20378,  0, PR_MIO, PR_CMBO,  ATA_SA150, "PDC20378" },
153      { ATA_PDC20379,  0, PR_MIO, PR_CMBO,  ATA_SA150, "PDC20379" },
154      { ATA_PDC20571,  0, PR_MIO, PR_CMBO2, ATA_SA150, "PDC20571" },
155      { ATA_PDC20575,  0, PR_MIO, PR_CMBO2, ATA_SA150, "PDC20575" },
156      { ATA_PDC20579,  0, PR_MIO, PR_CMBO2, ATA_SA150, "PDC20579" },
157      { ATA_PDC20771,  0, PR_MIO, PR_CMBO2, ATA_SA300, "PDC20771" },
158      { ATA_PDC40775,  0, PR_MIO, PR_CMBO2, ATA_SA300, "PDC40775" },
159      { ATA_PDC20617,  0, PR_MIO, PR_PATA,  ATA_UDMA6, "PDC20617" },
160      { ATA_PDC20618,  0, PR_MIO, PR_PATA,  ATA_UDMA6, "PDC20618" },
161      { ATA_PDC20619,  0, PR_MIO, PR_PATA,  ATA_UDMA6, "PDC20619" },
162      { ATA_PDC20620,  0, PR_MIO, PR_PATA,  ATA_UDMA6, "PDC20620" },
163      { ATA_PDC20621,  0, PR_MIO, PR_SX4X,  ATA_UDMA5, "PDC20621" },
164      { ATA_PDC20622,  0, PR_MIO, PR_SX4X,  ATA_SA150, "PDC20622" },
165      { ATA_PDC40518,  0, PR_MIO, PR_SATA2, ATA_SA150, "PDC40518" },
166      { ATA_PDC40519,  0, PR_MIO, PR_SATA2, ATA_SA150, "PDC40519" },
167      { ATA_PDC40718,  0, PR_MIO, PR_SATA2, ATA_SA300, "PDC40718" },
168      { ATA_PDC40719,  0, PR_MIO, PR_SATA2, ATA_SA300, "PDC40719" },
169      { ATA_PDC40779,  0, PR_MIO, PR_SATA2, ATA_SA300, "PDC40779" },
170      { 0, 0, 0, 0, 0, 0}};
171     char buffer[64];
172     uintptr_t devid = 0;
173
174     if (pci_get_vendor(dev) != ATA_PROMISE_ID)
175         return ENXIO;
176
177     if (!(idx = ata_match_chip(dev, ids)))
178         return ENXIO;
179
180     /* if we are on a SuperTrak SX6000 dont attach */
181     if ((idx->cfg2 & PR_SX6K) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
182         !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
183                        GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
184         devid == ATA_I960RM) 
185         return ENXIO;
186
187     strcpy(buffer, "Promise ");
188     strcat(buffer, idx->text);
189
190     /* if we are on a FastTrak TX4, adjust the interrupt resource */
191     if ((idx->cfg2 & PR_TX4) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
192         !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
193                        GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
194         ((devid == ATA_DEC_21150) || (devid == ATA_DEC_21150_1))) {
195         static long start = 0, end = 0;
196
197         if (pci_get_slot(dev) == 1) {
198             bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
199             strcat(buffer, " (channel 0+1)");
200         }
201         else if (pci_get_slot(dev) == 2 && start && end) {
202             bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
203             strcat(buffer, " (channel 2+3)");
204         }
205         else {
206             start = end = 0;
207         }
208     }
209     sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
210     device_set_desc_copy(dev, buffer);
211     ctlr->chip = idx;
212     ctlr->chipinit = ata_promise_chipinit;
213     return 0;
214 }
215
216 static int
217 ata_promise_chipinit(device_t dev)
218 {
219     struct ata_pci_controller *ctlr = device_get_softc(dev);
220     int fake_reg, stat_reg;
221
222     if (ata_setup_interrupt(dev, ata_generic_intr))
223         return ENXIO;
224
225     switch  (ctlr->chip->cfg1) {
226     case PR_NEW:
227         /* setup clocks */
228         ATA_OUTB(ctlr->r_res1, 0x11, ATA_INB(ctlr->r_res1, 0x11) | 0x0a);
229         /* FALLTHROUGH */
230
231     case PR_OLD:
232         /* enable burst mode */
233         ATA_OUTB(ctlr->r_res1, 0x1f, ATA_INB(ctlr->r_res1, 0x1f) | 0x01);
234         ctlr->ch_attach = ata_promise_ch_attach;
235         ctlr->setmode = ata_promise_setmode;
236         return 0;
237
238     case PR_TX:
239         ctlr->ch_attach = ata_promise_tx2_ch_attach;
240         ctlr->setmode = ata_promise_setmode;
241         return 0;
242
243     case PR_MIO:
244         ctlr->r_type1 = SYS_RES_MEMORY;
245         ctlr->r_rid1 = PCIR_BAR(4);
246         if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
247                                                     &ctlr->r_rid1, RF_ACTIVE)))
248             goto failnfree;
249
250         ctlr->r_type2 = SYS_RES_MEMORY;
251         ctlr->r_rid2 = PCIR_BAR(3);
252         if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
253                                                     &ctlr->r_rid2, RF_ACTIVE)))
254             goto failnfree;
255
256         if (ctlr->chip->cfg2 == PR_SX4X) {
257             struct ata_promise_sx4 *hpkt;
258             u_int32_t dimm = ATA_INL(ctlr->r_res2, 0x000c0080);
259
260             if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
261                 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL,
262                                ata_promise_sx4_intr, ctlr, &ctlr->handle)) {
263                 device_printf(dev, "unable to setup interrupt\n");
264                 goto failnfree;
265             }
266
267             /* print info about cache memory */
268             device_printf(dev, "DIMM size %dMB @ 0x%08x%s\n",
269                           (((dimm >> 16) & 0xff)-((dimm >> 24) & 0xff)+1) << 4,
270                           ((dimm >> 24) & 0xff),
271                           ATA_INL(ctlr->r_res2, 0x000c0088) & (1<<16) ?
272                           " ECC enabled" : "" );
273
274             /* adjust cache memory parameters */
275             ATA_OUTL(ctlr->r_res2, 0x000c000c, 
276                      (ATA_INL(ctlr->r_res2, 0x000c000c) & 0xffff0000));
277
278             /* setup host packet controls */
279             hpkt = malloc(sizeof(struct ata_promise_sx4),
280                           M_TEMP, M_NOWAIT | M_ZERO);
281             mtx_init(&hpkt->mtx, "ATA promise HPKT lock", NULL, MTX_DEF);
282             TAILQ_INIT(&hpkt->queue);
283             hpkt->busy = 0;
284             device_set_ivars(dev, hpkt);
285             ctlr->ch_attach = ata_promise_mio_ch_attach;
286             ctlr->reset = ata_promise_mio_reset;
287             ctlr->setmode = ata_promise_setmode;
288             ctlr->channels = 4;
289             return 0;
290         }
291
292         /* mio type controllers need an interrupt intercept */
293         if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
294             bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL,
295                                ata_promise_mio_intr, ctlr, &ctlr->handle)) {
296                 device_printf(dev, "unable to setup interrupt\n");
297                 goto failnfree;
298         }
299
300         switch (ctlr->chip->cfg2) {
301         case PR_PATA:
302             ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x01) > 0) +
303                              ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 2;
304             goto sata150;
305         case PR_CMBO:
306             ctlr->channels = 3;
307             goto sata150;
308         case PR_SATA:
309             ctlr->channels = 4;
310 sata150:
311             fake_reg = 0x60;
312             stat_reg = 0x6c;
313             break;
314
315         case PR_CMBO2: 
316             ctlr->channels = 3;
317             goto sataii;
318         case PR_SATA2:
319         default:
320             ctlr->channels = 4;
321 sataii:
322             fake_reg = 0x54;
323             stat_reg = 0x60;
324             break;
325         }
326
327         /* prime fake interrupt register */
328         ATA_OUTL(ctlr->r_res2, fake_reg, 0xffffffff);
329
330         /* clear SATA status and unmask interrupts */
331         ATA_OUTL(ctlr->r_res2, stat_reg, 0x000000ff);
332
333         /* enable "long burst length" on gen2 chips */
334         if ((ctlr->chip->cfg2 == PR_SATA2) || (ctlr->chip->cfg2 == PR_CMBO2))
335             ATA_OUTL(ctlr->r_res2, 0x44, ATA_INL(ctlr->r_res2, 0x44) | 0x2000);
336
337         ctlr->ch_attach = ata_promise_mio_ch_attach;
338         ctlr->reset = ata_promise_mio_reset;
339         ctlr->setmode = ata_promise_mio_setmode;
340
341         return 0;
342     }
343
344 failnfree:
345     if (ctlr->r_res2)
346         bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
347     if (ctlr->r_res1)
348         bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
349     return ENXIO;
350 }
351
352 static int
353 ata_promise_ch_attach(device_t dev)
354 {
355     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
356     struct ata_channel *ch = device_get_softc(dev);
357
358     if (ctlr->chip->cfg1 == PR_NEW)
359         ata_promise_dmainit(dev);
360
361     if (ata_pci_ch_attach(dev))
362         return ENXIO;
363
364     ch->hw.status = ata_promise_status;
365     return 0;
366 }
367
368 static int
369 ata_promise_status(device_t dev)
370 {
371     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
372     struct ata_channel *ch = device_get_softc(dev);
373
374     if (ATA_INL(ctlr->r_res1, 0x1c) & (ch->unit ? 0x00004000 : 0x00000400)) {
375         return ata_pci_status(dev);
376     }
377     return 0;
378 }
379
380 static int
381 ata_promise_dmastart(struct ata_request *request)
382 {
383     struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
384     struct ata_channel *ch = device_get_softc(request->parent);
385     struct ata_device *atadev  = device_get_softc(request->dev);
386
387     if (atadev->flags & ATA_D_48BIT_ACTIVE) {
388         ATA_OUTB(ctlr->r_res1, 0x11,
389                  ATA_INB(ctlr->r_res1, 0x11) | (ch->unit ? 0x08 : 0x02));
390         ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20,
391                  ((request->flags & ATA_R_READ) ? 0x05000000 : 0x06000000) |
392                  (request->bytecount >> 1));
393     }
394     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
395                  (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
396     ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, request->dma->sg_bus);
397     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
398                  ((request->flags & ATA_R_READ) ? ATA_BMCMD_WRITE_READ : 0) |
399                  ATA_BMCMD_START_STOP);
400     ch->dma.flags |= ATA_DMA_ACTIVE;
401     return 0;
402 }
403
404 static int
405 ata_promise_dmastop(struct ata_request *request)
406 {
407     struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
408     struct ata_channel *ch = device_get_softc(request->parent);
409     struct ata_device *atadev  = device_get_softc(request->dev);
410     int error;
411
412     if (atadev->flags & ATA_D_48BIT_ACTIVE) {
413         ATA_OUTB(ctlr->r_res1, 0x11,
414                  ATA_INB(ctlr->r_res1, 0x11) & ~(ch->unit ? 0x08 : 0x02));
415         ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20, 0);
416     }
417     error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT);
418     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
419                  ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
420     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR); 
421     ch->dma.flags &= ~ATA_DMA_ACTIVE;
422     return error;
423 }
424
425 static void
426 ata_promise_dmareset(device_t dev)
427 {
428     struct ata_channel *ch = device_get_softc(dev);
429
430     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
431                  ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
432     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR); 
433     ch->flags &= ~ATA_DMA_ACTIVE;
434 }
435
436 static void
437 ata_promise_dmainit(device_t dev)
438 {
439     struct ata_channel *ch = device_get_softc(dev);
440
441     ata_dmainit(dev);
442     ch->dma.start = ata_promise_dmastart;
443     ch->dma.stop = ata_promise_dmastop;
444     ch->dma.reset = ata_promise_dmareset;
445 }
446
447 static void
448 ata_promise_setmode(device_t dev, int mode)
449 {
450     device_t gparent = GRANDPARENT(dev);
451     struct ata_pci_controller *ctlr = device_get_softc(gparent);
452     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
453     struct ata_device *atadev = device_get_softc(dev);
454     int devno = (ch->unit << 1) + atadev->unit;
455     int error;
456     u_int32_t timings[][2] = {
457     /*    PR_OLD      PR_NEW               mode */
458         { 0x004ff329, 0x004fff2f },     /* PIO 0 */
459         { 0x004fec25, 0x004ff82a },     /* PIO 1 */
460         { 0x004fe823, 0x004ff026 },     /* PIO 2 */
461         { 0x004fe622, 0x004fec24 },     /* PIO 3 */
462         { 0x004fe421, 0x004fe822 },     /* PIO 4 */
463         { 0x004567f3, 0x004acef6 },     /* MWDMA 0 */
464         { 0x004467f3, 0x0048cef6 },     /* MWDMA 1 */
465         { 0x004367f3, 0x0046cef6 },     /* MWDMA 2 */
466         { 0x004367f3, 0x0046cef6 },     /* UDMA 0 */
467         { 0x004247f3, 0x00448ef6 },     /* UDMA 1 */
468         { 0x004127f3, 0x00436ef6 },     /* UDMA 2 */
469         { 0,          0x00424ef6 },     /* UDMA 3 */
470         { 0,          0x004127f3 },     /* UDMA 4 */
471         { 0,          0x004127f3 }      /* UDMA 5 */
472     };
473
474     mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
475
476     switch (ctlr->chip->cfg1) {
477     case PR_OLD:
478     case PR_NEW:
479         if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x50, 2) &
480                                  (ch->unit ? 1 << 11 : 1 << 10))) {
481             ata_print_cable(dev, "controller");
482             mode = ATA_UDMA2;
483         }
484         if (ata_atapi(dev) && mode > ATA_PIO_MAX)
485             mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
486         break;
487
488     case PR_TX:
489         ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
490         if (mode > ATA_UDMA2 &&
491             ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x04) {
492             ata_print_cable(dev, "controller");
493             mode = ATA_UDMA2;
494         }
495         break;
496    
497     case PR_MIO:
498         if (mode > ATA_UDMA2 &&
499             (ATA_INL(ctlr->r_res2,
500                      (ctlr->chip->cfg2 & PR_SX4X ? 0x000c0260 : 0x0260) +
501                      (ch->unit << 7)) & 0x01000000)) {
502             ata_print_cable(dev, "controller");
503             mode = ATA_UDMA2;
504         }
505         break;
506     }
507
508     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
509
510     if (bootverbose)
511         device_printf(dev, "%ssetting %s on %s chip\n",
512                      (error) ? "FAILURE " : "",
513                      ata_mode2str(mode), ctlr->chip->text);
514     if (!error) {
515         if (ctlr->chip->cfg1 < PR_TX)
516             pci_write_config(gparent, 0x60 + (devno << 2),
517                              timings[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
518         atadev->mode = mode;
519     }
520     return;
521 }
522
523 static int
524 ata_promise_tx2_ch_attach(device_t dev)
525 {
526     struct ata_channel *ch = device_get_softc(dev);
527
528     if (ata_pci_ch_attach(dev))
529         return ENXIO;
530
531     ch->hw.status = ata_promise_tx2_status;
532     return 0;
533 }
534
535 static int
536 ata_promise_tx2_status(device_t dev)
537 {
538     struct ata_channel *ch = device_get_softc(dev);
539
540     ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
541     if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x20) {
542         return ata_pci_status(dev);
543     }
544     return 0;
545 }
546
547 static int
548 ata_promise_mio_ch_attach(device_t dev)
549 {
550     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
551     struct ata_channel *ch = device_get_softc(dev);
552     int offset = (ctlr->chip->cfg2 & PR_SX4X) ? 0x000c0000 : 0;
553     int i;
554
555     ata_promise_mio_dmainit(dev);
556
557     for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
558         ch->r_io[i].res = ctlr->r_res2;
559         ch->r_io[i].offset = offset + 0x0200 + (i << 2) + (ch->unit << 7); 
560     }
561     ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
562     ch->r_io[ATA_CONTROL].offset = offset + 0x0238 + (ch->unit << 7);
563     ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
564     ata_default_registers(dev);
565     if ((ctlr->chip->cfg2 & (PR_SATA | PR_SATA2)) ||
566         ((ctlr->chip->cfg2 & (PR_CMBO | PR_CMBO2)) && ch->unit < 2)) {
567         ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
568         ch->r_io[ATA_SSTATUS].offset = 0x400 + (ch->unit << 8);
569         ch->r_io[ATA_SERROR].res = ctlr->r_res2;
570         ch->r_io[ATA_SERROR].offset = 0x404 + (ch->unit << 8);
571         ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
572         ch->r_io[ATA_SCONTROL].offset = 0x408 + (ch->unit << 8);
573         ch->flags |= ATA_NO_SLAVE;
574     }
575     ch->flags |= ATA_USE_16BIT;
576
577     ata_generic_hw(dev);
578     if (ctlr->chip->cfg2 & PR_SX4X) {
579         ch->hw.command = ata_promise_sx4_command;
580     }
581     else {
582         ch->hw.command = ata_promise_mio_command;
583         ch->hw.status = ata_promise_mio_status;
584         ch->hw.softreset = ata_promise_mio_softreset;
585         ch->hw.pm_read = ata_promise_mio_pm_read;
586         ch->hw.pm_write = ata_promise_mio_pm_write;
587      }
588     return 0;
589 }
590
591 static void
592 ata_promise_mio_intr(void *data)
593 {
594     struct ata_pci_controller *ctlr = data;
595     struct ata_channel *ch;
596     u_int32_t vector;
597     int unit, fake_reg;
598
599     switch (ctlr->chip->cfg2) {
600     case PR_PATA:
601     case PR_CMBO:
602     case PR_SATA:
603         fake_reg = 0x60;
604         break;
605     case PR_CMBO2: 
606     case PR_SATA2:
607     default:
608         fake_reg = 0x54;
609         break;
610     }
611
612     /*
613      * since reading interrupt status register on early "mio" chips
614      * clears the status bits we cannot read it for each channel later on
615      * in the generic interrupt routine.
616      * store the bits in an unused register in the chip so we can read
617      * it from there safely to get around this "feature".
618      */
619     vector = ATA_INL(ctlr->r_res2, 0x040);
620     ATA_OUTL(ctlr->r_res2, 0x040, vector);
621     ATA_OUTL(ctlr->r_res2, fake_reg, vector);
622
623     for (unit = 0; unit < ctlr->channels; unit++) {
624         if ((ch = ctlr->interrupt[unit].argument))
625             ctlr->interrupt[unit].function(ch);
626     }
627
628     ATA_OUTL(ctlr->r_res2, fake_reg, 0xffffffff);
629 }
630
631 static int
632 ata_promise_mio_status(device_t dev)
633 {
634     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
635     struct ata_channel *ch = device_get_softc(dev);
636     struct ata_connect_task *tp;
637     u_int32_t fake_reg, stat_reg, vector, status;
638
639     switch (ctlr->chip->cfg2) {
640     case PR_PATA:
641     case PR_CMBO:
642     case PR_SATA:
643         fake_reg = 0x60;
644         stat_reg = 0x6c;
645         break;
646     case PR_CMBO2: 
647     case PR_SATA2:
648     default:
649         fake_reg = 0x54;
650         stat_reg = 0x60;
651         break;
652     }
653
654     /* read and acknowledge interrupt */
655     vector = ATA_INL(ctlr->r_res2, fake_reg);
656
657     /* read and clear interface status */
658     status = ATA_INL(ctlr->r_res2, stat_reg);
659     ATA_OUTL(ctlr->r_res2, stat_reg, status & (0x00000011 << ch->unit));
660
661     /* check for and handle disconnect events */
662     if ((status & (0x00000001 << ch->unit)) &&
663         (tp = (struct ata_connect_task *)
664               malloc(sizeof(struct ata_connect_task),
665                      M_ATA, M_NOWAIT | M_ZERO))) {
666
667         if (bootverbose)
668             device_printf(dev, "DISCONNECT requested\n");
669         tp->action = ATA_C_DETACH;
670         tp->dev = dev;
671         TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
672         taskqueue_enqueue(taskqueue_thread, &tp->task);
673     }
674
675     /* check for and handle connect events */
676     if ((status & (0x00000010 << ch->unit)) &&
677         (tp = (struct ata_connect_task *)
678               malloc(sizeof(struct ata_connect_task),
679                      M_ATA, M_NOWAIT | M_ZERO))) {
680
681         if (bootverbose)
682             device_printf(dev, "CONNECT requested\n");
683         tp->action = ATA_C_ATTACH;
684         tp->dev = dev;
685         TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
686         taskqueue_enqueue(taskqueue_thread, &tp->task);
687     }
688
689     /* do we have any device action ? */
690     return (vector & (1 << (ch->unit + 1)));
691 }
692
693 static int
694 ata_promise_mio_command(struct ata_request *request)
695 {
696     struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
697     struct ata_channel *ch = device_get_softc(request->parent);
698     struct ata_device *atadev = device_get_softc(request->dev);
699
700     u_int32_t *wordp = (u_int32_t *)ch->dma.work;
701
702     ATA_OUTL(ctlr->r_res2, (ch->unit + 1) << 2, 0x00000001);
703
704     /* set portmultiplier port */
705     ATA_OUTB(ctlr->r_res2, 0x4e8 + (ch->unit << 8), atadev->unit & 0x0f);
706
707     /* XXX SOS add ATAPI commands support later */
708     switch (request->u.ata.command) {
709     default:
710         return ata_generic_command(request);
711
712     case ATA_READ_DMA:
713     case ATA_READ_DMA48:
714         wordp[0] = htole32(0x04 | ((ch->unit + 1) << 16) | (0x00 << 24));
715         break;
716
717     case ATA_WRITE_DMA:
718     case ATA_WRITE_DMA48:
719         wordp[0] = htole32(0x00 | ((ch->unit + 1) << 16) | (0x00 << 24));
720         break;
721     }
722     wordp[1] = htole32(request->dma->sg_bus);
723     wordp[2] = 0;
724     ata_promise_apkt((u_int8_t*)wordp, request);
725
726     ATA_OUTL(ctlr->r_res2, 0x0240 + (ch->unit << 7), ch->dma.work_bus);
727     return 0;
728 }
729
730 static void
731 ata_promise_mio_reset(device_t dev)
732 {
733     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
734     struct ata_channel *ch = device_get_softc(dev);
735     struct ata_promise_sx4 *hpktp;
736
737     switch (ctlr->chip->cfg2) {
738     case PR_SX4X:
739
740         /* softreset channel ATA module */
741         hpktp = device_get_ivars(ctlr->dev);
742         ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7), ch->unit + 1);
743         ata_udelay(1000);
744         ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7),
745                  (ATA_INL(ctlr->r_res2, 0xc0260 + (ch->unit << 7)) &
746                   ~0x00003f9f) | (ch->unit + 1));
747
748         /* softreset HOST module */ /* XXX SOS what about other outstandings */
749         mtx_lock(&hpktp->mtx);
750         ATA_OUTL(ctlr->r_res2, 0xc012c,
751                  (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f) | (1 << 11));
752         DELAY(10);
753         ATA_OUTL(ctlr->r_res2, 0xc012c,
754                  (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f));
755         hpktp->busy = 0;
756         mtx_unlock(&hpktp->mtx);
757         ata_generic_reset(dev);
758         break;
759
760     case PR_PATA:
761     case PR_CMBO:
762     case PR_SATA:
763         if ((ctlr->chip->cfg2 == PR_SATA) ||
764             ((ctlr->chip->cfg2 == PR_CMBO) && (ch->unit < 2))) {
765
766             /* mask plug/unplug intr */
767             ATA_OUTL(ctlr->r_res2, 0x06c, (0x00110000 << ch->unit));
768         }
769
770         /* softreset channels ATA module */
771         ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
772         ata_udelay(10000);
773         ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
774                  (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
775                   ~0x00003f9f) | (ch->unit + 1));
776
777         if ((ctlr->chip->cfg2 == PR_SATA) ||
778             ((ctlr->chip->cfg2 == PR_CMBO) && (ch->unit < 2))) {
779
780             if (ata_sata_phy_reset(dev))
781                 ata_generic_reset(dev);
782
783             /* reset and enable plug/unplug intr */
784             ATA_OUTL(ctlr->r_res2, 0x06c, (0x00000011 << ch->unit));
785         }
786         else
787             ata_generic_reset(dev);
788         break;
789
790     case PR_CMBO2:
791     case PR_SATA2:
792         if ((ctlr->chip->cfg2 == PR_SATA2) ||
793             ((ctlr->chip->cfg2 == PR_CMBO2) && (ch->unit < 2))) {
794             /* set portmultiplier port */
795             //ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
796
797             /* mask plug/unplug intr */
798             ATA_OUTL(ctlr->r_res2, 0x060, (0x00110000 << ch->unit));
799         }
800
801         /* softreset channels ATA module */
802         ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
803         ata_udelay(10000);
804         ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
805                  (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
806                   ~0x00003f9f) | (ch->unit + 1));
807
808         if ((ctlr->chip->cfg2 == PR_SATA2) ||
809             ((ctlr->chip->cfg2 == PR_CMBO2) && (ch->unit < 2))) {
810
811             /* set PHY mode to "improved" */
812             ATA_OUTL(ctlr->r_res2, 0x414 + (ch->unit << 8),
813                      (ATA_INL(ctlr->r_res2, 0x414 + (ch->unit << 8)) &
814                      ~0x00000003) | 0x00000001);
815
816             if (ata_sata_phy_reset(dev)) {
817                 u_int32_t signature = ch->hw.softreset(dev, ATA_PM);
818
819                 if (1 | bootverbose)
820                     device_printf(dev, "SIGNATURE: %08x\n", signature);
821
822                 switch (signature) {
823                 case 0x00000101:
824                     ch->devices = ATA_ATA_MASTER;
825                     break;
826                 case 0x96690101:
827                     ch->devices = ATA_PORTMULTIPLIER;
828                     ata_pm_identify(dev);
829                     break;
830                 case 0xeb140101:
831                     ch->devices = ATA_ATAPI_MASTER;
832                     break;
833                 default: /* SOS XXX */
834                     if (bootverbose)
835                         device_printf(dev,
836                                       "No signature, asuming disk device\n");
837                     ch->devices = ATA_ATA_MASTER;
838                 }
839                 if (bootverbose)
840                     device_printf(dev, "promise_mio_reset devices=%08x\n",
841                                   ch->devices);
842
843             }
844
845             /* reset and enable plug/unplug intr */
846             ATA_OUTL(ctlr->r_res2, 0x060, (0x00000011 << ch->unit));
847
848             ///* set portmultiplier port */
849             ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x00);
850         }
851         else
852             ata_generic_reset(dev);
853         break;
854
855     }
856 }
857
858 static int
859 ata_promise_mio_pm_read(device_t dev, int port, int reg, u_int32_t *result)
860 {
861     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
862     struct ata_channel *ch = device_get_softc(dev);
863     int timeout = 0;
864
865     /* set portmultiplier port */
866     ATA_OUTB(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
867
868     ATA_IDX_OUTB(ch, ATA_FEATURE, reg);
869     ATA_IDX_OUTB(ch, ATA_DRIVE, port);
870
871     ATA_IDX_OUTB(ch, ATA_COMMAND, ATA_READ_PM);
872
873     while (timeout < 1000000) {
874         u_int8_t status = ATA_IDX_INB(ch, ATA_STATUS);
875         if (!(status & ATA_S_BUSY))
876             break;
877         timeout += 1000;
878         DELAY(1000);
879     }
880     if (timeout >= 1000000)
881         return ATA_E_ABORT;
882
883     *result = ATA_IDX_INB(ch, ATA_COUNT) |
884               (ATA_IDX_INB(ch, ATA_SECTOR) << 8) |
885               (ATA_IDX_INB(ch, ATA_CYL_LSB) << 16) |
886               (ATA_IDX_INB(ch, ATA_CYL_MSB) << 24);
887     return 0;
888 }
889
890 static int
891 ata_promise_mio_pm_write(device_t dev, int port, int reg, u_int32_t value)
892 {
893     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
894     struct ata_channel *ch = device_get_softc(dev);
895     int timeout = 0;
896
897     /* set portmultiplier port */
898     ATA_OUTB(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
899
900     ATA_IDX_OUTB(ch, ATA_FEATURE, reg);
901     ATA_IDX_OUTB(ch, ATA_DRIVE, port);
902     ATA_IDX_OUTB(ch, ATA_COUNT, value & 0xff);
903     ATA_IDX_OUTB(ch, ATA_SECTOR, (value >> 8) & 0xff);
904     ATA_IDX_OUTB(ch, ATA_CYL_LSB, (value >> 16) & 0xff);
905     ATA_IDX_OUTB(ch, ATA_CYL_MSB, (value >> 24) & 0xff);
906
907     ATA_IDX_OUTB(ch, ATA_COMMAND, ATA_WRITE_PM);
908
909     while (timeout < 1000000) {
910         u_int8_t status = ATA_IDX_INB(ch, ATA_STATUS);
911         if (!(status & ATA_S_BUSY))
912             break;
913         timeout += 1000;
914         DELAY(1000);
915     }
916     if (timeout >= 1000000)
917         return ATA_E_ABORT;
918
919     return ATA_IDX_INB(ch, ATA_ERROR);
920 }
921
922 /* must be called with ATA channel locked and state_mtx held */
923 static u_int32_t
924 ata_promise_mio_softreset(device_t dev, int port)
925 {
926     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
927     struct ata_channel *ch = device_get_softc(dev);
928     int timeout;
929
930     /* set portmultiplier port */
931     ATA_OUTB(ctlr->r_res2, 0x4e8 + (ch->unit << 8), port & 0x0f);
932
933     /* softreset device on this channel */
934     ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | ATA_DEV(ATA_MASTER));
935     DELAY(10);
936     ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_IDS | ATA_A_RESET);
937     ata_udelay(10000); 
938     ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_IDS);
939     ata_udelay(150000);
940     ATA_IDX_INB(ch, ATA_ERROR);
941
942     /* wait for BUSY to go inactive */
943     for (timeout = 0; timeout < 100; timeout++) {
944         u_int8_t err, stat;
945
946         err = ATA_IDX_INB(ch, ATA_ERROR);
947         stat = ATA_IDX_INB(ch, ATA_STATUS);
948
949         //if (stat == err && timeout > (stat & ATA_S_BUSY ? 100 : 10))
950             //break;
951
952         if (!(stat & ATA_S_BUSY)) {
953             //if ((err & 0x7f) == ATA_E_ILI) {
954                 return ATA_IDX_INB(ch, ATA_COUNT) |
955                        (ATA_IDX_INB(ch, ATA_SECTOR) << 8) |
956                        (ATA_IDX_INB(ch, ATA_CYL_LSB) << 16) |
957                        (ATA_IDX_INB(ch, ATA_CYL_MSB) << 24);
958             //}
959             //else if (stat & 0x0f) {
960                 //stat |= ATA_S_BUSY;
961             //}
962         }
963
964         if (!(stat & ATA_S_BUSY) || (stat == 0xff && timeout > 10))
965             break;
966         ata_udelay(100000);
967     }
968     return -1;
969 }
970
971 static void
972 ata_promise_mio_dmainit(device_t dev)
973 {
974     struct ata_channel *ch = device_get_softc(dev);
975
976     ata_dmainit(dev);
977     /* note start and stop are not used here */
978     ch->dma.setprd = ata_promise_mio_setprd;
979 }
980
981
982 #define MAXLASTSGSIZE (32 * sizeof(u_int32_t))
983 static void 
984 ata_promise_mio_setprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
985 {
986     struct ata_dmasetprd_args *args = xsc;
987     struct ata_dma_prdentry *prd = args->dmatab;
988     int i;
989
990     if ((args->error = error))
991         return;
992
993     for (i = 0; i < nsegs; i++) {
994         prd[i].addr = htole32(segs[i].ds_addr);
995         prd[i].count = htole32(segs[i].ds_len);
996     }
997     if (segs[i - 1].ds_len > MAXLASTSGSIZE) {
998         //printf("split last SG element of %u\n", segs[i - 1].ds_len);
999         prd[i - 1].count = htole32(segs[i - 1].ds_len - MAXLASTSGSIZE);
1000         prd[i].count = htole32(MAXLASTSGSIZE);
1001         prd[i].addr = htole32(segs[i - 1].ds_addr +
1002                               (segs[i - 1].ds_len - MAXLASTSGSIZE));
1003         nsegs++;
1004         i++;
1005     }
1006     prd[i - 1].count |= htole32(ATA_DMA_EOT);
1007     KASSERT(nsegs <= ATA_DMA_ENTRIES, ("too many DMA segment entries\n"));
1008     args->nsegs = nsegs;
1009 }
1010
1011 static void
1012 ata_promise_mio_setmode(device_t dev, int mode)
1013 {
1014     device_t gparent = GRANDPARENT(dev);
1015     struct ata_pci_controller *ctlr = device_get_softc(gparent);
1016     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1017
1018     if ( (ctlr->chip->cfg2 == PR_SATA) ||
1019         ((ctlr->chip->cfg2 == PR_CMBO) && (ch->unit < 2)) ||
1020         (ctlr->chip->cfg2 == PR_SATA2) ||
1021         ((ctlr->chip->cfg2 == PR_CMBO2) && (ch->unit < 2)))
1022         ata_sata_setmode(dev, mode);
1023     else
1024         ata_promise_setmode(dev, mode);
1025 }
1026
1027 static void
1028 ata_promise_sx4_intr(void *data)
1029 {
1030     struct ata_pci_controller *ctlr = data;
1031     struct ata_channel *ch;
1032     u_int32_t vector = ATA_INL(ctlr->r_res2, 0x000c0480);
1033     int unit;
1034
1035     for (unit = 0; unit < ctlr->channels; unit++) {
1036         if (vector & (1 << (unit + 1)))
1037             if ((ch = ctlr->interrupt[unit].argument))
1038                 ctlr->interrupt[unit].function(ch);
1039         if (vector & (1 << (unit + 5)))
1040             if ((ch = ctlr->interrupt[unit].argument))
1041                 ata_promise_queue_hpkt(ctlr,
1042                                        htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
1043                                                ATA_PDC_HPKT_OFFSET));
1044         if (vector & (1 << (unit + 9))) {
1045             ata_promise_next_hpkt(ctlr);
1046             if ((ch = ctlr->interrupt[unit].argument))
1047                 ctlr->interrupt[unit].function(ch);
1048         }
1049         if (vector & (1 << (unit + 13))) {
1050             ata_promise_next_hpkt(ctlr);
1051             if ((ch = ctlr->interrupt[unit].argument))
1052                 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
1053                          htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
1054                          ATA_PDC_APKT_OFFSET));
1055         }
1056     }
1057 }
1058
1059 static int
1060 ata_promise_sx4_command(struct ata_request *request)
1061 {
1062     device_t gparent = GRANDPARENT(request->dev);
1063     struct ata_pci_controller *ctlr = device_get_softc(gparent);
1064     struct ata_channel *ch = device_get_softc(request->parent);
1065     struct ata_dma_prdentry *prd = request->dma->sg;
1066     caddr_t window = rman_get_virtual(ctlr->r_res1);
1067     u_int32_t *wordp;
1068     int i, idx, length = 0;
1069
1070     /* XXX SOS add ATAPI commands support later */
1071     switch (request->u.ata.command) {    
1072
1073     default:
1074         return -1;
1075
1076     case ATA_ATA_IDENTIFY:
1077     case ATA_READ:
1078     case ATA_READ48:
1079     case ATA_READ_MUL:
1080     case ATA_READ_MUL48:
1081     case ATA_WRITE:
1082     case ATA_WRITE48:
1083     case ATA_WRITE_MUL:
1084     case ATA_WRITE_MUL48:
1085         ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
1086         return ata_generic_command(request);
1087
1088     case ATA_SETFEATURES:
1089     case ATA_FLUSHCACHE:
1090     case ATA_FLUSHCACHE48:
1091     case ATA_SLEEP:
1092     case ATA_SET_MULTI:
1093         wordp = (u_int32_t *)
1094             (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
1095         wordp[0] = htole32(0x08 | ((ch->unit + 1)<<16) | (0x00 << 24));
1096         wordp[1] = 0;
1097         wordp[2] = 0;
1098         ata_promise_apkt((u_int8_t *)wordp, request);
1099         ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
1100         ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
1101         ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
1102                  htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_APKT_OFFSET));
1103         return 0;
1104
1105     case ATA_READ_DMA:
1106     case ATA_READ_DMA48:
1107     case ATA_WRITE_DMA:
1108     case ATA_WRITE_DMA48:
1109         wordp = (u_int32_t *)
1110             (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HSG_OFFSET);
1111         i = idx = 0;
1112         do {
1113             wordp[idx++] = prd[i].addr;
1114             wordp[idx++] = prd[i].count;
1115             length += (prd[i].count & ~ATA_DMA_EOT);
1116         } while (!(prd[i++].count & ATA_DMA_EOT));
1117
1118         wordp = (u_int32_t *)
1119             (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_LSG_OFFSET);
1120         wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
1121         wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
1122
1123         wordp = (u_int32_t *)
1124             (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_ASG_OFFSET);
1125         wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
1126         wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
1127
1128         wordp = (u_int32_t *)
1129             (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET);
1130         if (request->flags & ATA_R_READ)
1131             wordp[0] = htole32(0x14 | ((ch->unit+9)<<16) | ((ch->unit+5)<<24));
1132         if (request->flags & ATA_R_WRITE)
1133             wordp[0] = htole32(0x00 | ((ch->unit+13)<<16) | (0x00<<24));
1134         wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_HSG_OFFSET);
1135         wordp[2] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_LSG_OFFSET);
1136         wordp[3] = 0;
1137
1138         wordp = (u_int32_t *)
1139             (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
1140         if (request->flags & ATA_R_READ)
1141             wordp[0] = htole32(0x04 | ((ch->unit+5)<<16) | (0x00<<24));
1142         if (request->flags & ATA_R_WRITE)
1143             wordp[0] = htole32(0x10 | ((ch->unit+1)<<16) | ((ch->unit+13)<<24));
1144         wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_ASG_OFFSET);
1145         wordp[2] = 0;
1146         ata_promise_apkt((u_int8_t *)wordp, request);
1147         ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
1148
1149         if (request->flags & ATA_R_READ) {
1150             ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+5)<<2), 0x00000001);
1151             ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+9)<<2), 0x00000001);
1152             ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
1153                 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET));
1154         }
1155         if (request->flags & ATA_R_WRITE) {
1156             ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+1)<<2), 0x00000001);
1157             ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+13)<<2), 0x00000001);
1158             ata_promise_queue_hpkt(ctlr,
1159                 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET));
1160         }
1161         return 0;
1162     }
1163 }
1164
1165 static int
1166 ata_promise_apkt(u_int8_t *bytep, struct ata_request *request)
1167
1168     struct ata_device *atadev = device_get_softc(request->dev);
1169     int i = 12;
1170
1171     bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_PDC_WAIT_NBUSY|ATA_DRIVE;
1172     bytep[i++] = ATA_D_IBM | ATA_D_LBA | ATA_DEV(atadev->unit);
1173     bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_CTL;
1174     bytep[i++] = ATA_A_4BIT;
1175
1176     if (atadev->flags & ATA_D_48BIT_ACTIVE) {
1177         bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_FEATURE;
1178         bytep[i++] = request->u.ata.feature >> 8;
1179         bytep[i++] = request->u.ata.feature;
1180         bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_COUNT;
1181         bytep[i++] = request->u.ata.count >> 8;
1182         bytep[i++] = request->u.ata.count;
1183         bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_SECTOR;
1184         bytep[i++] = request->u.ata.lba >> 24;
1185         bytep[i++] = request->u.ata.lba;
1186         bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
1187         bytep[i++] = request->u.ata.lba >> 32;
1188         bytep[i++] = request->u.ata.lba >> 8;
1189         bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
1190         bytep[i++] = request->u.ata.lba >> 40;
1191         bytep[i++] = request->u.ata.lba >> 16;
1192         bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
1193         bytep[i++] = ATA_D_LBA | ATA_DEV(atadev->unit);
1194     }
1195     else {
1196         bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_FEATURE;
1197         bytep[i++] = request->u.ata.feature;
1198         bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_COUNT;
1199         bytep[i++] = request->u.ata.count;
1200         bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_SECTOR;
1201         bytep[i++] = request->u.ata.lba;
1202         bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
1203         bytep[i++] = request->u.ata.lba >> 8;
1204         bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
1205         bytep[i++] = request->u.ata.lba >> 16;
1206         bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
1207         bytep[i++] = (atadev->flags & ATA_D_USE_CHS ? 0 : ATA_D_LBA) |
1208                      ATA_D_IBM | ATA_DEV(atadev->unit) |
1209                      ((request->u.ata.lba >> 24)&0xf);
1210     }
1211     bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_END | ATA_COMMAND;
1212     bytep[i++] = request->u.ata.command;
1213     return i;
1214 }
1215
1216 static void
1217 ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt)
1218 {
1219     struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
1220
1221     mtx_lock(&hpktp->mtx);
1222     if (hpktp->busy) {
1223         struct host_packet *hp = 
1224             malloc(sizeof(struct host_packet), M_TEMP, M_NOWAIT | M_ZERO);
1225         hp->addr = hpkt;
1226         TAILQ_INSERT_TAIL(&hpktp->queue, hp, chain);
1227     }
1228     else {
1229         hpktp->busy = 1;
1230         ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
1231     }
1232     mtx_unlock(&hpktp->mtx);
1233 }
1234
1235 static void
1236 ata_promise_next_hpkt(struct ata_pci_controller *ctlr)
1237 {
1238     struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
1239     struct host_packet *hp;
1240
1241     mtx_lock(&hpktp->mtx);
1242     if ((hp = TAILQ_FIRST(&hpktp->queue))) {
1243         TAILQ_REMOVE(&hpktp->queue, hp, chain);
1244         ATA_OUTL(ctlr->r_res2, 0x000c0100, hp->addr);
1245         free(hp, M_TEMP);
1246     }
1247     else
1248         hpktp->busy = 0;
1249     mtx_unlock(&hpktp->mtx);
1250 }
1251
1252 ATA_DECLARE_DRIVER(ata_promise);